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