blob: b93cc3f031c62a31c96100ec0dd64615bc76f324 [file] [log] [blame]
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +00001/*
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 Salomon9f545bc2017-11-06 10:36:57 -05008#include "GrContext.h"
9#include "GrContextOptions.h"
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000010#include "SkCanvas.h"
11#include "SkGraphics.h"
Brian Salomon9f545bc2017-11-06 10:36:57 -050012#include "SkImage.h"
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000013#include "SkTypeface.h"
Brian Salomon9f545bc2017-11-06 10:36:57 -050014#include "gm.h"
15#include "sk_tool_utils.h"
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000016
skia.committer@gmail.comf29c3802013-10-10 07:01:40 +000017// GM to stress the GPU font cache
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000018
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000019static SkScalar draw_string(SkCanvas* canvas, const SkString& text, SkScalar x,
20 SkScalar y, const SkPaint& paint) {
Cary Clark2a475ea2017-04-28 15:35:12 -040021 canvas->drawString(text, x, y, paint);
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000022 return x + paint.measureText(text.c_str(), text.size());
23}
24
25class FontCacheGM : public skiagm::GM {
26public:
Brian Salomon9f545bc2017-11-06 10:36:57 -050027 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.comf29c3802013-10-10 07:01:40 +000033
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000034protected:
mtklein36352bf2015-03-25 18:17:31 -070035 SkString onShortName() override {
jvanverth@google.com73d13d72013-10-09 18:53:35 +000036 return SkString("fontcache");
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000037 }
38
Brian Salomon9f545bc2017-11-06 10:36:57 -050039 SkISize onISize() override { return SkISize::Make(kSize, kSize); }
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000040
mtklein36352bf2015-03-25 18:17:31 -070041 void onOnceBeforeDraw() override {
Ben Wagner71319502017-07-27 10:45:29 -040042 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 Salomon9f545bc2017-11-06 10:36:57 -050044 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.org338a49f2013-10-09 18:12:23 +000049 }
skia.committer@gmail.comf29c3802013-10-10 07:01:40 +000050
mtklein36352bf2015-03-25 18:17:31 -070051 void onDraw(SkCanvas* canvas) override {
Brian Salomon9f545bc2017-11-06 10:36:57 -050052 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.org338a49f2013-10-09 18:12:23 +000062 }
63
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000064private:
Brian Salomon9f545bc2017-11-06 10:36:57 -050065 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.org338a49f2013-10-09 18:12:23 +0000111 typedef GM INHERITED;
112};
113
Brian Salomon9f545bc2017-11-06 10:36:57 -0500114constexpr SkScalar FontCacheGM::kSize;
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +0000115
116//////////////////////////////////////////////////////////////////////////////
117
halcanary385fe4d2015-08-26 13:07:48 -0700118DEF_GM(return new FontCacheGM;)