4 <span id="description" style="color: white">
5 This test checks for drawing webgl to canvas 2d. The test process is as follows:
6 1. draw a green rect on a webgl context.
7 2. draw a red rect on a canvas 2d context, and check a pixel (should be red).
8 3. draw the webgl contents on the canvas 2d context, and check a pixel (should be green).
9 4. wait for few frames.
10 5. draw a red rect on the canvas 2d context, and check a pixel (should be red).
11 6. draw the webgl contents on the canvas 2d context, and check a pixel (see below explanation).
13 Above test is executed for both preserve and non-preserve webgl contexts.
14 For the preserve webgl context, the pixel on #6 is green.
15 For the non-preserve webgl context, the pixel on #6 is undefined.[1]
17 [1] http://www.khronos.org/registry/webgl/specs/latest/1.0/.
18 "This default behavior can be changed by setting the preserveDrawingBuffer
19 attribute of the WebGLContextAttributes object. If this flag is true, the
20 contents of the drawing buffer shall be preserved until the author either clears
21 or overwrites them. If this flag is false, attempting to perform operations
22 using this context as a source image after the rendering function has returned
23 can lead to undefined behavior.".
25 <canvas id="preserve-canvas3d" width="100" height="100"></canvas>
26 <canvas id="preserve-canvas2d" width="100" height="100"></canvas>
27 <canvas id="nonpreserve-canvas3d" width="100" height="100"></canvas>
28 <canvas id="nonpreserve-canvas2d" width="100" height="100"></canvas>
29 <script src="../../../resources/js-test.js"></script>
31 if (window.testRunner) {
32 testRunner.dumpAsText();
33 testRunner.waitUntilDone();
37 var preserve_canvas3D;
39 var nonpreserve_ctx2D;
40 var nonpreserve_canvas3D;
44 function createContexts() {
45 preserve_ctx2D = document.getElementById("preserve-canvas2d").getContext("2d");
46 preserve_canvas3D = document.getElementById('preserve-canvas3d');
47 preserve_gl = preserve_canvas3D.getContext('webgl', {'preserveDrawingBuffer': true});
48 nonpreserve_ctx2D = document.getElementById("nonpreserve-canvas2d").getContext("2d");
49 nonpreserve_canvas3D = document.getElementById('nonpreserve-canvas3d');
50 nonpreserve_gl = nonpreserve_canvas3D.getContext('webgl', {'preserveDrawingBuffer': false});
53 function renderWebGL(gl) {
54 gl.clearColor(0, 1, 0, 1);
55 gl.clear(gl.COLOR_BUFFER_BIT);
58 function drawWebGLToCanvas2D(ctx2D, canvas3D, isDrawingBufferUndefined) {
59 // draw red rect on canvas 2d.
60 ctx2D.fillStyle = 'red';
61 ctx2D.fillRect(0, 0, 100, 100);
62 var imageData = ctx2D.getImageData(0, 0, 1, 1);
63 imgdata = imageData.data;
64 shouldBe("imgdata[0]", "255");
65 shouldBe("imgdata[1]", "0");
66 shouldBe("imgdata[2]", "0");
68 // draw the webgl contents (green rect) on the canvas 2d context.
69 ctx2D.drawImage(canvas3D, 0, 0);
70 ctx2D.getImageData(0, 0, 1, 1);
71 imageData = ctx2D.getImageData(0, 0, 1, 1);
72 imgdata = imageData.data;
73 if (isDrawingBufferUndefined) {
74 // Current implementation draws transparent texture on the canvas 2d context,
75 // although the spec said it leads to undefined behavior.
76 shouldBe("imgdata[0]", "255");
77 shouldBe("imgdata[1]", "0");
78 shouldBe("imgdata[2]", "0");
80 shouldBe("imgdata[0]", "0");
81 shouldBe("imgdata[1]", "255");
82 shouldBe("imgdata[2]", "0");
85 if (isDrawingBufferUndefined && window.testRunner)
86 testRunner.notifyDone();
89 function asyncTest() {
90 debug("Check for drawing webgl to canvas 2d several frames after drawing webgl contents.")
91 debug("1) when drawingBuffer is preserved.")
92 drawWebGLToCanvas2D(preserve_ctx2D, preserve_canvas3D, false);
93 debug("2) when drawingBuffer is not preserved. It leads to undefined behavior.")
94 drawWebGLToCanvas2D(nonpreserve_ctx2D, nonpreserve_canvas3D, true);
97 function startTestAfterFirstPaint() {
98 // create both canvas 2d and webgl contexts.
100 // prepare webgl contents.
101 renderWebGL(preserve_gl);
102 renderWebGL(nonpreserve_gl);
104 debug("Check for drawing webgl to canvas 2d on the same frame.")
105 debug("1) when drawingBuffer is preserved.")
106 drawWebGLToCanvas2D(preserve_ctx2D, preserve_canvas3D, false);
107 debug("2) when drawingBuffer is not preserved.")
108 drawWebGLToCanvas2D(nonpreserve_ctx2D, nonpreserve_canvas3D, false);
110 if (window.testRunner) {
111 testRunner.waitUntilDone();
112 testRunner.displayAsyncThen(asyncTest);
114 window.requestAnimationFrame(asyncTest);
118 window.onload = function () {
119 window.requestAnimationFrame(startTestAfterFirstPaint);