2 Copyright (c) 2019 The Khronos Group Inc.
3 Use of this source code is governed by an MIT-style license that can be
4 found in the LICENSE.txt file.
10 <meta charset=
"utf-8">
11 <title>Draw WebGL to Canvas2D Repeatedly
</title>
12 <link rel=
"stylesheet" href=
"../../resources/js-test-style.css"/>
13 <script src=
"../../js/js-test-pre.js"></script>
14 <script src=
"../../js/webgl-test-utils.js"></script>
17 <div id=
"description"></div>
18 <div id=
"console"></div>
19 <canvas id=
"canvas-webgl" width=
"256" height=
"256"></canvas>
20 <canvas id=
"canvas-2d" width=
"256" height=
"256"></canvas>
23 const wtu
= WebGLTestUtils
;
27 function drawTo2DCanvas(c2
, webGLCanvas
) {
28 // Always clear 2D canvas to solid green first.
29 c2
.clearRect(0, 0, sz
, sz
);
30 c2
.fillStyle
= 'rgb(0,255,0)';
31 c2
.fillRect(0, 0, sz
, sz
);
32 // Draw WebGL canvas to this canvas.
33 c2
.drawImage(webGLCanvas
, 0, 0);
38 debug('Repeatedly drawing a WebGL canvas to a 2D canvas should only draw the most recently rendered WebGL content.');
39 debug('Regression test for <a href="http://crbug.com/894021">http://crbug.com/894021</a>');
41 let c2
= document
.getElementById('canvas-2d').getContext('2d');
42 let webGLCanvas
= document
.getElementById('canvas-webgl');
43 let gl
= wtu
.create3DContext(webGLCanvas
, { alpha
: true, antialias
: false, premultipliedAlpha
: true });
46 // Clear left half of WebGL canvas to red and right half to
48 gl
.disable(gl
.SCISSOR_TEST
);
49 gl
.clearColor(0.0, 0.0, 0.0, 0.0);
50 gl
.clear(gl
.COLOR_BUFFER_BIT
);
51 gl
.scissor(0, 0, sz
/ 2, sz
);
52 gl
.enable(gl
.SCISSOR_TEST
);
53 gl
.clearColor(1.0, 0.0, 0.0, 1.0);
54 gl
.clear(gl
.COLOR_BUFFER_BIT
);
56 drawTo2DCanvas(c2
, webGLCanvas
);
58 // Clear right half of WebGL canvas to red and left half to
60 gl
.disable(gl
.SCISSOR_TEST
);
61 gl
.clearColor(0.0, 0.0, 0.0, 0.0);
62 gl
.clear(gl
.COLOR_BUFFER_BIT
);
63 gl
.scissor(sz
/ 2, 0, sz
/ 2, sz
);
64 gl
.enable(gl
.SCISSOR_TEST
);
65 gl
.clearColor(1.0, 0.0, 0.0, 1.0);
66 gl
.clear(gl
.COLOR_BUFFER_BIT
);
68 drawTo2DCanvas(c2
, webGLCanvas
);
70 // Clear WebGL canvas to transparent black.
71 gl
.disable(gl
.SCISSOR_TEST
);
72 gl
.clearColor(0.0, 0.0, 0.0, 0.0);
73 gl
.clear(gl
.COLOR_BUFFER_BIT
);
75 drawTo2DCanvas(c2
, webGLCanvas
);
77 // 2D canvas should be green.
78 // In the error case, the rendering results from earlier draws were
79 // being accumulated, so the 2D canvas was ultimately red.
80 wtu
.checkCanvasRect(c2
, 0, 0, sz
, sz
,
88 requestAnimationFrame(runTest
);