blob: 582ce347f94aab2cf9be9f0c3176b9f7297ab9c5 [file] [log] [blame]
Jayant Chowdharybe543d42018-08-15 13:16:14 -07001/*
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>
Jayant Chowdharye0bb61b2018-11-14 23:59:12 -080019#include <cutils/native_handle.h>
20#include <mediautils/AImageReaderUtils.h>
Jayant Chowdharybe543d42018-08-15 13:16:14 -070021
22namespace android {
23namespace hardware {
24namespace cameraservice {
25namespace utils {
26namespace conversion {
27
28using hardware::graphics::bufferqueue::V1_0::utils::H2BGraphicBufferProducer;
Jayant Chowdharye0bb61b2018-11-14 23:59:12 -080029using aimg::AImageReader_getHGBPFromHandle;
Jayant Chowdharybe543d42018-08-15 13:16:14 -070030
31// Note: existing data in dst will be gone. Caller still owns the memory of src
32void 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
42int32_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
54int32_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
71int 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
84hardware::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 hardware::camera2::params::OutputConfiguration outputConfiguration(
93 iGBPs, convertFromHidl(hOutputConfiguration.rotation),
94 hOutputConfiguration.windowGroupId, OutputConfiguration::SURFACE_TYPE_UNKNOWN, 0, 0,
95 (windowHandles.size() > 1));
96 return outputConfiguration;
97}
98
99// The camera metadata here is cloned. Since we're reading metadata over
100// hwbinder we would need to clone it in order to avoid aligment issues.
101bool convertFromHidl(const HCameraMetadata &src, CameraMetadata *dst) {
102 const camera_metadata_t *buffer = reinterpret_cast<const camera_metadata_t*>(src.data());
103 size_t expectedSize = src.size();
Shuzhen Wang639ed122018-12-06 14:42:52 -0800104 if (buffer != nullptr) {
105 int res = validate_camera_metadata_structure(buffer, &expectedSize);
106 if (res == OK || res == CAMERA_METADATA_VALIDATION_SHIFTED) {
107 *dst = buffer;
108 } else {
109 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
110 return false;
111 }
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700112 }
113 return true;
114}
115
116HCameraDeviceStatus convertToHidlCameraDeviceStatus(int32_t status) {
117 HCameraDeviceStatus deviceStatus = HCameraDeviceStatus::STATUS_UNKNOWN;
118 switch(status) {
119 case hardware::ICameraServiceListener::STATUS_NOT_PRESENT:
120 deviceStatus = HCameraDeviceStatus::STATUS_NOT_PRESENT;
121 break;
122 case hardware::ICameraServiceListener::STATUS_PRESENT:
123 deviceStatus = HCameraDeviceStatus::STATUS_PRESENT;
124 break;
125 case hardware::ICameraServiceListener::STATUS_ENUMERATING:
126 deviceStatus = HCameraDeviceStatus::STATUS_ENUMERATING;
127 break;
128 case hardware::ICameraServiceListener::STATUS_NOT_AVAILABLE:
129 deviceStatus = HCameraDeviceStatus::STATUS_NOT_AVAILABLE;
130 break;
131 default:
132 break;
133 }
134 return deviceStatus;
135}
136
137HCaptureResultExtras convertToHidl(const CaptureResultExtras &captureResultExtras) {
138 HCaptureResultExtras hCaptureResultExtras;
Jayant Chowdhary70da5772018-11-20 18:50:31 -0800139 hCaptureResultExtras.requestId = captureResultExtras.requestId;
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700140 hCaptureResultExtras.burstId = captureResultExtras.burstId;
141 hCaptureResultExtras.frameNumber = captureResultExtras.frameNumber;
142 hCaptureResultExtras.partialResultCount = captureResultExtras.partialResultCount;
143 hCaptureResultExtras.errorStreamId = captureResultExtras.errorStreamId;
144 return hCaptureResultExtras;
145}
146
147HErrorCode convertToHidl(int32_t errorCode) {
148 switch(errorCode) {
149 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED:
150 return HErrorCode::CAMERA_DISCONNECTED;
151 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE :
152 return HErrorCode::CAMERA_DEVICE;
153 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE:
154 return HErrorCode::CAMERA_SERVICE;
155 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
156 return HErrorCode::CAMERA_REQUEST;
157 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
158 return HErrorCode::CAMERA_RESULT;
159 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
160 return HErrorCode::CAMERA_BUFFER;
161 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED:
162 return HErrorCode::CAMERA_DISABLED;
163 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR:
164 return HErrorCode::CAMERA_INVALID_ERROR;
165 default:
166 return HErrorCode::CAMERA_UNKNOWN_ERROR;
167 }
168}
169
170void convertToHidl(const std::vector<hardware::CameraStatus> &src,
171 hidl_vec<HCameraStatusAndId>* dst) {
172 dst->resize(src.size());
173 size_t i = 0;
174 for (auto &statusAndId : src) {
175 auto &a = (*dst)[i++];
176 a.cameraId = statusAndId.cameraId.c_str();
177 a.deviceStatus = convertToHidlCameraDeviceStatus(statusAndId.status);
178 }
179 return;
180}
181
182void convertToHidl(
183 const hardware::camera2::utils::SubmitInfo &submitInfo,
184 frameworks::cameraservice::device::V2_0::SubmitInfo *hSubmitInfo) {
185 hSubmitInfo->requestId = submitInfo.mRequestId;
186 hSubmitInfo->lastFrameNumber = submitInfo.mLastFrameNumber;
187}
188
189HStatus B2HStatus(const binder::Status &bStatus) {
190 HStatus status = HStatus::NO_ERROR;
191 if (bStatus.isOk()) {
192 // NO Error here
193 return status;
194 }
195 switch(bStatus.serviceSpecificErrorCode()) {
196 case hardware::ICameraService::ERROR_DISCONNECTED:
197 status = HStatus::DISCONNECTED;
198 break;
199 case hardware::ICameraService::ERROR_CAMERA_IN_USE:
200 status = HStatus::CAMERA_IN_USE;
201 break;
202 case hardware::ICameraService::ERROR_MAX_CAMERAS_IN_USE:
203 status = HStatus::MAX_CAMERAS_IN_USE;
204 break;
205 case hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT:
206 status = HStatus::ILLEGAL_ARGUMENT;
207 break;
208 case hardware::ICameraService::ERROR_DEPRECATED_HAL:
209 // Should not reach here since we filtered legacy HALs earlier
210 status = HStatus::DEPRECATED_HAL;
211 break;
212 case hardware::ICameraService::ERROR_DISABLED:
213 status = HStatus::DISABLED;
214 break;
215 case hardware::ICameraService::ERROR_PERMISSION_DENIED:
216 status = HStatus::PERMISSION_DENIED;
217 break;
218 case hardware::ICameraService::ERROR_INVALID_OPERATION:
219 status = HStatus::INVALID_OPERATION;
220 break;
221 default:
222 status = HStatus::UNKNOWN_ERROR;
223 break;
224 }
225 return status;
226}
227
Jayant Chowdhary0c947272018-08-15 14:42:04 -0700228HPhysicalCaptureResultInfo convertToHidl(
229 const PhysicalCaptureResultInfo &physicalCaptureResultInfo,
230 std::shared_ptr<CaptureResultMetadataQueue> &captureResultMetadataQueue) {
231 HPhysicalCaptureResultInfo hPhysicalCaptureResultInfo;
232 hPhysicalCaptureResultInfo.physicalCameraId =
233 String8(physicalCaptureResultInfo.mPhysicalCameraId).string();
234 const camera_metadata_t *rawMetadata =
235 physicalCaptureResultInfo.mPhysicalCameraMetadata.getAndLock();
236 // Try using fmq at first.
237 size_t metadata_size = get_camera_metadata_size(rawMetadata);
238 if ((metadata_size > 0) && (captureResultMetadataQueue->availableToWrite() > 0)) {
239 if (captureResultMetadataQueue->write((uint8_t *)rawMetadata, metadata_size)) {
240 hPhysicalCaptureResultInfo.physicalCameraMetadata.fmqMetadataSize(metadata_size);
241 } else {
242 ALOGW("%s Couldn't use fmq, falling back to hwbinder", __FUNCTION__);
243 HCameraMetadata metadata;
244 convertToHidl(rawMetadata, &metadata);
245 hPhysicalCaptureResultInfo.physicalCameraMetadata.metadata(std::move(metadata));
246 }
247 }
248 physicalCaptureResultInfo.mPhysicalCameraMetadata.unlock(rawMetadata);
249 return hPhysicalCaptureResultInfo;
250}
251
252hidl_vec<HPhysicalCaptureResultInfo> convertToHidl(
253 const std::vector<PhysicalCaptureResultInfo> &physicalCaptureResultInfos,
254 std::shared_ptr<CaptureResultMetadataQueue> &captureResultMetadataQueue) {
255 hidl_vec<HPhysicalCaptureResultInfo> hPhysicalCaptureResultInfos;
256 hPhysicalCaptureResultInfos.resize(physicalCaptureResultInfos.size());
257 size_t i = 0;
258 for (auto &physicalCaptureResultInfo : physicalCaptureResultInfos) {
259 hPhysicalCaptureResultInfos[i++] = convertToHidl(physicalCaptureResultInfo,
260 captureResultMetadataQueue);
261 }
262 return hPhysicalCaptureResultInfos;
263}
264
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700265} //conversion
266} // utils
267} //cameraservice
268} // hardware
269} // android