1 /*
2  * Copyright (C) 2019 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_MULTICAM_REALTIME_PROCESS_BLOCK_H_
18 #define HARDWARE_GOOGLE_CAMERA_HAL_MULTICAM_REALTIME_PROCESS_BLOCK_H_
19 
20 #include <map>
21 #include <shared_mutex>
22 #include <vector>
23 
24 #include "pipeline_request_id_manager.h"
25 #include "process_block.h"
26 #include "result_processor.h"
27 
28 namespace android {
29 namespace google_camera_hal {
30 
31 // MultiCameraRtProcessBlock implements a real-time ProcessBlock that can
32 // process real-time capture requests for multiple physical cameras.
33 // MultiCameraRtProcessBlock only supports a logical camera with multiple
34 // physical cameras. It also only supports physical output streams.
35 class MultiCameraRtProcessBlock : public ProcessBlock {
36  public:
37   // Create a MultiCameraRtProcessBlock.
38   // device_session_hwl is owned by the caller and must be valid during the
39   // lifetime of this MultiCameraRtProcessBlock.
40   static std::unique_ptr<MultiCameraRtProcessBlock> Create(
41       CameraDeviceSessionHwl* device_session_hwl);
42 
43   virtual ~MultiCameraRtProcessBlock() = default;
44 
45   // Override functions of ProcessBlock start.
46   // Only physical output streams are supported.
47   status_t ConfigureStreams(const StreamConfiguration& stream_config,
48                             const StreamConfiguration& overall_config) override;
49 
50   status_t SetResultProcessor(
51       std::unique_ptr<ResultProcessor> result_processor) override;
52 
53   status_t GetConfiguredHalStreams(
54       std::vector<HalStream>* hal_streams) const override;
55 
56   status_t ProcessRequests(
57       const std::vector<ProcessBlockRequest>& process_block_requests,
58       const CaptureRequest& remaining_session_request) override;
59 
60   status_t Flush() override;
61 
62   void RepeatingRequestEnd(int32_t frame_number,
63                            const std::vector<int32_t>& stream_ids) override;
64   // Override functions of ProcessBlock end.
65 
66   // Prepare pipeline by camera id
67   status_t PrepareBlockByCameraId(uint32_t camera_id, uint32_t frame_number);
68 
69  protected:
70   MultiCameraRtProcessBlock(CameraDeviceSessionHwl* device_session_hwl);
71 
72  private:
73   // Camera ID of this process block.
74   const uint32_t kCameraId;
75 
76   // Define a configured stream.
77   struct ConfiguredStream {
78     uint32_t pipeline_id = 0;
79     Stream stream;
80   };
81 
82   // Map from a camera ID to the camera's stream configuration.
83   using CameraStreamConfigurationMap = std::map<uint32_t, StreamConfiguration>;
84 
85   // If the real-time process block supports the device session.
86   static bool IsSupported(CameraDeviceSessionHwl* device_session_hwl);
87 
88   // Invoked when the HWL pipeline sends a result.
89   void NotifyHwlPipelineResult(std::unique_ptr<HwlPipelineResult> hwl_result);
90 
91   // Invoked when the HWL pipeline sends a message.
92   void NotifyHwlPipelineMessage(uint32_t pipeline_id,
93                                 const NotifyMessage& message);
94 
95   // Get a map from a camera ID to the camera's stream configuration.
96   // Each camera will have its own stream configuration.
97   status_t GetCameraStreamConfigurationMap(
98       const StreamConfiguration& stream_config,
99       CameraStreamConfigurationMap* camera_stream_config_map) const;
100 
101   // Get the camera ID that a buffer will be captured from. Must be called with
102   // configure_shared_mutex_ locked.
103   status_t GetBufferPhysicalCameraIdLocked(const StreamBuffer& buffer,
104                                            uint32_t* camera_id) const;
105 
106   // Get the pipeline ID that a buffer will be captured from. Must be called
107   // with configure_shared_mutex_ locked.
108   status_t GetOutputBufferPipelineIdLocked(const StreamBuffer& buffer,
109                                            uint32_t* pipeline_id) const;
110 
111   // Return if requests are valid. Must be called with configure_shared_mutex_ locked.
112   bool AreRequestsValidLocked(
113       const std::vector<ProcessBlockRequest>& requests) const;
114 
115   // Forward the pending requests to result processor.
116   status_t ForwardPendingRequests(
117       const std::vector<ProcessBlockRequest>& process_block_requests,
118       const CaptureRequest& remaining_session_request);
119 
120   HwlPipelineCallback hwl_pipeline_callback_;
121   CameraDeviceSessionHwl* device_session_hwl_ = nullptr;
122 
123   mutable std::shared_mutex configure_shared_mutex_;
124 
125   bool is_configured_ = false;  // Must be protected by configure_shared_mutex_.
126 
127   // Map from physical camera ID to HWL pipeline ID. Must be protected by
128   // configure_shared_mutex_.
129   std::unordered_map<uint32_t, uint32_t> camera_pipeline_ids_;
130 
131   // Map from a stream ID to the configured streams. Must be protected by
132   // configure_shared_mutex_.
133   std::unordered_map<uint32_t, ConfiguredStream> configured_streams_;
134 
135   std::mutex result_processor_mutex_;
136 
137   // Result processor. Must be protected by result_processor_mutex_.
138   std::unique_ptr<ResultProcessor> result_processor_ = nullptr;
139 
140   // Pipeline request id manager
141   std::unique_ptr<PipelineRequestIdManager> request_id_manager_ = nullptr;
142 };
143 
144 }  // namespace google_camera_hal
145 }  // namespace android
146 
147 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_MULTICAM_REALTIME_PROCESS_BLOCK_H_
148