OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 |
| 3 /* |
| 4 ** Copyright (c) 2012 The Khronos Group Inc. |
| 5 ** |
| 6 ** Permission is hereby granted, free of charge, to any person obtaining a |
| 7 ** copy of this software and/or associated documentation files (the |
| 8 ** "Materials"), to deal in the Materials without restriction, including |
| 9 ** without limitation the rights to use, copy, modify, merge, publish, |
| 10 ** distribute, sublicense, and/or sell copies of the Materials, and to |
| 11 ** permit persons to whom the Materials are furnished to do so, subject to |
| 12 ** the following conditions: |
| 13 ** |
| 14 ** The above copyright notice and this permission notice shall be included |
| 15 ** in all copies or substantial portions of the Materials. |
| 16 ** |
| 17 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 18 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 19 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 20 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 21 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 22 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 23 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. |
| 24 */ |
| 25 |
| 26 --> |
| 27 |
| 28 <!DOCTYPE html> |
| 29 <html> |
| 30 <head> |
| 31 <meta charset="utf-8"> |
| 32 <title>WebGL Canvas Conformance Tests</title> |
| 33 <link rel="stylesheet" href="../../resources/js-test-style.css"/> |
| 34 <script src="../../resources/desktop-gl-constants.js" type="text/javascript"></s
cript> |
| 35 <script src="../../resources/js-test-pre.js"></script> |
| 36 <script src="../resources/webgl-test.js"></script> |
| 37 </head> |
| 38 <body> |
| 39 <div id="description"></div> |
| 40 <div id="console"></div> |
| 41 <canvas id="canvas" style="width: 50px; height: 50px;"> </canvas> |
| 42 <canvas id="canvas2d" width="40" height="40"> </canvas> |
| 43 <script> |
| 44 if (window.initNonKhronosFramework) { |
| 45 window.initNonKhronosFramework(true); |
| 46 } |
| 47 |
| 48 description("This test ensures WebGL implementations interact correctly with the
canvas tag."); |
| 49 |
| 50 debug(""); |
| 51 debug("Canvas.getContext"); |
| 52 |
| 53 var err; |
| 54 var canvas = document.getElementById("canvas"); |
| 55 var canvas2d = document.getElementById("canvas2d"); |
| 56 var ctx2d = canvas2d.getContext("2d"); |
| 57 var gl = create3DContext(canvas); |
| 58 if (!gl) { |
| 59 testFailed("context does not exist"); |
| 60 } else { |
| 61 testPassed("context exists"); |
| 62 |
| 63 debug(""); |
| 64 debug("Checking canvas and WebGL interaction"); |
| 65 |
| 66 // Check that a canvas with no width or height is 300x150 pixels |
| 67 shouldBe('canvas.width', '300'); |
| 68 shouldBe('canvas.height', '150'); |
| 69 |
| 70 // Check get a 4 value gl parameter as a csv string. |
| 71 function getValue4v(name) { |
| 72 var v = gl.getParameter(name); |
| 73 var result = '' + |
| 74 v[0] + ',' + |
| 75 v[1] + ',' + |
| 76 v[2] + ',' + |
| 77 v[3]; |
| 78 return result; |
| 79 } |
| 80 |
| 81 function getViewport() { |
| 82 return getValue4v(gl.VIEWPORT); |
| 83 } |
| 84 |
| 85 function getClearColor() { |
| 86 return getValue4v(gl.COLOR_CLEAR_VALUE); |
| 87 } |
| 88 |
| 89 function isAboutEqual(a, b) { |
| 90 return Math.abs(a - b) < 0.01; |
| 91 } |
| 92 |
| 93 function isAboutEqualInt(a, b) { |
| 94 return Math.abs(a - b) < 3; |
| 95 } |
| 96 |
| 97 function checkCanvasContentIs(r3d,g3d,b3d,a3d) { |
| 98 var r2d; |
| 99 var g2d; |
| 100 var b2d; |
| 101 var a2d; |
| 102 |
| 103 function checkPixel(x, y, r3d,g3d,b3d,a3d) { |
| 104 var offset = (y * 40 + x) * 4; |
| 105 r2d = imgData.data[offset]; |
| 106 g2d = imgData.data[offset + 1]; |
| 107 b2d = imgData.data[offset + 2]; |
| 108 a2d = imgData.data[offset + 3]; |
| 109 //debug('' + x + ', ' + y + "(" + offset + ") = " + r2d + ", " + g2d + ",
" + b2d + ", " + a2d); |
| 110 return isAboutEqualInt(r2d, r3d) && |
| 111 isAboutEqualInt(g2d, g3d) && |
| 112 isAboutEqualInt(b2d, b3d) && |
| 113 isAboutEqualInt(a2d, a3d); |
| 114 } |
| 115 |
| 116 function checkPixels(r3d,g3d,b3d,a3d) { |
| 117 return checkPixel(0, 0, r3d, g3d, b3d, a3d) && |
| 118 checkPixel(0, 39, r3d, g3d, b3d, a3d) && |
| 119 checkPixel(39, 0, r3d, g3d, b3d, a3d) && |
| 120 checkPixel(39, 39, r3d, g3d, b3d, a3d) && |
| 121 checkPixel(0, 0, r3d, g3d, b3d, a3d); |
| 122 }; |
| 123 |
| 124 // Set to just take the color from the 3d canvas |
| 125 ctx2d.globalCompositeOperation = 'copy'; |
| 126 |
| 127 // fill 2d canvas with orange |
| 128 ctx2d.fillStyle = "rgb(255,192,128)"; |
| 129 ctx2d.fillRect (0, 0, 40, 40); |
| 130 |
| 131 // get the image data |
| 132 var imgData = ctx2d.getImageData(0, 0, 40, 40); |
| 133 |
| 134 // check it got cleared. |
| 135 if (!checkPixels(255, 192, 128, 255)) { |
| 136 testFailed("unable to fill 2d context."); |
| 137 return; |
| 138 } |
| 139 |
| 140 // draw 3d canvas on top. |
| 141 ctx2d.drawImage(canvas, 0,0, 40, 40); |
| 142 |
| 143 // get the image data |
| 144 var imgData = ctx2d.getImageData(0, 0, 40, 40); |
| 145 |
| 146 // Check it's the expected color. |
| 147 if (!checkPixels(r3d, g3d, b3d, a3d)) { |
| 148 testFailed("pixels are " + r2d + "," + g2d + "," + b2d + "," + a2d + |
| 149 " expected " + r3d + "," + g3d + "," + b3d + "," + a3d); |
| 150 } else { |
| 151 testPassed("pixels are " + r3d + "," + g3d + "," + b3d + "," + a3d); |
| 152 } |
| 153 } |
| 154 |
| 155 checkCanvasContentIs(0, 0, 0, 0); |
| 156 shouldBe('getViewport()', '"0,0,300,150"'); |
| 157 |
| 158 // Change the display size of the canvas and check |
| 159 // the viewport size does not change. |
| 160 debug(""); |
| 161 debug("change display size of canvas and see that viewport does not change"); |
| 162 canvas.style.width = "100px"; |
| 163 canvas.style.height = "25px"; |
| 164 var intervalId; |
| 165 intervalId = window.setInterval(function() { |
| 166 if (canvas.clientWidth == 100 && |
| 167 canvas.clientHeight == 25) { |
| 168 window.clearInterval(intervalId); |
| 169 shouldBe('getViewport()', '"0,0,300,150"'); |
| 170 shouldBe('canvas.width', '300'); |
| 171 shouldBe('canvas.height', '150'); |
| 172 |
| 173 // Change the actual size of the canvas |
| 174 // Check that the viewport does not change. |
| 175 // Check that the clear color does not change. |
| 176 // Check that the color mask does not change. |
| 177 debug(""); |
| 178 debug("change the actual size of the canvas and see that the viewport does
not change"); |
| 179 gl.clearColor(0.25, 0.5, 0.75, 1); |
| 180 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
| 181 checkCanvasContentIs(64, 128, 192, 255); |
| 182 gl.colorMask(0,0,0,0); |
| 183 glErrorShouldBe(gl, gl.NO_ERROR, "No GL errors before resizing the canvas"
); |
| 184 canvas.width = 400; |
| 185 canvas.height = 10; |
| 186 err = gl.getError(); |
| 187 // Some implementations might lost the context when resizing |
| 188 if (err != gl.CONTEXT_LOST_WEBGL) { |
| 189 shouldBe("err", "gl.NO_ERROR"); |
| 190 var v = gl.getParameter(gl.COLOR_CLEAR_VALUE); |
| 191 assertMsg(isAboutEqual(v[0], 0.25) && |
| 192 isAboutEqual(v[1], 0.5) && |
| 193 isAboutEqual(v[2], 0.75) && |
| 194 isAboutEqual(v[3], 1), |
| 195 "gl.clearColor should not change after canvas resize"); |
| 196 v = gl.getParameter(gl.COLOR_WRITEMASK); |
| 197 assertMsg(isAboutEqual(v[0], 0) && |
| 198 isAboutEqual(v[1], 0) && |
| 199 isAboutEqual(v[2], 0) && |
| 200 isAboutEqual(v[3], 0), |
| 201 "gl.colorMask should not change after canvas resize"); |
| 202 shouldBe('getViewport()', '"0,0,300,150"'); |
| 203 checkCanvasContentIs(0, 0, 0, 0); |
| 204 } |
| 205 |
| 206 debug(""); |
| 207 finishTest(); |
| 208 } |
| 209 }, 1000/30); |
| 210 } |
| 211 </script> |
| 212 |
| 213 </body> |
| 214 </html> |
OLD | NEW |