tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

iterable-test.js (5703B)


      1 /*
      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.
      5 */
      6 IterableTest = (function() {
      7 
      8  var wtu = WebGLTestUtils;
      9 
     10  function run(test, iterations) {
     11    var target = iterations || 10;
     12    var count = 0;
     13 
     14    function doNextTest() {
     15      ++count;
     16      debug("Test " + count + " of " + target);
     17      var success = test();
     18      if (count < target && success !== false) {
     19        wtu.dispatchPromise(doNextTest);
     20      } else {
     21        finishTest();
     22      }
     23    }
     24 
     25    doNextTest();
     26  }
     27 
     28  // Creates a canvas and a texture then exits. There are
     29  // no references to either so both should be garbage collected.
     30  function createContextCreationAndDestructionTest() {
     31    var textureSize = null;
     32 
     33    return function() {
     34      var canvas = document.createElement("canvas");
     35      // This is safe for any device. See drawingBufferWidth in spec.
     36      canvas.width = 2048;
     37      canvas.height = 2048;
     38      var gl = wtu.create3DContext(canvas);
     39      if (textureSize === null) {
     40        var maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE);
     41        textureSize = Math.min(1024, maxTextureSize);
     42      }
     43      var tex = gl.createTexture();
     44      gl.bindTexture(gl.TEXTURE_2D, tex);
     45      gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, textureSize, textureSize, 0, gl.RGBA, gl.UNSIGNED_BYTE,
     46                    null);
     47      gl.clear(gl.COLOR_BUFFER_BIT);
     48      wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors");
     49 
     50      return true;
     51    };
     52  }
     53 
     54  // Creates many small canvases and attaches them to the DOM.
     55  // This tests an edge case discovered in Chrome where the creation of multiple
     56  // WebGL contexts would eventually lead to context creation failure.
     57  // (crbug.com/319265) The test does not require that old contexts remain
     58  // valid, only that new ones can be created.
     59  function createContextCreationTest() {
     60    return function() {
     61      var canvas = document.createElement("canvas");
     62      canvas.width = 1;
     63      canvas.height = 1;
     64 
     65      document.body.appendChild(canvas);
     66 
     67      var gl = wtu.create3DContext(canvas);
     68      if (!gl) {
     69        return false;
     70      }
     71 
     72      gl.clear(gl.COLOR_BUFFER_BIT);
     73      wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors");
     74 
     75      return true;
     76    };
     77  }
     78 
     79  // Draws rectangle on a passed canvas with preserveDrawingBuffer
     80  // and antialiasing ON, tests rect color on every iteration.
     81  function createMultisampleCorruptionTest(gl) {
     82    var lastContext = null;
     83    // Allocate a read back buffer in advance and reuse it for all iterations
     84    // to avoid memory issues because of late garbage collection.
     85    var readBackBuf = new Uint8Array(gl.canvas.width * gl.canvas.height * 4);
     86 
     87    var program = wtu.loadStandardProgram(gl);
     88    var uniforms = wtu.getUniformMap(gl, program);
     89    gl.useProgram(program);
     90 
     91    gl.clearColor(1.0, 0.0, 0.0, 1.0);
     92    gl.clear(gl.COLOR_BUFFER_BIT);
     93 
     94    var vertexObject = gl.createBuffer();
     95    gl.bindBuffer(gl.ARRAY_BUFFER, vertexObject);
     96    gl.bufferData(gl.ARRAY_BUFFER, new Float32Array([ 0,2.5,0, 1.5,1.5,0, 2.5,1.5,0 ]), gl.STATIC_DRAW);
     97    gl.enableVertexAttribArray(0);
     98    gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
     99    gl.vertexAttrib3f(1, 0.0, 0.0, 1.0);
    100 
    101    var identityMat = new Float32Array([
    102      1, 0, 0, 0,
    103      0, 1, 0, 0,
    104      0, 0, 1, 0,
    105      0, 0, 0, 1
    106    ]);
    107 
    108    gl.uniformMatrix4fv(uniforms.u_modelViewProjMatrix.location, false, identityMat);
    109 
    110    function test() {
    111      var gl2 = wtu.create3DContext(null, {antialias: true});
    112 
    113      gl2.canvas.width = gl2.canvas.height = 1024;
    114      gl2.canvas.style.width = gl2.canvas.style.height = "1px";
    115      document.body.appendChild(gl2.canvas);
    116 
    117      gl2.clearColor(1.0, 0.0, 0.0, 1.0);
    118      gl2.clear(gl2.COLOR_BUFFER_BIT);
    119 
    120      if(lastContext) {
    121          gl.drawArrays(gl.TRIANGLES, 0, 3);
    122          var msg = "Canvas should be red";
    123          wtu.checkCanvasRectColor(gl,
    124              0, 0, gl.canvas.width, gl.canvas.height,
    125              [255, 0, 0, 255], null,
    126              function() {
    127                  testPassed(msg);
    128              },
    129              function() {
    130                  testFailed(msg);
    131                  return false;
    132              },
    133          debug, readBackBuf);
    134          document.body.removeChild(lastContext.canvas);
    135      }
    136 
    137      lastContext = gl2;
    138      return true;
    139    };
    140 
    141    // First pass does initialization
    142    test();
    143 
    144    return test;
    145  }
    146 
    147  // Draws repeatedly to a large canvas with preserveDrawingBuffer enabled to
    148  // try and provoke a context loss.
    149  function createPreserveDrawingBufferLeakTest(gl) {
    150    var contextActive = true;
    151    gl.canvas.addEventListener("webglcontextlost", () => {
    152      testFailed("Context was lost");
    153      contextActive = false;
    154    });
    155 
    156    function test() {
    157      var x = Math.random() * gl.drawingBufferWidth;
    158      var y = Math.random() * gl.drawingBufferHeight;
    159      var width = Math.random() * (gl.drawingBufferWidth - x);
    160      var height = Math.random() * (gl.drawingBufferHeight - y);
    161 
    162      gl.enable(gl.SCISSOR_TEST);
    163      gl.scissor(x, y, width, height);
    164      gl.clearColor(Math.random(), Math.random(), Math.random(), 1.0);
    165      gl.clear(gl.COLOR_BUFFER_BIT);
    166      wtu.glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors");
    167 
    168      return contextActive;
    169    };
    170 
    171    return test;
    172  }
    173 
    174  return {
    175    run: run,
    176 
    177    createContextCreationAndDestructionTest: createContextCreationAndDestructionTest,
    178    createContextCreationTest: createContextCreationTest,
    179    createMultisampleCorruptionTest: createMultisampleCorruptionTest,
    180    createPreserveDrawingBufferLeakTest: createPreserveDrawingBufferLeakTest
    181  };
    182 
    183 })();