blob: 1a39510157d4b55c5a0aa23035dee0f07a91018c [file] [log] [blame]
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -08001/*
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 Chowdhary13f9b2f2020-12-02 22:46:15 -080023#include "utils/SessionConfigurationUtils.h"
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -080024
25namespace android {
26
27namespace camera3 {
28
Shuzhen Wang8c75a642020-10-29 21:58:53 -070029void 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 Wangdbdf72b2019-11-13 11:22:12 -080042
43status_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
53status_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 Chowdhary13f9b2f2020-12-02 22:46:15 -0800132static 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 Wang1c834da2019-12-18 11:17:03 -0800146ZoomRatioMapper::ZoomRatioMapper(const CameraMetadata* deviceInfo,
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800147 bool supportNativeZoomRatio, bool usePrecorrectArray) {
Shuzhen Wang8c75a642020-10-29 21:58:53 -0700148 initRemappedKeys();
149
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800150 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 Wangdbdf72b2019-11-13 11:22:12 -0800158
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800159 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 Wangdbdf72b2019-11-13 11:22:12 -0800169
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800170 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 Wangdbdf72b2019-11-13 11:22:12 -0800188
189 if (usePrecorrectArray) {
190 mArrayWidth = arrayW;
191 mArrayHeight = arrayH;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800192 mArrayWidthMaximumResolution = arrayMaximumResolutionW;
193 mArrayHeightMaximumResolution = arrayMaximumResolutionH;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800194 } else {
195 mArrayWidth = activeW;
196 mArrayHeight = activeH;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800197 mArrayWidthMaximumResolution = activeMaximumResolutionW;
198 mArrayHeightMaximumResolution = activeMaximumResolutionH;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800199 }
200 mHalSupportsZoomRatio = supportNativeZoomRatio;
201
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800202 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 Wang1c834da2019-12-18 11:17:03 -0800205 mIsValid = true;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800206}
207
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800208status_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 Wangdbdf72b2019-11-13 11:22:12 -0800235status_t ZoomRatioMapper::updateCaptureRequest(CameraMetadata* request) {
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800236 if (!mIsValid) return INVALID_OPERATION;
237
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800238 status_t res = OK;
239 bool zoomRatioIs1 = true;
240 camera_metadata_entry_t entry;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800241 int arrayHeight, arrayWidth = 0;
242 res = getArrayDimensionsToBeUsed(request, &arrayWidth, &arrayHeight);
243 if (res != OK) {
244 return res;
245 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800246 entry = request->find(ANDROID_CONTROL_ZOOM_RATIO);
247 if (entry.count == 1 && entry.data.f[0] != 1.0f) {
248 zoomRatioIs1 = false;
Shuzhen Wangc2dae592020-11-30 20:16:06 -0800249
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 Chowdhary13f9b2f2020-12-02 22:46:15 -0800255 if (cropWidth < arrayWidth && cropHeight < arrayHeight) {
Shuzhen Wangc2dae592020-11-30 20:16:06 -0800256 cropRegionEntry.data.i32[0] = 0;
257 cropRegionEntry.data.i32[1] = 0;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800258 cropRegionEntry.data.i32[2] = arrayWidth;
259 cropRegionEntry.data.i32[3] = arrayHeight;
Shuzhen Wangc2dae592020-11-30 20:16:06 -0800260 }
261 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800262 }
263
264 if (mHalSupportsZoomRatio && zoomRatioIs1) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800265 res = separateZoomFromCropLocked(request, false/*isResult*/, arrayWidth, arrayHeight);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800266 } else if (!mHalSupportsZoomRatio && !zoomRatioIs1) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800267 res = combineZoomAndCropLocked(request, false/*isResult*/, arrayWidth, arrayHeight);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800268 }
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
279status_t ZoomRatioMapper::updateCaptureResult(CameraMetadata* result, bool requestedZoomRatioIs1) {
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800280 if (!mIsValid) return INVALID_OPERATION;
281
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800282 status_t res = OK;
283
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800284 int arrayHeight, arrayWidth = 0;
285 res = getArrayDimensionsToBeUsed(result, &arrayWidth, &arrayHeight);
286 if (res != OK) {
287 return res;
288 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800289 if (mHalSupportsZoomRatio && requestedZoomRatioIs1) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800290 res = combineZoomAndCropLocked(result, true/*isResult*/, arrayWidth, arrayHeight);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800291 } else if (!mHalSupportsZoomRatio && !requestedZoomRatioIs1) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800292 res = separateZoomFromCropLocked(result, true/*isResult*/, arrayWidth, arrayHeight);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800293 } 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 Chowdhary13f9b2f2020-12-02 22:46:15 -0800304status_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 Wangdbdf72b2019-11-13 11:22:12 -0800309 float zoomRatio = 1.0;
310
311 camera_metadata_ro_entry_t entry;
312 entry = metadata->find(ANDROID_SCALER_CROP_REGION);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800313 if (entry.count != 4) {
314 *zoomRatioRet = 1;
315 return OK;
316 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800317 // Center of the preCorrection/active size
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800318 float arrayCenterX = arrayWidth / 2.0;
319 float arrayCenterY = arrayHeight / 2.0;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800320
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 Chowdhary13f9b2f2020-12-02 22:46:15 -0800329 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 Wangdbdf72b2019-11-13 11:22:12 -0800333
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 Chowdhary13f9b2f2020-12-02 22:46:15 -0800339 *zoomRatioRet = zoomRatio;
340 return OK;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800341}
342
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800343status_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 Wangdbdf72b2019-11-13 11:22:12 -0800353
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 Wang9b0d9532020-04-14 17:14:20 -0700371 // Top left (inclusive)
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800372 scaleCoordinates(entry.data.i32 + j, 1, zoomRatio, true /*clamp*/, arrayWidth,
373 arrayHeight);
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700374 // 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 Chowdhary13f9b2f2020-12-02 22:46:15 -0800378 scaleCoordinates(entry.data.i32 + j + 2, 1, zoomRatio, true /*clamp*/, arrayWidth,
379 arrayHeight);
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700380 entry.data.i32[j+2] += 1;
381 entry.data.i32[j+3] += 1;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800382 }
383 }
384
385 for (auto rect : kRectsToCorrect) {
386 entry = metadata->find(rect);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800387 scaleRects(entry.data.i32, entry.count / 4, zoomRatio, arrayWidth, arrayHeight);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800388 }
389
390 if (isResult) {
391 for (auto pts : kResultPointsToCorrectNoClamp) {
392 entry = metadata->find(pts);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800393 scaleCoordinates(entry.data.i32, entry.count / 2, zoomRatio, false /*clamp*/,
394 arrayWidth, arrayHeight);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800395 }
396 }
397
398 return OK;
399}
400
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800401status_t ZoomRatioMapper::combineZoomAndCropLocked(CameraMetadata* metadata, bool isResult,
402 int arrayWidth, int arrayHeight) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800403 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 Wangdbdf72b2019-11-13 11:22:12 -0800411 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 Wang9b0d9532020-04-14 17:14:20 -0700418 // Top-left (inclusive)
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800419 scaleCoordinates(entry.data.i32 + j, 1, 1.0 / zoomRatio, true /*clamp*/, arrayWidth,
420 arrayHeight);
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700421 // 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 Chowdhary13f9b2f2020-12-02 22:46:15 -0800425 scaleCoordinates(entry.data.i32 + j + 2, 1, 1.0 / zoomRatio, true /*clamp*/, arrayWidth,
426 arrayHeight);
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700427 entry.data.i32[j+2] += 1;
428 entry.data.i32[j+3] += 1;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800429 }
430 }
431 for (auto rect : kRectsToCorrect) {
432 entry = metadata->find(rect);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800433 scaleRects(entry.data.i32, entry.count / 4, 1.0 / zoomRatio, arrayWidth, arrayHeight);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800434 }
435 if (isResult) {
436 for (auto pts : kResultPointsToCorrectNoClamp) {
437 entry = metadata->find(pts);
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800438 scaleCoordinates(entry.data.i32, entry.count / 2, 1.0 / zoomRatio, false /*clamp*/,
439 arrayWidth, arrayHeight);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800440 }
441 }
442
443 zoomRatio = 1.0;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800444 status_t res = metadata->update(ANDROID_CONTROL_ZOOM_RATIO, &zoomRatio, 1);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800445 if (res != OK) {
446 return res;
447 }
448
449 return OK;
450}
451
452void ZoomRatioMapper::scaleCoordinates(int32_t* coordPairs, int coordCount,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800453 float scaleRatio, bool clamp, int32_t arrayWidth, int32_t arrayHeight) {
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700454 // 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 Wangdbdf72b2019-11-13 11:22:12 -0800463 for (int i = 0; i < coordCount * 2; i += 2) {
464 float x = coordPairs[i];
465 float y = coordPairs[i + 1];
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800466 float xCentered = x - (arrayWidth - 2) / 2;
467 float yCentered = y - (arrayHeight - 2) / 2;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800468 float scaledX = xCentered * scaleRatio;
469 float scaledY = yCentered * scaleRatio;
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800470 scaledX += (arrayWidth - 2) / 2;
471 scaledY += (arrayHeight - 2) / 2;
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700472 coordPairs[i] = static_cast<int32_t>(std::round(scaledX));
473 coordPairs[i+1] = static_cast<int32_t>(std::round(scaledY));
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800474 // Clamp to within activeArray/preCorrectionActiveArray
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700475 if (clamp) {
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800476 int32_t right = arrayWidth - 1;
477 int32_t bottom = arrayHeight - 1;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800478 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
487void ZoomRatioMapper::scaleRects(int32_t* rects, int rectCount,
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800488 float scaleRatio, int32_t arrayWidth, int32_t arrayHeight) {
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800489 for (int i = 0; i < rectCount * 4; i += 4) {
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700490 // 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 Wangdbdf72b2019-11-13 11:22:12 -0800492 int32_t coords[4] = {
493 rects[i],
494 rects[i + 1],
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700495 rects[i] + rects[i + 2] - 1,
496 rects[i + 1] + rects[i + 3] - 1
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800497 };
498
499 // top-left
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800500 scaleCoordinates(coords, 1, scaleRatio, true /*clamp*/, arrayWidth, arrayHeight);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800501 // bottom-right
Jayant Chowdhary13f9b2f2020-12-02 22:46:15 -0800502 scaleCoordinates(coords+2, 1, scaleRatio, true /*clamp*/, arrayWidth, arrayHeight);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800503
504 // Map back to (l, t, width, height)
505 rects[i] = coords[0];
506 rects[i + 1] = coords[1];
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700507 rects[i + 2] = coords[2] - coords[0] + 1;
508 rects[i + 3] = coords[3] - coords[1] + 1;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800509 }
510}
511
512} // namespace camera3
513
514} // namespace android