|
1 <!-- |
|
2 Copyright (c) 2011 The Chromium Authors. All rights reserved. |
|
3 Use of this source code is governed by a BSD-style license that can be |
|
4 found in the LICENSE file. |
|
5 --> |
|
6 <!DOCTYPE html> |
|
7 <html> |
|
8 <head> |
|
9 <meta charset="utf-8"> |
|
10 <link rel="stylesheet" href="../../resources/js-test-style.css"/> |
|
11 <script src="../../resources/js-test-pre.js"></script> |
|
12 <script src="../resources/webgl-test.js"></script> |
|
13 <script src="../resources/webgl-test-utils.js"></script> |
|
14 <script> |
|
15 var wtu = WebGLTestUtils; |
|
16 var gl = null; |
|
17 var textureLoc = null; |
|
18 var successfullyParsed = false; |
|
19 var imgCanvas; |
|
20 var red = [255, 0, 0]; |
|
21 var green = [0, 255, 0]; |
|
22 |
|
23 function init() |
|
24 { |
|
25 if (window.initNonKhronosFramework) { |
|
26 window.initNonKhronosFramework(true); |
|
27 } |
|
28 |
|
29 description('Verify texImage2D and texSubImage2D code paths taking Images'); |
|
30 |
|
31 gl = wtu.create3DContext("example"); |
|
32 var program = wtu.setupTexturedQuad(gl); |
|
33 |
|
34 gl.clearColor(0,0,0,1); |
|
35 gl.clearDepth(1); |
|
36 |
|
37 textureLoc = gl.getUniformLocation(program, "tex"); |
|
38 |
|
39 wtu.loadTexture(gl, "../resources/red-green.png", runTest); |
|
40 } |
|
41 |
|
42 // These two declarations need to be global for "shouldBe" to see them |
|
43 var buf = null; |
|
44 var idx = 0; |
|
45 var pixel = [0, 0, 0]; |
|
46 var correctColor = null; |
|
47 |
|
48 function runOneIteration(image, useTexSubImage2D, flipY, topColor, bottomColor) |
|
49 { |
|
50 debug('Testing ' + (useTexSubImage2D ? 'texSubImage2D' : 'texImage2D') + |
|
51 ' with flipY=' + flipY); |
|
52 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); |
|
53 // Disable any writes to the alpha channel |
|
54 gl.colorMask(1, 1, 1, 0); |
|
55 var texture = gl.createTexture(); |
|
56 // Bind the texture to texture unit 0 |
|
57 gl.bindTexture(gl.TEXTURE_2D, texture); |
|
58 // Set up texture parameters |
|
59 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); |
|
60 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); |
|
61 // Set up pixel store parameters |
|
62 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY); |
|
63 gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false); |
|
64 gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE); |
|
65 // Upload the image into the texture |
|
66 if (useTexSubImage2D) { |
|
67 // Initialize the texture to black first |
|
68 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, image.width, image.height, 0, |
|
69 gl.RGBA, gl.UNSIGNED_BYTE, null); |
|
70 gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl.RGBA, gl.UNSIGNED_BYTE, image); |
|
71 } else { |
|
72 gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, image); |
|
73 } |
|
74 |
|
75 // Point the uniform sampler to texture unit 0 |
|
76 gl.uniform1i(textureLoc, 0); |
|
77 // Draw the triangles |
|
78 wtu.drawQuad(gl, [0, 0, 0, 255]); |
|
79 // Check a few pixels near the top and bottom and make sure they have |
|
80 // the right color. |
|
81 debug("Checking lower left corner"); |
|
82 wtu.checkCanvasRect(gl, 4, 4, 2, 2, bottomColor, |
|
83 "shouldBe " + bottomColor); |
|
84 debug("Checking upper left corner"); |
|
85 wtu.checkCanvasRect(gl, 4, gl.canvas.height - 8, 2, 2, topColor, |
|
86 "shouldBe " + topColor); |
|
87 } |
|
88 |
|
89 function runTestOnImage(image) { |
|
90 runOneIteration(image, false, true, red, green); |
|
91 runOneIteration(image, false, false, green, red); |
|
92 runOneIteration(image, true, true, red, green); |
|
93 runOneIteration(image, true, false, green, red); |
|
94 } |
|
95 |
|
96 function runTest(image) |
|
97 { |
|
98 runTestOnImage(image); |
|
99 |
|
100 imgCanvas = document.createElement("canvas"); |
|
101 imgCanvas.width = 1; |
|
102 imgCanvas.height = 2; |
|
103 var imgCtx = imgCanvas.getContext("2d"); |
|
104 var imgData = imgCtx.createImageData(1, 2); |
|
105 imgData.data[0] = red[0]; |
|
106 imgData.data[1] = red[1]; |
|
107 imgData.data[2] = red[2]; |
|
108 imgData.data[3] = 255; |
|
109 imgData.data[4] = green[0]; |
|
110 imgData.data[5] = green[1]; |
|
111 imgData.data[6] = green[2]; |
|
112 imgData.data[7] = 255; |
|
113 imgCtx.putImageData(imgData, 0, 0); |
|
114 |
|
115 // apparently Image is different than <img>. |
|
116 var newImage = new Image(); |
|
117 newImage.onload = function() { |
|
118 runTest2(newImage); |
|
119 }; |
|
120 newImage.src = imgCanvas.toDataURL(); |
|
121 } |
|
122 |
|
123 function runTest2(image) { |
|
124 runTestOnImage(image); |
|
125 |
|
126 var newImage = document.createElement("img"); |
|
127 newImage.onload = function() { |
|
128 runTest3(newImage); |
|
129 }; |
|
130 newImage.src = imgCanvas.toDataURL(); |
|
131 } |
|
132 |
|
133 function runTest3(image) { |
|
134 runTestOnImage(image); |
|
135 |
|
136 glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors"); |
|
137 finishTest(); |
|
138 } |
|
139 |
|
140 </script> |
|
141 </head> |
|
142 <body onload="init()"> |
|
143 <canvas id="example" width="32px" height="32px"></canvas> |
|
144 <div id="description"></div> |
|
145 <div id="console"></div> |
|
146 </body> |
|
147 </html> |
|
148 |
|
149 |