1289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza/*
2289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * Copyright 2014 The Android Open Source Project
3289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza *
4289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * Licensed under the Apache License, Version 2.0 (the "License");
5289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * you may not use this file except in compliance with the License.
6289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * You may obtain a copy of the License at
7289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza *
8289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza *      http://www.apache.org/licenses/LICENSE-2.0
9289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza *
10289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * Unless required by applicable law or agreed to in writing, software
11289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * distributed under the License is distributed on an "AS IS" BASIS,
12289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * See the License for the specific language governing permissions and
14289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * limitations under the License.
15289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza */
16289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
17289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza#ifndef ANDROID_GUI_BUFFERQUEUECONSUMER_H
18289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza#define ANDROID_GUI_BUFFERQUEUECONSUMER_H
19289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
20289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza#include <EGL/egl.h>
21289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza#include <EGL/eglext.h>
22289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
233e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza#include <gui/BufferQueueDefs.h>
24289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza#include <gui/IGraphicBufferConsumer.h>
25289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
26289ade165e60b5f71734d30e535f16eb1f4313adDan Stozanamespace android {
27289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
28289ade165e60b5f71734d30e535f16eb1f4313adDan Stozaclass BufferQueueCore;
29289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
30289ade165e60b5f71734d30e535f16eb1f4313adDan Stozaclass BufferQueueConsumer : public BnGraphicBufferConsumer {
31289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
32289ade165e60b5f71734d30e535f16eb1f4313adDan Stozapublic:
33289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    BufferQueueConsumer(const sp<BufferQueueCore>& core);
34289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    virtual ~BufferQueueConsumer();
35289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
36289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // acquireBuffer attempts to acquire ownership of the next pending buffer in
37289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // the BufferQueue. If no buffer is pending then it returns
38289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // NO_BUFFER_AVAILABLE. If a buffer is successfully acquired, the
39289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // information about the buffer is returned in BufferItem.  If the buffer
40289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // returned had previously been acquired then the BufferItem::mGraphicBuffer
41289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // field of buffer is set to NULL and it is assumed that the consumer still
42289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // holds a reference to the buffer.
43289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    //
44289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // If expectedPresent is nonzero, it indicates the time when the buffer
45289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // will be displayed on screen. If the buffer's timestamp is farther in the
46289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // future, the buffer won't be acquired, and PRESENT_LATER will be
47289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // returned.  The presentation time is in nanoseconds, and the time base
48289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // is CLOCK_MONOTONIC.
49289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    virtual status_t acquireBuffer(BufferItem* outBuffer,
50289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza            nsecs_t expectedPresent);
51289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
529f3053de78630815d60cf48a2cf2348cc5867c45Dan Stoza    // See IGraphicBufferConsumer::detachBuffer
539f3053de78630815d60cf48a2cf2348cc5867c45Dan Stoza    virtual status_t detachBuffer(int slot);
549f3053de78630815d60cf48a2cf2348cc5867c45Dan Stoza
559f3053de78630815d60cf48a2cf2348cc5867c45Dan Stoza    // See IGraphicBufferConsumer::attachBuffer
569f3053de78630815d60cf48a2cf2348cc5867c45Dan Stoza    virtual status_t attachBuffer(int* slot, const sp<GraphicBuffer>& buffer);
579f3053de78630815d60cf48a2cf2348cc5867c45Dan Stoza
58289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // releaseBuffer releases a buffer slot from the consumer back to the
59289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // BufferQueue.  This may be done while the buffer's contents are still
60289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // being accessed.  The fence will signal when the buffer is no longer
61289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // in use. frameNumber is used to indentify the exact buffer returned.
62289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    //
63289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
64289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // any references to the just-released buffer that it might have, as if it
65289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // had received a onBuffersReleased() call with a mask set for the released
66289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // buffer.
67289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    //
68289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // Note that the dependencies on EGL will be removed once we switch to using
69289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // the Android HW Sync HAL.
70289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    virtual status_t releaseBuffer(int slot, uint64_t frameNumber,
71289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza            const sp<Fence>& releaseFence, EGLDisplay display,
72289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza            EGLSyncKHR fence);
73289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
74289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // connect connects a consumer to the BufferQueue.  Only one
75289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // consumer may be connected, and when that consumer disconnects the
76289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // BufferQueue is placed into the "abandoned" state, causing most
77289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // interactions with the BufferQueue by the producer to fail.
78289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // controlledByApp indicates whether the consumer is controlled by
79289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // the application.
80289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    //
81289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // consumerListener may not be NULL.
82289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    virtual status_t connect(const sp<IConsumerListener>& consumerListener,
83289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza            bool controlledByApp);
84289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
85289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // disconnect disconnects a consumer from the BufferQueue. All
86289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // buffers will be freed and the BufferQueue is placed in the "abandoned"
87289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // state, causing most interactions with the BufferQueue by the producer to
88289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // fail.
89289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    virtual status_t disconnect();
90289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
91289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // getReleasedBuffers sets the value pointed to by outSlotMask to a bit mask
92289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // indicating which buffer slots have been released by the BufferQueue
93289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // but have not yet been released by the consumer.
94289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    //
95289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // This should be called from the onBuffersReleased() callback.
96febd4f4f462444bfcb3f0618d07ac77e3fc1f6adDan Stoza    virtual status_t getReleasedBuffers(uint64_t* outSlotMask);
97289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
98289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // setDefaultBufferSize is used to set the size of buffers returned by
99289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // dequeueBuffer when a width and height of zero is requested.  Default
100289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // is 1x1.
101289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    virtual status_t setDefaultBufferSize(uint32_t width, uint32_t height);
102289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
103289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // setDefaultMaxBufferCount sets the default value for the maximum buffer
104289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // count (the initial default is 2). If the producer has requested a
105289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // buffer count using setBufferCount, the default buffer count will only
106289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // take effect if the producer sets the count back to zero.
107289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    //
108289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
109289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    virtual status_t setDefaultMaxBufferCount(int bufferCount);
110289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
111289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // disableAsyncBuffer disables the extra buffer used in async mode
112289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // (when both producer and consumer have set their "isControlledByApp"
113289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // flag) and has dequeueBuffer() return WOULD_BLOCK instead.
114289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    //
115289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // This can only be called before connect().
116289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    virtual status_t disableAsyncBuffer();
117289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
118289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // setMaxAcquiredBufferCount sets the maximum number of buffers that can
119289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // be acquired by the consumer at one time (default 1).  This call will
120289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // fail if a producer is connected to the BufferQueue.
121289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers);
122289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
123289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // setConsumerName sets the name used in logging
124289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    virtual void setConsumerName(const String8& name);
125289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
126289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // setDefaultBufferFormat allows the BufferQueue to create
127289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // GraphicBuffers of a defaultFormat if no format is specified
128289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // in dequeueBuffer.  Formats are enumerated in graphics.h; the
129289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // initial default is HAL_PIXEL_FORMAT_RGBA_8888.
130289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    virtual status_t setDefaultBufferFormat(uint32_t defaultFormat);
131289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
132289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
133289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // These are merged with the bits passed to dequeueBuffer.  The values are
134289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
135289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    virtual status_t setConsumerUsageBits(uint32_t usage);
136289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
137289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // setTransformHint bakes in rotation to buffers so overlays can be used.
138289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // The values are enumerated in window.h, e.g.
139289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // NATIVE_WINDOW_TRANSFORM_ROT_90.  The default is 0 (no transform).
140289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    virtual status_t setTransformHint(uint32_t hint);
141289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
142399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall    // Retrieve the sideband buffer stream, if any.
143399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall    virtual sp<NativeHandle> getSidebandStream() const;
144399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall
145289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // dump our state in a String
146289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    virtual void dump(String8& result, const char* prefix) const;
147289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
1483e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // Functions required for backwards compatibility.
1493e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // These will be modified/renamed in IGraphicBufferConsumer and will be
1503e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // removed from this class at that time. See b/13306289.
1513e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
1523e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    virtual status_t releaseBuffer(int buf, uint64_t frameNumber,
1533e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza            EGLDisplay display, EGLSyncKHR fence,
1543e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza            const sp<Fence>& releaseFence) {
1553e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza        return releaseBuffer(buf, frameNumber, releaseFence, display, fence);
1563e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    }
1573e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
1583e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    virtual status_t consumerConnect(const sp<IConsumerListener>& consumer,
1593e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza            bool controlledByApp) {
1603e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza        return connect(consumer, controlledByApp);
1613e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    }
1623e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
1633e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    virtual status_t consumerDisconnect() { return disconnect(); }
1643e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
1653e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // End functions required for backwards compatibility
1663e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
167289ade165e60b5f71734d30e535f16eb1f4313adDan Stozaprivate:
168289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    sp<BufferQueueCore> mCore;
1693e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
1703e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // This references mCore->mSlots. Lock mCore->mMutex while accessing.
1713e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    BufferQueueDefs::SlotsType& mSlots;
1723e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
1733e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // This is a cached copy of the name stored in the BufferQueueCore.
1743e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // It's updated during setConsumerName.
1753e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    String8 mConsumerName;
176289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
177289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza}; // class BufferQueueConsumer
178289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
179289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza} // namespace android
180289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
181289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza#endif
182