Skip to content

Commit

Permalink
Apply Aiks transforms in the canvas space, not world space (flutter#91)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdero authored and dnfield committed Apr 27, 2022
1 parent 1d6dc1e commit 5b1b9df
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
42 changes: 39 additions & 3 deletions impeller/aiks/aiks_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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()));
Expand Down Expand Up @@ -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
2 changes: 1 addition & 1 deletion impeller/aiks/canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down

0 comments on commit 5b1b9df

Please sign in to comment.