Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / canvas / webgl / resources / tex-image-and-sub-image-2d-with-svg-image.js
blob7ebd339e24c87d521ce740ebaa0824b235a1b879
1 function generateTest(pixelFormat, pixelType, pathToTestRoot, prologue) {
2 var wtu = WebGLTestUtils;
3 var gl = null;
4 var textureLoc = null;
5 var successfullyParsed = false;
6 var imgCanvas;
7 var black = [0, 0, 0];
8 var green = [0, 255, 0];
9 var image = new Image();
10 var imageLoaded = false;
11 var poisonImage = new Image();
12 var poisonImageLoaded = false;
14 var init = function()
16 if (window.initNonKhronosFramework) {
17 window.initNonKhronosFramework(true);
20 description('Verify texImage2D and texSubImage2D code paths taking SVG image elements');
22 gl = wtu.create3DContext("example");
24 if (!prologue(gl)) {
25 finishTest();
26 return;
29 var program = wtu.setupTexturedQuad(gl);
31 gl.clearColor(0,0,0,1);
32 gl.clearDepth(1);
34 textureLoc = gl.getUniformLocation(program, "tex");
36 image.width = 10;
37 image.height = 10;
38 image.onload = function () {
39 imageLoaded = true;
40 runTest();
42 image.src = pathToTestRoot + "/resources/transparent-green.svg";
44 poisonImage.width = 10;
45 poisonImage.height = 10;
46 poisonImage.onload = function () {
47 poisonImageLoaded = true;
48 runTest();
50 poisonImage.src = pathToTestRoot + "/resources/red-green.svg";
54 function runOneIteration(useTexSubImage2D, flipY, topColor, bottomColor)
56 debug('Testing ' + (useTexSubImage2D ? 'texSubImage2D' : 'texImage2D') +
57 ' with flipY=' + flipY);
58 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
59 var texture = gl.createTexture();
60 // Bind the texture to texture unit 0.
61 gl.bindTexture(gl.TEXTURE_2D, texture);
62 // Set up texture parameters.
63 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
64 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
65 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
66 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
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], poisonImage);
77 gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl[pixelFormat], gl[pixelType], image);
78 } else {
79 gl.texImage2D(gl.TEXTURE_2D, 0, gl[pixelFormat], gl[pixelFormat], gl[pixelType], poisonImage);
80 gl.texImage2D(gl.TEXTURE_2D, 0, gl[pixelFormat], gl[pixelFormat], gl[pixelType], image);
83 // Point the uniform sampler to texture unit 0.
84 gl.uniform1i(textureLoc, 0);
85 // Draw the triangles.
86 wtu.drawQuad(gl, [0, 255, 0, 255]);
87 // Check a few pixels near the top and bottom and make sure they have
88 // the right color.
89 debug("Checking lower left corner");
90 wtu.checkCanvasRect(gl, 4, 4, 2, 2, bottomColor,
91 "shouldBe " + bottomColor);
92 debug("Checking upper left corner");
93 wtu.checkCanvasRect(gl, 4, gl.canvas.height - 8, 2, 2, topColor,
94 "shouldBe " + topColor);
97 function runTest()
99 if (!imageLoaded || !poisonImageLoaded)
100 return;
102 runOneIteration(false, true, black, green);
103 runOneIteration(false, false, green, black);
104 runOneIteration(true, true, black, green);
105 runOneIteration(true, false, green, black);
106 finishTest();
109 return init;