2 Copyright (c) 2019 The Khronos Group Inc.
3 Use of this source code is governed by an MIT-style license that can be
4 found in the LICENSE.txt file.
10 <meta charset=
"utf-8">
11 <title>TexImage2D of
2D Canvas after multiple drawImages
</title>
12 <link rel=
"stylesheet" href=
"../../../resources/js-test-style.css"/>
13 <script src=
"../../../js/js-test-pre.js"></script>
14 <script src=
"../../../js/webgl-test-utils.js"></script>
17 const wtu
= WebGLTestUtils
;
20 let greenImage
= null;
22 function loadImageAndStart(startFunc
) {
23 let c
= document
.createElement('canvas');
26 let ctx
= c
.getContext('2d');
27 ctx
.fillStyle
= 'rgb(0,255,0)';
28 ctx
.fillRect(0, 0, sz
, sz
);
29 greenImage
= wtu
.makeImageFromCanvas(c
, startFunc
);
34 debug('Regression test for <a href="http://crbug.com/878545">http://crbug.com/878545</a>');
35 // Mimics the image-to-texture upload path from the old JavaScript
36 // port of O3D, salvaged at https://github.com/petersont/o3d .
38 // Create a canvas and draw the entire image into it.
39 let bigCanvas
= document
.createElement('canvas');
40 bigCanvas
.width
= greenImage
.naturalWidth
;
41 bigCanvas
.height
= greenImage
.naturalHeight
;
42 let bc
= bigCanvas
.getContext('2d');
43 bc
.drawImage(greenImage
, 0, 0, bigCanvas
.width
, bigCanvas
.height
);
44 // Create a temp canvas to flip vertically.
45 let tempCanvas
= document
.createElement('canvas');
46 tempCanvas
.width
= bigCanvas
.width
;
47 tempCanvas
.height
= bigCanvas
.height
;
48 let tc
= tempCanvas
.getContext('2d');
49 tc
.translate(0, tempCanvas
.height
);
51 tc
.drawImage(bigCanvas
, 0, 0, tempCanvas
.width
, tempCanvas
.height
);
52 // Set up to draw via WebGL.
53 let c3d
= document
.getElementById('canvas');
54 let gl
= wtu
.create3DContext(c3d
);
55 let prog
= wtu
.setupTexturedQuad(gl
);
56 gl
.uniform1i(gl
.getUniformLocation(prog
, 'tex'), 0);
57 let tex
= gl
.createTexture();
58 // Iterate through the large canvas, drawing pieces of it to a
60 let scratchCanvas
= document
.createElement('canvas');
61 const width
= tempCanvas
.width
/ 2;
62 const height
= tempCanvas
.height
/ 2;
63 let bb
= new Uint8Array(4 * c3d
.width
* c3d
.height
);
64 for (let jj
= 0; jj
< 2; ++jj
) {
65 for (let ii
= 0; ii
< 2; ++ii
) {
67 scratchCanvas
.width
= width
;
68 scratchCanvas
.height
= height
;
69 let ctx
= scratchCanvas
.getContext('2d');
71 ctx
.translate(-(ii
* width
), -(jj
* height
));
73 ctx
.drawImage(tempCanvas
, 0, 0, tempCanvas
.width
, tempCanvas
.height
);
74 gl
.bindTexture(gl
.TEXTURE_2D
, tex
);
75 gl
.texImage2D(gl
.TEXTURE_2D
, 0, gl
.RGBA
, gl
.RGBA
, gl
.UNSIGNED_BYTE
,
78 gl
.generateMipmap(gl
.TEXTURE_2D
);
79 // Draw and check that rendering occurred properly.
80 gl
.uniform1i(gl
.getUniformLocation(prog
, 'tex'), 0);
83 wtu
.checkCanvasRect(gl
, 1, 1, c3d
.width
- 2, c3d
.height
- 2,
95 <body onload=
"loadImageAndStart(runTest)">
96 <div id=
"description"></div>
97 <div id=
"console"></div>
98 <canvas id=
"canvas" width=
"40" height=
"40"></canvas>