summaryrefslogtreecommitdiffstats
path: root/dom/canvas/test/webgl-conf/checkout/conformance/offscreencanvas/context-attribute-preserve-drawing-buffer.html
blob: d230b69d9d222f7b446c698c9e2b76172d5c3e02 (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
83
84
85
86
87
88
89
90
91
92
93
<!--
Copyright (c) 2019 The Khronos Group Inc.
Use of this source code is governed by an MIT-style license that can be
found in the LICENSE.txt file.
-->
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>OffscreenCanavs context attribute preserveDrawingBuffer</title>
<link rel="stylesheet" href="../../resources/js-test-style.css"/>
<script src="../../js/js-test-pre.js"></script>
<script src="../../js/webgl-test-utils.js"></script>
<script src="../../js/tests/canvas-tests-utils.js"></script>
</head>
<body>
<div id="description"></div>
<div id="canvases"></div>
<div id="console"></div>
<script>
"use strict";
description("This test checks whether OffscreenCanvas webgl context honors the preserveDrawingBuffer flag.");
const wtu = WebGLTestUtils;
const pixels = new Uint8Array(4);

function checkPixels(color) {
  return (color[0] === pixels[0]) &&
    (color[1] === pixels[1]) &&
    (color[2] === pixels[2]) &&
    (color[3] === pixels[3]);
}

const nextFrame = () => new Promise(r => requestAnimationFrame(r));

async function getPixelsFromOffscreenWebgl(preserveFlag, color, msg) {
  const canvas = document.createElement("canvas");
  document.getElementById("canvases").appendChild(canvas);
  const offscreenCanvas = transferredOffscreenCanvasCreation(canvas, 10, 10);
  const gl = offscreenCanvas.getContext("webgl", {preserveDrawingBuffer: preserveFlag});

  // Draw some color on gl
  gl.clearColor(1, 0, 1, 1);
  gl.clear(gl.COLOR_BUFFER_BIT);

  let t;
  const t0 = await nextFrame();
  const timeDuration = preserveFlag ? 500 : 2000;
  for (;;) {
    t = await nextFrame();

    gl.readPixels(0, 0, 1, 1, gl.RGBA, gl.UNSIGNED_BYTE, pixels);
    if (preserveFlag) {
      if (!checkPixels(color)) {
        testFailed(msg + '\nexpected: ' + color.toString() + ' was ' + pixels.toString());
        return;
      }
    } else {
      if (checkPixels(color)) {
        testPassed(msg);
        return;
      }
    }

    // Keep checking until it takes up to a certain time.
    // preserveDrawingBuffer:false seems flaky on Chrome's test bots; run that test for longer.
    if (t > t0 + timeDuration) {
      break;
    }
  }

  if (preserveFlag) {
    testPassed(msg);
  } else {
    testFailed(msg + '\nafter ' + timeDuration + ' ms, expected: ' + color.toString() + ' was ' + pixels.toString());
  }
}

(async () => {
  if (!window.OffscreenCanvas) {
      testPassed("No OffscreenCanvas support");
  } else {
    // Test if OffscreenCanvas.webgl retains contents if preserveDrawingBuffer is true.
    await getPixelsFromOffscreenWebgl(true, [255,0,255,255], "should be preserved");

    // Test if OffscreenCanvas.webgl loses contents if preserveDrawingBuffer is false.
    await getPixelsFromOffscreenWebgl(false, [0, 0, 0, 0], "should not be preserved");
  }
  finishTest();
})();

</script>
</body>
</html>