Truly independent web browser
LibWeb: Run transform animations inside an SVG on the compositor
Andreas Kling685886e20dc6
diff --git a/Libraries/LibWeb/DOM/Document.cpp b/Libraries/LibWeb/DOM/Document.cppindex f583765054b..989dab0b52a 100644--- a/Libraries/LibWeb/DOM/Document.cpp+++ b/Libraries/LibWeb/DOM/Document.cpp@@ -8448,6 +8448,49 @@ static Painting::CompositorAnimationKeyframes const& compositor_animation_keyfra});}+// Whether a box of an SVG container paints or places itself by its bounding box, which includes the transforms of the+// boxes inside it.+static bool svg_box_uses_its_bounding_box(Layout::Node const& svg_box)+{+ auto const* element = as_if<Element>(svg_box.dom_node());+ if (!element)+ return false;+ auto computed_style = element->computed_style();+ if (!computed_style)+ return true;+ auto is_none = [&](CSS::PropertyID property) {+ auto value = computed_style->computed_style_value(property);+ return value && value->to_string(CSS::SerializationMode::Normal) == "none"sv;+ };+ if (!is_none(CSS::PropertyID::Filter) || !is_none(CSS::PropertyID::ClipPath) || !is_none(CSS::PropertyID::MaskImage))+ return true;+ if (is_none(CSS::PropertyID::Transform))+ return false;+ auto transform_box = computed_style->computed_style_value(CSS::PropertyID::TransformBox);+ return !transform_box || transform_box->to_keyword() != CSS::Keyword::ViewBox;+}++static bool svg_transform_may_run_on_the_compositor(Element const& target, Layout::Node const& layout_node)+{+ // NB: An outer SVG viewport embedded in HTML uses the CSS box transform independently of its SVG contents.+ if (layout_node.kind() == Layout::RustFFI::NodeKind::SVGSVGBox) {+ auto parent = target.parent_element();+ return parent && parent->namespace_uri() == Namespace::HTML;+ }++ // NB: The compositor has no animations for the documents of SVG images, and replaces the matrix that the x and y+ // of a <use> element are part of.+ if (target.document().is_decoded_svg() || is<SVG::SVGUseElement>(target))+ return false;++ // NB: A box's own bounding box leaves out its own transform, but those of the containers around it include it.+ for (auto const* ancestor = layout_node.parent(); ancestor && ancestor->is_svg_box(); ancestor = ancestor->parent()) {+ if (svg_box_uses_its_bounding_box(*ancestor))+ return false;+ }+ return true;+}+// Builds the compositor animation of one target kind for an effect the compositor could drive, and// keeps it pending with the effect. The checks here are the ones that read the animation objects; the// builder in Rust lowers and validates the keyframes.@@ -8496,12 +8539,8 @@ static Painting::CompositorAnimationEffectState::BuildOutcome build_compositor_aif (!target.has_value())return {};if (target->element().namespace_uri() == Namespace::SVG) {- // NB: An outer SVG viewport embedded in HTML uses the CSS box transform independently of its SVG- // contents. Internal SVG transforms also affect SVG geometry and must remain on the main thread.auto const* layout_node = target->unsafe_layout_node(read);- auto parent = target->element().parent_element();- if (!targets_transform || !layout_node || layout_node->kind() != Layout::RustFFI::NodeKind::SVGSVGBox- || !parent || parent->namespace_uri() != Namespace::HTML)+ if (!targets_transform || !layout_node || !svg_transform_may_run_on_the_compositor(target->element(), *layout_node))return {};}auto monotonic_time_at_anchor_ms = [&]() -> Optional<double> {diff --git a/Tests/LibWeb/Text/expected/css/compositor-svg-internal-transform.txt b/Tests/LibWeb/Text/expected/css/compositor-svg-internal-transform.txtindex 7194249fbc8..cea7813d76a 100644--- a/Tests/LibWeb/Text/expected/css/compositor-svg-internal-transform.txt+++ b/Tests/LibWeb/Text/expected/css/compositor-svg-internal-transform.txt@@ -1,9 +1,9 @@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+ a rect: true+ a rect in a group: true+ a rect in a transformed group: true+ a group: true+ a rect in a translucent group: trueruns on the compositor, though it must not:a rect in a group with a filter: falsea rect in a group with a clip path: false@@ -12,9 +12,9 @@ runs on the compositor, though it must not:a rect in a mask: falsea use element: falsea 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+the main thread leaves the animated style alone: true+the main thread asks for no frames: true+the main thread runs no rendering updates: true+the box a script reads runs on the compositor: trueheight of its group at the start: 10-height of its group halfway: 10+height of its group halfway: 7.5diff --git a/Tests/LibWeb/Text/expected/css/compositor-svg-root-transform.txt b/Tests/LibWeb/Text/expected/css/compositor-svg-root-transform.txtindex c9bbc435fdc..7c695f16365 100644--- a/Tests/LibWeb/Text/expected/css/compositor-svg-root-transform.txt+++ b/Tests/LibWeb/Text/expected/css/compositor-svg-root-transform.txt@@ -2,4 +2,4 @@ outer SVG uses compositor: trueno animated style overlays: trueno main-thread frame pump: truerotation uses CSS box center: true-internal and nested SVG stay on main thread: true+nested SVG stays on the main thread: truediff --git a/Tests/LibWeb/Text/input/css/compositor-svg-root-transform.html b/Tests/LibWeb/Text/input/css/compositor-svg-root-transform.htmlindex 33ec4a23536..ee69e78f8df 100644--- a/Tests/LibWeb/Text/input/css/compositor-svg-root-transform.html+++ b/Tests/LibWeb/Text/input/css/compositor-svg-root-transform.html@@ -5,7 +5,7 @@#spinner { display: block; width: 40px; height: 20px; transform-origin: 50% 50%; }</style><svg id="spinner" viewBox="0 0 80 40"><path d="M0 0H80V40H0Z" fill="green" /></svg>-<svg width="40" height="40"><g id="internal"><rect width="10" height="10" /></g><svg id="nested" width="10" height="10"></svg></svg>+<svg width="40" height="40"><svg id="nested" width="10" height="10"></svg></svg><script>asyncTest(async done => {internals.updateStyle();@@ -35,12 +35,11 @@const rect = spinner.getBoundingClientRect();println(`rotation uses CSS box center: ${Math.abs(rect.x - 10) < 0.1 && Math.abs(rect.y + 10) < 0.1 && Math.abs(rect.width - 20) < 0.1 && Math.abs(rect.height - 40) < 0.1}`);animation.cancel();- makeAnimation(document.getElementById("internal"));makeAnimation(document.getElementById("nested"));timeline.setTime(500);for (let i = 0; i < 4; ++i)await new Promise(requestAnimationFrame);- println(`internal and nested SVG stay on main thread: ${internals.getStyleInvalidationCounters().compositorVisualAnimations === 0}`);+ println(`nested SVG stays on the main thread: ${internals.getStyleInvalidationCounters().compositorVisualAnimations === 0}`);done();});</script>