Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support to bitmaprenderer in OffscreenCanvas #16032

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Canvas's ImageBitmapRenderingContext test</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel="help" href="https://html.spec.whatwg.org/multipage/scripting.html#the-imagebitmap-rendering-context">
<script>
var width = 10;
var height = 10;

function testImageBitmap(image, opts, expectedR, expectedG, expectedB, expectedA)
{
var dstCanvas = new OffscreenCanvas(width,height);
var dstCtx = dstCanvas.getContext('bitmaprenderer', opts);
dstCtx.transferFromImageBitmap(image);

var myCanvas = document.createElement('canvas');
myCanvas.width = width;
myCanvas.height = height;
var myCtx = myCanvas.getContext('2d');
myCtx.drawImage(dstCanvas, 0, 0);
var color = myCtx.getImageData(5, 5, 1, 1).data;
assert_array_approx_equals(color, [expectedR, expectedG, expectedB, expectedA],1);
}

promise_test(function() {
var srcCanvas = document.createElement('canvas');
srcCanvas.width = width;
srcCanvas.height = height;
var ctx = srcCanvas.getContext('2d');
ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
ctx.fillRect(0, 0, width, height);
return createImageBitmap(srcCanvas).then(function(image) {
testImageBitmap(image, {alpha: false}, 0, 255, 0, 128);
});
}, "Test that an ImageBitmapRenderingContext with alpha disabled makes the canvas opaque");

promise_test(function() {
var srcCanvas = document.createElement('canvas');
srcCanvas.width = width;
srcCanvas.height = height;
var ctx = srcCanvas.getContext('2d');
ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
ctx.fillRect(0, 0, width, height);
return createImageBitmap(srcCanvas).then(function(image) {
testImageBitmap(image, {alpha: true}, 0, 255, 0, 128);
});
}, "Test that an ImageBitmapRenderingContext with alpha enabled preserves the alpha");

promise_test(function() {
var srcCanvas = document.createElement('canvas');
srcCanvas.width = width;
srcCanvas.height = height;
var ctx = srcCanvas.getContext('2d');
ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
ctx.fillRect(0, 0, width, height);
return createImageBitmap(srcCanvas).then(function(image) {
testImageBitmap(image, {}, 0, 255, 0, 128);
});
}, "Test that the 'alpha' context creation attribute is true by default");

</script>
17 changes: 17 additions & 0 deletions imagebitmap-renderingcontext/context-creation-offscreen.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Canvas's ImageBitmapRenderingContext test</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel="help" href="https://html.spec.whatwg.org/multipage/scripting.html#the-imagebitmap-rendering-context">
<script>
var width = 10;
var height = 10;

test(function() {
var canvas = new OffscreenCanvas(60,60);
var ctx = canvas.getContext('bitmaprenderer');
assert_true(ctx instanceof ImageBitmapRenderingContext);
}, "Test that canvas.getContext('bitmaprenderer') returns an instance of ImageBitmapRenderingContext");

</script>
25 changes: 7 additions & 18 deletions imagebitmap-renderingcontext/context-creation-with-alpha.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,21 @@
var width = 10;
var height = 10;

function testCanvas(ctx, r, g, b, a)
{
var color = ctx.getImageData(5, 5, 1, 1).data;
assert_array_equals(color, [r, g, b, a]);
}

