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 <log/log.h>
19
20 #include <hardware/gralloc.h>
21 #include <system/graphics-base.h>
22
23 #include "mock_device_session_hwl.h"
24
25 namespace android {
26 namespace google_camera_hal {
27
28 using ::testing::_;
29 using ::testing::Invoke;
30
FakeCameraDeviceSessionHwl(uint32_t camera_id,const std::vector<uint32_t> & physical_camera_ids)31 FakeCameraDeviceSessionHwl::FakeCameraDeviceSessionHwl(
32 uint32_t camera_id, const std::vector<uint32_t>& physical_camera_ids)
33 : kCameraId(camera_id), kPhysicalCameraIds(physical_camera_ids) {
34 }
35
ConstructDefaultRequestSettings(RequestTemplate,std::unique_ptr<HalCameraMetadata> * default_settings)36 status_t FakeCameraDeviceSessionHwl::ConstructDefaultRequestSettings(
37 RequestTemplate /*type*/,
38 std::unique_ptr<HalCameraMetadata>* default_settings) {
39 if (default_settings == nullptr) {
40 return BAD_VALUE;
41 }
42
43 static constexpr uint32_t kDataBytes = 256;
44 static constexpr uint32_t kNumEntries = 10;
45 static constexpr int32_t kSensitivity = 200;
46
47 *default_settings = HalCameraMetadata::Create(kNumEntries, kDataBytes);
48 if (default_settings == nullptr) {
49 ALOGE("%s: Cannot create a HalCameraMetadata", __FUNCTION__);
50 return UNKNOWN_ERROR;
51 }
52
53 return (*default_settings)->Set(ANDROID_SENSOR_SENSITIVITY, &kSensitivity, 1);
54 }
55
PrepareConfigureStreams(const StreamConfiguration &)56 status_t FakeCameraDeviceSessionHwl::PrepareConfigureStreams(
57 const StreamConfiguration& /*overall_config*/) {
58 return OK;
59 }
60
ConfigurePipeline(uint32_t camera_id,HwlPipelineCallback hwl_pipeline_callback,const StreamConfiguration & request_config,const StreamConfiguration &,uint32_t * pipeline_id)61 status_t FakeCameraDeviceSessionHwl::ConfigurePipeline(
62 uint32_t camera_id, HwlPipelineCallback hwl_pipeline_callback,
63 const StreamConfiguration& request_config,
64 const StreamConfiguration& /*overall_config*/, uint32_t* pipeline_id) {
65 if (pipeline_id == nullptr) {
66 return BAD_VALUE;
67 }
68
69 // Check if the camera ID belongs to this camera.
70 if (camera_id != kCameraId &&
71 std::find(kPhysicalCameraIds.begin(), kPhysicalCameraIds.end(),
72 camera_id) == kPhysicalCameraIds.end()) {
73 ALOGE("%s: Unknown camera ID: %u", __FUNCTION__, camera_id);
74 return BAD_VALUE;
75 }
76
77 static constexpr uint32_t kDefaultMaxBuffers = 3;
78 std::vector<HalStream> hal_configured_streams;
79
80 for (auto& stream : request_config.streams) {
81 HalStream hal_stream = {};
82 hal_stream.id = stream.id;
83 hal_stream.override_format = stream.format;
84 hal_stream.producer_usage = stream.usage;
85 hal_stream.consumer_usage = GRALLOC_USAGE_HW_CAMERA_WRITE;
86 hal_stream.max_buffers = kDefaultMaxBuffers;
87 hal_stream.override_data_space = stream.data_space;
88 hal_stream.is_physical_camera_stream = stream.is_physical_camera_stream;
89 hal_stream.physical_camera_id = stream.physical_camera_id;
90
91 if (hal_stream.override_format == HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
92 hal_stream.override_format = HAL_PIXEL_FORMAT_YCRCB_420_SP;
93 }
94
95 hal_configured_streams.push_back(hal_stream);
96 }
97
98 {
99 std::lock_guard<std::mutex> lock(hwl_pipeline_lock_);
100
101 // Remember pipeline callback.
102 static uint32_t next_available_pipeline_id = 0;
103 hwl_pipeline_callbacks_.emplace(next_available_pipeline_id,
104 hwl_pipeline_callback);
105 *pipeline_id = next_available_pipeline_id++;
106
107 // Rememember configured HAL streams.
108 pipeline_hal_streams_map_.emplace(*pipeline_id, hal_configured_streams);
109 }
110
111 return OK;
112 }
113
BuildPipelines()114 status_t FakeCameraDeviceSessionHwl::BuildPipelines() {
115 std::lock_guard<std::mutex> lock(hwl_pipeline_lock_);
116 if (pipeline_hal_streams_map_.empty()) {
117 return NO_INIT;
118 }
119
120 return OK;
121 }
122
GetRequiredIntputStreams(const StreamConfiguration &,HwlOfflinePipelineRole pipeline_role,std::vector<Stream> * streams)123 status_t FakeCameraDeviceSessionHwl::GetRequiredIntputStreams(
124 const StreamConfiguration& /*overall_config*/,
125 HwlOfflinePipelineRole pipeline_role, std::vector<Stream>* streams) {
126 // Only supports offline ST
127 if (pipeline_role != HwlOfflinePipelineRole::kOfflineSmoothTransitionRole) {
128 return BAD_VALUE;
129 }
130 if (streams == nullptr) {
131 return BAD_VALUE;
132 }
133
134 for (int i = 0; i < 6; ++i) {
135 Stream internal_stream;
136
137 internal_stream.id = i;
138 streams->push_back(internal_stream);
139 }
140
141 return OK;
142 }
143
GetConfiguredHalStream(uint32_t pipeline_id,std::vector<HalStream> * hal_streams) const144 status_t FakeCameraDeviceSessionHwl::GetConfiguredHalStream(
145 uint32_t pipeline_id, std::vector<HalStream>* hal_streams) const {
146 std::lock_guard<std::mutex> lock(hwl_pipeline_lock_);
147 if (hal_streams == nullptr) {
148 return BAD_VALUE;
149 }
150
151 if (pipeline_hal_streams_map_.empty()) {
152 return NO_INIT;
153 }
154
155 if (pipeline_hal_streams_map_.find(pipeline_id) ==
156 pipeline_hal_streams_map_.end()) {
157 return NAME_NOT_FOUND;
158 }
159
160 *hal_streams = pipeline_hal_streams_map_.at(pipeline_id);
161 return OK;
162 }
163
DestroyPipelines()164 void FakeCameraDeviceSessionHwl::DestroyPipelines() {
165 std::lock_guard<std::mutex> lock(hwl_pipeline_lock_);
166 hwl_pipeline_callbacks_.clear();
167 pipeline_hal_streams_map_.clear();
168 }
169
SubmitRequests(uint32_t frame_number,const std::vector<HwlPipelineRequest> & requests)170 status_t FakeCameraDeviceSessionHwl::SubmitRequests(
171 uint32_t frame_number, const std::vector<HwlPipelineRequest>& requests) {
172 std::lock_guard<std::mutex> lock(hwl_pipeline_lock_);
173
174 for (auto& request : requests) {
175 auto callback = hwl_pipeline_callbacks_.find(request.pipeline_id);
176 if (callback == hwl_pipeline_callbacks_.end()) {
177 ALOGE("%s: Could not find callback for pipeline %u", __FUNCTION__,
178 request.pipeline_id);
179 return BAD_VALUE;
180 }
181
182 // Notify shutter.
183 NotifyMessage shutter_message = {.type = MessageType::kShutter,
184 .message.shutter = {
185 .frame_number = frame_number,
186 .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<IMulticamCoordinatorHwl>
CreateMulticamCoordinatorHwl()287 FakeCameraDeviceSessionHwl::CreateMulticamCoordinatorHwl() {
288 // Multicam coordinator not supported in this mock
289 return nullptr;
290 }
291
MockDeviceSessionHwl(uint32_t camera_id,const std::vector<uint32_t> & physical_camera_ids)292 MockDeviceSessionHwl::MockDeviceSessionHwl(
293 uint32_t camera_id, const std::vector<uint32_t>& physical_camera_ids)
294 : fake_session_hwl_(camera_id, physical_camera_ids) {
295 }
296
DelegateCallsToFakeSession()297 void MockDeviceSessionHwl::DelegateCallsToFakeSession() {
298 ON_CALL(*this, ConstructDefaultRequestSettings(_, _))
299 .WillByDefault(
300 Invoke(&fake_session_hwl_,
301 &FakeCameraDeviceSessionHwl::ConstructDefaultRequestSettings));
302
303 ON_CALL(*this, ConfigurePipeline(_, _, _, _, _))
304 .WillByDefault(Invoke(&fake_session_hwl_,
305 &FakeCameraDeviceSessionHwl::ConfigurePipeline));
306
307 ON_CALL(*this, BuildPipelines())
308 .WillByDefault(Invoke(&fake_session_hwl_,
309 &FakeCameraDeviceSessionHwl::BuildPipelines));
310
311 ON_CALL(*this, PreparePipeline(_, _))
312 .WillByDefault(Invoke(&fake_session_hwl_,
313 &FakeCameraDeviceSessionHwl::PreparePipeline));
314
315 ON_CALL(*this, GetRequiredIntputStreams(_, _, _))
316 .WillByDefault(
317 Invoke(&fake_session_hwl_,
318 &FakeCameraDeviceSessionHwl::GetRequiredIntputStreams));
319
320 ON_CALL(*this, GetConfiguredHalStream(_, _))
321 .WillByDefault(
322 Invoke(&fake_session_hwl_,
323 &FakeCameraDeviceSessionHwl::GetConfiguredHalStream));
324
325 ON_CALL(*this, DestroyPipelines())
326 .WillByDefault(Invoke(&fake_session_hwl_,
327 &FakeCameraDeviceSessionHwl::DestroyPipelines));
328
329 ON_CALL(*this, SubmitRequests(_, _))
330 .WillByDefault(Invoke(&fake_session_hwl_,
331 &FakeCameraDeviceSessionHwl::SubmitRequests));
332
333 ON_CALL(*this, Flush())
334 .WillByDefault(
335 Invoke(&fake_session_hwl_, &FakeCameraDeviceSessionHwl::Flush));
336
337 ON_CALL(*this, GetCameraId())
338 .WillByDefault(
339 Invoke(&fake_session_hwl_, &FakeCameraDeviceSessionHwl::GetCameraId));
340
341 ON_CALL(*this, GetPhysicalCameraIds())
342 .WillByDefault(Invoke(&fake_session_hwl_,
343 &FakeCameraDeviceSessionHwl::GetPhysicalCameraIds));
344
345 ON_CALL(*this, GetCameraCharacteristics(_))
346 .WillByDefault(
347 Invoke(&fake_session_hwl_,
348 &FakeCameraDeviceSessionHwl::GetCameraCharacteristics));
349
350 ON_CALL(*this, GetPhysicalCameraCharacteristics(_, _))
351 .WillByDefault(Invoke(
352 &fake_session_hwl_,
353 &FakeCameraDeviceSessionHwl::GetPhysicalCameraCharacteristics));
354 }
355
356 } // namespace google_camera_hal
357 } // namespace android
358