blob: 3addd80a52815574b47fff06cefa78239d5a4ced [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"
20#include "include/core/SkFontStyle.h"
21#include "include/core/SkFontTypes.h"
22#include "include/core/SkPaint.h"
23#include "include/core/SkRefCnt.h"
24#include "include/core/SkScalar.h"
25#include "include/core/SkSize.h"
26#include "include/core/SkString.h"
27#include "include/core/SkTextBlob.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "include/core/SkTypeface.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040029#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "include/gpu/GrContext.h"
31#include "include/gpu/GrContextOptions.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040032#include "include/private/GrTypesPriv.h"
33#include "include/private/SkTemplates.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050034#include "src/gpu/GrContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050035#include "tools/ToolUtils.h"
Jim Van Verth753403c2018-12-18 11:04:37 -050036
Ben Wagner7fde8e12019-05-01 17:28:53 -040037class GrRenderTargetContext;
38
Jim Van Verth753403c2018-12-18 11:04:37 -050039static sk_sp<SkTextBlob> make_blob(const SkString& text, const SkFont& font) {
40 size_t len = text.size();
41 SkAutoTArray<SkScalar> pos(len);
42 SkAutoTArray<SkGlyphID> glyphs(len);
43
44 font.textToGlyphs(text.c_str(), len, SkTextEncoding::kUTF8, glyphs.get(), len);
45 font.getXPos(glyphs.get(), len, pos.get());
46 return SkTextBlob::MakeFromPosTextH(text.c_str(), len, pos.get(), 0, font);
47}
48
Chris Dalton3a778372019-02-07 15:23:36 -070049class FontRegenGM : public skiagm::GpuGM {
Jim Van Verth753403c2018-12-18 11:04:37 -050050
51 void modifyGrContextOptions(GrContextOptions* options) override {
52 options->fGlyphCacheTextureMaximumBytes = 0;
53 options->fAllowMultipleGlyphCacheTextures = GrContextOptions::Enable::kNo;
54 }
55
Hal Canaryfa3305a2019-07-18 12:36:54 -040056 SkString onShortName() override { return SkString("fontregen"); }
Jim Van Verth753403c2018-12-18 11:04:37 -050057
Hal Canaryfa3305a2019-07-18 12:36:54 -040058 SkISize onISize() override { return {kSize, kSize}; }
Jim Van Verth753403c2018-12-18 11:04:37 -050059
60 void onOnceBeforeDraw() override {
Hal Canaryfa3305a2019-07-18 12:36:54 -040061 this->setBGColor(SK_ColorLTGRAY);
62
Mike Kleinea3f0142019-03-20 11:12:10 -050063 auto tf = ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Normal());
Jim Van Verth753403c2018-12-18 11:04:37 -050064
65 static const SkString kTexts[] = {
66 SkString("abcdefghijklmnopqrstuvwxyz"),
67 SkString("ABCDEFGHI"),
68 SkString("NOPQRSTUV")
69 };
70
71 SkFont font;
72 font.setEdging(SkFont::Edging::kAntiAlias);
73 font.setSubpixel(false);
74 font.setSize(80);
75 font.setTypeface(tf);
76
77 fBlobs[0] = make_blob(kTexts[0], font);
78 font.setSize(162);
79 fBlobs[1] = make_blob(kTexts[1], font);
80 fBlobs[2] = make_blob(kTexts[2], font);
81 }
82
Robert Phillips9882dae2019-03-04 11:00:10 -050083 void onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas) override {
Jim Van Verth753403c2018-12-18 11:04:37 -050084 SkPaint paint;
85 paint.setColor(SK_ColorBLACK);
86 canvas->drawTextBlob(fBlobs[0], 10, 80, paint);
87 canvas->drawTextBlob(fBlobs[1], 10, 225, paint);
Robert Phillips9882dae2019-03-04 11:00:10 -050088 context->flush();
Jim Van Verth753403c2018-12-18 11:04:37 -050089
90 paint.setColor(0xFF010101);
91 canvas->drawTextBlob(fBlobs[0], 10, 305, paint);
92 canvas->drawTextBlob(fBlobs[2], 10, 465, paint);
93
94 // Debugging tool for GPU.
95 static const bool kShowAtlas = false;
96 if (kShowAtlas) {
Robert Phillips19104af2019-03-05 12:51:52 -050097 auto img = context->priv().testingOnly_getFontAtlasImage(kA8_GrMaskFormat);
98 canvas->drawImage(img, 200, 0);
Jim Van Verth753403c2018-12-18 11:04:37 -050099 }
100 }
101
102private:
Hal Canaryfa3305a2019-07-18 12:36:54 -0400103 static constexpr int kSize = 512;
Jim Van Verth753403c2018-12-18 11:04:37 -0500104
105 sk_sp<SkTextBlob> fBlobs[3];
106 typedef GM INHERITED;
107};
108
Jim Van Verth753403c2018-12-18 11:04:37 -0500109//////////////////////////////////////////////////////////////////////////////
110
111DEF_GM(return new FontRegenGM())