function consumeImageBitmap(image, alphaVal, expectedR, expectedG, expectedB, expectedA)
function testImageBitmap(image, opts, expectedR, expectedG, expectedB, expectedA)
{
var dstCanvas = document.createElement('canvas');
dstCanvas.width = width;
dstCanvas.height = height;
var dstCtx;
if (alphaVal == 'true')
dstCtx = dstCanvas.getContext('bitmaprenderer', { alpha: true });
else if (alphaVal == 'false')
dstCtx = dstCanvas.getContext('bitmaprenderer', { alpha: false });
else
dstCtx = dstCanvas.getContext('bitmaprenderer');
var dstCtx = dstCanvas.getContext('bitmaprenderer', opts);
dstCtx.transferFromImageBitmap(image);

var myCanvas = document.createElement('canvas');
myCanvas.width = width;
myCanvas.height = height;
var myCtx = myCanvas.getContext('2d');
myCtx.drawImage(dstCanvas, 0, 0);
testCanvas(myCtx, expectedR, expectedG, expectedB, expectedA);
var color = myCtx.getImageData(5, 5, 1, 1).data;
assert_array_approx_equals(color, [expectedR, expectedG, expectedB, expectedA],1);
}

promise_test(function() {
Expand All @@ -44,7 +33,7 @@
ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
ctx.fillRect(0, 0, width, height);
return createImageBitmap(srcCanvas).then(function(image) {
consumeImageBitmap(image, 'false', 0, 127, 0, 255);
testImageBitmap(image, {alpha: false}, 0, 255, 0, 128);
});
}, "Test that an ImageBitmapRenderingContext with alpha disabled makes the canvas opaque");

Expand All @@ -56,7 +45,7 @@
ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
ctx.fillRect(0, 0, width, height);
return createImageBitmap(srcCanvas).then(function(image) {
consumeImageBitmap(image, 'true', 0, 255, 0, 127);
testImageBitmap(image, {alpha: true}, 0, 255, 0, 128);
});
}, "Test that an ImageBitmapRenderingContext with alpha enabled preserves the alpha");

Expand All @@ -68,7 +57,7 @@
ctx.fillStyle = 'rgba(0, 255, 0, 0.5)';
ctx.fillRect(0, 0, width, height);
return createImageBitmap(srcCanvas).then(function(image) {
consumeImageBitmap(image, '', 0, 255, 0, 127);
testImageBitmap(image, {}, 0, 255, 0, 128);
});
}, "Test that the 'alpha' context creation attribute is true by default");

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!DOCTYPE html>
<meta charset=utf-8>
<title>convertToBlob, OffscreenCanvas: origin-clean flag</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel="help" href="https://html.spec.whatwg.org/multipage/scripting.html#the-imagebitmap-rendering-context">
<script>
const crossOriginImageUrl = "http://{{domains[www1]}}:{{ports[http][0]}}/images/red.png";

async_test(t => {
var img = new Image();
// Use a cross-origin URL.
img.src = crossOriginImageUrl;

img.onload = t.step_func(e => {
createImageBitmap(img, 0, 0, img.width, img.height)
.then(t.step_func(bitmap => {
var canvasBitmap = new OffscreenCanvas(img.width,img.height);
var contextBitmap = canvasBitmap.getContext("bitmaprenderer");
contextBitmap.transferFromImageBitmap(bitmap);
promise_rejects(t,
new DOMException(
'convertToBlob on a tainted OffscreenCanvas has to throw',
'SecurityError'),
canvasBitmap.convertToBlob(),
"convertToBlob on a tainted OffscreenCanvas has to throw").then(t.step_func_done());
}));
});
}, "Test that call convertToBlob on a tainted OffscreenCanvas throws exception");
</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Canvas's ImageBitmapRenderingContext test</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel="help" href="https://html.spec.whatwg.org/multipage/scripting.html#the-imagebitmap-rendering-context">
<script>
var width = 10;
var height = 10;

function testCanvas(ctx, r, g, b, a)
{
var color = ctx.getImageData(5, 5, 1, 1).data;
assert_array_equals(color, [r, g, b, a]);
}

