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/BufferQueueDefs.h> 29 #include <ui/Fence.h> 30 #include <ui/GraphicBuffer.h> 31 #include <ui/Rect.h> 32 #include <ui/Region.h> 33 34 #include <gui/FrameTimestamps.h> 35 #include <gui/HdrMetadata.h> 36 37 #include <hidl/HybridInterface.h> 38 #include <android/hardware/graphics/bufferqueue/1.0/IGraphicBufferProducer.h> 39 #include <android/hardware/graphics/bufferqueue/2.0/IGraphicBufferProducer.h> 40 41 #include <optional> 42 #include <vector> 43 44 namespace android { 45 // ---------------------------------------------------------------------------- 46 47 class IProducerListener; 48 class NativeHandle; 49 class Surface; 50 51 using HGraphicBufferProducerV1_0 = 52 ::android::hardware::graphics::bufferqueue::V1_0:: 53 IGraphicBufferProducer; 54 using HGraphicBufferProducerV2_0 = 55 ::android::hardware::graphics::bufferqueue::V2_0:: 56 IGraphicBufferProducer; 57 58 /* 59 * This class defines the Binder IPC interface for the producer side of 60 * a queue of graphics buffers. It's used to send graphics data from one 61 * component to another. For example, a class that decodes video for 62 * playback might use this to provide frames. This is typically done 63 * indirectly, through Surface. 64 * 65 * The underlying mechanism is a BufferQueue, which implements 66 * BnGraphicBufferProducer. In normal operation, the producer calls 67 * dequeueBuffer() to get an empty buffer, fills it with data, then 68 * calls queueBuffer() to make it available to the consumer. 69 * 70 * This class was previously called ISurfaceTexture. 71 */ 72 #ifndef NO_BINDER 73 class IGraphicBufferProducer : public IInterface { 74 DECLARE_HYBRID_META_INTERFACE(GraphicBufferProducer, 75 HGraphicBufferProducerV1_0, 76 HGraphicBufferProducerV2_0) 77 #else 78 class IGraphicBufferProducer : public RefBase { 79 #endif 80 public: 81 enum { 82 // A flag returned by dequeueBuffer when the client needs to call 83 // requestBuffer immediately thereafter. 84 BUFFER_NEEDS_REALLOCATION = BufferQueueDefs::BUFFER_NEEDS_REALLOCATION, 85 // A flag returned by dequeueBuffer when all mirrored slots should be 86 // released by the client. This flag should always be processed first. 87 RELEASE_ALL_BUFFERS = BufferQueueDefs::RELEASE_ALL_BUFFERS, 88 }; 89 90 enum { 91 // A parcelable magic indicates using Binder BufferQueue as transport 92 // backend. 93 USE_BUFFER_QUEUE = 0x62717565, // 'bque' 94 // A parcelable magic indicates using BufferHub as transport backend. 95 USE_BUFFER_HUB = 0x62687562, // 'bhub' 96 }; 97 98 // requestBuffer requests a new buffer for the given index. The server (i.e. 99 // the IGraphicBufferProducer implementation) assigns the newly created 100 // buffer to the given slot index, and the client is expected to mirror the 101 // slot->buffer mapping so that it's not necessary to transfer a 102 // GraphicBuffer for every dequeue operation. 103 // 104 // The slot must be in the range of [0, NUM_BUFFER_SLOTS). 105 // 106 // Return of a value other than NO_ERROR means an error has occurred: 107 // * NO_INIT - the buffer queue has been abandoned or the producer is not 108 // connected. 109 // * BAD_VALUE - one of the two conditions occurred: 110 // * slot was out of range (see above) 111 // * buffer specified by the slot is not dequeued 112 virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf) = 0; 113 114 // setMaxDequeuedBufferCount sets the maximum number of buffers that can be 115 // dequeued by the producer at one time. If this method succeeds, any new 116 // buffer slots will be both unallocated and owned by the BufferQueue object 117 // (i.e. they are not owned by the producer or consumer). Calling this may 118 // also cause some buffer slots to be emptied. If the caller is caching the 119 // contents of the buffer slots, it should empty that cache after calling 120 // this method. 121 // 122 // This function should not be called with a value of maxDequeuedBuffers 123 // that is less than the number of currently dequeued buffer slots. Doing so 124 // will result in a BAD_VALUE error. 125 // 126 // The buffer count should be at least 1 (inclusive), but at most 127 // (NUM_BUFFER_SLOTS - the minimum undequeued buffer count) (exclusive). The 128 // minimum undequeued buffer count can be obtained by calling 129 // query(NATIVE_WINDOW_MIN_UNDEQUEUED_BUFFERS). 130 // 131 // Return of a value other than NO_ERROR means an error has occurred: 132 // * NO_INIT - the buffer queue has been abandoned. 133 // * BAD_VALUE - one of the below conditions occurred: 134 // * bufferCount was out of range (see above). 135 // * client would have more than the requested number of dequeued 136 // buffers after this call. 137 // * this call would cause the maxBufferCount value to be exceeded. 138 // * failure to adjust the number of available slots. 139 virtual status_t setMaxDequeuedBufferCount(int maxDequeuedBuffers) = 0; 140 141 // Set the async flag if the producer intends to asynchronously queue 142 // buffers without blocking. Typically this is used for triple-buffering 143 // and/or when the swap interval is set to zero. 144 // 145 // Enabling async mode will internally allocate an additional buffer to 146 // allow for the asynchronous behavior. If it is not enabled queue/dequeue 147 // calls may block. 148 // 149 // Return of a value other than NO_ERROR means an error has occurred: 150 // * NO_INIT - the buffer queue has been abandoned. 151 // * BAD_VALUE - one of the following has occurred: 152 // * this call would cause the maxBufferCount value to be 153 // exceeded 154 // * failure to adjust the number of available slots. 155 virtual status_t setAsyncMode(bool async) = 0; 156 157 // dequeueBuffer requests a new buffer slot for the client to use. Ownership 158 // of the slot is transfered to the client, meaning that the server will not 159 // use the contents of the buffer associated with that slot. 160 // 161 // The slot index returned may or may not contain a buffer (client-side). 162 // If the slot is empty the client should call requestBuffer to assign a new 163 // buffer to that slot. 164 // 165 // Once the client is done filling this buffer, it is expected to transfer 166 // buffer ownership back to the server with either cancelBuffer on 167 // the dequeued slot or to fill in the contents of its associated buffer 168 // contents and call queueBuffer. 169 // 170 // If dequeueBuffer returns the BUFFER_NEEDS_REALLOCATION flag, the client is 171 // expected to call requestBuffer immediately. 172 // 173 // If dequeueBuffer returns the RELEASE_ALL_BUFFERS flag, the client is 174 // expected to release all of the mirrored slot->buffer mappings. 175 // 176 // The fence parameter will be updated to hold the fence associated with 177 // the buffer. The contents of the buffer must not be overwritten until the 178 // fence signals. If the fence is Fence::NO_FENCE, the buffer may be written 179 // immediately. 180 // 181 // The width and height parameters must be no greater than the minimum of 182 // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv). 183 // An error due to invalid dimensions might not be reported until 184 // updateTexImage() is called. If width and height are both zero, the 185 // default values specified by setDefaultBufferSize() are used instead. 186 // 187 // If the format is 0, the default format will be used. 188 // 189 // The usage argument specifies gralloc buffer usage flags. The values 190 // are enumerated in <gralloc.h>, e.g. GRALLOC_USAGE_HW_RENDER. These 191 // will be merged with the usage flags specified by 192 // IGraphicBufferConsumer::setConsumerUsageBits. 193 // 194 // This call will block until a buffer is available to be dequeued. If 195 // both the producer and consumer are controlled by the app, then this call 196 // can never block and will return WOULD_BLOCK if no buffer is available. 197 // 198 // A non-negative value with flags set (see above) will be returned upon 199 // success. 200 // 201 // Return of a negative means an error has occurred: 202 // * NO_INIT - the buffer queue has been abandoned or the producer is not 203 // connected. 204 // * BAD_VALUE - both in async mode and buffer count was less than the 205 // max numbers of buffers that can be allocated at once. 206 // * INVALID_OPERATION - cannot attach the buffer because it would cause 207 // too many buffers to be dequeued, either because 208 // the producer already has a single buffer dequeued 209 // and did not set a buffer count, or because a 210 // buffer count was set and this call would cause 211 // it to be exceeded. 212 // * WOULD_BLOCK - no buffer is currently available, and blocking is disabled 213 // since both the producer/consumer are controlled by app 214 // * NO_MEMORY - out of memory, cannot allocate the graphics buffer. 215 // * TIMED_OUT - the timeout set by setDequeueTimeout was exceeded while 216 // waiting for a buffer to become available. 217 // 218 // All other negative values are an unknown error returned downstream 219 // from the graphics allocator (typically errno). 220 virtual status_t dequeueBuffer(int* slot, sp<Fence>* fence, uint32_t w, uint32_t h, 221 PixelFormat format, uint64_t usage, uint64_t* outBufferAge, 222 FrameEventHistoryDelta* outTimestamps) = 0; 223 224 // detachBuffer attempts to remove all ownership of the buffer in the given 225 // slot from the buffer queue. If this call succeeds, the slot will be 226 // freed, and there will be no way to obtain the buffer from this interface. 227 // The freed slot will remain unallocated until either it is selected to 228 // hold a freshly allocated buffer in dequeueBuffer or a buffer is attached 229 // to the slot. The buffer must have already been dequeued, and the caller 230 // must already possesses the sp<GraphicBuffer> (i.e., must have called 231 // requestBuffer). 232 // 233 // Return of a value other than NO_ERROR means an error has occurred: 234 // * NO_INIT - the buffer queue has been abandoned or the producer is not 235 // connected. 236 // * BAD_VALUE - the given slot number is invalid, either because it is 237 // out of the range [0, NUM_BUFFER_SLOTS), or because the slot 238 // it refers to is not currently dequeued and requested. 239 virtual status_t detachBuffer(int slot) = 0; 240 241 // detachNextBuffer is equivalent to calling dequeueBuffer, requestBuffer, 242 // and detachBuffer in sequence, except for two things: 243 // 244 // 1) It is unnecessary to know the dimensions, format, or usage of the 245 // next buffer. 246 // 2) It will not block, since if it cannot find an appropriate buffer to 247 // return, it will return an error instead. 248 // 249 // Only slots that are free but still contain a GraphicBuffer will be 250 // considered, and the oldest of those will be returned. outBuffer is 251 // equivalent to outBuffer from the requestBuffer call, and outFence is 252 // equivalent to fence from the dequeueBuffer call. 253 // 254 // Return of a value other than NO_ERROR means an error has occurred: 255 // * NO_INIT - the buffer queue has been abandoned or the producer is not 256 // connected. 257 // * BAD_VALUE - either outBuffer or outFence were NULL. 258 // * NO_MEMORY - no slots were found that were both free and contained a 259 // GraphicBuffer. 260 virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer, 261 sp<Fence>* outFence) = 0; 262 263 // attachBuffer attempts to transfer ownership of a buffer to the buffer 264 // queue. If this call succeeds, it will be as if this buffer was dequeued 265 // from the returned slot number. As such, this call will fail if attaching 266 // this buffer would cause too many buffers to be simultaneously dequeued. 267 // 268 // If attachBuffer returns the RELEASE_ALL_BUFFERS flag, the caller is 269 // expected to release all of the mirrored slot->buffer mappings. 270 // 271 // A non-negative value with flags set (see above) will be returned upon 272 // success. 273 // 274 // Return of a negative value means an error has occurred: 275 // * NO_INIT - the buffer queue has been abandoned or the producer is not 276 // connected. 277 // * BAD_VALUE - outSlot or buffer were NULL, invalid combination of 278 // async mode and buffer count override, or the generation 279 // number of the buffer did not match the buffer queue. 280 // * INVALID_OPERATION - cannot attach the buffer because it would cause 281 // too many buffers to be dequeued, either because 282 // the producer already has a single buffer dequeued 283 // and did not set a buffer count, or because a 284 // buffer count was set and this call would cause 285 // it to be exceeded. 286 // * WOULD_BLOCK - no buffer slot is currently available, and blocking is 287 // disabled since both the producer/consumer are 288 // controlled by the app. 289 // * TIMED_OUT - the timeout set by setDequeueTimeout was exceeded while 290 // waiting for a slot to become available. 291 virtual status_t attachBuffer(int* outSlot, 292 const sp<GraphicBuffer>& buffer) = 0; 293 294 struct QueueBufferInput : public Flattenable<QueueBufferInput> { QueueBufferInputQueueBufferInput295 explicit inline QueueBufferInput(const Parcel& parcel) { 296 parcel.read(*this); 297 } 298 299 // timestamp - a monotonically increasing value in nanoseconds 300 // isAutoTimestamp - if the timestamp was synthesized at queue time 301 // dataSpace - description of the contents, interpretation depends on format 302 // crop - a crop rectangle that's used as a hint to the consumer 303 // scalingMode - a set of flags from NATIVE_WINDOW_SCALING_* in <window.h> 304 // transform - a set of flags from NATIVE_WINDOW_TRANSFORM_* in <window.h> 305 // fence - a fence that the consumer must wait on before reading the buffer, 306 // set this to Fence::NO_FENCE if the buffer is ready immediately 307 // sticky - the sticky transform set in Surface (only used by the LEGACY 308 // camera mode). 309 // getFrameTimestamps - whether or not the latest frame timestamps 310 // should be retrieved from the consumer. 311 // slot - the slot index to queue. This is used only by queueBuffers(). 312 // queueBuffer() ignores this value and uses the argument `slot` 313 // instead. 314 inline QueueBufferInput(int64_t _timestamp, bool _isAutoTimestamp, 315 android_dataspace _dataSpace, const Rect& _crop, 316 int _scalingMode, uint32_t _transform, const sp<Fence>& _fence, 317 uint32_t _sticky = 0, bool _getFrameTimestamps = false, 318 int _slot = -1) 319 : timestamp(_timestamp), isAutoTimestamp(_isAutoTimestamp), 320 dataSpace(_dataSpace), crop(_crop), scalingMode(_scalingMode), 321 transform(_transform), stickyTransform(_sticky), 322 fence(_fence), surfaceDamage(), 323 getFrameTimestamps(_getFrameTimestamps), slot(_slot) { } 324 325 QueueBufferInput() = default; 326 327 inline void deflate(int64_t* outTimestamp, bool* outIsAutoTimestamp, 328 android_dataspace* outDataSpace, 329 Rect* outCrop, int* outScalingMode, 330 uint32_t* outTransform, sp<Fence>* outFence, 331 uint32_t* outStickyTransform = nullptr, 332 bool* outGetFrameTimestamps = nullptr, 333 int* outSlot = nullptr) const { 334 *outTimestamp = timestamp; 335 *outIsAutoTimestamp = bool(isAutoTimestamp); 336 *outDataSpace = dataSpace; 337 *outCrop = crop; 338 *outScalingMode = scalingMode; 339 *outTransform = transform; 340 *outFence = fence; 341 if (outStickyTransform != nullptr) { 342 *outStickyTransform = stickyTransform; 343 } 344 if (outGetFrameTimestamps) { 345 *outGetFrameTimestamps = getFrameTimestamps; 346 } 347 if (outSlot) { 348 *outSlot = slot; 349 } 350 } 351 352 // Flattenable protocol 353 static constexpr size_t minFlattenedSize(); 354 size_t getFlattenedSize() const; 355 size_t getFdCount() const; 356 status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const; 357 status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count); 358 getSurfaceDamageQueueBufferInput359 const Region& getSurfaceDamage() const { return surfaceDamage; } setSurfaceDamageQueueBufferInput360 void setSurfaceDamage(const Region& damage) { surfaceDamage = damage; } 361 getHdrMetadataQueueBufferInput362 const HdrMetadata& getHdrMetadata() const { return hdrMetadata; } setHdrMetadataQueueBufferInput363 void setHdrMetadata(const HdrMetadata& metadata) { hdrMetadata = metadata; } 364 365 int64_t timestamp{0}; 366 int isAutoTimestamp{0}; 367 android_dataspace dataSpace{HAL_DATASPACE_UNKNOWN}; 368 Rect crop; 369 int scalingMode{0}; 370 uint32_t transform{0}; 371 uint32_t stickyTransform{0}; 372 sp<Fence> fence; 373 Region surfaceDamage; 374 bool getFrameTimestamps{false}; 375 int slot{-1}; 376 HdrMetadata hdrMetadata; 377 }; 378 379 struct QueueBufferOutput : public Flattenable<QueueBufferOutput> { 380 QueueBufferOutput() = default; 381 382 // Moveable. 383 QueueBufferOutput(QueueBufferOutput&& src) = default; 384 QueueBufferOutput& operator=(QueueBufferOutput&& src) = default; 385 // Not copyable. 386 QueueBufferOutput(const QueueBufferOutput& src) = delete; 387 QueueBufferOutput& operator=(const QueueBufferOutput& src) = delete; 388 389 // Flattenable protocol 390 static constexpr size_t minFlattenedSize(); 391 size_t getFlattenedSize() const; 392 size_t getFdCount() const; 393 status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const; 394 status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count); 395 396 uint32_t width{0}; 397 uint32_t height{0}; 398 uint32_t transformHint{0}; 399 uint32_t numPendingBuffers{0}; 400 uint64_t nextFrameNumber{0}; 401 FrameEventHistoryDelta frameTimestamps; 402 bool bufferReplaced{false}; 403 int maxBufferCount{0}; 404 status_t result{NO_ERROR}; 405 }; 406 407 // queueBuffer indicates that the client has finished filling in the 408 // contents of the buffer associated with slot and transfers ownership of 409 // that slot back to the server. 410 // 411 // It is not valid to call queueBuffer on a slot that is not owned 412 // by the client or one for which a buffer associated via requestBuffer 413 // (an attempt to do so will fail with a return value of BAD_VALUE). 414 // 415 // In addition, the input must be described by the client (as documented 416 // below). Any other properties (zero point, etc) 417 // are client-dependent, and should be documented by the client. 418 // 419 // The slot must be in the range of [0, NUM_BUFFER_SLOTS). 420 // 421 // Upon success, the output will be filled with meaningful values 422 // (refer to the documentation below). 423 // 424 // Note: QueueBufferInput::slot was added to QueueBufferInput to be used by 425 // queueBuffers(), the batched version of queueBuffer(). The non-batched 426 // method (queueBuffer()) uses `slot` and ignores `input.slot`. 427 // 428 // Return of a value other than NO_ERROR means an error has occurred: 429 // * NO_INIT - the buffer queue has been abandoned or the producer is not 430 // connected. 431 // * BAD_VALUE - one of the below conditions occurred: 432 // * fence was NULL 433 // * scaling mode was unknown 434 // * both in async mode and buffer count was less than the 435 // max numbers of buffers that can be allocated at once 436 // * slot index was out of range (see above). 437 // * the slot was not in the dequeued state 438 // * the slot was enqueued without requesting a buffer 439 // * crop rect is out of bounds of the buffer dimensions 440 virtual status_t queueBuffer(int slot, const QueueBufferInput& input, 441 QueueBufferOutput* output) = 0; 442 443 // cancelBuffer indicates that the client does not wish to fill in the 444 // buffer associated with slot and transfers ownership of the slot back to 445 // the server. 446 // 447 // The buffer is not queued for use by the consumer. 448 // 449 // The slot must be in the range of [0, NUM_BUFFER_SLOTS). 450 // 451 // The buffer will not be overwritten until the fence signals. The fence 452 // will usually be the one obtained from dequeueBuffer. 453 // 454 // Return of a value other than NO_ERROR means an error has occurred: 455 // * NO_INIT - the buffer queue has been abandoned or the producer is not 456 // connected. 457 // * BAD_VALUE - one of the below conditions occurred: 458 // * fence was NULL 459 // * slot index was out of range (see above). 460 // * the slot was not in the dequeued state 461 virtual status_t cancelBuffer(int slot, const sp<Fence>& fence) = 0; 462 463 // query retrieves some information for this surface 464 // 'what' tokens allowed are that of NATIVE_WINDOW_* in <window.h> 465 // 466 // Return of a value other than NO_ERROR means an error has occurred: 467 // * NO_INIT - the buffer queue has been abandoned. 468 // * BAD_VALUE - what was out of range 469 virtual int query(int what, int* value) = 0; 470 471 // connect attempts to connect a client API to the IGraphicBufferProducer. 472 // This must be called before any other IGraphicBufferProducer methods are 473 // called except for getAllocator. A consumer must be already connected. 474 // 475 // This method will fail if the connect was previously called on the 476 // IGraphicBufferProducer and no corresponding disconnect call was made. 477 // 478 // The listener is an optional binder callback object that can be used if 479 // the producer wants to be notified when the consumer releases a buffer 480 // back to the BufferQueue. It is also used to detect the death of the 481 // producer. If only the latter functionality is desired, there is a 482 // StubProducerListener class in IProducerListener.h that can be used. 483 // 484 // The api should be one of the NATIVE_WINDOW_API_* values in <window.h> 485 // 486 // The producerControlledByApp should be set to true if the producer is hosted 487 // by an untrusted process (typically app_process-forked processes). If both 488 // the producer and the consumer are app-controlled then all buffer queues 489 // will operate in async mode regardless of the async flag. 490 // 491 // Upon success, the output will be filled with meaningful data 492 // (refer to QueueBufferOutput documentation above). 493 // 494 // Return of a value other than NO_ERROR means an error has occurred: 495 // * NO_INIT - one of the following occurred: 496 // * the buffer queue was abandoned 497 // * no consumer has yet connected 498 // * BAD_VALUE - one of the following has occurred: 499 // * the producer is already connected 500 // * api was out of range (see above). 501 // * output was NULL. 502 // * Failure to adjust the number of available slots. This can 503 // happen because of trying to allocate/deallocate the async 504 // buffer in response to the value of producerControlledByApp. 505 // * DEAD_OBJECT - the token is hosted by an already-dead process 506 // 507 // Additional negative errors may be returned by the internals, they 508 // should be treated as opaque fatal unrecoverable errors. 509 virtual status_t connect(const sp<IProducerListener>& listener, 510 int api, bool producerControlledByApp, QueueBufferOutput* output) = 0; 511 512 enum class DisconnectMode { 513 // Disconnect only the specified API. 514 Api, 515 // Disconnect any API originally connected from the process calling disconnect. 516 AllLocal 517 }; 518 519 // disconnect attempts to disconnect a client API from the 520 // IGraphicBufferProducer. Calling this method will cause any subsequent 521 // calls to other IGraphicBufferProducer methods to fail except for 522 // getAllocator and connect. Successfully calling connect after this will 523 // allow the other methods to succeed again. 524 // 525 // The api should be one of the NATIVE_WINDOW_API_* values in <window.h> 526 // 527 // Alternatively if mode is AllLocal, then the API value is ignored, and any API 528 // connected from the same PID calling disconnect will be disconnected. 529 // 530 // Disconnecting from an abandoned IGraphicBufferProducer is legal and 531 // is considered a no-op. 532 // 533 // Return of a value other than NO_ERROR means an error has occurred: 534 // * NO_INIT - the producer is not connected 535 // * BAD_VALUE - one of the following has occurred: 536 // * the api specified does not match the one that was connected 537 // * api was out of range (see above). 538 // * DEAD_OBJECT - the token is hosted by an already-dead process 539 virtual status_t disconnect(int api, DisconnectMode mode = DisconnectMode::Api) = 0; 540 541 // Attaches a sideband buffer stream to the IGraphicBufferProducer. 542 // 543 // A sideband stream is a device-specific mechanism for passing buffers 544 // from the producer to the consumer without using dequeueBuffer/ 545 // queueBuffer. If a sideband stream is present, the consumer can choose 546 // whether to acquire buffers from the sideband stream or from the queued 547 // buffers. 548 // 549 // Passing NULL or a different stream handle will detach the previous 550 // handle if any. 551 virtual status_t setSidebandStream(const sp<NativeHandle>& stream) = 0; 552 553 // Allocates buffers based on the given dimensions/format. 554 // 555 // This function will allocate up to the maximum number of buffers 556 // permitted by the current BufferQueue configuration. It will use the 557 // given format, dimensions, and usage bits, which are interpreted in the 558 // same way as for dequeueBuffer, and the async flag must be set the same 559 // way as for dequeueBuffer to ensure that the correct number of buffers are 560 // allocated. This is most useful to avoid an allocation delay during 561 // dequeueBuffer. If there are already the maximum number of buffers 562 // allocated, this function has no effect. 563 virtual void allocateBuffers(uint32_t width, uint32_t height, 564 PixelFormat format, uint64_t usage) = 0; 565 566 // Sets whether dequeueBuffer is allowed to allocate new buffers. 567 // 568 // Normally dequeueBuffer does not discriminate between free slots which 569 // already have an allocated buffer and those which do not, and will 570 // allocate a new buffer if the slot doesn't have a buffer or if the slot's 571 // buffer doesn't match the requested size, format, or usage. This method 572 // allows the producer to restrict the eligible slots to those which already 573 // have an allocated buffer of the correct size, format, and usage. If no 574 // eligible slot is available, dequeueBuffer will block or return an error 575 // as usual. 576 virtual status_t allowAllocation(bool allow) = 0; 577 578 // Sets the current generation number of the BufferQueue. 579 // 580 // This generation number will be inserted into any buffers allocated by the 581 // BufferQueue, and any attempts to attach a buffer with a different 582 // generation number will fail. Buffers already in the queue are not 583 // affected and will retain their current generation number. The generation 584 // number defaults to 0. 585 virtual status_t setGenerationNumber(uint32_t generationNumber) = 0; 586 587 // Returns the name of the connected consumer. 588 virtual String8 getConsumerName() const = 0; 589 590 // Used to enable/disable shared buffer mode. 591 // 592 // When shared buffer mode is enabled the first buffer that is queued or 593 // dequeued will be cached and returned to all subsequent calls to 594 // dequeueBuffer and acquireBuffer. This allows the producer and consumer to 595 // simultaneously access the same buffer. 596 virtual status_t setSharedBufferMode(bool sharedBufferMode) = 0; 597 598 // Used to enable/disable auto-refresh. 599 // 600 // Auto refresh has no effect outside of shared buffer mode. In shared 601 // buffer mode, when enabled, it indicates to the consumer that it should 602 // attempt to acquire buffers even if it is not aware of any being 603 // available. 604 virtual status_t setAutoRefresh(bool autoRefresh) = 0; 605 606 // Sets how long dequeueBuffer will wait for a buffer to become available 607 // before returning an error (TIMED_OUT). 608 // 609 // This timeout also affects the attachBuffer call, which will block if 610 // there is not a free slot available into which the attached buffer can be 611 // placed. 612 // 613 // By default, the BufferQueue will wait forever, which is indicated by a 614 // timeout of -1. If set (to a value other than -1), this will disable 615 // non-blocking mode and its corresponding spare buffer (which is used to 616 // ensure a buffer is always available). 617 // 618 // Note well: queueBuffer will stop buffer dropping behavior if timeout is 619 // strictly positive. If timeout is zero or negative, previous buffer 620 // dropping behavior will not be changed. 621 // 622 // Return of a value other than NO_ERROR means an error has occurred: 623 // * BAD_VALUE - Failure to adjust the number of available slots. This can 624 // happen because of trying to allocate/deallocate the async 625 // buffer. 626 virtual status_t setDequeueTimeout(nsecs_t timeout) = 0; 627 628 // Used to enable/disable buffer drop behavior of queueBuffer. 629 // If it's not used, legacy drop behavior will be retained. 630 virtual status_t setLegacyBufferDrop(bool drop); 631 632 // Returns the last queued buffer along with a fence which must signal 633 // before the contents of the buffer are read. If there are no buffers in 634 // the queue, outBuffer will be populated with nullptr and outFence will be 635 // populated with Fence::NO_FENCE 636 // 637 // outTransformMatrix is not modified if outBuffer is null. 638 // 639 // Returns NO_ERROR or the status of the Binder transaction 640 virtual status_t getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer, 641 sp<Fence>* outFence, float outTransformMatrix[16]) = 0; 642 643 // Returns the last queued buffer along with a fence which must signal 644 // before the contents of the buffer are read. If there are no buffers in 645 // the queue, outBuffer will be populated with nullptr and outFence will be 646 // populated with Fence::NO_FENCE 647 // 648 // outRect & outTransform are not modified if outBuffer is null. 649 // 650 // Returns NO_ERROR or the status of the Binder transaction 651 virtual status_t getLastQueuedBuffer([[maybe_unused]] sp<GraphicBuffer>* outBuffer, 652 [[maybe_unused]] sp<Fence>* outFence, 653 [[maybe_unused]] Rect* outRect, 654 [[maybe_unused]] uint32_t* outTransform) { 655 // Too many things implement IGraphicBufferProducer... 656 return UNKNOWN_TRANSACTION; 657 } 658 659 // Gets the frame events that haven't already been retrieved. getFrameTimestamps(FrameEventHistoryDelta *)660 virtual void getFrameTimestamps(FrameEventHistoryDelta* /*outDelta*/) {} 661 662 // Returns a unique id for this BufferQueue 663 virtual status_t getUniqueId(uint64_t* outId) const = 0; 664 665 // Returns the consumer usage flags for this BufferQueue. This returns the 666 // full 64-bit usage flags, rather than the truncated 32-bit usage flags 667 // returned by querying the now deprecated 668 // NATIVE_WINDOW_CONSUMER_USAGE_BITS attribute. 669 virtual status_t getConsumerUsage(uint64_t* outUsage) const = 0; 670 671 // Enable/disable the auto prerotation at buffer allocation when the buffer 672 // size is driven by the consumer. 673 // 674 // When buffer size is driven by the consumer and the transform hint 675 // specifies a 90 or 270 degree rotation, if auto prerotation is enabled, 676 // the width and height used for dequeueBuffer will be additionally swapped. 677 virtual status_t setAutoPrerotation(bool autoPrerotation); 678 679 struct RequestBufferOutput : public Flattenable<RequestBufferOutput> { 680 RequestBufferOutput() = default; 681 682 // Flattenable protocol 683 static constexpr size_t minFlattenedSize(); 684 size_t getFlattenedSize() const; 685 size_t getFdCount() const; 686 status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const; 687 status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count); 688 689 status_t result; 690 sp<GraphicBuffer> buffer; 691 }; 692 693 // Batched version of requestBuffer(). 694 // This method behaves like a sequence of requestBuffer() calls. 695 // The return value of the batched method will only be about the 696 // transaction. For a local call, the return value will always be NO_ERROR. 697 virtual status_t requestBuffers( 698 const std::vector<int32_t>& slots, 699 std::vector<RequestBufferOutput>* outputs); 700 701 struct DequeueBufferInput : public LightFlattenable<DequeueBufferInput> { 702 DequeueBufferInput() = default; 703 704 // LightFlattenable protocol isFixedSizeDequeueBufferInput705 inline bool isFixedSize() const { return true; } 706 size_t getFlattenedSize() const; 707 status_t flatten(void* buffer, size_t size) const; 708 status_t unflatten(void const* buffer, size_t size); 709 710 uint32_t width; 711 uint32_t height; 712 PixelFormat format; 713 uint64_t usage; 714 bool getTimestamps; 715 }; 716 717 struct DequeueBufferOutput : public Flattenable<DequeueBufferOutput> { 718 DequeueBufferOutput() = default; 719 720 // Flattenable protocol 721 static constexpr size_t minFlattenedSize(); 722 size_t getFlattenedSize() const; 723 size_t getFdCount() const; 724 status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const; 725 status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count); 726 727 status_t result; 728 int slot = -1; 729 sp<Fence> fence = Fence::NO_FENCE; 730 uint64_t bufferAge; 731 std::optional<FrameEventHistoryDelta> timestamps; 732 }; 733 734 // Batched version of dequeueBuffer(). 735 // This method behaves like a sequence of dequeueBuffer() calls. 736 // The return value of the batched method will only be about the 737 // transaction. For a local call, the return value will always be NO_ERROR. 738 virtual status_t dequeueBuffers( 739 const std::vector<DequeueBufferInput>& inputs, 740 std::vector<DequeueBufferOutput>* outputs); 741 742 // Batched version of detachBuffer(). 743 // This method behaves like a sequence of detachBuffer() calls. 744 // The return value of the batched method will only be about the 745 // transaction. For a local call, the return value will always be NO_ERROR. 746 virtual status_t detachBuffers(const std::vector<int32_t>& slots, 747 std::vector<status_t>* results); 748 749 750 struct AttachBufferOutput : public LightFlattenable<AttachBufferOutput> { 751 AttachBufferOutput() = default; 752 753 // LightFlattenable protocol isFixedSizeAttachBufferOutput754 inline bool isFixedSize() const { return true; } 755 size_t getFlattenedSize() const; 756 status_t flatten(void* buffer, size_t size) const; 757 status_t unflatten(void const* buffer, size_t size); 758 759 status_t result; 760 int slot; 761 }; 762 // Batched version of attachBuffer(). 763 // This method behaves like a sequence of attachBuffer() calls. 764 // The return value of the batched method will only be about the 765 // transaction. For a local call, the return value will always be NO_ERROR. 766 virtual status_t attachBuffers( 767 const std::vector<sp<GraphicBuffer>>& buffers, 768 std::vector<AttachBufferOutput>* outputs); 769 770 // Batched version of queueBuffer(). 771 // This method behaves like a sequence of queueBuffer() calls. 772 // The return value of the batched method will only be about the 773 // transaction. For a local call, the return value will always be NO_ERROR. 774 // 775 // Note: QueueBufferInput::slot was added to QueueBufferInput to include the 776 // `slot` input argument of the non-batched method queueBuffer(). 777 virtual status_t queueBuffers(const std::vector<QueueBufferInput>& inputs, 778 std::vector<QueueBufferOutput>* outputs); 779 780 struct CancelBufferInput : public Flattenable<CancelBufferInput> { 781 CancelBufferInput() = default; 782 783 // Flattenable protocol 784 static constexpr size_t minFlattenedSize(); 785 size_t getFlattenedSize() const; 786 size_t getFdCount() const; 787 status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const; 788 status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count); 789 790 int slot; 791 sp<Fence> fence; 792 }; 793 // Batched version of cancelBuffer(). 794 // This method behaves like a sequence of cancelBuffer() calls. 795 // The return value of the batched method will only be about the 796 // transaction. For a local call, the return value will always be NO_ERROR. 797 virtual status_t cancelBuffers( 798 const std::vector<CancelBufferInput>& inputs, 799 std::vector<status_t>* results); 800 801 struct QueryOutput : public LightFlattenable<QueryOutput> { 802 QueryOutput() = default; 803 804 // LightFlattenable protocol isFixedSizeQueryOutput805 inline bool isFixedSize() const { return true; } 806 size_t getFlattenedSize() const; 807 status_t flatten(void* buffer, size_t size) const; 808 status_t unflatten(void const* buffer, size_t size); 809 810 status_t result; 811 int64_t value; 812 }; 813 // Batched version of query(). 814 // This method behaves like a sequence of query() calls. 815 // The return value of the batched method will only be about the 816 // transaction. For a local call, the return value will always be NO_ERROR. 817 virtual status_t query(const std::vector<int32_t> inputs, 818 std::vector<QueryOutput>* outputs); 819 820 #ifndef NO_BINDER 821 // Static method exports any IGraphicBufferProducer object to a parcel. It 822 // handles null producer as well. 823 static status_t exportToParcel(const sp<IGraphicBufferProducer>& producer, 824 Parcel* parcel); 825 826 // Factory method that creates a new IBGP instance from the parcel. 827 static sp<IGraphicBufferProducer> createFromParcel(const Parcel* parcel); 828 829 protected: 830 // Exports the current producer as a binder parcelable object. Note that the 831 // producer must be disconnected to be exportable. After successful export, 832 // the producer queue can no longer be connected again. Returns NO_ERROR 833 // when the export is successful and writes an implementation defined 834 // parcelable object into the parcel. For traditional Android BufferQueue, 835 // it writes a strong binder object; for BufferHub, it writes a 836 // ProducerQueueParcelable object. 837 virtual status_t exportToParcel(Parcel* parcel); 838 #endif 839 }; 840 841 // ---------------------------------------------------------------------------- 842 #ifndef NO_BINDER 843 class BnGraphicBufferProducer : public BnInterface<IGraphicBufferProducer> 844 { 845 public: 846 virtual status_t onTransact( uint32_t code, 847 const Parcel& data, 848 Parcel* reply, 849 uint32_t flags = 0); 850 }; 851 #else 852 class BnGraphicBufferProducer : public IGraphicBufferProducer { 853 }; 854 #endif 855 856 // ---------------------------------------------------------------------------- 857 }; // namespace android 858 859 #endif // ANDROID_GUI_IGRAPHICBUFFERPRODUCER_H 860