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