| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 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_GUI_BUFFERQUEUECORE_H |
| 18 | #define ANDROID_GUI_BUFFERQUEUECORE_H |
| 19 | |
| Dan Stoza | 1c87e47 | 2015-03-13 14:40:34 -0700 | [diff] [blame] | 20 | #include <gui/BufferItem.h> |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 21 | #include <gui/BufferQueueDefs.h> |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 22 | #include <gui/BufferSlot.h> |
| 23 | |
| 24 | #include <utils/Condition.h> |
| 25 | #include <utils/Mutex.h> |
| Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 26 | #include <utils/NativeHandle.h> |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 27 | #include <utils/RefBase.h> |
| 28 | #include <utils/String8.h> |
| 29 | #include <utils/StrongPointer.h> |
| 30 | #include <utils/Trace.h> |
| 31 | #include <utils/Vector.h> |
| 32 | |
| Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 33 | #include <list> |
| 34 | #include <set> |
| 35 | |
| Dan Albert | 7d83187 | 2014-09-09 09:21:28 -0700 | [diff] [blame] | 36 | #define BQ_LOGV(x, ...) ALOGV("[%s] " x, mConsumerName.string(), ##__VA_ARGS__) |
| 37 | #define BQ_LOGD(x, ...) ALOGD("[%s] " x, mConsumerName.string(), ##__VA_ARGS__) |
| 38 | #define BQ_LOGI(x, ...) ALOGI("[%s] " x, mConsumerName.string(), ##__VA_ARGS__) |
| 39 | #define BQ_LOGW(x, ...) ALOGW("[%s] " x, mConsumerName.string(), ##__VA_ARGS__) |
| 40 | #define BQ_LOGE(x, ...) ALOGE("[%s] " x, mConsumerName.string(), ##__VA_ARGS__) |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 41 | |
| 42 | #define ATRACE_BUFFER_INDEX(index) \ |
| 43 | if (ATRACE_ENABLED()) { \ |
| 44 | char ___traceBuf[1024]; \ |
| 45 | snprintf(___traceBuf, 1024, "%s: %d", \ |
| 46 | mCore->mConsumerName.string(), (index)); \ |
| 47 | android::ScopedTrace ___bufTracer(ATRACE_TAG, ___traceBuf); \ |
| 48 | } |
| 49 | |
| 50 | namespace android { |
| 51 | |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 52 | class IConsumerListener; |
| 53 | class IGraphicBufferAlloc; |
| Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 54 | class IProducerListener; |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 55 | |
| 56 | class BufferQueueCore : public virtual RefBase { |
| 57 | |
| 58 | friend class BufferQueueProducer; |
| 59 | friend class BufferQueueConsumer; |
| 60 | |
| 61 | public: |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 62 | // Used as a placeholder slot number when the value isn't pointing to an |
| 63 | // existing buffer. |
| Dan Stoza | 1c87e47 | 2015-03-13 14:40:34 -0700 | [diff] [blame] | 64 | enum { INVALID_BUFFER_SLOT = BufferItem::INVALID_BUFFER_SLOT }; |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 65 | |
| 66 | // We reserve two slots in order to guarantee that the producer and |
| 67 | // consumer can run asynchronously. |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 68 | enum { MAX_MAX_ACQUIRED_BUFFERS = BufferQueueDefs::NUM_BUFFER_SLOTS - 2 }; |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 69 | |
| 70 | // The default API number used to indicate that no producer is connected |
| 71 | enum { NO_CONNECTED_API = 0 }; |
| 72 | |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 73 | typedef Vector<BufferItem> Fifo; |
| 74 | |
| 75 | // BufferQueueCore manages a pool of gralloc memory slots to be used by |
| 76 | // producers and consumers. allocator is used to allocate all the needed |
| 77 | // gralloc buffers. |
| 78 | BufferQueueCore(const sp<IGraphicBufferAlloc>& allocator = NULL); |
| 79 | virtual ~BufferQueueCore(); |
| 80 | |
| 81 | private: |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 82 | // Dump our state in a string |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 83 | void dump(String8& result, const char* prefix) const; |
| 84 | |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 85 | // getMinUndequeuedBufferCountLocked returns the minimum number of buffers |
| 86 | // that must remain in a state other than DEQUEUED. The async parameter |
| 87 | // tells whether we're in asynchronous mode. |
| Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame^] | 88 | int getMinUndequeuedBufferCountLocked() const; |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 89 | |
| 90 | // getMinMaxBufferCountLocked returns the minimum number of buffers allowed |
| 91 | // given the current BufferQueue state. The async parameter tells whether |
| 92 | // we're in asynchonous mode. |
| Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame^] | 93 | int getMinMaxBufferCountLocked() const; |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 94 | |
| 95 | // getMaxBufferCountLocked returns the maximum number of buffers that can be |
| 96 | // allocated at once. This value depends on the following member variables: |
| 97 | // |
| Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame^] | 98 | // mMaxDequeuedBufferCount |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 99 | // mMaxAcquiredBufferCount |
| Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame^] | 100 | // mMaxBufferCount |
| 101 | // mAsyncMode |
| 102 | // mDequeueBufferCannotBlock |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 103 | // |
| 104 | // Any time one of these member variables is changed while a producer is |
| 105 | // connected, mDequeueCondition must be broadcast. |
| Pablo Ceballos | 567dbbb | 2015-08-26 18:59:08 -0700 | [diff] [blame^] | 106 | int getMaxBufferCountLocked() const; |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 107 | |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 108 | // freeBufferLocked frees the GraphicBuffer and sync resources for the |
| 109 | // given slot. |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 110 | void freeBufferLocked(int slot); |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 111 | |
| 112 | // freeAllBuffersLocked frees the GraphicBuffer and sync resources for |
| 113 | // all slots. |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 114 | void freeAllBuffersLocked(); |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 115 | |
| 116 | // stillTracking returns true iff the buffer item is still being tracked |
| 117 | // in one of the slots. |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 118 | bool stillTracking(const BufferItem* item) const; |
| 119 | |
| Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 120 | // waitWhileAllocatingLocked blocks until mIsAllocating is false. |
| 121 | void waitWhileAllocatingLocked() const; |
| 122 | |
| Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 123 | // validateConsistencyLocked ensures that the free lists are in sync with |
| 124 | // the information stored in mSlots |
| 125 | void validateConsistencyLocked() const; |
| 126 | |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 127 | // mAllocator is the connection to SurfaceFlinger that is used to allocate |
| 128 | // new GraphicBuffer objects. |
| 129 | sp<IGraphicBufferAlloc> mAllocator; |
| 130 | |
| 131 | // mMutex is the mutex used to prevent concurrent access to the member |
| 132 | // variables of BufferQueueCore objects. It must be locked whenever any |
| 133 | // member variable is accessed. |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 134 | mutable Mutex mMutex; |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 135 | |
| 136 | // mIsAbandoned indicates that the BufferQueue will no longer be used to |
| 137 | // consume image buffers pushed to it using the IGraphicBufferProducer |
| 138 | // interface. It is initialized to false, and set to true in the |
| 139 | // consumerDisconnect method. A BufferQueue that is abandoned will return |
| 140 | // the NO_INIT error from all IGraphicBufferProducer methods capable of |
| 141 | // returning an error. |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 142 | bool mIsAbandoned; |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 143 | |
| 144 | // mConsumerControlledByApp indicates whether the connected consumer is |
| 145 | // controlled by the application. |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 146 | bool mConsumerControlledByApp; |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 147 | |
| 148 | // mConsumerName is a string used to identify the BufferQueue in log |
| 149 | // messages. It is set by the IGraphicBufferConsumer::setConsumerName |
| 150 | // method. |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 151 | String8 mConsumerName; |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 152 | |
| 153 | // mConsumerListener is used to notify the connected consumer of |
| 154 | // asynchronous events that it may wish to react to. It is initially |
| 155 | // set to NULL and is written by consumerConnect and consumerDisconnect. |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 156 | sp<IConsumerListener> mConsumerListener; |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 157 | |
| 158 | // mConsumerUsageBits contains flags that the consumer wants for |
| 159 | // GraphicBuffers. |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 160 | uint32_t mConsumerUsageBits; |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 161 | |
| 162 | // mConnectedApi indicates the producer API that is currently connected |
| 163 | // to this BufferQueue. It defaults to NO_CONNECTED_API, and gets updated |
| 164 | // by the connect and disconnect methods. |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 165 | int mConnectedApi; |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 166 | |
| 167 | // mConnectedProducerToken is used to set a binder death notification on |
| 168 | // the producer. |
| Dan Stoza | f0eaf25 | 2014-03-21 13:05:51 -0700 | [diff] [blame] | 169 | sp<IProducerListener> mConnectedProducerListener; |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 170 | |
| 171 | // mSlots is an array of buffer slots that must be mirrored on the producer |
| 172 | // side. This allows buffer ownership to be transferred between the producer |
| 173 | // and consumer without sending a GraphicBuffer over Binder. The entire |
| 174 | // array is initialized to NULL at construction time, and buffers are |
| 175 | // allocated for a slot when requestBuffer is called with that slot's index. |
| 176 | BufferQueueDefs::SlotsType mSlots; |
| 177 | |
| 178 | // mQueue is a FIFO of queued buffers used in synchronous mode. |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 179 | Fifo mQueue; |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 180 | |
| Dan Stoza | 0de7ea7 | 2015-04-23 13:20:51 -0700 | [diff] [blame] | 181 | // mFreeSlots contains all of the slots which are FREE and do not currently |
| 182 | // have a buffer attached |
| 183 | std::set<int> mFreeSlots; |
| 184 | |
| 185 | // mFreeBuffers contains all of the slots which are FREE and currently have |
| 186 | // a buffer attached |
| 187 | std::list<int> mFreeBuffers; |
| 188 | |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 189 | // mDequeueCondition is a condition variable used for dequeueBuffer in |
| 190 | // synchronous mode. |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 191 | mutable Condition mDequeueCondition; |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 192 | |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 193 | // mDequeueBufferCannotBlock indicates whether dequeueBuffer is allowed to |
| 194 | // block. This flag is set during connect when both the producer and |
| 195 | // consumer are controlled by the application. |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 196 | bool mDequeueBufferCannotBlock; |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 197 | |
| 198 | // mDefaultBufferFormat can be set so it will override the buffer format |
| 199 | // when it isn't specified in dequeueBuffer. |
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 200 | PixelFormat mDefaultBufferFormat; |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 201 | |
| 202 | // mDefaultWidth holds the default width of allocated buffers. It is used |
| 203 | // in dequeueBuffer if a width and height of 0 are specified. |
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 204 | uint32_t mDefaultWidth; |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 205 | |
| 206 | // mDefaultHeight holds the default height of allocated buffers. It is used |
| 207 | // in dequeueBuffer if a width and height of 0 are specified. |
| Dan Stoza | 3be1c6b | 2014-11-18 10:24:03 -0800 | [diff] [blame] | 208 | uint32_t mDefaultHeight; |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 209 | |
| Eino-Ville Talvala | 82c6bcc | 2015-02-19 16:10:43 -0800 | [diff] [blame] | 210 | // mDefaultBufferDataSpace holds the default dataSpace of queued buffers. |
| 211 | // It is used in queueBuffer if a dataspace of 0 (HAL_DATASPACE_UNKNOWN) |
| 212 | // is specified. |
| 213 | android_dataspace mDefaultBufferDataSpace; |
| 214 | |
| Pablo Ceballos | 19e3e06 | 2015-08-19 16:16:06 -0700 | [diff] [blame] | 215 | // mMaxBufferCount is the limit on the number of buffers that will be |
| 216 | // allocated at one time. This limit can be set by the consumer. |
| 217 | int mMaxBufferCount; |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 218 | |
| 219 | // mMaxAcquiredBufferCount is the number of buffers that the consumer may |
| 220 | // acquire at one time. It defaults to 1, and can be changed by the consumer |
| 221 | // via setMaxAcquiredBufferCount, but this may only be done while no |
| 222 | // producer is connected to the BufferQueue. This value is used to derive |
| 223 | // the value returned for the MIN_UNDEQUEUED_BUFFERS query to the producer. |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 224 | int mMaxAcquiredBufferCount; |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 225 | |
| Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 226 | // mMaxDequeuedBufferCount is the number of buffers that the producer may |
| 227 | // dequeue at one time. It defaults to 1, and can be changed by the producer |
| 228 | // via setMaxDequeuedBufferCount. |
| 229 | int mMaxDequeuedBufferCount; |
| 230 | |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 231 | // mBufferHasBeenQueued is true once a buffer has been queued. It is reset |
| 232 | // when something causes all buffers to be freed (e.g., changing the buffer |
| 233 | // count). |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 234 | bool mBufferHasBeenQueued; |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 235 | |
| 236 | // mFrameCounter is the free running counter, incremented on every |
| 237 | // successful queueBuffer call and buffer allocation. |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 238 | uint64_t mFrameCounter; |
| Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame] | 239 | |
| 240 | // mTransformHint is used to optimize for screen rotations. |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 241 | uint32_t mTransformHint; |
| 242 | |
| Jesse Hall | 399184a | 2014-03-03 15:42:54 -0800 | [diff] [blame] | 243 | // mSidebandStream is a handle to the sideband buffer stream, if any |
| 244 | sp<NativeHandle> mSidebandStream; |
| 245 | |
| Antoine Labour | 78014f3 | 2014-07-15 21:17:03 -0700 | [diff] [blame] | 246 | // mIsAllocating indicates whether a producer is currently trying to allocate buffers (which |
| 247 | // releases mMutex while doing the allocation proper). Producers should not modify any of the |
| 248 | // FREE slots while this is true. mIsAllocatingCondition is signaled when this value changes to |
| 249 | // false. |
| 250 | bool mIsAllocating; |
| 251 | |
| 252 | // mIsAllocatingCondition is a condition variable used by producers to wait until mIsAllocating |
| 253 | // becomes false. |
| 254 | mutable Condition mIsAllocatingCondition; |
| Dan Stoza | 9de7293 | 2015-04-16 17:28:43 -0700 | [diff] [blame] | 255 | |
| 256 | // mAllowAllocation determines whether dequeueBuffer is allowed to allocate |
| 257 | // new buffers |
| 258 | bool mAllowAllocation; |
| Dan Stoza | 4afd8b6 | 2015-02-25 16:49:08 -0800 | [diff] [blame] | 259 | |
| 260 | // mBufferAge tracks the age of the contents of the most recently dequeued |
| 261 | // buffer as the number of frames that have elapsed since it was last queued |
| 262 | uint64_t mBufferAge; |
| Dan Stoza | ecc5040 | 2015-04-28 14:42:06 -0700 | [diff] [blame] | 263 | |
| Dan Stoza | 812ed06 | 2015-06-02 15:45:22 -0700 | [diff] [blame] | 264 | // mGenerationNumber stores the current generation number of the attached |
| 265 | // producer. Any attempt to attach a buffer with a different generation |
| 266 | // number will fail. |
| 267 | uint32_t mGenerationNumber; |
| 268 | |
| Pablo Ceballos | fa45535 | 2015-08-12 17:47:47 -0700 | [diff] [blame] | 269 | // mAsyncMode indicates whether or not async mode is enabled. |
| 270 | // In async mode an extra buffer will be allocated to allow the producer to |
| 271 | // enqueue buffers without blocking. |
| 272 | bool mAsyncMode; |
| 273 | |
| Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 274 | }; // class BufferQueueCore |
| 275 | |
| 276 | } // namespace android |
| 277 | |
| 278 | #endif |