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_CAMERA_DEVICE_SESSION_HWL_IMPL_H
18 #define EMULATOR_CAMERA_HAL_HWL_CAMERA_DEVICE_SESSION_HWL_IMPL_H
19 
20 #include <camera_device_session_hwl.h>
21 
22 #include <memory>
23 #include <set>
24 
25 #include "EmulatedCameraDeviceHWLImpl.h"
26 #include "EmulatedRequestProcessor.h"
27 #include "EmulatedTorchState.h"
28 #include "multicam_coordinator_hwl.h"
29 #include "utils/StreamConfigurationMap.h"
30 
31 namespace android {
32 
33 using google_camera_hal::CameraDeviceHwl;
34 using google_camera_hal::CameraDeviceSessionHwl;
35 using google_camera_hal::CaptureRequest;
36 using google_camera_hal::CaptureResult;
37 using google_camera_hal::Dimension;
38 using google_camera_hal::HalStream;
39 using google_camera_hal::HwlOfflinePipelineRole;
40 using google_camera_hal::HwlPipelineCallback;
41 using google_camera_hal::HwlPipelineRequest;
42 using google_camera_hal::HwlSessionCallback;
43 using google_camera_hal::IMulticamCoordinatorHwl;
44 using google_camera_hal::RequestTemplate;
45 using google_camera_hal::SessionDataKey;
46 using google_camera_hal::Stream;
47 using google_camera_hal::StreamConfiguration;
48 using google_camera_hal::ZoomRatioMapperHwl;
49 
50 class EmulatedCameraZoomRatioMapperHwlImpl : public ZoomRatioMapperHwl {
51  public:
52   EmulatedCameraZoomRatioMapperHwlImpl(
53       const std::unordered_map<uint32_t, std::pair<Dimension, Dimension>>& dims);
54   virtual ~EmulatedCameraZoomRatioMapperHwlImpl() = default;
55 
56   // Limit zoom ratio if concurrent mode is on
LimitZoomRatioIfConcurrent(float *)57   virtual void LimitZoomRatioIfConcurrent(float*) const override{};
58 
59   // Get the array dimensions to be used for this capture request / result
60   virtual bool GetActiveArrayDimensionToBeUsed(
61       uint32_t camera_id, const HalCameraMetadata* settings,
62       Dimension* active_array_dimension) const override;
63   // Apply zoom ratio to capture request
UpdateCaptureRequest(CaptureRequest *)64   virtual void UpdateCaptureRequest(CaptureRequest*) override{};
65 
66   // Apply zoom ratio to capture result
UpdateCaptureResult(CaptureResult *)67   virtual void UpdateCaptureResult(CaptureResult*) override{};
68 
69   static std::unique_ptr<EmulatedCameraZoomRatioMapperHwlImpl> Create(
70       const std::unordered_map<uint32_t, std::pair<Dimension, Dimension>>& dims);
71 
72  private:
73   // camera id -> {max res dimension (array size), default dimension }
74   std::unordered_map<uint32_t, std::pair<Dimension, Dimension>>
75       camera_ids_to_dimensions_;
76 };
77 
78 // Implementation of CameraDeviceSessionHwl interface
79 class EmulatedCameraDeviceSessionHwlImpl : public CameraDeviceSessionHwl {
80  public:
81   static std::unique_ptr<EmulatedCameraDeviceSessionHwlImpl> Create(
82       uint32_t camera_id, std::unique_ptr<EmulatedCameraDeviceInfo> device_info,
83       PhysicalDeviceMapPtr physical_devices,
84       std::shared_ptr<EmulatedTorchState> torch_state);
85 
86   virtual ~EmulatedCameraDeviceSessionHwlImpl();
87 
88   // Override functions in CameraDeviceSessionHwl
89   status_t ConstructDefaultRequestSettings(
90       RequestTemplate type,
91       std::unique_ptr<HalCameraMetadata>* default_settings) override;
92 
PrepareConfigureStreams(const StreamConfiguration &)93   status_t PrepareConfigureStreams(
94       const StreamConfiguration& /*request_config*/) override {
95     return OK;
96   }  // Noop for now
97 
98   status_t ConfigurePipeline(uint32_t physical_camera_id,
99                              HwlPipelineCallback hwl_pipeline_callback,
100                              const StreamConfiguration& request_config,
101                              const StreamConfiguration& overall_config,
102                              uint32_t* pipeline_id) override;
103 
104   status_t BuildPipelines() override;
105 
106   std::set<int32_t> GetHalBufferManagedStreams(
107       const StreamConfiguration& config) override;
108 
PreparePipeline(uint32_t,uint32_t)109   status_t PreparePipeline(uint32_t /*pipeline_id*/,
110                            uint32_t /*frame_number*/) override {
111     return OK;
112   }  // Noop for now
113 
GetRequiredIntputStreams(const StreamConfiguration &,HwlOfflinePipelineRole,std::vector<Stream> *)114   status_t GetRequiredIntputStreams(const StreamConfiguration& /*overall_config*/,
115                                     HwlOfflinePipelineRole /*pipeline_role*/,
116                                     std::vector<Stream>* /*streams*/) override {
117     // N/A
118     return INVALID_OPERATION;
119   }
120 
121   status_t GetConfiguredHalStream(
122       uint32_t pipeline_id, std::vector<HalStream>* hal_streams) const override;
123 
124   void DestroyPipelines() override;
125 
126   status_t SubmitRequests(uint32_t frame_number,
127                           std::vector<HwlPipelineRequest>& requests) override;
128 
129   status_t Flush() override;
130 
131   uint32_t GetCameraId() const override;
132 
133   std::vector<uint32_t> GetPhysicalCameraIds() const override;
134 
135   status_t GetCameraCharacteristics(
136       std::unique_ptr<HalCameraMetadata>* characteristics) const override;
137 
138   status_t GetPhysicalCameraCharacteristics(
139       uint32_t physical_camera_id,
140       std::unique_ptr<HalCameraMetadata>* characteristics) const override;
141 
SetSessionData(SessionDataKey,void *)142   status_t SetSessionData(SessionDataKey /*key*/
143                                     ,
144                                     void* /*value*/) override {
145     return OK;
146   }  // Noop for now
147 
GetSessionData(SessionDataKey,void **)148   status_t GetSessionData(SessionDataKey /*key*/,
149                           void** /*value*/) const override {
150     return OK;
151   }  // Noop for now
152 
153   void SetSessionCallback(
154       const HwlSessionCallback& hwl_session_callback) override;
155 
FilterResultMetadata(HalCameraMetadata *)156   status_t FilterResultMetadata(HalCameraMetadata* /*metadata*/) const override {
157     return OK;
158   }  // Noop for now
159 
CreateMulticamCoordinatorHwl()160   std::unique_ptr<IMulticamCoordinatorHwl> CreateMulticamCoordinatorHwl()
161       override {
162     return nullptr;
163   }
164 
IsReconfigurationRequired(const HalCameraMetadata *,const HalCameraMetadata *,bool * reconfiguration_required)165   status_t IsReconfigurationRequired(
166       const HalCameraMetadata* /*old_session*/,
167       const HalCameraMetadata* /*new_session*/,
168       bool* reconfiguration_required) const override {
169     if (reconfiguration_required == nullptr) {
170       return BAD_VALUE;
171     }
172     *reconfiguration_required = true;
173     return OK;
174   }
175 
GetZoomRatioMapperHwl()176   std::unique_ptr<google_camera_hal::ZoomRatioMapperHwl> GetZoomRatioMapperHwl()
177       override {
178     return std::move(zoom_ratio_mapper_hwl_impl_);
179   }
180   // End override functions in CameraDeviceSessionHwl
181 
182  private:
183   status_t Initialize(uint32_t camera_id,
184                       std::unique_ptr<EmulatedCameraDeviceInfo> device_info);
185   status_t InitializeRequestProcessor();
186 
187   status_t CheckOutputFormatsForInput(
188       const HwlPipelineRequest& request,
189       const std::unordered_map<uint32_t, EmulatedStream>& streams,
190       const std::unique_ptr<StreamConfigurationMap>& stream_configuration_map,
191       android_pixel_format_t input_format);
192 
EmulatedCameraDeviceSessionHwlImpl(PhysicalDeviceMapPtr physical_devices,std::shared_ptr<EmulatedTorchState> torch_state)193   EmulatedCameraDeviceSessionHwlImpl(
194       PhysicalDeviceMapPtr physical_devices,
195       std::shared_ptr<EmulatedTorchState> torch_state)
196       : torch_state_(torch_state),
197         physical_device_map_(std::move(physical_devices)) {
198   }
199 
200   uint8_t max_pipeline_depth_ = 0;
201 
202   // Protects the API entry points
203   mutable std::mutex api_mutex_;
204   uint32_t camera_id_ = 0;
205   bool error_state_ = false;
206   bool pipelines_built_ = false;
207   bool has_raw_stream_ = false;
208   bool supports_session_hal_buf_manager_ = false;
209   std::unique_ptr<EmulatedCameraDeviceInfo> device_info_;
210   std::vector<EmulatedPipeline> pipelines_;
211   std::shared_ptr<EmulatedRequestProcessor> request_processor_;
212   std::unique_ptr<StreamConfigurationMap> stream_configuration_map_;
213   PhysicalStreamConfigurationMap physical_stream_configuration_map_;
214   PhysicalStreamConfigurationMap physical_stream_configuration_map_max_resolution_;
215   std::unique_ptr<StreamConfigurationMap> stream_configuration_map_max_resolution_;
216   SensorCharacteristics sensor_chars_;
217   std::shared_ptr<EmulatedTorchState> torch_state_;
218   PhysicalDeviceMapPtr physical_device_map_;
219   LogicalCharacteristics logical_chars_;
220   HwlSessionCallback session_callback_;
221   DynamicStreamIdMapType dynamic_stream_id_map_;
222   std::unique_ptr<EmulatedCameraZoomRatioMapperHwlImpl> zoom_ratio_mapper_hwl_impl_;
223 };
224 
225 }  // namespace android
226 
227 #endif
228