| Flutter Impeller |

dl_golden_blur_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 "impeller/display_list/dl_golden_unittests.h"
6
7 #include "flutter/display_list/dl_builder.h"
8 #include "flutter/display_list/effects/dl_mask_filter.h"
9 #include "flutter/testing/testing.h"
10 #include "gtest/gtest.h"
11 #include "impeller/typographer/backends/skia/text_frame_skia.h"
12 #include "txt/platform.h"
13
14 namespace flutter {
15 namespace testing {
16
17 using impeller::Font;
18
19 namespace {
20 struct TextRenderOptions {
21 bool stroke = false;
22 SkScalar font_size = 50;
23 DlColor color = DlColor::kYellow();
24 std::shared_ptr<DlMaskFilter> mask_filter;
25 };
26
27 bool RenderTextInCanvasSkia(DlCanvas* canvas,
28 const std::string& text,
29 const std::string_view& font_fixture,
30 SkPoint position,
31 const TextRenderOptions& options = {}) {
32 auto c_font_fixture = std::string(font_fixture);
33 auto mapping = flutter::testing::OpenFixtureAsSkData(c_font_fixture.c_str());
34 if (!mapping) {
35 return false;
36 }
37 sk_sp<SkFontMgr> font_mgr = txt::GetDefaultFontManager();
38 SkFont sk_font(font_mgr->makeFromData(mapping), options.font_size);
39 auto blob = SkTextBlob::MakeFromString(text.c_str(), sk_font);
40 if (!blob) {
41 return false;
42 }
43
44 auto frame = impeller::MakeTextFrameFromTextBlobSkia(blob);
45
46 DlPaint text_paint;
47 text_paint.setColor(options.color);
48 text_paint.setMaskFilter(options.mask_filter);
49 // text_paint.mask_blur_descriptor = options.mask_blur_descriptor;
50 // text_paint.stroke_width = 1;
51 // text_paint.style =
52 // options.stroke ? Paint::Style::kStroke : Paint::Style::kFill;
53 canvas->DrawTextFrame(frame, position.x(), position.y(), text_paint);
54 return true;
55 }
56
57 } // namespace
58
59 TEST_P(DlGoldenTest, TextBlurMaskFilterRespectCTM) {
60 impeller::Point content_scale = GetContentScale();
61 auto draw = [&](DlCanvas* canvas,
62 const std::vector<std::unique_ptr<DlImage>>& images) {
63 canvas->DrawColor(DlColor(0xff111111));
64 canvas->Scale(content_scale.x, content_scale.y);
65 canvas->Scale(2, 2);
66 TextRenderOptions options;
67 options.mask_filter =
68 DlBlurMaskFilter::Make(DlBlurStyle::kNormal, /*sigma=*/10,
69 /*respect_ctm=*/true);
70 ASSERT_TRUE(RenderTextInCanvasSkia(canvas, "hello world",
71 "Roboto-Regular.ttf",
72 SkPoint::Make(101, 101), options));
73 options.mask_filter = nullptr;
74 options.color = DlColor::kRed();
75 ASSERT_TRUE(RenderTextInCanvasSkia(canvas, "hello world",
76 "Roboto-Regular.ttf",
77 SkPoint::Make(100, 100), options));
78 };
79
80 DisplayListBuilder builder;
81 draw(&builder, /*images=*/{});
82
83 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
84 }
85
86 TEST_P(DlGoldenTest, TextBlurMaskFilterDisrespectCTM) {
87 impeller::Point content_scale = GetContentScale();
88 auto draw = [&](DlCanvas* canvas,
89 const std::vector<std::unique_ptr<DlImage>>& images) {
90 canvas->DrawColor(DlColor(0xff111111));
91 canvas->Scale(content_scale.x, content_scale.y);
92 canvas->Scale(2, 2);
93 TextRenderOptions options;
94 options.mask_filter =
95 DlBlurMaskFilter::Make(DlBlurStyle::kNormal, /*sigma=*/10,
96 /*respect_ctm=*/false);
97 ASSERT_TRUE(RenderTextInCanvasSkia(canvas, "hello world",
98 "Roboto-Regular.ttf",
99 SkPoint::Make(101, 101), options));
100 options.mask_filter = nullptr;
101 options.color = DlColor::kRed();
102 ASSERT_TRUE(RenderTextInCanvasSkia(canvas, "hello world",
103 "Roboto-Regular.ttf",
104 SkPoint::Make(100, 100), options));
105 };
106
107 DisplayListBuilder builder;
108 draw(&builder, /*images=*/{});
109
110 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
111 }
112
113 } // namespace testing
114 } // namespace flutter
impeller::TPoint::y
Type y
Definition: point.h:31
impeller::Font
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:35
font_size
SkScalar font_size
Definition: dl_golden_blur_unittests.cc:22
flutter::testing::TEST_P
TEST_P(DlGoldenTest, TextBlurMaskFilterRespectCTM)
Definition: dl_golden_blur_unittests.cc:59
text_frame_skia.h
dl_golden_unittests.h
flutter
Definition: dl_golden_blur_unittests.cc:14
impeller::MakeTextFrameFromTextBlobSkia
std::shared_ptr< TextFrame > MakeTextFrameFromTextBlobSkia(const sk_sp< SkTextBlob > &blob)
Definition: text_frame_skia.cc:61
impeller::TPoint::x
Type x
Definition: point.h:30
impeller::testing::RenderTextInCanvasSkia
bool RenderTextInCanvasSkia(const std::shared_ptr< Context > &context, Canvas &canvas, const std::string &text, const std::string_view &font_fixture, TextRenderOptions options={})
Definition: aiks_unittests.cc:550
mask_filter
std::shared_ptr< DlMaskFilter > mask_filter
Definition: dl_golden_blur_unittests.cc:24
impeller::TPoint< Scalar >
color
DlColor color
Definition: dl_golden_blur_unittests.cc:23
stroke
bool stroke
Definition: dl_golden_blur_unittests.cc:21
impeller::DlPlayground
Definition: dl_playground.h:15