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 #define LOG_TAG "EmulatedCameraDeviceInfo"
18 #define ATRACE_TAG ATRACE_TAG_CAMERA
19 
20 #include "EmulatedCameraDeviceInfo.h"
21 
22 #include <inttypes.h>
23 #include <log/log.h>
24 
25 namespace android {
26 
27 const std::set<uint8_t> EmulatedCameraDeviceInfo::kSupportedCapabilites = {
28     ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE,
29     ANDROID_REQUEST_AVAILABLE_CAPABILITIES_MANUAL_SENSOR,
30     ANDROID_REQUEST_AVAILABLE_CAPABILITIES_MANUAL_POST_PROCESSING,
31     ANDROID_REQUEST_AVAILABLE_CAPABILITIES_RAW,
32     ANDROID_REQUEST_AVAILABLE_CAPABILITIES_READ_SENSOR_SETTINGS,
33     ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BURST_CAPTURE,
34     ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DEPTH_OUTPUT,
35     ANDROID_REQUEST_AVAILABLE_CAPABILITIES_PRIVATE_REPROCESSING,
36     ANDROID_REQUEST_AVAILABLE_CAPABILITIES_YUV_REPROCESSING,
37     ANDROID_REQUEST_AVAILABLE_CAPABILITIES_LOGICAL_MULTI_CAMERA,
38     ANDROID_REQUEST_AVAILABLE_CAPABILITIES_REMOSAIC_REPROCESSING,
39     ANDROID_REQUEST_AVAILABLE_CAPABILITIES_ULTRA_HIGH_RESOLUTION_SENSOR,
40     ANDROID_REQUEST_AVAILABLE_CAPABILITIES_DYNAMIC_RANGE_TEN_BIT,
41     ANDROID_REQUEST_AVAILABLE_CAPABILITIES_STREAM_USE_CASE,
42     ANDROID_REQUEST_AVAILABLE_CAPABILITIES_COLOR_SPACE_PROFILES};
43 
44 const std::set<uint8_t> EmulatedCameraDeviceInfo::kSupportedHWLevels = {
45     ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_LIMITED,
46     ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_FULL,
47     ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_3,
48 };
49 
50 const std::vector<int64_t> EmulatedCameraDeviceInfo::kSupportedUseCases = {
51     ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_DEFAULT,
52     ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_PREVIEW,
53     ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_STILL_CAPTURE,
54     ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_RECORD,
55     ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_PREVIEW_VIDEO_STILL,
56     ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES_VIDEO_CALL};
57 
Create(std::unique_ptr<HalCameraMetadata> static_metadata)58 std::unique_ptr<EmulatedCameraDeviceInfo> EmulatedCameraDeviceInfo::Create(
59     std::unique_ptr<HalCameraMetadata> static_metadata) {
60   auto deviceInfo = std::make_unique<EmulatedCameraDeviceInfo>();
61   if (deviceInfo == nullptr) {
62     ALOGE("%s: Creating EmulatedCameraDeviceInfo failed.", __FUNCTION__);
63     return nullptr;
64   }
65 
66   status_t res = deviceInfo->Initialize(std::move(static_metadata));
67   if (res != OK) {
68     ALOGE("%s: Failed to initialize EmulatedCameraDeviceInfo: %s(%d)",
69           __FUNCTION__, strerror(-res), res);
70     return nullptr;
71   }
72 
73   return deviceInfo;
74 }
75 
Clone(const EmulatedCameraDeviceInfo & other)76 std::unique_ptr<EmulatedCameraDeviceInfo> EmulatedCameraDeviceInfo::Clone(
77     const EmulatedCameraDeviceInfo& other) {
78   std::unique_ptr<HalCameraMetadata> static_metadata =
79       HalCameraMetadata::Clone(other.static_metadata_.get());
80 
81   return EmulatedCameraDeviceInfo::Create(std::move(static_metadata));
82 }
83 
InitializeSensorDefaults()84 status_t EmulatedCameraDeviceInfo::InitializeSensorDefaults() {
85   camera_metadata_ro_entry_t entry;
86   auto ret =
87       static_metadata_->Get(ANDROID_SENSOR_INFO_SENSITIVITY_RANGE, &entry);
88   if ((ret == OK) && (entry.count == 2)) {
89     sensor_sensitivity_range_ =
90         std::make_pair(entry.data.i32[0], entry.data.i32[1]);
91   } else if (!supports_manual_sensor_) {
92     sensor_sensitivity_range_ =
93         std::make_pair(EmulatedSensor::kSupportedSensitivityRange[0],
94                        EmulatedSensor::kSupportedSensitivityRange[1]);
95   } else {
96     ALOGE("%s: Manual sensor devices must advertise sensor sensitivity range!",
97           __FUNCTION__);
98     return BAD_VALUE;
99   }
100 
101   ret = static_metadata_->Get(ANDROID_SENSOR_INFO_EXPOSURE_TIME_RANGE, &entry);
102   if ((ret == OK) && (entry.count == 2)) {
103     sensor_exposure_time_range_ =
104         std::make_pair(entry.data.i64[0], entry.data.i64[1]);
105   } else if (!supports_manual_sensor_) {
106     sensor_exposure_time_range_ =
107         std::make_pair(EmulatedSensor::kSupportedExposureTimeRange[0],
108                        EmulatedSensor::kSupportedExposureTimeRange[1]);
109   } else {
110     ALOGE(
111         "%s: Manual sensor devices must advertise sensor exposure time range!",
112         __FUNCTION__);
113     return BAD_VALUE;
114   }
115 
116   ret = static_metadata_->Get(ANDROID_SENSOR_INFO_MAX_FRAME_DURATION, &entry);
117   if ((ret == OK) && (entry.count == 1)) {
118     sensor_max_frame_duration_ = entry.data.i64[0];
119   } else if (!supports_manual_sensor_) {
120     sensor_max_frame_duration_ = EmulatedSensor::kSupportedFrameDurationRange[1];
121   } else {
122     ALOGE("%s: Manual sensor devices must advertise sensor max frame duration!",
123           __FUNCTION__);
124     return BAD_VALUE;
125   }
126 
127   if (supports_manual_sensor_) {
128     if (available_requests_.find(ANDROID_SENSOR_SENSITIVITY) ==
129         available_requests_.end()) {
130       ALOGE(
131           "%s: Sensor sensitivity must be configurable on manual sensor "
132           "devices!",
133           __FUNCTION__);
134       return BAD_VALUE;
135     }
136 
137     if (available_requests_.find(ANDROID_SENSOR_EXPOSURE_TIME) ==
138         available_requests_.end()) {
139       ALOGE(
140           "%s: Sensor exposure time must be configurable on manual sensor "
141           "devices!",
142           __FUNCTION__);
143       return BAD_VALUE;
144     }
145 
146     if (available_requests_.find(ANDROID_SENSOR_FRAME_DURATION) ==
147         available_requests_.end()) {
148       ALOGE(
149           "%s: Sensor frame duration must be configurable on manual sensor "
150           "devices!",
151           __FUNCTION__);
152       return BAD_VALUE;
153     }
154   }
155 
156   report_rolling_shutter_skew_ =
157       available_results_.find(ANDROID_SENSOR_ROLLING_SHUTTER_SKEW) !=
158       available_results_.end();
159   report_sensitivity_ = available_results_.find(ANDROID_SENSOR_SENSITIVITY) !=
160                         available_results_.end();
161   report_exposure_time_ =
162       available_results_.find(ANDROID_SENSOR_EXPOSURE_TIME) !=
163       available_results_.end();
164   report_frame_duration_ =
165       available_results_.find(ANDROID_SENSOR_FRAME_DURATION) !=
166       available_results_.end();
167   report_neutral_color_point_ =
168       available_results_.find(ANDROID_SENSOR_NEUTRAL_COLOR_POINT) !=
169       available_results_.end();
170   report_green_split_ = available_results_.find(ANDROID_SENSOR_GREEN_SPLIT) !=
171                         available_results_.end();
172   report_noise_profile_ =
173       available_results_.find(ANDROID_SENSOR_NOISE_PROFILE) !=
174       available_results_.end();
175 
176   if (is_raw_capable_ && !report_green_split_) {
177     ALOGE("%s: RAW capable devices must be able to report the noise profile!",
178           __FUNCTION__);
179     return BAD_VALUE;
180   }
181 
182   if (is_raw_capable_ && !report_neutral_color_point_) {
183     ALOGE(
184         "%s: RAW capable devices must be able to report the neutral color "
185         "point!",
186         __FUNCTION__);
187     return BAD_VALUE;
188   }
189 
190   if (is_raw_capable_ && !report_green_split_) {
191     ALOGE("%s: RAW capable devices must be able to report the green split!",
192           __FUNCTION__);
193     return BAD_VALUE;
194   }
195   if (available_results_.find(ANDROID_SENSOR_TIMESTAMP) ==
196       available_results_.end()) {
197     ALOGE("%s: Sensor timestamp must always be part of the results!",
198           __FUNCTION__);
199     return BAD_VALUE;
200   }
201 
202   ret = static_metadata_->Get(ANDROID_SENSOR_AVAILABLE_TEST_PATTERN_MODES,
203                               &entry);
204   if (ret == OK) {
205     available_test_pattern_modes_.insert(entry.data.i32,
206                                          entry.data.i32 + entry.count);
207   } else {
208     ALOGE("%s: No available test pattern modes!", __FUNCTION__);
209     return BAD_VALUE;
210   }
211 
212   sensor_exposure_time_ = GetClosestValue(EmulatedSensor::kDefaultExposureTime,
213                                           sensor_exposure_time_range_.first,
214                                           sensor_exposure_time_range_.second);
215   sensor_frame_duration_ =
216       GetClosestValue(EmulatedSensor::kDefaultFrameDuration,
217                       EmulatedSensor::kSupportedFrameDurationRange[0],
218                       sensor_max_frame_duration_);
219   sensor_sensitivity_ = GetClosestValue(EmulatedSensor::kDefaultSensitivity,
220                                         sensor_sensitivity_range_.first,
221                                         sensor_sensitivity_range_.second);
222 
223   bool off_test_pattern_mode_supported =
224       available_test_pattern_modes_.find(ANDROID_SENSOR_TEST_PATTERN_MODE_OFF) !=
225       available_test_pattern_modes_.end();
226   int32_t test_pattern_mode = (off_test_pattern_mode_supported)
227                                   ? ANDROID_SENSOR_TEST_PATTERN_MODE_OFF
228                                   : *available_test_pattern_modes_.begin();
229   int32_t test_pattern_data[4] = {0, 0, 0, 0};
230 
231   for (size_t idx = 0; idx < kTemplateCount; idx++) {
232     if (default_requests_[idx].get() == nullptr) {
233       continue;
234     }
235 
236     default_requests_[idx]->Set(ANDROID_SENSOR_EXPOSURE_TIME,
237                                 &sensor_exposure_time_, 1);
238     default_requests_[idx]->Set(ANDROID_SENSOR_FRAME_DURATION,
239                                 &sensor_frame_duration_, 1);
240     default_requests_[idx]->Set(ANDROID_SENSOR_SENSITIVITY,
241                                 &sensor_sensitivity_, 1);
242     default_requests_[idx]->Set(ANDROID_SENSOR_TEST_PATTERN_MODE,
243                                 &test_pattern_mode, 1);
244     default_requests_[idx]->Set(ANDROID_SENSOR_TEST_PATTERN_DATA,
245                                 test_pattern_data, 4);
246   }
247 
248   return OK;
249 }
250 
InitializeStatisticsDefaults()251 status_t EmulatedCameraDeviceInfo::InitializeStatisticsDefaults() {
252   camera_metadata_ro_entry_t entry;
253   auto ret = static_metadata_->Get(
254       ANDROID_STATISTICS_INFO_AVAILABLE_FACE_DETECT_MODES, &entry);
255   if (ret == OK) {
256     available_face_detect_modes_.insert(entry.data.u8,
257                                         entry.data.u8 + entry.count);
258   } else {
259     ALOGE("%s: No available face detect modes!", __FUNCTION__);
260     return BAD_VALUE;
261   }
262 
263   ret = static_metadata_->Get(
264       ANDROID_STATISTICS_INFO_AVAILABLE_LENS_SHADING_MAP_MODES, &entry);
265   if (ret == OK) {
266     available_lens_shading_map_modes_.insert(entry.data.u8,
267                                              entry.data.u8 + entry.count);
268   } else {
269     ALOGE("%s: No available lens shading modes!", __FUNCTION__);
270     return BAD_VALUE;
271   }
272 
273   ret = static_metadata_->Get(
274       ANDROID_STATISTICS_INFO_AVAILABLE_HOT_PIXEL_MAP_MODES, &entry);
275   if (ret == OK) {
276     available_hot_pixel_map_modes_.insert(entry.data.u8,
277                                           entry.data.u8 + entry.count);
278   } else if (is_raw_capable_) {
279     ALOGE("%s: RAW capable device must support hot pixel map modes!",
280           __FUNCTION__);
281     return BAD_VALUE;
282   } else {
283     available_hot_pixel_map_modes_.emplace(
284         ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF);
285   }
286 
287   bool hot_pixel_mode_off_supported =
288       available_hot_pixel_map_modes_.find(
289           ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF) !=
290       available_hot_pixel_map_modes_.end();
291   bool face_detect_mode_off_supported =
292       available_face_detect_modes_.find(
293           ANDROID_STATISTICS_FACE_DETECT_MODE_OFF) !=
294       available_face_detect_modes_.end();
295   bool lens_shading_map_mode_off_supported =
296       available_lens_shading_map_modes_.find(
297           ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_ON) !=
298       available_lens_shading_map_modes_.end();
299   bool lens_shading_map_mode_on_supported =
300       available_lens_shading_map_modes_.find(
301           ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_ON) !=
302       available_lens_shading_map_modes_.end();
303   if (is_raw_capable_ && !lens_shading_map_mode_on_supported) {
304     ALOGE("%s: RAW capable device must support lens shading map reporting!",
305           __FUNCTION__);
306     return BAD_VALUE;
307   }
308 
309   if (lens_shading_map_mode_on_supported &&
310       (available_results_.find(ANDROID_STATISTICS_LENS_SHADING_MAP) ==
311        available_results_.end())) {
312     ALOGE(
313         "%s: Lens shading map reporting available but corresponding result key "
314         "is absent!",
315         __FUNCTION__);
316     return BAD_VALUE;
317   }
318 
319   if (lens_shading_map_mode_on_supported &&
320       ((shading_map_size_[0] == 0) || (shading_map_size_[1] == 0))) {
321     ALOGE(
322         "%s: Lens shading map reporting available but without valid shading "
323         "map size!",
324         __FUNCTION__);
325     return BAD_VALUE;
326   }
327 
328   report_lens_intrinsics_samples_ =
329       (available_results_.find(ANDROID_STATISTICS_LENS_INTRINSIC_SAMPLES) !=
330        available_results_.end()) &&
331       (available_results_.find(ANDROID_STATISTICS_LENS_INTRINSIC_TIMESTAMPS) !=
332        available_results_.end());
333 
334   report_scene_flicker_ =
335       available_results_.find(ANDROID_STATISTICS_SCENE_FLICKER) !=
336       available_results_.end();
337 
338   uint8_t face_detect_mode = face_detect_mode_off_supported
339                                  ? ANDROID_STATISTICS_FACE_DETECT_MODE_OFF
340                                  : *available_face_detect_modes_.begin();
341   uint8_t hot_pixel_map_mode = hot_pixel_mode_off_supported
342                                    ? ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE_OFF
343                                    : *available_hot_pixel_map_modes_.begin();
344   uint8_t lens_shading_map_mode =
345       lens_shading_map_mode_off_supported
346           ? ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_OFF
347           : *available_lens_shading_map_modes_.begin();
348   for (size_t idx = 0; idx < kTemplateCount; idx++) {
349     if (default_requests_[idx].get() == nullptr) {
350       continue;
351     }
352 
353     if ((static_cast<RequestTemplate>(idx) == RequestTemplate::kStillCapture) &&
354         is_raw_capable_ && lens_shading_map_mode_on_supported) {
355       uint8_t lens_shading_map_on = ANDROID_STATISTICS_LENS_SHADING_MAP_MODE_ON;
356       default_requests_[idx]->Set(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE,
357                                   &lens_shading_map_on, 1);
358     } else {
359       default_requests_[idx]->Set(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE,
360                                   &lens_shading_map_mode, 1);
361     }
362 
363     default_requests_[idx]->Set(ANDROID_STATISTICS_FACE_DETECT_MODE,
364                                 &face_detect_mode, 1);
365     default_requests_[idx]->Set(ANDROID_STATISTICS_HOT_PIXEL_MAP_MODE,
366                                 &hot_pixel_map_mode, 1);
367   }
368 
369   return InitializeBlackLevelDefaults();
370 }
371 
InitializeControlSceneDefaults()372 status_t EmulatedCameraDeviceInfo::InitializeControlSceneDefaults() {
373   camera_metadata_ro_entry_t entry;
374   auto ret =
375       static_metadata_->Get(ANDROID_CONTROL_AVAILABLE_SCENE_MODES, &entry);
376   if (ret == OK) {
377     available_scenes_.insert(entry.data.u8, entry.data.u8 + entry.count);
378   } else {
379     ALOGE("%s: No available scene modes!", __FUNCTION__);
380     return BAD_VALUE;
381   }
382 
383   if ((entry.count == 1) &&
384       (entry.data.u8[0] == ANDROID_CONTROL_SCENE_MODE_DISABLED)) {
385     scenes_supported_ = false;
386     return OK;
387   } else {
388     scenes_supported_ = true;
389   }
390 
391   if (available_requests_.find(ANDROID_CONTROL_SCENE_MODE) ==
392       available_requests_.end()) {
393     ALOGE("%s: Scene mode cannot be set!", __FUNCTION__);
394     return BAD_VALUE;
395   }
396 
397   if (available_results_.find(ANDROID_CONTROL_SCENE_MODE) ==
398       available_results_.end()) {
399     ALOGE("%s: Scene mode cannot be reported!", __FUNCTION__);
400     return BAD_VALUE;
401   }
402 
403   camera_metadata_ro_entry_t overrides_entry;
404   ret = static_metadata_->Get(ANDROID_CONTROL_SCENE_MODE_OVERRIDES,
405                               &overrides_entry);
406   if ((ret == OK) && ((overrides_entry.count / 3) == available_scenes_.size()) &&
407       ((overrides_entry.count % 3) == 0)) {
408     for (size_t i = 0; i < entry.count; i++) {
409       SceneOverride scene(overrides_entry.data.u8[i * 3],
410                           overrides_entry.data.u8[i * 3 + 1],
411                           overrides_entry.data.u8[i * 3 + 2]);
412       if (available_ae_modes_.find(scene.ae_mode) == available_ae_modes_.end()) {
413         ALOGE("%s: AE scene mode override: %d not supported!", __FUNCTION__,
414               scene.ae_mode);
415         return BAD_VALUE;
416       }
417       if (available_awb_modes_.find(scene.awb_mode) ==
418           available_awb_modes_.end()) {
419         ALOGE("%s: AWB scene mode override: %d not supported!", __FUNCTION__,
420               scene.awb_mode);
421         return BAD_VALUE;
422       }
423       if (available_af_modes_.find(scene.af_mode) == available_af_modes_.end()) {
424         ALOGE("%s: AF scene mode override: %d not supported!", __FUNCTION__,
425               scene.af_mode);
426         return BAD_VALUE;
427       }
428       scene_overrides_.emplace(entry.data.u8[i], scene);
429     }
430   } else {
431     ALOGE("%s: No available scene overrides!", __FUNCTION__);
432     return BAD_VALUE;
433   }
434 
435   return OK;
436 }
437 
InitializeControlAFDefaults()438 status_t EmulatedCameraDeviceInfo::InitializeControlAFDefaults() {
439   camera_metadata_ro_entry_t entry;
440   auto ret = static_metadata_->Get(ANDROID_CONTROL_AF_AVAILABLE_MODES, &entry);
441   if (ret == OK) {
442     available_af_modes_.insert(entry.data.u8, entry.data.u8 + entry.count);
443   } else {
444     ALOGE("%s: No available AF modes!", __FUNCTION__);
445     return BAD_VALUE;
446   }
447   // Off mode must always be present
448   if (available_af_modes_.find(ANDROID_CONTROL_AF_MODE_OFF) ==
449       available_af_modes_.end()) {
450     ALOGE("%s: AF off control mode must always be present!", __FUNCTION__);
451     return BAD_VALUE;
452   }
453 
454   if (available_requests_.find(ANDROID_CONTROL_AF_MODE) ==
455       available_requests_.end()) {
456     ALOGE("%s: Clients must be able to set AF mode!", __FUNCTION__);
457     return BAD_VALUE;
458   }
459 
460   if (available_requests_.find(ANDROID_CONTROL_AF_TRIGGER) ==
461       available_requests_.end()) {
462     ALOGE("%s: Clients must be able to set AF trigger!", __FUNCTION__);
463     return BAD_VALUE;
464   }
465   if (available_results_.find(ANDROID_CONTROL_AF_TRIGGER) ==
466       available_results_.end()) {
467     ALOGE("%s: AF trigger must be reported!", __FUNCTION__);
468     return BAD_VALUE;
469   }
470 
471   if (available_results_.find(ANDROID_CONTROL_AF_MODE) ==
472       available_results_.end()) {
473     ALOGE("%s: AF mode must be reported!", __FUNCTION__);
474     return BAD_VALUE;
475   }
476 
477   if (available_results_.find(ANDROID_CONTROL_AF_STATE) ==
478       available_results_.end()) {
479     ALOGE("%s: AF state must be reported!", __FUNCTION__);
480     return BAD_VALUE;
481   }
482 
483   bool auto_mode_present =
484       available_af_modes_.find(ANDROID_CONTROL_AF_MODE_AUTO) !=
485       available_af_modes_.end();
486   bool picture_caf_mode_present =
487       available_af_modes_.find(ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE) !=
488       available_af_modes_.end();
489   bool video_caf_mode_present =
490       available_af_modes_.find(ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO) !=
491       available_af_modes_.end();
492   af_supported_ = auto_mode_present && (minimum_focus_distance_ > .0f);
493   picture_caf_supported_ =
494       picture_caf_mode_present && (minimum_focus_distance_ > .0f);
495   video_caf_supported_ =
496       video_caf_mode_present && (minimum_focus_distance_ > .0f);
497 
498   return OK;
499 }
500 
InitializeControlAWBDefaults()501 status_t EmulatedCameraDeviceInfo::InitializeControlAWBDefaults() {
502   camera_metadata_ro_entry_t entry;
503   auto ret = static_metadata_->Get(ANDROID_CONTROL_AWB_AVAILABLE_MODES, &entry);
504   if (ret == OK) {
505     available_awb_modes_.insert(entry.data.u8, entry.data.u8 + entry.count);
506   } else {
507     ALOGE("%s: No available AWB modes!", __FUNCTION__);
508     return BAD_VALUE;
509   }
510   // Auto mode must always be present
511   if (available_awb_modes_.find(ANDROID_CONTROL_AWB_MODE_AUTO) ==
512       available_awb_modes_.end()) {
513     ALOGE("%s: AWB auto control mode must always be present!", __FUNCTION__);
514     return BAD_VALUE;
515   }
516 
517   if (available_results_.find(ANDROID_CONTROL_AWB_MODE) ==
518       available_results_.end()) {
519     ALOGE("%s: AWB mode must be reported!", __FUNCTION__);
520     return BAD_VALUE;
521   }
522 
523   if (available_results_.find(ANDROID_CONTROL_AWB_STATE) ==
524       available_results_.end()) {
525     ALOGE("%s: AWB state must be reported!", __FUNCTION__);
526     return BAD_VALUE;
527   }
528 
529   ret = static_metadata_->Get(ANDROID_CONTROL_AWB_LOCK_AVAILABLE, &entry);
530   if ((ret == OK) && (entry.count == 1)) {
531     awb_lock_available_ =
532         entry.data.u8[0] == ANDROID_CONTROL_AWB_LOCK_AVAILABLE_TRUE;
533   } else {
534     ALOGV("%s: No available AWB lock!", __FUNCTION__);
535     awb_lock_available_ = false;
536   }
537   report_awb_lock_ = available_results_.find(ANDROID_CONTROL_AWB_LOCK) !=
538                      available_results_.end();
539 
540   return OK;
541 }
542 
InitializeBlackLevelDefaults()543 status_t EmulatedCameraDeviceInfo::InitializeBlackLevelDefaults() {
544   if (is_level_full_or_higher_) {
545     if (available_requests_.find(ANDROID_BLACK_LEVEL_LOCK) ==
546         available_requests_.end()) {
547       ALOGE(
548           "%s: Full or above capable devices must be able to set the black "
549           "level lock!",
550           __FUNCTION__);
551       return BAD_VALUE;
552     }
553 
554     if (available_results_.find(ANDROID_BLACK_LEVEL_LOCK) ==
555         available_results_.end()) {
556       ALOGE(
557           "%s: Full or above capable devices must be able to report the black "
558           "level lock!",
559           __FUNCTION__);
560       return BAD_VALUE;
561     }
562 
563     report_black_level_lock_ = true;
564     uint8_t blackLevelLock = ANDROID_BLACK_LEVEL_LOCK_OFF;
565     for (size_t idx = 0; idx < kTemplateCount; idx++) {
566       if (default_requests_[idx].get() == nullptr) {
567         continue;
568       }
569 
570       default_requests_[idx]->Set(ANDROID_BLACK_LEVEL_LOCK, &blackLevelLock, 1);
571     }
572   }
573 
574   return InitializeEdgeDefaults();
575 }
576 
InitializeControlAEDefaults()577 status_t EmulatedCameraDeviceInfo::InitializeControlAEDefaults() {
578   camera_metadata_ro_entry_t entry;
579   auto ret = static_metadata_->Get(ANDROID_CONTROL_AE_AVAILABLE_MODES, &entry);
580   if (ret == OK) {
581     available_ae_modes_.insert(entry.data.u8, entry.data.u8 + entry.count);
582   } else {
583     ALOGE("%s: No available AE modes!", __FUNCTION__);
584     return BAD_VALUE;
585   }
586   // On mode must always be present
587   if (available_ae_modes_.find(ANDROID_CONTROL_AE_MODE_ON) ==
588       available_ae_modes_.end()) {
589     ALOGE("%s: AE on control mode must always be present!", __FUNCTION__);
590     return BAD_VALUE;
591   }
592 
593   if (available_results_.find(ANDROID_CONTROL_AE_MODE) ==
594       available_results_.end()) {
595     ALOGE("%s: AE mode must be reported!", __FUNCTION__);
596     return BAD_VALUE;
597   }
598 
599   if (available_results_.find(ANDROID_CONTROL_AE_STATE) ==
600       available_results_.end()) {
601     ALOGE("%s: AE state must be reported!", __FUNCTION__);
602     return BAD_VALUE;
603   }
604 
605   ret = static_metadata_->Get(ANDROID_CONTROL_AE_LOCK_AVAILABLE, &entry);
606   if ((ret == OK) && (entry.count == 1)) {
607     ae_lock_available_ =
608         entry.data.u8[0] == ANDROID_CONTROL_AE_LOCK_AVAILABLE_TRUE;
609   } else {
610     ALOGV("%s: No available AE lock!", __FUNCTION__);
611     ae_lock_available_ = false;
612   }
613   report_ae_lock_ = available_results_.find(ANDROID_CONTROL_AE_LOCK) !=
614                     available_results_.end();
615 
616   if (supports_manual_sensor_) {
617     if (!ae_lock_available_) {
618       ALOGE("%s: AE lock must always be available for manual sensors!",
619             __FUNCTION__);
620       return BAD_VALUE;
621     }
622     auto off_mode = available_control_modes_.find(ANDROID_CONTROL_MODE_OFF);
623     if (off_mode == available_control_modes_.end()) {
624       ALOGE("%s: Off control mode must always be present for manual sensors!",
625             __FUNCTION__);
626       return BAD_VALUE;
627     }
628 
629     off_mode = available_ae_modes_.find(ANDROID_CONTROL_AE_MODE_OFF);
630     if (off_mode == available_ae_modes_.end()) {
631       ALOGE(
632           "%s: AE off control mode must always be present for manual sensors!",
633           __FUNCTION__);
634       return BAD_VALUE;
635     }
636   }
637 
638   if (available_requests_.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER) ==
639       available_requests_.end()) {
640     ALOGE("%s: Clients must be able to set AE pre-capture trigger!",
641           __FUNCTION__);
642     return BAD_VALUE;
643   }
644 
645   if (available_results_.find(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER) ==
646       available_results_.end()) {
647     ALOGE("%s: AE pre-capture trigger must be reported!", __FUNCTION__);
648     return BAD_VALUE;
649   }
650 
651   ret = static_metadata_->Get(ANDROID_CONTROL_AE_AVAILABLE_ANTIBANDING_MODES,
652                               &entry);
653   if (ret == OK) {
654     available_antibanding_modes_.insert(entry.data.u8,
655                                         entry.data.u8 + entry.count);
656   } else {
657     ALOGE("%s: No available antibanding modes!", __FUNCTION__);
658     return BAD_VALUE;
659   }
660 
661   ret = static_metadata_->Get(ANDROID_CONTROL_AE_COMPENSATION_RANGE, &entry);
662   if ((ret == OK) && (entry.count == 2)) {
663     exposure_compensation_range_[0] = entry.data.i32[0];
664     exposure_compensation_range_[1] = entry.data.i32[1];
665   } else {
666     ALOGE("%s: No available exposure compensation range!", __FUNCTION__);
667     return BAD_VALUE;
668   }
669 
670   ret = static_metadata_->Get(ANDROID_CONTROL_AE_COMPENSATION_STEP, &entry);
671   if ((ret == OK) && (entry.count == 1)) {
672     exposure_compensation_step_ = entry.data.r[0];
673   } else {
674     ALOGE("%s: No available exposure compensation step!", __FUNCTION__);
675     return BAD_VALUE;
676   }
677 
678   bool ae_comp_requests =
679       available_requests_.find(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION) !=
680       available_requests_.end();
681   bool ae_comp_results =
682       available_results_.find(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION) !=
683       available_results_.end();
684   exposure_compensation_supported_ =
685       ((exposure_compensation_range_[0] < 0) &&
686        (exposure_compensation_range_[1] > 0) &&
687        (exposure_compensation_step_.denominator > 0) &&
688        (exposure_compensation_step_.numerator > 0)) &&
689       ae_comp_results && ae_comp_requests;
690 
691   return OK;
692 }
693 
InitializeMeteringRegionDefault(uint32_t tag,int32_t * region)694 status_t EmulatedCameraDeviceInfo::InitializeMeteringRegionDefault(
695     uint32_t tag, int32_t* region /*out*/) {
696   if (region == nullptr) {
697     return BAD_VALUE;
698   }
699   if (available_requests_.find(tag) == available_requests_.end()) {
700     ALOGE("%s: %d metering region configuration must be supported!",
701           __FUNCTION__, tag);
702     return BAD_VALUE;
703   }
704   if (available_results_.find(tag) == available_results_.end()) {
705     ALOGE("%s: %d metering region must be reported!", __FUNCTION__, tag);
706     return BAD_VALUE;
707   }
708 
709   region[0] = scaler_crop_region_default_[0];
710   region[1] = scaler_crop_region_default_[1];
711   region[2] = scaler_crop_region_default_[2];
712   region[3] = scaler_crop_region_default_[3];
713   region[4] = 0;
714 
715   return OK;
716 }
717 
InitializeControlDefaults()718 status_t EmulatedCameraDeviceInfo::InitializeControlDefaults() {
719   camera_metadata_ro_entry_t entry;
720   int32_t metering_area[5] = {0};  // (top, left, width, height, wight)
721   auto ret = static_metadata_->Get(ANDROID_CONTROL_AVAILABLE_MODES, &entry);
722   if (ret == OK) {
723     available_control_modes_.insert(entry.data.u8, entry.data.u8 + entry.count);
724   } else {
725     ALOGE("%s: No available control modes!", __FUNCTION__);
726     return BAD_VALUE;
727   }
728 
729   available_sensor_pixel_modes_.insert(ANDROID_SENSOR_PIXEL_MODE_DEFAULT);
730 
731   if (SupportsCapability(
732           ANDROID_REQUEST_AVAILABLE_CAPABILITIES_ULTRA_HIGH_RESOLUTION_SENSOR)) {
733     available_sensor_pixel_modes_.insert(
734         ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION);
735   }
736 
737   // Auto mode must always be present
738   if (available_control_modes_.find(ANDROID_CONTROL_MODE_AUTO) ==
739       available_control_modes_.end()) {
740     ALOGE("%s: Auto control modes must always be present!", __FUNCTION__);
741     return BAD_VALUE;
742   }
743 
744   // Capture intent must always be user configurable
745   if (available_requests_.find(ANDROID_CONTROL_CAPTURE_INTENT) ==
746       available_requests_.end()) {
747     ALOGE("%s: Clients must be able to set the capture intent!", __FUNCTION__);
748     return BAD_VALUE;
749   }
750 
751   ret = static_metadata_->Get(ANDROID_CONTROL_AE_AVAILABLE_TARGET_FPS_RANGES,
752                               &entry);
753   if ((ret == OK) && ((entry.count % 2) == 0)) {
754     available_fps_ranges_.reserve(entry.count / 2);
755     for (size_t i = 0; i < entry.count; i += 2) {
756       FPSRange range(entry.data.i32[i], entry.data.i32[i + 1]);
757       if (range.min_fps > range.max_fps) {
758         ALOGE("%s: Minimum framerate: %d bigger than maximum framerate: %d",
759               __FUNCTION__, range.min_fps, range.max_fps);
760         return BAD_VALUE;
761       }
762       if ((range.max_fps >= kMinimumStreamingFPS) &&
763           (range.max_fps == range.min_fps) && (ae_target_fps_.max_fps == 0)) {
764         ae_target_fps_ = range;
765       }
766       available_fps_ranges_.push_back(range);
767     }
768   } else {
769     ALOGE("%s: No available framerate ranges!", __FUNCTION__);
770     return BAD_VALUE;
771   }
772 
773   if (ae_target_fps_.max_fps == 0) {
774     ALOGE("%s: No minimum streaming capable framerate range available!",
775           __FUNCTION__);
776     return BAD_VALUE;
777   }
778 
779   if (available_requests_.find(ANDROID_CONTROL_AE_TARGET_FPS_RANGE) ==
780       available_requests_.end()) {
781     ALOGE("%s: Clients must be able to set the target framerate range!",
782           __FUNCTION__);
783     return BAD_VALUE;
784   }
785 
786   if (available_results_.find(ANDROID_CONTROL_AE_TARGET_FPS_RANGE) ==
787       available_results_.end()) {
788     ALOGE("%s: Target framerate must be reported!", __FUNCTION__);
789     return BAD_VALUE;
790   }
791 
792   report_extended_scene_mode_ =
793       available_results_.find(ANDROID_CONTROL_EXTENDED_SCENE_MODE) !=
794       available_results_.end();
795 
796   if (is_backward_compatible_) {
797     ret = static_metadata_->Get(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
798                                 &entry);
799     if (ret == OK) {
800       post_raw_boost_ = entry.data.i32[0];
801     } else {
802       ALOGW("%s: No available post RAW boost! Setting default!", __FUNCTION__);
803       post_raw_boost_ = 100;
804     }
805     report_post_raw_boost_ =
806         available_results_.find(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST) !=
807         available_results_.end();
808 
809     ret = static_metadata_->Get(ANDROID_CONTROL_AVAILABLE_EFFECTS, &entry);
810     if ((ret == OK) && (entry.count > 0)) {
811       available_effects_.insert(entry.data.u8, entry.data.u8 + entry.count);
812       if (available_effects_.find(ANDROID_CONTROL_EFFECT_MODE_OFF) ==
813           available_effects_.end()) {
814         ALOGE("%s: Off color effect mode not supported!", __FUNCTION__);
815         return BAD_VALUE;
816       }
817     } else {
818       ALOGE("%s: No available effects!", __FUNCTION__);
819       return BAD_VALUE;
820     }
821 
822     ret = static_metadata_->Get(
823         ANDROID_CONTROL_AVAILABLE_VIDEO_STABILIZATION_MODES, &entry);
824     if ((ret == OK) && (entry.count > 0)) {
825       available_vstab_modes_.insert(entry.data.u8, entry.data.u8 + entry.count);
826       if (available_vstab_modes_.find(
827               ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF) ==
828           available_vstab_modes_.end()) {
829         ALOGE("%s: Off video stabilization mode not supported!", __FUNCTION__);
830         return BAD_VALUE;
831       }
832       if (available_vstab_modes_.find(
833               ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_ON) !=
834           available_vstab_modes_.end()) {
835         vstab_available_ = true;
836       }
837     } else {
838       ALOGE("%s: No available video stabilization modes!", __FUNCTION__);
839       return BAD_VALUE;
840     }
841 
842     ret = static_metadata_->Get(ANDROID_SCALER_AVAILABLE_MAX_DIGITAL_ZOOM,
843                                 &entry);
844     if ((ret == OK) && (entry.count > 0)) {
845       if (entry.count != 1) {
846         ALOGE("%s: Invalid max digital zoom capability!", __FUNCTION__);
847         return BAD_VALUE;
848       }
849       max_zoom_ = entry.data.f[0];
850     } else {
851       ALOGE("%s: No available max digital zoom", __FUNCTION__);
852       return BAD_VALUE;
853     }
854 
855     ret = static_metadata_->Get(ANDROID_CONTROL_ZOOM_RATIO_RANGE, &entry);
856     if ((ret == OK) && (entry.count > 0)) {
857       if (entry.count != 2) {
858         ALOGE("%s: Invalid zoom ratio range capability!", __FUNCTION__);
859         return BAD_VALUE;
860       }
861 
862       if (entry.data.f[1] != max_zoom_) {
863         ALOGE("%s: Max zoom ratio must be equal to max digital zoom",
864               __FUNCTION__);
865         return BAD_VALUE;
866       }
867 
868       if (entry.data.f[1] < entry.data.f[0]) {
869         ALOGE("%s: Max zoom ratio must be larger than min zoom ratio",
870               __FUNCTION__);
871         return BAD_VALUE;
872       }
873 
874       // Validity check request and result keys
875       if (available_requests_.find(ANDROID_CONTROL_ZOOM_RATIO) ==
876           available_requests_.end()) {
877         ALOGE("%s: Zoom ratio tag must be available in available request keys",
878               __FUNCTION__);
879         return BAD_VALUE;
880       }
881       if (available_results_.find(ANDROID_CONTROL_ZOOM_RATIO) ==
882           available_results_.end()) {
883         ALOGE("%s: Zoom ratio tag must be available in available result keys",
884               __FUNCTION__);
885         return BAD_VALUE;
886       }
887 
888       zoom_ratio_supported_ = true;
889       min_zoom_ = entry.data.f[0];
890     }
891 
892     ret = static_metadata_->Get(
893         ANDROID_CONTROL_AVAILABLE_EXTENDED_SCENE_MODE_MAX_SIZES, &entry);
894     if ((ret == OK) && (entry.count > 0)) {
895       if (entry.count % 3 != 0) {
896         ALOGE("%s: Invalid bokeh capabilities!", __FUNCTION__);
897         return BAD_VALUE;
898       }
899 
900       camera_metadata_ro_entry_t zoom_ratio_ranges_entry;
901       ret = static_metadata_->Get(
902           ANDROID_CONTROL_AVAILABLE_EXTENDED_SCENE_MODE_ZOOM_RATIO_RANGES,
903           &zoom_ratio_ranges_entry);
904       if (ret != OK ||
905           zoom_ratio_ranges_entry.count / 2 != entry.count / 3 - 1) {
906         ALOGE("%s: Invalid bokeh mode zoom ratio ranges.", __FUNCTION__);
907         return BAD_VALUE;
908       }
909 
910       // Validity check request and characteristics keys
911       if (available_requests_.find(ANDROID_CONTROL_EXTENDED_SCENE_MODE) ==
912           available_requests_.end()) {
913         ALOGE("%s: Extended scene mode must be configurable for this device",
914               __FUNCTION__);
915         return BAD_VALUE;
916       }
917       if (available_characteristics_.find(
918               ANDROID_CONTROL_AVAILABLE_EXTENDED_SCENE_MODE_MAX_SIZES) ==
919               available_characteristics_.end() ||
920           available_characteristics_.find(
921               ANDROID_CONTROL_AVAILABLE_EXTENDED_SCENE_MODE_ZOOM_RATIO_RANGES) ==
922               available_characteristics_.end()) {
923         ALOGE(
924             "%s: ExtendedSceneMode maxSizes and zoomRatioRanges "
925             "characteristics keys must "
926             "be available",
927             __FUNCTION__);
928         return BAD_VALUE;
929       }
930 
931       // Derive available bokeh caps.
932       StreamConfigurationMap stream_configuration_map(*static_metadata_);
933       std::set<StreamSize> yuv_sizes = stream_configuration_map.GetOutputSizes(
934           HAL_PIXEL_FORMAT_YCBCR_420_888);
935       bool has_extended_scene_mode_off = false;
936       for (size_t i = 0, j = 0; i < entry.count; i += 3) {
937         int32_t mode = entry.data.i32[i];
938         int32_t max_width = entry.data.i32[i + 1];
939         int32_t max_height = entry.data.i32[i + 2];
940         float min_zoom_ratio, max_zoom_ratio;
941 
942         if (mode < ANDROID_CONTROL_EXTENDED_SCENE_MODE_DISABLED ||
943             (mode > ANDROID_CONTROL_EXTENDED_SCENE_MODE_BOKEH_CONTINUOUS &&
944              mode < ANDROID_CONTROL_EXTENDED_SCENE_MODE_VENDOR_START)) {
945           ALOGE("%s: Invalid extended scene mode %d", __FUNCTION__, mode);
946           return BAD_VALUE;
947         }
948 
949         if (mode == ANDROID_CONTROL_EXTENDED_SCENE_MODE_DISABLED) {
950           has_extended_scene_mode_off = true;
951           if (max_width != 0 || max_height != 0) {
952             ALOGE(
953                 "%s: Invalid max width or height for "
954                 "EXTENDED_SCENE_MODE_DISABLED",
955                 __FUNCTION__);
956             return BAD_VALUE;
957           }
958           min_zoom_ratio = min_zoom_;
959           max_zoom_ratio = max_zoom_;
960         } else if (yuv_sizes.find({max_width, max_height}) == yuv_sizes.end()) {
961           ALOGE("%s: Invalid max width or height for extended scene mode %d",
962                 __FUNCTION__, mode);
963           return BAD_VALUE;
964         } else {
965           min_zoom_ratio = zoom_ratio_ranges_entry.data.f[j];
966           max_zoom_ratio = zoom_ratio_ranges_entry.data.f[j + 1];
967           j += 2;
968         }
969 
970         ExtendedSceneModeCapability cap(mode, max_width, max_height,
971                                         min_zoom_ratio, max_zoom_ratio);
972         available_extended_scene_mode_caps_.push_back(cap);
973       }
974       if (!has_extended_scene_mode_off) {
975         ALOGE("%s: Off extended scene mode not supported!", __FUNCTION__);
976         return BAD_VALUE;
977       }
978     }
979 
980     ret = static_metadata_->Get(ANDROID_CONTROL_MAX_REGIONS, &entry);
981     if ((ret == OK) && (entry.count == 3)) {
982       max_ae_regions_ = entry.data.i32[0];
983       max_awb_regions_ = entry.data.i32[1];
984       max_af_regions_ = entry.data.i32[2];
985     } else {
986       ALOGE(
987           "%s: Metering regions must be available for backward compatible "
988           "devices!",
989           __FUNCTION__);
990       return BAD_VALUE;
991     }
992 
993     if ((is_level_full_or_higher_) &&
994         ((max_ae_regions_ == 0) || (max_af_regions_ == 0))) {
995       ALOGE(
996           "%s: Full and higher level cameras must support at AF and AE "
997           "metering regions",
998           __FUNCTION__);
999       return BAD_VALUE;
1000     }
1001 
1002     if (max_ae_regions_ > 0) {
1003       ret = InitializeMeteringRegionDefault(ANDROID_CONTROL_AE_REGIONS,
1004                                             ae_metering_region_);
1005       if (ret != OK) {
1006         return ret;
1007       }
1008     }
1009 
1010     if (max_awb_regions_ > 0) {
1011       ret = InitializeMeteringRegionDefault(ANDROID_CONTROL_AWB_REGIONS,
1012                                             awb_metering_region_);
1013       if (ret != OK) {
1014         return ret;
1015       }
1016     }
1017 
1018     if (max_af_regions_ > 0) {
1019       ret = InitializeMeteringRegionDefault(ANDROID_CONTROL_AF_REGIONS,
1020                                             af_metering_region_);
1021       if (ret != OK) {
1022         return ret;
1023       }
1024     }
1025 
1026     ret = InitializeControlAEDefaults();
1027     if (ret != OK) {
1028       return ret;
1029     }
1030 
1031     ret = InitializeControlAWBDefaults();
1032     if (ret != OK) {
1033       return ret;
1034     }
1035 
1036     ret = InitializeControlAFDefaults();
1037     if (ret != OK) {
1038       return ret;
1039     }
1040 
1041     ret = InitializeControlSceneDefaults();
1042     if (ret != OK) {
1043       return ret;
1044     }
1045   }
1046 
1047   for (size_t idx = 0; idx < kTemplateCount; idx++) {
1048     auto template_idx = static_cast<RequestTemplate>(idx);
1049     if (default_requests_[idx].get() == nullptr) {
1050       continue;
1051     }
1052 
1053     uint8_t intent = ANDROID_CONTROL_CAPTURE_INTENT_CUSTOM;
1054     uint8_t control_mode, ae_mode, awb_mode, af_mode, scene_mode, vstab_mode;
1055     control_mode = ANDROID_CONTROL_MODE_AUTO;
1056     ae_mode = ANDROID_CONTROL_AE_MODE_ON;
1057     awb_mode = ANDROID_CONTROL_AWB_MODE_AUTO;
1058     af_mode = af_supported_ ? ANDROID_CONTROL_AF_MODE_AUTO
1059                             : ANDROID_CONTROL_AF_MODE_OFF;
1060     scene_mode = ANDROID_CONTROL_SCENE_MODE_DISABLED;
1061     vstab_mode = ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF;
1062     uint8_t effect_mode = ANDROID_CONTROL_EFFECT_MODE_OFF;
1063     uint8_t ae_lock = ANDROID_CONTROL_AE_LOCK_OFF;
1064     uint8_t awb_lock = ANDROID_CONTROL_AWB_LOCK_OFF;
1065     int32_t ae_target_fps[] = {ae_target_fps_.min_fps, ae_target_fps_.max_fps};
1066     float zoom_ratio = 1.0f;
1067     switch (template_idx) {
1068       case RequestTemplate::kManual:
1069         intent = ANDROID_CONTROL_CAPTURE_INTENT_MANUAL;
1070         control_mode = ANDROID_CONTROL_MODE_OFF;
1071         ae_mode = ANDROID_CONTROL_AE_MODE_OFF;
1072         awb_mode = ANDROID_CONTROL_AWB_MODE_OFF;
1073         af_mode = ANDROID_CONTROL_AF_MODE_OFF;
1074         break;
1075       case RequestTemplate::kZeroShutterLag:
1076         intent = ANDROID_CONTROL_CAPTURE_INTENT_ZERO_SHUTTER_LAG;
1077         if (picture_caf_supported_) {
1078           af_mode = ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE;
1079         }
1080         break;
1081       case RequestTemplate::kPreview:
1082         intent = ANDROID_CONTROL_CAPTURE_INTENT_PREVIEW;
1083         if (picture_caf_supported_) {
1084           af_mode = ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE;
1085         }
1086         break;
1087       case RequestTemplate::kStillCapture:
1088         intent = ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE;
1089         if (picture_caf_supported_) {
1090           af_mode = ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE;
1091         }
1092         break;
1093       case RequestTemplate::kVideoRecord:
1094         intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_RECORD;
1095         if (video_caf_supported_) {
1096           af_mode = ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO;
1097         }
1098         if (vstab_available_) {
1099           vstab_mode = ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_ON;
1100         }
1101         break;
1102       case RequestTemplate::kVideoSnapshot:
1103         intent = ANDROID_CONTROL_CAPTURE_INTENT_VIDEO_SNAPSHOT;
1104         if (video_caf_supported_) {
1105           af_mode = ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO;
1106         }
1107         if (vstab_available_) {
1108           vstab_mode = ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_ON;
1109         }
1110         break;
1111       default:
1112         // Noop
1113         break;
1114     }
1115 
1116     if (intent != ANDROID_CONTROL_CAPTURE_INTENT_CUSTOM) {
1117       default_requests_[idx]->Set(ANDROID_CONTROL_CAPTURE_INTENT, &intent, 1);
1118       default_requests_[idx]->Set(ANDROID_CONTROL_MODE, &control_mode, 1);
1119       default_requests_[idx]->Set(ANDROID_CONTROL_AE_MODE, &ae_mode, 1);
1120       default_requests_[idx]->Set(ANDROID_CONTROL_AE_TARGET_FPS_RANGE,
1121                                   ae_target_fps, ARRAY_SIZE(ae_target_fps));
1122       default_requests_[idx]->Set(ANDROID_CONTROL_AWB_MODE, &awb_mode, 1);
1123       default_requests_[idx]->Set(ANDROID_CONTROL_AF_MODE, &af_mode, 1);
1124       if (is_backward_compatible_) {
1125         default_requests_[idx]->Set(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
1126                                     &post_raw_boost_, 1);
1127         if (vstab_available_) {
1128           default_requests_[idx]->Set(ANDROID_CONTROL_VIDEO_STABILIZATION_MODE,
1129                                       &vstab_mode, 1);
1130         }
1131         if (ae_lock_available_) {
1132           default_requests_[idx]->Set(ANDROID_CONTROL_AE_LOCK, &ae_lock, 1);
1133         }
1134         if (awb_lock_available_) {
1135           default_requests_[idx]->Set(ANDROID_CONTROL_AWB_LOCK, &awb_lock, 1);
1136         }
1137         if (scenes_supported_) {
1138           default_requests_[idx]->Set(ANDROID_CONTROL_SCENE_MODE, &scene_mode,
1139                                       1);
1140         }
1141         if (max_ae_regions_ > 0) {
1142           default_requests_[idx]->Set(ANDROID_CONTROL_AE_REGIONS, metering_area,
1143                                       ARRAY_SIZE(metering_area));
1144         }
1145         if (max_awb_regions_ > 0) {
1146           default_requests_[idx]->Set(ANDROID_CONTROL_AWB_REGIONS,
1147                                       metering_area, ARRAY_SIZE(metering_area));
1148         }
1149         if (max_af_regions_ > 0) {
1150           default_requests_[idx]->Set(ANDROID_CONTROL_AF_REGIONS, metering_area,
1151                                       ARRAY_SIZE(metering_area));
1152         }
1153         if (exposure_compensation_supported_) {
1154           default_requests_[idx]->Set(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
1155                                       &exposure_compensation_, 1);
1156         }
1157         if (zoom_ratio_supported_) {
1158           default_requests_[idx]->Set(ANDROID_CONTROL_ZOOM_RATIO, &zoom_ratio,
1159                                       1);
1160         }
1161         bool is_auto_antbanding_supported =
1162             available_antibanding_modes_.find(
1163                 ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO) !=
1164             available_antibanding_modes_.end();
1165         uint8_t antibanding_mode = is_auto_antbanding_supported
1166                                        ? ANDROID_CONTROL_AE_ANTIBANDING_MODE_AUTO
1167                                        : *available_antibanding_modes_.begin();
1168         default_requests_[idx]->Set(ANDROID_CONTROL_AE_ANTIBANDING_MODE,
1169                                     &antibanding_mode, 1);
1170         default_requests_[idx]->Set(ANDROID_CONTROL_EFFECT_MODE, &effect_mode,
1171                                     1);
1172         uint8_t ae_trigger = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
1173         default_requests_[idx]->Set(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1174                                     &ae_trigger, 1);
1175         uint8_t af_trigger = ANDROID_CONTROL_AF_TRIGGER_IDLE;
1176         default_requests_[idx]->Set(ANDROID_CONTROL_AF_TRIGGER, &af_trigger, 1);
1177       }
1178     }
1179 
1180     int32_t settings_override = ANDROID_CONTROL_SETTINGS_OVERRIDE_OFF;
1181     default_requests_[idx]->Set(ANDROID_CONTROL_SETTINGS_OVERRIDE,
1182                                 &settings_override, 1);
1183   }
1184 
1185   return InitializeHotPixelDefaults();
1186 }
1187 
InitializeTonemapDefaults()1188 status_t EmulatedCameraDeviceInfo::InitializeTonemapDefaults() {
1189   if (is_backward_compatible_) {
1190     camera_metadata_ro_entry_t entry;
1191     auto ret =
1192         static_metadata_->Get(ANDROID_TONEMAP_AVAILABLE_TONE_MAP_MODES, &entry);
1193     if (ret == OK) {
1194       available_tonemap_modes_.insert(entry.data.u8,
1195                                       entry.data.u8 + entry.count);
1196     } else {
1197       ALOGE("%s: No available tonemap modes!", __FUNCTION__);
1198       return BAD_VALUE;
1199     }
1200 
1201     if ((is_level_full_or_higher_) && (available_tonemap_modes_.size() < 3)) {
1202       ALOGE(
1203           "%s: Full and higher level cameras must support at least three or "
1204           "more tonemap modes",
1205           __FUNCTION__);
1206       return BAD_VALUE;
1207     }
1208 
1209     bool fast_mode_supported =
1210         available_tonemap_modes_.find(ANDROID_TONEMAP_MODE_FAST) !=
1211         available_tonemap_modes_.end();
1212     bool hq_mode_supported =
1213         available_tonemap_modes_.find(ANDROID_TONEMAP_MODE_HIGH_QUALITY) !=
1214         available_tonemap_modes_.end();
1215     uint8_t tonemap_mode = *available_tonemap_modes_.begin();
1216     for (size_t idx = 0; idx < kTemplateCount; idx++) {
1217       if (default_requests_[idx].get() == nullptr) {
1218         continue;
1219       }
1220 
1221       switch (static_cast<RequestTemplate>(idx)) {
1222         case RequestTemplate::kVideoRecord:  // Pass-through
1223         case RequestTemplate::kPreview:
1224           if (fast_mode_supported) {
1225             tonemap_mode = ANDROID_TONEMAP_MODE_FAST;
1226           }
1227           break;
1228         case RequestTemplate::kVideoSnapshot:  // Pass-through
1229         case RequestTemplate::kStillCapture:
1230           if (hq_mode_supported) {
1231             tonemap_mode = ANDROID_TONEMAP_MODE_HIGH_QUALITY;
1232           }
1233           break;
1234         default:
1235           // Noop
1236           break;
1237       }
1238 
1239       default_requests_[idx]->Set(ANDROID_TONEMAP_MODE, &tonemap_mode, 1);
1240       default_requests_[idx]->Set(
1241           ANDROID_TONEMAP_CURVE_RED, EmulatedSensor::kDefaultToneMapCurveRed,
1242           ARRAY_SIZE(EmulatedSensor::kDefaultToneMapCurveRed));
1243       default_requests_[idx]->Set(
1244           ANDROID_TONEMAP_CURVE_GREEN, EmulatedSensor::kDefaultToneMapCurveGreen,
1245           ARRAY_SIZE(EmulatedSensor::kDefaultToneMapCurveGreen));
1246       default_requests_[idx]->Set(
1247           ANDROID_TONEMAP_CURVE_BLUE, EmulatedSensor::kDefaultToneMapCurveBlue,
1248           ARRAY_SIZE(EmulatedSensor::kDefaultToneMapCurveBlue));
1249     }
1250   }
1251 
1252   return InitializeStatisticsDefaults();
1253 }
1254 
InitializeEdgeDefaults()1255 status_t EmulatedCameraDeviceInfo::InitializeEdgeDefaults() {
1256   if (is_backward_compatible_) {
1257     camera_metadata_ro_entry_t entry;
1258     auto ret = static_metadata_->Get(ANDROID_EDGE_AVAILABLE_EDGE_MODES, &entry);
1259     if (ret == OK) {
1260       available_edge_modes_.insert(entry.data.u8, entry.data.u8 + entry.count);
1261     } else {
1262       ALOGE("%s: No available edge modes!", __FUNCTION__);
1263       return BAD_VALUE;
1264     }
1265 
1266     report_edge_mode_ =
1267         available_results_.find(ANDROID_EDGE_MODE) != available_results_.end();
1268     bool is_fast_mode_supported =
1269         available_edge_modes_.find(ANDROID_EDGE_MODE_FAST) !=
1270         available_edge_modes_.end();
1271     bool is_hq_mode_supported =
1272         available_edge_modes_.find(ANDROID_EDGE_MODE_HIGH_QUALITY) !=
1273         available_edge_modes_.end();
1274     bool is_zsl_mode_supported =
1275         available_edge_modes_.find(ANDROID_EDGE_MODE_ZERO_SHUTTER_LAG) !=
1276         available_edge_modes_.end();
1277     uint8_t edge_mode = *available_ae_modes_.begin();
1278     for (size_t idx = 0; idx < kTemplateCount; idx++) {
1279       if (default_requests_[idx].get() == nullptr) {
1280         continue;
1281       }
1282 
1283       switch (static_cast<RequestTemplate>(idx)) {
1284         case RequestTemplate::kVideoRecord:  // Pass-through
1285         case RequestTemplate::kPreview:
1286           if (is_fast_mode_supported) {
1287             edge_mode = ANDROID_EDGE_MODE_FAST;
1288           }
1289           break;
1290         case RequestTemplate::kVideoSnapshot:  // Pass-through
1291         case RequestTemplate::kStillCapture:
1292           if (is_hq_mode_supported) {
1293             edge_mode = ANDROID_EDGE_MODE_HIGH_QUALITY;
1294           }
1295           break;
1296         case RequestTemplate::kZeroShutterLag:
1297           if (is_zsl_mode_supported) {
1298             edge_mode = ANDROID_EDGE_MODE_ZERO_SHUTTER_LAG;
1299           }
1300           break;
1301         default:
1302           // Noop
1303           break;
1304       }
1305 
1306       default_requests_[idx]->Set(ANDROID_EDGE_MODE, &edge_mode, 1);
1307     }
1308   }
1309 
1310   return InitializeShadingDefaults();
1311 }
1312 
InitializeColorCorrectionDefaults()1313 status_t EmulatedCameraDeviceInfo::InitializeColorCorrectionDefaults() {
1314   camera_metadata_ro_entry_t entry;
1315   auto ret = static_metadata_->Get(
1316       ANDROID_COLOR_CORRECTION_AVAILABLE_ABERRATION_MODES, &entry);
1317   if (ret == OK) {
1318     available_color_aberration_modes_.insert(entry.data.u8,
1319                                              entry.data.u8 + entry.count);
1320   } else if (supports_manual_post_processing_) {
1321     ALOGE(
1322         "%s: Devices capable of manual post-processing must support color "
1323         "abberation!",
1324         __FUNCTION__);
1325     return BAD_VALUE;
1326   }
1327 
1328   if (!available_color_aberration_modes_.empty()) {
1329     bool is_fast_mode_supported =
1330         available_color_aberration_modes_.find(
1331             ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST) !=
1332         available_color_aberration_modes_.end();
1333     bool is_hq_mode_supported =
1334         available_color_aberration_modes_.find(
1335             ANDROID_COLOR_CORRECTION_ABERRATION_MODE_HIGH_QUALITY) !=
1336         available_color_aberration_modes_.end();
1337     uint8_t color_aberration = *available_color_aberration_modes_.begin();
1338     uint8_t color_correction_mode = ANDROID_COLOR_CORRECTION_MODE_FAST;
1339     for (size_t idx = 0; idx < kTemplateCount; idx++) {
1340       if (default_requests_[idx].get() == nullptr) {
1341         continue;
1342       }
1343 
1344       switch (static_cast<RequestTemplate>(idx)) {
1345         case RequestTemplate::kVideoRecord:  // Pass-through
1346         case RequestTemplate::kPreview:
1347           if (is_fast_mode_supported) {
1348             color_aberration = ANDROID_COLOR_CORRECTION_ABERRATION_MODE_FAST;
1349           }
1350           break;
1351         case RequestTemplate::kVideoSnapshot:  // Pass-through
1352         case RequestTemplate::kStillCapture:
1353           if (is_hq_mode_supported) {
1354             color_aberration =
1355                 ANDROID_COLOR_CORRECTION_ABERRATION_MODE_HIGH_QUALITY;
1356           }
1357           break;
1358         default:
1359           // Noop
1360           break;
1361       }
1362 
1363       default_requests_[idx]->Set(ANDROID_COLOR_CORRECTION_ABERRATION_MODE,
1364                                   &color_aberration, 1);
1365       if (is_backward_compatible_) {
1366         default_requests_[idx]->Set(ANDROID_COLOR_CORRECTION_MODE,
1367                                     &color_correction_mode, 1);
1368         default_requests_[idx]->Set(
1369             ANDROID_COLOR_CORRECTION_TRANSFORM,
1370             EmulatedSensor::kDefaultColorTransform,
1371             ARRAY_SIZE(EmulatedSensor::kDefaultColorTransform));
1372         default_requests_[idx]->Set(
1373             ANDROID_COLOR_CORRECTION_GAINS,
1374             EmulatedSensor::kDefaultColorCorrectionGains,
1375             ARRAY_SIZE(EmulatedSensor::kDefaultColorCorrectionGains));
1376       }
1377     }
1378   }
1379 
1380   return InitializeSensorDefaults();
1381 }
1382 
InitializeScalerDefaults()1383 status_t EmulatedCameraDeviceInfo::InitializeScalerDefaults() {
1384   if (is_backward_compatible_) {
1385     camera_metadata_ro_entry_t entry;
1386     auto ret =
1387         static_metadata_->Get(ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE, &entry);
1388     if ((ret == OK) && (entry.count == 4)) {
1389       scaler_crop_region_default_[0] = entry.data.i32[0];
1390       scaler_crop_region_default_[1] = entry.data.i32[1];
1391       scaler_crop_region_default_[2] = entry.data.i32[2];
1392       scaler_crop_region_default_[3] = entry.data.i32[3];
1393     } else {
1394       ALOGE("%s: Sensor pixel array size is not available!", __FUNCTION__);
1395       return BAD_VALUE;
1396     }
1397 
1398     if (SupportsCapability(
1399             ANDROID_REQUEST_AVAILABLE_CAPABILITIES_ULTRA_HIGH_RESOLUTION_SENSOR)) {
1400       ret = static_metadata_->Get(
1401           ANDROID_SENSOR_INFO_ACTIVE_ARRAY_SIZE_MAXIMUM_RESOLUTION, &entry);
1402       if ((ret == OK) && (entry.count == 4)) {
1403         scaler_crop_region_max_resolution_[0] = entry.data.i32[0];
1404         scaler_crop_region_max_resolution_[1] = entry.data.i32[1];
1405         scaler_crop_region_max_resolution_[2] = entry.data.i32[2];
1406         scaler_crop_region_max_resolution_[3] = entry.data.i32[3];
1407       } else {
1408         ALOGE(
1409             "%s: Sensor pixel array size maximum resolution is not available!",
1410             __FUNCTION__);
1411         return BAD_VALUE;
1412       }
1413     }
1414 
1415     if (available_requests_.find(ANDROID_SCALER_CROP_REGION) ==
1416         available_requests_.end()) {
1417       ALOGE(
1418           "%s: Backward compatible devices must support scaler crop "
1419           "configuration!",
1420           __FUNCTION__);
1421       return BAD_VALUE;
1422     }
1423     if (available_results_.find(ANDROID_SCALER_CROP_REGION) ==
1424         available_results_.end()) {
1425       ALOGE("%s: Scaler crop must reported on backward compatible devices!",
1426             __FUNCTION__);
1427       return BAD_VALUE;
1428     }
1429     if (available_results_.find(
1430             ANDROID_LOGICAL_MULTI_CAMERA_ACTIVE_PHYSICAL_SENSOR_CROP_REGION) !=
1431         available_results_.end()) {
1432       report_active_sensor_crop_ = true;
1433     }
1434     ret = static_metadata_->Get(ANDROID_SCALER_AVAILABLE_ROTATE_AND_CROP_MODES,
1435                                 &entry);
1436     if ((ret == OK) && (entry.count > 0)) {
1437       // Listing rotate and crop, so need to make sure it's consistently reported
1438       if (available_requests_.find(ANDROID_SCALER_ROTATE_AND_CROP) ==
1439           available_requests_.end()) {
1440         ALOGE(
1441             "%s: Rotate and crop must be listed in request keys if supported!",
1442             __FUNCTION__);
1443         return BAD_VALUE;
1444       }
1445       if (available_results_.find(ANDROID_SCALER_ROTATE_AND_CROP) ==
1446           available_results_.end()) {
1447         ALOGE("%s: Rotate and crop must be listed in result keys if supported!",
1448               __FUNCTION__);
1449         return BAD_VALUE;
1450       }
1451       if (available_characteristics_.find(
1452               ANDROID_SCALER_AVAILABLE_ROTATE_AND_CROP_MODES) ==
1453           available_characteristics_.end()) {
1454         ALOGE(
1455             "%s: Rotate and crop must be listed in characteristics keys if "
1456             "supported!",
1457             __FUNCTION__);
1458         return BAD_VALUE;
1459       }
1460       report_rotate_and_crop_ = true;
1461       for (size_t i = 0; i < entry.count; i++) {
1462         if (entry.data.u8[i] == ANDROID_SCALER_ROTATE_AND_CROP_AUTO) {
1463           rotate_and_crop_ = ANDROID_SCALER_ROTATE_AND_CROP_AUTO;
1464         }
1465         available_rotate_crop_modes_.insert(entry.data.u8[i]);
1466       }
1467     }
1468 
1469     for (size_t idx = 0; idx < kTemplateCount; idx++) {
1470       if (default_requests_[idx].get() == nullptr) {
1471         continue;
1472       }
1473 
1474       default_requests_[idx]->Set(ANDROID_SCALER_CROP_REGION,
1475                                   scaler_crop_region_default_,
1476                                   ARRAY_SIZE(scaler_crop_region_default_));
1477       if (report_rotate_and_crop_) {
1478         default_requests_[idx]->Set(ANDROID_SCALER_ROTATE_AND_CROP,
1479                                     &rotate_and_crop_, 1);
1480       }
1481     }
1482   }
1483 
1484   return InitializeControlDefaults();
1485 }
1486 
InitializeShadingDefaults()1487 status_t EmulatedCameraDeviceInfo::InitializeShadingDefaults() {
1488   camera_metadata_ro_entry_t entry;
1489   auto ret = static_metadata_->Get(ANDROID_SHADING_AVAILABLE_MODES, &entry);
1490   if (ret == OK) {
1491     available_shading_modes_.insert(entry.data.u8, entry.data.u8 + entry.count);
1492   } else {
1493     ALOGE("%s: No available lens shading modes!", __FUNCTION__);
1494     return BAD_VALUE;
1495   }
1496 
1497   if (supports_manual_post_processing_ &&
1498       (available_shading_modes_.size() < 2)) {
1499     ALOGE(
1500         "%s: Devices capable of manual post-processing need to support at "
1501         "least "
1502         "two"
1503         " lens shading modes!",
1504         __FUNCTION__);
1505     return BAD_VALUE;
1506   }
1507 
1508   bool is_fast_mode_supported =
1509       available_shading_modes_.find(ANDROID_SHADING_MODE_FAST) !=
1510       available_shading_modes_.end();
1511   bool is_hq_mode_supported =
1512       available_shading_modes_.find(ANDROID_SHADING_MODE_HIGH_QUALITY) !=
1513       available_shading_modes_.end();
1514   uint8_t shading_mode = *available_shading_modes_.begin();
1515   for (size_t idx = 0; idx < kTemplateCount; idx++) {
1516     if (default_requests_[idx].get() == nullptr) {
1517       continue;
1518     }
1519 
1520     switch (static_cast<RequestTemplate>(idx)) {
1521       case RequestTemplate::kVideoRecord:  // Pass-through
1522       case RequestTemplate::kPreview:
1523         if (is_fast_mode_supported) {
1524           shading_mode = ANDROID_SHADING_MODE_FAST;
1525         }
1526         break;
1527       case RequestTemplate::kVideoSnapshot:  // Pass-through
1528       case RequestTemplate::kStillCapture:
1529         if (is_hq_mode_supported) {
1530           shading_mode = ANDROID_SHADING_MODE_HIGH_QUALITY;
1531         }
1532         break;
1533       default:
1534         // Noop
1535         break;
1536     }
1537 
1538     default_requests_[idx]->Set(ANDROID_SHADING_MODE, &shading_mode, 1);
1539   }
1540 
1541   return InitializeNoiseReductionDefaults();
1542 }
1543 
InitializeNoiseReductionDefaults()1544 status_t EmulatedCameraDeviceInfo::InitializeNoiseReductionDefaults() {
1545   camera_metadata_ro_entry_t entry;
1546   auto ret = static_metadata_->Get(
1547       ANDROID_NOISE_REDUCTION_AVAILABLE_NOISE_REDUCTION_MODES, &entry);
1548   if (ret == OK) {
1549     available_noise_reduction_modes_.insert(entry.data.u8,
1550                                             entry.data.u8 + entry.count);
1551   } else {
1552     ALOGE("%s: No available noise reduction modes!", __FUNCTION__);
1553     return BAD_VALUE;
1554   }
1555 
1556   if ((is_level_full_or_higher_) &&
1557       (available_noise_reduction_modes_.size() < 2)) {
1558     ALOGE(
1559         "%s: Full and above device must support at least two noise reduction "
1560         "modes!",
1561         __FUNCTION__);
1562     return BAD_VALUE;
1563   }
1564 
1565   bool is_fast_mode_supported =
1566       available_noise_reduction_modes_.find(ANDROID_NOISE_REDUCTION_MODE_FAST) !=
1567       available_noise_reduction_modes_.end();
1568   bool is_hq_mode_supported = available_noise_reduction_modes_.find(
1569                                   ANDROID_NOISE_REDUCTION_MODE_HIGH_QUALITY) !=
1570                               available_noise_reduction_modes_.end();
1571   bool is_zsl_mode_supported =
1572       available_noise_reduction_modes_.find(
1573           ANDROID_NOISE_REDUCTION_MODE_ZERO_SHUTTER_LAG) !=
1574       available_noise_reduction_modes_.end();
1575   uint8_t noise_reduction_mode = *available_noise_reduction_modes_.begin();
1576   for (size_t idx = 0; idx < kTemplateCount; idx++) {
1577     if (default_requests_[idx].get() == nullptr) {
1578       continue;
1579     }
1580 
1581     switch (static_cast<RequestTemplate>(idx)) {
1582       case RequestTemplate::kVideoRecord:    // Pass-through
1583       case RequestTemplate::kVideoSnapshot:  // Pass-through
1584       case RequestTemplate::kPreview:
1585         if (is_fast_mode_supported) {
1586           noise_reduction_mode = ANDROID_NOISE_REDUCTION_MODE_FAST;
1587         }
1588         break;
1589       case RequestTemplate::kStillCapture:
1590         if (is_hq_mode_supported) {
1591           noise_reduction_mode = ANDROID_NOISE_REDUCTION_MODE_HIGH_QUALITY;
1592         }
1593         break;
1594       case RequestTemplate::kZeroShutterLag:
1595         if (is_zsl_mode_supported) {
1596           noise_reduction_mode = ANDROID_NOISE_REDUCTION_MODE_ZERO_SHUTTER_LAG;
1597         }
1598         break;
1599       default:
1600         // Noop
1601         break;
1602     }
1603 
1604     default_requests_[idx]->Set(ANDROID_NOISE_REDUCTION_MODE,
1605                                 &noise_reduction_mode, 1);
1606   }
1607 
1608   return InitializeColorCorrectionDefaults();
1609 }
1610 
InitializeHotPixelDefaults()1611 status_t EmulatedCameraDeviceInfo::InitializeHotPixelDefaults() {
1612   camera_metadata_ro_entry_t entry;
1613   auto ret = static_metadata_->Get(ANDROID_HOT_PIXEL_AVAILABLE_HOT_PIXEL_MODES,
1614                                    &entry);
1615   if (ret == OK) {
1616     available_hot_pixel_modes_.insert(entry.data.u8,
1617                                       entry.data.u8 + entry.count);
1618   } else {
1619     ALOGE("%s: No available hotpixel modes!", __FUNCTION__);
1620     return BAD_VALUE;
1621   }
1622 
1623   if ((is_level_full_or_higher_) && (available_hot_pixel_modes_.size() < 2)) {
1624     ALOGE(
1625         "%s: Full and higher level cameras must support at least fast and hq "
1626         "hotpixel modes",
1627         __FUNCTION__);
1628     return BAD_VALUE;
1629   }
1630 
1631   bool fast_mode_supported =
1632       available_hot_pixel_modes_.find(ANDROID_HOT_PIXEL_MODE_FAST) !=
1633       available_hot_pixel_modes_.end();
1634   bool hq_mode_supported =
1635       available_hot_pixel_modes_.find(ANDROID_HOT_PIXEL_MODE_HIGH_QUALITY) !=
1636       available_hot_pixel_modes_.end();
1637   uint8_t hotpixel_mode = *available_hot_pixel_modes_.begin();
1638   for (size_t idx = 0; idx < kTemplateCount; idx++) {
1639     if (default_requests_[idx].get() == nullptr) {
1640       continue;
1641     }
1642 
1643     switch (static_cast<RequestTemplate>(idx)) {
1644       case RequestTemplate::kVideoRecord:  // Pass-through
1645       case RequestTemplate::kPreview:
1646         if (fast_mode_supported) {
1647           hotpixel_mode = ANDROID_HOT_PIXEL_MODE_FAST;
1648         }
1649         break;
1650       case RequestTemplate::kVideoSnapshot:  // Pass-through
1651       case RequestTemplate::kStillCapture:
1652         if (hq_mode_supported) {
1653           hotpixel_mode = ANDROID_HOT_PIXEL_MODE_HIGH_QUALITY;
1654         }
1655         break;
1656       default:
1657         // Noop
1658         break;
1659     }
1660 
1661     default_requests_[idx]->Set(ANDROID_HOT_PIXEL_MODE, &hotpixel_mode, 1);
1662   }
1663 
1664   return InitializeTonemapDefaults();
1665 }
1666 
InitializeFlashDefaults()1667 status_t EmulatedCameraDeviceInfo::InitializeFlashDefaults() {
1668   camera_metadata_ro_entry_t entry;
1669   auto ret = static_metadata_->Get(ANDROID_FLASH_INFO_AVAILABLE, &entry);
1670   if ((ret == OK) && (entry.count == 1)) {
1671     is_flash_supported_ = entry.data.u8[0];
1672   } else {
1673     ALOGE("%s: No available flash info!", __FUNCTION__);
1674     return BAD_VALUE;
1675   }
1676 
1677   if (is_flash_supported_) {
1678     flash_state_ = ANDROID_FLASH_STATE_READY;
1679   } else {
1680     flash_state_ = ANDROID_FLASH_STATE_UNAVAILABLE;
1681   }
1682 
1683   uint8_t flash_mode = ANDROID_FLASH_MODE_OFF;
1684   for (size_t idx = 0; idx < kTemplateCount; idx++) {
1685     if (default_requests_[idx].get() == nullptr) {
1686       continue;
1687     }
1688 
1689     default_requests_[idx]->Set(ANDROID_FLASH_MODE, &flash_mode, 1);
1690   }
1691 
1692   return InitializeScalerDefaults();
1693 }
1694 
InitializeLensDefaults()1695 status_t EmulatedCameraDeviceInfo::InitializeLensDefaults() {
1696   camera_metadata_ro_entry_t entry;
1697   auto ret =
1698       static_metadata_->Get(ANDROID_LENS_INFO_MINIMUM_FOCUS_DISTANCE, &entry);
1699   if ((ret == OK) && (entry.count == 1)) {
1700     minimum_focus_distance_ = entry.data.f[0];
1701   } else {
1702     ALOGW("%s: No available minimum focus distance assuming fixed focus!",
1703           __FUNCTION__);
1704     minimum_focus_distance_ = .0f;
1705   }
1706 
1707   ret = static_metadata_->Get(ANDROID_LENS_INFO_AVAILABLE_APERTURES, &entry);
1708   if ((ret == OK) && (entry.count > 0)) {
1709     // TODO: add support for multiple apertures
1710     aperture_ = entry.data.f[0];
1711   } else {
1712     ALOGE("%s: No available aperture!", __FUNCTION__);
1713     return BAD_VALUE;
1714   }
1715 
1716   ret = static_metadata_->Get(ANDROID_LENS_INFO_AVAILABLE_FOCAL_LENGTHS, &entry);
1717   if ((ret == OK) && (entry.count > 0)) {
1718     focal_length_ = entry.data.f[0];
1719   } else {
1720     ALOGE("%s: No available focal length!", __FUNCTION__);
1721     return BAD_VALUE;
1722   }
1723 
1724   ret = static_metadata_->Get(ANDROID_LENS_INFO_SHADING_MAP_SIZE, &entry);
1725   if ((ret == OK) && (entry.count == 2)) {
1726     shading_map_size_[0] = entry.data.i32[0];
1727     shading_map_size_[1] = entry.data.i32[1];
1728   } else if (is_raw_capable_) {
1729     ALOGE("%s: No available shading map size!", __FUNCTION__);
1730     return BAD_VALUE;
1731   }
1732 
1733   ret = static_metadata_->Get(ANDROID_LENS_INFO_AVAILABLE_FILTER_DENSITIES,
1734                               &entry);
1735   if ((ret == OK) && (entry.count > 0)) {
1736     // TODO: add support for multiple filter densities
1737     filter_density_ = entry.data.f[0];
1738   } else {
1739     ALOGE("%s: No available filter density!", __FUNCTION__);
1740     return BAD_VALUE;
1741   }
1742 
1743   ret = static_metadata_->Get(ANDROID_LENS_INFO_AVAILABLE_OPTICAL_STABILIZATION,
1744                               &entry);
1745   if ((ret == OK) && (entry.count > 0)) {
1746     // TODO: add support for multiple OIS modes
1747     available_ois_modes_.insert(entry.data.u8, entry.data.u8 + entry.count);
1748     if (available_ois_modes_.find(ANDROID_LENS_OPTICAL_STABILIZATION_MODE_OFF) ==
1749         available_ois_modes_.end()) {
1750       ALOGE("%s: OIS off mode not supported!", __FUNCTION__);
1751       return BAD_VALUE;
1752     }
1753   } else {
1754     ALOGE("%s: No available OIS modes!", __FUNCTION__);
1755     return BAD_VALUE;
1756   }
1757 
1758   ret = static_metadata_->Get(ANDROID_LENS_POSE_ROTATION, &entry);
1759   if ((ret == OK) && (entry.count == ARRAY_SIZE(pose_rotation_))) {
1760     memcpy(pose_rotation_, entry.data.f, sizeof(pose_rotation_));
1761   }
1762   ret = static_metadata_->Get(ANDROID_LENS_POSE_TRANSLATION, &entry);
1763   if ((ret == OK) && (entry.count == ARRAY_SIZE(pose_translation_))) {
1764     memcpy(pose_translation_, entry.data.f, sizeof(pose_translation_));
1765   }
1766   ret = static_metadata_->Get(ANDROID_LENS_INTRINSIC_CALIBRATION, &entry);
1767   if ((ret == OK) && (entry.count == ARRAY_SIZE(intrinsic_calibration_))) {
1768     memcpy(intrinsic_calibration_, entry.data.f, sizeof(intrinsic_calibration_));
1769   }
1770 
1771   ret = static_metadata_->Get(ANDROID_LENS_DISTORTION, &entry);
1772   if ((ret == OK) && (entry.count == ARRAY_SIZE(distortion_))) {
1773     memcpy(distortion_, entry.data.f, sizeof(distortion_));
1774   }
1775 
1776   report_focus_distance_ =
1777       available_results_.find(ANDROID_LENS_FOCUS_DISTANCE) !=
1778       available_results_.end();
1779   report_focus_range_ = available_results_.find(ANDROID_LENS_FOCUS_RANGE) !=
1780                         available_results_.end();
1781   report_filter_density_ =
1782       available_results_.find(ANDROID_LENS_FILTER_DENSITY) !=
1783       available_results_.end();
1784   report_ois_mode_ =
1785       available_results_.find(ANDROID_LENS_OPTICAL_STABILIZATION_MODE) !=
1786       available_results_.end();
1787   report_pose_rotation_ = available_results_.find(ANDROID_LENS_POSE_ROTATION) !=
1788                           available_results_.end();
1789   report_pose_translation_ =
1790       available_results_.find(ANDROID_LENS_POSE_TRANSLATION) !=
1791       available_results_.end();
1792   report_intrinsic_calibration_ =
1793       available_results_.find(ANDROID_LENS_INTRINSIC_CALIBRATION) !=
1794       available_results_.end();
1795   report_distortion_ = available_results_.find(ANDROID_LENS_DISTORTION) !=
1796                        available_results_.end();
1797 
1798   focus_distance_ = minimum_focus_distance_;
1799   for (size_t idx = 0; idx < kTemplateCount; idx++) {
1800     if (default_requests_[idx].get() == nullptr) {
1801       continue;
1802     }
1803 
1804     default_requests_[idx]->Set(ANDROID_LENS_APERTURE, &aperture_, 1);
1805     default_requests_[idx]->Set(ANDROID_LENS_FOCAL_LENGTH, &focal_length_, 1);
1806     default_requests_[idx]->Set(ANDROID_LENS_FOCUS_DISTANCE, &focus_distance_,
1807                                 1);
1808     default_requests_[idx]->Set(ANDROID_LENS_OPTICAL_STABILIZATION_MODE,
1809                                 &ois_mode_, 1);
1810   }
1811 
1812   return InitializeFlashDefaults();
1813 }
1814 
InitializeInfoDefaults()1815 status_t EmulatedCameraDeviceInfo::InitializeInfoDefaults() {
1816   camera_metadata_ro_entry_t entry;
1817   auto ret =
1818       static_metadata_->Get(ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL, &entry);
1819   if ((ret == OK) && (entry.count == 1)) {
1820     if (kSupportedHWLevels.find(entry.data.u8[0]) ==
1821         kSupportedCapabilites.end()) {
1822       ALOGE("%s: HW Level: %u not supported", __FUNCTION__, entry.data.u8[0]);
1823       return BAD_VALUE;
1824     }
1825   } else {
1826     ALOGE("%s: No available hardware level!", __FUNCTION__);
1827     return BAD_VALUE;
1828   }
1829 
1830   supported_hw_level_ = entry.data.u8[0];
1831   is_level_full_or_higher_ =
1832       (supported_hw_level_ == ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_FULL) ||
1833       (supported_hw_level_ == ANDROID_INFO_SUPPORTED_HARDWARE_LEVEL_3);
1834 
1835   return InitializeReprocessDefaults();
1836 }
1837 
InitializeReprocessDefaults()1838 status_t EmulatedCameraDeviceInfo::InitializeReprocessDefaults() {
1839   if (supports_private_reprocessing_ || supports_yuv_reprocessing_ ||
1840       supports_remosaic_reprocessing_) {
1841     StreamConfigurationMap config_map(*static_metadata_);
1842     if (!config_map.SupportsReprocessing()) {
1843       ALOGE(
1844           "%s: Reprocess capability present but InputOutput format map is "
1845           "absent!",
1846           __FUNCTION__);
1847       return BAD_VALUE;
1848     }
1849 
1850     auto input_formats = config_map.GetInputFormats();
1851     for (const auto& input_format : input_formats) {
1852       auto output_formats =
1853           config_map.GetValidOutputFormatsForInput(input_format);
1854       for (const auto& output_format : output_formats) {
1855         if (!EmulatedSensor::IsReprocessPathSupported(
1856                 EmulatedSensor::OverrideFormat(
1857                     input_format,
1858                     ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD),
1859                 EmulatedSensor::OverrideFormat(
1860                     output_format,
1861                     ANDROID_REQUEST_AVAILABLE_DYNAMIC_RANGE_PROFILES_MAP_STANDARD))) {
1862           ALOGE(
1863               "%s: Input format: 0x%x to output format: 0x%x reprocess is"
1864               " currently not supported!",
1865               __FUNCTION__, input_format, output_format);
1866           return BAD_VALUE;
1867         }
1868       }
1869     }
1870   }
1871 
1872   return InitializeLensDefaults();
1873 }
1874 
InitializeRequestDefaults()1875 status_t EmulatedCameraDeviceInfo::InitializeRequestDefaults() {
1876   camera_metadata_ro_entry_t entry;
1877   auto ret =
1878       static_metadata_->Get(ANDROID_REQUEST_AVAILABLE_CAPABILITIES, &entry);
1879   if ((ret == OK) && (entry.count > 0)) {
1880     for (size_t i = 0; i < entry.count; i++) {
1881       if (kSupportedCapabilites.find(entry.data.u8[i]) ==
1882           kSupportedCapabilites.end()) {
1883         ALOGE("%s: Capability: %u not supported", __FUNCTION__,
1884               entry.data.u8[i]);
1885         return BAD_VALUE;
1886       }
1887     }
1888   } else {
1889     ALOGE("%s: No available capabilities!", __FUNCTION__);
1890     return BAD_VALUE;
1891   }
1892   available_capabilities_.insert(entry.data.u8, entry.data.u8 + entry.count);
1893 
1894   ret = static_metadata_->Get(ANDROID_REQUEST_PIPELINE_MAX_DEPTH, &entry);
1895   if ((ret == OK) && (entry.count == 1)) {
1896     if (entry.data.u8[0] == 0) {
1897       ALOGE("%s: Maximum request pipeline depth must have a non zero value!",
1898             __FUNCTION__);
1899       return BAD_VALUE;
1900     }
1901   } else {
1902     ALOGE("%s: Maximum request pipeline depth absent!", __FUNCTION__);
1903     return BAD_VALUE;
1904   }
1905   max_pipeline_depth_ = entry.data.u8[0];
1906 
1907   ret = static_metadata_->Get(ANDROID_REQUEST_PARTIAL_RESULT_COUNT, &entry);
1908   if ((ret == OK) && (entry.count == 1)) {
1909     if (entry.data.i32[0] > 2) {
1910       ALOGW("%s: Partial result count greater than 2 not supported!",
1911             __FUNCTION__);
1912     }
1913     partial_result_count_ = entry.data.i32[0];
1914   }
1915 
1916   ret = static_metadata_->Get(ANDROID_REQUEST_AVAILABLE_CHARACTERISTICS_KEYS,
1917                               &entry);
1918   if ((ret != OK) || (entry.count == 0)) {
1919     ALOGE("%s: No available characteristic keys!", __FUNCTION__);
1920     return BAD_VALUE;
1921   }
1922   available_characteristics_.insert(entry.data.i32,
1923                                     entry.data.i32 + entry.count);
1924 
1925   ret = static_metadata_->Get(ANDROID_REQUEST_AVAILABLE_RESULT_KEYS, &entry);
1926   if ((ret != OK) || (entry.count == 0)) {
1927     ALOGE("%s: No available result keys!", __FUNCTION__);
1928     return BAD_VALUE;
1929   }
1930   available_results_.insert(entry.data.i32, entry.data.i32 + entry.count);
1931 
1932   ret = static_metadata_->Get(ANDROID_REQUEST_AVAILABLE_REQUEST_KEYS, &entry);
1933   if ((ret != OK) || (entry.count == 0)) {
1934     ALOGE("%s: No available request keys!", __FUNCTION__);
1935     return BAD_VALUE;
1936   }
1937   available_requests_.insert(entry.data.i32, entry.data.i32 + entry.count);
1938 
1939   supports_manual_sensor_ =
1940       SupportsCapability(ANDROID_REQUEST_AVAILABLE_CAPABILITIES_MANUAL_SENSOR);
1941   supports_manual_post_processing_ = SupportsCapability(
1942       ANDROID_REQUEST_AVAILABLE_CAPABILITIES_MANUAL_POST_PROCESSING);
1943   supports_private_reprocessing_ = SupportsCapability(
1944       ANDROID_REQUEST_AVAILABLE_CAPABILITIES_PRIVATE_REPROCESSING);
1945   supports_yuv_reprocessing_ = SupportsCapability(
1946       ANDROID_REQUEST_AVAILABLE_CAPABILITIES_YUV_REPROCESSING);
1947   supports_remosaic_reprocessing_ = SupportsCapability(
1948       ANDROID_REQUEST_AVAILABLE_CAPABILITIES_REMOSAIC_REPROCESSING);
1949   is_backward_compatible_ = SupportsCapability(
1950       ANDROID_REQUEST_AVAILABLE_CAPABILITIES_BACKWARD_COMPATIBLE);
1951   is_raw_capable_ =
1952       SupportsCapability(ANDROID_REQUEST_AVAILABLE_CAPABILITIES_RAW);
1953   supports_stream_use_case_ =
1954       SupportsCapability(ANDROID_REQUEST_AVAILABLE_CAPABILITIES_STREAM_USE_CASE);
1955 
1956   if (supports_manual_sensor_) {
1957     auto templateIdx = static_cast<size_t>(RequestTemplate::kManual);
1958     default_requests_[templateIdx] = HalCameraMetadata::Create(1, 10);
1959   }
1960 
1961   if (supports_stream_use_case_) {
1962     ret = static_metadata_->Get(ANDROID_SCALER_AVAILABLE_STREAM_USE_CASES,
1963                                 &entry);
1964     if (ret != OK) {
1965       ALOGE("%s: No available stream use cases!", __FUNCTION__);
1966       return BAD_VALUE;
1967     }
1968     for (int64_t useCase : kSupportedUseCases) {
1969       if (std::find(entry.data.i64, entry.data.i64 + entry.count, useCase) ==
1970           entry.data.i64 + entry.count) {
1971         ALOGE("%s: Mandatory stream use case %" PRId64 " not found!",
1972               __FUNCTION__, useCase);
1973         return BAD_VALUE;
1974       }
1975     }
1976   }
1977 
1978   for (size_t templateIdx = 0; templateIdx < kTemplateCount; templateIdx++) {
1979     switch (static_cast<RequestTemplate>(templateIdx)) {
1980       case RequestTemplate::kPreview:
1981       case RequestTemplate::kStillCapture:
1982       case RequestTemplate::kVideoRecord:
1983       case RequestTemplate::kVideoSnapshot:
1984         default_requests_[templateIdx] = HalCameraMetadata::Create(1, 10);
1985         break;
1986       default:
1987         // Noop
1988         break;
1989     }
1990   }
1991 
1992   if (supports_yuv_reprocessing_ || supports_private_reprocessing_) {
1993     auto templateIdx = static_cast<size_t>(RequestTemplate::kZeroShutterLag);
1994     default_requests_[templateIdx] = HalCameraMetadata::Create(1, 10);
1995   }
1996 
1997   return InitializeInfoDefaults();
1998 }
1999 
SupportsCapability(uint8_t cap)2000 bool EmulatedCameraDeviceInfo::SupportsCapability(uint8_t cap) {
2001   return available_capabilities_.find(cap) != available_capabilities_.end();
2002 }
2003 
2004 }  // namespace android
2005