• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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                                          .readout_timestamp_ns = 0,
187                                      }};
188     callback->second.notify(request.pipeline_id, shutter_message);
189 
190     // Send out result.
191     auto result = std::make_unique<HwlPipelineResult>();
192     result->camera_id = kCameraId;
193     result->pipeline_id = request.pipeline_id;
194     result->frame_number = frame_number;
195     result->result_metadata = HalCameraMetadata::Clone(request.settings.get());
196     result->input_buffers = request.input_buffers;
197     result->output_buffers = request.output_buffers;
198     result->partial_result = 1;
199     callback->second.process_pipeline_result(std::move(result));
200   }
201 
202   return OK;
203 }
204 
Flush()205 status_t FakeCameraDeviceSessionHwl::Flush() {
206   return OK;
207 }
208 
GetCameraId() const209 uint32_t FakeCameraDeviceSessionHwl::GetCameraId() const {
210   return kCameraId;
211 }
212 
GetPhysicalCameraIds() const213 std::vector<uint32_t> FakeCameraDeviceSessionHwl::GetPhysicalCameraIds() const {
214   return kPhysicalCameraIds;
215 }
216 
GetCameraCharacteristics(std::unique_ptr<HalCameraMetadata> * characteristics) const217 status_t FakeCameraDeviceSessionHwl::GetCameraCharacteristics(
218     std::unique_ptr<HalCameraMetadata>* characteristics) const {
219   if (characteristics == nullptr) {
220     return BAD_VALUE;
221   }
222 
223   (*characteristics) = HalCameraMetadata::Create(/*num_entries=*/0,
224                                                  /*data_bytes=*/0);
225 
226   if (*characteristics == nullptr) {
227     return NO_MEMORY;
228   }
229 
230   return OK;
231 }
232 
GetPhysicalCameraCharacteristics(uint32_t,std::unique_ptr<HalCameraMetadata> * characteristics) const233 status_t FakeCameraDeviceSessionHwl::GetPhysicalCameraCharacteristics(
234     uint32_t /*physical_camera_id*/,
235     std::unique_ptr<HalCameraMetadata>* characteristics) const {
236   if (characteristics == nullptr) {
237     return BAD_VALUE;
238   }
239 
240   (*characteristics) = HalCameraMetadata::Create(/*num_entries=*/0,
241                                                  /*data_bytes=*/0);
242 
243   if (*characteristics == nullptr) {
244     return NO_MEMORY;
245   }
246 
247   return OK;
248 }
249 
SetSessionData(SessionDataKey,void *)250 status_t FakeCameraDeviceSessionHwl::SetSessionData(SessionDataKey /*key*/,
251                                                     void* /*value*/) {
252   return OK;
253 }
254 
GetSessionData(SessionDataKey,void **) const255 status_t FakeCameraDeviceSessionHwl::GetSessionData(SessionDataKey /*key*/,
256                                                     void** /*value*/) const {
257   return OK;
258 }
259 
SetSessionCallback(const HwlSessionCallback &)260 void FakeCameraDeviceSessionHwl::SetSessionCallback(
261     const HwlSessionCallback& /*hwl_session_callback*/) {
262   return;
263 }
264 
FilterResultMetadata(HalCameraMetadata *) const265 status_t FakeCameraDeviceSessionHwl::FilterResultMetadata(
266     HalCameraMetadata* /*metadata*/) const {
267   return OK;
268 }
269 
IsReconfigurationRequired(const HalCameraMetadata *,const HalCameraMetadata *,bool * reconfiguration_required) const270 status_t FakeCameraDeviceSessionHwl::IsReconfigurationRequired(
271     const HalCameraMetadata* /*old_session*/,
272     const HalCameraMetadata* /*new_session*/,
273     bool* reconfiguration_required) const {
274   if (reconfiguration_required == nullptr) {
275     return BAD_VALUE;
276   }
277   *reconfiguration_required = true;
278   return OK;
279 }
280 
281 std::unique_ptr<ZoomRatioMapperHwl>
GetZoomRatioMapperHwl()282 FakeCameraDeviceSessionHwl::GetZoomRatioMapperHwl() {
283   return nullptr;
284 }
285 
286 std::unique_ptr<google::camera_common::Profiler>
GetProfiler(uint32_t,int)287 FakeCameraDeviceSessionHwl::GetProfiler(uint32_t /* camera_id */,
288                                         int /* option */) {
289   return nullptr;
290 }
291 
292 std::unique_ptr<IMulticamCoordinatorHwl>
CreateMulticamCoordinatorHwl()293 FakeCameraDeviceSessionHwl::CreateMulticamCoordinatorHwl() {
294   // Multicam coordinator not supported in this mock
295   return nullptr;
296 }
297 
MockDeviceSessionHwl(uint32_t camera_id,const std::vector<uint32_t> & physical_camera_ids)298 MockDeviceSessionHwl::MockDeviceSessionHwl(
299     uint32_t camera_id, const std::vector<uint32_t>& physical_camera_ids)
300     : fake_session_hwl_(camera_id, physical_camera_ids) {
301 }
302 
DelegateCallsToFakeSession()303 void MockDeviceSessionHwl::DelegateCallsToFakeSession() {
304   ON_CALL(*this, ConstructDefaultRequestSettings(_, _))
305       .WillByDefault(
306           Invoke(&fake_session_hwl_,
307                  &FakeCameraDeviceSessionHwl::ConstructDefaultRequestSettings));
308 
309   ON_CALL(*this, ConfigurePipeline(_, _, _, _, _))
310       .WillByDefault(Invoke(&fake_session_hwl_,
311                             &FakeCameraDeviceSessionHwl::ConfigurePipeline));
312 
313   ON_CALL(*this, BuildPipelines())
314       .WillByDefault(Invoke(&fake_session_hwl_,
315                             &FakeCameraDeviceSessionHwl::BuildPipelines));
316 
317   ON_CALL(*this, PreparePipeline(_, _))
318       .WillByDefault(Invoke(&fake_session_hwl_,
319                             &FakeCameraDeviceSessionHwl::PreparePipeline));
320 
321   ON_CALL(*this, GetRequiredIntputStreams(_, _, _))
322       .WillByDefault(
323           Invoke(&fake_session_hwl_,
324                  &FakeCameraDeviceSessionHwl::GetRequiredIntputStreams));
325 
326   ON_CALL(*this, GetConfiguredHalStream(_, _))
327       .WillByDefault(
328           Invoke(&fake_session_hwl_,
329                  &FakeCameraDeviceSessionHwl::GetConfiguredHalStream));
330 
331   ON_CALL(*this, DestroyPipelines())
332       .WillByDefault(Invoke(&fake_session_hwl_,
333                             &FakeCameraDeviceSessionHwl::DestroyPipelines));
334 
335   ON_CALL(*this, SubmitRequests(_, _))
336       .WillByDefault(Invoke(&fake_session_hwl_,
337                             &FakeCameraDeviceSessionHwl::SubmitRequests));
338 
339   ON_CALL(*this, Flush())
340       .WillByDefault(
341           Invoke(&fake_session_hwl_, &FakeCameraDeviceSessionHwl::Flush));
342 
343   ON_CALL(*this, GetCameraId())
344       .WillByDefault(
345           Invoke(&fake_session_hwl_, &FakeCameraDeviceSessionHwl::GetCameraId));
346 
347   ON_CALL(*this, GetPhysicalCameraIds())
348       .WillByDefault(Invoke(&fake_session_hwl_,
349                             &FakeCameraDeviceSessionHwl::GetPhysicalCameraIds));
350 
351   ON_CALL(*this, GetCameraCharacteristics(_))
352       .WillByDefault(
353           Invoke(&fake_session_hwl_,
354                  &FakeCameraDeviceSessionHwl::GetCameraCharacteristics));
355 
356   ON_CALL(*this, GetPhysicalCameraCharacteristics(_, _))
357       .WillByDefault(Invoke(
358           &fake_session_hwl_,
359           &FakeCameraDeviceSessionHwl::GetPhysicalCameraCharacteristics));
360 }
361 
362 }  // namespace google_camera_hal
363 }  // namespace android
364