<!--
Copyright (c) 2019 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WebGL Out Of Resources Test</title>
<link rel="stylesheet" href="../resources/js-test-style.css"/>
<script src="../../devtools/src/debug/webgl-debug.js"></script>
<script src="../js/js-test-pre.js"></script>
<script src="../js/webgl-test-utils.js"></script>
</head>
<body>
<div id="description"></div>
<div id="console"></div>
<canvas id="canvas" width="2" height="2"> </canvas>
<canvas id="canvas2" width="2" height="2"> </canvas>
<script>
"use strict";
wtu = WebGLTestUtils;
window.onload = init;
debug("Tests a WebGL program that tries to use all of vram.");

function init() {
  if (confirm(
      "After clicking OK your machine may become unresponsive or crash.")) {
    main();
  } else {
    debug("cancelled");
  }
}

function main() {
  debug("");
  debug("Canvas.getContext");

  var gl = wtu.create3DContext("canvas");
  if (!gl) {
    testFailed("context does not exist");
  } else {
    testPassed("context exists");

    var program = wtu.setupColorQuad(gl);
    gl.useProgram(program);

    debug("");
    debug("Checking for out of memory handling.");

    var size = gl.getParameter(gl.MAX_RENDERBUFFER_SIZE);
    debug("max render buffer size: " + size);

    var allocateFramebuffers = true;
    var itervalId;
    var count = 0;

    gl = WebGLDebugUtils.makeDebugContext(gl, function(err, functionName, args) {
          assertMsg(err == gl.OUT_OF_MEMORY,
                    "correctly returns gl.OUT_OF_MEMORY when out of memory");
          stop("got: " + wtu.glEnumToString(gl, err));
        });

    var fbos = [];

    intervalId = window.setInterval(function() {
      ++count;
      var mem = count * size * size * 4;
      debug("#" + count + " : memory allocated so far " + (mem / 1024 / 1024) + "MB");
      var tex = gl.createTexture();
      gl.bindTexture(gl.TEXTURE_2D, tex);
      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE);
      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE);
      gl.texImage2D(gl.TEXTURE_2D,
                    0,                 // level
                    gl.RGBA,           // internalFormat
                    size,              // width
                    size,              // height
                    0,                 // border
                    gl.RGBA,           // format
                    gl.UNSIGNED_BYTE,  // type
                    null);             // data
      var err = gl.getError();
      if (err != gl.NO_ERROR) {
        stop("got error: " + wtu.glEnumToString(gl, err));
        return;
      }
      if (allocateFramebuffers) {
        var fb = gl.createFramebuffer();
        gl.bindFramebuffer(gl.FRAMEBUFFER, fb);
        gl.framebufferTexture2D(
            gl.FRAMEBUFFER,
            gl.COLOR_ATTACHMENT0,
            gl.TEXTURE_2D,
            tex,
            0);
        var status = gl.checkFramebufferStatus(gl.FRAMEBUFFER);
        if (status != gl.FRAMEBUFFER_COMPLETE) {
          stop("got: " + wtu.glEnumToString(gl, status));
          return;
        }
        // use the framebuffer
        wtu.drawFloatColorQuad(gl, [1, Math.random(), 1, 1]);
      }
    }, 200);

    function stop(msg) {
      debug(msg);
      window.clearInterval(intervalId);
      finishTest();
    }
  }
}
</script>
</body>
</html>
