summaryrefslogtreecommitdiff
path: root/devices/EmulatedCamera/hwl/EmulatedRequestState.h
blob: 3af793e335bd5c0a2051a018296f996d5f43c3f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifndef EMULATOR_CAMERA_HAL_HWL_REQUEST_STATE_H
#define EMULATOR_CAMERA_HAL_HWL_REQUEST_STATE_H

#include <mutex>
#include <unordered_map>

#include "EmulatedSensor.h"
#include "hwl_types.h"

namespace android {

using google_camera_hal::HalCameraMetadata;
using google_camera_hal::HalStream;
using google_camera_hal::HwlPipelineCallback;
using google_camera_hal::HwlPipelineRequest;
using google_camera_hal::RequestTemplate;

struct PendingRequest;

class EmulatedRequestState {
 public:
  EmulatedRequestState(uint32_t camera_id) : camera_id_(camera_id) {
  }
  virtual ~EmulatedRequestState() {
  }

  status_t Initialize(std::unique_ptr<HalCameraMetadata> static_meta);

  status_t GetDefaultRequest(
      RequestTemplate type,
      std::unique_ptr<HalCameraMetadata>* default_settings /*out*/);

  std::unique_ptr<HwlPipelineResult> InitializeResult(uint32_t pipeline_id,
                                                      uint32_t frame_number);

  status_t InitializeSensorSettings(
      std::unique_ptr<HalCameraMetadata> request_settings,
      uint32_t override_frame_number,
      EmulatedSensor::SensorSettings* sensor_settings /*out*/);

 private:
  bool SupportsCapability(uint8_t cap);

  status_t InitializeRequestDefaults();
  status_t InitializeSensorDefaults();
  status_t InitializeFlashDefaults();
  status_t InitializeControlDefaults();
  status_t InitializeControlAEDefaults();
  status_t InitializeControlAWBDefaults();
  status_t InitializeControlAFDefaults();
  status_t InitializeControlSceneDefaults();
  status_t InitializeHotPixelDefaults();
  status_t InitializeStatisticsDefaults();
  status_t InitializeTonemapDefaults();
  status_t InitializeBlackLevelDefaults();
  status_t InitializeEdgeDefaults();
  status_t InitializeShadingDefaults();
  status_t InitializeNoiseReductionDefaults();
  status_t InitializeColorCorrectionDefaults();
  status_t InitializeScalerDefaults();
  status_t InitializeReprocessDefaults();
  status_t InitializeMeteringRegionDefault(uint32_t tag,
                                           int32_t* region /*out*/);
  status_t InitializeControlefaults();
  status_t InitializeInfoDefaults();
  status_t InitializeLensDefaults();

  status_t ProcessAE();
  status_t ProcessAF();
  status_t ProcessAWB();
  status_t DoFakeAE();
  status_t CompensateAE();
  status_t Update3AMeteringRegion(uint32_t tag,
                                  const HalCameraMetadata& settings,
                                  int32_t* region /*out*/);

  std::mutex request_state_mutex_;
  std::unique_ptr<HalCameraMetadata> request_settings_;

  // Supported capabilities and features
  static const std::set<uint8_t> kSupportedCapabilites;
  static const std::set<uint8_t> kSupportedHWLevels;
  std::unique_ptr<HalCameraMetadata> static_metadata_;
  static const std::vector<int64_t> kSupportedUseCases;

  // android.blacklevel.*
  uint8_t black_level_lock_ = ANDROID_BLACK_LEVEL_LOCK_ON;
  bool report_black_level_lock_ = false;

  // android.colorcorrection.*
  std::set<uint8_t> available_color_aberration_modes_;

  // android.edge.*
  std::set<uint8_t> available_edge_modes_;
  bool report_edge_mode_ = false;

  // android.shading.*
  std::set<uint8_t> available_shading_modes_;

  // android.noiseReduction.*
  std::set<uint8_t> available_noise_reduction_modes_;

  // android.request.*
  std::set<uint8_t> available_capabilities_;
  std::set<int32_t> available_characteristics_;
  std::set<int32_t> available_results_;
  std::set<int32_t> available_requests_;
  uint8_t max_pipeline_depth_ = 0;
  int32_t partial_result_count_ = 1;  // TODO: add support for partial results
  bool supports_manual_sensor_ = false;
  bool supports_manual_post_processing_ = false;
  bool is_backward_compatible_ = false;
  bool is_raw_capable_ = false;
  bool supports_private_reprocessing_ = false;
  bool supports_yuv_reprocessing_ = false;
  bool supports_remosaic_reprocessing_ = false;
  bool supports_stream_use_case_ = false;

