Skip to content

Commit

Permalink
Add explicit scaling methods for Point/Vector2 (flutter#119)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdero authored and dnfield committed Apr 27, 2022
1 parent 8075d38 commit 685ae23
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 9 deletions.
6 changes: 5 additions & 1 deletion impeller/aiks/canvas.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ void Canvas::Translate(const Vector3& offset) {
Concat(Matrix::MakeTranslation(offset));
}

void Canvas::Scale(const Vector2& scale) {
Concat(Matrix::MakeScale(scale));
}

void Canvas::Scale(const Vector3& scale) {
Concat(Matrix::MakeScale(scale));
}
Expand Down Expand Up @@ -283,7 +287,7 @@ void Canvas::DrawTextFrame(TextFrame text_frame, Point position, Paint paint) {
auto text_contents = std::make_shared<TextContents>();
text_contents->SetTextFrame(std::move(text_frame));
text_contents->SetGlyphAtlas(std::move(lazy_glyph_atlas));
text_contents->SetColor(paint.color.Premultiply());
text_contents->SetColor(paint.color);

Entity entity;
entity.SetTransformation(GetCurrentTransformation() *
Expand Down
2 changes: 2 additions & 0 deletions impeller/aiks/canvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ class Canvas {

void Translate(const Vector3& offset);

void Scale(const Vector2& scale);

void Scale(const Vector3& scale);

void Skew(Scalar sx, Scalar sy);
Expand Down
2 changes: 1 addition & 1 deletion impeller/entity/contents/text_contents.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ bool TextContents::Render(const ContentContext& renderer,
frame_info.atlas_size =
Point{static_cast<Scalar>(atlas->GetTexture()->GetSize().width),
static_cast<Scalar>(atlas->GetTexture()->GetSize().height)};
frame_info.text_color = ToVector(color_);
frame_info.text_color = ToVector(color_.Premultiply());
VS::BindFrameInfo(cmd, pass.GetTransientsBuffer().EmplaceUniform(frame_info));

// Common fragment uniforms for all glyphs.
Expand Down
25 changes: 18 additions & 7 deletions impeller/geometry/geometry_unittests.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,24 @@ TEST(GeometryTest, RotationMatrix) {
}

TEST(GeometryTest, InvertMultMatrix) {
auto rotation = Matrix::MakeRotationZ(Radians{M_PI_4});
auto invert = rotation.Invert();
auto expect = Matrix{0.707, -0.707, 0, 0, //
0.707, 0.707, 0, 0, //
0, 0, 1, 0, //
0, 0, 0, 1};
ASSERT_MATRIX_NEAR(invert, expect);
{
auto rotation = Matrix::MakeRotationZ(Radians{M_PI_4});
auto invert = rotation.Invert();
auto expect = Matrix{0.707, -0.707, 0, 0, //
0.707, 0.707, 0, 0, //
0, 0, 1, 0, //
0, 0, 0, 1};
ASSERT_MATRIX_NEAR(invert, expect);
}
{
auto scale = Matrix::MakeScale(Vector2{2, 4});
auto invert = scale.Invert();
auto expect = Matrix{0.5, 0, 0, 0, //
0, 0.25, 0, 0, //
0, 0, 1, 0, //
0, 0, 0, 1};
ASSERT_MATRIX_NEAR(invert, expect);
}
}

TEST(GeometryTest, MutliplicationMatrix) {
Expand Down
4 changes: 4 additions & 0 deletions impeller/geometry/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ struct Matrix {
// clang-format on
}

static constexpr Matrix MakeScale(const Vector2& s) {
return MakeScale(Vector3(s.x, s.y, 1.0));
}

static constexpr Matrix MakeSkew(Scalar sx, Scalar sy) {
// clang-format off
return Matrix(1.0, sy , 0.0, 0.0,
Expand Down

0 comments on commit 685ae23

Please sign in to comment.