blob: 6c99700d935702a1583ebc67ab6acec87f832346 [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
13#if SK_SUPPORT_GPU
14
Brian Salomon9f545bc2017-11-06 10:36:57 -050015#include "GrContext.h"
Robert Phillips0c4b7b12018-03-06 08:20:37 -050016#include "GrContextPriv.h"
Brian Salomon9f545bc2017-11-06 10:36:57 -050017#include "GrContextOptions.h"
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000018#include "SkCanvas.h"
19#include "SkGraphics.h"
Brian Salomon9f545bc2017-11-06 10:36:57 -050020#include "SkImage.h"
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000021#include "SkTypeface.h"
Brian Salomon9f545bc2017-11-06 10:36:57 -050022#include "gm.h"
23#include "sk_tool_utils.h"
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000024
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000025static SkScalar draw_string(SkCanvas* canvas, const SkString& text, SkScalar x,
26 SkScalar y, const SkPaint& paint) {
Cary Clark2a475ea2017-04-28 15:35:12 -040027 canvas->drawString(text, x, y, paint);
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000028 return x + paint.measureText(text.c_str(), text.size());
29}
30
31class FontCacheGM : public skiagm::GM {
32public:
Jim Van Verthfc4f7682018-01-25 16:26:25 -050033 FontCacheGM(GrContextOptions::Enable allowMultipleTextures)
34 : fAllowMultipleTextures(allowMultipleTextures) {
35 this->setBGColor(SK_ColorLTGRAY);
36 }
Brian Salomon9f545bc2017-11-06 10:36:57 -050037
38 void modifyGrContextOptions(GrContextOptions* options) override {
39 options->fGlyphCacheTextureMaximumBytes = 0;
Jim Van Verthfc4f7682018-01-25 16:26:25 -050040 options->fAllowMultipleGlyphCacheTextures = fAllowMultipleTextures;
Brian Salomon9f545bc2017-11-06 10:36:57 -050041 }
skia.committer@gmail.comf29c3802013-10-10 07:01:40 +000042
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000043protected:
mtklein36352bf2015-03-25 18:17:31 -070044 SkString onShortName() override {
Jim Van Verthfc4f7682018-01-25 16:26:25 -050045 SkString name("fontcache");
46 if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) {
47 name.append("-mt");
48 }
49 return name;
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000050 }
51
Brian Salomon9f545bc2017-11-06 10:36:57 -050052 SkISize onISize() override { return SkISize::Make(kSize, kSize); }
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000053
mtklein36352bf2015-03-25 18:17:31 -070054 void onOnceBeforeDraw() override {
Ben Wagner71319502017-07-27 10:45:29 -040055 fTypefaces[0] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Italic());
56 fTypefaces[1] = sk_tool_utils::create_portable_typeface("sans-serif",SkFontStyle::Italic());
Brian Salomon9f545bc2017-11-06 10:36:57 -050057 fTypefaces[2] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Normal());
58 fTypefaces[3] =
59 sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Normal());
60 fTypefaces[4] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Bold());
61 fTypefaces[5] = sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Bold());
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000062 }
skia.committer@gmail.comf29c3802013-10-10 07:01:40 +000063
mtklein36352bf2015-03-25 18:17:31 -070064 void onDraw(SkCanvas* canvas) override {
Jim Van Verthfc4f7682018-01-25 16:26:25 -050065 GrRenderTargetContext* renderTargetContext =
66 canvas->internal_private_accessTopLayerRenderTargetContext();
67 if (!renderTargetContext) {
68 skiagm::GM::DrawGpuOnlyMessage(canvas);
69 return;
70 }
71
Brian Salomon9f545bc2017-11-06 10:36:57 -050072 canvas->clear(SK_ColorLTGRAY);
73 this->drawText(canvas);
74 // Debugging tool for GPU.
75 static const bool kShowAtlas = false;
76 if (kShowAtlas) {
77 if (auto ctx = canvas->getGrContext()) {
Robert Phillips0c4b7b12018-03-06 08:20:37 -050078 auto img = ctx->contextPriv().getFontAtlasImage_ForTesting(kA8_GrMaskFormat);
Brian Salomon9f545bc2017-11-06 10:36:57 -050079 canvas->drawImage(img, 0, 0);
80 }
81 }
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000082 }
83
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +000084private:
Brian Salomon9f545bc2017-11-06 10:36:57 -050085 void drawText(SkCanvas* canvas) {
86 static const int kSizes[] = {8, 9, 10, 11, 12, 13, 18, 20, 25};
87
88 static const SkString kTexts[] = {SkString("ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
89 SkString("abcdefghijklmnopqrstuvwxyz"),
90 SkString("0123456789"),
91 SkString("!@#$%^&*()<>[]{}")};
92 SkPaint paint;
93 paint.setAntiAlias(true);
94 paint.setLCDRenderText(false);
95 paint.setSubpixelText(true);
96
97 static const SkScalar kSubPixelInc = 1 / 2.f;
98 SkScalar x = 0;
99 SkScalar y = 10;
100 SkScalar subpixelX = 0;
101 SkScalar subpixelY = 0;
102 bool offsetX = true;
103
Jim Van Verthfc4f7682018-01-25 16:26:25 -0500104 if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) {
105 canvas->scale(10, 10);
106 }
107
Brian Salomon9f545bc2017-11-06 10:36:57 -0500108 do {
109 for (auto s : kSizes) {
110 auto size = 2 * s;
111 paint.setTextSize(size);
112 for (const auto& typeface : fTypefaces) {
113 paint.setTypeface(typeface);
114 for (const auto& text : kTexts) {
115 x = size + draw_string(canvas, text, x + subpixelX, y + subpixelY, paint);
116 x = SkScalarCeilToScalar(x);
117 if (x + 100 > kSize) {
118 x = 0;
119 y += SkScalarCeilToScalar(size + 3);
120 if (y > kSize) {
121 return;
122 }
123 }
124 }
125 }
126 (offsetX ? subpixelX : subpixelY) += kSubPixelInc;
127 offsetX = !offsetX;
128 }
129 } while (true);
130 }
131
132 static constexpr SkScalar kSize = 1280;
133
Jim Van Verthfc4f7682018-01-25 16:26:25 -0500134 GrContextOptions::Enable fAllowMultipleTextures;
Brian Salomon9f545bc2017-11-06 10:36:57 -0500135 sk_sp<SkTypeface> fTypefaces[6];
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +0000136 typedef GM INHERITED;
137};
138
Brian Salomon9f545bc2017-11-06 10:36:57 -0500139constexpr SkScalar FontCacheGM::kSize;
commit-bot@chromium.org338a49f2013-10-09 18:12:23 +0000140
141//////////////////////////////////////////////////////////////////////////////
142
Jim Van Verthfc4f7682018-01-25 16:26:25 -0500143DEF_GM(return new FontCacheGM(GrContextOptions::Enable::kNo))
144DEF_GM(return new FontCacheGM(GrContextOptions::Enable::kYes))
145
146#endif