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_CAPTURE_SESSION_WRAPPER_PROCESS_BLOCK_H_
18 #define HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_CAPTURE_SESSION_WRAPPER_PROCESS_BLOCK_H_
19 
20 #include <shared_mutex>
21 #include <vector>
22 
23 #include "camera_device_session.h"
24 #include "capture_session.h"
25 #include "process_block.h"
26 
27 namespace android {
28 namespace google_camera_hal {
29 
30 // CaptureSessionWrapperProcessBlock implements a real-time ProcessBlock.
31 // It can process real-time capture requests for a single physical camera or a
32 // logical camera. It creates an embedded CaptureSession and the coming requests
33 // will be processed by the embedded CaptureSession.
34 class CaptureSessionWrapperProcessBlock : public ProcessBlock {
35  public:
36   // Create a CaptureSessionWrapperProcessBlock.
37   // device_session_hwl and capture_session are owned by the caller and must be
38   // valid during the lifetime of this CaptureSessionWrapperProcessBlock.
39   static std::unique_ptr<CaptureSessionWrapperProcessBlock> Create(
40       const std::vector<ExternalCaptureSessionFactory*>&
41           external_capture_session_entries,
42       const std::vector<CaptureSessionEntryFuncs>& capture_session_entries,
43       HwlSessionCallback hwl_session_callback,
44       CameraBufferAllocatorHwl* camera_buffer_allocator_hwl,
45       CameraDeviceSessionHwl* camera_device_session_hwl,
46       std::vector<HalStream>* hal_config);
47 
48   virtual ~CaptureSessionWrapperProcessBlock() = default;
49 
50   // Override functions of ProcessBlock start.
51   status_t ConfigureStreams(const StreamConfiguration& stream_config,
52                             const StreamConfiguration& overall_config) override;
53 
54   status_t SetResultProcessor(
55       std::unique_ptr<ResultProcessor> result_processor) override;
56 
57   status_t GetConfiguredHalStreams(
58       std::vector<HalStream>* hal_streams) const override;
59 
60   status_t ProcessRequests(
61       const std::vector<ProcessBlockRequest>& process_block_requests,
62       const CaptureRequest& remaining_session_request) override;
63 
64   status_t Flush() override;
65   // Override functions of ProcessBlock end.
66 
67   void RepeatingRequestEnd(int32_t frame_number,
68                            const std::vector<int32_t>& stream_ids) override;
69 
70  protected:
71   CaptureSessionWrapperProcessBlock(
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_config);
79 
80  private:
81   // Camera ID of this process block.
82   const uint32_t kCameraId;
83 
84   // If the real-time process block supports the device session.
85   static bool IsSupported(CameraDeviceSessionHwl* device_session_hwl);
86 
87   // Invoked when the HWL pipeline sends a result.
88   void NotifyHwlPipelineResult(std::unique_ptr<HwlPipelineResult> hwl_result);
89 
90   // Invoked when the HWL pipeline sends a message.
91   void NotifyHwlPipelineMessage(uint32_t pipeline_id,
92                                 const NotifyMessage& message);
93 
94   mutable std::shared_mutex configure_shared_mutex_;
95 
96   // If streams are configured. Must be protected by configure_shared_mutex_.
97   bool is_configured_ = false;
98 
99   // HWL pipeline ID. Must be protected by configure_shared_mutex_.
100   uint32_t pipeline_id_ = 0;
101 
102   std::mutex result_processor_lock_;
103 
104   // Result processor. Must be protected by result_processor_lock_.
105   std::unique_ptr<ResultProcessor> result_processor_;
106 
107   std::unique_ptr<CaptureSession> embedded_capture_session_;
108 
109   ProcessCaptureResultFunc process_capture_result_;
110   NotifyFunc notify_;
111 
112   const std::vector<ExternalCaptureSessionFactory*>&
113       external_capture_session_entries_;
114   const std::vector<CaptureSessionEntryFuncs>& capture_session_entries_;
115   HwlSessionCallback hwl_session_callback_;
116   CameraBufferAllocatorHwl* camera_buffer_allocator_hwl_ = nullptr;
117   CameraDeviceSessionHwl* camera_device_session_hwl_ = nullptr;
118 
119   std::vector<HalStream>* hal_config_;
120 };
121 
122 }  // namespace google_camera_hal
123 }  // namespace android
124 
125 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_CAPTURE_SESSION_WRAPPER_PROCESS_BLOCK_H_