offscreencanvas-transfer-image-bitmap.js (2203B)
1 function testTransferToImageBitmap(webglContextVersion, bitmap) { 2 var internalFormat = "RGBA"; 3 var pixelFormat = "RGBA"; 4 var pixelType = "UNSIGNED_BYTE"; 5 6 var width = 32; 7 var height = 32; 8 var canvas = document.createElement("canvas"); 9 canvas.width = width; 10 canvas.height = height; 11 var gl = WebGLTestUtils.create3DContext(canvas); 12 gl.clearColor(0,0,0,1); 13 gl.clearDepth(1); 14 gl.disable(gl.BLEND); 15 16 TexImageUtils.setupTexturedQuad(gl, internalFormat); 17 18 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); 19 // Enable writes to the RGBA channels 20 gl.colorMask(1, 1, 1, 0); 21 var texture = gl.createTexture(); 22 // Bind the texture to texture unit 0 23 gl.bindTexture(gl.TEXTURE_2D, texture); 24 // Set up texture parameters 25 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST); 26 gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST); 27 28 var targets = [gl.TEXTURE_2D]; 29 // Upload the image into the texture 30 for (var tt = 0; tt < targets.length; ++tt) { 31 gl.texImage2D(targets[tt], 0, gl[internalFormat], gl[pixelFormat], gl[pixelType], bitmap); 32 } 33 for (var tt = 0; tt < targets.length; ++tt) { 34 // Draw the triangles 35 gl.clearColor(0, 0, 0, 1); 36 gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT); 37 gl.drawArrays(gl.TRIANGLES, 0, 6); 38 39 var buf = new Uint8Array(width * height * 4); 40 gl.readPixels(0, 0, width, height, gl.RGBA, gl.UNSIGNED_BYTE, buf); 41 _checkCanvas(buf, width, height, webglContextVersion); 42 } 43 } 44 45 function _checkCanvas(buf, width, height, webglContextVersion) 46 { 47 for (var i = 0; i < width * height; i++) { 48 if (buf[i * 4] != 255 || buf[i * 4 + 1] != 255 || 49 buf[i * 4 + 2] != 0 || buf[i * 4 + 3] != 255) { 50 testFailed("OffscreenCanvas." + webglContextVersion + 51 ": This pixel should be [255, 255, 0, 255], but it is: [" + buf[i * 4] + ", " + 52 buf[i * 4 + 1] + ", " + buf[i * 4 + 2] + ", " + buf[i * 4 + 3] + "]."); 53 return; 54 } 55 } 56 testPassed("TransferToImageBitmap test on OffscreenCanvas." + webglContextVersion + " passed"); 57 }