Flowchart.jpg

ConsumerListener 是 BufferQueue 给消费者回调事件的接口,定义在 IConsumerListener.h。这个回调是在 BufferQueueProducer 中调用的。

// ConsumerListener is the interface through which the BufferQueue notifies the consumer of events
// that the consumer may wish to react to. Because the consumer will generally have a mutex that is
// locked during calls from the consumer to the BufferQueue, these calls from the BufferQueue to the
// consumer *MUST* be called only when the BufferQueue mutex is NOT locked.
class ConsumerListener : public virtual RefBase {
    virtual void onDisconnect() {} /* Asynchronous */
    virtual void onFrameDequeued(const uint64_t) {}
    virtual void onFrameCancelled(const uint64_t) {}
    virtual void onFrameDetached(const uint64_t) {}
    virtual void onFrameAvailable(const BufferItem& item) = 0; /* Asynchronous */
    virtual void onFrameReplaced(const BufferItem& /* item */) {} /* Asynchronous */
    virtual void onBuffersReleased() = 0; /* Asynchronous */
    virtual void onSidebandStreamChanged() = 0; /* Asynchronous */
    virtual void addAndGetFrameTimestamps(const NewFrameEventsEntry* /*newTimestamps*/,
                                          FrameEventHistoryDelta* /*outDelta*/) {}
};

IConsumerListener 继承 ConsumerListener 和 IInterface。

class IConsumerListener : public ConsumerListener, public IInterface {
public:
    DECLARE_META_INTERFACE(ConsumerListener)
};

class BnConsumerListener : public SafeBnInterface<IConsumerListener> {
public:
    BnConsumerListener() : SafeBnInterface<IConsumerListener>("BnConsumerListener") {}

    status_t onTransact(uint32_t code, const Parcel& data, Parcel* reply,
                        uint32_t flags = 0) override;
};

BufferQueue 中定义了 ProxyConsumerListener,它继承于 BnConsumerListener,有成员 ConsumerListener 弱引用。ProxyConsumerListener 的实现就是转发给 mConsumerListener

// ProxyConsumerListener is a ConsumerListener implementation that keeps a weak
// reference to the actual consumer object.  It forwards all calls to that
// consumer object so long as it exists.
//
// This class exists to avoid having a circular reference between the
// BufferQueue object and the consumer object.  The reason this can't be a weak
// reference in the BufferQueue class is because we're planning to expose the
// consumer side of a BufferQueue as a binder interface, which doesn't support
// weak references.
class ProxyConsumerListener : public BnConsumerListener {
public:
    explicit ProxyConsumerListener(const wp<ConsumerListener>& consumerListener);
    ~ProxyConsumerListener() override;
    void onDisconnect() override;
    void onFrameAvailable(const BufferItem& item) override;
    void onFrameReplaced(const BufferItem& item) override;
    void onBuffersReleased() override;
    void onSidebandStreamChanged() override;
    void onFrameDequeued(const uint64_t bufferId) override;
    void onFrameCancelled(const uint64_t bufferId) override;
    void onFrameDetached(const uint64_t bufferID) override;
    void addAndGetFrameTimestamps(
            const NewFrameEventsEntry* newTimestamps,
            FrameEventHistoryDelta* outDelta) override;
private:
    // mConsumerListener is a weak reference to the IConsumerListener.  This is
    // the raison d'etre of ProxyConsumerListener.
    wp<ConsumerListener> mConsumerListener;
};