  // android.control.*
  struct SceneOverride {
    uint8_t ae_mode, awb_mode, af_mode;
    SceneOverride()
        : ae_mode(ANDROID_CONTROL_AE_MODE_OFF),
          awb_mode(ANDROID_CONTROL_AWB_MODE_OFF),
          af_mode(ANDROID_CONTROL_AF_MODE_OFF) {
    }
    SceneOverride(uint8_t ae, uint8_t awb, uint8_t af)
        : ae_mode(ae), awb_mode(awb), af_mode(af) {
    }
  };

  struct FPSRange {
    int32_t min_fps, max_fps;
    FPSRange() : min_fps(-1), max_fps(-1) {
    }
    FPSRange(int32_t min, int32_t max) : min_fps(min), max_fps(max) {
    }
  };

  struct ExtendedSceneModeCapability {
    int32_t mode, max_width, max_height;
    float min_zoom, max_zoom;
    ExtendedSceneModeCapability()
        : mode(ANDROID_CONTROL_EXTENDED_SCENE_MODE_DISABLED),
          max_width(-1),
          max_height(-1),
          min_zoom(1.0f),
          max_zoom(1.0f) {
    }
    ExtendedSceneModeCapability(int32_t m, int32_t w, int32_t h, float min_z,
                                float max_z)
        : mode(m), max_width(w), max_height(h), min_zoom(min_z), max_zoom(max_z) {
    }
  };

  std::set<uint8_t> available_control_modes_;
  std::set<uint8_t> available_ae_modes_;
  std::set<uint8_t> available_af_modes_;
  std::set<uint8_t> available_awb_modes_;
  std::set<uint8_t> available_scenes_;
  std::set<uint8_t> available_antibanding_modes_;
  std::set<uint8_t> available_effects_;
  std::set<uint8_t> available_vstab_modes_;
  std::set<uint8_t> available_sensor_pixel_modes_;
  std::vector<ExtendedSceneModeCapability> available_extended_scene_mode_caps_;
  std::unordered_map<uint8_t, SceneOverride> scene_overrides_;
  std::vector<FPSRange> available_fps_ranges_;
  int32_t exposure_compensation_range_[2] = {0, 0};
  float max_zoom_ = 1.0f;
  bool zoom_ratio_supported_ = false;
  float min_zoom_ = 1.0f;
  camera_metadata_rational exposure_compensation_step_ = {0, 1};
  bool exposure_compensation_supported_ = false;
  int32_t exposure_compensation_ = 0;
  int32_t ae_metering_region_[5] = {0, 0, 0, 0, 0};
  int32_t awb_metering_region_[5] = {0, 0, 0, 0, 0};
  int32_t af_metering_region_[5] = {0, 0, 0, 0, 0};
  size_t max_ae_regions_ = 0;
  size_t max_awb_regions_ = 0;
  size_t max_af_regions_ = 0;
  uint8_t control_mode_ = ANDROID_CONTROL_MODE_AUTO;
  uint8_t sensor_pixel_mode_ = ANDROID_SENSOR_PIXEL_MODE_DEFAULT;
  uint8_t scene_mode_ = ANDROID_CONTROL_SCENE_MODE_DISABLED;
  uint8_t ae_mode_ = ANDROID_CONTROL_AE_MODE_ON;
  uint8_t awb_mode_ = ANDROID_CONTROL_AWB_MODE_AUTO;
  uint8_t af_mode_ = ANDROID_CONTROL_AF_MODE_AUTO;
  uint8_t ae_lock_ = ANDROID_CONTROL_AE_LOCK_OFF;
  uint8_t ae_state_ = ANDROID_CONTROL_AE_STATE_INACTIVE;
  uint8_t awb_state_ = ANDROID_CONTROL_AWB_STATE_INACTIVE;
  uint8_t awb_lock_ = ANDROID_CONTROL_AWB_LOCK_OFF;
  uint8_t af_state_ = ANDROID_CONTROL_AF_STATE_INACTIVE;
  uint8_t af_trigger_ = ANDROID_CONTROL_AF_TRIGGER_IDLE;
  uint8_t ae_trigger_ = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
  uint8_t autoframing_ = ANDROID_CONTROL_AUTOFRAMING_OFF;
  FPSRange ae_target_fps_ = {0, 0};
  float zoom_ratio_ = 1.0f;
  uint8_t extended_scene_mode_ = ANDROID_CONTROL_EXTENDED_SCENE_MODE_DISABLED;
  static const int32_t kMinimumStreamingFPS = 20;
  bool ae_lock_available_ = false;
  bool report_ae_lock_ = false;
  bool scenes_supported_ = false;
  size_t ae_frame_counter_ = 0;
  bool vstab_available_ = false;
  const size_t kAEPrecaptureMinFrames = 10;
  // Fake AE related constants
  const float kExposureTrackRate = .2f;  // This is the rate at which the fake
                                         // AE will reach the calculated target
  const size_t kStableAeMaxFrames =
      100;  // The number of frames the fake AE will stay in converged state
  // After fake AE switches to state searching the exposure
  // time will wander randomly in region defined by min/max below.
  const float kExposureWanderMin = -2;
  const float kExposureWanderMax = 1;
  const uint32_t kAETargetThreshold =
      10;  // Defines a threshold for reaching the AE target
  int32_t post_raw_boost_ = 100;
  bool report_post_raw_boost_ = false;
  nsecs_t ae_target_exposure_time_ = EmulatedSensor::kDefaultExposureTime;
  nsecs_t current_exposure_time_ = EmulatedSensor::kDefaultExposureTime;
  bool awb_lock_available_ = false;
  bool report_awb_lock_ = false;
  bool af_mode_changed_ = false;
  bool af_supported_ = false;
  bool picture_caf_supported_ = false;
  bool video_caf_supported_ = false;
  int32_t settings_override_ = ANDROID_CONTROL_SETTINGS_OVERRIDE_OFF;
  uint32_t settings_overriding_frame_number_ = 0;

