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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
|
<!--
/*
** Copyright (c) 2012 The Khronos Group Inc.
**
** Permission is hereby granted, free of charge, to any person obtaining a
** copy of this software and/or associated documentation files (the
** "Materials"), to deal in the Materials without restriction, including
** without limitation the rights to use, copy, modify, merge, publish,
** distribute, sublicense, and/or sell copies of the Materials, and to
** permit persons to whom the Materials are furnished to do so, subject to
** the following conditions:
**
** The above copyright notice and this permission notice shall be included
** in all copies or substantial portions of the Materials.
**
** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
*/
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Test the WebGL premultipliedAlpha context creation flag.</title>
<link rel="stylesheet" href="../../resources/js-test-style.css"/>
<script src="../../js/js-test-pre.js"></script>
<script src="../../js/webgl-test-utils.js"> </script>
</head>
<body>
<div id="description"></div><div id="console"></div>
<script>
"use strict";
var wtu = WebGLTestUtils;
// wtu.create3DContext(...) will set antialias to false by default
// if the antialias property is not set to true explicitly.
// To cover the antialias case, it needs to set antialias to true
// explicitly.
var tests = [
// If premultipliedAlpha is true and antialias is false then
// [texture] [canvas] [dataURL]
// 32, 64, 128, 128 -> 64, 128, 255, 128 -> 64, 128, 255, 128
{ creationAttributes: {},
sentColor: [32, 64, 128, 128],
expectedColor: [64, 128, 255, 128],
errorRange: 2,
imageFormat: "image/png"
},
// If premultipliedAlpha is true and antialias is true then
// [texture] [canvas] [dataURL]
// 32, 64, 128, 128 -> 64, 128, 255, 128 -> 64, 128, 255, 128
{ creationAttributes: {antialias: true},
sentColor: [32, 64, 128, 128],
expectedColor: [64, 128, 255, 128],
errorRange: 2,
imageFormat: "image/png"
},
// If premultipliedAlpha is true and antialias is false then
// [texture] [canvas] [texture]
// 32, 64, 128, 128 -> 64, 128, 255, 128 -> 64, 128, 255, 128
{ creationAttributes: {},
sentColor: [32, 64, 128, 128],
expectedColor: [64, 128, 255, 128],
errorRange: 2,
},
// If premultipliedAlpha is true and antialias is true then
// [texture] [canvas] [texture]
// 32, 64, 128, 128 -> 64, 128, 255, 128 -> 64, 128, 255, 128
{ creationAttributes: {antialias: true},
sentColor: [32, 64, 128, 128],
expectedColor: [64, 128, 255, 128],
errorRange: 2,
},
// If premultipliedAlpha is false and antialias is false then
// [texture] [canvas] [dataURL]
// 255, 192, 128, 1 -> 255, 192, 128, 1 -> 255, 192, 128, 1
{ creationAttributes: {premultipliedAlpha: false},
sentColor: [255, 192, 128, 1],
expectedColor: [255, 192, 128, 1],
errorRange: 0,
imageFormat: "image/png"
},
// If premultipliedAlpha is false and antialias is true then
// [texture] [canvas] [dataURL]
// 255, 192, 128, 1 -> 255, 192, 128, 1 -> 255, 192, 128, 1
{ creationAttributes: {premultipliedAlpha: false, antialias: true},
sentColor: [255, 192, 128, 1],
expectedColor: [255, 192, 128, 1],
errorRange: 0,
imageFormat: "image/png"
},
// If premultipliedAlpha is false and antialias is false then
// [texture] [canvas] [texture]
// 255, 192, 128, 1 -> 255, 192, 128, 1 -> 255, 192, 128, 1
{ creationAttributes: {premultipliedAlpha: false},
sentColor: [255, 192, 128, 1],
expectedColor: [255, 192, 128, 1],
errorRange: 0,
},
// If premultipliedAlpha is false and antialias is true then
// [texture] [canvas] [texture]
// 255, 192, 128, 1 -> 255, 192, 128, 1 -> 255, 192, 128, 1
{ creationAttributes: {premultipliedAlpha: false, antialias: true},
sentColor: [255, 192, 128, 1],
expectedColor: [255, 192, 128, 1],
errorRange: 0,
},
// If premultipliedAlpha is false and antialias is false then
// [texture] [canvas] [dataURL]
// 255, 255, 255, 128 -> 255, 255, 255, 128 -> 128, 128, 128, 255
{ creationAttributes: {premultipliedAlpha: false},
sentColor: [255, 255, 255, 128],
expectedColor: [128, 128, 128, 255],
errorRange: 2,
imageFormat: "image/jpeg"
},
// If premultipliedAlpha is false and antialias is true then
// [texture] [canvas] [dataURL]
// 255, 255, 255, 128 -> 255, 255, 255, 128 -> 128, 128, 128, 255
{ creationAttributes: {premultipliedAlpha: false, antialias: true},
sentColor: [255, 255, 255, 128],
expectedColor: [128, 128, 128, 255],
errorRange: 2,
imageFormat: "image/jpeg"
},
// If premultipliedAlpha is true and antialias is false then
// [texture] [canvas] [dataURL]
// 128, 128, 128, 128 -> 255, 255, 255, 128 -> 128, 128, 128, 255
{ creationAttributes: {},
sentColor: [128, 128, 128, 128],
expectedColor: [128, 128, 128, 255],
errorRange: 2,
imageFormat: "image/jpeg"
},
// If premultipliedAlpha is true and antialias is true then
// [texture] [canvas] [dataURL]
// 128, 128, 128, 128 -> 255, 255, 255, 128 -> 128, 128, 128, 255
{ creationAttributes: {antialias: true},
sentColor: [128, 128, 128, 128],
expectedColor: [128, 128, 128, 255],
errorRange: 2,
imageFormat: "image/jpeg"
}
];
var g_count = 0;
var gl;
var canvas;
var premultipliedAlpha;
enableJSTestPreVerboseLogging();
description("Test the WebGL premultipliedAlpha context creation flag.");
doNextTest();
function doNextTest() {
if (g_count < tests.length) {
var test = tests[g_count++];
canvas = document.createElement("canvas");
// Need to preserve drawing buffer to load it in a callback
test.creationAttributes.preserveDrawingBuffer = true;
gl = wtu.create3DContext(canvas, test.creationAttributes);
var premultipliedAlpha = test.creationAttributes.premultipliedAlpha != false;
var antialias = test.creationAttributes.antialias == true;
debug("")
debug("testing: premultipliedAlpha: " + premultipliedAlpha
+ ", antialias: " + antialias
+ ", imageFormat: " + test.imageFormat);
shouldBe('gl.getContextAttributes().premultipliedAlpha', premultipliedAlpha.toString());
shouldBeTrue('gl.getContextAttributes().preserveDrawingBuffer');
wtu.log(gl.getContextAttributes());
var program = wtu.setupTexturedQuad(gl);
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");
var tex = gl.createTexture();
wtu.fillTexture(gl, tex, 2, 2, test.sentColor, 0);
var loc = gl.getUniformLocation(program, "tex");
gl.uniform1i(loc, 0);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
wtu.clearAndDrawUnitQuad(gl);
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from drawing.");
var loadTexture = function() {
debug("loadTexture called");
var pngTex = gl.createTexture();
// not needed as it's the default
// gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
wtu.failIfGLError(gl, 'gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE);');
gl.bindTexture(gl.TEXTURE_2D, pngTex);
if (test.imageFormat) {
// create texture from image
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, this);
} else {
// create texture from canvas
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas);
}
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from creating copy.");
wtu.clearAndDrawUnitQuad(gl);
wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from 2nd drawing.");
wtu.checkCanvas(
gl, test.expectedColor,
"should draw with " + test.expectedColor, test.errorRange);
doNextTest();
}
var loadTextureError = function() {
testFailed("Creating image from canvas failed. Image src: " + this.src);
finishTest();
}
var shrinkString = function(string) {
if (string.length < 63) {
return string;
}
return string.substr(0, 30) + "..." + string.substr(string.length - 30);
}
if (test.imageFormat) {
// Load canvas into string using toDataURL
debug("Calling canvas.toDataURL('" + test.imageFormat + "')");
var imageUrl = canvas.toDataURL(test.imageFormat);
debug("imageUrl = '" + shrinkString(imageUrl) + "'");
if (test.imageFormat != "image/png" &&
(imageUrl.indexOf("data:image/png,") == 0 ||
imageUrl.indexOf("data:image/png;") == 0)) {
debug("Image format " + test.imageFormat + " not supported; skipping");
setTimeout(doNextTest, 0);
} else {
// Load string into the texture
debug("Waiting for image.onload");
var input = wtu.makeImage(imageUrl, loadTexture, loadTextureError);
}
} else {
// Load canvas into the texture asynchronously (to prevent unbounded stack consumption)
debug("Waiting for setTimeout");
setTimeout(loadTexture, 0);
}
} else {
var successfullyParsed = true;
finishTest();
}
}
</script>
</body>
</html>
|