<!DOCTYPE html>
<html>
<head>
<title>texImage2D with large ImageBitmap.</title>
<script src="../../../resources/js-test.js"></script>
<script src="resources/webgl-test.js"> </script>
<script src="resources/webgl-test-utils-khr.js"> </script>
</head>
<body>
<canvas id="example"></canvas>
<div id="description"></div>
<script>
"use strict";

let wtu = WebGLTestUtils;

async function loadImage(url) {
    const res = await fetch(url, { mode: 'cors' });
    const blob = await res.blob();
    const bitmap = await createImageBitmap(blob);
    return bitmap;
}

async function test() {
  let red = [255, 0, 0, 255];
  let green = [0, 255, 0, 255];

  let ctx2d = document.createElement("canvas").getContext("2d");
  ctx2d.fillStyle = "#ff0000";
  ctx2d.fillRect(0, 0, 300, 150);
  wtu.checkCanvasRect(ctx2d, 0, 0, 300, 150, red, "gpu process restart check canvas should be red", 0);

  let canvas = document.getElementById("example");
  let gl = wtu.create3DContext(canvas, {'depth': false, 'antialias': false, stencil: 'false'});
  let program = wtu.setupTexturedQuad(gl);
  let loc = gl.getUniformLocation(program, "tex");
  gl.uniform1i(loc, 0);

  gl.disable(gl.BLEND);
  gl.disable(gl.DEPTH_TEST);
  gl.pixelStorei(gl.UNPACK_COLORSPACE_CONVERSION_WEBGL, gl.NONE);
  let width = canvas.width;
  let height = canvas.height;

  glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup.");
  let bitmapCount = 6;
  let uploadIterations = 5;
  let bitmaps = [];
  for (let i = 0; i < bitmapCount; ++i)
    bitmaps.push(await loadImage('resources/green-4000x4000.png'));
  for (let i = 0; i < uploadIterations; ++i) {
    for (let bitmap of bitmaps) {
      let tex = gl.createTexture();
      gl.bindTexture(gl.TEXTURE_2D, tex);
      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.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.NEAREST);
      gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.NEAREST);
      gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, bitmap);
      glErrorShouldBe(gl, gl.NO_ERROR, "Should be no errors from setup");
      wtu.drawUnitQuad(gl);
      gl.deleteTexture(tex);
      wtu.checkCanvasRect(gl, 0, 0, width, height, green, "should be green", 0);
      gl.clear(gl.COLOR_BUFFER_BIT);
    }
  }
  wtu.checkCanvasRect(ctx2d, 0, 0, 300, 150, red, "gpu process restart check canvas should be red", 0);
  ctx2d.fillStyle = "#00ff00";
  ctx2d.fillRect(0, 0, 300, 150);
  wtu.checkCanvasRect(ctx2d, 0, 0, 300, 150, green, "gpu process restart check canvas should be green", 0);
}

initNonKhronosFramework(true);

test().then(() => {
  nonKhronosFrameworkNotifyDone()
});
var successfullyParsed = true;
</script>
