draw-webgl-to-canvas-2d-repeatedly.html (2819B)
1 <!-- 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. 5 --> 6 7 <!DOCTYPE html> 8 <html> 9 <head> 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> 15 </head> 16 <body> 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> 21 <script> 22 "use strict"; 23 const wtu = WebGLTestUtils; 24 25 const sz = 256; 26 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); 34 } 35 36 function runTest() { 37 description(); 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>'); 40 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 }); 44 let tolerance = 2; 45 46 // Clear left half of WebGL canvas to red and right half to 47 // transparent black. 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); 55 // Draw to 2D canvas. 56 drawTo2DCanvas(c2, webGLCanvas); 57 58 // Clear right half of WebGL canvas to red and left half to 59 // transparent black. 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); 67 // Draw to 2D canvas. 68 drawTo2DCanvas(c2, webGLCanvas); 69 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); 74 // Draw to 2D canvas. 75 drawTo2DCanvas(c2, webGLCanvas); 76 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, 81 [ 0, 255, 0, 255 ], 82 "should be green", 83 tolerance); 84 85 finishTest(); 86 } 87 88 requestAnimationFrame(runTest); 89 </script> 90 </body> 91 </html>