Flutter Engine
The Flutter Engine
fontregen.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2018 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8// GM to stress TextBlob regeneration and the GPU font cache
9// It's not necessary to run this with CPU configs
10//
11// The point here is to draw a set of text that will fit in one Plot, and then some large
12// text. After a flush we draw the first set of text again with a slightly different color,
13// and then enough new large text to spill the entire atlas. What *should* happen is that
14// the Plot with the first set of text will not get overwritten by the new large text.
15
16#include "gm/gm.h"
19#include "include/core/SkFont.h"
26#include "include/core/SkSize.h"
37#include "tools/ToolUtils.h"
39
40#if defined(SK_GRAPHITE)
42#endif
43
44using namespace skia_private;
46
48 size_t len = text.size();
51
52 font.textToGlyphs(text.c_str(), len, SkTextEncoding::kUTF8, glyphs.get(), len);
53 font.getXPos(glyphs.get(), len, pos.get());
54 return SkTextBlob::MakeFromPosTextH(text.c_str(), len, pos.get(), 0, font);
55}
56
57class FontRegenGM : public skiagm::GM {
58
59 void modifyGrContextOptions(GrContextOptions* options) override {
60 options->fGlyphCacheTextureMaximumBytes = 0;
61 options->fAllowMultipleGlyphCacheTextures = GrContextOptions::Enable::kNo;
62 }
63
64#if defined(SK_GRAPHITE)
66 options->fGlyphCacheTextureMaximumBytes = 0;
67 options->fAllowMultipleAtlasTextures = false;
68 }
69#endif
70
71 SkString getName() const override { return SkString("fontregen"); }
72
73 SkISize getISize() override { return {kSize, kSize}; }
74
75 void onOnceBeforeDraw() override {
77
79
80 static const SkString kTexts[] = {
81 SkString("abcdefghijklmnopqrstuvwxyz"),
82 SkString("ABCDEFGHI"),
83 SkString("NOPQRSTUV")
84 };
85
88 font.setSubpixel(false);
89 font.setSize(80);
90 font.setTypeface(tf);
91
92 fBlobs[0] = make_blob(kTexts[0], font);
93 font.setSize(162);
94 fBlobs[1] = make_blob(kTexts[1], font);
95 fBlobs[2] = make_blob(kTexts[2], font);
96 }
97
98 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
99
100
102 paint.setColor(SK_ColorBLACK);
103 canvas->drawTextBlob(fBlobs[0], 10, 80, paint);
104 canvas->drawTextBlob(fBlobs[1], 10, 225, paint);
105
106 auto dContext = GrAsDirectContext(canvas->recordingContext());
107 if (dContext) {
108 dContext->flushAndSubmit();
109 }
110
111 paint.setColor(0xFF010101);
112 canvas->drawTextBlob(fBlobs[0], 10, 305, paint);
113 canvas->drawTextBlob(fBlobs[2], 10, 465, paint);
114
115 // Debugging tool for GPU.
116 static const bool kShowAtlas = false;
117 if (kShowAtlas && dContext) {
118 auto img = dContext->priv().testingOnly_getFontAtlasImage(MaskFormat::kA8);
119 canvas->drawImage(img, 200, 0);
120 }
121
122 return DrawResult::kOk;
123 }
124
125private:
126 inline static constexpr int kSize = 512;
127
128 sk_sp<SkTextBlob> fBlobs[3];
129 using INHERITED = GM;
130};
131
132//////////////////////////////////////////////////////////////////////////////
133
134DEF_GM(return new FontRegenGM())
135
136///////////////////////////////////////////////////////////////////////////////
137
138class BadAppleGM : public skiagm::GM {
139 SkString getName() const override { return SkString("badapple"); }
140
141 SkISize getISize() override { return {kSize, kSize}; }
142
143 void onOnceBeforeDraw() override {
144 this->setBGColor(SK_ColorWHITE);
145 auto fm = ToolUtils::TestFontMgr();
146
147 static const SkString kTexts[] = {
148 SkString("Meet"),
149 SkString("iPad Pro"),
150 };
151
154 font.setSubpixel(true);
155 font.setSize(256);
156
157 fBlobs[0] = make_blob(kTexts[0], font);
158 fBlobs[1] = make_blob(kTexts[1], font);
159 }
160
161 void onDraw(SkCanvas* canvas) override {
163 paint.setColor(0xFF111111);
164 canvas->drawTextBlob(fBlobs[0], 10, 260, paint);
165 canvas->drawTextBlob(fBlobs[1], 10, 500, paint);
166 }
167
168private:
169 inline static constexpr int kSize = 512;
170
171 sk_sp<SkTextBlob> fBlobs[3];
172 using INHERITED = GM;
173};
174
175//////////////////////////////////////////////////////////////////////////////
176
177DEF_GM(return new BadAppleGM())
const char * options
uint16_t glyphs[5]
Definition: FontMgrTest.cpp:46
static GrDirectContext * GrAsDirectContext(GrContext_Base *base)
SkPoint pos
constexpr SkColor SK_ColorLTGRAY
Definition: SkColor.h:118
constexpr SkColor SK_ColorBLACK
Definition: SkColor.h:103
constexpr SkColor SK_ColorWHITE
Definition: SkColor.h:122
@ kUTF8
uses bytes to represent UTF-8 or ASCII
virtual GrRecordingContext * recordingContext() const
Definition: SkCanvas.cpp:1637
void drawTextBlob(const SkTextBlob *blob, SkScalar x, SkScalar y, const SkPaint &paint)
Definition: SkCanvas.cpp:2484
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
static constexpr SkFontStyle Normal()
Definition: SkFontStyle.h:66
Definition: SkFont.h:35
@ kAntiAlias
may have transparent pixels on glyph edges
@ kSubpixelAntiAlias
glyph positioned in pixel using transparency
static sk_sp< SkTextBlob > MakeFromPosTextH(const void *text, size_t byteLength, const SkScalar xpos[], SkScalar constY, const SkFont &font, SkTextEncoding encoding=SkTextEncoding::kUTF8)
Definition: SkTextBlob.cpp:817
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
virtual void modifyGraphiteContextOptions(skgpu::graphite::ContextOptions *) const
Definition: gm.h:179
void setBGColor(SkColor)
Definition: gm.cpp:159
const Paint & paint
Definition: color_source.cc:38
static sk_sp< SkTextBlob > make_blob(const SkString &text, const SkFont &font)
Definition: fontregen.cpp:47
#define DEF_GM(CODE)
Definition: gm.h:40
std::u16string text
constexpr int kSize
SkFont DefaultPortableFont()
sk_sp< SkTypeface > CreatePortableTypeface(const char *name, SkFontStyle style)
sk_sp< SkFontMgr > TestFontMgr()
font
Font Metadata and Metrics.
MaskFormat
Definition: AtlasTypes.h:98
DrawResult
Definition: gm.h:104
Definition: SkSize.h:16