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
RepeatingRequestEnd(int32_t,const std::vector<int32_t> &)209 void FakeCameraDeviceSessionHwl::RepeatingRequestEnd(
210 int32_t /*frame_number*/, const std::vector<int32_t>& /*stream_ids*/) {
211 }
212
GetCameraId() const213 uint32_t FakeCameraDeviceSessionHwl::GetCameraId() const {
214 return kCameraId;
215 }
216
GetPhysicalCameraIds() const217 std::vector<uint32_t> FakeCameraDeviceSessionHwl::GetPhysicalCameraIds() const {
218 return kPhysicalCameraIds;
219 }
220
GetCameraCharacteristics(std::unique_ptr<HalCameraMetadata> * characteristics) const221 status_t FakeCameraDeviceSessionHwl::GetCameraCharacteristics(
222 std::unique_ptr<HalCameraMetadata>* characteristics) const {
223 if (characteristics == nullptr) {
224 return BAD_VALUE;
225 }
226
227 (*characteristics) = HalCameraMetadata::Create(/*num_entries=*/0,
228 /*data_bytes=*/0);
229
230 if (*characteristics == nullptr) {
231 return NO_MEMORY;
232 }
233
234 return OK;
235 }
236
GetPhysicalCameraCharacteristics(uint32_t,std::unique_ptr<HalCameraMetadata> * characteristics) const237 status_t FakeCameraDeviceSessionHwl::GetPhysicalCameraCharacteristics(
238 uint32_t /*physical_camera_id*/,
239 std::unique_ptr<HalCameraMetadata>* characteristics) const {
240 if (characteristics == nullptr) {
241 return BAD_VALUE;
242 }
243
244 (*characteristics) = HalCameraMetadata::Create(/*num_entries=*/0,
245 /*data_bytes=*/0);
246
247 if (*characteristics == nullptr) {
248 return NO_MEMORY;
249 }
250
251 return OK;
252 }
253
SetSessionData(SessionDataKey,void *)254 status_t FakeCameraDeviceSessionHwl::SetSessionData(SessionDataKey /*key*/,
255 void* /*value*/) {
256 return OK;
257 }
258
GetSessionData(SessionDataKey,void **) const259 status_t FakeCameraDeviceSessionHwl::GetSessionData(SessionDataKey /*key*/,
260 void** /*value*/) const {
261 return OK;
262 }
263
SetSessionCallback(const HwlSessionCallback &)264 void FakeCameraDeviceSessionHwl::SetSessionCallback(
265 const HwlSessionCallback& /*hwl_session_callback*/) {
266 return;
267 }
268
FilterResultMetadata(HalCameraMetadata *) const269 status_t FakeCameraDeviceSessionHwl::FilterResultMetadata(
270 HalCameraMetadata* /*metadata*/) const {
271 return OK;
272 }
273
IsReconfigurationRequired(const HalCameraMetadata *,const HalCameraMetadata *,bool * reconfiguration_required) const274 status_t FakeCameraDeviceSessionHwl::IsReconfigurationRequired(
275 const HalCameraMetadata* /*old_session*/,
276 const HalCameraMetadata* /*new_session*/,
277 bool* reconfiguration_required) const {
278 if (reconfiguration_required == nullptr) {
279 return BAD_VALUE;
280 }
281 *reconfiguration_required = true;
282 return OK;
283 }
284
285 std::unique_ptr<ZoomRatioMapperHwl>
GetZoomRatioMapperHwl()286 FakeCameraDeviceSessionHwl::GetZoomRatioMapperHwl() {
287 return nullptr;
288 }
289
290 std::unique_ptr<google::camera_common::Profiler>
GetProfiler(uint32_t,int)291 FakeCameraDeviceSessionHwl::GetProfiler(uint32_t /* camera_id */,
292 int /* option */) {
293 return nullptr;
294 }
295
296 std::unique_ptr<IMulticamCoordinatorHwl>
CreateMulticamCoordinatorHwl()297 FakeCameraDeviceSessionHwl::CreateMulticamCoordinatorHwl() {
298 // Multicam coordinator not supported in this mock
299 return nullptr;
300 }
301
MockDeviceSessionHwl(uint32_t camera_id,const std::vector<uint32_t> & physical_camera_ids)302 MockDeviceSessionHwl::MockDeviceSessionHwl(
303 uint32_t camera_id, const std::vector<uint32_t>& physical_camera_ids)
304 : fake_session_hwl_(camera_id, physical_camera_ids) {
305 }
306
DelegateCallsToFakeSession()307 void MockDeviceSessionHwl::DelegateCallsToFakeSession() {
308 ON_CALL(*this, ConstructDefaultRequestSettings(_, _))
309 .WillByDefault(
310 Invoke(&fake_session_hwl_,
311 &FakeCameraDeviceSessionHwl::ConstructDefaultRequestSettings));
312
313 ON_CALL(*this, ConfigurePipeline(_, _, _, _, _))
314 .WillByDefault(Invoke(&fake_session_hwl_,
315 &FakeCameraDeviceSessionHwl::ConfigurePipeline));
316
317 ON_CALL(*this, BuildPipelines())
318 .WillByDefault(Invoke(&fake_session_hwl_,
319 &FakeCameraDeviceSessionHwl::BuildPipelines));
320
321 ON_CALL(*this, PreparePipeline(_, _))
322 .WillByDefault(Invoke(&fake_session_hwl_,
323 &FakeCameraDeviceSessionHwl::PreparePipeline));
324
325 ON_CALL(*this, GetRequiredIntputStreams(_, _, _))
326 .WillByDefault(
327 Invoke(&fake_session_hwl_,
328 &FakeCameraDeviceSessionHwl::GetRequiredIntputStreams));
329
330 ON_CALL(*this, GetConfiguredHalStream(_, _))
331 .WillByDefault(
332 Invoke(&fake_session_hwl_,
333 &FakeCameraDeviceSessionHwl::GetConfiguredHalStream));
334
335 ON_CALL(*this, DestroyPipelines())
336 .WillByDefault(Invoke(&fake_session_hwl_,
337 &FakeCameraDeviceSessionHwl::DestroyPipelines));
338
339 ON_CALL(*this, SubmitRequests(_, _))
340 .WillByDefault(Invoke(&fake_session_hwl_,
341 &FakeCameraDeviceSessionHwl::SubmitRequests));
342
343 ON_CALL(*this, Flush())
344 .WillByDefault(
345 Invoke(&fake_session_hwl_, &FakeCameraDeviceSessionHwl::Flush));
346
347 ON_CALL(*this, RepeatingRequestEnd(_, _))
348 .WillByDefault(Invoke(&fake_session_hwl_,
349 &FakeCameraDeviceSessionHwl::RepeatingRequestEnd));
350
351 ON_CALL(*this, GetCameraId())
352 .WillByDefault(
353 Invoke(&fake_session_hwl_, &FakeCameraDeviceSessionHwl::GetCameraId));
354
355 ON_CALL(*this, GetPhysicalCameraIds())
356 .WillByDefault(Invoke(&fake_session_hwl_,
357 &FakeCameraDeviceSessionHwl::GetPhysicalCameraIds));
358
359 ON_CALL(*this, GetCameraCharacteristics(_))
360 .WillByDefault(
361 Invoke(&fake_session_hwl_,
362 &FakeCameraDeviceSessionHwl::GetCameraCharacteristics));
363
364 ON_CALL(*this, GetPhysicalCameraCharacteristics(_, _))
365 .WillByDefault(Invoke(
366 &fake_session_hwl_,
367 &FakeCameraDeviceSessionHwl::GetPhysicalCameraCharacteristics));
368 }
369
370 } // namespace google_camera_hal
371 } // namespace android
372