Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 "Camera3-ZoomRatioMapper" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| 20 | #include <algorithm> |
| 21 | |
| 22 | #include "device3/ZoomRatioMapper.h" |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 23 | #include "utils/SessionConfigurationUtils.h" |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | namespace camera3 { |
| 28 | |
Shuzhen Wang | 8c75a64 | 2020-10-29 21:58:53 -0700 | [diff] [blame] | 29 | void ZoomRatioMapper::initRemappedKeys() { |
| 30 | mRemappedKeys.insert( |
| 31 | kMeteringRegionsToCorrect.begin(), |
| 32 | kMeteringRegionsToCorrect.end()); |
| 33 | mRemappedKeys.insert( |
| 34 | kRectsToCorrect.begin(), |
| 35 | kRectsToCorrect.end()); |
| 36 | mRemappedKeys.insert( |
| 37 | kResultPointsToCorrectNoClamp.begin(), |
| 38 | kResultPointsToCorrectNoClamp.end()); |
| 39 | |
| 40 | mRemappedKeys.insert(ANDROID_CONTROL_ZOOM_RATIO); |
| 41 | } |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 42 | |
| 43 | status_t ZoomRatioMapper::initZoomRatioInTemplate(CameraMetadata *request) { |
| 44 | camera_metadata_entry_t entry; |
| 45 | entry = request->find(ANDROID_CONTROL_ZOOM_RATIO); |
| 46 | float defaultZoomRatio = 1.0f; |
| 47 | if (entry.count == 0) { |
| 48 | return request->update(ANDROID_CONTROL_ZOOM_RATIO, &defaultZoomRatio, 1); |
| 49 | } |
| 50 | return OK; |
| 51 | } |
| 52 | |
| 53 | status_t ZoomRatioMapper::overrideZoomRatioTags( |
| 54 | CameraMetadata* deviceInfo, bool* supportNativeZoomRatio) { |
| 55 | if (deviceInfo == nullptr || supportNativeZoomRatio == nullptr) { |
| 56 | return BAD_VALUE; |
| 57 | } |
| 58 | |
| 59 | camera_metadata_entry_t entry; |
| 60 | entry = deviceInfo->find(ANDROID_CONTROL_ZOOM_RATIO_RANGE); |
| 61 | if (entry.count != 2 && entry.count != 0) return BAD_VALUE; |
| 62 | |
| 63 | // Hal has zoom ratio support |
| 64 | if (entry.count == 2) { |
| 65 | *supportNativeZoomRatio = true; |
| 66 | return OK; |
| 67 | } |
| 68 | |
| 69 | // Hal has no zoom ratio support |
| 70 | *supportNativeZoomRatio = false; |
| 71 | |
| 72 | entry = deviceInfo->find(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM); |
| 73 | if (entry.count != 1) { |
| 74 | ALOGI("%s: Camera device doesn't support SCALER_AVAILABLE_MAX_DIGITAL_ZOOM key!", |
| 75 | __FUNCTION__); |
| 76 | return OK; |
| 77 | } |
| 78 | |
| 79 | float zoomRange[] = {1.0f, entry.data.f[0]}; |
| 80 | status_t res = deviceInfo->update(ANDROID_CONTROL_ZOOM_RATIO_RANGE, zoomRange, 2); |
| 81 | if (res != OK) { |
| 82 | ALOGE("%s: Failed to update CONTROL_ZOOM_RATIO_RANGE key: %s (%d)", |
| 83 | __FUNCTION__, strerror(-res), res); |
| 84 | return res; |
| 85 | } |
| 86 | |
| 87 | std::vector<int32_t> requestKeys; |
| 88 | entry = deviceInfo->find(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS); |
| 89 | if (entry.count > 0) { |
| 90 | requestKeys.insert(requestKeys.end(), entry.data.i32, entry.data.i32 + entry.count); |
| 91 | } |
| 92 | requestKeys.push_back(ANDROID_CONTROL_ZOOM_RATIO); |
| 93 | res = deviceInfo->update(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS, |
| 94 | requestKeys.data(), requestKeys.size()); |
| 95 | if (res != OK) { |
| 96 | ALOGE("%s: Failed to update REQUEST_AVAILABLE_REQUEST_KEYS: %s (%d)", |
| 97 | __FUNCTION__, strerror(-res), res); |
| 98 | return res; |
| 99 | } |
| 100 | |
| 101 | std::vector<int32_t> resultKeys; |
| 102 | entry = deviceInfo->find(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS); |
| 103 | if (entry.count > 0) { |
| 104 | resultKeys.insert(resultKeys.end(), entry.data.i32, entry.data.i32 + entry.count); |
| 105 | } |
| 106 | resultKeys.push_back(ANDROID_CONTROL_ZOOM_RATIO); |
| 107 | res = deviceInfo->update(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS, |
| 108 | resultKeys.data(), resultKeys.size()); |
| 109 | if (res != OK) { |
| 110 | ALOGE("%s: Failed to update REQUEST_AVAILABLE_RESULT_KEYS: %s (%d)", |
| 111 | __FUNCTION__, strerror(-res), res); |
| 112 | return res; |
| 113 | } |
| 114 | |
| 115 | std::vector<int32_t> charKeys; |
| 116 | entry = deviceInfo->find(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS); |
| 117 | if (entry.count > 0) { |
| 118 | charKeys.insert(charKeys.end(), entry.data.i32, entry.data.i32 + entry.count); |
| 119 | } |
| 120 | charKeys.push_back(ANDROID_CONTROL_ZOOM_RATIO_RANGE); |
| 121 | res = deviceInfo->update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, |
| 122 | charKeys.data(), charKeys.size()); |
| 123 | if (res != OK) { |
| 124 | ALOGE("%s: Failed to update REQUEST_AVAILABLE_CHARACTERISTICS_KEYS: %s (%d)", |
| 125 | __FUNCTION__, strerror(-res), res); |
| 126 | return res; |
| 127 | } |
| 128 | |
| 129 | return OK; |
| 130 | } |
| 131 | |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 132 | static bool getArrayWidthAndHeight(const CameraMetadata *deviceInfo, |
| 133 | int32_t arrayTag, int32_t *width, int32_t *height) { |
| 134 | if (width == nullptr || height == nullptr) { |
| 135 | ALOGE("%s: width / height nullptr", __FUNCTION__); |
| 136 | return false; |
| 137 | } |
| 138 | camera_metadata_ro_entry_t entry; |
| 139 | entry = deviceInfo->find(arrayTag); |
| 140 | if (entry.count != 4) return false; |
| 141 | *width = entry.data.i32[2]; |
| 142 | *height = entry.data.i32[3]; |
| 143 | return true; |
| 144 | } |
| 145 | |
Shuzhen Wang | 1c834da | 2019-12-18 11:17:03 -0800 | [diff] [blame] | 146 | ZoomRatioMapper::ZoomRatioMapper(const CameraMetadata* deviceInfo, |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 147 | bool supportNativeZoomRatio, bool usePrecorrectArray) { |
Shuzhen Wang | 8c75a64 | 2020-10-29 21:58:53 -0700 | [diff] [blame] | 148 | initRemappedKeys(); |
| 149 | |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 150 | int32_t arrayW = 0; |
| 151 | int32_t arrayH = 0; |
| 152 | int32_t arrayMaximumResolutionW = 0; |
| 153 | int32_t arrayMaximumResolutionH = 0; |
| 154 | int32_t activeW = 0; |
| 155 | int32_t activeH = 0; |
| 156 | int32_t activeMaximumResolutionW = 0; |
| 157 | int32_t activeMaximumResolutionH = 0; |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 158 | |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 159 | if (!getArrayWidthAndHeight(deviceInfo, ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE, |
| 160 | &arrayW, &arrayH)) { |
| 161 | ALOGE("%s: Couldn't get pre correction active array size", __FUNCTION__); |
| 162 | return; |
| 163 | } |
| 164 | if (!getArrayWidthAndHeight(deviceInfo, ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE, |
| 165 | &activeW, &activeH)) { |
| 166 | ALOGE("%s: Couldn't get active array size", __FUNCTION__); |
| 167 | return; |
| 168 | } |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 169 | |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 170 | bool isUltraHighResolutionSensor = |
| 171 | camera3::SessionConfigurationUtils::isUltraHighResolutionSensor(*deviceInfo); |
| 172 | if (isUltraHighResolutionSensor) { |
| 173 | if (!getArrayWidthAndHeight(deviceInfo, |
| 174 | ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE_MAXIMUM_RESOLUTION, |
| 175 | &arrayMaximumResolutionW, &arrayMaximumResolutionH)) { |
| 176 | ALOGE("%s: Couldn't get maximum resolution pre correction active array size", |
| 177 | __FUNCTION__); |
| 178 | return; |
| 179 | } |
| 180 | if (!getArrayWidthAndHeight(deviceInfo, |
| 181 | ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE_MAXIMUM_RESOLUTION, |
| 182 | &activeMaximumResolutionW, &activeMaximumResolutionH)) { |
| 183 | ALOGE("%s: Couldn't get maximum resolution pre correction active array size", |
| 184 | __FUNCTION__); |
| 185 | return; |
| 186 | } |
| 187 | } |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 188 | |
| 189 | if (usePrecorrectArray) { |
| 190 | mArrayWidth = arrayW; |
| 191 | mArrayHeight = arrayH; |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 192 | mArrayWidthMaximumResolution = arrayMaximumResolutionW; |
| 193 | mArrayHeightMaximumResolution = arrayMaximumResolutionH; |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 194 | } else { |
| 195 | mArrayWidth = activeW; |
| 196 | mArrayHeight = activeH; |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 197 | mArrayWidthMaximumResolution = activeMaximumResolutionW; |
| 198 | mArrayHeightMaximumResolution = activeMaximumResolutionH; |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 199 | } |
| 200 | mHalSupportsZoomRatio = supportNativeZoomRatio; |
| 201 | |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 202 | ALOGV("%s: array size: %d x %d, full res array size: %d x %d, mHalSupportsZoomRatio %d", |
| 203 | __FUNCTION__, mArrayWidth, mArrayHeight, mArrayWidthMaximumResolution, |
| 204 | mArrayHeightMaximumResolution, mHalSupportsZoomRatio); |
Shuzhen Wang | 1c834da | 2019-12-18 11:17:03 -0800 | [diff] [blame] | 205 | mIsValid = true; |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 208 | status_t ZoomRatioMapper::getArrayDimensionsToBeUsed(const CameraMetadata *settings, |
| 209 | int32_t *arrayWidth, int32_t *arrayHeight) { |
| 210 | if (settings == nullptr || arrayWidth == nullptr || arrayHeight == nullptr) { |
| 211 | return BAD_VALUE; |
| 212 | } |
| 213 | // First we get the sensorPixelMode from the settings metadata. |
| 214 | int32_t sensorPixelMode = ANDROID_SENSOR_PIXEL_MODE_DEFAULT; |
| 215 | camera_metadata_ro_entry sensorPixelModeEntry = settings->find(ANDROID_SENSOR_PIXEL_MODE); |
| 216 | if (sensorPixelModeEntry.count != 0) { |
| 217 | sensorPixelMode = sensorPixelModeEntry.data.u8[0]; |
| 218 | if (sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_DEFAULT && |
| 219 | sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) { |
| 220 | ALOGE("%s: Request sensor pixel mode is not one of the valid values %d", |
| 221 | __FUNCTION__, sensorPixelMode); |
| 222 | return BAD_VALUE; |
| 223 | } |
| 224 | } |
| 225 | if (sensorPixelMode == ANDROID_SENSOR_PIXEL_MODE_DEFAULT) { |
| 226 | *arrayWidth = mArrayWidth; |
| 227 | *arrayHeight = mArrayHeight; |
| 228 | } else { |
| 229 | *arrayWidth = mArrayWidthMaximumResolution; |
| 230 | *arrayHeight = mArrayHeightMaximumResolution; |
| 231 | } |
| 232 | return OK; |
| 233 | } |
| 234 | |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 235 | status_t ZoomRatioMapper::updateCaptureRequest(CameraMetadata* request) { |
Shuzhen Wang | 1c834da | 2019-12-18 11:17:03 -0800 | [diff] [blame] | 236 | if (!mIsValid) return INVALID_OPERATION; |
| 237 | |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 238 | status_t res = OK; |
| 239 | bool zoomRatioIs1 = true; |
| 240 | camera_metadata_entry_t entry; |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 241 | int arrayHeight, arrayWidth = 0; |
| 242 | res = getArrayDimensionsToBeUsed(request, &arrayWidth, &arrayHeight); |
| 243 | if (res != OK) { |
| 244 | return res; |
| 245 | } |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 246 | entry = request->find(ANDROID_CONTROL_ZOOM_RATIO); |
| 247 | if (entry.count == 1 && entry.data.f[0] != 1.0f) { |
| 248 | zoomRatioIs1 = false; |
Shuzhen Wang | c2dae59 | 2020-11-30 20:16:06 -0800 | [diff] [blame] | 249 | |
| 250 | // If cropRegion is windowboxing, override it with activeArray |
| 251 | camera_metadata_entry_t cropRegionEntry = request->find(ANDROID_SCALER_CROP_REGION); |
| 252 | if (cropRegionEntry.count == 4) { |
| 253 | int cropWidth = cropRegionEntry.data.i32[2]; |
| 254 | int cropHeight = cropRegionEntry.data.i32[3]; |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 255 | if (cropWidth < arrayWidth && cropHeight < arrayHeight) { |
Shuzhen Wang | c2dae59 | 2020-11-30 20:16:06 -0800 | [diff] [blame] | 256 | cropRegionEntry.data.i32[0] = 0; |
| 257 | cropRegionEntry.data.i32[1] = 0; |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 258 | cropRegionEntry.data.i32[2] = arrayWidth; |
| 259 | cropRegionEntry.data.i32[3] = arrayHeight; |
Shuzhen Wang | c2dae59 | 2020-11-30 20:16:06 -0800 | [diff] [blame] | 260 | } |
| 261 | } |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | if (mHalSupportsZoomRatio && zoomRatioIs1) { |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 265 | res = separateZoomFromCropLocked(request, false/*isResult*/, arrayWidth, arrayHeight); |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 266 | } else if (!mHalSupportsZoomRatio && !zoomRatioIs1) { |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 267 | res = combineZoomAndCropLocked(request, false/*isResult*/, arrayWidth, arrayHeight); |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | // If CONTROL_ZOOM_RATIO is in request, but HAL doesn't support |
| 271 | // CONTROL_ZOOM_RATIO, remove it from the request. |
| 272 | if (!mHalSupportsZoomRatio && entry.count == 1) { |
| 273 | request->erase(ANDROID_CONTROL_ZOOM_RATIO); |
| 274 | } |
| 275 | |
| 276 | return res; |
| 277 | } |
| 278 | |
| 279 | status_t ZoomRatioMapper::updateCaptureResult(CameraMetadata* result, bool requestedZoomRatioIs1) { |
Shuzhen Wang | 1c834da | 2019-12-18 11:17:03 -0800 | [diff] [blame] | 280 | if (!mIsValid) return INVALID_OPERATION; |
| 281 | |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 282 | status_t res = OK; |
| 283 | |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 284 | int arrayHeight, arrayWidth = 0; |
| 285 | res = getArrayDimensionsToBeUsed(result, &arrayWidth, &arrayHeight); |
| 286 | if (res != OK) { |
| 287 | return res; |
| 288 | } |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 289 | if (mHalSupportsZoomRatio && requestedZoomRatioIs1) { |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 290 | res = combineZoomAndCropLocked(result, true/*isResult*/, arrayWidth, arrayHeight); |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 291 | } else if (!mHalSupportsZoomRatio && !requestedZoomRatioIs1) { |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 292 | res = separateZoomFromCropLocked(result, true/*isResult*/, arrayWidth, arrayHeight); |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 293 | } else { |
| 294 | camera_metadata_entry_t entry = result->find(ANDROID_CONTROL_ZOOM_RATIO); |
| 295 | if (entry.count == 0) { |
| 296 | float zoomRatio1x = 1.0f; |
| 297 | result->update(ANDROID_CONTROL_ZOOM_RATIO, &zoomRatio1x, 1); |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | return res; |
| 302 | } |
| 303 | |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 304 | status_t ZoomRatioMapper::deriveZoomRatio(const CameraMetadata* metadata, float *zoomRatioRet, |
| 305 | int arrayWidth, int arrayHeight) { |
| 306 | if (metadata == nullptr || zoomRatioRet == nullptr) { |
| 307 | return BAD_VALUE; |
| 308 | } |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 309 | float zoomRatio = 1.0; |
| 310 | |
| 311 | camera_metadata_ro_entry_t entry; |
| 312 | entry = metadata->find(ANDROID_SCALER_CROP_REGION); |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 313 | if (entry.count != 4) { |
| 314 | *zoomRatioRet = 1; |
| 315 | return OK; |
| 316 | } |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 317 | // Center of the preCorrection/active size |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 318 | float arrayCenterX = arrayWidth / 2.0; |
| 319 | float arrayCenterY = arrayHeight / 2.0; |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 320 | |
| 321 | // Re-map crop region to coordinate system centered to (arrayCenterX, |
| 322 | // arrayCenterY). |
| 323 | float cropRegionLeft = arrayCenterX - entry.data.i32[0] ; |
| 324 | float cropRegionTop = arrayCenterY - entry.data.i32[1]; |
| 325 | float cropRegionRight = entry.data.i32[0] + entry.data.i32[2] - arrayCenterX; |
| 326 | float cropRegionBottom = entry.data.i32[1] + entry.data.i32[3] - arrayCenterY; |
| 327 | |
| 328 | // Calculate the scaling factor for left, top, bottom, right |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 329 | float zoomRatioLeft = std::max(arrayWidth / (2 * cropRegionLeft), 1.0f); |
| 330 | float zoomRatioTop = std::max(arrayHeight / (2 * cropRegionTop), 1.0f); |
| 331 | float zoomRatioRight = std::max(arrayWidth / (2 * cropRegionRight), 1.0f); |
| 332 | float zoomRatioBottom = std::max(arrayHeight / (2 * cropRegionBottom), 1.0f); |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 333 | |
| 334 | // Use minimum scaling factor to handle letterboxing or pillarboxing |
| 335 | zoomRatio = std::min(std::min(zoomRatioLeft, zoomRatioRight), |
| 336 | std::min(zoomRatioTop, zoomRatioBottom)); |
| 337 | |
| 338 | ALOGV("%s: derived zoomRatio is %f", __FUNCTION__, zoomRatio); |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 339 | *zoomRatioRet = zoomRatio; |
| 340 | return OK; |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 341 | } |
| 342 | |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 343 | status_t ZoomRatioMapper::separateZoomFromCropLocked(CameraMetadata* metadata, bool isResult, |
| 344 | int arrayWidth, int arrayHeight) { |
| 345 | float zoomRatio = 1.0; |
| 346 | status_t res = deriveZoomRatio(metadata, &zoomRatio, arrayWidth, arrayHeight); |
| 347 | |
| 348 | if (res != OK) { |
| 349 | ALOGE("%s: Failed to derive zoom ratio: %s(%d)", |
| 350 | __FUNCTION__, strerror(-res), res); |
| 351 | return res; |
| 352 | } |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 353 | |
| 354 | // Update zoomRatio metadata tag |
| 355 | res = metadata->update(ANDROID_CONTROL_ZOOM_RATIO, &zoomRatio, 1); |
| 356 | if (res != OK) { |
| 357 | ALOGE("%s: Failed to update ANDROID_CONTROL_ZOOM_RATIO: %s(%d)", |
| 358 | __FUNCTION__, strerror(-res), res); |
| 359 | return res; |
| 360 | } |
| 361 | |
| 362 | // Scale regions using zoomRatio |
| 363 | camera_metadata_entry_t entry; |
| 364 | for (auto region : kMeteringRegionsToCorrect) { |
| 365 | entry = metadata->find(region); |
| 366 | for (size_t j = 0; j < entry.count; j += 5) { |
| 367 | int32_t weight = entry.data.i32[j + 4]; |
| 368 | if (weight == 0) { |
| 369 | continue; |
| 370 | } |
Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 371 | // Top left (inclusive) |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 372 | scaleCoordinates(entry.data.i32 + j, 1, zoomRatio, true /*clamp*/, arrayWidth, |
| 373 | arrayHeight); |
Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 374 | // Bottom right (exclusive): Use adjacent inclusive pixel to |
| 375 | // calculate. |
| 376 | entry.data.i32[j+2] -= 1; |
| 377 | entry.data.i32[j+3] -= 1; |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 378 | scaleCoordinates(entry.data.i32 + j + 2, 1, zoomRatio, true /*clamp*/, arrayWidth, |
| 379 | arrayHeight); |
Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 380 | entry.data.i32[j+2] += 1; |
| 381 | entry.data.i32[j+3] += 1; |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | |
| 385 | for (auto rect : kRectsToCorrect) { |
| 386 | entry = metadata->find(rect); |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 387 | scaleRects(entry.data.i32, entry.count / 4, zoomRatio, arrayWidth, arrayHeight); |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | if (isResult) { |
| 391 | for (auto pts : kResultPointsToCorrectNoClamp) { |
| 392 | entry = metadata->find(pts); |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 393 | scaleCoordinates(entry.data.i32, entry.count / 2, zoomRatio, false /*clamp*/, |
| 394 | arrayWidth, arrayHeight); |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 395 | } |
| 396 | } |
| 397 | |
| 398 | return OK; |
| 399 | } |
| 400 | |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 401 | status_t ZoomRatioMapper::combineZoomAndCropLocked(CameraMetadata* metadata, bool isResult, |
| 402 | int arrayWidth, int arrayHeight) { |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 403 | float zoomRatio = 1.0f; |
| 404 | camera_metadata_entry_t entry; |
| 405 | entry = metadata->find(ANDROID_CONTROL_ZOOM_RATIO); |
| 406 | if (entry.count == 1) { |
| 407 | zoomRatio = entry.data.f[0]; |
| 408 | } |
| 409 | |
| 410 | // Unscale regions with zoomRatio |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 411 | for (auto region : kMeteringRegionsToCorrect) { |
| 412 | entry = metadata->find(region); |
| 413 | for (size_t j = 0; j < entry.count; j += 5) { |
| 414 | int32_t weight = entry.data.i32[j + 4]; |
| 415 | if (weight == 0) { |
| 416 | continue; |
| 417 | } |
Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 418 | // Top-left (inclusive) |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 419 | scaleCoordinates(entry.data.i32 + j, 1, 1.0 / zoomRatio, true /*clamp*/, arrayWidth, |
| 420 | arrayHeight); |
Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 421 | // Bottom-right (exclusive): Use adjacent inclusive pixel to |
| 422 | // calculate. |
| 423 | entry.data.i32[j+2] -= 1; |
| 424 | entry.data.i32[j+3] -= 1; |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 425 | scaleCoordinates(entry.data.i32 + j + 2, 1, 1.0 / zoomRatio, true /*clamp*/, arrayWidth, |
| 426 | arrayHeight); |
Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 427 | entry.data.i32[j+2] += 1; |
| 428 | entry.data.i32[j+3] += 1; |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | for (auto rect : kRectsToCorrect) { |
| 432 | entry = metadata->find(rect); |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 433 | scaleRects(entry.data.i32, entry.count / 4, 1.0 / zoomRatio, arrayWidth, arrayHeight); |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 434 | } |
| 435 | if (isResult) { |
| 436 | for (auto pts : kResultPointsToCorrectNoClamp) { |
| 437 | entry = metadata->find(pts); |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 438 | scaleCoordinates(entry.data.i32, entry.count / 2, 1.0 / zoomRatio, false /*clamp*/, |
| 439 | arrayWidth, arrayHeight); |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 440 | } |
| 441 | } |
| 442 | |
| 443 | zoomRatio = 1.0; |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 444 | status_t res = metadata->update(ANDROID_CONTROL_ZOOM_RATIO, &zoomRatio, 1); |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 445 | if (res != OK) { |
| 446 | return res; |
| 447 | } |
| 448 | |
| 449 | return OK; |
| 450 | } |
| 451 | |
| 452 | void ZoomRatioMapper::scaleCoordinates(int32_t* coordPairs, int coordCount, |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 453 | float scaleRatio, bool clamp, int32_t arrayWidth, int32_t arrayHeight) { |
Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 454 | // A pixel's coordinate is represented by the position of its top-left corner. |
| 455 | // To avoid the rounding error, we use the coordinate for the center of the |
| 456 | // pixel instead: |
| 457 | // 1. First shift the coordinate system half pixel both horizontally and |
| 458 | // vertically, so that [x, y] is the center of the pixel, not the top-left corner. |
| 459 | // 2. Do zoom operation to scale the coordinate relative to the center of |
| 460 | // the active array (shifted by 0.5 pixel as well). |
| 461 | // 3. Shift the coordinate system back by directly using the pixel center |
| 462 | // coordinate. |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 463 | for (int i = 0; i < coordCount * 2; i += 2) { |
| 464 | float x = coordPairs[i]; |
| 465 | float y = coordPairs[i + 1]; |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 466 | float xCentered = x - (arrayWidth - 2) / 2; |
| 467 | float yCentered = y - (arrayHeight - 2) / 2; |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 468 | float scaledX = xCentered * scaleRatio; |
| 469 | float scaledY = yCentered * scaleRatio; |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 470 | scaledX += (arrayWidth - 2) / 2; |
| 471 | scaledY += (arrayHeight - 2) / 2; |
Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 472 | coordPairs[i] = static_cast<int32_t>(std::round(scaledX)); |
| 473 | coordPairs[i+1] = static_cast<int32_t>(std::round(scaledY)); |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 474 | // Clamp to within activeArray/preCorrectionActiveArray |
Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 475 | if (clamp) { |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 476 | int32_t right = arrayWidth - 1; |
| 477 | int32_t bottom = arrayHeight - 1; |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 478 | coordPairs[i] = |
| 479 | std::min(right, std::max(0, coordPairs[i])); |
| 480 | coordPairs[i+1] = |
| 481 | std::min(bottom, std::max(0, coordPairs[i+1])); |
| 482 | } |
| 483 | ALOGV("%s: coordinates: %d, %d", __FUNCTION__, coordPairs[i], coordPairs[i+1]); |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | void ZoomRatioMapper::scaleRects(int32_t* rects, int rectCount, |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 488 | float scaleRatio, int32_t arrayWidth, int32_t arrayHeight) { |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 489 | for (int i = 0; i < rectCount * 4; i += 4) { |
Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 490 | // Map from (l, t, width, height) to (l, t, l+width-1, t+height-1), |
| 491 | // where both top-left and bottom-right are inclusive. |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 492 | int32_t coords[4] = { |
| 493 | rects[i], |
| 494 | rects[i + 1], |
Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 495 | rects[i] + rects[i + 2] - 1, |
| 496 | rects[i + 1] + rects[i + 3] - 1 |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 497 | }; |
| 498 | |
| 499 | // top-left |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 500 | scaleCoordinates(coords, 1, scaleRatio, true /*clamp*/, arrayWidth, arrayHeight); |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 501 | // bottom-right |
Jayant Chowdhary | 13f9b2f | 2020-12-02 22:46:15 -0800 | [diff] [blame] | 502 | scaleCoordinates(coords+2, 1, scaleRatio, true /*clamp*/, arrayWidth, arrayHeight); |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 503 | |
| 504 | // Map back to (l, t, width, height) |
| 505 | rects[i] = coords[0]; |
| 506 | rects[i + 1] = coords[1]; |
Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 507 | rects[i + 2] = coords[2] - coords[0] + 1; |
| 508 | rects[i + 3] = coords[3] - coords[1] + 1; |
Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 509 | } |
| 510 | } |
| 511 | |
| 512 | } // namespace camera3 |
| 513 | |
| 514 | } // namespace android |