Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / canvas / webgl / resources / tex-image-and-sub-image-2d-with-image-data.js
blobfef69366bc60635a08beafe04c9330b26cc54886
1 function generateTest(pixelFormat, pixelType, prologue) {
2 var wtu = WebGLTestUtils;
3 var gl = null;
4 var textureLoc = null;
5 var successfullyParsed = false;
6 var imageData = null;
8 var init = function()
10 if (window.initNonKhronosFramework) {
11 window.initNonKhronosFramework(true);
14 description('Verify texImage2D and texSubImage2D code paths taking ImageData (' + pixelFormat + '/' + pixelType + ')');
16 gl = wtu.create3DContext("example");
18 if (!prologue(gl)) {
19 finishTest();
20 return;
23 var program = wtu.setupTexturedQuad(gl);
24 gl.clearColor(0,0,0,1);
25 gl.clearDepth(1);
26 gl.disable(gl.BLEND);
28 textureLoc = gl.getUniformLocation(program, "tex");
30 var canvas2d = document.getElementById("texcanvas");
31 var context2d = canvas2d.getContext("2d");
32 imageData = context2d.createImageData(1, 2);
33 var data = imageData.data;
34 data[0] = 255;
35 data[1] = 0;
36 data[2] = 0;
37 data[3] = 255;
38 data[4] = 0;
39 data[5] = 255;
40 data[6] = 0;
41 data[7] = 0;
43 runTest();
46 function runOneIteration(useTexSubImage2D, flipY, premultiplyAlpha, topColor, bottomColor)
48 debug('Testing ' + (useTexSubImage2D ? 'texSubImage2D' : 'texImage2D') +
49 ' with flipY=' + flipY + ' and premultiplyAlpha=' + premultiplyAlpha);
50 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
51 // Enable writes to the RGBA channels
52 gl.colorMask(1, 1, 1, 0);
53 var texture = gl.createTexture();
54 // Bind the texture to texture unit 0
55 gl.bindTexture(gl.TEXTURE_2D, texture);
56 // Set up texture parameters
57 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
58 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
59 // Set up pixel store parameters
60 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY);
61 gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, premultiplyAlpha);
62 // Upload the image into the texture
63 if (useTexSubImage2D) {
64 // Initialize the texture to black first
65 gl.texImage2D(gl.TEXTURE_2D, 0, gl[pixelFormat], 1, 2, 0,
66 gl[pixelFormat], gl[pixelType], null);
67 gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl[pixelFormat], gl[pixelType], imageData);
68 } else {
69 gl.texImage2D(gl.TEXTURE_2D, 0, gl[pixelFormat], gl[pixelFormat], gl[pixelType], imageData);
72 // Point the uniform sampler to texture unit 0
73 gl.uniform1i(textureLoc, 0);
74 // Draw the triangles
75 wtu.drawQuad(gl, [0, 0, 0, 255]);
77 // Check the top pixel and bottom pixel and make sure they have
78 // the right color.
79 debug("Checking bottom pixel");
80 wtu.checkCanvasRect(gl, 0, 0, 1, 1, bottomColor, "shouldBe " + bottomColor);
81 debug("Checking top pixel");
82 wtu.checkCanvasRect(gl, 0, 1, 1, 1, topColor, "shouldBe " + topColor);
85 function runTest()
87 var red = [255, 0, 0, 255];
88 var green = [0, 255, 0, 255];
89 var redPremultiplyAlpha = [255, 0, 0, 255];
90 var greenPremultiplyAlpha = [0, 0, 0, 255];
92 runOneIteration(false, true, false,
93 red, green);
94 runOneIteration(false, false, false,
95 green, red);
96 runOneIteration(false, true, true,
97 redPremultiplyAlpha, greenPremultiplyAlpha);
98 runOneIteration(false, false, true,
99 greenPremultiplyAlpha, redPremultiplyAlpha);
100 runOneIteration(true, true, false,
101 red, green);
102 runOneIteration(true, false, false,
103 green, red);
104 runOneIteration(true, true, true,
105 redPremultiplyAlpha, greenPremultiplyAlpha);
106 runOneIteration(true, false, true,
107 greenPremultiplyAlpha, redPremultiplyAlpha);
109 glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
110 finishTest();
113 return init;