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