1 /*
2  _* Copyright (C) 2013-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 "HWLUtils"
18 #include "HWLUtils.h"
19 #include <log/log.h>
20 #include "utils.h"
21 
22 #include <map>
23 
24 namespace android {
25 
26 using google_camera_hal::utils::HasCapability;
27 
GetSensorCharacteristics(const HalCameraMetadata * metadata,SensorCharacteristics * sensor_chars)28 status_t GetSensorCharacteristics(const HalCameraMetadata* metadata,
29                                   SensorCharacteristics* sensor_chars /*out*/) {
30   if ((metadata == nullptr) || (sensor_chars == nullptr)) {
31     return BAD_VALUE;
32   }
33 
34   status_t ret = OK;
35   camera_metadata_ro_entry_t entry;
36   ret = metadata->Get(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE, &entry);
37   if ((ret != OK) || (entry.count != 2)) {
38     ALOGE("%s: Invalid ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE!", __FUNCTION__);
39     return BAD_VALUE;
40   }
41   sensor_chars->width = entry.data.i32[0];
42   sensor_chars->height = entry.data.i32[1];
43   sensor_chars->full_res_width = sensor_chars->width;
44   sensor_chars->full_res_height = sensor_chars->height;
45 
46   ret = metadata->Get(ANDROID_SENSOR_INFO_PIXEL_ARRAY_SIZE_MAXIMUM_RESOLUTION,
47                       &entry);
48   if ((ret == OK) && (entry.count == 2)) {
49     sensor_chars->full_res_width = entry.data.i32[0];
50     sensor_chars->full_res_height = entry.data.i32[1];
51     sensor_chars->quad_bayer_sensor = true;
52   }
53 
54   ret = metadata->Get(ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS, &entry);
55   if ((ret != OK) || (entry.count != 3)) {
56     ALOGE("%s: Invalid ANDROID_REQUEST_MAX_NUM_OUTPUT_STREAMS!", __FUNCTION__);
57     return BAD_VALUE;
58   }
59 
60   sensor_chars->max_raw_streams = entry.data.i32[0];
61   sensor_chars->max_processed_streams = entry.data.i32[1];
62   sensor_chars->max_stalling_streams = entry.data.i32[2];
63 
64   if (HasCapability(metadata,
65                     ANDROID_REQUEST_AVAILABLE_CAPABILITIES_MANUAL_SENSOR)) {
66     ret = metadata->Get(ANDROID_SENSOR_INFO_EXPOSURE_TIME_RANGE, &entry);
67     if ((ret != OK) ||
68         (entry.count != ARRAY_SIZE(sensor_chars->exposure_time_range))) {
69       ALOGE("%s: Invalid ANDROID_SENSOR_INFO_EXPOSURE_TIME_RANGE!",
70             __FUNCTION__);
71       return BAD_VALUE;
72     }
73     memcpy(sensor_chars->exposure_time_range, entry.data.i64,
74            sizeof(sensor_chars->exposure_time_range));
75 
76     ret = metadata->Get(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION, &entry);
77     if ((ret != OK) || (entry.count != 1)) {
78       ALOGE("%s: Invalid ANDROID_SENSOR_INFO_MAX_FRAME_DURATION!", __FUNCTION__);
79       return BAD_VALUE;
80     }
81     sensor_chars->frame_duration_range[1] = entry.data.i64[0];
82     sensor_chars->frame_duration_range[0] =
83         EmulatedSensor::kSupportedFrameDurationRange[0];
84 
85     ret = metadata->Get(ANDROID_SENSOR_INFO_SENSITIVITY_RANGE, &entry);
86     if ((ret != OK) ||
87         (entry.count != ARRAY_SIZE(sensor_chars->sensitivity_range))) {
88       ALOGE("%s: Invalid ANDROID_SENSOR_INFO_SENSITIVITY_RANGE!", __FUNCTION__);
89       return BAD_VALUE;
90     }
91     memcpy(sensor_chars->sensitivity_range, entry.data.i64,
92            sizeof(sensor_chars->sensitivity_range));
93   } else {
94     memcpy(sensor_chars->exposure_time_range,
95            EmulatedSensor::kSupportedExposureTimeRange,
96            sizeof(sensor_chars->exposure_time_range));
97     memcpy(sensor_chars->frame_duration_range,
98            EmulatedSensor::kSupportedFrameDurationRange,
99            sizeof(sensor_chars->frame_duration_range));
100     memcpy(sensor_chars->sensitivity_range,
101            EmulatedSensor::kSupportedSensitivityRange,
102            sizeof(sensor_chars->sensitivity_range));
103   }
104 
105   if (HasCapability(metadata, ANDROID_REQUEST_AVAILABLE_CAPABILITIES_RAW)) {
106     ret = metadata->Get(ANDROID_SENSOR_INFO_COLOR_FILTER_ARRANGEMENT, &entry);
107     if ((ret != OK) || (entry.count != 1)) {
108       ALOGE("%s: Invalid ANDROID_SENSOR_INFO_COLOR_FILTER_ARRANGEMENT!",
109             __FUNCTION__);
110       return BAD_VALUE;
111     }
112 
113     sensor_chars->color_arangement = static_cast<
114         camera_metadata_enum_android_sensor_info_color_filter_arrangement>(
115         entry.data.u8[0]);
116 
117     ret = metadata->Get(ANDROID_SENSOR_INFO_WHITE_LEVEL, &entry);
118     if ((ret != OK) || (entry.count != 1)) {
119       ALOGE("%s: Invalid ANDROID_SENSOR_INFO_WHITE_LEVEL!", __FUNCTION__);
120       return BAD_VALUE;
121     }
122     sensor_chars->max_raw_value = entry.data.i32[0];
123 
124     ret = metadata->Get(ANDROID_SENSOR_BLACK_LEVEL_PATTERN, &entry);
125     if ((ret != OK) ||
126         (entry.count != ARRAY_SIZE(sensor_chars->black_level_pattern))) {
127       ALOGE("%s: Invalid ANDROID_SENSOR_BLACK_LEVEL_PATTERN!", __FUNCTION__);
128       return BAD_VALUE;
129     }
130 
131     memcpy(sensor_chars->black_level_pattern, entry.data.i32,
132            sizeof(sensor_chars->black_level_pattern));
133 
134     ret = metadata->Get(ANDROID_LENS_INFO_SHADING_MAP_SIZE, &entry);
135     if ((ret == OK) && (entry.count == 2)) {
136       sensor_chars->lens_shading_map_size[0] = entry.data.i32[0];
137       sensor_chars->lens_shading_map_size[1] = entry.data.i32[1];
138     } else {
139       ALOGE("%s: No available shading map size!", __FUNCTION__);
140       return BAD_VALUE;
141     }
142 
143     ret = metadata->Get(ANDROID_SENSOR_COLOR_TRANSFORM1, &entry);
144     if ((ret != OK) || (entry.count != (3 * 3))) {  // 3x3 rational matrix
145       ALOGE("%s: Invalid ANDROID_SENSOR_COLOR_TRANSFORM1!", __FUNCTION__);
146       return BAD_VALUE;
147     }
148 
149     sensor_chars->color_filter.rX = RAT_TO_FLOAT(entry.data.r[0]);
150     sensor_chars->color_filter.rY = RAT_TO_FLOAT(entry.data.r[1]);
151     sensor_chars->color_filter.rZ = RAT_TO_FLOAT(entry.data.r[2]);
152     sensor_chars->color_filter.grX = RAT_TO_FLOAT(entry.data.r[3]);
153     sensor_chars->color_filter.grY = RAT_TO_FLOAT(entry.data.r[4]);
154     sensor_chars->color_filter.grZ = RAT_TO_FLOAT(entry.data.r[5]);
155     sensor_chars->color_filter.gbX = RAT_TO_FLOAT(entry.data.r[3]);
156     sensor_chars->color_filter.gbY = RAT_TO_FLOAT(entry.data.r[4]);
157     sensor_chars->color_filter.gbZ = RAT_TO_FLOAT(entry.data.r[5]);
158     sensor_chars->color_filter.bX = RAT_TO_FLOAT(entry.data.r[6]);
159     sensor_chars->color_filter.bY = RAT_TO_FLOAT(entry.data.r[7]);
160     sensor_chars->color_filter.bZ = RAT_TO_FLOAT(entry.data.r[8]);
161   } else {
162     sensor_chars->color_arangement = static_cast<
163         camera_metadata_enum_android_sensor_info_color_filter_arrangement>(
164         EmulatedSensor::kSupportedColorFilterArrangement);
165     sensor_chars->max_raw_value = EmulatedSensor::kDefaultMaxRawValue;
166     memcpy(sensor_chars->black_level_pattern,
167            EmulatedSensor::kDefaultBlackLevelPattern,
168            sizeof(sensor_chars->black_level_pattern));
169   }
170 
171   if (HasCapability(
172           metadata,
173           ANDROID_REQUEST_AVAILABLE_CAPABILITIES_PRIVATE_REPROCESSING) ||
174       HasCapability(metadata,
175                     ANDROID_REQUEST_AVAILABLE_CAPABILITIES_YUV_REPROCESSING)) {
176     ret = metadata->Get(ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS, &entry);
177     if ((ret != OK) || (entry.count != 1)) {
178       ALOGE("%s: Invalid ANDROID_REQUEST_MAX_NUM_INPUT_STREAMS!", __FUNCTION__);
179       return BAD_VALUE;
180     }
181 
182     sensor_chars->max_input_streams = entry.data.i32[0];
183   }
184 
185   ret = metadata->Get(ANDROID_REQUEST_PIPELINE_MAX_DEPTH, &entry);
186   if ((ret == OK) && (entry.count == 1)) {
187     if (entry.data.u8[0] == 0) {
188       ALOGE("%s: Maximum request pipeline must have a non zero value!",
189             __FUNCTION__);
190       return BAD_VALUE;
191     }
192     sensor_chars->max_pipeline_depth = entry.data.u8[0];
193   } else {
194     ALOGE("%s: Maximum request pipeline depth absent!", __FUNCTION__);
195     return BAD_VALUE;
196   }
197 
198   ret = metadata->Get(ANDROID_SENSOR_ORIENTATION, &entry);
199   if ((ret == OK) && (entry.count == 1)) {
200     sensor_chars->orientation = entry.data.i32[0];
201   } else {
202     ALOGE("%s: Sensor orientation absent!", __FUNCTION__);
203     return BAD_VALUE;
204   }
205 
206   ret = metadata->Get(ANDROID_LENS_FACING, &entry);
207   if ((ret == OK) && (entry.count == 1)) {
208     sensor_chars->is_front_facing = false;
209     if (ANDROID_LENS_FACING_FRONT == entry.data.u8[0]) {
210       sensor_chars->is_front_facing = true;
211     }
212   } else {
213     ALOGE("%s: Lens facing absent!", __FUNCTION__);
214     return BAD_VALUE;
215   }
216   return ret;
217 }
218 
ClonePhysicalDeviceMap(const PhysicalDeviceMapPtr & src)219 PhysicalDeviceMapPtr ClonePhysicalDeviceMap(const PhysicalDeviceMapPtr& src) {
220   auto ret = std::make_unique<PhysicalDeviceMap>();
221   for (const auto& it : *src) {
222     ret->emplace(it.first, std::make_pair(it.second.first,
223         HalCameraMetadata::Clone(it.second.second.get())));
224   }
225   return ret;
226 }
227 
228 }  // namespace android
229