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
   72
   73
   74
   75
   76
   77
   78
   79
   80
   81
   82
   83
   84
   85
   86
   87
   88
   89
   90
   91
   92
   93
   94
   95
   96
   97
   98
   99
  100
  101
  102
  103
  104
  105
  106
  107
  108
  109
  110
  111
  112
  113
  114
  115
  116
  117
  118
  119
  120
  121
  122

content / test / data / gpu / pixel_offscreen_canvas_ibrc_test.js [blame]

// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

let oc;
let gl;
const sz = 256;

let canvas;
let ibrc;

let workerMode = false;
let worker;

//---------------------------------------------------------------------------
// Functions to be called either on the main thread or worker

function setupOffscreenCanvas(highPerformance) {
  oc = new OffscreenCanvas(sz, sz);
  let attribs = {};
  if (highPerformance)
    attribs['powerPreference'] = 'high-performance';
  gl = oc.getContext('webgl', attribs);
}

function doRender() {
  gl.clearColor(0.0, 1.0, 0.0, 1.0);
  gl.clear(gl.COLOR_BUFFER_BIT);
  let ib = oc.transferToImageBitmap();
  if (workerMode) {
    self.postMessage({frame: ib}, [ib]);
  } else {
    ibrc.transferFromImageBitmap(ib);
  }
}

//---------------------------------------------------------------------------
// Functions to be called only on the main thread

function initialize(useWorker, selfURL) {
  canvas = document.createElement('canvas');
  canvas.style = 'position: absolute; top: 0px; left: 0px;';
  canvas.width = 256;
  canvas.height = 256;
  document.body.appendChild(canvas);
  ibrc = canvas.getContext('bitmaprenderer');

  workerMode = useWorker;
  if (workerMode) {
    worker = new Worker(selfURL);
    worker.onmessage = function(e) {
      ibrc.transferFromImageBitmap(e.data.frame);
      waitForFinish();
    }
  }

  sendResult("READY");
}

function setup(highPerformance) {
  if (workerMode) {
    worker.postMessage({command: 'setup', highPerformance: highPerformance});
  } else {
    setupOffscreenCanvas(highPerformance);
  }
}

function render() {
  if (workerMode) {
    worker.postMessage({command: 'render'});
  } else {
    doRender();
    waitForFinish();
  }
}

function logOutput(s) {
  if (window.domAutomationController)
    window.domAutomationController.log(s);
  else
    console.log(s);
}

function sendResult(status, detail) {
  logOutput(status + ' ' + detail);
  if (window.domAutomationController) {
    window.domAutomationController.send(status);
  }
}

function waitForFinish()
{
  let numFramesBeforeEnd = 15;

  function waitForFinishImpl() {
    if (--numFramesBeforeEnd == 0) {
      sendResult("SUCCESS", "Test complete");
    } else {
      window.requestAnimationFrame(waitForFinishImpl);
    }
  }

  window.requestAnimationFrame(waitForFinishImpl);
}

//---------------------------------------------------------------------------
// Code to be executed only on the worker

if (this.document === undefined) {
  workerMode = true;
  // We're on a worker - set up postMessage handler.
  self.onmessage = function(e) {
    switch (e.data.command) {
    case 'setup':
      setupOffscreenCanvas(e.data.highPerformance);
      break;
    case 'render':
      doRender();
      break;
    }
  }
}