blob: 53d241d13c7e906d860c79267e107200ebfb21b8 [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
Jim Van Verthfc4f7682018-01-25 16:26:25 -05008// 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 Salomon9f545bc2017-11-06 10:36:57 -050013#include "GrContext.h"
Robert Phillips0c4b7b12018-03-06 08:20:37 -050014#include "GrContextPriv.h"
Brian Salomon9f545bc2017-11-06 10:36:57 -050015#include "GrContextOptions.h"
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000016#include "SkCanvas.h"
17#include "SkGraphics.h"
Brian Salomon9f545bc2017-11-06 10:36:57 -050018#include "SkImage.h"
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000019#include "SkTypeface.h"
Brian Salomon9f545bc2017-11-06 10:36:57 -050020#include "gm.h"
21#include "sk_tool_utils.h"
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000022
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000023static SkScalar draw_string(SkCanvas* canvas, const SkString& text, SkScalar x,
Mike Reed94cca602018-12-02 16:04:27 -050024 SkScalar y, const SkFont& font) {
25 SkPaint paint;
Hal Canary89a644b2019-01-07 09:36:09 -050026 canvas->drawString(text, x, y, font, paint);
Mike Reed94cca602018-12-02 16:04:27 -050027 return x + font.measureText(text.c_str(), text.size(), kUTF8_SkTextEncoding);
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000028}
29
Chris Dalton3a778372019-02-07 15:23:36 -070030class FontCacheGM : public skiagm::GpuGM {
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000031public:
Jim Van Verthfc4f7682018-01-25 16:26:25 -050032 FontCacheGM(GrContextOptions::Enable allowMultipleTextures)
33 : fAllowMultipleTextures(allowMultipleTextures) {
34 this->setBGColor(SK_ColorLTGRAY);
35 }
Brian Salomon9f545bc2017-11-06 10:36:57 -050036
37 void modifyGrContextOptions(GrContextOptions* options) override {
38 options->fGlyphCacheTextureMaximumBytes = 0;
Jim Van Verthfc4f7682018-01-25 16:26:25 -050039 options->fAllowMultipleGlyphCacheTextures = fAllowMultipleTextures;
Brian Salomon9f545bc2017-11-06 10:36:57 -050040 }
skia.committer@gmail.comf29c3802013-10-10 07:01:40 +000041
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000042protected:
mtklein36352bf2015-03-25 18:17:31 -070043 SkString onShortName() override {
Jim Van Verthfc4f7682018-01-25 16:26:25 -050044 SkString name("fontcache");
45 if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) {
46 name.append("-mt");
47 }
48 return name;
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000049 }
50
Brian Salomon9f545bc2017-11-06 10:36:57 -050051 SkISize onISize() override { return SkISize::Make(kSize, kSize); }
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000052
mtklein36352bf2015-03-25 18:17:31 -070053 void onOnceBeforeDraw() override {
Ben Wagner71319502017-07-27 10:45:29 -040054 fTypefaces[0] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Italic());
55 fTypefaces[1] = sk_tool_utils::create_portable_typeface("sans-serif",SkFontStyle::Italic());
Brian Salomon9f545bc2017-11-06 10:36:57 -050056 fTypefaces[2] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Normal());
57 fTypefaces[3] =
58 sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Normal());
59 fTypefaces[4] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Bold());
60 fTypefaces[5] = sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Bold());
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000061 }
skia.committer@gmail.comf29c3802013-10-10 07:01:40 +000062
Chris Dalton50e24d72019-02-07 16:20:09 -070063 void onDraw(GrContext*, GrRenderTargetContext*, SkCanvas* canvas) override {
Brian Salomon9f545bc2017-11-06 10:36:57 -050064 this->drawText(canvas);
65 // Debugging tool for GPU.
66 static const bool kShowAtlas = false;
67 if (kShowAtlas) {
68 if (auto ctx = canvas->getGrContext()) {
Robert Phillipsdbaf3172019-02-06 15:12:53 -050069 auto img = ctx->priv().testingOnly_getFontAtlasImage(kA8_GrMaskFormat);
Brian Salomon9f545bc2017-11-06 10:36:57 -050070 canvas->drawImage(img, 0, 0);
71 }
72 }
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000073 }
74
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000075private:
Brian Salomon9f545bc2017-11-06 10:36:57 -050076 void drawText(SkCanvas* canvas) {
77 static const int kSizes[] = {8, 9, 10, 11, 12, 13, 18, 20, 25};
78
79 static const SkString kTexts[] = {SkString("ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
80 SkString("abcdefghijklmnopqrstuvwxyz"),
81 SkString("0123456789"),
82 SkString("!@#$%^&*()<>[]{}")};
Mike Reed94cca602018-12-02 16:04:27 -050083 SkFont font;
84 font.setEdging(SkFont::Edging::kAntiAlias);
85 font.setSubpixel(true);
Brian Salomon9f545bc2017-11-06 10:36:57 -050086
87 static const SkScalar kSubPixelInc = 1 / 2.f;
88 SkScalar x = 0;
89 SkScalar y = 10;
90 SkScalar subpixelX = 0;
91 SkScalar subpixelY = 0;
92 bool offsetX = true;
93
Jim Van Verthfc4f7682018-01-25 16:26:25 -050094 if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) {
95 canvas->scale(10, 10);
96 }
97
Brian Salomon9f545bc2017-11-06 10:36:57 -050098 do {
99 for (auto s : kSizes) {
100 auto size = 2 * s;
Mike Reed94cca602018-12-02 16:04:27 -0500101 font.setSize(size);
Brian Salomon9f545bc2017-11-06 10:36:57 -0500102 for (const auto& typeface : fTypefaces) {
Mike Reed94cca602018-12-02 16:04:27 -0500103 font.setTypeface(typeface);
Brian Salomon9f545bc2017-11-06 10:36:57 -0500104 for (const auto& text : kTexts) {
Mike Reed94cca602018-12-02 16:04:27 -0500105 x = size + draw_string(canvas, text, x + subpixelX, y + subpixelY, font);
Brian Salomon9f545bc2017-11-06 10:36:57 -0500106 x = SkScalarCeilToScalar(x);
107 if (x + 100 > kSize) {
108 x = 0;
109 y += SkScalarCeilToScalar(size + 3);
110 if (y > kSize) {
111 return;
112 }
113 }
114 }
115 }
116 (offsetX ? subpixelX : subpixelY) += kSubPixelInc;
117 offsetX = !offsetX;
118 }
119 } while (true);
120 }
121
122 static constexpr SkScalar kSize = 1280;
123
Jim Van Verthfc4f7682018-01-25 16:26:25 -0500124 GrContextOptions::Enable fAllowMultipleTextures;
Brian Salomon9f545bc2017-11-06 10:36:57 -0500125 sk_sp<SkTypeface> fTypefaces[6];
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +0000126 typedef GM INHERITED;
127};
128
Brian Salomon9f545bc2017-11-06 10:36:57 -0500129constexpr SkScalar FontCacheGM::kSize;
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +0000130
131//////////////////////////////////////////////////////////////////////////////
132
Jim Van Verthfc4f7682018-01-25 16:26:25 -0500133DEF_GM(return new FontCacheGM(GrContextOptions::Enable::kNo))
134DEF_GM(return new FontCacheGM(GrContextOptions::Enable::kYes))