GraphicBufferListener.cpp revision e7f4e676bb88b17241d71731f9ea50c18cfcb039
1/*
2 * Copyright (C) 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_NDEBUG 0
18#define LOG_TAG "GraphicBufferListener"
19
20#include <media/stagefright/foundation/ADebug.h>
21#include <media/stagefright/foundation/AMessage.h>
22#include <media/stagefright/MediaErrors.h>
23
24#include "GraphicBufferListener.h"
25
26namespace android {
27
28status_t GraphicBufferListener::init(
29        const sp<AMessage> &notify,
30        size_t bufferWidth, size_t bufferHeight, size_t bufferCount) {
31    mNotify = notify;
32
33    String8 name("GraphicBufferListener");
34    BufferQueue::createBufferQueue(&mProducer, &mConsumer);
35    mConsumer->setConsumerName(name);
36    mConsumer->setDefaultBufferSize(bufferWidth, bufferHeight);
37    mConsumer->setConsumerUsageBits(GRALLOC_USAGE_SW_READ_OFTEN);
38
39    status_t err = mConsumer->setMaxAcquiredBufferCount(bufferCount);
40    if (err != NO_ERROR) {
41        ALOGE("Unable to set BQ max acquired buffer count to %u: %d",
42                bufferCount, err);
43        return err;
44    }
45
46    wp<BufferQueue::ConsumerListener> listener =
47        static_cast<BufferQueue::ConsumerListener*>(this);
48    sp<BufferQueue::ProxyConsumerListener> proxy =
49        new BufferQueue::ProxyConsumerListener(listener);
50
51    err = mConsumer->consumerConnect(proxy, false);
52    if (err != NO_ERROR) {
53        ALOGE("Error connecting to BufferQueue: %s (%d)",
54                strerror(-err), err);
55        return err;
56    }
57
58    ALOGV("init() successful.");
59
60    return OK;
61}
62
63void GraphicBufferListener::onFrameAvailable() {
64    ALOGV("onFrameAvailable() called");
65
66    {
67        Mutex::Autolock autoLock(mMutex);
68        mNumFramesAvailable++;
69    }
70
71    sp<AMessage> notify = mNotify->dup();
72    mNotify->setWhat(kWhatFrameAvailable);
73    mNotify->post();
74}
75
76void GraphicBufferListener::onBuffersReleased() {
77    ALOGV("onBuffersReleased() called");
78    // nothing to do
79}
80
81void GraphicBufferListener::onSidebandStreamChanged() {
82    ALOGW("GraphicBufferListener cannot consume sideband streams.");
83    // nothing to do
84}
85
86BufferQueue::BufferItem GraphicBufferListener::getBufferItem() {
87    BufferQueue::BufferItem item;
88
89    {
90        Mutex::Autolock autoLock(mMutex);
91        if (mNumFramesAvailable <= 0) {
92            ALOGE("getBuffer() called with no frames available");
93            return item;
94        }
95        mNumFramesAvailable--;
96    }
97
98    status_t err = mConsumer->acquireBuffer(&item, 0);
99    if (err == BufferQueue::NO_BUFFER_AVAILABLE) {
100        // shouldn't happen, since we track num frames available
101        ALOGE("frame was not available");
102        item.mBuf = -1;
103        return item;
104    } else if (err != OK) {
105        ALOGE("acquireBuffer returned err=%d", err);
106        item.mBuf = -1;
107        return item;
108    }
109
110    // Wait for it to become available.
111    err = item.mFence->waitForever("GraphicBufferListener::getBufferItem");
112    if (err != OK) {
113        ALOGW("failed to wait for buffer fence: %d", err);
114        // keep going
115    }
116
117    // If this is the first time we're seeing this buffer, add it to our
118    // slot table.
119    if (item.mGraphicBuffer != NULL) {
120        ALOGV("setting mBufferSlot %d", item.mBuf);
121        mBufferSlot[item.mBuf] = item.mGraphicBuffer;
122    }
123
124    return item;
125}
126
127sp<GraphicBuffer> GraphicBufferListener::getBuffer(
128        BufferQueue::BufferItem item) {
129    sp<GraphicBuffer> buf;
130    if (item.mBuf < 0 || item.mBuf >= BufferQueue::NUM_BUFFER_SLOTS) {
131        ALOGE("getBuffer() received invalid BufferItem: mBuf==%d", item.mBuf);
132        return buf;
133    }
134
135    buf = mBufferSlot[item.mBuf];
136    CHECK(buf.get() != NULL);
137
138    return buf;
139}
140
141status_t GraphicBufferListener::releaseBuffer(
142        BufferQueue::BufferItem item) {
143    if (item.mBuf < 0 || item.mBuf >= BufferQueue::NUM_BUFFER_SLOTS) {
144        ALOGE("getBuffer() received invalid BufferItem: mBuf==%d", item.mBuf);
145        return ERROR_OUT_OF_RANGE;
146    }
147
148    mConsumer->releaseBuffer(item.mBuf, item.mFrameNumber,
149            EGL_NO_DISPLAY, EGL_NO_SYNC_KHR, Fence::NO_FENCE);
150
151    return OK;
152}
153
154}   // namespace android
155