summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/more/functions/readPixelsBadArgs.html
blob: 3d155ed61bf501bdd3e6d76e0a032fe431b0469e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!--
Copyright (c) 2019 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->
<link rel="stylesheet" type="text/css" href="../unit.css" />
<script type="application/javascript" src="../unit.js"></script>
<script type="application/javascript" src="../util.js"></script>
<script type="application/javascript" src="../../../js/webgl-test-utils.js"></script>
</head><body>
<canvas id="gl" width="16" height="16"></canvas>
<canvas id="c" width="128" height="128"></canvas>
<img id="i">
<script type="application/javascript">
var wtu = WebGLTestUtils;
var defaultImgUrl = "https://get.webgl.org/conformance-resources/opengl_logo.jpg";
var localImgUrl = "../../../resources/opengl_logo.jpg";

Tests.autoinit = false; // Prevents the test from running until the image is loaded

Tests.startUnit = function () {
  var canvas = document.getElementById('gl');
  var gl = wrapGLContext(getGLContext(canvas));
  return [gl];
}

Tests.testReadPixels = function(gl) {
  // we can't know if this is going to fail because of negative width
  // or because the buffer size doesn't match the dimensions.
  assertSomeGLError(gl, "negative width",
      function(){gl.readPixels(0,0,-1,1, gl.RGBA, gl.UNSIGNED_BYTE,
          new Uint8Array(4));});
  assertSomeGLError(gl,  "negative height",
      function(){gl.readPixels(0,0,1,-1, gl.RGBA, gl.UNSIGNED_BYTE,
          new Uint8Array(4));});
  assertOk("negative x",
      function(){gl.readPixels(-1,0,1,1, gl.RGBA, gl.UNSIGNED_BYTE,
          new Uint8Array(4));});
  assertOk("negative y",
      function(){gl.readPixels(0,-1,1,1, gl.RGBA, gl.UNSIGNED_BYTE,
          new Uint8Array(4));});
  assertOk("height > backbuffer height",
      function(){gl.readPixels(0,0,16,17, gl.RGBA, gl.UNSIGNED_BYTE,
          new Uint8Array(16*17*4));});
  assertOk("width > backbuffer width",
      function(){gl.readPixels(0,0,17,16, gl.RGBA, gl.UNSIGNED_BYTE,
          new Uint8Array(17*16*4));});
  assertOk("width, height = 0",
      function(){gl.readPixels(0,0,0,0, gl.RGBA, gl.UNSIGNED_BYTE,
          new Uint8Array(0));});
  // we can't know if this is going to fail because of negative width
  // or because the buffer size doesn't match the dimensions.
  assertSomeGLError(gl, "bad format",
      function(){gl.readPixels(0,0,1,1, gl.FLOAT, gl.UNSIGNED_BYTE,
          new Uint8Array(4*4));});
  // we can't know if this is going to fail because of negative width
  // or because the buffer size doesn't match the dimensions.
  assertSomeGLError(gl, "bad type",
      function(){gl.readPixels(0,0,1,1, gl.ALPHA, gl.FLOAT,
          new Uint8Array(1*4));});
}

Tests.testReadPixelsSOPIMG = function(gl) {
    var img = document.getElementById("i");
    var tex = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, tex);
    // SOP failure
    assertThrowNoGLError(gl, "throw because img is from another domain",
      function(){gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img);});
    gl.bindTexture(gl.TEXTURE_2D, null);
    assertOk("canvas still origin-clean",
      function(){gl.readPixels(0,0,1,1, gl.RGBA, gl.UNSIGNED_BYTE,
          new Uint8Array(4));});
    gl.deleteTexture(tex);
}
Tests.testReadPixelsSOPCanvas = function(gl) {
    var img = document.getElementById("i");
    var c = document.getElementById("c");
    c.getContext("2d").drawImage(img, 0, 0);
    assertFail("canvas throws because not origin clean",
      function(){c.getContext("2d").getImageData(0,0,1,1);});
    var tex = gl.createTexture();
    gl.bindTexture(gl.TEXTURE_2D, tex);
    // SOP failure
    assertThrowNoGLError(gl, "throw because canvas is not origin clean",
      function(){gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, c);});
    gl.bindTexture(gl.TEXTURE_2D, null);
    assertOk("canvas still origin-clean",
      function(){gl.readPixels(0,0,1,1, gl.RGBA, gl.UNSIGNED_BYTE,
          new Uint8Array(4));});
    gl.deleteTexture(tex);
}

Tests.endUnit = function(gl) {
};

(async function() {
  const img = document.getElementById('i');
  try {
    await wtu.awaitOrTimeout(wtu.loadCrossOriginImage(img, defaultImgUrl, localImgUrl));
  } catch (e) {
    testFailed(`Image setup failed (${e}).`);
    finishTest();
    return;
  }
  initTests();
})();
</script>
</body></html>