Revert "revert surfaceflinger leak fix as it uncovered a crasher on xoom"

This reverts commit af6edba59e.

Change-Id: I7793d3ca8a4d20a2b188364f47854328ab5f586d
This commit is contained in:
Mathias Agopian
2011-06-15 20:41:24 -07:00
parent d20d72d193
commit 52a4399088
6 changed files with 90 additions and 10 deletions

View File

@ -117,6 +117,20 @@ public:
typedef RefBase basetype; typedef RefBase basetype;
// used to override the RefBase destruction.
class Destroyer {
friend class RefBase;
friend class weakref_type;
public:
virtual ~Destroyer();
private:
virtual void destroy(RefBase const* base) = 0;
};
// Make sure to never acquire a strong reference from this function. The
// same restrictions than for destructors apply.
void setDestroyer(Destroyer* destroyer);
protected: protected:
RefBase(); RefBase();
virtual ~RefBase(); virtual ~RefBase();

View File

@ -49,6 +49,11 @@ namespace android {
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
RefBase::Destroyer::~Destroyer() {
}
// ---------------------------------------------------------------------------
class RefBase::weakref_impl : public RefBase::weakref_type class RefBase::weakref_impl : public RefBase::weakref_type
{ {
public: public:
@ -56,7 +61,7 @@ public:
volatile int32_t mWeak; volatile int32_t mWeak;
RefBase* const mBase; RefBase* const mBase;
volatile int32_t mFlags; volatile int32_t mFlags;
Destroyer* mDestroyer;
#if !DEBUG_REFS #if !DEBUG_REFS
@ -65,6 +70,7 @@ public:
, mWeak(0) , mWeak(0)
, mBase(base) , mBase(base)
, mFlags(0) , mFlags(0)
, mDestroyer(0)
{ {
} }
@ -357,9 +363,13 @@ void RefBase::decStrong(const void* id) const
if (c == 1) { if (c == 1) {
const_cast<RefBase*>(this)->onLastStrongRef(id); const_cast<RefBase*>(this)->onLastStrongRef(id);
if ((refs->mFlags&OBJECT_LIFETIME_WEAK) != OBJECT_LIFETIME_WEAK) { if ((refs->mFlags&OBJECT_LIFETIME_WEAK) != OBJECT_LIFETIME_WEAK) {
if (refs->mDestroyer) {
refs->mDestroyer->destroy(this);
} else {
delete this; delete this;
} }
} }
}
refs->decWeak(id); refs->decWeak(id);
} }
@ -390,7 +400,9 @@ int32_t RefBase::getStrongCount() const
return mRefs->mStrong; return mRefs->mStrong;
} }
void RefBase::setDestroyer(RefBase::Destroyer* destroyer) {
mRefs->mDestroyer = destroyer;
}
RefBase* RefBase::weakref_type::refBase() const RefBase* RefBase::weakref_type::refBase() const
{ {
@ -414,19 +426,31 @@ void RefBase::weakref_type::decWeak(const void* id)
if (c != 1) return; if (c != 1) return;
if ((impl->mFlags&OBJECT_LIFETIME_WEAK) != OBJECT_LIFETIME_WEAK) { if ((impl->mFlags&OBJECT_LIFETIME_WEAK) != OBJECT_LIFETIME_WEAK) {
if (impl->mStrong == INITIAL_STRONG_VALUE) if (impl->mStrong == INITIAL_STRONG_VALUE) {
if (impl->mBase) {
if (impl->mDestroyer) {
impl->mDestroyer->destroy(impl->mBase);
} else {
delete impl->mBase; delete impl->mBase;
else { }
}
} else {
// LOGV("Freeing refs %p of old RefBase %p\n", this, impl->mBase); // LOGV("Freeing refs %p of old RefBase %p\n", this, impl->mBase);
delete impl; delete impl;
} }
} else { } else {
impl->mBase->onLastWeakRef(id); impl->mBase->onLastWeakRef(id);
if ((impl->mFlags&OBJECT_LIFETIME_FOREVER) != OBJECT_LIFETIME_FOREVER) { if ((impl->mFlags&OBJECT_LIFETIME_FOREVER) != OBJECT_LIFETIME_FOREVER) {
if (impl->mBase) {
if (impl->mDestroyer) {
impl->mDestroyer->destroy(impl->mBase);
} else {
delete impl->mBase; delete impl->mBase;
} }
} }
} }
}
}
bool RefBase::weakref_type::attemptIncStrong(const void* id) bool RefBase::weakref_type::attemptIncStrong(const void* id)
{ {
@ -545,10 +569,12 @@ RefBase::RefBase()
RefBase::~RefBase() RefBase::~RefBase()
{ {
if ((mRefs->mFlags & OBJECT_LIFETIME_WEAK) == OBJECT_LIFETIME_WEAK) {
if (mRefs->mWeak == 0) { if (mRefs->mWeak == 0) {
delete mRefs; delete mRefs;
} }
} }
}
void RefBase::extendObjectLifetime(int32_t mode) void RefBase::extendObjectLifetime(int32_t mode)
{ {

View File

@ -61,6 +61,7 @@ Layer::Layer(SurfaceFlinger* flinger,
mBufferManager(mTextureManager), mBufferManager(mTextureManager),
mWidth(0), mHeight(0), mNeedsScaling(false), mFixedSize(false) mWidth(0), mHeight(0), mNeedsScaling(false), mFixedSize(false)
{ {
setDestroyer(this);
} }
Layer::~Layer() Layer::~Layer()
@ -77,6 +78,10 @@ Layer::~Layer()
} }
} }
void Layer::destroy(RefBase const* base) {
mFlinger->destroyLayer(static_cast<LayerBase const*>(base));
}
status_t Layer::setToken(const sp<UserClient>& userClient, status_t Layer::setToken(const sp<UserClient>& userClient,
SharedClient* sharedClient, int32_t token) SharedClient* sharedClient, int32_t token)
{ {

View File

@ -44,7 +44,7 @@ class UserClient;
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
class Layer : public LayerBaseClient class Layer : public LayerBaseClient, private RefBase::Destroyer
{ {
public: public:
Layer(SurfaceFlinger* flinger, DisplayID display, Layer(SurfaceFlinger* flinger, DisplayID display,
@ -92,6 +92,7 @@ public:
return mFreezeLock; } return mFreezeLock; }
protected: protected:
virtual void destroy(RefBase const* base);
virtual void dump(String8& result, char* scratch, size_t size) const; virtual void dump(String8& result, char* scratch, size_t size) const;
private: private:

View File

@ -387,6 +387,9 @@ bool SurfaceFlinger::threadLoop()
{ {
waitForEvent(); waitForEvent();
// call Layer's destructor
handleDestroyLayers();
// check for transactions // check for transactions
if (UNLIKELY(mConsoleSignals)) { if (UNLIKELY(mConsoleSignals)) {
handleConsoleEvents(); handleConsoleEvents();
@ -580,6 +583,31 @@ void SurfaceFlinger::handleTransactionLocked(uint32_t transactionFlags)
commitTransaction(); commitTransaction();
} }
void SurfaceFlinger::destroyLayer(LayerBase const* layer)
{
Mutex::Autolock _l(mDestroyedLayerLock);
mDestroyedLayers.add(layer);
signalEvent();
}
void SurfaceFlinger::handleDestroyLayers()
{
Vector<LayerBase const *> destroyedLayers;
{ // scope for the lock
Mutex::Autolock _l(mDestroyedLayerLock);
destroyedLayers = mDestroyedLayers;
mDestroyedLayers.clear();
}
// call destructors without a lock held
const size_t count = destroyedLayers.size();
for (size_t i=0 ; i<count ; i++) {
//LOGD("destroying %s", destroyedLayers[i]->getName().string());
delete destroyedLayers[i];
}
}
sp<FreezeLock> SurfaceFlinger::getFreezeLock() const sp<FreezeLock> SurfaceFlinger::getFreezeLock() const
{ {
return new FreezeLock(const_cast<SurfaceFlinger *>(this)); return new FreezeLock(const_cast<SurfaceFlinger *>(this));

View File

@ -233,6 +233,7 @@ public:
status_t addLayer(const sp<LayerBase>& layer); status_t addLayer(const sp<LayerBase>& layer);
status_t invalidateLayerVisibility(const sp<LayerBase>& layer); status_t invalidateLayerVisibility(const sp<LayerBase>& layer);
void invalidateHwcGeometry(); void invalidateHwcGeometry();
void destroyLayer(LayerBase const* layer);
sp<Layer> getLayer(const sp<ISurface>& sur) const; sp<Layer> getLayer(const sp<ISurface>& sur) const;
@ -306,6 +307,7 @@ private:
void handleConsoleEvents(); void handleConsoleEvents();
void handleTransaction(uint32_t transactionFlags); void handleTransaction(uint32_t transactionFlags);
void handleTransactionLocked(uint32_t transactionFlags); void handleTransactionLocked(uint32_t transactionFlags);
void handleDestroyLayers();
void computeVisibleRegions( void computeVisibleRegions(
LayerVector& currentLayers, LayerVector& currentLayers,
@ -428,6 +430,10 @@ private:
mutable Barrier mReadyToRunBarrier; mutable Barrier mReadyToRunBarrier;
// protected by mDestroyedLayerLock;
mutable Mutex mDestroyedLayerLock;
Vector<LayerBase const *> mDestroyedLayers;
// atomic variables // atomic variables
enum { enum {
eConsoleReleased = 1, eConsoleReleased = 1,