blob: 258cd2f9aabb15ab8197a27f7583d4e218fffc3b [file] [log] [blame]
Jamie Gennis8ba32fa2010-12-20 11:27:26 -08001/*
2 * Copyright (C) 2010 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
Andy McFadden2adaf042012-12-18 09:49:45 -080017#ifndef ANDROID_GUI_IGRAPHICBUFFERPRODUCER_H
18#define ANDROID_GUI_IGRAPHICBUFFERPRODUCER_H
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080019
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <utils/Errors.h>
24#include <utils/RefBase.h>
25
26#include <binder/IInterface.h>
27
Jesse Hallf7857542012-06-14 15:26:33 -070028#include <ui/Fence.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080029#include <ui/GraphicBuffer.h>
30#include <ui/Rect.h>
Dan Stoza5065a552015-03-17 16:23:42 -070031#include <ui/Region.h>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080032
Pablo Ceballosce796e72016-02-04 19:10:51 -080033#include <gui/FrameTimestamps.h>
34
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080035namespace android {
36// ----------------------------------------------------------------------------
37
Dan Stozaf0eaf252014-03-21 13:05:51 -070038class IProducerListener;
Jesse Hall399184a2014-03-03 15:42:54 -080039class NativeHandle;
Mathias Agopiane3c697f2013-02-14 17:11:02 -080040class Surface;
Mathias Agopianeafabcd2011-04-20 14:20:59 -070041
Andy McFadden0273adb2012-12-12 17:10:08 -080042/*
Andy McFadden466a1922013-01-08 11:25:51 -080043 * This class defines the Binder IPC interface for the producer side of
44 * a queue of graphics buffers. It's used to send graphics data from one
45 * component to another. For example, a class that decodes video for
46 * playback might use this to provide frames. This is typically done
Mathias Agopiane3c697f2013-02-14 17:11:02 -080047 * indirectly, through Surface.
Andy McFadden0273adb2012-12-12 17:10:08 -080048 *
Andy McFadden466a1922013-01-08 11:25:51 -080049 * The underlying mechanism is a BufferQueue, which implements
50 * BnGraphicBufferProducer. In normal operation, the producer calls
51 * dequeueBuffer() to get an empty buffer, fills it with data, then
52 * calls queueBuffer() to make it available to the consumer.
Andy McFadden0273adb2012-12-12 17:10:08 -080053 *
Andy McFadden2adaf042012-12-18 09:49:45 -080054 * This class was previously called ISurfaceTexture.
Andy McFadden0273adb2012-12-12 17:10:08 -080055 */
Andy McFadden2adaf042012-12-18 09:49:45 -080056class IGraphicBufferProducer : public IInterface
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080057{
58public:
Colin Cross17576de2016-09-26 13:07:06 -070059 DECLARE_META_INTERFACE(GraphicBufferProducer)
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080060
Mathias Agopian80727112011-05-02 19:51:12 -070061 enum {
Igor Murashkin7d2d1602013-11-12 18:02:20 -080062 // A flag returned by dequeueBuffer when the client needs to call
63 // requestBuffer immediately thereafter.
Mathias Agopian80727112011-05-02 19:51:12 -070064 BUFFER_NEEDS_REALLOCATION = 0x1,
Igor Murashkin7d2d1602013-11-12 18:02:20 -080065 // A flag returned by dequeueBuffer when all mirrored slots should be
66 // released by the client. This flag should always be processed first.
Mathias Agopian80727112011-05-02 19:51:12 -070067 RELEASE_ALL_BUFFERS = 0x2,
68 };
Mathias Agopiana5c75c02011-03-31 19:10:24 -070069
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080070 // requestBuffer requests a new buffer for the given index. The server (i.e.
Andy McFadden2adaf042012-12-18 09:49:45 -080071 // the IGraphicBufferProducer implementation) assigns the newly created
72 // buffer to the given slot index, and the client is expected to mirror the
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080073 // slot->buffer mapping so that it's not necessary to transfer a
74 // GraphicBuffer for every dequeue operation.
Igor Murashkin7d2d1602013-11-12 18:02:20 -080075 //
76 // The slot must be in the range of [0, NUM_BUFFER_SLOTS).
77 //
78 // Return of a value other than NO_ERROR means an error has occurred:
Pablo Ceballos583b1b32015-09-03 18:23:52 -070079 // * NO_INIT - the buffer queue has been abandoned or the producer is not
80 // connected.
Igor Murashkin7d2d1602013-11-12 18:02:20 -080081 // * BAD_VALUE - one of the two conditions occurred:
82 // * slot was out of range (see above)
83 // * buffer specified by the slot is not dequeued
Jamie Gennis7b305ff2011-07-19 12:08:33 -070084 virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf) = 0;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080085
Pablo Ceballosfa455352015-08-12 17:47:47 -070086 // setMaxDequeuedBufferCount sets the maximum number of buffers that can be
Pablo Ceballos72daab62015-12-07 16:38:43 -080087 // dequeued by the producer at one time. If this method succeeds, any new
88 // buffer slots will be both unallocated and owned by the BufferQueue object
89 // (i.e. they are not owned by the producer or consumer). Calling this may
90 // also cause some buffer slots to be emptied. If the caller is caching the
Pablo Ceballosfa455352015-08-12 17:47:47 -070091 // contents of the buffer slots, it should empty that cache after calling
92 // this method.
93 //
Pablo Ceballos72daab62015-12-07 16:38:43 -080094 // This function should not be called with a value of maxDequeuedBuffers
95 // that is less than the number of currently dequeued buffer slots. Doing so
96 // will result in a BAD_VALUE error.
Pablo Ceballosfa455352015-08-12 17:47:47 -070097 //
98 // The buffer count should be at least 1 (inclusive), but at most
99 // (NUM_BUFFER_SLOTS - the minimum undequeued buffer count) (exclusive). The
100 // minimum undequeued buffer count can be obtained by calling
101 // query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS).
102 //
103 // Return of a value other than NO_ERROR means an error has occurred:
104 // * NO_INIT - the buffer queue has been abandoned.
105 // * BAD_VALUE - one of the below conditions occurred:
Pablo Ceballos72daab62015-12-07 16:38:43 -0800106 // * bufferCount was out of range (see above).
107 // * client would have more than the requested number of dequeued
108 // buffers after this call.
109 // * this call would cause the maxBufferCount value to be exceeded.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800110 // * failure to adjust the number of available slots.
Pablo Ceballosfa455352015-08-12 17:47:47 -0700111 virtual status_t setMaxDequeuedBufferCount(int maxDequeuedBuffers) = 0;
112
113 // Set the async flag if the producer intends to asynchronously queue
114 // buffers without blocking. Typically this is used for triple-buffering
115 // and/or when the swap interval is set to zero.
116 //
117 // Enabling async mode will internally allocate an additional buffer to
118 // allow for the asynchronous behavior. If it is not enabled queue/dequeue
119 // calls may block.
120 //
Pablo Ceballosfa455352015-08-12 17:47:47 -0700121 // Return of a value other than NO_ERROR means an error has occurred:
122 // * NO_INIT - the buffer queue has been abandoned.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800123 // * BAD_VALUE - one of the following has occurred:
124 // * this call would cause the maxBufferCount value to be
Pablo Ceballosb687a282015-10-06 11:14:51 -0700125 // exceeded
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800126 // * failure to adjust the number of available slots.
Pablo Ceballosfa455352015-08-12 17:47:47 -0700127 virtual status_t setAsyncMode(bool async) = 0;
128
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800129 // dequeueBuffer requests a new buffer slot for the client to use. Ownership
130 // of the slot is transfered to the client, meaning that the server will not
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800131 // use the contents of the buffer associated with that slot.
132 //
133 // The slot index returned may or may not contain a buffer (client-side).
134 // If the slot is empty the client should call requestBuffer to assign a new
135 // buffer to that slot.
136 //
137 // Once the client is done filling this buffer, it is expected to transfer
138 // buffer ownership back to the server with either cancelBuffer on
139 // the dequeued slot or to fill in the contents of its associated buffer
140 // contents and call queueBuffer.
141 //
142 // If dequeueBuffer returns the BUFFER_NEEDS_REALLOCATION flag, the client is
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700143 // expected to call requestBuffer immediately.
Jesse Hallf7857542012-06-14 15:26:33 -0700144 //
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800145 // If dequeueBuffer returns the RELEASE_ALL_BUFFERS flag, the client is
146 // expected to release all of the mirrored slot->buffer mappings.
147 //
Jesse Hallf7857542012-06-14 15:26:33 -0700148 // The fence parameter will be updated to hold the fence associated with
149 // the buffer. The contents of the buffer must not be overwritten until the
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800150 // fence signals. If the fence is Fence::NO_FENCE, the buffer may be written
Jesse Hallf7857542012-06-14 15:26:33 -0700151 // immediately.
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700152 //
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800153 // The width and height parameters must be no greater than the minimum of
154 // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
155 // An error due to invalid dimensions might not be reported until
156 // updateTexImage() is called. If width and height are both zero, the
157 // default values specified by setDefaultBufferSize() are used instead.
158 //
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800159 // If the format is 0, the default format will be used.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800160 //
161 // The usage argument specifies gralloc buffer usage flags. The values
162 // are enumerated in <gralloc.h>, e.g. GRALLOC_USAGE_HW_RENDER. These
163 // will be merged with the usage flags specified by
164 // IGraphicBufferConsumer::setConsumerUsageBits.
165 //
166 // This call will block until a buffer is available to be dequeued. If
167 // both the producer and consumer are controlled by the app, then this call
168 // can never block and will return WOULD_BLOCK if no buffer is available.
169 //
170 // A non-negative value with flags set (see above) will be returned upon
171 // success.
172 //
173 // Return of a negative means an error has occurred:
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700174 // * NO_INIT - the buffer queue has been abandoned or the producer is not
175 // connected.
Dan Stoza9f3053d2014-03-06 15:14:33 -0800176 // * BAD_VALUE - both in async mode and buffer count was less than the
177 // max numbers of buffers that can be allocated at once.
178 // * INVALID_OPERATION - cannot attach the buffer because it would cause
179 // too many buffers to be dequeued, either because
180 // the producer already has a single buffer dequeued
181 // and did not set a buffer count, or because a
182 // buffer count was set and this call would cause
183 // it to be exceeded.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800184 // * WOULD_BLOCK - no buffer is currently available, and blocking is disabled
185 // since both the producer/consumer are controlled by app
186 // * NO_MEMORY - out of memory, cannot allocate the graphics buffer.
Dan Stoza127fc632015-06-30 13:43:32 -0700187 // * TIMED_OUT - the timeout set by setDequeueTimeout was exceeded while
188 // waiting for a buffer to become available.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800189 //
190 // All other negative values are an unknown error returned downstream
191 // from the graphics allocator (typically errno).
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700192 virtual status_t dequeueBuffer(int* slot, sp<Fence>* fence, uint32_t w,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700193 uint32_t h, PixelFormat format, uint32_t usage,
194 FrameEventHistoryDelta* outTimestamps) = 0;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800195
Dan Stoza9f3053d2014-03-06 15:14:33 -0800196 // detachBuffer attempts to remove all ownership of the buffer in the given
197 // slot from the buffer queue. If this call succeeds, the slot will be
198 // freed, and there will be no way to obtain the buffer from this interface.
199 // The freed slot will remain unallocated until either it is selected to
200 // hold a freshly allocated buffer in dequeueBuffer or a buffer is attached
201 // to the slot. The buffer must have already been dequeued, and the caller
202 // must already possesses the sp<GraphicBuffer> (i.e., must have called
203 // requestBuffer).
204 //
205 // Return of a value other than NO_ERROR means an error has occurred:
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700206 // * NO_INIT - the buffer queue has been abandoned or the producer is not
207 // connected.
Dan Stoza9f3053d2014-03-06 15:14:33 -0800208 // * BAD_VALUE - the given slot number is invalid, either because it is
209 // out of the range [0, NUM_BUFFER_SLOTS), or because the slot
210 // it refers to is not currently dequeued and requested.
211 virtual status_t detachBuffer(int slot) = 0;
212
Dan Stozad9822a32014-03-28 15:25:31 -0700213 // detachNextBuffer is equivalent to calling dequeueBuffer, requestBuffer,
214 // and detachBuffer in sequence, except for two things:
215 //
216 // 1) It is unnecessary to know the dimensions, format, or usage of the
217 // next buffer.
218 // 2) It will not block, since if it cannot find an appropriate buffer to
219 // return, it will return an error instead.
220 //
221 // Only slots that are free but still contain a GraphicBuffer will be
222 // considered, and the oldest of those will be returned. outBuffer is
223 // equivalent to outBuffer from the requestBuffer call, and outFence is
224 // equivalent to fence from the dequeueBuffer call.
225 //
226 // Return of a value other than NO_ERROR means an error has occurred:
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700227 // * NO_INIT - the buffer queue has been abandoned or the producer is not
228 // connected.
Dan Stozad9822a32014-03-28 15:25:31 -0700229 // * BAD_VALUE - either outBuffer or outFence were NULL.
230 // * NO_MEMORY - no slots were found that were both free and contained a
231 // GraphicBuffer.
232 virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer,
233 sp<Fence>* outFence) = 0;
234
Dan Stoza9f3053d2014-03-06 15:14:33 -0800235 // attachBuffer attempts to transfer ownership of a buffer to the buffer
236 // queue. If this call succeeds, it will be as if this buffer was dequeued
237 // from the returned slot number. As such, this call will fail if attaching
238 // this buffer would cause too many buffers to be simultaneously dequeued.
239 //
240 // If attachBuffer returns the RELEASE_ALL_BUFFERS flag, the caller is
241 // expected to release all of the mirrored slot->buffer mappings.
242 //
243 // A non-negative value with flags set (see above) will be returned upon
244 // success.
245 //
246 // Return of a negative value means an error has occurred:
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700247 // * NO_INIT - the buffer queue has been abandoned or the producer is not
248 // connected.
Dan Stoza812ed062015-06-02 15:45:22 -0700249 // * BAD_VALUE - outSlot or buffer were NULL, invalid combination of
250 // async mode and buffer count override, or the generation
251 // number of the buffer did not match the buffer queue.
Dan Stoza9f3053d2014-03-06 15:14:33 -0800252 // * INVALID_OPERATION - cannot attach the buffer because it would cause
253 // too many buffers to be dequeued, either because
254 // the producer already has a single buffer dequeued
255 // and did not set a buffer count, or because a
256 // buffer count was set and this call would cause
257 // it to be exceeded.
258 // * WOULD_BLOCK - no buffer slot is currently available, and blocking is
259 // disabled since both the producer/consumer are
260 // controlled by the app.
Dan Stoza127fc632015-06-30 13:43:32 -0700261 // * TIMED_OUT - the timeout set by setDequeueTimeout was exceeded while
262 // waiting for a slot to become available.
Dan Stoza9f3053d2014-03-06 15:14:33 -0800263 virtual status_t attachBuffer(int* outSlot,
264 const sp<GraphicBuffer>& buffer) = 0;
265
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800266 // queueBuffer indicates that the client has finished filling in the
267 // contents of the buffer associated with slot and transfers ownership of
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800268 // that slot back to the server.
269 //
270 // It is not valid to call queueBuffer on a slot that is not owned
271 // by the client or one for which a buffer associated via requestBuffer
272 // (an attempt to do so will fail with a return value of BAD_VALUE).
273 //
274 // In addition, the input must be described by the client (as documented
275 // below). Any other properties (zero point, etc)
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800276 // are client-dependent, and should be documented by the client.
Mathias Agopian97c602c2011-07-19 15:24:46 -0700277 //
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800278 // The slot must be in the range of [0, NUM_BUFFER_SLOTS).
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700279 //
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800280 // Upon success, the output will be filled with meaningful values
281 // (refer to the documentation below).
282 //
283 // Return of a value other than NO_ERROR means an error has occurred:
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700284 // * NO_INIT - the buffer queue has been abandoned or the producer is not
285 // connected.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800286 // * BAD_VALUE - one of the below conditions occurred:
287 // * fence was NULL
288 // * scaling mode was unknown
289 // * both in async mode and buffer count was less than the
290 // max numbers of buffers that can be allocated at once
291 // * slot index was out of range (see above).
292 // * the slot was not in the dequeued state
293 // * the slot was enqueued without requesting a buffer
294 // * crop rect is out of bounds of the buffer dimensions
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700295
Mathias Agopiane1424282013-07-29 21:24:40 -0700296 struct QueueBufferInput : public Flattenable<QueueBufferInput> {
297 friend class Flattenable<QueueBufferInput>;
Chih-Hung Hsieh65d47872016-09-01 11:39:25 -0700298 explicit inline QueueBufferInput(const Parcel& parcel);
Brian Andersonbaaad322016-07-22 15:55:13 -0700299
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800300 // timestamp - a monotonically increasing value in nanoseconds
301 // isAutoTimestamp - if the timestamp was synthesized at queue time
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800302 // dataSpace - description of the contents, interpretation depends on format
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800303 // crop - a crop rectangle that's used as a hint to the consumer
304 // scalingMode - a set of flags from NATIVE_WINDOW_SCALING_* in <window.h>
305 // transform - a set of flags from NATIVE_WINDOW_TRANSFORM_* in <window.h>
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800306 // fence - a fence that the consumer must wait on before reading the buffer,
307 // set this to Fence::NO_FENCE if the buffer is ready immediately
Ruben Brunk1681d952014-06-27 15:51:55 -0700308 // sticky - the sticky transform set in Surface (only used by the LEGACY
309 // camera mode).
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700310 // getFrameTimestamps - whether or not the latest frame timestamps
311 // should be retrieved from the consumer.
Colin Cross382ecd32016-09-26 13:33:59 -0700312 inline QueueBufferInput(int64_t _timestamp, bool _isAutoTimestamp,
313 android_dataspace _dataSpace, const Rect& _crop,
314 int _scalingMode, uint32_t _transform, const sp<Fence>& _fence,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700315 uint32_t _sticky = 0, bool _getFrameTimestamps = false)
Colin Cross382ecd32016-09-26 13:33:59 -0700316 : timestamp(_timestamp), isAutoTimestamp(_isAutoTimestamp),
317 dataSpace(_dataSpace), crop(_crop), scalingMode(_scalingMode),
318 transform(_transform), stickyTransform(_sticky), fence(_fence),
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700319 surfaceDamage(), getFrameTimestamps(_getFrameTimestamps) { }
Brian Andersonbaaad322016-07-22 15:55:13 -0700320
Andy McFadden3c256212013-08-16 14:55:39 -0700321 inline void deflate(int64_t* outTimestamp, bool* outIsAutoTimestamp,
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800322 android_dataspace* outDataSpace,
323 Rect* outCrop, int* outScalingMode,
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700324 uint32_t* outTransform, sp<Fence>* outFence,
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700325 uint32_t* outStickyTransform = nullptr,
326 bool* outGetFrameTimestamps = nullptr) const {
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700327 *outTimestamp = timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700328 *outIsAutoTimestamp = bool(isAutoTimestamp);
Eino-Ville Talvala82c6bcc2015-02-19 16:10:43 -0800329 *outDataSpace = dataSpace;
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700330 *outCrop = crop;
331 *outScalingMode = scalingMode;
332 *outTransform = transform;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700333 *outFence = fence;
Ruben Brunk1681d952014-06-27 15:51:55 -0700334 if (outStickyTransform != NULL) {
335 *outStickyTransform = stickyTransform;
336 }
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700337 if (outGetFrameTimestamps) {
338 *outGetFrameTimestamps = getFrameTimestamps;
339 }
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700340 }
Jesse Hallc777b0b2012-06-28 12:52:05 -0700341
Mathias Agopiane1424282013-07-29 21:24:40 -0700342 // Flattenable protocol
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700343 static constexpr size_t minFlattenedSize();
Mathias Agopiane1424282013-07-29 21:24:40 -0700344 size_t getFlattenedSize() const;
345 size_t getFdCount() const;
346 status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
347 status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700348
Dan Stoza5065a552015-03-17 16:23:42 -0700349 const Region& getSurfaceDamage() const { return surfaceDamage; }
350 void setSurfaceDamage(const Region& damage) { surfaceDamage = damage; }
351
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700352 private:
Pablo Ceballosbd3577e2016-06-20 17:40:34 -0700353 int64_t timestamp{0};
354 int isAutoTimestamp{0};
355 android_dataspace dataSpace{HAL_DATASPACE_UNKNOWN};
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700356 Rect crop;
Pablo Ceballosbd3577e2016-06-20 17:40:34 -0700357 int scalingMode{0};
358 uint32_t transform{0};
359 uint32_t stickyTransform{0};
Jesse Hallc777b0b2012-06-28 12:52:05 -0700360 sp<Fence> fence;
Dan Stoza5065a552015-03-17 16:23:42 -0700361 Region surfaceDamage;
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700362 bool getFrameTimestamps{false};
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700363 };
364
Brian Andersonbaaad322016-07-22 15:55:13 -0700365 struct QueueBufferOutput : public Flattenable<QueueBufferOutput> {
Brian Anderson3d4039d2016-09-23 16:31:30 -0700366 QueueBufferOutput() = default;
367
368 // Moveable.
369 QueueBufferOutput(QueueBufferOutput&& src) = default;
370 QueueBufferOutput& operator=(QueueBufferOutput&& src) = default;
371 // Not copyable.
372 QueueBufferOutput(const QueueBufferOutput& src) = delete;
373 QueueBufferOutput& operator=(const QueueBufferOutput& src) = delete;
374
Brian Andersonbaaad322016-07-22 15:55:13 -0700375 // Flattenable protocol
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700376 static constexpr size_t minFlattenedSize();
Brian Andersonbaaad322016-07-22 15:55:13 -0700377 size_t getFlattenedSize() const;
378 size_t getFdCount() const;
379 status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
380 status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
381
Pablo Ceballosbd3577e2016-06-20 17:40:34 -0700382 uint32_t width{0};
383 uint32_t height{0};
384 uint32_t transformHint{0};
385 uint32_t numPendingBuffers{0};
Pablo Ceballosbc8c1922016-07-01 14:15:41 -0700386 uint64_t nextFrameNumber{0};
Brian Anderson7c3ba8a2016-07-25 12:48:08 -0700387 FrameEventHistoryDelta frameTimestamps;
Shuzhen Wang22f842b2017-01-18 23:02:36 -0800388 bool bufferReplaced{false};
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700389 };
390
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700391 virtual status_t queueBuffer(int slot, const QueueBufferInput& input,
392 QueueBufferOutput* output) = 0;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800393
394 // cancelBuffer indicates that the client does not wish to fill in the
395 // buffer associated with slot and transfers ownership of the slot back to
396 // the server.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800397 //
398 // The buffer is not queued for use by the consumer.
399 //
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700400 // The slot must be in the range of [0, NUM_BUFFER_SLOTS).
401 //
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800402 // The buffer will not be overwritten until the fence signals. The fence
403 // will usually be the one obtained from dequeueBuffer.
Pablo Ceballos583b1b32015-09-03 18:23:52 -0700404 //
405 // Return of a value other than NO_ERROR means an error has occurred:
406 // * NO_INIT - the buffer queue has been abandoned or the producer is not
407 // connected.
408 // * BAD_VALUE - one of the below conditions occurred:
409 // * fence was NULL
410 // * slot index was out of range (see above).
411 // * the slot was not in the dequeued state
412 virtual status_t cancelBuffer(int slot, const sp<Fence>& fence) = 0;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800413
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700414 // query retrieves some information for this surface
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800415 // 'what' tokens allowed are that of NATIVE_WINDOW_* in <window.h>
416 //
417 // Return of a value other than NO_ERROR means an error has occurred:
418 // * NO_INIT - the buffer queue has been abandoned.
419 // * BAD_VALUE - what was out of range
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700420 virtual int query(int what, int* value) = 0;
Mathias Agopian80727112011-05-02 19:51:12 -0700421
Andy McFadden2adaf042012-12-18 09:49:45 -0800422 // connect attempts to connect a client API to the IGraphicBufferProducer.
423 // This must be called before any other IGraphicBufferProducer methods are
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800424 // called except for getAllocator. A consumer must be already connected.
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700425 //
426 // This method will fail if the connect was previously called on the
Andy McFadden2adaf042012-12-18 09:49:45 -0800427 // IGraphicBufferProducer and no corresponding disconnect call was made.
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700428 //
Dan Stozaf0eaf252014-03-21 13:05:51 -0700429 // The listener is an optional binder callback object that can be used if
430 // the producer wants to be notified when the consumer releases a buffer
431 // back to the BufferQueue. It is also used to detect the death of the
432 // producer. If only the latter functionality is desired, there is a
433 // DummyProducerListener class in IProducerListener.h that can be used.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800434 //
435 // The api should be one of the NATIVE_WINDOW_API_* values in <window.h>
436 //
437 // The producerControlledByApp should be set to true if the producer is hosted
438 // by an untrusted process (typically app_process-forked processes). If both
439 // the producer and the consumer are app-controlled then all buffer queues
440 // will operate in async mode regardless of the async flag.
441 //
442 // Upon success, the output will be filled with meaningful data
443 // (refer to QueueBufferOutput documentation above).
444 //
445 // Return of a value other than NO_ERROR means an error has occurred:
446 // * NO_INIT - one of the following occurred:
447 // * the buffer queue was abandoned
448 // * no consumer has yet connected
449 // * BAD_VALUE - one of the following has occurred:
450 // * the producer is already connected
451 // * api was out of range (see above).
Igor Murashkin7ea777f2013-11-18 16:58:36 -0800452 // * output was NULL.
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800453 // * Failure to adjust the number of available slots. This can
454 // happen because of trying to allocate/deallocate the async
455 // buffer in response to the value of producerControlledByApp.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800456 // * DEAD_OBJECT - the token is hosted by an already-dead process
457 //
458 // Additional negative errors may be returned by the internals, they
459 // should be treated as opaque fatal unrecoverable errors.
Dan Stozaf0eaf252014-03-21 13:05:51 -0700460 virtual status_t connect(const sp<IProducerListener>& listener,
Mathias Agopian365857d2013-09-11 19:35:45 -0700461 int api, bool producerControlledByApp, QueueBufferOutput* output) = 0;
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700462
Robert Carr97b9c862016-09-08 13:54:35 -0700463 enum class DisconnectMode {
464 // Disconnect only the specified API.
465 Api,
466 // Disconnect any API originally connected from the process calling disconnect.
467 AllLocal
468 };
469
Andy McFadden2adaf042012-12-18 09:49:45 -0800470 // disconnect attempts to disconnect a client API from the
471 // IGraphicBufferProducer. Calling this method will cause any subsequent
472 // calls to other IGraphicBufferProducer methods to fail except for
473 // getAllocator and connect. Successfully calling connect after this will
474 // allow the other methods to succeed again.
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700475 //
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800476 // The api should be one of the NATIVE_WINDOW_API_* values in <window.h>
477 //
Robert Carr97b9c862016-09-08 13:54:35 -0700478 // Alternatively if mode is AllLocal, then the API value is ignored, and any API
479 // connected from the same PID calling disconnect will be disconnected.
480 //
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800481 // Disconnecting from an abandoned IGraphicBufferProducer is legal and
482 // is considered a no-op.
483 //
484 // Return of a value other than NO_ERROR means an error has occurred:
485 // * BAD_VALUE - one of the following has occurred:
486 // * the api specified does not match the one that was connected
487 // * api was out of range (see above).
488 // * DEAD_OBJECT - the token is hosted by an already-dead process
Robert Carr97b9c862016-09-08 13:54:35 -0700489 virtual status_t disconnect(int api, DisconnectMode mode = DisconnectMode::Api) = 0;
Jesse Hall399184a2014-03-03 15:42:54 -0800490
491 // Attaches a sideband buffer stream to the IGraphicBufferProducer.
492 //
493 // A sideband stream is a device-specific mechanism for passing buffers
494 // from the producer to the consumer without using dequeueBuffer/
495 // queueBuffer. If a sideband stream is present, the consumer can choose
496 // whether to acquire buffers from the sideband stream or from the queued
497 // buffers.
498 //
499 // Passing NULL or a different stream handle will detach the previous
500 // handle if any.
501 virtual status_t setSidebandStream(const sp<NativeHandle>& stream) = 0;
Dan Stoza29a3e902014-06-20 13:13:57 -0700502
503 // Allocates buffers based on the given dimensions/format.
504 //
505 // This function will allocate up to the maximum number of buffers
506 // permitted by the current BufferQueue configuration. It will use the
507 // given format, dimensions, and usage bits, which are interpreted in the
508 // same way as for dequeueBuffer, and the async flag must be set the same
509 // way as for dequeueBuffer to ensure that the correct number of buffers are
510 // allocated. This is most useful to avoid an allocation delay during
511 // dequeueBuffer. If there are already the maximum number of buffers
512 // allocated, this function has no effect.
Pablo Ceballos567dbbb2015-08-26 18:59:08 -0700513 virtual void allocateBuffers(uint32_t width, uint32_t height,
Dan Stoza3be1c6b2014-11-18 10:24:03 -0800514 PixelFormat format, uint32_t usage) = 0;
Dan Stoza9de72932015-04-16 17:28:43 -0700515
516 // Sets whether dequeueBuffer is allowed to allocate new buffers.
517 //
518 // Normally dequeueBuffer does not discriminate between free slots which
519 // already have an allocated buffer and those which do not, and will
520 // allocate a new buffer if the slot doesn't have a buffer or if the slot's
521 // buffer doesn't match the requested size, format, or usage. This method
522 // allows the producer to restrict the eligible slots to those which already
523 // have an allocated buffer of the correct size, format, and usage. If no
524 // eligible slot is available, dequeueBuffer will block or return an error
525 // as usual.
526 virtual status_t allowAllocation(bool allow) = 0;
Dan Stoza812ed062015-06-02 15:45:22 -0700527
528 // Sets the current generation number of the BufferQueue.
529 //
530 // This generation number will be inserted into any buffers allocated by the
531 // BufferQueue, and any attempts to attach a buffer with a different
532 // generation number will fail. Buffers already in the queue are not
533 // affected and will retain their current generation number. The generation
534 // number defaults to 0.
535 virtual status_t setGenerationNumber(uint32_t generationNumber) = 0;
Dan Stozac6f30bd2015-06-08 09:32:50 -0700536
537 // Returns the name of the connected consumer.
538 virtual String8 getConsumerName() const = 0;
Dan Stoza7dde5992015-05-22 09:51:44 -0700539
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700540 // Used to enable/disable shared buffer mode.
Pablo Ceballosccdfd602015-10-07 15:05:45 -0700541 //
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700542 // When shared buffer mode is enabled the first buffer that is queued or
Pablo Ceballos295a9fc2016-03-14 16:02:19 -0700543 // dequeued will be cached and returned to all subsequent calls to
544 // dequeueBuffer and acquireBuffer. This allows the producer and consumer to
545 // simultaneously access the same buffer.
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700546 virtual status_t setSharedBufferMode(bool sharedBufferMode) = 0;
Dan Stoza127fc632015-06-30 13:43:32 -0700547
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800548 // Used to enable/disable auto-refresh.
549 //
Pablo Ceballos3559fbf2016-03-17 15:50:23 -0700550 // Auto refresh has no effect outside of shared buffer mode. In shared
Pablo Ceballosff95aab2016-01-13 17:09:58 -0800551 // buffer mode, when enabled, it indicates to the consumer that it should
552 // attempt to acquire buffers even if it is not aware of any being
553 // available.
554 virtual status_t setAutoRefresh(bool autoRefresh) = 0;
555
Dan Stoza127fc632015-06-30 13:43:32 -0700556 // Sets how long dequeueBuffer will wait for a buffer to become available
557 // before returning an error (TIMED_OUT).
558 //
559 // This timeout also affects the attachBuffer call, which will block if
560 // there is not a free slot available into which the attached buffer can be
561 // placed.
562 //
563 // By default, the BufferQueue will wait forever, which is indicated by a
564 // timeout of -1. If set (to a value other than -1), this will disable
565 // non-blocking mode and its corresponding spare buffer (which is used to
566 // ensure a buffer is always available).
Pablo Ceballos23b4abe2016-01-08 12:15:22 -0800567 //
568 // Return of a value other than NO_ERROR means an error has occurred:
569 // * BAD_VALUE - Failure to adjust the number of available slots. This can
570 // happen because of trying to allocate/deallocate the async
571 // buffer.
Dan Stoza127fc632015-06-30 13:43:32 -0700572 virtual status_t setDequeueTimeout(nsecs_t timeout) = 0;
Dan Stoza50101d02016-04-07 16:53:23 -0700573
574 // Returns the last queued buffer along with a fence which must signal
575 // before the contents of the buffer are read. If there are no buffers in
576 // the queue, outBuffer will be populated with nullptr and outFence will be
577 // populated with Fence::NO_FENCE
578 //
John Reck1a61da52016-04-28 13:18:15 -0700579 // outTransformMatrix is not modified if outBuffer is null.
580 //
Dan Stoza50101d02016-04-07 16:53:23 -0700581 // Returns NO_ERROR or the status of the Binder transaction
582 virtual status_t getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
John Reck1a61da52016-04-28 13:18:15 -0700583 sp<Fence>* outFence, float outTransformMatrix[16]) = 0;
Pablo Ceballosce796e72016-02-04 19:10:51 -0800584
Brian Anderson3890c392016-07-25 12:48:08 -0700585 // Gets the frame events that haven't already been retrieved.
586 virtual void getFrameTimestamps(FrameEventHistoryDelta* /*outDelta*/) {}
Pablo Ceballos0ade2472016-06-30 16:48:02 -0700587
Pablo Ceballos8e3e92b2016-06-27 17:56:53 -0700588 // Returns a unique id for this BufferQueue
589 virtual status_t getUniqueId(uint64_t* outId) const = 0;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800590};
591
592// ----------------------------------------------------------------------------
593
Andy McFadden2adaf042012-12-18 09:49:45 -0800594class BnGraphicBufferProducer : public BnInterface<IGraphicBufferProducer>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800595{
596public:
597 virtual status_t onTransact( uint32_t code,
598 const Parcel& data,
599 Parcel* reply,
600 uint32_t flags = 0);
601};
602
603// ----------------------------------------------------------------------------
604}; // namespace android
605
Andy McFadden2adaf042012-12-18 09:49:45 -0800606#endif // ANDROID_GUI_IGRAPHICBUFFERPRODUCER_H