BufferQueueConsumer.h revision 3e96f1982fda358424b0b75f394cbf7c1794a072
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_BUFFERQUEUECONSUMER_H
18#define ANDROID_GUI_BUFFERQUEUECONSUMER_H
19
20#include <EGL/egl.h>
21#include <EGL/eglext.h>
22
23#include <gui/BufferQueueDefs.h>
24#include <gui/IGraphicBufferConsumer.h>
25
26namespace android {
27
28class BufferQueueCore;
29
30class BufferQueueConsumer : public BnGraphicBufferConsumer {
31
32public:
33    BufferQueueConsumer(const sp<BufferQueueCore>& core);
34    virtual ~BufferQueueConsumer();
35
36    // acquireBuffer attempts to acquire ownership of the next pending buffer in
37    // the BufferQueue. If no buffer is pending then it returns
38    // NO_BUFFER_AVAILABLE. If a buffer is successfully acquired, the
39    // information about the buffer is returned in BufferItem.  If the buffer
40    // returned had previously been acquired then the BufferItem::mGraphicBuffer
41    // field of buffer is set to NULL and it is assumed that the consumer still
42    // holds a reference to the buffer.
43    //
44    // If expectedPresent is nonzero, it indicates the time when the buffer
45    // will be displayed on screen. If the buffer's timestamp is farther in the
46    // future, the buffer won't be acquired, and PRESENT_LATER will be
47    // returned.  The presentation time is in nanoseconds, and the time base
48    // is CLOCK_MONOTONIC.
49    virtual status_t acquireBuffer(BufferItem* outBuffer,
50            nsecs_t expectedPresent);
51
52    // releaseBuffer releases a buffer slot from the consumer back to the
53    // BufferQueue.  This may be done while the buffer's contents are still
54    // being accessed.  The fence will signal when the buffer is no longer
55    // in use. frameNumber is used to indentify the exact buffer returned.
56    //
57    // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
58    // any references to the just-released buffer that it might have, as if it
59    // had received a onBuffersReleased() call with a mask set for the released
60    // buffer.
61    //
62    // Note that the dependencies on EGL will be removed once we switch to using
63    // the Android HW Sync HAL.
64    virtual status_t releaseBuffer(int slot, uint64_t frameNumber,
65            const sp<Fence>& releaseFence, EGLDisplay display,
66            EGLSyncKHR fence);
67
68    // connect connects a consumer to the BufferQueue.  Only one
69    // consumer may be connected, and when that consumer disconnects the
70    // BufferQueue is placed into the "abandoned" state, causing most
71    // interactions with the BufferQueue by the producer to fail.
72    // controlledByApp indicates whether the consumer is controlled by
73    // the application.
74    //
75    // consumerListener may not be NULL.
76    virtual status_t connect(const sp<IConsumerListener>& consumerListener,
77            bool controlledByApp);
78
79    // disconnect disconnects a consumer from the BufferQueue. All
80    // buffers will be freed and the BufferQueue is placed in the "abandoned"
81    // state, causing most interactions with the BufferQueue by the producer to
82    // fail.
83    virtual status_t disconnect();
84
85    // getReleasedBuffers sets the value pointed to by outSlotMask to a bit mask
86    // indicating which buffer slots have been released by the BufferQueue
87    // but have not yet been released by the consumer.
88    //
89    // This should be called from the onBuffersReleased() callback.
90    virtual status_t getReleasedBuffers(uint32_t* outSlotMask);
91
92    // setDefaultBufferSize is used to set the size of buffers returned by
93    // dequeueBuffer when a width and height of zero is requested.  Default
94    // is 1x1.
95    virtual status_t setDefaultBufferSize(uint32_t width, uint32_t height);
96
97    // setDefaultMaxBufferCount sets the default value for the maximum buffer
98    // count (the initial default is 2). If the producer has requested a
99    // buffer count using setBufferCount, the default buffer count will only
100    // take effect if the producer sets the count back to zero.
101    //
102    // The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
103    virtual status_t setDefaultMaxBufferCount(int bufferCount);
104
105    // disableAsyncBuffer disables the extra buffer used in async mode
106    // (when both producer and consumer have set their "isControlledByApp"
107    // flag) and has dequeueBuffer() return WOULD_BLOCK instead.
108    //
109    // This can only be called before connect().
110    virtual status_t disableAsyncBuffer();
111
112    // setMaxAcquiredBufferCount sets the maximum number of buffers that can
113    // be acquired by the consumer at one time (default 1).  This call will
114    // fail if a producer is connected to the BufferQueue.
115    virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers);
116
117    // setConsumerName sets the name used in logging
118    virtual void setConsumerName(const String8& name);
119
120    // setDefaultBufferFormat allows the BufferQueue to create
121    // GraphicBuffers of a defaultFormat if no format is specified
122    // in dequeueBuffer.  Formats are enumerated in graphics.h; the
123    // initial default is HAL_PIXEL_FORMAT_RGBA_8888.
124    virtual status_t setDefaultBufferFormat(uint32_t defaultFormat);
125
126    // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
127    // These are merged with the bits passed to dequeueBuffer.  The values are
128    // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
129    virtual status_t setConsumerUsageBits(uint32_t usage);
130
131    // setTransformHint bakes in rotation to buffers so overlays can be used.
132    // The values are enumerated in window.h, e.g.
133    // NATIVE_WINDOW_TRANSFORM_ROT_90.  The default is 0 (no transform).
134    virtual status_t setTransformHint(uint32_t hint);
135
136    // dump our state in a String
137    virtual void dump(String8& result, const char* prefix) const;
138
139    // Functions required for backwards compatibility.
140    // These will be modified/renamed in IGraphicBufferConsumer and will be
141    // removed from this class at that time. See b/13306289.
142
143    virtual status_t releaseBuffer(int buf, uint64_t frameNumber,
144            EGLDisplay display, EGLSyncKHR fence,
145            const sp<Fence>& releaseFence) {
146        return releaseBuffer(buf, frameNumber, releaseFence, display, fence);
147    }
148
149    virtual status_t consumerConnect(const sp<IConsumerListener>& consumer,
150            bool controlledByApp) {
151        return connect(consumer, controlledByApp);
152    }
153
154    virtual status_t consumerDisconnect() { return disconnect(); }
155
156    // End functions required for backwards compatibility
157
158private:
159    sp<BufferQueueCore> mCore;
160
161    // This references mCore->mSlots. Lock mCore->mMutex while accessing.
162    BufferQueueDefs::SlotsType& mSlots;
163
164    // This is a cached copy of the name stored in the BufferQueueCore.
165    // It's updated during setConsumerName.
166    String8 mConsumerName;
167
168}; // class BufferQueueConsumer
169
170} // namespace android
171
172#endif
173