blob: a373627a17a9f671faaf5efd630a405744e6e416 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -050011#include "gm/gm.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "include/core/SkCanvas.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040013#include "include/core/SkColor.h"
14#include "include/core/SkFont.h"
15#include "include/core/SkFontStyle.h"
16#include "include/core/SkFontTypes.h"
17#include "include/core/SkPaint.h"
18#include "include/core/SkRefCnt.h"
19#include "include/core/SkScalar.h"
20#include "include/core/SkSize.h"
21#include "include/core/SkString.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "include/core/SkTypeface.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "include/gpu/GrContextOptions.h"
Robert Phillipsb27b38b2020-07-10 16:23:47 -040024#include "include/gpu/GrDirectContext.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040025#include "include/private/GrTypesPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrContextPriv.h"
27#include "tools/ToolUtils.h"
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000028
Ben Wagner7fde8e12019-05-01 17:28:53 -040029class GrRenderTargetContext;
30
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000031static SkScalar draw_string(SkCanvas* canvas, const SkString& text, SkScalar x,
Mike Reed94cca602018-12-02 16:04:27 -050032 SkScalar y, const SkFont& font) {
33 SkPaint paint;
Hal Canary89a644b2019-01-07 09:36:09 -050034 canvas->drawString(text, x, y, font, paint);
Ben Wagner51e15a62019-05-07 15:38:46 -040035 return x + font.measureText(text.c_str(), text.size(), SkTextEncoding::kUTF8);
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000036}
37
Chris Dalton3a778372019-02-07 15:23:36 -070038class FontCacheGM : public skiagm::GpuGM {
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000039public:
Jim Van Verthfc4f7682018-01-25 16:26:25 -050040 FontCacheGM(GrContextOptions::Enable allowMultipleTextures)
41 : fAllowMultipleTextures(allowMultipleTextures) {
42 this->setBGColor(SK_ColorLTGRAY);
43 }
Brian Salomon9f545bc2017-11-06 10:36:57 -050044
45 void modifyGrContextOptions(GrContextOptions* options) override {
46 options->fGlyphCacheTextureMaximumBytes = 0;
Jim Van Verthfc4f7682018-01-25 16:26:25 -050047 options->fAllowMultipleGlyphCacheTextures = fAllowMultipleTextures;
Brian Salomon9f545bc2017-11-06 10:36:57 -050048 }
skia.committer@gmail.comf29c3802013-10-10 07:01:40 +000049
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000050protected:
mtklein36352bf2015-03-25 18:17:31 -070051 SkString onShortName() override {
Jim Van Verthfc4f7682018-01-25 16:26:25 -050052 SkString name("fontcache");
53 if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) {
54 name.append("-mt");
55 }
56 return name;
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000057 }
58
Brian Salomon9f545bc2017-11-06 10:36:57 -050059 SkISize onISize() override { return SkISize::Make(kSize, kSize); }
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000060
mtklein36352bf2015-03-25 18:17:31 -070061 void onOnceBeforeDraw() override {
Mike Kleinea3f0142019-03-20 11:12:10 -050062 fTypefaces[0] = ToolUtils::create_portable_typeface("serif", SkFontStyle::Italic());
63 fTypefaces[1] = ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Italic());
64 fTypefaces[2] = ToolUtils::create_portable_typeface("serif", SkFontStyle::Normal());
65 fTypefaces[3] = ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Normal());
66 fTypefaces[4] = ToolUtils::create_portable_typeface("serif", SkFontStyle::Bold());
67 fTypefaces[5] = ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Bold());
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000068 }
skia.committer@gmail.comf29c3802013-10-10 07:01:40 +000069
Robert Phillips95c250c2020-06-29 15:36:12 -040070 void onDraw(GrRecordingContext*, GrRenderTargetContext*, SkCanvas* canvas) override {
Brian Salomon9f545bc2017-11-06 10:36:57 -050071 this->drawText(canvas);
72 // Debugging tool for GPU.
73 static const bool kShowAtlas = false;
74 if (kShowAtlas) {
Robert Phillipsb27b38b2020-07-10 16:23:47 -040075 if (auto direct = GrAsDirectContext(canvas->recordingContext())) {
76 auto img = direct->priv().testingOnly_getFontAtlasImage(kA8_GrMaskFormat);
Brian Salomon9f545bc2017-11-06 10:36:57 -050077 canvas->drawImage(img, 0, 0);
78 }
79 }
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000080 }
81
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000082private:
Brian Salomon9f545bc2017-11-06 10:36:57 -050083 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("!@#$%^&*()<>[]{}")};
Mike Reed94cca602018-12-02 16:04:27 -050090 SkFont font;
91 font.setEdging(SkFont::Edging::kAntiAlias);
92 font.setSubpixel(true);
Brian Salomon9f545bc2017-11-06 10:36:57 -050093
94 static const SkScalar kSubPixelInc = 1 / 2.f;
95 SkScalar x = 0;
96 SkScalar y = 10;
97 SkScalar subpixelX = 0;
98 SkScalar subpixelY = 0;
99 bool offsetX = true;
100
Jim Van Verthfc4f7682018-01-25 16:26:25 -0500101 if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) {
102 canvas->scale(10, 10);
103 }
104
Brian Salomon9f545bc2017-11-06 10:36:57 -0500105 do {
106 for (auto s : kSizes) {
107 auto size = 2 * s;
Mike Reed94cca602018-12-02 16:04:27 -0500108 font.setSize(size);
Brian Salomon9f545bc2017-11-06 10:36:57 -0500109 for (const auto& typeface : fTypefaces) {
Mike Reed94cca602018-12-02 16:04:27 -0500110 font.setTypeface(typeface);
Brian Salomon9f545bc2017-11-06 10:36:57 -0500111 for (const auto& text : kTexts) {
Mike Reed94cca602018-12-02 16:04:27 -0500112 x = size + draw_string(canvas, text, x + subpixelX, y + subpixelY, font);
Brian Salomon9f545bc2017-11-06 10:36:57 -0500113 x = SkScalarCeilToScalar(x);
114 if (x + 100 > kSize) {
115 x = 0;
116 y += SkScalarCeilToScalar(size + 3);
117 if (y > kSize) {
118 return;
119 }
120 }
121 }
122 }
123 (offsetX ? subpixelX : subpixelY) += kSubPixelInc;
124 offsetX = !offsetX;
125 }
126 } while (true);
127 }
128
129 static constexpr SkScalar kSize = 1280;
130
Jim Van Verthfc4f7682018-01-25 16:26:25 -0500131 GrContextOptions::Enable fAllowMultipleTextures;
Brian Salomon9f545bc2017-11-06 10:36:57 -0500132 sk_sp<SkTypeface> fTypefaces[6];
John Stiles7571f9e2020-09-02 22:42:33 -0400133 using INHERITED = GM;
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +0000134};
135
Brian Salomon9f545bc2017-11-06 10:36:57 -0500136constexpr SkScalar FontCacheGM::kSize;
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +0000137
138//////////////////////////////////////////////////////////////////////////////
139
Jim Van Verthfc4f7682018-01-25 16:26:25 -0500140DEF_GM(return new FontCacheGM(GrContextOptions::Enable::kNo))
141DEF_GM(return new FontCacheGM(GrContextOptions::Enable::kYes))