blob: 76ed6f6d2bd43b1756e180d4a6a320030cf2a769 [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();
104 int res = validate_camera_metadata_structure(buffer, &expectedSize);
105 if (res == OK || res == CAMERA_METADATA_VALIDATION_SHIFTED) {
106 *dst = buffer;
107 } else {
108 ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
109 return false;
110 }
111 return true;
112}
113
114HCameraDeviceStatus convertToHidlCameraDeviceStatus(int32_t status) {
115 HCameraDeviceStatus deviceStatus = HCameraDeviceStatus::STATUS_UNKNOWN;
116 switch(status) {
117 case hardware::ICameraServiceListener::STATUS_NOT_PRESENT:
118 deviceStatus = HCameraDeviceStatus::STATUS_NOT_PRESENT;
119 break;
120 case hardware::ICameraServiceListener::STATUS_PRESENT:
121 deviceStatus = HCameraDeviceStatus::STATUS_PRESENT;
122 break;
123 case hardware::ICameraServiceListener::STATUS_ENUMERATING:
124 deviceStatus = HCameraDeviceStatus::STATUS_ENUMERATING;
125 break;
126 case hardware::ICameraServiceListener::STATUS_NOT_AVAILABLE:
127 deviceStatus = HCameraDeviceStatus::STATUS_NOT_AVAILABLE;
128 break;
129 default:
130 break;
131 }
132 return deviceStatus;
133}
134
135HCaptureResultExtras convertToHidl(const CaptureResultExtras &captureResultExtras) {
136 HCaptureResultExtras hCaptureResultExtras;
Jayant Chowdhary70da5772018-11-20 18:50:31 -0800137 hCaptureResultExtras.requestId = captureResultExtras.requestId;
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700138 hCaptureResultExtras.burstId = captureResultExtras.burstId;
139 hCaptureResultExtras.frameNumber = captureResultExtras.frameNumber;
140 hCaptureResultExtras.partialResultCount = captureResultExtras.partialResultCount;
141 hCaptureResultExtras.errorStreamId = captureResultExtras.errorStreamId;
142 return hCaptureResultExtras;
143}
144
145HErrorCode convertToHidl(int32_t errorCode) {
146 switch(errorCode) {
147 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED:
148 return HErrorCode::CAMERA_DISCONNECTED;
149 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE :
150 return HErrorCode::CAMERA_DEVICE;
151 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE:
152 return HErrorCode::CAMERA_SERVICE;
153 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
154 return HErrorCode::CAMERA_REQUEST;
155 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
156 return HErrorCode::CAMERA_RESULT;
157 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
158 return HErrorCode::CAMERA_BUFFER;
159 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED:
160 return HErrorCode::CAMERA_DISABLED;
161 case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR:
162 return HErrorCode::CAMERA_INVALID_ERROR;
163 default:
164 return HErrorCode::CAMERA_UNKNOWN_ERROR;
165 }
166}
167
168void convertToHidl(const std::vector<hardware::CameraStatus> &src,
169 hidl_vec<HCameraStatusAndId>* dst) {
170 dst->resize(src.size());
171 size_t i = 0;
172 for (auto &statusAndId : src) {
173 auto &a = (*dst)[i++];
174 a.cameraId = statusAndId.cameraId.c_str();
175 a.deviceStatus = convertToHidlCameraDeviceStatus(statusAndId.status);
176 }
177 return;
178}
179
180void convertToHidl(
181 const hardware::camera2::utils::SubmitInfo &submitInfo,
182 frameworks::cameraservice::device::V2_0::SubmitInfo *hSubmitInfo) {
183 hSubmitInfo->requestId = submitInfo.mRequestId;
184 hSubmitInfo->lastFrameNumber = submitInfo.mLastFrameNumber;
185}
186
187HStatus B2HStatus(const binder::Status &bStatus) {
188 HStatus status = HStatus::NO_ERROR;
189 if (bStatus.isOk()) {
190 // NO Error here
191 return status;
192 }
193 switch(bStatus.serviceSpecificErrorCode()) {
194 case hardware::ICameraService::ERROR_DISCONNECTED:
195 status = HStatus::DISCONNECTED;
196 break;
197 case hardware::ICameraService::ERROR_CAMERA_IN_USE:
198 status = HStatus::CAMERA_IN_USE;
199 break;
200 case hardware::ICameraService::ERROR_MAX_CAMERAS_IN_USE:
201 status = HStatus::MAX_CAMERAS_IN_USE;
202 break;
203 case hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT:
204 status = HStatus::ILLEGAL_ARGUMENT;
205 break;
206 case hardware::ICameraService::ERROR_DEPRECATED_HAL:
207 // Should not reach here since we filtered legacy HALs earlier
208 status = HStatus::DEPRECATED_HAL;
209 break;
210 case hardware::ICameraService::ERROR_DISABLED:
211 status = HStatus::DISABLED;
212 break;
213 case hardware::ICameraService::ERROR_PERMISSION_DENIED:
214 status = HStatus::PERMISSION_DENIED;
215 break;
216 case hardware::ICameraService::ERROR_INVALID_OPERATION:
217 status = HStatus::INVALID_OPERATION;
218 break;
219 default:
220 status = HStatus::UNKNOWN_ERROR;
221 break;
222 }
223 return status;
224}
225
Jayant Chowdhary0c947272018-08-15 14:42:04 -0700226HPhysicalCaptureResultInfo convertToHidl(
227 const PhysicalCaptureResultInfo &physicalCaptureResultInfo,
228 std::shared_ptr<CaptureResultMetadataQueue> &captureResultMetadataQueue) {
229 HPhysicalCaptureResultInfo hPhysicalCaptureResultInfo;
230 hPhysicalCaptureResultInfo.physicalCameraId =
231 String8(physicalCaptureResultInfo.mPhysicalCameraId).string();
232 const camera_metadata_t *rawMetadata =
233 physicalCaptureResultInfo.mPhysicalCameraMetadata.getAndLock();
234 // Try using fmq at first.
235 size_t metadata_size = get_camera_metadata_size(rawMetadata);
236 if ((metadata_size > 0) && (captureResultMetadataQueue->availableToWrite() > 0)) {
237 if (captureResultMetadataQueue->write((uint8_t *)rawMetadata, metadata_size)) {
238 hPhysicalCaptureResultInfo.physicalCameraMetadata.fmqMetadataSize(metadata_size);
239 } else {
240 ALOGW("%s Couldn't use fmq, falling back to hwbinder", __FUNCTION__);
241 HCameraMetadata metadata;
242 convertToHidl(rawMetadata, &metadata);
243 hPhysicalCaptureResultInfo.physicalCameraMetadata.metadata(std::move(metadata));
244 }
245 }
246 physicalCaptureResultInfo.mPhysicalCameraMetadata.unlock(rawMetadata);
247 return hPhysicalCaptureResultInfo;
248}
249
250hidl_vec<HPhysicalCaptureResultInfo> convertToHidl(
251 const std::vector<PhysicalCaptureResultInfo> &physicalCaptureResultInfos,
252 std::shared_ptr<CaptureResultMetadataQueue> &captureResultMetadataQueue) {
253 hidl_vec<HPhysicalCaptureResultInfo> hPhysicalCaptureResultInfos;
254 hPhysicalCaptureResultInfos.resize(physicalCaptureResultInfos.size());
255 size_t i = 0;
256 for (auto &physicalCaptureResultInfo : physicalCaptureResultInfos) {
257 hPhysicalCaptureResultInfos[i++] = convertToHidl(physicalCaptureResultInfo,
258 captureResultMetadataQueue);
259 }
260 return hPhysicalCaptureResultInfos;
261}
262
Jayant Chowdharybe543d42018-08-15 13:16:14 -0700263} //conversion
264} // utils
265} //cameraservice
266} // hardware
267} // android