promise_test(function() {
function testTransferFromImageBitmapNullability(greenImage) {
var bitmapCanvas = new OffscreenCanvas(width,height);
var bitmapCtx = bitmapCanvas.getContext('bitmaprenderer');
bitmapCtx.transferFromImageBitmap(greenImage);

// Make sure the bitmap renderer canvas is filled correctly.
var pngImage = document.getElementById("img");
bitmapCanvas.convertToBlob().then(function(blob){
pngImage.src = URL.createObjectURL(blob);
var myCanvasToTest = document.createElement('canvas');
myCanvasToTest.width = width;
myCanvasToTest.height = height;
var myCtxToTest = myCanvasToTest.getContext('2d');
myCtxToTest.drawImage(pngImage, 0, 0);
testCanvas(myCtxToTest, 0, 255, 0, 255);
});
}

var greenCanvas = document.createElement('canvas');
greenCanvas.width = width;
greenCanvas.height = height;
var greenCtx = greenCanvas.getContext('2d');
greenCtx.fillStyle = '#0f0';
greenCtx.fillRect(0, 0, width, height);

return Promise.all([
createImageBitmap(greenCanvas),
]).then(([greenImage]) => {
testTransferFromImageBitmapNullability(greenImage);
});
},'Test that transferToBlob works and produce the expected image');

</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Canvas's ImageBitmapRenderingContext test</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel="help" href="https://html.spec.whatwg.org/multipage/scripting.html#the-imagebitmap-rendering-context">
<script>
var width = 10;
var height = 10;

function testCanvas(bitmapCanvas, r, g, b, a)
{
var myCanvas = document.createElement('canvas');
myCanvas.width = width;
myCanvas.height = height;
var myCtx = myCanvas.getContext('2d');
myCtx.drawImage(bitmapCanvas, 0, 0);
var color = myCtx.getImageData(5, 5, 1, 1).data;
assert_array_equals(color, [r, g, b, a]);
}

promise_test(function() {
function testTransferFromImageBitmapNullability(greenImage) {
var bitmapCanvas = new OffscreenCanvas(width,height);
var bitmapCtx = bitmapCanvas.getContext('bitmaprenderer');
bitmapCtx.transferFromImageBitmap(greenImage);

// Make sure the bitmap renderer canvas is filled correctly.
var myCanvas = document.createElement('canvas');
var myCtx = myCanvas.getContext('bitmaprenderer');
myCtx.transferFromImageBitmap(bitmapCanvas.transferToImageBitmap());
testCanvas(myCanvas, 0, 255, 0, 255);

// Test that after transfering for second time to ImageBitmap produces
// a black bitmap of the same size
var myCanvas2 = document.createElement('canvas');
var myCtx2 = myCanvas2.getContext('bitmaprenderer');
myCtx2.transferFromImageBitmap(bitmapCanvas.transferToImageBitmap());
testCanvas(myCanvas2, 0, 0, 0, 255);
}

var greenCanvas = document.createElement('canvas');
greenCanvas.width = width;
greenCanvas.height = height;
var greenCtx = greenCanvas.getContext('2d');
greenCtx.fillStyle = '#0f0';
greenCtx.fillRect(0, 0, width, height);

return Promise.all([
createImageBitmap(greenCanvas),
]).then(([greenImage]) => {
testTransferFromImageBitmapNullability(greenImage);
});
},'Test that transferToImageBitmap works and that resets the imagebitmap to black');

</script>
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<!DOCTYPE html>
<meta charset="utf-8">
<title>Canvas's ImageBitmapRenderingContext test</title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<link rel="help" href="https://html.spec.whatwg.org/multipage/scripting.html#the-imagebitmap-rendering-context">
<script>
var width = 10;
var height = 10;

function testCanvas(bitmapCanvas, r, g, b, a)
{
var myCanvas = document.createElement('canvas');
myCanvas.width = width;
myCanvas.height = height;
var myCtx = myCanvas.getContext('2d');
myCtx.drawImage(bitmapCanvas, 0, 0);
var color = myCtx.getImageData(5, 5, 1, 1).data;
assert_array_equals(color, [r, g, b, a]);
}

