content/canvas/test/webgl-conformance/conformance/reading/read-pixels-test.html

Thu, 15 Jan 2015 21:03:48 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 15 Jan 2015 21:03:48 +0100
branch
TOR_BUG_9701
changeset 11
deefc01c0e14
permissions
-rw-r--r--

Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)

michael@0 1 <!--
michael@0 2 Copyright (c) 2011 The Chromium Authors. All rights reserved.
michael@0 3 Use of this source code is governed by a BSD-style license that can be
michael@0 4 found in the LICENSE file.
michael@0 5 -->
michael@0 6 <!DOCTYPE html>
michael@0 7 <html>
michael@0 8 <head>
michael@0 9 <meta charset="utf-8">
michael@0 10 <title>WebGL ReadPixels conformance test.</title>
michael@0 11 <link rel="stylesheet" href="../../resources/js-test-style.css"/>
michael@0 12 <script src="../../resources/js-test-pre.js"></script>
michael@0 13 <script src="../resources/webgl-test.js"> </script>
michael@0 14 <script src="../resources/webgl-test-utils.js"> </script>
michael@0 15 </head>
michael@0 16 <body>
michael@0 17 <canvas id="example" width="200" height="200" style="width: 20px; height: 20px"></canvas>
michael@0 18 <div id="description"></div>
michael@0 19 <div id="console"></div>
michael@0 20 <script>
michael@0 21 description("Checks that ReadPixels works as expected.");
michael@0 22 testPassed("read-pixels-test start: " + (new Date().getTime()));
michael@0 23
michael@0 24 var wtu = WebGLTestUtils;
michael@0 25 var canvas = document.getElementById("example");
michael@0 26 var gl = create3DContext(canvas);
michael@0 27
michael@0 28 if (window.initNonKhronosFramework) {
michael@0 29 window.initNonKhronosFramework(true);
michael@0 30 }
michael@0 31
michael@0 32 var actual;
michael@0 33 var expected;
michael@0 34 var width = 2;
michael@0 35 var height = 2;
michael@0 36 var continueTestFunc = continueTestPart1;
michael@0 37
michael@0 38 gl.clearColor(1, 1, 1, 1);
michael@0 39 gl.clear(gl.COLOR_BUFFER_BIT);
michael@0 40
michael@0 41 // Resize the canvas to 2x2. This is an attempt to get stuff in the backbuffer.
michael@0 42 // that shouldn't be there.
michael@0 43 canvas.addEventListener("webglcontextlost", function(e) { e.preventDefault(); }, false);
michael@0 44 canvas.addEventListener("webglcontextrestored", continueTestAfterContextRestored, false);
michael@0 45 canvas.width = width;
michael@0 46 canvas.height = height;
michael@0 47 if (gl.getError() != gl.CONTEXT_LOST_WEBGL) {
michael@0 48 continueTestPart1();
michael@0 49 }
michael@0 50
michael@0 51 function continueTestAfterContextRestored() {
michael@0 52 window.gl = create3DContext(canvas);
michael@0 53 var func = continueTestFunc;
michael@0 54 window.continueTestFunc = function() { testFailed("should not be here"); };
michael@0 55 func();
michael@0 56 }
michael@0 57
michael@0 58 function continueTestPart1() {
michael@0 59 gl.clearColor(0.5, 0.7, 1.0, 1);
michael@0 60 gl.clear(gl.COLOR_BUFFER_BIT);
michael@0 61
michael@0 62 var innerColor = [0.5, 0.7, 1.0, 1];
michael@0 63 var outerColor = [0, 0, 0, 0];
michael@0 64
michael@0 65 var tests = [
michael@0 66 { msg: 'in range', checkColor: innerColor, x: 0, y: 0,
michael@0 67 oneColor: innerColor, oneX: 0, oneY: 0},
michael@0 68 { msg: 'off top left', checkColor: outerColor, x: -1, y: -1,
michael@0 69 oneColor: innerColor, oneX: 1, oneY: 1},
michael@0 70 { msg: 'off bottom right', checkColor: outerColor, x: 1, y: 1,
michael@0 71 oneColor: innerColor, oneX: 0, oneY: 0},
michael@0 72 { msg: 'completely off top ', checkColor: outerColor, x: 0, y: -2,
michael@0 73 oneColor: outerColor, oneX: 0, oneY: 0},
michael@0 74 { msg: 'completely off bottom', checkColor: outerColor, x: 0, y: 2,
michael@0 75 oneColor: outerColor, oneX: 0, oneY: 0},
michael@0 76 { msg: 'completely off left', checkColor: outerColor, x: -2, y: 0,
michael@0 77 oneColor: outerColor, oneX: 0, oneY: 0},
michael@0 78 { msg: 'completeley off right', checkColor: outerColor, x: 2, y: 0,
michael@0 79 oneColor: outerColor, oneX: 0, oneY: 0}
michael@0 80 ];
michael@0 81
michael@0 82 for (var tt = 0; tt < tests.length; ++tt) {
michael@0 83 var test = tests[tt];
michael@0 84 debug("");
michael@0 85 debug("checking: " + test.msg);
michael@0 86 checkBuffer(test.checkColor, test.x, test.y,
michael@0 87 test.oneColor, test.oneX, test.oneY);
michael@0 88 }
michael@0 89
michael@0 90 glErrorShouldBe(gl, gl.NO_ERROR, "there should be no GL errors");
michael@0 91
michael@0 92 function checkBuffer(checkColor, x, y, oneColor, oneX, oneY) {
michael@0 93 var buf = new Uint8Array(width * height * 4);
michael@0 94 gl.readPixels(x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf);
michael@0 95 for (var yy = 0; yy < height; ++yy) {
michael@0 96 for (var xx = 0; xx < width; ++xx) {
michael@0 97 var offset = (yy * width + xx) * 4;
michael@0 98 var expectedColors = (oneX == xx && oneY == yy) ? oneColor : checkColor;
michael@0 99 for (var cc = 0; cc < 4; ++cc) {
michael@0 100 var expectedColor = expectedColors[cc] * 255;
michael@0 101 var color = buf[offset + cc];
michael@0 102 var diff = Math.abs(expectedColor - color);
michael@0 103 assertMsg(diff < 3,
michael@0 104 "color pixel at " + xx + ", " + yy + " should be about " + expectedColor);
michael@0 105 }
michael@0 106 }
michael@0 107 }
michael@0 108 }
michael@0 109
michael@0 110 var badFormats = [
michael@0 111 {
michael@0 112 format: gl.RGB,
michael@0 113 type: gl.UNSIGNED_BYTE,
michael@0 114 dest: new Uint8Array(3),
michael@0 115 error: gl.INVALID_OPERATION
michael@0 116 },
michael@0 117 {
michael@0 118 format: gl.RGB,
michael@0 119 type: gl.UNSIGNED_SHORT_5_6_5,
michael@0 120 dest: new Uint8Array(3),
michael@0 121 error: gl.INVALID_OPERATION
michael@0 122 },
michael@0 123 {
michael@0 124 format: gl.RGBA,
michael@0 125 type: gl.UNSIGNED_SHORT_5_5_5_1,
michael@0 126 dest: new Uint16Array(1),
michael@0 127 error: gl.INVALID_OPERATION
michael@0 128 },
michael@0 129 {
michael@0 130 format: gl.RGBA,
michael@0 131 type: gl.UNSIGNED_SHORT_4_4_4_4,
michael@0 132 dest: new Uint16Array(1),
michael@0 133 error: gl.INVALID_OPERATION
michael@0 134 },
michael@0 135 {
michael@0 136 format: gl.ALPHA,
michael@0 137 type: gl.UNSIGNED_BYTE,
michael@0 138 dest: new Uint8Array(1),
michael@0 139 error: gl.INVALID_OPERATION
michael@0 140 },
michael@0 141 {
michael@0 142 format: gl.LUMINANCE,
michael@0 143 type: gl.UNSIGNED_BYTE,
michael@0 144 dest: new Uint8Array(1),
michael@0 145 error: gl.INVALID_ENUM
michael@0 146 },
michael@0 147 {
michael@0 148 format: gl.LUMINANCE_ALPHA,
michael@0 149 type: gl.UNSIGNED_BYTE,
michael@0 150 dest: new Uint8Array(2),
michael@0 151 error: gl.INVALID_ENUM
michael@0 152 }
michael@0 153 ];
michael@0 154 debug("");
michael@0 155 debug("check disallowed formats");
michael@0 156 for (var tt = 0; tt < badFormats.length; ++ tt) {
michael@0 157 var info = badFormats[tt]
michael@0 158 var format = info.format;
michael@0 159 var type = info.type;
michael@0 160 var dest = info.dest;
michael@0 161 var error = info.error;
michael@0 162 gl.readPixels(0, 0, 1, 1, format, type, dest);
michael@0 163 // note that the GL error is INVALID_OPERATION if both format and type are invalid, but
michael@0 164 // INVALID_ENUM if only one is.
michael@0 165 glErrorShouldBe(
michael@0 166 gl, error,
michael@0 167 "Should not be able to read as " + wtu.glEnumToString(gl, format) +
michael@0 168 " / " + wtu.glEnumToString(gl, type));
michael@0 169 }
michael@0 170
michael@0 171 debug("");
michael@0 172 debug("check reading with lots of drawing");
michael@0 173 continueTestFunc = continueTestPart2;
michael@0 174 width = 1024;
michael@0 175 height = 1024;
michael@0 176 canvas.width = width;
michael@0 177 canvas.height = height;
michael@0 178 if (gl.getError() != gl.CONTEXT_LOST_WEBGL) {
michael@0 179 continueTestPart2();
michael@0 180 }
michael@0 181 }
michael@0 182
michael@0 183 function continueTestPart2() {
michael@0 184 testPassed("read-pixels-test continueTestPart2: " + (new Date().getTime()));
michael@0 185 gl.viewport(0, 0, 1024, 1024);
michael@0 186 testPassed("read-pixels-test before setupTexturedQuad: " + (new Date().getTime()));
michael@0 187 var program = wtu.setupTexturedQuad(gl);
michael@0 188 testPassed("read-pixels-test after setupTexturedQuad: " + (new Date().getTime()));
michael@0 189 var loc = gl.getUniformLocation(program, "tex");
michael@0 190 gl.disable(gl.BLEND);
michael@0 191 gl.disable(gl.DEPTH_TEST);
michael@0 192 var colors = [[255, 0, 0, 255], [0, 255, 0, 255], [0, 0, 255, 255]];
michael@0 193 var textures = [];
michael@0 194 var results = [];
michael@0 195 testPassed("read-pixels-test before first loop: " + (new Date().getTime()));
michael@0 196 for (var ii = 0; ii < colors.length; ++ii) {
michael@0 197 gl.activeTexture(gl.TEXTURE0 + ii);
michael@0 198 var tex = gl.createTexture();
michael@0 199 testPassed("read-pixels-test first loop, ii = " + ii + ", before fillTexture: " + (new Date().getTime()));
michael@0 200 wtu.fillTexture(gl, tex, 1, 1, colors[ii]);
michael@0 201 testPassed("read-pixels-test first loop, ii = " + ii + ", after fillTexture: " + (new Date().getTime()));
michael@0 202 textures.push(tex);
michael@0 203 }
michael@0 204 for (var ii = 0; ii < colors.length; ++ii) {
michael@0 205 testPassed("read-pixels-test second loop, ii = " + ii + ": " + (new Date().getTime()));
michael@0 206 for (var jj = 0; jj < 300 + ii + 1; ++jj) {
michael@0 207 gl.uniform1i(loc, jj % 3);
michael@0 208 gl.drawArrays(gl.TRIANGLES, 0, 6);
michael@0 209 }
michael@0 210 var buf = new Uint8Array(4);
michael@0 211 testPassed("read-pixels-test second loop, before readpixels: " + (new Date().getTime()));
michael@0 212 gl.readPixels(512, 512, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, buf);
michael@0 213 testPassed("read-pixels-test second loop, after readpixels: " + (new Date().getTime()));
michael@0 214 results.push(buf);
michael@0 215 for (var kk = 0; kk < 99; ++kk) {
michael@0 216 gl.uniform1i(loc, (jj + kk) % 3);
michael@0 217 gl.drawArrays(gl.TRIANGLES, 0, 6);
michael@0 218 }
michael@0 219 testPassed("read-pixels-test second loop end: " + (new Date().getTime()));
michael@0 220 }
michael@0 221 for (var ii = 0; ii < colors.length; ++ii) {
michael@0 222 testPassed("read-pixels-test third loop, ii = " + ii + ": " + (new Date().getTime()));
michael@0 223 var buf = results[ii];
michael@0 224 var color = colors[ii];
michael@0 225 actual = [buf[0], buf[1], buf[2], buf[3]];
michael@0 226 expected = [color[0], color[1], color[2], color[3]];
michael@0 227 shouldBe("actual", "expected");
michael@0 228 testPassed("read-pixels-test third loop end: " + (new Date().getTime()));
michael@0 229 }
michael@0 230 glErrorShouldBe(gl, gl.NO_ERROR, "there should be no GL errors");
michael@0 231
michael@0 232 debug("");
michael@0 233
michael@0 234 // bug 763355: temporary logging of exceptions here to understand intermittent timeout
michael@0 235 // in this test, apparently in this finishTest() call.
michael@0 236 try {
michael@0 237 testPassed("read-pixels-test calling finishTest: " + (new Date().getTime()));
michael@0 238 finishTest();
michael@0 239 testPassed("read-pixels-test after finishTest: " + (new Date().getTime()));
michael@0 240 } catch(e) {
michael@0 241 testFailed("finishTest generated exception: " + e);
michael@0 242 }
michael@0 243 }
michael@0 244 </script>
michael@0 245 </body>
michael@0 246 </html>
michael@0 247

mercurial