mirror of
https://github.com/roytam1/basilisk55.git
synced 2026-05-26 15:02:46 +00:00
import from UXP: Issue #2073 - m-c 1651587: Make image::Image release efficient on main thread (9a39001c)
This commit is contained in:
@@ -61,14 +61,8 @@ AnimationSurfaceProvider::DropImageReference()
|
||||
return; // Nothing to do.
|
||||
}
|
||||
|
||||
// RasterImage objects need to be destroyed on the main thread. We also need
|
||||
// to destroy them asynchronously, because if our surface cache entry is
|
||||
// destroyed and we were the only thing keeping |mImage| alive, RasterImage's
|
||||
// destructor may call into the surface cache while whatever code caused us to
|
||||
// get evicted is holding the surface cache lock, causing deadlock.
|
||||
RefPtr<RasterImage> image = mImage;
|
||||
mImage = nullptr;
|
||||
NS_ReleaseOnMainThread(image.forget(), /* aAlwaysProxy = */ true);
|
||||
// RasterImage objects need to be destroyed on the main thread.
|
||||
SurfaceCache::ReleaseImageOnMainThread(mImage.forget());
|
||||
}
|
||||
|
||||
void
|
||||
|
||||
@@ -49,7 +49,8 @@ DecodedSurfaceProvider::DropImageReference()
|
||||
// get evicted is holding the surface cache lock, causing deadlock.
|
||||
RefPtr<RasterImage> image = mImage;
|
||||
mImage = nullptr;
|
||||
NS_ReleaseOnMainThread(image.forget(), /* aAlwaysProxy = */ true);
|
||||
SurfaceCache::ReleaseImageOnMainThread(image.forget(),
|
||||
/* aAlwaysProxy = */ true);
|
||||
}
|
||||
|
||||
DrawableFrameRef
|
||||
|
||||
+1
-1
@@ -78,7 +78,7 @@ Decoder::~Decoder()
|
||||
if (mImage && !NS_IsMainThread()) {
|
||||
// Dispatch mImage to main thread to prevent it from being destructed by the
|
||||
// decode thread.
|
||||
NS_ReleaseOnMainThread(mImage.forget());
|
||||
SurfaceCache::ReleaseImageOnMainThread(mImage.forget());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -437,8 +437,7 @@ RasterImage::OnSurfaceDiscarded(const SurfaceKey& aSurfaceKey)
|
||||
mAnimationState && aSurfaceKey.Playback() == PlaybackType::eAnimated;
|
||||
|
||||
RefPtr<RasterImage> image = this;
|
||||
NS_DispatchToMainThread(NS_NewRunnableFunction(
|
||||
[=]() -> void {
|
||||
NS_DispatchToMainThread(NS_NewRunnableFunction([=]() -> void {
|
||||
image->OnSurfaceDiscardedInternal(animatedFramesDiscarded);
|
||||
}));
|
||||
}
|
||||
|
||||
+57
-1
@@ -835,6 +835,32 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void ReleaseImageOnMainThread(already_AddRefed<image::Image>&& aImage,
|
||||
const StaticMutexAutoLock& aAutoLock) {
|
||||
RefPtr<image::Image> image = aImage;
|
||||
if (!image) {
|
||||
return;
|
||||
}
|
||||
|
||||
bool needsDispatch = mReleasingImagesOnMainThread.IsEmpty();
|
||||
mReleasingImagesOnMainThread.AppendElement(image);
|
||||
|
||||
if (!needsDispatch) {
|
||||
// There is already a ongoing task for ClearReleasingImages().
|
||||
return;
|
||||
}
|
||||
|
||||
NS_DispatchToMainThread(NS_NewRunnableFunction([]() -> void {
|
||||
SurfaceCache::ClearReleasingImages();
|
||||
}));
|
||||
}
|
||||
|
||||
void TakeReleasingImages(nsTArray<RefPtr<image::Image>>& aImage,
|
||||
const StaticMutexAutoLock& aAutoLock) {
|
||||
MOZ_ASSERT(NS_IsMainThread());
|
||||
aImage.SwapElements(mReleasingImagesOnMainThread);
|
||||
}
|
||||
|
||||
private:
|
||||
already_AddRefed<ImageSurfaceCache> GetImageCache(const ImageKey aImageKey)
|
||||
{
|
||||
@@ -942,7 +968,8 @@ private:
|
||||
nsRefPtrHashtable<nsPtrHashKey<Image>,
|
||||
ImageSurfaceCache> mImageCaches;
|
||||
SurfaceTracker mExpirationTracker;
|
||||
RefPtr<MemoryPressureObserver> mMemoryPressureObserver;
|
||||
RefPtr<MemoryPressureObserver> mMemoryPressureObserver;
|
||||
nsTArray<RefPtr<image::Image>> mReleasingImagesOnMainThread;
|
||||
const uint32_t mDiscardFactor;
|
||||
const Cost mMaxCost;
|
||||
Cost mAvailableCost;
|
||||
@@ -1160,5 +1187,34 @@ SurfaceCache::MaximumCapacity()
|
||||
return sInstance->MaximumCapacity();
|
||||
}
|
||||
|
||||
/* static */
|
||||
void SurfaceCache::ReleaseImageOnMainThread(
|
||||
already_AddRefed<image::Image> aImage, bool aAlwaysProxy) {
|
||||
if (NS_IsMainThread() && !aAlwaysProxy) {
|
||||
RefPtr<image::Image> image = std::move(aImage);
|
||||
return;
|
||||
}
|
||||
|
||||
StaticMutexAutoLock lock(sInstanceMutex);
|
||||
if (sInstance) {
|
||||
sInstance->ReleaseImageOnMainThread(std::move(aImage), lock);
|
||||
} else {
|
||||
NS_ReleaseOnMainThread(std::move(aImage), /* aAlwaysProxy */ true);
|
||||
}
|
||||
}
|
||||
|
||||
/* static */
|
||||
void SurfaceCache::ClearReleasingImages() {
|
||||
MOZ_ASSERT(NS_IsMainThread());
|
||||
|
||||
nsTArray<RefPtr<image::Image>> images;
|
||||
{
|
||||
StaticMutexAutoLock lock(sInstanceMutex);
|
||||
if (sInstance) {
|
||||
sInstance->TakeReleasingImages(images, lock);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace image
|
||||
} // namespace mozilla
|
||||
|
||||
@@ -417,6 +417,18 @@ struct SurfaceCache
|
||||
*/
|
||||
static size_t MaximumCapacity();
|
||||
|
||||
/**
|
||||
* Release image on main thread.
|
||||
* The function uses SurfaceCache to release pending releasing images quickly.
|
||||
*/
|
||||
static void ReleaseImageOnMainThread(already_AddRefed<image::Image> aImage,
|
||||
bool aAlwaysProxy = false);
|
||||
|
||||
/**
|
||||
* Clear all pending releasing images.
|
||||
*/
|
||||
static void ClearReleasingImages();
|
||||
|
||||
private:
|
||||
virtual ~SurfaceCache() = 0; // Forbid instantiation.
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user