1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "GCH_HidlUtils"
18 //#define LOG_NDEBUG 0
19 #include <log/log.h>
20 #include <regex>
21 
22 #include "hidl_camera_device.h"
23 #include "hidl_camera_provider.h"
24 #include "hidl_utils.h"
25 
26 namespace android {
27 namespace hardware {
28 namespace camera {
29 namespace implementation {
30 namespace hidl_utils {
31 
32 using ::android::hardware::camera::device::V3_2::ErrorCode;
33 using ::android::hardware::camera::device::V3_2::ErrorMsg;
34 using ::android::hardware::camera::device::V3_2::MsgType;
35 using ::android::hardware::camera::device::V3_2::ShutterMsg;
36 using ::android::hardware::camera::device::V3_5::implementation::HidlCameraDevice;
37 using ::android::hardware::camera::provider::V2_6::implementation::HidlCameraProvider;
38 
ConvertToHidlVendorTagType(google_camera_hal::CameraMetadataType hal_type,CameraMetadataType * hidl_type)39 status_t ConvertToHidlVendorTagType(
40     google_camera_hal::CameraMetadataType hal_type,
41     CameraMetadataType* hidl_type) {
42   if (hidl_type == nullptr) {
43     ALOGE("%s: hidl_type is nullptr.", __FUNCTION__);
44     return BAD_VALUE;
45   }
46 
47   switch (hal_type) {
48     case google_camera_hal::CameraMetadataType::kByte:
49       *hidl_type = CameraMetadataType::BYTE;
50       break;
51     case google_camera_hal::CameraMetadataType::kInt32:
52       *hidl_type = CameraMetadataType::INT32;
53       break;
54     case google_camera_hal::CameraMetadataType::kFloat:
55       *hidl_type = CameraMetadataType::FLOAT;
56       break;
57     case google_camera_hal::CameraMetadataType::kInt64:
58       *hidl_type = CameraMetadataType::INT64;
59       break;
60     case google_camera_hal::CameraMetadataType::kDouble:
61       *hidl_type = CameraMetadataType::DOUBLE;
62       break;
63     case google_camera_hal::CameraMetadataType::kRational:
64       *hidl_type = CameraMetadataType::RATIONAL;
65       break;
66     default:
67       ALOGE("%s: Unknown google_camera_hal::CameraMetadataType: %u",
68             __FUNCTION__, hal_type);
69       return BAD_VALUE;
70   }
71 
72   return OK;
73 }
74 
ConvertToHidlResourceCost(const google_camera_hal::CameraResourceCost & hal_cost,CameraResourceCost * hidl_cost)75 status_t ConvertToHidlResourceCost(
76     const google_camera_hal::CameraResourceCost& hal_cost,
77     CameraResourceCost* hidl_cost) {
78   if (hidl_cost == nullptr) {
79     ALOGE("%s: hidl_cost is nullptr.", __FUNCTION__);
80     return BAD_VALUE;
81   }
82 
83   hidl_cost->resourceCost = hal_cost.resource_cost;
84   hidl_cost->conflictingDevices.resize(hal_cost.conflicting_devices.size());
85 
86   for (uint32_t i = 0; i < hal_cost.conflicting_devices.size(); i++) {
87     hidl_cost->conflictingDevices[i] =
88         "device@" + HidlCameraDevice::kDeviceVersion + "/" +
89         HidlCameraProvider::kProviderName + "/" +
90         std::to_string(hal_cost.conflicting_devices[i]);
91   }
92 
93   return OK;
94 }
95 
ConvertToHidlVendorTagSections(const std::vector<google_camera_hal::VendorTagSection> & hal_sections,hidl_vec<VendorTagSection> * hidl_sections)96 status_t ConvertToHidlVendorTagSections(
97     const std::vector<google_camera_hal::VendorTagSection>& hal_sections,
98     hidl_vec<VendorTagSection>* hidl_sections) {
99   if (hidl_sections == nullptr) {
100     ALOGE("%s: hidl_sections is nullptr.", __FUNCTION__);
101     return BAD_VALUE;
102   }
103 
104   hidl_sections->resize(hal_sections.size());
105   for (uint32_t i = 0; i < hal_sections.size(); i++) {
106     (*hidl_sections)[i].sectionName = hal_sections[i].section_name;
107     (*hidl_sections)[i].tags.resize(hal_sections[i].tags.size());
108 
109     for (uint32_t j = 0; j < hal_sections[i].tags.size(); j++) {
110       (*hidl_sections)[i].tags[j].tagId = hal_sections[i].tags[j].tag_id;
111       (*hidl_sections)[i].tags[j].tagName = hal_sections[i].tags[j].tag_name;
112       status_t res =
113           ConvertToHidlVendorTagType(hal_sections[i].tags[j].tag_type,
114                                      &(*hidl_sections)[i].tags[j].tagType);
115       if (res != OK) {
116         ALOGE("%s: Converting to hidl vendor tag type failed. ", __FUNCTION__);
117         return res;
118       }
119     }
120   }
121 
122   return OK;
123 }
124 
ConvertToHidlStatus(status_t hal_status)125 Status ConvertToHidlStatus(status_t hal_status) {
126   switch (hal_status) {
127     case OK:
128       return Status::OK;
129     case BAD_VALUE:
130       return Status::ILLEGAL_ARGUMENT;
131     case INVALID_OPERATION:
132       return Status::OPERATION_NOT_SUPPORTED;
133     default:
134       return Status::INTERNAL_ERROR;
135   }
136 }
137 
ConvertToHalTemplateType(RequestTemplate hidl_template,google_camera_hal::RequestTemplate * hal_template)138 status_t ConvertToHalTemplateType(
139     RequestTemplate hidl_template,
140     google_camera_hal::RequestTemplate* hal_template) {
141   if (hal_template == nullptr) {
142     ALOGE("%s: hal_template is nullptr.", __FUNCTION__);
143     return BAD_VALUE;
144   }
145 
146   switch (hidl_template) {
147     case RequestTemplate::PREVIEW:
148       *hal_template = google_camera_hal::RequestTemplate::kPreview;
149       break;
150     case RequestTemplate::STILL_CAPTURE:
151       *hal_template = google_camera_hal::RequestTemplate::kStillCapture;
152       break;
153     case RequestTemplate::VIDEO_RECORD:
154       *hal_template = google_camera_hal::RequestTemplate::kVideoRecord;
155       break;
156     case RequestTemplate::VIDEO_SNAPSHOT:
157       *hal_template = google_camera_hal::RequestTemplate::kVideoSnapshot;
158       break;
159     case RequestTemplate::ZERO_SHUTTER_LAG:
160       *hal_template = google_camera_hal::RequestTemplate::kZeroShutterLag;
161       break;
162     case RequestTemplate::MANUAL:
163       *hal_template = google_camera_hal::RequestTemplate::kManual;
164       break;
165     default:
166       ALOGE("%s: Unknown HIDL RequestTemplate: %u", __FUNCTION__,
167             hidl_template);
168       return BAD_VALUE;
169   }
170 
171   return OK;
172 }
173 
ConvertToHidlHalStreamConfig(const std::vector<google_camera_hal::HalStream> & hal_configured_streams,HalStreamConfiguration * hidl_hal_stream_config)174 status_t ConvertToHidlHalStreamConfig(
175     const std::vector<google_camera_hal::HalStream>& hal_configured_streams,
176     HalStreamConfiguration* hidl_hal_stream_config) {
177   if (hidl_hal_stream_config == nullptr) {
178     ALOGE("%s: hidl_hal_stream_config is nullptr.", __FUNCTION__);
179     return BAD_VALUE;
180   }
181 
182   hidl_hal_stream_config->streams.resize(hal_configured_streams.size());
183 
184   for (uint32_t i = 0; i < hal_configured_streams.size(); i++) {
185     if (hal_configured_streams[i].is_physical_camera_stream) {
186       hidl_hal_stream_config->streams[i].physicalCameraId =
187           std::to_string(hal_configured_streams[i].physical_camera_id);
188     }
189 
190     hidl_hal_stream_config->streams[i].v3_3.overrideDataSpace =
191         hal_configured_streams[i].override_data_space;
192 
193     hidl_hal_stream_config->streams[i].v3_3.v3_2.id =
194         hal_configured_streams[i].id;
195 
196     hidl_hal_stream_config->streams[i].v3_3.v3_2.overrideFormat =
197         (::android::hardware::graphics::common::V1_0::PixelFormat)
198             hal_configured_streams[i]
199                 .override_format;
200 
201     hidl_hal_stream_config->streams[i].v3_3.v3_2.producerUsage =
202         hal_configured_streams[i].producer_usage;
203 
204     hidl_hal_stream_config->streams[i].v3_3.v3_2.consumerUsage =
205         hal_configured_streams[i].consumer_usage;
206 
207     hidl_hal_stream_config->streams[i].v3_3.v3_2.maxBuffers =
208         hal_configured_streams[i].max_buffers;
209   }
210 
211   return OK;
212 }
213 
WriteToResultMetadataQueue(camera_metadata_t * metadata,MessageQueue<uint8_t,kSynchronizedReadWrite> * result_metadata_queue)214 status_t WriteToResultMetadataQueue(
215     camera_metadata_t* metadata,
216     MessageQueue<uint8_t, kSynchronizedReadWrite>* result_metadata_queue) {
217   if (result_metadata_queue == nullptr) {
218     return BAD_VALUE;
219   }
220 
221   if (result_metadata_queue->availableToWrite() <= 0) {
222     ALOGW("%s: result_metadata_queue is not available to write", __FUNCTION__);
223     return BAD_VALUE;
224   }
225 
226   uint32_t size = get_camera_metadata_size(metadata);
227   bool success = result_metadata_queue->write(
228       reinterpret_cast<const uint8_t*>(metadata), size);
229   if (!success) {
230     ALOGW("%s: Writing to result metadata queue failed. (size=%u)",
231           __FUNCTION__, size);
232     return INVALID_OPERATION;
233   }
234 
235   return OK;
236 }
237 
238 // Try writing result metadata to result metadata queue. If failed, return
239 // the metadata to the caller in out_hal_metadata.
TryWritingToResultMetadataQueue(std::unique_ptr<google_camera_hal::HalCameraMetadata> hal_metadata,MessageQueue<uint8_t,kSynchronizedReadWrite> * result_metadata_queue,uint64_t * fmq_result_size,std::unique_ptr<google_camera_hal::HalCameraMetadata> * out_hal_metadata)240 status_t TryWritingToResultMetadataQueue(
241     std::unique_ptr<google_camera_hal::HalCameraMetadata> hal_metadata,
242     MessageQueue<uint8_t, kSynchronizedReadWrite>* result_metadata_queue,
243     uint64_t* fmq_result_size,
244     std::unique_ptr<google_camera_hal::HalCameraMetadata>* out_hal_metadata) {
245   if (out_hal_metadata == nullptr) {
246     ALOGE("%s: out_hal_metadata is nullptr.", __FUNCTION__);
247     return BAD_VALUE;
248   }
249 
250   *out_hal_metadata = std::move(hal_metadata);
251 
252   if (fmq_result_size == nullptr) {
253     ALOGE("%s: fmq_result_size is nullptr", __FUNCTION__);
254     return BAD_VALUE;
255   }
256 
257   *fmq_result_size = 0;
258   if (*out_hal_metadata == nullptr) {
259     return OK;
260   }
261 
262   camera_metadata_t* metadata = (*out_hal_metadata)->ReleaseCameraMetadata();
263   // Temporarily use the raw metadata to write to metadata queue.
264   status_t res = WriteToResultMetadataQueue(metadata, result_metadata_queue);
265   *out_hal_metadata = google_camera_hal::HalCameraMetadata::Create(metadata);
266 
267   if (res != OK) {
268     ALOGW("%s: Writing to result metadata queue failed: %s(%d)", __FUNCTION__,
269           strerror(-res), res);
270     return res;
271   }
272 
273   *fmq_result_size = (*out_hal_metadata)->GetCameraMetadataSize();
274   *out_hal_metadata = nullptr;
275   return OK;
276 }
277 
ConverToHidlResultMetadata(MessageQueue<uint8_t,kSynchronizedReadWrite> * result_metadata_queue,std::unique_ptr<google_camera_hal::HalCameraMetadata> hal_metadata,CameraMetadata * hidl_metadata,uint64_t * fmq_result_size)278 status_t ConverToHidlResultMetadata(
279     MessageQueue<uint8_t, kSynchronizedReadWrite>* result_metadata_queue,
280     std::unique_ptr<google_camera_hal::HalCameraMetadata> hal_metadata,
281     CameraMetadata* hidl_metadata, uint64_t* fmq_result_size) {
282   if (TryWritingToResultMetadataQueue(std::move(hal_metadata),
283                                       result_metadata_queue, fmq_result_size,
284                                       &hal_metadata) == OK) {
285     return OK;
286   }
287 
288   // If writing to metadata queue failed, attach the metadata to hidl_metadata.
289   if (hidl_metadata == nullptr) {
290     ALOGE("%s: hidl_metadata is nullptr", __FUNCTION__);
291     return BAD_VALUE;
292   }
293 
294   uint32_t metadata_size = hal_metadata->GetCameraMetadataSize();
295   hidl_metadata->setToExternal(
296       reinterpret_cast<uint8_t*>(hal_metadata->ReleaseCameraMetadata()),
297       metadata_size, /*shouldOwn=*/true);
298 
299   return OK;
300 }
301 
ConvertToHidlBufferStatus(google_camera_hal::BufferStatus hal_status,BufferStatus * hidl_status)302 status_t ConvertToHidlBufferStatus(google_camera_hal::BufferStatus hal_status,
303                                    BufferStatus* hidl_status) {
304   if (hidl_status == nullptr) {
305     ALOGE("%s: hidl_status is nullptr.", __FUNCTION__);
306     return BAD_VALUE;
307   }
308 
309   switch (hal_status) {
310     case google_camera_hal::BufferStatus::kOk:
311       *hidl_status = BufferStatus::OK;
312       break;
313     case google_camera_hal::BufferStatus::kError:
314       *hidl_status = BufferStatus::ERROR;
315       break;
316     default:
317       ALOGE("%s: Unknown HAL buffer status: %u", __FUNCTION__, hal_status);
318       return BAD_VALUE;
319   }
320 
321   return OK;
322 }
323 
ConvertToHidlStreamBuffer(const google_camera_hal::StreamBuffer & hal_buffer,StreamBuffer * hidl_buffer)324 status_t ConvertToHidlStreamBuffer(
325     const google_camera_hal::StreamBuffer& hal_buffer,
326     StreamBuffer* hidl_buffer) {
327   if (hidl_buffer == nullptr) {
328     ALOGE("%s: hidl_buffer is nullptr", __FUNCTION__);
329     return BAD_VALUE;
330   }
331 
332   hidl_buffer->streamId = hal_buffer.stream_id;
333   hidl_buffer->bufferId = hal_buffer.buffer_id;
334   hidl_buffer->buffer = nullptr;
335 
336   status_t res =
337       ConvertToHidlBufferStatus(hal_buffer.status, &hidl_buffer->status);
338   if (res != OK) {
339     ALOGE("%s: Converting to HIDL buffer status failed: %s(%d)", __FUNCTION__,
340           strerror(-res), res);
341     return res;
342   }
343 
344   hidl_buffer->acquireFence = nullptr;
345   hidl_buffer->releaseFence = hal_buffer.release_fence;
346   return OK;
347 }
348 
ConvertToHidlCaptureResult_V3_2(MessageQueue<uint8_t,kSynchronizedReadWrite> * result_metadata_queue,google_camera_hal::CaptureResult * hal_result,device::V3_2::CaptureResult * hidl_result)349 status_t ConvertToHidlCaptureResult_V3_2(
350     MessageQueue<uint8_t, kSynchronizedReadWrite>* result_metadata_queue,
351     google_camera_hal::CaptureResult* hal_result,
352     device::V3_2::CaptureResult* hidl_result) {
353   if (hidl_result == nullptr) {
354     ALOGE("%s: hidl_result is nullptr.", __FUNCTION__);
355     return BAD_VALUE;
356   }
357 
358   if (hal_result == nullptr) {
359     ALOGE("%s: hal_result is nullptr.", __FUNCTION__);
360     return BAD_VALUE;
361   }
362 
363   hidl_result->frameNumber = hal_result->frame_number;
364 
365   status_t res = ConverToHidlResultMetadata(
366       result_metadata_queue, std::move(hal_result->result_metadata),
367       &hidl_result->result, &hidl_result->fmqResultSize);
368   if (res != OK) {
369     ALOGE("%s: Converting to HIDL result metadata failed: %s(%d).",
370           __FUNCTION__, strerror(-res), res);
371     return res;
372   }
373 
374   hidl_result->outputBuffers.resize(hal_result->output_buffers.size());
375   for (uint32_t i = 0; i < hidl_result->outputBuffers.size(); i++) {
376     res = ConvertToHidlStreamBuffer(hal_result->output_buffers[i],
377                                     &hidl_result->outputBuffers[i]);
378     if (res != OK) {
379       ALOGE("%s: Converting to HIDL output stream buffer failed: %s(%d)",
380             __FUNCTION__, strerror(-res), res);
381       return res;
382     }
383   }
384 
385   uint32_t num_input_buffers = hal_result->input_buffers.size();
386   if (num_input_buffers > 0) {
387     if (num_input_buffers > 1) {
388       ALOGW("%s: HAL result should not have more than 1 input buffer. (=%u)",
389             __FUNCTION__, num_input_buffers);
390     }
391 
392     res = ConvertToHidlStreamBuffer(hal_result->input_buffers[0],
393                                     &hidl_result->inputBuffer);
394     if (res != OK) {
395       ALOGE("%s: Converting to HIDL input stream buffer failed: %s(%d)",
396             __FUNCTION__, strerror(-res), res);
397       return res;
398     }
399   } else {
400     hidl_result->inputBuffer.streamId = -1;
401   }
402 
403   hidl_result->partialResult = hal_result->partial_result;
404   return OK;
405 }
406 
ConvertToHidlCaptureResult(MessageQueue<uint8_t,kSynchronizedReadWrite> * result_metadata_queue,std::unique_ptr<google_camera_hal::CaptureResult> hal_result,CaptureResult * hidl_result)407 status_t ConvertToHidlCaptureResult(
408     MessageQueue<uint8_t, kSynchronizedReadWrite>* result_metadata_queue,
409     std::unique_ptr<google_camera_hal::CaptureResult> hal_result,
410     CaptureResult* hidl_result) {
411   if (hidl_result == nullptr) {
412     ALOGE("%s: hidl_result is nullptr.", __FUNCTION__);
413     return BAD_VALUE;
414   }
415 
416   if (hal_result == nullptr) {
417     ALOGE("%s: hal_result is nullptr.", __FUNCTION__);
418     return BAD_VALUE;
419   }
420 
421   status_t res = ConvertToHidlCaptureResult_V3_2(
422       result_metadata_queue, hal_result.get(), &hidl_result->v3_2);
423   if (res != OK) {
424     ALOGE("%s: Converting to V3.2 HIDL result failed: %s(%d).", __FUNCTION__,
425           strerror(-res), res);
426     return res;
427   }
428 
429   uint32_t num_physical_metadata = hal_result->physical_metadata.size();
430   hidl_result->physicalCameraMetadata.resize(num_physical_metadata);
431 
432   for (uint32_t i = 0; i < num_physical_metadata; i++) {
433     hidl_result->physicalCameraMetadata[i].physicalCameraId =
434         std::to_string(hal_result->physical_metadata[i].physical_camera_id);
435 
436     res = ConverToHidlResultMetadata(
437         result_metadata_queue,
438         std::move(hal_result->physical_metadata[i].metadata),
439         &hidl_result->physicalCameraMetadata[i].metadata,
440         &hidl_result->physicalCameraMetadata[i].fmqMetadataSize);
441     if (res != OK) {
442       ALOGE("%s: Converting to HIDL physical metadata failed: %s(%d).",
443             __FUNCTION__, strerror(-res), res);
444       return res;
445     }
446   }
447 
448   return OK;
449 }
450 
ConvertToHidlErrorMessage(const google_camera_hal::ErrorMessage & hal_error,ErrorMsg * hidl_error)451 status_t ConvertToHidlErrorMessage(
452     const google_camera_hal::ErrorMessage& hal_error, ErrorMsg* hidl_error) {
453   if (hidl_error == nullptr) {
454     ALOGE("%s: hidl_error is nullptr.", __FUNCTION__);
455     return BAD_VALUE;
456   }
457 
458   hidl_error->frameNumber = hal_error.frame_number;
459   hidl_error->errorStreamId = hal_error.error_stream_id;
460 
461   switch (hal_error.error_code) {
462     case google_camera_hal::ErrorCode::kErrorDevice:
463       hidl_error->errorCode = ErrorCode::ERROR_DEVICE;
464       break;
465     case google_camera_hal::ErrorCode::kErrorRequest:
466       hidl_error->errorCode = ErrorCode::ERROR_REQUEST;
467       break;
468     case google_camera_hal::ErrorCode::kErrorResult:
469       hidl_error->errorCode = ErrorCode::ERROR_RESULT;
470       break;
471     case google_camera_hal::ErrorCode::kErrorBuffer:
472       hidl_error->errorCode = ErrorCode::ERROR_BUFFER;
473       break;
474     default:
475       ALOGE("%s: Unknown error code: %u", __FUNCTION__, hal_error.error_code);
476       return BAD_VALUE;
477   }
478 
479   return OK;
480 }
481 
ConvertToHidlShutterMessage(const google_camera_hal::ShutterMessage & hal_shutter,ShutterMsg * hidl_shutter)482 status_t ConvertToHidlShutterMessage(
483     const google_camera_hal::ShutterMessage& hal_shutter,
484     ShutterMsg* hidl_shutter) {
485   if (hidl_shutter == nullptr) {
486     ALOGE("%s: hidl_shutter is nullptr.", __FUNCTION__);
487     return BAD_VALUE;
488   }
489 
490   hidl_shutter->frameNumber = hal_shutter.frame_number;
491   hidl_shutter->timestamp = hal_shutter.timestamp_ns;
492   return OK;
493 }
494 
ConverToHidlNotifyMessage(const google_camera_hal::NotifyMessage & hal_message,NotifyMsg * hidl_message)495 status_t ConverToHidlNotifyMessage(
496     const google_camera_hal::NotifyMessage& hal_message,
497     NotifyMsg* hidl_message) {
498   if (hidl_message == nullptr) {
499     ALOGE("%s: hidl_message is nullptr.", __FUNCTION__);
500     return BAD_VALUE;
501   }
502 
503   status_t res;
504   switch (hal_message.type) {
505     case google_camera_hal::MessageType::kError:
506       hidl_message->type = MsgType::ERROR;
507       res = ConvertToHidlErrorMessage(hal_message.message.error,
508                                       &hidl_message->msg.error);
509       if (res != OK) {
510         ALOGE("%s: Converting to HIDL error message failed: %s(%d)",
511               __FUNCTION__, strerror(-res), res);
512         return res;
513       }
514       break;
515     case google_camera_hal::MessageType::kShutter:
516       hidl_message->type = MsgType::SHUTTER;
517       res = ConvertToHidlShutterMessage(hal_message.message.shutter,
518                                         &hidl_message->msg.shutter);
519       if (res != OK) {
520         ALOGE("%s: Converting to HIDL shutter message failed: %s(%d)",
521               __FUNCTION__, strerror(-res), res);
522         return res;
523       }
524       break;
525     default:
526       ALOGE("%s: Unknown message type: %u", __FUNCTION__, hal_message.type);
527       return BAD_VALUE;
528   }
529 
530   return OK;
531 }
532 
ConvertToHidlCameraDeviceStatus(google_camera_hal::CameraDeviceStatus hal_camera_device_status,CameraDeviceStatus * hidl_camera_device_status)533 status_t ConvertToHidlCameraDeviceStatus(
534     google_camera_hal::CameraDeviceStatus hal_camera_device_status,
535     CameraDeviceStatus* hidl_camera_device_status) {
536   if (hidl_camera_device_status == nullptr) {
537     ALOGE("%s: hidl_camera_device_status is nullptr.", __FUNCTION__);
538     return BAD_VALUE;
539   }
540 
541   switch (hal_camera_device_status) {
542     case google_camera_hal::CameraDeviceStatus::kNotPresent:
543       *hidl_camera_device_status = CameraDeviceStatus::NOT_PRESENT;
544       break;
545     case google_camera_hal::CameraDeviceStatus::kPresent:
546       *hidl_camera_device_status = CameraDeviceStatus::PRESENT;
547       break;
548     case google_camera_hal::CameraDeviceStatus::kEnumerating:
549       *hidl_camera_device_status = CameraDeviceStatus::ENUMERATING;
550       break;
551     default:
552       ALOGE("%s: Unknown HAL camera device status: %u", __FUNCTION__,
553             hal_camera_device_status);
554       return BAD_VALUE;
555   }
556 
557   return OK;
558 }
559 
ConvertToHidlTorchModeStatus(google_camera_hal::TorchModeStatus hal_torch_status,TorchModeStatus * hidl_torch_status)560 status_t ConvertToHidlTorchModeStatus(
561     google_camera_hal::TorchModeStatus hal_torch_status,
562     TorchModeStatus* hidl_torch_status) {
563   if (hidl_torch_status == nullptr) {
564     ALOGE("%s: hidl_torch_status is nullptr.", __FUNCTION__);
565     return BAD_VALUE;
566   }
567 
568   switch (hal_torch_status) {
569     case google_camera_hal::TorchModeStatus::kNotAvailable:
570       *hidl_torch_status = TorchModeStatus::NOT_AVAILABLE;
571       break;
572     case google_camera_hal::TorchModeStatus::kAvailableOff:
573       *hidl_torch_status = TorchModeStatus::AVAILABLE_OFF;
574       break;
575     case google_camera_hal::TorchModeStatus::kAvailableOn:
576       *hidl_torch_status = TorchModeStatus::AVAILABLE_ON;
577       break;
578     default:
579       ALOGE("%s: Unknown HAL torch mode status: %u", __FUNCTION__,
580             hal_torch_status);
581       return BAD_VALUE;
582   }
583 
584   return OK;
585 }
586 
ConvertToHidlBufferRequest(const std::vector<google_camera_hal::BufferRequest> & hal_buffer_requests,hidl_vec<BufferRequest> * hidl_buffer_requests)587 status_t ConvertToHidlBufferRequest(
588     const std::vector<google_camera_hal::BufferRequest>& hal_buffer_requests,
589     hidl_vec<BufferRequest>* hidl_buffer_requests) {
590   if (hidl_buffer_requests == nullptr) {
591     ALOGE("%s: hidl_buffer_request is nullptr.", __FUNCTION__);
592     return BAD_VALUE;
593   }
594 
595   hidl_buffer_requests->resize(hal_buffer_requests.size());
596   for (uint32_t i = 0; i < hal_buffer_requests.size(); i++) {
597     (*hidl_buffer_requests)[i].streamId = hal_buffer_requests[i].stream_id;
598     (*hidl_buffer_requests)[i].numBuffersRequested =
599         hal_buffer_requests[i].num_buffers_requested;
600   }
601   return OK;
602 }
603 
ConvertToHalBufferStatus(BufferStatus hidl_status,google_camera_hal::BufferStatus * hal_status)604 status_t ConvertToHalBufferStatus(BufferStatus hidl_status,
605                                   google_camera_hal::BufferStatus* hal_status) {
606   if (hal_status == nullptr) {
607     ALOGE("%s: hal_status is nullptr.", __FUNCTION__);
608     return BAD_VALUE;
609   }
610 
611   switch (hidl_status) {
612     case BufferStatus::OK:
613       *hal_status = google_camera_hal::BufferStatus::kOk;
614       break;
615     case BufferStatus::ERROR:
616       *hal_status = google_camera_hal::BufferStatus::kError;
617       break;
618     default:
619       ALOGE("%s: Unknown HIDL buffer status: %u", __FUNCTION__, hidl_status);
620       return BAD_VALUE;
621   }
622 
623   return OK;
624 }
625 
ConvertToHalStreamBuffer(const StreamBuffer & hidl_buffer,google_camera_hal::StreamBuffer * hal_buffer)626 status_t ConvertToHalStreamBuffer(const StreamBuffer& hidl_buffer,
627                                   google_camera_hal::StreamBuffer* hal_buffer) {
628   if (hal_buffer == nullptr) {
629     ALOGE("%s: hal_buffer is nullptr.", __FUNCTION__);
630     return BAD_VALUE;
631   }
632 
633   hal_buffer->stream_id = hidl_buffer.streamId;
634   hal_buffer->buffer_id = hidl_buffer.bufferId;
635   hal_buffer->buffer = hidl_buffer.buffer.getNativeHandle();
636 
637   status_t res =
638       ConvertToHalBufferStatus(hidl_buffer.status, &hal_buffer->status);
639   if (res != OK) {
640     ALOGE("%s: Converting to HAL buffer status failed: %s(%d)", __FUNCTION__,
641           strerror(-res), res);
642     return res;
643   }
644 
645   hal_buffer->acquire_fence = hidl_buffer.acquireFence.getNativeHandle();
646   hal_buffer->release_fence = hidl_buffer.releaseFence.getNativeHandle();
647 
648   return OK;
649 }
650 
ConvertToHalMetadata(uint32_t message_queue_setting_size,MessageQueue<uint8_t,kSynchronizedReadWrite> * request_metadata_queue,const CameraMetadata & request_settings,std::unique_ptr<google_camera_hal::HalCameraMetadata> * hal_metadata)651 status_t ConvertToHalMetadata(
652     uint32_t message_queue_setting_size,
653     MessageQueue<uint8_t, kSynchronizedReadWrite>* request_metadata_queue,
654     const CameraMetadata& request_settings,
655     std::unique_ptr<google_camera_hal::HalCameraMetadata>* hal_metadata) {
656   if (hal_metadata == nullptr) {
657     ALOGE("%s: hal_metadata is nullptr.", __FUNCTION__);
658     return BAD_VALUE;
659   }
660 
661   const camera_metadata_t* metadata = nullptr;
662   CameraMetadata metadata_queue_settings;
663 
664   if (message_queue_setting_size == 0) {
665     // Use the settings in the request.
666     if (request_settings.size() != 0) {
667       metadata =
668           reinterpret_cast<const camera_metadata_t*>(request_settings.data());
669     }
670   } else {
671     // Read the settings from request metadata queue.
672     if (request_metadata_queue == nullptr) {
673       ALOGE("%s: request_metadata_queue is nullptr", __FUNCTION__);
674       return BAD_VALUE;
675     }
676 
677     metadata_queue_settings.resize(message_queue_setting_size);
678     bool success = request_metadata_queue->read(metadata_queue_settings.data(),
679                                                 message_queue_setting_size);
680     if (!success) {
681       ALOGE("%s: Failed to read from request metadata queue.", __FUNCTION__);
682       return BAD_VALUE;
683     }
684 
685     metadata = reinterpret_cast<const camera_metadata_t*>(
686         metadata_queue_settings.data());
687   }
688 
689   if (metadata == nullptr) {
690     *hal_metadata = nullptr;
691     return OK;
692   }
693 
694   *hal_metadata = google_camera_hal::HalCameraMetadata::Clone(metadata);
695   return OK;
696 }
697 
ConvertToHalCaptureRequest(const CaptureRequest & hidl_request,MessageQueue<uint8_t,kSynchronizedReadWrite> * request_metadata_queue,google_camera_hal::CaptureRequest * hal_request)698 status_t ConvertToHalCaptureRequest(
699     const CaptureRequest& hidl_request,
700     MessageQueue<uint8_t, kSynchronizedReadWrite>* request_metadata_queue,
701     google_camera_hal::CaptureRequest* hal_request) {
702   if (hal_request == nullptr) {
703     ALOGE("%s: hal_request is nullptr.", __FUNCTION__);
704     return BAD_VALUE;
705   }
706 
707   hal_request->frame_number = hidl_request.v3_2.frameNumber;
708 
709   status_t res = ConvertToHalMetadata(
710       hidl_request.v3_2.fmqSettingsSize, request_metadata_queue,
711       hidl_request.v3_2.settings, &hal_request->settings);
712   if (res != OK) {
713     ALOGE("%s: Converting metadata failed: %s(%d)", __FUNCTION__,
714           strerror(-res), res);
715     return res;
716   }
717 
718   google_camera_hal::StreamBuffer hal_buffer = {};
719   if (hidl_request.v3_2.inputBuffer.buffer != nullptr) {
720     res = ConvertToHalStreamBuffer(hidl_request.v3_2.inputBuffer, &hal_buffer);
721     if (res != OK) {
722       ALOGE("%s: Converting hal stream buffer failed: %s(%d)", __FUNCTION__,
723             strerror(-res), res);
724       return res;
725     }
726 
727     hal_request->input_buffers.push_back(hal_buffer);
728   }
729 
730   for (auto& buffer : hidl_request.v3_2.outputBuffers) {
731     hal_buffer = {};
732     status_t res = ConvertToHalStreamBuffer(buffer, &hal_buffer);
733     if (res != OK) {
734       ALOGE("%s: Converting hal stream buffer failed: %s(%d)", __FUNCTION__,
735             strerror(-res), res);
736       return res;
737     }
738 
739     hal_request->output_buffers.push_back(hal_buffer);
740   }
741 
742   for (auto hidl_physical_settings : hidl_request.physicalCameraSettings) {
743     std::unique_ptr<google_camera_hal::HalCameraMetadata> hal_physical_settings;
744     res = ConvertToHalMetadata(
745         hidl_physical_settings.fmqSettingsSize, request_metadata_queue,
746         hidl_physical_settings.settings, &hal_physical_settings);
747     if (res != OK) {
748       ALOGE("%s: Converting to HAL metadata failed: %s(%d)", __FUNCTION__,
749             strerror(-res), res);
750       return res;
751     }
752 
753     uint32_t camera_id = std::stoul(hidl_physical_settings.physicalCameraId);
754     hal_request->physical_camera_settings.emplace(
755         camera_id, std::move(hal_physical_settings));
756   }
757 
758   return OK;
759 }
760 
ConvertToHalBufferCaches(const hidl_vec<BufferCache> & hidl_buffer_caches,std::vector<google_camera_hal::BufferCache> * hal_buffer_caches)761 status_t ConvertToHalBufferCaches(
762     const hidl_vec<BufferCache>& hidl_buffer_caches,
763     std::vector<google_camera_hal::BufferCache>* hal_buffer_caches) {
764   if (hal_buffer_caches == nullptr) {
765     ALOGE("%s: hal_buffer_caches is nullptr.", __FUNCTION__);
766     return BAD_VALUE;
767   }
768 
769   for (auto hidl_cache : hidl_buffer_caches) {
770     google_camera_hal::BufferCache hal_cache;
771     hal_cache.stream_id = hidl_cache.streamId;
772     hal_cache.buffer_id = hidl_cache.bufferId;
773 
774     hal_buffer_caches->push_back(hal_cache);
775   }
776 
777   return OK;
778 }
779 
ConvertToHalStreamConfigurationMode(StreamConfigurationMode hidl_mode,google_camera_hal::StreamConfigurationMode * hal_mode)780 status_t ConvertToHalStreamConfigurationMode(
781     StreamConfigurationMode hidl_mode,
782     google_camera_hal::StreamConfigurationMode* hal_mode) {
783   if (hal_mode == nullptr) {
784     ALOGE("%s: hal_mode is nullptr.", __FUNCTION__);
785     return BAD_VALUE;
786   }
787 
788   switch (hidl_mode) {
789     case StreamConfigurationMode::NORMAL_MODE:
790       *hal_mode = google_camera_hal::StreamConfigurationMode::kNormal;
791       break;
792     case StreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE:
793       *hal_mode =
794           google_camera_hal::StreamConfigurationMode::kConstrainedHighSpeed;
795       break;
796     default:
797       ALOGE("%s: Unknown configuration mode %u", __FUNCTION__, hidl_mode);
798       return BAD_VALUE;
799   }
800 
801   return OK;
802 }
803 
ConverToHalStreamConfig(const StreamConfiguration & hidl_stream_config,google_camera_hal::StreamConfiguration * hal_stream_config)804 status_t ConverToHalStreamConfig(
805     const StreamConfiguration& hidl_stream_config,
806     google_camera_hal::StreamConfiguration* hal_stream_config) {
807   if (hal_stream_config == nullptr) {
808     ALOGE("%s: hal_stream_config is nullptr.", __FUNCTION__);
809     return BAD_VALUE;
810   }
811 
812   status_t res;
813 
814   for (auto hidl_stream : hidl_stream_config.v3_4.streams) {
815     google_camera_hal::Stream hal_stream;
816     res = ConvertToHalStream(hidl_stream, &hal_stream);
817     if (res != OK) {
818       ALOGE("%s: Converting to HAL stream failed: %s(%d)", __FUNCTION__,
819             strerror(-res), res);
820       return res;
821     }
822 
823     hal_stream_config->streams.push_back(hal_stream);
824   }
825 
826   res = ConvertToHalStreamConfigurationMode(
827       hidl_stream_config.v3_4.operationMode, &hal_stream_config->operation_mode);
828   if (res != OK) {
829     ALOGE("%s: Converting to HAL opeation mode failed: %s(%d)", __FUNCTION__,
830           strerror(-res), res);
831     return res;
832   }
833 
834   res = ConvertToHalMetadata(0, nullptr, hidl_stream_config.v3_4.sessionParams,
835                              &hal_stream_config->session_params);
836   if (res != OK) {
837     ALOGE("%s: Converting to HAL metadata failed: %s(%d)", __FUNCTION__,
838           strerror(-res), res);
839     return res;
840   }
841 
842   hal_stream_config->stream_config_counter =
843       hidl_stream_config.streamConfigCounter;
844 
845   return OK;
846 }
847 
ConverToHalStreamConfig(const device::V3_4::StreamConfiguration & hidl_stream_config,google_camera_hal::StreamConfiguration * hal_stream_config)848 status_t ConverToHalStreamConfig(
849     const device::V3_4::StreamConfiguration& hidl_stream_config,
850     google_camera_hal::StreamConfiguration* hal_stream_config) {
851   if (hal_stream_config == nullptr) {
852     ALOGE("%s: hal_stream_config is nullptr.", __FUNCTION__);
853     return BAD_VALUE;
854   }
855 
856   status_t res;
857   for (auto hidl_stream : hidl_stream_config.streams) {
858     google_camera_hal::Stream hal_stream;
859     res = ConvertToHalStream(hidl_stream, &hal_stream);
860     if (res != OK) {
861       ALOGE("%s: Converting to HAL stream failed: %s(%d)", __FUNCTION__,
862             strerror(-res), res);
863       return res;
864     }
865     hal_stream_config->streams.push_back(hal_stream);
866   }
867 
868   res = ConvertToHalStreamConfigurationMode(hidl_stream_config.operationMode,
869                                             &hal_stream_config->operation_mode);
870   if (res != OK) {
871     ALOGE("%s: Converting to HAL opeation mode failed: %s(%d)", __FUNCTION__,
872           strerror(-res), res);
873     return res;
874   }
875 
876   res = ConvertToHalMetadata(0, nullptr, hidl_stream_config.sessionParams,
877                              &hal_stream_config->session_params);
878   if (res != OK) {
879     ALOGE("%s: Converting to HAL metadata failed: %s(%d)", __FUNCTION__,
880           strerror(-res), res);
881     return res;
882   }
883 
884   return OK;
885 }
886 
ConvertToHalStreamType(StreamType hidl_stream_type,google_camera_hal::StreamType * hal_stream_type)887 status_t ConvertToHalStreamType(StreamType hidl_stream_type,
888                                 google_camera_hal::StreamType* hal_stream_type) {
889   if (hal_stream_type == nullptr) {
890     ALOGE("%s: hal_stream_type is nullptr.", __FUNCTION__);
891     return BAD_VALUE;
892   }
893 
894   switch (hidl_stream_type) {
895     case StreamType::OUTPUT:
896       *hal_stream_type = google_camera_hal::StreamType::kOutput;
897       break;
898     case StreamType::INPUT:
899       *hal_stream_type = google_camera_hal::StreamType::kInput;
900       break;
901     default:
902       ALOGE("%s: Unknown stream type: %u", __FUNCTION__, hidl_stream_type);
903       return BAD_VALUE;
904   }
905 
906   return OK;
907 }
908 
ConvertToHalStreamRotation(StreamRotation hidl_stream_rotation,google_camera_hal::StreamRotation * hal_stream_rotation)909 status_t ConvertToHalStreamRotation(
910     StreamRotation hidl_stream_rotation,
911     google_camera_hal::StreamRotation* hal_stream_rotation) {
912   if (hal_stream_rotation == nullptr) {
913     ALOGE("%s: hal_stream_rotation is nullptr.", __FUNCTION__);
914     return BAD_VALUE;
915   }
916 
917   switch (hidl_stream_rotation) {
918     case StreamRotation::ROTATION_0:
919       *hal_stream_rotation = google_camera_hal::StreamRotation::kRotation0;
920       break;
921     case StreamRotation::ROTATION_90:
922       *hal_stream_rotation = google_camera_hal::StreamRotation::kRotation90;
923       break;
924     case StreamRotation::ROTATION_180:
925       *hal_stream_rotation = google_camera_hal::StreamRotation::kRotation180;
926       break;
927     case StreamRotation::ROTATION_270:
928       *hal_stream_rotation = google_camera_hal::StreamRotation::kRotation270;
929       break;
930     default:
931       ALOGE("%s: Unknown stream rotation: %u", __FUNCTION__,
932             hidl_stream_rotation);
933       return BAD_VALUE;
934   }
935 
936   return OK;
937 }
938 
ConvertToHalStream(const Stream & hidl_stream,google_camera_hal::Stream * hal_stream)939 status_t ConvertToHalStream(const Stream& hidl_stream,
940                             google_camera_hal::Stream* hal_stream) {
941   if (hal_stream == nullptr) {
942     ALOGE("%s: hal_stream is nullptr.", __FUNCTION__);
943     return BAD_VALUE;
944   }
945 
946   *hal_stream = {};
947 
948   hal_stream->id = hidl_stream.v3_2.id;
949 
950   status_t res = ConvertToHalStreamType(hidl_stream.v3_2.streamType,
951                                         &hal_stream->stream_type);
952   if (res != OK) {
953     ALOGE("%s: Converting to HAL stream type failed: %s(%d)", __FUNCTION__,
954           strerror(-res), res);
955     return res;
956   }
957 
958   hal_stream->width = hidl_stream.v3_2.width;
959   hal_stream->height = hidl_stream.v3_2.height;
960   hal_stream->format = (android_pixel_format_t)hidl_stream.v3_2.format;
961   hal_stream->usage = (uint64_t)hidl_stream.v3_2.usage;
962   hal_stream->data_space = (android_dataspace_t)hidl_stream.v3_2.dataSpace;
963 
964   res = ConvertToHalStreamRotation(hidl_stream.v3_2.rotation,
965                                    &hal_stream->rotation);
966   if (res != OK) {
967     ALOGE("%s: Converting to HAL stream rotation failed: %s(%d)", __FUNCTION__,
968           strerror(-res), res);
969     return res;
970   }
971 
972   if (hidl_stream.physicalCameraId.empty()) {
973     hal_stream->is_physical_camera_stream = false;
974   } else {
975     hal_stream->is_physical_camera_stream = true;
976     hal_stream->physical_camera_id = std::stoul(hidl_stream.physicalCameraId);
977   }
978 
979   hal_stream->buffer_size = hidl_stream.bufferSize;
980 
981   return OK;
982 }
983 
ConvertToHalTorchMode(TorchMode hidl_torch_mode,google_camera_hal::TorchMode * hal_torch_mode)984 status_t ConvertToHalTorchMode(TorchMode hidl_torch_mode,
985                                google_camera_hal::TorchMode* hal_torch_mode) {
986   if (hal_torch_mode == nullptr) {
987     ALOGE("%s: hal_torch_mode is nullptr.", __FUNCTION__);
988     return BAD_VALUE;
989   }
990 
991   switch (hidl_torch_mode) {
992     case TorchMode::ON:
993       *hal_torch_mode = google_camera_hal::TorchMode::kOn;
994       break;
995     case TorchMode::OFF:
996       *hal_torch_mode = google_camera_hal::TorchMode::kOff;
997       break;
998     default:
999       ALOGE("%s: Unknown torch mode: %u", __FUNCTION__, hidl_torch_mode);
1000       return BAD_VALUE;
1001   }
1002 
1003   return OK;
1004 }
1005 
ConvertToHalBufferRequestStatus(const BufferRequestStatus & hidl_buffer_request_status,google_camera_hal::BufferRequestStatus * hal_buffer_request_status)1006 status_t ConvertToHalBufferRequestStatus(
1007     const BufferRequestStatus& hidl_buffer_request_status,
1008     google_camera_hal::BufferRequestStatus* hal_buffer_request_status) {
1009   if (hal_buffer_request_status == nullptr) {
1010     ALOGE("%s: hal_buffer_request_status is nullptr.", __FUNCTION__);
1011     return BAD_VALUE;
1012   }
1013 
1014   switch (hidl_buffer_request_status) {
1015     case BufferRequestStatus::OK:
1016       *hal_buffer_request_status = google_camera_hal::BufferRequestStatus::kOk;
1017       break;
1018     case BufferRequestStatus::FAILED_PARTIAL:
1019       *hal_buffer_request_status =
1020           google_camera_hal::BufferRequestStatus::kFailedPartial;
1021       break;
1022     case BufferRequestStatus::FAILED_CONFIGURING:
1023       *hal_buffer_request_status =
1024           google_camera_hal::BufferRequestStatus::kFailedConfiguring;
1025       break;
1026     case BufferRequestStatus::FAILED_ILLEGAL_ARGUMENTS:
1027       *hal_buffer_request_status =
1028           google_camera_hal::BufferRequestStatus::kFailedIllegalArgs;
1029       break;
1030     case BufferRequestStatus::FAILED_UNKNOWN:
1031       *hal_buffer_request_status =
1032           google_camera_hal::BufferRequestStatus::kFailedUnknown;
1033       break;
1034     default:
1035       ALOGE("%s: Failed unknown buffer request error code %d", __FUNCTION__,
1036             hidl_buffer_request_status);
1037       return BAD_VALUE;
1038   }
1039 
1040   return OK;
1041 }
1042 
ConvertToHalBufferReturnStatus(const StreamBufferRet & hidl_stream_buffer_return,google_camera_hal::BufferReturn * hal_buffer_return)1043 status_t ConvertToHalBufferReturnStatus(
1044     const StreamBufferRet& hidl_stream_buffer_return,
1045     google_camera_hal::BufferReturn* hal_buffer_return) {
1046   if (hal_buffer_return == nullptr) {
1047     ALOGE("%s: hal_buffer_return is nullptr.", __FUNCTION__);
1048     return BAD_VALUE;
1049   }
1050 
1051   if (hidl_stream_buffer_return.val.getDiscriminator() ==
1052       StreamBuffersVal::hidl_discriminator::error) {
1053     switch (hidl_stream_buffer_return.val.error()) {
1054       case StreamBufferRequestError::NO_BUFFER_AVAILABLE:
1055         hal_buffer_return->val.error =
1056             google_camera_hal::StreamBufferRequestError::kNoBufferAvailable;
1057         break;
1058       case StreamBufferRequestError::MAX_BUFFER_EXCEEDED:
1059         hal_buffer_return->val.error =
1060             google_camera_hal::StreamBufferRequestError::kMaxBufferExceeded;
1061         break;
1062       case StreamBufferRequestError::STREAM_DISCONNECTED:
1063         hal_buffer_return->val.error =
1064             google_camera_hal::StreamBufferRequestError::kStreamDisconnected;
1065         break;
1066       case StreamBufferRequestError::UNKNOWN_ERROR:
1067         hal_buffer_return->val.error =
1068             google_camera_hal::StreamBufferRequestError::kUnknownError;
1069         break;
1070       default:
1071         ALOGE("%s: Unknown StreamBufferRequestError %d", __FUNCTION__,
1072               hidl_stream_buffer_return.val.error());
1073         return BAD_VALUE;
1074     }
1075   } else {
1076     hal_buffer_return->val.error =
1077         google_camera_hal::StreamBufferRequestError::kOk;
1078   }
1079 
1080   return OK;
1081 }
1082 }  // namespace hidl_utils
1083 }  // namespace implementation
1084 }  // namespace camera
1085 }  // namespace hardware
1086 }  // namespace android
1087