Flutter Engine
The Flutter Engine
fontcache.cpp
Go to the documentation of this file.
1/*
2 * Copyright 2013 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 the GPU font cache
9// It's not necessary to run this with CPU configs
10
11#include "gm/gm.h"
14#include "include/core/SkFont.h"
20#include "include/core/SkSize.h"
27#include "tools/ToolUtils.h"
29
31
33 SkScalar y, const SkFont& font) {
35 canvas->drawString(text, x, y, font, paint);
36 return x + font.measureText(text.c_str(), text.size(), SkTextEncoding::kUTF8);
37}
38
39class FontCacheGM : public skiagm::GM {
40public:
42 : fAllowMultipleTextures(allowMultipleTextures) {
44 }
45
47 options->fGlyphCacheTextureMaximumBytes = 0;
48 options->fAllowMultipleGlyphCacheTextures = fAllowMultipleTextures;
49 }
50
51protected:
52 SkString getName() const override {
53 SkString name("fontcache");
54 if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) {
55 name.append("-mt");
56 }
57 return name;
58 }
59
60 SkISize getISize() override { return SkISize::Make(kSize, kSize); }
61
62 void onOnceBeforeDraw() override {
64 fTypefaces[1] = ToolUtils::CreatePortableTypeface("sans-serif", SkFontStyle::Italic());
66 fTypefaces[3] = ToolUtils::CreatePortableTypeface("sans-serif", SkFontStyle::Normal());
67 fTypefaces[4] = ToolUtils::CreatePortableTypeface("serif", SkFontStyle::Bold());
68 fTypefaces[5] = ToolUtils::CreatePortableTypeface("sans-serif", SkFontStyle::Bold());
69 }
70
71 void onDraw(SkCanvas* canvas) override {
72 this->drawText(canvas);
73 // Debugging tool for GPU.
74 static const bool kShowAtlas = false;
75 if (kShowAtlas) {
76 if (auto dContext = GrAsDirectContext(canvas->recordingContext())) {
77 auto img = dContext->priv().testingOnly_getFontAtlasImage(MaskFormat::kA8);
78 canvas->drawImage(img, 0, 0);
79 }
80 }
81 }
82
83private:
84 void drawText(SkCanvas* canvas) {
85 static const int kSizes[] = {8, 9, 10, 11, 12, 13, 18, 20, 25};
86
87 static const SkString kTexts[] = {SkString("ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
88 SkString("abcdefghijklmnopqrstuvwxyz"),
89 SkString("0123456789"),
90 SkString("!@#$%^&*()<>[]{}")};
93 font.setSubpixel(true);
94
95 static const SkScalar kSubPixelInc = 1 / 2.f;
96 SkScalar x = 0;
97 SkScalar y = 10;
98 SkScalar subpixelX = 0;
99 SkScalar subpixelY = 0;
100 bool offsetX = true;
101
102 if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) {
103 canvas->scale(10, 10);
104 }
105
106 do {
107 for (auto s : kSizes) {
108 auto size = 2 * s;
109 font.setSize(size);
110 for (const auto& typeface : fTypefaces) {
111 font.setTypeface(typeface);
112 for (const auto& text : kTexts) {
113 x = size + draw_string(canvas, text, x + subpixelX, y + subpixelY, font);
115 if (x + 100 > kSize) {
116 x = 0;
118 if (y > kSize) {
119 return;
120 }
121 }
122 }
123 }
124 (offsetX ? subpixelX : subpixelY) += kSubPixelInc;
125 offsetX = !offsetX;
126 }
127 } while (true);
128 }
129
130 inline static constexpr SkScalar kSize = 1280;
131
132 GrContextOptions::Enable fAllowMultipleTextures;
133 sk_sp<SkTypeface> fTypefaces[6];
134 using INHERITED = GM;
135};
136
137//////////////////////////////////////////////////////////////////////////////
138
const char * options
static GrDirectContext * GrAsDirectContext(GrContext_Base *base)
constexpr SkColor SK_ColorLTGRAY
Definition: SkColor.h:118
@ kUTF8
uses bytes to represent UTF-8 or ASCII
#define SkScalarCeilToScalar(x)
Definition: SkScalar.h:31
void onOnceBeforeDraw() override
Definition: fontcache.cpp:62
void onDraw(SkCanvas *canvas) override
Definition: fontcache.cpp:71
SkISize getISize() override
Definition: fontcache.cpp:60
FontCacheGM(GrContextOptions::Enable allowMultipleTextures)
Definition: fontcache.cpp:41
void modifyGrContextOptions(GrContextOptions *options) override
Definition: fontcache.cpp:46
SkString getName() const override
Definition: fontcache.cpp:52
virtual GrRecordingContext * recordingContext() const
Definition: SkCanvas.cpp:1637
void scale(SkScalar sx, SkScalar sy)
Definition: SkCanvas.cpp:1289
void drawString(const char str[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint)
Definition: SkCanvas.h:1803
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition: SkCanvas.h:1528
static constexpr SkFontStyle Italic()
Definition: SkFontStyle.h:72
static constexpr SkFontStyle Bold()
Definition: SkFontStyle.h:69
static constexpr SkFontStyle Normal()
Definition: SkFontStyle.h:66
Definition: SkFont.h:35
@ kAntiAlias
may have transparent pixels on glyph edges
Definition: gm.h:110
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition: gm.cpp:81
void setBGColor(SkColor)
Definition: gm.cpp:159
const Paint & paint
Definition: color_source.cc:38
float SkScalar
Definition: extension.cpp:12
struct MyStruct s
static SkScalar draw_string(SkCanvas *canvas, const SkString &text, SkScalar x, SkScalar y, const SkFont &font)
Definition: fontcache.cpp:32
#define DEF_GM(CODE)
Definition: gm.h:40
std::u16string text
double y
double x
SkFont DefaultPortableFont()
sk_sp< SkTypeface > CreatePortableTypeface(const char *name, SkFontStyle style)
static const size_t kSizes[]
Definition: zone_test.cc:202
DEF_SWITCHES_START aot vmservice shared library name
Definition: switches.h:32
it will be possible to load the file into Perfetto s trace viewer disable asset Prevents usage of any non test fonts unless they were explicitly Loaded via prefetched default font Indicates whether the embedding started a prefetch of the default font manager before creating the engine run In non interactive keep the shell running after the Dart script has completed enable serial On low power devices with low core running concurrent GC tasks on threads can cause them to contend with the UI thread which could potentially lead to jank This option turns off all concurrent GC activities domain network JSON encoded network policy per domain This overrides the DisallowInsecureConnections switch Embedder can specify whether to allow or disallow insecure connections at a domain level old gen heap size
Definition: switches.h:259
font
Font Metadata and Metrics.
MaskFormat
Definition: AtlasTypes.h:98
SkScalar offsetX
Definition: SkSize.h:16
static constexpr SkISize Make(int32_t w, int32_t h)
Definition: SkSize.h:20