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.js
blobe7ff8b649e5f5f5cc59da2e0a1a533a38fc17df3
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 red = [255, 0, 0];
8 var green = [0, 255, 0];
10 var init = function()
12 if (window.initNonKhronosFramework) {
13 window.initNonKhronosFramework(true);
16 description('Verify texImage2D and texSubImage2D code paths taking image elements (' + pixelFormat + '/' + pixelType + ')');
18 gl = wtu.create3DContext("example");
20 if (!prologue(gl)) {
21 finishTest();
22 return;
25 var program = wtu.setupTexturedQuad(gl);
27 gl.clearColor(0,0,0,1);
28 gl.clearDepth(1);
30 textureLoc = gl.getUniformLocation(program, "tex");
32 wtu.loadTexture(gl, pathToTestRoot + "/resources/red-green.png", runTest);
35 function runOneIteration(image, useTexSubImage2D, flipY, topColor, bottomColor)
37 debug('Testing ' + (useTexSubImage2D ? 'texSubImage2D' : 'texImage2D') +
38 ' with flipY=' + flipY);
39 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
40 // Disable any writes to the alpha channel
41 gl.colorMask(1, 1, 1, 0);
42 var texture = gl.createTexture();
43 // Bind the texture to texture unit 0
44 gl.bindTexture(gl.TEXTURE_2D, texture);
45 // Set up texture parameters
46 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
47 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
48 // Set up pixel store parameters
49 gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, flipY);
50 gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false);
51 gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE);
52 // Upload the image into the texture
53 if (useTexSubImage2D) {
54 // Initialize the texture to black first
55 gl.texImage2D(gl.TEXTURE_2D, 0, gl[pixelFormat], image.width, image.height, 0,
56 gl[pixelFormat], gl[pixelType], null);
57 gl.texSubImage2D(gl.TEXTURE_2D, 0, 0, 0, gl[pixelFormat], gl[pixelType], image);
58 } else {
59 gl.texImage2D(gl.TEXTURE_2D, 0, gl[pixelFormat], gl[pixelFormat], gl[pixelType], image);
62 // Point the uniform sampler to texture unit 0
63 gl.uniform1i(textureLoc, 0);
64 // Draw the triangles
65 wtu.drawQuad(gl, [0, 0, 0, 255]);
66 // Check a few pixels near the top and bottom and make sure they have
67 // the right color.
68 debug("Checking lower left corner");
69 wtu.checkCanvasRect(gl, 4, 4, 2, 2, bottomColor,
70 "shouldBe " + bottomColor);
71 debug("Checking upper left corner");
72 wtu.checkCanvasRect(gl, 4, gl.canvas.height - 8, 2, 2, topColor,
73 "shouldBe " + topColor);
76 function runTestOnImage(image) {
77 runOneIteration(image, false, true, red, green);
78 runOneIteration(image, false, false, green, red);
79 runOneIteration(image, true, true, red, green);
80 runOneIteration(image, true, false, green, red);
83 function runTest(image)
85 runTestOnImage(image);
87 imgCanvas = document.createElement("canvas");
88 imgCanvas.width = 1;
89 imgCanvas.height = 2;
90 var imgCtx = imgCanvas.getContext("2d");
91 var imgData = imgCtx.createImageData(1, 2);
92 imgData.data[0] = red[0];
93 imgData.data[1] = red[1];
94 imgData.data[2] = red[2];
95 imgData.data[3] = 255;
96 imgData.data[4] = green[0];
97 imgData.data[5] = green[1];
98 imgData.data[6] = green[2];
99 imgData.data[7] = 255;
100 imgCtx.putImageData(imgData, 0, 0);
102 // apparently Image is different than <img>.
103 var newImage = new Image();
104 newImage.onload = function() {
105 runTest2(newImage);
107 newImage.src = imgCanvas.toDataURL();
110 function runTest2(image) {
111 runTestOnImage(image);
113 var newImage = document.createElement("img");
114 newImage.onload = function() {
115 runTest3(newImage);
117 newImage.src = imgCanvas.toDataURL();
120 function runTest3(image) {
121 runTestOnImage(image);
123 glErrorShouldBe(gl, gl.NO_ERROR, "should be no errors");
124 finishTest();
127 return init;