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

content / test / data / gpu / offscreencanvas_imagebitmap_from_worker_uses_overlay.html [blame]

<!DOCTYPE HTML>
<html>
<head>
<title>Canvas displaying an ImageBitmap that comes from a worker uses CALayerOverlay Test</title>

<script id="workerCode">
  self.onmessage = function(event) {
    const offscreenCanvas = new OffscreenCanvas(100, 100);
    const ctx = offscreenCanvas.getContext("2d");
    ctx.fillStyle = event.data.color;
    ctx.fillRect(0, 0, 100, 100);
    let img = offscreenCanvas.transferToImageBitmap();
    self.postMessage(img, [img]);
  }
</script>

<script>
// '--enable-gpu-benchmarking' is required for this test.
async function main() {
  const workerCode = document.querySelector("#workerCode").textContent;
  const blob = new Blob([workerCode], { type: 'text/javascript' });
  const url = URL.createObjectURL(blob);
  const worker = new Worker(url);

  const canvas = document.getElementById('canvas');
  const context = canvas.getContext('bitmaprenderer');

  function errorCodeHandler(errorCode) {
    // gfx::kCALayerSuccess = 0
    if (errorCode == 0) {
      if (window.domAutomationController) {
        window.domAutomationController.send("SUCCESS");
      }
      worker.postMessage({color: 'DarkGreen'});
    } else {
      console.log("CALayerOverlay is not used. Error code:" , errorCode);
      if (window.domAutomationController) {
        window.domAutomationController.send("FAILED");
      }
      worker.postMessage({color: 'Red'});
    }
  }


  function draw() {
    if (!chrome.gpuBenchmarking.addCoreAnimationStatusEventListener(errorCodeHandler)) {
     console.log("addCoreAnimationStatusEventListener fails! Use '--enable-gpu-benchmarking'");
     if (window.domAutomationController) {
       window.domAutomationController.send("FAILED");
     }
    }
  }

  let first_draw = true;
  worker.postMessage({color: 'Black'});
  worker.addEventListener('message', (event) => {
    context.transferFromImageBitmap(event.data);
    if (first_draw) {
      window.requestAnimationFrame(draw);
      first_draw = false;
    }
  });
}

</script>
</head>
<body onload="main()">
<canvas id="canvas" width="100" height="100" style="position:absolute; top:0px; left:0px"</canvas>
</body>
</html>