Bug 1073219 - Use a simple RAII struct instead of nsRefPtr to manage mLayerCount for MaskLayerImageKey. r=dholbert

This commit is contained in:
Michael Layzell 2015-07-12 21:09:57 -07:00
Родитель e1824580b7
Коммит 155069d388
3 изменённых файлов: 42 добавлений и 13 удалений

Просмотреть файл

@ -1380,7 +1380,8 @@ struct MaskLayerUserData : public LayerUserData
mAppUnitsPerDevPixel == aOther.mAppUnitsPerDevPixel; mAppUnitsPerDevPixel == aOther.mAppUnitsPerDevPixel;
} }
nsRefPtr<const MaskLayerImageCache::MaskLayerImageKey> mImageKey; // Keeps a MaskLayerImageKey alive by managing its mLayerCount member-var
MaskLayerImageCache::MaskLayerImageKeyRef mImageKey;
// properties of the mask layer; the mask layer may be re-used if these // properties of the mask layer; the mask layer may be re-used if these
// remain unchanged. // remain unchanged.
nsTArray<DisplayItemClip::RoundedRect> mRoundedClipRects; nsTArray<DisplayItemClip::RoundedRect> mRoundedClipRects;
@ -5914,7 +5915,7 @@ ContainerState::CreateMaskLayer(Layer *aLayer,
userData->mOffset = newData.mOffset; userData->mOffset = newData.mOffset;
userData->mAppUnitsPerDevPixel = newData.mAppUnitsPerDevPixel; userData->mAppUnitsPerDevPixel = newData.mAppUnitsPerDevPixel;
userData->mRoundedClipRects.SwapElements(newData.mRoundedClipRects); userData->mRoundedClipRects.SwapElements(newData.mRoundedClipRects);
userData->mImageKey = lookupKey; userData->mImageKey.Reset(lookupKey);
return maskLayer.forget(); return maskLayer.forget();
} }

Просмотреть файл

@ -57,14 +57,6 @@ MaskLayerImageCache::PutImage(const MaskLayerImageKey* aKey, ImageContainer* aCo
entry->mContainer = aContainer; entry->mContainer = aContainer;
} }
// This case is particularly 'clever' because it uses AddRef/Release to track the use
// not to release the object.
template<>
struct HasDangerousPublicDestructor<MaskLayerImageCache::MaskLayerImageKey>
{
static const bool value = true;
};
MaskLayerImageCache::MaskLayerImageKey::MaskLayerImageKey() MaskLayerImageCache::MaskLayerImageKey::MaskLayerImageKey()
: mLayerCount(0) : mLayerCount(0)
, mRoundedClipRects() , mRoundedClipRects()

Просмотреть файл

@ -136,8 +136,8 @@ public:
~MaskLayerImageKey(); ~MaskLayerImageKey();
void AddRef() const { ++mLayerCount; } void IncLayerCount() const { ++mLayerCount; }
void Release() const void DecLayerCount() const
{ {
NS_ASSERTION(mLayerCount > 0, "Inconsistent layer count"); NS_ASSERTION(mLayerCount > 0, "Inconsistent layer count");
--mLayerCount; --mLayerCount;
@ -163,7 +163,43 @@ public:
nsTArray<PixelRoundedRect> mRoundedClipRects; nsTArray<PixelRoundedRect> mRoundedClipRects;
}; };
/**
* This struct maintains a reference to a MaskLayerImageKey, via a variant on
* refcounting. When a key is passed in via Reset(), we increment the
* passed-in key's mLayerCount, and we decrement its mLayerCount when we're
* destructed (or when the key is replaced via a second Reset() call).
*
* However, unlike standard refcounting smart-pointers, this object does
* *not* delete the tracked MaskLayerImageKey -- instead, deletion happens
* in MaskLayerImageCache::Sweep(), for any keys whose mLayerCount is 0.
*/
struct MaskLayerImageKeyRef
{
~MaskLayerImageKeyRef()
{
if (mRawPtr) {
mRawPtr->DecLayerCount();
}
}
MaskLayerImageKeyRef() : mRawPtr(nullptr) {}
MaskLayerImageKeyRef(const MaskLayerImageKeyRef&) = delete;
void operator=(const MaskLayerImageKeyRef&) = delete;
void Reset(const MaskLayerImageKey* aPtr)
{
MOZ_ASSERT(aPtr, "Cannot initialize a MaskLayerImageKeyRef with a null pointer");
aPtr->IncLayerCount();
if (mRawPtr) {
mRawPtr->DecLayerCount();
}
mRawPtr = aPtr;
}
private:
const MaskLayerImageKey* mRawPtr;
};
// Find an image container for aKey, returns nullptr if there is no suitable // Find an image container for aKey, returns nullptr if there is no suitable
// cached image. If there is an image, then aKey is set to point at the stored // cached image. If there is an image, then aKey is set to point at the stored
// key for the image. // key for the image.