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 | |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 13 | #include "GrContext.h" |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 14 | #include "GrContextPriv.h" |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 15 | #include "GrContextOptions.h" |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 16 | #include "SkCanvas.h" |
| 17 | #include "SkGraphics.h" |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 18 | #include "SkImage.h" |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 19 | #include "SkTypeface.h" |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 20 | #include "gm.h" |
| 21 | #include "sk_tool_utils.h" |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 22 | |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 23 | static SkScalar draw_string(SkCanvas* canvas, const SkString& text, SkScalar x, |
| 24 | SkScalar y, const SkPaint& paint) { |
Cary Clark | 2a475ea | 2017-04-28 15:35:12 -0400 | [diff] [blame] | 25 | canvas->drawString(text, x, y, paint); |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 26 | return x + paint.measureText(text.c_str(), text.size()); |
| 27 | } |
| 28 | |
| 29 | class FontCacheGM : public skiagm::GM { |
| 30 | public: |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 31 | FontCacheGM(GrContextOptions::Enable allowMultipleTextures) |
| 32 | : fAllowMultipleTextures(allowMultipleTextures) { |
| 33 | this->setBGColor(SK_ColorLTGRAY); |
| 34 | } |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 35 | |
| 36 | void modifyGrContextOptions(GrContextOptions* options) override { |
| 37 | options->fGlyphCacheTextureMaximumBytes = 0; |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 38 | options->fAllowMultipleGlyphCacheTextures = fAllowMultipleTextures; |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 39 | } |
skia.committer@gmail.com | f29c380 | 2013-10-10 07:01:40 +0000 | [diff] [blame] | 40 | |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 41 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 42 | SkString onShortName() override { |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 43 | SkString name("fontcache"); |
| 44 | if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) { |
| 45 | name.append("-mt"); |
| 46 | } |
| 47 | return name; |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 48 | } |
| 49 | |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 50 | SkISize onISize() override { return SkISize::Make(kSize, kSize); } |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 51 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 52 | void onOnceBeforeDraw() override { |
Ben Wagner | 7131950 | 2017-07-27 10:45:29 -0400 | [diff] [blame] | 53 | fTypefaces[0] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Italic()); |
| 54 | fTypefaces[1] = sk_tool_utils::create_portable_typeface("sans-serif",SkFontStyle::Italic()); |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 55 | fTypefaces[2] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Normal()); |
| 56 | fTypefaces[3] = |
| 57 | sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Normal()); |
| 58 | fTypefaces[4] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Bold()); |
| 59 | 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] | 60 | } |
skia.committer@gmail.com | f29c380 | 2013-10-10 07:01:40 +0000 | [diff] [blame] | 61 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 62 | void onDraw(SkCanvas* canvas) override { |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 63 | GrRenderTargetContext* renderTargetContext = |
| 64 | canvas->internal_private_accessTopLayerRenderTargetContext(); |
| 65 | if (!renderTargetContext) { |
| 66 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
| 67 | return; |
| 68 | } |
| 69 | |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 70 | canvas->clear(SK_ColorLTGRAY); |
| 71 | this->drawText(canvas); |
| 72 | // Debugging tool for GPU. |
| 73 | static const bool kShowAtlas = false; |
| 74 | if (kShowAtlas) { |
| 75 | if (auto ctx = canvas->getGrContext()) { |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 76 | auto img = ctx->contextPriv().getFontAtlasImage_ForTesting(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("!@#$%^&*()<>[]{}")}; |
| 90 | SkPaint paint; |
| 91 | paint.setAntiAlias(true); |
| 92 | paint.setLCDRenderText(false); |
| 93 | paint.setSubpixelText(true); |
| 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; |
| 109 | paint.setTextSize(size); |
| 110 | for (const auto& typeface : fTypefaces) { |
| 111 | paint.setTypeface(typeface); |
| 112 | for (const auto& text : kTexts) { |
| 113 | x = size + draw_string(canvas, text, x + subpixelX, y + subpixelY, paint); |
| 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 | |
| 130 | static constexpr SkScalar kSize = 1280; |
| 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]; |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 134 | typedef GM INHERITED; |
| 135 | }; |
| 136 | |
Brian Salomon | 9f545bc | 2017-11-06 10:36:57 -0500 | [diff] [blame] | 137 | constexpr SkScalar FontCacheGM::kSize; |
commit-bot@chromium.org | 338a49f | 2013-10-09 18:12:23 +0000 | [diff] [blame] | 138 | |
| 139 | ////////////////////////////////////////////////////////////////////////////// |
| 140 | |
Jim Van Verth | fc4f768 | 2018-01-25 16:26:25 -0500 | [diff] [blame] | 141 | DEF_GM(return new FontCacheGM(GrContextOptions::Enable::kNo)) |
| 142 | DEF_GM(return new FontCacheGM(GrContextOptions::Enable::kYes)) |