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    // setMaxBufferCount sets the maximum value for the number of buffers used
192    // in the buffer queue (the initial default is NUM_BUFFER_SLOTS). If a call
193    // to setMaxAcquiredBufferCount (by the consumer), or a call to setAsyncMode
194    // or setMaxDequeuedBufferCount (by the producer), would cause this value to
195    // be exceeded then that call will fail. This call will fail if a producer
196    // is connected to the BufferQueue.
197    //
198    // The count must be between 1 and NUM_BUFFER_SLOTS, inclusive. The count
199    // cannot be less than maxAcquiredBufferCount.
200    //
201    // Return of a value other than NO_ERROR means an error has occurred:
202    // * BAD_VALUE - one of the below conditions occurred:
203    //             * bufferCount was out of range (see above).
204    //             * failure to adjust the number of available slots.
205    // * INVALID_OPERATION - attempting to call this after a producer connected.
206    virtual status_t setMaxBufferCount(int bufferCount) = 0;
207
208    // setMaxAcquiredBufferCount sets the maximum number of buffers that can
209    // be acquired by the consumer at one time (default 1). If this method
210    // succeeds, any new buffer slots will be both unallocated and owned by the
211    // BufferQueue object (i.e. they are not owned by the producer or consumer).
212    // Calling this may also cause some buffer slots to be emptied.
213    //
214    // This function should not be called with a value of maxAcquiredBuffers
215    // that is less than the number of currently acquired buffer slots. Doing so
216    // will result in a BAD_VALUE error.
217    //
218    // maxAcquiredBuffers must be (inclusive) between 1 and
219    // MAX_MAX_ACQUIRED_BUFFERS. It also cannot cause the maxBufferCount value
220    // to be exceeded.
221    //
222    // Return of a value other than NO_ERROR means an error has occurred:
223    // * NO_INIT - the buffer queue has been abandoned
224    // * BAD_VALUE - one of the below conditions occurred:
225    //             * maxAcquiredBuffers was out of range (see above).
226    //             * failure to adjust the number of available slots.
227    //             * client would have more than the requested number of
228    //               acquired buffers after this call
229    // * INVALID_OPERATION - attempting to call this after a producer connected.
230    virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers) = 0;
231
232    // setConsumerName sets the name used in logging
233    virtual void setConsumerName(const String8& name) = 0;
234
235    // setDefaultBufferFormat allows the BufferQueue to create
236    // GraphicBuffers of a defaultFormat if no format is specified
237    // in dequeueBuffer.
238    // The initial default is PIXEL_FORMAT_RGBA_8888.
239    //
240    // Return of a value other than NO_ERROR means an unknown error has occurred.
241    virtual status_t setDefaultBufferFormat(PixelFormat defaultFormat) = 0;
242
243    // setDefaultBufferDataSpace is a request to the producer to provide buffers
244    // of the indicated dataSpace. The producer may ignore this request.
245    // The initial default is HAL_DATASPACE_UNKNOWN.
246    //
247    // Return of a value other than NO_ERROR means an unknown error has occurred.
248    virtual status_t setDefaultBufferDataSpace(
249            android_dataspace defaultDataSpace) = 0;
250
251    // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
252    // These are merged with the bits passed to dequeueBuffer.  The values are
253    // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
254    //
255    // Return of a value other than NO_ERROR means an unknown error has occurred.
256    virtual status_t setConsumerUsageBits(uint32_t usage) = 0;
257
258    // setTransformHint bakes in rotation to buffers so overlays can be used.
259    // The values are enumerated in window.h, e.g.
260    // NATIVE_WINDOW_TRANSFORM_ROT_90.  The default is 0 (no transform).
261    //
262    // Return of a value other than NO_ERROR means an unknown error has occurred.
263    virtual status_t setTransformHint(uint32_t hint) = 0;
264
265    // Retrieve the sideband buffer stream, if any.
266    virtual sp<NativeHandle> getSidebandStream() const = 0;
267
268    // dump state into a string
269    virtual void dump(String8& result, const char* prefix) const = 0;
270
271public:
272    DECLARE_META_INTERFACE(GraphicBufferConsumer);
273};
274
275// ----------------------------------------------------------------------------
276
277class BnGraphicBufferConsumer : public BnInterface<IGraphicBufferConsumer>
278{
279public:
280    virtual status_t    onTransact( uint32_t code,
281                                    const Parcel& data,
282                                    Parcel* reply,
283                                    uint32_t flags = 0);
284};
285
286// ----------------------------------------------------------------------------
287}; // namespace android
288
289#endif // ANDROID_GUI_IGRAPHICBUFFERCONSUMER_H
290