blob: 70025308c5aa3e83c0bf497cdb8934ec8253da6e [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>
31
32namespace android {
33// ----------------------------------------------------------------------------
34
Mathias Agopiane3c697f2013-02-14 17:11:02 -080035class Surface;
Mathias Agopianeafabcd2011-04-20 14:20:59 -070036
Andy McFadden0273adb2012-12-12 17:10:08 -080037/*
Andy McFadden466a1922013-01-08 11:25:51 -080038 * This class defines the Binder IPC interface for the producer side of
39 * a queue of graphics buffers. It's used to send graphics data from one
40 * component to another. For example, a class that decodes video for
41 * playback might use this to provide frames. This is typically done
Mathias Agopiane3c697f2013-02-14 17:11:02 -080042 * indirectly, through Surface.
Andy McFadden0273adb2012-12-12 17:10:08 -080043 *
Andy McFadden466a1922013-01-08 11:25:51 -080044 * The underlying mechanism is a BufferQueue, which implements
45 * BnGraphicBufferProducer. In normal operation, the producer calls
46 * dequeueBuffer() to get an empty buffer, fills it with data, then
47 * calls queueBuffer() to make it available to the consumer.
Andy McFadden0273adb2012-12-12 17:10:08 -080048 *
Andy McFadden2adaf042012-12-18 09:49:45 -080049 * This class was previously called ISurfaceTexture.
Andy McFadden0273adb2012-12-12 17:10:08 -080050 */
Andy McFadden2adaf042012-12-18 09:49:45 -080051class IGraphicBufferProducer : public IInterface
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080052{
53public:
Andy McFadden2adaf042012-12-18 09:49:45 -080054 DECLARE_META_INTERFACE(GraphicBufferProducer);
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080055
Mathias Agopian80727112011-05-02 19:51:12 -070056 enum {
Igor Murashkin7d2d1602013-11-12 18:02:20 -080057 // A flag returned by dequeueBuffer when the client needs to call
58 // requestBuffer immediately thereafter.
Mathias Agopian80727112011-05-02 19:51:12 -070059 BUFFER_NEEDS_REALLOCATION = 0x1,
Igor Murashkin7d2d1602013-11-12 18:02:20 -080060 // A flag returned by dequeueBuffer when all mirrored slots should be
61 // released by the client. This flag should always be processed first.
Mathias Agopian80727112011-05-02 19:51:12 -070062 RELEASE_ALL_BUFFERS = 0x2,
63 };
Mathias Agopiana5c75c02011-03-31 19:10:24 -070064
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080065 // requestBuffer requests a new buffer for the given index. The server (i.e.
Andy McFadden2adaf042012-12-18 09:49:45 -080066 // the IGraphicBufferProducer implementation) assigns the newly created
67 // buffer to the given slot index, and the client is expected to mirror the
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080068 // slot->buffer mapping so that it's not necessary to transfer a
69 // GraphicBuffer for every dequeue operation.
Igor Murashkin7d2d1602013-11-12 18:02:20 -080070 //
71 // The slot must be in the range of [0, NUM_BUFFER_SLOTS).
72 //
73 // Return of a value other than NO_ERROR means an error has occurred:
74 // * NO_INIT - the buffer queue has been abandoned.
75 // * BAD_VALUE - one of the two conditions occurred:
76 // * slot was out of range (see above)
77 // * buffer specified by the slot is not dequeued
Jamie Gennis7b305ff2011-07-19 12:08:33 -070078 virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf) = 0;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -080079
80 // setBufferCount sets the number of buffer slots available. Calling this
81 // will also cause all buffer slots to be emptied. The caller should empty
82 // its mirrored copy of the buffer slots when calling this method.
Igor Murashkin7d2d1602013-11-12 18:02:20 -080083 //
84 // This function should not be called when there are any dequeued buffer
85 // slots, doing so will result in a BAD_VALUE error returned.
86 //
Igor Murashkin7ea777f2013-11-18 16:58:36 -080087 // The buffer count should be at most NUM_BUFFER_SLOTS (inclusive), but at least
88 // the minimum undequeued buffer count (exclusive). The minimum value
89 // can be obtained by calling query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS).
90 // In particular the range is (minUndequeudBuffers, NUM_BUFFER_SLOTS].
91 //
92 // The buffer count may also be set to 0 (the default), to indicate that
93 // the producer does not wish to set a value.
Igor Murashkin7d2d1602013-11-12 18:02:20 -080094 //
95 // Return of a value other than NO_ERROR means an error has occurred:
96 // * NO_INIT - the buffer queue has been abandoned.
97 // * BAD_VALUE - one of the below conditions occurred:
98 // * bufferCount was out of range (see above)
99 // * client has one or more buffers dequeued
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800100 virtual status_t setBufferCount(int bufferCount) = 0;
101
102 // dequeueBuffer requests a new buffer slot for the client to use. Ownership
103 // of the slot is transfered to the client, meaning that the server will not
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800104 // use the contents of the buffer associated with that slot.
105 //
106 // The slot index returned may or may not contain a buffer (client-side).
107 // If the slot is empty the client should call requestBuffer to assign a new
108 // buffer to that slot.
109 //
110 // Once the client is done filling this buffer, it is expected to transfer
111 // buffer ownership back to the server with either cancelBuffer on
112 // the dequeued slot or to fill in the contents of its associated buffer
113 // contents and call queueBuffer.
114 //
115 // If dequeueBuffer returns the BUFFER_NEEDS_REALLOCATION flag, the client is
Mathias Agopiana5c75c02011-03-31 19:10:24 -0700116 // expected to call requestBuffer immediately.
Jesse Hallf7857542012-06-14 15:26:33 -0700117 //
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800118 // If dequeueBuffer returns the RELEASE_ALL_BUFFERS flag, the client is
119 // expected to release all of the mirrored slot->buffer mappings.
120 //
Jesse Hallf7857542012-06-14 15:26:33 -0700121 // The fence parameter will be updated to hold the fence associated with
122 // the buffer. The contents of the buffer must not be overwritten until the
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800123 // fence signals. If the fence is Fence::NO_FENCE, the buffer may be written
Jesse Hallf7857542012-06-14 15:26:33 -0700124 // immediately.
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700125 //
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800126 // The async parameter sets whether we're in asynchronous mode for this
127 // dequeueBuffer() call.
128 //
129 // The width and height parameters must be no greater than the minimum of
130 // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
131 // An error due to invalid dimensions might not be reported until
132 // updateTexImage() is called. If width and height are both zero, the
133 // default values specified by setDefaultBufferSize() are used instead.
134 //
135 // The pixel formats are enumerated in <graphics.h>, e.g.
136 // HAL_PIXEL_FORMAT_RGBA_8888. If the format is 0, the default format
137 // will be used.
138 //
139 // The usage argument specifies gralloc buffer usage flags. The values
140 // are enumerated in <gralloc.h>, e.g. GRALLOC_USAGE_HW_RENDER. These
141 // will be merged with the usage flags specified by
142 // IGraphicBufferConsumer::setConsumerUsageBits.
143 //
144 // This call will block until a buffer is available to be dequeued. If
145 // both the producer and consumer are controlled by the app, then this call
146 // can never block and will return WOULD_BLOCK if no buffer is available.
147 //
148 // A non-negative value with flags set (see above) will be returned upon
149 // success.
150 //
151 // Return of a negative means an error has occurred:
152 // * NO_INIT - the buffer queue has been abandoned.
153 // * BAD_VALUE - one of the below conditions occurred:
154 // * both in async mode and buffer count was less than the
155 // max numbers of buffers that can be allocated at once
156 // * attempting dequeue more than one buffer at a time
157 // without setting the buffer count with setBufferCount()
158 // * -EBUSY - attempting to dequeue too many buffers at a time
159 // * WOULD_BLOCK - no buffer is currently available, and blocking is disabled
160 // since both the producer/consumer are controlled by app
161 // * NO_MEMORY - out of memory, cannot allocate the graphics buffer.
162 //
163 // All other negative values are an unknown error returned downstream
164 // from the graphics allocator (typically errno).
165 virtual status_t dequeueBuffer(int* slot, sp<Fence>* fence, bool async,
Jesse Hallf7857542012-06-14 15:26:33 -0700166 uint32_t w, uint32_t h, uint32_t format, uint32_t usage) = 0;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800167
168 // queueBuffer indicates that the client has finished filling in the
169 // contents of the buffer associated with slot and transfers ownership of
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800170 // that slot back to the server.
171 //
172 // It is not valid to call queueBuffer on a slot that is not owned
173 // by the client or one for which a buffer associated via requestBuffer
174 // (an attempt to do so will fail with a return value of BAD_VALUE).
175 //
176 // In addition, the input must be described by the client (as documented
177 // below). Any other properties (zero point, etc)
Eino-Ville Talvala1d01a122011-02-18 11:02:42 -0800178 // are client-dependent, and should be documented by the client.
Mathias Agopian97c602c2011-07-19 15:24:46 -0700179 //
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800180 // The slot must be in the range of [0, NUM_BUFFER_SLOTS).
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700181 //
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800182 // Upon success, the output will be filled with meaningful values
183 // (refer to the documentation below).
184 //
185 // Return of a value other than NO_ERROR means an error has occurred:
186 // * NO_INIT - the buffer queue has been abandoned.
187 // * BAD_VALUE - one of the below conditions occurred:
188 // * fence was NULL
189 // * scaling mode was unknown
190 // * both in async mode and buffer count was less than the
191 // max numbers of buffers that can be allocated at once
192 // * slot index was out of range (see above).
193 // * the slot was not in the dequeued state
194 // * the slot was enqueued without requesting a buffer
195 // * crop rect is out of bounds of the buffer dimensions
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700196
Mathias Agopiane1424282013-07-29 21:24:40 -0700197 struct QueueBufferInput : public Flattenable<QueueBufferInput> {
198 friend class Flattenable<QueueBufferInput>;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700199 inline QueueBufferInput(const Parcel& parcel);
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800200 // timestamp - a monotonically increasing value in nanoseconds
201 // isAutoTimestamp - if the timestamp was synthesized at queue time
202 // crop - a crop rectangle that's used as a hint to the consumer
203 // scalingMode - a set of flags from NATIVE_WINDOW_SCALING_* in <window.h>
204 // transform - a set of flags from NATIVE_WINDOW_TRANSFORM_* in <window.h>
205 // async - if the buffer is queued in asynchronous mode
206 // fence - a fence that the consumer must wait on before reading the buffer,
207 // set this to Fence::NO_FENCE if the buffer is ready immediately
Andy McFadden3c256212013-08-16 14:55:39 -0700208 inline QueueBufferInput(int64_t timestamp, bool isAutoTimestamp,
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700209 const Rect& crop, int scalingMode, uint32_t transform, bool async,
210 const sp<Fence>& fence)
Andy McFadden3c256212013-08-16 14:55:39 -0700211 : timestamp(timestamp), isAutoTimestamp(isAutoTimestamp), crop(crop),
212 scalingMode(scalingMode), transform(transform), async(async),
213 fence(fence) { }
214 inline void deflate(int64_t* outTimestamp, bool* outIsAutoTimestamp,
215 Rect* outCrop, int* outScalingMode, uint32_t* outTransform,
216 bool* outAsync, sp<Fence>* outFence) const {
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700217 *outTimestamp = timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700218 *outIsAutoTimestamp = bool(isAutoTimestamp);
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700219 *outCrop = crop;
220 *outScalingMode = scalingMode;
221 *outTransform = transform;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700222 *outAsync = bool(async);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700223 *outFence = fence;
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700224 }
Jesse Hallc777b0b2012-06-28 12:52:05 -0700225
Mathias Agopiane1424282013-07-29 21:24:40 -0700226 // Flattenable protocol
227 size_t getFlattenedSize() const;
228 size_t getFdCount() const;
229 status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
230 status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
Jesse Hallc777b0b2012-06-28 12:52:05 -0700231
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700232 private:
233 int64_t timestamp;
Andy McFadden3c256212013-08-16 14:55:39 -0700234 int isAutoTimestamp;
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700235 Rect crop;
236 int scalingMode;
237 uint32_t transform;
Mathias Agopian7cdd7862013-07-18 22:10:56 -0700238 int async;
Jesse Hallc777b0b2012-06-28 12:52:05 -0700239 sp<Fence> fence;
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700240 };
241
242 // QueueBufferOutput must be a POD structure
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800243 struct __attribute__ ((__packed__)) QueueBufferOutput {
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700244 inline QueueBufferOutput() { }
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800245 // outWidth - filled with default width applied to the buffer
246 // outHeight - filled with default height applied to the buffer
247 // outTransformHint - filled with default transform applied to the buffer
248 // outNumPendingBuffers - num buffers queued that haven't yet been acquired
249 // (counting the currently queued buffer)
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700250 inline void deflate(uint32_t* outWidth,
Mathias Agopian2488b202012-04-20 17:19:28 -0700251 uint32_t* outHeight,
252 uint32_t* outTransformHint,
253 uint32_t* outNumPendingBuffers) const {
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700254 *outWidth = width;
255 *outHeight = height;
256 *outTransformHint = transformHint;
Mathias Agopian2488b202012-04-20 17:19:28 -0700257 *outNumPendingBuffers = numPendingBuffers;
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700258 }
259 inline void inflate(uint32_t inWidth, uint32_t inHeight,
Mathias Agopian2488b202012-04-20 17:19:28 -0700260 uint32_t inTransformHint, uint32_t inNumPendingBuffers) {
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700261 width = inWidth;
262 height = inHeight;
263 transformHint = inTransformHint;
Mathias Agopian2488b202012-04-20 17:19:28 -0700264 numPendingBuffers = inNumPendingBuffers;
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700265 }
266 private:
267 uint32_t width;
268 uint32_t height;
269 uint32_t transformHint;
Mathias Agopian2488b202012-04-20 17:19:28 -0700270 uint32_t numPendingBuffers;
Mathias Agopianf0bc2f12012-04-09 16:14:01 -0700271 };
272
273 virtual status_t queueBuffer(int slot,
274 const QueueBufferInput& input, QueueBufferOutput* output) = 0;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800275
276 // cancelBuffer indicates that the client does not wish to fill in the
277 // buffer associated with slot and transfers ownership of the slot back to
278 // the server.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800279 //
280 // The buffer is not queued for use by the consumer.
281 //
282 // The buffer will not be overwritten until the fence signals. The fence
283 // will usually be the one obtained from dequeueBuffer.
Jesse Hall4c00cc12013-03-15 21:34:30 -0700284 virtual void cancelBuffer(int slot, const sp<Fence>& fence) = 0;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800285
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700286 // query retrieves some information for this surface
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800287 // 'what' tokens allowed are that of NATIVE_WINDOW_* in <window.h>
288 //
289 // Return of a value other than NO_ERROR means an error has occurred:
290 // * NO_INIT - the buffer queue has been abandoned.
291 // * BAD_VALUE - what was out of range
Mathias Agopianeafabcd2011-04-20 14:20:59 -0700292 virtual int query(int what, int* value) = 0;
Mathias Agopian80727112011-05-02 19:51:12 -0700293
Andy McFadden2adaf042012-12-18 09:49:45 -0800294 // connect attempts to connect a client API to the IGraphicBufferProducer.
295 // This must be called before any other IGraphicBufferProducer methods are
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800296 // called except for getAllocator. A consumer must be already connected.
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700297 //
298 // This method will fail if the connect was previously called on the
Andy McFadden2adaf042012-12-18 09:49:45 -0800299 // IGraphicBufferProducer and no corresponding disconnect call was made.
Mathias Agopian5bfc2452011-08-08 19:14:03 -0700300 //
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800301 // The token needs to be any opaque binder object that lives in the
Mathias Agopian365857d2013-09-11 19:35:45 -0700302 // producer process -- it is solely used for obtaining a death notification
303 // when the producer is killed.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800304 //
305 // The api should be one of the NATIVE_WINDOW_API_* values in <window.h>
306 //
307 // The producerControlledByApp should be set to true if the producer is hosted
308 // by an untrusted process (typically app_process-forked processes). If both
309 // the producer and the consumer are app-controlled then all buffer queues
310 // will operate in async mode regardless of the async flag.
311 //
312 // Upon success, the output will be filled with meaningful data
313 // (refer to QueueBufferOutput documentation above).
314 //
315 // Return of a value other than NO_ERROR means an error has occurred:
316 // * NO_INIT - one of the following occurred:
317 // * the buffer queue was abandoned
318 // * no consumer has yet connected
319 // * BAD_VALUE - one of the following has occurred:
320 // * the producer is already connected
321 // * api was out of range (see above).
Igor Murashkin7ea777f2013-11-18 16:58:36 -0800322 // * output was NULL.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800323 // * DEAD_OBJECT - the token is hosted by an already-dead process
324 //
325 // Additional negative errors may be returned by the internals, they
326 // should be treated as opaque fatal unrecoverable errors.
Mathias Agopian365857d2013-09-11 19:35:45 -0700327 virtual status_t connect(const sp<IBinder>& token,
328 int api, bool producerControlledByApp, QueueBufferOutput* output) = 0;
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700329
Andy McFadden2adaf042012-12-18 09:49:45 -0800330 // disconnect attempts to disconnect a client API from the
331 // IGraphicBufferProducer. Calling this method will cause any subsequent
332 // calls to other IGraphicBufferProducer methods to fail except for
333 // getAllocator and connect. Successfully calling connect after this will
334 // allow the other methods to succeed again.
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700335 //
Andy McFadden2adaf042012-12-18 09:49:45 -0800336 // This method will fail if the the IGraphicBufferProducer is not currently
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700337 // connected to the specified client API.
Igor Murashkin7d2d1602013-11-12 18:02:20 -0800338 //
339 // The api should be one of the NATIVE_WINDOW_API_* values in <window.h>
340 //
341 // Disconnecting from an abandoned IGraphicBufferProducer is legal and
342 // is considered a no-op.
343 //
344 // Return of a value other than NO_ERROR means an error has occurred:
345 // * BAD_VALUE - one of the following has occurred:
346 // * the api specified does not match the one that was connected
347 // * api was out of range (see above).
348 // * DEAD_OBJECT - the token is hosted by an already-dead process
Jamie Gennisfe0a87b2011-07-13 19:12:20 -0700349 virtual status_t disconnect(int api) = 0;
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800350};
351
352// ----------------------------------------------------------------------------
353
Andy McFadden2adaf042012-12-18 09:49:45 -0800354class BnGraphicBufferProducer : public BnInterface<IGraphicBufferProducer>
Jamie Gennis8ba32fa2010-12-20 11:27:26 -0800355{
356public:
357 virtual status_t onTransact( uint32_t code,
358 const Parcel& data,
359 Parcel* reply,
360 uint32_t flags = 0);
361};
362
363// ----------------------------------------------------------------------------
364}; // namespace android
365
Andy McFadden2adaf042012-12-18 09:49:45 -0800366#endif // ANDROID_GUI_IGRAPHICBUFFERPRODUCER_H