1 /*
2  * Copyright (C) 2023 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 #ifndef EMULATOR_CAMERA_HAL_HWL_CAMERA_DEVICE_INFO_H
18 #define EMULATOR_CAMERA_HAL_HWL_CAMERA_DEVICE_INFO_H
19 
20 #include <log/log.h>
21 
22 #include <memory>
23 #include <set>
24 #include <unordered_map>
25 #include <utility>
26 #include <vector>
27 
28 #include "hal_camera_metadata.h"
29 #include "hal_types.h"
30 #include "utils/HWLUtils.h"
31 
32 namespace android {
33 
34 using google_camera_hal::HalCameraMetadata;
35 using google_camera_hal::kTemplateCount;
36 using google_camera_hal::RequestTemplate;
37 
38 template <typename T>
GetClosestValue(T val,T min,T max)39 T GetClosestValue(T val, T min, T max) {
40   if ((min > max) || ((val >= min) && (val <= max))) {
41     return val;
42   } else if (val > max) {
43     return max;
44   } else {
45     return min;
46   }
47 }
48 
49 struct EmulatedCameraDeviceInfo {
50   static std::unique_ptr<EmulatedCameraDeviceInfo> Create(
51       std::unique_ptr<HalCameraMetadata> static_metadata);
52 
53   static std::unique_ptr<EmulatedCameraDeviceInfo> Clone(
54       const EmulatedCameraDeviceInfo& other);
55 
56   std::unique_ptr<HalCameraMetadata> static_metadata_;
57   std::unique_ptr<HalCameraMetadata> default_requests_[kTemplateCount];
58 
59   static const std::set<uint8_t> kSupportedCapabilites;
60   static const std::set<uint8_t> kSupportedHWLevels;
61   static const std::vector<int64_t> kSupportedUseCases;
62 
63   static const int32_t kMinimumStreamingFPS = 20;
64 
65   // android.blacklevel.*
66   bool report_black_level_lock_ = false;
67   uint8_t black_level_lock_ = ANDROID_BLACK_LEVEL_LOCK_ON;
68 
69   // android.colorcorrection.*
70   std::set<uint8_t> available_color_aberration_modes_;
71 
72   // android.edge.*
73   std::set<uint8_t> available_edge_modes_;
74   bool report_edge_mode_ = false;
75 
76   // android.shading.*
77   std::set<uint8_t> available_shading_modes_;
78 
79   // android.noiseReduction.*
80   std::set<uint8_t> available_noise_reduction_modes_;
81 
82   uint8_t partial_result_count_ = 1;
83 
84   // android.request.*
85   std::set<uint8_t> available_capabilities_;
86   std::set<int32_t> available_characteristics_;
87   std::set<int32_t> available_results_;
88   std::set<int32_t> available_requests_;
89   uint8_t max_pipeline_depth_ = 0;
90   bool supports_manual_sensor_ = false;
91   bool supports_manual_post_processing_ = false;
92   bool supports_remosaic_reprocessing_ = false;
93   bool supports_private_reprocessing_ = false;
94   bool supports_yuv_reprocessing_ = false;
95   bool is_backward_compatible_ = false;
96   bool is_raw_capable_ = false;
97   bool supports_stream_use_case_ = false;
98 
99   // android.info.*
100   bool is_level_full_or_higher_ = false;
101   // Set to true if the camera device has HW level FULL or LEVEL3
102   uint8_t supported_hw_level_ = 0;
103 
104   // android.lens.*
105   float minimum_focus_distance_ = 0.f;
106   float aperture_ = 0.f;
107   float focal_length_ = 0.f;
108   float focus_distance_ = 0.f;
109   bool report_focus_distance_ = false;
110   uint8_t lens_state_ = ANDROID_LENS_STATE_STATIONARY;
111   bool report_focus_range_ = false;
112   float filter_density_ = 0.f;
113   bool report_filter_density_ = false;
114   std::set<uint8_t> available_ois_modes_;
115   uint8_t ois_mode_ = ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF;
116   bool report_ois_mode_ = false;
117   float pose_rotation_[4] = {.0f};
118   float pose_translation_[3] = {.0f};
119   float distortion_[5] = {.0f};
120   float intrinsic_calibration_[5] = {.0f};
121   bool report_pose_rotation_ = false;
122   bool report_pose_translation_ = false;
123   bool report_distortion_ = false;
124   bool report_intrinsic_calibration_ = false;
125   bool report_active_sensor_crop_ = false;
126   bool report_lens_intrinsics_samples_ = false;
127   int32_t shading_map_size_[2] = {0};
128 
129   // android.control.*
130   struct SceneOverride {
131     uint8_t ae_mode, awb_mode, af_mode;
SceneOverrideEmulatedCameraDeviceInfo::SceneOverride132     SceneOverride()
133         : ae_mode(ANDROID_CONTROL_AE_MODE_OFF),
134           awb_mode(ANDROID_CONTROL_AWB_MODE_OFF),
135           af_mode(ANDROID_CONTROL_AF_MODE_OFF) {
136     }
SceneOverrideEmulatedCameraDeviceInfo::SceneOverride137     SceneOverride(uint8_t ae, uint8_t awb, uint8_t af)
138         : ae_mode(ae), awb_mode(awb), af_mode(af) {
139     }
140   };
141 
142   struct FPSRange {
143     int32_t min_fps, max_fps;
FPSRangeEmulatedCameraDeviceInfo::FPSRange144     FPSRange() : min_fps(-1), max_fps(-1) {
145     }
FPSRangeEmulatedCameraDeviceInfo::FPSRange146     FPSRange(int32_t min, int32_t max) : min_fps(min), max_fps(max) {
147     }
148   };
149 
150   struct ExtendedSceneModeCapability {
151     int32_t mode, max_width, max_height;
152     float min_zoom, max_zoom;
ExtendedSceneModeCapabilityEmulatedCameraDeviceInfo::ExtendedSceneModeCapability153     ExtendedSceneModeCapability()
154         : mode(ANDROID_CONTROL_EXTENDED_SCENE_MODE_DISABLED),
155           max_width(-1),
156           max_height(-1),
157           min_zoom(1.0f),
158           max_zoom(1.0f) {
159     }
ExtendedSceneModeCapabilityEmulatedCameraDeviceInfo::ExtendedSceneModeCapability160     ExtendedSceneModeCapability(int32_t m, int32_t w, int32_t h, float min_z,
161                                 float max_z)
162         : mode(m), max_width(w), max_height(h), min_zoom(min_z), max_zoom(max_z) {
163     }
164   };
165 
166   std::set<uint8_t> available_control_modes_;
167   std::set<uint8_t> available_ae_modes_;
168   std::set<uint8_t> available_af_modes_;
169   std::set<uint8_t> available_awb_modes_;
170   std::set<uint8_t> available_scenes_;
171   std::set<uint8_t> available_antibanding_modes_;
172   std::set<uint8_t> available_effects_;
173   std::set<uint8_t> available_vstab_modes_;
174   std::set<uint8_t> available_sensor_pixel_modes_;
175   std::vector<ExtendedSceneModeCapability> available_extended_scene_mode_caps_;
176   std::unordered_map<uint8_t, SceneOverride> scene_overrides_;
177   std::vector<FPSRange> available_fps_ranges_;
178   int32_t exposure_compensation_range_[2] = {0, 0};
179   float max_zoom_ = 1.0f;
180   bool zoom_ratio_supported_ = false;
181   float min_zoom_ = 1.0f;
182   camera_metadata_rational exposure_compensation_step_ = {0, 1};
183   bool exposure_compensation_supported_ = false;
184   int32_t exposure_compensation_ = 0;
185   int32_t ae_metering_region_[5] = {0, 0, 0, 0, 0};
186   int32_t awb_metering_region_[5] = {0, 0, 0, 0, 0};
187   int32_t af_metering_region_[5] = {0, 0, 0, 0, 0};
188   size_t max_ae_regions_ = 0;
189   size_t max_awb_regions_ = 0;
190   size_t max_af_regions_ = 0;
191   uint8_t control_mode_ = ANDROID_CONTROL_MODE_AUTO;
192   uint8_t sensor_pixel_mode_ = ANDROID_SENSOR_PIXEL_MODE_DEFAULT;
193   uint8_t scene_mode_ = ANDROID_CONTROL_SCENE_MODE_DISABLED;
194   uint8_t ae_mode_ = ANDROID_CONTROL_AE_MODE_ON;
195   uint8_t awb_mode_ = ANDROID_CONTROL_AWB_MODE_AUTO;
196   uint8_t af_mode_ = ANDROID_CONTROL_AF_MODE_AUTO;
197   uint8_t ae_lock_ = ANDROID_CONTROL_AE_LOCK_OFF;
198   uint8_t ae_state_ = ANDROID_CONTROL_AE_STATE_INACTIVE;
199   uint8_t awb_state_ = ANDROID_CONTROL_AWB_STATE_INACTIVE;
200   uint8_t awb_lock_ = ANDROID_CONTROL_AWB_LOCK_OFF;
201   uint8_t af_state_ = ANDROID_CONTROL_AF_STATE_INACTIVE;
202   uint8_t af_trigger_ = ANDROID_CONTROL_AF_TRIGGER_IDLE;
203   uint8_t ae_trigger_ = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
204   uint8_t autoframing_ = ANDROID_CONTROL_AUTOFRAMING_OFF;
205   FPSRange ae_target_fps_ = {0, 0};
206   float zoom_ratio_ = 1.0f;
207   uint8_t extended_scene_mode_ = ANDROID_CONTROL_EXTENDED_SCENE_MODE_DISABLED;
208   bool ae_lock_available_ = false;
209   bool report_ae_lock_ = false;
210   bool scenes_supported_ = false;
211   bool vstab_available_ = false;
212   int32_t post_raw_boost_ = 100;
213   bool report_post_raw_boost_ = false;
214   bool awb_lock_available_ = false;
215   bool report_awb_lock_ = false;
216   bool af_supported_ = false;
217   bool picture_caf_supported_ = false;
218   bool video_caf_supported_ = false;
219   int32_t settings_override_ = ANDROID_CONTROL_SETTINGS_OVERRIDE_OFF;
220 
221   // android.flash.*
222   bool is_flash_supported_ = false;
223   uint8_t flash_state_ = ANDROID_FLASH_STATE_UNAVAILABLE;
224   int32_t flash_strength_level_ = 1;
225 
226   // android.hotpixel.*
227   std::set<uint8_t> available_hot_pixel_modes_;
228 
229   // android.scaler.*
230   bool report_rotate_and_crop_ = false;
231   uint8_t rotate_and_crop_ = ANDROID_SCALER_ROTATE_AND_CROP_NONE;
232   int32_t scaler_crop_region_default_[4] = {0, 0, 0, 0};
233   int32_t scaler_crop_region_max_resolution_[4] = {0, 0, 0, 0};
234   std::set<uint8_t> available_rotate_crop_modes_;
235 
236   // android.sensor.*
237   std::pair<int32_t, int32_t> sensor_sensitivity_range_;
238   std::pair<nsecs_t, nsecs_t> sensor_exposure_time_range_;
239   nsecs_t sensor_max_frame_duration_ =
240       EmulatedSensor::kSupportedFrameDurationRange[1];
241   nsecs_t sensor_exposure_time_ = EmulatedSensor::kDefaultExposureTime;
242   nsecs_t sensor_frame_duration_ = EmulatedSensor::kDefaultFrameDuration;
243   int32_t sensor_sensitivity_ = EmulatedSensor::kDefaultSensitivity;
244   bool report_frame_duration_ = false;
245   bool report_sensitivity_ = false;
246   bool report_exposure_time_ = false;
247   std::set<int32_t> available_test_pattern_modes_;
248   bool report_rolling_shutter_skew_ = false;
249   bool report_neutral_color_point_ = false;
250   bool report_green_split_ = false;
251   bool report_noise_profile_ = false;
252   bool report_extended_scene_mode_ = false;
253   uint32_t timestamp_source_ = ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN;
254 
255   // android.statistics.*
256   uint8_t current_scene_flicker_ = ANDROID_STATISTICS_SCENE_FLICKER_NONE;
257   std::set<uint8_t> available_hot_pixel_map_modes_;
258   std::set<uint8_t> available_lens_shading_map_modes_;
259   std::set<uint8_t> available_face_detect_modes_;
260   bool report_scene_flicker_ = false;
261 
262   // android.tonemap.*
263   std::set<uint8_t> available_tonemap_modes_;
264 
265  private:
InitializeEmulatedCameraDeviceInfo266   status_t Initialize(unique_ptr<HalCameraMetadata> staticMetadata) {
267     static_metadata_ = std::move(staticMetadata);
268     return InitializeRequestDefaults();
269   }
270 
271   status_t InitializeRequestDefaults();
272   status_t InitializeSensorDefaults();
273   status_t InitializeFlashDefaults();
274   status_t InitializeControlDefaults();
275   status_t InitializeControlAEDefaults();
276   status_t InitializeControlAWBDefaults();
277   status_t InitializeControlAFDefaults();
278   status_t InitializeControlSceneDefaults();
279   status_t InitializeHotPixelDefaults();
280   status_t InitializeStatisticsDefaults();
281   status_t InitializeTonemapDefaults();
282   status_t InitializeBlackLevelDefaults();
283   status_t InitializeEdgeDefaults();
284   status_t InitializeShadingDefaults();
285   status_t InitializeNoiseReductionDefaults();
286   status_t InitializeColorCorrectionDefaults();
287   status_t InitializeScalerDefaults();
288   status_t InitializeReprocessDefaults();
289   status_t InitializeMeteringRegionDefault(uint32_t tag,
290                                            int32_t* region /*out*/);
291   status_t InitializeControlefaults();
292   status_t InitializeInfoDefaults();
293   status_t InitializeLensDefaults();
294 
295   bool SupportsCapability(uint8_t cap);
296 };
297 
298 }  // namespace android
299 
300 #endif  // EMULATOR_CAMERA_HAL_HWL_CAMERA_DEVICE_INFO_H
301