IGraphicBufferConsumer.h revision d723bd7669b4fc88dc282d8bf8ba5ecb2849d22f
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 Fence;
38class GraphicBuffer;
39class IConsumerListener;
40class NativeHandle;
41
42class IGraphicBufferConsumer : public IInterface {
43
44public:
45
46    // public facing structure for BufferSlot
47    class BufferItem : public Flattenable<BufferItem> {
48        friend class Flattenable<BufferItem>;
49        size_t getPodSize() const;
50        size_t getFlattenedSize() const;
51        size_t getFdCount() const;
52        status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
53        status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
54
55    public:
56        // The default value of mBuf, used to indicate this doesn't correspond to a slot.
57        enum { INVALID_BUFFER_SLOT = -1 };
58        BufferItem();
59
60        // mGraphicBuffer points to the buffer allocated for this slot, or is NULL
61        // if the buffer in this slot has been acquired in the past (see
62        // BufferSlot.mAcquireCalled).
63        sp<GraphicBuffer> mGraphicBuffer;
64
65        // mFence is a fence that will signal when the buffer is idle.
66        sp<Fence> mFence;
67
68        // mCrop is the current crop rectangle for this buffer slot.
69        Rect mCrop;
70
71        // mTransform is the current transform flags for this buffer slot.
72        // refer to NATIVE_WINDOW_TRANSFORM_* in <window.h>
73        uint32_t mTransform;
74
75        // mScalingMode is the current scaling mode for this buffer slot.
76        // refer to NATIVE_WINDOW_SCALING_* in <window.h>
77        uint32_t mScalingMode;
78
79        // mTimestamp is the current timestamp for this buffer slot. This gets
80        // to set by queueBuffer each time this slot is queued. This value
81        // is guaranteed to be monotonically increasing for each newly
82        // acquired buffer.
83        int64_t mTimestamp;
84
85        // mIsAutoTimestamp indicates whether mTimestamp was generated
86        // automatically when the buffer was queued.
87        bool mIsAutoTimestamp;
88
89        // mFrameNumber is the number of the queued frame for this slot.
90        uint64_t mFrameNumber;
91
92        // mBuf is the slot index of this buffer (default INVALID_BUFFER_SLOT).
93        int mBuf;
94
95        // mIsDroppable whether this buffer was queued with the
96        // property that it can be replaced by a new buffer for the purpose of
97        // making sure dequeueBuffer() won't block.
98        // i.e.: was the BufferQueue in "mDequeueBufferCannotBlock" when this buffer
99        // was queued.
100        bool mIsDroppable;
101
102        // Indicates whether this buffer has been seen by a consumer yet
103        bool mAcquireCalled;
104
105        // Indicates this buffer must be transformed by the inverse transform of the screen
106        // it is displayed onto. This is applied after mTransform.
107        bool mTransformToDisplayInverse;
108    };
109
110    enum {
111        // Returned by releaseBuffer, after which the consumer must
112        // free any references to the just-released buffer that it might have.
113        STALE_BUFFER_SLOT = 1,
114        // Returned by dequeueBuffer if there are no pending buffers available.
115        NO_BUFFER_AVAILABLE,
116        // Returned by dequeueBuffer if it's too early for the buffer to be acquired.
117        PRESENT_LATER,
118    };
119
120    // acquireBuffer attempts to acquire ownership of the next pending buffer in
121    // the BufferQueue.  If no buffer is pending then it returns
122    // NO_BUFFER_AVAILABLE.  If a buffer is successfully acquired, the
123    // information about the buffer is returned in BufferItem.
124    //
125    // If the buffer returned had previously been
126    // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
127    // NULL and it is assumed that the consumer still holds a reference to the
128    // buffer.
129    //
130    // If presentWhen is non-zero, it indicates the time when the buffer will
131    // be displayed on screen.  If the buffer's timestamp is farther in the
132    // future, the buffer won't be acquired, and PRESENT_LATER will be
133    // returned.  The presentation time is in nanoseconds, and the time base
134    // is CLOCK_MONOTONIC.
135    //
136    // Return of NO_ERROR means the operation completed as normal.
137    //
138    // Return of a positive value means the operation could not be completed
139    //    at this time, but the user should try again later:
140    // * NO_BUFFER_AVAILABLE - no buffer is pending (nothing queued by producer)
141    // * PRESENT_LATER - the buffer's timestamp is farther in the future
142    //
143    // Return of a negative value means an error has occurred:
144    // * INVALID_OPERATION - too many buffers have been acquired
145    virtual status_t acquireBuffer(BufferItem* buffer, nsecs_t presentWhen) = 0;
146
147    // detachBuffer attempts to remove all ownership of the buffer in the given
148    // slot from the buffer queue. If this call succeeds, the slot will be
149    // freed, and there will be no way to obtain the buffer from this interface.
150    // The freed slot will remain unallocated until either it is selected to
151    // hold a freshly allocated buffer in dequeueBuffer or a buffer is attached
152    // to the slot. The buffer must have already been acquired.
153    //
154    // Return of a value other than NO_ERROR means an error has occurred:
155    // * BAD_VALUE - the given slot number is invalid, either because it is
156    //               out of the range [0, NUM_BUFFER_SLOTS) or because the slot
157    //               it refers to is not currently acquired.
158    virtual status_t detachBuffer(int slot) = 0;
159
160    // attachBuffer attempts to transfer ownership of a buffer to the buffer
161    // queue. If this call succeeds, it will be as if this buffer was acquired
162    // from the returned slot number. As such, this call will fail if attaching
163    // this buffer would cause too many buffers to be simultaneously acquired.
164    //
165    // If the buffer is successfully attached, its frameNumber is initialized
166    // to 0. This must be passed into the releaseBuffer call or else the buffer
167    // will be deallocated as stale.
168    //
169    // Return of a value other than NO_ERROR means an error has occurred:
170    // * BAD_VALUE - outSlot or buffer were NULL
171    // * INVALID_OPERATION - cannot attach the buffer because it would cause too
172    //                       many buffers to be acquired.
173    // * NO_MEMORY - no free slots available
174    virtual status_t attachBuffer(int *outSlot,
175            const sp<GraphicBuffer>& buffer) = 0;
176
177    // releaseBuffer releases a buffer slot from the consumer back to the
178    // BufferQueue.  This may be done while the buffer's contents are still
179    // being accessed.  The fence will signal when the buffer is no longer
180    // in use. frameNumber is used to indentify the exact buffer returned.
181    //
182    // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
183    // any references to the just-released buffer that it might have, as if it
184    // had received a onBuffersReleased() call with a mask set for the released
185    // buffer.
186    //
187    // Note that the dependencies on EGL will be removed once we switch to using
188    // the Android HW Sync HAL.
189    //
190    // Return of NO_ERROR means the operation completed as normal.
191    //
192    // Return of a positive value means the operation could not be completed
193    //    at this time, but the user should try again later:
194    // * STALE_BUFFER_SLOT - see above (second paragraph)
195    //
196    // Return of a negative value means an error has occurred:
197    // * BAD_VALUE - one of the following could've happened:
198    //               * the buffer slot was invalid
199    //               * the fence was NULL
200    //               * the buffer slot specified is not in the acquired state
201    virtual status_t releaseBuffer(int buf, uint64_t frameNumber,
202            EGLDisplay display, EGLSyncKHR fence,
203            const sp<Fence>& releaseFence) = 0;
204
205    // consumerConnect connects a consumer to the BufferQueue.  Only one
206    // consumer may be connected, and when that consumer disconnects the
207    // BufferQueue is placed into the "abandoned" state, causing most
208    // interactions with the BufferQueue by the producer to fail.
209    // controlledByApp indicates whether the consumer is controlled by
210    // the application.
211    //
212    // consumer may not be NULL.
213    //
214    // Return of a value other than NO_ERROR means an error has occurred:
215    // * NO_INIT - the buffer queue has been abandoned
216    // * BAD_VALUE - a NULL consumer was provided
217    virtual status_t consumerConnect(const sp<IConsumerListener>& consumer, bool controlledByApp) = 0;
218
219    // consumerDisconnect disconnects a consumer from the BufferQueue. All
220    // buffers will be freed and the BufferQueue is placed in the "abandoned"
221    // state, causing most interactions with the BufferQueue by the producer to
222    // fail.
223    //
224    // Return of a value other than NO_ERROR means an error has occurred:
225    // * BAD_VALUE - no consumer is currently connected
226    virtual status_t consumerDisconnect() = 0;
227
228    // getReleasedBuffers sets the value pointed to by slotMask to a bit set.
229    // Each bit index with a 1 corresponds to a released buffer slot with that
230    // index value.  In particular, a released buffer is one that has
231    // been released by the BufferQueue but have not yet been released by the consumer.
232    //
233    // This should be called from the onBuffersReleased() callback.
234    //
235    // Return of a value other than NO_ERROR means an error has occurred:
236    // * NO_INIT - the buffer queue has been abandoned.
237    virtual status_t getReleasedBuffers(uint64_t* slotMask) = 0;
238
239    // setDefaultBufferSize is used to set the size of buffers returned by
240    // dequeueBuffer when a width and height of zero is requested.  Default
241    // is 1x1.
242    //
243    // Return of a value other than NO_ERROR means an error has occurred:
244    // * BAD_VALUE - either w or h was zero
245    virtual status_t setDefaultBufferSize(uint32_t w, uint32_t h) = 0;
246
247    // setDefaultMaxBufferCount sets the default value for the maximum buffer
248    // count (the initial default is 2). If the producer has requested a
249    // buffer count using setBufferCount, the default buffer count will only
250    // take effect if the producer sets the count back to zero.
251    //
252    // The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
253    //
254    // Return of a value other than NO_ERROR means an error has occurred:
255    // * BAD_VALUE - bufferCount was out of range (see above).
256    virtual status_t setDefaultMaxBufferCount(int bufferCount) = 0;
257
258    // disableAsyncBuffer disables the extra buffer used in async mode
259    // (when both producer and consumer have set their "isControlledByApp"
260    // flag) and has dequeueBuffer() return WOULD_BLOCK instead.
261    //
262    // This can only be called before consumerConnect().
263    //
264    // Return of a value other than NO_ERROR means an error has occurred:
265    // * INVALID_OPERATION - attempting to call this after consumerConnect.
266    virtual status_t disableAsyncBuffer() = 0;
267
268    // setMaxAcquiredBufferCount sets the maximum number of buffers that can
269    // be acquired by the consumer at one time (default 1).  This call will
270    // fail if a producer is connected to the BufferQueue.
271    //
272    // maxAcquiredBuffers must be (inclusive) between 1 and MAX_MAX_ACQUIRED_BUFFERS.
273    //
274    // Return of a value other than NO_ERROR means an error has occurred:
275    // * BAD_VALUE - maxAcquiredBuffers was out of range (see above).
276    // * INVALID_OPERATION - attempting to call this after a producer connected.
277    virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers) = 0;
278
279    // setConsumerName sets the name used in logging
280    virtual void setConsumerName(const String8& name) = 0;
281
282    // setDefaultBufferFormat allows the BufferQueue to create
283    // GraphicBuffers of a defaultFormat if no format is specified
284    // in dequeueBuffer.
285    // The initial default is PIXEL_FORMAT_RGBA_8888.
286    //
287    // Return of a value other than NO_ERROR means an unknown error has occurred.
288    virtual status_t setDefaultBufferFormat(PixelFormat defaultFormat) = 0;
289
290    // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
291    // These are merged with the bits passed to dequeueBuffer.  The values are
292    // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
293    //
294    // Return of a value other than NO_ERROR means an unknown error has occurred.
295    virtual status_t setConsumerUsageBits(uint32_t usage) = 0;
296
297    // setTransformHint bakes in rotation to buffers so overlays can be used.
298    // The values are enumerated in window.h, e.g.
299    // NATIVE_WINDOW_TRANSFORM_ROT_90.  The default is 0 (no transform).
300    //
301    // Return of a value other than NO_ERROR means an unknown error has occurred.
302    virtual status_t setTransformHint(uint32_t hint) = 0;
303
304    // Retrieve the sideband buffer stream, if any.
305    virtual sp<NativeHandle> getSidebandStream() const = 0;
306
307    // dump state into a string
308    virtual void dump(String8& result, const char* prefix) const = 0;
309
310public:
311    DECLARE_META_INTERFACE(GraphicBufferConsumer);
312};
313
314// ----------------------------------------------------------------------------
315
316class BnGraphicBufferConsumer : public BnInterface<IGraphicBufferConsumer>
317{
318public:
319    virtual status_t    onTransact( uint32_t code,
320                                    const Parcel& data,
321                                    Parcel* reply,
322                                    uint32_t flags = 0);
323};
324
325// ----------------------------------------------------------------------------
326}; // namespace android
327
328#endif // ANDROID_GUI_IGRAPHICBUFFERCONSUMER_H
329