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_GOOGLE_CAMERA_HAL_REALTIME_PROCESS_BLOCK_H_
18 #define HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_REALTIME_PROCESS_BLOCK_H_
19 
20 #include <shared_mutex>
21 #include <vector>
22 
23 #include "process_block.h"
24 
25 namespace android {
26 namespace google_camera_hal {
27 
28 // RealtimeProcessBlock implements a real-time ProcessBlock.
29 // It can process real-time capture requests for a single physical camera.
30 class RealtimeProcessBlock : public ProcessBlock {
31  public:
32   // Create a RealtimeProcessBlock.
33   // device_session_hwl is owned by the caller and must be valid during the
34   // lifetime of this RealtimeProcessBlock.
35   static std::unique_ptr<RealtimeProcessBlock> Create(
36       CameraDeviceSessionHwl* device_session_hwl);
37 
38   virtual ~RealtimeProcessBlock() = default;
39 
40   // Override functions of ProcessBlock start.
41   // All output streams must be physical streams. RealtimeProcessBlock does not
42   // support logical output streams.
43   status_t ConfigureStreams(const StreamConfiguration& stream_config,
44                             const StreamConfiguration& overall_config) override;
45 
46   status_t SetResultProcessor(
47       std::unique_ptr<ResultProcessor> result_processor) override;
48 
49   status_t GetConfiguredHalStreams(
50       std::vector<HalStream>* hal_streams) const override;
51 
52   status_t ProcessRequests(
53       const std::vector<ProcessBlockRequest>& process_block_requests,
54       const CaptureRequest& remaining_session_request) override;
55 
56   status_t Flush() override;
57   // Override functions of ProcessBlock end.
58 
59   void RepeatingRequestEnd(int32_t frame_number,
60                            const std::vector<int32_t>& stream_ids) override;
61 
62  protected:
63   RealtimeProcessBlock(CameraDeviceSessionHwl* device_session_hwl);
64 
65  private:
66   // Camera ID of this process block.
67   const uint32_t kCameraId;
68 
69   // If the real-time process block supports the device session.
70   static bool IsSupported(CameraDeviceSessionHwl* device_session_hwl);
71 
72   // Invoked when the HWL pipeline sends a result.
73   void NotifyHwlPipelineResult(std::unique_ptr<HwlPipelineResult> hwl_result);
74 
75   // Invoked when the HWL pipeline sends a batched result.
76   void NotifyHwlPipelineBatchResult(
77       std::vector<std::unique_ptr<HwlPipelineResult>> hwl_results);
78 
79   // Invoked when the HWL pipeline sends a message.
80   void NotifyHwlPipelineMessage(uint32_t pipeline_id,
81                                 const NotifyMessage& message);
82 
83   HwlPipelineCallback hwl_pipeline_callback_;
84   CameraDeviceSessionHwl* device_session_hwl_ = nullptr;
85 
86   mutable std::shared_mutex configure_shared_mutex_;
87 
88   // If streams are configured. Must be protected by configure_shared_mutex_.
89   bool is_configured_ = false;
90 
91   // HWL pipeline ID. Must be protected by configure_shared_mutex_.
92   uint32_t pipeline_id_ = 0;
93 
94   std::mutex result_processor_lock_;
95 
96   // Result processor. Must be protected by result_processor_lock_.
97   std::unique_ptr<ResultProcessor> result_processor_ = nullptr;
98 };
99 
100 }  // namespace google_camera_hal
101 }  // namespace android
102 
103 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_GOOGLE_CAMERA_HAL_REALTIME_PROCESS_BLOCK_H_
104