Flutter Impeller
canvas_unittests.cc
Go to the documentation of this file.
1 // Copyright 2013 The Flutter Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "flutter/testing/testing.h"
9 
10 namespace impeller {
11 namespace testing {
12 
13 std::unique_ptr<Canvas> CreateTestCanvas(
14  ContentContext& context,
15  std::optional<Rect> cull_rect = std::nullopt) {
16  RenderTarget render_target = context.GetRenderTargetCache()->CreateOffscreen(
17  *context.GetContext(), {1, 1}, 1);
18 
19  if (cull_rect.has_value()) {
20  return std::make_unique<Canvas>(context, render_target, false,
21  cull_rect.value());
22  }
23  return std::make_unique<Canvas>(context, render_target, false);
24 }
25 
26 TEST_P(AiksTest, TransformMultipliesCorrectly) {
27  ContentContext context(GetContext(), nullptr);
28  auto canvas = CreateTestCanvas(context);
29 
30  ASSERT_MATRIX_NEAR(canvas->GetCurrentTransform(), Matrix());
31 
32  // clang-format off
33  canvas->Translate(Vector3(100, 200));
35  canvas->GetCurrentTransform(),
36  Matrix( 1, 0, 0, 0,
37  0, 1, 0, 0,
38  0, 0, 1, 0,
39  100, 200, 0, 1));
40 
41  canvas->Rotate(Radians(kPiOver2));
43  canvas->GetCurrentTransform(),
44  Matrix( 0, 1, 0, 0,
45  -1, 0, 0, 0,
46  0, 0, 1, 0,
47  100, 200, 0, 1));
48 
49  canvas->Scale(Vector3(2, 3));
51  canvas->GetCurrentTransform(),
52  Matrix( 0, 2, 0, 0,
53  -3, 0, 0, 0,
54  0, 0, 0, 0,
55  100, 200, 0, 1));
56 
57  canvas->Translate(Vector3(100, 200));
59  canvas->GetCurrentTransform(),
60  Matrix( 0, 2, 0, 0,
61  -3, 0, 0, 0,
62  0, 0, 0, 0,
63  -500, 400, 0, 1));
64  // clang-format on
65 }
66 
67 TEST_P(AiksTest, CanvasCanPushPopCTM) {
68  ContentContext context(GetContext(), nullptr);
69  auto canvas = CreateTestCanvas(context);
70 
71  ASSERT_EQ(canvas->GetSaveCount(), 1u);
72  ASSERT_EQ(canvas->Restore(), false);
73 
74  canvas->Translate(Size{100, 100});
75  canvas->Save(10);
76  ASSERT_EQ(canvas->GetSaveCount(), 2u);
77  ASSERT_MATRIX_NEAR(canvas->GetCurrentTransform(),
78  Matrix::MakeTranslation({100.0, 100.0, 0.0}));
79  ASSERT_TRUE(canvas->Restore());
80  ASSERT_EQ(canvas->GetSaveCount(), 1u);
81  ASSERT_MATRIX_NEAR(canvas->GetCurrentTransform(),
82  Matrix::MakeTranslation({100.0, 100.0, 0.0}));
83 }
84 
85 TEST_P(AiksTest, CanvasCTMCanBeUpdated) {
86  ContentContext context(GetContext(), nullptr);
87  auto canvas = CreateTestCanvas(context);
88 
89  Matrix identity;
90  ASSERT_MATRIX_NEAR(canvas->GetCurrentTransform(), identity);
91  canvas->Translate(Size{100, 100});
92  ASSERT_MATRIX_NEAR(canvas->GetCurrentTransform(),
93  Matrix::MakeTranslation({100.0, 100.0, 0.0}));
94 }
95 
96 } // namespace testing
97 } // namespace impeller
impeller::AiksPlayground
Definition: aiks_playground.h:16
impeller::testing::CreateTestCanvas
std::unique_ptr< Canvas > CreateTestCanvas(ContentContext &context, std::optional< Rect > cull_rect=std::nullopt)
Definition: canvas_unittests.cc:13
geometry_asserts.h
aiks_unittests.h
impeller::Matrix::MakeTranslation
static constexpr Matrix MakeTranslation(const Vector3 &t)
Definition: matrix.h:95
impeller::kPiOver2
constexpr float kPiOver2
Definition: constants.h:32
impeller::TSize< Scalar >
impeller::ContentContext::GetContext
std::shared_ptr< Context > GetContext() const
Definition: content_context.cc:550
impeller::Radians
Definition: scalar.h:43
canvas.h
impeller::RenderTarget
Definition: render_target.h:38
ASSERT_MATRIX_NEAR
#define ASSERT_MATRIX_NEAR(a, b)
Definition: geometry_asserts.h:189
impeller::testing::TEST_P
TEST_P(AiksTest, DrawAtlasNoColor)
Definition: aiks_dl_atlas_unittests.cc:78
impeller::ContentContext::GetRenderTargetCache
const std::shared_ptr< RenderTargetAllocator > & GetRenderTargetCache() const
Definition: content_context.h:725
impeller
Definition: allocation.cc:12
impeller::ContentContext
Definition: content_context.h:366
impeller::Matrix
A 4x4 matrix using column-major storage.
Definition: matrix.h:37
impeller::Vector3
Definition: vector.h:20