blob: 1bc20810271b4e148751e321ade7d25290b38782 [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"
23
24namespace android {
25
26namespace camera3 {
27
Shuzhen Wang8c75a642020-10-29 21:58:53 -070028void 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 Wangdbdf72b2019-11-13 11:22:12 -080041
42status_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
52status_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 Wang1c834da2019-12-18 11:17:03 -0800131ZoomRatioMapper::ZoomRatioMapper(const CameraMetadata* deviceInfo,
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800132 bool supportNativeZoomRatio, bool usePrecorrectArray) {
Shuzhen Wang8c75a642020-10-29 21:58:53 -0700133 initRemappedKeys();
134
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800135 camera_metadata_ro_entry_t entry;
136
137 entry = deviceInfo->find(ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE);
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800138 if (entry.count != 4) return;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800139 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 Wang1c834da2019-12-18 11:17:03 -0800143 if (entry.count != 4) return;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800144 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 Wang1c834da2019-12-18 11:17:03 -0800158 mIsValid = true;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800159}
160
161status_t ZoomRatioMapper::updateCaptureRequest(CameraMetadata* request) {
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800162 if (!mIsValid) return INVALID_OPERATION;
163
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800164 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;
Shuzhen Wangc2dae592020-11-30 20:16:06 -0800171
172 // If cropRegion is windowboxing, override it with activeArray
173 camera_metadata_entry_t cropRegionEntry = request->find(ANDROID_SCALER_CROP_REGION);
174 if (cropRegionEntry.count == 4) {
175 int cropWidth = cropRegionEntry.data.i32[2];
176 int cropHeight = cropRegionEntry.data.i32[3];
177 if (cropWidth < mArrayWidth && cropHeight < mArrayHeight) {
178 cropRegionEntry.data.i32[0] = 0;
179 cropRegionEntry.data.i32[1] = 0;
180 cropRegionEntry.data.i32[2] = mArrayWidth;
181 cropRegionEntry.data.i32[3] = mArrayHeight;
182 }
183 }
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800184 }
185
186 if (mHalSupportsZoomRatio && zoomRatioIs1) {
187 res = separateZoomFromCropLocked(request, false/*isResult*/);
188 } else if (!mHalSupportsZoomRatio && !zoomRatioIs1) {
189 res = combineZoomAndCropLocked(request, false/*isResult*/);
190 }
191
192 // If CONTROL_ZOOM_RATIO is in request, but HAL doesn't support
193 // CONTROL_ZOOM_RATIO, remove it from the request.
194 if (!mHalSupportsZoomRatio && entry.count == 1) {
195 request->erase(ANDROID_CONTROL_ZOOM_RATIO);
196 }
197
198 return res;
199}
200
201status_t ZoomRatioMapper::updateCaptureResult(CameraMetadata* result, bool requestedZoomRatioIs1) {
Shuzhen Wang1c834da2019-12-18 11:17:03 -0800202 if (!mIsValid) return INVALID_OPERATION;
203
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800204 status_t res = OK;
205
206 if (mHalSupportsZoomRatio && requestedZoomRatioIs1) {
207 res = combineZoomAndCropLocked(result, true/*isResult*/);
208 } else if (!mHalSupportsZoomRatio && !requestedZoomRatioIs1) {
209 res = separateZoomFromCropLocked(result, true/*isResult*/);
210 } else {
211 camera_metadata_entry_t entry = result->find(ANDROID_CONTROL_ZOOM_RATIO);
212 if (entry.count == 0) {
213 float zoomRatio1x = 1.0f;
214 result->update(ANDROID_CONTROL_ZOOM_RATIO, &zoomRatio1x, 1);
215 }
216 }
217
218 return res;
219}
220
221float ZoomRatioMapper::deriveZoomRatio(const CameraMetadata* metadata) {
222 float zoomRatio = 1.0;
223
224 camera_metadata_ro_entry_t entry;
225 entry = metadata->find(ANDROID_SCALER_CROP_REGION);
226 if (entry.count != 4) return zoomRatio;
227
228 // Center of the preCorrection/active size
229 float arrayCenterX = mArrayWidth / 2.0;
230 float arrayCenterY = mArrayHeight / 2.0;
231
232 // Re-map crop region to coordinate system centered to (arrayCenterX,
233 // arrayCenterY).
234 float cropRegionLeft = arrayCenterX - entry.data.i32[0] ;
235 float cropRegionTop = arrayCenterY - entry.data.i32[1];
236 float cropRegionRight = entry.data.i32[0] + entry.data.i32[2] - arrayCenterX;
237 float cropRegionBottom = entry.data.i32[1] + entry.data.i32[3] - arrayCenterY;
238
239 // Calculate the scaling factor for left, top, bottom, right
240 float zoomRatioLeft = std::max(mArrayWidth / (2 * cropRegionLeft), 1.0f);
241 float zoomRatioTop = std::max(mArrayHeight / (2 * cropRegionTop), 1.0f);
242 float zoomRatioRight = std::max(mArrayWidth / (2 * cropRegionRight), 1.0f);
243 float zoomRatioBottom = std::max(mArrayHeight / (2 * cropRegionBottom), 1.0f);
244
245 // Use minimum scaling factor to handle letterboxing or pillarboxing
246 zoomRatio = std::min(std::min(zoomRatioLeft, zoomRatioRight),
247 std::min(zoomRatioTop, zoomRatioBottom));
248
249 ALOGV("%s: derived zoomRatio is %f", __FUNCTION__, zoomRatio);
250 return zoomRatio;
251}
252
253status_t ZoomRatioMapper::separateZoomFromCropLocked(CameraMetadata* metadata, bool isResult) {
254 status_t res;
255 float zoomRatio = deriveZoomRatio(metadata);
256
257 // Update zoomRatio metadata tag
258 res = metadata->update(ANDROID_CONTROL_ZOOM_RATIO, &zoomRatio, 1);
259 if (res != OK) {
260 ALOGE("%s: Failed to update ANDROID_CONTROL_ZOOM_RATIO: %s(%d)",
261 __FUNCTION__, strerror(-res), res);
262 return res;
263 }
264
265 // Scale regions using zoomRatio
266 camera_metadata_entry_t entry;
267 for (auto region : kMeteringRegionsToCorrect) {
268 entry = metadata->find(region);
269 for (size_t j = 0; j < entry.count; j += 5) {
270 int32_t weight = entry.data.i32[j + 4];
271 if (weight == 0) {
272 continue;
273 }
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700274 // Top left (inclusive)
275 scaleCoordinates(entry.data.i32 + j, 1, zoomRatio, true /*clamp*/);
276 // Bottom right (exclusive): Use adjacent inclusive pixel to
277 // calculate.
278 entry.data.i32[j+2] -= 1;
279 entry.data.i32[j+3] -= 1;
280 scaleCoordinates(entry.data.i32 + j + 2, 1, zoomRatio, true /*clamp*/);
281 entry.data.i32[j+2] += 1;
282 entry.data.i32[j+3] += 1;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800283 }
284 }
285
286 for (auto rect : kRectsToCorrect) {
287 entry = metadata->find(rect);
288 scaleRects(entry.data.i32, entry.count / 4, zoomRatio);
289 }
290
291 if (isResult) {
292 for (auto pts : kResultPointsToCorrectNoClamp) {
293 entry = metadata->find(pts);
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700294 scaleCoordinates(entry.data.i32, entry.count / 2, zoomRatio, false /*clamp*/);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800295 }
296 }
297
298 return OK;
299}
300
301status_t ZoomRatioMapper::combineZoomAndCropLocked(CameraMetadata* metadata, bool isResult) {
302 float zoomRatio = 1.0f;
303 camera_metadata_entry_t entry;
304 entry = metadata->find(ANDROID_CONTROL_ZOOM_RATIO);
305 if (entry.count == 1) {
306 zoomRatio = entry.data.f[0];
307 }
308
309 // Unscale regions with zoomRatio
310 status_t res;
311 for (auto region : kMeteringRegionsToCorrect) {
312 entry = metadata->find(region);
313 for (size_t j = 0; j < entry.count; j += 5) {
314 int32_t weight = entry.data.i32[j + 4];
315 if (weight == 0) {
316 continue;
317 }
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700318 // Top-left (inclusive)
319 scaleCoordinates(entry.data.i32 + j, 1, 1.0 / zoomRatio, true /*clamp*/);
320 // Bottom-right (exclusive): Use adjacent inclusive pixel to
321 // calculate.
322 entry.data.i32[j+2] -= 1;
323 entry.data.i32[j+3] -= 1;
324 scaleCoordinates(entry.data.i32 + j + 2, 1, 1.0 / zoomRatio, true /*clamp*/);
325 entry.data.i32[j+2] += 1;
326 entry.data.i32[j+3] += 1;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800327 }
328 }
329 for (auto rect : kRectsToCorrect) {
330 entry = metadata->find(rect);
331 scaleRects(entry.data.i32, entry.count / 4, 1.0 / zoomRatio);
332 }
333 if (isResult) {
334 for (auto pts : kResultPointsToCorrectNoClamp) {
335 entry = metadata->find(pts);
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700336 scaleCoordinates(entry.data.i32, entry.count / 2, 1.0 / zoomRatio, false /*clamp*/);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800337 }
338 }
339
340 zoomRatio = 1.0;
341 res = metadata->update(ANDROID_CONTROL_ZOOM_RATIO, &zoomRatio, 1);
342 if (res != OK) {
343 return res;
344 }
345
346 return OK;
347}
348
349void ZoomRatioMapper::scaleCoordinates(int32_t* coordPairs, int coordCount,
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700350 float scaleRatio, bool clamp) {
351 // A pixel's coordinate is represented by the position of its top-left corner.
352 // To avoid the rounding error, we use the coordinate for the center of the
353 // pixel instead:
354 // 1. First shift the coordinate system half pixel both horizontally and
355 // vertically, so that [x, y] is the center of the pixel, not the top-left corner.
356 // 2. Do zoom operation to scale the coordinate relative to the center of
357 // the active array (shifted by 0.5 pixel as well).
358 // 3. Shift the coordinate system back by directly using the pixel center
359 // coordinate.
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800360 for (int i = 0; i < coordCount * 2; i += 2) {
361 float x = coordPairs[i];
362 float y = coordPairs[i + 1];
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700363 float xCentered = x - (mArrayWidth - 2) / 2;
364 float yCentered = y - (mArrayHeight - 2) / 2;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800365 float scaledX = xCentered * scaleRatio;
366 float scaledY = yCentered * scaleRatio;
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700367 scaledX += (mArrayWidth - 2) / 2;
368 scaledY += (mArrayHeight - 2) / 2;
369 coordPairs[i] = static_cast<int32_t>(std::round(scaledX));
370 coordPairs[i+1] = static_cast<int32_t>(std::round(scaledY));
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800371 // Clamp to within activeArray/preCorrectionActiveArray
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700372 if (clamp) {
373 int32_t right = mArrayWidth - 1;
374 int32_t bottom = mArrayHeight - 1;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800375 coordPairs[i] =
376 std::min(right, std::max(0, coordPairs[i]));
377 coordPairs[i+1] =
378 std::min(bottom, std::max(0, coordPairs[i+1]));
379 }
380 ALOGV("%s: coordinates: %d, %d", __FUNCTION__, coordPairs[i], coordPairs[i+1]);
381 }
382}
383
384void ZoomRatioMapper::scaleRects(int32_t* rects, int rectCount,
385 float scaleRatio) {
386 for (int i = 0; i < rectCount * 4; i += 4) {
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700387 // Map from (l, t, width, height) to (l, t, l+width-1, t+height-1),
388 // where both top-left and bottom-right are inclusive.
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800389 int32_t coords[4] = {
390 rects[i],
391 rects[i + 1],
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700392 rects[i] + rects[i + 2] - 1,
393 rects[i + 1] + rects[i + 3] - 1
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800394 };
395
396 // top-left
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700397 scaleCoordinates(coords, 1, scaleRatio, true /*clamp*/);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800398 // bottom-right
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700399 scaleCoordinates(coords+2, 1, scaleRatio, true /*clamp*/);
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800400
401 // Map back to (l, t, width, height)
402 rects[i] = coords[0];
403 rects[i + 1] = coords[1];
Shuzhen Wang9b0d9532020-04-14 17:14:20 -0700404 rects[i + 2] = coords[2] - coords[0] + 1;
405 rects[i + 3] = coords[3] - coords[1] + 1;
Shuzhen Wangdbdf72b2019-11-13 11:22:12 -0800406 }
407}
408
409} // namespace camera3
410
411} // namespace android