SurfaceFlinger学习

SurfaceFlinger学习

参考资料:

https://blog.csdn.net/hexiaolong2009/article/details/99225637

BufferState

BufferState tracks the states in which a buffer slot can be

    uint32_t mDequeueCount;
    uint32_t mQueueCount;
    uint32_t mAcquireCount;
    bool mShared;
    // A buffer can be in one of five states, represented as below:
    //
    //         | mShared | mDequeueCount | mQueueCount | mAcquireCount |
    // --------|---------|---------------|-------------|---------------|
    // FREE    |  false  |       0       |      0      |       0       |
    // DEQUEUED|  false  |       1       |      0      |       0       |
    // QUEUED  |  false  |       0       |      1      |       0       |
    // ACQUIRED|  false  |       0       |      0      |       1       |
    // SHARED  |  true   |      any      |     any     |      any      |

FREE--->DEQUEUED--->QUEUED--->ACQUIRED--->FREE

    // FREE indicates that the buffer is available to be dequeued by the
    // producer. The slot is "owned" by BufferQueue. It transitions to DEQUEUED
    // when dequeueBuffer is called.
    //
    // DEQUEUED indicates that the buffer has been dequeued by the producer, but
    // has not yet been queued or canceled. The producer may modify the
    // buffer's contents as soon as the associated release fence is signaled.
    // The slot is "owned" by the producer. It can transition to QUEUED (via
    // queueBuffer or attachBuffer) or back to FREE (via cancelBuffer or
    // detachBuffer).
    //
    // QUEUED indicates that the buffer has been filled by the producer and
    // queued for use by the consumer. The buffer contents may continue to be
    // modified for a finite time, so the contents must not be accessed until
    // the associated fence is signaled. The slot is "owned" by BufferQueue. It
    // can transition to ACQUIRED (via acquireBuffer) or to FREE (if another
    // buffer is queued in asynchronous mode).
    //
    // ACQUIRED indicates that the buffer has been acquired by the consumer. As
    // with QUEUED, the contents must not be accessed by the consumer until the
    // acquire fence is signaled. The slot is "owned" by the consumer. It
    // transitions to FREE when releaseBuffer (or detachBuffer) is called. A
    // detached buffer can also enter the ACQUIRED state via attachBuffer.
    //
    // SHARED indicates that this buffer is being used in shared buffer
    // mode. It can be in any combination of the other states at the same time,
    // except for FREE (since that excludes being in any other state). It can
    // also be dequeued, queued, or acquired multiple times.

BufferSlot

struct BufferSlot {
        sp mGraphicBuffer;
        BufferState mBufferState;
        bool mRequestBufferCalled;
coreduffer.jpg

mSlots = mFreeSlots + mFreeBuffers + mActiveBuffers + mUnusedSlots

BufferQueueCore

BufferQueueCore 负责维护 BufferQueue 的基本数据结构,而 BufferQueueProducer 和 BufferQueueConsumer 则负责提供操作 BufferQueue 的基本接口

#include 
#include 
#include 
class IConsumerListener;
class IProducerListener;

class BufferQueueCore : public virtual RefBase {

    friend class BufferQueueProducer;
    friend class BufferQueueConsumer;

    typedef Vector Fifo;//数组链表
    /*BufferItem的部分成员变量
    sp mGraphicBuffer;
    int mSlot;
    */
    bool mIsAbandoned; 
    bool mIsAllocating;
    
    mutable std::condition_variable mDequeueCondition;
    mutable std::condition_variable mIsAllocatingCondition;
    // mConsumerListener is used to notify the connected consumer of
    // asynchronous events that it may wish to react to. It is initially
    // set to NULL and is written by consumerConnect and consumerDisconnect.
    sp mConsumerListener;

    // mLinkedToDeath is used to set a binder death notification on
    // the producer.
    sp mLinkedToDeath;

    // mConnectedProducerListener is used to handle the onBufferReleased
    // and onBuffersDiscarded notification.
    sp mConnectedProducerListener;

