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