1/*
2 * Copyright (C) 2013 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_IGRAPHICBUFFERCONSUMER_H
18#define ANDROID_GUI_IGRAPHICBUFFERCONSUMER_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <utils/Errors.h>
24#include <utils/RefBase.h>
25#include <utils/Timers.h>
26
27#include <binder/IInterface.h>
28#include <ui/PixelFormat.h>
29#include <ui/Rect.h>
30
31#include <EGL/egl.h>
32#include <EGL/eglext.h>
33
34namespace android {
35// ----------------------------------------------------------------------------
36
37class BufferItem;
38class Fence;
39class GraphicBuffer;
40class IConsumerListener;
41class NativeHandle;
42
43class IGraphicBufferConsumer : public IInterface {
44
45public:
46    enum {
47        // Returned by releaseBuffer, after which the consumer must
48        // free any references to the just-released buffer that it might have.
49        STALE_BUFFER_SLOT = 1,
50        // Returned by dequeueBuffer if there are no pending buffers available.
51        NO_BUFFER_AVAILABLE,
52        // Returned by dequeueBuffer if it's too early for the buffer to be acquired.
53        PRESENT_LATER,
54    };
55
56    // acquireBuffer attempts to acquire ownership of the next pending buffer in
57    // the BufferQueue.  If no buffer is pending then it returns
58    // NO_BUFFER_AVAILABLE.  If a buffer is successfully acquired, the
59    // information about the buffer is returned in BufferItem.
60    //
61    // If the buffer returned had previously been
62    // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
63    // NULL and it is assumed that the consumer still holds a reference to the
64    // buffer.
65    //
66    // If presentWhen is non-zero, it indicates the time when the buffer will
67    // be displayed on screen.  If the buffer's timestamp is farther in the
68    // future, the buffer won't be acquired, and PRESENT_LATER will be
69    // returned.  The presentation time is in nanoseconds, and the time base
70    // is CLOCK_MONOTONIC.
71    //
72    // If maxFrameNumber is non-zero, it indicates that acquireBuffer should
73    // only return a buffer with a frame number less than or equal to
74    // maxFrameNumber. If no such frame is available (such as when a buffer has
75    // been replaced but the consumer has not received the onFrameReplaced
76    // callback), then PRESENT_LATER will be returned.
77    //
78    // Return of NO_ERROR means the operation completed as normal.
79    //
80    // Return of a positive value means the operation could not be completed
81    //    at this time, but the user should try again later:
82    // * NO_BUFFER_AVAILABLE - no buffer is pending (nothing queued by producer)
83    // * PRESENT_LATER - the buffer's timestamp is farther in the future
84    //
85    // Return of a negative value means an error has occurred:
86    // * INVALID_OPERATION - too many buffers have been acquired
87    virtual status_t acquireBuffer(BufferItem* buffer, nsecs_t presentWhen,
88            uint64_t maxFrameNumber = 0) = 0;
89
90    // detachBuffer attempts to remove all ownership of the buffer in the given
91    // slot from the buffer queue. If this call succeeds, the slot will be
92    // freed, and there will be no way to obtain the buffer from this interface.
93    // The freed slot will remain unallocated until either it is selected to
94    // hold a freshly allocated buffer in dequeueBuffer or a buffer is attached
95    // to the slot. The buffer must have already been acquired.
96    //
97    // Return of a value other than NO_ERROR means an error has occurred:
98    // * BAD_VALUE - the given slot number is invalid, either because it is
99    //               out of the range [0, NUM_BUFFER_SLOTS) or because the slot
100    //               it refers to is not currently acquired.
101    virtual status_t detachBuffer(int slot) = 0;
102
103    // attachBuffer attempts to transfer ownership of a buffer to the buffer
104    // queue. If this call succeeds, it will be as if this buffer was acquired
105    // from the returned slot number. As such, this call will fail if attaching
106    // this buffer would cause too many buffers to be simultaneously acquired.
107    //
108    // If the buffer is successfully attached, its frameNumber is initialized
109    // to 0. This must be passed into the releaseBuffer call or else the buffer
110    // will be deallocated as stale.
111    //
112    // Return of a value other than NO_ERROR means an error has occurred:
113    // * BAD_VALUE - outSlot or buffer were NULL, or the generation number of
114    //               the buffer did not match the buffer queue.
115    // * INVALID_OPERATION - cannot attach the buffer because it would cause too
116    //                       many buffers to be acquired.
117    // * NO_MEMORY - no free slots available
118    virtual status_t attachBuffer(int *outSlot,
119            const sp<GraphicBuffer>& buffer) = 0;
120
121    // releaseBuffer releases a buffer slot from the consumer back to the
122    // BufferQueue.  This may be done while the buffer's contents are still
123    // being accessed.  The fence will signal when the buffer is no longer
124    // in use. frameNumber is used to indentify the exact buffer returned.
125    //
126    // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
127    // any references to the just-released buffer that it might have, as if it
128    // had received a onBuffersReleased() call with a mask set for the released
129    // buffer.
130    //
131    // Note that the dependencies on EGL will be removed once we switch to using
132    // the Android HW Sync HAL.
133    //
134    // Return of NO_ERROR means the operation completed as normal.
135    //
136    // Return of a positive value means the operation could not be completed
137    //    at this time, but the user should try again later:
138    // * STALE_BUFFER_SLOT - see above (second paragraph)
139    //
140    // Return of a negative value means an error has occurred:
141    // * BAD_VALUE - one of the following could've happened:
142    //               * the buffer slot was invalid
143    //               * the fence was NULL
144    //               * the buffer slot specified is not in the acquired state
145    virtual status_t releaseBuffer(int buf, uint64_t frameNumber,
146            EGLDisplay display, EGLSyncKHR fence,
147            const sp<Fence>& releaseFence) = 0;
148
149    // consumerConnect connects a consumer to the BufferQueue.  Only one
150    // consumer may be connected, and when that consumer disconnects the
151    // BufferQueue is placed into the "abandoned" state, causing most
152    // interactions with the BufferQueue by the producer to fail.
153    // controlledByApp indicates whether the consumer is controlled by
154    // the application.
155    //
156    // consumer may not be NULL.
157    //
158    // Return of a value other than NO_ERROR means an error has occurred:
159    // * NO_INIT - the buffer queue has been abandoned
160    // * BAD_VALUE - a NULL consumer was provided
161    virtual status_t consumerConnect(const sp<IConsumerListener>& consumer, bool controlledByApp) = 0;
162
163    // consumerDisconnect disconnects a consumer from the BufferQueue. All
164    // buffers will be freed and the BufferQueue is placed in the "abandoned"
165    // state, causing most interactions with the BufferQueue by the producer to
166    // fail.
167    //
168    // Return of a value other than NO_ERROR means an error has occurred:
169    // * BAD_VALUE - no consumer is currently connected
170    virtual status_t consumerDisconnect() = 0;
171
172    // getReleasedBuffers sets the value pointed to by slotMask to a bit set.
173    // Each bit index with a 1 corresponds to a released buffer slot with that
174    // index value.  In particular, a released buffer is one that has
175    // been released by the BufferQueue but have not yet been released by the consumer.
176    //
177    // This should be called from the onBuffersReleased() callback.
178    //
179    // Return of a value other than NO_ERROR means an error has occurred:
180    // * NO_INIT - the buffer queue has been abandoned.
181    virtual status_t getReleasedBuffers(uint64_t* slotMask) = 0;
182
183    // setDefaultBufferSize is used to set the size of buffers returned by
184    // dequeueBuffer when a width and height of zero is requested.  Default
185    // is 1x1.
186    //
187    // Return of a value other than NO_ERROR means an error has occurred:
188    // * BAD_VALUE - either w or h was zero
189    virtual status_t setDefaultBufferSize(uint32_t w, uint32_t h) = 0;
190
191    // setDefaultMaxBufferCount sets the default value for the maximum buffer
192    // count (the initial default is 2). If the producer has requested a
193    // buffer count using setBufferCount, the default buffer count will only
194    // take effect if the producer sets the count back to zero.
195    //
196    // The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
197    //
198    // Return of a value other than NO_ERROR means an error has occurred:
199    // * BAD_VALUE - bufferCount was out of range (see above).
200    virtual status_t setDefaultMaxBufferCount(int bufferCount) = 0;
201
202    // disableAsyncBuffer disables the extra buffer used in async mode
203    // (when both producer and consumer have set their "isControlledByApp"
204    // flag) and has dequeueBuffer() return WOULD_BLOCK instead.
205    //
206    // This can only be called before consumerConnect().
207    //
208    // Return of a value other than NO_ERROR means an error has occurred:
209    // * INVALID_OPERATION - attempting to call this after consumerConnect.
210    virtual status_t disableAsyncBuffer() = 0;
211
212    // setMaxAcquiredBufferCount sets the maximum number of buffers that can
213    // be acquired by the consumer at one time (default 1).  This call will
214    // fail if a producer is connected to the BufferQueue.
215    //
216    // maxAcquiredBuffers must be (inclusive) between 1 and MAX_MAX_ACQUIRED_BUFFERS.
217    //
218    // Return of a value other than NO_ERROR means an error has occurred:
219    // * BAD_VALUE - maxAcquiredBuffers was out of range (see above).
220    // * INVALID_OPERATION - attempting to call this after a producer connected.
221    virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers) = 0;
222
223    // setConsumerName sets the name used in logging
224    virtual void setConsumerName(const String8& name) = 0;
225
226    // setDefaultBufferFormat allows the BufferQueue to create
227    // GraphicBuffers of a defaultFormat if no format is specified
228    // in dequeueBuffer.
229    // The initial default is PIXEL_FORMAT_RGBA_8888.
230    //
231    // Return of a value other than NO_ERROR means an unknown error has occurred.
232    virtual status_t setDefaultBufferFormat(PixelFormat defaultFormat) = 0;
233
234    // setDefaultBufferDataSpace is a request to the producer to provide buffers
235    // of the indicated dataSpace. The producer may ignore this request.
236    // The initial default is HAL_DATASPACE_UNKNOWN.
237    //
238    // Return of a value other than NO_ERROR means an unknown error has occurred.
239    virtual status_t setDefaultBufferDataSpace(
240            android_dataspace defaultDataSpace) = 0;
241
242    // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
243    // These are merged with the bits passed to dequeueBuffer.  The values are
244    // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
245    //
246    // Return of a value other than NO_ERROR means an unknown error has occurred.
247    virtual status_t setConsumerUsageBits(uint32_t usage) = 0;
248
249    // setTransformHint bakes in rotation to buffers so overlays can be used.
250    // The values are enumerated in window.h, e.g.
251    // NATIVE_WINDOW_TRANSFORM_ROT_90.  The default is 0 (no transform).
252    //
253    // Return of a value other than NO_ERROR means an unknown error has occurred.
254    virtual status_t setTransformHint(uint32_t hint) = 0;
255
256    // Retrieve the sideband buffer stream, if any.
257    virtual sp<NativeHandle> getSidebandStream() const = 0;
258
259    // dump state into a string
260    virtual void dump(String8& result, const char* prefix) const = 0;
261
262public:
263    DECLARE_META_INTERFACE(GraphicBufferConsumer);
264};
265
266// ----------------------------------------------------------------------------
267
268class BnGraphicBufferConsumer : public BnInterface<IGraphicBufferConsumer>
269{
270public:
271    virtual status_t    onTransact( uint32_t code,
272                                    const Parcel& data,
273                                    Parcel* reply,
274                                    uint32_t flags = 0);
275};
276
277// ----------------------------------------------------------------------------
278}; // namespace android
279
280#endif // ANDROID_GUI_IGRAPHICBUFFERCONSUMER_H
281