| 1 |
/* |
| 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 |
|
| 8 |
// GM to stress the GPU font cache |
| 9 |
// It's not necessary to run this with CPU configs |
| 10 |
|
| 11 |
#include "gm/gm.h" |
| 12 |
#include "include/core/SkCanvas.h" |
| 13 |
#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" |
| 22 |
#include "include/core/SkTypeface.h" |
| 23 |
#include "src/gpu/AtlasTypes.h" |
| 24 |
#include "tools/ToolUtils.h" |
| 25 |
#include "tools/fonts/FontToolUtils.h" |
| 26 |
|
| 27 |
#if defined(SK_GANESH) |
| 28 |
#include "include/gpu/ganesh/GrContextOptions.h" |
| 29 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 30 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 31 |
#include "src/gpu/AtlasTypes.h" |
| 32 |
#include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 33 |
#endif |
| 34 |
|
| 35 |
using MaskFormat = skgpu::MaskFormat; |
| 36 |
|
| 37 |
static SkScalar draw_string(SkCanvas* canvas, const SkString& text, SkScalar x, |
| 38 |
SkScalar y, const SkFont& font) { |
| 39 |
SkPaint paint; |
| 40 |
canvas->drawString(text, x, y, font, paint); |
| 41 |
return x + font.measureText(text.c_str(), text.size(), SkTextEncoding::kUTF8); |
| 42 |
} |
| 43 |
|
| 44 |
class FontCacheGM : public skiagm::GM { |
| 45 |
public: |
| 46 |
FontCacheGM(bool allowMultipleTextures) : fAllowMultipleTextures(allowMultipleTextures) { |
| 47 |
this->setBGColor(SK_ColorLTGRAY); |
| 48 |
} |
| 49 |
|
| 50 |
#if defined(SK_GANESH) |
| 51 |
void modifyGrContextOptions(GrContextOptions* options) override { |
| 52 |
options->fGlyphCacheTextureMaximumBytes = 0; |
| 53 |
using Enable = GrContextOptions::Enable; |
| 54 |
options->fAllowMultipleGlyphCacheTextures = fAllowMultipleTextures ? Enable::kYes |
| 55 |
: Enable::kNo; |
| 56 |
} |
| 57 |
#endif |
| 58 |
|
| 59 |
protected: |
| 60 |
SkString getName() const override { |
| 61 |
SkString name("fontcache"); |
| 62 |
if (fAllowMultipleTextures) { |
| 63 |
name.append("-mt"); |
| 64 |
} |
| 65 |
return name; |
| 66 |
} |
| 67 |
|
| 68 |
SkISize getISize() override { return SkISize::Make(kSize, kSize); } |
| 69 |
|
| 70 |
void onOnceBeforeDraw() override { |
| 71 |
fTypefaces[0] = ToolUtils::CreatePortableTypeface("serif", SkFontStyle::Italic()); |
| 72 |
fTypefaces[1] = ToolUtils::CreatePortableTypeface("sans-serif", SkFontStyle::Italic()); |
| 73 |
fTypefaces[2] = ToolUtils::CreatePortableTypeface("serif", SkFontStyle::Normal()); |
| 74 |
fTypefaces[3] = ToolUtils::CreatePortableTypeface("sans-serif", SkFontStyle::Normal()); |
| 75 |
fTypefaces[4] = ToolUtils::CreatePortableTypeface("serif", SkFontStyle::Bold()); |
| 76 |
fTypefaces[5] = ToolUtils::CreatePortableTypeface("sans-serif", SkFontStyle::Bold()); |
| 77 |
} |
| 78 |
|
| 79 |
void onDraw(SkCanvas* canvas) override { |
| 80 |
this->drawText(canvas); |
| 81 |
#if defined(SK_GANESH) |
| 82 |
// Debugging tool for Ganesh. |
| 83 |
static const bool kShowAtlas = false; |
| 84 |
if (kShowAtlas) { |
| 85 |
if (auto dContext = GrAsDirectContext(canvas->recordingContext())) { |
| 86 |
auto img = dContext->priv().testingOnly_getFontAtlasImage(MaskFormat::kA8); |
| 87 |
canvas->drawImage(img, 0, 0); |
| 88 |
} |
| 89 |
} |
| 90 |
#endif |
| 91 |
} |
| 92 |
|
| 93 |
private: |
| 94 |
void drawText(SkCanvas* canvas) { |
| 95 |
static const int kSizes[] = {8, 9, 10, 11, 12, 13, 18, 20, 25}; |
| 96 |
|
| 97 |
static const SkString kTexts[] = {SkString("ABCDEFGHIJKLMNOPQRSTUVWXYZ"), |
| 98 |
SkString("abcdefghijklmnopqrstuvwxyz"), |
| 99 |
SkString("0123456789"), |
| 100 |
SkString("!@#$%^&*()<>[]{}")}; |
| 101 |
SkFont font = ToolUtils::DefaultPortableFont(); |
| 102 |
font.setEdging(SkFont::Edging::kAntiAlias); |
| 103 |
font.setSubpixel(true); |
| 104 |
|
| 105 |
static const SkScalar kSubPixelInc = 1 / 2.f; |
| 106 |
SkScalar x = 0; |
| 107 |
SkScalar y = 10; |
| 108 |
SkScalar subpixelX = 0; |
| 109 |
SkScalar subpixelY = 0; |
| 110 |
bool offsetX = true; |
| 111 |
|
| 112 |
if (fAllowMultipleTextures) { |
| 113 |
canvas->scale(10, 10); |
| 114 |
} |
| 115 |
|
| 116 |
do { |
| 117 |
for (auto s : kSizes) { |
| 118 |
auto size = 2 * s; |
| 119 |
font.setSize(size); |
| 120 |
for (const auto& typeface : fTypefaces) { |
| 121 |
font.setTypeface(typeface); |
| 122 |
for (const auto& text : kTexts) { |
| 123 |
x = size + draw_string(canvas, text, x + subpixelX, y + subpixelY, font); |
| 124 |
x = SkScalarCeilToScalar(x); |
| 125 |
if (x + 100 > kSize) { |
| 126 |
x = 0; |
| 127 |
y += SkScalarCeilToScalar(size + 3); |
| 128 |
if (y > kSize) { |
| 129 |
return; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
(offsetX ? subpixelX : subpixelY) += kSubPixelInc; |
| 135 |
offsetX = !offsetX; |
| 136 |
} |
| 137 |
} while (true); |
| 138 |
} |
| 139 |
|
| 140 |
inline static constexpr SkScalar kSize = 1280; |
| 141 |
|
| 142 |
bool fAllowMultipleTextures; |
| 143 |
sk_sp<SkTypeface> fTypefaces[6]; |
| 144 |
using INHERITED = GM; |
| 145 |
}; |
| 146 |
|
| 147 |
////////////////////////////////////////////////////////////////////////////// |
| 148 |
|
| 149 |
DEF_GM(return new FontCacheGM(/*allowMultipleTextures=*/true)) |
| 150 |
DEF_GM(return new FontCacheGM(/*allowMultipleTextures=*/false)) |