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"
12 #include "include/core/SkCanvas.h"
13 #include "include/core/SkColor.h"
14 #include "include/core/SkFont.h"
15 #include "include/core/SkFontStyle.h"
16 #include "include/core/SkFontTypes.h"
17 #include "include/core/SkPaint.h"
18 #include "include/core/SkRefCnt.h"
19 #include "include/core/SkScalar.h"
20 #include "include/core/SkSize.h"
21 #include "include/core/SkString.h"
22 #include "include/core/SkTypeface.h"
23 #include "include/gpu/GrContextOptions.h"
24 #include "include/gpu/GrDirectContext.h"
25 #include "include/private/GrTypesPriv.h"
26 #include "src/gpu/GrDirectContextPriv.h"
27 #include "tools/ToolUtils.h"
28
draw_string(SkCanvas * canvas,const SkString & text,SkScalar x,SkScalar y,const SkFont & font)29 static SkScalar draw_string(SkCanvas* canvas, const SkString& text, SkScalar x,
30 SkScalar y, const SkFont& font) {
31 SkPaint paint;
32 canvas->drawString(text, x, y, font, paint);
33 return x + font.measureText(text.c_str(), text.size(), SkTextEncoding::kUTF8);
34 }
35
36 class FontCacheGM : public skiagm::GM {
37 public:
FontCacheGM(GrContextOptions::Enable allowMultipleTextures)38 FontCacheGM(GrContextOptions::Enable allowMultipleTextures)
39 : fAllowMultipleTextures(allowMultipleTextures) {
40 this->setBGColor(SK_ColorLTGRAY);
41 }
42
modifyGrContextOptions(GrContextOptions * options)43 void modifyGrContextOptions(GrContextOptions* options) override {
44 options->fGlyphCacheTextureMaximumBytes = 0;
45 options->fAllowMultipleGlyphCacheTextures = fAllowMultipleTextures;
46 }
47
48 protected:
onShortName()49 SkString onShortName() override {
50 SkString name("fontcache");
51 if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) {
52 name.append("-mt");
53 }
54 return name;
55 }
56
onISize()57 SkISize onISize() override { return SkISize::Make(kSize, kSize); }
58
onOnceBeforeDraw()59 void onOnceBeforeDraw() override {
60 fTypefaces[0] = ToolUtils::create_portable_typeface("serif", SkFontStyle::Italic());
61 fTypefaces[1] = ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Italic());
62 fTypefaces[2] = ToolUtils::create_portable_typeface("serif", SkFontStyle::Normal());
63 fTypefaces[3] = ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Normal());
64 fTypefaces[4] = ToolUtils::create_portable_typeface("serif", SkFontStyle::Bold());
65 fTypefaces[5] = ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Bold());
66 }
67
onDraw(SkCanvas * canvas)68 void onDraw(SkCanvas* canvas) override {
69 this->drawText(canvas);
70 // Debugging tool for GPU.
71 static const bool kShowAtlas = false;
72 if (kShowAtlas) {
73 if (auto dContext = GrAsDirectContext(canvas->recordingContext())) {
74 auto img = dContext->priv().testingOnly_getFontAtlasImage(kA8_GrMaskFormat);
75 canvas->drawImage(img, 0, 0);
76 }
77 }
78 }
79
80 private:
drawText(SkCanvas * canvas)81 void drawText(SkCanvas* canvas) {
82 static const int kSizes[] = {8, 9, 10, 11, 12, 13, 18, 20, 25};
83
84 static const SkString kTexts[] = {SkString("ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
85 SkString("abcdefghijklmnopqrstuvwxyz"),
86 SkString("0123456789"),
87 SkString("!@#$%^&*()<>[]{}")};
88 SkFont font;
89 font.setEdging(SkFont::Edging::kAntiAlias);
90 font.setSubpixel(true);
91
92 static const SkScalar kSubPixelInc = 1 / 2.f;
93 SkScalar x = 0;
94 SkScalar y = 10;
95 SkScalar subpixelX = 0;
96 SkScalar subpixelY = 0;
97 bool offsetX = true;
98
99 if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) {
100 canvas->scale(10, 10);
101 }
102
103 do {
104 for (auto s : kSizes) {
105 auto size = 2 * s;
106 font.setSize(size);
107 for (const auto& typeface : fTypefaces) {
108 font.setTypeface(typeface);
109 for (const auto& text : kTexts) {
110 x = size + draw_string(canvas, text, x + subpixelX, y + subpixelY, font);
111 x = SkScalarCeilToScalar(x);
112 if (x + 100 > kSize) {
113 x = 0;
114 y += SkScalarCeilToScalar(size + 3);
115 if (y > kSize) {
116 return;
117 }
118 }
119 }
120 }
121 (offsetX ? subpixelX : subpixelY) += kSubPixelInc;
122 offsetX = !offsetX;
123 }
124 } while (true);
125 }
126
127 inline static constexpr SkScalar kSize = 1280;
128
129 GrContextOptions::Enable fAllowMultipleTextures;
130 sk_sp<SkTypeface> fTypefaces[6];
131 using INHERITED = GM;
132 };
133
134 //////////////////////////////////////////////////////////////////////////////
135
136 DEF_GM(return new FontCacheGM(GrContextOptions::Enable::kNo))
137 DEF_GM(return new FontCacheGM(GrContextOptions::Enable::kYes))
138