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