blob: 4fbfca72b1b39f1239a43f13bce9bc89881104e9 [file] [log] [blame]
Dan Stoza289ade12014-02-28 11:17:17 -08001/*
2 * Copyright 2014 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_BUFFERQUEUEPRODUCER_H
18#define ANDROID_GUI_BUFFERQUEUEPRODUCER_H
19
Dan Stoza3e96f192014-03-03 10:16:19 -080020#include <gui/BufferQueueDefs.h>
Dan Stoza289ade12014-02-28 11:17:17 -080021#include <gui/IGraphicBufferProducer.h>
22
23namespace android {
24
25class BufferSlot;
26
27class BufferQueueProducer : public BnGraphicBufferProducer,
28 private IBinder::DeathRecipient {
Dan Stoza289ade12014-02-28 11:17:17 -080029public:
Dan Stoza3e96f192014-03-03 10:16:19 -080030 friend class BufferQueue; // Needed to access binderDied
31
Dan Stoza289ade12014-02-28 11:17:17 -080032 BufferQueueProducer(const sp<BufferQueueCore>& core);
33 virtual ~BufferQueueProducer();
34
35 // requestBuffer returns the GraphicBuffer for slot N.
36 //
37 // In normal operation, this is called the first time slot N is returned
38 // by dequeueBuffer. It must be called again if dequeueBuffer returns
39 // flags indicating that previously-returned buffers are no longer valid.
40 virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf);
41
42 // setBufferCount updates the number of available buffer slots. If this
43 // method succeeds, buffer slots will be both unallocated and owned by
44 // the BufferQueue object (i.e. they are not owned by the producer or
45 // consumer).
46 //
47 // This will fail if the producer has dequeued any buffers, or if
48 // bufferCount is invalid. bufferCount must generally be a value
49 // between the minimum undequeued buffer count (exclusive) and NUM_BUFFER_SLOTS
50 // (inclusive). It may also be set to zero (the default) to indicate
51 // that the producer does not wish to set a value. The minimum value
52 // can be obtained by calling query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS,
53 // ...).
54 //
55 // This may only be called by the producer. The consumer will be told
56 // to discard buffers through the onBuffersReleased callback.
57 virtual status_t setBufferCount(int bufferCount);
58
Pablo Ceballosfa455352015-08-12 17:47:47 -070059 // see IGraphicsBufferProducer::setMaxDequeuedBufferCount
60 virtual status_t setMaxDequeuedBufferCount(int maxDequeuedBuffers);
61
62 // see IGraphicsBufferProducer::setAsyncMode
63 virtual status_t setAsyncMode(bool async);
64
Dan Stoza289ade12014-02-28 11:17:17 -080065 // dequeueBuffer gets the next buffer slot index for the producer to use.
66 // If a buffer slot is available then that slot index is written to the
67 // location pointed to by the buf argument and a status of OK is returned.
68 // If no slot is available then a status of -EBUSY is returned and buf is
69 // unmodified.
70 //
71 // The outFence parameter will be updated to hold the fence associated with
72 // the buffer. The contents of the buffer must not be overwritten until the
73 // fence signals. If the fence is Fence::NO_FENCE, the buffer may be
74 // written immediately.
75 //
76 // The width and height parameters must be no greater than the minimum of
77 // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
78 // An error due to invalid dimensions might not be reported until
79 // updateTexImage() is called. If width and height are both zero, the
80 // default values specified by setDefaultBufferSize() are used instead.
81 //
Dan Stoza3be1c6b2014-11-18 10:24:03 -080082 // If the format is 0, the default format will be used.
Dan Stoza289ade12014-02-28 11:17:17 -080083 //
84 // The usage argument specifies gralloc buffer usage flags. The values
85 // are enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER. These
86 // will be merged with the usage flags specified by setConsumerUsageBits.
87 //
88 // The return value may be a negative error value or a non-negative
89 // collection of flags. If the flags are set, the return values are
90 // valid, but additional actions must be performed.
91 //
92 // If IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION is set, the
93 // producer must discard cached GraphicBuffer references for the slot
94 // returned in buf.
95 // If IGraphicBufferProducer::RELEASE_ALL_BUFFERS is set, the producer
96 // must discard cached GraphicBuffer references for all slots.
97 //
98 // In both cases, the producer will need to call requestBuffer to get a
99 // GraphicBuffer handle for the returned slot.
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800100 virtual status_t dequeueBuffer(int *outSlot, sp<Fence>* outFence,
101 bool async, uint32_t width, uint32_t height, PixelFormat format,
102 uint32_t usage);
Dan Stoza289ade12014-02-28 11:17:17 -0800103
Dan Stoza9f3053d2014-03-06 15:14:33 -0800104 // See IGraphicBufferProducer::detachBuffer
105 virtual status_t detachBuffer(int slot);
106
Dan Stozad9822a32014-03-28 15:25:31 -0700107 // See IGraphicBufferProducer::detachNextBuffer
108 virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer,
109 sp<Fence>* outFence);
110
Dan Stoza9f3053d2014-03-06 15:14:33 -0800111 // See IGraphicBufferProducer::attachBuffer
112 virtual status_t attachBuffer(int* outSlot, const sp<GraphicBuffer>& buffer);
113
Dan Stoza289ade12014-02-28 11:17:17 -0800114 // queueBuffer returns a filled buffer to the BufferQueue.
115 //
116 // Additional data is provided in the QueueBufferInput struct. Notably,
117 // a timestamp must be provided for the buffer. The timestamp is in
118 // nanoseconds, and must be monotonically increasing. Its other semantics
119 // (zero point, etc) are producer-specific and should be documented by the
120 // producer.
121 //
122 // The caller may provide a fence that signals when all rendering
123 // operations have completed. Alternatively, NO_FENCE may be used,
124 // indicating that the buffer is ready immediately.
125 //
126 // Some values are returned in the output struct: the current settings
127 // for default width and height, the current transform hint, and the
128 // number of queued buffers.
129 virtual status_t queueBuffer(int slot,
130 const QueueBufferInput& input, QueueBufferOutput* output);
131
132 // cancelBuffer returns a dequeued buffer to the BufferQueue, but doesn't
133 // queue it for use by the consumer.
134 //
135 // The buffer will not be overwritten until the fence signals. The fence
136 // will usually be the one obtained from dequeueBuffer.
137 virtual void cancelBuffer(int slot, const sp<Fence>& fence);
138
139 // Query native window attributes. The "what" values are enumerated in
140 // window.h (e.g. NATIVE_WINDOW_FORMAT).
141 virtual int query(int what, int* outValue);
142
143 // connect attempts to connect a producer API to the BufferQueue. This
144 // must be called before any other IGraphicBufferProducer methods are
145 // called except for getAllocator. A consumer must already be connected.
146 //
147 // This method will fail if connect was previously called on the
148 // BufferQueue and no corresponding disconnect call was made (i.e. if
149 // it's still connected to a producer).
150 //
151 // APIs are enumerated in window.h (e.g. NATIVE_WINDOW_API_CPU).
Dan Stozaf0eaf252014-03-21 13:05:51 -0700152 virtual status_t connect(const sp<IProducerListener>& listener,
Dan Stoza289ade12014-02-28 11:17:17 -0800153 int api, bool producerControlledByApp, QueueBufferOutput* output);
154
155 // disconnect attempts to disconnect a producer API from the BufferQueue.
156 // Calling this method will cause any subsequent calls to other
157 // IGraphicBufferProducer methods to fail except for getAllocator and connect.
158 // Successfully calling connect after this will allow the other methods to
159 // succeed again.
160 //
161 // This method will fail if the the BufferQueue is not currently
162 // connected to the specified producer API.
163 virtual status_t disconnect(int api);
164
Jesse Hall399184a2014-03-03 15:42:54 -0800165 // Attaches a sideband buffer stream to the IGraphicBufferProducer.
166 //
167 // A sideband stream is a device-specific mechanism for passing buffers
168 // from the producer to the consumer without using dequeueBuffer/
169 // queueBuffer. If a sideband stream is present, the consumer can choose
170 // whether to acquire buffers from the sideband stream or from the queued
171 // buffers.
172 //
173 // Passing NULL or a different stream handle will detach the previous
174 // handle if any.
175 virtual status_t setSidebandStream(const sp<NativeHandle>& stream);
176
Dan Stoza29a3e902014-06-20 13:13:57 -0700177 // See IGraphicBufferProducer::allocateBuffers
178 virtual void allocateBuffers(bool async, uint32_t width, uint32_t height,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800179 PixelFormat format, uint32_t usage);
Dan Stoza29a3e902014-06-20 13:13:57 -0700180
Dan Stoza9de72932015-04-16 17:28:43 -0700181 // See IGraphicBufferProducer::allowAllocation
182 virtual status_t allowAllocation(bool allow);
183
Dan Stoza812ed062015-06-02 15:45:22 -0700184 // See IGraphicBufferProducer::setGenerationNumber
185 virtual status_t setGenerationNumber(uint32_t generationNumber);
186
Dan Stozac6f30bd2015-06-08 09:32:50 -0700187 // See IGraphicBufferProducer::getConsumerName
188 virtual String8 getConsumerName() const override;
189
Dan Stoza289ade12014-02-28 11:17:17 -0800190private:
191 // This is required by the IBinder::DeathRecipient interface
192 virtual void binderDied(const wp<IBinder>& who);
193
Dan Stoza9f3053d2014-03-06 15:14:33 -0800194 // waitForFreeSlotThenRelock finds the oldest slot in the FREE state. It may
195 // block if there are no available slots and we are not in non-blocking
196 // mode (producer and consumer controlled by the application). If it blocks,
197 // it will release mCore->mMutex while blocked so that other operations on
198 // the BufferQueue may succeed.
199 status_t waitForFreeSlotThenRelock(const char* caller, bool async,
200 int* found, status_t* returnFlags) const;
201
Dan Stoza289ade12014-02-28 11:17:17 -0800202 sp<BufferQueueCore> mCore;
Dan Stoza3e96f192014-03-03 10:16:19 -0800203
204 // This references mCore->mSlots. Lock mCore->mMutex while accessing.
205 BufferQueueDefs::SlotsType& mSlots;
Dan Stoza289ade12014-02-28 11:17:17 -0800206
207 // This is a cached copy of the name stored in the BufferQueueCore.
208 // It's updated during connect and dequeueBuffer (which should catch
209 // most updates).
210 String8 mConsumerName;
211
Ruben Brunk1681d952014-06-27 15:51:55 -0700212 uint32_t mStickyTransform;
213
Eric Penner99a0afb2014-09-30 11:28:30 -0700214 // This saves the fence from the last queueBuffer, such that the
215 // next queueBuffer call can throttle buffer production. The prior
216 // queueBuffer's fence is not nessessarily available elsewhere,
217 // since the previous buffer might have already been acquired.
218 sp<Fence> mLastQueueBufferFence;
219
Dan Stoza8dc55392014-11-04 11:37:46 -0800220 // Take-a-ticket system for ensuring that onFrame* callbacks are called in
221 // the order that frames are queued. While the BufferQueue lock
222 // (mCore->mMutex) is held, a ticket is retained by the producer. After
223 // dropping the BufferQueue lock, the producer must wait on the condition
224 // variable until the current callback ticket matches its retained ticket.
225 Mutex mCallbackMutex;
226 int mNextCallbackTicket; // Protected by mCore->mMutex
227 int mCurrentCallbackTicket; // Protected by mCallbackMutex
228 Condition mCallbackCondition;
229
Dan Stoza289ade12014-02-28 11:17:17 -0800230}; // class BufferQueueProducer
231
232} // namespace android
233
234#endif