xref: /aosp_15_r20/external/skia/gm/fontcache.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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/ganesh/GrContextOptions.h"
24 #include "include/gpu/ganesh/GrDirectContext.h"
25 #include "include/private/gpu/ganesh/GrTypesPriv.h"
26 #include "src/gpu/ganesh/GrDirectContextPriv.h"
27 #include "tools/ToolUtils.h"
28 #include "tools/fonts/FontToolUtils.h"
29 
30 using MaskFormat = skgpu::MaskFormat;
31 
draw_string(SkCanvas * canvas,const SkString & text,SkScalar x,SkScalar y,const SkFont & font)32 static SkScalar draw_string(SkCanvas* canvas, const SkString& text, SkScalar x,
33                            SkScalar y, const SkFont& font) {
34     SkPaint paint;
35     canvas->drawString(text, x, y, font, paint);
36     return x + font.measureText(text.c_str(), text.size(), SkTextEncoding::kUTF8);
37 }
38 
39 class FontCacheGM : public skiagm::GM {
40 public:
FontCacheGM(GrContextOptions::Enable allowMultipleTextures)41     FontCacheGM(GrContextOptions::Enable allowMultipleTextures)
42         : fAllowMultipleTextures(allowMultipleTextures) {
43         this->setBGColor(SK_ColorLTGRAY);
44     }
45 
modifyGrContextOptions(GrContextOptions * options)46     void modifyGrContextOptions(GrContextOptions* options) override {
47         options->fGlyphCacheTextureMaximumBytes = 0;
48         options->fAllowMultipleGlyphCacheTextures = fAllowMultipleTextures;
49     }
50 
51 protected:
getName() const52     SkString getName() const override {
53         SkString name("fontcache");
54         if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) {
55             name.append("-mt");
56         }
57         return name;
58     }
59 
getISize()60     SkISize getISize() override { return SkISize::Make(kSize, kSize); }
61 
onOnceBeforeDraw()62     void onOnceBeforeDraw() override {
63         fTypefaces[0] = ToolUtils::CreatePortableTypeface("serif", SkFontStyle::Italic());
64         fTypefaces[1] = ToolUtils::CreatePortableTypeface("sans-serif", SkFontStyle::Italic());
65         fTypefaces[2] = ToolUtils::CreatePortableTypeface("serif", SkFontStyle::Normal());
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 
onDraw(SkCanvas * canvas)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 
83 private:
drawText(SkCanvas * canvas)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("!@#$%^&*()<>[]{}")};
91         SkFont font = ToolUtils::DefaultPortableFont();
92         font.setEdging(SkFont::Edging::kAntiAlias);
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);
114                         x = SkScalarCeilToScalar(x);
115                         if (x + 100 > kSize) {
116                             x = 0;
117                             y += SkScalarCeilToScalar(size + 3);
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 
139 DEF_GM(return new FontCacheGM(GrContextOptions::Enable::kNo))
140 DEF_GM(return new FontCacheGM(GrContextOptions::Enable::kYes))
141