blob: b2a664bf67df836b44aa587bf17ed57f035651a3 [file] [log] [blame]
Jim Van Verth753403c2018-12-18 11:04:37 -05001/*
2 * Copyright 2018 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 TextBlob regeneration and the GPU font cache
9// It's not necessary to run this with CPU configs
10//
11// The point here is to draw a set of text that will fit in one Plot, and then some large
12// text. After a flush we draw the first set of text again with a slightly different color,
13// and then enough new large text to spill the entire atlas. What *should* happen is that
14// the Plot with the first set of text will not get overwritten by the new large text.
15
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "gm/gm.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040018#include "include/core/SkColor.h"
19#include "include/core/SkFont.h"
Jim Van Verth34b72ae2019-11-14 13:42:06 -050020#include "include/core/SkFontMgr.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040021#include "include/core/SkFontStyle.h"
22#include "include/core/SkFontTypes.h"
23#include "include/core/SkPaint.h"
24#include "include/core/SkRefCnt.h"
25#include "include/core/SkScalar.h"
26#include "include/core/SkSize.h"
27#include "include/core/SkString.h"
28#include "include/core/SkTextBlob.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "include/core/SkTypeface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040030#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "include/gpu/GrContextOptions.h"
Robert Phillipsb7bfbc22020-07-01 12:55:01 -040032#include "include/gpu/GrDirectContext.h"
33#include "include/gpu/GrRecordingContext.h"
Kevin Lubick46572b42023-01-18 13:11:06 -050034#include "include/private/base/SkTemplates.h"
Robert Phillips59ba27b2022-04-12 09:59:38 -040035#include "include/private/gpu/ganesh/GrTypesPriv.h"
Greg Daniel719239c2022-04-07 11:20:24 -040036#include "src/gpu/ganesh/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050037#include "tools/ToolUtils.h"
Kevin Lubicke836c3a2023-10-20 06:55:35 -040038#include "tools/fonts/FontToolUtils.h"
Jim Van Verth753403c2018-12-18 11:04:37 -050039
Robert Phillips9360fae2023-06-20 13:34:37 -040040#if defined(SK_GRAPHITE)
41#include "include/gpu/graphite/ContextOptions.h"
42#endif
43
Herb Derby3b3bcf02023-01-17 15:12:15 -050044using namespace skia_private;
Jim Van Verthdfab6632022-04-21 10:51:22 -040045using MaskFormat = skgpu::MaskFormat;
46
Jim Van Verth753403c2018-12-18 11:04:37 -050047static sk_sp<SkTextBlob> make_blob(const SkString& text, const SkFont& font) {
48 size_t len = text.size();
Herb Derby3b3bcf02023-01-17 15:12:15 -050049 AutoTArray<SkScalar> pos(len);
50 AutoTArray<SkGlyphID> glyphs(len);
Jim Van Verth753403c2018-12-18 11:04:37 -050051
52 font.textToGlyphs(text.c_str(), len, SkTextEncoding::kUTF8, glyphs.get(), len);
53 font.getXPos(glyphs.get(), len, pos.get());
54 return SkTextBlob::MakeFromPosTextH(text.c_str(), len, pos.get(), 0, font);
55}
56
Robert Phillipsedcd4312021-06-03 10:14:16 -040057class FontRegenGM : public skiagm::GM {
Jim Van Verth753403c2018-12-18 11:04:37 -050058
59 void modifyGrContextOptions(GrContextOptions* options) override {
60 options->fGlyphCacheTextureMaximumBytes = 0;
61 options->fAllowMultipleGlyphCacheTextures = GrContextOptions::Enable::kNo;
62 }
63
Robert Phillips9360fae2023-06-20 13:34:37 -040064#if defined(SK_GRAPHITE)
65 void modifyGraphiteContextOptions(skgpu::graphite::ContextOptions* options) const override {
66 options->fGlyphCacheTextureMaximumBytes = 0;
67 options->fAllowMultipleGlyphCacheTextures = false;
68 }
69#endif
70
Leandro Lovisolo24fa2112023-08-15 19:05:17 +000071 SkString getName() const override { return SkString("fontregen"); }
Jim Van Verth753403c2018-12-18 11:04:37 -050072
Leandro Lovisolo8f023882023-08-15 21:13:52 +000073 SkISize getISize() override { return {kSize, kSize}; }
Jim Van Verth753403c2018-12-18 11:04:37 -050074
75 void onOnceBeforeDraw() override {
Hal Canaryfa3305a2019-07-18 12:36:54 -040076 this->setBGColor(SK_ColorLTGRAY);
77
Kevin Lubicke836c3a2023-10-20 06:55:35 -040078 auto tf = ToolUtils::CreatePortableTypeface("sans-serif", SkFontStyle::Normal());
Jim Van Verth753403c2018-12-18 11:04:37 -050079
80 static const SkString kTexts[] = {
81 SkString("abcdefghijklmnopqrstuvwxyz"),
82 SkString("ABCDEFGHI"),
83 SkString("NOPQRSTUV")
84 };
85
86 SkFont font;
87 font.setEdging(SkFont::Edging::kAntiAlias);
88 font.setSubpixel(false);
89 font.setSize(80);
90 font.setTypeface(tf);
91
92 fBlobs[0] = make_blob(kTexts[0], font);
93 font.setSize(162);
94 fBlobs[1] = make_blob(kTexts[1], font);
95 fBlobs[2] = make_blob(kTexts[2], font);
96 }
97
Robert Phillipsedcd4312021-06-03 10:14:16 -040098 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
Robert Phillips88d8aba2023-02-22 11:04:06 -050099
Robert Phillips95c250c2020-06-29 15:36:12 -0400100
Jim Van Verth753403c2018-12-18 11:04:37 -0500101 SkPaint paint;
102 paint.setColor(SK_ColorBLACK);
103 canvas->drawTextBlob(fBlobs[0], 10, 80, paint);
104 canvas->drawTextBlob(fBlobs[1], 10, 225, paint);
Robert Phillips88d8aba2023-02-22 11:04:06 -0500105
106 auto dContext = GrAsDirectContext(canvas->recordingContext());
107 if (dContext) {
108 dContext->flushAndSubmit();
109 }
Jim Van Verth753403c2018-12-18 11:04:37 -0500110
111 paint.setColor(0xFF010101);
112 canvas->drawTextBlob(fBlobs[0], 10, 305, paint);
113 canvas->drawTextBlob(fBlobs[2], 10, 465, paint);
114
115 // Debugging tool for GPU.
116 static const bool kShowAtlas = false;
Robert Phillips88d8aba2023-02-22 11:04:06 -0500117 if (kShowAtlas && dContext) {
Jim Van Verthdfab6632022-04-21 10:51:22 -0400118 auto img = dContext->priv().testingOnly_getFontAtlasImage(MaskFormat::kA8);
Robert Phillips19104af2019-03-05 12:51:52 -0500119 canvas->drawImage(img, 200, 0);
Jim Van Verth753403c2018-12-18 11:04:37 -0500120 }
Robert Phillipsedcd4312021-06-03 10:14:16 -0400121
122 return DrawResult::kOk;
Jim Van Verth753403c2018-12-18 11:04:37 -0500123 }
124
125private:
Brian Salomon9fa47cc2021-10-08 18:48:26 -0400126 inline static constexpr int kSize = 512;
Jim Van Verth753403c2018-12-18 11:04:37 -0500127
128 sk_sp<SkTextBlob> fBlobs[3];
John Stiles7571f9e2020-09-02 22:42:33 -0400129 using INHERITED = GM;
Jim Van Verth753403c2018-12-18 11:04:37 -0500130};
131
Jim Van Verth753403c2018-12-18 11:04:37 -0500132//////////////////////////////////////////////////////////////////////////////
133
134DEF_GM(return new FontRegenGM())
Jim Van Verth34b72ae2019-11-14 13:42:06 -0500135
136///////////////////////////////////////////////////////////////////////////////
137
Robert Phillipsedcd4312021-06-03 10:14:16 -0400138class BadAppleGM : public skiagm::GM {
Leandro Lovisolo24fa2112023-08-15 19:05:17 +0000139 SkString getName() const override { return SkString("badapple"); }
Jim Van Verth34b72ae2019-11-14 13:42:06 -0500140
Leandro Lovisolo8f023882023-08-15 21:13:52 +0000141 SkISize getISize() override { return {kSize, kSize}; }
Jim Van Verth34b72ae2019-11-14 13:42:06 -0500142
143 void onOnceBeforeDraw() override {
144 this->setBGColor(SK_ColorWHITE);
Kevin Lubick1e971192023-11-10 16:14:44 -0500145 auto fm = ToolUtils::TestFontMgr();
Jim Van Verth34b72ae2019-11-14 13:42:06 -0500146
147 static const SkString kTexts[] = {
148 SkString("Meet"),
149 SkString("iPad Pro"),
150 };
151
Kevin Lubickbca43ec2023-10-30 10:11:22 -0400152 SkFont font = ToolUtils::DefaultPortableFont();
Jim Van Verth34b72ae2019-11-14 13:42:06 -0500153 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
154 font.setSubpixel(true);
155 font.setSize(256);
156
157 fBlobs[0] = make_blob(kTexts[0], font);
158 fBlobs[1] = make_blob(kTexts[1], font);
159 }
160
Robert Phillipsedcd4312021-06-03 10:14:16 -0400161 void onDraw(SkCanvas* canvas) override {
Jim Van Verth34b72ae2019-11-14 13:42:06 -0500162 SkPaint paint;
163 paint.setColor(0xFF111111);
164 canvas->drawTextBlob(fBlobs[0], 10, 260, paint);
165 canvas->drawTextBlob(fBlobs[1], 10, 500, paint);
Jim Van Verth34b72ae2019-11-14 13:42:06 -0500166 }
167
168private:
Brian Salomon9fa47cc2021-10-08 18:48:26 -0400169 inline static constexpr int kSize = 512;
Jim Van Verth34b72ae2019-11-14 13:42:06 -0500170
171 sk_sp<SkTextBlob> fBlobs[3];
John Stiles7571f9e2020-09-02 22:42:33 -0400172 using INHERITED = GM;
Jim Van Verth34b72ae2019-11-14 13:42:06 -0500173};
174
175//////////////////////////////////////////////////////////////////////////////
176
177DEF_GM(return new BadAppleGM())