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