blob: c9c8cf7480bc3f3abede6332073dca06a738c3c1 [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,
24 SkScalar y, const SkPaint& paint) {
Cary Clark2a475ea2017-04-28 15:35:12 -040025 canvas->drawString(text, x, y, paint);
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000026 return x + paint.measureText(text.c_str(), text.size());
27}
28
29class FontCacheGM : public skiagm::GM {
30public:
Jim Van Verthfc4f7682018-01-25 16:26:25 -050031 FontCacheGM(GrContextOptions::Enable allowMultipleTextures)
32 : fAllowMultipleTextures(allowMultipleTextures) {
33 this->setBGColor(SK_ColorLTGRAY);
34 }
Brian Salomon9f545bc2017-11-06 10:36:57 -050035
36 void modifyGrContextOptions(GrContextOptions* options) override {
37 options->fGlyphCacheTextureMaximumBytes = 0;
Jim Van Verthfc4f7682018-01-25 16:26:25 -050038 options->fAllowMultipleGlyphCacheTextures = fAllowMultipleTextures;
Brian Salomon9f545bc2017-11-06 10:36:57 -050039 }
skia.committer@gmail.comf29c3802013-10-10 07:01:40 +000040
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000041protected:
mtklein36352bf2015-03-25 18:17:31 -070042 SkString onShortName() override {
Jim Van Verthfc4f7682018-01-25 16:26:25 -050043 SkString name("fontcache");
44 if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) {
45 name.append("-mt");
46 }
47 return name;
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000048 }
49
Brian Salomon9f545bc2017-11-06 10:36:57 -050050 SkISize onISize() override { return SkISize::Make(kSize, kSize); }
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000051
mtklein36352bf2015-03-25 18:17:31 -070052 void onOnceBeforeDraw() override {
Ben Wagner71319502017-07-27 10:45:29 -040053 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 Salomon9f545bc2017-11-06 10:36:57 -050055 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.org338a49f2013-10-09 18:12:23 +000060 }
skia.committer@gmail.comf29c3802013-10-10 07:01:40 +000061
mtklein36352bf2015-03-25 18:17:31 -070062 void onDraw(SkCanvas* canvas) override {
Jim Van Verthfc4f7682018-01-25 16:26:25 -050063 GrRenderTargetContext* renderTargetContext =
64 canvas->internal_private_accessTopLayerRenderTargetContext();
65 if (!renderTargetContext) {
66 skiagm::GM::DrawGpuOnlyMessage(canvas);
67 return;
68 }
69
Brian Salomon9f545bc2017-11-06 10:36:57 -050070 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 Phillips0c4b7b12018-03-06 08:20:37 -050076 auto img = ctx->contextPriv().getFontAtlasImage_ForTesting(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("!@#$%^&*()<>[]{}")};
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 Verthfc4f7682018-01-25 16:26:25 -0500102 if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) {
103 canvas->scale(10, 10);
104 }
105
Brian Salomon9f545bc2017-11-06 10:36:57 -0500106 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 Verthfc4f7682018-01-25 16:26:25 -0500132 GrContextOptions::Enable fAllowMultipleTextures;
Brian Salomon9f545bc2017-11-06 10:36:57 -0500133 sk_sp<SkTypeface> fTypefaces[6];
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +0000134 typedef GM INHERITED;
135};
136
Brian Salomon9f545bc2017-11-06 10:36:57 -0500137constexpr SkScalar FontCacheGM::kSize;
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +0000138
139//////////////////////////////////////////////////////////////////////////////
140
Jim Van Verthfc4f7682018-01-25 16:26:25 -0500141DEF_GM(return new FontCacheGM(GrContextOptions::Enable::kNo))
142DEF_GM(return new FontCacheGM(GrContextOptions::Enable::kYes))