1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "MockDeviceSessionHwl"
18 #include "mock_device_session_hwl.h"
19 
20 #include <hardware/gralloc.h>
21 #include <log/log.h>
22 #include <system/graphics-base.h>
23 
24 namespace android {
25 namespace google_camera_hal {
26 
27 using ::testing::_;
28 using ::testing::Invoke;
29 
FakeCameraDeviceSessionHwl(uint32_t camera_id,const std::vector<uint32_t> & physical_camera_ids)30 FakeCameraDeviceSessionHwl::FakeCameraDeviceSessionHwl(
31     uint32_t camera_id, const std::vector<uint32_t>& physical_camera_ids)
32     : kCameraId(camera_id), kPhysicalCameraIds(physical_camera_ids) {
33 }
34 
ConstructDefaultRequestSettings(RequestTemplate,std::unique_ptr<HalCameraMetadata> * default_settings)35 status_t FakeCameraDeviceSessionHwl::ConstructDefaultRequestSettings(
36     RequestTemplate /*type*/,
37     std::unique_ptr<HalCameraMetadata>* default_settings) {
38   if (default_settings == nullptr) {
39     return BAD_VALUE;
40   }
41 
42   static constexpr uint32_t kDataBytes = 256;
43   static constexpr uint32_t kNumEntries = 10;
44   static constexpr int32_t kSensitivity = 200;
45 
46   *default_settings = HalCameraMetadata::Create(kNumEntries, kDataBytes);
47   if (default_settings == nullptr) {
48     ALOGE("%s: Cannot create a HalCameraMetadata", __FUNCTION__);
49     return UNKNOWN_ERROR;
50   }
51 
52   return (*default_settings)->Set(ANDROID_SENSOR_SENSITIVITY, &kSensitivity, 1);
53 }
54 
PrepareConfigureStreams(const StreamConfiguration &)55 status_t FakeCameraDeviceSessionHwl::PrepareConfigureStreams(
56     const StreamConfiguration& /*overall_config*/) {
57   return OK;
58 }
59 
ConfigurePipeline(uint32_t camera_id,HwlPipelineCallback hwl_pipeline_callback,const StreamConfiguration & request_config,const StreamConfiguration &,uint32_t * pipeline_id)60 status_t FakeCameraDeviceSessionHwl::ConfigurePipeline(
61     uint32_t camera_id, HwlPipelineCallback hwl_pipeline_callback,
62     const StreamConfiguration& request_config,
63     const StreamConfiguration& /*overall_config*/, uint32_t* pipeline_id) {
64   if (pipeline_id == nullptr) {
65     return BAD_VALUE;
66   }
67 
68   // Check if the camera ID belongs to this camera.
69   if (camera_id != kCameraId &&
70       std::find(kPhysicalCameraIds.begin(), kPhysicalCameraIds.end(),
71                 camera_id) == kPhysicalCameraIds.end()) {
72     ALOGE("%s: Unknown camera ID: %u", __FUNCTION__, camera_id);
73     return BAD_VALUE;
74   }
75 
76   static constexpr uint32_t kDefaultMaxBuffers = 3;
77   std::vector<HalStream> hal_configured_streams;
78 
79   for (auto& stream : request_config.streams) {
80     HalStream hal_stream = {};
81     hal_stream.id = stream.id;
82     hal_stream.override_format = stream.format;
83     hal_stream.producer_usage = stream.usage;
84     hal_stream.consumer_usage = GRALLOC_USAGE_HW_CAMERA_WRITE;
85     hal_stream.max_buffers = kDefaultMaxBuffers;
86     hal_stream.override_data_space = stream.data_space;
87     hal_stream.is_physical_camera_stream = stream.is_physical_camera_stream;
88     hal_stream.physical_camera_id = stream.physical_camera_id;
89 
90     if (hal_stream.override_format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
91       hal_stream.override_format = HAL_PIXEL_FORMAT_YCRCB_420_SP;
92     }
93 
94     hal_configured_streams.push_back(hal_stream);
95   }
96 
97   {
98     std::lock_guard<std::mutex> lock(hwl_pipeline_lock_);
99 
100     // Remember pipeline callback.
101     static uint32_t next_available_pipeline_id = 0;
102     hwl_pipeline_callbacks_.emplace(next_available_pipeline_id,
103                                     hwl_pipeline_callback);
104     *pipeline_id = next_available_pipeline_id++;
105 
106     // Rememember configured HAL streams.
107     pipeline_hal_streams_map_.emplace(*pipeline_id, hal_configured_streams);
108   }
109 
110   return OK;
111 }
112 
BuildPipelines()113 status_t FakeCameraDeviceSessionHwl::BuildPipelines() {
114   std::lock_guard<std::mutex> lock(hwl_pipeline_lock_);
115   if (pipeline_hal_streams_map_.empty()) {
116     return NO_INIT;
117   }
118 
119   return OK;
120 }
121 
GetRequiredIntputStreams(const StreamConfiguration &,HwlOfflinePipelineRole pipeline_role,std::vector<Stream> * streams)122 status_t FakeCameraDeviceSessionHwl::GetRequiredIntputStreams(
123     const StreamConfiguration& /*overall_config*/,
124     HwlOfflinePipelineRole pipeline_role, std::vector<Stream>* streams) {
125   // Only supports offline ST
126   if (pipeline_role != HwlOfflinePipelineRole::kOfflineSmoothTransitionRole) {
127     return BAD_VALUE;
128   }
129   if (streams == nullptr) {
130     return BAD_VALUE;
131   }
132 
133   for (int i = 0; i < 6; ++i) {
134     Stream internal_stream;
135 
136     internal_stream.id = i;
137     streams->push_back(internal_stream);
138   }
139 
140   return OK;
141 }
142 
GetConfiguredHalStream(uint32_t pipeline_id,std::vector<HalStream> * hal_streams) const143 status_t FakeCameraDeviceSessionHwl::GetConfiguredHalStream(
144     uint32_t pipeline_id, std::vector<HalStream>* hal_streams) const {
145   std::lock_guard<std::mutex> lock(hwl_pipeline_lock_);
146   if (hal_streams == nullptr) {
147     return BAD_VALUE;
148   }
149 
150   if (pipeline_hal_streams_map_.empty()) {
151     return NO_INIT;
152   }
153 
154   if (pipeline_hal_streams_map_.find(pipeline_id) ==
155       pipeline_hal_streams_map_.end()) {
156     return NAME_NOT_FOUND;
157   }
158 
159   *hal_streams = pipeline_hal_streams_map_.at(pipeline_id);
160   return OK;
161 }
162 
DestroyPipelines()163 void FakeCameraDeviceSessionHwl::DestroyPipelines() {
164   std::lock_guard<std::mutex> lock(hwl_pipeline_lock_);
165   hwl_pipeline_callbacks_.clear();
166   pipeline_hal_streams_map_.clear();
167 }
168 
SubmitRequests(uint32_t frame_number,std::vector<HwlPipelineRequest> & requests)169 status_t FakeCameraDeviceSessionHwl::SubmitRequests(
170     uint32_t frame_number, std::vector<HwlPipelineRequest>& requests) {
171   std::lock_guard<std::mutex> lock(hwl_pipeline_lock_);
172 
173   for (auto& request : requests) {
174     auto callback = hwl_pipeline_callbacks_.find(request.pipeline_id);
175     if (callback == hwl_pipeline_callbacks_.end()) {
176       ALOGE("%s: Could not find callback for pipeline %u", __FUNCTION__,
177             request.pipeline_id);
178       return BAD_VALUE;
179     }
180 
181     // Notify shutter.
182     NotifyMessage shutter_message = {.type = MessageType::kShutter,
183                                      .message.shutter = {
184                                          .frame_number = frame_number,
185                                          .timestamp_ns = 0,
186                                      }};
187     callback->second.notify(request.pipeline_id, shutter_message);
188 
189     // Send out result.
190     auto result = std::make_unique<HwlPipelineResult>();
191     result->camera_id = kCameraId;
192     result->pipeline_id = request.pipeline_id;
193     result->frame_number = frame_number;
194     result->result_metadata = HalCameraMetadata::Clone(request.settings.get());
195     result->input_buffers = request.input_buffers;
196     result->output_buffers = request.output_buffers;
197     result->partial_result = 1;
198     callback->second.process_pipeline_result(std::move(result));
199   }
200 
201   return OK;
202 }
203 
Flush()204 status_t FakeCameraDeviceSessionHwl::Flush() {
205   return OK;
206 }
207 
GetCameraId() const208 uint32_t FakeCameraDeviceSessionHwl::GetCameraId() const {
209   return kCameraId;
210 }
211 
GetPhysicalCameraIds() const212 std::vector<uint32_t> FakeCameraDeviceSessionHwl::GetPhysicalCameraIds() const {
213   return kPhysicalCameraIds;
214 }
215 
GetCameraCharacteristics(std::unique_ptr<HalCameraMetadata> * characteristics) const216 status_t FakeCameraDeviceSessionHwl::GetCameraCharacteristics(
217     std::unique_ptr<HalCameraMetadata>* characteristics) const {
218   if (characteristics == nullptr) {
219     return BAD_VALUE;
220   }
221 
222   (*characteristics) = HalCameraMetadata::Create(/*num_entries=*/0,
223                                                  /*data_bytes=*/0);
224 
225   if (*characteristics == nullptr) {
226     return NO_MEMORY;
227   }
228 
229   return OK;
230 }
231 
GetPhysicalCameraCharacteristics(uint32_t,std::unique_ptr<HalCameraMetadata> * characteristics) const232 status_t FakeCameraDeviceSessionHwl::GetPhysicalCameraCharacteristics(
233     uint32_t /*physical_camera_id*/,
234     std::unique_ptr<HalCameraMetadata>* characteristics) const {
235   if (characteristics == nullptr) {
236     return BAD_VALUE;
237   }
238 
239   (*characteristics) = HalCameraMetadata::Create(/*num_entries=*/0,
240                                                  /*data_bytes=*/0);
241 
242   if (*characteristics == nullptr) {
243     return NO_MEMORY;
244   }
245 
246   return OK;
247 }
248 
SetSessionData(SessionDataKey,void *)249 status_t FakeCameraDeviceSessionHwl::SetSessionData(SessionDataKey /*key*/,
250                                                     void* /*value*/) {
251   return OK;
252 }
253 
GetSessionData(SessionDataKey,void **) const254 status_t FakeCameraDeviceSessionHwl::GetSessionData(SessionDataKey /*key*/,
255                                                     void** /*value*/) const {
256   return OK;
257 }
258 
SetSessionCallback(const HwlSessionCallback &)259 void FakeCameraDeviceSessionHwl::SetSessionCallback(
260     const HwlSessionCallback& /*hwl_session_callback*/) {
261   return;
262 }
263 
FilterResultMetadata(HalCameraMetadata *) const264 status_t FakeCameraDeviceSessionHwl::FilterResultMetadata(
265     HalCameraMetadata* /*metadata*/) const {
266   return OK;
267 }
268 
IsReconfigurationRequired(const HalCameraMetadata *,const HalCameraMetadata *,bool * reconfiguration_required) const269 status_t FakeCameraDeviceSessionHwl::IsReconfigurationRequired(
270     const HalCameraMetadata* /*old_session*/,
271     const HalCameraMetadata* /*new_session*/,
272     bool* reconfiguration_required) const {
273   if (reconfiguration_required == nullptr) {
274     return BAD_VALUE;
275   }
276   *reconfiguration_required = true;
277   return OK;
278 }
279 
280 std::unique_ptr<ZoomRatioMapperHwl>
GetZoomRatioMapperHwl()281 FakeCameraDeviceSessionHwl::GetZoomRatioMapperHwl() {
282   return nullptr;
283 }
284 
285 std::unique_ptr<google::camera_common::Profiler>
GetProfiler(uint32_t,int)286 FakeCameraDeviceSessionHwl::GetProfiler(uint32_t /* camera_id */,
287                                         int /* option */) {
288   return nullptr;
289 }
290 
291 std::unique_ptr<IMulticamCoordinatorHwl>
CreateMulticamCoordinatorHwl()292 FakeCameraDeviceSessionHwl::CreateMulticamCoordinatorHwl() {
293   // Multicam coordinator not supported in this mock
294   return nullptr;
295 }
296 
MockDeviceSessionHwl(uint32_t camera_id,const std::vector<uint32_t> & physical_camera_ids)297 MockDeviceSessionHwl::MockDeviceSessionHwl(
298     uint32_t camera_id, const std::vector<uint32_t>& physical_camera_ids)
299     : fake_session_hwl_(camera_id, physical_camera_ids) {
300 }
301 
DelegateCallsToFakeSession()302 void MockDeviceSessionHwl::DelegateCallsToFakeSession() {
303   ON_CALL(*this, ConstructDefaultRequestSettings(_, _))
304       .WillByDefault(
305           Invoke(&fake_session_hwl_,
306                  &FakeCameraDeviceSessionHwl::ConstructDefaultRequestSettings));
307 
308   ON_CALL(*this, ConfigurePipeline(_, _, _, _, _))
309       .WillByDefault(Invoke(&fake_session_hwl_,
310                             &FakeCameraDeviceSessionHwl::ConfigurePipeline));
311 
312   ON_CALL(*this, BuildPipelines())
313       .WillByDefault(Invoke(&fake_session_hwl_,
314                             &FakeCameraDeviceSessionHwl::BuildPipelines));
315 
316   ON_CALL(*this, PreparePipeline(_, _))
317       .WillByDefault(Invoke(&fake_session_hwl_,
318                             &FakeCameraDeviceSessionHwl::PreparePipeline));
319 
320   ON_CALL(*this, GetRequiredIntputStreams(_, _, _))
321       .WillByDefault(
322           Invoke(&fake_session_hwl_,
323                  &FakeCameraDeviceSessionHwl::GetRequiredIntputStreams));
324 
325   ON_CALL(*this, GetConfiguredHalStream(_, _))
326       .WillByDefault(
327           Invoke(&fake_session_hwl_,
328                  &FakeCameraDeviceSessionHwl::GetConfiguredHalStream));
329 
330   ON_CALL(*this, DestroyPipelines())
331       .WillByDefault(Invoke(&fake_session_hwl_,
332                             &FakeCameraDeviceSessionHwl::DestroyPipelines));
333 
334   ON_CALL(*this, SubmitRequests(_, _))
335       .WillByDefault(Invoke(&fake_session_hwl_,
336                             &FakeCameraDeviceSessionHwl::SubmitRequests));
337 
338   ON_CALL(*this, Flush())
339       .WillByDefault(
340           Invoke(&fake_session_hwl_, &FakeCameraDeviceSessionHwl::Flush));
341 
342   ON_CALL(*this, GetCameraId())
343       .WillByDefault(
344           Invoke(&fake_session_hwl_, &FakeCameraDeviceSessionHwl::GetCameraId));
345 
346   ON_CALL(*this, GetPhysicalCameraIds())
347       .WillByDefault(Invoke(&fake_session_hwl_,
348                             &FakeCameraDeviceSessionHwl::GetPhysicalCameraIds));
349 
350   ON_CALL(*this, GetCameraCharacteristics(_))
351       .WillByDefault(
352           Invoke(&fake_session_hwl_,
353                  &FakeCameraDeviceSessionHwl::GetCameraCharacteristics));
354 
355   ON_CALL(*this, GetPhysicalCameraCharacteristics(_, _))
356       .WillByDefault(Invoke(
357           &fake_session_hwl_,
358           &FakeCameraDeviceSessionHwl::GetPhysicalCameraCharacteristics));
359 }
360 
361 }  // namespace google_camera_hal
362 }  // namespace android
363