1289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza/*
2289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * Copyright 2014 The Android Open Source Project
3289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza *
4289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * Licensed under the Apache License, Version 2.0 (the "License");
5289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * you may not use this file except in compliance with the License.
6289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * You may obtain a copy of the License at
7289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza *
8289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza *      http://www.apache.org/licenses/LICENSE-2.0
9289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza *
10289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * Unless required by applicable law or agreed to in writing, software
11289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * distributed under the License is distributed on an "AS IS" BASIS,
12289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * See the License for the specific language governing permissions and
14289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza * limitations under the License.
15289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza */
16289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
17289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza#ifndef ANDROID_GUI_BUFFERQUEUEPRODUCER_H
18289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza#define ANDROID_GUI_BUFFERQUEUEPRODUCER_H
19289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
203e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza#include <gui/BufferQueueDefs.h>
21289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza#include <gui/IGraphicBufferProducer.h>
22289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
23289ade165e60b5f71734d30e535f16eb1f4313adDan Stozanamespace android {
24289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
2589702476bb4e450ab8b312f886658728c02f98c0Colin Crossstruct BufferSlot;
26289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
27289ade165e60b5f71734d30e535f16eb1f4313adDan Stozaclass BufferQueueProducer : public BnGraphicBufferProducer,
28289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza                            private IBinder::DeathRecipient {
29289ade165e60b5f71734d30e535f16eb1f4313adDan Stozapublic:
303e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    friend class BufferQueue; // Needed to access binderDied
313e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
32468051e20be19130572231266db306396a56402bIrvel    BufferQueueProducer(const sp<BufferQueueCore>& core, bool consumerIsSurfaceFlinger = false);
3364865fddc2cfaa8ba3d81ef582369435328cb6d7Yi Kong    ~BufferQueueProducer() override;
34289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
35289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // requestBuffer returns the GraphicBuffer for slot N.
36289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    //
37289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // In normal operation, this is called the first time slot N is returned
38289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // by dequeueBuffer.  It must be called again if dequeueBuffer returns
39289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // flags indicating that previously-returned buffers are no longer valid.
40289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf);
41289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
42fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    // see IGraphicsBufferProducer::setMaxDequeuedBufferCount
43fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    virtual status_t setMaxDequeuedBufferCount(int maxDequeuedBuffers);
44fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
45fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    // see IGraphicsBufferProducer::setAsyncMode
46fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos    virtual status_t setAsyncMode(bool async);
47fa455354557f6283ff3a7d76979e52fd251c155fPablo Ceballos
48289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // dequeueBuffer gets the next buffer slot index for the producer to use.
49289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // If a buffer slot is available then that slot index is written to the
50289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // location pointed to by the buf argument and a status of OK is returned.
51289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // If no slot is available then a status of -EBUSY is returned and buf is
52289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // unmodified.
53289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    //
54289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // The outFence parameter will be updated to hold the fence associated with
55289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // the buffer. The contents of the buffer must not be overwritten until the
56289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // fence signals. If the fence is Fence::NO_FENCE, the buffer may be
57289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // written immediately.
58289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    //
59289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // The width and height parameters must be no greater than the minimum of
60289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
61289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // An error due to invalid dimensions might not be reported until
62289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // updateTexImage() is called.  If width and height are both zero, the
63289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // default values specified by setDefaultBufferSize() are used instead.
64289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    //
653be1c6b60a188dc10025e2ce156c11fac050625dDan Stoza    // If the format is 0, the default format will be used.
66289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    //
67289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // The usage argument specifies gralloc buffer usage flags.  The values
68289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // are enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER.  These
69289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // will be merged with the usage flags specified by setConsumerUsageBits.
70289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    //
71289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // The return value may be a negative error value or a non-negative
72289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // collection of flags.  If the flags are set, the return values are
73289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // valid, but additional actions must be performed.
74289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    //
75289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // If IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION is set, the
76289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // producer must discard cached GraphicBuffer references for the slot
77289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // returned in buf.
78289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // If IGraphicBufferProducer::RELEASE_ALL_BUFFERS is set, the producer
79289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // must discard cached GraphicBuffer references for all slots.
80289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    //
81289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // In both cases, the producer will need to call requestBuffer to get a
82289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // GraphicBuffer handle for the returned slot.
83a2eb34cfbe089deb9a519e9702e17d9dfe26f9e8Ian Elliott    virtual status_t dequeueBuffer(int* outSlot, sp<Fence>* outFence, uint32_t width,
84a2eb34cfbe089deb9a519e9702e17d9dfe26f9e8Ian Elliott                                   uint32_t height, PixelFormat format, uint64_t usage,
85a2eb34cfbe089deb9a519e9702e17d9dfe26f9e8Ian Elliott                                   uint64_t* outBufferAge,
86a2eb34cfbe089deb9a519e9702e17d9dfe26f9e8Ian Elliott                                   FrameEventHistoryDelta* outTimestamps) override;
87289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
889f3053de78630815d60cf48a2cf2348cc5867c45Dan Stoza    // See IGraphicBufferProducer::detachBuffer
899f3053de78630815d60cf48a2cf2348cc5867c45Dan Stoza    virtual status_t detachBuffer(int slot);
909f3053de78630815d60cf48a2cf2348cc5867c45Dan Stoza
91d9822a3843017444364899afc3c23fb5be6b9cb9Dan Stoza    // See IGraphicBufferProducer::detachNextBuffer
92d9822a3843017444364899afc3c23fb5be6b9cb9Dan Stoza    virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer,
93d9822a3843017444364899afc3c23fb5be6b9cb9Dan Stoza            sp<Fence>* outFence);
94d9822a3843017444364899afc3c23fb5be6b9cb9Dan Stoza
959f3053de78630815d60cf48a2cf2348cc5867c45Dan Stoza    // See IGraphicBufferProducer::attachBuffer
969f3053de78630815d60cf48a2cf2348cc5867c45Dan Stoza    virtual status_t attachBuffer(int* outSlot, const sp<GraphicBuffer>& buffer);
979f3053de78630815d60cf48a2cf2348cc5867c45Dan Stoza
98289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // queueBuffer returns a filled buffer to the BufferQueue.
99289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    //
100289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // Additional data is provided in the QueueBufferInput struct.  Notably,
101289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // a timestamp must be provided for the buffer. The timestamp is in
102289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // nanoseconds, and must be monotonically increasing. Its other semantics
103289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // (zero point, etc) are producer-specific and should be documented by the
104289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // producer.
105289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    //
106289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // The caller may provide a fence that signals when all rendering
107289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // operations have completed.  Alternatively, NO_FENCE may be used,
108289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // indicating that the buffer is ready immediately.
109289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    //
110289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // Some values are returned in the output struct: the current settings
111289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // for default width and height, the current transform hint, and the
112289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // number of queued buffers.
113289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    virtual status_t queueBuffer(int slot,
114289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza            const QueueBufferInput& input, QueueBufferOutput* output);
115289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
116289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // cancelBuffer returns a dequeued buffer to the BufferQueue, but doesn't
117289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // queue it for use by the consumer.
118289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    //
119289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // The buffer will not be overwritten until the fence signals.  The fence
120289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // will usually be the one obtained from dequeueBuffer.
121583b1b32191992d6ada58b3c61c71932a71c0c4bPablo Ceballos    virtual status_t cancelBuffer(int slot, const sp<Fence>& fence);
122289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
123289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // Query native window attributes.  The "what" values are enumerated in
124289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // window.h (e.g. NATIVE_WINDOW_FORMAT).
125289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    virtual int query(int what, int* outValue);
126289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
127289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // connect attempts to connect a producer API to the BufferQueue.  This
128289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // must be called before any other IGraphicBufferProducer methods are
129289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // called except for getAllocator.  A consumer must already be connected.
130289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    //
131289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // This method will fail if connect was previously called on the
132289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // BufferQueue and no corresponding disconnect call was made (i.e. if
133289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // it's still connected to a producer).
134289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    //
135289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // APIs are enumerated in window.h (e.g. NATIVE_WINDOW_API_CPU).
136f0eaf25e9247edf4d124bedaeb863f7abdf35a3eDan Stoza    virtual status_t connect(const sp<IProducerListener>& listener,
137289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza            int api, bool producerControlledByApp, QueueBufferOutput* output);
138289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
13997b9c86338e2d364d47ea7522c2d81a8014f0e07Robert Carr    // See IGraphicBufferProducer::disconnect
14097b9c86338e2d364d47ea7522c2d81a8014f0e07Robert Carr    virtual status_t disconnect(int api, DisconnectMode mode = DisconnectMode::Api);
141289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
142399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall    // Attaches a sideband buffer stream to the IGraphicBufferProducer.
143399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall    //
144399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall    // A sideband stream is a device-specific mechanism for passing buffers
145399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall    // from the producer to the consumer without using dequeueBuffer/
146399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall    // queueBuffer. If a sideband stream is present, the consumer can choose
147399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall    // whether to acquire buffers from the sideband stream or from the queued
148399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall    // buffers.
149399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall    //
150399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall    // Passing NULL or a different stream handle will detach the previous
151399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall    // handle if any.
152399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall    virtual status_t setSidebandStream(const sp<NativeHandle>& stream);
153399184a4cd728ea1421fb0bc1722274a29e38f4aJesse Hall
15429a3e90879fd96404c971e7187cd0e05927bbce0Dan Stoza    // See IGraphicBufferProducer::allocateBuffers
155567dbbb6dd42be5013fcde0dadb3316d85f2fa0dPablo Ceballos    virtual void allocateBuffers(uint32_t width, uint32_t height,
156cb496acbe593326e8d5d563847067d02b2df40ecMathias Agopian            PixelFormat format, uint64_t usage) override;
15729a3e90879fd96404c971e7187cd0e05927bbce0Dan Stoza
1589de7293b0a1b01ebe6fb1ab4a498f144adc8029fDan Stoza    // See IGraphicBufferProducer::allowAllocation
1599de7293b0a1b01ebe6fb1ab4a498f144adc8029fDan Stoza    virtual status_t allowAllocation(bool allow);
1609de7293b0a1b01ebe6fb1ab4a498f144adc8029fDan Stoza
161812ed0644f8f8f71ca403f4e5793f0dbc1fcf9b2Dan Stoza    // See IGraphicBufferProducer::setGenerationNumber
162812ed0644f8f8f71ca403f4e5793f0dbc1fcf9b2Dan Stoza    virtual status_t setGenerationNumber(uint32_t generationNumber);
163812ed0644f8f8f71ca403f4e5793f0dbc1fcf9b2Dan Stoza
164c6f30bdee1f634eb90d68cb76efe935b6535a1e8Dan Stoza    // See IGraphicBufferProducer::getConsumerName
165c6f30bdee1f634eb90d68cb76efe935b6535a1e8Dan Stoza    virtual String8 getConsumerName() const override;
166c6f30bdee1f634eb90d68cb76efe935b6535a1e8Dan Stoza
1673559fbf93801e2c0d9d8fb246fb9b867a361b464Pablo Ceballos    // See IGraphicBufferProducer::setSharedBufferMode
1683559fbf93801e2c0d9d8fb246fb9b867a361b464Pablo Ceballos    virtual status_t setSharedBufferMode(bool sharedBufferMode) override;
169127fc63e8a15366b4395f1363e8e18eb058d1709Dan Stoza
170ff95aabbcc6e8606acbd7933c90eeb9b8b382a21Pablo Ceballos    // See IGraphicBufferProducer::setAutoRefresh
171ff95aabbcc6e8606acbd7933c90eeb9b8b382a21Pablo Ceballos    virtual status_t setAutoRefresh(bool autoRefresh) override;
172ff95aabbcc6e8606acbd7933c90eeb9b8b382a21Pablo Ceballos
173127fc63e8a15366b4395f1363e8e18eb058d1709Dan Stoza    // See IGraphicBufferProducer::setDequeueTimeout
174127fc63e8a15366b4395f1363e8e18eb058d1709Dan Stoza    virtual status_t setDequeueTimeout(nsecs_t timeout) override;
175ccdfd60d79a8b7f1ed6401d0f2e8e29166a10584Pablo Ceballos
17650101d02a8eae555887282a5f761fdec57bdaf30Dan Stoza    // See IGraphicBufferProducer::getLastQueuedBuffer
17750101d02a8eae555887282a5f761fdec57bdaf30Dan Stoza    virtual status_t getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
1781a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck            sp<Fence>* outFence, float outTransformMatrix[16]) override;
17950101d02a8eae555887282a5f761fdec57bdaf30Dan Stoza
180ce796e78a57018f186b062199c75d94545318acaPablo Ceballos    // See IGraphicBufferProducer::getFrameTimestamps
1813890c3995c4a52439844faeb80b5503d42b977d8Brian Anderson    virtual void getFrameTimestamps(FrameEventHistoryDelta* outDelta) override;
182ce796e78a57018f186b062199c75d94545318acaPablo Ceballos
1838e3e92b906db431c4fa822f21242977d4ee99942Pablo Ceballos    // See IGraphicBufferProducer::getUniqueId
1848e3e92b906db431c4fa822f21242977d4ee99942Pablo Ceballos    virtual status_t getUniqueId(uint64_t* outId) const override;
1858e3e92b906db431c4fa822f21242977d4ee99942Pablo Ceballos
186e2786ea5aec3a12d948feb85ffbb535fc89c0fe6Chia-I Wu    // See IGraphicBufferProducer::getConsumerUsage
187e2786ea5aec3a12d948feb85ffbb535fc89c0fe6Chia-I Wu    virtual status_t getConsumerUsage(uint64_t* outUsage) const override;
188e2786ea5aec3a12d948feb85ffbb535fc89c0fe6Chia-I Wu
189289ade165e60b5f71734d30e535f16eb1f4313adDan Stozaprivate:
190289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // This is required by the IBinder::DeathRecipient interface
191289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    virtual void binderDied(const wp<IBinder>& who);
192289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
1935ecfb68ffd63d352df0392dca6e95ef67a66c679Dan Stoza    // Returns the slot of the next free buffer if one is available or
1945ecfb68ffd63d352df0392dca6e95ef67a66c679Dan Stoza    // BufferQueueCore::INVALID_BUFFER_SLOT otherwise
1955ecfb68ffd63d352df0392dca6e95ef67a66c679Dan Stoza    int getFreeBufferLocked() const;
1965ecfb68ffd63d352df0392dca6e95ef67a66c679Dan Stoza
19723b4abe024ea88c45e0b94c80e1fb537a573b143Pablo Ceballos    // Returns the next free slot if one is available or
19823b4abe024ea88c45e0b94c80e1fb537a573b143Pablo Ceballos    // BufferQueueCore::INVALID_BUFFER_SLOT otherwise
19923b4abe024ea88c45e0b94c80e1fb537a573b143Pablo Ceballos    int getFreeSlotLocked() const;
2005ecfb68ffd63d352df0392dca6e95ef67a66c679Dan Stoza
2013890c3995c4a52439844faeb80b5503d42b977d8Brian Anderson    void addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
2023890c3995c4a52439844faeb80b5503d42b977d8Brian Anderson            FrameEventHistoryDelta* outDelta);
203d6927fb1143398370c0885844bfb58923ef740b7Brian Anderson
2049f3053de78630815d60cf48a2cf2348cc5867c45Dan Stoza    // waitForFreeSlotThenRelock finds the oldest slot in the FREE state. It may
2059f3053de78630815d60cf48a2cf2348cc5867c45Dan Stoza    // block if there are no available slots and we are not in non-blocking
2069f3053de78630815d60cf48a2cf2348cc5867c45Dan Stoza    // mode (producer and consumer controlled by the application). If it blocks,
2079f3053de78630815d60cf48a2cf2348cc5867c45Dan Stoza    // it will release mCore->mMutex while blocked so that other operations on
2089f3053de78630815d60cf48a2cf2348cc5867c45Dan Stoza    // the BufferQueue may succeed.
2095ecfb68ffd63d352df0392dca6e95ef67a66c679Dan Stoza    enum class FreeSlotCaller {
2105ecfb68ffd63d352df0392dca6e95ef67a66c679Dan Stoza        Dequeue,
2115ecfb68ffd63d352df0392dca6e95ef67a66c679Dan Stoza        Attach,
2125ecfb68ffd63d352df0392dca6e95ef67a66c679Dan Stoza    };
21323b4abe024ea88c45e0b94c80e1fb537a573b143Pablo Ceballos    status_t waitForFreeSlotThenRelock(FreeSlotCaller caller, int* found) const;
2149f3053de78630815d60cf48a2cf2348cc5867c45Dan Stoza
215289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    sp<BufferQueueCore> mCore;
2163e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza
2173e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    // This references mCore->mSlots. Lock mCore->mMutex while accessing.
2183e96f1982fda358424b0b75f394cbf7c1794a072Dan Stoza    BufferQueueDefs::SlotsType& mSlots;
219289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
220289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // This is a cached copy of the name stored in the BufferQueueCore.
221289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // It's updated during connect and dequeueBuffer (which should catch
222289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    // most updates).
223289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza    String8 mConsumerName;
224289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
2251681d95989271f3a9ac0dbb93d10e4a29f2b4444Ruben Brunk    uint32_t mStickyTransform;
2261681d95989271f3a9ac0dbb93d10e4a29f2b4444Ruben Brunk
227468051e20be19130572231266db306396a56402bIrvel    // This controls whether the GraphicBuffer pointer in the BufferItem is
228468051e20be19130572231266db306396a56402bIrvel    // cleared after being queued
229468051e20be19130572231266db306396a56402bIrvel    bool mConsumerIsSurfaceFlinger;
230468051e20be19130572231266db306396a56402bIrvel
23199a0afbaee9eddabc2b544e3a5c432901c1d498cEric Penner    // This saves the fence from the last queueBuffer, such that the
23299a0afbaee9eddabc2b544e3a5c432901c1d498cEric Penner    // next queueBuffer call can throttle buffer production. The prior
23399a0afbaee9eddabc2b544e3a5c432901c1d498cEric Penner    // queueBuffer's fence is not nessessarily available elsewhere,
23499a0afbaee9eddabc2b544e3a5c432901c1d498cEric Penner    // since the previous buffer might have already been acquired.
23599a0afbaee9eddabc2b544e3a5c432901c1d498cEric Penner    sp<Fence> mLastQueueBufferFence;
23699a0afbaee9eddabc2b544e3a5c432901c1d498cEric Penner
2371a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck    Rect mLastQueuedCrop;
2381a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck    uint32_t mLastQueuedTransform;
2391a61da5e28fa16ad556a58193c8bbeb32a5f636dJohn Reck
2408dc55396fc9bc425b5e2c82e76a38080f2a655ffDan Stoza    // Take-a-ticket system for ensuring that onFrame* callbacks are called in
2418dc55396fc9bc425b5e2c82e76a38080f2a655ffDan Stoza    // the order that frames are queued. While the BufferQueue lock
2428dc55396fc9bc425b5e2c82e76a38080f2a655ffDan Stoza    // (mCore->mMutex) is held, a ticket is retained by the producer. After
2438dc55396fc9bc425b5e2c82e76a38080f2a655ffDan Stoza    // dropping the BufferQueue lock, the producer must wait on the condition
2448dc55396fc9bc425b5e2c82e76a38080f2a655ffDan Stoza    // variable until the current callback ticket matches its retained ticket.
2458dc55396fc9bc425b5e2c82e76a38080f2a655ffDan Stoza    Mutex mCallbackMutex;
2468dc55396fc9bc425b5e2c82e76a38080f2a655ffDan Stoza    int mNextCallbackTicket; // Protected by mCore->mMutex
2478dc55396fc9bc425b5e2c82e76a38080f2a655ffDan Stoza    int mCurrentCallbackTicket; // Protected by mCallbackMutex
2488dc55396fc9bc425b5e2c82e76a38080f2a655ffDan Stoza    Condition mCallbackCondition;
2498dc55396fc9bc425b5e2c82e76a38080f2a655ffDan Stoza
250127fc63e8a15366b4395f1363e8e18eb058d1709Dan Stoza    // Sets how long dequeueBuffer or attachBuffer will block if a buffer or
251127fc63e8a15366b4395f1363e8e18eb058d1709Dan Stoza    // slot is not yet available.
252127fc63e8a15366b4395f1363e8e18eb058d1709Dan Stoza    nsecs_t mDequeueTimeout;
253127fc63e8a15366b4395f1363e8e18eb058d1709Dan Stoza
254289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza}; // class BufferQueueProducer
255289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
256289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza} // namespace android
257289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza
258289ade165e60b5f71734d30e535f16eb1f4313adDan Stoza#endif
259