﻿<!--
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 toDataURL test</title>
<link rel="stylesheet" href="../../resources/js-test-style.css"/>
<script src="../../js/js-test-pre.js"></script>
<script src="../../js/webgl-test-utils.js"> </script>
</head>
<body>
<canvas width="20" height="20" style="border: 1px solid black; width: 16px; height: 16px" id="c3d"></canvas>
<canvas width="20" height="20" style="border: 1px solid black; width: 16px; height: 16px" id="c2d"></canvas>
<div id="description"></div>
<div id="console"></div>
<script type="application/javascript">
var wtu = WebGLTestUtils;
var numTests = 10;
var gl;
var ctx;

var main = function() {
  description();
  ctx = document.getElementById("c2d").getContext("2d");

  var updateCanvas = function(width, height, attributes) {
    var canvas = document.getElementById("c3d");

    if (gl && (gl.attributes !== attributes)) {
      // Attributes changed, recreate the canvas
      var newCanvas = canvas.cloneNode();
      canvas.parentNode.replaceChild(newCanvas, canvas);
      canvas = newCanvas;
      gl = undefined;
    }

    canvas.width = width;
    canvas.height = height;

    if (!gl) {
      gl = wtu.create3DContext(canvas, attributes);

      if (!gl) {
        testFailed("can't create 3d context");
        return;
      }

      gl.attributes = attributes;
    }

    return gl;
  };

  var clearRect = function(gl, x, y, width, height, color) {
    gl.clearColor(color[0] / 255, color[1] / 255, color[2] / 255, color[3] / 255);
    gl.scissor(x, y, width, height);
    gl.clear(gl.COLOR_BUFFER_BIT);
  };

  var testSize = function(width, height, attributes, callback) {
    let attributesDebugMessage = "";
    if (attributes) {
      attributesDebugMessage = " attributes: " + JSON.stringify(attributes);
    }
    debug("testing " + width + " by " + height + attributesDebugMessage);
    var gl = updateCanvas(width, height, attributes);
    if (!gl) {
      callback();
      return;
    }

    gl.viewport(0, 0, width, height);
    gl.enable(gl.SCISSOR_TEST);

    var bottomColor = [255, 0, 0, 255];
    var topColor = [0, 255, 0, 255];
    var rightColor = [0, 0, 255, 255];
    var halfHeight = Math.floor(height / 2);
    var topHeight = height - halfHeight;
    var canvasTopHeight = height - topHeight;
    clearRect(gl, 0, 0, width, halfHeight, bottomColor);
    clearRect(gl, 0, halfHeight, width, topHeight, topColor);
    clearRect(gl, width - 1, 0, 1, height, rightColor);

    // Performs gl.canvas.toDataURL() internally
    var img = wtu.makeImageFromCanvas(gl.canvas, function() {
      ctx.canvas.width = width;
      ctx.canvas.height = height;
      ctx.imageSmoothingEnabled = false;
      ctx.drawImage(img, 0, 0);
      wtu.checkCanvasRect(ctx, 0, 0, width - 1, topHeight, topColor);
      wtu.checkCanvasRect(ctx, 0, topHeight, width - 1, halfHeight, bottomColor);
      wtu.checkCanvasRect(ctx, width - 1, 0, 1, height, rightColor);
      debug("");
      callback();
    });
  };

  const premultipliedAlphaAttributes =  {
    premultipliedAlpha: false,
  };
  var tests = [
    { width:  16    , height:  16    , },
    { width:  16 - 1, height:  16    , },
    { width:  16 - 1, height:  16 - 1, },
    { width:  16 + 1, height:  16 - 1, },
    { width:  16 - 1, height:  16 + 1, },
    { width: 256    , height: 256    , },
    { width: 256 - 1, height: 256    , },
    { width: 256 - 1, height: 256 - 1, },
    { width: 256 + 1, height: 256 - 1, },
    { width: 256 - 1, height: 256 + 1, },
    { width: 512    , height: 512    , },
    { width: 512 - 1, height: 512    , },
    { width: 512 - 1, height: 512 - 1, },
    { width: 512 + 1, height: 512 - 1, },
    { width: 512 - 1, height: 512 + 1, },
    { width:  16    , height:  16    , attributes: premultipliedAlphaAttributes},
  ];
  var testIndex = 0;
  var runNextTest = function() {
    if (testIndex == tests.length) {
      finishTest();
      return;
    }
    var test = tests[testIndex++];
    testSize(test.width, test.height, test.attributes, function() {
      setTimeout(runNextTest, 0);
    })
  };
  runNextTest();
};
main();
var successfullyParsed = true;
</script>
</body>
</html>

