From 08511a88f70aee78d3ac9cb1d98a0803ff80befa Mon Sep 17 00:00:00 2001 From: Anton Ivanov Date: Mon, 14 Apr 2025 15:39:30 -0700 Subject: Revert "Delay initialization of a ConsumerBase instance to construction of a sp/wp." This reverts commit 4efd0d936e2a2bfd9a46432270d7960062265c7b. Reason for revert: Caused b/409264080 Bug: 393217449 (cherry picked from https://googleplex-android-review.googlesource.com/q/commit:39266357b892a5b1f1d0a232d9706e512615b08d) Merged-In: Ia41cc9c08ce48d67f7b06b50adf6c573e4325482 Change-Id: Ia41cc9c08ce48d67f7b06b50adf6c573e4325482 --- libs/gui/ConsumerBase.cpp | 32 ++++++++++++++------------------ libs/gui/include/gui/ConsumerBase.h | 9 +-------- 2 files changed, 15 insertions(+), 26 deletions(-) diff --git a/libs/gui/ConsumerBase.cpp b/libs/gui/ConsumerBase.cpp index 5b89c6e17e..1a975de08d 100644 --- a/libs/gui/ConsumerBase.cpp +++ b/libs/gui/ConsumerBase.cpp @@ -31,7 +31,6 @@ #include #include #include -#include #include #include #include @@ -69,8 +68,8 @@ ConsumerBase::ConsumerBase(const sp& bufferQueue, bool c #endif mAbandoned(false), mConsumer(bufferQueue), - mPrevFinalReleaseFence(Fence::NO_FENCE), - mIsControlledByApp(controlledByApp) { + mPrevFinalReleaseFence(Fence::NO_FENCE) { + initialize(controlledByApp); } #if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) @@ -80,11 +79,11 @@ ConsumerBase::ConsumerBase(bool controlledByApp, bool consumerIsSurfaceFlinger) mSlots(BufferQueueDefs::NUM_BUFFER_SLOTS), #endif mAbandoned(false), - mPrevFinalReleaseFence(Fence::NO_FENCE), - mIsControlledByApp(controlledByApp) { + mPrevFinalReleaseFence(Fence::NO_FENCE) { sp producer; BufferQueue::createBufferQueue(&producer, &mConsumer, consumerIsSurfaceFlinger); mSurface = sp::make(producer, controlledByApp); + initialize(controlledByApp); } ConsumerBase::ConsumerBase(const sp& producer, @@ -96,27 +95,24 @@ ConsumerBase::ConsumerBase(const sp& producer, mAbandoned(false), mConsumer(consumer), mSurface(sp::make(producer, controlledByApp)), - mPrevFinalReleaseFence(Fence::NO_FENCE), - mIsControlledByApp(controlledByApp) { + mPrevFinalReleaseFence(Fence::NO_FENCE) { + initialize(controlledByApp); } #endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ) -void ConsumerBase::onFirstRef() { - ConsumerListener::onFirstRef(); - initialize(); -} - -void ConsumerBase::initialize() { +void ConsumerBase::initialize(bool controlledByApp) { // Choose a name using the PID and a process-unique ID. mName = String8::format("unnamed-%d-%d", getpid(), createProcessUniqueId()); - // Here we depend on an sp/wp having been created for `this`. For this - // reason, initialize() cannot be called from a ctor. - wp listener = wp::fromExisting(this); - sp proxy = sp::make(listener); + // Note that we can't create an sp<...>(this) in a ctor that will not keep a + // reference once the ctor ends, as that would cause the refcount of 'this' + // dropping to 0 at the end of the ctor. Since all we need is a wp<...> + // that's what we create. + wp listener = static_cast(this); + sp proxy = new BufferQueue::ProxyConsumerListener(listener); - status_t err = mConsumer->consumerConnect(proxy, mIsControlledByApp); + status_t err = mConsumer->consumerConnect(proxy, controlledByApp); if (err != NO_ERROR) { CB_LOGE("ConsumerBase: error connecting to BufferQueue: %s (%d)", strerror(-err), err); diff --git a/libs/gui/include/gui/ConsumerBase.h b/libs/gui/include/gui/ConsumerBase.h index d2215ef7e6..63c1ef3132 100644 --- a/libs/gui/include/gui/ConsumerBase.h +++ b/libs/gui/include/gui/ConsumerBase.h @@ -139,8 +139,7 @@ private: ConsumerBase(const ConsumerBase&); void operator=(const ConsumerBase&); - // Requires `this` to be sp/wp so must not be called from ctor. - void initialize(); + void initialize(bool controlledByApp); protected: // ConsumerBase constructs a new ConsumerBase object to consume image @@ -255,10 +254,6 @@ protected: const sp graphicBuffer, EGLDisplay display = EGL_NO_DISPLAY, EGLSyncKHR eglFence = EGL_NO_SYNC_KHR); #endif - // Required to complete initialization, so `final` lest overrides forget to - // delegate. - void onFirstRef() override final; - // returns true iff the slot still has the graphicBuffer in it. bool stillTracking(int slot, const sp graphicBuffer); @@ -334,8 +329,6 @@ protected: // releaseBufferLocked. sp mPrevFinalReleaseFence; - const bool mIsControlledByApp; - // mMutex is the mutex used to prevent concurrent access to the member // variables of ConsumerBase objects. It must be locked whenever the // member variables are accessed or when any of the *Locked methods are -- cgit v1.2.3-59-g8ed1b