readPixelsBadArgs.html (4387B)
1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <!-- 6 Copyright (c) 2019 The Khronos Group Inc. 7 Use of this source code is governed by an MIT-style license that can be 8 found in the LICENSE.txt file. 9 --> 10 <link rel="stylesheet" type="text/css" href="../unit.css" /> 11 <script type="application/javascript" src="../unit.js"></script> 12 <script type="application/javascript" src="../util.js"></script> 13 <script type="application/javascript" src="../../../js/webgl-test-utils.js"></script> 14 </head><body> 15 <canvas id="gl" width="16" height="16"></canvas> 16 <canvas id="c" width="128" height="128"></canvas> 17 <img id="i"> 18 <script type="application/javascript"> 19 var wtu = WebGLTestUtils; 20 var defaultImgUrl = "https://get.webgl.org/conformance-resources/opengl_logo.jpg"; 21 var localImgUrl = "../../../resources/opengl_logo.jpg"; 22 23 Tests.autoinit = false; // Prevents the test from running until the image is loaded 24 25 Tests.startUnit = function () { 26 var canvas = document.getElementById('gl'); 27 var gl = wrapGLContext(getGLContext(canvas)); 28 return [gl]; 29 } 30 31 Tests.testReadPixels = function(gl) { 32 // we can't know if this is going to fail because of negative width 33 // or because the buffer size doesn't match the dimensions. 34 assertSomeGLError(gl, "negative width", 35 function(){gl.readPixels(0,0,-1,1, gl.RGBA, gl.UNSIGNED_BYTE, 36 new Uint8Array(4));}); 37 assertSomeGLError(gl, "negative height", 38 function(){gl.readPixels(0,0,1,-1, gl.RGBA, gl.UNSIGNED_BYTE, 39 new Uint8Array(4));}); 40 assertOk("negative x", 41 function(){gl.readPixels(-1,0,1,1, gl.RGBA, gl.UNSIGNED_BYTE, 42 new Uint8Array(4));}); 43 assertOk("negative y", 44 function(){gl.readPixels(0,-1,1,1, gl.RGBA, gl.UNSIGNED_BYTE, 45 new Uint8Array(4));}); 46 assertOk("height > backbuffer height", 47 function(){gl.readPixels(0,0,16,17, gl.RGBA, gl.UNSIGNED_BYTE, 48 new Uint8Array(16*17*4));}); 49 assertOk("width > backbuffer width", 50 function(){gl.readPixels(0,0,17,16, gl.RGBA, gl.UNSIGNED_BYTE, 51 new Uint8Array(17*16*4));}); 52 assertOk("width, height = 0", 53 function(){gl.readPixels(0,0,0,0, gl.RGBA, gl.UNSIGNED_BYTE, 54 new Uint8Array(0));}); 55 // we can't know if this is going to fail because of negative width 56 // or because the buffer size doesn't match the dimensions. 57 assertSomeGLError(gl, "bad format", 58 function(){gl.readPixels(0,0,1,1, gl.FLOAT, gl.UNSIGNED_BYTE, 59 new Uint8Array(4*4));}); 60 // we can't know if this is going to fail because of negative width 61 // or because the buffer size doesn't match the dimensions. 62 assertSomeGLError(gl, "bad type", 63 function(){gl.readPixels(0,0,1,1, gl.ALPHA, gl.FLOAT, 64 new Uint8Array(1*4));}); 65 } 66 67 Tests.testReadPixelsSOPIMG = function(gl) { 68 var img = document.getElementById("i"); 69 var tex = gl.createTexture(); 70 gl.bindTexture(gl.TEXTURE_2D, tex); 71 // SOP failure 72 assertThrowNoGLError(gl, "throw because img is from another domain", 73 function(){gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, img);}); 74 gl.bindTexture(gl.TEXTURE_2D, null); 75 assertOk("canvas still origin-clean", 76 function(){gl.readPixels(0,0,1,1, gl.RGBA, gl.UNSIGNED_BYTE, 77 new Uint8Array(4));}); 78 gl.deleteTexture(tex); 79 } 80 Tests.testReadPixelsSOPCanvas = function(gl) { 81 var img = document.getElementById("i"); 82 var c = document.getElementById("c"); 83 c.getContext("2d").drawImage(img, 0, 0); 84 assertFail("canvas throws because not origin clean", 85 function(){c.getContext("2d").getImageData(0,0,1,1);}); 86 var tex = gl.createTexture(); 87 gl.bindTexture(gl.TEXTURE_2D, tex); 88 // SOP failure 89 assertThrowNoGLError(gl, "throw because canvas is not origin clean", 90 function(){gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, c);}); 91 gl.bindTexture(gl.TEXTURE_2D, null); 92 assertOk("canvas still origin-clean", 93 function(){gl.readPixels(0,0,1,1, gl.RGBA, gl.UNSIGNED_BYTE, 94 new Uint8Array(4));}); 95 gl.deleteTexture(tex); 96 } 97 98 Tests.endUnit = function(gl) { 99 }; 100 101 (async function() { 102 const img = document.getElementById('i'); 103 try { 104 await wtu.awaitOrTimeout(wtu.loadCrossOriginImage(img, defaultImgUrl, localImgUrl)); 105 } catch (e) { 106 testFailed(`Image setup failed (${e}).`); 107 finishTest(); 108 return; 109 } 110 initTests(); 111 })(); 112 </script> 113 </body></html>