blob: 87148154a1da32228275416207f6531f9687eec7 [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/GrContext.h"
32#include "include/gpu/GrContextOptions.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040033#include "include/private/GrTypesPriv.h"
34#include "include/private/SkTemplates.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "src/gpu/GrContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050036#include "tools/ToolUtils.h"
Jim Van Verth753403c2018-12-18 11:04:37 -050037
Ben Wagner7fde8e12019-05-01 17:28:53 -040038class GrRenderTargetContext;
39
Jim Van Verth753403c2018-12-18 11:04:37 -050040static sk_sp<SkTextBlob> make_blob(const SkString& text, const SkFont& font) {
41 size_t len = text.size();
42 SkAutoTArray<SkScalar> pos(len);
43 SkAutoTArray<SkGlyphID> glyphs(len);
44
45 font.textToGlyphs(text.c_str(), len, SkTextEncoding::kUTF8, glyphs.get(), len);
46 font.getXPos(glyphs.get(), len, pos.get());
47 return SkTextBlob::MakeFromPosTextH(text.c_str(), len, pos.get(), 0, font);
48}
49
Chris Dalton3a778372019-02-07 15:23:36 -070050class FontRegenGM : public skiagm::GpuGM {
Jim Van Verth753403c2018-12-18 11:04:37 -050051
52 void modifyGrContextOptions(GrContextOptions* options) override {
53 options->fGlyphCacheTextureMaximumBytes = 0;
54 options->fAllowMultipleGlyphCacheTextures = GrContextOptions::Enable::kNo;
55 }
56
Hal Canaryfa3305a2019-07-18 12:36:54 -040057 SkString onShortName() override { return SkString("fontregen"); }
Jim Van Verth753403c2018-12-18 11:04:37 -050058
Hal Canaryfa3305a2019-07-18 12:36:54 -040059 SkISize onISize() override { return {kSize, kSize}; }
Jim Van Verth753403c2018-12-18 11:04:37 -050060
61 void onOnceBeforeDraw() override {
Hal Canaryfa3305a2019-07-18 12:36:54 -040062 this->setBGColor(SK_ColorLTGRAY);
63
Mike Kleinea3f0142019-03-20 11:12:10 -050064 auto tf = ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Normal());
Jim Van Verth753403c2018-12-18 11:04:37 -050065
66 static const SkString kTexts[] = {
67 SkString("abcdefghijklmnopqrstuvwxyz"),
68 SkString("ABCDEFGHI"),
69 SkString("NOPQRSTUV")
70 };
71
72 SkFont font;
73 font.setEdging(SkFont::Edging::kAntiAlias);
74 font.setSubpixel(false);
75 font.setSize(80);
76 font.setTypeface(tf);
77
78 fBlobs[0] = make_blob(kTexts[0], font);
79 font.setSize(162);
80 fBlobs[1] = make_blob(kTexts[1], font);
81 fBlobs[2] = make_blob(kTexts[2], font);
82 }
83
Robert Phillips9882dae2019-03-04 11:00:10 -050084 void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override {
Jim Van Verth753403c2018-12-18 11:04:37 -050085 SkPaint paint;
86 paint.setColor(SK_ColorBLACK);
87 canvas->drawTextBlob(fBlobs[0], 10, 80, paint);
88 canvas->drawTextBlob(fBlobs[1], 10, 225, paint);
Robert Phillips9882dae2019-03-04 11:00:10 -050089 context->flush();
Jim Van Verth753403c2018-12-18 11:04:37 -050090
91 paint.setColor(0xFF010101);
92 canvas->drawTextBlob(fBlobs[0], 10, 305, paint);
93 canvas->drawTextBlob(fBlobs[2], 10, 465, paint);
94
95 // Debugging tool for GPU.
96 static const bool kShowAtlas = false;
97 if (kShowAtlas) {
Robert Phillips19104af2019-03-05 12:51:52 -050098 auto img = context->priv().testingOnly_getFontAtlasImage(kA8_GrMaskFormat);
99 canvas->drawImage(img, 200, 0);
Jim Van Verth753403c2018-12-18 11:04:37 -0500100 }
101 }
102
103private:
Hal Canaryfa3305a2019-07-18 12:36:54 -0400104 static constexpr int kSize = 512;
Jim Van Verth753403c2018-12-18 11:04:37 -0500105
106 sk_sp<SkTextBlob> fBlobs[3];
107 typedef GM INHERITED;
108};
109
Jim Van Verth753403c2018-12-18 11:04:37 -0500110//////////////////////////////////////////////////////////////////////////////
111
112DEF_GM(return new FontRegenGM())
Jim Van Verth34b72ae2019-11-14 13:42:06 -0500113
114///////////////////////////////////////////////////////////////////////////////
115
116class BadAppleGM : public skiagm::GpuGM {
117
118 SkString onShortName() override { return SkString("badapple"); }
119
120 SkISize onISize() override { return {kSize, kSize}; }
121
122 void onOnceBeforeDraw() override {
123 this->setBGColor(SK_ColorWHITE);
124 auto fm = SkFontMgr::RefDefault();
125
126 static const SkString kTexts[] = {
127 SkString("Meet"),
128 SkString("iPad Pro"),
129 };
130
131 SkFont font;
132 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
133 font.setSubpixel(true);
134 font.setSize(256);
135
136 fBlobs[0] = make_blob(kTexts[0], font);
137 fBlobs[1] = make_blob(kTexts[1], font);
138 }
139
140 void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override {
141 SkPaint paint;
142 paint.setColor(0xFF111111);
143 canvas->drawTextBlob(fBlobs[0], 10, 260, paint);
144 canvas->drawTextBlob(fBlobs[1], 10, 500, paint);
145 context->flush();
146 }
147
148private:
149 static constexpr int kSize = 512;
150
151 sk_sp<SkTextBlob> fBlobs[3];
152 typedef GM INHERITED;
153};
154
155//////////////////////////////////////////////////////////////////////////////
156
157DEF_GM(return new BadAppleGM())