1 /*
2  * Copyright (C) 2021 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_ZSL_SNAPSHOT_CAPTURE_SESSION_H_
18 #define HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_ZSL_SNAPSHOT_CAPTURE_SESSION_H_
19 
20 #include <vector>
21 
22 #include "basic_result_processor.h"
23 #include "camera_buffer_allocator_hwl.h"
24 #include "camera_device_session_hwl.h"
25 #include "capture_session.h"
26 #include "capture_session_utils.h"
27 #include "capture_session_wrapper_process_block.h"
28 #include "hwl_types.h"
29 #include "process_block.h"
30 #include "realtime_zsl_request_processor.h"
31 #include "realtime_zsl_result_processor.h"
32 #include "realtime_zsl_result_request_processor.h"
33 #include "request_processor.h"
34 #include "result_processor.h"
35 #include "snapshot_request_processor.h"
36 #include "snapshot_result_processor.h"
37 #include "zsl_result_dispatcher.h"
38 
39 namespace android {
40 namespace google_camera_hal {
41 
42 class CameraDeviceSession;
43 
44 // ZslSnapshotCaptureSession implements a CaptureSession that contains two
45 // process chains
46 //
47 //  1.SnapshotRequestProcessor->SnapshotProcessBlock->SnapshotResultProcessor
48 //
49 //  2.RealtimeZslRequestProcessor->CaptureSessionWrapperProcessBlock->RealtimeZslResultRequestProcessor->DenoiseProcessBlock->BasicResultProcessor
50 //                                    ||  /\
51 //                                    \/  ||
52 //                             embedded capture session
53 class ZslSnapshotCaptureSession : public CaptureSession {
54  public:
55   // Return if the device session HWL and stream configuration are supported.
56   static bool IsStreamConfigurationSupported(
57       CameraDeviceSessionHwl* device_session_hwl,
58       const StreamConfiguration& stream_config);
59 
60   // Create a ZslSnapshotCaptureSession.
61   //
62   // device_session_hwl is owned by the caller and must be valid during the
63   // lifetime of ZslSnapshotCaptureSession.
64   // stream_config is the stream configuration.
65   // process_capture_result is the callback function to notify results.
66   // notify is the callback function to notify messages.
67   // hal_configured_streams will be filled with HAL configured streams.
68   // camera_allocator_hwl is owned by the caller and must be valid during the
69   // lifetime of ZslSnapshotCaptureSession.
70   static std::unique_ptr<CaptureSession> Create(
71       const StreamConfiguration& stream_config,
72       const std::vector<ExternalCaptureSessionFactory*>&
73           external_capture_session_entries,
74       const std::vector<CaptureSessionEntryFuncs>& capture_session_entries,
75       HwlSessionCallback hwl_session_callback,
76       CameraBufferAllocatorHwl* camera_buffer_allocator_hwl,
77       CameraDeviceSessionHwl* camera_device_session_hwl,
78       std::vector<HalStream>* hal_configured_streams,
79       ProcessCaptureResultFunc process_capture_result, NotifyFunc notify);
80 
81   virtual ~ZslSnapshotCaptureSession();
82 
83   // Override functions in CaptureSession start.
84   status_t ProcessRequest(const CaptureRequest& request) override;
85 
86   status_t Flush() override;
87   // Override functions in CaptureSession end.
88 
89   void RepeatingRequestEnd(int32_t frame_number,
90                            const std::vector<int32_t>& stream_ids) override;
91 
92  protected:
93   ZslSnapshotCaptureSession(
94       const std::vector<ExternalCaptureSessionFactory*>&
95           external_capture_session_entries,
96       const std::vector<CaptureSessionEntryFuncs>& capture_session_entries,
97       HwlSessionCallback hwl_session_callback,
98       CameraBufferAllocatorHwl* camera_buffer_allocator_hwl,
99       CameraDeviceSessionHwl* camera_device_session_hwl);
100 
101  private:
102   static constexpr uint32_t kPartialResult = 1;
103   static constexpr int kAdditionalBufferNumber = 3;
104 
105   status_t Initialize(CameraDeviceSessionHwl* device_session_hwl,
106                       const StreamConfiguration& stream_config,
107                       ProcessCaptureResultFunc process_capture_result,
108                       NotifyFunc notify,
109                       std::vector<HalStream>* hal_configured_streams);
110 
111   status_t SetupSnapshotProcessChain(
112       const StreamConfiguration& stream_config,
113       ProcessCaptureResultFunc process_capture_result, NotifyFunc notify);
114 
115   status_t SetupRealtimeProcessChain(
116       const StreamConfiguration& stream_config,
117       ProcessCaptureResultFunc process_capture_result, NotifyFunc notify);
118 
119   status_t ConfigureSnapshotStreams(const StreamConfiguration& stream_config);
120 
121   // Configure streams for request processor and process block.
122   status_t ConfigureStreams(const StreamConfiguration& stream_config,
123                             RequestProcessor* request_processor,
124                             ProcessBlock* process_block,
125                             ProcessCaptureResultFunc process_capture_result,
126                             NotifyFunc notify, int32_t& additiona_stream_id);
127 
128   // Build pipelines and return HAL configured streams.
129   status_t BuildPipelines(ProcessBlock* process_block,
130                           ProcessBlock* snapshot_process_block,
131                           std::vector<HalStream>* hal_configured_streams);
132 
133   status_t PurgeHalConfiguredStream(
134       const StreamConfiguration& stream_config,
135       std::vector<HalStream>* hal_configured_streams);
136 
137   std::unique_ptr<ProcessBlock> CreateSnapshotProcessBlock();
138   std::unique_ptr<ProcessBlock> CreateDenoiseProcessBlock();
139 
140   // Invoked when receiving a result from result processor.
141   void ProcessCaptureResult(std::unique_ptr<CaptureResult> result);
142 
143   // Invoked when receiving a message from result processor.
144   void NotifyHalMessage(const NotifyMessage& message);
145 
146   std::unique_ptr<InternalStreamManager> internal_stream_manager_;
147 
148   std::unique_ptr<RealtimeZslRequestProcessor> realtime_request_processor_;
149   RealtimeZslResultRequestProcessor* realtime_zsl_result_request_processor_ =
150       nullptr;
151   // CaptureSessionWrapperProcessBlock will be owned and released by
152   // RealtimeZslRequestProcessor.
153   CaptureSessionWrapperProcessBlock* realtime_process_block_ = nullptr;
154 
155   std::unique_ptr<SnapshotRequestProcessor> snapshot_request_processor_;
156   // SnapshotProcessBlock will be owned and released by
157   // SnapshotRequestProcessor.
158   ProcessBlock* snapshot_process_block_ = nullptr;
159   // SnapshotResultProcessor will be owned and released by SnapshotProcessBlock.
160   SnapshotResultProcessor* snapshot_result_processor_ = nullptr;
161 
162   BasicResultProcessor* basic_result_processor_ = nullptr;
163 
164   // Use this stream id to check the request is ZSL compatible
165   int32_t hal_preview_stream_id_ = -1;
166 
167   int32_t additional_stream_id_ = -1;
168 
169   std::unique_ptr<ZslResultDispatcher> result_dispatcher_;
170 
171   std::mutex callback_lock_;
172   // The following callbacks must be protected by callback_lock_.
173   ProcessCaptureResultFunc process_capture_result_;
174   NotifyFunc notify_;
175   // For error notify to framework directly
176   NotifyFunc device_session_notify_;
177 
178   const std::vector<ExternalCaptureSessionFactory*>&
179       external_capture_session_entries_;
180   const std::vector<CaptureSessionEntryFuncs>& capture_session_entries_;
181   HwlSessionCallback hwl_session_callback_;
182   CameraBufferAllocatorHwl* camera_buffer_allocator_hwl_ = nullptr;
183   // device_session_hwl_ is owned by the client.
184   CameraDeviceSessionHwl* camera_device_session_hwl_ = nullptr;
185 
186   std::vector<HalStream>* hal_config_ = nullptr;
187 
188   using GetProcessBlockFactoryFunc = ExternalProcessBlockFactory* (*)();
189   GetProcessBlockFactoryFunc snapshot_process_block_factory_;
190   // Opened library handles that should be closed on destruction
191   void* snapshot_process_block_lib_handle_ = nullptr;
192 
193   GetProcessBlockFactoryFunc denoise_process_block_factory_;
194   // Opened library handles that should be closed on destruction
195   void* denoise_process_block_lib_handle_ = nullptr;
196 
197   // Partial result count reported by HAL
198   uint32_t partial_result_count_ = 1;
199 
200   // Whether video software denoise is enabled
201   bool video_sw_denoise_enabled_ = false;
202 };
203 
204 }  // namespace google_camera_hal
205 }  // namespace android
206 
207 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_ZSL_SNAPSHOT_CAPTURE_SESSION_H_