BufferQueueConsumer.cpp revision 20c5672883796c0dedf38f51dc2fc6f140b09ae6
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#include <inttypes.h>
18
19#define LOG_TAG "BufferQueueConsumer"
20#define ATRACE_TAG ATRACE_TAG_GRAPHICS
21//#define LOG_NDEBUG 0
22
23#include <gui/BufferItem.h>
24#include <gui/BufferQueueConsumer.h>
25#include <gui/BufferQueueCore.h>
26#include <gui/IConsumerListener.h>
27#include <gui/IProducerListener.h>
28
29namespace android {
30
31BufferQueueConsumer::BufferQueueConsumer(const sp<BufferQueueCore>& core) :
32    mCore(core),
33    mSlots(core->mSlots),
34    mConsumerName() {}
35
36BufferQueueConsumer::~BufferQueueConsumer() {}
37
38status_t BufferQueueConsumer::acquireBuffer(BufferItem* outBuffer,
39        nsecs_t expectedPresent) {
40    ATRACE_CALL();
41    Mutex::Autolock lock(mCore->mMutex);
42
43    // Check that the consumer doesn't currently have the maximum number of
44    // buffers acquired. We allow the max buffer count to be exceeded by one
45    // buffer so that the consumer can successfully set up the newly acquired
46    // buffer before releasing the old one.
47    int numAcquiredBuffers = 0;
48    for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
49        if (mSlots[s].mBufferState == BufferSlot::ACQUIRED) {
50            ++numAcquiredBuffers;
51        }
52    }
53    if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
54        BQ_LOGE("acquireBuffer: max acquired buffer count reached: %d (max %d)",
55                numAcquiredBuffers, mCore->mMaxAcquiredBufferCount);
56        return INVALID_OPERATION;
57    }
58
59    // Check if the queue is empty.
60    // In asynchronous mode the list is guaranteed to be one buffer deep,
61    // while in synchronous mode we use the oldest buffer.
62    if (mCore->mQueue.empty()) {
63        return NO_BUFFER_AVAILABLE;
64    }
65
66    BufferQueueCore::Fifo::iterator front(mCore->mQueue.begin());
67
68    // If expectedPresent is specified, we may not want to return a buffer yet.
69    // If it's specified and there's more than one buffer queued, we may want
70    // to drop a buffer.
71    if (expectedPresent != 0) {
72        const int MAX_REASONABLE_NSEC = 1000000000ULL; // 1 second
73
74        // The 'expectedPresent' argument indicates when the buffer is expected
75        // to be presented on-screen. If the buffer's desired present time is
76        // earlier (less) than expectedPresent -- meaning it will be displayed
77        // on time or possibly late if we show it as soon as possible -- we
78        // acquire and return it. If we don't want to display it until after the
79        // expectedPresent time, we return PRESENT_LATER without acquiring it.
80        //
81        // To be safe, we don't defer acquisition if expectedPresent is more
82        // than one second in the future beyond the desired present time
83        // (i.e., we'd be holding the buffer for a long time).
84        //
85        // NOTE: Code assumes monotonic time values from the system clock
86        // are positive.
87
88        // Start by checking to see if we can drop frames. We skip this check if
89        // the timestamps are being auto-generated by Surface. If the app isn't
90        // generating timestamps explicitly, it probably doesn't want frames to
91        // be discarded based on them.
92        while (mCore->mQueue.size() > 1 && !mCore->mQueue[0].mIsAutoTimestamp) {
93            // If entry[1] is timely, drop entry[0] (and repeat). We apply an
94            // additional criterion here: we only drop the earlier buffer if our
95            // desiredPresent falls within +/- 1 second of the expected present.
96            // Otherwise, bogus desiredPresent times (e.g., 0 or a small
97            // relative timestamp), which normally mean "ignore the timestamp
98            // and acquire immediately", would cause us to drop frames.
99            //
100            // We may want to add an additional criterion: don't drop the
101            // earlier buffer if entry[1]'s fence hasn't signaled yet.
102            const BufferItem& bufferItem(mCore->mQueue[1]);
103            nsecs_t desiredPresent = bufferItem.mTimestamp;
104            if (desiredPresent < expectedPresent - MAX_REASONABLE_NSEC ||
105                    desiredPresent > expectedPresent) {
106                // This buffer is set to display in the near future, or
107                // desiredPresent is garbage. Either way we don't want to drop
108                // the previous buffer just to get this on the screen sooner.
109                BQ_LOGV("acquireBuffer: nodrop desire=%" PRId64 " expect=%"
110                        PRId64 " (%" PRId64 ") now=%" PRId64,
111                        desiredPresent, expectedPresent,
112                        desiredPresent - expectedPresent,
113                        systemTime(CLOCK_MONOTONIC));
114                break;
115            }
116
117            BQ_LOGV("acquireBuffer: drop desire=%" PRId64 " expect=%" PRId64
118                    " size=%zu",
119                    desiredPresent, expectedPresent, mCore->mQueue.size());
120            if (mCore->stillTracking(front)) {
121                // Front buffer is still in mSlots, so mark the slot as free
122                mSlots[front->mSlot].mBufferState = BufferSlot::FREE;
123            }
124            mCore->mQueue.erase(front);
125            front = mCore->mQueue.begin();
126        }
127
128        // See if the front buffer is due
129        nsecs_t desiredPresent = front->mTimestamp;
130        if (desiredPresent > expectedPresent &&
131                desiredPresent < expectedPresent + MAX_REASONABLE_NSEC) {
132            BQ_LOGV("acquireBuffer: defer desire=%" PRId64 " expect=%" PRId64
133                    " (%" PRId64 ") now=%" PRId64,
134                    desiredPresent, expectedPresent,
135                    desiredPresent - expectedPresent,
136                    systemTime(CLOCK_MONOTONIC));
137            return PRESENT_LATER;
138        }
139
140        BQ_LOGV("acquireBuffer: accept desire=%" PRId64 " expect=%" PRId64 " "
141                "(%" PRId64 ") now=%" PRId64, desiredPresent, expectedPresent,
142                desiredPresent - expectedPresent,
143                systemTime(CLOCK_MONOTONIC));
144    }
145
146    int slot = front->mSlot;
147    *outBuffer = *front;
148    ATRACE_BUFFER_INDEX(slot);
149
150    BQ_LOGV("acquireBuffer: acquiring { slot=%d/%" PRIu64 " buffer=%p }",
151            slot, front->mFrameNumber, front->mGraphicBuffer->handle);
152    // If the front buffer is still being tracked, update its slot state
153    if (mCore->stillTracking(front)) {
154        mSlots[slot].mAcquireCalled = true;
155        mSlots[slot].mNeedsCleanupOnRelease = false;
156        mSlots[slot].mBufferState = BufferSlot::ACQUIRED;
157        mSlots[slot].mFence = Fence::NO_FENCE;
158    }
159
160    // If the buffer has previously been acquired by the consumer, set
161    // mGraphicBuffer to NULL to avoid unnecessarily remapping this buffer
162    // on the consumer side
163    if (outBuffer->mAcquireCalled) {
164        outBuffer->mGraphicBuffer = NULL;
165    }
166
167    mCore->mQueue.erase(front);
168
169    // We might have freed a slot while dropping old buffers, or the producer
170    // may be blocked waiting for the number of buffers in the queue to
171    // decrease.
172    mCore->mDequeueCondition.broadcast();
173
174    ATRACE_INT(mCore->mConsumerName.string(), mCore->mQueue.size());
175
176    return NO_ERROR;
177}
178
179status_t BufferQueueConsumer::detachBuffer(int slot) {
180    ATRACE_CALL();
181    ATRACE_BUFFER_INDEX(slot);
182    BQ_LOGV("detachBuffer(C): slot %d", slot);
183    Mutex::Autolock lock(mCore->mMutex);
184
185    if (mCore->mIsAbandoned) {
186        BQ_LOGE("detachBuffer(C): BufferQueue has been abandoned");
187        return NO_INIT;
188    }
189
190    if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
191        BQ_LOGE("detachBuffer(C): slot index %d out of range [0, %d)",
192                slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
193        return BAD_VALUE;
194    } else if (mSlots[slot].mBufferState != BufferSlot::ACQUIRED) {
195        BQ_LOGE("detachBuffer(C): slot %d is not owned by the consumer "
196                "(state = %d)", slot, mSlots[slot].mBufferState);
197        return BAD_VALUE;
198    }
199
200    mCore->freeBufferLocked(slot);
201    mCore->mDequeueCondition.broadcast();
202
203    return NO_ERROR;
204}
205
206status_t BufferQueueConsumer::attachBuffer(int* outSlot,
207        const sp<android::GraphicBuffer>& buffer) {
208    ATRACE_CALL();
209
210    if (outSlot == NULL) {
211        BQ_LOGE("attachBuffer(P): outSlot must not be NULL");
212        return BAD_VALUE;
213    } else if (buffer == NULL) {
214        BQ_LOGE("attachBuffer(P): cannot attach NULL buffer");
215        return BAD_VALUE;
216    }
217
218    Mutex::Autolock lock(mCore->mMutex);
219
220    // Make sure we don't have too many acquired buffers and find a free slot
221    // to put the buffer into (the oldest if there are multiple).
222    int numAcquiredBuffers = 0;
223    int found = BufferQueueCore::INVALID_BUFFER_SLOT;
224    for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
225        if (mSlots[s].mBufferState == BufferSlot::ACQUIRED) {
226            ++numAcquiredBuffers;
227        } else if (mSlots[s].mBufferState == BufferSlot::FREE) {
228            if (found == BufferQueueCore::INVALID_BUFFER_SLOT ||
229                    mSlots[s].mFrameNumber < mSlots[found].mFrameNumber) {
230                found = s;
231            }
232        }
233    }
234
235    if (numAcquiredBuffers >= mCore->mMaxAcquiredBufferCount + 1) {
236        BQ_LOGE("attachBuffer(P): max acquired buffer count reached: %d "
237                "(max %d)", numAcquiredBuffers,
238                mCore->mMaxAcquiredBufferCount);
239        return INVALID_OPERATION;
240    }
241    if (found == BufferQueueCore::INVALID_BUFFER_SLOT) {
242        BQ_LOGE("attachBuffer(P): could not find free buffer slot");
243        return NO_MEMORY;
244    }
245
246    *outSlot = found;
247    ATRACE_BUFFER_INDEX(*outSlot);
248    BQ_LOGV("attachBuffer(C): returning slot %d", *outSlot);
249
250    // If these are modified, they also need to be modified in
251    // CpuConsumer::attachAndReleaseBuffer
252    mSlots[*outSlot].mGraphicBuffer = buffer;
253    mSlots[*outSlot].mFence = Fence::NO_FENCE;
254    mSlots[*outSlot].mFrameNumber = 0;
255
256    // Changes to these do not need to be propagated to CpuConsumer
257    mSlots[*outSlot].mBufferState = BufferSlot::ACQUIRED;
258    mSlots[*outSlot].mAttachedByConsumer = true;
259    mSlots[*outSlot].mNeedsCleanupOnRelease = false;
260
261    // mAcquireCalled tells BufferQueue that it doesn't need to send a valid
262    // GraphicBuffer pointer on the next acquireBuffer call, which decreases
263    // Binder traffic by not un/flattening the GraphicBuffer. However, it
264    // requires that the consumer maintain a cached copy of the slot <--> buffer
265    // mappings, which is why the consumer doesn't need the valid pointer on
266    // acquire.
267    //
268    // The StreamSplitter is one of the primary users of the attach/detach
269    // logic, and while it is running, all buffers it acquires are immediately
270    // detached, and all buffers it eventually releases are ones that were
271    // attached (as opposed to having been obtained from acquireBuffer), so it
272    // doesn't make sense to maintain the slot/buffer mappings, which would
273    // become invalid for every buffer during detach/attach. By setting this to
274    // false, the valid GraphicBuffer pointer will always be sent with acquire
275    // for attached buffers.
276    mSlots[*outSlot].mAcquireCalled = false;
277
278    return NO_ERROR;
279}
280
281status_t BufferQueueConsumer::releaseBuffer(int slot, uint64_t frameNumber,
282        const sp<Fence>& releaseFence, EGLDisplay eglDisplay,
283        EGLSyncKHR eglFence) {
284    ATRACE_CALL();
285    ATRACE_BUFFER_INDEX(slot);
286
287    if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS ||
288            releaseFence == NULL) {
289        return BAD_VALUE;
290    }
291
292    sp<IProducerListener> listener;
293    { // Autolock scope
294        Mutex::Autolock lock(mCore->mMutex);
295
296        // If the frame number has changed because the buffer has been reallocated,
297        // we can ignore this releaseBuffer for the old buffer
298        if (frameNumber != mSlots[slot].mFrameNumber) {
299            return STALE_BUFFER_SLOT;
300        }
301
302        // Make sure this buffer hasn't been queued while acquired by the consumer
303        BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
304        while (current != mCore->mQueue.end()) {
305            if (current->mSlot == slot) {
306                BQ_LOGE("releaseBuffer: buffer slot %d pending release is "
307                        "currently queued", slot);
308                return BAD_VALUE;
309            }
310            ++current;
311        }
312
313        if (mSlots[slot].mBufferState == BufferSlot::ACQUIRED) {
314            mSlots[slot].mEglDisplay = eglDisplay;
315            mSlots[slot].mEglFence = eglFence;
316            mSlots[slot].mFence = releaseFence;
317            mSlots[slot].mBufferState = BufferSlot::FREE;
318            listener = mCore->mConnectedProducerListener;
319            BQ_LOGV("releaseBuffer: releasing slot %d", slot);
320        } else if (mSlots[slot].mNeedsCleanupOnRelease) {
321            BQ_LOGV("releaseBuffer: releasing a stale buffer slot %d "
322                    "(state = %d)", slot, mSlots[slot].mBufferState);
323            mSlots[slot].mNeedsCleanupOnRelease = false;
324            return STALE_BUFFER_SLOT;
325        } else {
326            BQ_LOGV("releaseBuffer: attempted to release buffer slot %d "
327                    "but its state was %d", slot, mSlots[slot].mBufferState);
328            return BAD_VALUE;
329        }
330
331        mCore->mDequeueCondition.broadcast();
332    } // Autolock scope
333
334    // Call back without lock held
335    if (listener != NULL) {
336        listener->onBufferReleased();
337    }
338
339    return NO_ERROR;
340}
341
342status_t BufferQueueConsumer::connect(
343        const sp<IConsumerListener>& consumerListener, bool controlledByApp) {
344    ATRACE_CALL();
345
346    if (consumerListener == NULL) {
347        BQ_LOGE("connect(C): consumerListener may not be NULL");
348        return BAD_VALUE;
349    }
350
351    BQ_LOGV("connect(C): controlledByApp=%s",
352            controlledByApp ? "true" : "false");
353
354    Mutex::Autolock lock(mCore->mMutex);
355
356    if (mCore->mIsAbandoned) {
357        BQ_LOGE("connect(C): BufferQueue has been abandoned");
358        return NO_INIT;
359    }
360
361    mCore->mConsumerListener = consumerListener;
362    mCore->mConsumerControlledByApp = controlledByApp;
363
364    return NO_ERROR;
365}
366
367status_t BufferQueueConsumer::disconnect() {
368    ATRACE_CALL();
369
370    BQ_LOGV("disconnect(C)");
371
372    Mutex::Autolock lock(mCore->mMutex);
373
374    if (mCore->mConsumerListener == NULL) {
375        BQ_LOGE("disconnect(C): no consumer is connected");
376        return BAD_VALUE;
377    }
378
379    mCore->mIsAbandoned = true;
380    mCore->mConsumerListener = NULL;
381    mCore->mQueue.clear();
382    mCore->freeAllBuffersLocked();
383    mCore->mDequeueCondition.broadcast();
384    return NO_ERROR;
385}
386
387status_t BufferQueueConsumer::getReleasedBuffers(uint64_t *outSlotMask) {
388    ATRACE_CALL();
389
390    if (outSlotMask == NULL) {
391        BQ_LOGE("getReleasedBuffers: outSlotMask may not be NULL");
392        return BAD_VALUE;
393    }
394
395    Mutex::Autolock lock(mCore->mMutex);
396
397    if (mCore->mIsAbandoned) {
398        BQ_LOGE("getReleasedBuffers: BufferQueue has been abandoned");
399        return NO_INIT;
400    }
401
402    uint64_t mask = 0;
403    for (int s = 0; s < BufferQueueDefs::NUM_BUFFER_SLOTS; ++s) {
404        if (!mSlots[s].mAcquireCalled) {
405            mask |= (1ULL << s);
406        }
407    }
408
409    // Remove from the mask queued buffers for which acquire has been called,
410    // since the consumer will not receive their buffer addresses and so must
411    // retain their cached information
412    BufferQueueCore::Fifo::iterator current(mCore->mQueue.begin());
413    while (current != mCore->mQueue.end()) {
414        if (current->mAcquireCalled) {
415            mask &= ~(1ULL << current->mSlot);
416        }
417        ++current;
418    }
419
420    BQ_LOGV("getReleasedBuffers: returning mask %#" PRIx64, mask);
421    *outSlotMask = mask;
422    return NO_ERROR;
423}
424
425status_t BufferQueueConsumer::setDefaultBufferSize(uint32_t width,
426        uint32_t height) {
427    ATRACE_CALL();
428
429    if (width == 0 || height == 0) {
430        BQ_LOGV("setDefaultBufferSize: dimensions cannot be 0 (width=%u "
431                "height=%u)", width, height);
432        return BAD_VALUE;
433    }
434
435    BQ_LOGV("setDefaultBufferSize: width=%u height=%u", width, height);
436
437    Mutex::Autolock lock(mCore->mMutex);
438    mCore->mDefaultWidth = width;
439    mCore->mDefaultHeight = height;
440    return NO_ERROR;
441}
442
443status_t BufferQueueConsumer::setDefaultMaxBufferCount(int bufferCount) {
444    ATRACE_CALL();
445    Mutex::Autolock lock(mCore->mMutex);
446    return mCore->setDefaultMaxBufferCountLocked(bufferCount);
447}
448
449status_t BufferQueueConsumer::disableAsyncBuffer() {
450    ATRACE_CALL();
451
452    Mutex::Autolock lock(mCore->mMutex);
453
454    if (mCore->mConsumerListener != NULL) {
455        BQ_LOGE("disableAsyncBuffer: consumer already connected");
456        return INVALID_OPERATION;
457    }
458
459    BQ_LOGV("disableAsyncBuffer");
460    mCore->mUseAsyncBuffer = false;
461    return NO_ERROR;
462}
463
464status_t BufferQueueConsumer::setMaxAcquiredBufferCount(
465        int maxAcquiredBuffers) {
466    ATRACE_CALL();
467
468    if (maxAcquiredBuffers < 1 ||
469            maxAcquiredBuffers > BufferQueueCore::MAX_MAX_ACQUIRED_BUFFERS) {
470        BQ_LOGE("setMaxAcquiredBufferCount: invalid count %d",
471                maxAcquiredBuffers);
472        return BAD_VALUE;
473    }
474
475    Mutex::Autolock lock(mCore->mMutex);
476
477    if (mCore->mConnectedApi != BufferQueueCore::NO_CONNECTED_API) {
478        BQ_LOGE("setMaxAcquiredBufferCount: producer is already connected");
479        return INVALID_OPERATION;
480    }
481
482    BQ_LOGV("setMaxAcquiredBufferCount: %d", maxAcquiredBuffers);
483    mCore->mMaxAcquiredBufferCount = maxAcquiredBuffers;
484    return NO_ERROR;
485}
486
487void BufferQueueConsumer::setConsumerName(const String8& name) {
488    ATRACE_CALL();
489    BQ_LOGV("setConsumerName: '%s'", name.string());
490    Mutex::Autolock lock(mCore->mMutex);
491    mCore->mConsumerName = name;
492    mConsumerName = name;
493}
494
495status_t BufferQueueConsumer::setDefaultBufferFormat(PixelFormat defaultFormat) {
496    ATRACE_CALL();
497    BQ_LOGV("setDefaultBufferFormat: %u", defaultFormat);
498    Mutex::Autolock lock(mCore->mMutex);
499    mCore->mDefaultBufferFormat = defaultFormat;
500    return NO_ERROR;
501}
502
503status_t BufferQueueConsumer::setDefaultBufferDataSpace(
504        android_dataspace defaultDataSpace) {
505    ATRACE_CALL();
506    BQ_LOGV("setDefaultBufferDataSpace: %u", defaultDataSpace);
507    Mutex::Autolock lock(mCore->mMutex);
508    mCore->mDefaultBufferDataSpace = defaultDataSpace;
509    return NO_ERROR;
510}
511
512status_t BufferQueueConsumer::setConsumerUsageBits(uint32_t usage) {
513    ATRACE_CALL();
514    BQ_LOGV("setConsumerUsageBits: %#x", usage);
515    Mutex::Autolock lock(mCore->mMutex);
516    mCore->mConsumerUsageBits = usage;
517    return NO_ERROR;
518}
519
520status_t BufferQueueConsumer::setTransformHint(uint32_t hint) {
521    ATRACE_CALL();
522    BQ_LOGV("setTransformHint: %#x", hint);
523    Mutex::Autolock lock(mCore->mMutex);
524    mCore->mTransformHint = hint;
525    return NO_ERROR;
526}
527
528sp<NativeHandle> BufferQueueConsumer::getSidebandStream() const {
529    return mCore->mSidebandStream;
530}
531
532void BufferQueueConsumer::dump(String8& result, const char* prefix) const {
533    mCore->dump(result, prefix);
534}
535
536} // namespace android
537