• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 #define LOG_TAG "AidlUtils"
18 
19 #include <aidl/AidlUtils.h>
20 #include <aidl/VndkVersionMetadataTags.h>
21 #include <aidlcommonsupport/NativeHandle.h>
22 #include <device3/Camera3StreamInterface.h>
23 #include <gui/bufferqueue/1.0/H2BGraphicBufferProducer.h>
24 #include <mediautils/AImageReaderUtils.h>
25 
26 namespace android::hardware::cameraservice::utils::conversion::aidl {
27 
28 using aimg::AImageReader_getHGBPFromHandle;
29 using hardware::graphics::bufferqueue::V1_0::utils::H2BGraphicBufferProducer;
30 
31 // Note: existing data in dst will be gone. Caller still owns the memory of src
cloneToAidl(const camera_metadata_t * src,SCameraMetadata * dst)32 void cloneToAidl(const camera_metadata_t* src, SCameraMetadata* dst) {
33     if (src == nullptr) {
34         ALOGW("%s:attempt to convert empty metadata to AIDL", __FUNCTION__);
35         return;
36     }
37     size_t size = get_camera_metadata_size(src);
38     uint8_t* startPtr = (uint8_t*)src;
39     uint8_t* endPtr = startPtr + size;
40     dst->metadata.assign(startPtr, endPtr);
41 }
42 
43 // The camera metadata here is cloned. Since we're reading metadata over
44 // the binder we would need to clone it in order to avoid alignment issues.
cloneFromAidl(const SCameraMetadata & src,CameraMetadata * dst)45 bool cloneFromAidl(const SCameraMetadata &src, CameraMetadata *dst) {
46     const camera_metadata_t *buffer =
47             reinterpret_cast<const camera_metadata_t*>(src.metadata.data());
48     size_t expectedSize = src.metadata.size();
49     if (buffer != nullptr) {
50         int res = validate_camera_metadata_structure(buffer, &expectedSize);
51         if (res == OK || res == CAMERA_METADATA_VALIDATION_SHIFTED) {
52             *dst = buffer;
53         } else {
54             ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__);
55             return false;
56         }
57     }
58     return true;
59 }
60 
convertFromAidl(SStreamConfigurationMode streamConfigurationMode)61 int32_t convertFromAidl(SStreamConfigurationMode streamConfigurationMode) {
62     switch (streamConfigurationMode) {
63         case SStreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE:
64             return camera2::ICameraDeviceUser::CONSTRAINED_HIGH_SPEED_MODE;
65         case SStreamConfigurationMode::NORMAL_MODE:
66             return camera2::ICameraDeviceUser::NORMAL_MODE;
67         default:
68             // TODO: Fix this
69             return camera2::ICameraDeviceUser::VENDOR_MODE_START;
70     }
71 }
72 
convertFromAidl(const SOutputConfiguration & src)73 UOutputConfiguration convertFromAidl(const SOutputConfiguration &src) {
74     std::vector<sp<IGraphicBufferProducer>> iGBPs;
75     auto &windowHandles = src.windowHandles;
76     iGBPs.reserve(windowHandles.size());
77 
78     for (auto &handle : windowHandles) {
79         native_handle_t* nh = makeFromAidl(handle);
80         iGBPs.push_back(new H2BGraphicBufferProducer(AImageReader_getHGBPFromHandle(nh)));
81         native_handle_delete(nh);
82     }
83     String16 physicalCameraId16(src.physicalCameraId.c_str());
84     UOutputConfiguration outputConfiguration(
85         iGBPs, convertFromAidl(src.rotation), physicalCameraId16,
86         src.windowGroupId, OutputConfiguration::SURFACE_TYPE_UNKNOWN, 0, 0,
87         (windowHandles.size() > 1));
88     return outputConfiguration;
89 }
90 
convertFromAidl(const SSessionConfiguration & src)91 USessionConfiguration convertFromAidl(const SSessionConfiguration &src) {
92     USessionConfiguration sessionConfig(src.inputWidth, src.inputHeight,
93                                         src.inputFormat, static_cast<int>(src.operationMode));
94 
95     for (const auto& os : src.outputStreams) {
96         UOutputConfiguration config = convertFromAidl(os);
97         sessionConfig.addOutputConfiguration(config);
98     }
99 
100     return sessionConfig;
101 }
102 
convertFromAidl(SOutputConfiguration::Rotation rotation)103 int convertFromAidl(SOutputConfiguration::Rotation rotation) {
104     switch(rotation) {
105         case SOutputConfiguration::Rotation::R270:
106             return android::camera3::CAMERA_STREAM_ROTATION_270;
107         case SOutputConfiguration::Rotation::R180:
108             return android::camera3::CAMERA_STREAM_ROTATION_180;
109         case SOutputConfiguration::Rotation::R90:
110             return android::camera3::CAMERA_STREAM_ROTATION_90;
111         case SOutputConfiguration::Rotation::R0:
112         default:
113             return android::camera3::CAMERA_STREAM_ROTATION_0;
114     }
115 }
116 
convertFromAidl(STemplateId templateId)117 int32_t convertFromAidl(STemplateId templateId) {
118     switch(templateId) {
119         case STemplateId::PREVIEW:
120             return camera2::ICameraDeviceUser::TEMPLATE_PREVIEW;
121         case STemplateId::STILL_CAPTURE:
122             return camera2::ICameraDeviceUser::TEMPLATE_STILL_CAPTURE;
123         case STemplateId::RECORD:
124             return camera2::ICameraDeviceUser::TEMPLATE_RECORD;
125         case STemplateId::VIDEO_SNAPSHOT:
126             return camera2::ICameraDeviceUser::TEMPLATE_VIDEO_SNAPSHOT;
127         case STemplateId::ZERO_SHUTTER_LAG:
128             return camera2::ICameraDeviceUser::TEMPLATE_ZERO_SHUTTER_LAG;
129         case STemplateId::MANUAL:
130             return camera2::ICameraDeviceUser::TEMPLATE_MANUAL;
131     }
132 }
133 
convertToAidl(const camera2::utils::SubmitInfo & submitInfo,SSubmitInfo * hSubmitInfo)134 void convertToAidl(const camera2::utils::SubmitInfo& submitInfo, SSubmitInfo* hSubmitInfo) {
135     hSubmitInfo->requestId = submitInfo.mRequestId;
136     hSubmitInfo->lastFrameNumber = submitInfo.mLastFrameNumber;
137 }
138 
139 
convertToAidl(const binder::Status & status)140 SStatus convertToAidl(const binder::Status &status) {
141     if (status.isOk()) {
142         return SStatus::NO_ERROR;
143     }
144     if (status.exceptionCode() != EX_SERVICE_SPECIFIC) {
145         return SStatus::UNKNOWN_ERROR;
146     }
147 
148     switch (status.serviceSpecificErrorCode()) {
149         case hardware::ICameraService::ERROR_DISCONNECTED:
150             return SStatus::DISCONNECTED;
151         case hardware::ICameraService::ERROR_CAMERA_IN_USE:
152             return SStatus::CAMERA_IN_USE;
153         case hardware::ICameraService::ERROR_MAX_CAMERAS_IN_USE:
154             return SStatus::MAX_CAMERAS_IN_USE;
155         case hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT:
156             return SStatus::ILLEGAL_ARGUMENT;
157         case hardware::ICameraService::ERROR_DEPRECATED_HAL:
158             // Should not reach here since we filtered legacy HALs earlier
159             return SStatus::DEPRECATED_HAL;
160         case hardware::ICameraService::ERROR_DISABLED:
161             return SStatus::DISABLED;
162         case hardware::ICameraService::ERROR_PERMISSION_DENIED:
163             return SStatus::PERMISSION_DENIED;
164         case hardware::ICameraService::ERROR_INVALID_OPERATION:
165             return SStatus::INVALID_OPERATION;
166         default:
167             return SStatus::UNKNOWN_ERROR;
168     }
169 }
170 
convertToAidl(const UCaptureResultExtras & src)171 SCaptureResultExtras convertToAidl(const UCaptureResultExtras &src) {
172     SCaptureResultExtras dst;
173     dst.requestId = src.requestId;
174     dst.burstId = src.burstId;
175     dst.frameNumber = src.frameNumber;
176     dst.partialResultCount = src.partialResultCount;
177     dst.errorStreamId = src.errorStreamId;
178     dst.errorPhysicalCameraId = String8(src.errorPhysicalCameraId).string();
179     return dst;
180 }
181 
convertToAidl(int32_t errorCode)182 SErrorCode convertToAidl(int32_t errorCode) {
183     switch(errorCode) {
184         case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED:
185             return SErrorCode::CAMERA_DISCONNECTED;
186         case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE :
187             return SErrorCode::CAMERA_DEVICE;
188         case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE:
189             return SErrorCode::CAMERA_SERVICE;
190         case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST:
191             return SErrorCode::CAMERA_REQUEST;
192         case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT:
193             return SErrorCode::CAMERA_RESULT;
194         case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER:
195             return SErrorCode::CAMERA_BUFFER;
196         case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED:
197             return SErrorCode::CAMERA_DISABLED;
198         case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR:
199             return SErrorCode::CAMERA_INVALID_ERROR;
200         default:
201             return SErrorCode::CAMERA_UNKNOWN_ERROR;
202     }
203 }
204 
convertToAidl(const std::vector<UPhysicalCaptureResultInfo> & src,std::shared_ptr<CaptureResultMetadataQueue> & fmq)205 std::vector<SPhysicalCaptureResultInfo> convertToAidl(
206         const std::vector<UPhysicalCaptureResultInfo>& src,
207         std::shared_ptr<CaptureResultMetadataQueue>& fmq) {
208     std::vector<SPhysicalCaptureResultInfo> dst;
209     dst.resize(src.size());
210     size_t i = 0;
211     for (auto &physicalCaptureResultInfo : src) {
212         dst[i++] = convertToAidl(physicalCaptureResultInfo, fmq);
213     }
214     return dst;
215 }
216 
convertToAidl(const UPhysicalCaptureResultInfo & src,std::shared_ptr<CaptureResultMetadataQueue> & fmq)217 SPhysicalCaptureResultInfo convertToAidl(const UPhysicalCaptureResultInfo & src,
218                                          std::shared_ptr<CaptureResultMetadataQueue> & fmq) {
219     SPhysicalCaptureResultInfo dst;
220     dst.physicalCameraId = String8(src.mPhysicalCameraId).string();
221 
222     const camera_metadata_t *rawMetadata = src.mPhysicalCameraMetadata.getAndLock();
223     // Try using fmq at first.
224     size_t metadata_size = get_camera_metadata_size(rawMetadata);
225     if ((metadata_size > 0) && (fmq->availableToWrite() > 0)) {
226         if (fmq->write((int8_t *)rawMetadata, metadata_size)) {
227             dst.physicalCameraMetadata.set<SCaptureMetadataInfo::fmqMetadataSize>(metadata_size);
228         } else {
229             ALOGW("%s Couldn't use fmq, falling back to hwbinder", __FUNCTION__);
230             SCameraMetadata metadata;
231             cloneToAidl(rawMetadata, &metadata);
232             dst.physicalCameraMetadata.set<SCaptureMetadataInfo::metadata>(std::move(metadata));
233         }
234     }
235     src.mPhysicalCameraMetadata.unlock(rawMetadata);
236     return dst;
237 }
238 
convertToAidl(const std::vector<hardware::CameraStatus> & src,std::vector<SCameraStatusAndId> * dst)239 void convertToAidl(const std::vector<hardware::CameraStatus> &src,
240                    std::vector<SCameraStatusAndId>* dst) {
241     dst->resize(src.size());
242     size_t i = 0;
243     for (const auto &statusAndId : src) {
244         auto &a = (*dst)[i++];
245         a.cameraId = statusAndId.cameraId.c_str();
246         a.deviceStatus = convertCameraStatusToAidl(statusAndId.status);
247         size_t numUnvailPhysicalCameras = statusAndId.unavailablePhysicalIds.size();
248         a.unavailPhysicalCameraIds.resize(numUnvailPhysicalCameras);
249         for (size_t j = 0; j < numUnvailPhysicalCameras; j++) {
250             a.unavailPhysicalCameraIds[j] = statusAndId.unavailablePhysicalIds[j].c_str();
251         }
252     }
253 }
254 
convertCameraStatusToAidl(int32_t src)255 SCameraDeviceStatus convertCameraStatusToAidl(int32_t src) {
256     SCameraDeviceStatus deviceStatus = SCameraDeviceStatus::STATUS_UNKNOWN;
257     switch(src) {
258         case hardware::ICameraServiceListener::STATUS_NOT_PRESENT:
259             deviceStatus = SCameraDeviceStatus::STATUS_NOT_PRESENT;
260             break;
261         case hardware::ICameraServiceListener::STATUS_PRESENT:
262             deviceStatus = SCameraDeviceStatus::STATUS_PRESENT;
263             break;
264         case hardware::ICameraServiceListener::STATUS_ENUMERATING:
265             deviceStatus = SCameraDeviceStatus::STATUS_ENUMERATING;
266             break;
267         case hardware::ICameraServiceListener::STATUS_NOT_AVAILABLE:
268             deviceStatus = SCameraDeviceStatus::STATUS_NOT_AVAILABLE;
269             break;
270         default:
271             break;
272     }
273     return deviceStatus;
274 }
275 
areBindersEqual(const ndk::SpAIBinder & b1,const ndk::SpAIBinder & b2)276 bool areBindersEqual(const ndk::SpAIBinder& b1, const ndk::SpAIBinder& b2) {
277     return !AIBinder_lt(b1.get(), b2.get()) && !AIBinder_lt(b2.get(), b1.get());
278 }
279 
filterVndkKeys(int vndkVersion,CameraMetadata & metadata,bool isStatic)280 status_t filterVndkKeys(int vndkVersion, CameraMetadata &metadata, bool isStatic) {
281     if (vndkVersion == __ANDROID_API_FUTURE__) {
282         // VNDK version in ro.vndk.version is a version code-name that
283         // corresponds to the current version.
284         return OK;
285     }
286     const auto &apiLevelToKeys =
287             isStatic ? static_api_level_to_keys : dynamic_api_level_to_keys;
288     // Find the vndk versions above the given vndk version. All the vndk
289     // versions above the given one, need to have their keys filtered from the
290     // metadata in order to avoid metadata invalidation.
291     auto it = apiLevelToKeys.upper_bound(vndkVersion);
292     while (it != apiLevelToKeys.end()) {
293         for (const auto &key : it->second) {
294             status_t res = metadata.erase(key);
295             if (res != OK) {
296                 ALOGE("%s metadata key %d could not be erased", __FUNCTION__, key);
297                 return res;
298             }
299         }
300         it++;
301     }
302     return OK;
303 }
304 
305 } // namespace android::hardware::cameraservice::utils::conversion::aidl