1 /*
2 * Copyright (C) 2022 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 "GCH_AidlUtils"
18 // #define LOG_NDEBUG 0
19 #include "aidl_utils.h"
20
21 #include <aidlcommonsupport/NativeHandle.h>
22 #include <log/log.h>
23 #include <system/camera_metadata.h>
24
25 #include <regex>
26
27 #include "aidl_camera_device.h"
28 #include "aidl_camera_provider.h"
29
30 namespace android {
31 namespace hardware {
32 namespace camera {
33 namespace implementation {
34 namespace aidl_utils {
35
36 using AidlCameraProvider = provider::implementation::AidlCameraProvider;
37 using AidlCameraDevice = device::implementation::AidlCameraDevice;
38 using AidlStatus = aidl::android::hardware::camera::common::Status;
39 using DynamicRangeProfile = google_camera_hal::DynamicRangeProfile;
40 using ColorSpaceProfile = google_camera_hal::ColorSpaceProfile;
41
ConvertToAidlReturn(status_t hal_status)42 ScopedAStatus ConvertToAidlReturn(status_t hal_status) {
43 switch (hal_status) {
44 case OK:
45 return ScopedAStatus::ok();
46 case BAD_VALUE:
47 return ScopedAStatus::fromServiceSpecificError(
48 static_cast<int32_t>(Status::ILLEGAL_ARGUMENT));
49 case -EBUSY:
50 return ScopedAStatus::fromServiceSpecificError(
51 static_cast<int32_t>(Status::CAMERA_IN_USE));
52 case -EUSERS:
53 return ScopedAStatus::fromServiceSpecificError(
54 static_cast<int32_t>(Status::MAX_CAMERAS_IN_USE));
55 case UNKNOWN_TRANSACTION:
56 case INVALID_OPERATION:
57 return ScopedAStatus::fromServiceSpecificError(
58 static_cast<int32_t>(Status::OPERATION_NOT_SUPPORTED));
59 case DEAD_OBJECT:
60 return ScopedAStatus::fromServiceSpecificError(
61 static_cast<int32_t>(Status::CAMERA_DISCONNECTED));
62 default:
63 return ScopedAStatus::fromServiceSpecificError(
64 static_cast<int32_t>(Status::INTERNAL_ERROR));
65 }
66 }
67
ConvertToAidlVendorTagType(google_camera_hal::CameraMetadataType hal_type,CameraMetadataType * aidl_type)68 status_t ConvertToAidlVendorTagType(
69 google_camera_hal::CameraMetadataType hal_type,
70 CameraMetadataType* aidl_type) {
71 if (aidl_type == nullptr) {
72 ALOGE("%s: aidl_type is nullptr.", __FUNCTION__);
73 return BAD_VALUE;
74 }
75
76 switch (hal_type) {
77 case google_camera_hal::CameraMetadataType::kByte:
78 *aidl_type = CameraMetadataType::BYTE;
79 break;
80 case google_camera_hal::CameraMetadataType::kInt32:
81 *aidl_type = CameraMetadataType::INT32;
82 break;
83 case google_camera_hal::CameraMetadataType::kFloat:
84 *aidl_type = CameraMetadataType::FLOAT;
85 break;
86 case google_camera_hal::CameraMetadataType::kInt64:
87 *aidl_type = CameraMetadataType::INT64;
88 break;
89 case google_camera_hal::CameraMetadataType::kDouble:
90 *aidl_type = CameraMetadataType::DOUBLE;
91 break;
92 case google_camera_hal::CameraMetadataType::kRational:
93 *aidl_type = CameraMetadataType::RATIONAL;
94 break;
95 default:
96 ALOGE("%s: Unknown google_camera_hal::CameraMetadataType: %u",
97 __FUNCTION__, hal_type);
98 return BAD_VALUE;
99 }
100
101 return OK;
102 }
103
ConvertToAidlVendorTagSections(const std::vector<google_camera_hal::VendorTagSection> & hal_sections,std::vector<VendorTagSection> * aidl_sections)104 status_t ConvertToAidlVendorTagSections(
105 const std::vector<google_camera_hal::VendorTagSection>& hal_sections,
106 std::vector<VendorTagSection>* aidl_sections) {
107 if (aidl_sections == nullptr) {
108 ALOGE("%s: aidl_sections is nullptr.", __FUNCTION__);
109 return BAD_VALUE;
110 }
111
112 aidl_sections->resize(hal_sections.size());
113 for (uint32_t i = 0; i < hal_sections.size(); i++) {
114 (*aidl_sections)[i].sectionName = hal_sections[i].section_name;
115 (*aidl_sections)[i].tags.resize(hal_sections[i].tags.size());
116
117 for (uint32_t j = 0; j < hal_sections[i].tags.size(); j++) {
118 (*aidl_sections)[i].tags[j].tagId = hal_sections[i].tags[j].tag_id;
119 (*aidl_sections)[i].tags[j].tagName = hal_sections[i].tags[j].tag_name;
120 status_t res =
121 ConvertToAidlVendorTagType(hal_sections[i].tags[j].tag_type,
122 &(*aidl_sections)[i].tags[j].tagType);
123 if (res != OK) {
124 ALOGE("%s: Converting to aidl vendor tag type failed. ", __FUNCTION__);
125 return res;
126 }
127 }
128 }
129 return OK;
130 }
131
ConvertToAidlResourceCost(const google_camera_hal::CameraResourceCost & hal_cost,CameraResourceCost * aidl_cost)132 status_t ConvertToAidlResourceCost(
133 const google_camera_hal::CameraResourceCost& hal_cost,
134 CameraResourceCost* aidl_cost) {
135 if (aidl_cost == nullptr) {
136 ALOGE("%s: aidl_cost is nullptr.", __FUNCTION__);
137 return BAD_VALUE;
138 }
139
140 aidl_cost->resourceCost = hal_cost.resource_cost;
141 aidl_cost->conflictingDevices.resize(hal_cost.conflicting_devices.size());
142
143 for (uint32_t i = 0; i < hal_cost.conflicting_devices.size(); i++) {
144 aidl_cost->conflictingDevices[i] =
145 "device@" + AidlCameraDevice::kDeviceVersion + "/" +
146 AidlCameraProvider::kProviderName + "/" +
147 std::to_string(hal_cost.conflicting_devices[i]);
148 }
149
150 return OK;
151 }
152
ConvertToHalTemplateType(RequestTemplate aidl_template,google_camera_hal::RequestTemplate * hal_template)153 status_t ConvertToHalTemplateType(
154 RequestTemplate aidl_template,
155 google_camera_hal::RequestTemplate* hal_template) {
156 if (hal_template == nullptr) {
157 ALOGE("%s: hal_template is nullptr.", __FUNCTION__);
158 return BAD_VALUE;
159 }
160
161 switch (aidl_template) {
162 case RequestTemplate::PREVIEW:
163 *hal_template = google_camera_hal::RequestTemplate::kPreview;
164 break;
165 case RequestTemplate::STILL_CAPTURE:
166 *hal_template = google_camera_hal::RequestTemplate::kStillCapture;
167 break;
168 case RequestTemplate::VIDEO_RECORD:
169 *hal_template = google_camera_hal::RequestTemplate::kVideoRecord;
170 break;
171 case RequestTemplate::VIDEO_SNAPSHOT:
172 *hal_template = google_camera_hal::RequestTemplate::kVideoSnapshot;
173 break;
174 case RequestTemplate::ZERO_SHUTTER_LAG:
175 *hal_template = google_camera_hal::RequestTemplate::kZeroShutterLag;
176 break;
177 case RequestTemplate::MANUAL:
178 *hal_template = google_camera_hal::RequestTemplate::kManual;
179 break;
180 default:
181 ALOGE("%s: Unknown AIDL RequestTemplate: %u", __FUNCTION__, aidl_template);
182 return BAD_VALUE;
183 }
184
185 return OK;
186 }
187
ConvertToAidlHalStreamConfig(const std::vector<google_camera_hal::HalStream> & hal_configured_streams,std::vector<HalStream> * aidl_hal_streams)188 status_t ConvertToAidlHalStreamConfig(
189 const std::vector<google_camera_hal::HalStream>& hal_configured_streams,
190 std::vector<HalStream>* aidl_hal_streams) {
191 if (aidl_hal_streams == nullptr) {
192 ALOGE("%s: aidl_hal_streams is nullptr.", __FUNCTION__);
193 return BAD_VALUE;
194 }
195
196 aidl_hal_streams->resize(hal_configured_streams.size());
197
198 for (uint32_t i = 0; i < hal_configured_streams.size(); i++) {
199 auto& dst = (*aidl_hal_streams)[i];
200 dst.supportOffline = false;
201 if (hal_configured_streams[i].is_physical_camera_stream) {
202 dst.physicalCameraId =
203 std::to_string(hal_configured_streams[i].physical_camera_id);
204 }
205
206 dst.overrideDataSpace =
207 static_cast<aidl::android::hardware::graphics::common::Dataspace>(
208 hal_configured_streams[i].override_data_space);
209
210 dst.id = hal_configured_streams[i].id;
211
212 dst.overrideFormat =
213 static_cast<aidl::android::hardware::graphics::common::PixelFormat>(
214 hal_configured_streams[i].override_format);
215
216 dst.producerUsage =
217 static_cast<aidl::android::hardware::graphics::common::BufferUsage>(
218 hal_configured_streams[i].producer_usage);
219
220 dst.consumerUsage =
221 static_cast<aidl::android::hardware::graphics::common::BufferUsage>(
222 hal_configured_streams[i].consumer_usage);
223
224 dst.maxBuffers = hal_configured_streams[i].max_buffers;
225 }
226
227 return OK;
228 }
229
WriteToResultMetadataQueue(camera_metadata_t * metadata,AidlMessageQueue<int8_t,SynchronizedReadWrite> * result_metadata_queue)230 status_t WriteToResultMetadataQueue(
231 camera_metadata_t* metadata,
232 AidlMessageQueue<int8_t, SynchronizedReadWrite>* result_metadata_queue) {
233 if (result_metadata_queue == nullptr) {
234 return BAD_VALUE;
235 }
236
237 if (result_metadata_queue->availableToWrite() <= 0) {
238 ALOGW("%s: result_metadata_queue is not available to write", __FUNCTION__);
239 return BAD_VALUE;
240 }
241
242 uint32_t size = get_camera_metadata_size(metadata);
243 bool success = result_metadata_queue->write(
244 reinterpret_cast<const int8_t*>(metadata), size);
245 if (!success) {
246 ALOGW("%s: Writing to result metadata queue failed. (size=%u)",
247 __FUNCTION__, size);
248 return INVALID_OPERATION;
249 }
250
251 return OK;
252 }
253
254 // Try writing result metadata to result metadata queue. If failed, return
255 // the metadata to the caller in out_hal_metadata.
TryWritingToResultMetadataQueue(std::unique_ptr<google_camera_hal::HalCameraMetadata> hal_metadata,AidlMessageQueue<int8_t,SynchronizedReadWrite> * result_metadata_queue,uint64_t * fmq_result_size,std::unique_ptr<google_camera_hal::HalCameraMetadata> * out_hal_metadata)256 status_t TryWritingToResultMetadataQueue(
257 std::unique_ptr<google_camera_hal::HalCameraMetadata> hal_metadata,
258 AidlMessageQueue<int8_t, SynchronizedReadWrite>* result_metadata_queue,
259 uint64_t* fmq_result_size,
260 std::unique_ptr<google_camera_hal::HalCameraMetadata>* out_hal_metadata) {
261 if (out_hal_metadata == nullptr) {
262 ALOGE("%s: out_hal_metadata is nullptr.", __FUNCTION__);
263 return BAD_VALUE;
264 }
265
266 *out_hal_metadata = std::move(hal_metadata);
267
268 if (fmq_result_size == nullptr) {
269 ALOGE("%s: fmq_result_size is nullptr", __FUNCTION__);
270 return BAD_VALUE;
271 }
272
273 *fmq_result_size = 0;
274 if (*out_hal_metadata == nullptr) {
275 return OK;
276 }
277
278 camera_metadata_t* metadata = (*out_hal_metadata)->ReleaseCameraMetadata();
279 // Temporarily use the raw metadata to write to metadata queue.
280 status_t res = WriteToResultMetadataQueue(metadata, result_metadata_queue);
281 *out_hal_metadata = google_camera_hal::HalCameraMetadata::Create(metadata);
282
283 if (res != OK) {
284 ALOGW("%s: Writing to result metadata queue failed: %s(%d)", __FUNCTION__,
285 strerror(-res), res);
286 return res;
287 }
288
289 *fmq_result_size = (*out_hal_metadata)->GetCameraMetadataSize();
290 *out_hal_metadata = nullptr;
291 return OK;
292 }
293
ConvertToAidlResultMetadata(AidlMessageQueue<int8_t,SynchronizedReadWrite> * result_metadata_queue,std::unique_ptr<google_camera_hal::HalCameraMetadata> hal_metadata,std::vector<uint8_t> * aidl_metadata,uint64_t * fmq_result_size)294 status_t ConvertToAidlResultMetadata(
295 AidlMessageQueue<int8_t, SynchronizedReadWrite>* result_metadata_queue,
296 std::unique_ptr<google_camera_hal::HalCameraMetadata> hal_metadata,
297 std::vector<uint8_t>* aidl_metadata, uint64_t* fmq_result_size) {
298 if (TryWritingToResultMetadataQueue(std::move(hal_metadata),
299 result_metadata_queue, fmq_result_size,
300 &hal_metadata) == OK) {
301 return OK;
302 }
303
304 // If writing to metadata queue failed, attach the metadata to aidl_metadata.
305 if (aidl_metadata == nullptr) {
306 ALOGE("%s: aidl_metadata is nullptr", __FUNCTION__);
307 return BAD_VALUE;
308 }
309
310 uint32_t metadata_size = hal_metadata->GetCameraMetadataSize();
311 const auto* metadata_p =
312 reinterpret_cast<const uint8_t*>(hal_metadata->GetRawCameraMetadata());
313 // TODO: Do we reallly need to copy here ?
314 aidl_metadata->assign(metadata_p, metadata_p + metadata_size);
315
316 return OK;
317 }
318
ConvertToAidlBufferStatus(google_camera_hal::BufferStatus hal_status,BufferStatus * aidl_status)319 status_t ConvertToAidlBufferStatus(google_camera_hal::BufferStatus hal_status,
320 BufferStatus* aidl_status) {
321 if (aidl_status == nullptr) {
322 ALOGE("%s: aidl_status is nullptr.", __FUNCTION__);
323 return BAD_VALUE;
324 }
325
326 switch (hal_status) {
327 case google_camera_hal::BufferStatus::kOk:
328 *aidl_status = BufferStatus::OK;
329 break;
330 case google_camera_hal::BufferStatus::kError:
331 *aidl_status = BufferStatus::ERROR;
332 break;
333 default:
334 ALOGE("%s: Unknown HAL buffer status: %u", __FUNCTION__, hal_status);
335 return BAD_VALUE;
336 }
337
338 return OK;
339 }
340
makeToAidlIfNotNull(const native_handle_t * nh)341 aidl::android::hardware::common::NativeHandle makeToAidlIfNotNull(
342 const native_handle_t* nh) {
343 if (nh == nullptr) {
344 return aidl::android::hardware::common::NativeHandle();
345 }
346 return makeToAidl(nh);
347 }
348
ConvertToAidlStreamBuffer(const google_camera_hal::StreamBuffer & hal_buffer,StreamBuffer * aidl_buffer)349 status_t ConvertToAidlStreamBuffer(
350 const google_camera_hal::StreamBuffer& hal_buffer,
351 StreamBuffer* aidl_buffer) {
352 if (aidl_buffer == nullptr) {
353 ALOGE("%s: aidl_buffer is nullptr", __FUNCTION__);
354 return BAD_VALUE;
355 }
356
357 aidl_buffer->streamId = hal_buffer.stream_id;
358 aidl_buffer->bufferId = hal_buffer.buffer_id;
359 aidl_buffer->buffer = aidl::android::hardware::common::NativeHandle();
360
361 status_t res =
362 ConvertToAidlBufferStatus(hal_buffer.status, &aidl_buffer->status);
363 if (res != OK) {
364 ALOGE("%s: Converting to AIDL buffer status failed: %s(%d)", __FUNCTION__,
365 strerror(-res), res);
366 return res;
367 }
368
369 aidl_buffer->acquireFence = aidl::android::hardware::common::NativeHandle();
370 aidl_buffer->releaseFence = makeToAidlIfNotNull(hal_buffer.release_fence);
371 return OK;
372 }
373
ConvertToAidlCaptureResultInternal(AidlMessageQueue<int8_t,SynchronizedReadWrite> * result_metadata_queue,google_camera_hal::CaptureResult * hal_result,CaptureResult * aidl_result)374 status_t ConvertToAidlCaptureResultInternal(
375 AidlMessageQueue<int8_t, SynchronizedReadWrite>* result_metadata_queue,
376 google_camera_hal::CaptureResult* hal_result, CaptureResult* aidl_result) {
377 if (aidl_result == nullptr) {
378 ALOGE("%s: aidl_result is nullptr.", __FUNCTION__);
379 return BAD_VALUE;
380 }
381
382 if (hal_result == nullptr) {
383 ALOGE("%s: hal_result is nullptr.", __FUNCTION__);
384 return BAD_VALUE;
385 }
386
387 aidl_result->frameNumber = hal_result->frame_number;
388
389 status_t res = ConvertToAidlResultMetadata(
390 result_metadata_queue, std::move(hal_result->result_metadata),
391 &aidl_result->result.metadata, (uint64_t*)&aidl_result->fmqResultSize);
392 if (res != OK) {
393 ALOGE("%s: Converting to AIDL result metadata failed: %s(%d).",
394 __FUNCTION__, strerror(-res), res);
395 return res;
396 }
397
398 aidl_result->outputBuffers.resize(hal_result->output_buffers.size());
399 for (uint32_t i = 0; i < aidl_result->outputBuffers.size(); i++) {
400 res = ConvertToAidlStreamBuffer(hal_result->output_buffers[i],
401 &aidl_result->outputBuffers[i]);
402 if (res != OK) {
403 ALOGE("%s: Converting to AIDL output stream buffer failed: %s(%d)",
404 __FUNCTION__, strerror(-res), res);
405 return res;
406 }
407 }
408
409 uint32_t num_input_buffers = hal_result->input_buffers.size();
410 if (num_input_buffers > 0) {
411 if (num_input_buffers > 1) {
412 ALOGW("%s: HAL result should not have more than 1 input buffer. (=%u)",
413 __FUNCTION__, num_input_buffers);
414 }
415
416 res = ConvertToAidlStreamBuffer(hal_result->input_buffers[0],
417 &aidl_result->inputBuffer);
418 if (res != OK) {
419 ALOGE("%s: Converting to AIDL input stream buffer failed: %s(%d)",
420 __FUNCTION__, strerror(-res), res);
421 return res;
422 }
423 } else {
424 aidl_result->inputBuffer.streamId = -1;
425 }
426
427 aidl_result->partialResult = hal_result->partial_result;
428 return OK;
429 }
430
ConvertToAidlCaptureResult(AidlMessageQueue<int8_t,SynchronizedReadWrite> * result_metadata_queue,std::unique_ptr<google_camera_hal::CaptureResult> hal_result,CaptureResult * aidl_result)431 status_t ConvertToAidlCaptureResult(
432 AidlMessageQueue<int8_t, SynchronizedReadWrite>* result_metadata_queue,
433 std::unique_ptr<google_camera_hal::CaptureResult> hal_result,
434 CaptureResult* aidl_result) {
435 if (aidl_result == nullptr) {
436 ALOGE("%s: aidl_result is nullptr.", __FUNCTION__);
437 return BAD_VALUE;
438 }
439
440 if (hal_result == nullptr) {
441 ALOGE("%s: hal_result is nullptr.", __FUNCTION__);
442 return BAD_VALUE;
443 }
444
445 status_t res = ConvertToAidlCaptureResultInternal(
446 result_metadata_queue, hal_result.get(), aidl_result);
447 if (res != OK) {
448 ALOGE("%s: Converting to AIDL result internal failed: %s(%d).",
449 __FUNCTION__, strerror(-res), res);
450 return res;
451 }
452
453 uint32_t num_physical_metadata = hal_result->physical_metadata.size();
454 aidl_result->physicalCameraMetadata.resize(num_physical_metadata);
455
456 for (uint32_t i = 0; i < num_physical_metadata; i++) {
457 aidl_result->physicalCameraMetadata[i].physicalCameraId =
458 std::to_string(hal_result->physical_metadata[i].physical_camera_id);
459
460 res = ConvertToAidlResultMetadata(
461 result_metadata_queue,
462 std::move(hal_result->physical_metadata[i].metadata),
463 &aidl_result->physicalCameraMetadata[i].metadata.metadata,
464 (uint64_t*)&aidl_result->physicalCameraMetadata[i].fmqMetadataSize);
465 if (res != OK) {
466 ALOGE("%s: Converting to AIDL physical metadata failed: %s(%d).",
467 __FUNCTION__, strerror(-res), res);
468 return res;
469 }
470 }
471
472 return OK;
473 }
474
ConvertToAidlErrorMessage(const google_camera_hal::ErrorMessage & hal_error,NotifyMsg * aidl_msg)475 status_t ConvertToAidlErrorMessage(
476 const google_camera_hal::ErrorMessage& hal_error, NotifyMsg* aidl_msg) {
477 using Tag = aidl::android::hardware::camera::device::NotifyMsg::Tag;
478 if (aidl_msg == nullptr) {
479 ALOGE("%s: aidl_msg is nullptr.", __FUNCTION__);
480 return BAD_VALUE;
481 }
482
483 ErrorMsg aidl_error;
484 aidl_error.frameNumber = hal_error.frame_number;
485 aidl_error.errorStreamId = hal_error.error_stream_id;
486
487 switch (hal_error.error_code) {
488 case google_camera_hal::ErrorCode::kErrorDevice:
489 aidl_error.errorCode = ErrorCode::ERROR_DEVICE;
490 break;
491 case google_camera_hal::ErrorCode::kErrorRequest:
492 aidl_error.errorCode = ErrorCode::ERROR_REQUEST;
493 break;
494 case google_camera_hal::ErrorCode::kErrorResult:
495 aidl_error.errorCode = ErrorCode::ERROR_RESULT;
496 break;
497 case google_camera_hal::ErrorCode::kErrorBuffer:
498 aidl_error.errorCode = ErrorCode::ERROR_BUFFER;
499 break;
500 default:
501 ALOGE("%s: Unknown error code: %u", __FUNCTION__, hal_error.error_code);
502 return BAD_VALUE;
503 }
504 aidl_msg->set<Tag::error>(aidl_error);
505 return OK;
506 }
507
ConvertToAidlShutterMessage(const google_camera_hal::ShutterMessage & hal_shutter,NotifyMsg * aidl_msg)508 status_t ConvertToAidlShutterMessage(
509 const google_camera_hal::ShutterMessage& hal_shutter, NotifyMsg* aidl_msg) {
510 using Tag = aidl::android::hardware::camera::device::NotifyMsg::Tag;
511 if (aidl_msg == nullptr) {
512 ALOGE("%s: aidl_msg is nullptr.", __FUNCTION__);
513 return BAD_VALUE;
514 }
515 ShutterMsg aidl_shutter;
516 aidl_shutter.frameNumber = hal_shutter.frame_number;
517 aidl_shutter.timestamp = hal_shutter.timestamp_ns;
518 aidl_shutter.readoutTimestamp = hal_shutter.readout_timestamp_ns;
519 aidl_msg->set<Tag::shutter>(aidl_shutter);
520 return OK;
521 }
522
ConverToAidlNotifyMessage(const google_camera_hal::NotifyMessage & hal_message,NotifyMsg * aidl_message)523 status_t ConverToAidlNotifyMessage(
524 const google_camera_hal::NotifyMessage& hal_message,
525 NotifyMsg* aidl_message) {
526 if (aidl_message == nullptr) {
527 ALOGE("%s: aidl_message is nullptr.", __FUNCTION__);
528 return BAD_VALUE;
529 }
530
531 status_t res;
532 switch (hal_message.type) {
533 case google_camera_hal::MessageType::kError:
534 res = ConvertToAidlErrorMessage(hal_message.message.error, aidl_message);
535 if (res != OK) {
536 ALOGE("%s: Converting to AIDL error message failed: %s(%d)",
537 __FUNCTION__, strerror(-res), res);
538 return res;
539 }
540 break;
541 case google_camera_hal::MessageType::kShutter:
542 res = ConvertToAidlShutterMessage(hal_message.message.shutter,
543 aidl_message);
544 if (res != OK) {
545 ALOGE("%s: Converting to AIDL shutter message failed: %s(%d)",
546 __FUNCTION__, strerror(-res), res);
547 return res;
548 }
549 break;
550 default:
551 ALOGE("%s: Unknown message type: %u", __FUNCTION__, hal_message.type);
552 return BAD_VALUE;
553 }
554
555 return OK;
556 }
557
ConvertToAidlCameraDeviceStatus(google_camera_hal::CameraDeviceStatus hal_camera_device_status,CameraDeviceStatus * aidl_camera_device_status)558 status_t ConvertToAidlCameraDeviceStatus(
559 google_camera_hal::CameraDeviceStatus hal_camera_device_status,
560 CameraDeviceStatus* aidl_camera_device_status) {
561 if (aidl_camera_device_status == nullptr) {
562 ALOGE("%s: aidl_camera_device_status is nullptr.", __FUNCTION__);
563 return BAD_VALUE;
564 }
565
566 switch (hal_camera_device_status) {
567 case google_camera_hal::CameraDeviceStatus::kNotPresent:
568 *aidl_camera_device_status = CameraDeviceStatus::NOT_PRESENT;
569 break;
570 case google_camera_hal::CameraDeviceStatus::kPresent:
571 *aidl_camera_device_status = CameraDeviceStatus::PRESENT;
572 break;
573 case google_camera_hal::CameraDeviceStatus::kEnumerating:
574 *aidl_camera_device_status = CameraDeviceStatus::ENUMERATING;
575 break;
576 default:
577 ALOGE("%s: Unknown HAL camera device status: %u", __FUNCTION__,
578 hal_camera_device_status);
579 return BAD_VALUE;
580 }
581
582 return OK;
583 }
584
ConvertToAidlTorchModeStatus(google_camera_hal::TorchModeStatus hal_torch_status,TorchModeStatus * aidl_torch_status)585 status_t ConvertToAidlTorchModeStatus(
586 google_camera_hal::TorchModeStatus hal_torch_status,
587 TorchModeStatus* aidl_torch_status) {
588 if (aidl_torch_status == nullptr) {
589 ALOGE("%s: aidl_torch_status is nullptr.", __FUNCTION__);
590 return BAD_VALUE;
591 }
592
593 switch (hal_torch_status) {
594 case google_camera_hal::TorchModeStatus::kNotAvailable:
595 *aidl_torch_status = TorchModeStatus::NOT_AVAILABLE;
596 break;
597 case google_camera_hal::TorchModeStatus::kAvailableOff:
598 *aidl_torch_status = TorchModeStatus::AVAILABLE_OFF;
599 break;
600 case google_camera_hal::TorchModeStatus::kAvailableOn:
601 *aidl_torch_status = TorchModeStatus::AVAILABLE_ON;
602 break;
603 default:
604 ALOGE("%s: Unknown HAL torch mode status: %u", __FUNCTION__,
605 hal_torch_status);
606 return BAD_VALUE;
607 }
608
609 return OK;
610 }
611
ConvertToAidlBufferRequest(const std::vector<google_camera_hal::BufferRequest> & hal_buffer_requests,std::vector<BufferRequest> * aidl_buffer_requests)612 status_t ConvertToAidlBufferRequest(
613 const std::vector<google_camera_hal::BufferRequest>& hal_buffer_requests,
614 std::vector<BufferRequest>* aidl_buffer_requests) {
615 if (aidl_buffer_requests == nullptr) {
616 ALOGE("%s: aidl_buffer_request is nullptr.", __FUNCTION__);
617 return BAD_VALUE;
618 }
619
620 aidl_buffer_requests->resize(hal_buffer_requests.size());
621 for (uint32_t i = 0; i < hal_buffer_requests.size(); i++) {
622 (*aidl_buffer_requests)[i].streamId = hal_buffer_requests[i].stream_id;
623 (*aidl_buffer_requests)[i].numBuffersRequested =
624 hal_buffer_requests[i].num_buffers_requested;
625 }
626 return OK;
627 }
628
ConvertToHalBufferStatus(BufferStatus aidl_status,google_camera_hal::BufferStatus * hal_status)629 status_t ConvertToHalBufferStatus(BufferStatus aidl_status,
630 google_camera_hal::BufferStatus* hal_status) {
631 if (hal_status == nullptr) {
632 ALOGE("%s: hal_status is nullptr.", __FUNCTION__);
633 return BAD_VALUE;
634 }
635
636 switch (aidl_status) {
637 case BufferStatus::OK:
638 *hal_status = google_camera_hal::BufferStatus::kOk;
639 break;
640 case BufferStatus::ERROR:
641 *hal_status = google_camera_hal::BufferStatus::kError;
642 break;
643 default:
644 ALOGE("%s: Unknown AIDL buffer status: %u", __FUNCTION__, aidl_status);
645 return BAD_VALUE;
646 }
647
648 return OK;
649 }
650
IsAidlNativeHandleNull(const NativeHandle & handle)651 bool IsAidlNativeHandleNull(const NativeHandle& handle) {
652 return (handle.fds.size() == 0 && handle.ints.size() == 0);
653 }
654
makeFromAidlIfNotNull(const NativeHandle & handle)655 native_handle_t* makeFromAidlIfNotNull(const NativeHandle& handle) {
656 if (IsAidlNativeHandleNull(handle)) {
657 return nullptr;
658 }
659 return makeFromAidl(handle);
660 }
661
662 // We have a handles_to_delete parameter since makeFromAidl creates a
663 // native_handle_t
ConvertToHalStreamBuffer(const StreamBuffer & aidl_buffer,google_camera_hal::StreamBuffer * hal_buffer,std::vector<native_handle_t * > * handles_to_delete)664 status_t ConvertToHalStreamBuffer(
665 const StreamBuffer& aidl_buffer, google_camera_hal::StreamBuffer* hal_buffer,
666 std::vector<native_handle_t*>* handles_to_delete) {
667 if (hal_buffer == nullptr || handles_to_delete == nullptr) {
668 ALOGE("%s: hal_buffer / handles_to_delete is nullptr.", __FUNCTION__);
669 return BAD_VALUE;
670 }
671
672 hal_buffer->stream_id = aidl_buffer.streamId;
673 hal_buffer->buffer_id = aidl_buffer.bufferId;
674 native_handle_t* buf_handle = makeFromAidlIfNotNull(aidl_buffer.buffer);
675 hal_buffer->buffer = buf_handle;
676 if (buf_handle != nullptr) {
677 handles_to_delete->emplace_back(buf_handle);
678 }
679
680 status_t res =
681 ConvertToHalBufferStatus(aidl_buffer.status, &hal_buffer->status);
682 if (res != OK) {
683 ALOGE("%s: Converting to HAL buffer status failed: %s(%d)", __FUNCTION__,
684 strerror(-res), res);
685 return res;
686 }
687
688 native_handle_t* acquire_handle =
689 makeFromAidlIfNotNull(aidl_buffer.acquireFence);
690 native_handle_t* release_handle =
691 makeFromAidlIfNotNull(aidl_buffer.releaseFence);
692 hal_buffer->acquire_fence = acquire_handle;
693 hal_buffer->release_fence = release_handle;
694 if (acquire_handle != nullptr) {
695 handles_to_delete->emplace_back(acquire_handle);
696 }
697
698 if (release_handle != nullptr) {
699 handles_to_delete->emplace_back(release_handle);
700 }
701
702 return OK;
703 }
704
ConvertToHalMetadata(uint32_t message_queue_setting_size,AidlMessageQueue<int8_t,SynchronizedReadWrite> * request_metadata_queue,const std::vector<uint8_t> & request_settings,std::unique_ptr<google_camera_hal::HalCameraMetadata> * hal_metadata)705 status_t ConvertToHalMetadata(
706 uint32_t message_queue_setting_size,
707 AidlMessageQueue<int8_t, SynchronizedReadWrite>* request_metadata_queue,
708 const std::vector<uint8_t>& request_settings,
709 std::unique_ptr<google_camera_hal::HalCameraMetadata>* hal_metadata) {
710 if (hal_metadata == nullptr) {
711 ALOGE("%s: hal_metadata is nullptr.", __FUNCTION__);
712 return BAD_VALUE;
713 }
714
715 const camera_metadata_t* metadata = nullptr;
716 std::vector<int8_t> metadata_queue_settings;
717 const size_t min_camera_metadata_size =
718 calculate_camera_metadata_size(/*entry_count=*/0, /*data_count=*/0);
719
720 if (message_queue_setting_size == 0) {
721 // Use the settings in the request.
722 if (request_settings.size() != 0) {
723 if (request_settings.size() < min_camera_metadata_size) {
724 ALOGE("%s: The size of request_settings is %zu, which is not valid",
725 __FUNCTION__, request_settings.size());
726 return BAD_VALUE;
727 }
728
729 metadata =
730 reinterpret_cast<const camera_metadata_t*>(request_settings.data());
731
732 size_t metadata_size = get_camera_metadata_size(metadata);
733 if (metadata_size != request_settings.size()) {
734 ALOGE(
735 "%s: Mismatch between camera metadata size (%zu) and request "
736 "setting size (%zu)",
737 __FUNCTION__, metadata_size, request_settings.size());
738 return BAD_VALUE;
739 }
740 }
741 } else {
742 // Read the settings from request metadata queue.
743 if (request_metadata_queue == nullptr) {
744 ALOGE("%s: request_metadata_queue is nullptr", __FUNCTION__);
745 return BAD_VALUE;
746 }
747 if (message_queue_setting_size < min_camera_metadata_size) {
748 ALOGE("%s: invalid message queue setting size: %u", __FUNCTION__,
749 message_queue_setting_size);
750 return BAD_VALUE;
751 }
752
753 metadata_queue_settings.resize(message_queue_setting_size);
754 bool success = request_metadata_queue->read(metadata_queue_settings.data(),
755 message_queue_setting_size);
756 if (!success) {
757 ALOGE("%s: Failed to read from request metadata queue.", __FUNCTION__);
758 return BAD_VALUE;
759 }
760
761 metadata = reinterpret_cast<const camera_metadata_t*>(
762 metadata_queue_settings.data());
763
764 size_t metadata_size = get_camera_metadata_size(metadata);
765 if (metadata_size != message_queue_setting_size) {
766 ALOGE(
767 "%s: Mismatch between camera metadata size (%zu) and message "
768 "queue setting size (%u)",
769 __FUNCTION__, metadata_size, message_queue_setting_size);
770 return BAD_VALUE;
771 }
772 }
773
774 if (metadata == nullptr) {
775 *hal_metadata = nullptr;
776 return OK;
777 }
778
779 // Validates the injected metadata structure. This prevents memory access
780 // violation that could be introduced by malformed metadata.
781 // (b/236688120) In general we trust metadata sent from Framework, but this is
782 // to defend an exploit chain that skips Framework's validation.
783 if (validate_camera_metadata_structure(metadata, /*expected_size=*/NULL) !=
784 OK) {
785 ALOGE("%s: Failed to validate the metadata structure", __FUNCTION__);
786 return BAD_VALUE;
787 }
788
789 *hal_metadata = google_camera_hal::HalCameraMetadata::Clone(metadata);
790 return OK;
791 }
792
ConvertToHalCaptureRequest(const CaptureRequest & aidl_request,AidlMessageQueue<int8_t,SynchronizedReadWrite> * request_metadata_queue,google_camera_hal::CaptureRequest * hal_request,std::vector<native_handle_t * > * handles_to_delete)793 status_t ConvertToHalCaptureRequest(
794 const CaptureRequest& aidl_request,
795 AidlMessageQueue<int8_t, SynchronizedReadWrite>* request_metadata_queue,
796 google_camera_hal::CaptureRequest* hal_request,
797 std::vector<native_handle_t*>* handles_to_delete) {
798 if (hal_request == nullptr) {
799 ALOGE("%s: hal_request is nullptr.", __FUNCTION__);
800 return BAD_VALUE;
801 }
802
803 hal_request->frame_number = aidl_request.frameNumber;
804
805 status_t res = ConvertToHalMetadata(
806 aidl_request.fmqSettingsSize, request_metadata_queue,
807 aidl_request.settings.metadata, &hal_request->settings);
808 if (res != OK) {
809 ALOGE("%s: Converting metadata failed: %s(%d)", __FUNCTION__,
810 strerror(-res), res);
811 return res;
812 }
813
814 google_camera_hal::StreamBuffer hal_buffer = {};
815 if (!IsAidlNativeHandleNull(aidl_request.inputBuffer.buffer)) {
816 res = ConvertToHalStreamBuffer(aidl_request.inputBuffer, &hal_buffer,
817 handles_to_delete);
818 if (res != OK) {
819 ALOGE("%s: Converting hal stream buffer failed: %s(%d)", __FUNCTION__,
820 strerror(-res), res);
821 return res;
822 }
823
824 hal_request->input_buffers.push_back(hal_buffer);
825 hal_request->input_width = aidl_request.inputWidth;
826 hal_request->input_height = aidl_request.inputHeight;
827 }
828
829 for (auto& buffer : aidl_request.outputBuffers) {
830 hal_buffer = {};
831 status_t res =
832 ConvertToHalStreamBuffer(buffer, &hal_buffer, handles_to_delete);
833 if (res != OK) {
834 ALOGE("%s: Converting hal stream buffer failed: %s(%d)", __FUNCTION__,
835 strerror(-res), res);
836 return res;
837 }
838
839 hal_request->output_buffers.push_back(hal_buffer);
840 }
841
842 for (auto aidl_physical_settings : aidl_request.physicalCameraSettings) {
843 std::unique_ptr<google_camera_hal::HalCameraMetadata> hal_physical_settings;
844 res = ConvertToHalMetadata(
845 aidl_physical_settings.fmqSettingsSize, request_metadata_queue,
846 aidl_physical_settings.settings.metadata, &hal_physical_settings);
847 if (res != OK) {
848 ALOGE("%s: Converting to HAL metadata failed: %s(%d)", __FUNCTION__,
849 strerror(-res), res);
850 return res;
851 }
852
853 uint32_t camera_id = std::stoul(aidl_physical_settings.physicalCameraId);
854 hal_request->physical_camera_settings.emplace(
855 camera_id, std::move(hal_physical_settings));
856 }
857
858 return OK;
859 }
860
ConvertToHalBufferCaches(const std::vector<BufferCache> & aidl_buffer_caches,std::vector<google_camera_hal::BufferCache> * hal_buffer_caches)861 status_t ConvertToHalBufferCaches(
862 const std::vector<BufferCache>& aidl_buffer_caches,
863 std::vector<google_camera_hal::BufferCache>* hal_buffer_caches) {
864 if (hal_buffer_caches == nullptr) {
865 ALOGE("%s: hal_buffer_caches is nullptr.", __FUNCTION__);
866 return BAD_VALUE;
867 }
868
869 for (auto aidl_cache : aidl_buffer_caches) {
870 google_camera_hal::BufferCache hal_cache;
871 hal_cache.stream_id = aidl_cache.streamId;
872 hal_cache.buffer_id = aidl_cache.bufferId;
873
874 hal_buffer_caches->push_back(hal_cache);
875 }
876
877 return OK;
878 }
879
ConvertToHalStreamConfigurationMode(StreamConfigurationMode aidl_mode,google_camera_hal::StreamConfigurationMode * hal_mode)880 status_t ConvertToHalStreamConfigurationMode(
881 StreamConfigurationMode aidl_mode,
882 google_camera_hal::StreamConfigurationMode* hal_mode) {
883 if (hal_mode == nullptr) {
884 ALOGE("%s: hal_mode is nullptr.", __FUNCTION__);
885 return BAD_VALUE;
886 }
887
888 switch (aidl_mode) {
889 case StreamConfigurationMode::NORMAL_MODE:
890 *hal_mode = google_camera_hal::StreamConfigurationMode::kNormal;
891 break;
892 case StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE:
893 *hal_mode =
894 google_camera_hal::StreamConfigurationMode::kConstrainedHighSpeed;
895 break;
896 default:
897 ALOGE("%s: Unknown configuration mode %u", __FUNCTION__, aidl_mode);
898 return BAD_VALUE;
899 }
900
901 return OK;
902 }
903
sensorPixelModeContains(const Stream & aidl_stream,uint32_t key)904 static bool sensorPixelModeContains(const Stream& aidl_stream, uint32_t key) {
905 using aidl::android::hardware::camera::metadata::SensorPixelMode;
906 for (auto& i : aidl_stream.sensorPixelModesUsed) {
907 if (i == static_cast<SensorPixelMode>(key)) {
908 return true;
909 }
910 }
911 return false;
912 }
913
FixSensorPixelModesInStreamConfig(StreamConfiguration * out_aidl_stream_config)914 void FixSensorPixelModesInStreamConfig(
915 StreamConfiguration* out_aidl_stream_config) {
916 if (out_aidl_stream_config == nullptr) {
917 ALOGE("%s: input stream config is NULL", __FUNCTION__);
918 return;
919 }
920
921 // Get the sensor pixel modes in the stream config, do one pass and check if
922 // default is present in all.
923 using SensorPixelMode =
924 aidl::android::hardware::camera::metadata::SensorPixelMode;
925 for (const auto& stream : out_aidl_stream_config->streams) {
926 const auto& sensorPixelModes = stream.sensorPixelModesUsed;
927 if ((std::count(sensorPixelModes.begin(), sensorPixelModes.end(),
928 static_cast<SensorPixelMode>(
929 ANDROID_SENSOR_PIXEL_MODE_DEFAULT)) == 0)) {
930 return;
931 }
932 }
933
934 // All of them contain DEFAULT, just override them to be default only.
935 for (auto& stream : out_aidl_stream_config->streams) {
936 stream.sensorPixelModesUsed.clear();
937 stream.sensorPixelModesUsed.push_back(
938 static_cast<SensorPixelMode>(ANDROID_SENSOR_PIXEL_MODE_DEFAULT));
939 }
940 }
941
ConvertToHalStreamConfig(const StreamConfiguration & aidl_stream_config,google_camera_hal::StreamConfiguration * hal_stream_config)942 status_t ConvertToHalStreamConfig(
943 const StreamConfiguration& aidl_stream_config,
944 google_camera_hal::StreamConfiguration* hal_stream_config) {
945 if (hal_stream_config == nullptr) {
946 ALOGE("%s: hal_stream_config is nullptr.", __FUNCTION__);
947 return BAD_VALUE;
948 }
949
950 status_t res;
951
952 for (auto aidl_stream : aidl_stream_config.streams) {
953 google_camera_hal::Stream hal_stream;
954 res = ConvertToHalStream(aidl_stream, &hal_stream);
955 if (res != OK) {
956 ALOGE("%s: Converting to HAL stream failed: %s(%d)", __FUNCTION__,
957 strerror(-res), res);
958 return res;
959 }
960 hal_stream_config->streams.push_back(hal_stream);
961 }
962
963 res = ConvertToHalStreamConfigurationMode(aidl_stream_config.operationMode,
964 &hal_stream_config->operation_mode);
965 if (res != OK) {
966 ALOGE("%s: Converting to HAL opeation mode failed: %s(%d)", __FUNCTION__,
967 strerror(-res), res);
968 return res;
969 }
970
971 res = ConvertToHalMetadata(0, nullptr,
972 aidl_stream_config.sessionParams.metadata,
973 &hal_stream_config->session_params);
974 if (res != OK) {
975 ALOGE("%s: Converting to HAL metadata failed: %s(%d)", __FUNCTION__,
976 strerror(-res), res);
977 return res;
978 }
979
980 hal_stream_config->stream_config_counter =
981 aidl_stream_config.streamConfigCounter;
982 hal_stream_config->multi_resolution_input_image =
983 aidl_stream_config.multiResolutionInputImage;
984 hal_stream_config->log_id = aidl_stream_config.logId;
985
986 return OK;
987 }
988
ConvertToHalStreamType(StreamType aidl_stream_type,google_camera_hal::StreamType * hal_stream_type)989 status_t ConvertToHalStreamType(StreamType aidl_stream_type,
990 google_camera_hal::StreamType* hal_stream_type) {
991 if (hal_stream_type == nullptr) {
992 ALOGE("%s: hal_stream_type is nullptr.", __FUNCTION__);
993 return BAD_VALUE;
994 }
995
996 switch (aidl_stream_type) {
997 case StreamType::OUTPUT:
998 *hal_stream_type = google_camera_hal::StreamType::kOutput;
999 break;
1000 case StreamType::INPUT:
1001 *hal_stream_type = google_camera_hal::StreamType::kInput;
1002 break;
1003 default:
1004 ALOGE("%s: Unknown stream type: %u", __FUNCTION__, aidl_stream_type);
1005 return BAD_VALUE;
1006 }
1007
1008 return OK;
1009 }
1010
ConvertToHalStreamRotation(StreamRotation aidl_stream_rotation,google_camera_hal::StreamRotation * hal_stream_rotation)1011 status_t ConvertToHalStreamRotation(
1012 StreamRotation aidl_stream_rotation,
1013 google_camera_hal::StreamRotation* hal_stream_rotation) {
1014 if (hal_stream_rotation == nullptr) {
1015 ALOGE("%s: hal_stream_rotation is nullptr.", __FUNCTION__);
1016 return BAD_VALUE;
1017 }
1018
1019 switch (aidl_stream_rotation) {
1020 case StreamRotation::ROTATION_0:
1021 *hal_stream_rotation = google_camera_hal::StreamRotation::kRotation0;
1022 break;
1023 case StreamRotation::ROTATION_90:
1024 *hal_stream_rotation = google_camera_hal::StreamRotation::kRotation90;
1025 break;
1026 case StreamRotation::ROTATION_180:
1027 *hal_stream_rotation = google_camera_hal::StreamRotation::kRotation180;
1028 break;
1029 case StreamRotation::ROTATION_270:
1030 *hal_stream_rotation = google_camera_hal::StreamRotation::kRotation270;
1031 break;
1032 default:
1033 ALOGE("%s: Unknown stream rotation: %u", __FUNCTION__,
1034 aidl_stream_rotation);
1035 return BAD_VALUE;
1036 }
1037
1038 return OK;
1039 }
1040
ConvertToHalStream(const Stream & aidl_stream,google_camera_hal::Stream * hal_stream)1041 status_t ConvertToHalStream(const Stream& aidl_stream,
1042 google_camera_hal::Stream* hal_stream) {
1043 if (hal_stream == nullptr) {
1044 ALOGE("%s: hal_stream is nullptr.", __FUNCTION__);
1045 return BAD_VALUE;
1046 }
1047
1048 *hal_stream = {};
1049
1050 hal_stream->id = aidl_stream.id;
1051
1052 status_t res =
1053 ConvertToHalStreamType(aidl_stream.streamType, &hal_stream->stream_type);
1054 if (res != OK) {
1055 ALOGE("%s: Converting to HAL stream type failed: %s(%d)", __FUNCTION__,
1056 strerror(-res), res);
1057 return res;
1058 }
1059
1060 hal_stream->width = aidl_stream.width;
1061 hal_stream->height = aidl_stream.height;
1062 hal_stream->format = (android_pixel_format_t)aidl_stream.format;
1063 hal_stream->usage = (uint64_t)aidl_stream.usage;
1064 hal_stream->data_space = (android_dataspace_t)aidl_stream.dataSpace;
1065
1066 res = ConvertToHalStreamRotation(aidl_stream.rotation, &hal_stream->rotation);
1067 if (res != OK) {
1068 ALOGE("%s: Converting to HAL stream rotation failed: %s(%d)", __FUNCTION__,
1069 strerror(-res), res);
1070 return res;
1071 }
1072
1073 if (aidl_stream.physicalCameraId.empty()) {
1074 hal_stream->is_physical_camera_stream = false;
1075 } else {
1076 hal_stream->is_physical_camera_stream = true;
1077 hal_stream->physical_camera_id = std::stoul(aidl_stream.physicalCameraId);
1078 }
1079
1080 hal_stream->buffer_size = aidl_stream.bufferSize;
1081 hal_stream->group_id = aidl_stream.groupId;
1082
1083 hal_stream->intended_for_max_resolution_mode = sensorPixelModeContains(
1084 aidl_stream, ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION);
1085 hal_stream->intended_for_default_resolution_mode =
1086 aidl_stream.sensorPixelModesUsed.size() > 0
1087 ? sensorPixelModeContains(aidl_stream,
1088 ANDROID_SENSOR_PIXEL_MODE_DEFAULT)
1089 : true;
1090 hal_stream->dynamic_profile =
1091 static_cast<DynamicRangeProfile>(aidl_stream.dynamicRangeProfile);
1092 hal_stream->color_space =
1093 static_cast<ColorSpaceProfile>(aidl_stream.colorSpace);
1094
1095 hal_stream->use_case =
1096 static_cast<camera_metadata_enum_android_scaler_available_stream_use_cases>(
1097 aidl_stream.useCase);
1098
1099 return OK;
1100 }
1101
ConvertToHalBufferRequestStatus(const BufferRequestStatus & aidl_buffer_request_status,google_camera_hal::BufferRequestStatus * hal_buffer_request_status)1102 status_t ConvertToHalBufferRequestStatus(
1103 const BufferRequestStatus& aidl_buffer_request_status,
1104 google_camera_hal::BufferRequestStatus* hal_buffer_request_status) {
1105 if (hal_buffer_request_status == nullptr) {
1106 ALOGE("%s: hal_buffer_request_status is nullptr.", __FUNCTION__);
1107 return BAD_VALUE;
1108 }
1109
1110 switch (aidl_buffer_request_status) {
1111 case BufferRequestStatus::OK:
1112 *hal_buffer_request_status = google_camera_hal::BufferRequestStatus::kOk;
1113 break;
1114 case BufferRequestStatus::FAILED_PARTIAL:
1115 *hal_buffer_request_status =
1116 google_camera_hal::BufferRequestStatus::kFailedPartial;
1117 break;
1118 case BufferRequestStatus::FAILED_CONFIGURING:
1119 *hal_buffer_request_status =
1120 google_camera_hal::BufferRequestStatus::kFailedConfiguring;
1121 break;
1122 case BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS:
1123 *hal_buffer_request_status =
1124 google_camera_hal::BufferRequestStatus::kFailedIllegalArgs;
1125 break;
1126 case BufferRequestStatus::FAILED_UNKNOWN:
1127 *hal_buffer_request_status =
1128 google_camera_hal::BufferRequestStatus::kFailedUnknown;
1129 break;
1130 default:
1131 ALOGE("%s: Failed unknown buffer request error code %d", __FUNCTION__,
1132 aidl_buffer_request_status);
1133 return BAD_VALUE;
1134 }
1135
1136 return OK;
1137 }
1138
ConvertToHalBufferReturnStatus(const StreamBufferRet & aidl_stream_buffer_return,google_camera_hal::BufferReturn * hal_buffer_return)1139 status_t ConvertToHalBufferReturnStatus(
1140 const StreamBufferRet& aidl_stream_buffer_return,
1141 google_camera_hal::BufferReturn* hal_buffer_return) {
1142 using Tag = aidl::android::hardware::camera::device::StreamBuffersVal::Tag;
1143 if (hal_buffer_return == nullptr) {
1144 ALOGE("%s: hal_buffer_return is nullptr.", __FUNCTION__);
1145 return BAD_VALUE;
1146 }
1147
1148 if (aidl_stream_buffer_return.val.getTag() == Tag::error) {
1149 switch (aidl_stream_buffer_return.val.get<Tag::error>()) {
1150 case StreamBufferRequestError::NO_BUFFER_AVAILABLE:
1151 hal_buffer_return->val.error =
1152 google_camera_hal::StreamBufferRequestError::kNoBufferAvailable;
1153 break;
1154 case StreamBufferRequestError::MAX_BUFFER_EXCEEDED:
1155 hal_buffer_return->val.error =
1156 google_camera_hal::StreamBufferRequestError::kMaxBufferExceeded;
1157 break;
1158 case StreamBufferRequestError::STREAM_DISCONNECTED:
1159 hal_buffer_return->val.error =
1160 google_camera_hal::StreamBufferRequestError::kStreamDisconnected;
1161 break;
1162 case StreamBufferRequestError::UNKNOWN_ERROR:
1163 hal_buffer_return->val.error =
1164 google_camera_hal::StreamBufferRequestError::kUnknownError;
1165 break;
1166 default:
1167 ALOGE("%s: Unknown StreamBufferRequestError %d", __FUNCTION__,
1168 aidl_stream_buffer_return.val.get<Tag::error>());
1169 return BAD_VALUE;
1170 }
1171 } else {
1172 hal_buffer_return->val.error =
1173 google_camera_hal::StreamBufferRequestError::kOk;
1174 }
1175
1176 return OK;
1177 }
1178
ConvertToHalDeviceState(int64_t aidl_device_state,google_camera_hal::DeviceState & hal_device_state)1179 status_t ConvertToHalDeviceState(
1180 int64_t aidl_device_state,
1181 google_camera_hal::DeviceState& hal_device_state) {
1182 switch (aidl_device_state) {
1183 case ICameraProvider::DEVICE_STATE_NORMAL:
1184 hal_device_state = google_camera_hal::DeviceState::kNormal;
1185 break;
1186 case ICameraProvider::DEVICE_STATE_BACK_COVERED:
1187 hal_device_state = google_camera_hal::DeviceState::kBackCovered;
1188 break;
1189 case ICameraProvider::DEVICE_STATE_FRONT_COVERED:
1190 hal_device_state = google_camera_hal::DeviceState::kFrontCovered;
1191 break;
1192 case ICameraProvider::DEVICE_STATE_FOLDED:
1193 hal_device_state = google_camera_hal::DeviceState::kFolded;
1194 break;
1195 default:
1196 ALOGE("%s: Failed unknown device state", __FUNCTION__);
1197 return BAD_VALUE;
1198 }
1199
1200 return OK;
1201 }
1202
1203 } // namespace aidl_utils
1204 } // namespace implementation
1205 } // namespace camera
1206 } // namespace hardware
1207 } // namespace android
1208