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 hardware::camera2::utils::SubmitInfo & submitInfo,frameworks::cameraservice::device::V2_0::SubmitInfo * hSubmitInfo)200 void convertToHidl(
201 const hardware::camera2::utils::SubmitInfo &submitInfo,
202 frameworks::cameraservice::device::V2_0::SubmitInfo *hSubmitInfo) {
203 hSubmitInfo->requestId = submitInfo.mRequestId;
204 hSubmitInfo->lastFrameNumber = submitInfo.mLastFrameNumber;
205 }
206
B2HStatus(const binder::Status & bStatus)207 HStatus B2HStatus(const binder::Status &bStatus) {
208 HStatus status = HStatus::NO_ERROR;
209 if (bStatus.isOk()) {
210 // NO Error here
211 return status;
212 }
213 switch(bStatus.serviceSpecificErrorCode()) {
214 case hardware::ICameraService::ERROR_DISCONNECTED:
215 status = HStatus::DISCONNECTED;
216 break;
217 case hardware::ICameraService::ERROR_CAMERA_IN_USE:
218 status = HStatus::CAMERA_IN_USE;
219 break;
220 case hardware::ICameraService::ERROR_MAX_CAMERAS_IN_USE:
221 status = HStatus::MAX_CAMERAS_IN_USE;
222 break;
223 case hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT:
224 status = HStatus::ILLEGAL_ARGUMENT;
225 break;
226 case hardware::ICameraService::ERROR_DEPRECATED_HAL:
227 // Should not reach here since we filtered legacy HALs earlier
228 status = HStatus::DEPRECATED_HAL;
229 break;
230 case hardware::ICameraService::ERROR_DISABLED:
231 status = HStatus::DISABLED;
232 break;
233 case hardware::ICameraService::ERROR_PERMISSION_DENIED:
234 status = HStatus::PERMISSION_DENIED;
235 break;
236 case hardware::ICameraService::ERROR_INVALID_OPERATION:
237 status = HStatus::INVALID_OPERATION;
238 break;
239 default:
240 status = HStatus::UNKNOWN_ERROR;
241 break;
242 }
243 return status;
244 }
245
convertToHidl(const PhysicalCaptureResultInfo & physicalCaptureResultInfo,std::shared_ptr<CaptureResultMetadataQueue> & captureResultMetadataQueue)246 HPhysicalCaptureResultInfo convertToHidl(
247 const PhysicalCaptureResultInfo &physicalCaptureResultInfo,
248 std::shared_ptr<CaptureResultMetadataQueue> &captureResultMetadataQueue) {
249 HPhysicalCaptureResultInfo hPhysicalCaptureResultInfo;
250 hPhysicalCaptureResultInfo.physicalCameraId =
251 String8(physicalCaptureResultInfo.mPhysicalCameraId).string();
252 const camera_metadata_t *rawMetadata =
253 physicalCaptureResultInfo.mPhysicalCameraMetadata.getAndLock();
254 // Try using fmq at first.
255 size_t metadata_size = get_camera_metadata_size(rawMetadata);
256 if ((metadata_size > 0) && (captureResultMetadataQueue->availableToWrite() > 0)) {
257 if (captureResultMetadataQueue->write((uint8_t *)rawMetadata, metadata_size)) {
258 hPhysicalCaptureResultInfo.physicalCameraMetadata.fmqMetadataSize(metadata_size);
259 } else {
260 ALOGW("%s Couldn't use fmq, falling back to hwbinder", __FUNCTION__);
261 HCameraMetadata metadata;
262 convertToHidl(rawMetadata, &metadata);
263 hPhysicalCaptureResultInfo.physicalCameraMetadata.metadata(std::move(metadata));
264 }
265 }
266 physicalCaptureResultInfo.mPhysicalCameraMetadata.unlock(rawMetadata);
267 return hPhysicalCaptureResultInfo;
268 }
269
convertToHidl(const std::vector<PhysicalCaptureResultInfo> & physicalCaptureResultInfos,std::shared_ptr<CaptureResultMetadataQueue> & captureResultMetadataQueue)270 hidl_vec<HPhysicalCaptureResultInfo> convertToHidl(
271 const std::vector<PhysicalCaptureResultInfo> &physicalCaptureResultInfos,
272 std::shared_ptr<CaptureResultMetadataQueue> &captureResultMetadataQueue) {
273 hidl_vec<HPhysicalCaptureResultInfo> hPhysicalCaptureResultInfos;
274 hPhysicalCaptureResultInfos.resize(physicalCaptureResultInfos.size());
275 size_t i = 0;
276 for (auto &physicalCaptureResultInfo : physicalCaptureResultInfos) {
277 hPhysicalCaptureResultInfos[i++] = convertToHidl(physicalCaptureResultInfo,
278 captureResultMetadataQueue);
279 }
280 return hPhysicalCaptureResultInfos;
281 }
282
283 } //conversion
284 } // utils
285 } //cameraservice
286 } // hardware
287 } // android
288