Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / canvas / webgl / resources / draw-webgl-to-canvas-2d.js
blob28d2247a1426900a206b1d340b70d709b18b9fed
1 if (window.testRunner) {
2 testRunner.dumpAsText();
3 testRunner.waitUntilDone();
6 var preserve_ctx2D;
7 var preserve_canvas3D;
8 var preserve_gl;
9 var nonpreserve_ctx2D;
10 var nonpreserve_canvas3D;
11 var nonpreserve_gl;
12 var imgdata;
14 function renderWebGL(gl) {
15 gl.clearColor(0, 1, 0, 1);
16 gl.clear(gl.COLOR_BUFFER_BIT);
19 function drawWebGLToCanvas2D(ctx2D, canvas3D, isDrawingBufferUndefined) {
20 // draw red rect on canvas 2d.
21 ctx2D.fillStyle = 'red';
22 ctx2D.fillRect(0, 0, 100, 100);
23 var imageData = ctx2D.getImageData(0, 0, 1, 1);
24 imgdata = imageData.data;
25 shouldBe("imgdata[0]", "255");
26 shouldBe("imgdata[1]", "0");
27 shouldBe("imgdata[2]", "0");
29 // draw the webgl contents (green rect) on the canvas 2d context.
30 ctx2D.drawImage(canvas3D, 0, 0);
31 ctx2D.getImageData(0, 0, 1, 1);
32 imageData = ctx2D.getImageData(0, 0, 1, 1);
33 imgdata = imageData.data;
34 if (isDrawingBufferUndefined) {
35 // Current implementation draws transparent texture on the canvas 2d context,
36 // although the spec said it leads to undefined behavior.
37 shouldBe("imgdata[0]", "255");
38 shouldBe("imgdata[1]", "0");
39 shouldBe("imgdata[2]", "0");
40 } else {
41 shouldBe("imgdata[0]", "0");
42 shouldBe("imgdata[1]", "255");
43 shouldBe("imgdata[2]", "0");
46 if (isDrawingBufferUndefined && window.testRunner)
47 testRunner.notifyDone();
50 function asyncTest() {
51 debug("Check for drawing webgl to canvas 2d several frames after drawing webgl contents.")
52 debug("1) when drawingBuffer is preserved.")
53 drawWebGLToCanvas2D(preserve_ctx2D, preserve_canvas3D, false);
54 debug("2) when drawingBuffer is not preserved. It leads to undefined behavior.")
55 drawWebGLToCanvas2D(nonpreserve_ctx2D, nonpreserve_canvas3D, true);
58 function startTestAfterFirstPaint() {
59 // create both canvas 2d and webgl contexts.
60 createContexts();
61 // prepare webgl contents.
62 renderWebGL(preserve_gl);
63 renderWebGL(nonpreserve_gl);
65 debug("Check for drawing webgl to canvas 2d on the same frame.")
66 debug("1) when drawingBuffer is preserved.")
67 drawWebGLToCanvas2D(preserve_ctx2D, preserve_canvas3D, false);
68 debug("2) when drawingBuffer is not preserved.")
69 drawWebGLToCanvas2D(nonpreserve_ctx2D, nonpreserve_canvas3D, false);
71 if (window.testRunner) {
72 testRunner.waitUntilDone();
73 testRunner.layoutAndPaintAsyncThen(asyncTest);
74 } else {
75 window.requestAnimationFrame(asyncTest);
79 window.onload = function () {
80 window.requestAnimationFrame(startTestAfterFirstPaint);