2013-10-09 18:12:23 +00:00
|
|
|
/*
|
|
|
|
* Copyright 2013 Google Inc.
|
|
|
|
*
|
|
|
|
* Use of this source code is governed by a BSD-style license that can be
|
|
|
|
* found in the LICENSE file.
|
|
|
|
*/
|
|
|
|
|
2018-01-25 21:26:25 +00:00
|
|
|
// GM to stress the GPU font cache
|
|
|
|
// It's not necessary to run this with CPU configs
|
|
|
|
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "gm/gm.h"
|
|
|
|
#include "include/core/SkCanvas.h"
|
2019-05-01 21:28:53 +00:00
|
|
|
#include "include/core/SkColor.h"
|
|
|
|
#include "include/core/SkFont.h"
|
|
|
|
#include "include/core/SkFontStyle.h"
|
|
|
|
#include "include/core/SkFontTypes.h"
|
|
|
|
#include "include/core/SkPaint.h"
|
|
|
|
#include "include/core/SkRefCnt.h"
|
|
|
|
#include "include/core/SkScalar.h"
|
|
|
|
#include "include/core/SkSize.h"
|
|
|
|
#include "include/core/SkString.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "include/core/SkTypeface.h"
|
|
|
|
#include "include/gpu/GrContextOptions.h"
|
2020-07-10 20:23:47 +00:00
|
|
|
#include "include/gpu/GrDirectContext.h"
|
2022-04-12 13:59:38 +00:00
|
|
|
#include "include/private/gpu/ganesh/GrTypesPriv.h"
|
2022-04-07 15:20:24 +00:00
|
|
|
#include "src/gpu/ganesh/GrDirectContextPriv.h"
|
2019-04-23 17:05:21 +00:00
|
|
|
#include "tools/ToolUtils.h"
|
2013-10-09 18:12:23 +00:00
|
|
|
|
|
|
|
static SkScalar draw_string(SkCanvas* canvas, const SkString& text, SkScalar x,
|
2018-12-02 21:04:27 +00:00
|
|
|
SkScalar y, const SkFont& font) {
|
|
|
|
SkPaint paint;
|
2019-01-07 14:36:09 +00:00
|
|
|
canvas->drawString(text, x, y, font, paint);
|
2019-05-07 19:38:46 +00:00
|
|
|
return x + font.measureText(text.c_str(), text.size(), SkTextEncoding::kUTF8);
|
2013-10-09 18:12:23 +00:00
|
|
|
}
|
|
|
|
|
2021-06-03 14:14:16 +00:00
|
|
|
class FontCacheGM : public skiagm::GM {
|
2013-10-09 18:12:23 +00:00
|
|
|
public:
|
2018-01-25 21:26:25 +00:00
|
|
|
FontCacheGM(GrContextOptions::Enable allowMultipleTextures)
|
|
|
|
: fAllowMultipleTextures(allowMultipleTextures) {
|
|
|
|
this->setBGColor(SK_ColorLTGRAY);
|
|
|
|
}
|
2017-11-06 15:36:57 +00:00
|
|
|
|
|
|
|
void modifyGrContextOptions(GrContextOptions* options) override {
|
|
|
|
options->fGlyphCacheTextureMaximumBytes = 0;
|
2018-01-25 21:26:25 +00:00
|
|
|
options->fAllowMultipleGlyphCacheTextures = fAllowMultipleTextures;
|
2017-11-06 15:36:57 +00:00
|
|
|
}
|
2013-10-10 07:01:40 +00:00
|
|
|
|
2013-10-09 18:12:23 +00:00
|
|
|
protected:
|
2015-03-26 01:17:31 +00:00
|
|
|
SkString onShortName() override {
|
2018-01-25 21:26:25 +00:00
|
|
|
SkString name("fontcache");
|
|
|
|
if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) {
|
|
|
|
name.append("-mt");
|
|
|
|
}
|
|
|
|
return name;
|
2013-10-09 18:12:23 +00:00
|
|
|
}
|
|
|
|
|
2017-11-06 15:36:57 +00:00
|
|
|
SkISize onISize() override { return SkISize::Make(kSize, kSize); }
|
2013-10-09 18:12:23 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onOnceBeforeDraw() override {
|
2019-03-20 16:12:10 +00:00
|
|
|
fTypefaces[0] = ToolUtils::create_portable_typeface("serif", SkFontStyle::Italic());
|
|
|
|
fTypefaces[1] = ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Italic());
|
|
|
|
fTypefaces[2] = ToolUtils::create_portable_typeface("serif", SkFontStyle::Normal());
|
|
|
|
fTypefaces[3] = ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Normal());
|
|
|
|
fTypefaces[4] = ToolUtils::create_portable_typeface("serif", SkFontStyle::Bold());
|
|
|
|
fTypefaces[5] = ToolUtils::create_portable_typeface("sans-serif", SkFontStyle::Bold());
|
2013-10-09 18:12:23 +00:00
|
|
|
}
|
2013-10-10 07:01:40 +00:00
|
|
|
|
2021-06-03 14:14:16 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2017-11-06 15:36:57 +00:00
|
|
|
this->drawText(canvas);
|
|
|
|
// Debugging tool for GPU.
|
|
|
|
static const bool kShowAtlas = false;
|
|
|
|
if (kShowAtlas) {
|
2021-06-03 14:14:16 +00:00
|
|
|
if (auto dContext = GrAsDirectContext(canvas->recordingContext())) {
|
|
|
|
auto img = dContext->priv().testingOnly_getFontAtlasImage(kA8_GrMaskFormat);
|
2017-11-06 15:36:57 +00:00
|
|
|
canvas->drawImage(img, 0, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void drawText(SkCanvas* canvas) {
|
|
|
|
static const int kSizes[] = {8, 9, 10, 11, 12, 13, 18, 20, 25};
|
|
|
|
|
|
|
|
static const SkString kTexts[] = {SkString("ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
|
|
|
|
SkString("abcdefghijklmnopqrstuvwxyz"),
|
|
|
|
SkString("0123456789"),
|
|
|
|
SkString("!@#$%^&*()<>[]{}")};
|
2018-12-02 21:04:27 +00:00
|
|
|
SkFont font;
|
|
|
|
font.setEdging(SkFont::Edging::kAntiAlias);
|
|
|
|
font.setSubpixel(true);
|
2017-11-06 15:36:57 +00:00
|
|
|
|
|
|
|
static const SkScalar kSubPixelInc = 1 / 2.f;
|
|
|
|
SkScalar x = 0;
|
|
|
|
SkScalar y = 10;
|
|
|
|
SkScalar subpixelX = 0;
|
|
|
|
SkScalar subpixelY = 0;
|
|
|
|
bool offsetX = true;
|
|
|
|
|
2018-01-25 21:26:25 +00:00
|
|
|
if (GrContextOptions::Enable::kYes == fAllowMultipleTextures) {
|
|
|
|
canvas->scale(10, 10);
|
|
|
|
}
|
|
|
|
|
2017-11-06 15:36:57 +00:00
|
|
|
do {
|
|
|
|
for (auto s : kSizes) {
|
|
|
|
auto size = 2 * s;
|
2018-12-02 21:04:27 +00:00
|
|
|
font.setSize(size);
|
2017-11-06 15:36:57 +00:00
|
|
|
for (const auto& typeface : fTypefaces) {
|
2018-12-02 21:04:27 +00:00
|
|
|
font.setTypeface(typeface);
|
2017-11-06 15:36:57 +00:00
|
|
|
for (const auto& text : kTexts) {
|
2018-12-02 21:04:27 +00:00
|
|
|
x = size + draw_string(canvas, text, x + subpixelX, y + subpixelY, font);
|
2017-11-06 15:36:57 +00:00
|
|
|
x = SkScalarCeilToScalar(x);
|
|
|
|
if (x + 100 > kSize) {
|
|
|
|
x = 0;
|
|
|
|
y += SkScalarCeilToScalar(size + 3);
|
|
|
|
if (y > kSize) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
(offsetX ? subpixelX : subpixelY) += kSubPixelInc;
|
|
|
|
offsetX = !offsetX;
|
|
|
|
}
|
|
|
|
} while (true);
|
2013-10-09 18:12:23 +00:00
|
|
|
}
|
|
|
|
|
2021-10-08 22:48:26 +00:00
|
|
|
inline static constexpr SkScalar kSize = 1280;
|
2017-11-06 15:36:57 +00:00
|
|
|
|
2018-01-25 21:26:25 +00:00
|
|
|
GrContextOptions::Enable fAllowMultipleTextures;
|
2017-11-06 15:36:57 +00:00
|
|
|
sk_sp<SkTypeface> fTypefaces[6];
|
2020-09-03 02:42:33 +00:00
|
|
|
using INHERITED = GM;
|
2013-10-09 18:12:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2018-01-25 21:26:25 +00:00
|
|
|
DEF_GM(return new FontCacheGM(GrContextOptions::Enable::kNo))
|
|
|
|
DEF_GM(return new FontCacheGM(GrContextOptions::Enable::kYes))
|