Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 27955eec authored by Anton Ivanov's avatar Anton Ivanov
Browse files

Harden construction of BBQ and its helpers.

BLASTBufferQueue and related classes are meant to be refcounted, so ensure they are
constructed with sp<>::make().  Enforce this invariant by making
constructors private.

Bug: 393217449
Test: presubmit
Flag: EXEMPT_refactor
Change-Id: I093ca5f220abcce775c36470a1d304fb03a2206c
parent a3d7a6f1
Loading
Loading
Loading
Loading
+11 −11
Original line number Diff line number Diff line
@@ -197,12 +197,12 @@ BLASTBufferQueue::BLASTBufferQueue(const std::string& name, bool updateDestinati
        mUpdateDestinationFrame(updateDestinationFrame) {
    createBufferQueue(&mProducer, &mConsumer);
#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
    mBufferItemConsumer = new BLASTBufferItemConsumer(mProducer, mConsumer,
    mBufferItemConsumer = sp<BLASTBufferItemConsumer>::make(mProducer, mConsumer,
                                                            GraphicBuffer::USAGE_HW_COMPOSER |
                                                                    GraphicBuffer::USAGE_HW_TEXTURE,
                                                            1, false, this);
#else
    mBufferItemConsumer = new BLASTBufferItemConsumer(mConsumer,
    mBufferItemConsumer = sp<BLASTBufferItemConsumer>::make(mConsumer,
                                                            GraphicBuffer::USAGE_HW_COMPOSER |
                                                                    GraphicBuffer::USAGE_HW_TEXTURE,
                                                            1, false, this);
@@ -1120,10 +1120,10 @@ ANDROID_SINGLETON_STATIC_INSTANCE(AsyncWorker);
class AsyncProducerListener : public BnProducerListener {
private:
    const sp<IProducerListener> mListener;

public:
    AsyncProducerListener(const sp<IProducerListener>& listener) : mListener(listener) {}
    friend class sp<AsyncProducerListener>;

public:
    void onBufferReleased() override {
        AsyncWorker::getInstance().post([listener = mListener]() { listener->onBufferReleased(); });
    }
@@ -1177,7 +1177,7 @@ public:
            return BufferQueueProducer::connect(listener, api, producerControlledByApp, output);
        }

        return BufferQueueProducer::connect(new AsyncProducerListener(listener), api,
        return BufferQueueProducer::connect(sp<AsyncProducerListener>::make(listener), api,
                                            producerControlledByApp, output);
    }

+22 −17
Original line number Diff line number Diff line
@@ -46,21 +46,6 @@ class BufferItemConsumer;

class BLASTBufferItemConsumer : public BufferItemConsumer {
public:
#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
    BLASTBufferItemConsumer(const sp<IGraphicBufferProducer>& producer,
                            const sp<IGraphicBufferConsumer>& consumer, uint64_t consumerUsage,
                            int bufferCount, bool controlledByApp, wp<BLASTBufferQueue> bbq)
          : BufferItemConsumer(producer, consumer, consumerUsage, bufferCount, controlledByApp),
#else
    BLASTBufferItemConsumer(const sp<IGraphicBufferConsumer>& consumer, uint64_t consumerUsage,
                            int bufferCount, bool controlledByApp, wp<BLASTBufferQueue> bbq)
          : BufferItemConsumer(consumer, consumerUsage, bufferCount, controlledByApp),
#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
            mBLASTBufferQueue(std::move(bbq)),
            mCurrentlyConnected(false),
            mPreviouslyConnected(false) {
    }

    void onDisconnect() override EXCLUDES(mMutex);
    void addAndGetFrameTimestamps(const NewFrameEventsEntry* newTimestamps,
                                  FrameEventHistoryDelta* outDelta) override EXCLUDES(mMutex);
@@ -81,6 +66,23 @@ protected:
#endif

private:
#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
    BLASTBufferItemConsumer(const sp<IGraphicBufferProducer>& producer,
                            const sp<IGraphicBufferConsumer>& consumer, uint64_t consumerUsage,
                            int bufferCount, bool controlledByApp, wp<BLASTBufferQueue> bbq)
          : BufferItemConsumer(producer, consumer, consumerUsage, bufferCount, controlledByApp),
#else
    BLASTBufferItemConsumer(const sp<IGraphicBufferConsumer>& consumer, uint64_t consumerUsage,
                            int bufferCount, bool controlledByApp, wp<BLASTBufferQueue> bbq)
          : BufferItemConsumer(consumer, consumerUsage, bufferCount, controlledByApp),
#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
            mBLASTBufferQueue(std::move(bbq)),
            mCurrentlyConnected(false),
            mPreviouslyConnected(false) {
    }

    friend class sp<BLASTBufferItemConsumer>;

    const wp<BLASTBufferQueue> mBLASTBufferQueue;

    uint64_t mCurrentFrameNumber GUARDED_BY(mMutex) = 0;
@@ -94,8 +96,6 @@ private:

class BLASTBufferQueue : public ConsumerBase::FrameAvailableListener {
public:
    BLASTBufferQueue(const std::string& name, bool updateDestinationFrame = true);

    sp<IGraphicBufferProducer> getIGraphicBufferProducer() const {
        return mProducer;
    }
@@ -158,8 +158,13 @@ public:
    void onFirstRef() override;

private:
    // Not public to ensure construction via sp<>::make().
    BLASTBufferQueue(const std::string& name, bool updateDestinationFrame = true);

    friend class sp<BLASTBufferQueue>;
    friend class BLASTBufferQueueHelper;
    friend class BBQBufferQueueProducer;
    friend class TestBLASTBufferQueue;
#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(BUFFER_RELEASE_CHANNEL)
    friend class BBQBufferQueueCore;
#endif
+2 −2
Original line number Diff line number Diff line
@@ -114,7 +114,7 @@ private:
class BLASTBufferQueueHelper {
public:
    BLASTBufferQueueHelper(const sp<SurfaceControl>& sc, int width, int height) {
        mBlastBufferQueueAdapter = new TestBLASTBufferQueue("TestBLASTBufferQueue", sc, width,
        mBlastBufferQueueAdapter = sp<TestBLASTBufferQueue>::make("TestBLASTBufferQueue", sc, width,
                                                                  height, PIXEL_FORMAT_RGBA_8888);
    }