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 HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_RGBIRD_CAPTURE_SESSION_H_
18 #define HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_RGBIRD_CAPTURE_SESSION_H_
19 
20 #include "camera_buffer_allocator_hwl.h"
21 #include "camera_device_session_hwl.h"
22 #include "capture_session.h"
23 #include "depth_process_block.h"
24 #include "hwl_types.h"
25 #include "result_dispatcher.h"
26 #include "result_processor.h"
27 #include "rgbird_depth_result_processor.h"
28 #include "rgbird_result_request_processor.h"
29 #include "rgbird_rt_request_processor.h"
30 
31 namespace android {
32 namespace google_camera_hal {
33 
34 // RgbirdCaptureSession implements a CaptureSession that contains a single
35 // process chain that consists of
36 //
37 //   RgbirdRtRequestProcessor -> MultiCameraRtProcessBlock ->
38 //     RgbirdResultRequestProcessor -> DepthProcessBlock ->
39 //     BasicResultProcessor
40 //
41 // It only supports a camera device session that consists of one RGB and two
42 // IR cameras.
43 class RgbirdCaptureSession : public CaptureSession {
44  public:
45   // Return if the device session HWL and stream configuration are supported.
46   static bool IsStreamConfigurationSupported(
47       CameraDeviceSessionHwl* device_session_hwl,
48       const StreamConfiguration& stream_config);
49 
50   // Create a RgbirdCaptureSession.
51   //
52   // device_session_hwl is owned by the caller and must be valid during the
53   // lifetime of RgbirdCaptureSession.
54   // stream_config is the stream configuration.
55   // process_capture_result is the callback function to notify results.
56   // notify is the callback function to notify messages.
57   // hal_configured_streams will be filled with HAL configured streams.
58   // camera_allocator_hwl is owned by the caller and must be valid during the
59   // lifetime of RgbirdCaptureSession
60   static std::unique_ptr<CaptureSession> Create(
61       CameraDeviceSessionHwl* device_session_hwl,
62       const StreamConfiguration& stream_config,
63       ProcessCaptureResultFunc process_capture_result, NotifyFunc notify,
64       HwlSessionCallback session_callback,
65       std::vector<HalStream>* hal_configured_streams,
66       CameraBufferAllocatorHwl* camera_allocator_hwl = nullptr);
67 
68   virtual ~RgbirdCaptureSession();
69 
70   // Override functions in CaptureSession start.
71   status_t ProcessRequest(const CaptureRequest& request) override;
72 
73   status_t Flush() override;
74   // Override functions in CaptureSession end.
75 
76  protected:
77   RgbirdCaptureSession() = default;
78 
79  private:
80   static constexpr int32_t kInvalidStreamId = -1;
81   static const uint32_t kRgbRawBufferCount = 16;
82   // Min required buffer count of internal raw stream.
83   static const uint32_t kRgbMinRawBufferCount = 12;
84   static constexpr uint32_t kPartialResult = 1;
85   static const android_pixel_format_t kHdrplusRawFormat = HAL_PIXEL_FORMAT_RAW10;
86   static const uint32_t kDefaultInternalBufferCount = 8;
87 
88   status_t Initialize(CameraDeviceSessionHwl* device_session_hwl,
89                       const StreamConfiguration& stream_config,
90                       ProcessCaptureResultFunc process_capture_result,
91                       NotifyFunc notify,
92                       HwlRequestBuffersFunc request_stream_buffers,
93                       std::vector<HalStream>* hal_configured_streams);
94 
95   // Create a process chain that contains a realtime process block and a
96   // depth process block.
97   status_t CreateProcessChain(const StreamConfiguration& stream_config,
98                               ProcessCaptureResultFunc process_capture_result,
99                               NotifyFunc notify,
100                               std::vector<HalStream>* hal_configured_streams);
101 
102   // Check if all streams in stream_config are also in
103   // process_block_stream_config.
104   bool AreAllStreamsConfigured(
105       const StreamConfiguration& stream_config,
106       const StreamConfiguration& process_block_stream_config) const;
107 
108   // Setup realtime process chain
109   status_t SetupRealtimeProcessChain(
110       const StreamConfiguration& stream_config,
111       ProcessCaptureResultFunc process_capture_result, NotifyFunc notify,
112       std::unique_ptr<ProcessBlock>* realtime_process_block,
113       std::unique_ptr<RgbirdResultRequestProcessor>* realtime_result_processor,
114       std::unique_ptr<ProcessBlock>* depth_process_block,
115       std::unique_ptr<ResultProcessor>* depth_result_processor);
116 
117   // Setup hdrplus process chain
118   status_t SetupHdrplusProcessChain(
119       const StreamConfiguration& stream_config,
120       ProcessCaptureResultFunc process_capture_result, NotifyFunc notify,
121       std::unique_ptr<ProcessBlock>* hdrplus_process_block,
122       std::unique_ptr<ResultProcessor>* hdrplus_result_processor);
123 
124   // Configure streams for the process chain.
125   status_t ConfigureStreams(const StreamConfiguration& stream_config,
126                             RequestProcessor* request_processor,
127                             ProcessBlock* process_block,
128                             StreamConfiguration* process_block_stream_config);
129 
130   // Build pipelines and return HAL configured streams.
131   // Allocate internal raw buffer
132   status_t BuildPipelines(const StreamConfiguration& stream_config,
133                           ProcessBlock* realtime_process_block,
134                           ProcessBlock* depth_process_block,
135                           ProcessBlock* hdrplus_process_block,
136                           std::vector<HalStream>* hal_configured_streams);
137 
138   // Connect the process chain.
139   status_t ConnectProcessChain(RequestProcessor* request_processor,
140                                std::unique_ptr<ProcessBlock> process_block,
141                                std::unique_ptr<ResultProcessor> result_processor);
142 
143   // Invoked when receiving a result from result processor.
144   void ProcessCaptureResult(std::unique_ptr<CaptureResult> result);
145 
146   // Invoked when reciving a message from result processor.
147   void NotifyHalMessage(const NotifyMessage& message);
148 
149   // Get internal rgb raw stream id from request processor.
150   status_t ConfigureHdrplusRawStreamId(
151       const StreamConfiguration& process_block_stream_config);
152 
153   // Get internal RGB YUV stream id and IR RAW streams from request processor in
154   // case depth is configured.
155   status_t SetDepthInternalStreamId(
156       const StreamConfiguration& process_block_stream_config,
157       const StreamConfiguration& stream_config);
158 
159   // Combine usage of realtime and HDR+ hal stream
160   // And allocate internal rgb raw stream buffers
161   status_t ConfigureHdrplusUsageAndBuffers(
162       std::vector<HalStream>* hal_configured_streams,
163       std::vector<HalStream>* hdrplus_hal_configured_streams);
164 
165   // Allocate buffers for internal stream buffer managers
166   status_t AllocateInternalBuffers(
167       const StreamConfiguration& framework_stream_config,
168       std::vector<HalStream>* hal_configured_streams,
169       ProcessBlock* hdrplus_process_block);
170 
171   // Initialize physical camera ids from the camera characteristics
172   status_t InitializeCameraIds(CameraDeviceSessionHwl* device_session_hwl);
173 
174   // Remove internal streams from the hal configured stream list
175   status_t PurgeHalConfiguredStream(
176       const StreamConfiguration& stream_config,
177       std::vector<HalStream>* hal_configured_streams);
178 
179   // Determine if a depth process block is needed the capture session
180   bool NeedDepthProcessBlock() const;
181 
182   // Create stream config for the Depth process chain segment
183   // Keep all output stream from stream_config, change rt internal streams added
184   // for depth processing as input streams.
185   status_t MakeDepthStreamConfig(
186       const StreamConfiguration& rt_process_block_stream_config,
187       const StreamConfiguration& stream_config,
188       StreamConfiguration* depth_stream_config);
189 
190   // Create the segment of chain that contains a depth process block
191   status_t CreateDepthChainSegment(
192       std::unique_ptr<DepthProcessBlock>* depth_process_block,
193       std::unique_ptr<RgbirdDepthResultProcessor>* depth_result_processor,
194       RgbirdResultRequestProcessor* rt_result_processor,
195       const StreamConfiguration& overall_config,
196       const StreamConfiguration& stream_config,
197       StreamConfiguration* depth_block_stream_config);
198 
199   // Setup the offline segment connecting to the realtime process chain
200   status_t SetupDepthChainSegment(
201       const StreamConfiguration& stream_config,
202       RgbirdResultRequestProcessor* realtime_result_processor,
203       std::unique_ptr<ProcessBlock>* depth_process_block,
204       std::unique_ptr<ResultProcessor>* depth_result_processor,
205       StreamConfiguration* rt_process_block_stream_config);
206 
207   // device_session_hwl_ is owned by the client.
208   CameraDeviceSessionHwl* device_session_hwl_ = nullptr;
209   std::unique_ptr<InternalStreamManager> internal_stream_manager_;
210 
211   std::unique_ptr<RgbirdRtRequestProcessor> rt_request_processor_;
212 
213   std::unique_ptr<RequestProcessor> hdrplus_request_processor_;
214 
215   std::unique_ptr<ResultDispatcher> result_dispatcher_;
216 
217   std::mutex callback_lock_;
218   // The following callbacks must be protected by callback_lock_.
219   ProcessCaptureResultFunc process_capture_result_;
220   NotifyFunc notify_;
221   HwlRequestBuffersFunc request_stream_buffers_;
222 
223   // For error notify to framework directly
224   NotifyFunc device_session_notify_;
225   int32_t rgb_raw_stream_id_ = kInvalidStreamId;
226   bool is_hdrplus_supported_ = false;
227 
228   // Whether the stream configuration has depth stream
229   bool has_depth_stream_ = false;
230   // Internal YUV stream id if there is a depth stream configured
231   int32_t rgb_internal_yuv_stream_id_ = kInvalidStreamId;
232   // Internal IR source stream id
233   int32_t ir1_internal_raw_stream_id_ = kInvalidStreamId;
234   // Internal IR target stream id
235   int32_t ir2_internal_raw_stream_id_ = kInvalidStreamId;
236 
237   // Camera ids parsed from the characteristics
238   uint32_t rgb_camera_id_ = 0;
239   // Ir1 generates the src buffer for depth
240   uint32_t ir1_camera_id_ = 0;
241   // Ir2 generates the tar buffer for depth
242   uint32_t ir2_camera_id_ = 0;
243 
244   // TODO(b/128633958): remove this after FLL syncing is verified
245   bool force_internal_stream_ = false;
246   // Use this stream id to check the request is HDR+ compatible
247   int32_t hal_preview_stream_id_ = -1;
248 };
249 
250 }  // namespace google_camera_hal
251 }  // namespace android
252 
253 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_RGBIRD_CAPTURE_SESSION_H_
254