1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "EmulatedRequestState"
18 #define ATRACE_TAG ATRACE_TAG_CAMERA
19 
20 #include "EmulatedRequestState.h"
21 
22 #include <inttypes.h>
23 #include <log/log.h>
24 #include <utils/HWLUtils.h>
25 
26 #include "EmulatedRequestProcessor.h"
27 
28 namespace android {
29 
30 using google_camera_hal::HwlPipelineResult;
31 
Update3AMeteringRegion(uint32_t tag,const HalCameraMetadata & settings,int32_t * region)32 status_t EmulatedRequestState::Update3AMeteringRegion(
33     uint32_t tag, const HalCameraMetadata& settings, int32_t* region /*out*/) {
34   if ((region == nullptr) || ((tag != ANDROID_CONTROL_AE_REGIONS) &&
35                               (tag != ANDROID_CONTROL_AF_REGIONS) &&
36                               (tag != ANDROID_CONTROL_AWB_REGIONS))) {
37     return BAD_VALUE;
38   }
39 
40   camera_metadata_ro_entry_t entry;
41   auto ret = settings.Get(ANDROID_SCALER_CROP_REGION, &entry);
42   if ((ret == OK) && (entry.count > 0)) {
43     int32_t crop_region[4];
44     crop_region[0] = entry.data.i32[0];
45     crop_region[1] = entry.data.i32[1];
46     crop_region[2] = entry.data.i32[2] + crop_region[0];
47     crop_region[3] = entry.data.i32[3] + crop_region[1];
48     ret = settings.Get(tag, &entry);
49     if ((ret == OK) && (entry.count > 0)) {
50       const int32_t* a_region = entry.data.i32;
51       // calculate the intersection of 3A and CROP regions
52       if (a_region[0] < crop_region[2] && crop_region[0] < a_region[2] &&
53           a_region[1] < crop_region[3] && crop_region[1] < a_region[3]) {
54         region[0] = std::max(a_region[0], crop_region[0]);
55         region[1] = std::max(a_region[1], crop_region[1]);
56         region[2] = std::min(a_region[2], crop_region[2]);
57         region[3] = std::min(a_region[3], crop_region[3]);
58         region[4] = entry.data.i32[4];
59       }
60     }
61   }
62 
63   return OK;
64 }
65 
CompensateAE()66 status_t EmulatedRequestState::CompensateAE() {
67   auto& info = *device_info_;
68 
69   if (!info.exposure_compensation_supported_) {
70     info.sensor_exposure_time_ = current_exposure_time_;
71     return OK;
72   }
73 
74   camera_metadata_ro_entry_t entry;
75   auto ret =
76       request_settings_->Get(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION, &entry);
77   if ((ret == OK) && (entry.count == 1)) {
78     info.exposure_compensation_ = entry.data.i32[0];
79   } else {
80     ALOGW("%s: AE compensation absent from request,  re-using previous value!",
81           __FUNCTION__);
82   }
83 
84   float ae_compensation = ::powf(
85       2, info.exposure_compensation_ *
86              ((static_cast<float>(info.exposure_compensation_step_.numerator) /
87                info.exposure_compensation_step_.denominator)));
88 
89   info.sensor_exposure_time_ = GetClosestValue(
90       static_cast<nsecs_t>(ae_compensation * current_exposure_time_),
91       info.sensor_exposure_time_range_.first,
92       info.sensor_exposure_time_range_.second);
93 
94   return OK;
95 }
96 
DoFakeAE()97 status_t EmulatedRequestState::DoFakeAE() {
98   auto& info = *device_info_;
99 
100   camera_metadata_ro_entry_t entry;
101   auto ret = request_settings_->Get(ANDROID_CONTROL_AE_LOCK, &entry);
102   if ((ret == OK) && (entry.count == 1)) {
103     info.ae_lock_ = entry.data.u8[0];
104   } else {
105     info.ae_lock_ = ANDROID_CONTROL_AE_LOCK_OFF;
106   }
107 
108   if (info.ae_lock_ == ANDROID_CONTROL_AE_LOCK_ON) {
109     info.ae_state_ = ANDROID_CONTROL_AE_STATE_LOCKED;
110     return OK;
111   }
112 
113   EmulatedCameraDeviceInfo::FPSRange fps_range;
114   ret = request_settings_->Get(ANDROID_CONTROL_AE_TARGET_FPS_RANGE, &entry);
115   if ((ret == OK) && (entry.count == 2)) {
116     for (const auto& it : info.available_fps_ranges_) {
117       if ((it.min_fps == entry.data.i32[0]) &&
118           (it.max_fps == entry.data.i32[1])) {
119         fps_range = {entry.data.i32[0], entry.data.i32[1]};
120         break;
121       }
122     }
123     if (fps_range.max_fps == 0) {
124       ALOGE("%s: Unsupported framerate range [%d, %d]", __FUNCTION__,
125             entry.data.i32[0], entry.data.i32[1]);
126       return BAD_VALUE;
127     }
128   } else {
129     fps_range = *info.available_fps_ranges_.begin();
130   }
131 
132   ret = request_settings_->Get(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER, &entry);
133   if ((ret == OK) && (entry.count == 1)) {
134     info.ae_trigger_ = entry.data.u8[0];
135   } else {
136     info.ae_trigger_ = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
137   }
138 
139   nsecs_t min_frame_duration =
140       GetClosestValue(ms2ns(1000 / fps_range.max_fps),
141                       EmulatedSensor::kSupportedFrameDurationRange[0],
142                       info.sensor_max_frame_duration_);
143   nsecs_t max_frame_duration =
144       GetClosestValue(ms2ns(1000 / fps_range.min_fps),
145                       EmulatedSensor::kSupportedFrameDurationRange[0],
146                       info.sensor_max_frame_duration_);
147   info.sensor_frame_duration_ = (max_frame_duration + min_frame_duration) / 2;
148 
149   // Face priority mode usually changes the AE algorithm behavior by
150   // using the regions of interest associated with detected faces.
151   // Try to emulate this behavior by slightly increasing the target exposure
152   // time compared to normal operation.
153   if (info.exposure_compensation_supported_) {
154     float max_ae_compensation = ::powf(
155         2, info.exposure_compensation_range_[1] *
156                ((static_cast<float>(info.exposure_compensation_step_.numerator) /
157                  info.exposure_compensation_step_.denominator)));
158     ae_target_exposure_time_ = GetClosestValue(
159         static_cast<nsecs_t>(info.sensor_frame_duration_ / max_ae_compensation),
160         info.sensor_exposure_time_range_.first,
161         info.sensor_exposure_time_range_.second);
162   } else if (info.scene_mode_ == ANDROID_CONTROL_SCENE_MODE_FACE_PRIORITY) {
163     ae_target_exposure_time_ = GetClosestValue(
164         info.sensor_frame_duration_ / 4, info.sensor_exposure_time_range_.first,
165         info.sensor_exposure_time_range_.second);
166   } else {
167     ae_target_exposure_time_ = GetClosestValue(
168         info.sensor_frame_duration_ / 5, info.sensor_exposure_time_range_.first,
169         info.sensor_exposure_time_range_.second);
170   }
171 
172   if ((info.ae_trigger_ == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_START) ||
173       (info.ae_state_ == ANDROID_CONTROL_AE_STATE_PRECAPTURE)) {
174     if (info.ae_state_ != ANDROID_CONTROL_AE_STATE_PRECAPTURE) {
175       ae_frame_counter_ = 0;
176     }
177 
178     if (info.ae_trigger_ == ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL) {
179       // Done with precapture
180       ae_frame_counter_ = 0;
181       info.ae_state_ = ANDROID_CONTROL_AE_STATE_CONVERGED;
182       info.ae_trigger_ = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_CANCEL;
183     } else if ((ae_frame_counter_ > kAEPrecaptureMinFrames) &&
184                (abs(ae_target_exposure_time_ - current_exposure_time_) <
185                 ae_target_exposure_time_ / kAETargetThreshold)) {
186       // Done with precapture
187       ae_frame_counter_ = 0;
188       info.ae_state_ = ANDROID_CONTROL_AE_STATE_CONVERGED;
189       info.ae_trigger_ = ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER_IDLE;
190     } else {
191       // Converge some more
192       current_exposure_time_ +=
193           (ae_target_exposure_time_ - current_exposure_time_) *
194           kExposureTrackRate;
195       ae_frame_counter_++;
196       info.ae_state_ = ANDROID_CONTROL_AE_STATE_PRECAPTURE;
197     }
198   } else {
199     switch (info.ae_state_) {
200       case ANDROID_CONTROL_AE_STATE_INACTIVE:
201         info.ae_state_ = ANDROID_CONTROL_AE_STATE_SEARCHING;
202         break;
203       case ANDROID_CONTROL_AE_STATE_CONVERGED:
204         ae_frame_counter_++;
205         if (ae_frame_counter_ > kStableAeMaxFrames) {
206           float exposure_step = ((double)rand_r(&rand_seed_) / RAND_MAX) *
207                                     (kExposureWanderMax - kExposureWanderMin) +
208                                 kExposureWanderMin;
209           ae_target_exposure_time_ =
210               GetClosestValue(static_cast<nsecs_t>(ae_target_exposure_time_ *
211                                                    std::pow(2, exposure_step)),
212                               info.sensor_exposure_time_range_.first,
213                               info.sensor_exposure_time_range_.second);
214           info.ae_state_ = ANDROID_CONTROL_AE_STATE_SEARCHING;
215         }
216         break;
217       case ANDROID_CONTROL_AE_STATE_SEARCHING:
218         current_exposure_time_ +=
219             (ae_target_exposure_time_ - current_exposure_time_) *
220             kExposureTrackRate;
221         if (abs(ae_target_exposure_time_ - current_exposure_time_) <
222             ae_target_exposure_time_ / kAETargetThreshold) {
223           // Close enough
224           info.ae_state_ = ANDROID_CONTROL_AE_STATE_CONVERGED;
225           ae_frame_counter_ = 0;
226         }
227         break;
228       case ANDROID_CONTROL_AE_STATE_LOCKED:
229         info.ae_state_ = ANDROID_CONTROL_AE_STATE_CONVERGED;
230         ae_frame_counter_ = 0;
231         break;
232       default:
233         ALOGE("%s: Unexpected AE state %d!", __FUNCTION__, info.ae_state_);
234         return INVALID_OPERATION;
235     }
236   }
237 
238   return OK;
239 }
240 
ProcessAWB()241 status_t EmulatedRequestState::ProcessAWB() {
242   auto& info = *device_info_;
243 
244   if (info.max_awb_regions_ > 0) {
245     auto ret =
246         Update3AMeteringRegion(ANDROID_CONTROL_AWB_REGIONS, *request_settings_,
247                                info.awb_metering_region_);
248     if (ret != OK) {
249       return ret;
250     }
251   }
252   if (((info.awb_mode_ == ANDROID_CONTROL_AWB_MODE_OFF) ||
253        (info.control_mode_ == ANDROID_CONTROL_MODE_OFF)) &&
254       info.supports_manual_post_processing_) {
255     // TODO: Add actual manual support
256   } else if (info.is_backward_compatible_) {
257     camera_metadata_ro_entry_t entry;
258     auto ret = request_settings_->Get(ANDROID_CONTROL_AWB_LOCK, &entry);
259     if ((ret == OK) && (entry.count == 1)) {
260       info.awb_lock_ = entry.data.u8[0];
261     } else {
262       info.awb_lock_ = ANDROID_CONTROL_AWB_LOCK_OFF;
263     }
264 
265     if (info.awb_lock_ == ANDROID_CONTROL_AWB_LOCK_ON) {
266       info.awb_state_ = ANDROID_CONTROL_AWB_STATE_LOCKED;
267     } else {
268       info.awb_state_ = ANDROID_CONTROL_AWB_STATE_CONVERGED;
269     }
270   } else {
271     // No color output support no need for AWB
272   }
273 
274   return OK;
275 }
276 
ProcessAF()277 status_t EmulatedRequestState::ProcessAF() {
278   auto& info = *device_info_;
279   camera_metadata_ro_entry entry;
280 
281   if (info.max_af_regions_ > 0) {
282     auto ret =
283         Update3AMeteringRegion(ANDROID_CONTROL_AF_REGIONS, *request_settings_,
284                                info.af_metering_region_);
285     if (ret != OK) {
286       return ret;
287     }
288   }
289   if (info.af_mode_ == ANDROID_CONTROL_AF_MODE_OFF) {
290     camera_metadata_ro_entry_t entry;
291     auto ret = request_settings_->Get(ANDROID_LENS_FOCUS_DISTANCE, &entry);
292     if ((ret == OK) && (entry.count == 1)) {
293       if ((entry.data.f[0] >= 0.f) &&
294           (entry.data.f[0] <= info.minimum_focus_distance_)) {
295         info.focus_distance_ = entry.data.f[0];
296       } else {
297         ALOGE(
298             "%s: Unsupported focus distance, It should be within "
299             "[%5.2f, %5.2f]",
300             __FUNCTION__, 0.f, info.minimum_focus_distance_);
301       }
302     }
303 
304     info.af_state_ = ANDROID_CONTROL_AF_STATE_INACTIVE;
305     return OK;
306   }
307 
308   auto ret = request_settings_->Get(ANDROID_CONTROL_AF_TRIGGER, &entry);
309   if ((ret == OK) && (entry.count == 1)) {
310     info.af_trigger_ = entry.data.u8[0];
311   } else {
312     info.af_trigger_ = ANDROID_CONTROL_AF_TRIGGER_IDLE;
313   }
314 
315   /**
316    * Simulate AF triggers. Transition at most 1 state per frame.
317    * - Focusing always succeeds (goes into locked, or PASSIVE_SCAN).
318    */
319 
320   bool af_trigger_start = false;
321   switch (info.af_trigger_) {
322     case ANDROID_CONTROL_AF_TRIGGER_IDLE:
323       break;
324     case ANDROID_CONTROL_AF_TRIGGER_START:
325       af_trigger_start = true;
326       break;
327     case ANDROID_CONTROL_AF_TRIGGER_CANCEL:
328       // Cancel trigger always transitions into INACTIVE
329       info.af_state_ = ANDROID_CONTROL_AF_STATE_INACTIVE;
330 
331       // Stay in 'inactive' until at least next frame
332       return OK;
333     default:
334       ALOGE("%s: Unknown AF trigger value", __FUNCTION__);
335       return BAD_VALUE;
336   }
337 
338   // If we get down here, we're either in ANDROID_CONTROL_AF_MODE_AUTO,
339   // ANDROID_CONTROL_AF_MODE_MACRO, ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO,
340   // ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE and no other modes like
341   // ANDROID_CONTROL_AF_MODE_OFF or ANDROID_CONTROL_AF_MODE_EDOF
342   switch (info.af_state_) {
343     case ANDROID_CONTROL_AF_STATE_INACTIVE:
344       if (af_trigger_start) {
345         switch (info.af_mode_) {
346           case ANDROID_CONTROL_AF_MODE_AUTO:
347             // fall-through
348           case ANDROID_CONTROL_AF_MODE_MACRO:
349             info.af_state_ = ANDROID_CONTROL_AF_STATE_ACTIVE_SCAN;
350             break;
351           case ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO:
352             // fall-through
353           case ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE:
354             info.af_state_ = ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED;
355             break;
356         }
357       } else {
358         // At least one frame stays in INACTIVE
359         if (!af_mode_changed_) {
360           switch (info.af_mode_) {
361             case ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO:
362               // fall-through
363             case ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE:
364               info.af_state_ = ANDROID_CONTROL_AF_STATE_PASSIVE_SCAN;
365               break;
366           }
367         }
368       }
369       break;
370     case ANDROID_CONTROL_AF_STATE_PASSIVE_SCAN:
371       /**
372        * When the AF trigger is activated, the algorithm should finish
373        * its PASSIVE_SCAN if active, and then transition into AF_FOCUSED
374        * or AF_NOT_FOCUSED as appropriate
375        */
376       if (af_trigger_start) {
377         // Randomly transition to focused or not focused
378         if (rand_r(&rand_seed_) % 3) {
379           info.af_state_ = ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED;
380         } else {
381           info.af_state_ = ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED;
382         }
383       }
384       /**
385        * When the AF trigger is not involved, the AF algorithm should
386        * start in INACTIVE state, and then transition into PASSIVE_SCAN
387        * and PASSIVE_FOCUSED states
388        */
389       else {
390         // Randomly transition to passive focus
391         if (rand_r(&rand_seed_) % 3 == 0) {
392           info.af_state_ = ANDROID_CONTROL_AF_STATE_PASSIVE_FOCUSED;
393         }
394       }
395 
396       break;
397     case ANDROID_CONTROL_AF_STATE_PASSIVE_FOCUSED:
398       if (af_trigger_start) {
399         // Randomly transition to focused or not focused
400         if (rand_r(&rand_seed_) % 3) {
401           info.af_state_ = ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED;
402         } else {
403           info.af_state_ = ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED;
404         }
405       }
406       // TODO: initiate passive scan (PASSIVE_SCAN)
407       break;
408     case ANDROID_CONTROL_AF_STATE_ACTIVE_SCAN:
409       // Simulate AF sweep completing instantaneously
410 
411       // Randomly transition to focused or not focused
412       if (rand_r(&rand_seed_) % 3) {
413         info.af_state_ = ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED;
414       } else {
415         info.af_state_ = ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED;
416       }
417       break;
418     case ANDROID_CONTROL_AF_STATE_FOCUSED_LOCKED:
419       if (af_trigger_start) {
420         switch (info.af_mode_) {
421           case ANDROID_CONTROL_AF_MODE_AUTO:
422             // fall-through
423           case ANDROID_CONTROL_AF_MODE_MACRO:
424             info.af_state_ = ANDROID_CONTROL_AF_STATE_ACTIVE_SCAN;
425             break;
426           case ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO:
427             // fall-through
428           case ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE:
429             // continuous autofocus => trigger start has no effect
430             break;
431         }
432       }
433       break;
434     case ANDROID_CONTROL_AF_STATE_NOT_FOCUSED_LOCKED:
435       if (af_trigger_start) {
436         switch (info.af_mode_) {
437           case ANDROID_CONTROL_AF_MODE_AUTO:
438             // fall-through
439           case ANDROID_CONTROL_AF_MODE_MACRO:
440             info.af_state_ = ANDROID_CONTROL_AF_STATE_ACTIVE_SCAN;
441             break;
442           case ANDROID_CONTROL_AF_MODE_CONTINUOUS_VIDEO:
443             // fall-through
444           case ANDROID_CONTROL_AF_MODE_CONTINUOUS_PICTURE:
445             // continuous autofocus => trigger start has no effect
446             break;
447         }
448       }
449       break;
450     default:
451       ALOGE("%s: Bad af state %d", __FUNCTION__, info.af_state_);
452   }
453 
454   return OK;
455 }
456 
ProcessAE()457 status_t EmulatedRequestState::ProcessAE() {
458   auto& info = *device_info_;
459   if (info.max_ae_regions_ > 0) {
460     auto ret =
461         Update3AMeteringRegion(ANDROID_CONTROL_AE_REGIONS, *request_settings_,
462                                info.ae_metering_region_);
463     if (ret != OK) {
464       ALOGE("%s: Failed updating the 3A metering regions: %d, (%s)",
465             __FUNCTION__, ret, strerror(-ret));
466     }
467   }
468 
469   camera_metadata_ro_entry_t entry;
470   bool auto_ae_mode = false;
471   bool auto_ae_flash_mode = false;
472   switch (info.ae_mode_) {
473     case ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH:
474     case ANDROID_CONTROL_AE_MODE_ON_ALWAYS_FLASH:
475     case ANDROID_CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE:
476       auto_ae_flash_mode = true;
477       [[fallthrough]];
478     case ANDROID_CONTROL_AE_MODE_ON:
479       auto_ae_mode = true;
480   };
481   if (((info.ae_mode_ == ANDROID_CONTROL_AE_MODE_OFF) ||
482        (info.control_mode_ == ANDROID_CONTROL_MODE_OFF)) &&
483       info.supports_manual_sensor_) {
484     auto ret = request_settings_->Get(ANDROID_SENSOR_EXPOSURE_TIME, &entry);
485     if ((ret == OK) && (entry.count == 1)) {
486       if ((entry.data.i64[0] >= info.sensor_exposure_time_range_.first) &&
487           (entry.data.i64[0] <= info.sensor_exposure_time_range_.second)) {
488         info.sensor_exposure_time_ = entry.data.i64[0];
489       } else {
490         ALOGE("%s: Sensor exposure time %" PRId64
491               " not within supported range[%" PRId64 ", %" PRId64 "]",
492               __FUNCTION__, entry.data.i64[0],
493               info.sensor_exposure_time_range_.first,
494               info.sensor_exposure_time_range_.second);
495         // Use last valid value
496       }
497     }
498 
499     ret = request_settings_->Get(ANDROID_SENSOR_FRAME_DURATION, &entry);
500     if ((ret == OK) && (entry.count == 1)) {
501       if ((entry.data.i64[0] >=
502            EmulatedSensor::kSupportedFrameDurationRange[0]) &&
503           (entry.data.i64[0] <= info.sensor_max_frame_duration_)) {
504         info.sensor_frame_duration_ = entry.data.i64[0];
505       } else {
506         ALOGE("%s: Sensor frame duration %" PRId64
507               " not within supported range[%" PRId64 ", %" PRId64 "]",
508               __FUNCTION__, entry.data.i64[0],
509               EmulatedSensor::kSupportedFrameDurationRange[0],
510               info.sensor_max_frame_duration_);
511         // Use last valid value
512       }
513     }
514 
515     if (info.sensor_frame_duration_ < info.sensor_exposure_time_) {
516       info.sensor_frame_duration_ = info.sensor_exposure_time_;
517     }
518 
519     ret = request_settings_->Get(ANDROID_SENSOR_SENSITIVITY, &entry);
520     if ((ret == OK) && (entry.count == 1)) {
521       if ((entry.data.i32[0] >= info.sensor_sensitivity_range_.first) &&
522           (entry.data.i32[0] <= info.sensor_sensitivity_range_.second)) {
523         info.sensor_sensitivity_ = entry.data.i32[0];
524       } else {
525         ALOGE("%s: Sensor sensitivity %d not within supported range[%d, %d]",
526               __FUNCTION__, entry.data.i32[0],
527               info.sensor_sensitivity_range_.first,
528               info.sensor_sensitivity_range_.second);
529         // Use last valid value
530       }
531     }
532     info.ae_state_ = ANDROID_CONTROL_AE_STATE_INACTIVE;
533   } else if (info.is_backward_compatible_ && auto_ae_mode) {
534     auto ret = DoFakeAE();
535     if (ret != OK) {
536       ALOGE("%s: Failed fake AE: %d, (%s)", __FUNCTION__, ret, strerror(-ret));
537     }
538 
539     // Do AE compensation on the results of the AE
540     ret = CompensateAE();
541     if (ret != OK) {
542       ALOGE("%s: Failed during AE compensation: %d, (%s)", __FUNCTION__, ret,
543             strerror(-ret));
544     }
545   } else {
546     ALOGI(
547         "%s: No emulation for current AE mode using previous sensor settings!",
548         __FUNCTION__);
549   }
550 
551   if (info.is_flash_supported_) {
552     info.flash_state_ = ANDROID_FLASH_STATE_READY;
553     // Flash fires only if the request manually enables it (SINGLE/TORCH)
554     // and the appropriate AE mode is set or during still capture with auto
555     // flash AE modes.
556     bool manual_flash_mode = false;
557     auto ret = request_settings_->Get(ANDROID_FLASH_MODE, &entry);
558     if ((ret == OK) && (entry.count == 1)) {
559       if ((entry.data.u8[0] == ANDROID_FLASH_MODE_SINGLE) ||
560           (entry.data.u8[0] == ANDROID_FLASH_MODE_TORCH)) {
561         manual_flash_mode = true;
562       }
563     }
564     if (manual_flash_mode && !auto_ae_flash_mode) {
565       info.flash_state_ = ANDROID_FLASH_STATE_FIRED;
566     } else {
567       bool is_still_capture = false;
568       ret = request_settings_->Get(ANDROID_CONTROL_CAPTURE_INTENT, &entry);
569       if ((ret == OK) && (entry.count == 1)) {
570         if (entry.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE) {
571           is_still_capture = true;
572         }
573       }
574       if (is_still_capture && auto_ae_flash_mode) {
575         info.flash_state_ = ANDROID_FLASH_STATE_FIRED;
576       }
577     }
578   } else {
579     info.flash_state_ = ANDROID_FLASH_STATE_UNAVAILABLE;
580   }
581 
582   return OK;
583 }
584 
InitializeSensorSettings(std::unique_ptr<HalCameraMetadata> request_settings,uint32_t override_frame_number,EmulatedSensor::SensorSettings * sensor_settings)585 status_t EmulatedRequestState::InitializeSensorSettings(
586     std::unique_ptr<HalCameraMetadata> request_settings,
587     uint32_t override_frame_number,
588     EmulatedSensor::SensorSettings* sensor_settings /*out*/) {
589   auto& info = *device_info_;
590   if ((sensor_settings == nullptr) || (request_settings.get() == nullptr)) {
591     return BAD_VALUE;
592   }
593 
594   std::lock_guard<std::mutex> lock(request_state_mutex_);
595   request_settings_ = std::move(request_settings);
596   camera_metadata_ro_entry_t entry;
597   auto ret = request_settings_->Get(ANDROID_CONTROL_MODE, &entry);
598   if ((ret == OK) && (entry.count == 1)) {
599     if (info.available_control_modes_.find(entry.data.u8[0]) !=
600         info.available_control_modes_.end()) {
601       info.control_mode_ = entry.data.u8[0];
602     } else {
603       ALOGE("%s: Unsupported control mode!", __FUNCTION__);
604       return BAD_VALUE;
605     }
606   }
607 
608   ret = request_settings_->Get(ANDROID_SENSOR_PIXEL_MODE, &entry);
609   if ((ret == OK) && (entry.count == 1)) {
610     if (info.available_sensor_pixel_modes_.find(entry.data.u8[0]) !=
611         info.available_sensor_pixel_modes_.end()) {
612       info.sensor_pixel_mode_ = entry.data.u8[0];
613     } else {
614       ALOGE("%s: Unsupported control sensor pixel  mode!", __FUNCTION__);
615       return BAD_VALUE;
616     }
617   }
618 
619   ret = request_settings_->Get(ANDROID_CONTROL_SCENE_MODE, &entry);
620   if ((ret == OK) && (entry.count == 1)) {
621     // Disabled scene is not expected to be among the available scene list
622     if ((entry.data.u8[0] == ANDROID_CONTROL_SCENE_MODE_DISABLED) ||
623         (info.available_scenes_.find(entry.data.u8[0]) !=
624          info.available_scenes_.end())) {
625       info.scene_mode_ = entry.data.u8[0];
626     } else {
627       ALOGE("%s: Unsupported scene mode!", __FUNCTION__);
628       return BAD_VALUE;
629     }
630   }
631 
632   float min_zoom = info.min_zoom_, max_zoom = info.max_zoom_;
633   ret = request_settings_->Get(ANDROID_CONTROL_EXTENDED_SCENE_MODE, &entry);
634   if ((ret == OK) && (entry.count == 1)) {
635     bool extended_scene_mode_valid = false;
636     for (const auto& cap : info.available_extended_scene_mode_caps_) {
637       if (cap.mode == entry.data.u8[0]) {
638         info.extended_scene_mode_ = entry.data.u8[0];
639         min_zoom = cap.min_zoom;
640         max_zoom = cap.max_zoom;
641         extended_scene_mode_valid = true;
642         break;
643       }
644     }
645     if (!extended_scene_mode_valid) {
646       ALOGE("%s: Unsupported extended scene mode %d!", __FUNCTION__,
647             entry.data.u8[0]);
648       return BAD_VALUE;
649     }
650     if (info.extended_scene_mode_ !=
651         ANDROID_CONTROL_EXTENDED_SCENE_MODE_DISABLED) {
652       info.scene_mode_ = ANDROID_CONTROL_SCENE_MODE_FACE_PRIORITY;
653     }
654   }
655 
656   // Check zoom ratio range and override to supported range
657   ret = request_settings_->Get(ANDROID_CONTROL_ZOOM_RATIO, &entry);
658   if ((ret == OK) && (entry.count == 1)) {
659     info.zoom_ratio_ = std::min(std::max(entry.data.f[0], min_zoom), max_zoom);
660   }
661 
662   // Check settings override
663   ret = request_settings_->Get(ANDROID_CONTROL_SETTINGS_OVERRIDE, &entry);
664   if ((ret == OK) && (entry.count == 1)) {
665     info.settings_override_ = entry.data.i32[0];
666   }
667 
668   // Store settings override frame number
669   if (override_frame_number != 0) {
670     settings_overriding_frame_number_ = override_frame_number;
671   }
672 
673   // Check rotate_and_crop setting
674   ret = request_settings_->Get(ANDROID_SCALER_ROTATE_AND_CROP, &entry);
675   if ((ret == OK) && (entry.count == 1)) {
676     if (info.available_rotate_crop_modes_.find(entry.data.u8[0]) !=
677         info.available_rotate_crop_modes_.end()) {
678       info.rotate_and_crop_ = entry.data.u8[0];
679     } else {
680       ALOGE("%s: Unsupported rotate and crop mode: %u", __FUNCTION__, entry.data.u8[0]);
681       return BAD_VALUE;
682     }
683   }
684 
685   // Check video stabilization parameter
686   uint8_t vstab_mode = ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF;
687   ret = request_settings_->Get(ANDROID_CONTROL_VIDEO_STABILIZATION_MODE, &entry);
688   if ((ret == OK) && (entry.count == 1)) {
689     if (info.available_vstab_modes_.find(entry.data.u8[0]) !=
690         info.available_vstab_modes_.end()) {
691       vstab_mode = entry.data.u8[0];
692     } else {
693       ALOGE("%s: Unsupported video stabilization mode: %u! Video stabilization will be disabled!",
694             __FUNCTION__, entry.data.u8[0]);
695     }
696   }
697 
698   // Check autoframing
699   ret = request_settings_->Get(ANDROID_CONTROL_AUTOFRAMING, &entry);
700   if ((ret == OK) && (entry.count == 1)) {
701     info.autoframing_ = entry.data.i32[0];
702     if (info.autoframing_ == ANDROID_CONTROL_AUTOFRAMING_ON) {
703       // Set zoom_ratio to be a hard-coded value to test autoframing.
704       info.zoom_ratio_ = 1.7f;
705       vstab_mode = ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF;
706     }
707   }
708 
709   // Check manual flash strength level
710   ret = request_settings_->Get(ANDROID_FLASH_STRENGTH_LEVEL, &entry);
711   if ((ret == OK) && (entry.count == 1)) {
712     info.flash_strength_level_ = entry.data.i32[0];
713     if (ANDROID_FLASH_SINGLE_STRENGTH_MAX_LEVEL > 1 &&
714         ANDROID_FLASH_TORCH_STRENGTH_MAX_LEVEL > 1 && info.is_flash_supported_) {
715       ALOGI("%s: Device supports manual flash strength control", __FUNCTION__);
716       info.flash_strength_level_ = entry.data.i32[0];
717     } else {
718       ALOGI("%s: Device does not support manual flash strength control",
719             __FUNCTION__);
720       return BAD_VALUE;
721     }
722   }
723 
724   // Check video stabilization parameter
725   uint8_t edge_mode = ANDROID_EDGE_MODE_OFF;
726   ret = request_settings_->Get(ANDROID_EDGE_MODE, &entry);
727   if ((ret == OK) && (entry.count == 1)) {
728     if (info.available_edge_modes_.find(entry.data.u8[0]) !=
729         info.available_edge_modes_.end()) {
730       edge_mode = entry.data.u8[0];
731     } else {
732       ALOGE("%s: Unsupported edge mode: %u", __FUNCTION__, entry.data.u8[0]);
733       return BAD_VALUE;
734     }
735   }
736 
737   // Check test pattern parameter
738   uint8_t test_pattern_mode = ANDROID_SENSOR_TEST_PATTERN_MODE_OFF;
739   ret = request_settings_->Get(ANDROID_SENSOR_TEST_PATTERN_MODE, &entry);
740   if ((ret == OK) && (entry.count == 1)) {
741     if (info.available_test_pattern_modes_.find(entry.data.u8[0]) !=
742         info.available_test_pattern_modes_.end()) {
743       test_pattern_mode = entry.data.u8[0];
744     } else {
745       ALOGE("%s: Unsupported test pattern mode: %u", __FUNCTION__,
746             entry.data.u8[0]);
747       return BAD_VALUE;
748     }
749   }
750   uint32_t test_pattern_data[4] = {0, 0, 0, 0};
751   if (test_pattern_mode == ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR) {
752     ret = request_settings_->Get(ANDROID_SENSOR_TEST_PATTERN_DATA, &entry);
753     if ((ret == OK) && (entry.count == 4)) {
754       // 'Convert' from i32 to u32 here
755       memcpy(test_pattern_data, entry.data.i32, sizeof(test_pattern_data));
756     }
757   }
758   // BLACK is just SOLID_COLOR with all-zero data
759   if (test_pattern_mode == ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK) {
760     test_pattern_mode = ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR;
761   }
762 
763   // 3A modes are active in case the scene is disabled or set to face priority
764   // or the control mode is not using scenes
765   if ((info.scene_mode_ == ANDROID_CONTROL_SCENE_MODE_DISABLED) ||
766       (info.scene_mode_ == ANDROID_CONTROL_SCENE_MODE_FACE_PRIORITY) ||
767       (info.control_mode_ != ANDROID_CONTROL_MODE_USE_SCENE_MODE)) {
768     ret = request_settings_->Get(ANDROID_CONTROL_AE_MODE, &entry);
769     if ((ret == OK) && (entry.count == 1)) {
770       if (info.available_ae_modes_.find(entry.data.u8[0]) !=
771           info.available_ae_modes_.end()) {
772         info.ae_mode_ = entry.data.u8[0];
773       } else {
774         ALOGE("%s: Unsupported AE mode! Using last valid mode!", __FUNCTION__);
775       }
776     }
777 
778     ret = request_settings_->Get(ANDROID_CONTROL_AWB_MODE, &entry);
779     if ((ret == OK) && (entry.count == 1)) {
780       if (info.available_awb_modes_.find(entry.data.u8[0]) !=
781           info.available_awb_modes_.end()) {
782         info.awb_mode_ = entry.data.u8[0];
783       } else {
784         ALOGE("%s: Unsupported AWB mode! Using last valid mode!", __FUNCTION__);
785       }
786     }
787 
788     ret = request_settings_->Get(ANDROID_CONTROL_AF_MODE, &entry);
789     if ((ret == OK) && (entry.count == 1)) {
790       if (info.available_af_modes_.find(entry.data.u8[0]) !=
791           info.available_af_modes_.end()) {
792         af_mode_changed_ = info.af_mode_ != entry.data.u8[0];
793         info.af_mode_ = entry.data.u8[0];
794       } else {
795         ALOGE("%s: Unsupported AF mode! Using last valid mode!", __FUNCTION__);
796       }
797     }
798   } else {
799     auto it = info.scene_overrides_.find(info.scene_mode_);
800     if (it != info.scene_overrides_.end()) {
801       info.ae_mode_ = it->second.ae_mode;
802       info.awb_mode_ = it->second.awb_mode;
803       af_mode_changed_ = info.af_mode_ != entry.data.u8[0];
804       info.af_mode_ = it->second.af_mode;
805     } else {
806       ALOGW(
807           "%s: Current scene has no overrides! Using the currently active 3A "
808           "modes!",
809           __FUNCTION__);
810     }
811   }
812 
813   ret = ProcessAE();
814   if (ret != OK) {
815     return ret;
816   }
817 
818   ret = ProcessAWB();
819   if (ret != OK) {
820     return ret;
821   }
822 
823   ret = ProcessAF();
824   if (ret != OK) {
825     return ret;
826   }
827 
828   ret = request_settings_->Get(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE, &entry);
829   if ((ret == OK) && (entry.count == 1)) {
830     if (info.available_lens_shading_map_modes_.find(entry.data.u8[0]) !=
831         info.available_lens_shading_map_modes_.end()) {
832       sensor_settings->lens_shading_map_mode = entry.data.u8[0];
833     } else {
834       ALOGE("%s: Unsupported lens shading map mode!", __FUNCTION__);
835     }
836   }
837 
838   ret = info.static_metadata_->Get(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE, &entry);
839   if ((ret == OK) && (entry.count == 1)) {
840     if (entry.data.u8[0] == ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
841       info.timestamp_source_ = ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME;
842     } else if (entry.data.u8[0] != ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN) {
843       ALOGE("%s: Unsupported timestamp source", __FUNCTION__);
844     }
845   }
846 
847   sensor_settings->exposure_time = info.sensor_exposure_time_;
848   sensor_settings->frame_duration = info.sensor_frame_duration_;
849   sensor_settings->gain = info.sensor_sensitivity_;
850   sensor_settings->report_neutral_color_point = info.report_neutral_color_point_;
851   sensor_settings->report_green_split = info.report_green_split_;
852   sensor_settings->report_noise_profile = info.report_noise_profile_;
853   sensor_settings->zoom_ratio = info.zoom_ratio_;
854   sensor_settings->report_rotate_and_crop = info.report_rotate_and_crop_;
855   sensor_settings->rotate_and_crop = info.rotate_and_crop_;
856   sensor_settings->report_video_stab = !info.available_vstab_modes_.empty();
857   sensor_settings->video_stab = vstab_mode;
858   sensor_settings->report_edge_mode = info.report_edge_mode_;
859   sensor_settings->edge_mode = edge_mode;
860   sensor_settings->sensor_pixel_mode = info.sensor_pixel_mode_;
861   sensor_settings->test_pattern_mode = test_pattern_mode;
862   sensor_settings->timestamp_source = info.timestamp_source_;
863   memcpy(sensor_settings->test_pattern_data, test_pattern_data,
864          sizeof(sensor_settings->test_pattern_data));
865 
866   return OK;
867 }
868 
GetPartialResultCount(bool is_partial_result)869 uint32_t EmulatedRequestState::GetPartialResultCount(bool is_partial_result) {
870   uint32_t res = 0;
871   auto& info = *device_info_;
872 
873   if (is_partial_result) {
874     res = 1;
875   } else {
876     res = info.partial_result_count_ ? info.partial_result_count_ : 1;
877   }
878 
879   return res;
880 }
881 
InitializePartialResult(uint32_t pipeline_id,uint32_t frame_number)882 std::unique_ptr<HwlPipelineResult> EmulatedRequestState::InitializePartialResult(
883     uint32_t pipeline_id, uint32_t frame_number) {
884   auto& info = *device_info_;
885   std::lock_guard<std::mutex> lock(request_state_mutex_);
886   auto result = std::make_unique<HwlPipelineResult>();
887 
888   if (info.partial_result_count_ > 1) {
889     result->camera_id = camera_id_;
890     result->pipeline_id = pipeline_id;
891     result->frame_number = frame_number;
892     result->result_metadata = HalCameraMetadata::Create(0, 0);
893     result->partial_result = GetPartialResultCount(/*is partial result*/ true);
894   }
895 
896   return result;
897 }
898 
InitializeResult(uint32_t pipeline_id,uint32_t frame_number)899 std::unique_ptr<HwlPipelineResult> EmulatedRequestState::InitializeResult(
900     uint32_t pipeline_id, uint32_t frame_number) {
901   auto& info = *device_info_;
902   std::lock_guard<std::mutex> lock(request_state_mutex_);
903   auto result = std::make_unique<HwlPipelineResult>();
904   result->camera_id = camera_id_;
905   result->pipeline_id = pipeline_id;
906   result->frame_number = frame_number;
907   result->result_metadata = HalCameraMetadata::Clone(request_settings_.get());
908   result->partial_result = GetPartialResultCount(/*is partial result*/ false);
909 
910   // Results supported on all emulated devices
911   result->result_metadata->Set(ANDROID_REQUEST_PIPELINE_DEPTH,
912                                &info.max_pipeline_depth_, 1);
913   result->result_metadata->Set(ANDROID_CONTROL_MODE, &info.control_mode_, 1);
914   result->result_metadata->Set(ANDROID_SENSOR_PIXEL_MODE,
915                                &info.sensor_pixel_mode_, 1);
916 
917   result->result_metadata->Set(ANDROID_CONTROL_AF_MODE, &info.af_mode_, 1);
918   result->result_metadata->Set(ANDROID_CONTROL_AF_STATE, &info.af_state_, 1);
919   result->result_metadata->Set(ANDROID_CONTROL_AWB_MODE, &info.awb_mode_, 1);
920   result->result_metadata->Set(ANDROID_CONTROL_AWB_STATE, &info.awb_state_, 1);
921   result->result_metadata->Set(ANDROID_CONTROL_AE_MODE, &info.ae_mode_, 1);
922   result->result_metadata->Set(ANDROID_CONTROL_AE_STATE, &info.ae_state_, 1);
923   // If the overriding frame number isn't larger than current frame number,
924   // use 0.
925   int32_t settings_override = info.settings_override_;
926   uint32_t overriding_frame_number = settings_overriding_frame_number_;
927   if (overriding_frame_number <= frame_number) {
928     overriding_frame_number = frame_number;
929     settings_override = ANDROID_CONTROL_SETTINGS_OVERRIDE_OFF;
930   }
931   result->result_metadata->Set(ANDROID_CONTROL_SETTINGS_OVERRIDE,
932                                &settings_override, 1);
933   result->result_metadata->Set(ANDROID_CONTROL_SETTINGS_OVERRIDING_FRAME_NUMBER,
934                                (int32_t*)&overriding_frame_number, 1);
935   result->result_metadata->Set(ANDROID_CONTROL_AUTOFRAMING, &info.autoframing_,
936                                1);
937   uint8_t autoframing_state = ANDROID_CONTROL_AUTOFRAMING_STATE_INACTIVE;
938   if (info.autoframing_ == ANDROID_CONTROL_AUTOFRAMING_ON) {
939     autoframing_state = ANDROID_CONTROL_AUTOFRAMING_STATE_CONVERGED;
940   }
941   result->result_metadata->Set(ANDROID_CONTROL_AUTOFRAMING_STATE,
942                                &autoframing_state, 1);
943 
944   int32_t fps_range[] = {info.ae_target_fps_.min_fps,
945                          info.ae_target_fps_.max_fps};
946   result->result_metadata->Set(ANDROID_CONTROL_AE_TARGET_FPS_RANGE, fps_range,
947                                ARRAY_SIZE(fps_range));
948   result->result_metadata->Set(ANDROID_FLASH_STATE, &info.flash_state_, 1);
949   result->result_metadata->Set(ANDROID_LENS_STATE, &info.lens_state_, 1);
950 
951   // Results depending on device capability and features
952   if (info.is_backward_compatible_) {
953     result->result_metadata->Set(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
954                                  &info.ae_trigger_, 1);
955     result->result_metadata->Set(ANDROID_CONTROL_AF_TRIGGER, &info.af_trigger_,
956                                  1);
957     uint8_t vstab_mode = ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF;
958     result->result_metadata->Set(ANDROID_CONTROL_VIDEO_STABILIZATION_MODE,
959                                  &vstab_mode, 1);
960     if (info.exposure_compensation_supported_) {
961       result->result_metadata->Set(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
962                                    &info.exposure_compensation_, 1);
963     }
964   }
965   if (info.ae_lock_available_ && info.report_ae_lock_) {
966     result->result_metadata->Set(ANDROID_CONTROL_AE_LOCK, &info.ae_lock_, 1);
967   }
968   if (info.awb_lock_available_ && info.report_awb_lock_) {
969     result->result_metadata->Set(ANDROID_CONTROL_AWB_LOCK, &info.awb_lock_, 1);
970   }
971   if (info.scenes_supported_) {
972     result->result_metadata->Set(ANDROID_CONTROL_SCENE_MODE, &info.scene_mode_,
973                                  1);
974   }
975   if (info.max_ae_regions_ > 0) {
976     result->result_metadata->Set(ANDROID_CONTROL_AE_REGIONS,
977                                  info.ae_metering_region_,
978                                  ARRAY_SIZE(info.ae_metering_region_));
979   }
980   if (info.max_awb_regions_ > 0) {
981     result->result_metadata->Set(ANDROID_CONTROL_AWB_REGIONS,
982                                  info.awb_metering_region_,
983                                  ARRAY_SIZE(info.awb_metering_region_));
984   }
985   if (info.max_af_regions_ > 0) {
986     result->result_metadata->Set(ANDROID_CONTROL_AF_REGIONS,
987                                  info.af_metering_region_,
988                                  ARRAY_SIZE(info.af_metering_region_));
989   }
990   if (info.report_exposure_time_) {
991     result->result_metadata->Set(ANDROID_SENSOR_EXPOSURE_TIME,
992                                  &info.sensor_exposure_time_, 1);
993   } else {
994     result->result_metadata->Erase(ANDROID_SENSOR_EXPOSURE_TIME);
995   }
996   if (info.report_frame_duration_) {
997     result->result_metadata->Set(ANDROID_SENSOR_FRAME_DURATION,
998                                  &info.sensor_frame_duration_, 1);
999   } else {
1000     result->result_metadata->Erase(ANDROID_SENSOR_FRAME_DURATION);
1001   }
1002   if (info.report_sensitivity_) {
1003     result->result_metadata->Set(ANDROID_SENSOR_SENSITIVITY,
1004                                  &info.sensor_sensitivity_, 1);
1005   } else {
1006     result->result_metadata->Erase(ANDROID_SENSOR_SENSITIVITY);
1007   }
1008   if (info.report_rolling_shutter_skew_) {
1009     result->result_metadata->Set(
1010         ANDROID_SENSOR_ROLLING_SHUTTER_SKEW,
1011         &EmulatedSensor::kSupportedFrameDurationRange[0], 1);
1012   }
1013   if (info.report_post_raw_boost_) {
1014     result->result_metadata->Set(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
1015                                  &info.post_raw_boost_, 1);
1016   }
1017   if (info.report_focus_distance_) {
1018     result->result_metadata->Set(ANDROID_LENS_FOCUS_DISTANCE,
1019                                  &info.focus_distance_, 1);
1020   }
1021   if (info.report_focus_range_) {
1022     float focus_range[2] = {};
1023     focus_range[0] = info.focus_distance_;
1024     result->result_metadata->Set(ANDROID_LENS_FOCUS_RANGE, focus_range, ARRAY_SIZE(focus_range));
1025   }
1026   if (info.report_filter_density_) {
1027     result->result_metadata->Set(ANDROID_LENS_FILTER_DENSITY,
1028                                  &info.filter_density_, 1);
1029   }
1030   if (info.report_ois_mode_) {
1031     result->result_metadata->Set(ANDROID_LENS_OPTICAL_STABILIZATION_MODE,
1032                                  &info.ois_mode_, 1);
1033   }
1034   if (info.report_pose_rotation_) {
1035     result->result_metadata->Set(ANDROID_LENS_POSE_ROTATION, info.pose_rotation_,
1036                                  ARRAY_SIZE(info.pose_rotation_));
1037   }
1038   if (info.report_pose_translation_) {
1039     result->result_metadata->Set(ANDROID_LENS_POSE_TRANSLATION,
1040                                  info.pose_translation_,
1041                                  ARRAY_SIZE(info.pose_translation_));
1042   }
1043   if (info.report_intrinsic_calibration_) {
1044     result->result_metadata->Set(ANDROID_LENS_INTRINSIC_CALIBRATION,
1045                                  info.intrinsic_calibration_,
1046                                  ARRAY_SIZE(info.intrinsic_calibration_));
1047   }
1048   if (info.report_lens_intrinsics_samples_) {
1049     result->result_metadata->Set(ANDROID_STATISTICS_LENS_INTRINSIC_SAMPLES,
1050                                  info.intrinsic_calibration_,
1051                                  ARRAY_SIZE(info.intrinsic_calibration_));
1052   }
1053   if (info.report_distortion_) {
1054     result->result_metadata->Set(ANDROID_LENS_DISTORTION, info.distortion_,
1055                                  ARRAY_SIZE(info.distortion_));
1056   }
1057   if (info.report_black_level_lock_) {
1058     result->result_metadata->Set(ANDROID_BLACK_LEVEL_LOCK,
1059                                  &info.black_level_lock_, 1);
1060   }
1061   if (info.report_scene_flicker_) {
1062     result->result_metadata->Set(ANDROID_STATISTICS_SCENE_FLICKER,
1063                                  &info.current_scene_flicker_, 1);
1064   }
1065   if (info.zoom_ratio_supported_) {
1066     result->result_metadata->Set(ANDROID_CONTROL_ZOOM_RATIO, &info.zoom_ratio_,
1067                                  1);
1068     int32_t* chosen_crop_region = info.scaler_crop_region_default_;
1069     if (info.sensor_pixel_mode_ == ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) {
1070       chosen_crop_region = info.scaler_crop_region_max_resolution_;
1071     }
1072     result->result_metadata->Set(ANDROID_SCALER_CROP_REGION, chosen_crop_region,
1073                                  ARRAY_SIZE(info.scaler_crop_region_default_));
1074     if (info.report_active_sensor_crop_) {
1075       int32_t active_crop_region[4];
1076       // width
1077       active_crop_region[2] =
1078           (info.scaler_crop_region_default_[2] / info.zoom_ratio_);
1079       // height
1080       active_crop_region[3] =
1081           (info.scaler_crop_region_default_[3] / info.zoom_ratio_);
1082       // left
1083       active_crop_region[0] =
1084           (info.scaler_crop_region_default_[2] - active_crop_region[2]) / 2;
1085       // top
1086       active_crop_region[1] =
1087           (info.scaler_crop_region_default_[3] - active_crop_region[3]) / 2;
1088       result->result_metadata->Set(
1089           ANDROID_LOGICAL_MULTI_CAMERA_ACTIVE_PHYSICAL_SENSOR_CROP_REGION,
1090           active_crop_region, ARRAY_SIZE(info.scaler_crop_region_default_));
1091     }
1092   }
1093   if (info.report_extended_scene_mode_) {
1094     result->result_metadata->Set(ANDROID_CONTROL_EXTENDED_SCENE_MODE,
1095                                  &info.extended_scene_mode_, 1);
1096   }
1097   return result;
1098 }
1099 
Initialize(std::unique_ptr<EmulatedCameraDeviceInfo> deviceInfo)1100 status_t EmulatedRequestState::Initialize(
1101     std::unique_ptr<EmulatedCameraDeviceInfo> deviceInfo) {
1102   std::lock_guard<std::mutex> lock(request_state_mutex_);
1103   device_info_ = std::move(deviceInfo);
1104 
1105   return OK;
1106 }
1107 
GetDefaultRequest(RequestTemplate type,std::unique_ptr<HalCameraMetadata> * default_settings)1108 status_t EmulatedRequestState::GetDefaultRequest(
1109     RequestTemplate type, std::unique_ptr<HalCameraMetadata>* default_settings) {
1110   if (default_settings == nullptr) {
1111     ALOGE("%s default_settings is nullptr", __FUNCTION__);
1112     return BAD_VALUE;
1113   }
1114 
1115   std::lock_guard<std::mutex> lock(request_state_mutex_);
1116   auto idx = static_cast<size_t>(type);
1117   if (idx >= kTemplateCount) {
1118     ALOGE("%s: Unexpected request type: %d", __FUNCTION__, type);
1119     return BAD_VALUE;
1120   }
1121 
1122   if (device_info_->default_requests_[idx].get() == nullptr) {
1123     ALOGE("%s: Unsupported request type: %d", __FUNCTION__, type);
1124     return BAD_VALUE;
1125   }
1126 
1127   *default_settings = HalCameraMetadata::Clone(
1128       device_info_->default_requests_[idx]->GetRawCameraMetadata());
1129 
1130   return OK;
1131 }
1132 
1133 }  // namespace android
1134