Zhijun He | 125684a | 2015-12-26 15:07:30 -0800 | [diff] [blame^] | 1 | /* |
| 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 | |
| 27 | namespace android { |
| 28 | |
| 29 | namespace camera3 { |
| 30 | |
| 31 | struct StreamInfo; |
| 32 | |
| 33 | /** |
| 34 | * A class managing the graphic buffers that is used by camera output streams. It allocates and |
| 35 | * hands out Gralloc buffers to the clients (e.g., Camera3OutputStream) based on the requests. |
| 36 | * When clients request a buffer, buffer manager will pick a buffer if there are some already |
| 37 | * allocated buffer available, will allocate a buffer otherwise. When there are too many allocated |
| 38 | * buffer maintained by the buffer manager, it will dynamically deallocate some buffers that are |
| 39 | * solely owned by this buffer manager. |
| 40 | * In doing so, it reduces the memory footprint unless it is already minimal without impacting |
| 41 | * performance. |
| 42 | * |
| 43 | */ |
| 44 | class Camera3BufferManager: public virtual RefBase { |
| 45 | public: |
| 46 | Camera3BufferManager(const sp<IGraphicBufferAlloc>& allocator = NULL); |
| 47 | |
| 48 | virtual ~Camera3BufferManager(); |
| 49 | |
| 50 | /** |
| 51 | * This method registers an output stream to this buffer manager by using the provided stream |
| 52 | * information. |
| 53 | * |
| 54 | * The stream info includes the necessary information such as stream size, format, buffer count, |
| 55 | * usage flags, etc. for the buffer manager to allocate and hand out buffers for this stream. |
| 56 | * |
| 57 | * It's illegal to call this method if the stream is not CONFIGURED yet, as some critical |
| 58 | * stream properties (e.g., combined usage flags) are only available in this state. It is also |
| 59 | * illegal to call this method with an invalid stream set ID (CAMERA3_STREAM_SET_ID_INVALID), |
| 60 | * as the invalid stream set ID indicates that this stream doesn't intend to use buffer manager. |
| 61 | * |
| 62 | * |
| 63 | * Once a stream is successfully registered to this buffer manager, the buffer manager takes |
| 64 | * over the buffer allocation role and provides buffers to this stream via getBufferForStream(). |
| 65 | * The returned buffer can be sent to the camera HAL for image output, and then queued to the |
| 66 | * ANativeWindow (Surface) for downstream consumer to acquire. Once the image buffer is released |
| 67 | * by the consumer end point, the BufferQueueProducer callback onBufferReleased will call |
| 68 | * returnBufferForStream() to return the free buffer to this buffer manager. If the stream |
| 69 | * uses buffer manager to manage the stream buffers, it should disable the BufferQueue |
| 70 | * allocation via IGraphicBufferProducer::allowAllocation(false). |
| 71 | * |
| 72 | * Registering an already registered stream has no effect. |
| 73 | * |
| 74 | * Return values: |
| 75 | * |
| 76 | * OK: Registration of the new stream was successful. |
| 77 | * BAD_VALUE: This stream is not at CONFIGURED state, or the stream ID or stream set |
| 78 | * ID are invalid, or attempting to register the same stream to multiple |
| 79 | * stream sets, or other stream properties are invalid. |
| 80 | * INVALID_OPERATION: This buffer manager doesn't support buffer sharing across this stream |
| 81 | * and other streams that were already registered with the same stream set |
| 82 | * ID. |
| 83 | */ |
| 84 | status_t registerStream(const StreamInfo &streamInfo); |
| 85 | |
| 86 | /** |
| 87 | * This method unregisters a stream from this buffer manager. |
| 88 | * |
| 89 | * After a stream is unregistered, further getBufferForStream() calls will fail for this stream. |
| 90 | * After all streams for a given stream set are unregistered, all the buffers solely owned (for |
| 91 | * this stream set) by this buffer manager will be freed; all buffers subsequently returned to |
| 92 | * this buffer manager for this stream set will be freed immediately. |
| 93 | * |
| 94 | * Return values: |
| 95 | * |
| 96 | * OK: Removal of the a stream from this buffer manager was successful. |
| 97 | * BAD_VALUE: stream ID or stream set ID are invalid, or stream ID and stream set ID |
| 98 | * combination doesn't match what was registered, or this stream wasn't registered |
| 99 | * to this buffer manager before. |
| 100 | */ |
| 101 | status_t unregisterStream(int streamId, int streamSetId); |
| 102 | |
| 103 | /** |
| 104 | * This method obtains a buffer for a stream from this buffer manager. |
| 105 | * |
| 106 | * This method returns the first free buffer from the free buffer list (associated with this |
| 107 | * stream set) if there is any. Otherwise, it will allocate a buffer for this stream, return |
| 108 | * it and increment its count of handed-out buffers. When the total number of allocated buffers |
| 109 | * is too high, it may deallocate the unused buffers to save memory footprint of this stream |
| 110 | * set. |
| 111 | * |
| 112 | * After this call, the client takes over the ownership of this buffer if it is not freed. |
| 113 | * |
| 114 | * Return values: |
| 115 | * |
| 116 | * OK: Getting buffer for this stream was successful. |
| 117 | * BAD_VALUE: stream ID or streamSetId are invalid, or stream ID and stream set ID |
| 118 | * combination doesn't match what was registered, or this stream wasn't registered |
| 119 | * to this buffer manager before. |
| 120 | * NO_MEMORY: Unable to allocate a buffer for this stream at this time. |
| 121 | */ |
| 122 | status_t getBufferForStream(int streamId, int streamSetId, sp<GraphicBuffer>* gb, int* fenceFd); |
| 123 | |
| 124 | /** |
| 125 | * This method returns a buffer for a stream to this buffer manager. |
| 126 | * |
| 127 | * When a buffer is returned, it is treated as a free buffer and may either be reused for future |
| 128 | * getBufferForStream() calls, or freed if there total number of outstanding allocated buffers |
| 129 | * is too large. The latter only applies to the case where the buffer are physically shared |
| 130 | * between streams in the same stream set. A physically shared buffer is the buffer that has one |
| 131 | * physical back store but multiple handles. Multiple stream can access the same physical memory |
| 132 | * with their own handles. Physically shared buffer can only be supported by Gralloc HAL V1. |
| 133 | * See hardware/libhardware/include/hardware/gralloc1.h for more details. |
| 134 | * |
| 135 | * |
| 136 | * This call takes the ownership of the returned buffer if it was allocated by this buffer |
| 137 | * manager; clients should not use this buffer after this call. Attempting to access this buffer |
| 138 | * after this call will have undefined behavior. Holding a reference to this buffer after this |
| 139 | * call may cause memory leakage. If a BufferQueue is used to track the buffers handed out by |
| 140 | * this buffer queue, it is recommended to call detachNextBuffer() from the buffer queue after |
| 141 | * BufferQueueProducer onBufferReleased callback is fired, and return it to this buffer manager. |
| 142 | * |
| 143 | * OK: Buffer return for this stream was successful. |
| 144 | * BAD_VALUE: stream ID or streamSetId are invalid, or stream ID and stream set ID combination |
| 145 | * doesn't match what was registered, or this stream wasn't registered to this |
| 146 | * buffer manager before. |
| 147 | */ |
| 148 | status_t returnBufferForStream(int streamId, int streamSetId, const sp<GraphicBuffer>& buffer, |
| 149 | int fenceFd); |
| 150 | |
| 151 | /** |
| 152 | * Dump the buffer manager statistics. |
| 153 | */ |
| 154 | void dump(int fd, const Vector<String16> &args) const; |
| 155 | |
| 156 | private: |
| 157 | /** |
| 158 | * Lock to synchronize the access to the methods of this class. |
| 159 | */ |
| 160 | mutable Mutex mLock; |
| 161 | |
| 162 | static const size_t kMaxBufferCount = BufferQueueDefs::NUM_BUFFER_SLOTS; |
| 163 | |
| 164 | /** |
| 165 | * mAllocator is the connection to SurfaceFlinger that is used to allocate new GraphicBuffer |
| 166 | * objects. |
| 167 | */ |
| 168 | sp<IGraphicBufferAlloc> mAllocator; |
| 169 | |
| 170 | struct GraphicBufferEntry { |
| 171 | sp<GraphicBuffer> graphicBuffer; |
| 172 | int fenceFd; |
| 173 | GraphicBufferEntry(const sp<GraphicBuffer>& gb = 0, int fd = -1) : |
| 174 | graphicBuffer(gb), |
| 175 | fenceFd(fd) {} |
| 176 | }; |
| 177 | |
| 178 | /** |
| 179 | * A buffer entry (indexed by stream ID) represents a single physically allocated buffer. For |
| 180 | * Gralloc V0, since each physical buffer is associated with one stream, this is |
| 181 | * a single entry map. For Gralloc V1, one physical buffer can be shared between different |
| 182 | * streams in one stream set, so this entry may include multiple entries, where the different |
| 183 | * graphic buffers have the same common Gralloc backing store. |
| 184 | */ |
| 185 | typedef int StreamId; |
| 186 | typedef KeyedVector<StreamId, GraphicBufferEntry> BufferEntry; |
| 187 | |
| 188 | typedef std::list<BufferEntry> BufferList; |
| 189 | |
| 190 | /** |
| 191 | * Stream info map (indexed by stream ID) tracks all the streams registered to a particular |
| 192 | * stream set. |
| 193 | */ |
| 194 | typedef KeyedVector<StreamId, StreamInfo> InfoMap; |
| 195 | |
| 196 | /** |
| 197 | * Stream set buffer count map (indexed by stream ID) tracks all buffer counts of the streams |
| 198 | * registered to a particular stream set. |
| 199 | */ |
| 200 | typedef KeyedVector<StreamId, size_t> BufferCountMap; |
| 201 | |
| 202 | /** |
| 203 | * StreamSet keeps track of the stream info, free buffer list and hand-out buffer counts for |
| 204 | * each stream set. |
| 205 | */ |
| 206 | struct StreamSet { |
| 207 | /** |
| 208 | * Stream set buffer count water mark representing the max number of allocated buffers |
| 209 | * (hand-out buffers + free buffers) count for each stream set. For a given stream set, when |
| 210 | * getBufferForStream() is called on this buffer manager, if the total allocated buffer |
| 211 | * count exceeds this water mark, the buffer manager will attempt to reduce it as follows: |
| 212 | * |
| 213 | * In getBufferForStream(), find a buffer associated with other streams (inside the same |
| 214 | * stream set) on the free buffer list and free it. For Gralloc V1, can just free the top |
| 215 | * of the free buffer list if the physical buffer sharing in this stream is supported. |
| 216 | * |
| 217 | * For a particular stream set, a larger allocatedBufferWaterMark increases the memory |
| 218 | * footprint of the stream set, but reduces the chance that getBufferForStream() will have |
| 219 | * to allocate a new buffer. We assume that the streams in one stream set are not streaming |
| 220 | * simultaneously, the max allocated buffer count water mark for a stream set will the max |
| 221 | * of all streams' total buffer counts. This will avoid new buffer allocation in steady |
| 222 | * streaming state. |
| 223 | */ |
| 224 | size_t allocatedBufferWaterMark; |
| 225 | /** |
| 226 | * The stream info for all streams in this set |
| 227 | */ |
| 228 | InfoMap streamInfoMap; |
| 229 | /** |
| 230 | * The free buffer list for all the buffers belong to this set. The free buffers are |
| 231 | * returned by the returnBufferForStream() call, and available for reuse. |
| 232 | */ |
| 233 | BufferList freeBuffers; |
| 234 | /** |
| 235 | * The count of the buffers that were handed out to the streams of this set. |
| 236 | */ |
| 237 | BufferCountMap handoutBufferCountMap; |
| 238 | StreamSet() { |
| 239 | allocatedBufferWaterMark = 0; |
| 240 | } |
| 241 | }; |
| 242 | |
| 243 | /** |
| 244 | * Stream set map managed by this buffer manager. |
| 245 | */ |
| 246 | typedef int StreamSetId; |
| 247 | KeyedVector<StreamSetId, StreamSet> mStreamSetMap; |
| 248 | |
| 249 | // TODO: There is no easy way to query the Gralloc version in this code yet, we have different |
| 250 | // code paths for different Gralloc versions, hardcode something here for now. |
| 251 | const uint32_t mGrallocVersion = GRALLOC_DEVICE_API_VERSION_0_1; |
| 252 | |
| 253 | /** |
| 254 | * Check if this stream was successfully registered already. This method needs to be called with |
| 255 | * mLock held. |
| 256 | */ |
| 257 | bool checkIfStreamRegisteredLocked(int streamId, int streamSetId) const; |
| 258 | |
| 259 | /** |
| 260 | * Add a buffer entry to the BufferList. This method needs to be called with mLock held. |
| 261 | */ |
| 262 | status_t addBufferToBufferListLocked(BufferList &bufList, const BufferEntry &buffer); |
| 263 | |
| 264 | /** |
| 265 | * Remove all buffers from the BufferList. |
| 266 | * |
| 267 | * Note that this doesn't mean that the buffers are freed after this call. A buffer is freed |
| 268 | * only if all other references to it are dropped. |
| 269 | * |
| 270 | * This method needs to be called with mLock held. |
| 271 | */ |
| 272 | status_t removeBuffersFromBufferListLocked(BufferList &bufList, int streamId); |
| 273 | |
| 274 | /** |
| 275 | * Get the first available buffer from the buffer list for this stream. The graphicBuffer inside |
| 276 | * this entry will be NULL if there is no any GraphicBufferEntry found. After this call, the |
| 277 | * GraphicBufferEntry will be removed from the BufferList if a GraphicBufferEntry is found. |
| 278 | * |
| 279 | * This method needs to be called with mLock held. |
| 280 | * |
| 281 | */ |
| 282 | GraphicBufferEntry getFirstBufferFromBufferListLocked(BufferList& buffers, int streamId); |
| 283 | }; |
| 284 | |
| 285 | } // namespace camera3 |
| 286 | } // namespace android |
| 287 | |
| 288 | #endif // ANDROID_SERVERS_CAMERA3_BUFFER_MANAGER_H |