Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / canvas / webgl / resources / tex-image-and-sub-image-2d-with-canvas.js
blob47bc09851adcce8be3ed7e047393333a30cecac6
1 function generateTest(pixelFormat, pixelType, prologue) {
2 var wtu = WebGLTestUtils;
3 var gl = null;
4 var textureLoc = null;
5 var successfullyParsed = false;
7 var init = function()
9 if (window.initNonKhronosFramework) {
10 window.initNonKhronosFramework(true);
13 description('Verify texImage2D and texSubImage2D code paths taking canvas elements (' + pixelFormat + '/' + pixelType + ')');
15 gl = wtu.create3DContext("example");
17 if (!prologue(gl)) {
18 finishTest();
19 return;
22 var program = wtu.setupTexturedQuad(gl);
24 gl.clearColor(0,0,0,1);
25 gl.clearDepth(1);
27 var testCanvas = document.createElement('canvas');
28 testCanvas.width = 1;
29 testCanvas.height = 2;
30 var ctx = testCanvas.getContext("2d");
31 ctx.fillStyle = "#ff0000";
32 ctx.fillRect(0,0,1,1);
33 ctx.fillStyle = "#00ff00";
34 ctx.fillRect(0,1,1,1);
35 runTest(testCanvas);
38 function runOneIteration(image, useTexSubImage2D, flipY, topColor, bottomColor)
40 debug('Testing ' + (useTexSubImage2D ? 'texSubImage2D' : 'texImage2D') +
41 ' with flipY=' + flipY);
42 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
43 // Disable any writes to the alpha channel
44 gl.colorMask(1, 1, 1, 0);
45 var texture = gl.createTexture();
46 // Bind the texture to texture unit 0
47 gl.bindTexture(gl.TEXTURE_2D, texture);
48 // Set up texture parameters
49 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
50 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
51 // Set up pixel store parameters
52 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY);
53 gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
54 gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE);
55 // Upload the image into the texture
56 if (useTexSubImage2D) {
57 // Initialize the texture to black first
58 gl.texImage2D(gl.TEXTURE_2D, 0, gl[pixelFormat], image.width, image.height, 0,
59 gl[pixelFormat], gl[pixelType], null);
60 gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl[pixelFormat], gl[pixelType], image);
61 } else {
62 gl.texImage2D(gl.TEXTURE_2D, 0, gl[pixelFormat], gl[pixelFormat], gl[pixelType], image);
65 // Point the uniform sampler to texture unit 0
66 gl.uniform1i(textureLoc, 0);
67 // Draw the triangles
68 wtu.drawQuad(gl, [0, 0, 0, 255]);
69 // Check a few pixels near the top and bottom and make sure they have
70 // the right color.
71 debug("Checking lower left corner");
72 wtu.checkCanvasRect(gl, 4, 4, 2, 2, bottomColor,
73 "shouldBe " + bottomColor);
74 debug("Checking upper left corner");
75 wtu.checkCanvasRect(gl, 4, gl.canvas.height - 8, 2, 2, topColor,
76 "shouldBe " + topColor);
79 function runTest(image)
81 var red = [255, 0, 0];
82 var green = [0, 255, 0];
83 runOneIteration(image, false, true, red, green);
84 runOneIteration(image, false, false, green, red);
85 runOneIteration(image, true, true, red, green);
86 runOneIteration(image, true, false, green, red);
88 glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
89 finishTest();
92 return init;