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 #include <unordered_map>
23 
24 #include "camera_buffer_allocator_hwl.h"
25 #include "hal_buffer_allocator.h"
26 #include "hal_types.h"
27 #include "hwl_buffer_allocator.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   virtual ~InternalStreamManager() = default;
40 
41   // stream contains the stream info to be registered. if stream.id is smaller
42   // than kStreamIdReserve, stream_id will be ignored and will be filled with
43   // a unique stream ID.
44   status_t RegisterNewInternalStream(const Stream& stream, int32_t* stream_id);
45 
46   // Allocate buffers for a stream.
47   // hal_stream is the HAL configured stream. It will be combined with the
48   // stream information (set via RegisterNewInternalStream) to allocate buffers.
49   // This method will allocate hal_stream.max_buffers immediately and at most
50   // (hal_stream.max_buffers + additional_num_buffers) buffers.
51   // If need_vendor_buffer is true, the external buffer allocator must be passed
52   // in when create the internal stream manager in create() function.
53   status_t AllocateBuffers(const HalStream& hal_stream,
54                            uint32_t additional_num_buffers = 0,
55                            bool need_vendor_buffer = false);
56 
57   // Allocate shared buffers for streams.
58   // hal_streams are the HAL configured streams. They will be combined with the
59   // stream information (set via RegisterNewInternalStream) to allocate buffers.
60   // This method will allocate the maximum of all hal_stream.max_buffers
61   // immediately and at most (total of hal_stream.max_buffers +
62   // additional_num_buffers).
63   // If need_vendor_buffer is true, the external buffer allocator must be passed
64   // in when create the internal stream manager in create() function.
65   status_t AllocateSharedBuffers(const std::vector<HalStream>& hal_streams,
66                                  uint32_t additional_num_buffers = 0,
67                                  bool need_vendor_buffer = false);
68 
69   // Free a stream and its stream buffers.
70   void FreeStream(int32_t stream_id);
71 
72   // Get a stream buffer from internal stream manager.
73   status_t GetStreamBuffer(int32_t stream_id, StreamBuffer* buffer);
74 
75   // Return a stream buffer to internal stream manager.
76   status_t ReturnStreamBuffer(const StreamBuffer& buffer);
77 
78   // Return a stream buffer with frame number to internal stream manager.
79   status_t ReturnFilledBuffer(uint32_t frame_number, const StreamBuffer& buffer);
80 
81   // Return a metadata to internal stream manager.
82   status_t ReturnMetadata(int32_t stream_id, uint32_t frame_number,
83                           const HalCameraMetadata* metadata);
84 
85   // Get the most recent buffer and metadata.
86   status_t GetMostRecentStreamBuffer(
87       int32_t stream_id, std::vector<StreamBuffer>* input_buffers,
88       std::vector<std::unique_ptr<HalCameraMetadata>>* input_buffer_metadata,
89       uint32_t payload_frames);
90 
91   // Return the buffer from GetMostRecentStreamBuffer
92   status_t ReturnZslStreamBuffers(uint32_t frame_number, int32_t stream_id);
93 
94   // Check the pending buffer is empty or not
95   bool IsPendingBufferEmpty(int32_t stream_id);
96 
97  private:
98   static constexpr int32_t kMinFilledBuffers = 3;
99   static constexpr int32_t kStreamIdStart = kHalInternalStreamStart;
100   static constexpr int32_t kStreamIdReserve =
101       kImplementationDefinedInternalStreamStart;
102   static constexpr int32_t kInvalidStreamId = -1;
103 
104   // Initialize internal stream manager
105   void Initialize(IHalBufferAllocator* buffer_allocator);
106 
107   // Return if a stream is registered. Must be called with stream_mutex_ locked.
108   status_t IsStreamRegisteredLocked(int32_t stream_id) const;
109 
110   // Return if a stream is allocated. Must be called with stream_mutex_ locked.
111   status_t IsStreamAllocatedLocked(int32_t stream_id) const;
112 
113   // Get a buffer descriptor by combining stream and hal stream.
114   status_t GetBufferDescriptor(const Stream& stream, const HalStream& hal_stream,
115                                uint32_t additional_num_buffers,
116                                HalBufferDescriptor* buffer_descriptor);
117 
118   // Get the stream ID of the owner of stream_id's buffer manager. Protected by
119   // stream_mutex_.
120   int32_t GetBufferManagerOwnerIdLocked(int32_t stream_id);
121 
122   // Return if two streams and hal_streams are compatible and can share buffers.
123   bool AreStreamsCompatible(const Stream& stream_0,
124                             const HalStream& hal_stream_0,
125                             const Stream& stream_1,
126                             const HalStream& hal_stream_1) const;
127 
128   // Return if all hal_streams can share buffers. Protected by stream_mutex_.
129   bool CanHalStreamsShareBuffersLocked(
130       const std::vector<HalStream>& hal_streams) const;
131 
132   // Find a new owner for the buffer manager that old_owner_stream_id owns and
133   // remove the old stream. A new owner is one of the streams that share the
134   // same buffer manager. If a new owner cannot be found, the buffer manager
135   // will be destroyed. Protected by stream_mutex_.
136   status_t RemoveOwnerStreamIdLocked(int32_t old_owner_stream_id);
137 
138   // Allocate buffers. Protected by stream_mutex_.
139   status_t AllocateBuffersLocked(const HalStream& hal_stream,
140                                  uint32_t additional_num_buffers,
141                                  bool need_vendor_buffer);
142 
143   std::mutex stream_mutex_;
144 
145   // Next available stream ID. Protected by stream_mutex_.
146   int32_t next_available_stream_id_ = kStreamIdStart;
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 
165 }  // namespace google_camera_hal
166 }  // namespace android
167 
168 #endif  // HARDWARE_GOOGLE_CAMERA_HAL_INTERNAL_STREAM_MANAGER_H_
169