Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "gm/gm.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 18 | #include "include/core/SkColor.h" |
| 19 | #include "include/core/SkFont.h" |
Jim Van Verth | 34b72ae | 2019-11-14 13:42:06 -0500 | [diff] [blame] | 20 | #include "include/core/SkFontMgr.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 21 | #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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 29 | #include "include/core/SkTypeface.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 30 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 31 | #include "include/gpu/GrContextOptions.h" |
Robert Phillips | b7bfbc2 | 2020-07-01 12:55:01 -0400 | [diff] [blame] | 32 | #include "include/gpu/GrDirectContext.h" |
| 33 | #include "include/gpu/GrRecordingContext.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 34 | #include "include/private/GrTypesPriv.h" |
| 35 | #include "include/private/SkTemplates.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 36 | #include "src/gpu/GrContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 37 | #include "tools/ToolUtils.h" |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 38 | |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 39 | class GrRenderTargetContext; |
| 40 | |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 41 | static sk_sp<SkTextBlob> make_blob(const SkString& text, const SkFont& font) { |
| 42 | size_t len = text.size(); |
| 43 | SkAutoTArray<SkScalar> pos(len); |
| 44 | SkAutoTArray<SkGlyphID> glyphs(len); |
| 45 | |
| 46 | font.textToGlyphs(text.c_str(), len, SkTextEncoding::kUTF8, glyphs.get(), len); |
| 47 | font.getXPos(glyphs.get(), len, pos.get()); |
| 48 | return SkTextBlob::MakeFromPosTextH(text.c_str(), len, pos.get(), 0, font); |
| 49 | } |
| 50 | |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 51 | class FontRegenGM : public skiagm::GpuGM { |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 52 | |
| 53 | void modifyGrContextOptions(GrContextOptions* options) override { |
| 54 | options->fGlyphCacheTextureMaximumBytes = 0; |
| 55 | options->fAllowMultipleGlyphCacheTextures = GrContextOptions::Enable::kNo; |
| 56 | } |
| 57 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 58 | SkString onShortName() override { return SkString("fontregen"); } |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 59 | |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 60 | SkISize onISize() override { return {kSize, kSize}; } |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 61 | |
| 62 | void onOnceBeforeDraw() override { |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 63 | this->setBGColor(SK_ColorLTGRAY); |
| 64 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 65 | auto tf = ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Normal()); |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 66 | |
| 67 | static const SkString kTexts[] = { |
| 68 | SkString("abcdefghijklmnopqrstuvwxyz"), |
| 69 | SkString("ABCDEFGHI"), |
| 70 | SkString("NOPQRSTUV") |
| 71 | }; |
| 72 | |
| 73 | SkFont font; |
| 74 | font.setEdging(SkFont::Edging::kAntiAlias); |
| 75 | font.setSubpixel(false); |
| 76 | font.setSize(80); |
| 77 | font.setTypeface(tf); |
| 78 | |
| 79 | fBlobs[0] = make_blob(kTexts[0], font); |
| 80 | font.setSize(162); |
| 81 | fBlobs[1] = make_blob(kTexts[1], font); |
| 82 | fBlobs[2] = make_blob(kTexts[2], font); |
| 83 | } |
| 84 | |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 85 | void onDraw(GrRecordingContext* context, GrRenderTargetContext*, SkCanvas* canvas) override { |
Robert Phillips | f8f45d9 | 2020-07-01 11:11:18 -0400 | [diff] [blame] | 86 | auto direct = context->asDirectContext(); |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 87 | if (!direct) { |
| 88 | return; |
| 89 | } |
| 90 | |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 91 | SkPaint paint; |
| 92 | paint.setColor(SK_ColorBLACK); |
| 93 | canvas->drawTextBlob(fBlobs[0], 10, 80, paint); |
| 94 | canvas->drawTextBlob(fBlobs[1], 10, 225, paint); |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 95 | direct->flushAndSubmit(); |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 96 | |
| 97 | paint.setColor(0xFF010101); |
| 98 | canvas->drawTextBlob(fBlobs[0], 10, 305, paint); |
| 99 | canvas->drawTextBlob(fBlobs[2], 10, 465, paint); |
| 100 | |
| 101 | // Debugging tool for GPU. |
| 102 | static const bool kShowAtlas = false; |
| 103 | if (kShowAtlas) { |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 104 | auto img = direct->priv().testingOnly_getFontAtlasImage(kA8_GrMaskFormat); |
Robert Phillips | 19104af | 2019-03-05 12:51:52 -0500 | [diff] [blame] | 105 | canvas->drawImage(img, 200, 0); |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | private: |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 110 | static constexpr int kSize = 512; |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 111 | |
| 112 | sk_sp<SkTextBlob> fBlobs[3]; |
| 113 | typedef GM INHERITED; |
| 114 | }; |
| 115 | |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 116 | ////////////////////////////////////////////////////////////////////////////// |
| 117 | |
| 118 | DEF_GM(return new FontRegenGM()) |
Jim Van Verth | 34b72ae | 2019-11-14 13:42:06 -0500 | [diff] [blame] | 119 | |
| 120 | /////////////////////////////////////////////////////////////////////////////// |
| 121 | |
| 122 | class BadAppleGM : public skiagm::GpuGM { |
| 123 | |
| 124 | SkString onShortName() override { return SkString("badapple"); } |
| 125 | |
| 126 | SkISize onISize() override { return {kSize, kSize}; } |
| 127 | |
| 128 | void onOnceBeforeDraw() override { |
| 129 | this->setBGColor(SK_ColorWHITE); |
| 130 | auto fm = SkFontMgr::RefDefault(); |
| 131 | |
| 132 | static const SkString kTexts[] = { |
| 133 | SkString("Meet"), |
| 134 | SkString("iPad Pro"), |
| 135 | }; |
| 136 | |
| 137 | SkFont font; |
| 138 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 139 | font.setSubpixel(true); |
| 140 | font.setSize(256); |
| 141 | |
| 142 | fBlobs[0] = make_blob(kTexts[0], font); |
| 143 | fBlobs[1] = make_blob(kTexts[1], font); |
| 144 | } |
| 145 | |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 146 | void onDraw(GrRecordingContext* context, GrRenderTargetContext*, SkCanvas* canvas) override { |
Jim Van Verth | 34b72ae | 2019-11-14 13:42:06 -0500 | [diff] [blame] | 147 | SkPaint paint; |
| 148 | paint.setColor(0xFF111111); |
| 149 | canvas->drawTextBlob(fBlobs[0], 10, 260, paint); |
| 150 | canvas->drawTextBlob(fBlobs[1], 10, 500, paint); |
Jim Van Verth | 34b72ae | 2019-11-14 13:42:06 -0500 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | private: |
| 154 | static constexpr int kSize = 512; |
| 155 | |
| 156 | sk_sp<SkTextBlob> fBlobs[3]; |
| 157 | typedef GM INHERITED; |
| 158 | }; |
| 159 | |
| 160 | ////////////////////////////////////////////////////////////////////////////// |
| 161 | |
| 162 | DEF_GM(return new BadAppleGM()) |