blob: 631cf275ea7dcceb5e973ddf9fe081d2e25e153c [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"
Robert Phillips59ba27b2022-04-12 09:59:38 -040025#include "include/private/gpu/ganesh/GrTypesPriv.h"
Greg Daniel719239c2022-04-07 11:20:24 -040026#include "src/gpu/ganesh/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "tools/ToolUtils.h"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040028#include "tools/fonts/FontToolUtils.h"
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000029
Jim Van Verthdfab6632022-04-21 10:51:22 -040030using MaskFormat = skgpu::MaskFormat;
31
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000032static SkScalar draw_string(SkCanvas* canvas, const SkString& text, SkScalar x,
Mike Reed94cca602018-12-02 16:04:27 -050033 SkScalar y, const SkFont& font) {
34 SkPaint paint;
Hal Canary89a644b2019-01-07 09:36:09 -050035 canvas->drawString(text, x, y, font, paint);
Ben Wagner51e15a62019-05-07 15:38:46 -040036 return x + font.measureText(text.c_str(), text.size(), SkTextEncoding::kUTF8);
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000037}
38
Robert Phillipsedcd4312021-06-03 10:14:16 -040039class FontCacheGM : public skiagm::GM {
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000040public:
Jim Van Verthfc4f7682018-01-25 16:26:25 -050041 FontCacheGM(GrContextOptions::Enable allowMultipleTextures)
42 : fAllowMultipleTextures(allowMultipleTextures) {
43 this->setBGColor(SK_ColorLTGRAY);
44 }
Brian Salomon9f545bc2017-11-06 10:36:57 -050045
46 void modifyGrContextOptions(GrContextOptions* options) override {
47 options->fGlyphCacheTextureMaximumBytes = 0;
Jim Van Verthfc4f7682018-01-25 16:26:25 -050048 options->fAllowMultipleGlyphCacheTextures = fAllowMultipleTextures;
Brian Salomon9f545bc2017-11-06 10:36:57 -050049 }
skia.committer@gmail.comf29c3802013-10-10 07:01:40 +000050
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000051protected:
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000052 SkString getName() const override {
Jim Van Verthfc4f7682018-01-25 16:26:25 -050053 SkString name("fontcache");
54 if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) {
55 name.append("-mt");
56 }
57 return name;
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000058 }
59
Leandro Lovisolo8f023882023-08-15 21:13:52 +000060 SkISize getISize() override { return SkISize::Make(kSize, kSize); }
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000061
mtklein36352bf2015-03-25 18:17:31 -070062 void onOnceBeforeDraw() override {
Kevin Lubicke836c3a2023-10-20 06:55:35 -040063 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());
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000069 }
skia.committer@gmail.comf29c3802013-10-10 07:01:40 +000070
Robert Phillipsedcd4312021-06-03 10:14:16 -040071 void onDraw(SkCanvas* canvas) override {
Brian Salomon9f545bc2017-11-06 10:36:57 -050072 this->drawText(canvas);
73 // Debugging tool for GPU.
74 static const bool kShowAtlas = false;
75 if (kShowAtlas) {
Robert Phillipsedcd4312021-06-03 10:14:16 -040076 if (auto dContext = GrAsDirectContext(canvas->recordingContext())) {
Jim Van Verthdfab6632022-04-21 10:51:22 -040077 auto img = dContext->priv().testingOnly_getFontAtlasImage(MaskFormat::kA8);
Brian Salomon9f545bc2017-11-06 10:36:57 -050078 canvas->drawImage(img, 0, 0);
79 }
80 }
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000081 }
82
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000083private:
Brian Salomon9f545bc2017-11-06 10:36:57 -050084 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("!@#$%^&*()<>[]{}")};
Kevin Lubickbca43ec2023-10-30 10:11:22 -040091 SkFont font = ToolUtils::DefaultPortableFont();
Mike Reed94cca602018-12-02 16:04:27 -050092 font.setEdging(SkFont::Edging::kAntiAlias);
93 font.setSubpixel(true);
Brian Salomon9f545bc2017-11-06 10:36:57 -050094
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
Jim Van Verthfc4f7682018-01-25 16:26:25 -0500102 if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) {
103 canvas->scale(10, 10);
104 }
105
Brian Salomon9f545bc2017-11-06 10:36:57 -0500106 do {
107 for (auto s : kSizes) {
108 auto size = 2 * s;
Mike Reed94cca602018-12-02 16:04:27 -0500109 font.setSize(size);
Brian Salomon9f545bc2017-11-06 10:36:57 -0500110 for (const auto& typeface : fTypefaces) {
Mike Reed94cca602018-12-02 16:04:27 -0500111 font.setTypeface(typeface);
Brian Salomon9f545bc2017-11-06 10:36:57 -0500112 for (const auto& text : kTexts) {
Mike Reed94cca602018-12-02 16:04:27 -0500113 x = size + draw_string(canvas, text, x + subpixelX, y + subpixelY, font);
Brian Salomon9f545bc2017-11-06 10:36:57 -0500114 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
Brian Salomon9fa47cc2021-10-08 18:48:26 -0400130 inline static constexpr SkScalar kSize = 1280;
Brian Salomon9f545bc2017-11-06 10:36:57 -0500131
Jim Van Verthfc4f7682018-01-25 16:26:25 -0500132 GrContextOptions::Enable fAllowMultipleTextures;
Brian Salomon9f545bc2017-11-06 10:36:57 -0500133 sk_sp<SkTypeface> fTypefaces[6];
John Stiles7571f9e2020-09-02 22:42:33 -0400134 using INHERITED = GM;
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +0000135};
136
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +0000137//////////////////////////////////////////////////////////////////////////////
138
Jim Van Verthfc4f7682018-01-25 16:26:25 -0500139DEF_GM(return new FontCacheGM(GrContextOptions::Enable::kNo))
140DEF_GM(return new FontCacheGM(GrContextOptions::Enable::kYes))