From 424e896e09ff525a671a87139975f2d1e0bf2e59 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Wed, 23 Oct 2024 14:53:32 -0700 Subject: [PATCH] [Impeller] libImpeller: Allow appending to the transformation stack. Currently @lyceel has a workaround where they get the transform, create a new one, and call SetTransform. They would like to avoid this. Fixes https://github.com/flutter/flutter/issues/156604 --- impeller/toolkit/interop/dl_builder.cc | 5 +++++ impeller/toolkit/interop/dl_builder.h | 2 ++ impeller/toolkit/interop/impeller.cc | 6 ++++++ impeller/toolkit/interop/impeller.h | 12 ++++++++++++ impeller/toolkit/interop/impeller_unittests.cc | 9 +++++++++ 5 files changed, 34 insertions(+) diff --git a/impeller/toolkit/interop/dl_builder.cc b/impeller/toolkit/interop/dl_builder.cc index c0c5885354e12..75b389bb6bc8b 100644 --- a/impeller/toolkit/interop/dl_builder.cc +++ b/impeller/toolkit/interop/dl_builder.cc @@ -55,6 +55,11 @@ void DisplayListBuilder::SetTransform(const Matrix& matrix) { builder_.SetTransform(&sk_matrix); } +void DisplayListBuilder::Transform(const Matrix& matrix) { + const auto sk_matrix = SkM44::ColMajor(matrix.m); + builder_.Transform(&sk_matrix); +} + void DisplayListBuilder::ResetTransform() { builder_.TransformReset(); } diff --git a/impeller/toolkit/interop/dl_builder.h b/impeller/toolkit/interop/dl_builder.h index 59468d37e4864..142ff0635129a 100644 --- a/impeller/toolkit/interop/dl_builder.h +++ b/impeller/toolkit/interop/dl_builder.h @@ -51,6 +51,8 @@ class DisplayListBuilder final void SetTransform(const Matrix& matrix); + void Transform(const Matrix& matrix); + void ResetTransform(); uint32_t GetSaveCount() const; diff --git a/impeller/toolkit/interop/impeller.cc b/impeller/toolkit/interop/impeller.cc index 931b2a71025a7..31135453b09b4 100644 --- a/impeller/toolkit/interop/impeller.cc +++ b/impeller/toolkit/interop/impeller.cc @@ -167,6 +167,12 @@ void ImpellerDisplayListBuilderSetTransform(ImpellerDisplayListBuilder builder, GetPeer(builder)->SetTransform(ToImpellerType(*transform)); } +IMPELLER_EXTERN_C +void ImpellerDisplayListBuilderTransform(ImpellerDisplayListBuilder builder, + const ImpellerMatrix* transform) { + GetPeer(builder)->Transform(ToImpellerType(*transform)); +} + IMPELLER_EXTERN_C void ImpellerDisplayListBuilderGetTransform(ImpellerDisplayListBuilder builder, ImpellerMatrix* out_transform) { diff --git a/impeller/toolkit/interop/impeller.h b/impeller/toolkit/interop/impeller.h index f7fcf9cc5b913..e009ac475eb56 100644 --- a/impeller/toolkit/interop/impeller.h +++ b/impeller/toolkit/interop/impeller.h @@ -1643,6 +1643,18 @@ void ImpellerDisplayListBuilderTranslate( float x_translation, float y_translation); +//------------------------------------------------------------------------------ +/// @brief Appends the the provided transformation to the transformation +/// already on the save stack. +/// +/// @param[in] builder The builder. +/// @param[in] transform The transform to append. +/// +IMPELLER_EXPORT +void ImpellerDisplayListBuilderTransform( + ImpellerDisplayListBuilder IMPELLER_NONNULL builder, + const ImpellerMatrix* IMPELLER_NONNULL transform); + //------------------------------------------------------------------------------ /// @brief Clear the transformation on top of the save stack and replace it /// with a new value. diff --git a/impeller/toolkit/interop/impeller_unittests.cc b/impeller/toolkit/interop/impeller_unittests.cc index a386aaf956359..83a084e25ebb4 100644 --- a/impeller/toolkit/interop/impeller_unittests.cc +++ b/impeller/toolkit/interop/impeller_unittests.cc @@ -69,6 +69,15 @@ TEST_P(InteropPlaygroundTest, CanDrawRect) { color = {1.0, 0.0, 0.0, 1.0}; ImpellerPaintSetColor(paint.GetC(), &color); ImpellerDisplayListBuilderTranslate(builder.GetC(), 110, 210); + ImpellerMatrix scale_transform = { + // clang-format off + 2.0, 0.0, 0.0, 0.0, // + 0.0, 2.0, 0.0, 0.0, // + 0.0, 0.0, 1.0, 0.0, // + 0.0, 0.0, 0.0, 1.0, // + // clang-format on + }; + ImpellerDisplayListBuilderTransform(builder.GetC(), &scale_transform); ImpellerDisplayListBuilderDrawRect(builder.GetC(), &rect, paint.GetC()); auto dl = Adopt( ImpellerDisplayListBuilderCreateDisplayListNew(builder.GetC()));