OLD | NEW |
(Empty) | |
| 1 /* |
| 2 ** Copyright (c) 2012 The Khronos Group Inc. |
| 3 ** |
| 4 ** Permission is hereby granted, free of charge, to any person obtaining a |
| 5 ** copy of this software and/or associated documentation files (the |
| 6 ** "Materials"), to deal in the Materials without restriction, including |
| 7 ** without limitation the rights to use, copy, modify, merge, publish, |
| 8 ** distribute, sublicense, and/or sell copies of the Materials, and to |
| 9 ** permit persons to whom the Materials are furnished to do so, subject to |
| 10 ** the following conditions: |
| 11 ** |
| 12 ** The above copyright notice and this permission notice shall be included |
| 13 ** in all copies or substantial portions of the Materials. |
| 14 ** |
| 15 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 17 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 18 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
| 19 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
| 20 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
| 21 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. |
| 22 */ |
| 23 |
| 24 function generateTest(pixelFormat, pixelType, prologue) { |
| 25 var wtu = WebGLTestUtils; |
| 26 var gl = null; |
| 27 var successfullyParsed = false; |
| 28 |
| 29 var init = function() |
| 30 { |
| 31 description('Verify texImage2D and texSubImage2D code paths taking canva
s elements (' + pixelFormat + '/' + pixelType + ')'); |
| 32 |
| 33 gl = wtu.create3DContext("example"); |
| 34 |
| 35 if (!prologue(gl)) { |
| 36 finishTest(); |
| 37 return; |
| 38 } |
| 39 |
| 40 var program = wtu.setupTexturedQuad(gl); |
| 41 |
| 42 gl.clearColor(0,0,0,1); |
| 43 gl.clearDepth(1); |
| 44 |
| 45 var testCanvas = document.createElement('canvas'); |
| 46 runTest(testCanvas); |
| 47 //document.body.appendChild(testCanvas); |
| 48 } |
| 49 |
| 50 function setCanvasToRedGreen(ctx) { |
| 51 var width = ctx.canvas.width; |
| 52 var height = ctx.canvas.height; |
| 53 var halfHeight = Math.floor(height / 2); |
| 54 ctx.fillStyle = "#ff0000"; |
| 55 ctx.fillRect(0, 0, width, halfHeight); |
| 56 ctx.fillStyle = "#00ff00"; |
| 57 ctx.fillRect(0, halfHeight, width, height - halfHeight); |
| 58 } |
| 59 |
| 60 function drawTextInCanvas(ctx) { |
| 61 var width = ctx.canvas.width; |
| 62 var height = ctx.canvas.height; |
| 63 ctx.fillStyle = "#ffffff"; |
| 64 ctx.fillRect(0, 0, width, height); |
| 65 ctx.font = '20pt Arial'; |
| 66 ctx.fillStyle = 'black'; |
| 67 ctx.textAlign = "center"; |
| 68 ctx.textBaseline = "middle"; |
| 69 ctx.fillText("1234567890", width / 2, height / 4); |
| 70 } |
| 71 |
| 72 function setCanvasTo257x257(ctx) { |
| 73 ctx.canvas.width = 257; |
| 74 ctx.canvas.height = 257; |
| 75 setCanvasToRedGreen(ctx); |
| 76 } |
| 77 |
| 78 function setCanvasTo1x2(ctx) { |
| 79 ctx.canvas.width = 1; |
| 80 ctx.canvas.height = 2; |
| 81 setCanvasToRedGreen(ctx); |
| 82 } |
| 83 |
| 84 function runOneIteration(canvas, useTexSubImage2D, flipY, topColor, bottomCo
lor, opt_texture, opt_fontTest) |
| 85 { |
| 86 debug('Testing ' + (useTexSubImage2D ? 'texSubImage2D' : 'texImage2D') + |
| 87 ' with flipY=' + flipY + ' canvas size: ' + canvas.width + 'x' + c
anvas.height + |
| 88 (opt_fontTest ? " with fonts" : " with red-green")); |
| 89 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
| 90 // Disable any writes to the alpha channel |
| 91 // No idea why this was here. If it's important put it back and leave a
comment as to why |
| 92 // gl.colorMask(1, 1, 1, 0); |
| 93 if (!opt_texture) { |
| 94 var texture = gl.createTexture(); |
| 95 // Bind the texture to texture unit 0 |
| 96 gl.bindTexture(gl.TEXTURE_2D, texture); |
| 97 // Set up texture parameters |
| 98 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); |
| 99 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); |
| 100 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
; |
| 101 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
; |
| 102 } else { |
| 103 var texture = opt_texture; |
| 104 } |
| 105 // Set up pixel store parameters |
| 106 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY); |
| 107 gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); |
| 108 gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE); |
| 109 // Upload the image into the texture |
| 110 if (useTexSubImage2D) { |
| 111 // Initialize the texture to black first |
| 112 gl.texImage2D(gl.TEXTURE_2D, 0, gl[pixelFormat], canvas.width, canva
s.height, 0, |
| 113 gl[pixelFormat], gl[pixelType], null); |
| 114 gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl[pixelFormat], gl[pixelTy
pe], canvas); |
| 115 } else { |
| 116 gl.texImage2D(gl.TEXTURE_2D, 0, gl[pixelFormat], gl[pixelFormat], gl
[pixelType], canvas); |
| 117 } |
| 118 |
| 119 // Draw the triangles |
| 120 wtu.clearAndDrawUnitQuad(gl, [0, 255, 0, 255]); |
| 121 |
| 122 if (opt_fontTest) { |
| 123 var width = gl.canvas.width; |
| 124 var height = gl.canvas.height; |
| 125 var halfHeight = Math.floor(height / 2); |
| 126 var top = flipY ? 0 : (height - halfHeight); |
| 127 var bottom = flipY ? (height - halfHeight) : 0; |
| 128 // check half is a solid color. |
| 129 wtu.checkCanvasRect( |
| 130 gl, 0, top, width, halfHeight, |
| 131 [255, 255, 255, 255], |
| 132 "should be white"); |
| 133 // check other half is not a solid color. |
| 134 wtu.checkCanvasRectColor( |
| 135 gl, 0, bottom, width, halfHeight, |
| 136 [255, 255, 255, 255], 0, |
| 137 function() { |
| 138 testFailed("font missing"); |
| 139 }, |
| 140 function() { |
| 141 testPassed("font renderered"); |
| 142 }, |
| 143 debug); |
| 144 } else { |
| 145 // Check a few pixels near the top and bottom and make sure they hav
e |
| 146 // the right color. |
| 147 var width = gl.canvas.width; |
| 148 var height = gl.canvas.height; |
| 149 var halfHeight = Math.floor(height / 2); |
| 150 debug("Checking bottom"); |
| 151 wtu.checkCanvasRect(gl, 0, 0, width, halfHeight, bottomColor, |
| 152 "shouldBe " + bottomColor); |
| 153 debug("Checking top"); |
| 154 wtu.checkCanvasRect(gl, 0, height - halfHeight, width, halfHeight, t
opColor, |
| 155 "shouldBe " + topColor); |
| 156 } |
| 157 |
| 158 if (false) { |
| 159 var m = new Image(); |
| 160 m.src = gl.canvas.toDataURL(); |
| 161 document.getElementById("console").appendChild(m); |
| 162 document.getElementById("console").appendChild(document.createElement(
"hr")); |
| 163 } |
| 164 |
| 165 return texture; |
| 166 } |
| 167 |
| 168 function runTest(canvas) |
| 169 { |
| 170 var ctx = canvas.getContext("2d"); |
| 171 var red = [255, 0, 0]; |
| 172 var green = [0, 255, 0]; |
| 173 |
| 174 var count = 4; |
| 175 var caseNdx = 0; |
| 176 |
| 177 var cases = [ |
| 178 { sub: false, flipY: true, top: red, bottom: green, font: false,
init: setCanvasTo1x2 }, |
| 179 { sub: false, flipY: false, top: green, bottom: red, font: false }
, |
| 180 { sub: true, flipY: true, top: red, bottom: green, font: false }
, |
| 181 { sub: true, flipY: false, top: green, bottom: red, font: false }
, |
| 182 { sub: false, flipY: true, top: red, bottom: green, font: false,
init: setCanvasTo257x257 }, |
| 183 { sub: false, flipY: false, top: green, bottom: red, font: false }
, |
| 184 { sub: true, flipY: true, top: red, bottom: green, font: false }
, |
| 185 { sub: true, flipY: false, top: green, bottom: red, font: false }
, |
| 186 { sub: false, flipY: true, top: red, bottom: green, font: true, i
nit: drawTextInCanvas }, |
| 187 { sub: false, flipY: false, top: green, bottom: red, font: true }, |
| 188 { sub: true, flipY: true, top: red, bottom: green, font: true }, |
| 189 { sub: true, flipY: false, top: green, bottom: red, font: true }, |
| 190 ]; |
| 191 |
| 192 var texture; |
| 193 function runNextTest() { |
| 194 var c = cases[caseNdx]; |
| 195 if (c.init) { |
| 196 c.init(ctx); |
| 197 } |
| 198 texture = runOneIteration(canvas, c.sub, c.flipY, c.top, c.bottom, t
exture, c.font); |
| 199 // for the first 2 iterations always make a new texture. |
| 200 if (count > 2) { |
| 201 texture = undefined; |
| 202 } |
| 203 ++caseNdx; |
| 204 if (caseNdx == cases.length) { |
| 205 caseNdx = 0; |
| 206 --count; |
| 207 if (!count) { |
| 208 glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors"); |
| 209 finishTest(); |
| 210 return; |
| 211 } |
| 212 } |
| 213 wtu.waitForComposite(gl, runNextTest); |
| 214 } |
| 215 runNextTest(); |
| 216 } |
| 217 |
| 218 return init; |
| 219 } |
OLD | NEW |