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