From 75e0b21940e8e285b77874d4c08ac6b9b1eaa177 Mon Sep 17 00:00:00 2001 From: Sam Atkins Date: Tue, 19 Jul 2022 15:18:20 +0100 Subject: [PATCH] LibWeb: Store calculated transformation matrix on the StackingContext This is mainly so we can easily read that matrix later, but also has the benefit of only calculating the matrix once, instead of every time we paint. :^) --- .../Libraries/LibWeb/Painting/StackingContext.cpp | 14 +++++++------- .../Libraries/LibWeb/Painting/StackingContext.h | 5 ++++- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/Userland/Libraries/LibWeb/Painting/StackingContext.cpp b/Userland/Libraries/LibWeb/Painting/StackingContext.cpp index c205b6a23a..8f3a6c4963 100644 --- a/Userland/Libraries/LibWeb/Painting/StackingContext.cpp +++ b/Userland/Libraries/LibWeb/Painting/StackingContext.cpp @@ -1,5 +1,6 @@ /* * Copyright (c) 2020-2022, Andreas Kling + * Copyright (c) 2022, Sam Atkins * * SPDX-License-Identifier: BSD-2-Clause */ @@ -28,6 +29,7 @@ static void paint_node(Layout::Node const& layout_node, PaintContext& context, P StackingContext::StackingContext(Layout::Box& box, StackingContext* parent) : m_box(box) + , m_transform(combine_transformations(m_box.computed_values().transformations())) , m_parent(parent) { VERIFY(m_parent != this); @@ -269,10 +271,9 @@ Gfx::FloatMatrix4x4 StackingContext::combine_transformations(Vector const& transformations) const +Gfx::AffineTransform StackingContext::affine_transform_matrix() const { - auto matrix = combine_transformations(transformations); - auto* m = matrix.elements(); + auto* m = m_transform.elements(); return Gfx::AffineTransform(m[0][0], m[1][0], m[0][1], m[1][1], m[0][3], m[1][3]); } @@ -287,7 +288,7 @@ void StackingContext::paint(PaintContext& context) const if (opacity == 0.0f) return; - auto affine_transform = combine_transformations_2d(m_box.computed_values().transformations()); + auto affine_transform = affine_transform_matrix(); if (opacity < 1.0f || !affine_transform.is_identity()) { auto bitmap_or_error = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, context.painter().target()->size()); @@ -332,8 +333,7 @@ Optional StackingContext::hit_test(Gfx::FloatPoint const& positio return {}; auto transform_origin = this->transform_origin(); - auto affine_transform = combine_transformations_2d(m_box.computed_values().transformations()); - auto transformed_position = affine_transform.inverse().value_or({}).map(position - transform_origin) + transform_origin; + auto transformed_position = affine_transform_matrix().inverse().value_or({}).map(position - transform_origin) + transform_origin; // FIXME: Support more overflow variations. if (paintable().computed_values().overflow_x() == CSS::Overflow::Hidden && paintable().computed_values().overflow_y() == CSS::Overflow::Hidden) { @@ -446,7 +446,7 @@ void StackingContext::dump(int indent) const builder.append("auto"sv); builder.append(')'); - auto affine_transform = combine_transformations_2d(m_box.computed_values().transformations()); + auto affine_transform = affine_transform_matrix(); if (!affine_transform.is_identity()) { builder.appendff(", transform: {}", affine_transform); } diff --git a/Userland/Libraries/LibWeb/Painting/StackingContext.h b/Userland/Libraries/LibWeb/Painting/StackingContext.h index 9b26eef0fd..13e9d677f6 100644 --- a/Userland/Libraries/LibWeb/Painting/StackingContext.h +++ b/Userland/Libraries/LibWeb/Painting/StackingContext.h @@ -34,19 +34,22 @@ public: void paint(PaintContext&) const; Optional hit_test(Gfx::FloatPoint const&, HitTestType) const; + Gfx::FloatMatrix4x4 const& transform_matrix() const { return m_transform; } + Gfx::AffineTransform affine_transform_matrix() const; + void dump(int indent = 0) const; void sort(); private: Layout::Box& m_box; + Gfx::FloatMatrix4x4 m_transform; StackingContext* const m_parent { nullptr }; Vector m_children; void paint_internal(PaintContext&) const; Gfx::FloatMatrix4x4 get_transformation_matrix(CSS::Transformation const& transformation) const; Gfx::FloatMatrix4x4 combine_transformations(Vector const& transformations) const; - Gfx::AffineTransform combine_transformations_2d(Vector const& transformations) const; Gfx::FloatPoint transform_origin() const; };