Home | History | Annotate | Download | only in gui
      1 /*
      2  * Copyright 2014 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_BUFFERQUEUEPRODUCER_H
     18 #define ANDROID_GUI_BUFFERQUEUEPRODUCER_H
     19 
     20 #include <gui/BufferQueueDefs.h>
     21 #include <gui/IGraphicBufferProducer.h>
     22 
     23 namespace android {
     24 
     25 struct BufferSlot;
     26 
     27 class BufferQueueProducer : public BnGraphicBufferProducer,
     28                             private IBinder::DeathRecipient {
     29 public:
     30     friend class BufferQueue; // Needed to access binderDied
     31 
     32     BufferQueueProducer(const sp<BufferQueueCore>& core, bool consumerIsSurfaceFlinger = false);
     33     ~BufferQueueProducer() override;
     34 
     35     // requestBuffer returns the GraphicBuffer for slot N.
     36     //
     37     // In normal operation, this is called the first time slot N is returned
     38     // by dequeueBuffer.  It must be called again if dequeueBuffer returns
     39     // flags indicating that previously-returned buffers are no longer valid.
     40     virtual status_t requestBuffer(int slot, sp<GraphicBuffer>* buf);
     41 
     42     // see IGraphicsBufferProducer::setMaxDequeuedBufferCount
     43     virtual status_t setMaxDequeuedBufferCount(int maxDequeuedBuffers);
     44 
     45     // see IGraphicsBufferProducer::setAsyncMode
     46     virtual status_t setAsyncMode(bool async);
     47 
     48     // dequeueBuffer gets the next buffer slot index for the producer to use.
     49     // If a buffer slot is available then that slot index is written to the
     50     // location pointed to by the buf argument and a status of OK is returned.
     51     // If no slot is available then a status of -EBUSY is returned and buf is
     52     // unmodified.
     53     //
     54     // The outFence parameter will be updated to hold the fence associated with
     55     // the buffer. The contents of the buffer must not be overwritten until the
     56     // fence signals. If the fence is Fence::NO_FENCE, the buffer may be
     57     // written immediately.
     58     //
     59     // The width and height parameters must be no greater than the minimum of
     60     // GL_MAX_VIEWPORT_DIMS and GL_MAX_TEXTURE_SIZE (see: glGetIntegerv).
     61     // An error due to invalid dimensions might not be reported until
     62     // updateTexImage() is called.  If width and height are both zero, the
     63     // default values specified by setDefaultBufferSize() are used instead.
     64     //
     65     // If the format is 0, the default format will be used.
     66     //
     67     // The usage argument specifies gralloc buffer usage flags.  The values
     68     // are enumerated in gralloc.h, e.g. GRALLOC_USAGE_HW_RENDER.  These
     69     // will be merged with the usage flags specified by setConsumerUsageBits.
     70     //
     71     // The return value may be a negative error value or a non-negative
     72     // collection of flags.  If the flags are set, the return values are
     73     // valid, but additional actions must be performed.
     74     //
     75     // If IGraphicBufferProducer::BUFFER_NEEDS_REALLOCATION is set, the
     76     // producer must discard cached GraphicBuffer references for the slot
     77     // returned in buf.
     78     // If IGraphicBufferProducer::RELEASE_ALL_BUFFERS is set, the producer
     79     // must discard cached GraphicBuffer references for all slots.
     80     //
     81     // In both cases, the producer will need to call requestBuffer to get a
     82     // GraphicBuffer handle for the returned slot.
     83     virtual status_t dequeueBuffer(int* outSlot, sp<Fence>* outFence, uint32_t width,
     84                                    uint32_t height, PixelFormat format, uint64_t usage,
     85                                    uint64_t* outBufferAge,
     86                                    FrameEventHistoryDelta* outTimestamps) override;
     87 
     88     // See IGraphicBufferProducer::detachBuffer
     89     virtual status_t detachBuffer(int slot);
     90 
     91     // See IGraphicBufferProducer::detachNextBuffer
     92     virtual status_t detachNextBuffer(sp<GraphicBuffer>* outBuffer,
     93             sp<Fence>* outFence);
     94 
     95     // See IGraphicBufferProducer::attachBuffer
     96     virtual status_t attachBuffer(int* outSlot, const sp<GraphicBuffer>& buffer);
     97 
     98     // queueBuffer returns a filled buffer to the BufferQueue.
     99     //
    100     // Additional data is provided in the QueueBufferInput struct.  Notably,
    101     // a timestamp must be provided for the buffer. The timestamp is in
    102     // nanoseconds, and must be monotonically increasing. Its other semantics
    103     // (zero point, etc) are producer-specific and should be documented by the
    104     // producer.
    105     //
    106     // The caller may provide a fence that signals when all rendering
    107     // operations have completed.  Alternatively, NO_FENCE may be used,
    108     // indicating that the buffer is ready immediately.
    109     //
    110     // Some values are returned in the output struct: the current settings
    111     // for default width and height, the current transform hint, and the
    112     // number of queued buffers.
    113     virtual status_t queueBuffer(int slot,
    114             const QueueBufferInput& input, QueueBufferOutput* output);
    115 
    116     // cancelBuffer returns a dequeued buffer to the BufferQueue, but doesn't
    117     // queue it for use by the consumer.
    118     //
    119     // The buffer will not be overwritten until the fence signals.  The fence
    120     // will usually be the one obtained from dequeueBuffer.
    121     virtual status_t cancelBuffer(int slot, const sp<Fence>& fence);
    122 
    123     // Query native window attributes.  The "what" values are enumerated in
    124     // window.h (e.g. NATIVE_WINDOW_FORMAT).
    125     virtual int query(int what, int* outValue);
    126 
    127     // connect attempts to connect a producer API to the BufferQueue.  This
    128     // must be called before any other IGraphicBufferProducer methods are
    129     // called except for getAllocator.  A consumer must already be connected.
    130     //
    131     // This method will fail if connect was previously called on the
    132     // BufferQueue and no corresponding disconnect call was made (i.e. if
    133     // it's still connected to a producer).
    134     //
    135     // APIs are enumerated in window.h (e.g. NATIVE_WINDOW_API_CPU).
    136     virtual status_t connect(const sp<IProducerListener>& listener,
    137             int api, bool producerControlledByApp, QueueBufferOutput* output);
    138 
    139     // See IGraphicBufferProducer::disconnect
    140     virtual status_t disconnect(int api, DisconnectMode mode = DisconnectMode::Api);
    141 
    142     // Attaches a sideband buffer stream to the IGraphicBufferProducer.
    143     //
    144     // A sideband stream is a device-specific mechanism for passing buffers
    145     // from the producer to the consumer without using dequeueBuffer/
    146     // queueBuffer. If a sideband stream is present, the consumer can choose
    147     // whether to acquire buffers from the sideband stream or from the queued
    148     // buffers.
    149     //
    150     // Passing NULL or a different stream handle will detach the previous
    151     // handle if any.
    152     virtual status_t setSidebandStream(const sp<NativeHandle>& stream);
    153 
    154     // See IGraphicBufferProducer::allocateBuffers
    155     virtual void allocateBuffers(uint32_t width, uint32_t height,
    156             PixelFormat format, uint64_t usage) override;
    157 
    158     // See IGraphicBufferProducer::allowAllocation
    159     virtual status_t allowAllocation(bool allow);
    160 
    161     // See IGraphicBufferProducer::setGenerationNumber
    162     virtual status_t setGenerationNumber(uint32_t generationNumber);
    163 
    164     // See IGraphicBufferProducer::getConsumerName
    165     virtual String8 getConsumerName() const override;
    166 
    167     // See IGraphicBufferProducer::setSharedBufferMode
    168     virtual status_t setSharedBufferMode(bool sharedBufferMode) override;
    169 
    170     // See IGraphicBufferProducer::setAutoRefresh
    171     virtual status_t setAutoRefresh(bool autoRefresh) override;
    172 
    173     // See IGraphicBufferProducer::setDequeueTimeout
    174     virtual status_t setDequeueTimeout(nsecs_t timeout) override;
    175 
    176     // See IGraphicBufferProducer::getLastQueuedBuffer
    177     virtual status_t getLastQueuedBuffer(sp<GraphicBuffer>* outBuffer,
    178             sp<Fence>* outFence, float outTransformMatrix[16]) override;
    179 
    180     // See IGraphicBufferProducer::getFrameTimestamps
    181     virtual void getFrameTimestamps(FrameEventHistoryDelta* outDelta) override;
    182 
    183     // See IGraphicBufferProducer::getUniqueId
    184     virtual status_t getUniqueId(uint64_t* outId) const override;
    185 
    186     // See IGraphicBufferProducer::getConsumerUsage
    187     virtual status_t getConsumerUsage(uint64_t* outUsage) const override;
    188 
    189 private:
    190     // This is required by the IBinder::DeathRecipient interface
    191     virtual void binderDied(const wp<IBinder>& who);
    192 
    193     // Returns the slot of the next free buffer if one is available or
    194     // BufferQueueCore::INVALID_BUFFER_SLOT otherwise
    195     int getFreeBufferLocked() const;
    196 
    197     // Returns the next free slot if one is available or
    198     // BufferQueueCore::INVALID_BUFFER_SLOT otherwise
    199     int getFreeSlotLocked() const;
    200 
    201     void addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
    202             FrameEventHistoryDelta* outDelta);
    203 
    204     // waitForFreeSlotThenRelock finds the oldest slot in the FREE state. It may
    205     // block if there are no available slots and we are not in non-blocking
    206     // mode (producer and consumer controlled by the application). If it blocks,
    207     // it will release mCore->mMutex while blocked so that other operations on
    208     // the BufferQueue may succeed.
    209     enum class FreeSlotCaller {
    210         Dequeue,
    211         Attach,
    212     };
    213     status_t waitForFreeSlotThenRelock(FreeSlotCaller caller, int* found) const;
    214 
    215     sp<BufferQueueCore> mCore;
    216 
    217     // This references mCore->mSlots. Lock mCore->mMutex while accessing.
    218     BufferQueueDefs::SlotsType& mSlots;
    219 
    220     // This is a cached copy of the name stored in the BufferQueueCore.
    221     // It's updated during connect and dequeueBuffer (which should catch
    222     // most updates).
    223     String8 mConsumerName;
    224 
    225     uint32_t mStickyTransform;
    226 
    227     // This controls whether the GraphicBuffer pointer in the BufferItem is
    228     // cleared after being queued
    229     bool mConsumerIsSurfaceFlinger;
    230 
    231     // This saves the fence from the last queueBuffer, such that the
    232     // next queueBuffer call can throttle buffer production. The prior
    233     // queueBuffer's fence is not nessessarily available elsewhere,
    234     // since the previous buffer might have already been acquired.
    235     sp<Fence> mLastQueueBufferFence;
    236 
    237     Rect mLastQueuedCrop;
    238     uint32_t mLastQueuedTransform;
    239 
    240     // Take-a-ticket system for ensuring that onFrame* callbacks are called in
    241     // the order that frames are queued. While the BufferQueue lock
    242     // (mCore->mMutex) is held, a ticket is retained by the producer. After
    243     // dropping the BufferQueue lock, the producer must wait on the condition
    244     // variable until the current callback ticket matches its retained ticket.
    245     Mutex mCallbackMutex;
    246     int mNextCallbackTicket; // Protected by mCore->mMutex
    247     int mCurrentCallbackTicket; // Protected by mCallbackMutex
    248     Condition mCallbackCondition;
    249 
    250     // Sets how long dequeueBuffer or attachBuffer will block if a buffer or
    251     // slot is not yet available.
    252     nsecs_t mDequeueTimeout;
    253 
    254 }; // class BufferQueueProducer
    255 
    256 } // namespace android
    257 
    258 #endif
    259