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_INTERNAL_STREAM_MANAGER_H_
18 #define HARDWARE_GOOGLE_CAMERA_HAL_INTERNAL_STREAM_MANAGER_H_
19 
20 #include <hardware/gralloc.h>
21 #include <utils/Errors.h>
22 
23 #include <unordered_map>
24 
25 #include "camera_buffer_allocator_hwl.h"
26 #include "hal_buffer_allocator.h"
27 #include "hal_types.h"
28 #include "zsl_buffer_manager.h"
29 
30 namespace android {
31 namespace google_camera_hal {
32 
33 // InternalStreamManager manages internal streams. It can be used to
34 // create internal streams and allocate internal stream buffers.
35 class InternalStreamManager {
36  public:
37   static std::unique_ptr<InternalStreamManager> Create(
38       IHalBufferAllocator* buffer_allocator = nullptr,
39       int partial_result_count = 1);
40   virtual ~InternalStreamManager() = default;
41 
42   // stream contains the stream info to be registered. if stream.id is smaller
43   // than kStreamIdReserve, stream_id will be ignored and will be filled with
44   // a unique stream ID.
45   status_t RegisterNewInternalStream(const Stream& stream, int32_t* stream_id);
46 
47   // Allocate buffers for a stream.
48   // hal_stream is the HAL configured stream. It will be combined with the
49   // stream information (set via RegisterNewInternalStream) to allocate buffers.
50   // This method will allocate hal_stream.max_buffers immediately and at most
51   // (hal_stream.max_buffers + additional_num_buffers) buffers.
52   // If need_vendor_buffer is true, the external buffer allocator must be passed
53   // in when create the internal stream manager in create() function.
54   status_t AllocateBuffers(const HalStream& hal_stream,
55                            uint32_t additional_num_buffers = 0,
56                            bool need_vendor_buffer = false);
57 
58   // Allocate shared buffers for streams.
59   // hal_streams are the HAL configured streams. They will be combined with the
60   // stream information (set via RegisterNewInternalStream) to allocate buffers.
61   // This method will allocate the maximum of all hal_stream.max_buffers
62   // immediately and at most (total of hal_stream.max_buffers +
63   // additional_num_buffers).
64   // If need_vendor_buffer is true, the external buffer allocator must be passed
65   // in when create the internal stream manager in create() function.
66   status_t AllocateSharedBuffers(const std::vector<HalStream>& hal_streams,
67                                  uint32_t additional_num_buffers = 0,
68                                  bool need_vendor_buffer = false);
69 
70   // Free a stream and its stream buffers.
71   void FreeStream(int32_t stream_id);
72 
73   // Get a stream buffer from internal stream manager.
74   status_t GetStreamBuffer(int32_t stream_id, StreamBuffer* buffer);
75 
76   // Return a stream buffer to internal stream manager.
77   status_t ReturnStreamBuffer(const StreamBuffer& buffer);
78 
79   // Return a stream buffer with frame number to internal stream manager.
80   status_t ReturnFilledBuffer(uint32_t frame_number, const StreamBuffer& buffer);
81 
82   // Return a metadata to internal stream manager.
83   status_t ReturnMetadata(int32_t stream_id, uint32_t frame_number,
84                           const HalCameraMetadata* metadata,
85                           int partial_result = 1);
86 
87   // Get the most recent buffer and metadata.
88   status_t GetMostRecentStreamBuffer(
89       int32_t stream_id, std::vector<StreamBuffer>* input_buffers,
90       std::vector<std::unique_ptr<HalCameraMetadata>>* input_buffer_metadata,
91       uint32_t payload_frames, int32_t min_filled_buffers = kMinFilledBuffers);
92 
93   // Return the buffer from GetMostRecentStreamBuffer
94   status_t ReturnZslStreamBuffers(uint32_t frame_number, int32_t stream_id);
95 
96   // Check the pending buffer is empty or not
97   bool IsPendingBufferEmpty(int32_t stream_id);
98 
99  private:
100   static constexpr int32_t kMinFilledBuffers = 3;
101   static constexpr int32_t kStreamIdStart = kHalInternalStreamStart;
102   static constexpr int32_t kStreamIdReserve =
103       kImplementationDefinedInternalStreamStart;
104   static constexpr int32_t kInvalidStreamId = -1;
105 
106   // Initialize internal stream manager
107   void Initialize(IHalBufferAllocator* buffer_allocator,
108                   int partial_result_count);
109 
110   // Return if a stream is registered. Must be called with stream_mutex_ locked.
111   status_t IsStreamRegisteredLocked(int32_t stream_id) const;
112 
113   // Return if a stream is allocated. Must be called with stream_mutex_ locked.
114   status_t IsStreamAllocatedLocked(int32_t stream_id) const;
115 
116   // Get a buffer descriptor by combining stream and hal stream.
117   status_t GetBufferDescriptor(const Stream& stream, const HalStream& hal_stream,
118                                uint32_t additional_num_buffers,
119                                HalBufferDescriptor* buffer_descriptor);
120 
121   // Get the stream ID of the owner of stream_id's buffer manager. Protected by
122   // stream_mutex_.
123   int32_t GetBufferManagerOwnerIdLocked(int32_t stream_id);
124 
125   // Return if two streams and hal_streams are compatible and can share buffers.
126   bool AreStreamsCompatible(const Stream& stream_0,
127                             const HalStream& hal_stream_0,
128                             const Stream& stream_1,
129                             const HalStream& hal_stream_1) const;
130 
131   // Return if all hal_streams can share buffers. Protected by stream_mutex_.
132   bool CanHalStreamsShareBuffersLocked(
133       const std::vector<HalStream>& hal_streams) const;
134 
135   // Find a new owner for the buffer manager that old_owner_stream_id owns and
136   // remove the old stream. A new owner is one of the streams that share the
137   // same buffer manager. If a new owner cannot be found, the buffer manager
138   // will be destroyed. Protected by stream_mutex_.
139   status_t RemoveOwnerStreamIdLocked(int32_t old_owner_stream_id);
140 
141   // Allocate buffers. Protected by stream_mutex_.
142   status_t AllocateBuffersLocked(const HalStream& hal_stream,
143                                  uint32_t additional_num_buffers,
144                                  bool need_vendor_buffer);
145 
146   std::mutex stream_mutex_;
147 
148   // Map from stream ID to registered stream. Protected by stream_mutex_.
149   std::unordered_map<int32_t, Stream> registered_streams_;
150 
151   // Map from stream ID to its buffer manager owner's stream ID.
152   // For example, if shared_stream_owner_ids_[A] == B, stream A and stream B
153   // share the same buffer manager and stream B is the owner.
154   std::unordered_map<int32_t, int32_t> shared_stream_owner_ids_;
155 
156   // Map from stream ID to ZSL buffer manager it owns. If a stream doesn't own
157   // a buffer manager, the owner stream can be looked up with
158   // shared_stream_owner_ids_. Protected by stream_mutex_.
159   std::unordered_map<int32_t, std::unique_ptr<ZslBufferManager>> buffer_managers_;
160 
161   // external buffer allocator
162   IHalBufferAllocator* hwl_buffer_allocator_ = nullptr;
163 
164   // Partial result count reported by camera HAL
165   int partial_result_count_ = 1;
166 };
167 
168 }  // namespace google_camera_hal
169 }  // namespace android
170 
171 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_INTERNAL_STREAM_MANAGER_H_
172