Home | History | Annotate | Download | only in gui
      1 /*
      2  * Copyright (C) 2013 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_IGRAPHICBUFFERCONSUMER_H
     18 #define ANDROID_GUI_IGRAPHICBUFFERCONSUMER_H
     19 
     20 #include <stdint.h>
     21 #include <sys/types.h>
     22 
     23 #include <utils/Errors.h>
     24 #include <utils/RefBase.h>
     25 #include <utils/Timers.h>
     26 
     27 #include <binder/IInterface.h>
     28 #include <ui/PixelFormat.h>
     29 #include <ui/Rect.h>
     30 #include <gui/OccupancyTracker.h>
     31 
     32 #include <EGL/egl.h>
     33 #include <EGL/eglext.h>
     34 
     35 namespace android {
     36 // ----------------------------------------------------------------------------
     37 
     38 class BufferItem;
     39 class Fence;
     40 class GraphicBuffer;
     41 class IConsumerListener;
     42 class NativeHandle;
     43 
     44 class IGraphicBufferConsumer : public IInterface {
     45 
     46 public:
     47     enum {
     48         // Returned by releaseBuffer, after which the consumer must
     49         // free any references to the just-released buffer that it might have.
     50         STALE_BUFFER_SLOT = 1,
     51         // Returned by dequeueBuffer if there are no pending buffers available.
     52         NO_BUFFER_AVAILABLE,
     53         // Returned by dequeueBuffer if it's too early for the buffer to be acquired.
     54         PRESENT_LATER,
     55     };
     56 
     57     // acquireBuffer attempts to acquire ownership of the next pending buffer in
     58     // the BufferQueue.  If no buffer is pending then it returns
     59     // NO_BUFFER_AVAILABLE.  If a buffer is successfully acquired, the
     60     // information about the buffer is returned in BufferItem.
     61     //
     62     // If the buffer returned had previously been
     63     // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
     64     // NULL and it is assumed that the consumer still holds a reference to the
     65     // buffer.
     66     //
     67     // If presentWhen is non-zero, it indicates the time when the buffer will
     68     // be displayed on screen.  If the buffer's timestamp is farther in the
     69     // future, the buffer won't be acquired, and PRESENT_LATER will be
     70     // returned.  The presentation time is in nanoseconds, and the time base
     71     // is CLOCK_MONOTONIC.
     72     //
     73     // If maxFrameNumber is non-zero, it indicates that acquireBuffer should
     74     // only return a buffer with a frame number less than or equal to
     75     // maxFrameNumber. If no such frame is available (such as when a buffer has
     76     // been replaced but the consumer has not received the onFrameReplaced
     77     // callback), then PRESENT_LATER will be returned.
     78     //
     79     // Return of NO_ERROR means the operation completed as normal.
     80     //
     81     // Return of a positive value means the operation could not be completed
     82     //    at this time, but the user should try again later:
     83     // * NO_BUFFER_AVAILABLE - no buffer is pending (nothing queued by producer)
     84     // * PRESENT_LATER - the buffer's timestamp is farther in the future
     85     //
     86     // Return of a negative value means an error has occurred:
     87     // * INVALID_OPERATION - too many buffers have been acquired
     88     virtual status_t acquireBuffer(BufferItem* buffer, nsecs_t presentWhen,
     89             uint64_t maxFrameNumber = 0) = 0;
     90 
     91     // detachBuffer attempts to remove all ownership of the buffer in the given
     92     // slot from the buffer queue. If this call succeeds, the slot will be
     93     // freed, and there will be no way to obtain the buffer from this interface.
     94     // The freed slot will remain unallocated until either it is selected to
     95     // hold a freshly allocated buffer in dequeueBuffer or a buffer is attached
     96     // to the slot. The buffer must have already been acquired.
     97     //
     98     // Return of a value other than NO_ERROR means an error has occurred:
     99     // * BAD_VALUE - the given slot number is invalid, either because it is
    100     //               out of the range [0, NUM_BUFFER_SLOTS) or because the slot
    101     //               it refers to is not currently acquired.
    102     virtual status_t detachBuffer(int slot) = 0;
    103 
    104     // attachBuffer attempts to transfer ownership of a buffer to the buffer
    105     // queue. If this call succeeds, it will be as if this buffer was acquired
    106     // from the returned slot number. As such, this call will fail if attaching
    107     // this buffer would cause too many buffers to be simultaneously acquired.
    108     //
    109     // If the buffer is successfully attached, its frameNumber is initialized
    110     // to 0. This must be passed into the releaseBuffer call or else the buffer
    111     // will be deallocated as stale.
    112     //
    113     // Return of a value other than NO_ERROR means an error has occurred:
    114     // * BAD_VALUE - outSlot or buffer were NULL, or the generation number of
    115     //               the buffer did not match the buffer queue.
    116     // * INVALID_OPERATION - cannot attach the buffer because it would cause too
    117     //                       many buffers to be acquired.
    118     // * NO_MEMORY - no free slots available
    119     virtual status_t attachBuffer(int *outSlot,
    120             const sp<GraphicBuffer>& buffer) = 0;
    121 
    122     // releaseBuffer releases a buffer slot from the consumer back to the
    123     // BufferQueue.  This may be done while the buffer's contents are still
    124     // being accessed.  The fence will signal when the buffer is no longer
    125     // in use. frameNumber is used to indentify the exact buffer returned.
    126     //
    127     // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
    128     // any references to the just-released buffer that it might have, as if it
    129     // had received a onBuffersReleased() call with a mask set for the released
    130     // buffer.
    131     //
    132     // Note that the dependencies on EGL will be removed once we switch to using
    133     // the Android HW Sync HAL.
    134     //
    135     // Return of NO_ERROR means the operation completed as normal.
    136     //
    137     // Return of a positive value means the operation could not be completed
    138     //    at this time, but the user should try again later:
    139     // * STALE_BUFFER_SLOT - see above (second paragraph)
    140     //
    141     // Return of a negative value means an error has occurred:
    142     // * BAD_VALUE - one of the following could've happened:
    143     //               * the buffer slot was invalid
    144     //               * the fence was NULL
    145     //               * the buffer slot specified is not in the acquired state
    146     virtual status_t releaseBuffer(int buf, uint64_t frameNumber,
    147             EGLDisplay display, EGLSyncKHR fence,
    148             const sp<Fence>& releaseFence) = 0;
    149 
    150     // consumerConnect connects a consumer to the BufferQueue.  Only one
    151     // consumer may be connected, and when that consumer disconnects the
    152     // BufferQueue is placed into the "abandoned" state, causing most
    153     // interactions with the BufferQueue by the producer to fail.
    154     // controlledByApp indicates whether the consumer is controlled by
    155     // the application.
    156     //
    157     // consumer may not be NULL.
    158     //
    159     // Return of a value other than NO_ERROR means an error has occurred:
    160     // * NO_INIT - the buffer queue has been abandoned
    161     // * BAD_VALUE - a NULL consumer was provided
    162     virtual status_t consumerConnect(const sp<IConsumerListener>& consumer, bool controlledByApp) = 0;
    163 
    164     // consumerDisconnect disconnects a consumer from the BufferQueue. All
    165     // buffers will be freed and the BufferQueue is placed in the "abandoned"
    166     // state, causing most interactions with the BufferQueue by the producer to
    167     // fail.
    168     //
    169     // Return of a value other than NO_ERROR means an error has occurred:
    170     // * BAD_VALUE - no consumer is currently connected
    171     virtual status_t consumerDisconnect() = 0;
    172 
    173     // getReleasedBuffers sets the value pointed to by slotMask to a bit set.
    174     // Each bit index with a 1 corresponds to a released buffer slot with that
    175     // index value.  In particular, a released buffer is one that has
    176     // been released by the BufferQueue but have not yet been released by the consumer.
    177     //
    178     // This should be called from the onBuffersReleased() callback.
    179     //
    180     // Return of a value other than NO_ERROR means an error has occurred:
    181     // * NO_INIT - the buffer queue has been abandoned.
    182     virtual status_t getReleasedBuffers(uint64_t* slotMask) = 0;
    183 
    184     // setDefaultBufferSize is used to set the size of buffers returned by
    185     // dequeueBuffer when a width and height of zero is requested.  Default
    186     // is 1x1.
    187     //
    188     // Return of a value other than NO_ERROR means an error has occurred:
    189     // * BAD_VALUE - either w or h was zero
    190     virtual status_t setDefaultBufferSize(uint32_t w, uint32_t h) = 0;
    191 
    192     // setMaxBufferCount sets the maximum value for the number of buffers used
    193     // in the buffer queue (the initial default is NUM_BUFFER_SLOTS). If a call
    194     // to setMaxAcquiredBufferCount (by the consumer), or a call to setAsyncMode
    195     // or setMaxDequeuedBufferCount (by the producer), would cause this value to
    196     // be exceeded then that call will fail. This call will fail if a producer
    197     // is connected to the BufferQueue.
    198     //
    199     // The count must be between 1 and NUM_BUFFER_SLOTS, inclusive. The count
    200     // cannot be less than maxAcquiredBufferCount.
    201     //
    202     // Return of a value other than NO_ERROR means an error has occurred:
    203     // * BAD_VALUE - one of the below conditions occurred:
    204     //             * bufferCount was out of range (see above).
    205     //             * failure to adjust the number of available slots.
    206     // * INVALID_OPERATION - attempting to call this after a producer connected.
    207     virtual status_t setMaxBufferCount(int bufferCount) = 0;
    208 
    209     // setMaxAcquiredBufferCount sets the maximum number of buffers that can
    210     // be acquired by the consumer at one time (default 1). If this method
    211     // succeeds, any new buffer slots will be both unallocated and owned by the
    212     // BufferQueue object (i.e. they are not owned by the producer or consumer).
    213     // Calling this may also cause some buffer slots to be emptied.
    214     //
    215     // This function should not be called with a value of maxAcquiredBuffers
    216     // that is less than the number of currently acquired buffer slots. Doing so
    217     // will result in a BAD_VALUE error.
    218     //
    219     // maxAcquiredBuffers must be (inclusive) between 1 and
    220     // MAX_MAX_ACQUIRED_BUFFERS. It also cannot cause the maxBufferCount value
    221     // to be exceeded.
    222     //
    223     // Return of a value other than NO_ERROR means an error has occurred:
    224     // * NO_INIT - the buffer queue has been abandoned
    225     // * BAD_VALUE - one of the below conditions occurred:
    226     //             * maxAcquiredBuffers was out of range (see above).
    227     //             * failure to adjust the number of available slots.
    228     //             * client would have more than the requested number of
    229     //               acquired buffers after this call
    230     // * INVALID_OPERATION - attempting to call this after a producer connected.
    231     virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers) = 0;
    232 
    233     // setConsumerName sets the name used in logging
    234     virtual void setConsumerName(const String8& name) = 0;
    235 
    236     // setDefaultBufferFormat allows the BufferQueue to create
    237     // GraphicBuffers of a defaultFormat if no format is specified
    238     // in dequeueBuffer.
    239     // The initial default is PIXEL_FORMAT_RGBA_8888.
    240     //
    241     // Return of a value other than NO_ERROR means an unknown error has occurred.
    242     virtual status_t setDefaultBufferFormat(PixelFormat defaultFormat) = 0;
    243 
    244     // setDefaultBufferDataSpace is a request to the producer to provide buffers
    245     // of the indicated dataSpace. The producer may ignore this request.
    246     // The initial default is HAL_DATASPACE_UNKNOWN.
    247     //
    248     // Return of a value other than NO_ERROR means an unknown error has occurred.
    249     virtual status_t setDefaultBufferDataSpace(
    250             android_dataspace defaultDataSpace) = 0;
    251 
    252     // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
    253     // These are merged with the bits passed to dequeueBuffer.  The values are
    254     // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
    255     //
    256     // Return of a value other than NO_ERROR means an unknown error has occurred.
    257     virtual status_t setConsumerUsageBits(uint32_t usage) = 0;
    258 
    259     // setTransformHint bakes in rotation to buffers so overlays can be used.
    260     // The values are enumerated in window.h, e.g.
    261     // NATIVE_WINDOW_TRANSFORM_ROT_90.  The default is 0 (no transform).
    262     //
    263     // Return of a value other than NO_ERROR means an unknown error has occurred.
    264     virtual status_t setTransformHint(uint32_t hint) = 0;
    265 
    266     // Retrieve the sideband buffer stream, if any.
    267     virtual sp<NativeHandle> getSidebandStream() const = 0;
    268 
    269     // Retrieves any stored segments of the occupancy history of this
    270     // BufferQueue and clears them. Optionally closes out the pending segment if
    271     // forceFlush is true.
    272     virtual status_t getOccupancyHistory(bool forceFlush,
    273             std::vector<OccupancyTracker::Segment>* outHistory) = 0;
    274 
    275     // discardFreeBuffers releases all currently-free buffers held by the queue,
    276     // in order to reduce the memory consumption of the queue to the minimum
    277     // possible without discarding data.
    278     virtual status_t discardFreeBuffers() = 0;
    279 
    280     // dump state into a string
    281     virtual void dump(String8& result, const char* prefix) const = 0;
    282 
    283 public:
    284     DECLARE_META_INTERFACE(GraphicBufferConsumer);
    285 };
    286 
    287 // ----------------------------------------------------------------------------
    288 
    289 class BnGraphicBufferConsumer : public BnInterface<IGraphicBufferConsumer>
    290 {
    291 public:
    292     virtual status_t    onTransact( uint32_t code,
    293                                     const Parcel& data,
    294                                     Parcel* reply,
    295                                     uint32_t flags = 0);
    296 };
    297 
    298 // ----------------------------------------------------------------------------
    299 }; // namespace android
    300 
    301 #endif // ANDROID_GUI_IGRAPHICBUFFERCONSUMER_H
    302