Issue #2073 - m-c 1383404: make SourceBuffer::Compact more efficient (squashed)

The first part also means that Compact no longer needs the SurfaceCache lock (used to be via CreateChunk->CanHold),
which avoids potential deadlocks during shutdown that m-c 523950 would otherwise cause
This commit is contained in:
Martok 2023-01-03 22:55:13 +01:00 committed by roytam1
commit 03a4a17ccf
6 changed files with 80 additions and 54 deletions

View file

@ -111,8 +111,32 @@ BadImage(const char* aMessage, RefPtr<T>& aImage)
return aImage.forget();
}
static void
SetSourceSizeHint(RasterImage* aImage, uint32_t aSize)
{
// Pass anything usable on so that the RasterImage can preallocate
// its source buffer.
if (aSize == 0) {
return;
}
// Bound by something reasonable
uint32_t sizeHint = std::min<uint32_t>(aSize, 20000000);
nsresult rv = aImage->SetSourceSizeHint(sizeHint);
if (NS_FAILED(rv)) {
// Flush memory, try to get some back, and try again.
rv = nsMemory::HeapMinimize(true);
nsresult rv2 = aImage->SetSourceSizeHint(sizeHint);
// If we've still failed at this point, things are going downhill.
if (NS_FAILED(rv) || NS_FAILED(rv2)) {
NS_WARNING("About to hit OOM in imagelib!");
}
}
}
/* static */ already_AddRefed<Image>
ImageFactory::CreateAnonymousImage(const nsCString& aMimeType)
ImageFactory::CreateAnonymousImage(const nsCString& aMimeType,
uint32_t aSizeHint /* = 0 */)
{
nsresult rv;
@ -127,6 +151,7 @@ ImageFactory::CreateAnonymousImage(const nsCString& aMimeType)
return BadImage("RasterImage::Init failed", newImage);
}
SetSourceSizeHint(newImage, aSizeHint);
return newImage.forget();
}
@ -231,25 +256,7 @@ ImageFactory::CreateRasterImage(nsIRequest* aRequest,
newImage->SetInnerWindowID(aInnerWindowId);
uint32_t len = GetContentSize(aRequest);
// Pass anything usable on so that the RasterImage can preallocate
// its source buffer.
if (len > 0) {
// Bound by something reasonable
uint32_t sizeHint = std::min<uint32_t>(len, 20000000);
rv = newImage->SetSourceSizeHint(sizeHint);
if (NS_FAILED(rv)) {
// Flush memory, try to get some back, and try again.
rv = nsMemory::HeapMinimize(true);
nsresult rv2 = newImage->SetSourceSizeHint(sizeHint);
// If we've still failed at this point, things are going downhill.
if (NS_FAILED(rv) || NS_FAILED(rv2)) {
NS_WARNING("About to hit OOM in imagelib!");
}
}
}
SetSourceSizeHint(newImage, GetContentSize(aRequest));
return newImage.forget();
}