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