Skip to content

Truly independent web browser

Tests/LibWeb: Test transform animations inside an SVG on the compositor

Andreas Kling0c0d93d08657
diff --git a/Tests/LibWeb/Ref/expected/compositor-svg-internal-transform-ref.html b/Tests/LibWeb/Ref/expected/compositor-svg-internal-transform-ref.html
new file mode 100644
index 00000000000..419b377a33d
--- /dev/null
+++ b/Tests/LibWeb/Ref/expected/compositor-svg-internal-transform-ref.html
@@ -0,0 +1,13 @@
+<!DOCTYPE html>
+<style>
+ body { margin: 0; }
+ svg { display: block; width: 96px; height: 96px; fill: green; }
+</style>
+<svg viewBox="0 0 24 24">
+ <rect x="2" y="3" width="4" height="2"></rect>
+ <rect x="11" y="4" width="4" height="4"></rect>
+ <rect x="18" y="4" width="4" height="4"></rect>
+ <rect x="3" y="10" width="2" height="4"></rect>
+ <rect x="10" y="18" width="4" height="4"></rect>
+ <rect x="18" y="13" width="4" height="2"></rect>
+</svg>
diff --git a/Tests/LibWeb/Ref/input/compositor-svg-internal-transform.html b/Tests/LibWeb/Ref/input/compositor-svg-internal-transform.html
new file mode 100644
index 00000000000..3ff6bc90e71
--- /dev/null
+++ b/Tests/LibWeb/Ref/input/compositor-svg-internal-transform.html
@@ -0,0 +1,33 @@
+<!DOCTYPE html>
+<html class="reftest-wait">
+<link rel="match" href="../expected/compositor-svg-internal-transform-ref.html">
+<style>
+ body { margin: 0; }
+ svg { display: block; width: 96px; height: 96px; fill: green; }
+ .box { transform-box: fill-box; transform-origin: center; }
+ @keyframes squash { from, to { transform: scaleY(0.5); } }
+ @keyframes shift { from, to { transform: translate(3px, 2px); } }
+ @keyframes shift-by-its-size { from, to { transform: translate(100%, 50%); } }
+ @keyframes turn { from, to { transform: rotate(90deg); } }
+ @keyframes grow { from, to { transform: scale(2); } }
+</style>
+<svg viewBox="0 0 24 24">
+ <rect class="box" style="animation: squash 1s linear infinite" x="2" y="2" width="4" height="4"></rect>
+ <rect style="animation: shift 1s linear infinite" x="8" y="2" width="4" height="4"></rect>
+ <rect class="box" style="animation: shift-by-its-size 1s linear infinite" x="14" y="2" width="4" height="4"></rect>
+ <rect class="box" style="animation: turn 1s linear infinite" x="2" y="11" width="4" height="2"></rect>
+ <rect style="animation: grow 1s linear infinite" x="5" y="9" width="2" height="2"></rect>
+ <g transform="translate(2 2)"><rect class="box" style="animation: squash 1s linear infinite" x="16" y="10" width="4" height="4"></rect></g>
+</svg>
+<script>
+ // These animations stay on one value, so whichever side samples them draws what a box at that value draws. The
+ // reference spells out where each box ends up without a transform, under the same scale from the viewBox.
+ requestAnimationFrame(() => {
+ requestAnimationFrame(() => {
+ requestAnimationFrame(() => {
+ document.documentElement.classList.remove("reftest-wait");
+ });
+ });
+ });
+</script>
+</html>
diff --git a/Tests/LibWeb/Text/expected/css/compositor-svg-internal-transform.txt b/Tests/LibWeb/Text/expected/css/compositor-svg-internal-transform.txt
new file mode 100644
index 00000000000..7194249fbc8
--- /dev/null
+++ b/Tests/LibWeb/Text/expected/css/compositor-svg-internal-transform.txt
@@ -0,0 +1,20 @@
+runs on the compositor:
+ a rect: false
+ a rect in a group: false
+ a rect in a transformed group: false
+ a group: false
+ a rect in a translucent group: false
+runs on the compositor, though it must not:
+ a rect in a group with a filter: false
+ a rect in a group with a clip path: false
+ a rect in a group with a mask: false
+ a rect in a group transformed about its fill box: false
+ a rect in a mask: false
+ a use element: false
+ a nested svg: false
+the main thread leaves the animated style alone: false
+the main thread asks for no frames: false
+the main thread runs no rendering updates: false
+the box a script reads runs on the compositor: false
+height of its group at the start: 10
+height of its group halfway: 10
diff --git a/Tests/LibWeb/Text/input/css/compositor-svg-internal-transform.html b/Tests/LibWeb/Text/input/css/compositor-svg-internal-transform.html
new file mode 100644
index 00000000000..b7425bfdae4
--- /dev/null
+++ b/Tests/LibWeb/Text/input/css/compositor-svg-internal-transform.html
@@ -0,0 +1,93 @@
+<!DOCTYPE html>
+<script src="../include.js"></script>
+<style>
+ @keyframes pulse { from { transform: scaleY(0.5); } to { transform: scaleY(1); } }
+ .bar { animation: pulse 1s infinite; }
+ #cases { width: 100px; height: 100px; }
+</style>
+<svg id="cases" viewBox="0 0 24 24"></svg>
+<script>
+ function nextTask() {
+ const { promise, resolve } = Promise.withResolvers();
+ const channel = new MessageChannel();
+ channel.port1.onmessage = resolve;
+ channel.port2.postMessage(null);
+ return promise;
+ }
+
+ const rect = `<rect class="bar" width="5" height="5"/>`;
+ const onTheCompositor = {
+ "a rect": rect,
+ "a rect in a group": `<g>${rect}</g>`,
+ "a rect in a transformed group": `<g transform="translate(1 1)">${rect}</g>`,
+ "a group": `<g class="bar"><rect width="5" height="5"/></g>`,
+ "a rect in a translucent group": `<g opacity="0.5">${rect}</g>`,
+ };
+ // The bounding box of a container includes the transforms of the boxes inside it, so what reads that box needs
+ // the current transform. A <use> element's own translation is part of the matrix the animation would replace.
+ // Resources paint on the main thread each time they are used, and a nested viewport has a transform of its own.
+ const onTheMainThread = {
+ "a rect in a group with a filter": `<filter id="f"><feOffset dx="1"/></filter><g filter="url(#f)">${rect}</g>`,
+ "a rect in a group with a clip path": `<clipPath id="c"><rect width="9" height="9"/></clipPath><g clip-path="url(#c)">${rect}</g>`,
+ "a rect in a group with a mask": `<mask id="m"><rect width="9" height="9" fill="white"/></mask><g mask="url(#m)">${rect}</g>`,
+ "a rect in a group transformed about its fill box": `<g style="transform: rotate(5deg); transform-box: fill-box">${rect}</g>`,
+ "a rect in a mask": `<mask id="n">${rect}</mask><rect mask="url(#n)" width="9" height="9"/>`,
+ "a use element": `<defs><rect id="u" width="5" height="5"/></defs><use class="bar" href="#u" x="1"/>`,
+ "a nested svg": `<svg class="bar" width="5" height="5"><rect width="5" height="5"/></svg>`,
+ };
+
+ asyncTest(async done => {
+ internals.setManualRenderingOpportunities(true);
+ await nextTask();
+ let frameTime = performance.now();
+ const renderFrames = async () => {
+ for (let i = 0; i < 4; ++i) {
+ internals.injectRenderingOpportunity((frameTime += 16));
+ await nextTask();
+ }
+ };
+ const check = async (heading, group) => {
+ println(heading);
+ for (const [name, markup] of Object.entries(group)) {
+ cases.innerHTML = markup;
+ await renderFrames();
+ internals.resetStyleInvalidationCounters();
+ internals.updateCompositorAnimations();
+ println(` ${name}: ${internals.getStyleInvalidationCounters().compositorVisualAnimations === 1}`);
+ }
+ };
+ await check("runs on the compositor:", onTheCompositor);
+ await check("runs on the compositor, though it must not:", onTheMainThread);
+
+ cases.innerHTML = onTheCompositor["a rect in a group"];
+ await renderFrames();
+ await renderFrames();
+ internals.resetStyleInvalidationCounters();
+ internals.resetRenderingSchedulerCounters();
+ await renderFrames();
+ const counters = internals.getStyleInvalidationCounters();
+ const scheduler = internals.getRenderingSchedulerCounters();
+ println(`the main thread leaves the animated style alone: ${counters.animatedStyleOverlayBuilds === 0}`);
+ println(`the main thread asks for no frames: ${counters.animationFramePumpRequests === 0}`);
+ println(`the main thread runs no rendering updates: ${scheduler.updatesRun === 0}`);
+
+ // What a script reads comes from the current transform, while the compositor draws it.
+ cases.innerHTML = `<g><rect width="5" height="10"/></g>`;
+ const group = cases.firstElementChild;
+ const timeline = internals.createInternalAnimationTimeline();
+ const animation = new Animation(new KeyframeEffect(group.firstElementChild,
+ [{ transform: "scaleY(1)" }, { transform: "scaleY(0.5)" }],
+ { duration: 1000, iterations: Infinity }), timeline);
+ animation.play();
+ timeline.setTime(0);
+ await renderFrames();
+ internals.resetStyleInvalidationCounters();
+ internals.updateCompositorAnimations();
+ println(`the box a script reads runs on the compositor: ${internals.getStyleInvalidationCounters().compositorVisualAnimations === 1}`);
+ println(`height of its group at the start: ${group.getBBox().height}`);
+ await nextTask();
+ timeline.setTimeForObservation(500);
+ println(`height of its group halfway: ${group.getBBox().height}`);
+ done();
+ });
+</script>