BufferQueue.cpp revision febd4f4f462444bfcb3f0618d07ac77e3fc1f6ad
1/*
2 * Copyright (C) 2012 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 "BufferQueue"
18#define ATRACE_TAG ATRACE_TAG_GRAPHICS
19//#define LOG_NDEBUG 0
20
21#include <gui/BufferQueue.h>
22#include <gui/BufferQueueCore.h>
23
24namespace android {
25
26BufferQueue::ProxyConsumerListener::ProxyConsumerListener(
27        const wp<ConsumerListener>& consumerListener):
28        mConsumerListener(consumerListener) {}
29
30BufferQueue::ProxyConsumerListener::~ProxyConsumerListener() {}
31
32void BufferQueue::ProxyConsumerListener::onFrameAvailable() {
33    sp<ConsumerListener> listener(mConsumerListener.promote());
34    if (listener != NULL) {
35        listener->onFrameAvailable();
36    }
37}
38
39void BufferQueue::ProxyConsumerListener::onBuffersReleased() {
40    sp<ConsumerListener> listener(mConsumerListener.promote());
41    if (listener != NULL) {
42        listener->onBuffersReleased();
43    }
44}
45
46void BufferQueue::createBufferQueue(sp<IGraphicBufferProducer>* outProducer,
47        sp<IGraphicBufferConsumer>* outConsumer,
48        const sp<IGraphicBufferAlloc>& allocator) {
49    LOG_ALWAYS_FATAL_IF(outProducer == NULL,
50            "BufferQueue: outProducer must not be NULL");
51    LOG_ALWAYS_FATAL_IF(outConsumer == NULL,
52            "BufferQueue: outConsumer must not be NULL");
53
54    sp<BufferQueueCore> core(new BufferQueueCore(allocator));
55    LOG_ALWAYS_FATAL_IF(core == NULL,
56            "BufferQueue: failed to create BufferQueueCore");
57
58    sp<IGraphicBufferProducer> producer(new BufferQueueProducer(core));
59    LOG_ALWAYS_FATAL_IF(producer == NULL,
60            "BufferQueue: failed to create BufferQueueProducer");
61
62    sp<IGraphicBufferConsumer> consumer(new BufferQueueConsumer(core));
63    LOG_ALWAYS_FATAL_IF(consumer == NULL,
64            "BufferQueue: failed to create BufferQueueConsumer");
65
66    *outProducer = producer;
67    *outConsumer = consumer;
68}
69
70BufferQueue::BufferQueue(const sp<IGraphicBufferAlloc>& allocator) :
71    mProducer(),
72    mConsumer()
73{
74    sp<BufferQueueCore> core(new BufferQueueCore(allocator));
75    mProducer = new BufferQueueProducer(core);
76    mConsumer = new BufferQueueConsumer(core);
77}
78
79BufferQueue::~BufferQueue() {}
80
81void BufferQueue::binderDied(const wp<IBinder>& who) {
82    mProducer->binderDied(who);
83}
84
85int BufferQueue::query(int what, int* outValue) {
86    return mProducer->query(what, outValue);
87}
88
89status_t BufferQueue::setBufferCount(int bufferCount) {
90    return mProducer->setBufferCount(bufferCount);
91}
92
93status_t BufferQueue::requestBuffer(int slot, sp<GraphicBuffer>* buf) {
94    return mProducer->requestBuffer(slot, buf);
95}
96
97status_t BufferQueue::dequeueBuffer(int *outBuf, sp<Fence>* outFence, bool async,
98        uint32_t w, uint32_t h, uint32_t format, uint32_t usage) {
99    return mProducer->dequeueBuffer(outBuf, outFence, async, w, h, format, usage);
100}
101
102status_t BufferQueue::detachProducerBuffer(int slot) {
103    return mProducer->detachBuffer(slot);
104}
105
106status_t BufferQueue::attachProducerBuffer(int* slot,
107        const sp<GraphicBuffer>& buffer) {
108    return mProducer->attachBuffer(slot, buffer);
109}
110
111status_t BufferQueue::queueBuffer(int buf,
112        const QueueBufferInput& input, QueueBufferOutput* output) {
113    return mProducer->queueBuffer(buf, input, output);
114}
115
116void BufferQueue::cancelBuffer(int buf, const sp<Fence>& fence) {
117    mProducer->cancelBuffer(buf, fence);
118}
119
120status_t BufferQueue::connect(const sp<IBinder>& token,
121        int api, bool producerControlledByApp, QueueBufferOutput* output) {
122    return mProducer->connect(token, api, producerControlledByApp, output);
123}
124
125status_t BufferQueue::disconnect(int api) {
126    return mProducer->disconnect(api);
127}
128
129status_t BufferQueue::setSidebandStream(const sp<NativeHandle>& stream) {
130    return mProducer->setSidebandStream(stream);
131}
132
133status_t BufferQueue::acquireBuffer(BufferItem* buffer, nsecs_t presentWhen) {
134    return mConsumer->acquireBuffer(buffer, presentWhen);
135}
136
137status_t BufferQueue::detachConsumerBuffer(int slot) {
138    return mConsumer->detachBuffer(slot);
139}
140
141status_t BufferQueue::attachConsumerBuffer(int* slot,
142        const sp<GraphicBuffer>& buffer) {
143    return mConsumer->attachBuffer(slot, buffer);
144}
145
146status_t BufferQueue::releaseBuffer(
147        int buf, uint64_t frameNumber, EGLDisplay display,
148        EGLSyncKHR eglFence, const sp<Fence>& fence) {
149    return mConsumer->releaseBuffer(buf, frameNumber, fence, display, eglFence);
150}
151
152status_t BufferQueue::consumerConnect(const sp<IConsumerListener>& consumerListener,
153        bool controlledByApp) {
154    return mConsumer->connect(consumerListener, controlledByApp);
155}
156
157status_t BufferQueue::consumerDisconnect() {
158    return mConsumer->disconnect();
159}
160
161status_t BufferQueue::getReleasedBuffers(uint64_t* slotMask) {
162    return mConsumer->getReleasedBuffers(slotMask);
163}
164
165status_t BufferQueue::setDefaultBufferSize(uint32_t w, uint32_t h) {
166    return mConsumer->setDefaultBufferSize(w, h);
167}
168
169status_t BufferQueue::setDefaultMaxBufferCount(int bufferCount) {
170    return mConsumer->setDefaultMaxBufferCount(bufferCount);
171}
172
173status_t BufferQueue::disableAsyncBuffer() {
174    return mConsumer->disableAsyncBuffer();
175}
176
177status_t BufferQueue::setMaxAcquiredBufferCount(int maxAcquiredBuffers) {
178    return mConsumer->setMaxAcquiredBufferCount(maxAcquiredBuffers);
179}
180
181void BufferQueue::setConsumerName(const String8& name) {
182    mConsumer->setConsumerName(name);
183}
184
185status_t BufferQueue::setDefaultBufferFormat(uint32_t defaultFormat) {
186    return mConsumer->setDefaultBufferFormat(defaultFormat);
187}
188
189status_t BufferQueue::setConsumerUsageBits(uint32_t usage) {
190    return mConsumer->setConsumerUsageBits(usage);
191}
192
193status_t BufferQueue::setTransformHint(uint32_t hint) {
194    return mConsumer->setTransformHint(hint);
195}
196
197sp<NativeHandle> BufferQueue::getSidebandStream() const {
198    return mConsumer->getSidebandStream();
199}
200
201void BufferQueue::dump(String8& result, const char* prefix) const {
202    mConsumer->dump(result, prefix);
203}
204
205void BufferQueue::ProxyConsumerListener::onSidebandStreamChanged() {
206    sp<ConsumerListener> listener(mConsumerListener.promote());
207    if (listener != NULL) {
208        listener->onSidebandStreamChanged();
209    }
210}
211
212}; // namespace android
213