Flutter Engine
The Flutter Engine
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
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"
12#include "txt/platform.h"
13
14namespace flutter {
15namespace testing {
16
17using impeller::Font;
18
19namespace {
20struct TextRenderOptions {
21 bool stroke = false;
24 std::shared_ptr<DlMaskFilter> mask_filter;
25};
26
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 }
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
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
59TEST_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 =
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
81 draw(&builder, /*images=*/{});
82
83 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
84}
85
86TEST_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 =
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
108 draw(&builder, /*images=*/{});
109
110 ASSERT_TRUE(OpenPlaygroundHere(builder.Build()));
111}
112
113} // namespace testing
114} // namespace flutter
const char * options
static void draw(SkCanvas *canvas, SkRect &target, int x, int y)
Definition: aaclip.cpp:27
sk_sp< SkTypeface > makeFromData(sk_sp< SkData >, int ttcIndex=0) const
Definition: SkFontMgr.cpp:120
Definition: SkFont.h:35
static sk_sp< SkTextBlob > MakeFromString(const char *string, const SkFont &font, SkTextEncoding encoding=SkTextEncoding::kUTF8)
Definition: SkTextBlob.h:115
static std::shared_ptr< DlMaskFilter > Make(DlBlurStyle style, SkScalar sigma, bool respect_ctm=true)
Developer-facing API for rendering anything within the engine.
Definition: dl_canvas.h:38
virtual void Scale(SkScalar sx, SkScalar sy)=0
virtual void DrawColor(DlColor color, DlBlendMode mode=DlBlendMode::kSrcOver)=0
DlPaint & setColor(DlColor color)
Definition: dl_paint.h:70
DlPaint & setMaskFilter(const std::shared_ptr< DlMaskFilter > &filter)
Definition: dl_paint.h:170
Describes a typeface along with any modifications to its intrinsic properties.
Definition: font.h:35
DlColor color
SkScalar font_size
std::shared_ptr< DlMaskFilter > mask_filter
double frame
Definition: examples.cpp:31
float SkScalar
Definition: extension.cpp:12
std::u16string text
std::array< MockImage, 3 > images
Definition: mock_vulkan.cc:41
sk_sp< SkData > OpenFixtureAsSkData(const std::string &fixture_name)
Opens a fixture of the given file name and returns a Skia SkData holding its contents.
Definition: testing.cc:64
TEST_P(DlGoldenTest, TextBlurMaskFilterRespectCTM)
@ kNormal
fuzzy inside and outside
bool RenderTextInCanvasSkia(const std::shared_ptr< Context > &context, Canvas &canvas, const std::string &text, const std::string_view &font_fixture, TextRenderOptions options={})
std::shared_ptr< TextFrame > MakeTextFrameFromTextBlobSkia(const sk_sp< SkTextBlob > &blob)
flutter::DlCanvas DlCanvas
flutter::DlColor DlColor
flutter::DlPaint DlPaint
sk_sp< SkFontMgr > GetDefaultFontManager(uint32_t font_initialization_data)
Definition: platform.cc:17
static constexpr SkPoint Make(float x, float y)
Definition: SkPoint_impl.h:173
constexpr float y() const
Definition: SkPoint_impl.h:187
constexpr float x() const
Definition: SkPoint_impl.h:181
static constexpr DlColor kYellow()
Definition: dl_color.h:29
static constexpr DlColor kRed()
Definition: dl_color.h:24