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

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/canvas/test/webgl-conformance/conformance/reading/read-pixels-test.html	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,247 @@
     1.4 +<!--
     1.5 +Copyright (c) 2011 The Chromium Authors. All rights reserved.
     1.6 +Use of this source code is governed by a BSD-style license that can be
     1.7 +found in the LICENSE file.
     1.8 + -->
     1.9 +<!DOCTYPE html>
    1.10 +<html>
    1.11 +<head>
    1.12 +<meta charset="utf-8">
    1.13 +<title>WebGL ReadPixels conformance test.</title>
    1.14 +<link rel="stylesheet" href="../../resources/js-test-style.css"/>
    1.15 +<script src="../../resources/js-test-pre.js"></script>
    1.16 +<script src="../resources/webgl-test.js"> </script>
    1.17 +<script src="../resources/webgl-test-utils.js"> </script>
    1.18 +</head>
    1.19 +<body>
    1.20 +<canvas id="example" width="200" height="200" style="width: 20px; height: 20px"></canvas>
    1.21 +<div id="description"></div>
    1.22 +<div id="console"></div>
    1.23 +<script>
    1.24 +description("Checks that ReadPixels works as expected.");
    1.25 +testPassed("read-pixels-test start: " + (new Date().getTime()));
    1.26 +
    1.27 +var wtu = WebGLTestUtils;
    1.28 +var canvas = document.getElementById("example");
    1.29 +var gl = create3DContext(canvas);
    1.30 +
    1.31 +if (window.initNonKhronosFramework) {
    1.32 +   window.initNonKhronosFramework(true);
    1.33 +}
    1.34 +
    1.35 +var actual;
    1.36 +var expected;
    1.37 +var width = 2;
    1.38 +var height = 2;
    1.39 +var continueTestFunc = continueTestPart1;
    1.40 +
    1.41 +gl.clearColor(1, 1, 1, 1);
    1.42 +gl.clear(gl.COLOR_BUFFER_BIT);
    1.43 +
    1.44 +// Resize the canvas to 2x2. This is an attempt to get stuff in the backbuffer.
    1.45 +// that shouldn't be there.
    1.46 +canvas.addEventListener("webglcontextlost", function(e) { e.preventDefault(); }, false);
    1.47 +canvas.addEventListener("webglcontextrestored", continueTestAfterContextRestored, false);
    1.48 +canvas.width = width;
    1.49 +canvas.height = height;
    1.50 +if (gl.getError() != gl.CONTEXT_LOST_WEBGL) {
    1.51 +  continueTestPart1();
    1.52 +}
    1.53 +
    1.54 +function continueTestAfterContextRestored() {
    1.55 +  window.gl = create3DContext(canvas);
    1.56 +  var func = continueTestFunc;
    1.57 +  window.continueTestFunc = function() { testFailed("should not be here"); };
    1.58 +  func();
    1.59 +}
    1.60 +
    1.61 +function continueTestPart1() {
    1.62 +  gl.clearColor(0.5, 0.7, 1.0, 1);
    1.63 +  gl.clear(gl.COLOR_BUFFER_BIT);
    1.64 +
    1.65 +  var innerColor = [0.5, 0.7, 1.0, 1];
    1.66 +  var outerColor = [0, 0, 0, 0];
    1.67 +
    1.68 +  var tests = [
    1.69 +    { msg: 'in range', checkColor: innerColor, x:  0, y:  0,
    1.70 +      oneColor: innerColor, oneX: 0, oneY: 0},
    1.71 +    { msg: 'off top left', checkColor: outerColor, x: -1, y: -1,
    1.72 +      oneColor: innerColor, oneX: 1, oneY: 1},
    1.73 +    { msg: 'off bottom right', checkColor: outerColor, x:  1, y:  1,
    1.74 +      oneColor: innerColor, oneX: 0, oneY: 0},
    1.75 +    { msg: 'completely off top ', checkColor: outerColor, x:  0, y: -2,
    1.76 +      oneColor: outerColor, oneX: 0, oneY: 0},
    1.77 +    { msg: 'completely off bottom', checkColor: outerColor, x:  0, y:  2,
    1.78 +      oneColor: outerColor, oneX: 0, oneY: 0},
    1.79 +    { msg: 'completely off left', checkColor: outerColor, x: -2, y:  0,
    1.80 +      oneColor: outerColor, oneX: 0, oneY: 0},
    1.81 +    { msg: 'completeley off right', checkColor: outerColor, x:  2, y:  0,
    1.82 +      oneColor: outerColor, oneX: 0, oneY: 0}
    1.83 +  ];
    1.84 +
    1.85 +  for (var tt = 0; tt < tests.length; ++tt) {
    1.86 +    var test = tests[tt];
    1.87 +    debug("");
    1.88 +    debug("checking: " + test.msg);
    1.89 +    checkBuffer(test.checkColor, test.x, test.y,
    1.90 +                test.oneColor, test.oneX, test.oneY);
    1.91 +  }
    1.92 +
    1.93 +  glErrorShouldBe(gl, gl.NO_ERROR, "there should be no GL errors");
    1.94 +
    1.95 +  function checkBuffer(checkColor, x, y, oneColor, oneX, oneY) {
    1.96 +    var buf = new Uint8Array(width * height * 4);
    1.97 +    gl.readPixels(x, y, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf);
    1.98 +    for (var yy = 0; yy < height; ++yy) {
    1.99 +      for (var xx = 0; xx < width; ++xx) {
   1.100 +        var offset = (yy * width + xx) * 4;
   1.101 +        var expectedColors = (oneX == xx && oneY == yy) ? oneColor : checkColor;
   1.102 +        for (var cc = 0; cc < 4; ++cc) {
   1.103 +          var expectedColor = expectedColors[cc] * 255;
   1.104 +          var color = buf[offset + cc];
   1.105 +          var diff = Math.abs(expectedColor - color);
   1.106 +          assertMsg(diff < 3,
   1.107 +                    "color pixel at " + xx + ", " + yy + " should be about " + expectedColor);
   1.108 +        }
   1.109 +      }
   1.110 +    }
   1.111 +  }
   1.112 +
   1.113 +  var badFormats = [
   1.114 +    {
   1.115 +      format: gl.RGB,
   1.116 +      type: gl.UNSIGNED_BYTE,
   1.117 +      dest: new Uint8Array(3),
   1.118 +      error: gl.INVALID_OPERATION
   1.119 +    },
   1.120 +    {
   1.121 +      format: gl.RGB,
   1.122 +      type: gl.UNSIGNED_SHORT_5_6_5,
   1.123 +      dest: new Uint8Array(3),
   1.124 +      error: gl.INVALID_OPERATION
   1.125 +    },
   1.126 +    {
   1.127 +      format: gl.RGBA,
   1.128 +      type: gl.UNSIGNED_SHORT_5_5_5_1,
   1.129 +      dest: new Uint16Array(1),
   1.130 +      error: gl.INVALID_OPERATION
   1.131 +    },
   1.132 +    {
   1.133 +      format: gl.RGBA,
   1.134 +      type: gl.UNSIGNED_SHORT_4_4_4_4,
   1.135 +      dest: new Uint16Array(1),
   1.136 +      error: gl.INVALID_OPERATION
   1.137 +    },
   1.138 +    {
   1.139 +      format: gl.ALPHA,
   1.140 +      type: gl.UNSIGNED_BYTE,
   1.141 +      dest: new Uint8Array(1),
   1.142 +      error: gl.INVALID_OPERATION
   1.143 +    },
   1.144 +    {
   1.145 +      format: gl.LUMINANCE,
   1.146 +      type: gl.UNSIGNED_BYTE,
   1.147 +      dest: new Uint8Array(1),
   1.148 +      error: gl.INVALID_ENUM
   1.149 +    },
   1.150 +    {
   1.151 +      format: gl.LUMINANCE_ALPHA,
   1.152 +      type: gl.UNSIGNED_BYTE,
   1.153 +      dest: new Uint8Array(2),
   1.154 +      error: gl.INVALID_ENUM
   1.155 +    }
   1.156 +  ];
   1.157 +  debug("");
   1.158 +  debug("check disallowed formats");
   1.159 +  for (var tt = 0; tt < badFormats.length; ++ tt) {
   1.160 +    var info = badFormats[tt]
   1.161 +    var format = info.format;
   1.162 +    var type = info.type;
   1.163 +    var dest = info.dest;
   1.164 +    var error = info.error;
   1.165 +    gl.readPixels(0, 0, 1, 1, format, type, dest);
   1.166 +    // note that the GL error is INVALID_OPERATION if both format and type are invalid, but
   1.167 +    // INVALID_ENUM if only one is.
   1.168 +    glErrorShouldBe(
   1.169 +        gl, error,
   1.170 +        "Should not be able to read as " + wtu.glEnumToString(gl, format) +
   1.171 +        " / " + wtu.glEnumToString(gl, type));
   1.172 +  }
   1.173 +
   1.174 +  debug("");
   1.175 +  debug("check reading with lots of drawing");
   1.176 +  continueTestFunc = continueTestPart2;
   1.177 +  width = 1024;
   1.178 +  height = 1024;
   1.179 +  canvas.width = width;
   1.180 +  canvas.height = height;
   1.181 +  if (gl.getError() != gl.CONTEXT_LOST_WEBGL) {
   1.182 +    continueTestPart2();
   1.183 +  }
   1.184 +}
   1.185 +
   1.186 +function continueTestPart2() {
   1.187 +  testPassed("read-pixels-test continueTestPart2: " + (new Date().getTime()));
   1.188 +  gl.viewport(0, 0, 1024, 1024);
   1.189 +  testPassed("read-pixels-test before setupTexturedQuad: " + (new Date().getTime()));
   1.190 +  var program = wtu.setupTexturedQuad(gl);
   1.191 +  testPassed("read-pixels-test after setupTexturedQuad: " + (new Date().getTime()));
   1.192 +  var loc = gl.getUniformLocation(program, "tex");
   1.193 +  gl.disable(gl.BLEND);
   1.194 +  gl.disable(gl.DEPTH_TEST);
   1.195 +  var colors = [[255, 0, 0, 255], [0, 255, 0, 255], [0, 0, 255, 255]];
   1.196 +  var textures = [];
   1.197 +  var results = [];
   1.198 +  testPassed("read-pixels-test before first loop: " + (new Date().getTime()));
   1.199 +  for (var ii = 0; ii < colors.length; ++ii) {
   1.200 +    gl.activeTexture(gl.TEXTURE0 + ii);
   1.201 +    var tex = gl.createTexture();
   1.202 +    testPassed("read-pixels-test first loop, ii = " + ii + ", before fillTexture: " + (new Date().getTime()));
   1.203 +    wtu.fillTexture(gl, tex, 1, 1, colors[ii]);
   1.204 +    testPassed("read-pixels-test first loop, ii = " + ii + ", after fillTexture: " + (new Date().getTime()));
   1.205 +    textures.push(tex);
   1.206 +  }
   1.207 +  for (var ii = 0; ii < colors.length; ++ii) {
   1.208 +    testPassed("read-pixels-test second loop, ii = " + ii + ": " + (new Date().getTime()));
   1.209 +    for (var jj = 0; jj < 300 + ii + 1; ++jj) {
   1.210 +      gl.uniform1i(loc, jj % 3);
   1.211 +      gl.drawArrays(gl.TRIANGLES, 0, 6);
   1.212 +    }
   1.213 +    var buf = new Uint8Array(4);
   1.214 +    testPassed("read-pixels-test second loop, before readpixels: " + (new Date().getTime()));
   1.215 +    gl.readPixels(512, 512, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, buf);
   1.216 +    testPassed("read-pixels-test second loop, after readpixels: " + (new Date().getTime()));
   1.217 +    results.push(buf);
   1.218 +    for (var kk = 0; kk < 99; ++kk) {
   1.219 +      gl.uniform1i(loc, (jj + kk) % 3);
   1.220 +      gl.drawArrays(gl.TRIANGLES, 0, 6);
   1.221 +    }
   1.222 +    testPassed("read-pixels-test second loop end: " + (new Date().getTime()));
   1.223 +  }
   1.224 +  for (var ii = 0; ii < colors.length; ++ii) {
   1.225 +    testPassed("read-pixels-test third loop, ii = " + ii + ": " + (new Date().getTime()));
   1.226 +    var buf = results[ii];
   1.227 +    var color = colors[ii];
   1.228 +    actual = [buf[0], buf[1], buf[2], buf[3]];
   1.229 +    expected = [color[0], color[1], color[2], color[3]];
   1.230 +    shouldBe("actual", "expected");
   1.231 +    testPassed("read-pixels-test third loop end: " + (new Date().getTime()));
   1.232 +  }
   1.233 +  glErrorShouldBe(gl, gl.NO_ERROR, "there should be no GL errors");
   1.234 +
   1.235 +  debug("");
   1.236 +
   1.237 +  // bug 763355: temporary logging of exceptions here to understand intermittent timeout
   1.238 +  // in this test, apparently in this finishTest() call.
   1.239 +  try {
   1.240 +    testPassed("read-pixels-test calling finishTest: " + (new Date().getTime()));
   1.241 +    finishTest();
   1.242 +    testPassed("read-pixels-test after finishTest: " + (new Date().getTime()));
   1.243 +  } catch(e) {
   1.244 +    testFailed("finishTest generated exception: " + e);
   1.245 +  }
   1.246 +}
   1.247 +</script>
   1.248 +</body>
   1.249 +</html>
   1.250 +

mercurial