<!--
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 Canvas Conformance Tests</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>
<div id="description"></div>
<div id="console"></div>
<canvas id="canvas2d_0" width="400" height="400"> </canvas>
<canvas id="canvas2d_1" width="400" height="400"> </canvas>
<canvas id="canvas2d_2" width="400" height="400"> </canvas>
<canvas id="webgl" width="400" height="400"> </canvas>
<script>
"use strict";

description("This test ensures WebGL implementations interact correctly with the canvas 2D drawImage call.");

var err;
var wtu = WebGLTestUtils;

var canvas2d = [];
var ctx2d = [];
for (var i = 0; i < 3; i ++) {
  canvas2d[i] = document.getElementById("canvas2d_" + i);
  ctx2d[i] = canvas2d[i].getContext("2d");
}

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

  debug("");
  debug("Checking canvas and WebGL drawImage interaction");
  for (var count = 0; count < 10; count ++) {
    gl.clearColor(0.25, 0.5, 0.75, 1);
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    ctx2d[0].drawImage(canvas, 0, 0, canvas2d[0].width, canvas2d[0].height);

    gl.clearColor(1, 0, 0, 1);
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    ctx2d[1].drawImage(canvas, 0, 0, canvas2d[1].width, canvas2d[1].height);

    gl.clearColor(1, 0, 1, 1);
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
    ctx2d[2].drawImage(canvas, 0, 0, canvas2d[2].width, canvas2d[2].height);

    gl.clearColor(1, 1, 0, 1);
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

    wtu.checkCanvasRect(ctx2d[0], 0, 0, canvas2d[0].width, canvas2d[0].height, [64, 128, 192, 255],
                        "drawImage: Should be [64, 128, 192, 255]", 2);
    wtu.checkCanvasRect(ctx2d[1], 0, 0, canvas2d[1].width, canvas2d[1].height, [255, 0, 0, 255],
                        "drawImage: Should be [255, 0, 0, 255]", 2);
    wtu.checkCanvasRect(ctx2d[2], 0, 0, canvas2d[2].width, canvas2d[2].height, [255, 0, 255, 255],
                        "drawImage: Should be [255, 0, 255, 255]", 2);
  }

  err = gl.getError();
  debug("");
}
finishTest();
</script>

</body>
</html>
