1 /*
2  * Copyright (C) 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 #ifndef EMULATOR_CAMERA_HAL_HWL_REQUEST_STATE_H
18 #define EMULATOR_CAMERA_HAL_HWL_REQUEST_STATE_H
19 
20 #include <mutex>
21 #include <unordered_map>
22 
23 #include "EmulatedSensor.h"
24 #include "hwl_types.h"
25 
26 namespace android {
27 
28 using google_camera_hal::HalCameraMetadata;
29 using google_camera_hal::HalStream;
30 using google_camera_hal::HwlPipelineCallback;
31 using google_camera_hal::HwlPipelineRequest;
32 using google_camera_hal::RequestTemplate;
33 
34 struct PendingRequest;
35 
36 class EmulatedRequestState {
37  public:
EmulatedRequestState(uint32_t camera_id)38   EmulatedRequestState(uint32_t camera_id) : camera_id_(camera_id) {
39   }
~EmulatedRequestState()40   virtual ~EmulatedRequestState() {
41   }
42 
43   status_t Initialize(std::unique_ptr<HalCameraMetadata> static_meta);
44 
45   status_t GetDefaultRequest(
46       RequestTemplate type,
47       std::unique_ptr<HalCameraMetadata>* default_settings /*out*/);
48 
49   std::unique_ptr<HwlPipelineResult> InitializeResult(uint32_t pipeline_id,
50                                                       uint32_t frame_number);
51 
52   status_t InitializeSensorSettings(
53       std::unique_ptr<HalCameraMetadata> request_settings,
54       EmulatedSensor::SensorSettings* sensor_settings /*out*/);
55 
56  private:
57   bool SupportsCapability(uint8_t cap);
58 
59   status_t InitializeRequestDefaults();
60   status_t InitializeSensorDefaults();
61   status_t InitializeFlashDefaults();
62   status_t InitializeControlDefaults();
63   status_t InitializeControlAEDefaults();
64   status_t InitializeControlAWBDefaults();
65   status_t InitializeControlAFDefaults();
66   status_t InitializeControlSceneDefaults();
67   status_t InitializeHotPixelDefaults();
68   status_t InitializeStatisticsDefaults();
69   status_t InitializeTonemapDefaults();
70   status_t InitializeBlackLevelDefaults();
71   status_t InitializeEdgeDefaults();
72   status_t InitializeShadingDefaults();
73   status_t InitializeNoiseReductionDefaults();
74   status_t InitializeColorCorrectionDefaults();
75   status_t InitializeScalerDefaults();
76   status_t InitializeReprocessDefaults();
77   status_t InitializeMeteringRegionDefault(uint32_t tag,
78                                            int32_t* region /*out*/);
79   status_t InitializeControlefaults();
80   status_t InitializeInfoDefaults();
81   status_t InitializeLensDefaults();
82 
83   status_t ProcessAE();
84   status_t ProcessAF();
85   status_t ProcessAWB();
86   status_t DoFakeAE();
87   status_t CompensateAE();
88   status_t Update3AMeteringRegion(uint32_t tag,
89                                   const HalCameraMetadata& settings,
90                                   int32_t* region /*out*/);
91 
92   std::mutex request_state_mutex_;
93   std::unique_ptr<HalCameraMetadata> request_settings_;
94 
95   // Supported capabilities and features
96   static const std::set<uint8_t> kSupportedCapabilites;
97   static const std::set<uint8_t> kSupportedHWLevels;
98   std::unique_ptr<HalCameraMetadata> static_metadata_;
99 
100   // android.blacklevel.*
101   uint8_t black_level_lock_ = ANDROID_BLACK_LEVEL_LOCK_ON;
102   bool report_black_level_lock_ = false;
103 
104   // android.colorcorrection.*
105   std::set<uint8_t> available_color_aberration_modes_;
106 
107   // android.edge.*
108   std::set<uint8_t> available_edge_modes_;
109   bool report_edge_mode_ = false;
110 
111   // android.shading.*
112   std::set<uint8_t> available_shading_modes_;
113 
114   // android.noiseReduction.*
115   std::set<uint8_t> available_noise_reduction_modes_;
116 
117   // android.request.*
118   std::set<uint8_t> available_capabilities_;
119   std::set<int32_t> available_characteristics_;
120   std::set<int32_t> available_results_;
121   std::set<int32_t> available_requests_;
122   uint8_t max_pipeline_depth_ = 0;
123   int32_t partial_result_count_ = 1;  // TODO: add support for partial results
124   bool supports_manual_sensor_ = false;
125   bool supports_manual_post_processing_ = false;
126   bool is_backward_compatible_ = false;
127   bool is_raw_capable_ = false;
128   bool supports_private_reprocessing_ = false;
129   bool supports_yuv_reprocessing_ = false;
130   bool supports_remosaic_reprocessing_ = false;
131 
132   // android.control.*
133   struct SceneOverride {
134     uint8_t ae_mode, awb_mode, af_mode;
SceneOverrideSceneOverride135     SceneOverride()
136         : ae_mode(ANDROID_CONTROL_AE_MODE_OFF),
137           awb_mode(ANDROID_CONTROL_AWB_MODE_OFF),
138           af_mode(ANDROID_CONTROL_AF_MODE_OFF) {
139     }
SceneOverrideSceneOverride140     SceneOverride(uint8_t ae, uint8_t awb, uint8_t af)
141         : ae_mode(ae), awb_mode(awb), af_mode(af) {
142     }
143   };
144 
145   struct FPSRange {
146     int32_t min_fps, max_fps;
FPSRangeFPSRange147     FPSRange() : min_fps(-1), max_fps(-1) {
148     }
FPSRangeFPSRange149     FPSRange(int32_t min, int32_t max) : min_fps(min), max_fps(max) {
150     }
151   };
152 
153   struct ExtendedSceneModeCapability {
154     int32_t mode, max_width, max_height;
155     float min_zoom, max_zoom;
ExtendedSceneModeCapabilityExtendedSceneModeCapability156     ExtendedSceneModeCapability()
157         : mode(ANDROID_CONTROL_EXTENDED_SCENE_MODE_DISABLED),
158           max_width(-1),
159           max_height(-1),
160           min_zoom(1.0f),
161           max_zoom(1.0f) {
162     }
ExtendedSceneModeCapabilityExtendedSceneModeCapability163     ExtendedSceneModeCapability(int32_t m, int32_t w, int32_t h, float min_z,
164                                 float max_z)
165         : mode(m), max_width(w), max_height(h), min_zoom(min_z), max_zoom(max_z) {
166     }
167   };
168 
169   std::set<uint8_t> available_control_modes_;
170   std::set<uint8_t> available_ae_modes_;
171   std::set<uint8_t> available_af_modes_;
172   std::set<uint8_t> available_awb_modes_;
173   std::set<uint8_t> available_scenes_;
174   std::set<uint8_t> available_antibanding_modes_;
175   std::set<uint8_t> available_effects_;
176   std::set<uint8_t> available_vstab_modes_;
177   std::set<uint8_t> available_sensor_pixel_modes_;
178   std::vector<ExtendedSceneModeCapability> available_extended_scene_mode_caps_;
179   std::unordered_map<uint8_t, SceneOverride> scene_overrides_;
180   std::vector<FPSRange> available_fps_ranges_;
181   int32_t exposure_compensation_range_[2] = {0, 0};
182   float max_zoom_ = 1.0f;
183   bool zoom_ratio_supported_ = false;
184   float min_zoom_ = 1.0f;
185   camera_metadata_rational exposure_compensation_step_ = {0, 1};
186   bool exposure_compensation_supported_ = false;
187   int32_t exposure_compensation_ = 0;
188   int32_t ae_metering_region_[5] = {0, 0, 0, 0, 0};
189   int32_t awb_metering_region_[5] = {0, 0, 0, 0, 0};
190   int32_t af_metering_region_[5] = {0, 0, 0, 0, 0};
191   size_t max_ae_regions_ = 0;
192   size_t max_awb_regions_ = 0;
193   size_t max_af_regions_ = 0;
194   uint8_t control_mode_ = ANDROID_CONTROL_MODE_AUTO;
195   uint8_t sensor_pixel_mode_ = ANDROID_SENSOR_PIXEL_MODE_DEFAULT;
196   uint8_t scene_mode_ = ANDROID_CONTROL_SCENE_MODE_DISABLED;
197   uint8_t ae_mode_ = ANDROID_CONTROL_AE_MODE_ON;
198   uint8_t awb_mode_ = ANDROID_CONTROL_AWB_MODE_AUTO;
199   uint8_t af_mode_ = ANDROID_CONTROL_AF_MODE_AUTO;
200   uint8_t ae_lock_ = ANDROID_CONTROL_AE_LOCK_OFF;
201   uint8_t ae_state_ = ANDROID_CONTROL_AE_STATE_INACTIVE;
202   uint8_t awb_state_ = ANDROID_CONTROL_AWB_STATE_INACTIVE;
203   uint8_t awb_lock_ = ANDROID_CONTROL_AWB_LOCK_OFF;
204   uint8_t af_state_ = ANDROID_CONTROL_AF_STATE_INACTIVE;
205   uint8_t af_trigger_ = ANDROID_CONTROL_AF_TRIGGER_IDLE;
206   uint8_t ae_trigger_ = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
207   FPSRange ae_target_fps_ = {0, 0};
208   float zoom_ratio_ = 1.0f;
209   uint8_t extended_scene_mode_ = ANDROID_CONTROL_EXTENDED_SCENE_MODE_DISABLED;
210   static const int32_t kMinimumStreamingFPS = 20;
211   bool ae_lock_available_ = false;
212   bool report_ae_lock_ = false;
213   bool scenes_supported_ = false;
214   size_t ae_frame_counter_ = 0;
215   bool vstab_available_ = false;
216   const size_t kAEPrecaptureMinFrames = 10;
217   // Fake AE related constants
218   const float kExposureTrackRate = .2f;  // This is the rate at which the fake
219                                          // AE will reach the calculated target
220   const size_t kStableAeMaxFrames =
221       100;  // The number of frames the fake AE will stay in converged state
222   // After fake AE switches to state searching the exposure
223   // time will wander randomly in region defined by min/max below.
224   const float kExposureWanderMin = -2;
225   const float kExposureWanderMax = 1;
226   const uint32_t kAETargetThreshold =
227       10;  // Defines a threshold for reaching the AE target
228   int32_t post_raw_boost_ = 100;
229   bool report_post_raw_boost_ = false;
230   nsecs_t ae_target_exposure_time_ = EmulatedSensor::kDefaultExposureTime;
231   nsecs_t current_exposure_time_ = EmulatedSensor::kDefaultExposureTime;
232   bool awb_lock_available_ = false;
233   bool report_awb_lock_ = false;
234   bool af_mode_changed_ = false;
235   bool af_supported_ = false;
236   bool picture_caf_supported_ = false;
237   bool video_caf_supported_ = false;
238 
239   // android.flash.*
240   bool is_flash_supported_ = false;
241   uint8_t flash_state_ = ANDROID_FLASH_STATE_UNAVAILABLE;
242 
243   // android.sensor.*
244   std::pair<int32_t, int32_t> sensor_sensitivity_range_;
245   std::pair<nsecs_t, nsecs_t> sensor_exposure_time_range_;
246   nsecs_t sensor_max_frame_duration_ =
247       EmulatedSensor::kSupportedFrameDurationRange[1];
248   nsecs_t sensor_exposure_time_ = EmulatedSensor::kDefaultExposureTime;
249   nsecs_t sensor_frame_duration_ = EmulatedSensor::kDefaultFrameDuration;
250   int32_t sensor_sensitivity_ = EmulatedSensor::kDefaultSensitivity;
251   bool report_frame_duration_ = false;
252   bool report_sensitivity_ = false;
253   bool report_exposure_time_ = false;
254   std::set<int32_t> available_test_pattern_modes_;
255   bool report_rolling_shutter_skew_ = false;
256   bool report_neutral_color_point_ = false;
257   bool report_green_split_ = false;
258   bool report_noise_profile_ = false;
259   bool report_extended_scene_mode_ = false;
260 
261   // android.scaler.*
262   bool report_rotate_and_crop_ = false;
263   uint8_t rotate_and_crop_ = ANDROID_SCALER_ROTATE_AND_CROP_NONE;
264   int32_t scaler_crop_region_default_[4] = {0, 0, 0, 0};
265   int32_t scaler_crop_region_max_resolution_[4] = {0, 0, 0, 0};
266   std::set<uint8_t> available_rotate_crop_modes_;
267 
268   // android.statistics.*
269   std::set<uint8_t> available_hot_pixel_map_modes_;
270   std::set<uint8_t> available_lens_shading_map_modes_;
271   std::set<uint8_t> available_face_detect_modes_;
272   uint8_t current_scene_flicker_ = ANDROID_STATISTICS_SCENE_FLICKER_NONE;
273   bool report_scene_flicker_ = false;
274 
275   // android.tonemap.*
276   std::set<uint8_t> available_tonemap_modes_;
277 
278   // android.info.*
279   uint8_t supported_hw_level_ = 0;
280   static const size_t kTemplateCount =
281       static_cast<size_t>(RequestTemplate::kManual) + 1;
282   std::unique_ptr<HalCameraMetadata> default_requests_[kTemplateCount];
283   // Set to true if the camera device has HW level FULL or LEVEL3
284   bool is_level_full_or_higher_ = false;
285 
286   // android.lens.*
287   float minimum_focus_distance_ = 0.f;
288   float aperture_ = 0.f;
289   float focal_length_ = 0.f;
290   float focus_distance_ = 0.f;
291   bool report_focus_distance_ = false;
292   uint8_t lens_state_ = ANDROID_LENS_STATE_STATIONARY;
293   bool report_focus_range_ = false;
294   float filter_density_ = 0.f;
295   bool report_filter_density_ = false;
296   std::set<uint8_t> available_ois_modes_;
297   uint8_t ois_mode_ = ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF;
298   bool report_ois_mode_ = false;
299   float pose_rotation_[4] = {.0f};
300   float pose_translation_[3] = {.0f};
301   float distortion_[5] = {.0f};
302   float intrinsic_calibration_[5] = {.0f};
303   bool report_pose_rotation_ = false;
304   bool report_pose_translation_ = false;
305   bool report_distortion_ = false;
306   bool report_intrinsic_calibration_ = false;
307   int32_t shading_map_size_[2] = {0};
308 
309   unsigned int rand_seed_ = 1;
310 
311   // android.hotpixel.*
312   std::set<uint8_t> available_hot_pixel_modes_;
313 
314   uint32_t camera_id_;
315 
316   EmulatedRequestState(const EmulatedRequestState&) = delete;
317   EmulatedRequestState& operator=(const EmulatedRequestState&) = delete;
318 };
319 
320 }  // namespace android
321 
322 #endif  // EMULATOR_CAMERA_HAL_HWL_REQUEST_STATE_H
323