1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef EMULATOR_CAMERA_HAL_HWL_REQUEST_STATE_H
18 #define EMULATOR_CAMERA_HAL_HWL_REQUEST_STATE_H
19 
20 #include <mutex>
21 #include <unordered_map>
22 
23 #include "EmulatedCameraDeviceInfo.h"
24 #include "EmulatedSensor.h"
25 #include "hwl_types.h"
26 
27 namespace android {
28 
29 using google_camera_hal::HalCameraMetadata;
30 using google_camera_hal::HalStream;
31 using google_camera_hal::HwlPipelineCallback;
32 using google_camera_hal::HwlPipelineRequest;
33 using google_camera_hal::kTemplateCount;
34 using google_camera_hal::RequestTemplate;
35 
36 struct PendingRequest;
37 
38 class EmulatedRequestState {
39  public:
EmulatedRequestState(uint32_t camera_id)40   EmulatedRequestState(uint32_t camera_id) : camera_id_(camera_id) {
41   }
~EmulatedRequestState()42   virtual ~EmulatedRequestState() {
43   }
44 
45   status_t Initialize(std::unique_ptr<EmulatedCameraDeviceInfo> static_meta);
46 
47   status_t GetDefaultRequest(
48       RequestTemplate type,
49       std::unique_ptr<HalCameraMetadata>* default_settings /*out*/);
50 
51   std::unique_ptr<HwlPipelineResult> InitializeResult(uint32_t pipeline_id,
52                                                       uint32_t frame_number);
53   std::unique_ptr<HwlPipelineResult> InitializePartialResult(
54       uint32_t pipeline_id, uint32_t frame_number);
55 
56   status_t InitializeSensorSettings(
57       std::unique_ptr<HalCameraMetadata> request_settings,
58       uint32_t override_frame_number,
59       EmulatedSensor::SensorSettings* sensor_settings /*out*/);
60 
61   uint32_t GetPartialResultCount(bool is_partial_result);
62 
63  private:
64   status_t ProcessAE();
65   status_t ProcessAF();
66   status_t ProcessAWB();
67   status_t DoFakeAE();
68   status_t CompensateAE();
69   status_t Update3AMeteringRegion(uint32_t tag,
70                                   const HalCameraMetadata& settings,
71                                   int32_t* region /*out*/);
72 
73   std::mutex request_state_mutex_;
74   std::unique_ptr<HalCameraMetadata> request_settings_;
75 
76   // Supported capabilities and features
77   std::unique_ptr<EmulatedCameraDeviceInfo> device_info_;
78 
79   size_t ae_frame_counter_ = 0;
80   const size_t kAEPrecaptureMinFrames = 10;
81   // Fake AE related constants
82   const float kExposureTrackRate = .2f;  // This is the rate at which the fake
83                                          // AE will reach the calculated target
84   const size_t kStableAeMaxFrames =
85       100;  // The number of frames the fake AE will stay in converged state
86   // After fake AE switches to state searching the exposure
87   // time will wander randomly in region defined by min/max below.
88   const float kExposureWanderMin = -2;
89   const float kExposureWanderMax = 1;
90   const uint32_t kAETargetThreshold =
91       10;  // Defines a threshold for reaching the AE target
92   nsecs_t ae_target_exposure_time_ = EmulatedSensor::kDefaultExposureTime;
93   nsecs_t current_exposure_time_ = EmulatedSensor::kDefaultExposureTime;
94   bool af_mode_changed_ = false;
95   uint32_t settings_overriding_frame_number_ = 0;
96 
97   unsigned int rand_seed_ = 1;
98 
99   uint32_t camera_id_;
100 
101   EmulatedRequestState(const EmulatedRequestState&) = delete;
102   EmulatedRequestState& operator=(const EmulatedRequestState&) = delete;
103 };
104 
105 }  // namespace android
106 
107 #endif  // EMULATOR_CAMERA_HAL_HWL_REQUEST_STATE_H
108