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