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 | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 20 | #include <gui/BufferQueueDefs.h> |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 21 | #include <gui/BufferSlot.h> |
| 22 | |
| 23 | #include <utils/Condition.h> |
| 24 | #include <utils/Mutex.h> |
| 25 | #include <utils/RefBase.h> |
| 26 | #include <utils/String8.h> |
| 27 | #include <utils/StrongPointer.h> |
| 28 | #include <utils/Trace.h> |
| 29 | #include <utils/Vector.h> |
| 30 | |
| 31 | #define BQ_LOGV(x, ...) ALOGV("[%s] "x, mConsumerName.string(), ##__VA_ARGS__) |
| 32 | #define BQ_LOGD(x, ...) ALOGD("[%s] "x, mConsumerName.string(), ##__VA_ARGS__) |
| 33 | #define BQ_LOGI(x, ...) ALOGI("[%s] "x, mConsumerName.string(), ##__VA_ARGS__) |
| 34 | #define BQ_LOGW(x, ...) ALOGW("[%s] "x, mConsumerName.string(), ##__VA_ARGS__) |
| 35 | #define BQ_LOGE(x, ...) ALOGE("[%s] "x, mConsumerName.string(), ##__VA_ARGS__) |
| 36 | |
| 37 | #define ATRACE_BUFFER_INDEX(index) \ |
| 38 | if (ATRACE_ENABLED()) { \ |
| 39 | char ___traceBuf[1024]; \ |
| 40 | snprintf(___traceBuf, 1024, "%s: %d", \ |
| 41 | mCore->mConsumerName.string(), (index)); \ |
| 42 | android::ScopedTrace ___bufTracer(ATRACE_TAG, ___traceBuf); \ |
| 43 | } |
| 44 | |
| 45 | namespace android { |
| 46 | |
| 47 | class BufferItem; |
| 48 | class IBinder; |
| 49 | class IConsumerListener; |
| 50 | class IGraphicBufferAlloc; |
| 51 | |
| 52 | class BufferQueueCore : public virtual RefBase { |
| 53 | |
| 54 | friend class BufferQueueProducer; |
| 55 | friend class BufferQueueConsumer; |
| 56 | |
| 57 | public: |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 58 | // Used as a placeholder slot number when the value isn't pointing to an |
| 59 | // existing buffer. |
| 60 | enum { INVALID_BUFFER_SLOT = -1 }; // TODO: Extract from IGBC::BufferItem |
| 61 | |
| 62 | // We reserve two slots in order to guarantee that the producer and |
| 63 | // consumer can run asynchronously. |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 64 | enum { MAX_MAX_ACQUIRED_BUFFERS = BufferQueueDefs::NUM_BUFFER_SLOTS - 2 }; |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 65 | |
| 66 | // The default API number used to indicate that no producer is connected |
| 67 | enum { NO_CONNECTED_API = 0 }; |
| 68 | |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 69 | typedef Vector<BufferItem> Fifo; |
| 70 | |
| 71 | // BufferQueueCore manages a pool of gralloc memory slots to be used by |
| 72 | // producers and consumers. allocator is used to allocate all the needed |
| 73 | // gralloc buffers. |
| 74 | BufferQueueCore(const sp<IGraphicBufferAlloc>& allocator = NULL); |
| 75 | virtual ~BufferQueueCore(); |
| 76 | |
| 77 | private: |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 78 | // Dump our state in a string |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 79 | void dump(String8& result, const char* prefix) const; |
| 80 | |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 81 | // getMinUndequeuedBufferCountLocked returns the minimum number of buffers |
| 82 | // that must remain in a state other than DEQUEUED. The async parameter |
| 83 | // tells whether we're in asynchronous mode. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 84 | int getMinUndequeuedBufferCountLocked(bool async) const; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 85 | |
| 86 | // getMinMaxBufferCountLocked returns the minimum number of buffers allowed |
| 87 | // given the current BufferQueue state. The async parameter tells whether |
| 88 | // we're in asynchonous mode. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 89 | int getMinMaxBufferCountLocked(bool async) const; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 90 | |
| 91 | // getMaxBufferCountLocked returns the maximum number of buffers that can be |
| 92 | // allocated at once. This value depends on the following member variables: |
| 93 | // |
| 94 | // mDequeueBufferCannotBlock |
| 95 | // mMaxAcquiredBufferCount |
| 96 | // mDefaultMaxBufferCount |
| 97 | // mOverrideMaxBufferCount |
| 98 | // async parameter |
| 99 | // |
| 100 | // Any time one of these member variables is changed while a producer is |
| 101 | // connected, mDequeueCondition must be broadcast. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 102 | int getMaxBufferCountLocked(bool async) const; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 103 | |
| 104 | // setDefaultMaxBufferCountLocked sets the maximum number of buffer slots |
| 105 | // that will be used if the producer does not override the buffer slot |
| 106 | // count. The count must be between 2 and NUM_BUFFER_SLOTS, inclusive. The |
| 107 | // initial default is 2. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 108 | status_t setDefaultMaxBufferCountLocked(int count); |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 109 | |
| 110 | // freeBufferLocked frees the GraphicBuffer and sync resources for the |
| 111 | // given slot. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 112 | void freeBufferLocked(int slot); |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 113 | |
| 114 | // freeAllBuffersLocked frees the GraphicBuffer and sync resources for |
| 115 | // all slots. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 116 | void freeAllBuffersLocked(); |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 117 | |
| 118 | // stillTracking returns true iff the buffer item is still being tracked |
| 119 | // in one of the slots. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 120 | bool stillTracking(const BufferItem* item) const; |
| 121 | |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 122 | // mAllocator is the connection to SurfaceFlinger that is used to allocate |
| 123 | // new GraphicBuffer objects. |
| 124 | sp<IGraphicBufferAlloc> mAllocator; |
| 125 | |
| 126 | // mMutex is the mutex used to prevent concurrent access to the member |
| 127 | // variables of BufferQueueCore objects. It must be locked whenever any |
| 128 | // member variable is accessed. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 129 | mutable Mutex mMutex; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 130 | |
| 131 | // mIsAbandoned indicates that the BufferQueue will no longer be used to |
| 132 | // consume image buffers pushed to it using the IGraphicBufferProducer |
| 133 | // interface. It is initialized to false, and set to true in the |
| 134 | // consumerDisconnect method. A BufferQueue that is abandoned will return |
| 135 | // the NO_INIT error from all IGraphicBufferProducer methods capable of |
| 136 | // returning an error. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 137 | bool mIsAbandoned; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 138 | |
| 139 | // mConsumerControlledByApp indicates whether the connected consumer is |
| 140 | // controlled by the application. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 141 | bool mConsumerControlledByApp; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 142 | |
| 143 | // mConsumerName is a string used to identify the BufferQueue in log |
| 144 | // messages. It is set by the IGraphicBufferConsumer::setConsumerName |
| 145 | // method. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 146 | String8 mConsumerName; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 147 | |
| 148 | // mConsumerListener is used to notify the connected consumer of |
| 149 | // asynchronous events that it may wish to react to. It is initially |
| 150 | // set to NULL and is written by consumerConnect and consumerDisconnect. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 151 | sp<IConsumerListener> mConsumerListener; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 152 | |
| 153 | // mConsumerUsageBits contains flags that the consumer wants for |
| 154 | // GraphicBuffers. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 155 | uint32_t mConsumerUsageBits; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 156 | |
| 157 | // mConnectedApi indicates the producer API that is currently connected |
| 158 | // to this BufferQueue. It defaults to NO_CONNECTED_API, and gets updated |
| 159 | // by the connect and disconnect methods. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 160 | int mConnectedApi; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 161 | |
| 162 | // mConnectedProducerToken is used to set a binder death notification on |
| 163 | // the producer. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 164 | sp<IBinder> mConnectedProducerToken; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 165 | |
| 166 | // mSlots is an array of buffer slots that must be mirrored on the producer |
| 167 | // side. This allows buffer ownership to be transferred between the producer |
| 168 | // and consumer without sending a GraphicBuffer over Binder. The entire |
| 169 | // array is initialized to NULL at construction time, and buffers are |
| 170 | // allocated for a slot when requestBuffer is called with that slot's index. |
| 171 | BufferQueueDefs::SlotsType mSlots; |
| 172 | |
| 173 | // mQueue is a FIFO of queued buffers used in synchronous mode. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 174 | Fifo mQueue; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 175 | |
| 176 | // mOverrideMaxBufferCount is the limit on the number of buffers that will |
| 177 | // be allocated at one time. This value is set by the producer by calling |
| 178 | // setBufferCount. The default is 0, which means that the producer doesn't |
| 179 | // care about the number of buffers in the pool. In that case, |
| 180 | // mDefaultMaxBufferCount is used as the limit. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 181 | int mOverrideMaxBufferCount; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 182 | |
| 183 | // mDequeueCondition is a condition variable used for dequeueBuffer in |
| 184 | // synchronous mode. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 185 | mutable Condition mDequeueCondition; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 186 | |
| 187 | // mUseAsyncBuffer indicates whether an extra buffer is used in async mode |
| 188 | // to prevent dequeueBuffer from blocking. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 189 | bool mUseAsyncBuffer; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 190 | |
| 191 | // mDequeueBufferCannotBlock indicates whether dequeueBuffer is allowed to |
| 192 | // block. This flag is set during connect when both the producer and |
| 193 | // consumer are controlled by the application. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 194 | bool mDequeueBufferCannotBlock; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 195 | |
| 196 | // mDefaultBufferFormat can be set so it will override the buffer format |
| 197 | // when it isn't specified in dequeueBuffer. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 198 | uint32_t mDefaultBufferFormat; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 199 | |
| 200 | // mDefaultWidth holds the default width of allocated buffers. It is used |
| 201 | // in dequeueBuffer if a width and height of 0 are specified. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 202 | int mDefaultWidth; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 203 | |
| 204 | // mDefaultHeight holds the default height of allocated buffers. It is used |
| 205 | // in dequeueBuffer if a width and height of 0 are specified. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 206 | int mDefaultHeight; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 207 | |
| 208 | // mDefaultMaxBufferCount is the default limit on the number of buffers that |
| 209 | // will be allocated at one time. This default limit is set by the consumer. |
| 210 | // The limit (as opposed to the default limit) may be overriden by the |
| 211 | // producer. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 212 | int mDefaultMaxBufferCount; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 213 | |
| 214 | // mMaxAcquiredBufferCount is the number of buffers that the consumer may |
| 215 | // acquire at one time. It defaults to 1, and can be changed by the consumer |
| 216 | // via setMaxAcquiredBufferCount, but this may only be done while no |
| 217 | // producer is connected to the BufferQueue. This value is used to derive |
| 218 | // the value returned for the MIN_UNDEQUEUED_BUFFERS query to the producer. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 219 | int mMaxAcquiredBufferCount; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 220 | |
| 221 | // mBufferHasBeenQueued is true once a buffer has been queued. It is reset |
| 222 | // when something causes all buffers to be freed (e.g., changing the buffer |
| 223 | // count). |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 224 | bool mBufferHasBeenQueued; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 225 | |
| 226 | // mFrameCounter is the free running counter, incremented on every |
| 227 | // successful queueBuffer call and buffer allocation. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 228 | uint64_t mFrameCounter; |
Dan Stoza | 3e96f19 | 2014-03-03 10:16:19 -0800 | [diff] [blame^] | 229 | |
| 230 | // mTransformHint is used to optimize for screen rotations. |
Dan Stoza | 289ade1 | 2014-02-28 11:17:17 -0800 | [diff] [blame] | 231 | uint32_t mTransformHint; |
| 232 | |
| 233 | }; // class BufferQueueCore |
| 234 | |
| 235 | } // namespace android |
| 236 | |
| 237 | #endif |