16b091c53000c843211c218ce40287a7edca9bc63Daniel Lam/*
26b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * Copyright (C) 2012 The Android Open Source Project
36b091c53000c843211c218ce40287a7edca9bc63Daniel Lam *
46b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * Licensed under the Apache License, Version 2.0 (the "License");
56b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * you may not use this file except in compliance with the License.
66b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * You may obtain a copy of the License at
76b091c53000c843211c218ce40287a7edca9bc63Daniel Lam *
86b091c53000c843211c218ce40287a7edca9bc63Daniel Lam *      http://www.apache.org/licenses/LICENSE-2.0
96b091c53000c843211c218ce40287a7edca9bc63Daniel Lam *
106b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * Unless required by applicable law or agreed to in writing, software
116b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * distributed under the License is distributed on an "AS IS" BASIS,
126b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * See the License for the specific language governing permissions and
146b091c53000c843211c218ce40287a7edca9bc63Daniel Lam * limitations under the License.
156b091c53000c843211c218ce40287a7edca9bc63Daniel Lam */
166b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#ifndef ANDROID_GUI_BUFFERQUEUE_H
186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#define ANDROID_GUI_BUFFERQUEUE_H
196b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
206b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#include <EGL/egl.h>
21f71c4ae136f7749b9dfdaa2dd64d771868eeeb2dDaniel Lam#include <EGL/eglext.h>
226b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
23365857df8b94c959dea984a63013f6e7730ef976Mathias Agopian#include <binder/IBinder.h>
24365857df8b94c959dea984a63013f6e7730ef976Mathias Agopian
25a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian#include <gui/IConsumerListener.h>
2690ac799241f077a7b7e6c1875fd933864c8dd2a7Mathias Agopian#include <gui/IGraphicBufferAlloc.h>
272adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden#include <gui/IGraphicBufferProducer.h>
28a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian#include <gui/IGraphicBufferConsumer.h>
296b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
30ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall#include <ui/Fence.h>
316b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#include <ui/GraphicBuffer.h>
326b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
336b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#include <utils/String8.h>
346b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#include <utils/Vector.h>
356b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#include <utils/threads.h>
366b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
376b091c53000c843211c218ce40287a7edca9bc63Daniel Lamnamespace android {
386b091c53000c843211c218ce40287a7edca9bc63Daniel Lam// ----------------------------------------------------------------------------
396b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
40365857df8b94c959dea984a63013f6e7730ef976Mathias Agopianclass BufferQueue : public BnGraphicBufferProducer,
41365857df8b94c959dea984a63013f6e7730ef976Mathias Agopian                    public BnGraphicBufferConsumer,
42365857df8b94c959dea984a63013f6e7730ef976Mathias Agopian                    private IBinder::DeathRecipient {
436b091c53000c843211c218ce40287a7edca9bc63Daniel Lampublic:
446b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    enum { MIN_UNDEQUEUED_BUFFERS = 2 };
456b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    enum { NUM_BUFFER_SLOTS = 32 };
466b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    enum { NO_CONNECTED_API = 0 };
47eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    enum { INVALID_BUFFER_SLOT = -1 };
481585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden    enum { STALE_BUFFER_SLOT = 1, NO_BUFFER_AVAILABLE, PRESENT_LATER };
496b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
50c68f2ecfa02037144d1a3856f637a77f523cf416Jamie Gennis    // When in async mode we reserve two slots in order to guarantee that the
51c68f2ecfa02037144d1a3856f637a77f523cf416Jamie Gennis    // producer and consumer can run asynchronously.
52c68f2ecfa02037144d1a3856f637a77f523cf416Jamie Gennis    enum { MAX_MAX_ACQUIRED_BUFFERS = NUM_BUFFER_SLOTS - 2 };
53c68f2ecfa02037144d1a3856f637a77f523cf416Jamie Gennis
54a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    // for backward source compatibility
55a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    typedef ::android::ConsumerListener ConsumerListener;
56fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
57fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // ProxyConsumerListener is a ConsumerListener implementation that keeps a weak
58fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // reference to the actual consumer object.  It forwards all calls to that
59fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // consumer object so long as it exists.
60fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    //
61fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // This class exists to avoid having a circular reference between the
62fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // BufferQueue object and the consumer object.  The reason this can't be a weak
63fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // reference in the BufferQueue class is because we're planning to expose the
64fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // consumer side of a BufferQueue as a binder interface, which doesn't support
65fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // weak references.
66a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    class ProxyConsumerListener : public BnConsumerListener {
67fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    public:
68a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian        ProxyConsumerListener(const wp<ConsumerListener>& consumerListener);
69fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        virtual ~ProxyConsumerListener();
70fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        virtual void onFrameAvailable();
71fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        virtual void onBuffersReleased();
72fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    private:
73a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian        // mConsumerListener is a weak reference to the IConsumerListener.  This is
74fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis        // the raison d'etre of ProxyConsumerListener.
75a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian        wp<ConsumerListener> mConsumerListener;
766b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    };
776b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
78fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
7972f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    // BufferQueue manages a pool of gralloc memory slots to be used by
80595264f1af12e25dce57d7c5b1d52ed86ac0d0c9Mathias Agopian    // producers and consumers. allocator is used to allocate all the
81595264f1af12e25dce57d7c5b1d52ed86ac0d0c9Mathias Agopian    // needed gralloc buffers.
82595264f1af12e25dce57d7c5b1d52ed86ac0d0c9Mathias Agopian    BufferQueue(const sp<IGraphicBufferAlloc>& allocator = NULL);
836b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    virtual ~BufferQueue();
846b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
85a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    /*
86365857df8b94c959dea984a63013f6e7730ef976Mathias Agopian     * IBinder::DeathRecipient interface
87365857df8b94c959dea984a63013f6e7730ef976Mathias Agopian     */
88365857df8b94c959dea984a63013f6e7730ef976Mathias Agopian
89365857df8b94c959dea984a63013f6e7730ef976Mathias Agopian    virtual void binderDied(const wp<IBinder>& who);
90365857df8b94c959dea984a63013f6e7730ef976Mathias Agopian
91365857df8b94c959dea984a63013f6e7730ef976Mathias Agopian    /*
92a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian     * IGraphicBufferProducer interface
93a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian     */
94a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian
95753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // Query native window attributes.  The "what" values are enumerated in
96753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // window.h (e.g. NATIVE_WINDOW_FORMAT).
97b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam    virtual int query(int what, int* value);
98b856052c00dfef70d0957482c72c2979ffc4733aDaniel Lam
99753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // setBufferCount updates the number of available buffer slots.  If this
100753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // method succeeds, buffer slots will be both unallocated and owned by
101753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // the BufferQueue object (i.e. they are not owned by the producer or
102753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // consumer).
103753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    //
104753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // This will fail if the producer has dequeued any buffers, or if
105753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // bufferCount is invalid.  bufferCount must generally be a value
106753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // between the minimum undequeued buffer count and NUM_BUFFER_SLOTS
107753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // (inclusive).  It may also be set to zero (the default) to indicate
108753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // that the producer does not wish to set a value.  The minimum value
109753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // can be obtained by calling query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
110753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // ...).
111753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    //
112753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // This may only be called by the producer.  The consumer will be told
113753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // to discard buffers through the onBuffersReleased callback.
1146b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    virtual status_t setBufferCount(int bufferCount);
1156b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
116753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // requestBuffer returns the GraphicBuffer for slot N.
117753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    //
118753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // In normal operation, this is called the first time slot N is returned
119753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // by dequeueBuffer.  It must be called again if dequeueBuffer returns
120753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // flags indicating that previously-returned buffers are no longer valid.
1216b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf);
1226b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
123753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // dequeueBuffer gets the next buffer slot index for the producer to use.
124753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // If a buffer slot is available then that slot index is written to the
125753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // location pointed to by the buf argument and a status of OK is returned.
126753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // If no slot is available then a status of -EBUSY is returned and buf is
1276b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // unmodified.
128f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall    //
129f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall    // The fence parameter will be updated to hold the fence associated with
130f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall    // the buffer. The contents of the buffer must not be overwritten until the
131753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // fence signals. If the fence is Fence::NO_FENCE, the buffer may be
132753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // written immediately.
133f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall    //
1346b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // The width and height parameters must be no greater than the minimum of
1356b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
1366b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // An error due to invalid dimensions might not be reported until
137753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // updateTexImage() is called.  If width and height are both zero, the
138753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // default values specified by setDefaultBufferSize() are used instead.
139753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    //
140753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // The pixel formats are enumerated in graphics.h, e.g.
141753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // HAL_PIXEL_FORMAT_RGBA_8888.  If the format is 0, the default format
142753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // will be used.
143753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    //
144753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // The usage argument specifies gralloc buffer usage flags.  The values
145753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // are enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER.  These
146753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // will be merged with the usage flags specified by setConsumerUsageBits.
147753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    //
148753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // The return value may be a negative error value or a non-negative
149753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // collection of flags.  If the flags are set, the return values are
150753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // valid, but additional actions must be performed.
151753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    //
152753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // If IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION is set, the
153753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // producer must discard cached GraphicBuffer references for the slot
154753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // returned in buf.
155753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // If IGraphicBufferProducer::RELEASE_ALL_BUFFERS is set, the producer
156753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // must discard cached GraphicBuffer references for all slots.
157753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    //
158753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // In both cases, the producer will need to call requestBuffer to get a
159753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // GraphicBuffer handle for the returned slot.
1607cdd786fa80cf03551291ae8feca7b77583be1c5Mathias Agopian    virtual status_t dequeueBuffer(int *buf, sp<Fence>* fence, bool async,
161f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall            uint32_t width, uint32_t height, uint32_t format, uint32_t usage);
1626b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
163753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // queueBuffer returns a filled buffer to the BufferQueue.
164753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    //
165753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // Additional data is provided in the QueueBufferInput struct.  Notably,
166753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // a timestamp must be provided for the buffer. The timestamp is in
1676b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // nanoseconds, and must be monotonically increasing. Its other semantics
168753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // (zero point, etc) are producer-specific and should be documented by the
169753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // producer.
170753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    //
171753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // The caller may provide a fence that signals when all rendering
172753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // operations have completed.  Alternatively, NO_FENCE may be used,
173753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // indicating that the buffer is ready immediately.
174753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    //
175753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // Some values are returned in the output struct: the current settings
176753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // for default width and height, the current transform hint, and the
177753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // number of queued buffers.
178f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian    virtual status_t queueBuffer(int buf,
179f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian            const QueueBufferInput& input, QueueBufferOutput* output);
180f0bc2f1d8d37977bd3aef3d3326a70e9e69d4246Mathias Agopian
181753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // cancelBuffer returns a dequeued buffer to the BufferQueue, but doesn't
182753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // queue it for use by the consumer.
183753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    //
184753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // The buffer will not be overwritten until the fence signals.  The fence
185753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // will usually be the one obtained from dequeueBuffer.
1864c00cc11141da7d159eb2323b186ed344115c0f1Jesse Hall    virtual void cancelBuffer(int buf, const sp<Fence>& fence);
1876b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
188753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // connect attempts to connect a producer API to the BufferQueue.  This
189753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // must be called before any other IGraphicBufferProducer methods are
190753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // called except for getAllocator.  A consumer must already be connected.
191753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    //
192753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // This method will fail if connect was previously called on the
193753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // BufferQueue and no corresponding disconnect call was made (i.e. if
194753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // it's still connected to a producer).
1956b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    //
196753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // APIs are enumerated in window.h (e.g. NATIVE_WINDOW_API_CPU).
197365857df8b94c959dea984a63013f6e7730ef976Mathias Agopian    virtual status_t connect(const sp<IBinder>& token,
198365857df8b94c959dea984a63013f6e7730ef976Mathias Agopian            int api, bool producerControlledByApp, QueueBufferOutput* output);
1996b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
200753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // disconnect attempts to disconnect a producer API from the BufferQueue.
201753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // Calling this method will cause any subsequent calls to other
2022adaf04fab35cf47c824d74d901b54094e01ccd3Andy McFadden    // IGraphicBufferProducer methods to fail except for getAllocator and connect.
2036b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // Successfully calling connect after this will allow the other methods to
2046b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // succeed again.
2056b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    //
2066b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // This method will fail if the the BufferQueue is not currently
207753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // connected to the specified producer API.
2086b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    virtual status_t disconnect(int api);
2096b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
210a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    /*
211a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian     * IGraphicBufferConsumer interface
212a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian     */
213eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
214fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // acquireBuffer attempts to acquire ownership of the next pending buffer in
215fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // the BufferQueue.  If no buffer is pending then it returns -EINVAL.  If a
216fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // buffer is successfully acquired, the information about the buffer is
217fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // returned in BufferItem.  If the buffer returned had previously been
218fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
219fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // NULL and it is assumed that the consumer still holds a reference to the
220fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // buffer.
2211585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden    //
2221585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden    // If presentWhen is nonzero, it indicates the time when the buffer will
2231585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden    // be displayed on screen.  If the buffer's timestamp is farther in the
2241585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden    // future, the buffer won't be acquired, and PRESENT_LATER will be
2251585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden    // returned.  The presentation time is in nanoseconds, and the time base
2261585c4d9fbbba3ba70ae625923b85cd02cb8a0fdAndy McFadden    // is CLOCK_MONOTONIC.
227a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    virtual status_t acquireBuffer(BufferItem *buffer, nsecs_t presentWhen);
228eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
229eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // releaseBuffer releases a buffer slot from the consumer back to the
230753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // BufferQueue.  This may be done while the buffer's contents are still
231753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // being accessed.  The fence will signal when the buffer is no longer
232c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar    // in use. frameNumber is used to indentify the exact buffer returned.
233fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    //
234e41b318bc4708e1dee9364e73215ff0d51fb76a1Eino-Ville Talvala    // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
235e41b318bc4708e1dee9364e73215ff0d51fb76a1Eino-Ville Talvala    // any references to the just-released buffer that it might have, as if it
236e41b318bc4708e1dee9364e73215ff0d51fb76a1Eino-Ville Talvala    // had received a onBuffersReleased() call with a mask set for the released
237e41b318bc4708e1dee9364e73215ff0d51fb76a1Eino-Ville Talvala    // buffer.
238e41b318bc4708e1dee9364e73215ff0d51fb76a1Eino-Ville Talvala    //
239fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // Note that the dependencies on EGL will be removed once we switch to using
240fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // the Android HW Sync HAL.
241a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    virtual status_t releaseBuffer(int buf, uint64_t frameNumber,
242c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar            EGLDisplay display, EGLSyncKHR fence,
243f78575400977f644cf0b12beb2fa5fc278b6ed4cJesse Hall            const sp<Fence>& releaseFence);
244eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
245fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // consumerConnect connects a consumer to the BufferQueue.  Only one
246fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // consumer may be connected, and when that consumer disconnects the
247fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // BufferQueue is placed into the "abandoned" state, causing most
248fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // interactions with the BufferQueue by the producer to fail.
249595264f1af12e25dce57d7c5b1d52ed86ac0d0c9Mathias Agopian    // controlledByApp indicates whether the consumer is controlled by
250595264f1af12e25dce57d7c5b1d52ed86ac0d0c9Mathias Agopian    // the application.
251753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    //
252753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // consumer may not be NULL.
253a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    virtual status_t consumerConnect(const sp<IConsumerListener>& consumer, bool controlledByApp);
254fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
255eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // consumerDisconnect disconnects a consumer from the BufferQueue. All
256fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // buffers will be freed and the BufferQueue is placed in the "abandoned"
257fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // state, causing most interactions with the BufferQueue by the producer to
258fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // fail.
259a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    virtual status_t consumerDisconnect();
260eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
261fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // getReleasedBuffers sets the value pointed to by slotMask to a bit mask
262753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // indicating which buffer slots have been released by the BufferQueue
263fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // but have not yet been released by the consumer.
264753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    //
265753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // This should be called from the onBuffersReleased() callback.
266a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    virtual status_t getReleasedBuffers(uint32_t* slotMask);
267fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis
268eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // setDefaultBufferSize is used to set the size of buffers returned by
269753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // dequeueBuffer when a width and height of zero is requested.  Default
270753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // is 1x1.
271a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    virtual status_t setDefaultBufferSize(uint32_t w, uint32_t h);
272eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
273753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // setDefaultMaxBufferCount sets the default value for the maximum buffer
274753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // count (the initial default is 2). If the producer has requested a
275753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // buffer count using setBufferCount, the default buffer count will only
276753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // take effect if the producer sets the count back to zero.
277753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    //
278753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
279a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    virtual status_t setDefaultMaxBufferCount(int bufferCount);
280eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
281ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian    // disableAsyncBuffer disables the extra buffer used in async mode
282ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian    // (when both producer and consumer have set their "isControlledByApp"
283ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian    // flag) and has dequeueBuffer() return WOULD_BLOCK instead.
284ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian    //
285ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian    // This can only be called before consumerConnect().
286a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    virtual status_t disableAsyncBuffer();
287ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian
28872f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    // setMaxAcquiredBufferCount sets the maximum number of buffers that can
289753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // be acquired by the consumer at one time (default 1).  This call will
290753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // fail if a producer is connected to the BufferQueue.
291a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers);
29272f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis
293eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    // setConsumerName sets the name used in logging
294a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    virtual void setConsumerName(const String8& name);
295eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
296b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // setDefaultBufferFormat allows the BufferQueue to create
297b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // GraphicBuffers of a defaultFormat if no format is specified
298753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // in dequeueBuffer.  Formats are enumerated in graphics.h; the
299753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // initial default is HAL_PIXEL_FORMAT_RGBA_8888.
300a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    virtual status_t setDefaultBufferFormat(uint32_t defaultFormat);
301b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
302753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
303753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // These are merged with the bits passed to dequeueBuffer.  The values are
304753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
305a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    virtual status_t setConsumerUsageBits(uint32_t usage);
306b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
307753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // setTransformHint bakes in rotation to buffers so overlays can be used.
308753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // The values are enumerated in window.h, e.g.
309753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // NATIVE_WINDOW_TRANSFORM_ROT_90.  The default is 0 (no transform).
310a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    virtual status_t setTransformHint(uint32_t hint);
311eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
312db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian    // dump our state in a String
313db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian    virtual void dump(String8& result, const char* prefix) const;
314db89edc94bd2a78226b407f9f7261e202e7fa325Mathias Agopian
315ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian
316eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lamprivate:
317753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // freeBufferLocked frees the GraphicBuffer and sync resources for the
318753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // given slot.
3196b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    void freeBufferLocked(int index);
3206b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
321753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // freeAllBuffersLocked frees the GraphicBuffer and sync resources for
322753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // all slots.
3236b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    void freeAllBuffersLocked();
3246b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
32531a353da225af5329735451c761b430d82dfda1bJamie Gennis    // setDefaultMaxBufferCountLocked sets the maximum number of buffer slots
32631a353da225af5329735451c761b430d82dfda1bJamie Gennis    // that will be used if the producer does not override the buffer slot
327753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // count.  The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
328753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // The initial default is 2.
32931a353da225af5329735451c761b430d82dfda1bJamie Gennis    status_t setDefaultMaxBufferCountLocked(int count);
33031a353da225af5329735451c761b430d82dfda1bJamie Gennis
3317cdd786fa80cf03551291ae8feca7b77583be1c5Mathias Agopian    // getMinUndequeuedBufferCount returns the minimum number of buffers
3327cdd786fa80cf03551291ae8feca7b77583be1c5Mathias Agopian    // that must remain in a state other than DEQUEUED.
3337cdd786fa80cf03551291ae8feca7b77583be1c5Mathias Agopian    // The async parameter tells whether we're in asynchronous mode.
3347cdd786fa80cf03551291ae8feca7b77583be1c5Mathias Agopian    int getMinUndequeuedBufferCount(bool async) const;
3357cdd786fa80cf03551291ae8feca7b77583be1c5Mathias Agopian
33631a353da225af5329735451c761b430d82dfda1bJamie Gennis    // getMinBufferCountLocked returns the minimum number of buffers allowed
33731a353da225af5329735451c761b430d82dfda1bJamie Gennis    // given the current BufferQueue state.
3387cdd786fa80cf03551291ae8feca7b77583be1c5Mathias Agopian    // The async parameter tells whether we're in asynchronous mode.
3397cdd786fa80cf03551291ae8feca7b77583be1c5Mathias Agopian    int getMinMaxBufferCountLocked(bool async) const;
3406b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
341e191e6c34829aec406a9cfe3e95211f884a311ffJamie Gennis    // getMaxBufferCountLocked returns the maximum number of buffers that can
342e191e6c34829aec406a9cfe3e95211f884a311ffJamie Gennis    // be allocated at once.  This value depends upon the following member
343e191e6c34829aec406a9cfe3e95211f884a311ffJamie Gennis    // variables:
344e191e6c34829aec406a9cfe3e95211f884a311ffJamie Gennis    //
345a3fbda3cef04d51a35a3eb64b2f744a989800856Mathias Agopian    //      mDequeueBufferCannotBlock
34672f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    //      mMaxAcquiredBufferCount
347e191e6c34829aec406a9cfe3e95211f884a311ffJamie Gennis    //      mDefaultMaxBufferCount
348e191e6c34829aec406a9cfe3e95211f884a311ffJamie Gennis    //      mOverrideMaxBufferCount
3497cdd786fa80cf03551291ae8feca7b77583be1c5Mathias Agopian    //      async parameter
350e191e6c34829aec406a9cfe3e95211f884a311ffJamie Gennis    //
351e191e6c34829aec406a9cfe3e95211f884a311ffJamie Gennis    // Any time one of these member variables is changed while a producer is
352e191e6c34829aec406a9cfe3e95211f884a311ffJamie Gennis    // connected, mDequeueCondition must be broadcast.
3537cdd786fa80cf03551291ae8feca7b77583be1c5Mathias Agopian    int getMaxBufferCountLocked(bool async) const;
354e191e6c34829aec406a9cfe3e95211f884a311ffJamie Gennis
355c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar    // stillTracking returns true iff the buffer item is still being tracked
356c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar    // in one of the slots.
357c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar    bool stillTracking(const BufferItem *item) const;
358c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar
3596b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    struct BufferSlot {
3606b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3616b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        BufferSlot()
362eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        : mEglDisplay(EGL_NO_DISPLAY),
3636b091c53000c843211c218ce40287a7edca9bc63Daniel Lam          mBufferState(BufferSlot::FREE),
3646b091c53000c843211c218ce40287a7edca9bc63Daniel Lam          mRequestBufferCalled(false),
3656b091c53000c843211c218ce40287a7edca9bc63Daniel Lam          mFrameNumber(0),
366c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall          mEglFence(EGL_NO_SYNC_KHR),
3679abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam          mAcquireCalled(false),
3689abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam          mNeedsCleanupOnRelease(false) {
3696b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        }
3706b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3716b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // mGraphicBuffer points to the buffer allocated for this slot or is NULL
3726b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // if no buffer has been allocated.
3736b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        sp<GraphicBuffer> mGraphicBuffer;
3746b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
375753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden        // mEglDisplay is the EGLDisplay used to create EGLSyncKHR objects.
3766b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        EGLDisplay mEglDisplay;
3776b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3786b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // BufferState represents the different states in which a buffer slot
379753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden        // can be.  All slots are initially FREE.
3806b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        enum BufferState {
381753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            // FREE indicates that the buffer is available to be dequeued
382753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            // by the producer.  The buffer may be in use by the consumer for
383753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            // a finite time, so the buffer must not be modified until the
384753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            // associated fence is signaled.
385753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            //
386753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            // The slot is "owned" by BufferQueue.  It transitions to DEQUEUED
387753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            // when dequeueBuffer is called.
3886b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            FREE = 0,
3896b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
3906b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            // DEQUEUED indicates that the buffer has been dequeued by the
391753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            // producer, but has not yet been queued or canceled.  The
392753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            // producer may modify the buffer's contents as soon as the
393753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            // associated ready fence is signaled.
3946b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            //
395753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            // The slot is "owned" by the producer.  It can transition to
396753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            // QUEUED (via queueBuffer) or back to FREE (via cancelBuffer).
3976b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            DEQUEUED = 1,
3986b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
399753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            // QUEUED indicates that the buffer has been filled by the
400753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            // producer and queued for use by the consumer.  The buffer
401753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            // contents may continue to be modified for a finite time, so
402753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            // the contents must not be accessed until the associated fence
403753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            // is signaled.
404753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            //
405753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            // The slot is "owned" by BufferQueue.  It can transition to
406753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            // ACQUIRED (via acquireBuffer) or to FREE (if another buffer is
407753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            // queued in asynchronous mode).
4086b091c53000c843211c218ce40287a7edca9bc63Daniel Lam            QUEUED = 2,
409eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
410753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            // ACQUIRED indicates that the buffer has been acquired by the
411753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            // consumer.  As with QUEUED, the contents must not be accessed
412753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            // by the consumer until the fence is signaled.
413753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            //
414753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            // The slot is "owned" by the consumer.  It transitions to FREE
415753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden            // when releaseBuffer is called.
416eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam            ACQUIRED = 3
4176b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        };
4186b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4196b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // mBufferState is the current state of this buffer slot.
4206b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        BufferState mBufferState;
4216b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
422753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden        // mRequestBufferCalled is used for validating that the producer did
4236b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // call requestBuffer() when told to do so. Technically this is not
424753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden        // needed but useful for debugging and catching producer bugs.
4256b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        bool mRequestBufferCalled;
4266b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
427753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden        // mFrameNumber is the number of the queued frame for this slot.  This
428753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden        // is used to dequeue buffers in LRU order (useful because buffers
429753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden        // may be released before their release fence is signaled).
4306b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        uint64_t mFrameNumber;
4316b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
432c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        // mEglFence is the EGL sync object that must signal before the buffer
4336b091c53000c843211c218ce40287a7edca9bc63Daniel Lam        // associated with this buffer slot may be dequeued. It is initialized
434753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden        // to EGL_NO_SYNC_KHR when the buffer is created and may be set to a
435753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden        // new sync object in releaseBuffer.  (This is deprecated in favor of
436753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden        // mFence, below.)
437c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        EGLSyncKHR mEglFence;
438c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall
439c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        // mFence is a fence which will signal when work initiated by the
440c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        // previous owner of the buffer is finished. When the buffer is FREE,
441c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        // the fence indicates when the consumer has finished reading
442c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        // from the buffer, or when the producer has finished writing if it
443c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        // called cancelBuffer after queueing some writes. When the buffer is
444c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        // QUEUED, it indicates when the producer has finished filling the
445c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        // buffer. When the buffer is DEQUEUED or ACQUIRED, the fence has been
446c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        // passed to the consumer or producer along with ownership of the
447753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden        // buffer, and mFence is set to NO_FENCE.
448c777b0b3b9b0ea5d8e378fccde6935765e28e329Jesse Hall        sp<Fence> mFence;
449ef19414bd8b77a26f5751f3845be79025a8263feJesse Hall
450eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        // Indicates whether this buffer has been seen by a consumer yet
451eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam        bool mAcquireCalled;
4529abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam
453753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden        // Indicates whether this buffer needs to be cleaned up by the
454753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden        // consumer.  This is set when a buffer in ACQUIRED state is freed.
455753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden        // It causes releaseBuffer to return STALE_BUFFER_SLOT.
4569abe1ebc9575dc5a19bf1dfce6e9b02e03374457Daniel Lam        bool mNeedsCleanupOnRelease;
4576b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    };
4586b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
459753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // mSlots is the array of buffer slots that must be mirrored on the
460753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // producer side. This allows buffer ownership to be transferred between
461753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // the producer and consumer without sending a GraphicBuffer over binder.
462753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // The entire array is initialized to NULL at construction time, and
463753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // buffers are allocated for a slot when requestBuffer is called with
464753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // that slot's index.
4656b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    BufferSlot mSlots[NUM_BUFFER_SLOTS];
4666b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4676b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // mDefaultWidth holds the default width of allocated buffers. It is used
468753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // in dequeueBuffer() if a width and height of zero is specified.
4696b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    uint32_t mDefaultWidth;
4706b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4716b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // mDefaultHeight holds the default height of allocated buffers. It is used
472753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // in dequeueBuffer() if a width and height of zero is specified.
4736b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    uint32_t mDefaultHeight;
474a3fbda3cef04d51a35a3eb64b2f744a989800856Mathias Agopian
47572f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    // mMaxAcquiredBufferCount is the number of buffers that the consumer may
47672f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    // acquire at one time.  It defaults to 1 and can be changed by the
47772f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    // consumer via the setMaxAcquiredBufferCount method, but this may only be
47872f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    // done when no producer is connected to the BufferQueue.
47972f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    //
48072f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    // This value is used to derive the value returned for the
48172f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    // MIN_UNDEQUEUED_BUFFERS query by the producer.
48272f096fb1ad0a0deadbfac5f88627461905d38e8Jamie Gennis    int mMaxAcquiredBufferCount;
483abe61bfda4938abd932465e27c29ba9e41aea606Daniel Lam
48431a353da225af5329735451c761b430d82dfda1bJamie Gennis    // mDefaultMaxBufferCount is the default limit on the number of buffers
48531a353da225af5329735451c761b430d82dfda1bJamie Gennis    // that will be allocated at one time.  This default limit is set by the
48631a353da225af5329735451c761b430d82dfda1bJamie Gennis    // consumer.  The limit (as opposed to the default limit) may be
48731a353da225af5329735451c761b430d82dfda1bJamie Gennis    // overridden by the producer.
48831a353da225af5329735451c761b430d82dfda1bJamie Gennis    int mDefaultMaxBufferCount;
48931a353da225af5329735451c761b430d82dfda1bJamie Gennis
49031a353da225af5329735451c761b430d82dfda1bJamie Gennis    // mOverrideMaxBufferCount is the limit on the number of buffers that will
49131a353da225af5329735451c761b430d82dfda1bJamie Gennis    // be allocated at one time. This value is set by the image producer by
49231a353da225af5329735451c761b430d82dfda1bJamie Gennis    // calling setBufferCount. The default is zero, which means the producer
49331a353da225af5329735451c761b430d82dfda1bJamie Gennis    // doesn't care about the number of buffers in the pool. In that case
49431a353da225af5329735451c761b430d82dfda1bJamie Gennis    // mDefaultMaxBufferCount is used as the limit.
49531a353da225af5329735451c761b430d82dfda1bJamie Gennis    int mOverrideMaxBufferCount;
4966b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
4976b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // mGraphicBufferAlloc is the connection to SurfaceFlinger that is used to
4986b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // allocate new GraphicBuffer objects.
4996b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    sp<IGraphicBufferAlloc> mGraphicBufferAlloc;
5006b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
501fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // mConsumerListener is used to notify the connected consumer of
502fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // asynchronous events that it may wish to react to.  It is initially set
503fa5b40ebb8923133df12dc70591bfe35b3f1c9b3Jamie Gennis    // to NULL and is written by consumerConnect and consumerDisconnect.
504a4e19521ac4563f2ff6517bcfd63d9b8d33a6d0bMathias Agopian    sp<IConsumerListener> mConsumerListener;
5056b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
506595264f1af12e25dce57d7c5b1d52ed86ac0d0c9Mathias Agopian    // mConsumerControlledByApp whether the connected consumer is controlled by the
507595264f1af12e25dce57d7c5b1d52ed86ac0d0c9Mathias Agopian    // application.
508595264f1af12e25dce57d7c5b1d52ed86ac0d0c9Mathias Agopian    bool mConsumerControlledByApp;
509595264f1af12e25dce57d7c5b1d52ed86ac0d0c9Mathias Agopian
510595264f1af12e25dce57d7c5b1d52ed86ac0d0c9Mathias Agopian    // mDequeueBufferCannotBlock whether dequeueBuffer() isn't allowed to block.
511ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian    // this flag is set during connect() when both consumer and producer are controlled
512595264f1af12e25dce57d7c5b1d52ed86ac0d0c9Mathias Agopian    // by the application.
513595264f1af12e25dce57d7c5b1d52ed86ac0d0c9Mathias Agopian    bool mDequeueBufferCannotBlock;
514595264f1af12e25dce57d7c5b1d52ed86ac0d0c9Mathias Agopian
515ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian    // mUseAsyncBuffer whether an extra buffer is used in async mode to prevent
516ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian    // dequeueBuffer() from ever blocking.
517ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian    bool mUseAsyncBuffer;
518ad678e18b66f495efa78dc3b9ab99b579945c9e2Mathias Agopian
519753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // mConnectedApi indicates the producer API that is currently connected
520753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // to this BufferQueue.  It defaults to NO_CONNECTED_API (= 0), and gets
521753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // updated by the connect and disconnect methods.
5226b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    int mConnectedApi;
5236b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5246b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // mDequeueCondition condition used for dequeueBuffer in synchronous mode
5256b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mutable Condition mDequeueCondition;
5266b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5276b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // mQueue is a FIFO of queued buffers used in synchronous mode
528c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar    typedef Vector<BufferItem> Fifo;
5296b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    Fifo mQueue;
5306b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5316b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // mAbandoned indicates that the BufferQueue will no longer be used to
532753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // consume image buffers pushed to it using the IGraphicBufferProducer
533753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // interface.  It is initialized to false, and set to true in the
534753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // consumerDisconnect method.  A BufferQueue that has been abandoned will
535753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // return the NO_INIT error from all IGraphicBufferProducer methods
536753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // capable of returning an error.
5376b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    bool mAbandoned;
5386b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
539753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // mConsumerName is a string used to identify the BufferQueue in log
540753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // messages.  It is set by the setConsumerName method.
541eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    String8 mConsumerName;
5426b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5436b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // mMutex is the mutex used to prevent concurrent access to the member
5446b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // variables of BufferQueue objects. It must be locked whenever the
5456b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    // member variables are accessed.
5466b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    mutable Mutex mMutex;
5476b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
548753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // mFrameCounter is the free running counter, incremented on every
549c5d7b7d323bba8772a9005f7d300ad983a04733aLajos Molnar    // successful queueBuffer call, and buffer allocation.
5506b091c53000c843211c218ce40287a7edca9bc63Daniel Lam    uint64_t mFrameCounter;
551eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam
552753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // mBufferHasBeenQueued is true once a buffer has been queued.  It is
553753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // reset when something causes all buffers to be freed (e.g. changing the
554753e3415cdaa99f9453d1ea71b163bf1b148e127Andy McFadden    // buffer count).
555eae59d2ea77ef57aab203fb185a880ce37ac38d6Daniel Lam    bool mBufferHasBeenQueued;
556b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
557b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // mDefaultBufferFormat can be set so it will override
558b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // the buffer format when it isn't specified in dequeueBuffer
559b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    uint32_t mDefaultBufferFormat;
560b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
561b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // mConsumerUsageBits contains flags the consumer wants for GraphicBuffers
562b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    uint32_t mConsumerUsageBits;
563b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam
564b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    // mTransformHint is used to optimize for screen rotations
565b267579ba8dfe3f47d2a481c5a3c2254e3d565a1Daniel Lam    uint32_t mTransformHint;
566365857df8b94c959dea984a63013f6e7730ef976Mathias Agopian
567365857df8b94c959dea984a63013f6e7730ef976Mathias Agopian    // mConnectedProducerToken is used to set a binder death notification on the producer
568365857df8b94c959dea984a63013f6e7730ef976Mathias Agopian    sp<IBinder> mConnectedProducerToken;
5696b091c53000c843211c218ce40287a7edca9bc63Daniel Lam};
5706b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5716b091c53000c843211c218ce40287a7edca9bc63Daniel Lam// ----------------------------------------------------------------------------
5726b091c53000c843211c218ce40287a7edca9bc63Daniel Lam}; // namespace android
5736b091c53000c843211c218ce40287a7edca9bc63Daniel Lam
5746b091c53000c843211c218ce40287a7edca9bc63Daniel Lam#endif // ANDROID_GUI_BUFFERQUEUE_H
575