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" |
Kevin Lubick | 46572b4 | 2023-01-18 13:11:06 -0500 | [diff] [blame] | 34 | #include "include/private/base/SkTemplates.h" |
Robert Phillips | 59ba27b | 2022-04-12 09:59:38 -0400 | [diff] [blame] | 35 | #include "include/private/gpu/ganesh/GrTypesPriv.h" |
Greg Daniel | 719239c | 2022-04-07 11:20:24 -0400 | [diff] [blame] | 36 | #include "src/gpu/ganesh/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 37 | #include "tools/ToolUtils.h" |
Kevin Lubick | e836c3a | 2023-10-20 06:55:35 -0400 | [diff] [blame] | 38 | #include "tools/fonts/FontToolUtils.h" |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 39 | |
Robert Phillips | 9360fae | 2023-06-20 13:34:37 -0400 | [diff] [blame] | 40 | #if defined(SK_GRAPHITE) |
| 41 | #include "include/gpu/graphite/ContextOptions.h" |
| 42 | #endif |
| 43 | |
Herb Derby | 3b3bcf0 | 2023-01-17 15:12:15 -0500 | [diff] [blame] | 44 | using namespace skia_private; |
Jim Van Verth | dfab663 | 2022-04-21 10:51:22 -0400 | [diff] [blame] | 45 | using MaskFormat = skgpu::MaskFormat; |
| 46 | |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 47 | static sk_sp<SkTextBlob> make_blob(const SkString& text, const SkFont& font) { |
| 48 | size_t len = text.size(); |
Herb Derby | 3b3bcf0 | 2023-01-17 15:12:15 -0500 | [diff] [blame] | 49 | AutoTArray<SkScalar> pos(len); |
| 50 | AutoTArray<SkGlyphID> glyphs(len); |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 51 | |
| 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 Phillips | edcd431 | 2021-06-03 10:14:16 -0400 | [diff] [blame] | 57 | class FontRegenGM : public skiagm::GM { |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 58 | |
| 59 | void modifyGrContextOptions(GrContextOptions* options) override { |
| 60 | options->fGlyphCacheTextureMaximumBytes = 0; |
| 61 | options->fAllowMultipleGlyphCacheTextures = GrContextOptions::Enable::kNo; |
| 62 | } |
| 63 | |
Robert Phillips | 9360fae | 2023-06-20 13:34:37 -0400 | [diff] [blame] | 64 | #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 Lovisolo | 24fa211 | 2023-08-15 19:05:17 +0000 | [diff] [blame] | 71 | SkString getName() const override { return SkString("fontregen"); } |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 72 | |
Leandro Lovisolo | 8f02388 | 2023-08-15 21:13:52 +0000 | [diff] [blame] | 73 | SkISize getISize() override { return {kSize, kSize}; } |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 74 | |
| 75 | void onOnceBeforeDraw() override { |
Hal Canary | fa3305a | 2019-07-18 12:36:54 -0400 | [diff] [blame] | 76 | this->setBGColor(SK_ColorLTGRAY); |
| 77 | |
Kevin Lubick | e836c3a | 2023-10-20 06:55:35 -0400 | [diff] [blame] | 78 | auto tf = ToolUtils::CreatePortableTypeface("sans-serif", SkFontStyle::Normal()); |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 79 | |
| 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 Phillips | edcd431 | 2021-06-03 10:14:16 -0400 | [diff] [blame] | 98 | DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { |
Robert Phillips | 88d8aba | 2023-02-22 11:04:06 -0500 | [diff] [blame] | 99 | |
Robert Phillips | 95c250c | 2020-06-29 15:36:12 -0400 | [diff] [blame] | 100 | |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 101 | SkPaint paint; |
| 102 | paint.setColor(SK_ColorBLACK); |
| 103 | canvas->drawTextBlob(fBlobs[0], 10, 80, paint); |
| 104 | canvas->drawTextBlob(fBlobs[1], 10, 225, paint); |
Robert Phillips | 88d8aba | 2023-02-22 11:04:06 -0500 | [diff] [blame] | 105 | |
| 106 | auto dContext = GrAsDirectContext(canvas->recordingContext()); |
| 107 | if (dContext) { |
| 108 | dContext->flushAndSubmit(); |
| 109 | } |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 110 | |
| 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 Phillips | 88d8aba | 2023-02-22 11:04:06 -0500 | [diff] [blame] | 117 | if (kShowAtlas && dContext) { |
Jim Van Verth | dfab663 | 2022-04-21 10:51:22 -0400 | [diff] [blame] | 118 | auto img = dContext->priv().testingOnly_getFontAtlasImage(MaskFormat::kA8); |
Robert Phillips | 19104af | 2019-03-05 12:51:52 -0500 | [diff] [blame] | 119 | canvas->drawImage(img, 200, 0); |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 120 | } |
Robert Phillips | edcd431 | 2021-06-03 10:14:16 -0400 | [diff] [blame] | 121 | |
| 122 | return DrawResult::kOk; |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | private: |
Brian Salomon | 9fa47cc | 2021-10-08 18:48:26 -0400 | [diff] [blame] | 126 | inline static constexpr int kSize = 512; |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 127 | |
| 128 | sk_sp<SkTextBlob> fBlobs[3]; |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 129 | using INHERITED = GM; |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 130 | }; |
| 131 | |
Jim Van Verth | 753403c | 2018-12-18 11:04:37 -0500 | [diff] [blame] | 132 | ////////////////////////////////////////////////////////////////////////////// |
| 133 | |
| 134 | DEF_GM(return new FontRegenGM()) |
Jim Van Verth | 34b72ae | 2019-11-14 13:42:06 -0500 | [diff] [blame] | 135 | |
| 136 | /////////////////////////////////////////////////////////////////////////////// |
| 137 | |
Robert Phillips | edcd431 | 2021-06-03 10:14:16 -0400 | [diff] [blame] | 138 | class BadAppleGM : public skiagm::GM { |
Leandro Lovisolo | 24fa211 | 2023-08-15 19:05:17 +0000 | [diff] [blame] | 139 | SkString getName() const override { return SkString("badapple"); } |
Jim Van Verth | 34b72ae | 2019-11-14 13:42:06 -0500 | [diff] [blame] | 140 | |
Leandro Lovisolo | 8f02388 | 2023-08-15 21:13:52 +0000 | [diff] [blame] | 141 | SkISize getISize() override { return {kSize, kSize}; } |
Jim Van Verth | 34b72ae | 2019-11-14 13:42:06 -0500 | [diff] [blame] | 142 | |
| 143 | void onOnceBeforeDraw() override { |
| 144 | this->setBGColor(SK_ColorWHITE); |
Kevin Lubick | 1e97119 | 2023-11-10 16:14:44 -0500 | [diff] [blame] | 145 | auto fm = ToolUtils::TestFontMgr(); |
Jim Van Verth | 34b72ae | 2019-11-14 13:42:06 -0500 | [diff] [blame] | 146 | |
| 147 | static const SkString kTexts[] = { |
| 148 | SkString("Meet"), |
| 149 | SkString("iPad Pro"), |
| 150 | }; |
| 151 | |
Kevin Lubick | bca43ec | 2023-10-30 10:11:22 -0400 | [diff] [blame] | 152 | SkFont font = ToolUtils::DefaultPortableFont(); |
Jim Van Verth | 34b72ae | 2019-11-14 13:42:06 -0500 | [diff] [blame] | 153 | 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 Phillips | edcd431 | 2021-06-03 10:14:16 -0400 | [diff] [blame] | 161 | void onDraw(SkCanvas* canvas) override { |
Jim Van Verth | 34b72ae | 2019-11-14 13:42:06 -0500 | [diff] [blame] | 162 | SkPaint paint; |
| 163 | paint.setColor(0xFF111111); |
| 164 | canvas->drawTextBlob(fBlobs[0], 10, 260, paint); |
| 165 | canvas->drawTextBlob(fBlobs[1], 10, 500, paint); |
Jim Van Verth | 34b72ae | 2019-11-14 13:42:06 -0500 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | private: |
Brian Salomon | 9fa47cc | 2021-10-08 18:48:26 -0400 | [diff] [blame] | 169 | inline static constexpr int kSize = 512; |
Jim Van Verth | 34b72ae | 2019-11-14 13:42:06 -0500 | [diff] [blame] | 170 | |
| 171 | sk_sp<SkTextBlob> fBlobs[3]; |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 172 | using INHERITED = GM; |
Jim Van Verth | 34b72ae | 2019-11-14 13:42:06 -0500 | [diff] [blame] | 173 | }; |
| 174 | |
| 175 | ////////////////////////////////////////////////////////////////////////////// |
| 176 | |
| 177 | DEF_GM(return new BadAppleGM()) |