BufferQueueConsumer.cpp revision 399184a4cd728ea1421fb0bc1722274a29e38f4a
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#define LOG_TAG "BufferQueueConsumer" 18#define ATRACE_TAG ATRACE_TAG_GRAPHICS 19//#define LOG_NDEBUG 0 20 21#include <gui/BufferItem.h> 22#include <gui/BufferQueueConsumer.h> 23#include <gui/BufferQueueCore.h> 24#include <gui/IConsumerListener.h> 25 26namespace android { 27 28BufferQueueConsumer::BufferQueueConsumer(const sp<BufferQueueCore>& core) : 29 mCore(core), 30 mSlots(core->mSlots), 31 mConsumerName() {} 32 33BufferQueueConsumer::~BufferQueueConsumer() {} 34 35status_t BufferQueueConsumer::acquireBuffer(BufferItem* outBuffer, 36 nsecs_t expectedPresent) { 37 ATRACE_CALL(); 38 Mutex::Autolock lock(mCore->mMutex); 39 40 // Check that the consumer doesn't currently have the maximum number of 41 // buffers acquired. We allow the max buffer count to be exceeded by one 42 // buffer so that the consumer can successfully set up the newly acquired 43 // buffer before releasing the old one. 44 int numAcquiredBuffers = 0; 45 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) { 46 if (mSlots[s].mBufferState == BufferSlot::ACQUIRED) { 47 ++numAcquiredBuffers; 48 } 49 } 50 if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) { 51 BQ_LOGE("acquireBuffer: max acquired buffer count reached: %d (max %d)", 52 numAcquiredBuffers, mCore->mMaxAcquiredBufferCount); 53 return INVALID_OPERATION; 54 } 55 56 // Check if the queue is empty. 57 // In asynchronous mode the list is guaranteed to be one buffer deep, 58 // while in synchronous mode we use the oldest buffer. 59 if (mCore->mQueue.empty()) { 60 return NO_BUFFER_AVAILABLE; 61 } 62 63 BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin()); 64 65 // If expectedPresent is specified, we may not want to return a buffer yet. 66 // If it's specified and there's more than one buffer queued, we may want 67 // to drop a buffer. 68 if (expectedPresent != 0) { 69 const int MAX_REASONABLE_NSEC = 1000000000ULL; // 1 second 70 71 // The 'expectedPresent' argument indicates when the buffer is expected 72 // to be presented on-screen. If the buffer's desired present time is 73 // earlier (less) than expectedPresent -- meaning it will be displayed 74 // on time or possibly late if we show it as soon as possible -- we 75 // acquire and return it. If we don't want to display it until after the 76 // expectedPresent time, we return PRESENT_LATER without acquiring it. 77 // 78 // To be safe, we don't defer acquisition if expectedPresent is more 79 // than one second in the future beyond the desired present time 80 // (i.e., we'd be holding the buffer for a long time). 81 // 82 // NOTE: Code assumes monotonic time values from the system clock 83 // are positive. 84 85 // Start by checking to see if we can drop frames. We skip this check if 86 // the timestamps are being auto-generated by Surface. If the app isn't 87 // generating timestamps explicitly, it probably doesn't want frames to 88 // be discarded based on them. 89 while (mCore->mQueue.size() > 1 && !mCore->mQueue[0].mIsAutoTimestamp) { 90 // If entry[1] is timely, drop entry[0] (and repeat). We apply an 91 // additional criterion here: we only drop the earlier buffer if our 92 // desiredPresent falls within +/- 1 second of the expected present. 93 // Otherwise, bogus desiredPresent times (e.g., 0 or a small 94 // relative timestamp), which normally mean "ignore the timestamp 95 // and acquire immediately", would cause us to drop frames. 96 // 97 // We may want to add an additional criterion: don't drop the 98 // earlier buffer if entry[1]'s fence hasn't signaled yet. 99 const BufferItem& bufferItem(mCore->mQueue[1]); 100 nsecs_t desiredPresent = bufferItem.mTimestamp; 101 if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC || 102 desiredPresent > expectedPresent) { 103 // This buffer is set to display in the near future, or 104 // desiredPresent is garbage. Either way we don't want to drop 105 // the previous buffer just to get this on the screen sooner. 106 BQ_LOGV("acquireBuffer: nodrop desire=%lld expect=%lld " 107 "(%lld) now=%lld", desiredPresent, expectedPresent, 108 desiredPresent - expectedPresent, 109 systemTime(CLOCK_MONOTONIC)); 110 break; 111 } 112 113 BQ_LOGV("acquireBuffer: drop desire=%lld expect=%lld size=%d", 114 desiredPresent, expectedPresent, mCore->mQueue.size()); 115 if (mCore->stillTracking(front)) { 116 // Front buffer is still in mSlots, so mark the slot as free 117 mSlots[front->mSlot].mBufferState = BufferSlot::FREE; 118 } 119 mCore->mQueue.erase(front); 120 front = mCore->mQueue.begin(); 121 } 122 123 // See if the front buffer is due 124 nsecs_t desiredPresent = front->mTimestamp; 125 if (desiredPresent > expectedPresent && 126 desiredPresent < expectedPresent + MAX_REASONABLE_NSEC) { 127 BQ_LOGV("acquireBuffer: defer desire=%lld expect=%lld " 128 "(%lld) now=%lld", desiredPresent, expectedPresent, 129 desiredPresent - expectedPresent, 130 systemTime(CLOCK_MONOTONIC)); 131 return PRESENT_LATER; 132 } 133 134 BQ_LOGV("acquireBuffer: accept desire=%lld expect=%lld " 135 "(%lld) now=%lld", desiredPresent, expectedPresent, 136 desiredPresent - expectedPresent, 137 systemTime(CLOCK_MONOTONIC)); 138 } 139 140 int slot = front->mSlot; 141 *outBuffer = *front; 142 ATRACE_BUFFER_INDEX(slot); 143 144 BQ_LOGV("acquireBuffer: acquiring { slot=%d/%llu buffer=%p }", 145 slot, front->mFrameNumber, front->mGraphicBuffer->handle); 146 // If the front buffer is still being tracked, update its slot state 147 if (mCore->stillTracking(front)) { 148 mSlots[slot].mAcquireCalled = true; 149 mSlots[slot].mNeedsCleanupOnRelease = false; 150 mSlots[slot].mBufferState = BufferSlot::ACQUIRED; 151 mSlots[slot].mFence = Fence::NO_FENCE; 152 } 153 154 // If the buffer has previously been acquired by the consumer, set 155 // mGraphicBuffer to NULL to avoid unnecessarily remapping this buffer 156 // on the consumer side 157 if (outBuffer->mAcquireCalled) { 158 outBuffer->mGraphicBuffer = NULL; 159 } 160 161 mCore->mQueue.erase(front); 162 // TODO: Should this call be after we free a slot while dropping buffers? 163 // Simply acquiring the next buffer doesn't enable a producer to dequeue. 164 mCore->mDequeueCondition.broadcast(); 165 166 ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size()); 167 168 return NO_ERROR; 169} 170 171status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber, 172 const sp<Fence>& releaseFence, EGLDisplay eglDisplay, 173 EGLSyncKHR eglFence) { 174 ATRACE_CALL(); 175 ATRACE_BUFFER_INDEX(slot); 176 177 if (slot == BufferQueueCore::INVALID_BUFFER_SLOT || releaseFence == NULL) { 178 return BAD_VALUE; 179 } 180 181 Mutex::Autolock lock(mCore->mMutex); 182 183 // If the frame number has changed because the buffer has been reallocated, 184 // we can ignore this releaseBuffer for the old buffer 185 if (frameNumber != mSlots[slot].mFrameNumber) { 186 return STALE_BUFFER_SLOT; 187 } 188 189 // Make sure this buffer hasn't been queued while acquired by the consumer 190 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin()); 191 while (current != mCore->mQueue.end()) { 192 if (current->mSlot == slot) { 193 BQ_LOGE("releaseBuffer: buffer slot %d pending release is " 194 "currently queued", slot); 195 return -EINVAL; 196 } 197 ++current; 198 } 199 200 if (mSlots[slot].mBufferState == BufferSlot::ACQUIRED) { 201 mSlots[slot].mEglDisplay = eglDisplay; 202 mSlots[slot].mEglFence = eglFence; 203 mSlots[slot].mFence = releaseFence; 204 mSlots[slot].mBufferState = BufferSlot::FREE; 205 } else if (mSlots[slot].mNeedsCleanupOnRelease) { 206 BQ_LOGV("releaseBuffer: releasing a stale buffer slot %d " 207 "(state = %d)", slot, mSlots[slot].mBufferState); 208 mSlots[slot].mNeedsCleanupOnRelease = false; 209 return STALE_BUFFER_SLOT; 210 } else { 211 BQ_LOGV("releaseBuffer: attempted to release buffer slot %d " 212 "but its state was %d", slot, mSlots[slot].mBufferState); 213 return -EINVAL; 214 } 215 216 mCore->mDequeueCondition.broadcast(); 217 218 return NO_ERROR; 219} 220 221status_t BufferQueueConsumer::connect( 222 const sp<IConsumerListener>& consumerListener, bool controlledByApp) { 223 ATRACE_CALL(); 224 225 if (consumerListener == NULL) { 226 BQ_LOGE("connect(C): consumerListener may not be NULL"); 227 return BAD_VALUE; 228 } 229 230 BQ_LOGV("connect(C): controlledByApp=%s", 231 controlledByApp ? "true" : "false"); 232 233 Mutex::Autolock lock(mCore->mMutex); 234 235 if (mCore->mIsAbandoned) { 236 BQ_LOGE("connect(C): BufferQueue has been abandoned"); 237 return NO_INIT; 238 } 239 240 mCore->mConsumerListener = consumerListener; 241 mCore->mConsumerControlledByApp = controlledByApp; 242 243 return NO_ERROR; 244} 245 246status_t BufferQueueConsumer::disconnect() { 247 ATRACE_CALL(); 248 249 BQ_LOGV("disconnect(C)"); 250 251 Mutex::Autolock lock(mCore->mMutex); 252 253 if (mCore->mConsumerListener == NULL) { 254 BQ_LOGE("disconnect(C): no consumer is connected"); 255 return -EINVAL; 256 } 257 258 mCore->mIsAbandoned = true; 259 mCore->mConsumerListener = NULL; 260 mCore->mQueue.clear(); 261 mCore->freeAllBuffersLocked(); 262 mCore->mDequeueCondition.broadcast(); 263 return NO_ERROR; 264} 265 266status_t BufferQueueConsumer::getReleasedBuffers(uint32_t *outSlotMask) { 267 ATRACE_CALL(); 268 269 if (outSlotMask == NULL) { 270 BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL"); 271 return BAD_VALUE; 272 } 273 274 Mutex::Autolock lock(mCore->mMutex); 275 276 if (mCore->mIsAbandoned) { 277 BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned"); 278 return NO_INIT; 279 } 280 281 uint32_t mask = 0; 282 for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) { 283 if (!mSlots[s].mAcquireCalled) { 284 mask |= (1u << s); 285 } 286 } 287 288 // Remove from the mask queued buffers for which acquire has been called, 289 // since the consumer will not receive their buffer addresses and so must 290 // retain their cached information 291 BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin()); 292 while (current != mCore->mQueue.end()) { 293 if (current->mAcquireCalled) { 294 mask &= ~(1u << current->mSlot); 295 } 296 ++current; 297 } 298 299 BQ_LOGV("getReleasedBuffers: returning mask %#x", mask); 300 *outSlotMask = mask; 301 return NO_ERROR; 302} 303 304status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width, 305 uint32_t height) { 306 ATRACE_CALL(); 307 308 if (width == 0 || height == 0) { 309 BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u " 310 "height=%u)", width, height); 311 return BAD_VALUE; 312 } 313 314 BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height); 315 316 Mutex::Autolock lock(mCore->mMutex); 317 mCore->mDefaultWidth = width; 318 mCore->mDefaultHeight = height; 319 return NO_ERROR; 320} 321 322status_t BufferQueueConsumer::setDefaultMaxBufferCount(int bufferCount) { 323 ATRACE_CALL(); 324 Mutex::Autolock lock(mCore->mMutex); 325 return mCore->setDefaultMaxBufferCountLocked(bufferCount); 326} 327 328status_t BufferQueueConsumer::disableAsyncBuffer() { 329 ATRACE_CALL(); 330 331 Mutex::Autolock lock(mCore->mMutex); 332 333 if (mCore->mConsumerListener != NULL) { 334 BQ_LOGE("disableAsyncBuffer: consumer already connected"); 335 return INVALID_OPERATION; 336 } 337 338 BQ_LOGV("disableAsyncBuffer"); 339 mCore->mUseAsyncBuffer = false; 340 return NO_ERROR; 341} 342 343status_t BufferQueueConsumer::setMaxAcquiredBufferCount( 344 int maxAcquiredBuffers) { 345 ATRACE_CALL(); 346 347 if (maxAcquiredBuffers < 1 || 348 maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) { 349 BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d", 350 maxAcquiredBuffers); 351 return BAD_VALUE; 352 } 353 354 Mutex::Autolock lock(mCore->mMutex); 355 356 if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) { 357 BQ_LOGE("setMaxAcquiredBufferCount: producer is already connected"); 358 return INVALID_OPERATION; 359 } 360 361 BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers); 362 mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers; 363 return NO_ERROR; 364} 365 366void BufferQueueConsumer::setConsumerName(const String8& name) { 367 ATRACE_CALL(); 368 BQ_LOGV("setConsumerName: '%s'", name.string()); 369 Mutex::Autolock lock(mCore->mMutex); 370 mCore->mConsumerName = name; 371 mConsumerName = name; 372} 373 374status_t BufferQueueConsumer::setDefaultBufferFormat(uint32_t defaultFormat) { 375 ATRACE_CALL(); 376 BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat); 377 Mutex::Autolock lock(mCore->mMutex); 378 mCore->mDefaultBufferFormat = defaultFormat; 379 return NO_ERROR; 380} 381 382status_t BufferQueueConsumer::setConsumerUsageBits(uint32_t usage) { 383 ATRACE_CALL(); 384 BQ_LOGV("setConsumerUsageBits: %#x", usage); 385 Mutex::Autolock lock(mCore->mMutex); 386 mCore->mConsumerUsageBits = usage; 387 return NO_ERROR; 388} 389 390status_t BufferQueueConsumer::setTransformHint(uint32_t hint) { 391 ATRACE_CALL(); 392 BQ_LOGV("setTransformHint: %#x", hint); 393 Mutex::Autolock lock(mCore->mMutex); 394 mCore->mTransformHint = hint; 395 return NO_ERROR; 396} 397 398sp<NativeHandle> BufferQueueConsumer::getSidebandStream() const { 399 return mCore->mSidebandStream; 400} 401 402void BufferQueueConsumer::dump(String8& result, const char* prefix) const { 403 mCore->dump(result, prefix); 404} 405 406} // namespace android 407