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