3131#include < gui/BufferItem.h>
3232#include < gui/BufferQueue.h>
3333#include < gui/ConsumerBase.h>
34+ #include < gui/IConsumerListener.h>
3435#include < gui/ISurfaceComposer.h>
3536#include < gui/Surface.h>
3637#include < gui/SurfaceComposerClient.h>
@@ -68,8 +69,8 @@ ConsumerBase::ConsumerBase(const sp<IGraphicBufferConsumer>& bufferQueue, bool c
6869#endif
6970 mAbandoned (false ),
7071 mConsumer (bufferQueue),
71- mPrevFinalReleaseFence (Fence::NO_FENCE) {
72- initialize (controlledByApp);
72+ mPrevFinalReleaseFence (Fence::NO_FENCE),
73+ mIsControlledByApp (controlledByApp) {
7374}
7475
7576#if COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
@@ -79,11 +80,11 @@ ConsumerBase::ConsumerBase(bool controlledByApp, bool consumerIsSurfaceFlinger)
7980 mSlots (BufferQueueDefs::NUM_BUFFER_SLOTS),
8081#endif
8182 mAbandoned (false ),
82- mPrevFinalReleaseFence(Fence::NO_FENCE) {
83+ mPrevFinalReleaseFence(Fence::NO_FENCE),
84+ mIsControlledByApp(controlledByApp) {
8385 sp<IGraphicBufferProducer> producer;
8486 BufferQueue::createBufferQueue (&producer, &mConsumer , consumerIsSurfaceFlinger);
8587 mSurface = sp<Surface>::make (producer, controlledByApp);
86- initialize (controlledByApp);
8788}
8889
8990ConsumerBase::ConsumerBase (const sp<IGraphicBufferProducer>& producer,
@@ -95,24 +96,27 @@ ConsumerBase::ConsumerBase(const sp<IGraphicBufferProducer>& producer,
9596 mAbandoned (false ),
9697 mConsumer(consumer),
9798 mSurface(sp<Surface>::make(producer, controlledByApp)),
98- mPrevFinalReleaseFence(Fence::NO_FENCE) {
99- initialize (controlledByApp);
99+ mPrevFinalReleaseFence(Fence::NO_FENCE),
100+ mIsControlledByApp (controlledByApp) {
100101}
101102
102103#endif // COM_ANDROID_GRAPHICS_LIBGUI_FLAGS(WB_CONSUMER_BASE_OWNS_BQ)
103104
104- void ConsumerBase::initialize (bool controlledByApp) {
105+ void ConsumerBase::onFirstRef () {
106+ ConsumerListener::onFirstRef ();
107+ initialize ();
108+ }
109+
110+ void ConsumerBase::initialize () {
105111 // Choose a name using the PID and a process-unique ID.
106112 mName = String8::format (" unnamed-%d-%d" , getpid (), createProcessUniqueId ());
107113
108- // Note that we can't create an sp<...>(this) in a ctor that will not keep a
109- // reference once the ctor ends, as that would cause the refcount of 'this'
110- // dropping to 0 at the end of the ctor. Since all we need is a wp<...>
111- // that's what we create.
112- wp<ConsumerListener> listener = static_cast <ConsumerListener*>(this );
113- sp<IConsumerListener> proxy = new BufferQueue::ProxyConsumerListener (listener);
114+ // Here we depend on an sp/wp having been created for `this`. For this
115+ // reason, initialize() cannot be called from a ctor.
116+ wp<ConsumerListener> listener = wp<ConsumerListener>::fromExisting (this );
117+ sp<IConsumerListener> proxy = sp<BufferQueue::ProxyConsumerListener>::make (listener);
114118
115- status_t err = mConsumer ->consumerConnect (proxy, controlledByApp );
119+ status_t err = mConsumer ->consumerConnect (proxy, mIsControlledByApp );
116120 if (err != NO_ERROR) {
117121 CB_LOGE (" ConsumerBase: error connecting to BufferQueue: %s (%d)" ,
118122 strerror (-err), err);
0 commit comments