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     if (info.ae_priority_mode_ == ANDROID_CONTROL_AE_PRIORITY_MODE_SENSOR_EXPOSURE_TIME_PRIORITY) {
546       auto ret = request_settings_->Get(ANDROID_SENSOR_EXPOSURE_TIME, &entry);
547       if ((ret == OK) && (entry.count == 1)) {
548         info.sensor_exposure_time_  = GetExposureTimeClampToRange(entry.data.i64[0]);
549       }
550     }
551 
552     if (info.ae_priority_mode_ == ANDROID_CONTROL_AE_PRIORITY_MODE_SENSOR_SENSITIVITY_PRIORITY) {
553       auto ret = request_settings_->Get(ANDROID_SENSOR_SENSITIVITY, &entry);
554       if ((ret == OK) && (entry.count == 1)) {
555         info.sensor_sensitivity_ = GetSensitivityClampToRange(entry.data.i32[0]);
556       }
557     }
558   } else {
559     ALOGI(
560         "%s: No emulation for current AE mode using previous sensor settings!",
561         __FUNCTION__);
562   }
563 
564   if (info.is_flash_supported_) {
565     info.flash_state_ = ANDROID_FLASH_STATE_READY;
566     // Flash fires only if the request manually enables it (SINGLE/TORCH)
567     // and the appropriate AE mode is set or during still capture with auto
568     // flash AE modes.
569     bool manual_flash_mode = false;
570     auto ret = request_settings_->Get(ANDROID_FLASH_MODE, &entry);
571     if ((ret == OK) && (entry.count == 1)) {
572       if ((entry.data.u8[0] == ANDROID_FLASH_MODE_SINGLE) ||
573           (entry.data.u8[0] == ANDROID_FLASH_MODE_TORCH)) {
574         manual_flash_mode = true;
575       }
576     }
577     if (manual_flash_mode && !auto_ae_flash_mode) {
578       info.flash_state_ = ANDROID_FLASH_STATE_FIRED;
579     } else {
580       bool is_still_capture = false;
581       ret = request_settings_->Get(ANDROID_CONTROL_CAPTURE_INTENT, &entry);
582       if ((ret == OK) && (entry.count == 1)) {
583         if (entry.data.u8[0] == ANDROID_CONTROL_CAPTURE_INTENT_STILL_CAPTURE) {
584           is_still_capture = true;
585         }
586       }
587       if (is_still_capture && auto_ae_flash_mode) {
588         info.flash_state_ = ANDROID_FLASH_STATE_FIRED;
589       }
590     }
591   } else {
592     info.flash_state_ = ANDROID_FLASH_STATE_UNAVAILABLE;
593   }
594 
595   return OK;
596 }
597 
GetSensitivityClampToRange(uint32_t sensitivity)598 int EmulatedRequestState::GetSensitivityClampToRange(uint32_t sensitivity) {
599   auto& info = *device_info_;
600   uint32_t min_sensitivity = info.sensor_sensitivity_range_.first;
601   uint32_t max_sensitivity = info.sensor_sensitivity_range_.second;
602 
603   if ((sensitivity < min_sensitivity) ||
604       (sensitivity > max_sensitivity)) {
605     ALOGW("%s: Sensor sensitivity %d not within supported range[%d, %d]",
606         __FUNCTION__, sensitivity, min_sensitivity, max_sensitivity);
607   }
608 
609   return std::max(min_sensitivity, std::min(max_sensitivity, sensitivity));
610 }
611 
GetExposureTimeClampToRange(uint32_t exposure)612 int EmulatedRequestState::GetExposureTimeClampToRange(uint32_t exposure) {
613   auto& info = *device_info_;
614   uint32_t min_exposure = info.sensor_exposure_time_range_.first;
615   uint32_t max_exposure = info.sensor_exposure_time_range_.second;
616 
617   if ((exposure < min_exposure) ||
618       (exposure > max_exposure)) {
619     ALOGW("%s: Sensor exposure time %d not within supported range[%d, %d]",
620         __FUNCTION__, exposure, min_exposure, max_exposure);
621   }
622 
623   return std::max(min_exposure, std::min(max_exposure, exposure));
624 }
625 
InitializeSensorSettings(std::unique_ptr<HalCameraMetadata> request_settings,uint32_t override_frame_number,EmulatedSensor::SensorSettings * sensor_settings)626 status_t EmulatedRequestState::InitializeSensorSettings(
627     std::unique_ptr<HalCameraMetadata> request_settings,
628     uint32_t override_frame_number,
629     EmulatedSensor::SensorSettings* sensor_settings /*out*/) {
630   auto& info = *device_info_;
631   if ((sensor_settings == nullptr) || (request_settings.get() == nullptr)) {
632     return BAD_VALUE;
633   }
634 
635   std::lock_guard<std::mutex> lock(request_state_mutex_);
636   request_settings_ = std::move(request_settings);
637   camera_metadata_ro_entry_t entry;
638   auto ret = request_settings_->Get(ANDROID_CONTROL_MODE, &entry);
639   if ((ret == OK) && (entry.count == 1)) {
640     if (info.available_control_modes_.find(entry.data.u8[0]) !=
641         info.available_control_modes_.end()) {
642       info.control_mode_ = entry.data.u8[0];
643     } else {
644       ALOGE("%s: Unsupported control mode!", __FUNCTION__);
645       return BAD_VALUE;
646     }
647   }
648 
649   ret = request_settings_->Get(ANDROID_SENSOR_PIXEL_MODE, &entry);
650   if ((ret == OK) && (entry.count == 1)) {
651     if (info.available_sensor_pixel_modes_.find(entry.data.u8[0]) !=
652         info.available_sensor_pixel_modes_.end()) {
653       info.sensor_pixel_mode_ = entry.data.u8[0];
654     } else {
655       ALOGE("%s: Unsupported control sensor pixel  mode!", __FUNCTION__);
656       return BAD_VALUE;
657     }
658   }
659 
660   ret = request_settings_->Get(ANDROID_CONTROL_SCENE_MODE, &entry);
661   if ((ret == OK) && (entry.count == 1)) {
662     // Disabled scene is not expected to be among the available scene list
663     if ((entry.data.u8[0] == ANDROID_CONTROL_SCENE_MODE_DISABLED) ||
664         (info.available_scenes_.find(entry.data.u8[0]) !=
665          info.available_scenes_.end())) {
666       info.scene_mode_ = entry.data.u8[0];
667     } else {
668       ALOGE("%s: Unsupported scene mode!", __FUNCTION__);
669       return BAD_VALUE;
670     }
671   }
672 
673   float min_zoom = info.min_zoom_, max_zoom = info.max_zoom_;
674   ret = request_settings_->Get(ANDROID_CONTROL_EXTENDED_SCENE_MODE, &entry);
675   if ((ret == OK) && (entry.count == 1)) {
676     bool extended_scene_mode_valid = false;
677     for (const auto& cap : info.available_extended_scene_mode_caps_) {
678       if (cap.mode == entry.data.u8[0]) {
679         info.extended_scene_mode_ = entry.data.u8[0];
680         min_zoom = cap.min_zoom;
681         max_zoom = cap.max_zoom;
682         extended_scene_mode_valid = true;
683         break;
684       }
685     }
686     if (!extended_scene_mode_valid) {
687       ALOGE("%s: Unsupported extended scene mode %d!", __FUNCTION__,
688             entry.data.u8[0]);
689       return BAD_VALUE;
690     }
691     if (info.extended_scene_mode_ !=
692         ANDROID_CONTROL_EXTENDED_SCENE_MODE_DISABLED) {
693       info.scene_mode_ = ANDROID_CONTROL_SCENE_MODE_FACE_PRIORITY;
694     }
695   }
696 
697   // Check zoom ratio range and override to supported range
698   ret = request_settings_->Get(ANDROID_CONTROL_ZOOM_RATIO, &entry);
699   if ((ret == OK) && (entry.count == 1)) {
700     info.zoom_ratio_ = std::min(std::max(entry.data.f[0], min_zoom), max_zoom);
701   }
702 
703   // Check settings override
704   ret = request_settings_->Get(ANDROID_CONTROL_SETTINGS_OVERRIDE, &entry);
705   if ((ret == OK) && (entry.count == 1)) {
706     info.settings_override_ = entry.data.i32[0];
707   }
708 
709   // Store settings override frame number
710   if (override_frame_number != 0) {
711     settings_overriding_frame_number_ = override_frame_number;
712   }
713 
714   // Check rotate_and_crop setting
715   ret = request_settings_->Get(ANDROID_SCALER_ROTATE_AND_CROP, &entry);
716   if ((ret == OK) && (entry.count == 1)) {
717     if (info.available_rotate_crop_modes_.find(entry.data.u8[0]) !=
718         info.available_rotate_crop_modes_.end()) {
719       info.rotate_and_crop_ = entry.data.u8[0];
720     } else {
721       ALOGE("%s: Unsupported rotate and crop mode: %u", __FUNCTION__, entry.data.u8[0]);
722       return BAD_VALUE;
723     }
724   }
725 
726   // Check video stabilization parameter
727   uint8_t vstab_mode = ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF;
728   ret = request_settings_->Get(ANDROID_CONTROL_VIDEO_STABILIZATION_MODE, &entry);
729   if ((ret == OK) && (entry.count == 1)) {
730     if (info.available_vstab_modes_.find(entry.data.u8[0]) !=
731         info.available_vstab_modes_.end()) {
732       vstab_mode = entry.data.u8[0];
733     } else {
734       ALOGE("%s: Unsupported video stabilization mode: %u! Video stabilization will be disabled!",
735             __FUNCTION__, entry.data.u8[0]);
736     }
737   }
738 
739   // Check autoframing
740   ret = request_settings_->Get(ANDROID_CONTROL_AUTOFRAMING, &entry);
741   if ((ret == OK) && (entry.count == 1)) {
742     info.autoframing_ = entry.data.i32[0];
743     if (info.autoframing_ == ANDROID_CONTROL_AUTOFRAMING_ON) {
744       // Set zoom_ratio to be a hard-coded value to test autoframing.
745       info.zoom_ratio_ = 1.7f;
746       vstab_mode = ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF;
747     }
748   }
749 
750   // Check manual flash strength level
751   ret = request_settings_->Get(ANDROID_FLASH_STRENGTH_LEVEL, &entry);
752   if ((ret == OK) && (entry.count == 1)) {
753     info.flash_strength_level_ = entry.data.i32[0];
754     if (ANDROID_FLASH_SINGLE_STRENGTH_MAX_LEVEL > 1 &&
755         ANDROID_FLASH_TORCH_STRENGTH_MAX_LEVEL > 1 && info.is_flash_supported_) {
756       ALOGI("%s: Device supports manual flash strength control", __FUNCTION__);
757       info.flash_strength_level_ = entry.data.i32[0];
758     } else {
759       ALOGI("%s: Device does not support manual flash strength control",
760             __FUNCTION__);
761       return BAD_VALUE;
762     }
763   }
764 
765   // Check video stabilization parameter
766   uint8_t edge_mode = ANDROID_EDGE_MODE_OFF;
767   ret = request_settings_->Get(ANDROID_EDGE_MODE, &entry);
768   if ((ret == OK) && (entry.count == 1)) {
769     if (info.available_edge_modes_.find(entry.data.u8[0]) !=
770         info.available_edge_modes_.end()) {
771       edge_mode = entry.data.u8[0];
772     } else {
773       ALOGE("%s: Unsupported edge mode: %u", __FUNCTION__, entry.data.u8[0]);
774       return BAD_VALUE;
775     }
776   }
777 
778   // Check test pattern parameter
779   uint8_t test_pattern_mode = ANDROID_SENSOR_TEST_PATTERN_MODE_OFF;
780   ret = request_settings_->Get(ANDROID_SENSOR_TEST_PATTERN_MODE, &entry);
781   if ((ret == OK) && (entry.count == 1)) {
782     if (info.available_test_pattern_modes_.find(entry.data.u8[0]) !=
783         info.available_test_pattern_modes_.end()) {
784       test_pattern_mode = entry.data.u8[0];
785     } else {
786       ALOGE("%s: Unsupported test pattern mode: %u", __FUNCTION__,
787             entry.data.u8[0]);
788       return BAD_VALUE;
789     }
790   }
791   uint32_t test_pattern_data[4] = {0, 0, 0, 0};
792   if (test_pattern_mode == ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR) {
793     ret = request_settings_->Get(ANDROID_SENSOR_TEST_PATTERN_DATA, &entry);
794     if ((ret == OK) && (entry.count == 4)) {
795       // 'Convert' from i32 to u32 here
796       memcpy(test_pattern_data, entry.data.i32, sizeof(test_pattern_data));
797     }
798   }
799   // BLACK is just SOLID_COLOR with all-zero data
800   if (test_pattern_mode == ANDROID_SENSOR_TEST_PATTERN_MODE_BLACK) {
801     test_pattern_mode = ANDROID_SENSOR_TEST_PATTERN_MODE_SOLID_COLOR;
802   }
803 
804   // 3A modes are active in case the scene is disabled or set to face priority
805   // or the control mode is not using scenes
806   if ((info.scene_mode_ == ANDROID_CONTROL_SCENE_MODE_DISABLED) ||
807       (info.scene_mode_ == ANDROID_CONTROL_SCENE_MODE_FACE_PRIORITY) ||
808       (info.control_mode_ != ANDROID_CONTROL_MODE_USE_SCENE_MODE)) {
809     ret = request_settings_->Get(ANDROID_CONTROL_AE_MODE, &entry);
810     if ((ret == OK) && (entry.count == 1)) {
811       if (info.available_ae_modes_.find(entry.data.u8[0]) !=
812           info.available_ae_modes_.end()) {
813         info.ae_mode_ = entry.data.u8[0];
814       } else {
815         ALOGE("%s: Unsupported AE mode! Using last valid mode!", __FUNCTION__);
816       }
817     }
818 
819     ret = request_settings_->Get(ANDROID_CONTROL_AE_PRIORITY_MODE, &entry);
820     if ((ret == OK) && (entry.count == 1)) {
821       if (info.available_ae_priority_modes_.find(entry.data.u8[0]) !=
822           info.available_ae_priority_modes_.end()) {
823         info.ae_priority_mode_ = entry.data.u8[0];
824       } else {
825         ALOGE("%s: Unsupported AE priority mode! Using last valid mode!", __FUNCTION__);
826       }
827     }
828 
829     ret = request_settings_->Get(ANDROID_CONTROL_AWB_MODE, &entry);
830     if ((ret == OK) && (entry.count == 1)) {
831       if (info.available_awb_modes_.find(entry.data.u8[0]) !=
832           info.available_awb_modes_.end()) {
833         info.awb_mode_ = entry.data.u8[0];
834       } else {
835         ALOGE("%s: Unsupported AWB mode! Using last valid mode!", __FUNCTION__);
836       }
837     }
838 
839     ret = request_settings_->Get(ANDROID_CONTROL_AF_MODE, &entry);
840     if ((ret == OK) && (entry.count == 1)) {
841       if (info.available_af_modes_.find(entry.data.u8[0]) !=
842           info.available_af_modes_.end()) {
843         af_mode_changed_ = info.af_mode_ != entry.data.u8[0];
844         info.af_mode_ = entry.data.u8[0];
845       } else {
846         ALOGE("%s: Unsupported AF mode! Using last valid mode!", __FUNCTION__);
847       }
848     }
849   } else {
850     auto it = info.scene_overrides_.find(info.scene_mode_);
851     if (it != info.scene_overrides_.end()) {
852       info.ae_mode_ = it->second.ae_mode;
853       info.awb_mode_ = it->second.awb_mode;
854       af_mode_changed_ = info.af_mode_ != entry.data.u8[0];
855       info.af_mode_ = it->second.af_mode;
856     } else {
857       ALOGW(
858           "%s: Current scene has no overrides! Using the currently active 3A "
859           "modes!",
860           __FUNCTION__);
861     }
862   }
863 
864   ret = ProcessAE();
865   if (ret != OK) {
866     return ret;
867   }
868 
869   ret = ProcessAWB();
870   if (ret != OK) {
871     return ret;
872   }
873 
874   ret = ProcessAF();
875   if (ret != OK) {
876     return ret;
877   }
878 
879   ret = request_settings_->Get(ANDROID_STATISTICS_LENS_SHADING_MAP_MODE, &entry);
880   if ((ret == OK) && (entry.count == 1)) {
881     if (info.available_lens_shading_map_modes_.find(entry.data.u8[0]) !=
882         info.available_lens_shading_map_modes_.end()) {
883       sensor_settings->lens_shading_map_mode = entry.data.u8[0];
884     } else {
885       ALOGE("%s: Unsupported lens shading map mode!", __FUNCTION__);
886     }
887   }
888 
889   ret = info.static_metadata_->Get(ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE, &entry);
890   if ((ret == OK) && (entry.count == 1)) {
891     if (entry.data.u8[0] == ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME) {
892       info.timestamp_source_ = ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_REALTIME;
893     } else if (entry.data.u8[0] != ANDROID_SENSOR_INFO_TIMESTAMP_SOURCE_UNKNOWN) {
894       ALOGE("%s: Unsupported timestamp source", __FUNCTION__);
895     }
896   }
897 
898   sensor_settings->exposure_time = info.sensor_exposure_time_;
899   sensor_settings->frame_duration = info.sensor_frame_duration_;
900   sensor_settings->gain = info.sensor_sensitivity_;
901   sensor_settings->report_neutral_color_point = info.report_neutral_color_point_;
902   sensor_settings->report_green_split = info.report_green_split_;
903   sensor_settings->report_noise_profile = info.report_noise_profile_;
904   sensor_settings->zoom_ratio = info.zoom_ratio_;
905   sensor_settings->report_rotate_and_crop = info.report_rotate_and_crop_;
906   sensor_settings->rotate_and_crop = info.rotate_and_crop_;
907   sensor_settings->report_video_stab = !info.available_vstab_modes_.empty();
908   sensor_settings->video_stab = vstab_mode;
909   sensor_settings->report_edge_mode = info.report_edge_mode_;
910   sensor_settings->edge_mode = edge_mode;
911   sensor_settings->sensor_pixel_mode = info.sensor_pixel_mode_;
912   sensor_settings->test_pattern_mode = test_pattern_mode;
913   sensor_settings->timestamp_source = info.timestamp_source_;
914   memcpy(sensor_settings->test_pattern_data, test_pattern_data,
915          sizeof(sensor_settings->test_pattern_data));
916 
917   return OK;
918 }
919 
GetPartialResultCount(bool is_partial_result)920 uint32_t EmulatedRequestState::GetPartialResultCount(bool is_partial_result) {
921   uint32_t res = 0;
922   auto& info = *device_info_;
923 
924   if (is_partial_result) {
925     res = 1;
926   } else {
927     res = info.partial_result_count_ ? info.partial_result_count_ : 1;
928   }
929 
930   return res;
931 }
932 
InitializePartialResult(uint32_t pipeline_id,uint32_t frame_number)933 std::unique_ptr<HwlPipelineResult> EmulatedRequestState::InitializePartialResult(
934     uint32_t pipeline_id, uint32_t frame_number) {
935   auto& info = *device_info_;
936   std::lock_guard<std::mutex> lock(request_state_mutex_);
937   auto result = std::make_unique<HwlPipelineResult>();
938 
939   if (info.partial_result_count_ > 1) {
940     result->camera_id = camera_id_;
941     result->pipeline_id = pipeline_id;
942     result->frame_number = frame_number;
943     result->result_metadata = HalCameraMetadata::Create(0, 0);
944     result->partial_result = GetPartialResultCount(/*is partial result*/ true);
945   }
946 
947   return result;
948 }
949 
InitializeResult(uint32_t pipeline_id,uint32_t frame_number)950 std::unique_ptr<HwlPipelineResult> EmulatedRequestState::InitializeResult(
951     uint32_t pipeline_id, uint32_t frame_number) {
952   auto& info = *device_info_;
953   std::lock_guard<std::mutex> lock(request_state_mutex_);
954   auto result = std::make_unique<HwlPipelineResult>();
955   result->camera_id = camera_id_;
956   result->pipeline_id = pipeline_id;
957   result->frame_number = frame_number;
958   result->result_metadata = HalCameraMetadata::Clone(request_settings_.get());
959   result->partial_result = GetPartialResultCount(/*is partial result*/ false);
960 
961   // Results supported on all emulated devices
962   result->result_metadata->Set(ANDROID_REQUEST_PIPELINE_DEPTH,
963                                &info.max_pipeline_depth_, 1);
964   result->result_metadata->Set(ANDROID_CONTROL_MODE, &info.control_mode_, 1);
965   result->result_metadata->Set(ANDROID_SENSOR_PIXEL_MODE,
966                                &info.sensor_pixel_mode_, 1);
967 
968   result->result_metadata->Set(ANDROID_CONTROL_AF_MODE, &info.af_mode_, 1);
969   result->result_metadata->Set(ANDROID_CONTROL_AF_STATE, &info.af_state_, 1);
970   result->result_metadata->Set(ANDROID_CONTROL_AWB_MODE, &info.awb_mode_, 1);
971   result->result_metadata->Set(ANDROID_CONTROL_AWB_STATE, &info.awb_state_, 1);
972   result->result_metadata->Set(ANDROID_CONTROL_AE_MODE, &info.ae_mode_, 1);
973 
974   if (info.ae_mode_ == ANDROID_CONTROL_AE_MODE_OFF) {
975     // AE Priority mode should not work with AE mode OFF
976     uint8_t ae_priority_mode_off = ANDROID_CONTROL_AE_PRIORITY_MODE_OFF;
977     result->result_metadata->Set(ANDROID_CONTROL_AE_PRIORITY_MODE,
978             &ae_priority_mode_off, 1);
979   } else {
980     result->result_metadata->Set(ANDROID_CONTROL_AE_PRIORITY_MODE,
981             &info.ae_priority_mode_, 1);
982   }
983 
984   result->result_metadata->Set(ANDROID_CONTROL_AE_STATE, &info.ae_state_, 1);
985   // If the overriding frame number isn't larger than current frame number,
986   // use 0.
987   int32_t settings_override = info.settings_override_;
988   uint32_t overriding_frame_number = settings_overriding_frame_number_;
989   if (overriding_frame_number <= frame_number) {
990     overriding_frame_number = frame_number;
991     settings_override = ANDROID_CONTROL_SETTINGS_OVERRIDE_OFF;
992   }
993   result->result_metadata->Set(ANDROID_CONTROL_SETTINGS_OVERRIDE,
994                                &settings_override, 1);
995   result->result_metadata->Set(ANDROID_CONTROL_SETTINGS_OVERRIDING_FRAME_NUMBER,
996                                (int32_t*)&overriding_frame_number, 1);
997   result->result_metadata->Set(ANDROID_CONTROL_AUTOFRAMING, &info.autoframing_,
998                                1);
999   uint8_t autoframing_state = ANDROID_CONTROL_AUTOFRAMING_STATE_INACTIVE;
1000   if (info.autoframing_ == ANDROID_CONTROL_AUTOFRAMING_ON) {
1001     autoframing_state = ANDROID_CONTROL_AUTOFRAMING_STATE_CONVERGED;
1002   }
1003   result->result_metadata->Set(ANDROID_CONTROL_AUTOFRAMING_STATE,
1004                                &autoframing_state, 1);
1005 
1006   int32_t fps_range[] = {info.ae_target_fps_.min_fps,
1007                          info.ae_target_fps_.max_fps};
1008   result->result_metadata->Set(ANDROID_CONTROL_AE_TARGET_FPS_RANGE, fps_range,
1009                                ARRAY_SIZE(fps_range));
1010   result->result_metadata->Set(ANDROID_FLASH_STATE, &info.flash_state_, 1);
1011   result->result_metadata->Set(ANDROID_LENS_STATE, &info.lens_state_, 1);
1012 
1013   // Results depending on device capability and features
1014   if (info.is_backward_compatible_) {
1015     result->result_metadata->Set(ANDROID_CONTROL_AE_PRECAPTURE_TRIGGER,
1016                                  &info.ae_trigger_, 1);
1017     result->result_metadata->Set(ANDROID_CONTROL_AF_TRIGGER, &info.af_trigger_,
1018                                  1);
1019     uint8_t vstab_mode = ANDROID_CONTROL_VIDEO_STABILIZATION_MODE_OFF;
1020     result->result_metadata->Set(ANDROID_CONTROL_VIDEO_STABILIZATION_MODE,
1021                                  &vstab_mode, 1);
1022     if (info.exposure_compensation_supported_) {
1023       result->result_metadata->Set(ANDROID_CONTROL_AE_EXPOSURE_COMPENSATION,
1024                                    &info.exposure_compensation_, 1);
1025     }
1026   }
1027   if (info.ae_lock_available_ && info.report_ae_lock_) {
1028     result->result_metadata->Set(ANDROID_CONTROL_AE_LOCK, &info.ae_lock_, 1);
1029   }
1030   if (info.awb_lock_available_ && info.report_awb_lock_) {
1031     result->result_metadata->Set(ANDROID_CONTROL_AWB_LOCK, &info.awb_lock_, 1);
1032   }
1033   if (info.scenes_supported_) {
1034     result->result_metadata->Set(ANDROID_CONTROL_SCENE_MODE, &info.scene_mode_,
1035                                  1);
1036   }
1037   if (info.max_ae_regions_ > 0) {
1038     result->result_metadata->Set(ANDROID_CONTROL_AE_REGIONS,
1039                                  info.ae_metering_region_,
1040                                  ARRAY_SIZE(info.ae_metering_region_));
1041   }
1042   if (info.max_awb_regions_ > 0) {
1043     result->result_metadata->Set(ANDROID_CONTROL_AWB_REGIONS,
1044                                  info.awb_metering_region_,
1045                                  ARRAY_SIZE(info.awb_metering_region_));
1046   }
1047   if (info.max_af_regions_ > 0) {
1048     result->result_metadata->Set(ANDROID_CONTROL_AF_REGIONS,
1049                                  info.af_metering_region_,
1050                                  ARRAY_SIZE(info.af_metering_region_));
1051   }
1052   if (info.report_exposure_time_) {
1053     result->result_metadata->Set(ANDROID_SENSOR_EXPOSURE_TIME,
1054                                  &info.sensor_exposure_time_, 1);
1055   } else {
1056     result->result_metadata->Erase(ANDROID_SENSOR_EXPOSURE_TIME);
1057   }
1058   if (info.report_frame_duration_) {
1059     result->result_metadata->Set(ANDROID_SENSOR_FRAME_DURATION,
1060                                  &info.sensor_frame_duration_, 1);
1061   } else {
1062     result->result_metadata->Erase(ANDROID_SENSOR_FRAME_DURATION);
1063   }
1064   if (info.report_sensitivity_) {
1065     result->result_metadata->Set(ANDROID_SENSOR_SENSITIVITY,
1066                                  &info.sensor_sensitivity_, 1);
1067   } else {
1068     result->result_metadata->Erase(ANDROID_SENSOR_SENSITIVITY);
1069   }
1070   if (info.report_rolling_shutter_skew_) {
1071     result->result_metadata->Set(
1072         ANDROID_SENSOR_ROLLING_SHUTTER_SKEW,
1073         &EmulatedSensor::kSupportedFrameDurationRange[0], 1);
1074   }
1075   if (info.report_post_raw_boost_) {
1076     result->result_metadata->Set(ANDROID_CONTROL_POST_RAW_SENSITIVITY_BOOST,
1077                                  &info.post_raw_boost_, 1);
1078   }
1079   if (info.report_focus_distance_) {
1080     result->result_metadata->Set(ANDROID_LENS_FOCUS_DISTANCE,
1081                                  &info.focus_distance_, 1);
1082   }
1083   if (info.report_focus_range_) {
1084     float focus_range[2] = {};
1085     focus_range[0] = info.focus_distance_;
1086     result->result_metadata->Set(ANDROID_LENS_FOCUS_RANGE, focus_range, ARRAY_SIZE(focus_range));
1087   }
1088   if (info.report_filter_density_) {
1089     result->result_metadata->Set(ANDROID_LENS_FILTER_DENSITY,
1090                                  &info.filter_density_, 1);
1091   }
1092   if (info.report_ois_mode_) {
1093     result->result_metadata->Set(ANDROID_LENS_OPTICAL_STABILIZATION_MODE,
1094                                  &info.ois_mode_, 1);
1095   }
1096   if (info.report_pose_rotation_) {
1097     result->result_metadata->Set(ANDROID_LENS_POSE_ROTATION, info.pose_rotation_,
1098                                  ARRAY_SIZE(info.pose_rotation_));
1099   }
1100   if (info.report_pose_translation_) {
1101     result->result_metadata->Set(ANDROID_LENS_POSE_TRANSLATION,
1102                                  info.pose_translation_,
1103                                  ARRAY_SIZE(info.pose_translation_));
1104   }
1105   if (info.report_intrinsic_calibration_) {
1106     result->result_metadata->Set(ANDROID_LENS_INTRINSIC_CALIBRATION,
1107                                  info.intrinsic_calibration_,
1108                                  ARRAY_SIZE(info.intrinsic_calibration_));
1109   }
1110   if (info.report_lens_intrinsics_samples_) {
1111     result->result_metadata->Set(ANDROID_STATISTICS_LENS_INTRINSIC_SAMPLES,
1112                                  info.intrinsic_calibration_,
1113                                  ARRAY_SIZE(info.intrinsic_calibration_));
1114   }
1115   if (info.report_distortion_) {
1116     result->result_metadata->Set(ANDROID_LENS_DISTORTION, info.distortion_,
1117                                  ARRAY_SIZE(info.distortion_));
1118   }
1119   if (info.report_black_level_lock_) {
1120     result->result_metadata->Set(ANDROID_BLACK_LEVEL_LOCK,
1121                                  &info.black_level_lock_, 1);
1122   }
1123   if (info.report_scene_flicker_) {
1124     result->result_metadata->Set(ANDROID_STATISTICS_SCENE_FLICKER,
1125                                  &info.current_scene_flicker_, 1);
1126   }
1127   if (info.zoom_ratio_supported_) {
1128     result->result_metadata->Set(ANDROID_CONTROL_ZOOM_RATIO, &info.zoom_ratio_,
1129                                  1);
1130     int32_t* chosen_crop_region = info.scaler_crop_region_default_;
1131     if (info.sensor_pixel_mode_ == ANDROID_SENSOR_PIXEL_MODE_MAXIMUM_RESOLUTION) {
1132       chosen_crop_region = info.scaler_crop_region_max_resolution_;
1133     }
1134     result->result_metadata->Set(ANDROID_SCALER_CROP_REGION, chosen_crop_region,
1135                                  ARRAY_SIZE(info.scaler_crop_region_default_));
1136     if (info.report_active_sensor_crop_) {
1137       int32_t active_crop_region[4];
1138       // width
1139       active_crop_region[2] =
1140           (info.scaler_crop_region_default_[2] / info.zoom_ratio_);
1141       // height
1142       active_crop_region[3] =
1143           (info.scaler_crop_region_default_[3] / info.zoom_ratio_);
1144       // left
1145       active_crop_region[0] =
1146           (info.scaler_crop_region_default_[2] - active_crop_region[2]) / 2;
1147       // top
1148       active_crop_region[1] =
1149           (info.scaler_crop_region_default_[3] - active_crop_region[3]) / 2;
1150       result->result_metadata->Set(
1151           ANDROID_LOGICAL_MULTI_CAMERA_ACTIVE_PHYSICAL_SENSOR_CROP_REGION,
1152           active_crop_region, ARRAY_SIZE(info.scaler_crop_region_default_));
1153     }
1154   }
1155   if (info.report_extended_scene_mode_) {
1156     result->result_metadata->Set(ANDROID_CONTROL_EXTENDED_SCENE_MODE,
1157                                  &info.extended_scene_mode_, 1);
1158   }
1159   return result;
1160 }
1161 
Initialize(std::unique_ptr<EmulatedCameraDeviceInfo> deviceInfo)1162 status_t EmulatedRequestState::Initialize(
1163     std::unique_ptr<EmulatedCameraDeviceInfo> deviceInfo) {
1164   std::lock_guard<std::mutex> lock(request_state_mutex_);
1165   device_info_ = std::move(deviceInfo);
1166 
1167   return OK;
1168 }
1169 
GetDefaultRequest(RequestTemplate type,std::unique_ptr<HalCameraMetadata> * default_settings)1170 status_t EmulatedRequestState::GetDefaultRequest(
1171     RequestTemplate type, std::unique_ptr<HalCameraMetadata>* default_settings) {
1172   if (default_settings == nullptr) {
1173     ALOGE("%s default_settings is nullptr", __FUNCTION__);
1174     return BAD_VALUE;
1175   }
1176 
1177   std::lock_guard<std::mutex> lock(request_state_mutex_);
1178   auto idx = static_cast<size_t>(type);
1179   if (idx >= kTemplateCount) {
1180     ALOGE("%s: Unexpected request type: %d", __FUNCTION__, type);
1181     return BAD_VALUE;
1182   }
1183 
1184   if (device_info_->default_requests_[idx].get() == nullptr) {
1185     ALOGE("%s: Unsupported request type: %d", __FUNCTION__, type);
1186     return BAD_VALUE;
1187   }
1188 
1189   *default_settings = HalCameraMetadata::Clone(
1190       device_info_->default_requests_[idx]->GetRawCameraMetadata());
1191 
1192   return OK;
1193 }
1194 
1195 }  // namespace android
1196