| 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" | 
|  | 23 |  | 
|  | 24 | namespace android { | 
|  | 25 |  | 
|  | 26 | namespace camera3 { | 
|  | 27 |  | 
| Shuzhen Wang | 8c75a64 | 2020-10-29 21:58:53 -0700 | [diff] [blame] | 28 | void ZoomRatioMapper::initRemappedKeys() { | 
|  | 29 | mRemappedKeys.insert( | 
|  | 30 | kMeteringRegionsToCorrect.begin(), | 
|  | 31 | kMeteringRegionsToCorrect.end()); | 
|  | 32 | mRemappedKeys.insert( | 
|  | 33 | kRectsToCorrect.begin(), | 
|  | 34 | kRectsToCorrect.end()); | 
|  | 35 | mRemappedKeys.insert( | 
|  | 36 | kResultPointsToCorrectNoClamp.begin(), | 
|  | 37 | kResultPointsToCorrectNoClamp.end()); | 
|  | 38 |  | 
|  | 39 | mRemappedKeys.insert(ANDROID_CONTROL_ZOOM_RATIO); | 
|  | 40 | } | 
| Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 41 |  | 
|  | 42 | status_t ZoomRatioMapper::initZoomRatioInTemplate(CameraMetadata *request) { | 
|  | 43 | camera_metadata_entry_t entry; | 
|  | 44 | entry = request->find(ANDROID_CONTROL_ZOOM_RATIO); | 
|  | 45 | float defaultZoomRatio = 1.0f; | 
|  | 46 | if (entry.count == 0) { | 
|  | 47 | return request->update(ANDROID_CONTROL_ZOOM_RATIO, &defaultZoomRatio, 1); | 
|  | 48 | } | 
|  | 49 | return OK; | 
|  | 50 | } | 
|  | 51 |  | 
|  | 52 | status_t ZoomRatioMapper::overrideZoomRatioTags( | 
|  | 53 | CameraMetadata* deviceInfo, bool* supportNativeZoomRatio) { | 
|  | 54 | if (deviceInfo == nullptr || supportNativeZoomRatio == nullptr) { | 
|  | 55 | return BAD_VALUE; | 
|  | 56 | } | 
|  | 57 |  | 
|  | 58 | camera_metadata_entry_t entry; | 
|  | 59 | entry = deviceInfo->find(ANDROID_CONTROL_ZOOM_RATIO_RANGE); | 
|  | 60 | if (entry.count != 2 && entry.count != 0) return BAD_VALUE; | 
|  | 61 |  | 
|  | 62 | // Hal has zoom ratio support | 
|  | 63 | if (entry.count == 2) { | 
|  | 64 | *supportNativeZoomRatio = true; | 
|  | 65 | return OK; | 
|  | 66 | } | 
|  | 67 |  | 
|  | 68 | // Hal has no zoom ratio support | 
|  | 69 | *supportNativeZoomRatio = false; | 
|  | 70 |  | 
|  | 71 | entry = deviceInfo->find(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM); | 
|  | 72 | if (entry.count != 1) { | 
|  | 73 | ALOGI("%s: Camera device doesn't support SCALER_AVAILABLE_MAX_DIGITAL_ZOOM key!", | 
|  | 74 | __FUNCTION__); | 
|  | 75 | return OK; | 
|  | 76 | } | 
|  | 77 |  | 
|  | 78 | float zoomRange[] = {1.0f, entry.data.f[0]}; | 
|  | 79 | status_t res = deviceInfo->update(ANDROID_CONTROL_ZOOM_RATIO_RANGE, zoomRange, 2); | 
|  | 80 | if (res != OK) { | 
|  | 81 | ALOGE("%s: Failed to update CONTROL_ZOOM_RATIO_RANGE key: %s (%d)", | 
|  | 82 | __FUNCTION__, strerror(-res), res); | 
|  | 83 | return res; | 
|  | 84 | } | 
|  | 85 |  | 
|  | 86 | std::vector<int32_t> requestKeys; | 
|  | 87 | entry = deviceInfo->find(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS); | 
|  | 88 | if (entry.count > 0) { | 
|  | 89 | requestKeys.insert(requestKeys.end(), entry.data.i32, entry.data.i32 + entry.count); | 
|  | 90 | } | 
|  | 91 | requestKeys.push_back(ANDROID_CONTROL_ZOOM_RATIO); | 
|  | 92 | res = deviceInfo->update(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS, | 
|  | 93 | requestKeys.data(), requestKeys.size()); | 
|  | 94 | if (res != OK) { | 
|  | 95 | ALOGE("%s: Failed to update REQUEST_AVAILABLE_REQUEST_KEYS: %s (%d)", | 
|  | 96 | __FUNCTION__, strerror(-res), res); | 
|  | 97 | return res; | 
|  | 98 | } | 
|  | 99 |  | 
|  | 100 | std::vector<int32_t> resultKeys; | 
|  | 101 | entry = deviceInfo->find(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS); | 
|  | 102 | if (entry.count > 0) { | 
|  | 103 | resultKeys.insert(resultKeys.end(), entry.data.i32, entry.data.i32 + entry.count); | 
|  | 104 | } | 
|  | 105 | resultKeys.push_back(ANDROID_CONTROL_ZOOM_RATIO); | 
|  | 106 | res = deviceInfo->update(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS, | 
|  | 107 | resultKeys.data(), resultKeys.size()); | 
|  | 108 | if (res != OK) { | 
|  | 109 | ALOGE("%s: Failed to update REQUEST_AVAILABLE_RESULT_KEYS: %s (%d)", | 
|  | 110 | __FUNCTION__, strerror(-res), res); | 
|  | 111 | return res; | 
|  | 112 | } | 
|  | 113 |  | 
|  | 114 | std::vector<int32_t> charKeys; | 
|  | 115 | entry = deviceInfo->find(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS); | 
|  | 116 | if (entry.count > 0) { | 
|  | 117 | charKeys.insert(charKeys.end(), entry.data.i32, entry.data.i32 + entry.count); | 
|  | 118 | } | 
|  | 119 | charKeys.push_back(ANDROID_CONTROL_ZOOM_RATIO_RANGE); | 
|  | 120 | res = deviceInfo->update(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS, | 
|  | 121 | charKeys.data(), charKeys.size()); | 
|  | 122 | if (res != OK) { | 
|  | 123 | ALOGE("%s: Failed to update REQUEST_AVAILABLE_CHARACTERISTICS_KEYS: %s (%d)", | 
|  | 124 | __FUNCTION__, strerror(-res), res); | 
|  | 125 | return res; | 
|  | 126 | } | 
|  | 127 |  | 
|  | 128 | return OK; | 
|  | 129 | } | 
|  | 130 |  | 
| Shuzhen Wang | 1c834da | 2019-12-18 11:17:03 -0800 | [diff] [blame] | 131 | ZoomRatioMapper::ZoomRatioMapper(const CameraMetadata* deviceInfo, | 
| Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 132 | bool supportNativeZoomRatio, bool usePrecorrectArray) { | 
| Shuzhen Wang | 8c75a64 | 2020-10-29 21:58:53 -0700 | [diff] [blame] | 133 | initRemappedKeys(); | 
|  | 134 |  | 
| Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 135 | camera_metadata_ro_entry_t entry; | 
|  | 136 |  | 
|  | 137 | entry = deviceInfo->find(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE); | 
| Shuzhen Wang | 1c834da | 2019-12-18 11:17:03 -0800 | [diff] [blame] | 138 | if (entry.count != 4) return; | 
| Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 139 | int32_t arrayW = entry.data.i32[2]; | 
|  | 140 | int32_t arrayH = entry.data.i32[3]; | 
|  | 141 |  | 
|  | 142 | entry = deviceInfo->find(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE); | 
| Shuzhen Wang | 1c834da | 2019-12-18 11:17:03 -0800 | [diff] [blame] | 143 | if (entry.count != 4) return; | 
| Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 144 | int32_t activeW = entry.data.i32[2]; | 
|  | 145 | int32_t activeH = entry.data.i32[3]; | 
|  | 146 |  | 
|  | 147 | if (usePrecorrectArray) { | 
|  | 148 | mArrayWidth = arrayW; | 
|  | 149 | mArrayHeight = arrayH; | 
|  | 150 | } else { | 
|  | 151 | mArrayWidth = activeW; | 
|  | 152 | mArrayHeight = activeH; | 
|  | 153 | } | 
|  | 154 | mHalSupportsZoomRatio = supportNativeZoomRatio; | 
|  | 155 |  | 
|  | 156 | ALOGV("%s: array size: %d x %d, mHalSupportsZoomRatio %d", | 
|  | 157 | __FUNCTION__, mArrayWidth, mArrayHeight, mHalSupportsZoomRatio); | 
| Shuzhen Wang | 1c834da | 2019-12-18 11:17:03 -0800 | [diff] [blame] | 158 | mIsValid = true; | 
| Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 159 | } | 
|  | 160 |  | 
|  | 161 | status_t ZoomRatioMapper::updateCaptureRequest(CameraMetadata* request) { | 
| Shuzhen Wang | 1c834da | 2019-12-18 11:17:03 -0800 | [diff] [blame] | 162 | if (!mIsValid) return INVALID_OPERATION; | 
|  | 163 |  | 
| Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 164 | status_t res = OK; | 
|  | 165 | bool zoomRatioIs1 = true; | 
|  | 166 | camera_metadata_entry_t entry; | 
|  | 167 |  | 
|  | 168 | entry = request->find(ANDROID_CONTROL_ZOOM_RATIO); | 
|  | 169 | if (entry.count == 1 && entry.data.f[0] != 1.0f) { | 
|  | 170 | zoomRatioIs1 = false; | 
|  | 171 | } | 
|  | 172 |  | 
|  | 173 | if (mHalSupportsZoomRatio && zoomRatioIs1) { | 
|  | 174 | res = separateZoomFromCropLocked(request, false/*isResult*/); | 
|  | 175 | } else if (!mHalSupportsZoomRatio && !zoomRatioIs1) { | 
|  | 176 | res = combineZoomAndCropLocked(request, false/*isResult*/); | 
|  | 177 | } | 
|  | 178 |  | 
|  | 179 | // If CONTROL_ZOOM_RATIO is in request, but HAL doesn't support | 
|  | 180 | // CONTROL_ZOOM_RATIO, remove it from the request. | 
|  | 181 | if (!mHalSupportsZoomRatio && entry.count == 1) { | 
|  | 182 | request->erase(ANDROID_CONTROL_ZOOM_RATIO); | 
|  | 183 | } | 
|  | 184 |  | 
|  | 185 | return res; | 
|  | 186 | } | 
|  | 187 |  | 
|  | 188 | status_t ZoomRatioMapper::updateCaptureResult(CameraMetadata* result, bool requestedZoomRatioIs1) { | 
| Shuzhen Wang | 1c834da | 2019-12-18 11:17:03 -0800 | [diff] [blame] | 189 | if (!mIsValid) return INVALID_OPERATION; | 
|  | 190 |  | 
| Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 191 | status_t res = OK; | 
|  | 192 |  | 
|  | 193 | if (mHalSupportsZoomRatio && requestedZoomRatioIs1) { | 
|  | 194 | res = combineZoomAndCropLocked(result, true/*isResult*/); | 
|  | 195 | } else if (!mHalSupportsZoomRatio && !requestedZoomRatioIs1) { | 
|  | 196 | res = separateZoomFromCropLocked(result, true/*isResult*/); | 
|  | 197 | } else { | 
|  | 198 | camera_metadata_entry_t entry = result->find(ANDROID_CONTROL_ZOOM_RATIO); | 
|  | 199 | if (entry.count == 0) { | 
|  | 200 | float zoomRatio1x = 1.0f; | 
|  | 201 | result->update(ANDROID_CONTROL_ZOOM_RATIO, &zoomRatio1x, 1); | 
|  | 202 | } | 
|  | 203 | } | 
|  | 204 |  | 
|  | 205 | return res; | 
|  | 206 | } | 
|  | 207 |  | 
|  | 208 | float ZoomRatioMapper::deriveZoomRatio(const CameraMetadata* metadata) { | 
|  | 209 | float zoomRatio = 1.0; | 
|  | 210 |  | 
|  | 211 | camera_metadata_ro_entry_t entry; | 
|  | 212 | entry = metadata->find(ANDROID_SCALER_CROP_REGION); | 
|  | 213 | if (entry.count != 4) return zoomRatio; | 
|  | 214 |  | 
|  | 215 | // Center of the preCorrection/active size | 
|  | 216 | float arrayCenterX = mArrayWidth / 2.0; | 
|  | 217 | float arrayCenterY = mArrayHeight / 2.0; | 
|  | 218 |  | 
|  | 219 | // Re-map crop region to coordinate system centered to (arrayCenterX, | 
|  | 220 | // arrayCenterY). | 
|  | 221 | float cropRegionLeft = arrayCenterX - entry.data.i32[0] ; | 
|  | 222 | float cropRegionTop = arrayCenterY - entry.data.i32[1]; | 
|  | 223 | float cropRegionRight = entry.data.i32[0] + entry.data.i32[2] - arrayCenterX; | 
|  | 224 | float cropRegionBottom = entry.data.i32[1] + entry.data.i32[3] - arrayCenterY; | 
|  | 225 |  | 
|  | 226 | // Calculate the scaling factor for left, top, bottom, right | 
|  | 227 | float zoomRatioLeft = std::max(mArrayWidth / (2 * cropRegionLeft), 1.0f); | 
|  | 228 | float zoomRatioTop = std::max(mArrayHeight / (2 * cropRegionTop), 1.0f); | 
|  | 229 | float zoomRatioRight = std::max(mArrayWidth / (2 * cropRegionRight), 1.0f); | 
|  | 230 | float zoomRatioBottom = std::max(mArrayHeight / (2 * cropRegionBottom), 1.0f); | 
|  | 231 |  | 
|  | 232 | // Use minimum scaling factor to handle letterboxing or pillarboxing | 
|  | 233 | zoomRatio = std::min(std::min(zoomRatioLeft, zoomRatioRight), | 
|  | 234 | std::min(zoomRatioTop, zoomRatioBottom)); | 
|  | 235 |  | 
|  | 236 | ALOGV("%s: derived zoomRatio is %f", __FUNCTION__, zoomRatio); | 
|  | 237 | return zoomRatio; | 
|  | 238 | } | 
|  | 239 |  | 
|  | 240 | status_t ZoomRatioMapper::separateZoomFromCropLocked(CameraMetadata* metadata, bool isResult) { | 
|  | 241 | status_t res; | 
|  | 242 | float zoomRatio = deriveZoomRatio(metadata); | 
|  | 243 |  | 
|  | 244 | // Update zoomRatio metadata tag | 
|  | 245 | res = metadata->update(ANDROID_CONTROL_ZOOM_RATIO, &zoomRatio, 1); | 
|  | 246 | if (res != OK) { | 
|  | 247 | ALOGE("%s: Failed to update ANDROID_CONTROL_ZOOM_RATIO: %s(%d)", | 
|  | 248 | __FUNCTION__, strerror(-res), res); | 
|  | 249 | return res; | 
|  | 250 | } | 
|  | 251 |  | 
|  | 252 | // Scale regions using zoomRatio | 
|  | 253 | camera_metadata_entry_t entry; | 
|  | 254 | for (auto region : kMeteringRegionsToCorrect) { | 
|  | 255 | entry = metadata->find(region); | 
|  | 256 | for (size_t j = 0; j < entry.count; j += 5) { | 
|  | 257 | int32_t weight = entry.data.i32[j + 4]; | 
|  | 258 | if (weight == 0) { | 
|  | 259 | continue; | 
|  | 260 | } | 
| Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 261 | // Top left (inclusive) | 
|  | 262 | scaleCoordinates(entry.data.i32 + j, 1, zoomRatio, true /*clamp*/); | 
|  | 263 | // Bottom right (exclusive): Use adjacent inclusive pixel to | 
|  | 264 | // calculate. | 
|  | 265 | entry.data.i32[j+2] -= 1; | 
|  | 266 | entry.data.i32[j+3] -= 1; | 
|  | 267 | scaleCoordinates(entry.data.i32 + j + 2, 1, zoomRatio, true /*clamp*/); | 
|  | 268 | entry.data.i32[j+2] += 1; | 
|  | 269 | entry.data.i32[j+3] += 1; | 
| Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 270 | } | 
|  | 271 | } | 
|  | 272 |  | 
|  | 273 | for (auto rect : kRectsToCorrect) { | 
|  | 274 | entry = metadata->find(rect); | 
|  | 275 | scaleRects(entry.data.i32, entry.count / 4, zoomRatio); | 
|  | 276 | } | 
|  | 277 |  | 
|  | 278 | if (isResult) { | 
|  | 279 | for (auto pts : kResultPointsToCorrectNoClamp) { | 
|  | 280 | entry = metadata->find(pts); | 
| Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 281 | scaleCoordinates(entry.data.i32, entry.count / 2, zoomRatio, false /*clamp*/); | 
| Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 282 | } | 
|  | 283 | } | 
|  | 284 |  | 
|  | 285 | return OK; | 
|  | 286 | } | 
|  | 287 |  | 
|  | 288 | status_t ZoomRatioMapper::combineZoomAndCropLocked(CameraMetadata* metadata, bool isResult) { | 
|  | 289 | float zoomRatio = 1.0f; | 
|  | 290 | camera_metadata_entry_t entry; | 
|  | 291 | entry = metadata->find(ANDROID_CONTROL_ZOOM_RATIO); | 
|  | 292 | if (entry.count == 1) { | 
|  | 293 | zoomRatio = entry.data.f[0]; | 
|  | 294 | } | 
|  | 295 |  | 
|  | 296 | // Unscale regions with zoomRatio | 
|  | 297 | status_t res; | 
|  | 298 | for (auto region : kMeteringRegionsToCorrect) { | 
|  | 299 | entry = metadata->find(region); | 
|  | 300 | for (size_t j = 0; j < entry.count; j += 5) { | 
|  | 301 | int32_t weight = entry.data.i32[j + 4]; | 
|  | 302 | if (weight == 0) { | 
|  | 303 | continue; | 
|  | 304 | } | 
| Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 305 | // Top-left (inclusive) | 
|  | 306 | scaleCoordinates(entry.data.i32 + j, 1, 1.0 / zoomRatio, true /*clamp*/); | 
|  | 307 | // Bottom-right (exclusive): Use adjacent inclusive pixel to | 
|  | 308 | // calculate. | 
|  | 309 | entry.data.i32[j+2] -= 1; | 
|  | 310 | entry.data.i32[j+3] -= 1; | 
|  | 311 | scaleCoordinates(entry.data.i32 + j + 2, 1, 1.0 / zoomRatio, true /*clamp*/); | 
|  | 312 | entry.data.i32[j+2] += 1; | 
|  | 313 | entry.data.i32[j+3] += 1; | 
| Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 314 | } | 
|  | 315 | } | 
|  | 316 | for (auto rect : kRectsToCorrect) { | 
|  | 317 | entry = metadata->find(rect); | 
|  | 318 | scaleRects(entry.data.i32, entry.count / 4, 1.0 / zoomRatio); | 
|  | 319 | } | 
|  | 320 | if (isResult) { | 
|  | 321 | for (auto pts : kResultPointsToCorrectNoClamp) { | 
|  | 322 | entry = metadata->find(pts); | 
| Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 323 | scaleCoordinates(entry.data.i32, entry.count / 2, 1.0 / zoomRatio, false /*clamp*/); | 
| Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 324 | } | 
|  | 325 | } | 
|  | 326 |  | 
|  | 327 | zoomRatio = 1.0; | 
|  | 328 | res = metadata->update(ANDROID_CONTROL_ZOOM_RATIO, &zoomRatio, 1); | 
|  | 329 | if (res != OK) { | 
|  | 330 | return res; | 
|  | 331 | } | 
|  | 332 |  | 
|  | 333 | return OK; | 
|  | 334 | } | 
|  | 335 |  | 
|  | 336 | void ZoomRatioMapper::scaleCoordinates(int32_t* coordPairs, int coordCount, | 
| Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 337 | float scaleRatio, bool clamp) { | 
|  | 338 | // A pixel's coordinate is represented by the position of its top-left corner. | 
|  | 339 | // To avoid the rounding error, we use the coordinate for the center of the | 
|  | 340 | // pixel instead: | 
|  | 341 | // 1. First shift the coordinate system half pixel both horizontally and | 
|  | 342 | // vertically, so that [x, y] is the center of the pixel, not the top-left corner. | 
|  | 343 | // 2. Do zoom operation to scale the coordinate relative to the center of | 
|  | 344 | // the active array (shifted by 0.5 pixel as well). | 
|  | 345 | // 3. Shift the coordinate system back by directly using the pixel center | 
|  | 346 | // coordinate. | 
| Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 347 | for (int i = 0; i < coordCount * 2; i += 2) { | 
|  | 348 | float x = coordPairs[i]; | 
|  | 349 | float y = coordPairs[i + 1]; | 
| Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 350 | float xCentered = x - (mArrayWidth - 2) / 2; | 
|  | 351 | float yCentered = y - (mArrayHeight - 2) / 2; | 
| Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 352 | float scaledX = xCentered * scaleRatio; | 
|  | 353 | float scaledY = yCentered * scaleRatio; | 
| Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 354 | scaledX += (mArrayWidth - 2) / 2; | 
|  | 355 | scaledY += (mArrayHeight - 2) / 2; | 
|  | 356 | coordPairs[i] = static_cast<int32_t>(std::round(scaledX)); | 
|  | 357 | coordPairs[i+1] = static_cast<int32_t>(std::round(scaledY)); | 
| Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 358 | // Clamp to within activeArray/preCorrectionActiveArray | 
| Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 359 | if (clamp) { | 
|  | 360 | int32_t right = mArrayWidth - 1; | 
|  | 361 | int32_t bottom = mArrayHeight - 1; | 
| Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 362 | coordPairs[i] = | 
|  | 363 | std::min(right, std::max(0, coordPairs[i])); | 
|  | 364 | coordPairs[i+1] = | 
|  | 365 | std::min(bottom, std::max(0, coordPairs[i+1])); | 
|  | 366 | } | 
|  | 367 | ALOGV("%s: coordinates: %d, %d", __FUNCTION__, coordPairs[i], coordPairs[i+1]); | 
|  | 368 | } | 
|  | 369 | } | 
|  | 370 |  | 
|  | 371 | void ZoomRatioMapper::scaleRects(int32_t* rects, int rectCount, | 
|  | 372 | float scaleRatio) { | 
|  | 373 | for (int i = 0; i < rectCount * 4; i += 4) { | 
| Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 374 | // Map from (l, t, width, height) to (l, t, l+width-1, t+height-1), | 
|  | 375 | // where both top-left and bottom-right are inclusive. | 
| Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 376 | int32_t coords[4] = { | 
|  | 377 | rects[i], | 
|  | 378 | rects[i + 1], | 
| Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 379 | rects[i] + rects[i + 2] - 1, | 
|  | 380 | rects[i + 1] + rects[i + 3] - 1 | 
| Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 381 | }; | 
|  | 382 |  | 
|  | 383 | // top-left | 
| Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 384 | scaleCoordinates(coords, 1, scaleRatio, true /*clamp*/); | 
| Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 385 | // bottom-right | 
| Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 386 | scaleCoordinates(coords+2, 1, scaleRatio, true /*clamp*/); | 
| Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 387 |  | 
|  | 388 | // Map back to (l, t, width, height) | 
|  | 389 | rects[i] = coords[0]; | 
|  | 390 | rects[i + 1] = coords[1]; | 
| Shuzhen Wang | 9b0d953 | 2020-04-14 17:14:20 -0700 | [diff] [blame] | 391 | rects[i + 2] = coords[2] - coords[0] + 1; | 
|  | 392 | rects[i + 3] = coords[3] - coords[1] + 1; | 
| Shuzhen Wang | dbdf72b | 2019-11-13 11:22:12 -0800 | [diff] [blame] | 393 | } | 
|  | 394 | } | 
|  | 395 |  | 
|  | 396 | } // namespace camera3 | 
|  | 397 |  | 
|  | 398 | } // namespace android |