  // android.flash.*
  bool is_flash_supported_ = false;
  uint8_t flash_state_ = ANDROID_FLASH_STATE_UNAVAILABLE;

  // android.sensor.*
  std::pair<int32_t, int32_t> sensor_sensitivity_range_;
  std::pair<nsecs_t, nsecs_t> sensor_exposure_time_range_;
  nsecs_t sensor_max_frame_duration_ =
      EmulatedSensor::kSupportedFrameDurationRange[1];
  nsecs_t sensor_exposure_time_ = EmulatedSensor::kDefaultExposureTime;
  nsecs_t sensor_frame_duration_ = EmulatedSensor::kDefaultFrameDuration;
  int32_t sensor_sensitivity_ = EmulatedSensor::kDefaultSensitivity;
  bool report_frame_duration_ = false;
  bool report_sensitivity_ = false;
  bool report_exposure_time_ = false;
  std::set<int32_t> available_test_pattern_modes_;
  bool report_rolling_shutter_skew_ = false;
  bool report_neutral_color_point_ = false;
  bool report_green_split_ = false;
  bool report_noise_profile_ = false;
  bool report_extended_scene_mode_ = false;

  // android.scaler.*
  bool report_rotate_and_crop_ = false;
  uint8_t rotate_and_crop_ = ANDROID_SCALER_ROTATE_AND_CROP_NONE;
  int32_t scaler_crop_region_default_[4] = {0, 0, 0, 0};
  int32_t scaler_crop_region_max_resolution_[4] = {0, 0, 0, 0};
  std::set<uint8_t> available_rotate_crop_modes_;

  // android.statistics.*
  std::set<uint8_t> available_hot_pixel_map_modes_;
  std::set<uint8_t> available_lens_shading_map_modes_;
  std::set<uint8_t> available_face_detect_modes_;
  uint8_t current_scene_flicker_ = ANDROID_STATISTICS_SCENE_FLICKER_NONE;
  bool report_scene_flicker_ = false;

  // android.tonemap.*
  std::set<uint8_t> available_tonemap_modes_;

  // android.info.*
  uint8_t supported_hw_level_ = 0;
  static const size_t kTemplateCount =
      static_cast<size_t>(RequestTemplate::kManual) + 1;
  std::unique_ptr<HalCameraMetadata> default_requests_[kTemplateCount];
  // Set to true if the camera device has HW level FULL or LEVEL3
  bool is_level_full_or_higher_ = false;

  // android.lens.*
  float minimum_focus_distance_ = 0.f;
  float aperture_ = 0.f;
  float focal_length_ = 0.f;
  float focus_distance_ = 0.f;
  bool report_focus_distance_ = false;
  uint8_t lens_state_ = ANDROID_LENS_STATE_STATIONARY;
  bool report_focus_range_ = false;
  float filter_density_ = 0.f;
  bool report_filter_density_ = false;
  std::set<uint8_t> available_ois_modes_;
  uint8_t ois_mode_ = ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF;
  bool report_ois_mode_ = false;
  float pose_rotation_[4] = {.0f};
  float pose_translation_[3] = {.0f};
  float distortion_[5] = {.0f};
  float intrinsic_calibration_[5] = {.0f};
  bool report_pose_rotation_ = false;
  bool report_pose_translation_ = false;
  bool report_distortion_ = false;
  bool report_intrinsic_calibration_ = false;
  int32_t shading_map_size_[2] = {0};

  unsigned int rand_seed_ = 1;

  // android.hotpixel.*
  std::set<uint8_t> available_hot_pixel_modes_;

  uint32_t camera_id_;

  EmulatedRequestState(const EmulatedRequestState&) = delete;
  EmulatedRequestState& operator=(const EmulatedRequestState&) = delete;
};

}  // namespace android

#endif  // EMULATOR_CAMERA_HAL_HWL_REQUEST_STATE_H