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/Rect.h>
     29 
     30 #include <EGL/egl.h>
     31 #include <EGL/eglext.h>
     32 
     33 namespace android {
     34 // ----------------------------------------------------------------------------
     35 
     36 class Fence;
     37 class GraphicBuffer;
     38 class IConsumerListener;
     39 class NativeHandle;
     40 
     41 class IGraphicBufferConsumer : public IInterface {
     42 
     43 public:
     44 
     45     // public facing structure for BufferSlot
     46     class BufferItem : public Flattenable<BufferItem> {
     47         friend class Flattenable<BufferItem>;
     48         size_t getPodSize() const;
     49         size_t getFlattenedSize() const;
     50         size_t getFdCount() const;
     51         status_t flatten(void*& buffer, size_t& size, int*& fds, size_t& count) const;
     52         status_t unflatten(void const*& buffer, size_t& size, int const*& fds, size_t& count);
     53 
     54     public:
     55         // The default value of mBuf, used to indicate this doesn't correspond to a slot.
     56         enum { INVALID_BUFFER_SLOT = -1 };
     57         BufferItem();
     58 
     59         // mGraphicBuffer points to the buffer allocated for this slot, or is NULL
     60         // if the buffer in this slot has been acquired in the past (see
     61         // BufferSlot.mAcquireCalled).
     62         sp<GraphicBuffer> mGraphicBuffer;
     63 
     64         // mFence is a fence that will signal when the buffer is idle.
     65         sp<Fence> mFence;
     66 
     67         // mCrop is the current crop rectangle for this buffer slot.
     68         Rect mCrop;
     69 
     70         // mTransform is the current transform flags for this buffer slot.
     71         // refer to NATIVE_WINDOW_TRANSFORM_* in <window.h>
     72         uint32_t mTransform;
     73 
     74         // mScalingMode is the current scaling mode for this buffer slot.
     75         // refer to NATIVE_WINDOW_SCALING_* in <window.h>
     76         uint32_t mScalingMode;
     77 
     78         // mTimestamp is the current timestamp for this buffer slot. This gets
     79         // to set by queueBuffer each time this slot is queued. This value
     80         // is guaranteed to be monotonically increasing for each newly
     81         // acquired buffer.
     82         int64_t mTimestamp;
     83 
     84         // mIsAutoTimestamp indicates whether mTimestamp was generated
     85         // automatically when the buffer was queued.
     86         bool mIsAutoTimestamp;
     87 
     88         // mFrameNumber is the number of the queued frame for this slot.
     89         uint64_t mFrameNumber;
     90 
     91         // mBuf is the slot index of this buffer (default INVALID_BUFFER_SLOT).
     92         int mBuf;
     93 
     94         // mIsDroppable whether this buffer was queued with the
     95         // property that it can be replaced by a new buffer for the purpose of
     96         // making sure dequeueBuffer() won't block.
     97         // i.e.: was the BufferQueue in "mDequeueBufferCannotBlock" when this buffer
     98         // was queued.
     99         bool mIsDroppable;
    100 
    101         // Indicates whether this buffer has been seen by a consumer yet
    102         bool mAcquireCalled;
    103 
    104         // Indicates this buffer must be transformed by the inverse transform of the screen
    105         // it is displayed onto. This is applied after mTransform.
    106         bool mTransformToDisplayInverse;
    107     };
    108 
    109     enum {
    110         // Returned by releaseBuffer, after which the consumer must
    111         // free any references to the just-released buffer that it might have.
    112         STALE_BUFFER_SLOT = 1,
    113         // Returned by dequeueBuffer if there are no pending buffers available.
    114         NO_BUFFER_AVAILABLE,
    115         // Returned by dequeueBuffer if it's too early for the buffer to be acquired.
    116         PRESENT_LATER,
    117     };
    118 
    119     // acquireBuffer attempts to acquire ownership of the next pending buffer in
    120     // the BufferQueue.  If no buffer is pending then it returns
    121     // NO_BUFFER_AVAILABLE.  If a buffer is successfully acquired, the
    122     // information about the buffer is returned in BufferItem.
    123     //
    124     // If the buffer returned had previously been
    125     // acquired then the BufferItem::mGraphicBuffer field of buffer is set to
    126     // NULL and it is assumed that the consumer still holds a reference to the
    127     // buffer.
    128     //
    129     // If presentWhen is non-zero, it indicates the time when the buffer will
    130     // be displayed on screen.  If the buffer's timestamp is farther in the
    131     // future, the buffer won't be acquired, and PRESENT_LATER will be
    132     // returned.  The presentation time is in nanoseconds, and the time base
    133     // is CLOCK_MONOTONIC.
    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     // * NO_BUFFER_AVAILABLE - no buffer is pending (nothing queued by producer)
    140     // * PRESENT_LATER - the buffer's timestamp is farther in the future
    141     //
    142     // Return of a negative value means an error has occurred:
    143     // * INVALID_OPERATION - too many buffers have been acquired
    144     virtual status_t acquireBuffer(BufferItem* buffer, nsecs_t presentWhen) = 0;
    145 
    146     // detachBuffer attempts to remove all ownership of the buffer in the given
    147     // slot from the buffer queue. If this call succeeds, the slot will be
    148     // freed, and there will be no way to obtain the buffer from this interface.
    149     // The freed slot will remain unallocated until either it is selected to
    150     // hold a freshly allocated buffer in dequeueBuffer or a buffer is attached
    151     // to the slot. The buffer must have already been acquired.
    152     //
    153     // Return of a value other than NO_ERROR means an error has occurred:
    154     // * BAD_VALUE - the given slot number is invalid, either because it is
    155     //               out of the range [0, NUM_BUFFER_SLOTS) or because the slot
    156     //               it refers to is not currently acquired.
    157     virtual status_t detachBuffer(int slot) = 0;
    158 
    159     // attachBuffer attempts to transfer ownership of a buffer to the buffer
    160     // queue. If this call succeeds, it will be as if this buffer was acquired
    161     // from the returned slot number. As such, this call will fail if attaching
    162     // this buffer would cause too many buffers to be simultaneously acquired.
    163     //
    164     // If the buffer is successfully attached, its frameNumber is initialized
    165     // to 0. This must be passed into the releaseBuffer call or else the buffer
    166     // will be deallocated as stale.
    167     //
    168     // Return of a value other than NO_ERROR means an error has occurred:
    169     // * BAD_VALUE - outSlot or buffer were NULL
    170     // * INVALID_OPERATION - cannot attach the buffer because it would cause too
    171     //                       many buffers to be acquired.
    172     // * NO_MEMORY - no free slots available
    173     virtual status_t attachBuffer(int *outSlot,
    174             const sp<GraphicBuffer>& buffer) = 0;
    175 
    176     // releaseBuffer releases a buffer slot from the consumer back to the
    177     // BufferQueue.  This may be done while the buffer's contents are still
    178     // being accessed.  The fence will signal when the buffer is no longer
    179     // in use. frameNumber is used to indentify the exact buffer returned.
    180     //
    181     // If releaseBuffer returns STALE_BUFFER_SLOT, then the consumer must free
    182     // any references to the just-released buffer that it might have, as if it
    183     // had received a onBuffersReleased() call with a mask set for the released
    184     // buffer.
    185     //
    186     // Note that the dependencies on EGL will be removed once we switch to using
    187     // the Android HW Sync HAL.
    188     //
    189     // Return of NO_ERROR means the operation completed as normal.
    190     //
    191     // Return of a positive value means the operation could not be completed
    192     //    at this time, but the user should try again later:
    193     // * STALE_BUFFER_SLOT - see above (second paragraph)
    194     //
    195     // Return of a negative value means an error has occurred:
    196     // * BAD_VALUE - one of the following could've happened:
    197     //               * the buffer slot was invalid
    198     //               * the fence was NULL
    199     //               * the buffer slot specified is not in the acquired state
    200     virtual status_t releaseBuffer(int buf, uint64_t frameNumber,
    201             EGLDisplay display, EGLSyncKHR fence,
    202             const sp<Fence>& releaseFence) = 0;
    203 
    204     // consumerConnect connects a consumer to the BufferQueue.  Only one
    205     // consumer may be connected, and when that consumer disconnects the
    206     // BufferQueue is placed into the "abandoned" state, causing most
    207     // interactions with the BufferQueue by the producer to fail.
    208     // controlledByApp indicates whether the consumer is controlled by
    209     // the application.
    210     //
    211     // consumer may not be NULL.
    212     //
    213     // Return of a value other than NO_ERROR means an error has occurred:
    214     // * NO_INIT - the buffer queue has been abandoned
    215     // * BAD_VALUE - a NULL consumer was provided
    216     virtual status_t consumerConnect(const sp<IConsumerListener>& consumer, bool controlledByApp) = 0;
    217 
    218     // consumerDisconnect disconnects a consumer from the BufferQueue. All
    219     // buffers will be freed and the BufferQueue is placed in the "abandoned"
    220     // state, causing most interactions with the BufferQueue by the producer to
    221     // fail.
    222     //
    223     // Return of a value other than NO_ERROR means an error has occurred:
    224     // * BAD_VALUE - no consumer is currently connected
    225     virtual status_t consumerDisconnect() = 0;
    226 
    227     // getReleasedBuffers sets the value pointed to by slotMask to a bit set.
    228     // Each bit index with a 1 corresponds to a released buffer slot with that
    229     // index value.  In particular, a released buffer is one that has
    230     // been released by the BufferQueue but have not yet been released by the consumer.
    231     //
    232     // This should be called from the onBuffersReleased() callback.
    233     //
    234     // Return of a value other than NO_ERROR means an error has occurred:
    235     // * NO_INIT - the buffer queue has been abandoned.
    236     virtual status_t getReleasedBuffers(uint64_t* slotMask) = 0;
    237 
    238     // setDefaultBufferSize is used to set the size of buffers returned by
    239     // dequeueBuffer when a width and height of zero is requested.  Default
    240     // is 1x1.
    241     //
    242     // Return of a value other than NO_ERROR means an error has occurred:
    243     // * BAD_VALUE - either w or h was zero
    244     virtual status_t setDefaultBufferSize(uint32_t w, uint32_t h) = 0;
    245 
    246     // setDefaultMaxBufferCount sets the default value for the maximum buffer
    247     // count (the initial default is 2). If the producer has requested a
    248     // buffer count using setBufferCount, the default buffer count will only
    249     // take effect if the producer sets the count back to zero.
    250     //
    251     // The count must be between 2 and NUM_BUFFER_SLOTS, inclusive.
    252     //
    253     // Return of a value other than NO_ERROR means an error has occurred:
    254     // * BAD_VALUE - bufferCount was out of range (see above).
    255     virtual status_t setDefaultMaxBufferCount(int bufferCount) = 0;
    256 
    257     // disableAsyncBuffer disables the extra buffer used in async mode
    258     // (when both producer and consumer have set their "isControlledByApp"
    259     // flag) and has dequeueBuffer() return WOULD_BLOCK instead.
    260     //
    261     // This can only be called before consumerConnect().
    262     //
    263     // Return of a value other than NO_ERROR means an error has occurred:
    264     // * INVALID_OPERATION - attempting to call this after consumerConnect.
    265     virtual status_t disableAsyncBuffer() = 0;
    266 
    267     // setMaxAcquiredBufferCount sets the maximum number of buffers that can
    268     // be acquired by the consumer at one time (default 1).  This call will
    269     // fail if a producer is connected to the BufferQueue.
    270     //
    271     // maxAcquiredBuffers must be (inclusive) between 1 and MAX_MAX_ACQUIRED_BUFFERS.
    272     //
    273     // Return of a value other than NO_ERROR means an error has occurred:
    274     // * BAD_VALUE - maxAcquiredBuffers was out of range (see above).
    275     // * INVALID_OPERATION - attempting to call this after a producer connected.
    276     virtual status_t setMaxAcquiredBufferCount(int maxAcquiredBuffers) = 0;
    277 
    278     // setConsumerName sets the name used in logging
    279     virtual void setConsumerName(const String8& name) = 0;
    280 
    281     // setDefaultBufferFormat allows the BufferQueue to create
    282     // GraphicBuffers of a defaultFormat if no format is specified
    283     // in dequeueBuffer.  Formats are enumerated in graphics.h; the
    284     // initial default is HAL_PIXEL_FORMAT_RGBA_8888.
    285     //
    286     // Return of a value other than NO_ERROR means an unknown error has occurred.
    287     virtual status_t setDefaultBufferFormat(uint32_t defaultFormat) = 0;
    288 
    289     // setConsumerUsageBits will turn on additional usage bits for dequeueBuffer.
    290     // These are merged with the bits passed to dequeueBuffer.  The values are
    291     // enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER; the default is 0.
    292     //
    293     // Return of a value other than NO_ERROR means an unknown error has occurred.
    294     virtual status_t setConsumerUsageBits(uint32_t usage) = 0;
    295 
    296     // setTransformHint bakes in rotation to buffers so overlays can be used.
    297     // The values are enumerated in window.h, e.g.
    298     // NATIVE_WINDOW_TRANSFORM_ROT_90.  The default is 0 (no transform).
    299     //
    300     // Return of a value other than NO_ERROR means an unknown error has occurred.
    301     virtual status_t setTransformHint(uint32_t hint) = 0;
    302 
    303     // Retrieve the sideband buffer stream, if any.
    304     virtual sp<NativeHandle> getSidebandStream() const = 0;
    305 
    306     // dump state into a string
    307     virtual void dump(String8& result, const char* prefix) const = 0;
    308 
    309 public:
    310     DECLARE_META_INTERFACE(GraphicBufferConsumer);
    311 };
    312 
    313 // ----------------------------------------------------------------------------
    314 
    315 class BnGraphicBufferConsumer : public BnInterface<IGraphicBufferConsumer>
    316 {
    317 public:
    318     virtual status_t    onTransact( uint32_t code,
    319                                     const Parcel& data,
    320                                     Parcel* reply,
    321                                     uint32_t flags = 0);
    322 };
    323 
    324 // ----------------------------------------------------------------------------
    325 }; // namespace android
    326 
    327 #endif // ANDROID_GUI_IGRAPHICBUFFERCONSUMER_H
    328