Bug 1172796 - Part 6: Implements ImageBitmap::Close(). r=roc r=smaug

--HG--
extra : commitid : 85dot8oSPkP
This commit is contained in:
Morris Tseng 2015-12-18 14:52:17 +08:00
parent 25c5294a43
commit 4144a07339
5 changed files with 120 additions and 0 deletions

View File

@ -408,6 +408,14 @@ ImageBitmap::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
return ImageBitmapBinding::Wrap(aCx, this, aGivenProto);
}
void
ImageBitmap::Close()
{
mData = nullptr;
mSurface = nullptr;
mPictureRect.SetEmpty();
}
void
ImageBitmap::SetPictureRect(const IntRect& aRect, ErrorResult& aRv)
{
@ -419,6 +427,10 @@ ImageBitmap::PrepareForDrawTarget(gfx::DrawTarget* aTarget)
{
MOZ_ASSERT(aTarget);
if (!mData) {
return nullptr;
}
if (!mSurface) {
mSurface = mData->GetAsSourceSurface();
}

View File

@ -91,6 +91,8 @@ public:
return mPictureRect.Height();
}
void Close();
/*
* The PrepareForDrawTarget() might return null if the mPictureRect does not
* intersect with the size of mData.

View File

@ -246,6 +246,7 @@ support-files = captureStream_common.js
support-files = file_drawWindow_source.html file_drawWindow_common.js
skip-if = (buildapp == 'b2g' && toolkit != 'gonk')
[test_imagebitmap.html]
[test_imagebitmap_close.html]
[test_imagebitmap_cropping.html]
[test_imagebitmap_on_worker.html]
[test_imagebitmap_structuredclone.html]

View File

@ -0,0 +1,93 @@
<!DOCTYPE HTML>
<html>
<head>
<title>WebGL in OffscreenCanvas</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<script src="/tests/SimpleTest/WindowSnapshot.js"></script>
<link rel="stylesheet" href="/tests/SimpleTest/test.css">
</head>
<body>
<script type="text/js-worker">
function ok(expect, msg) {
postMessage({"type": "status", status: !!expect, msg: msg});
}
onmessage = function(event) {
var bitmap = event.data.bitmap;
ok(!!bitmap, "Get the ImageBitmap from the main script.");
bitmap.close();
ok(bitmap.width == 0 && bitmap.height == 0, "After close(), width and height should return 0");
postMessage({"type": "finish"});
}
</script>
<script>
SimpleTest.waitForExplicitFinish();
function createCanvas() {
var htmlCanvas = document.createElement('canvas');
htmlCanvas.width = 64;
htmlCanvas.height = 64;
document.body.appendChild(htmlCanvas);
return htmlCanvas;
}
function runTest() {
var canvas1 = createCanvas();
var ctx = canvas1.getContext("2d");
ctx.fillStyle = "#00FF00";
ctx.fillRect(0, 0, 64, 64);
var canvasRef = createCanvas();
var ctx = canvasRef.getContext("2d");
ctx.fillStyle = "#00FF00";
ctx.fillRect(0, 0, 64, 64);
createImageBitmap(canvas1).then(function(bmp) {
var canvas2 = createCanvas();
var ctx2 = canvas2.getContext("2d");
ctx2.drawImage(bmp, 0, 0);
ok(canvasRef.toDataURL() == canvas2.toDataURL(), "toDataURL should return same result.");
document.body.removeChild(canvas2);
bmp.close();
ok(bmp.width == 0 && bmp.height == 0, "After close(), width and height should return 0");
var canvas2 = createCanvas();
var ctx2 = canvas2.getContext("2d");
var beforeDrawImageDataURL = canvas2.toDataURL();
ctx2.drawImage(bmp, 0, 0);
var afterDrawImageDataURL = canvas2.toDataURL();
ok(beforeDrawImageDataURL == afterDrawImageDataURL,
"Drawing operations with a closed ImageBitmap should do nothing.");
runTestOnWorker();
});
}
function runTestOnWorker() {
var canvas1 = createCanvas();
var ctx = canvas1.getContext("2d");
ctx.fillStyle = "#00FF00";
ctx.fillRect(0, 0, 64, 64);
var blob = new Blob(Array.prototype.map.call(document.querySelectorAll("script[type=\"text\/js-worker\"]"), function (oScript) { return oScript.textContent; }),{type: "text/javascript"});
var worker = new Worker(window.URL.createObjectURL(blob));
createImageBitmap(canvas1).then(function(bmp) {
worker.postMessage({bitmap: bmp}, [bmp]);
worker.onmessage = function(event) {
if (event.data.type == "status") {
ok(event.data.status, event.data.msg);
} else if (event.data.type == "finish") {
SimpleTest.finish();
}
}
});
}
runTest();
</script>
</body>
</html>

View File

@ -23,6 +23,18 @@ interface ImageBitmap {
readonly attribute unsigned long height;
};
// It's crucial that there be a way to explicitly dispose of ImageBitmaps
// since they refer to potentially large graphics resources. Some uses
// of this API proposal will result in repeated allocations of ImageBitmaps,
// and garbage collection will not reliably reclaim them quickly enough.
// Here we reuse close(), which also exists on another Transferable type,
// MessagePort. Potentially, all Transferable types should inherit from a
// new interface type "Closeable".
partial interface ImageBitmap {
// Dispose of all graphical resources associated with this ImageBitmap.
void close();
};
[NoInterfaceObject, Exposed=(Window,Worker)]
interface ImageBitmapFactories {
[Throws]