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
|
|
|
|
|
|
|
|
#include "gm.h"
|
|
|
|
|
2017-11-06 15:36:57 +00:00
|
|
|
#include "GrContext.h"
|
2018-03-06 13:20:37 +00:00
|
|
|
#include "GrContextPriv.h"
|
2017-11-06 15:36:57 +00:00
|
|
|
#include "GrContextOptions.h"
|
2013-10-09 18:12:23 +00:00
|
|
|
#include "SkCanvas.h"
|
|
|
|
#include "SkGraphics.h"
|
2017-11-06 15:36:57 +00:00
|
|
|
#include "SkImage.h"
|
2013-10-09 18:12:23 +00:00
|
|
|
#include "SkTypeface.h"
|
2017-11-06 15:36:57 +00:00
|
|
|
#include "gm.h"
|
|
|
|
#include "sk_tool_utils.h"
|
2013-10-09 18:12:23 +00:00
|
|
|
|
|
|
|
static SkScalar draw_string(SkCanvas* canvas, const SkString& text, SkScalar x,
|
|
|
|
SkScalar y, const SkPaint& paint) {
|
2017-04-28 19:35:12 +00:00
|
|
|
canvas->drawString(text, x, y, paint);
|
2013-10-09 18:12:23 +00:00
|
|
|
return x + paint.measureText(text.c_str(), text.size());
|
|
|
|
}
|
|
|
|
|
|
|
|
class FontCacheGM : public skiagm::GM {
|
|
|
|
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 {
|
2017-07-27 14:45:29 +00:00
|
|
|
fTypefaces[0] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Italic());
|
|
|
|
fTypefaces[1] = sk_tool_utils::create_portable_typeface("sans-serif",SkFontStyle::Italic());
|
2017-11-06 15:36:57 +00:00
|
|
|
fTypefaces[2] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Normal());
|
|
|
|
fTypefaces[3] =
|
|
|
|
sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Normal());
|
|
|
|
fTypefaces[4] = sk_tool_utils::create_portable_typeface("serif", SkFontStyle::Bold());
|
|
|
|
fTypefaces[5] = sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Bold());
|
2013-10-09 18:12:23 +00:00
|
|
|
}
|
2013-10-10 07:01:40 +00:00
|
|
|
|
2015-03-26 01:17:31 +00:00
|
|
|
void onDraw(SkCanvas* canvas) override {
|
2018-01-25 21:26:25 +00:00
|
|
|
GrRenderTargetContext* renderTargetContext =
|
|
|
|
canvas->internal_private_accessTopLayerRenderTargetContext();
|
|
|
|
if (!renderTargetContext) {
|
|
|
|
skiagm::GM::DrawGpuOnlyMessage(canvas);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-06 15:36:57 +00:00
|
|
|
canvas->clear(SK_ColorLTGRAY);
|
|
|
|
this->drawText(canvas);
|
|
|
|
// Debugging tool for GPU.
|
|
|
|
static const bool kShowAtlas = false;
|
|
|
|
if (kShowAtlas) {
|
|
|
|
if (auto ctx = canvas->getGrContext()) {
|
2018-03-06 13:20:37 +00:00
|
|
|
auto img = ctx->contextPriv().getFontAtlasImage_ForTesting(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("!@#$%^&*()<>[]{}")};
|
2013-10-09 18:12:23 +00:00
|
|
|
SkPaint paint;
|
|
|
|
paint.setAntiAlias(true);
|
2017-11-06 15:36:57 +00:00
|
|
|
paint.setLCDRenderText(false);
|
2013-10-09 18:12:23 +00:00
|
|
|
paint.setSubpixelText(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;
|
|
|
|
paint.setTextSize(size);
|
|
|
|
for (const auto& typeface : fTypefaces) {
|
|
|
|
paint.setTypeface(typeface);
|
|
|
|
for (const auto& text : kTexts) {
|
|
|
|
x = size + draw_string(canvas, text, x + subpixelX, y + subpixelY, paint);
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-11-06 15:36:57 +00:00
|
|
|
static constexpr SkScalar kSize = 1280;
|
|
|
|
|
2018-01-25 21:26:25 +00:00
|
|
|
GrContextOptions::Enable fAllowMultipleTextures;
|
2017-11-06 15:36:57 +00:00
|
|
|
sk_sp<SkTypeface> fTypefaces[6];
|
2013-10-09 18:12:23 +00:00
|
|
|
typedef GM INHERITED;
|
|
|
|
};
|
|
|
|
|
2017-11-06 15:36:57 +00:00
|
|
|
constexpr SkScalar FontCacheGM::kSize;
|
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))
|