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