mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-25 05:41:12 +00:00
Bug 1312148 - report memory allocation while creating ImageBitmap; r=mtseng,smaug
Creating ImageBitmap from the following sources includes allocating new memory: (1) from ImageData. (2) from Blob. (3) from HTMLCanvasElement. (4) from CanvasRenderingContext2D. (5) from Structured-clone. (6) from Transferring. (7) from OffscreenCanvas. (8) from ArrayBuffer/TypedArray. We need to report to DOM so that the GC would be triggered appropriately. MozReview-Commit-ID: 7rvNsjVNqpz --HG-- extra : rebase_source : 8879c9a1723b65fcd1bae2ae7e8befb5be8a5c69 extra : source : c0fc415ed753cb675a605a6e6a55e369733c6646
This commit is contained in:
parent
9c32f727eb
commit
42ed861e1e
@ -34,6 +34,45 @@ NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ImageBitmap)
|
||||
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
||||
NS_INTERFACE_MAP_END
|
||||
|
||||
/*
|
||||
* This helper function is used to notify DOM that aBytes memory is allocated
|
||||
* here so that we could trigger GC appropriately.
|
||||
*/
|
||||
static void
|
||||
RegisterAllocation(nsIGlobalObject* aGlobal, size_t aBytes)
|
||||
{
|
||||
AutoJSAPI jsapi;
|
||||
if (jsapi.Init(aGlobal)) {
|
||||
JS_updateMallocCounter(jsapi.cx(), aBytes);
|
||||
}
|
||||
}
|
||||
|
||||
static void
|
||||
RegisterAllocation(nsIGlobalObject* aGlobal, SourceSurface* aSurface)
|
||||
{
|
||||
// Calculate how many bytes are used.
|
||||
const int bytesPerPixel = BytesPerPixel(aSurface->GetFormat());
|
||||
const size_t bytes =
|
||||
aSurface->GetSize().height * aSurface->GetSize().width * bytesPerPixel;
|
||||
|
||||
// Register.
|
||||
RegisterAllocation(aGlobal, bytes);
|
||||
}
|
||||
|
||||
static void
|
||||
RegisterAllocation(nsIGlobalObject* aGlobal, layers::Image* aImage)
|
||||
{
|
||||
// Calculate how many bytes are used.
|
||||
if (aImage->GetFormat() == mozilla::ImageFormat::PLANAR_YCBCR) {
|
||||
RegisterAllocation(aGlobal, aImage->AsPlanarYCbCrImage()->GetDataSize());
|
||||
} else if (aImage->GetFormat() == mozilla::ImageFormat::NV_IMAGE) {
|
||||
RegisterAllocation(aGlobal, aImage->AsNVImage()->GetBufferSize());
|
||||
} else {
|
||||
RefPtr<SourceSurface> surface = aImage->GetAsSourceSurface();
|
||||
RegisterAllocation(aGlobal, surface);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* If either aRect.width or aRect.height are negative, then return a new IntRect
|
||||
* which represents the same rectangle as the aRect does but with positive width
|
||||
@ -707,6 +746,9 @@ ImageBitmap::CreateFromCloneData(nsIGlobalObject* aGlobal,
|
||||
RefPtr<ImageBitmap> ret = new ImageBitmap(aGlobal, data,
|
||||
aData->mIsPremultipliedAlpha);
|
||||
|
||||
// Report memory allocation.
|
||||
RegisterAllocation(aGlobal, aData->mSurface);
|
||||
|
||||
ret->mIsCroppingAreaOutSideOfSourceImage =
|
||||
aData->mIsCroppingAreaOutSideOfSourceImage;
|
||||
|
||||
@ -741,6 +783,10 @@ ImageBitmap::CreateFromOffscreenCanvas(nsIGlobalObject* aGlobal,
|
||||
CreateImageFromSurface(surface);
|
||||
|
||||
RefPtr<ImageBitmap> ret = new ImageBitmap(aGlobal, data);
|
||||
|
||||
// Report memory allocation.
|
||||
RegisterAllocation(aGlobal, surface);
|
||||
|
||||
return ret.forget();
|
||||
}
|
||||
|
||||
@ -865,6 +911,7 @@ ImageBitmap::CreateInternal(nsIGlobalObject* aGlobal, HTMLCanvasElement& aCanvas
|
||||
// If the HTMLCanvasElement's rendering context is WebGL, then the snapshot
|
||||
// we got from the HTMLCanvasElement is a DataSourceSurface which is a copy
|
||||
// of the rendering context. We handle cropping in this case.
|
||||
bool needToReportMemoryAllocation = false;
|
||||
if ((aCanvasEl.GetCurrentContextType() == CanvasContextType::WebGL1 ||
|
||||
aCanvasEl.GetCurrentContextType() == CanvasContextType::WebGL2) &&
|
||||
aCropRect.isSome()) {
|
||||
@ -875,6 +922,7 @@ ImageBitmap::CreateInternal(nsIGlobalObject* aGlobal, HTMLCanvasElement& aCanvas
|
||||
RefPtr<DataSourceSurface> dataSurface = surface->GetDataSurface();
|
||||
croppedSurface = CropAndCopyDataSourceSurface(dataSurface, cropRect);
|
||||
cropRect.MoveTo(0, 0);
|
||||
needToReportMemoryAllocation = true;
|
||||
}
|
||||
else {
|
||||
croppedSurface = surface;
|
||||
@ -895,6 +943,11 @@ ImageBitmap::CreateInternal(nsIGlobalObject* aGlobal, HTMLCanvasElement& aCanvas
|
||||
|
||||
RefPtr<ImageBitmap> ret = new ImageBitmap(aGlobal, data);
|
||||
|
||||
// Report memory allocation if needed.
|
||||
if (needToReportMemoryAllocation) {
|
||||
RegisterAllocation(aGlobal, croppedSurface);
|
||||
}
|
||||
|
||||
// Set the picture rectangle.
|
||||
if (ret && aCropRect.isSome()) {
|
||||
ret->SetPictureRect(cropRect, aRv);
|
||||
@ -958,6 +1011,9 @@ ImageBitmap::CreateInternal(nsIGlobalObject* aGlobal, ImageData& aImageData,
|
||||
// ImageData's underlying data is not alpha-premultiplied.
|
||||
RefPtr<ImageBitmap> ret = new ImageBitmap(aGlobal, data, false);
|
||||
|
||||
// Report memory allocation.
|
||||
RegisterAllocation(aGlobal, data);
|
||||
|
||||
// The cropping information has been handled in the CreateImageFromRawData()
|
||||
// function.
|
||||
|
||||
@ -999,6 +1055,9 @@ ImageBitmap::CreateInternal(nsIGlobalObject* aGlobal, CanvasRenderingContext2D&
|
||||
|
||||
RefPtr<ImageBitmap> ret = new ImageBitmap(aGlobal, data);
|
||||
|
||||
// Report memory allocation.
|
||||
RegisterAllocation(aGlobal, surface);
|
||||
|
||||
// Set the picture rectangle.
|
||||
if (ret && aCropRect.isSome()) {
|
||||
ret->SetPictureRect(aCropRect.ref(), aRv);
|
||||
@ -1240,6 +1299,9 @@ protected:
|
||||
}
|
||||
}
|
||||
|
||||
// Report memory allocation.
|
||||
RegisterAllocation(mGlobalObject, imageBitmap->mData);
|
||||
|
||||
mPromise->MaybeResolve(imageBitmap);
|
||||
return true;
|
||||
}
|
||||
@ -1524,6 +1586,9 @@ ImageBitmap::ReadStructuredClone(JSContext* aCx,
|
||||
if (!GetOrCreateDOMReflector(aCx, imageBitmap, &value)) {
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
// Report memory allocation.
|
||||
RegisterAllocation(aParent, aClonedSurfaces[aIndex]);
|
||||
}
|
||||
|
||||
return &(value.toObject());
|
||||
@ -2112,6 +2177,9 @@ ImageBitmap::Create(nsIGlobalObject* aGlobal,
|
||||
// Assume the data from an external buffer is not alpha-premultiplied.
|
||||
RefPtr<ImageBitmap> imageBitmap = new ImageBitmap(aGlobal, data, false);
|
||||
|
||||
// Report memory allocation.
|
||||
RegisterAllocation(aGlobal, data);
|
||||
|
||||
// We don't need to call SetPictureRect() here because there is no cropping
|
||||
// supported and the ImageBitmap's mPictureRect is the size of the source
|
||||
// image in default
|
||||
|
Loading…
Reference in New Issue
Block a user