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