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 google_camera_hal::ConfigureStreamsReturn & hal_config,ConfigureStreamsRet * aidl_config)188 status_t ConvertToAidlHalStreamConfig(
189     const google_camera_hal::ConfigureStreamsReturn& hal_config,
190     ConfigureStreamsRet* aidl_config) {
191   if (aidl_config == nullptr) {
192     ALOGE("%s: aidl_config is nullptr.", __FUNCTION__);
193     return BAD_VALUE;
194   }
195 
196   aidl_config->halStreams.resize(hal_config.hal_streams.size());
197 
198   for (uint32_t i = 0; i < hal_config.hal_streams.size(); i++) {
199     auto& dst = aidl_config->halStreams[i];
200     dst.supportOffline = false;
201     if (hal_config.hal_streams[i].is_physical_camera_stream) {
202       dst.physicalCameraId =
203           std::to_string(hal_config.hal_streams[i].physical_camera_id);
204     }
205 
206     dst.overrideDataSpace =
207         static_cast<aidl::android::hardware::graphics::common::Dataspace>(
208             hal_config.hal_streams[i].override_data_space);
209 
210     dst.id = hal_config.hal_streams[i].id;
211 
212     dst.overrideFormat =
213         static_cast<aidl::android::hardware::graphics::common::PixelFormat>(
214             hal_config.hal_streams[i].override_format);
215 
216     dst.producerUsage =
217         static_cast<aidl::android::hardware::graphics::common::BufferUsage>(
218             hal_config.hal_streams[i].producer_usage);
219 
220     dst.consumerUsage =
221         static_cast<aidl::android::hardware::graphics::common::BufferUsage>(
222             hal_config.hal_streams[i].consumer_usage);
223 
224     dst.maxBuffers = hal_config.hal_streams[i].max_buffers;
225     dst.enableHalBufferManager = hal_config.hal_streams[i].is_hal_buffer_managed;
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 = makeToAidlIfNotNull(hal_buffer.acquire_fence);
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 
816   if (aidl_request.inputBuffer.streamId != -1) {
817     res = ConvertToHalStreamBuffer(aidl_request.inputBuffer, &hal_buffer,
818                                    handles_to_delete);
819     if (res != OK) {
820       ALOGE("%s: Converting hal stream buffer failed: %s(%d)", __FUNCTION__,
821             strerror(-res), res);
822       return res;
823     }
824 
825     hal_request->input_buffers.push_back(hal_buffer);
826     hal_request->input_width = aidl_request.inputWidth;
827     hal_request->input_height = aidl_request.inputHeight;
828   }
829 
830   for (auto& buffer : aidl_request.outputBuffers) {
831     hal_buffer = {};
832     status_t res =
833         ConvertToHalStreamBuffer(buffer, &hal_buffer, handles_to_delete);
834     if (res != OK) {
835       ALOGE("%s: Converting hal stream buffer failed: %s(%d)", __FUNCTION__,
836             strerror(-res), res);
837       return res;
838     }
839 
840     hal_request->output_buffers.push_back(hal_buffer);
841   }
842 
843   for (auto aidl_physical_settings : aidl_request.physicalCameraSettings) {
844     std::unique_ptr<google_camera_hal::HalCameraMetadata> hal_physical_settings;
845     res = ConvertToHalMetadata(
846         aidl_physical_settings.fmqSettingsSize, request_metadata_queue,
847         aidl_physical_settings.settings.metadata, &hal_physical_settings);
848     if (res != OK) {
849       ALOGE("%s: Converting to HAL metadata failed: %s(%d)", __FUNCTION__,
850             strerror(-res), res);
851       return res;
852     }
853 
854     uint32_t camera_id = std::stoul(aidl_physical_settings.physicalCameraId);
855     hal_request->physical_camera_settings.emplace(
856         camera_id, std::move(hal_physical_settings));
857   }
858 
859   return OK;
860 }
861 
ConvertToHalBufferCaches(const std::vector<BufferCache> & aidl_buffer_caches,std::vector<google_camera_hal::BufferCache> * hal_buffer_caches)862 status_t ConvertToHalBufferCaches(
863     const std::vector<BufferCache>& aidl_buffer_caches,
864     std::vector<google_camera_hal::BufferCache>* hal_buffer_caches) {
865   if (hal_buffer_caches == nullptr) {
866     ALOGE("%s: hal_buffer_caches is nullptr.", __FUNCTION__);
867     return BAD_VALUE;
868   }
869 
870   for (auto aidl_cache : aidl_buffer_caches) {
871     google_camera_hal::BufferCache hal_cache;
872     hal_cache.stream_id = aidl_cache.streamId;
873     hal_cache.buffer_id = aidl_cache.bufferId;
874 
875     hal_buffer_caches->push_back(hal_cache);
876   }
877 
878   return OK;
879 }
880 
ConvertToHalStreamConfigurationMode(StreamConfigurationMode aidl_mode,google_camera_hal::StreamConfigurationMode * hal_mode)881 status_t ConvertToHalStreamConfigurationMode(
882     StreamConfigurationMode aidl_mode,
883     google_camera_hal::StreamConfigurationMode* hal_mode) {
884   if (hal_mode == nullptr) {
885     ALOGE("%s: hal_mode is nullptr.", __FUNCTION__);
886     return BAD_VALUE;
887   }
888 
889   switch (aidl_mode) {
890     case StreamConfigurationMode::NORMAL_MODE:
891       *hal_mode = google_camera_hal::StreamConfigurationMode::kNormal;
892       break;
893     case StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE:
894       *hal_mode =
895           google_camera_hal::StreamConfigurationMode::kConstrainedHighSpeed;
896       break;
897     default:
898       ALOGE("%s: Unknown configuration mode %u", __FUNCTION__, aidl_mode);
899       return BAD_VALUE;
900   }
901 
902   return OK;
903 }
904 
sensorPixelModeContains(const Stream & aidl_stream,uint32_t key)905 static bool sensorPixelModeContains(const Stream& aidl_stream, uint32_t key) {
906   using aidl::android::hardware::camera::metadata::SensorPixelMode;
907   for (auto& i : aidl_stream.sensorPixelModesUsed) {
908     if (i == static_cast<SensorPixelMode>(key)) {
909       return true;
910     }
911   }
912   return false;
913 }
914 
FixSensorPixelModesInStreamConfig(StreamConfiguration * out_aidl_stream_config)915 void FixSensorPixelModesInStreamConfig(
916     StreamConfiguration* out_aidl_stream_config) {
917   if (out_aidl_stream_config == nullptr) {
918     ALOGE("%s: input stream config is NULL", __FUNCTION__);
919     return;
920   }
921 
922   // Get the sensor pixel modes in the stream config, do one pass and check if
923   // default is present in all.
924   using SensorPixelMode =
925       aidl::android::hardware::camera::metadata::SensorPixelMode;
926   for (const auto& stream : out_aidl_stream_config->streams) {
927     const auto& sensorPixelModes = stream.sensorPixelModesUsed;
928     if ((std::count(sensorPixelModes.begin(), sensorPixelModes.end(),
929                     static_cast<SensorPixelMode>(
930                         ANDROID_SENSOR_PIXEL_MODE_DEFAULT)) == 0)) {
931       return;
932     }
933   }
934 
935   // All of them contain DEFAULT, just override them to be default only.
936   for (auto& stream : out_aidl_stream_config->streams) {
937     stream.sensorPixelModesUsed.clear();
938     stream.sensorPixelModesUsed.push_back(
939         static_cast<SensorPixelMode>(ANDROID_SENSOR_PIXEL_MODE_DEFAULT));
940   }
941 }
942 
ConvertToHalStreamConfig(const StreamConfiguration & aidl_stream_config,google_camera_hal::StreamConfiguration * hal_stream_config)943 status_t ConvertToHalStreamConfig(
944     const StreamConfiguration& aidl_stream_config,
945     google_camera_hal::StreamConfiguration* hal_stream_config) {
946   if (hal_stream_config == nullptr) {
947     ALOGE("%s: hal_stream_config is nullptr.", __FUNCTION__);
948     return BAD_VALUE;
949   }
950 
951   status_t res;
952 
953   for (auto aidl_stream : aidl_stream_config.streams) {
954     google_camera_hal::Stream hal_stream;
955     res = ConvertToHalStream(aidl_stream, &hal_stream);
956     if (res != OK) {
957       ALOGE("%s: Converting to HAL stream failed: %s(%d)", __FUNCTION__,
958             strerror(-res), res);
959       return res;
960     }
961     hal_stream_config->streams.push_back(hal_stream);
962   }
963 
964   res = ConvertToHalStreamConfigurationMode(aidl_stream_config.operationMode,
965                                             &hal_stream_config->operation_mode);
966   if (res != OK) {
967     ALOGE("%s: Converting to HAL opeation mode failed: %s(%d)", __FUNCTION__,
968           strerror(-res), res);
969     return res;
970   }
971 
972   res = ConvertToHalMetadata(0, nullptr,
973                              aidl_stream_config.sessionParams.metadata,
974                              &hal_stream_config->session_params);
975   if (res != OK) {
976     ALOGE("%s: Converting to HAL metadata failed: %s(%d)", __FUNCTION__,
977           strerror(-res), res);
978     return res;
979   }
980 
981   hal_stream_config->stream_config_counter =
982       aidl_stream_config.streamConfigCounter;
983   hal_stream_config->multi_resolution_input_image =
984       aidl_stream_config.multiResolutionInputImage;
985   hal_stream_config->log_id = aidl_stream_config.logId;
986 
987   return OK;
988 }
989 
ConvertToHalStreamType(StreamType aidl_stream_type,google_camera_hal::StreamType * hal_stream_type)990 status_t ConvertToHalStreamType(StreamType aidl_stream_type,
991                                 google_camera_hal::StreamType* hal_stream_type) {
992   if (hal_stream_type == nullptr) {
993     ALOGE("%s: hal_stream_type is nullptr.", __FUNCTION__);
994     return BAD_VALUE;
995   }
996 
997   switch (aidl_stream_type) {
998     case StreamType::OUTPUT:
999       *hal_stream_type = google_camera_hal::StreamType::kOutput;
1000       break;
1001     case StreamType::INPUT:
1002       *hal_stream_type = google_camera_hal::StreamType::kInput;
1003       break;
1004     default:
1005       ALOGE("%s: Unknown stream type: %u", __FUNCTION__, aidl_stream_type);
1006       return BAD_VALUE;
1007   }
1008 
1009   return OK;
1010 }
1011 
ConvertToHalStreamRotation(StreamRotation aidl_stream_rotation,google_camera_hal::StreamRotation * hal_stream_rotation)1012 status_t ConvertToHalStreamRotation(
1013     StreamRotation aidl_stream_rotation,
1014     google_camera_hal::StreamRotation* hal_stream_rotation) {
1015   if (hal_stream_rotation == nullptr) {
1016     ALOGE("%s: hal_stream_rotation is nullptr.", __FUNCTION__);
1017     return BAD_VALUE;
1018   }
1019 
1020   switch (aidl_stream_rotation) {
1021     case StreamRotation::ROTATION_0:
1022       *hal_stream_rotation = google_camera_hal::StreamRotation::kRotation0;
1023       break;
1024     case StreamRotation::ROTATION_90:
1025       *hal_stream_rotation = google_camera_hal::StreamRotation::kRotation90;
1026       break;
1027     case StreamRotation::ROTATION_180:
1028       *hal_stream_rotation = google_camera_hal::StreamRotation::kRotation180;
1029       break;
1030     case StreamRotation::ROTATION_270:
1031       *hal_stream_rotation = google_camera_hal::StreamRotation::kRotation270;
1032       break;
1033     default:
1034       ALOGE("%s: Unknown stream rotation: %u", __FUNCTION__,
1035             aidl_stream_rotation);
1036       return BAD_VALUE;
1037   }
1038 
1039   return OK;
1040 }
1041 
ConvertToHalStream(const Stream & aidl_stream,google_camera_hal::Stream * hal_stream)1042 status_t ConvertToHalStream(const Stream& aidl_stream,
1043                             google_camera_hal::Stream* hal_stream) {
1044   if (hal_stream == nullptr) {
1045     ALOGE("%s: hal_stream is nullptr.", __FUNCTION__);
1046     return BAD_VALUE;
1047   }
1048 
1049   *hal_stream = {};
1050 
1051   hal_stream->id = aidl_stream.id;
1052 
1053   status_t res =
1054       ConvertToHalStreamType(aidl_stream.streamType, &hal_stream->stream_type);
1055   if (res != OK) {
1056     ALOGE("%s: Converting to HAL stream type failed: %s(%d)", __FUNCTION__,
1057           strerror(-res), res);
1058     return res;
1059   }
1060 
1061   hal_stream->width = aidl_stream.width;
1062   hal_stream->height = aidl_stream.height;
1063   hal_stream->format = (android_pixel_format_t)aidl_stream.format;
1064   hal_stream->usage = (uint64_t)aidl_stream.usage;
1065   hal_stream->data_space = (android_dataspace_t)aidl_stream.dataSpace;
1066 
1067   res = ConvertToHalStreamRotation(aidl_stream.rotation, &hal_stream->rotation);
1068   if (res != OK) {
1069     ALOGE("%s: Converting to HAL stream rotation failed: %s(%d)", __FUNCTION__,
1070           strerror(-res), res);
1071     return res;
1072   }
1073 
1074   if (aidl_stream.physicalCameraId.empty()) {
1075     hal_stream->is_physical_camera_stream = false;
1076   } else {
1077     hal_stream->is_physical_camera_stream = true;
1078     hal_stream->physical_camera_id = std::stoul(aidl_stream.physicalCameraId);
1079   }
1080 
1081   hal_stream->buffer_size = aidl_stream.bufferSize;
1082   hal_stream->group_id = aidl_stream.groupId;
1083 
1084   hal_stream->intended_for_max_resolution_mode = sensorPixelModeContains(
1085       aidl_stream, ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION);
1086   hal_stream->intended_for_default_resolution_mode =
1087       aidl_stream.sensorPixelModesUsed.size() > 0
1088           ? sensorPixelModeContains(aidl_stream,
1089                                     ANDROID_SENSOR_PIXEL_MODE_DEFAULT)
1090           : true;
1091   hal_stream->dynamic_profile =
1092       static_cast<DynamicRangeProfile>(aidl_stream.dynamicRangeProfile);
1093   hal_stream->color_space =
1094       static_cast<ColorSpaceProfile>(aidl_stream.colorSpace);
1095 
1096   hal_stream->use_case =
1097       static_cast<camera_metadata_enum_android_scaler_available_stream_use_cases>(
1098           aidl_stream.useCase);
1099 
1100   return OK;
1101 }
1102 
ConvertToHalBufferRequestStatus(const BufferRequestStatus & aidl_buffer_request_status,google_camera_hal::BufferRequestStatus * hal_buffer_request_status)1103 status_t ConvertToHalBufferRequestStatus(
1104     const BufferRequestStatus& aidl_buffer_request_status,
1105     google_camera_hal::BufferRequestStatus* hal_buffer_request_status) {
1106   if (hal_buffer_request_status == nullptr) {
1107     ALOGE("%s: hal_buffer_request_status is nullptr.", __FUNCTION__);
1108     return BAD_VALUE;
1109   }
1110 
1111   switch (aidl_buffer_request_status) {
1112     case BufferRequestStatus::OK:
1113       *hal_buffer_request_status = google_camera_hal::BufferRequestStatus::kOk;
1114       break;
1115     case BufferRequestStatus::FAILED_PARTIAL:
1116       *hal_buffer_request_status =
1117           google_camera_hal::BufferRequestStatus::kFailedPartial;
1118       break;
1119     case BufferRequestStatus::FAILED_CONFIGURING:
1120       *hal_buffer_request_status =
1121           google_camera_hal::BufferRequestStatus::kFailedConfiguring;
1122       break;
1123     case BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS:
1124       *hal_buffer_request_status =
1125           google_camera_hal::BufferRequestStatus::kFailedIllegalArgs;
1126       break;
1127     case BufferRequestStatus::FAILED_UNKNOWN:
1128       *hal_buffer_request_status =
1129           google_camera_hal::BufferRequestStatus::kFailedUnknown;
1130       break;
1131     default:
1132       ALOGE("%s: Failed unknown buffer request error code %d", __FUNCTION__,
1133             aidl_buffer_request_status);
1134       return BAD_VALUE;
1135   }
1136 
1137   return OK;
1138 }
1139 
ConvertToHalBufferReturnStatus(const StreamBufferRet & aidl_stream_buffer_return,google_camera_hal::BufferReturn * hal_buffer_return)1140 status_t ConvertToHalBufferReturnStatus(
1141     const StreamBufferRet& aidl_stream_buffer_return,
1142     google_camera_hal::BufferReturn* hal_buffer_return) {
1143   using Tag = aidl::android::hardware::camera::device::StreamBuffersVal::Tag;
1144   if (hal_buffer_return == nullptr) {
1145     ALOGE("%s: hal_buffer_return is nullptr.", __FUNCTION__);
1146     return BAD_VALUE;
1147   }
1148 
1149   if (aidl_stream_buffer_return.val.getTag() == Tag::error) {
1150     switch (aidl_stream_buffer_return.val.get<Tag::error>()) {
1151       case StreamBufferRequestError::NO_BUFFER_AVAILABLE:
1152         hal_buffer_return->val.error =
1153             google_camera_hal::StreamBufferRequestError::kNoBufferAvailable;
1154         break;
1155       case StreamBufferRequestError::MAX_BUFFER_EXCEEDED:
1156         hal_buffer_return->val.error =
1157             google_camera_hal::StreamBufferRequestError::kMaxBufferExceeded;
1158         break;
1159       case StreamBufferRequestError::STREAM_DISCONNECTED:
1160         hal_buffer_return->val.error =
1161             google_camera_hal::StreamBufferRequestError::kStreamDisconnected;
1162         break;
1163       case StreamBufferRequestError::UNKNOWN_ERROR:
1164         hal_buffer_return->val.error =
1165             google_camera_hal::StreamBufferRequestError::kUnknownError;
1166         break;
1167       default:
1168         ALOGE("%s: Unknown StreamBufferRequestError %d", __FUNCTION__,
1169               aidl_stream_buffer_return.val.get<Tag::error>());
1170         return BAD_VALUE;
1171     }
1172   } else {
1173     hal_buffer_return->val.error =
1174         google_camera_hal::StreamBufferRequestError::kOk;
1175   }
1176 
1177   return OK;
1178 }
1179 
ConvertToHalDeviceState(int64_t aidl_device_state,google_camera_hal::DeviceState & hal_device_state)1180 status_t ConvertToHalDeviceState(
1181     int64_t aidl_device_state,
1182     google_camera_hal::DeviceState& hal_device_state) {
1183   switch (aidl_device_state) {
1184     case ICameraProvider::DEVICE_STATE_NORMAL:
1185       hal_device_state = google_camera_hal::DeviceState::kNormal;
1186       break;
1187     case ICameraProvider::DEVICE_STATE_BACK_COVERED:
1188       hal_device_state = google_camera_hal::DeviceState::kBackCovered;
1189       break;
1190     case ICameraProvider::DEVICE_STATE_FRONT_COVERED:
1191       hal_device_state = google_camera_hal::DeviceState::kFrontCovered;
1192       break;
1193     case ICameraProvider::DEVICE_STATE_FOLDED:
1194       hal_device_state = google_camera_hal::DeviceState::kFolded;
1195       break;
1196     default:
1197       ALOGE("%s: Failed unknown device state", __FUNCTION__);
1198       return BAD_VALUE;
1199   }
1200 
1201   return OK;
1202 }
1203 
1204 }  // namespace aidl_utils
1205 }  // namespace implementation
1206 }  // namespace camera
1207 }  // namespace hardware
1208 }  // namespace android
1209