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