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.h"
12 
13 #include "GrContext.h"
14 #include "GrContextPriv.h"
15 #include "GrContextOptions.h"
16 #include "SkCanvas.h"
17 #include "SkGraphics.h"
18 #include "SkImage.h"
19 #include "SkTypeface.h"
20 #include "gm.h"
21 #include "sk_tool_utils.h"
22 
draw_string(SkCanvas * canvas,const SkString & text,SkScalar x,SkScalar y,const SkFont & font)23 static SkScalar draw_string(SkCanvas* canvas, const SkString& text, SkScalar x,
24                            SkScalar y, const SkFont& font) {
25     SkPaint paint;
26     canvas->drawString(text, x, y, font, paint);
27     return x + font.measureText(text.c_str(), text.size(), kUTF8_SkTextEncoding);
28 }
29 
30 class FontCacheGM : public skiagm::GM {
31 public:
FontCacheGM(GrContextOptions::Enable allowMultipleTextures)32     FontCacheGM(GrContextOptions::Enable allowMultipleTextures)
33         : fAllowMultipleTextures(allowMultipleTextures) {
34         this->setBGColor(SK_ColorLTGRAY);
35     }
36 
modifyGrContextOptions(GrContextOptions * options)37     void modifyGrContextOptions(GrContextOptions* options) override {
38         options->fGlyphCacheTextureMaximumBytes = 0;
39         options->fAllowMultipleGlyphCacheTextures = fAllowMultipleTextures;
40     }
41 
42 protected:
onShortName()43     SkString onShortName() override {
44         SkString name("fontcache");
45         if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) {
46             name.append("-mt");
47         }
48         return name;
49     }
50 
onISize()51     SkISize onISize() override { return SkISize::Make(kSize, kSize); }
52 
onOnceBeforeDraw()53     void onOnceBeforeDraw() override {
54         fTypefaces[0] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Italic());
55         fTypefaces[1] = sk_tool_utils::create_portable_typeface("sans-serif",SkFontStyle::Italic());
56         fTypefaces[2] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Normal());
57         fTypefaces[3] =
58                 sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Normal());
59         fTypefaces[4] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Bold());
60         fTypefaces[5] = sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Bold());
61     }
62 
onDraw(SkCanvas * canvas)63     void onDraw(SkCanvas* canvas) override {
64         GrRenderTargetContext* renderTargetContext =
65             canvas->internal_private_accessTopLayerRenderTargetContext();
66         if (!renderTargetContext) {
67             skiagm::GM::DrawGpuOnlyMessage(canvas);
68             return;
69         }
70 
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->contextPriv().getFontAtlasImage_ForTesting(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