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