OSDN Git Service

Fix argument types in IGraphicBufferProducer methods
[android-x86/frameworks-native.git] / include / gui / BufferQueue.h
1 /*
2  * Copyright (C) 2012 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_BUFFERQUEUE_H
18 #define ANDROID_GUI_BUFFERQUEUE_H
19
20 #include <EGL/egl.h>
21 #include <EGL/eglext.h>
22
23 #include <gui/IGraphicBufferAlloc.h>
24 #include <gui/IGraphicBufferProducer.h>
25
26 #include <ui/Fence.h>
27 #include <ui/GraphicBuffer.h>
28
29 #include <utils/String8.h>
30 #include <utils/Vector.h>
31 #include <utils/threads.h>
32
33 namespace android {
34 // ----------------------------------------------------------------------------
35
36 class BufferQueue : public BnGraphicBufferProducer {
37 public:
38     enum { MIN_UNDEQUEUED_BUFFERS = 2 };
39     enum { NUM_BUFFER_SLOTS = 32 };
40     enum { NO_CONNECTED_API = 0 };
41     enum { INVALID_BUFFER_SLOT = -1 };
42     enum { STALE_BUFFER_SLOT = 1, NO_BUFFER_AVAILABLE };
43
44     // When in async mode we reserve two slots in order to guarantee that the
45     // producer and consumer can run asynchronously.
46     enum { MAX_MAX_ACQUIRED_BUFFERS = NUM_BUFFER_SLOTS - 2 };
47
48     // ConsumerListener is the interface through which the BufferQueue notifies
49     // the consumer of events that the consumer may wish to react to.  Because
50     // the consumer will generally have a mutex that is locked during calls from
51     // teh consumer to the BufferQueue, these calls from the BufferQueue to the
52     // consumer *MUST* be called only when the BufferQueue mutex is NOT locked.
53     struct ConsumerListener : public virtual RefBase {
54         // onFrameAvailable is called from queueBuffer each time an additional
55         // frame becomes available for consumption. This means that frames that
56         // are queued while in asynchronous mode only trigger the callback if no
57         // previous frames are pending. Frames queued while in synchronous mode
58         // always trigger the callback.
59         //
60         // This is called without any lock held and can be called concurrently
61         // by multiple threads.
62         virtual void onFrameAvailable() = 0;
63
64         // onBuffersReleased is called to notify the buffer consumer that the
65         // BufferQueue has released its references to one or more GraphicBuffers
66         // contained in its slots.  The buffer consumer should then call
67         // BufferQueue::getReleasedBuffers to retrieve the list of buffers
68         //
69         // This is called without any lock held and can be called concurrently
70         // by multiple threads.
71         virtual void onBuffersReleased() = 0;
72     };
73
74     // ProxyConsumerListener is a ConsumerListener implementation that keeps a weak
75     // reference to the actual consumer object.  It forwards all calls to that
76     // consumer object so long as it exists.
77     //
78     // This class exists to avoid having a circular reference between the
79     // BufferQueue object and the consumer object.  The reason this can't be a weak
80     // reference in the BufferQueue class is because we're planning to expose the
81     // consumer side of a BufferQueue as a binder interface, which doesn't support
82     // weak references.
83     class ProxyConsumerListener : public BufferQueue::ConsumerListener {
84     public:
85
86         ProxyConsumerListener(const wp<BufferQueue::ConsumerListener>& consumerListener);
87         virtual ~ProxyConsumerListener();
88         virtual void onFrameAvailable();
89         virtual void onBuffersReleased();
90
91     private:
92
93         // mConsumerListener is a weak reference to the ConsumerListener.  This is
94         // the raison d'etre of ProxyConsumerListener.
95         wp<BufferQueue::ConsumerListener> mConsumerListener;
96     };
97
98
99     // BufferQueue manages a pool of gralloc memory slots to be used by
100     // producers and consumers. allowSynchronousMode specifies whether or not
101     // synchronous mode can be enabled by the producer. allocator is used to
102     // allocate all the needed gralloc buffers.
103     BufferQueue(bool allowSynchronousMode = true,
104             const sp<IGraphicBufferAlloc>& allocator = NULL);
105     virtual ~BufferQueue();
106
107     virtual int query(int what, int* value);
108
109     // setBufferCount updates the number of available buffer slots.  After
110     // calling this all buffer slots are both unallocated and owned by the
111     // BufferQueue object (i.e. they are not owned by the client).
112     virtual status_t setBufferCount(int bufferCount);
113
114     virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf);
115
116     // dequeueBuffer gets the next buffer slot index for the client to use. If a
117     // buffer slot is available then that slot index is written to the location
118     // pointed to by the buf argument and a status of OK is returned.  If no
119     // slot is available then a status of -EBUSY is returned and buf is
120     // unmodified.
121     //
122     // The fence parameter will be updated to hold the fence associated with
123     // the buffer. The contents of the buffer must not be overwritten until the
124     // fence signals. If the fence is NULL, the buffer may be written
125     // immediately.
126     //
127     // The width and height parameters must be no greater than the minimum of
128     // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
129     // An error due to invalid dimensions might not be reported until
130     // updateTexImage() is called.
131     virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence,
132             uint32_t width, uint32_t height, uint32_t format, uint32_t usage);
133
134     // queueBuffer returns a filled buffer to the BufferQueue. In addition, a
135     // timestamp must be provided for the buffer. The timestamp is in
136     // nanoseconds, and must be monotonically increasing. Its other semantics
137     // (zero point, etc) are client-dependent and should be documented by the
138     // client.
139     virtual status_t queueBuffer(int buf,
140             const QueueBufferInput& input, QueueBufferOutput* output);
141
142     virtual void cancelBuffer(int buf, const sp<Fence>& fence);
143
144     // setSynchronousMode set whether dequeueBuffer is synchronous or
145     // asynchronous. In synchronous mode, dequeueBuffer blocks until
146     // a buffer is available, the currently bound buffer can be dequeued and
147     // queued buffers will be retired in order.
148     // The default mode is asynchronous.
149     virtual status_t setSynchronousMode(bool enabled);
150
151     // connect attempts to connect a producer client API to the BufferQueue.
152     // This must be called before any other IGraphicBufferProducer methods are called
153     // except for getAllocator.
154     //
155     // This method will fail if the connect was previously called on the
156     // BufferQueue and no corresponding disconnect call was made.
157     virtual status_t connect(int api, QueueBufferOutput* output);
158
159     // disconnect attempts to disconnect a producer client API from the
160     // BufferQueue. Calling this method will cause any subsequent calls to other
161     // IGraphicBufferProducer methods to fail except for getAllocator and connect.
162     // Successfully calling connect after this will allow the other methods to
163     // succeed again.
164     //
165     // This method will fail if the the BufferQueue is not currently
166     // connected to the specified client API.
167     virtual status_t disconnect(int api);
168
169     // dump our state in a String
170     virtual void dump(String8& result) const;
171     virtual void dump(String8& result, const char* prefix, char* buffer, size_t SIZE) const;
172
173     // public facing structure for BufferSlot
174     struct BufferItem {
175
176         BufferItem()
177          :
178            mTransform(0),
179            mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
180            mTimestamp(0),
181            mFrameNumber(0),
182            mBuf(INVALID_BUFFER_SLOT) {
183              mCrop.makeInvalid();
184          }
185         // mGraphicBuffer points to the buffer allocated for this slot, or is NULL
186         // if the buffer in this slot has been acquired in the past (see
187         // BufferSlot.mAcquireCalled).
188         sp<GraphicBuffer> mGraphicBuffer;
189
190         // mCrop is the current crop rectangle for this buffer slot.
191         Rect mCrop;
192
193         // mTransform is the current transform flags for this buffer slot.
194         uint32_t mTransform;
195
196         // mScalingMode is the current scaling mode for this buffer slot.
197         uint32_t mScalingMode;
198
199         // mTimestamp is the current timestamp for this buffer slot. This gets
200         // to set by queueBuffer each time this slot is queued.
201         int64_t mTimestamp;
202
203         // mFrameNumber is the number of the queued frame for this slot.
204         uint64_t mFrameNumber;
205
206         // mBuf is the slot index of this buffer
207         int mBuf;
208
209         // mFence is a fence that will signal when the buffer is idle.
210         sp<Fence> mFence;
211     };
212
213     // The following public functions is the consumer facing interface
214
215     // acquireBuffer attempts to acquire ownership of the next pending buffer in
216     // the BufferQueue.  If no buffer is pending then it returns -EINVAL.  If a
217     // buffer is successfully acquired, the information about the buffer is
218     // returned in BufferItem.  If the buffer returned had previously been
219     // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
220     // NULL and it is assumed that the consumer still holds a reference to the
221     // buffer.
222     status_t acquireBuffer(BufferItem *buffer);
223
224     // releaseBuffer releases a buffer slot from the consumer back to the
225     // BufferQueue pending a fence sync.
226     //
227     // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
228     // any references to the just-released buffer that it might have, as if it
229     // had received a onBuffersReleased() call with a mask set for the released
230     // buffer.
231     //
232     // Note that the dependencies on EGL will be removed once we switch to using
233     // the Android HW Sync HAL.
234     status_t releaseBuffer(int buf, EGLDisplay display, EGLSyncKHR fence,
235             const sp<Fence>& releaseFence);
236
237     // consumerConnect connects a consumer to the BufferQueue.  Only one
238     // consumer may be connected, and when that consumer disconnects the
239     // BufferQueue is placed into the "abandoned" state, causing most
240     // interactions with the BufferQueue by the producer to fail.
241     status_t consumerConnect(const sp<ConsumerListener>& consumer);
242
243     // consumerDisconnect disconnects a consumer from the BufferQueue. All
244     // buffers will be freed and the BufferQueue is placed in the "abandoned"
245     // state, causing most interactions with the BufferQueue by the producer to
246     // fail.
247     status_t consumerDisconnect();
248
249     // getReleasedBuffers sets the value pointed to by slotMask to a bit mask
250     // indicating which buffer slots the have been released by the BufferQueue
251     // but have not yet been released by the consumer.
252     status_t getReleasedBuffers(uint32_t* slotMask);
253
254     // setDefaultBufferSize is used to set the size of buffers returned by
255     // requestBuffers when a with and height of zero is requested.
256     status_t setDefaultBufferSize(uint32_t w, uint32_t h);
257
258     // setDefaultBufferCount set the buffer count. If the client has requested
259     // a buffer count using setBufferCount, the server-buffer count will
260     // take effect once the client sets the count back to zero.
261     status_t setDefaultMaxBufferCount(int bufferCount);
262
263     // setMaxAcquiredBufferCount sets the maximum number of buffers that can
264     // be acquired by the consumer at one time.  This call will fail if a
265     // producer is connected to the BufferQueue.
266     status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers);
267
268     // isSynchronousMode returns whether the BufferQueue is currently in
269     // synchronous mode.
270     bool isSynchronousMode() const;
271
272     // setConsumerName sets the name used in logging
273     void setConsumerName(const String8& name);
274
275     // setDefaultBufferFormat allows the BufferQueue to create
276     // GraphicBuffers of a defaultFormat if no format is specified
277     // in dequeueBuffer
278     status_t setDefaultBufferFormat(uint32_t defaultFormat);
279
280     // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer
281     status_t setConsumerUsageBits(uint32_t usage);
282
283     // setTransformHint bakes in rotation to buffers so overlays can be used
284     status_t setTransformHint(uint32_t hint);
285
286 private:
287     // freeBufferLocked frees the resources (both GraphicBuffer and EGLImage)
288     // for the given slot.
289     void freeBufferLocked(int index);
290
291     // freeAllBuffersLocked frees the resources (both GraphicBuffer and
292     // EGLImage) for all slots.
293     void freeAllBuffersLocked();
294
295     // freeAllBuffersExceptHeadLocked frees the resources (both GraphicBuffer
296     // and EGLImage) for all slots except the head of mQueue
297     void freeAllBuffersExceptHeadLocked();
298
299     // drainQueueLocked drains the buffer queue if we're in synchronous mode
300     // returns immediately otherwise. It returns NO_INIT if the BufferQueue
301     // became abandoned or disconnected during this call.
302     status_t drainQueueLocked();
303
304     // drainQueueAndFreeBuffersLocked drains the buffer queue if we're in
305     // synchronous mode and free all buffers. In asynchronous mode, all buffers
306     // are freed except the current buffer.
307     status_t drainQueueAndFreeBuffersLocked();
308
309     // setDefaultMaxBufferCountLocked sets the maximum number of buffer slots
310     // that will be used if the producer does not override the buffer slot
311     // count.
312     status_t setDefaultMaxBufferCountLocked(int count);
313
314     // getMinBufferCountLocked returns the minimum number of buffers allowed
315     // given the current BufferQueue state.
316     int getMinMaxBufferCountLocked() const;
317
318     // getMinUndequeuedBufferCountLocked returns the minimum number of buffers
319     // that must remain in a state other than DEQUEUED.
320     int getMinUndequeuedBufferCountLocked() const;
321
322     // getMaxBufferCountLocked returns the maximum number of buffers that can
323     // be allocated at once.  This value depends upon the following member
324     // variables:
325     //
326     //      mSynchronousMode
327     //      mMaxAcquiredBufferCount
328     //      mDefaultMaxBufferCount
329     //      mOverrideMaxBufferCount
330     //
331     // Any time one of these member variables is changed while a producer is
332     // connected, mDequeueCondition must be broadcast.
333     int getMaxBufferCountLocked() const;
334
335     struct BufferSlot {
336
337         BufferSlot()
338         : mEglDisplay(EGL_NO_DISPLAY),
339           mBufferState(BufferSlot::FREE),
340           mRequestBufferCalled(false),
341           mTransform(0),
342           mScalingMode(NATIVE_WINDOW_SCALING_MODE_FREEZE),
343           mTimestamp(0),
344           mFrameNumber(0),
345           mEglFence(EGL_NO_SYNC_KHR),
346           mAcquireCalled(false),
347           mNeedsCleanupOnRelease(false) {
348             mCrop.makeInvalid();
349         }
350
351         // mGraphicBuffer points to the buffer allocated for this slot or is NULL
352         // if no buffer has been allocated.
353         sp<GraphicBuffer> mGraphicBuffer;
354
355         // mEglDisplay is the EGLDisplay used to create mEglImage.
356         EGLDisplay mEglDisplay;
357
358         // BufferState represents the different states in which a buffer slot
359         // can be.
360         enum BufferState {
361             // FREE indicates that the buffer is not currently being used and
362             // will not be used in the future until it gets dequeued and
363             // subsequently queued by the client.
364             // aka "owned by BufferQueue, ready to be dequeued"
365             FREE = 0,
366
367             // DEQUEUED indicates that the buffer has been dequeued by the
368             // client, but has not yet been queued or canceled. The buffer is
369             // considered 'owned' by the client, and the server should not use
370             // it for anything.
371             //
372             // Note that when in synchronous-mode (mSynchronousMode == true),
373             // the buffer that's currently attached to the texture may be
374             // dequeued by the client.  That means that the current buffer can
375             // be in either the DEQUEUED or QUEUED state.  In asynchronous mode,
376             // however, the current buffer is always in the QUEUED state.
377             // aka "owned by producer, ready to be queued"
378             DEQUEUED = 1,
379
380             // QUEUED indicates that the buffer has been queued by the client,
381             // and has not since been made available for the client to dequeue.
382             // Attaching the buffer to the texture does NOT transition the
383             // buffer away from the QUEUED state. However, in Synchronous mode
384             // the current buffer may be dequeued by the client under some
385             // circumstances. See the note about the current buffer in the
386             // documentation for DEQUEUED.
387             // aka "owned by BufferQueue, ready to be acquired"
388             QUEUED = 2,
389
390             // aka "owned by consumer, ready to be released"
391             ACQUIRED = 3
392         };
393
394         // mBufferState is the current state of this buffer slot.
395         BufferState mBufferState;
396
397         // mRequestBufferCalled is used for validating that the client did
398         // call requestBuffer() when told to do so. Technically this is not
399         // needed but useful for debugging and catching client bugs.
400         bool mRequestBufferCalled;
401
402         // mCrop is the current crop rectangle for this buffer slot.
403         Rect mCrop;
404
405         // mTransform is the current transform flags for this buffer slot.
406         // (example: NATIVE_WINDOW_TRANSFORM_ROT_90)
407         uint32_t mTransform;
408
409         // mScalingMode is the current scaling mode for this buffer slot.
410         // (example: NATIVE_WINDOW_SCALING_MODE_FREEZE)
411         uint32_t mScalingMode;
412
413         // mTimestamp is the current timestamp for this buffer slot. This gets
414         // to set by queueBuffer each time this slot is queued.
415         int64_t mTimestamp;
416
417         // mFrameNumber is the number of the queued frame for this slot.
418         uint64_t mFrameNumber;
419
420         // mEglFence is the EGL sync object that must signal before the buffer
421         // associated with this buffer slot may be dequeued. It is initialized
422         // to EGL_NO_SYNC_KHR when the buffer is created and (optionally, based
423         // on a compile-time option) set to a new sync object in updateTexImage.
424         EGLSyncKHR mEglFence;
425
426         // mFence is a fence which will signal when work initiated by the
427         // previous owner of the buffer is finished. When the buffer is FREE,
428         // the fence indicates when the consumer has finished reading
429         // from the buffer, or when the producer has finished writing if it
430         // called cancelBuffer after queueing some writes. When the buffer is
431         // QUEUED, it indicates when the producer has finished filling the
432         // buffer. When the buffer is DEQUEUED or ACQUIRED, the fence has been
433         // passed to the consumer or producer along with ownership of the
434         // buffer, and mFence is empty.
435         sp<Fence> mFence;
436
437         // Indicates whether this buffer has been seen by a consumer yet
438         bool mAcquireCalled;
439
440         // Indicates whether this buffer needs to be cleaned up by consumer
441         bool mNeedsCleanupOnRelease;
442     };
443
444     // mSlots is the array of buffer slots that must be mirrored on the client
445     // side. This allows buffer ownership to be transferred between the client
446     // and server without sending a GraphicBuffer over binder. The entire array
447     // is initialized to NULL at construction time, and buffers are allocated
448     // for a slot when requestBuffer is called with that slot's index.
449     BufferSlot mSlots[NUM_BUFFER_SLOTS];
450
451     // mDefaultWidth holds the default width of allocated buffers. It is used
452     // in requestBuffers() if a width and height of zero is specified.
453     uint32_t mDefaultWidth;
454
455     // mDefaultHeight holds the default height of allocated buffers. It is used
456     // in requestBuffers() if a width and height of zero is specified.
457     uint32_t mDefaultHeight;
458
459     // mMaxAcquiredBufferCount is the number of buffers that the consumer may
460     // acquire at one time.  It defaults to 1 and can be changed by the
461     // consumer via the setMaxAcquiredBufferCount method, but this may only be
462     // done when no producer is connected to the BufferQueue.
463     //
464     // This value is used to derive the value returned for the
465     // MIN_UNDEQUEUED_BUFFERS query by the producer.
466     int mMaxAcquiredBufferCount;
467
468     // mDefaultMaxBufferCount is the default limit on the number of buffers
469     // that will be allocated at one time.  This default limit is set by the
470     // consumer.  The limit (as opposed to the default limit) may be
471     // overridden by the producer.
472     int mDefaultMaxBufferCount;
473
474     // mOverrideMaxBufferCount is the limit on the number of buffers that will
475     // be allocated at one time. This value is set by the image producer by
476     // calling setBufferCount. The default is zero, which means the producer
477     // doesn't care about the number of buffers in the pool. In that case
478     // mDefaultMaxBufferCount is used as the limit.
479     int mOverrideMaxBufferCount;
480
481     // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
482     // allocate new GraphicBuffer objects.
483     sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
484
485     // mConsumerListener is used to notify the connected consumer of
486     // asynchronous events that it may wish to react to.  It is initially set
487     // to NULL and is written by consumerConnect and consumerDisconnect.
488     sp<ConsumerListener> mConsumerListener;
489
490     // mSynchronousMode whether we're in synchronous mode or not
491     bool mSynchronousMode;
492
493     // mAllowSynchronousMode whether we allow synchronous mode or not
494     const bool mAllowSynchronousMode;
495
496     // mConnectedApi indicates the API that is currently connected to this
497     // BufferQueue.  It defaults to NO_CONNECTED_API (= 0), and gets updated
498     // by the connect and disconnect methods.
499     int mConnectedApi;
500
501     // mDequeueCondition condition used for dequeueBuffer in synchronous mode
502     mutable Condition mDequeueCondition;
503
504     // mQueue is a FIFO of queued buffers used in synchronous mode
505     typedef Vector<int> Fifo;
506     Fifo mQueue;
507
508     // mAbandoned indicates that the BufferQueue will no longer be used to
509     // consume images buffers pushed to it using the IGraphicBufferProducer interface.
510     // It is initialized to false, and set to true in the abandon method.  A
511     // BufferQueue that has been abandoned will return the NO_INIT error from
512     // all IGraphicBufferProducer methods capable of returning an error.
513     bool mAbandoned;
514
515     // mName is a string used to identify the BufferQueue in log messages.
516     // It is set by the setName method.
517     String8 mConsumerName;
518
519     // mMutex is the mutex used to prevent concurrent access to the member
520     // variables of BufferQueue objects. It must be locked whenever the
521     // member variables are accessed.
522     mutable Mutex mMutex;
523
524     // mFrameCounter is the free running counter, incremented for every buffer queued
525     // with the surface Texture.
526     uint64_t mFrameCounter;
527
528     // mBufferHasBeenQueued is true once a buffer has been queued.  It is reset
529     // by changing the buffer count.
530     bool mBufferHasBeenQueued;
531
532     // mDefaultBufferFormat can be set so it will override
533     // the buffer format when it isn't specified in dequeueBuffer
534     uint32_t mDefaultBufferFormat;
535
536     // mConsumerUsageBits contains flags the consumer wants for GraphicBuffers
537     uint32_t mConsumerUsageBits;
538
539     // mTransformHint is used to optimize for screen rotations
540     uint32_t mTransformHint;
541 };
542
543 // ----------------------------------------------------------------------------
544 }; // namespace android
545
546 #endif // ANDROID_GUI_BUFFERQUEUE_H