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