    BufferQueueDefs::SlotsType mSlots;

    // mQueue is a FIFO of queued buffers used in synchronous mode.
    Fifo mQueue;
    // mFreeSlots contains all of the slots which are FREE and do not currently
    // have a buffer attached.
    std::set mFreeSlots;

    // mFreeBuffers contains all of the slots which are FREE and currently have
    // a buffer attached.
    std::list mFreeBuffers;

    // mUnusedSlots contains all slots that are currently unused. They should be
    // free and not have a buffer attached.
    std::list mUnusedSlots;

    // mActiveBuffers contains all slots which have a non-FREE buffer attached.
    std::set mActiveBuffers;

    // mDequeueCondition is a condition variable used for dequeueBuffer in
    // synchronous mode.
    mutable std::condition_variable mDequeueCondition;

ComposerService

class ComposerService : public Singleton
{
    sp mComposerService;
    sp mDeathObserver;
    Mutex mLock;

    ComposerService();
    void connectLocked();
    void composerServiceDied();
    friend class Singleton;
public:

    // Get a connection to the Composer Service.  This will block until
    // a connection is established.
    static sp getComposerService();
};

ConsumerBase

class ConsumerBase : public virtual RefBase,
        protected ConsumerListener {
public:
    struct FrameAvailableListener : public virtual RefBase {
        // See IConsumerListener::onFrame{Available,Replaced}
        virtual void onFrameAvailable(const BufferItem& item) = 0;
        virtual void onFrameReplaced(const BufferItem& /* item */) {}
        virtual void onFrameDequeued(const uint64_t){};
        virtual void onFrameCancelled(const uint64_t){};
        virtual void onFrameDetached(const uint64_t){};
    };
            
    wp mFrameAvailableListener;

    // The ConsumerBase has-a BufferQueue and is responsible for creating this object
    // if none is supplied
    sp mConsumer;

Surface

class Surface
    : public ANativeObjectBase

ISurfaceComposerClient

ISurfaceComposerClient,自定义接口(继承于IInterface),BnSurfaceComposerClient是Binder本地对象,BpSurfaceComposerClient是Binder代理对象,在cpp中定义。

class ISurfaceComposerClient : public IInterface {
public:
    DECLARE_META_INTERFACE(SurfaceComposerClient)//定义
        
  
class BnSurfaceComposerClient : public SafeBnInterface {
    public:
    BnSurfaceComposerClient()
          : SafeBnInterface("BnSurfaceComposerClient") {}

    status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags) override;
};
//cpp中的内部类
class BpSurfaceComposerClient : public SafeBpInterface {   
    
    IMPLEMENT_META_INTERFACE(SurfaceComposerClient, "android.ui.ISurfaceComposerClient");//实现

SurfaceComposerClient

class SurfaceComposerClient : public RefBase
    private:
    virtual void onFirstRef();

static sp getDefault();

    mutable     Mutex                       mLock;
                status_t                    mStatus;
                sp  mClient;
    //! Create a surface
    sp createSurface(const String8& name,              // name of the surface
                                     uint32_t w,                       // width in pixel
                                     uint32_t h,                       // height in pixel
                                     PixelFormat format,               // pixel-format desired
                                     uint32_t flags = 0,               // usage flags
                                     SurfaceControl* parent = nullptr, // parent
                                     LayerMetadata metadata = LayerMetadata(), // metadata
                                     uint32_t* outTransformHint = nullptr);

b4ef45bfc3abcc050cbc73085239fc2.jpg

https://www.jianshu.com/p/8e7a9a0b5726

ISurfaceComposer

ISurfaceComposer自定义接口,BnSurfaceComposer是Binder本地对象,BpSurfaceComposer是Binder代理对象

class ISurfaceComposer: public IInterface {

public:
    DECLARE_META_INTERFACE(SurfaceComposer)
    
class BnSurfaceComposer: public BnInterface
        virtual status_t onTransact(uint32_t code, const Parcel& data,
            Parcel* reply, uint32_t flags = 0);

//cpp内部类
class BpSurfaceComposer : public BpInterface
    
    IMPLEMENT_META_INTERFACE(SurfaceComposer, "android.ui.ISurfaceComposer");
    
        virtual sp createConnection()
    {
        Parcel data, reply;//data是发送的数据,reply是接受的数据
        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());//android.ui.ISurfaceComposer
        remote()->transact(BnSurfaceComposer::CREATE_CONNECTION, data, &reply);
            //remote()得到的是一个BpBinder代理对象,与BBinder本地对象进程间通信
        return interface_cast(reply.readStrongBinder());
            //得到一个BpBinder代理对象,以其为参数,转换为ISurfaceComposerClient类型的代理对象
    }
    virtual status_t captureScreen(const sp& display, sp* outBuffer,
                                   bool& outCapturedSecureLayers, ui::Dataspace reqDataspace,
                                   ui::PixelFormat reqPixelFormat, const Rect& sourceCrop,
                                   uint32_t reqWidth, uint32_t reqHeight, bool useIdentityTransform,
                                   ui::Rotation rotation, bool captureSecureLayers) {
        Parcel data, reply;
        data.writeInterfaceToken(ISurfaceComposer::getInterfaceDescriptor());
        data.writeStrongBinder(display);
        data.writeInt32(static_cast(reqDataspace));
        data.writeInt32(static_cast(reqPixelFormat));
        data.write(sourceCrop);
        data.writeUint32(reqWidth);
        data.writeUint32(reqHeight);
        data.writeInt32(static_cast(useIdentityTransform));
        data.writeInt32(static_cast(rotation));
        data.writeInt32(static_cast(captureSecureLayers));
        status_t result = remote()->transact(BnSurfaceComposer::CAPTURE_SCREEN, data, &reply);
        if (result != NO_ERROR) {
            ALOGE("captureScreen failed to transact: %d", result);
            return result;
        }
        result = reply.readInt32();
        if (result != NO_ERROR) {
            ALOGE("captureScreen failed to readInt32: %d", result);
            return result;
        }

        *outBuffer = new GraphicBuffer();
        reply.read(**outBuffer);
        outCapturedSecureLayers = reply.readBool();

        return result;
    }

SurfaceControl

// ---------------------------------------------------------------------------

class IGraphicBufferProducer;
class Surface;
class SurfaceComposerClient;

// ---------------------------------------------------------------------------

class SurfaceControl : public RefBase
    
        SurfaceControl(const sp& client, const sp& handle,
                   const sp& gbp, uint32_t transformHint = 0);
    friend class SurfaceComposerClient;
    friend class Surface;

    sp   mClient;//消费者
    sp                 mHandle;
    sp  mGraphicBufferProducer;//生产者
    mutable Mutex               mLock;
    mutable sp         mSurfaceData;
    uint32_t mTransformHint;

两个函数实例

status_t SurfaceControl::writeSurfaceToParcel(
        const sp& control, Parcel* parcel)
{
    sp bp;
    if (control != nullptr) {
        bp = control->mGraphicBufferProducer;
    }
    return parcel->writeStrongBinder(IInterface::asBinder(bp));
}

mClient->getClient()得到的是ISurfaceComposerClient(注意类数据成员)

void SurfaceControl::writeToParcel(Parcel* parcel)
{
    parcel->writeStrongBinder(ISurfaceComposerClient::asBinder(mClient->getClient()));
    parcel->writeStrongBinder(mHandle);
    parcel->writeStrongBinder(IGraphicBufferProducer::asBinder(mGraphicBufferProducer));
    parcel->writeUint32(mTransformHint);
}

ProducerListener

class ProducerListener : public virtual RefBase//

class IProducerListener : public ProducerListener, public IInterface


class BnProducerListener : public BnInterface
    //cpp内部类
    class BpProducerListener : public BpInterface
        
        
        IMPLEMENT_HYBRID_META_INTERFACE(ProducerListener,
        "android.gui.IProducerListener")

ConsumerListener

class ConsumerListener : public virtual RefBase {
    
class IConsumerListener : public ConsumerListener, public IInterface {
DECLARE_META_INTERFACE(ConsumerListener)
class BnConsumerListener : public SafeBnInterface {
    
        status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply,
                        uint32_t flags = 0) override;
//cpp内部类
    class BpConsumerListener : public SafeBpInterface {
        
        IMPLEMENT_META_INTERFACE(ConsumerListener, "android.gui.IConsumerListener");

通知事件类型:

enum class Tag : uint32_t {
    ON_DISCONNECT = IBinder::FIRST_CALL_TRANSACTION,
    ON_FRAME_AVAILABLE,
    ON_FRAME_REPLACED,
    ON_BUFFERS_RELEASED,
    ON_SIDEBAND_STREAM_CHANGED,
    ON_FRAME_DEQUEUED,
    ON_FRAME_CANCELLED,
    ON_FRAME_DETACHED,
    LAST = ON_FRAME_DETACHED,
};

IGraphicBufferProducer

class IGraphicBufferProducer : public IInterface {
    DECLARE_HYBRID_META_INTERFACE(GraphicBufferProducer,
    
 struct QueueBufferInput : public Flattenable {  
 struct QueueBufferOutput : public Flattenable {
                                  
class BnGraphicBufferProducer : public BnInterface
//cpp内部类
class BpGraphicBufferProducer : public BpInterface
virtual status_t requestBuffer(int slot, sp* buf) = 0;

virtual status_t dequeueBuffer(int* slot, sp* fence, uint32_t w, uint32_t h,
                                   PixelFormat format, uint64_t usage, uint64_t* outBufferAge,
                                   FrameEventHistoryDelta* outTimestamps) = 0;

virtual status_t detachBuffer(int slot) = 0;

virtual status_t attachBuffer(int* outSlot,
            const sp& buffer) = 0;

virtual status_t queueBuffer(int slot, const QueueBufferInput& input,
            QueueBufferOutput* output) = 0;

IGraphicBufferConsumer

class IGraphicBufferConsumer : public IInterface {
    public:
    DECLARE_META_INTERFACE(GraphicBufferConsumer)
class BnGraphicBufferConsumer : public SafeBnInterface {
//cpp内部类
    class BpGraphicBufferConsumer : public SafeBpInterface {
        using Signature = decltype(&IGraphicBufferConsumer::consumerConnect);//?
        IMPLEMENT_META_INTERFACE(GraphicBufferConsumer, "android.gui.IGraphicBufferConsumer");
    virtual status_t acquireBuffer(BufferItem* buffer, nsecs_t presentWhen,
                                   uint64_t maxFrameNumber = 0) = 0;

    virtual status_t detachBuffer(int slot) = 0;

BufferQueueProducer

Binder本地对象,IGraphicBufferProducer接口的实现,还注册了DeathRecipient,服务端死亡回调通知

class BufferQueueProducer : public BnGraphicBufferProducer,
                            private IBinder::DeathRecipient {
                                

    sp mCore;

    // This references mCore->mSlots. Lock mCore->mMutex while accessing.
    BufferQueueDefs::SlotsType& mSlots;

//const sp& buffer(mSlots[found].mGraphicBuffer);

status_t BufferQueueProducer::requestBuffer(int slot, sp* buf) {
    ATRACE_CALL();
    BQ_LOGV("requestBuffer: slot %d", slot);
    std::lock_guard lock(mCore->mMutex);

    if (mCore->mIsAbandoned) {
        BQ_LOGE("requestBuffer: BufferQueue has been abandoned");
        return NO_INIT;
    }

    if (mCore->mConnectedApi == BufferQueueCore::NO_CONNECTED_API) {
        BQ_LOGE("requestBuffer: BufferQueue has no connected producer");
        return NO_INIT;
    }

    if (slot < 0 || slot >= BufferQueueDefs::NUM_BUFFER_SLOTS) {
        BQ_LOGE("requestBuffer: slot index %d out of range [0, %d)",
                slot, BufferQueueDefs::NUM_BUFFER_SLOTS);
        return BAD_VALUE;
    } else if (!mSlots[slot].mBufferState.isDequeued()) {
        BQ_LOGE("requestBuffer: slot %d is not owned by the producer "
                "(state = %s)", slot, mSlots[slot].mBufferState.string());
        return BAD_VALUE;
    }

    mSlots[slot].mRequestBufferCalled = true;
    *buf = mSlots[slot].mGraphicBuffer;
    return NO_ERROR;
}
virtual status_t requestBuffer(int slot, sp* buf);

virtual status_t dequeueBuffer(int* outSlot, sp* outFence, uint32_t width,
                                   uint32_t height, PixelFormat format, uint64_t usage,
                                   uint64_t* outBufferAge,
                                   FrameEventHistoryDelta* outTimestamps) override;

    // See IGraphicBufferProducer::detachBuffer
    virtual status_t detachBuffer(int slot);

    // See IGraphicBufferProducer::attachBuffer
    virtual status_t attachBuffer(int* outSlot, const sp& buffer);

    virtual status_t queueBuffer(int slot,
            const QueueBufferInput& input, QueueBufferOutput* output);
    virtual status_t cancelBuffer(int slot, const sp& fence);

    virtual status_t connect(const sp& listener,
            int api, bool producerControlledByApp, QueueBufferOutput* output);

    // See IGraphicBufferProducer::disconnect
    virtual status_t disconnect(int api, DisconnectMode mode = DisconnectMode::Api);

BufferQueueConsumer

class BufferQueueConsumer : public BnGraphicBufferConsumer {
    sp mCore;

    // This references mCore->mSlots. Lock mCore->mMutex while accessing.
    BufferQueueDefs::SlotsType& mSlots;

    // This is a cached copy of the name stored in the BufferQueueCore.
    // It's updated during setConsumerName.
    String8 mConsumerName;

BufferItem

class BufferItem : public Flattenable {
    sp mGraphicBuffer;

    // mFence is a fence that will signal when the buffer is idle.
    sp mFence;
    uint32_t mTransform;

    // mSlot is the slot index of this buffer (default INVALID_BUFFER_SLOT).
    int mSlot;
    
    // Describes the portion of the surface that has been modified since the
    // previous frame
    Region mSurfaceDamage;

BufferQueue::createBufferQueue

https://blog.csdn.net/hexiaolong2009/article/details/99053375

void BufferQueue::createBufferQueue(sp* outProducer,
        sp* outConsumer,
        bool consumerIsSurfaceFlinger) {
    LOG_ALWAYS_FATAL_IF(outProducer == nullptr,
            "BufferQueue: outProducer must not be NULL");
    LOG_ALWAYS_FATAL_IF(outConsumer == nullptr,
            "BufferQueue: outConsumer must not be NULL");

    sp core(new BufferQueueCore());
    LOG_ALWAYS_FATAL_IF(core == nullptr,
            "BufferQueue: failed to create BufferQueueCore");

    sp producer(new BufferQueueProducer(core, consumerIsSurfaceFlinger));
    LOG_ALWAYS_FATAL_IF(producer == nullptr,
            "BufferQueue: failed to create BufferQueueProducer");

    sp consumer(new BufferQueueConsumer(core));
    LOG_ALWAYS_FATAL_IF(consumer == nullptr,
            "BufferQueue: failed to create BufferQueueConsumer");

    *outProducer = producer;
    *outConsumer = consumer;
}
    status_t consumerConnect(const sp& consumer, bool controlledByApp) override {
        using Signature = decltype(&IGraphicBufferConsumer::consumerConnect);
        return callRemote(Tag::CONSUMER_CONNECT, consumer, controlledByApp);
    }
    virtual status_t connect(const sp& listener,
            int api, bool producerControlledByApp, QueueBufferOutput* output) {

你可能感兴趣的:(SurfaceFlinger学习)