From 685ae23ad0d7491d238b2a0caec5a72ee756063a Mon Sep 17 00:00:00 2001 From: Brandon DeRosier Date: Wed, 6 Apr 2022 21:27:21 -0700 Subject: [PATCH] Add explicit scaling methods for Point/Vector2 (#119) --- impeller/aiks/canvas.cc | 6 +++++- impeller/aiks/canvas.h | 2 ++ impeller/entity/contents/text_contents.cc | 2 +- impeller/geometry/geometry_unittests.cc | 25 ++++++++++++++++------- impeller/geometry/matrix.h | 4 ++++ 5 files changed, 30 insertions(+), 9 deletions(-) diff --git a/impeller/aiks/canvas.cc b/impeller/aiks/canvas.cc index 8a7e001a214a9..1949ad8a40d00 100644 --- a/impeller/aiks/canvas.cc +++ b/impeller/aiks/canvas.cc @@ -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)); } @@ -283,7 +287,7 @@ void Canvas::DrawTextFrame(TextFrame text_frame, Point position, Paint paint) { auto text_contents = std::make_shared(); 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() * diff --git a/impeller/aiks/canvas.h b/impeller/aiks/canvas.h index 30eed9b6f07a5..71486def8e95e 100644 --- a/impeller/aiks/canvas.h +++ b/impeller/aiks/canvas.h @@ -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); diff --git a/impeller/entity/contents/text_contents.cc b/impeller/entity/contents/text_contents.cc index 018cebb661eab..f64957f58f14d 100644 --- a/impeller/entity/contents/text_contents.cc +++ b/impeller/entity/contents/text_contents.cc @@ -80,7 +80,7 @@ bool TextContents::Render(const ContentContext& renderer, frame_info.atlas_size = Point{static_cast(atlas->GetTexture()->GetSize().width), static_cast(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. diff --git a/impeller/geometry/geometry_unittests.cc b/impeller/geometry/geometry_unittests.cc index 111dd541a7445..705c54c4ca6cd 100644 --- a/impeller/geometry/geometry_unittests.cc +++ b/impeller/geometry/geometry_unittests.cc @@ -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) { diff --git a/impeller/geometry/matrix.h b/impeller/geometry/matrix.h index 539ae84ae37bd..10051467f45dd 100644 --- a/impeller/geometry/matrix.h +++ b/impeller/geometry/matrix.h @@ -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,