promise_test(function() {
function testTransferFromImageBitmapNullability(greenImage, redImage) {
var bitmapCanvas = new OffscreenCanvas(width,height);
var bitmapCtx = bitmapCanvas.getContext('bitmaprenderer');
bitmapCtx.transferFromImageBitmap(greenImage);

// Make sure the bitmap renderer canvas is filled correctly.
testCanvas(bitmapCanvas, 0, 255, 0, 255);

// Test if passing null resets the bitmap renderer canvas.
// Drawing the resetted canvas cannot change the destination canvas.
bitmapCtx.transferFromImageBitmap(null);
testCanvas(bitmapCanvas, 0, 0, 0, 0);

// Test if we can redraw the bitmap canvas correctly after reset.
bitmapCtx.transferFromImageBitmap(redImage);
testCanvas(bitmapCanvas, 255, 0, 0, 255);
}

var greenCanvas = document.createElement('canvas');
greenCanvas.width = width;
greenCanvas.height = height;
var greenCtx = greenCanvas.getContext('2d');
greenCtx.fillStyle = '#0f0';
greenCtx.fillRect(0, 0, width, height);

var redCanvas = document.createElement('canvas');
redCanvas.width = width;
redCanvas.height = height;
var redCtx = redCanvas.getContext('2d');
redCtx.fillStyle = '#f00';
redCtx.fillRect(0, 0, width, height);

return Promise.all([
createImageBitmap(greenCanvas),
createImageBitmap(redCanvas),
]).then(([greenImage, redImage]) => {
testTransferFromImageBitmapNullability(greenImage, redImage);
});
},'Test that transferFromImageBitmap(null) discards the previously transferred image');

</script>
30 changes: 10 additions & 20 deletions imagebitmap-renderingcontext/tranferFromImageBitmap-null.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@
var width = 10;
var height = 10;

function testCanvas(ctx, r, g, b, a)
function testCanvas(bitmapCanvas, r, g, b, a)
{
var color = ctx.getImageData(5, 5, 1, 1).data;
var myCanvas = document.createElement('canvas');
myCanvas.width = width;
myCanvas.height = height;
var myCtx = myCanvas.getContext('2d');
myCtx.drawImage(bitmapCanvas, 0, 0);
var color = myCtx.getImageData(5, 5, 1, 1).data;
assert_array_equals(color, [r, g, b, a]);
}

Expand All @@ -23,31 +28,16 @@
bitmapCtx.transferFromImageBitmap(greenImage);

// Make sure the bitmap renderer canvas is filled correctly.
var myCanvas = document.createElement('canvas');
myCanvas.width = width;
myCanvas.height = height;
var myCtx = myCanvas.getContext('2d');
myCtx.drawImage(bitmapCanvas, 0, 0);
testCanvas(myCtx, 0, 255, 0, 255);
testCanvas(bitmapCanvas, 0, 255, 0, 255);

// Test if passing null resets the bitmap renderer canvas.
// Drawing the resetted canvas cannot change the destination canvas.
bitmapCtx.transferFromImageBitmap(null);
var myCanvas2 = document.createElement('canvas');
myCanvas2.width = width;
myCanvas2.height = height;
var myCtx2 = myCanvas2.getContext('2d');
myCtx2.drawImage(bitmapCanvas, 0, 0);
testCanvas(myCtx2, 0, 0, 0, 0);
testCanvas(bitmapCanvas, 0, 0, 0, 0);

// Test if we can redraw the bitmap canvas correctly after reset.
bitmapCtx.transferFromImageBitmap(redImage);
var myCanvas3 = document.createElement('canvas');
myCanvas3.width = width;
myCanvas3.height = height;
var myCtx3 = myCanvas3.getContext('2d');
myCtx3.drawImage(bitmapCanvas, 0, 0);
testCanvas(myCtx3, 255, 0, 0, 255);
testCanvas(bitmapCanvas, 255, 0, 0, 255);
}

var greenCanvas = document.createElement('canvas');
Expand Down
Loading