From 5b1b9dfbf06ea841a080f0a7632f39fe7d70ac19 Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Fri, 18 Mar 2022 17:25:53 -0700 Subject: [PATCH] Apply Aiks transforms in the canvas space, not world space (#91) --- impeller/aiks/aiks_unittests.cc | 42 ++++++++++++++++++++++++++++++--- impeller/aiks/canvas.cc | 2 +- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/impeller/aiks/aiks_unittests.cc b/impeller/aiks/aiks_unittests.cc index 9b9acfa73eb6e..738abc6651056 100644 --- a/impeller/aiks/aiks_unittests.cc +++ b/impeller/aiks/aiks_unittests.cc @@ -132,9 +132,7 @@ TEST_F(AiksTest, ClipsUseCurrentTransform) { canvas.Translate(Vector3(300, 300)); for (int i = 0; i < 15; i++) { - canvas.Translate(-Vector3(300, 300)); canvas.Scale(Vector3(0.8, 0.8)); - canvas.Translate(Vector3(300, 300)); paint.color = colors[i % colors.size()]; canvas.ClipPath(PathBuilder{}.AddCircle({0, 0}, 300).TakePath()); @@ -202,7 +200,7 @@ TEST_F(AiksTest, CanPerformSkew) { Paint red; red.color = Color::Red(); - canvas.Skew(10, 125); + canvas.Skew(2, 5); canvas.DrawRect(Rect::MakeXYWH(0, 0, 100, 100), red); ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); @@ -417,5 +415,43 @@ TEST_F(AiksTest, PaintBlendModeIsRespected) { ASSERT_TRUE(OpenPlaygroundHere(canvas.EndRecordingAsPicture())); } +TEST_F(AiksTest, TransformMultipliesCorrectly) { + Canvas canvas; + ASSERT_MATRIX_NEAR(canvas.GetCurrentTransformation(), Matrix()); + + // clang-format off + canvas.Translate(Vector3(100, 200)); + ASSERT_MATRIX_NEAR( + canvas.GetCurrentTransformation(), + Matrix( 1, 0, 0, 0, + 0, 1, 0, 0, + 0, 0, 1, 0, + 100, 200, 0, 1)); + + canvas.Rotate(Radians(kPiOver2)); + ASSERT_MATRIX_NEAR( + canvas.GetCurrentTransformation(), + Matrix( 0, 1, 0, 0, + -1, 0, 0, 0, + 0, 0, 1, 0, + 100, 200, 0, 1)); + + canvas.Scale(Vector3(2, 3)); + ASSERT_MATRIX_NEAR( + canvas.GetCurrentTransformation(), + Matrix( 0, 2, 0, 0, + -3, 0, 0, 0, + 0, 0, 0, 0, + 100, 200, 0, 1)); + + canvas.Translate(Vector3(100, 200)); + ASSERT_MATRIX_NEAR( + canvas.GetCurrentTransformation(), + Matrix( 0, 2, 0, 0, + -3, 0, 0, 0, + 0, 0, 0, 0, + -500, 400, 0, 1)); +} + } // namespace testing } // namespace impeller diff --git a/impeller/aiks/canvas.cc b/impeller/aiks/canvas.cc index 5eb1d1b12f682..b77beb800b949 100644 --- a/impeller/aiks/canvas.cc +++ b/impeller/aiks/canvas.cc @@ -61,7 +61,7 @@ bool Canvas::Restore() { } void Canvas::Concat(const Matrix& xformation) { - xformation_stack_.back().xformation = xformation * GetCurrentTransformation(); + xformation_stack_.back().xformation = GetCurrentTransformation() * xformation; } void Canvas::ResetTransform() {