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