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 #ifndef HARDWARE_GOOGLE_CAMERA_HAL_HIDL_SERVICE_HIDL_UTILS_H_
18 #define HARDWARE_GOOGLE_CAMERA_HAL_HIDL_SERVICE_HIDL_UTILS_H_
19 
20 #include <android/hardware/camera/common/1.0/types.h>
21 #include <android/hardware/camera/device/3.7/ICameraDeviceSession.h>
22 #include <fmq/MessageQueue.h>
23 #include <hal_types.h>
24 #include <hidl/HidlSupport.h>
25 #include <memory>
26 #include "hidl_camera_provider.h"
27 
28 namespace android {
29 namespace hardware {
30 namespace camera {
31 namespace implementation {
32 namespace hidl_utils {
33 
34 using ::android::hardware::hidl_vec;
35 using ::android::hardware::camera::common::V1_0::CameraDeviceStatus;
36 using ::android::hardware::camera::common::V1_0::CameraMetadataType;
37 using ::android::hardware::camera::common::V1_0::CameraResourceCost;
38 using ::android::hardware::camera::common::V1_0::Status;
39 using ::android::hardware::camera::common::V1_0::TorchMode;
40 using ::android::hardware::camera::common::V1_0::TorchModeStatus;
41 using ::android::hardware::camera::common::V1_0::VendorTagSection;
42 using ::android::hardware::camera::device::V3_2::BufferCache;
43 using ::android::hardware::camera::device::V3_2::BufferStatus;
44 using ::android::hardware::camera::device::V3_2::CameraMetadata;
45 using ::android::hardware::camera::device::V3_2::NotifyMsg;
46 using ::android::hardware::camera::device::V3_2::RequestTemplate;
47 using ::android::hardware::camera::device::V3_2::StreamBuffer;
48 using ::android::hardware::camera::device::V3_2::StreamConfigurationMode;
49 using ::android::hardware::camera::device::V3_2::StreamRotation;
50 using ::android::hardware::camera::device::V3_2::StreamType;
51 using ::android::hardware::camera::device::V3_4::CaptureResult;
52 using ::android::hardware::camera::device::V3_4::Stream;
53 using ::android::hardware::camera::device::V3_5::BufferRequest;
54 using ::android::hardware::camera::device::V3_5::BufferRequestStatus;
55 using ::android::hardware::camera::device::V3_5::StreamBufferRequestError;
56 using ::android::hardware::camera::device::V3_5::StreamBufferRet;
57 using ::android::hardware::camera::device::V3_5::StreamBuffersVal;
58 using ::android::hardware::camera::device::V3_6::HalStreamConfiguration;
59 using ::android::hardware::camera::device::V3_7::CaptureRequest;
60 using ::android::hardware::camera::device::V3_7::StreamConfiguration;
61 using ::android::hardware::camera::provider::V2_5::DeviceState;
62 
63 // Util functions to convert the types between HIDL and Google Camera HAL.
64 
65 // Conversions from HAL to HIDL
66 status_t ConvertToHidlVendorTagSections(
67     const std::vector<google_camera_hal::VendorTagSection>& hal_sections,
68     hidl_vec<VendorTagSection>* hidl_sections);
69 
70 status_t ConvertToHidlVendorTagType(
71     google_camera_hal::CameraMetadataType hal_type,
72     CameraMetadataType* hidl_type);
73 
74 status_t ConvertToHidlResourceCost(
75     const google_camera_hal::CameraResourceCost& hal_cost,
76     CameraResourceCost* hidl_cost);
77 
78 status_t ConvertToHidlHalStreamConfig(
79     const std::vector<google_camera_hal::HalStream>& hal_configured_streams,
80     HalStreamConfiguration* hidl_hal_stream_config);
81 
82 // Convert a HAL result to a HIDL result. It will try to write the result
83 // metadata to result_metadata_queue. If it fails, it will write the result
84 // metadata in hidl_result.
85 status_t ConvertToHidlCaptureResult(
86     MessageQueue<uint8_t, kSynchronizedReadWrite>* result_metadata_queue,
87     std::unique_ptr<google_camera_hal::CaptureResult> hal_result,
88     CaptureResult* hidl_result);
89 
90 status_t ConverToHidlNotifyMessage(
91     const google_camera_hal::NotifyMessage& hal_message,
92     NotifyMsg* hidl_message);
93 
94 // Convert from HAL status_t to HIDL Status
95 // OK is converted to Status::OK.
96 // BAD_VALUE is converted to Status::ILLEGAL_ARGUMENT.
97 // -EBUSY is converted to Status::CAMERA_IN_USE.
98 // -EUSERS is converted to Status::MAX_CAMERAS_IN_USE.
99 // UNKNOWN_TRANSACTION is converted to Status::METHOD_NOT_SUPPORTED.
100 // INVALID_OPERATION is converted to Status::OPERATION_NOT_SUPPORTED.
101 // DEAD_OBJECT is converted to Status::CAMERA_DISCONNECTED.
102 // All other errors are converted to Status::INTERNAL_ERROR.
103 Status ConvertToHidlStatus(status_t hal_status);
104 
105 // Convert from HAL CameraDeviceStatus to HIDL CameraDeviceStatus
106 // kNotPresent is converted to CameraDeviceStatus::NOT_PRESENT.
107 // kPresent is converted to CameraDeviceStatus::PRESENT.
108 // kEnumerating is converted to CameraDeviceStatus::ENUMERATING.
109 status_t ConvertToHidlCameraDeviceStatus(
110     google_camera_hal::CameraDeviceStatus hal_camera_device_status,
111     CameraDeviceStatus* hidl_camera_device_status);
112 
113 // Convert from HAL TorchModeStatus to HIDL TorchModeStatus
114 // kNotAvailable is converted to TorchModeStatus::NOT_AVAILABLE.
115 // kAvailableOff is converted to TorchModeStatus::AVAILABLE_OFF.
116 // kAvailableOn is converted to TorchModeStatus::AVAILABLE_ON.
117 status_t ConvertToHidlTorchModeStatus(
118     google_camera_hal::TorchModeStatus hal_torch_status,
119     TorchModeStatus* hidl_torch_status);
120 
121 // Convert a HAL request to a HIDL request.
122 status_t ConvertToHidlBufferRequest(
123     const std::vector<google_camera_hal::BufferRequest>& hal_buffer_requests,
124     hidl_vec<BufferRequest>* hidl_buffer_requests);
125 
126 // Convert a HAL stream buffer to a HIDL hidl stream buffer.
127 status_t ConvertToHidlStreamBuffer(
128     const google_camera_hal::StreamBuffer& hal_buffer,
129     StreamBuffer* hidl_buffer);
130 
131 // Conversions from HIDL to HAL.
132 status_t ConvertToHalTemplateType(
133     RequestTemplate hidl_template,
134     google_camera_hal::RequestTemplate* hal_template);
135 
136 status_t ConvertToHalStreamBuffer(const StreamBuffer& hidl_buffer,
137                                   google_camera_hal::StreamBuffer* hal_buffer);
138 
139 status_t ConvertToHalMetadata(
140     uint32_t message_queue_setting_size,
141     MessageQueue<uint8_t, kSynchronizedReadWrite>* request_metadata_queue,
142     const CameraMetadata& request_settings,
143     std::unique_ptr<google_camera_hal::HalCameraMetadata>* hal_metadata);
144 
145 status_t ConvertToHalCaptureRequest(
146     const CaptureRequest& hidl_request,
147     MessageQueue<uint8_t, kSynchronizedReadWrite>* request_metadata_queue,
148     google_camera_hal::CaptureRequest* hal_request);
149 
150 status_t ConvertToHalBufferCaches(
151     const hidl_vec<BufferCache>& hidl_buffer_caches,
152     std::vector<google_camera_hal::BufferCache>* hal_buffer_caches);
153 
154 status_t ConverToHalStreamConfig(
155     const StreamConfiguration& hidl_stream_config,
156     google_camera_hal::StreamConfiguration* hal_stream_config);
157 
158 status_t ConverToHalStreamConfig(
159     const device::V3_4::StreamConfiguration& hidl_stream_config,
160     google_camera_hal::StreamConfiguration* hal_stream_config);
161 
162 status_t ConvertToHalStreamConfigurationMode(
163     StreamConfigurationMode hidl_mode,
164     google_camera_hal::StreamConfigurationMode* hal_mode);
165 
166 status_t ConvertToHalBufferStatus(BufferStatus hidl_status,
167                                   google_camera_hal::BufferStatus* hal_status);
168 
169 status_t ConvertToHalStream(const Stream& hidl_stream,
170                             google_camera_hal::Stream* hal_stream);
171 
172 status_t ConvertToHalStreamRotation(
173     StreamRotation hidl_stream_rotation,
174     google_camera_hal::StreamRotation* hal_stream_rotation);
175 
176 status_t ConvertToHalStreamType(StreamType hidl_stream_type,
177                                 google_camera_hal::StreamType* hal_stream_type);
178 
179 status_t ConvertToHalTorchMode(TorchMode hidl_torch_mode,
180                                google_camera_hal::TorchMode* hal_torch_mode);
181 
182 status_t ConvertToHalBufferRequestStatus(
183     const BufferRequestStatus& hidl_buffer_request_status,
184     google_camera_hal::BufferRequestStatus* hal_buffer_request_status);
185 
186 status_t ConvertToHalBufferReturnStatus(
187     const StreamBufferRet& hidl_stream_buffer_return,
188     google_camera_hal::BufferReturn* hal_buffer_return);
189 
190 status_t ConvertStreamConfigurationV34ToV37(
191     const device::V3_4::StreamConfiguration& config_3_4,
192     StreamConfiguration* config_3_7);
193 
194 status_t ConvertToHalDeviceState(
195     const hardware::hidl_bitfield<DeviceState> hidl_device_state,
196     google_camera_hal::DeviceState& hal_device_state);
197 
198 }  // namespace hidl_utils
199 }  // namespace implementation
200 }  // namespace camera
201 }  // namespace hardware
202 }  // namespace android
203 
204 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_HIDL_SERVICE_HIDL_UTILS_H_
205