<!--
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 required buffer clear behaviour 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>
<style type="text/css">
body {
    height: 3000px;
}
</style>
</head>
<body>
<div id="description"></div>
<canvas width="20" height="20" style="border: 1px solid blue;" id="c"></canvas>
<div id="console"></div>
<script>
description("This test ensures WebGL implementations correctly clear " +
            "the drawing buffer on composite if preserveDrawingBuffer is false.");
debug("");

var wtu = WebGLTestUtils;
var gl1 = wtu.create3DContext("c");
var gl2 = wtu.create3DContext();
shouldBeNonNull("gl1");
shouldBeNonNull("gl2");

shouldBeFalse('gl1.getContextAttributes().preserveDrawingBuffer');
shouldBeFalse('gl2.getContextAttributes().preserveDrawingBuffer');

function init(gl) {
    gl.clearColor(1, 0, 0, 1);
    gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);

    // enable scissor here, before compositing, to make sure it's correctly
    // ignored and restored
    gl.scissor(0, 10, 10, 10);
    gl.enable(gl.SCISSOR_TEST);
}

init(gl1);
init(gl2);

wtu.waitForComposite(function() {
    function clear(gl) {
        // scissor was set earlier
        gl.clearColor(0, 0, 1, 1);
        gl.clear(gl.COLOR_BUFFER_BIT);
    }
    clear(gl1);
    clear(gl2);

    debug("check on screen canvas");
    wtu.checkCanvasRect(gl1, 0, 10, 10, 10, [0, 0, 255, 255],
        "cleared corner should be blue, stencil should be preserved");
    wtu.checkCanvasRect(gl1, 0, 0, 10, 10, [0, 0, 0, 0],
        "remainder of buffer should be cleared");
    debug("check off screen canvas");
    wtu.checkCanvasRect(gl2, 0, 10, 10, 10, [0, 0, 255, 255],
        "cleared corner should be blue, stencil should be preserved");
    wtu.checkCanvasRect(gl2, 0, 0, 10, 10, [255, 0, 0, 255],
        "remainder of buffer should be un-cleared red");

    finishTest();
});

var successfullyParsed = true;
</script>
</body>
</html>

