2 Copyright (c) 2019 The Khronos Group Inc.
3 Use of this source code is governed by an MIT-style license that can be
4 found in the LICENSE.txt file.
10 <title>OffscreenCanavs context attribute preserveDrawingBuffer
</title>
11 <link rel=
"stylesheet" href=
"../../resources/js-test-style.css"/>
12 <script src=
"../../js/js-test-pre.js"></script>
13 <script src=
"../../js/webgl-test-utils.js"></script>
14 <script src=
"../../js/tests/canvas-tests-utils.js"></script>
17 <div id=
"description"></div>
18 <div id=
"canvases"></div>
19 <div id=
"console"></div>
22 description("This test checks whether OffscreenCanvas webgl context honors the preserveDrawingBuffer flag.");
23 const wtu
= WebGLTestUtils
;
24 const pixels
= new Uint8Array(4);
26 function checkPixels(color
) {
27 return (color
[0] === pixels
[0]) &&
28 (color
[1] === pixels
[1]) &&
29 (color
[2] === pixels
[2]) &&
30 (color
[3] === pixels
[3]);
33 const nextFrame
= () => new Promise(r
=> requestAnimationFrame(r
));
35 async
function getPixelsFromOffscreenWebgl(preserveFlag
, color
, msg
) {
36 const canvas
= document
.createElement("canvas");
37 document
.getElementById("canvases").appendChild(canvas
);
38 const offscreenCanvas
= transferredOffscreenCanvasCreation(canvas
, 10, 10);
39 const gl
= offscreenCanvas
.getContext("webgl", {preserveDrawingBuffer
: preserveFlag
});
41 // Draw some color on gl
42 gl
.clearColor(1, 0, 1, 1);
43 gl
.clear(gl
.COLOR_BUFFER_BIT
);
46 const t0
= await
nextFrame();
47 const timeDuration
= preserveFlag
? 500 : 2000;
49 t
= await
nextFrame();
51 gl
.readPixels(0, 0, 1, 1, gl
.RGBA
, gl
.UNSIGNED_BYTE
, pixels
);
53 if (!checkPixels(color
)) {
54 testFailed(msg
+ '\nexpected: ' + color
.toString() + ' was ' + pixels
.toString());
58 if (checkPixels(color
)) {
64 // Keep checking until it takes up to a certain time.
65 // preserveDrawingBuffer:false seems flaky on Chrome's test bots; run that test for longer.
66 if (t
> t0
+ timeDuration
) {
74 testFailed(msg
+ '\nafter ' + timeDuration
+ ' ms, expected: ' + color
.toString() + ' was ' + pixels
.toString());
79 if (!window
.OffscreenCanvas
) {
80 testPassed("No OffscreenCanvas support");
82 // Test if OffscreenCanvas.webgl retains contents if preserveDrawingBuffer is true.
83 await
getPixelsFromOffscreenWebgl(true, [255,0,255,255], "should be preserved");
85 // Test if OffscreenCanvas.webgl loses contents if preserveDrawingBuffer is false.
86 await
getPixelsFromOffscreenWebgl(false, [0, 0, 0, 0], "should not be preserved");