Jayant Chowdhary | be543d4 | 2018-08-15 13:16:14 -0700 | [diff] [blame^] | 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 <NdkImageReaderPriv.h> |
| 20 | |
| 21 | namespace android { |
| 22 | namespace hardware { |
| 23 | namespace cameraservice { |
| 24 | namespace utils { |
| 25 | namespace conversion { |
| 26 | |
| 27 | using hardware::graphics::bufferqueue::V1_0::utils::H2BGraphicBufferProducer; |
| 28 | |
| 29 | // Note: existing data in dst will be gone. Caller still owns the memory of src |
| 30 | void convertToHidl(const camera_metadata_t *src, HCameraMetadata* dst) { |
| 31 | if (src == nullptr) { |
| 32 | ALOGW("%s:attempt to convert empty metadata to Hidl", __FUNCTION__); |
| 33 | return; |
| 34 | } |
| 35 | size_t size = get_camera_metadata_size(src); |
| 36 | dst->setToExternal((uint8_t *) src, size); |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | int32_t convertFromHidl(HStreamConfigurationMode streamConfigurationMode) { |
| 41 | switch (streamConfigurationMode) { |
| 42 | case HStreamConfigurationMode::CONSTRAINED_HIGH_SPEED_MODE: |
| 43 | return camera2::ICameraDeviceUser::CONSTRAINED_HIGH_SPEED_MODE; |
| 44 | case HStreamConfigurationMode::NORMAL_MODE: |
| 45 | return camera2::ICameraDeviceUser::NORMAL_MODE; |
| 46 | default: |
| 47 | // TODO: Fix this |
| 48 | return camera2::ICameraDeviceUser::VENDOR_MODE_START; |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | int32_t convertFromHidl(HTemplateId templateId) { |
| 53 | switch(templateId) { |
| 54 | case HTemplateId::PREVIEW: |
| 55 | return camera2::ICameraDeviceUser::TEMPLATE_PREVIEW; |
| 56 | case HTemplateId::STILL_CAPTURE: |
| 57 | return camera2::ICameraDeviceUser::TEMPLATE_STILL_CAPTURE; |
| 58 | case HTemplateId::RECORD: |
| 59 | return camera2::ICameraDeviceUser::TEMPLATE_RECORD; |
| 60 | case HTemplateId::VIDEO_SNAPSHOT: |
| 61 | return camera2::ICameraDeviceUser::TEMPLATE_VIDEO_SNAPSHOT; |
| 62 | case HTemplateId::ZERO_SHUTTER_LAG: |
| 63 | return camera2::ICameraDeviceUser::TEMPLATE_ZERO_SHUTTER_LAG; |
| 64 | case HTemplateId::MANUAL: |
| 65 | return camera2::ICameraDeviceUser::TEMPLATE_MANUAL; |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | int convertFromHidl(HOutputConfiguration::Rotation rotation) { |
| 70 | switch(rotation) { |
| 71 | case HOutputConfiguration::Rotation::R0: |
| 72 | return 0; |
| 73 | case HOutputConfiguration::Rotation::R90: |
| 74 | return 1; |
| 75 | case HOutputConfiguration::Rotation::R180: |
| 76 | return 2; |
| 77 | case HOutputConfiguration::Rotation::R270: |
| 78 | return 3; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | hardware::camera2::params::OutputConfiguration convertFromHidl( |
| 83 | const HOutputConfiguration &hOutputConfiguration) { |
| 84 | std::vector<sp<IGraphicBufferProducer>> iGBPs; |
| 85 | auto &windowHandles = hOutputConfiguration.windowHandles; |
| 86 | iGBPs.reserve(windowHandles.size()); |
| 87 | for (auto &handle : windowHandles) { |
| 88 | iGBPs.push_back(new H2BGraphicBufferProducer(AImageReader_getHGBPFromHandle(handle))); |
| 89 | } |
| 90 | hardware::camera2::params::OutputConfiguration outputConfiguration( |
| 91 | iGBPs, convertFromHidl(hOutputConfiguration.rotation), |
| 92 | hOutputConfiguration.windowGroupId, OutputConfiguration::SURFACE_TYPE_UNKNOWN, 0, 0, |
| 93 | (windowHandles.size() > 1)); |
| 94 | return outputConfiguration; |
| 95 | } |
| 96 | |
| 97 | // The camera metadata here is cloned. Since we're reading metadata over |
| 98 | // hwbinder we would need to clone it in order to avoid aligment issues. |
| 99 | bool convertFromHidl(const HCameraMetadata &src, CameraMetadata *dst) { |
| 100 | const camera_metadata_t *buffer = reinterpret_cast<const camera_metadata_t*>(src.data()); |
| 101 | size_t expectedSize = src.size(); |
| 102 | int res = validate_camera_metadata_structure(buffer, &expectedSize); |
| 103 | if (res == OK || res == CAMERA_METADATA_VALIDATION_SHIFTED) { |
| 104 | *dst = buffer; |
| 105 | } else { |
| 106 | ALOGE("%s: Malformed camera metadata received from HAL", __FUNCTION__); |
| 107 | return false; |
| 108 | } |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | HCameraDeviceStatus convertToHidlCameraDeviceStatus(int32_t status) { |
| 113 | HCameraDeviceStatus deviceStatus = HCameraDeviceStatus::STATUS_UNKNOWN; |
| 114 | switch(status) { |
| 115 | case hardware::ICameraServiceListener::STATUS_NOT_PRESENT: |
| 116 | deviceStatus = HCameraDeviceStatus::STATUS_NOT_PRESENT; |
| 117 | break; |
| 118 | case hardware::ICameraServiceListener::STATUS_PRESENT: |
| 119 | deviceStatus = HCameraDeviceStatus::STATUS_PRESENT; |
| 120 | break; |
| 121 | case hardware::ICameraServiceListener::STATUS_ENUMERATING: |
| 122 | deviceStatus = HCameraDeviceStatus::STATUS_ENUMERATING; |
| 123 | break; |
| 124 | case hardware::ICameraServiceListener::STATUS_NOT_AVAILABLE: |
| 125 | deviceStatus = HCameraDeviceStatus::STATUS_NOT_AVAILABLE; |
| 126 | break; |
| 127 | default: |
| 128 | break; |
| 129 | } |
| 130 | return deviceStatus; |
| 131 | } |
| 132 | |
| 133 | HCaptureResultExtras convertToHidl(const CaptureResultExtras &captureResultExtras) { |
| 134 | HCaptureResultExtras hCaptureResultExtras; |
| 135 | hCaptureResultExtras.burstId = captureResultExtras.burstId; |
| 136 | hCaptureResultExtras.frameNumber = captureResultExtras.frameNumber; |
| 137 | hCaptureResultExtras.partialResultCount = captureResultExtras.partialResultCount; |
| 138 | hCaptureResultExtras.errorStreamId = captureResultExtras.errorStreamId; |
| 139 | return hCaptureResultExtras; |
| 140 | } |
| 141 | |
| 142 | HErrorCode convertToHidl(int32_t errorCode) { |
| 143 | switch(errorCode) { |
| 144 | case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISCONNECTED: |
| 145 | return HErrorCode::CAMERA_DISCONNECTED; |
| 146 | case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DEVICE : |
| 147 | return HErrorCode::CAMERA_DEVICE; |
| 148 | case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_SERVICE: |
| 149 | return HErrorCode::CAMERA_SERVICE; |
| 150 | case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_REQUEST: |
| 151 | return HErrorCode::CAMERA_REQUEST; |
| 152 | case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_RESULT: |
| 153 | return HErrorCode::CAMERA_RESULT; |
| 154 | case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_BUFFER: |
| 155 | return HErrorCode::CAMERA_BUFFER; |
| 156 | case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_DISABLED: |
| 157 | return HErrorCode::CAMERA_DISABLED; |
| 158 | case camera2::ICameraDeviceCallbacks::ERROR_CAMERA_INVALID_ERROR: |
| 159 | return HErrorCode::CAMERA_INVALID_ERROR; |
| 160 | default: |
| 161 | return HErrorCode::CAMERA_UNKNOWN_ERROR; |
| 162 | } |
| 163 | } |
| 164 | |
| 165 | void convertToHidl(const std::vector<hardware::CameraStatus> &src, |
| 166 | hidl_vec<HCameraStatusAndId>* dst) { |
| 167 | dst->resize(src.size()); |
| 168 | size_t i = 0; |
| 169 | for (auto &statusAndId : src) { |
| 170 | auto &a = (*dst)[i++]; |
| 171 | a.cameraId = statusAndId.cameraId.c_str(); |
| 172 | a.deviceStatus = convertToHidlCameraDeviceStatus(statusAndId.status); |
| 173 | } |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | void convertToHidl( |
| 178 | const hardware::camera2::utils::SubmitInfo &submitInfo, |
| 179 | frameworks::cameraservice::device::V2_0::SubmitInfo *hSubmitInfo) { |
| 180 | hSubmitInfo->requestId = submitInfo.mRequestId; |
| 181 | hSubmitInfo->lastFrameNumber = submitInfo.mLastFrameNumber; |
| 182 | } |
| 183 | |
| 184 | HStatus B2HStatus(const binder::Status &bStatus) { |
| 185 | HStatus status = HStatus::NO_ERROR; |
| 186 | if (bStatus.isOk()) { |
| 187 | // NO Error here |
| 188 | return status; |
| 189 | } |
| 190 | switch(bStatus.serviceSpecificErrorCode()) { |
| 191 | case hardware::ICameraService::ERROR_DISCONNECTED: |
| 192 | status = HStatus::DISCONNECTED; |
| 193 | break; |
| 194 | case hardware::ICameraService::ERROR_CAMERA_IN_USE: |
| 195 | status = HStatus::CAMERA_IN_USE; |
| 196 | break; |
| 197 | case hardware::ICameraService::ERROR_MAX_CAMERAS_IN_USE: |
| 198 | status = HStatus::MAX_CAMERAS_IN_USE; |
| 199 | break; |
| 200 | case hardware::ICameraService::ERROR_ILLEGAL_ARGUMENT: |
| 201 | status = HStatus::ILLEGAL_ARGUMENT; |
| 202 | break; |
| 203 | case hardware::ICameraService::ERROR_DEPRECATED_HAL: |
| 204 | // Should not reach here since we filtered legacy HALs earlier |
| 205 | status = HStatus::DEPRECATED_HAL; |
| 206 | break; |
| 207 | case hardware::ICameraService::ERROR_DISABLED: |
| 208 | status = HStatus::DISABLED; |
| 209 | break; |
| 210 | case hardware::ICameraService::ERROR_PERMISSION_DENIED: |
| 211 | status = HStatus::PERMISSION_DENIED; |
| 212 | break; |
| 213 | case hardware::ICameraService::ERROR_INVALID_OPERATION: |
| 214 | status = HStatus::INVALID_OPERATION; |
| 215 | break; |
| 216 | default: |
| 217 | status = HStatus::UNKNOWN_ERROR; |
| 218 | break; |
| 219 | } |
| 220 | return status; |
| 221 | } |
| 222 | |
| 223 | } //conversion |
| 224 | } // utils |
| 225 | } //cameraservice |
| 226 | } // hardware |
| 227 | } // android |