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