1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28
   29
   30
   31
   32
   33
   34
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
   51
   52
   53
   54
   55
   56
   57
   58
   59
   60
   61
   62
   63
   64
   65
   66
   67
   68
   69
   70
   71

content / test / data / gpu / webgl2-multisampling-high-power-switch-does-not-crash.html [blame]

<html>
<head>
<script type="text/javascript">
let canvas;
let gl;
let c2d;
let msrb;
let contextLost = false;

function send(str) {
  if (window.domAutomationController) {
    window.domAutomationController.send(str);
  } else {
    console.log(str);
  }
}

function animate() {
  if (!contextLost) {
    gl.clearColor(0, 1, 0, 1);
    gl.clear(gl.COLOR_BUFFER_BIT);
  }
  // Draw the WebGL canvas to the 2D canvas regardless of whether the
  // WebGL context has been lost.
  c2d.drawImage(canvas, 0, 0);
  requestAnimationFrame(animate);
}

function onLoad() {
  canvas = document.getElementById("canvas1");
  canvas.addEventListener('webglcontextlost', function(e) {
    console.log('Context lost');
    e.preventDefault();
    contextLost = true;
  }, false);
  canvas.addEventListener('webglcontextrestored', function(e) {
    contextLost = false;
    send("SUCCESS");
  }, false);
  gl = canvas.getContext('webgl2', {powerPreference: "low-power"});
  msrb = gl.createRenderbuffer();
  gl.bindRenderbuffer(gl.RENDERBUFFER, msrb);
  gl.renderbufferStorageMultisample(gl.RENDERBUFFER, 2, gl.RGBA8, 16, 16);
  gl.bindRenderbuffer(gl.RENDERBUFFER, null);
  let err = gl.getError();
  if (err != 0) {
    console.log("Error " + err + " allocating multisampled renderbuffer");
    send("FAILED");
    return;
  }
  // Fetch the 2D rendering context.
  let canvas2d = document.getElementById("canvas2d");
  c2d = canvas2d.getContext('2d');
  // Must draw to the WebGL canvas in order for the GPU switch to be detected.
  requestAnimationFrame(animate);
  // Wait a second before telling the harness to switch to the high-performance GPU.
  setTimeout(() => { send("LOADED"); }, 1000);
}

function runTest() {
  let c2 = document.getElementById("canvas2");
  let gl = c2.getContext('webgl2', {powerPreference: "high-performance"});
}
</script>
</head>
<body onload="onLoad()">
<canvas id="canvas1" width="16px" height="16px"></canvas>
<canvas id="canvas2" width="16px" height="16px"></canvas>
<canvas id="canvas2d" width="16px" height="16px"></canvas>
</body>
</html>