Bug 1935611 - Fix libyuv/libpng link failed for loongarch64. r=glandium,tnikkel,ng
[gecko.git] / dom / canvas / test / test_accelerated_canvas_context_loss.html
blob610c8d8fc338e3812f5d8defedced73d99ccb127
1 <!DOCTYPE HTML>
2 <html>
3 <head>
4 <meta charset="utf-8">
5 <title>Check for contextlost/restored events after GPU process restart</title>
6 <script src="/tests/SimpleTest/SimpleTest.js"></script>
7 <link rel="stylesheet" href="/tests/SimpleTest/test.css">
8 </head>
9 <body>
10 <canvas id="c" width="512" height="512"></canvas>
12 <script type="application/javascript">
14 function waitRAF() {
15 return new Promise((resolve, reject) => {
16 window.requestAnimationFrame(resolve);
17 });
20 async function restartGPUProcess() {
21 return await SpecialPowers.spawnChrome([], async () => {
22 const gfxInfo = Cc["@mozilla.org/gfx/info;1"].getService(Ci.nsIGfxInfo);
24 if (gfxInfo.usingGPUProcess) {
25 const { TestUtils } = ChromeUtils.importESModule(
26 "resource://testing-common/TestUtils.sys.mjs"
28 let promise = TestUtils.topicObserved("compositor-reinitialized");
30 const remoteCanvas = gfxInfo.usingRemoteCanvas;
31 const acceleratedCanvas = gfxInfo.usingAcceleratedCanvas;
32 ok(true, "Restarting GPU process, remote canvas " + remoteCanvas + ", accelerated canvas " + acceleratedCanvas);
34 gfxInfo.killGPUProcessForTests();
35 await promise;
37 return remoteCanvas || acceleratedCanvas;
40 ok(true, "Not using GPU process");
41 return false;
42 });
45 const canvas = document.getElementById("c");
46 const context = canvas.getContext("2d");
48 let restoredPromiseResolve;
49 let restoredPromiseReject;
51 const restoredPromise = new Promise((resolve, reject) => {
52 restoredPromiseResolve = resolve;
53 restoredPromiseReject = reject;
54 });
56 let countLostEvents = 0;
57 let countRestoredEvents = 0;
59 function onContextLost() {
60 ok(context.isContextLost(), "Canvas context should be lost during contextlost event");
62 try {
63 let transform = context.getTransform();
64 ok(transform.isIdentity, "Canvas context should return identity transform while context lost");
65 } catch (e) {}
67 countLostEvents += 1;
70 function onContextRestored() {
71 ok(!context.isContextLost(), "Canvas context should not be lost during contextrestored event");
72 countRestoredEvents += 1;
74 restoredPromiseResolve(true);
77 function waitContextRestored() {
78 let timeoutId = window.setTimeout(restoredPromiseReject, 5000);
79 return restoredPromise.then(() => {
80 window.clearTimeout(timeoutId);
81 });
84 async function start() {
85 try {
86 canvas.addEventListener("contextlost", onContextLost);
87 canvas.addEventListener("contextrestored", onContextRestored);
89 ok(!context.isContextLost(), "Canvas context should not be lost before initial fill");
91 context.fillStyle = 'red';
92 context.fill();
93 await waitRAF();
95 ok(!context.isContextLost(), "Canvas context should not be lost after initial fill");
97 let transform = context.getTransform();
98 ok(transform.isIdentity, "Canvas context should default to identity transform");
99 context.setTransform(2.0, 3.0, 4.0, 5.0, 6.0, 7.0);
100 transform = context.getTransform();
101 ok(!transform.isIdentity, "Canvas context should have non-identity transform");
103 const restarted = await restartGPUProcess();
104 const expectedEvents = restarted ? 1 : 0;
106 if (expectedEvents) {
107 await waitContextRestored();
109 await waitRAF();
111 is(countLostEvents, expectedEvents, "Should have fired " + expectedEvents + " contextlost events");
112 is(countRestoredEvents, expectedEvents, "Should have fired " + expectedEvents + " contextrestored events");
113 ok(!context.isContextLost(), "Canvas context should not be lost after restoration");
115 context.fillStyle = 'green';
116 context.fill();
117 await waitRAF();
119 ok(!context.isContextLost(), "Canvas context should not be lost at completion");
120 } catch (err) {
121 ok(false, "Caught exception " + err);
122 } finally {
123 SimpleTest.finish();
127 SimpleTest.waitForExplicitFinish();
128 SimpleTest.requestFlakyTimeout("Wait for failure condition");
129 start();
131 </script>
132 </body>
133 </html>