1 /*
2 * Copyright (C) 2018 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 #include <hidl/Convert.h>
18 #include <gui/bufferqueue/1.0/H2BGraphicBufferProducer.h>
19 #include <cutils/native_handle.h>
20 #include <mediautils/AImageReaderUtils.h>
21
22 namespace android {
23 namespace hardware {
24 namespace cameraservice {
25 namespace utils {
26 namespace conversion {
27
28 using hardware::graphics::bufferqueue::V1_0::utils::H2BGraphicBufferProducer;
29 using aimg::AImageReader_getHGBPFromHandle;
30
31 // Note: existing data in dst will be gone. Caller still owns the memory of src
convertToHidl(const camera_metadata_t * src,HCameraMetadata * dst)32 void convertToHidl(const camera_metadata_t *src, HCameraMetadata* dst) {
33 if (src == nullptr) {
34 ALOGW("%s:attempt to convert empty metadata to Hidl", __FUNCTION__);
35 return;
36 }
37 size_t size = get_camera_metadata_size(src);
38 dst->setToExternal((uint8_t *) src, size);
39 return;
40 }
41
convertFromHidl(HStreamConfigurationMode streamConfigurationMode)42 int32_t convertFromHidl(HStreamConfigurationMode streamConfigurationMode) {
43 switch (streamConfigurationMode) {
44 case HStreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE:
45 return camera2::ICameraDeviceUser::CONSTRAINED_HIGH_SPEED_MODE;
46 case HStreamConfigurationMode::NORMAL_MODE:
47 return camera2::ICameraDeviceUser::NORMAL_MODE;
48 default:
49 // TODO: Fix this
50 return camera2::ICameraDeviceUser::VENDOR_MODE_START;
51 }
52 }
53
convertFromHidl(HTemplateId templateId)54 int32_t convertFromHidl(HTemplateId templateId) {
55 switch(templateId) {
56 case HTemplateId::PREVIEW:
57 return camera2::ICameraDeviceUser::TEMPLATE_PREVIEW;
58 case HTemplateId::STILL_CAPTURE:
59 return camera2::ICameraDeviceUser::TEMPLATE_STILL_CAPTURE;
60 case HTemplateId::RECORD:
61 return camera2::ICameraDeviceUser::TEMPLATE_RECORD;
62 case HTemplateId::VIDEO_SNAPSHOT:
63 return camera2::ICameraDeviceUser::TEMPLATE_VIDEO_SNAPSHOT;
64 case HTemplateId::ZERO_SHUTTER_LAG:
65 return camera2::ICameraDeviceUser::TEMPLATE_ZERO_SHUTTER_LAG;
66 case HTemplateId::MANUAL:
67 return camera2::ICameraDeviceUser::TEMPLATE_MANUAL;
68 }
69 }
70
convertFromHidl(HOutputConfiguration::Rotation rotation)71 int convertFromHidl(HOutputConfiguration::Rotation rotation) {
72 switch(rotation) {
73 case HOutputConfiguration::Rotation::R0:
74 return 0;
75 case HOutputConfiguration::Rotation::R90:
76 return 1;
77 case HOutputConfiguration::Rotation::R180:
78 return 2;
79 case HOutputConfiguration::Rotation::R270:
80 return 3;
81 }
82 }
83
convertFromHidl(const HOutputConfiguration & hOutputConfiguration)84 hardware::camera2::params::OutputConfiguration convertFromHidl(
85 const HOutputConfiguration &hOutputConfiguration) {
86 std::vector<sp<IGraphicBufferProducer>> iGBPs;
87 auto &windowHandles = hOutputConfiguration.windowHandles;
88 iGBPs.reserve(windowHandles.size());
89 for (auto &handle : windowHandles) {
90 iGBPs.push_back(new H2BGraphicBufferProducer(AImageReader_getHGBPFromHandle(handle)));
91 }
92 String16 physicalCameraId16(hOutputConfiguration.physicalCameraId.c_str());
93 hardware::camera2::params::OutputConfiguration outputConfiguration(
94 iGBPs, convertFromHidl(hOutputConfiguration.rotation), physicalCameraId16,
95 hOutputConfiguration.windowGroupId, OutputConfiguration::SURFACE_TYPE_UNKNOWN, 0, 0,
96 (windowHandles.size() > 1));
97 return outputConfiguration;
98 }
99
convertFromHidl(const HSessionConfiguration & hSessionConfiguration)100 hardware::camera2::params::SessionConfiguration convertFromHidl(
101 const HSessionConfiguration &hSessionConfiguration) {
102 hardware::camera2::params::SessionConfiguration sessionConfig(
103 hSessionConfiguration.inputWidth, hSessionConfiguration.inputHeight,
104 hSessionConfiguration.inputFormat,
105 static_cast<int>(hSessionConfiguration.operationMode));
106
107 for (const auto& hConfig : hSessionConfiguration.outputStreams) {
108 hardware::camera2::params::OutputConfiguration config = convertFromHidl(hConfig);
109 sessionConfig.addOutputConfiguration(config);
110 }
111
112 return sessionConfig;
113 }
114
115 // The camera metadata here is cloned. Since we're reading metadata over
116 // hwbinder we would need to clone it in order to avoid aligment issues.
convertFromHidl(const HCameraMetadata & src,CameraMetadata * dst)117 bool convertFromHidl(const HCameraMetadata &src, CameraMetadata *dst) {
118 const camera_metadata_t *buffer = reinterpret_cast<const camera_metadata_t*>(src.data());
119 size_t expectedSize = src.size();
120 if (buffer != nullptr) {
121 int res = validate_camera_metadata_structure(buffer, &expectedSize);
122 if (res == OK || res == CAMERA_METADATA_VALIDATION_SHIFTED) {
123 *dst = buffer;
124 } else {
125 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
126 return false;
127 }
128 }
129 return true;
130 }
131
convertToHidlCameraDeviceStatus(int32_t status)132 HCameraDeviceStatus convertToHidlCameraDeviceStatus(int32_t status) {
133 HCameraDeviceStatus deviceStatus = HCameraDeviceStatus::STATUS_UNKNOWN;
134 switch(status) {
135 case hardware::ICameraServiceListener::STATUS_NOT_PRESENT:
136 deviceStatus = HCameraDeviceStatus::STATUS_NOT_PRESENT;
137 break;
138 case hardware::ICameraServiceListener::STATUS_PRESENT:
139 deviceStatus = HCameraDeviceStatus::STATUS_PRESENT;
140 break;
141 case hardware::ICameraServiceListener::STATUS_ENUMERATING:
142 deviceStatus = HCameraDeviceStatus::STATUS_ENUMERATING;
143 break;
144 case hardware::ICameraServiceListener::STATUS_NOT_AVAILABLE:
145 deviceStatus = HCameraDeviceStatus::STATUS_NOT_AVAILABLE;
146 break;
147 default:
148 break;
149 }
150 return deviceStatus;
151 }
152
convertToHidl(const CaptureResultExtras & captureResultExtras)153 HCaptureResultExtras convertToHidl(const CaptureResultExtras &captureResultExtras) {
154 HCaptureResultExtras hCaptureResultExtras;
155 hCaptureResultExtras.requestId = captureResultExtras.requestId;
156 hCaptureResultExtras.burstId = captureResultExtras.burstId;
157 hCaptureResultExtras.frameNumber = captureResultExtras.frameNumber;
158 hCaptureResultExtras.partialResultCount = captureResultExtras.partialResultCount;
159 hCaptureResultExtras.errorStreamId = captureResultExtras.errorStreamId;
160 hCaptureResultExtras.errorPhysicalCameraId = hidl_string(String8(
161 captureResultExtras.errorPhysicalCameraId).string());
162 return hCaptureResultExtras;
163 }
164
convertToHidl(int32_t errorCode)165 HErrorCode convertToHidl(int32_t errorCode) {
166 switch(errorCode) {
167 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED:
168 return HErrorCode::CAMERA_DISCONNECTED;
169 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE :
170 return HErrorCode::CAMERA_DEVICE;
171 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE:
172 return HErrorCode::CAMERA_SERVICE;
173 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
174 return HErrorCode::CAMERA_REQUEST;
175 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
176 return HErrorCode::CAMERA_RESULT;
177 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
178 return HErrorCode::CAMERA_BUFFER;
179 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED:
180 return HErrorCode::CAMERA_DISABLED;
181 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR:
182 return HErrorCode::CAMERA_INVALID_ERROR;
183 default:
184 return HErrorCode::CAMERA_UNKNOWN_ERROR;
185 }
186 }
187
convertToHidl(const std::vector<hardware::CameraStatus> & src,hidl_vec<HCameraStatusAndId> * dst)188 void convertToHidl(const std::vector<hardware::CameraStatus> &src,
189 hidl_vec<HCameraStatusAndId>* dst) {
190 dst->resize(src.size());
191 size_t i = 0;
192 for (auto &statusAndId : src) {
193 auto &a = (*dst)[i++];
194 a.cameraId = statusAndId.cameraId.c_str();
195 a.deviceStatus = convertToHidlCameraDeviceStatus(statusAndId.status);
196 }
197 return;
198 }
199
convertToHidl(const std::vector<hardware::CameraStatus> & src,hidl_vec<frameworks::cameraservice::service::V2_1::CameraStatusAndId> * dst)200 void convertToHidl(const std::vector<hardware::CameraStatus> &src,
201 hidl_vec<frameworks::cameraservice::service::V2_1::CameraStatusAndId>* dst) {
202 dst->resize(src.size());
203 size_t i = 0;
204 for (const auto &statusAndId : src) {
205 auto &a = (*dst)[i++];
206 a.v2_0.cameraId = statusAndId.cameraId.c_str();
207 a.v2_0.deviceStatus = convertToHidlCameraDeviceStatus(statusAndId.status);
208 size_t numUnvailPhysicalCameras = statusAndId.unavailablePhysicalIds.size();
209 a.unavailPhysicalCameraIds.resize(numUnvailPhysicalCameras);
210 for (size_t j = 0; j < numUnvailPhysicalCameras; j++) {
211 a.unavailPhysicalCameraIds[j] = statusAndId.unavailablePhysicalIds[j].c_str();
212 }
213 }
214 return;
215 }
216
convertToHidl(const hardware::camera2::utils::SubmitInfo & submitInfo,frameworks::cameraservice::device::V2_0::SubmitInfo * hSubmitInfo)217 void convertToHidl(
218 const hardware::camera2::utils::SubmitInfo &submitInfo,
219 frameworks::cameraservice::device::V2_0::SubmitInfo *hSubmitInfo) {
220 hSubmitInfo->requestId = submitInfo.mRequestId;
221 hSubmitInfo->lastFrameNumber = submitInfo.mLastFrameNumber;
222 }
223
B2HStatus(const binder::Status & bStatus)224 HStatus B2HStatus(const binder::Status &bStatus) {
225 HStatus status = HStatus::NO_ERROR;
226 if (bStatus.isOk()) {
227 // NO Error here
228 return status;
229 }
230 switch(bStatus.serviceSpecificErrorCode()) {
231 case hardware::ICameraService::ERROR_DISCONNECTED:
232 status = HStatus::DISCONNECTED;
233 break;
234 case hardware::ICameraService::ERROR_CAMERA_IN_USE:
235 status = HStatus::CAMERA_IN_USE;
236 break;
237 case hardware::ICameraService::ERROR_MAX_CAMERAS_IN_USE:
238 status = HStatus::MAX_CAMERAS_IN_USE;
239 break;
240 case hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT:
241 status = HStatus::ILLEGAL_ARGUMENT;
242 break;
243 case hardware::ICameraService::ERROR_DEPRECATED_HAL:
244 // Should not reach here since we filtered legacy HALs earlier
245 status = HStatus::DEPRECATED_HAL;
246 break;
247 case hardware::ICameraService::ERROR_DISABLED:
248 status = HStatus::DISABLED;
249 break;
250 case hardware::ICameraService::ERROR_PERMISSION_DENIED:
251 status = HStatus::PERMISSION_DENIED;
252 break;
253 case hardware::ICameraService::ERROR_INVALID_OPERATION:
254 status = HStatus::INVALID_OPERATION;
255 break;
256 default:
257 status = HStatus::UNKNOWN_ERROR;
258 break;
259 }
260 return status;
261 }
262
convertToHidl(const PhysicalCaptureResultInfo & physicalCaptureResultInfo,std::shared_ptr<CaptureResultMetadataQueue> & captureResultMetadataQueue)263 HPhysicalCaptureResultInfo convertToHidl(
264 const PhysicalCaptureResultInfo &physicalCaptureResultInfo,
265 std::shared_ptr<CaptureResultMetadataQueue> &captureResultMetadataQueue) {
266 HPhysicalCaptureResultInfo hPhysicalCaptureResultInfo;
267 hPhysicalCaptureResultInfo.physicalCameraId =
268 String8(physicalCaptureResultInfo.mPhysicalCameraId).string();
269 const camera_metadata_t *rawMetadata =
270 physicalCaptureResultInfo.mPhysicalCameraMetadata.getAndLock();
271 // Try using fmq at first.
272 size_t metadata_size = get_camera_metadata_size(rawMetadata);
273 if ((metadata_size > 0) && (captureResultMetadataQueue->availableToWrite() > 0)) {
274 if (captureResultMetadataQueue->write((uint8_t *)rawMetadata, metadata_size)) {
275 hPhysicalCaptureResultInfo.physicalCameraMetadata.fmqMetadataSize(metadata_size);
276 } else {
277 ALOGW("%s Couldn't use fmq, falling back to hwbinder", __FUNCTION__);
278 HCameraMetadata metadata;
279 convertToHidl(rawMetadata, &metadata);
280 hPhysicalCaptureResultInfo.physicalCameraMetadata.metadata(std::move(metadata));
281 }
282 }
283 physicalCaptureResultInfo.mPhysicalCameraMetadata.unlock(rawMetadata);
284 return hPhysicalCaptureResultInfo;
285 }
286
convertToHidl(const std::vector<PhysicalCaptureResultInfo> & physicalCaptureResultInfos,std::shared_ptr<CaptureResultMetadataQueue> & captureResultMetadataQueue)287 hidl_vec<HPhysicalCaptureResultInfo> convertToHidl(
288 const std::vector<PhysicalCaptureResultInfo> &physicalCaptureResultInfos,
289 std::shared_ptr<CaptureResultMetadataQueue> &captureResultMetadataQueue) {
290 hidl_vec<HPhysicalCaptureResultInfo> hPhysicalCaptureResultInfos;
291 hPhysicalCaptureResultInfos.resize(physicalCaptureResultInfos.size());
292 size_t i = 0;
293 for (auto &physicalCaptureResultInfo : physicalCaptureResultInfos) {
294 hPhysicalCaptureResultInfos[i++] = convertToHidl(physicalCaptureResultInfo,
295 captureResultMetadataQueue);
296 }
297 return hPhysicalCaptureResultInfos;
298 }
299
300 } //conversion
301 } // utils
302 } //cameraservice
303 } // hardware
304 } // android
305