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