5 <title>Test the WebGL premultipledAlpha context creation flag.
</title>
6 <script src=
"../../../resources/js-test.js"></script>
7 <script src=
"resources/webgl-test.js"> </script>
8 <script src=
"resources/webgl-test-utils.js"> </script>
11 <div id=
"description"></div><div id=
"console"></div>
13 var wtu
= WebGLTestUtils
;
16 // If premultipledAlpha is true then
17 // [texture] [canvas] [dataURL]
18 // 32, 64, 128, 128 -> 64, 128, 255, 128 -> 64, 128, 255, 128
19 { creationAttributes
: {},
20 sentColor
: [32, 64, 128, 128],
21 expectedColor
: [64, 128, 255, 128],
23 imageFormat
: "image/png"
25 // If premultipledAlpha is true then
26 // [texture] [canvas] [texture]
27 // 32, 64, 128, 128 -> 64, 128, 255, 128 -> 64, 128, 255, 128
28 { creationAttributes
: {},
29 sentColor
: [32, 64, 128, 128],
30 expectedColor
: [64, 128, 255, 128],
33 // If premultipledAlpha is false then
34 // [texture] [canvas] [dataURL]
35 // 255, 192, 128, 1 -> 255, 192, 128, 1 -> 255, 192, 128, 1
36 { creationAttributes
: {premultipliedAlpha
: false},
37 sentColor
: [255, 192, 128, 1],
38 expectedColor
: [255, 192, 128, 1],
40 imageFormat
: "image/png"
42 // If premultipledAlpha is false then
43 // [texture] [canvas] [texture]
44 // 255, 192, 128, 1 -> 255, 192, 128, 1 -> 255, 192, 128, 1
45 { creationAttributes
: {premultipliedAlpha
: false},
46 sentColor
: [255, 192, 128, 1],
47 expectedColor
: [255, 192, 128, 1],
50 // If premultipledAlpha is false then
51 // [texture] [canvas] [dataURL]
52 // 255, 255, 255, 128 -> 255, 255, 255, 128 -> 128, 128, 128, 255
53 { creationAttributes
: {premultipliedAlpha
: false},
54 sentColor
: [255, 255, 255, 128],
55 expectedColor
: [128, 128, 128, 255],
57 imageFormat
: "image/jpeg"
59 // If premultipledAlpha is true then
60 // [texture] [canvas] [dataURL]
61 // 128, 128, 128, 128 -> 255, 255, 255, 128 -> 128, 128, 128, 255
62 { creationAttributes
: {},
63 sentColor
: [128, 128, 128, 128],
64 expectedColor
: [128, 128, 128, 255],
66 imageFormat
: "image/jpeg"
73 var premultipledAlpha
;
75 window
.jsTestIsAsync
= true;
77 if (window
.testRunner
)
78 testRunner
.overridePreference("WebKitWebGLEnabled", "1");
80 description("Test the WebGL premultipledAlpha context creation flag.");
82 function doNextTest() {
83 if (g_count
< tests
.length
) {
84 var test
= tests
[g_count
++];
85 canvas
= document
.createElement("canvas");
86 // Need to preserve drawing buffer to load it in a callback
87 test
.creationAttributes
.preserveDrawingBuffer
= true;
88 gl
= wtu
.create3DContext(canvas
, test
.creationAttributes
);
89 var premultipliedAlpha
= test
.creationAttributes
.premultipliedAlpha
!= false;
91 debug("testing: premultipliedAlpha: " + premultipliedAlpha
+ " imageFormat: " + test
.imageFormat
);
93 shouldBe('gl.getContextAttributes().premultipledAlpha', 'premultipledAlpha');
94 shouldBeTrue('gl.getContextAttributes().preserveDrawingBuffer');
96 var program
= wtu
.setupTexturedQuad(gl
);
98 glErrorShouldBe(gl
, gl
.NO_ERROR
, "Should be no errors from setup.");
99 var tex
= gl
.createTexture();
100 wtu
.fillTexture(gl
, tex
, 2, 2, test
.sentColor
, 0);
101 var loc
= gl
.getUniformLocation(program
, "tex");
102 gl
.uniform1i(loc
, 0);
103 gl
.texParameteri(gl
.TEXTURE_2D
, gl
.TEXTURE_MIN_FILTER
, gl
.LINEAR
);
104 gl
.texParameteri(gl
.TEXTURE_2D
, gl
.TEXTURE_MAG_FILTER
, gl
.LINEAR
);
105 gl
.texParameteri(gl
.TEXTURE_2D
, gl
.TEXTURE_WRAP_S
, gl
.CLAMP_TO_EDGE
);
106 gl
.texParameteri(gl
.TEXTURE_2D
, gl
.TEXTURE_WRAP_T
, gl
.CLAMP_TO_EDGE
);
109 glErrorShouldBe(gl
, gl
.NO_ERROR
, "Should be no errors from drawing.");
111 function loadTexture() {
112 var pngTex
= gl
.createTexture();
113 // not needed as it's the default
114 // gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
115 gl
.pixelStorei(gl
.UNPACK_COLORSPACE_CONVERSION_WEBGL
, false);
116 gl
.bindTexture(gl
.TEXTURE_2D
, pngTex
);
117 if (test
.imageFormat
) {
118 // create texture from image
119 gl
.texImage2D(gl
.TEXTURE_2D
, 0, gl
.RGBA
, gl
.RGBA
, gl
.UNSIGNED_BYTE
, this);
121 // create texture from canvas
122 gl
.texImage2D(gl
.TEXTURE_2D
, 0, gl
.RGBA
, gl
.RGBA
, gl
.UNSIGNED_BYTE
, canvas
);
124 gl
.texParameteri(gl
.TEXTURE_2D
, gl
.TEXTURE_MIN_FILTER
, gl
.LINEAR
);
125 gl
.texParameteri(gl
.TEXTURE_2D
, gl
.TEXTURE_MAG_FILTER
, gl
.LINEAR
);
126 gl
.texParameteri(gl
.TEXTURE_2D
, gl
.TEXTURE_WRAP_S
, gl
.CLAMP_TO_EDGE
);
127 gl
.texParameteri(gl
.TEXTURE_2D
, gl
.TEXTURE_WRAP_T
, gl
.CLAMP_TO_EDGE
);
128 glErrorShouldBe(gl
, gl
.NO_ERROR
, "Should be no errors from creating copy.");
130 glErrorShouldBe(gl
, gl
.NO_ERROR
, "Should be no errors from 2nd drawing.");
132 gl
, test
.expectedColor
,
133 "should draw with " + test
.expectedColor
, test
.errorRange
);
138 if (test
.imageFormat
) {
139 // Load canvas into string using toDataURL
140 var imageUrl
= canvas
.toDataURL(test
.imageFormat
);
141 if (test
.imageFormat
!= "image/png" &&
142 (imageUrl
.indexOf("data:image/png,") == 0 ||
143 imageUrl
.indexOf("data:image/png;") == 0)) {
144 debug("Image format " + test
.imageFormat
+ " not supported; skipping");
145 setTimeout(doNextTest
, 0);
147 // Load string into the texture
148 var input
= document
.createElement("img");
149 input
.onload
= loadTexture
;
150 input
.src
= imageUrl
;
153 // Load canvas into the texture asynchronously (to prevent unbounded stack consumption)
154 setTimeout(loadTexture
, 0);