| 1 |
/* |
| 2 |
* Copyright 2018 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 TextBlob regeneration and the GPU font cache |
| 9 |
// It's not necessary to run this with CPU configs |
| 10 |
// |
| 11 |
// The point here is to draw a set of text that will fit in one Plot, and then some large |
| 12 |
// text. After a flush we draw the first set of text again with a slightly different color, |
| 13 |
// and then enough new large text to spill the entire atlas. What *should* happen is that |
| 14 |
// the Plot with the first set of text will not get overwritten by the new large text. |
| 15 |
|
| 16 |
#include "gm/gm.h" |
| 17 |
#include "include/core/SkCanvas.h" |
| 18 |
#include "include/core/SkColor.h" |
| 19 |
#include "include/core/SkFont.h" |
| 20 |
#include "include/core/SkFontMgr.h" |
| 21 |
#include "include/core/SkFontStyle.h" |
| 22 |
#include "include/core/SkFontTypes.h" |
| 23 |
#include "include/core/SkPaint.h" |
| 24 |
#include "include/core/SkRefCnt.h" |
| 25 |
#include "include/core/SkScalar.h" |
| 26 |
#include "include/core/SkSize.h" |
| 27 |
#include "include/core/SkString.h" |
| 28 |
#include "include/core/SkTextBlob.h" |
| 29 |
#include "include/core/SkTypeface.h" |
| 30 |
#include "include/core/SkTypes.h" |
| 31 |
#include "include/private/base/SkTemplates.h" |
| 32 |
#include "src/gpu/AtlasTypes.h" |
| 33 |
#include "tools/ToolUtils.h" |
| 34 |
#include "tools/fonts/FontToolUtils.h" |
| 35 |
|
| 36 |
#if defined(SK_GANESH) |
| 37 |
#include "include/gpu/ganesh/GrContextOptions.h" |
| 38 |
#include "include/gpu/ganesh/GrDirectContext.h" |
| 39 |
#include "include/gpu/ganesh/GrRecordingContext.h" |
| 40 |
#include "include/private/gpu/ganesh/GrTypesPriv.h" |
| 41 |
#include "src/gpu/AtlasTypes.h" |
| 42 |
#include "src/gpu/ganesh/GrDirectContextPriv.h" |
| 43 |
#endif |
| 44 |
|
| 45 |
#if defined(SK_GRAPHITE) |
| 46 |
#include "include/gpu/graphite/ContextOptions.h" |
| 47 |
#endif |
| 48 |
|
| 49 |
using namespace skia_private; |
| 50 |
using MaskFormat = skgpu::MaskFormat; |
| 51 |
|
| 52 |
static sk_sp<SkTextBlob> make_blob(const SkString& text, const SkFont& font) { |
| 53 |
size_t len = text.size(); |
| 54 |
AutoTArray<SkScalar> pos(len); |
| 55 |
AutoTArray<SkGlyphID> glyphs(len); |
| 56 |
|
| 57 |
font.textToGlyphs(text.c_str(), len, SkTextEncoding::kUTF8, glyphs); |
| 58 |
font.getXPos(glyphs, pos); |
| 59 |
return SkTextBlob::MakeFromPosTextH(text.c_str(), len, pos, 0, font); |
| 60 |
} |
| 61 |
|
| 62 |
class FontRegenGM : public skiagm::GM { |
| 63 |
#if defined(SK_GANESH) |
| 64 |
void modifyGrContextOptions(GrContextOptions* options) override { |
| 65 |
options->fGlyphCacheTextureMaximumBytes = 0; |
| 66 |
options->fAllowMultipleGlyphCacheTextures = GrContextOptions::Enable::kNo; |
| 67 |
} |
| 68 |
#endif |
| 69 |
|
| 70 |
#if defined(SK_GRAPHITE) |
| 71 |
void modifyGraphiteContextOptions(skgpu::graphite::ContextOptions* options) const override { |
| 72 |
options->fGlyphCacheTextureMaximumBytes = 0; |
| 73 |
options->fAllowMultipleAtlasTextures = false; |
| 74 |
} |
| 75 |
#endif |
| 76 |
|
| 77 |
SkString getName() const override { return SkString("fontregen"); } |
| 78 |
|
| 79 |
SkISize getISize() override { return {kSize, kSize}; } |
| 80 |
|
| 81 |
void onOnceBeforeDraw() override { |
| 82 |
this->setBGColor(SK_ColorLTGRAY); |
| 83 |
|
| 84 |
auto tf = ToolUtils::CreatePortableTypeface("sans-serif", SkFontStyle::Normal()); |
| 85 |
|
| 86 |
static const SkString kTexts[] = { |
| 87 |
SkString("abcdefghijklmnopqrstuvwxyz"), |
| 88 |
SkString("ABCDEFGHI"), |
| 89 |
SkString("NOPQRSTUV") |
| 90 |
}; |
| 91 |
|
| 92 |
SkFont font; |
| 93 |
font.setEdging(SkFont::Edging::kAntiAlias); |
| 94 |
font.setSubpixel(false); |
| 95 |
font.setSize(80); |
| 96 |
font.setTypeface(tf); |
| 97 |
|
| 98 |
fBlobs[0] = make_blob(kTexts[0], font); |
| 99 |
font.setSize(162); |
| 100 |
fBlobs[1] = make_blob(kTexts[1], font); |
| 101 |
fBlobs[2] = make_blob(kTexts[2], font); |
| 102 |
} |
| 103 |
|
| 104 |
DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override { |
| 105 |
|
| 106 |
|
| 107 |
SkPaint paint; |
| 108 |
paint.setColor(SK_ColorBLACK); |
| 109 |
canvas->drawTextBlob(fBlobs[0], 10, 80, paint); |
| 110 |
canvas->drawTextBlob(fBlobs[1], 10, 225, paint); |
| 111 |
|
| 112 |
#if defined(SK_GANESH) |
| 113 |
auto dContext = GrAsDirectContext(canvas->recordingContext()); |
| 114 |
if (dContext) { |
| 115 |
dContext->flushAndSubmit(); |
| 116 |
} |
| 117 |
#endif |
| 118 |
|
| 119 |
paint.setColor(0xFF010101); |
| 120 |
canvas->drawTextBlob(fBlobs[0], 10, 305, paint); |
| 121 |
canvas->drawTextBlob(fBlobs[2], 10, 465, paint); |
| 122 |
|
| 123 |
#if defined(SK_GANESH) |
| 124 |
// Debugging tool for Ganesh. |
| 125 |
static const bool kShowAtlas = false; |
| 126 |
if (kShowAtlas && dContext) { |
| 127 |
auto img = dContext->priv().testingOnly_getFontAtlasImage(MaskFormat::kA8); |
| 128 |
canvas->drawImage(img, 200, 0); |
| 129 |
} |
| 130 |
#endif |
| 131 |
|
| 132 |
return DrawResult::kOk; |
| 133 |
} |
| 134 |
|
| 135 |
private: |
| 136 |
inline static constexpr int kSize = 512; |
| 137 |
|
| 138 |
sk_sp<SkTextBlob> fBlobs[3]; |
| 139 |
using INHERITED = GM; |
| 140 |
}; |
| 141 |
|
| 142 |
////////////////////////////////////////////////////////////////////////////// |
| 143 |
|
| 144 |
DEF_GM(return new FontRegenGM()) |
| 145 |
|
| 146 |
/////////////////////////////////////////////////////////////////////////////// |
| 147 |
|
| 148 |
class BadAppleGM : public skiagm::GM { |
| 149 |
SkString getName() const override { return SkString("badapple"); } |
| 150 |
|
| 151 |
SkISize getISize() override { return {kSize, kSize}; } |
| 152 |
|
| 153 |
void onOnceBeforeDraw() override { |
| 154 |
this->setBGColor(SK_ColorWHITE); |
| 155 |
auto fm = ToolUtils::TestFontMgr(); |
| 156 |
|
| 157 |
static const SkString kTexts[] = { |
| 158 |
SkString("Meet"), |
| 159 |
SkString("iPad Pro"), |
| 160 |
}; |
| 161 |
|
| 162 |
SkFont font = ToolUtils::DefaultPortableFont(); |
| 163 |
font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 164 |
font.setSubpixel(true); |
| 165 |
font.setSize(256); |
| 166 |
|
| 167 |
fBlobs[0] = make_blob(kTexts[0], font); |
| 168 |
fBlobs[1] = make_blob(kTexts[1], font); |
| 169 |
} |
| 170 |
|
| 171 |
void onDraw(SkCanvas* canvas) override { |
| 172 |
SkPaint paint; |
| 173 |
paint.setColor(0xFF111111); |
| 174 |
canvas->drawTextBlob(fBlobs[0], 10, 260, paint); |
| 175 |
canvas->drawTextBlob(fBlobs[1], 10, 500, paint); |
| 176 |
} |
| 177 |
|
| 178 |
private: |
| 179 |
inline static constexpr int kSize = 512; |
| 180 |
|
| 181 |
sk_sp<SkTextBlob> fBlobs[3]; |
| 182 |
using INHERITED = GM; |
| 183 |
}; |
| 184 |
|
| 185 |
////////////////////////////////////////////////////////////////////////////// |
| 186 |
|
| 187 |
DEF_GM(return new BadAppleGM()) |