commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 8 | // GM to stress the GPU font cache |
| 9 | // It's not necessary to run this with CPU configs |
| 10 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "gm/gm.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 12 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 13 | #include "include/core/SkColor.h" |
| 14 | #include "include/core/SkFont.h" |
| 15 | #include "include/core/SkFontStyle.h" |
| 16 | #include "include/core/SkFontTypes.h" |
| 17 | #include "include/core/SkPaint.h" |
| 18 | #include "include/core/SkRefCnt.h" |
| 19 | #include "include/core/SkScalar.h" |
| 20 | #include "include/core/SkSize.h" |
| 21 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "include/core/SkTypeface.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "include/gpu/GrContextOptions.h" |
Robert Phillips | b27b38b | 2020-07-10 16:23:47 -0400 | [diff] [blame] | 24 | #include "include/gpu/GrDirectContext.h" |
Robert Phillips | 59ba27b | 2022-04-12 09:59:38 -0400 | [diff] [blame] | 25 | #include "include/private/gpu/ganesh/GrTypesPriv.h" |
Greg Daniel | 719239c | 2022-04-07 11:20:24 -0400 | [diff] [blame] | 26 | #include "src/gpu/ganesh/GrDirectContextPriv.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #include "tools/ToolUtils.h" |
Kevin Lubick | e836c3a | 2023-10-20 06:55:35 -0400 | [diff] [blame] | 28 | #include "tools/fonts/FontToolUtils.h" |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 29 | |
Jim Van Verth | dfab663 | 2022-04-21 10:51:22 -0400 | [diff] [blame] | 30 | using MaskFormat = skgpu::MaskFormat; |
| 31 | |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 32 | static SkScalar draw_string(SkCanvas* canvas, const SkString& text, SkScalar x, |
Mike Reed | 94cca60 | 2018-12-02 16:04:27 -0500 | [diff] [blame] | 33 | SkScalar y, const SkFont& font) { |
| 34 | SkPaint paint; |
Hal Canary | 89a644b | 2019-01-07 09:36:09 -0500 | [diff] [blame] | 35 | canvas->drawString(text, x, y, font, paint); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 36 | return x + font.measureText(text.c_str(), text.size(), SkTextEncoding::kUTF8); |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 37 | } |
| 38 | |
Robert Phillips | edcd431 | 2021-06-03 10:14:16 -0400 | [diff] [blame] | 39 | class FontCacheGM : public skiagm::GM { |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 40 | public: |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 41 | FontCacheGM(GrContextOptions::Enable allowMultipleTextures) |
| 42 | : fAllowMultipleTextures(allowMultipleTextures) { |
| 43 | this->setBGColor(SK_ColorLTGRAY); |
| 44 | } |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 45 | |
| 46 | void modifyGrContextOptions(GrContextOptions* options) override { |
| 47 | options->fGlyphCacheTextureMaximumBytes = 0; |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 48 | options->fAllowMultipleGlyphCacheTextures = fAllowMultipleTextures; |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 49 | } |
skia.committer@gmail.com | f29c380 | 2013-10-10 07:01:40 +0000 | [diff] [blame] | 50 | |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 51 | protected: |
Leandro Lovisolo | 24fa211 | 2023-08-15 19:05:17 +0000 | [diff] [blame] | 52 | SkString getName() const override { |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 53 | SkString name("fontcache"); |
| 54 | if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) { |
| 55 | name.append("-mt"); |
| 56 | } |
| 57 | return name; |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 58 | } |
| 59 | |
Leandro Lovisolo | 8f02388 | 2023-08-15 21:13:52 +0000 | [diff] [blame] | 60 | SkISize getISize() override { return SkISize::Make(kSize, kSize); } |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 61 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 62 | void onOnceBeforeDraw() override { |
Kevin Lubick | e836c3a | 2023-10-20 06:55:35 -0400 | [diff] [blame] | 63 | fTypefaces[0] = ToolUtils::CreatePortableTypeface("serif", SkFontStyle::Italic()); |
| 64 | fTypefaces[1] = ToolUtils::CreatePortableTypeface("sans-serif", SkFontStyle::Italic()); |
| 65 | fTypefaces[2] = ToolUtils::CreatePortableTypeface("serif", SkFontStyle::Normal()); |
| 66 | fTypefaces[3] = ToolUtils::CreatePortableTypeface("sans-serif", SkFontStyle::Normal()); |
| 67 | fTypefaces[4] = ToolUtils::CreatePortableTypeface("serif", SkFontStyle::Bold()); |
| 68 | fTypefaces[5] = ToolUtils::CreatePortableTypeface("sans-serif", SkFontStyle::Bold()); |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 69 | } |
skia.committer@gmail.com | f29c380 | 2013-10-10 07:01:40 +0000 | [diff] [blame] | 70 | |
Robert Phillips | edcd431 | 2021-06-03 10:14:16 -0400 | [diff] [blame] | 71 | void onDraw(SkCanvas* canvas) override { |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 72 | this->drawText(canvas); |
| 73 | // Debugging tool for GPU. |
| 74 | static const bool kShowAtlas = false; |
| 75 | if (kShowAtlas) { |
Robert Phillips | edcd431 | 2021-06-03 10:14:16 -0400 | [diff] [blame] | 76 | if (auto dContext = GrAsDirectContext(canvas->recordingContext())) { |
Jim Van Verth | dfab663 | 2022-04-21 10:51:22 -0400 | [diff] [blame] | 77 | auto img = dContext->priv().testingOnly_getFontAtlasImage(MaskFormat::kA8); |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 78 | canvas->drawImage(img, 0, 0); |
| 79 | } |
| 80 | } |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 81 | } |
| 82 | |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 83 | private: |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 84 | void drawText(SkCanvas* canvas) { |
| 85 | static const int kSizes[] = {8, 9, 10, 11, 12, 13, 18, 20, 25}; |
| 86 | |
| 87 | static const SkString kTexts[] = {SkString("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), |
| 88 | SkString("abcdefghijklmnopqrstuvwxyz"), |
| 89 | SkString("0123456789"), |
| 90 | SkString("!@#$%^&*()<>[]{}")}; |
Kevin Lubick | bca43ec | 2023-10-30 10:11:22 -0400 | [diff] [blame] | 91 | SkFont font = ToolUtils::DefaultPortableFont(); |
Mike Reed | 94cca60 | 2018-12-02 16:04:27 -0500 | [diff] [blame] | 92 | font.setEdging(SkFont::Edging::kAntiAlias); |
| 93 | font.setSubpixel(true); |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 94 | |
| 95 | static const SkScalar kSubPixelInc = 1 / 2.f; |
| 96 | SkScalar x = 0; |
| 97 | SkScalar y = 10; |
| 98 | SkScalar subpixelX = 0; |
| 99 | SkScalar subpixelY = 0; |
| 100 | bool offsetX = true; |
| 101 | |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 102 | if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) { |
| 103 | canvas->scale(10, 10); |
| 104 | } |
| 105 | |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 106 | do { |
| 107 | for (auto s : kSizes) { |
| 108 | auto size = 2 * s; |
Mike Reed | 94cca60 | 2018-12-02 16:04:27 -0500 | [diff] [blame] | 109 | font.setSize(size); |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 110 | for (const auto& typeface : fTypefaces) { |
Mike Reed | 94cca60 | 2018-12-02 16:04:27 -0500 | [diff] [blame] | 111 | font.setTypeface(typeface); |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 112 | for (const auto& text : kTexts) { |
Mike Reed | 94cca60 | 2018-12-02 16:04:27 -0500 | [diff] [blame] | 113 | x = size + draw_string(canvas, text, x + subpixelX, y + subpixelY, font); |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 114 | x = SkScalarCeilToScalar(x); |
| 115 | if (x + 100 > kSize) { |
| 116 | x = 0; |
| 117 | y += SkScalarCeilToScalar(size + 3); |
| 118 | if (y > kSize) { |
| 119 | return; |
| 120 | } |
| 121 | } |
| 122 | } |
| 123 | } |
| 124 | (offsetX ? subpixelX : subpixelY) += kSubPixelInc; |
| 125 | offsetX = !offsetX; |
| 126 | } |
| 127 | } while (true); |
| 128 | } |
| 129 | |
Brian Salomon | 9fa47cc | 2021-10-08 18:48:26 -0400 | [diff] [blame] | 130 | inline static constexpr SkScalar kSize = 1280; |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 131 | |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 132 | GrContextOptions::Enable fAllowMultipleTextures; |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 133 | sk_sp<SkTypeface> fTypefaces[6]; |
John Stiles | 7571f9e | 2020-09-02 22:42:33 -0400 | [diff] [blame] | 134 | using INHERITED = GM; |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 137 | ////////////////////////////////////////////////////////////////////////////// |
| 138 | |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 139 | DEF_GM(return new FontCacheGM(GrContextOptions::Enable::kNo)) |
| 140 | DEF_GM(return new FontCacheGM(GrContextOptions::Enable::kYes)) |