| // Copyright 2013 The Flutter Authors. All rights reserved. |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| |
| #include "flutter/testing/testing.h" |
| #include "impeller/display_list/aiks_unittests.h" |
| #include "impeller/display_list/canvas.h" |
| #include "impeller/geometry/geometry_asserts.h" |
| |
| namespace impeller { |
| namespace testing { |
| |
| std::unique_ptr<Canvas> CreateTestCanvas( |
| ContentContext& context, |
| std::optional<Rect> cull_rect = std::nullopt) { |
| RenderTarget render_target = context.GetRenderTargetCache()->CreateOffscreen( |
| *context.GetContext(), {1, 1}, 1); |
| |
| if (cull_rect.has_value()) { |
| return std::make_unique<Canvas>(context, render_target, false, |
| cull_rect.value()); |
| } |
| return std::make_unique<Canvas>(context, render_target, false); |
| } |
| |
| TEST_P(AiksTest, TransformMultipliesCorrectly) { |
| ContentContext context(GetContext(), nullptr); |
| auto canvas = CreateTestCanvas(context); |
| |
| ASSERT_MATRIX_NEAR(canvas->GetCurrentTransform(), Matrix()); |
| |
| // clang-format off |
| canvas->Translate(Vector3(100, 200)); |
| ASSERT_MATRIX_NEAR( |
| canvas->GetCurrentTransform(), |
| 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->GetCurrentTransform(), |
| 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->GetCurrentTransform(), |
| 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->GetCurrentTransform(), |
| Matrix( 0, 2, 0, 0, |
| -3, 0, 0, 0, |
| 0, 0, 0, 0, |
| -500, 400, 0, 1)); |
| // clang-format on |
| } |
| |
| TEST_P(AiksTest, CanvasCanPushPopCTM) { |
| ContentContext context(GetContext(), nullptr); |
| auto canvas = CreateTestCanvas(context); |
| |
| ASSERT_EQ(canvas->GetSaveCount(), 1u); |
| ASSERT_EQ(canvas->Restore(), false); |
| |
| canvas->Translate(Size{100, 100}); |
| canvas->Save(10); |
| ASSERT_EQ(canvas->GetSaveCount(), 2u); |
| ASSERT_MATRIX_NEAR(canvas->GetCurrentTransform(), |
| Matrix::MakeTranslation({100.0, 100.0, 0.0})); |
| ASSERT_TRUE(canvas->Restore()); |
| ASSERT_EQ(canvas->GetSaveCount(), 1u); |
| ASSERT_MATRIX_NEAR(canvas->GetCurrentTransform(), |
| Matrix::MakeTranslation({100.0, 100.0, 0.0})); |
| } |
| |
| TEST_P(AiksTest, CanvasCTMCanBeUpdated) { |
| ContentContext context(GetContext(), nullptr); |
| auto canvas = CreateTestCanvas(context); |
| |
| Matrix identity; |
| ASSERT_MATRIX_NEAR(canvas->GetCurrentTransform(), identity); |
| canvas->Translate(Size{100, 100}); |
| ASSERT_MATRIX_NEAR(canvas->GetCurrentTransform(), |
| Matrix::MakeTranslation({100.0, 100.0, 0.0})); |
| } |
| |
| } // namespace testing |
| } // namespace impeller |