• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             continue;
95         }
96         camera_metadata_entry meteringRegionEntry = request->find(entry.first);
97         if (meteringRegionEntry.count % 5 != 0) {
98             ALOGE("%s: Metering region entry for tag %d does not have a valid number of entries, "
99                     "skipping", __FUNCTION__, (int)entry.first);
100             continue;
101         }
102         for (size_t j = 0; j < meteringRegionEntry.count; j += 5) {
103             int32_t *meteringRegionStart = meteringRegionEntry.data.i32 + j;
104             meteringRegionStart[0] = 0;
105             meteringRegionStart[1] = 0;
106             meteringRegionStart[2] = mArrayWidthMaximumResolution;
107             meteringRegionStart[3] = mArrayHeightMaximumResolution;
108         }
109     }
110 }
111 
fixCropRegionsIfNeeded(CameraMetadata * request)112 void UHRCropAndMeteringRegionMapper::fixCropRegionsIfNeeded(CameraMetadata *request) {
113     if (request == nullptr) {
114       ALOGE("%s request is nullptr, can't fix crop region", __FUNCTION__);
115       return;
116     }
117     // Check if the scalerCropRegionSet key is set to TRUE, we don't
118     // need to correct the crop region.
119     camera_metadata_entry cropRegionSetEntry =
120             request->find(ANDROID_SCALER_CROP_REGION_SET);
121     if (cropRegionSetEntry.count == 1 &&
122         cropRegionSetEntry.data.u8[0] == ANDROID_SCALER_CROP_REGION_SET_TRUE) {
123         // crop regions set by client, doesn't need to be fixed.
124         return;
125     }
126     camera_metadata_entry_t cropRegionEntry = request->find(ANDROID_SCALER_CROP_REGION);
127     if (cropRegionEntry.count == 4) {
128         cropRegionEntry.data.i32[0] = 0;
129         cropRegionEntry.data.i32[1] = 0;
130         cropRegionEntry.data.i32[2] = mArrayWidthMaximumResolution;
131         cropRegionEntry.data.i32[3] = mArrayHeightMaximumResolution;
132     }
133 }
134 
updateCaptureRequest(CameraMetadata * request)135 status_t UHRCropAndMeteringRegionMapper::updateCaptureRequest(CameraMetadata* request) {
136     if (request == nullptr) {
137         ALOGE("%s Invalid request, request is nullptr", __FUNCTION__);
138         return BAD_VALUE;
139     }
140     if (!mIsValid) {
141         ALOGE("%s UHRCropAndMeteringRegionMapper didn't initialize correctly", __FUNCTION__);
142         return INVALID_OPERATION;
143     }
144 
145     camera_metadata_entry sensorPixelModeEntry = request->find(ANDROID_SENSOR_PIXEL_MODE);
146 
147     // Check if this is max resolution capture, if not, we don't need to do
148     // anything.
149     if (sensorPixelModeEntry.count != 0) {
150         int32_t sensorPixelMode = sensorPixelModeEntry.data.u8[0];
151         if (sensorPixelMode != ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) {
152             // Correction not needed for default mode requests.
153            return OK;
154         }
155     } else {
156         // sensor pixel mode not set -> default sensor pixel mode request, which
157         // doesn't need correction.
158         return OK;
159     }
160 
161     fixCropRegionsIfNeeded(request);
162     fixMeteringRegionsIfNeeded(request);
163     return OK;
164 }
165 
166 } // namespace camera3
167 
168 } // namespace android
169