Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / webgl / src / sdk / tests / conformance / resources / tex-image-and-sub-image-2d-with-image.js
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, pathToTestRoot, prologue) {
25     var wtu = WebGLTestUtils;
26     var gl = null;
27     var textureLoc = null;
28     var successfullyParsed = false;
29     var imgCanvas;
30     var red = [255, 0, 0];
31     var green = [0, 255, 0];
32
33     var init = function()
34     {
35         description('Verify texImage2D and texSubImage2D code paths taking image elements (' + pixelFormat + '/' + pixelType + ')');
36
37         gl = wtu.create3DContext("example");
38
39         if (!prologue(gl)) {
40             finishTest();
41             return;
42         }
43
44         var program = wtu.setupTexturedQuad(gl);
45
46         gl.clearColor(0,0,0,1);
47         gl.clearDepth(1);
48
49         textureLoc = gl.getUniformLocation(program, "tex");
50
51         wtu.loadTexture(gl, pathToTestRoot + "/resources/red-green.png", runTest);
52     }
53
54     function runOneIteration(image, useTexSubImage2D, flipY, topColor, bottomColor)
55     {
56         debug('Testing ' + (useTexSubImage2D ? 'texSubImage2D' : 'texImage2D') +
57               ' with flipY=' + flipY);
58         gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
59         // Disable any writes to the alpha channel
60         gl.colorMask(1, 1, 1, 0);
61         var texture = gl.createTexture();
62         // Bind the texture to texture unit 0
63         gl.bindTexture(gl.TEXTURE_2D, texture);
64         // Set up texture parameters
65         gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
66         gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
67         // Set up pixel store parameters
68         gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY);
69         gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
70         gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE);
71         // Upload the image into the texture
72         if (useTexSubImage2D) {
73             // Initialize the texture to black first
74             gl.texImage2D(gl.TEXTURE_2D, 0, gl[pixelFormat], image.width, image.height, 0,
75                           gl[pixelFormat], gl[pixelType], null);
76             gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl[pixelFormat], gl[pixelType], image);
77         } else {
78             gl.texImage2D(gl.TEXTURE_2D, 0, gl[pixelFormat], gl[pixelFormat], gl[pixelType], image);
79         }
80
81         // Point the uniform sampler to texture unit 0
82         gl.uniform1i(textureLoc, 0);
83         // Draw the triangles
84         wtu.clearAndDrawUnitQuad(gl, [0, 0, 0, 255]);
85         // Check a few pixels near the top and bottom and make sure they have
86         // the right color.
87         debug("Checking lower left corner");
88         wtu.checkCanvasRect(gl, 4, 4, 2, 2, bottomColor,
89                             "shouldBe " + bottomColor);
90         debug("Checking upper left corner");
91         wtu.checkCanvasRect(gl, 4, gl.canvas.height - 8, 2, 2, topColor,
92                             "shouldBe " + topColor);
93     }
94
95     function runTestOnImage(image) {
96         runOneIteration(image, false, true, red, green);
97         runOneIteration(image, false, false, green, red);
98         runOneIteration(image, true, true, red, green);
99         runOneIteration(image, true, false, green, red);
100     }
101
102     function runTest(image)
103     {
104         runTestOnImage(image);
105
106         imgCanvas = document.createElement("canvas");
107         imgCanvas.width = 1;
108         imgCanvas.height = 2;
109         var imgCtx = imgCanvas.getContext("2d");
110         var imgData = imgCtx.createImageData(1, 2);
111         imgData.data[0] = red[0];
112         imgData.data[1] = red[1];
113         imgData.data[2] = red[2];
114         imgData.data[3] = 255;
115         imgData.data[4] = green[0];
116         imgData.data[5] = green[1];
117         imgData.data[6] = green[2];
118         imgData.data[7] = 255;
119         imgCtx.putImageData(imgData, 0, 0);
120
121         // apparently Image is different than <img>.
122         var newImage =  new Image();
123         newImage.onload = function() {
124             runTest2(newImage);
125         };
126         newImage.onerror = function() {
127             testFailed("Creating image from canvas failed. Image src: " + this.src);
128             finishTest();
129         };
130         newImage.src = imgCanvas.toDataURL();
131     }
132
133     function runTest2(image) {
134         runTestOnImage(image);
135
136         wtu.makeImageFromCanvas(imgCanvas, function() {
137             runTest3(this);
138         });
139     }
140
141     function runTest3(image) {
142         runTestOnImage(image);
143
144         wtu.glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
145         finishTest();
146     }
147
148     return init;
149 }