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 | |
| 11 | #include "gm.h" |
| 12 | |
| 13 | #if SK_SUPPORT_GPU |
| 14 | |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 15 | #include "GrContext.h" |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 16 | #include "GrContextPriv.h" |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 17 | #include "GrContextOptions.h" |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 18 | #include "SkCanvas.h" |
| 19 | #include "SkGraphics.h" |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 20 | #include "SkImage.h" |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 21 | #include "SkTypeface.h" |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 22 | #include "gm.h" |
| 23 | #include "sk_tool_utils.h" |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 24 | |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 25 | static SkScalar draw_string(SkCanvas* canvas, const SkString& text, SkScalar x, |
| 26 | SkScalar y, const SkPaint& paint) { |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 27 | canvas->drawString(text, x, y, paint); |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 28 | return x + paint.measureText(text.c_str(), text.size()); |
| 29 | } |
| 30 | |
| 31 | class FontCacheGM : public skiagm::GM { |
| 32 | public: |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 33 | FontCacheGM(GrContextOptions::Enable allowMultipleTextures) |
| 34 | : fAllowMultipleTextures(allowMultipleTextures) { |
| 35 | this->setBGColor(SK_ColorLTGRAY); |
| 36 | } |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 37 | |
| 38 | void modifyGrContextOptions(GrContextOptions* options) override { |
| 39 | options->fGlyphCacheTextureMaximumBytes = 0; |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 40 | options->fAllowMultipleGlyphCacheTextures = fAllowMultipleTextures; |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 41 | } |
skia.committer@gmail.com | f29c380 | 2013-10-10 07:01:40 +0000 | [diff] [blame] | 42 | |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 43 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 44 | SkString onShortName() override { |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 45 | SkString name("fontcache"); |
| 46 | if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) { |
| 47 | name.append("-mt"); |
| 48 | } |
| 49 | return name; |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 50 | } |
| 51 | |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 52 | SkISize onISize() override { return SkISize::Make(kSize, kSize); } |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 53 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 54 | void onOnceBeforeDraw() override { |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 55 | fTypefaces[0] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Italic()); |
| 56 | fTypefaces[1] = sk_tool_utils::create_portable_typeface("sans-serif",SkFontStyle::Italic()); |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 57 | fTypefaces[2] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Normal()); |
| 58 | fTypefaces[3] = |
| 59 | sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Normal()); |
| 60 | fTypefaces[4] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Bold()); |
| 61 | fTypefaces[5] = sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Bold()); |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 62 | } |
skia.committer@gmail.com | f29c380 | 2013-10-10 07:01:40 +0000 | [diff] [blame] | 63 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 64 | void onDraw(SkCanvas* canvas) override { |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 65 | GrRenderTargetContext* renderTargetContext = |
| 66 | canvas->internal_private_accessTopLayerRenderTargetContext(); |
| 67 | if (!renderTargetContext) { |
| 68 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
| 69 | return; |
| 70 | } |
| 71 | |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 72 | canvas->clear(SK_ColorLTGRAY); |
| 73 | this->drawText(canvas); |
| 74 | // Debugging tool for GPU. |
| 75 | static const bool kShowAtlas = false; |
| 76 | if (kShowAtlas) { |
| 77 | if (auto ctx = canvas->getGrContext()) { |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 78 | auto img = ctx->contextPriv().getFontAtlasImage_ForTesting(kA8_GrMaskFormat); |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 79 | canvas->drawImage(img, 0, 0); |
| 80 | } |
| 81 | } |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 82 | } |
| 83 | |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 84 | private: |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 85 | void drawText(SkCanvas* canvas) { |
| 86 | static const int kSizes[] = {8, 9, 10, 11, 12, 13, 18, 20, 25}; |
| 87 | |
| 88 | static const SkString kTexts[] = {SkString("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), |
| 89 | SkString("abcdefghijklmnopqrstuvwxyz"), |
| 90 | SkString("0123456789"), |
| 91 | SkString("!@#$%^&*()<>[]{}")}; |
| 92 | SkPaint paint; |
| 93 | paint.setAntiAlias(true); |
| 94 | paint.setLCDRenderText(false); |
| 95 | paint.setSubpixelText(true); |
| 96 | |
| 97 | static const SkScalar kSubPixelInc = 1 / 2.f; |
| 98 | SkScalar x = 0; |
| 99 | SkScalar y = 10; |
| 100 | SkScalar subpixelX = 0; |
| 101 | SkScalar subpixelY = 0; |
| 102 | bool offsetX = true; |
| 103 | |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 104 | if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) { |
| 105 | canvas->scale(10, 10); |
| 106 | } |
| 107 | |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 108 | do { |
| 109 | for (auto s : kSizes) { |
| 110 | auto size = 2 * s; |
| 111 | paint.setTextSize(size); |
| 112 | for (const auto& typeface : fTypefaces) { |
| 113 | paint.setTypeface(typeface); |
| 114 | for (const auto& text : kTexts) { |
| 115 | x = size + draw_string(canvas, text, x + subpixelX, y + subpixelY, paint); |
| 116 | x = SkScalarCeilToScalar(x); |
| 117 | if (x + 100 > kSize) { |
| 118 | x = 0; |
| 119 | y += SkScalarCeilToScalar(size + 3); |
| 120 | if (y > kSize) { |
| 121 | return; |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | } |
| 126 | (offsetX ? subpixelX : subpixelY) += kSubPixelInc; |
| 127 | offsetX = !offsetX; |
| 128 | } |
| 129 | } while (true); |
| 130 | } |
| 131 | |
| 132 | static constexpr SkScalar kSize = 1280; |
| 133 | |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 134 | GrContextOptions::Enable fAllowMultipleTextures; |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 135 | sk_sp<SkTypeface> fTypefaces[6]; |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 136 | typedef GM INHERITED; |
| 137 | }; |
| 138 | |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 139 | constexpr SkScalar FontCacheGM::kSize; |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 140 | |
| 141 | ////////////////////////////////////////////////////////////////////////////// |
| 142 | |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 143 | DEF_GM(return new FontCacheGM(GrContextOptions::Enable::kNo)) |
| 144 | DEF_GM(return new FontCacheGM(GrContextOptions::Enable::kYes)) |
| 145 | |
| 146 | #endif |