1 /*
2 * Copyright (C) 2021 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-UHRCropAndMeteringRegionMapper"
18 #define ATRACE_TAG ATRACE_TAG_CAMERA
19 //#define LOG_NDEBUG 0
20
21 #include <algorithm>
22 #include <cmath>
23
24 #include "device3/UHRCropAndMeteringRegionMapper.h"
25 #include "utils/SessionConfigurationUtils.h"
26
27 namespace android {
28
29 namespace camera3 {
30 // For Capture request
31 // metering region -> {fwk private key for metering region set, true}
32 static std::unordered_map<uint32_t, std::pair<uint32_t, uint32_t>> kMeteringRegionsToCorrect = {
33 {ANDROID_CONTROL_AF_REGIONS,
34 {ANDROID_CONTROL_AF_REGIONS_SET, ANDROID_CONTROL_AF_REGIONS_SET_TRUE}},
35 {ANDROID_CONTROL_AE_REGIONS,
36 {ANDROID_CONTROL_AE_REGIONS_SET, ANDROID_CONTROL_AE_REGIONS_SET_TRUE}},
37 {ANDROID_CONTROL_AWB_REGIONS,
38 {ANDROID_CONTROL_AWB_REGIONS_SET, ANDROID_CONTROL_AWB_REGIONS_SET_TRUE}}
39 };
40
UHRCropAndMeteringRegionMapper(const CameraMetadata & deviceInfo,bool usePreCorrectedArray)41 UHRCropAndMeteringRegionMapper::UHRCropAndMeteringRegionMapper(const CameraMetadata &deviceInfo,
42 bool usePreCorrectedArray) {
43
44 if (usePreCorrectedArray) {
45 if (!SessionConfigurationUtils::getArrayWidthAndHeight(&deviceInfo,
46 ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE, &mArrayWidth,
47 &mArrayHeight)) {
48 ALOGE("%s: Couldn't get pre correction active array size", __FUNCTION__);
49 return;
50 }
51 if (!SessionConfigurationUtils::getArrayWidthAndHeight(&deviceInfo,
52 ANDROID_SENSOR_INFO_PRE_CORRECTION_ACTIVE_ARRAY_SIZE_MAXIMUM_RESOLUTION,
53 &mArrayWidthMaximumResolution, &mArrayHeightMaximumResolution)) {
54 ALOGE("%s: Couldn't get maximum resolution pre correction active array size",
55 __FUNCTION__);
56 return;
57 }
58 } else {
59 if (!SessionConfigurationUtils::getArrayWidthAndHeight(&deviceInfo,
60 ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE, &mArrayWidth,
61 &mArrayHeight)) {
62 ALOGE("%s: Couldn't get active array size", __FUNCTION__);
63 return;
64 }
65 if (!SessionConfigurationUtils::getArrayWidthAndHeight(&deviceInfo,
66 ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE_MAXIMUM_RESOLUTION,
67 &mArrayWidthMaximumResolution, &mArrayHeightMaximumResolution)) {
68 ALOGE("%s: Couldn't get maximum resolution active array size", __FUNCTION__);
69 return;
70 }
71
72 }
73
74 mIsValid = true;
75
76 ALOGV("%s: array size: %d x %d, full res array size: %d x %d,",
77 __FUNCTION__, mArrayWidth, mArrayHeight, mArrayWidthMaximumResolution,
78 mArrayHeightMaximumResolution);
79 }
80
fixMeteringRegionsIfNeeded(CameraMetadata * request)81 void UHRCropAndMeteringRegionMapper::fixMeteringRegionsIfNeeded(CameraMetadata *request) {
82 if (request == nullptr) {
83 ALOGE("%s request is nullptr, can't fix crop region", __FUNCTION__);
84 return;
85 }
86 for (const auto &entry : kMeteringRegionsToCorrect) {
87 // Check if the metering region Set key is set to TRUE, we don't
88 // need to correct the metering regions.
89 camera_metadata_entry meteringRegionsSetEntry =
90 request->find(entry.second.first);
91 if (meteringRegionsSetEntry.count == 1 &&
92 meteringRegionsSetEntry.data.u8[0] == entry.second.second) {
93 // metering region set by client, doesn't need to be fixed.
94 ALOGV("%s: Metering region %u set by client, they don't need to be fixed",
95 __FUNCTION__, entry.first);
96 continue;
97 }
98 camera_metadata_entry meteringRegionEntry = request->find(entry.first);
99 if (meteringRegionEntry.count % 5 != 0) {
100 ALOGE("%s: Metering region entry for tag %d does not have a valid number of entries, "
101 "skipping", __FUNCTION__, (int)entry.first);
102 continue;
103 }
104 for (size_t j = 0; j < meteringRegionEntry.count; j += 5) {
105 int32_t *meteringRegionStart = meteringRegionEntry.data.i32 + j;
106 meteringRegionStart[0] = 0;
107 meteringRegionStart[1] = 0;
108 meteringRegionStart[2] = mArrayWidthMaximumResolution;
109 meteringRegionStart[3] = mArrayHeightMaximumResolution;
110 }
111 }
112 }
113
fixCropRegionsIfNeeded(CameraMetadata * request)114 void UHRCropAndMeteringRegionMapper::fixCropRegionsIfNeeded(CameraMetadata *request) {
115 if (request == nullptr) {
116 ALOGE("%s request is nullptr, can't fix crop region", __FUNCTION__);
117 return;
118 }
119 // Check if the scalerCropRegionSet key is set to TRUE, we don't
120 // need to correct the crop region.
121 camera_metadata_entry cropRegionSetEntry =
122 request->find(ANDROID_SCALER_CROP_REGION_SET);
123 if (cropRegionSetEntry.count == 1 &&
124 cropRegionSetEntry.data.u8[0] == ANDROID_SCALER_CROP_REGION_SET_TRUE) {
125 // crop regions set by client, doesn't need to be fixed.
126 ALOGV("%s: crop region set by client, doesn't need to be fixed", __FUNCTION__);
127 return;
128 }
129 camera_metadata_entry_t cropRegionEntry = request->find(ANDROID_SCALER_CROP_REGION);
130 if (cropRegionEntry.count == 4) {
131 cropRegionEntry.data.i32[0] = 0;
132 cropRegionEntry.data.i32[1] = 0;
133 cropRegionEntry.data.i32[2] = mArrayWidthMaximumResolution;
134 cropRegionEntry.data.i32[3] = mArrayHeightMaximumResolution;
135 }
136 }
137
updateCaptureRequest(CameraMetadata * request)138 status_t UHRCropAndMeteringRegionMapper::updateCaptureRequest(CameraMetadata* request) {
139 if (request == nullptr) {
140 ALOGE("%s Invalid request, request is nullptr", __FUNCTION__);
141 return BAD_VALUE;
142 }
143 if (!mIsValid) {
144 ALOGE("%s UHRCropAndMeteringRegionMapper didn't initialize correctly", __FUNCTION__);
145 return INVALID_OPERATION;
146 }
147
148 camera_metadata_entry sensorPixelModeEntry = request->find(ANDROID_SENSOR_PIXEL_MODE);
149
150 // Check if this is max resolution capture, if not, we don't need to do
151 // anything.
152 if (sensorPixelModeEntry.count != 0) {
153 int32_t sensorPixelMode = sensorPixelModeEntry.data.u8[0];
154 if (sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) {
155 // Correction not needed for default mode requests.
156 return OK;
157 }
158 } else {
159 // sensor pixel mode not set -> default sensor pixel mode request, which
160 // doesn't need correction.
161 return OK;
162 }
163
164 fixCropRegionsIfNeeded(request);
165 fixMeteringRegionsIfNeeded(request);
166 return OK;
167 }
168
169 } // namespace camera3
170
171 } // namespace android
172