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