blob: 549bd99e2a297c0e8307a2848f757b797164a2a8 [file] [log] [blame]
Zhijun He125684a2015-12-26 15:07:30 -08001/*
2 * Copyright 2016 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 ANDROID_SERVERS_CAMERA3_BUFFER_MANAGER_H
18#define ANDROID_SERVERS_CAMERA3_BUFFER_MANAGER_H
19
20#include <list>
21#include <algorithm>
22#include <ui/GraphicBuffer.h>
23#include <utils/RefBase.h>
24#include <utils/KeyedVector.h>
25#include "Camera3OutputStream.h"
26
27namespace android {
28
29namespace camera3 {
30
31struct StreamInfo;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -070032class Camera3OutputStream;
Zhijun He125684a2015-12-26 15:07:30 -080033
34/**
35 * A class managing the graphic buffers that is used by camera output streams. It allocates and
36 * hands out Gralloc buffers to the clients (e.g., Camera3OutputStream) based on the requests.
37 * When clients request a buffer, buffer manager will pick a buffer if there are some already
38 * allocated buffer available, will allocate a buffer otherwise. When there are too many allocated
39 * buffer maintained by the buffer manager, it will dynamically deallocate some buffers that are
40 * solely owned by this buffer manager.
41 * In doing so, it reduces the memory footprint unless it is already minimal without impacting
42 * performance.
43 *
44 */
45class Camera3BufferManager: public virtual RefBase {
46public:
Mathias Agopian2752e5b2017-02-27 18:26:48 -080047 explicit Camera3BufferManager();
Zhijun He125684a2015-12-26 15:07:30 -080048
49 virtual ~Camera3BufferManager();
50
51 /**
52 * This method registers an output stream to this buffer manager by using the provided stream
53 * information.
54 *
55 * The stream info includes the necessary information such as stream size, format, buffer count,
56 * usage flags, etc. for the buffer manager to allocate and hand out buffers for this stream.
57 *
58 * It's illegal to call this method if the stream is not CONFIGURED yet, as some critical
59 * stream properties (e.g., combined usage flags) are only available in this state. It is also
60 * illegal to call this method with an invalid stream set ID (CAMERA3_STREAM_SET_ID_INVALID),
61 * as the invalid stream set ID indicates that this stream doesn't intend to use buffer manager.
62 *
63 *
64 * Once a stream is successfully registered to this buffer manager, the buffer manager takes
65 * over the buffer allocation role and provides buffers to this stream via getBufferForStream().
66 * The returned buffer can be sent to the camera HAL for image output, and then queued to the
67 * ANativeWindow (Surface) for downstream consumer to acquire. Once the image buffer is released
68 * by the consumer end point, the BufferQueueProducer callback onBufferReleased will call
69 * returnBufferForStream() to return the free buffer to this buffer manager. If the stream
70 * uses buffer manager to manage the stream buffers, it should disable the BufferQueue
71 * allocation via IGraphicBufferProducer::allowAllocation(false).
72 *
73 * Registering an already registered stream has no effect.
74 *
75 * Return values:
76 *
77 * OK: Registration of the new stream was successful.
78 * BAD_VALUE: This stream is not at CONFIGURED state, or the stream ID or stream set
79 * ID are invalid, or attempting to register the same stream to multiple
80 * stream sets, or other stream properties are invalid.
81 * INVALID_OPERATION: This buffer manager doesn't support buffer sharing across this stream
82 * and other streams that were already registered with the same stream set
83 * ID.
84 */
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -070085 status_t registerStream(wp<Camera3OutputStream>& stream, const StreamInfo &streamInfo);
Zhijun He125684a2015-12-26 15:07:30 -080086
87 /**
88 * This method unregisters a stream from this buffer manager.
89 *
90 * After a stream is unregistered, further getBufferForStream() calls will fail for this stream.
91 * After all streams for a given stream set are unregistered, all the buffers solely owned (for
92 * this stream set) by this buffer manager will be freed; all buffers subsequently returned to
93 * this buffer manager for this stream set will be freed immediately.
94 *
95 * Return values:
96 *
97 * OK: Removal of the a stream from this buffer manager was successful.
98 * BAD_VALUE: stream ID or stream set ID are invalid, or stream ID and stream set ID
99 * combination doesn't match what was registered, or this stream wasn't registered
100 * to this buffer manager before.
101 */
102 status_t unregisterStream(int streamId, int streamSetId);
103
104 /**
105 * This method obtains a buffer for a stream from this buffer manager.
106 *
107 * This method returns the first free buffer from the free buffer list (associated with this
108 * stream set) if there is any. Otherwise, it will allocate a buffer for this stream, return
109 * it and increment its count of handed-out buffers. When the total number of allocated buffers
110 * is too high, it may deallocate the unused buffers to save memory footprint of this stream
111 * set.
112 *
113 * After this call, the client takes over the ownership of this buffer if it is not freed.
114 *
115 * Return values:
116 *
117 * OK: Getting buffer for this stream was successful.
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700118 * ALREADY_EXISTS: Enough free buffers are already attached to this output buffer queue,
119 * user should just dequeue from the buffer queue.
Zhijun He125684a2015-12-26 15:07:30 -0800120 * BAD_VALUE: stream ID or streamSetId are invalid, or stream ID and stream set ID
121 * combination doesn't match what was registered, or this stream wasn't registered
122 * to this buffer manager before.
123 * NO_MEMORY: Unable to allocate a buffer for this stream at this time.
124 */
125 status_t getBufferForStream(int streamId, int streamSetId, sp<GraphicBuffer>* gb, int* fenceFd);
126
127 /**
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700128 * This method notifies the manager that a buffer has been released by the consumer.
129 *
130 * The buffer is not returned to the buffer manager, but is available for the stream the buffer
131 * is attached to for dequeuing.
132 *
133 * The notification lets the manager know how many buffers are directly available to the stream.
134 *
135 * If onBufferReleased is called for a given released buffer,
136 * returnBufferForStream may not be called for the same buffer, until the
137 * buffer has been reused. The manager will call detachBuffer on the stream
138 * if it needs the released buffer otherwise.
139 *
140 * Return values:
141 *
142 * OK: Buffer release was processed succesfully
143 * BAD_VALUE: stream ID or streamSetId are invalid, or stream ID and stream set ID
144 * combination doesn't match what was registered, or this stream wasn't registered
145 * to this buffer manager before.
146 */
147 status_t onBufferReleased(int streamId, int streamSetId);
148
149 /**
Shuzhen Wangfd76cf52017-05-15 10:01:04 -0700150 * This method notifies the manager that certain buffers has been removed from the
151 * buffer queue by detachBuffer from the consumer.
Zhijun He125684a2015-12-26 15:07:30 -0800152 *
Shuzhen Wangfd76cf52017-05-15 10:01:04 -0700153 * The notification lets the manager update its internal handout buffer count and
154 * attached buffer counts accordingly. When buffers are detached from
155 * consumer, both handout and attached counts are decremented.
Zhijun He125684a2015-12-26 15:07:30 -0800156 *
Shuzhen Wangfd76cf52017-05-15 10:01:04 -0700157 * Return values:
Zhijun He125684a2015-12-26 15:07:30 -0800158 *
Shuzhen Wangfd76cf52017-05-15 10:01:04 -0700159 * OK: Buffer removal was processed succesfully
160 * BAD_VALUE: stream ID or streamSetId are invalid, or stream ID and stream set ID
161 * combination doesn't match what was registered, or this stream wasn't registered
162 * to this buffer manager before, or the removed buffer count is larger than
163 * current total handoutCount or attachedCount.
Zhijun He125684a2015-12-26 15:07:30 -0800164 */
Shuzhen Wangfd76cf52017-05-15 10:01:04 -0700165 status_t onBuffersRemoved(int streamId, int streamSetId, size_t count);
Zhijun He125684a2015-12-26 15:07:30 -0800166
167 /**
168 * Dump the buffer manager statistics.
169 */
170 void dump(int fd, const Vector<String16> &args) const;
171
172private:
173 /**
174 * Lock to synchronize the access to the methods of this class.
175 */
176 mutable Mutex mLock;
177
178 static const size_t kMaxBufferCount = BufferQueueDefs::NUM_BUFFER_SLOTS;
179
Zhijun He125684a2015-12-26 15:07:30 -0800180 struct GraphicBufferEntry {
181 sp<GraphicBuffer> graphicBuffer;
182 int fenceFd;
Chih-Hung Hsiehd19d9942016-08-29 14:21:14 -0700183 explicit GraphicBufferEntry(const sp<GraphicBuffer>& gb = 0, int fd = -1) :
Zhijun He125684a2015-12-26 15:07:30 -0800184 graphicBuffer(gb),
185 fenceFd(fd) {}
186 };
187
188 /**
189 * A buffer entry (indexed by stream ID) represents a single physically allocated buffer. For
190 * Gralloc V0, since each physical buffer is associated with one stream, this is
191 * a single entry map. For Gralloc V1, one physical buffer can be shared between different
192 * streams in one stream set, so this entry may include multiple entries, where the different
193 * graphic buffers have the same common Gralloc backing store.
194 */
195 typedef int StreamId;
196 typedef KeyedVector<StreamId, GraphicBufferEntry> BufferEntry;
197
198 typedef std::list<BufferEntry> BufferList;
199
200 /**
201 * Stream info map (indexed by stream ID) tracks all the streams registered to a particular
202 * stream set.
203 */
204 typedef KeyedVector<StreamId, StreamInfo> InfoMap;
205
206 /**
207 * Stream set buffer count map (indexed by stream ID) tracks all buffer counts of the streams
208 * registered to a particular stream set.
209 */
210 typedef KeyedVector<StreamId, size_t> BufferCountMap;
211
212 /**
213 * StreamSet keeps track of the stream info, free buffer list and hand-out buffer counts for
214 * each stream set.
215 */
216 struct StreamSet {
217 /**
218 * Stream set buffer count water mark representing the max number of allocated buffers
219 * (hand-out buffers + free buffers) count for each stream set. For a given stream set, when
220 * getBufferForStream() is called on this buffer manager, if the total allocated buffer
221 * count exceeds this water mark, the buffer manager will attempt to reduce it as follows:
222 *
223 * In getBufferForStream(), find a buffer associated with other streams (inside the same
224 * stream set) on the free buffer list and free it. For Gralloc V1, can just free the top
225 * of the free buffer list if the physical buffer sharing in this stream is supported.
226 *
227 * For a particular stream set, a larger allocatedBufferWaterMark increases the memory
228 * footprint of the stream set, but reduces the chance that getBufferForStream() will have
229 * to allocate a new buffer. We assume that the streams in one stream set are not streaming
230 * simultaneously, the max allocated buffer count water mark for a stream set will the max
231 * of all streams' total buffer counts. This will avoid new buffer allocation in steady
232 * streaming state.
Zhijun He8d1a1542016-01-29 20:28:21 -0800233 *
234 * This water mark can be dynamically changed, and will grow when the hand-out buffer count
235 * of each stream increases, until it reaches the maxAllowedBufferCount.
Zhijun He125684a2015-12-26 15:07:30 -0800236 */
237 size_t allocatedBufferWaterMark;
Zhijun He8d1a1542016-01-29 20:28:21 -0800238
239 /**
240 * The max allowed buffer count for this stream set. It is the max of total number of
241 * buffers for each stream. This is the upper bound of the allocatedBufferWaterMark.
242 */
243 size_t maxAllowedBufferCount;
244
Zhijun He125684a2015-12-26 15:07:30 -0800245 /**
246 * The stream info for all streams in this set
247 */
248 InfoMap streamInfoMap;
249 /**
Zhijun He125684a2015-12-26 15:07:30 -0800250 * The count of the buffers that were handed out to the streams of this set.
251 */
252 BufferCountMap handoutBufferCountMap;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700253 /**
254 * The count of the buffers that are attached to the streams of this set.
255 * An attached buffer may be free or handed out
256 */
257 BufferCountMap attachedBufferCountMap;
258
Zhijun He125684a2015-12-26 15:07:30 -0800259 StreamSet() {
260 allocatedBufferWaterMark = 0;
Zhijun He8d1a1542016-01-29 20:28:21 -0800261 maxAllowedBufferCount = 0;
Zhijun He125684a2015-12-26 15:07:30 -0800262 }
263 };
264
265 /**
266 * Stream set map managed by this buffer manager.
267 */
268 typedef int StreamSetId;
269 KeyedVector<StreamSetId, StreamSet> mStreamSetMap;
Eino-Ville Talvala77c1a352016-06-13 12:32:43 -0700270 KeyedVector<StreamId, wp<Camera3OutputStream>> mStreamMap;
Zhijun He125684a2015-12-26 15:07:30 -0800271
272 // TODO: There is no easy way to query the Gralloc version in this code yet, we have different
273 // code paths for different Gralloc versions, hardcode something here for now.
274 const uint32_t mGrallocVersion = GRALLOC_DEVICE_API_VERSION_0_1;
275
276 /**
277 * Check if this stream was successfully registered already. This method needs to be called with
278 * mLock held.
279 */
280 bool checkIfStreamRegisteredLocked(int streamId, int streamSetId) const;
281
Zhijun He125684a2015-12-26 15:07:30 -0800282};
283
284} // namespace camera3
285} // namespace android
286
287#endif // ANDROID_SERVERS_CAMERA3_BUFFER_MANAGER_H