summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/reftest/webgl-utils.js
blob: 798e7fee0ff1ce35e156006d18a086b262f08f36 (plain)
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
"use strict";

function parseArgs() {
  var query = window.location.search.substring(1);

  var split = query.split("&");

  var args = {}
  for (var i = 0; i < split.length; i++) {
    var pair = split[i].split("=");

    var key = pair[0];
    var value = true;
    if (pair.length >= 2) {
      eval("value = " + decodeURIComponent(pair[1]) + ";");
    }

    args[key] = value;
  }

  return args;
}

var gArgs = null;
function arg(key, defaultVal) {
  if (gArgs === null) {
    gArgs = parseArgs();
  }

  if (!(key in gArgs))
    return defaultVal;

  return gArgs[key];
}

function initGL(canvas) {
  if (arg("nogl"))
    return null;

  var gl = null;

  var withAA = arg("aa", false);
  var withAlpha = arg("alpha", false);
  var withDepth = arg("depth", false);
  var withPremult = arg("premult", false);
  var withPreserve = arg("preserve", false);
  var withStencil = arg("stencil", false);

  try {
    var argDict = {
      alpha: withAlpha,
      depth: withDepth,
      stencil: withStencil,
      antialias: withAA,
      premultipliedAlpha: withPremult,
      preserveDrawingBuffer: withPreserve,
    };
    gl = canvas.getContext("webgl", argDict);
  } catch(e) {}

  return gl;
}

function rAF(func) {
  var raf = window.requestAnimationFrame;
  raf(func);
}

var MAX_WAIT_FOR_COMPOSITE_DELAY_MS = 500;

function waitForComposite(func) {
  var isDone = false;
  var doneFunc = function () {
    if (isDone)
      return;
    isDone = true;
    func();
  };

  rAF(doneFunc);
  setTimeout(doneFunc, MAX_WAIT_FOR_COMPOSITE_DELAY_MS);
}