IGraphicBufferProducer.h revision e5b755a045f4203fdd989047441259893c6fbe2d
1/* 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 17#ifndef ANDROID_GUI_IGRAPHICBUFFERPRODUCER_H 18#define ANDROID_GUI_IGRAPHICBUFFERPRODUCER_H 19 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 28#include <ui/Fence.h> 29#include <ui/GraphicBuffer.h> 30#include <ui/Rect.h> 31#include <ui/Region.h> 32 33namespace android { 34// ---------------------------------------------------------------------------- 35 36class IProducerListener; 37class NativeHandle; 38class Surface; 39 40/* 41 * This class defines the Binder IPC interface for the producer side of 42 * a queue of graphics buffers. It's used to send graphics data from one 43 * component to another. For example, a class that decodes video for 44 * playback might use this to provide frames. This is typically done 45 * indirectly, through Surface. 46 * 47 * The underlying mechanism is a BufferQueue, which implements 48 * BnGraphicBufferProducer. In normal operation, the producer calls 49 * dequeueBuffer() to get an empty buffer, fills it with data, then 50 * calls queueBuffer() to make it available to the consumer. 51 * 52 * This class was previously called ISurfaceTexture. 53 */ 54class IGraphicBufferProducer : public IInterface 55{ 56public: 57 DECLARE_META_INTERFACE(GraphicBufferProducer); 58 59 enum { 60 // A flag returned by dequeueBuffer when the client needs to call 61 // requestBuffer immediately thereafter. 62 BUFFER_NEEDS_REALLOCATION = 0x1, 63 // A flag returned by dequeueBuffer when all mirrored slots should be 64 // released by the client. This flag should always be processed first. 65 RELEASE_ALL_BUFFERS = 0x2, 66 }; 67 68 // requestBuffer requests a new buffer for the given index. The server (i.e. 69 // the IGraphicBufferProducer implementation) assigns the newly created 70 // buffer to the given slot index, and the client is expected to mirror the 71 // slot->buffer mapping so that it's not necessary to transfer a 72 // GraphicBuffer for every dequeue operation. 73 // 74 // The slot must be in the range of [0, NUM_BUFFER_SLOTS). 75 // 76 // Return of a value other than NO_ERROR means an error has occurred: 77 // * NO_INIT - the buffer queue has been abandoned. 78 // * BAD_VALUE - one of the two conditions occurred: 79 // * slot was out of range (see above) 80 // * buffer specified by the slot is not dequeued 81 virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf) = 0; 82 83 // setMaxDequeuedBufferCount sets the maximum number of buffers that can be 84 // dequeued by the producer at one time. If this method succeeds, buffer 85 // slots will be both unallocated and owned by the BufferQueue object (i.e. 86 // they are not owned by the producer or consumer). Calling this will also 87 // cause all buffer slots to be emptied. If the caller is caching the 88 // contents of the buffer slots, it should empty that cache after calling 89 // this method. 90 // 91 // This function should not be called when there are any currently dequeued 92 // buffer slots. Doing so will result in a BAD_VALUE error. 93 // 94 // The buffer count should be at least 1 (inclusive), but at most 95 // (NUM_BUFFER_SLOTS - the minimum undequeued buffer count) (exclusive). The 96 // minimum undequeued buffer count can be obtained by calling 97 // query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS). 98 // 99 // Return of a value other than NO_ERROR means an error has occurred: 100 // * NO_INIT - the buffer queue has been abandoned. 101 // * BAD_VALUE - one of the below conditions occurred: 102 // * bufferCount was out of range (see above) 103 // * client has one or more buffers dequeued 104 virtual status_t setMaxDequeuedBufferCount(int maxDequeuedBuffers) = 0; 105 106 // Set the async flag if the producer intends to asynchronously queue 107 // buffers without blocking. Typically this is used for triple-buffering 108 // and/or when the swap interval is set to zero. 109 // 110 // Enabling async mode will internally allocate an additional buffer to 111 // allow for the asynchronous behavior. If it is not enabled queue/dequeue 112 // calls may block. 113 // 114 // This function should not be called when there are any currently dequeued 115 // buffer slots, doing so will result in a BAD_VALUE error. 116 // 117 // Return of a value other than NO_ERROR means an error has occurred: 118 // * NO_INIT - the buffer queue has been abandoned. 119 // * BAD_VALUE - client has one or more buffers dequeued 120 virtual status_t setAsyncMode(bool async) = 0; 121 122 // dequeueBuffer requests a new buffer slot for the client to use. Ownership 123 // of the slot is transfered to the client, meaning that the server will not 124 // use the contents of the buffer associated with that slot. 125 // 126 // The slot index returned may or may not contain a buffer (client-side). 127 // If the slot is empty the client should call requestBuffer to assign a new 128 // buffer to that slot. 129 // 130 // Once the client is done filling this buffer, it is expected to transfer 131 // buffer ownership back to the server with either cancelBuffer on 132 // the dequeued slot or to fill in the contents of its associated buffer 133 // contents and call queueBuffer. 134 // 135 // If dequeueBuffer returns the BUFFER_NEEDS_REALLOCATION flag, the client is 136 // expected to call requestBuffer immediately. 137 // 138 // If dequeueBuffer returns the RELEASE_ALL_BUFFERS flag, the client is 139 // expected to release all of the mirrored slot->buffer mappings. 140 // 141 // The fence parameter will be updated to hold the fence associated with 142 // the buffer. The contents of the buffer must not be overwritten until the 143 // fence signals. If the fence is Fence::NO_FENCE, the buffer may be written 144 // immediately. 145 // 146 // The async parameter sets whether we're in asynchronous mode for this 147 // dequeueBuffer() call. 148 // 149 // The width and height parameters must be no greater than the minimum of 150 // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv). 151 // An error due to invalid dimensions might not be reported until 152 // updateTexImage() is called. If width and height are both zero, the 153 // default values specified by setDefaultBufferSize() are used instead. 154 // 155 // If the format is 0, the default format will be used. 156 // 157 // The usage argument specifies gralloc buffer usage flags. The values 158 // are enumerated in <gralloc.h>, e.g. GRALLOC_USAGE_HW_RENDER. These 159 // will be merged with the usage flags specified by 160 // IGraphicBufferConsumer::setConsumerUsageBits. 161 // 162 // This call will block until a buffer is available to be dequeued. If 163 // both the producer and consumer are controlled by the app, then this call 164 // can never block and will return WOULD_BLOCK if no buffer is available. 165 // 166 // A non-negative value with flags set (see above) will be returned upon 167 // success. 168 // 169 // Return of a negative means an error has occurred: 170 // * NO_INIT - the buffer queue has been abandoned. 171 // * BAD_VALUE - both in async mode and buffer count was less than the 172 // max numbers of buffers that can be allocated at once. 173 // * INVALID_OPERATION - cannot attach the buffer because it would cause 174 // too many buffers to be dequeued, either because 175 // the producer already has a single buffer dequeued 176 // and did not set a buffer count, or because a 177 // buffer count was set and this call would cause 178 // it to be exceeded. 179 // * WOULD_BLOCK - no buffer is currently available, and blocking is disabled 180 // since both the producer/consumer are controlled by app 181 // * NO_MEMORY - out of memory, cannot allocate the graphics buffer. 182 // 183 // All other negative values are an unknown error returned downstream 184 // from the graphics allocator (typically errno). 185 virtual status_t dequeueBuffer(int* slot, sp<Fence>* fence, bool async, 186 uint32_t w, uint32_t h, PixelFormat format, uint32_t usage) = 0; 187 188 // detachBuffer attempts to remove all ownership of the buffer in the given 189 // slot from the buffer queue. If this call succeeds, the slot will be 190 // freed, and there will be no way to obtain the buffer from this interface. 191 // The freed slot will remain unallocated until either it is selected to 192 // hold a freshly allocated buffer in dequeueBuffer or a buffer is attached 193 // to the slot. The buffer must have already been dequeued, and the caller 194 // must already possesses the sp<GraphicBuffer> (i.e., must have called 195 // requestBuffer). 196 // 197 // Return of a value other than NO_ERROR means an error has occurred: 198 // * NO_INIT - the buffer queue has been abandoned. 199 // * BAD_VALUE - the given slot number is invalid, either because it is 200 // out of the range [0, NUM_BUFFER_SLOTS), or because the slot 201 // it refers to is not currently dequeued and requested. 202 virtual status_t detachBuffer(int slot) = 0; 203 204 // detachNextBuffer is equivalent to calling dequeueBuffer, requestBuffer, 205 // and detachBuffer in sequence, except for two things: 206 // 207 // 1) It is unnecessary to know the dimensions, format, or usage of the 208 // next buffer. 209 // 2) It will not block, since if it cannot find an appropriate buffer to 210 // return, it will return an error instead. 211 // 212 // Only slots that are free but still contain a GraphicBuffer will be 213 // considered, and the oldest of those will be returned. outBuffer is 214 // equivalent to outBuffer from the requestBuffer call, and outFence is 215 // equivalent to fence from the dequeueBuffer call. 216 // 217 // Return of a value other than NO_ERROR means an error has occurred: 218 // * NO_INIT - the buffer queue has been abandoned. 219 // * BAD_VALUE - either outBuffer or outFence were NULL. 220 // * NO_MEMORY - no slots were found that were both free and contained a 221 // GraphicBuffer. 222 virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer, 223 sp<Fence>* outFence) = 0; 224 225 // attachBuffer attempts to transfer ownership of a buffer to the buffer 226 // queue. If this call succeeds, it will be as if this buffer was dequeued 227 // from the returned slot number. As such, this call will fail if attaching 228 // this buffer would cause too many buffers to be simultaneously dequeued. 229 // 230 // If attachBuffer returns the RELEASE_ALL_BUFFERS flag, the caller is 231 // expected to release all of the mirrored slot->buffer mappings. 232 // 233 // A non-negative value with flags set (see above) will be returned upon 234 // success. 235 // 236 // Return of a negative value means an error has occurred: 237 // * NO_INIT - the buffer queue has been abandoned. 238 // * BAD_VALUE - outSlot or buffer were NULL, invalid combination of 239 // async mode and buffer count override, or the generation 240 // number of the buffer did not match the buffer queue. 241 // * INVALID_OPERATION - cannot attach the buffer because it would cause 242 // too many buffers to be dequeued, either because 243 // the producer already has a single buffer dequeued 244 // and did not set a buffer count, or because a 245 // buffer count was set and this call would cause 246 // it to be exceeded. 247 // * WOULD_BLOCK - no buffer slot is currently available, and blocking is 248 // disabled since both the producer/consumer are 249 // controlled by the app. 250 virtual status_t attachBuffer(int* outSlot, 251 const sp<GraphicBuffer>& buffer) = 0; 252 253 // queueBuffer indicates that the client has finished filling in the 254 // contents of the buffer associated with slot and transfers ownership of 255 // that slot back to the server. 256 // 257 // It is not valid to call queueBuffer on a slot that is not owned 258 // by the client or one for which a buffer associated via requestBuffer 259 // (an attempt to do so will fail with a return value of BAD_VALUE). 260 // 261 // In addition, the input must be described by the client (as documented 262 // below). Any other properties (zero point, etc) 263 // are client-dependent, and should be documented by the client. 264 // 265 // The slot must be in the range of [0, NUM_BUFFER_SLOTS). 266 // 267 // Upon success, the output will be filled with meaningful values 268 // (refer to the documentation below). 269 // 270 // Return of a value other than NO_ERROR means an error has occurred: 271 // * NO_INIT - the buffer queue has been abandoned. 272 // * BAD_VALUE - one of the below conditions occurred: 273 // * fence was NULL 274 // * scaling mode was unknown 275 // * both in async mode and buffer count was less than the 276 // max numbers of buffers that can be allocated at once 277 // * slot index was out of range (see above). 278 // * the slot was not in the dequeued state 279 // * the slot was enqueued without requesting a buffer 280 // * crop rect is out of bounds of the buffer dimensions 281 282 struct QueueBufferInput : public Flattenable<QueueBufferInput> { 283 friend class Flattenable<QueueBufferInput>; 284 inline QueueBufferInput(const Parcel& parcel); 285 // timestamp - a monotonically increasing value in nanoseconds 286 // isAutoTimestamp - if the timestamp was synthesized at queue time 287 // dataSpace - description of the contents, interpretation depends on format 288 // crop - a crop rectangle that's used as a hint to the consumer 289 // scalingMode - a set of flags from NATIVE_WINDOW_SCALING_* in <window.h> 290 // transform - a set of flags from NATIVE_WINDOW_TRANSFORM_* in <window.h> 291 // async - if the buffer is queued in asynchronous mode 292 // fence - a fence that the consumer must wait on before reading the buffer, 293 // set this to Fence::NO_FENCE if the buffer is ready immediately 294 // sticky - the sticky transform set in Surface (only used by the LEGACY 295 // camera mode). 296 inline QueueBufferInput(int64_t timestamp, bool isAutoTimestamp, 297 android_dataspace dataSpace, const Rect& crop, int scalingMode, 298 uint32_t transform, bool async, const sp<Fence>& fence, 299 uint32_t sticky = 0) 300 : timestamp(timestamp), isAutoTimestamp(isAutoTimestamp), 301 dataSpace(dataSpace), crop(crop), scalingMode(scalingMode), 302 transform(transform), stickyTransform(sticky), 303 async(async), fence(fence), surfaceDamage() { } 304 inline void deflate(int64_t* outTimestamp, bool* outIsAutoTimestamp, 305 android_dataspace* outDataSpace, 306 Rect* outCrop, int* outScalingMode, 307 uint32_t* outTransform, bool* outAsync, sp<Fence>* outFence, 308 uint32_t* outStickyTransform = NULL) const { 309 *outTimestamp = timestamp; 310 *outIsAutoTimestamp = bool(isAutoTimestamp); 311 *outDataSpace = dataSpace; 312 *outCrop = crop; 313 *outScalingMode = scalingMode; 314 *outTransform = transform; 315 *outAsync = bool(async); 316 *outFence = fence; 317 if (outStickyTransform != NULL) { 318 *outStickyTransform = stickyTransform; 319 } 320 } 321 322 // Flattenable protocol 323 size_t getFlattenedSize() const; 324 size_t getFdCount() const; 325 status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const; 326 status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count); 327 328 const Region& getSurfaceDamage() const { return surfaceDamage; } 329 void setSurfaceDamage(const Region& damage) { surfaceDamage = damage; } 330 331 private: 332 int64_t timestamp; 333 int isAutoTimestamp; 334 android_dataspace dataSpace; 335 Rect crop; 336 int scalingMode; 337 uint32_t transform; 338 uint32_t stickyTransform; 339 int async; 340 sp<Fence> fence; 341 Region surfaceDamage; 342 }; 343 344 // QueueBufferOutput must be a POD structure 345 struct __attribute__ ((__packed__)) QueueBufferOutput { 346 inline QueueBufferOutput() { } 347 // outWidth - filled with default width applied to the buffer 348 // outHeight - filled with default height applied to the buffer 349 // outTransformHint - filled with default transform applied to the buffer 350 // outNumPendingBuffers - num buffers queued that haven't yet been acquired 351 // (counting the currently queued buffer) 352 inline void deflate(uint32_t* outWidth, 353 uint32_t* outHeight, 354 uint32_t* outTransformHint, 355 uint32_t* outNumPendingBuffers) const { 356 *outWidth = width; 357 *outHeight = height; 358 *outTransformHint = transformHint; 359 *outNumPendingBuffers = numPendingBuffers; 360 } 361 inline void inflate(uint32_t inWidth, uint32_t inHeight, 362 uint32_t inTransformHint, uint32_t inNumPendingBuffers) { 363 width = inWidth; 364 height = inHeight; 365 transformHint = inTransformHint; 366 numPendingBuffers = inNumPendingBuffers; 367 } 368 private: 369 uint32_t width; 370 uint32_t height; 371 uint32_t transformHint; 372 uint32_t numPendingBuffers; 373 }; 374 375 virtual status_t queueBuffer(int slot, 376 const QueueBufferInput& input, QueueBufferOutput* output) = 0; 377 378 // cancelBuffer indicates that the client does not wish to fill in the 379 // buffer associated with slot and transfers ownership of the slot back to 380 // the server. 381 // 382 // The buffer is not queued for use by the consumer. 383 // 384 // The buffer will not be overwritten until the fence signals. The fence 385 // will usually be the one obtained from dequeueBuffer. 386 virtual void cancelBuffer(int slot, const sp<Fence>& fence) = 0; 387 388 // query retrieves some information for this surface 389 // 'what' tokens allowed are that of NATIVE_WINDOW_* in <window.h> 390 // 391 // Return of a value other than NO_ERROR means an error has occurred: 392 // * NO_INIT - the buffer queue has been abandoned. 393 // * BAD_VALUE - what was out of range 394 virtual int query(int what, int* value) = 0; 395 396 // connect attempts to connect a client API to the IGraphicBufferProducer. 397 // This must be called before any other IGraphicBufferProducer methods are 398 // called except for getAllocator. A consumer must be already connected. 399 // 400 // This method will fail if the connect was previously called on the 401 // IGraphicBufferProducer and no corresponding disconnect call was made. 402 // 403 // The listener is an optional binder callback object that can be used if 404 // the producer wants to be notified when the consumer releases a buffer 405 // back to the BufferQueue. It is also used to detect the death of the 406 // producer. If only the latter functionality is desired, there is a 407 // DummyProducerListener class in IProducerListener.h that can be used. 408 // 409 // The api should be one of the NATIVE_WINDOW_API_* values in <window.h> 410 // 411 // The producerControlledByApp should be set to true if the producer is hosted 412 // by an untrusted process (typically app_process-forked processes). If both 413 // the producer and the consumer are app-controlled then all buffer queues 414 // will operate in async mode regardless of the async flag. 415 // 416 // Upon success, the output will be filled with meaningful data 417 // (refer to QueueBufferOutput documentation above). 418 // 419 // Return of a value other than NO_ERROR means an error has occurred: 420 // * NO_INIT - one of the following occurred: 421 // * the buffer queue was abandoned 422 // * no consumer has yet connected 423 // * BAD_VALUE - one of the following has occurred: 424 // * the producer is already connected 425 // * api was out of range (see above). 426 // * output was NULL. 427 // * DEAD_OBJECT - the token is hosted by an already-dead process 428 // 429 // Additional negative errors may be returned by the internals, they 430 // should be treated as opaque fatal unrecoverable errors. 431 virtual status_t connect(const sp<IProducerListener>& listener, 432 int api, bool producerControlledByApp, QueueBufferOutput* output) = 0; 433 434 // disconnect attempts to disconnect a client API from the 435 // IGraphicBufferProducer. Calling this method will cause any subsequent 436 // calls to other IGraphicBufferProducer methods to fail except for 437 // getAllocator and connect. Successfully calling connect after this will 438 // allow the other methods to succeed again. 439 // 440 // This method will fail if the the IGraphicBufferProducer is not currently 441 // connected to the specified client API. 442 // 443 // The api should be one of the NATIVE_WINDOW_API_* values in <window.h> 444 // 445 // Disconnecting from an abandoned IGraphicBufferProducer is legal and 446 // is considered a no-op. 447 // 448 // Return of a value other than NO_ERROR means an error has occurred: 449 // * BAD_VALUE - one of the following has occurred: 450 // * the api specified does not match the one that was connected 451 // * api was out of range (see above). 452 // * DEAD_OBJECT - the token is hosted by an already-dead process 453 virtual status_t disconnect(int api) = 0; 454 455 // Attaches a sideband buffer stream to the IGraphicBufferProducer. 456 // 457 // A sideband stream is a device-specific mechanism for passing buffers 458 // from the producer to the consumer without using dequeueBuffer/ 459 // queueBuffer. If a sideband stream is present, the consumer can choose 460 // whether to acquire buffers from the sideband stream or from the queued 461 // buffers. 462 // 463 // Passing NULL or a different stream handle will detach the previous 464 // handle if any. 465 virtual status_t setSidebandStream(const sp<NativeHandle>& stream) = 0; 466 467 // Allocates buffers based on the given dimensions/format. 468 // 469 // This function will allocate up to the maximum number of buffers 470 // permitted by the current BufferQueue configuration. It will use the 471 // given format, dimensions, and usage bits, which are interpreted in the 472 // same way as for dequeueBuffer, and the async flag must be set the same 473 // way as for dequeueBuffer to ensure that the correct number of buffers are 474 // allocated. This is most useful to avoid an allocation delay during 475 // dequeueBuffer. If there are already the maximum number of buffers 476 // allocated, this function has no effect. 477 virtual void allocateBuffers(bool async, uint32_t width, uint32_t height, 478 PixelFormat format, uint32_t usage) = 0; 479 480 // Sets whether dequeueBuffer is allowed to allocate new buffers. 481 // 482 // Normally dequeueBuffer does not discriminate between free slots which 483 // already have an allocated buffer and those which do not, and will 484 // allocate a new buffer if the slot doesn't have a buffer or if the slot's 485 // buffer doesn't match the requested size, format, or usage. This method 486 // allows the producer to restrict the eligible slots to those which already 487 // have an allocated buffer of the correct size, format, and usage. If no 488 // eligible slot is available, dequeueBuffer will block or return an error 489 // as usual. 490 virtual status_t allowAllocation(bool allow) = 0; 491 492 // Sets the current generation number of the BufferQueue. 493 // 494 // This generation number will be inserted into any buffers allocated by the 495 // BufferQueue, and any attempts to attach a buffer with a different 496 // generation number will fail. Buffers already in the queue are not 497 // affected and will retain their current generation number. The generation 498 // number defaults to 0. 499 virtual status_t setGenerationNumber(uint32_t generationNumber) = 0; 500 501 // Returns the name of the connected consumer. 502 virtual String8 getConsumerName() const = 0; 503}; 504 505// ---------------------------------------------------------------------------- 506 507class BnGraphicBufferProducer : public BnInterface<IGraphicBufferProducer> 508{ 509public: 510 virtual status_t onTransact( uint32_t code, 511 const Parcel& data, 512 Parcel* reply, 513 uint32_t flags = 0); 514}; 515 516// ---------------------------------------------------------------------------- 517}; // namespace android 518 519#endif // ANDROID_GUI_IGRAPHICBUFFERPRODUCER_H 520