blob: a2aa0c482e8bfedd569b7d8dccf18762cf620e07 [file] [log] [blame]
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +00001/*
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
Jim Van Verthfc4f7682018-01-25 16:26:25 -05008// GM to stress the GPU font cache
9// It's not necessary to run this with CPU configs
10
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "gm/gm.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/core/SkTypeface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/gpu/GrContextOptions.h"
Robert Phillipsb27b38b2020-07-10 16:23:47 -040024#include "include/gpu/GrDirectContext.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040025#include "include/private/GrTypesPriv.h"
Adlai Hollera0693042020-10-14 11:23:11 -040026#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "tools/ToolUtils.h"
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000028
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000029static SkScalar draw_string(SkCanvas* canvas, const SkString& text, SkScalar x,
Mike Reed94cca602018-12-02 16:04:27 -050030 SkScalar y, const SkFont& font) {
31 SkPaint paint;
Hal Canary89a644b2019-01-07 09:36:09 -050032 canvas->drawString(text, x, y, font, paint);
Ben Wagner51e15a62019-05-07 15:38:46 -040033 return x + font.measureText(text.c_str(), text.size(), SkTextEncoding::kUTF8);
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000034}
35
Robert Phillipsedcd4312021-06-03 10:14:16 -040036class FontCacheGM : public skiagm::GM {
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000037public:
Jim Van Verthfc4f7682018-01-25 16:26:25 -050038 FontCacheGM(GrContextOptions::Enable allowMultipleTextures)
39 : fAllowMultipleTextures(allowMultipleTextures) {
40 this->setBGColor(SK_ColorLTGRAY);
41 }
Brian Salomon9f545bc2017-11-06 10:36:57 -050042
43 void modifyGrContextOptions(GrContextOptions* options) override {
44 options->fGlyphCacheTextureMaximumBytes = 0;
Jim Van Verthfc4f7682018-01-25 16:26:25 -050045 options->fAllowMultipleGlyphCacheTextures = fAllowMultipleTextures;
Brian Salomon9f545bc2017-11-06 10:36:57 -050046 }
skia.committer@gmail.comf29c3802013-10-10 07:01:40 +000047
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000048protected:
mtklein36352bf2015-03-25 18:17:31 -070049 SkString onShortName() override {
Jim Van Verthfc4f7682018-01-25 16:26:25 -050050 SkString name("fontcache");
51 if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) {
52 name.append("-mt");
53 }
54 return name;
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000055 }
56
Brian Salomon9f545bc2017-11-06 10:36:57 -050057 SkISize onISize() override { return SkISize::Make(kSize, kSize); }
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000058
mtklein36352bf2015-03-25 18:17:31 -070059 void onOnceBeforeDraw() override {
Mike Kleinea3f0142019-03-20 11:12:10 -050060 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());
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000066 }
skia.committer@gmail.comf29c3802013-10-10 07:01:40 +000067
Robert Phillipsedcd4312021-06-03 10:14:16 -040068 void onDraw(SkCanvas* canvas) override {
Brian Salomon9f545bc2017-11-06 10:36:57 -050069 this->drawText(canvas);
70 // Debugging tool for GPU.
71 static const bool kShowAtlas = false;
72 if (kShowAtlas) {
Robert Phillipsedcd4312021-06-03 10:14:16 -040073 if (auto dContext = GrAsDirectContext(canvas->recordingContext())) {
74 auto img = dContext->priv().testingOnly_getFontAtlasImage(kA8_GrMaskFormat);
Brian Salomon9f545bc2017-11-06 10:36:57 -050075 canvas->drawImage(img, 0, 0);
76 }
77 }
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000078 }
79
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000080private:
Brian Salomon9f545bc2017-11-06 10:36:57 -050081 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("!@#$%^&*()<>[]{}")};
Mike Reed94cca602018-12-02 16:04:27 -050088 SkFont font;
89 font.setEdging(SkFont::Edging::kAntiAlias);
90 font.setSubpixel(true);
Brian Salomon9f545bc2017-11-06 10:36:57 -050091
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
Jim Van Verthfc4f7682018-01-25 16:26:25 -050099 if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) {
100 canvas->scale(10, 10);
101 }
102
Brian Salomon9f545bc2017-11-06 10:36:57 -0500103 do {
104 for (auto s : kSizes) {
105 auto size = 2 * s;
Mike Reed94cca602018-12-02 16:04:27 -0500106 font.setSize(size);
Brian Salomon9f545bc2017-11-06 10:36:57 -0500107 for (const auto& typeface : fTypefaces) {
Mike Reed94cca602018-12-02 16:04:27 -0500108 font.setTypeface(typeface);
Brian Salomon9f545bc2017-11-06 10:36:57 -0500109 for (const auto& text : kTexts) {
Mike Reed94cca602018-12-02 16:04:27 -0500110 x = size + draw_string(canvas, text, x + subpixelX, y + subpixelY, font);
Brian Salomon9f545bc2017-11-06 10:36:57 -0500111 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 static constexpr SkScalar kSize = 1280;
128
Jim Van Verthfc4f7682018-01-25 16:26:25 -0500129 GrContextOptions::Enable fAllowMultipleTextures;
Brian Salomon9f545bc2017-11-06 10:36:57 -0500130 sk_sp<SkTypeface> fTypefaces[6];
John Stiles7571f9e2020-09-02 22:42:33 -0400131 using INHERITED = GM;
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +0000132};
133
Brian Salomon9f545bc2017-11-06 10:36:57 -0500134constexpr SkScalar FontCacheGM::kSize;
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +0000135
136//////////////////////////////////////////////////////////////////////////////
137
Jim Van Verthfc4f7682018-01-25 16:26:25 -0500138DEF_GM(return new FontCacheGM(GrContextOptions::Enable::kNo))
139DEF_GM(return new FontCacheGM(GrContextOptions::Enable::kYes))