blob: 5ec147a1ae8230c875f847cfe689751ec9e9b401 [file] [log] [blame]
joshualitte49109f2015-07-17 12:47:39 -07001/*
2 * Copyright 2015 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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tools/ToolUtils.h"
joshualitt7f9c9eb2015-08-21 11:08:00 -07009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkCanvas.h"
11#include "include/core/SkFontMgr.h"
12#include "include/core/SkGraphics.h"
13#include "include/core/SkPaint.h"
14#include "include/core/SkPoint.h"
15#include "include/core/SkSurface.h"
16#include "include/core/SkTextBlob.h"
17#include "include/core/SkTypeface.h"
18#include "src/core/SkGlyphRun.h"
19#include "tools/fonts/RandomScalerContext.h"
joshualitte49109f2015-07-17 12:47:39 -070020
21#ifdef SK_BUILD_FOR_WIN
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022 #include "include/ports/SkTypeface_win.h"
joshualitte49109f2015-07-17 12:47:39 -070023#endif
24
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "tests/Test.h"
joshualitte49109f2015-07-17 12:47:39 -070026
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "include/gpu/GrContext.h"
28#include "src/gpu/GrContextPriv.h"
joshualitte49109f2015-07-17 12:47:39 -070029
fmalita37283c22016-09-13 10:00:23 -070030static void draw(SkCanvas* canvas, int redraw, const SkTArray<sk_sp<SkTextBlob>>& blobs) {
joshualitt404d9d62015-07-22 11:00:32 -070031 int yOffset = 0;
joshualitte49109f2015-07-17 12:47:39 -070032 for (int r = 0; r < redraw; r++) {
33 for (int i = 0; i < blobs.count(); i++) {
fmalita37283c22016-09-13 10:00:23 -070034 const auto& blob = blobs[i];
joshualitt404d9d62015-07-22 11:00:32 -070035 const SkRect& bounds = blob->bounds();
36 yOffset += SkScalarCeilToInt(bounds.height());
joshualitte49109f2015-07-17 12:47:39 -070037 SkPaint paint;
joshualitt404d9d62015-07-22 11:00:32 -070038 canvas->drawTextBlob(blob, 0, SkIntToScalar(yOffset), paint);
joshualitte49109f2015-07-17 12:47:39 -070039 }
40 }
41}
42
joshualitt11dfc8e2015-07-23 08:30:25 -070043static const int kWidth = 1024;
44static const int kHeight = 768;
joshualitte49109f2015-07-17 12:47:39 -070045
Herb Derbyd3895d82018-09-04 13:27:00 -040046static void setup_always_evict_atlas(GrContext* context) {
Robert Phillips9da87e02019-02-04 13:26:26 -050047 context->priv().getAtlasManager()->setAtlasSizesToMinimum_ForTesting();
Herb Derbyd3895d82018-09-04 13:27:00 -040048}
49
joshualitte49109f2015-07-17 12:47:39 -070050// This test hammers the GPU textblobcache and font atlas
kkinnunen15302832015-12-01 04:35:26 -080051static void text_blob_cache_inner(skiatest::Reporter* reporter, GrContext* context,
joshualitt7f9c9eb2015-08-21 11:08:00 -070052 int maxTotalText, int maxGlyphID, int maxFamilies, bool normal,
53 bool stressTest) {
joshualitte49109f2015-07-17 12:47:39 -070054 // setup surface
55 uint32_t flags = 0;
56 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
57
joshualitt7f9c9eb2015-08-21 11:08:00 -070058 // configure our context for maximum stressing of cache and atlas
59 if (stressTest) {
Herb Derbyd3895d82018-09-04 13:27:00 -040060 setup_always_evict_atlas(context);
Robert Phillipsdbaf3172019-02-06 15:12:53 -050061 context->priv().testingOnly_setTextBlobCacheLimit(0);
joshualitt7f9c9eb2015-08-21 11:08:00 -070062 }
63
Brian Osman7c597742019-03-26 11:10:11 -040064 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, kRGBA_8888_SkColorType,
65 kPremul_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070066 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, &props));
joshualitte49109f2015-07-17 12:47:39 -070067 REPORTER_ASSERT(reporter, surface);
68 if (!surface) {
69 return;
70 }
71
72 SkCanvas* canvas = surface->getCanvas();
73
Hal Canary342b7ac2016-11-04 11:49:42 -040074 sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
joshualitte49109f2015-07-17 12:47:39 -070075
joshualitt65e96b42015-07-31 11:45:22 -070076 int count = SkMin32(fm->countFamilies(), maxFamilies);
joshualitte49109f2015-07-17 12:47:39 -070077
78 // make a ton of text
joshualitt65e96b42015-07-31 11:45:22 -070079 SkAutoTArray<uint16_t> text(maxTotalText);
80 for (int i = 0; i < maxTotalText; i++) {
81 text[i] = i % maxGlyphID;
joshualitte49109f2015-07-17 12:47:39 -070082 }
83
84 // generate textblobs
fmalita37283c22016-09-13 10:00:23 -070085 SkTArray<sk_sp<SkTextBlob>> blobs;
joshualitte49109f2015-07-17 12:47:39 -070086 for (int i = 0; i < count; i++) {
Mike Reed70914f52018-11-23 13:08:33 -050087 SkFont font;
88 font.setSize(48); // draw big glyphs to really stress the atlas
joshualitte49109f2015-07-17 12:47:39 -070089
90 SkString familyName;
91 fm->getFamilyName(i, &familyName);
Hal Canary342b7ac2016-11-04 11:49:42 -040092 sk_sp<SkFontStyleSet> set(fm->createStyleSet(i));
joshualitte49109f2015-07-17 12:47:39 -070093 for (int j = 0; j < set->count(); ++j) {
94 SkFontStyle fs;
halcanary96fcdcc2015-08-27 07:41:13 -070095 set->getStyle(j, &fs, nullptr);
joshualitte49109f2015-07-17 12:47:39 -070096
joshualitt65e96b42015-07-31 11:45:22 -070097 // We use a typeface which randomy returns unexpected mask formats to fuzz
bungeman13b9c952016-05-12 10:09:30 -070098 sk_sp<SkTypeface> orig(set->createTypeface(j));
joshualitt65e96b42015-07-31 11:45:22 -070099 if (normal) {
Mike Reed70914f52018-11-23 13:08:33 -0500100 font.setTypeface(orig);
joshualitt65e96b42015-07-31 11:45:22 -0700101 } else {
Mike Reed70914f52018-11-23 13:08:33 -0500102 font.setTypeface(sk_make_sp<SkRandomTypeface>(orig, SkPaint(), true));
joshualitt65e96b42015-07-31 11:45:22 -0700103 }
joshualitte49109f2015-07-17 12:47:39 -0700104
105 SkTextBlobBuilder builder;
106 for (int aa = 0; aa < 2; aa++) {
107 for (int subpixel = 0; subpixel < 2; subpixel++) {
108 for (int lcd = 0; lcd < 2; lcd++) {
Mike Reed70914f52018-11-23 13:08:33 -0500109 font.setEdging(SkFont::Edging::kAlias);
110 if (aa) {
111 font.setEdging(SkFont::Edging::kAntiAlias);
112 if (lcd) {
113 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
114 }
joshualitt65e96b42015-07-31 11:45:22 -0700115 }
Mike Reed70914f52018-11-23 13:08:33 -0500116 font.setSubpixel(SkToBool(subpixel));
117 if (!SkToBool(lcd)) {
118 font.setSize(160);
119 }
120 const SkTextBlobBuilder::RunBuffer& run = builder.allocRun(font,
joshualitt65e96b42015-07-31 11:45:22 -0700121 maxTotalText,
joshualitte49109f2015-07-17 12:47:39 -0700122 0, 0,
halcanary96fcdcc2015-08-27 07:41:13 -0700123 nullptr);
joshualitt65e96b42015-07-31 11:45:22 -0700124 memcpy(run.glyphs, text.get(), maxTotalText * sizeof(uint16_t));
joshualitte49109f2015-07-17 12:47:39 -0700125 }
126 }
127 }
fmalita37283c22016-09-13 10:00:23 -0700128 blobs.emplace_back(builder.make());
joshualitte49109f2015-07-17 12:47:39 -0700129 }
130 }
131
joshualitt11dfc8e2015-07-23 08:30:25 -0700132 // create surface where LCD is impossible
Brian Osman7c597742019-03-26 11:10:11 -0400133 info = SkImageInfo::Make(kWidth, kHeight, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
joshualitt11dfc8e2015-07-23 08:30:25 -0700134 SkSurfaceProps propsNoLCD(0, kUnknown_SkPixelGeometry);
reede8f30622016-03-23 18:59:25 -0700135 auto surfaceNoLCD(canvas->makeSurface(info, &propsNoLCD));
joshualitt11dfc8e2015-07-23 08:30:25 -0700136 REPORTER_ASSERT(reporter, surface);
137 if (!surface) {
138 return;
139 }
140
141 SkCanvas* canvasNoLCD = surfaceNoLCD->getCanvas();
142
joshualitte49109f2015-07-17 12:47:39 -0700143 // test redraw
144 draw(canvas, 2, blobs);
joshualitt11dfc8e2015-07-23 08:30:25 -0700145 draw(canvasNoLCD, 2, blobs);
joshualitte49109f2015-07-17 12:47:39 -0700146
147 // test draw after free
kkinnunen15302832015-12-01 04:35:26 -0800148 context->freeGpuResources();
joshualitte49109f2015-07-17 12:47:39 -0700149 draw(canvas, 1, blobs);
150
kkinnunen15302832015-12-01 04:35:26 -0800151 context->freeGpuResources();
joshualitt11dfc8e2015-07-23 08:30:25 -0700152 draw(canvasNoLCD, 1, blobs);
153
joshualitte49109f2015-07-17 12:47:39 -0700154 // test draw after abandon
kkinnunen15302832015-12-01 04:35:26 -0800155 context->abandonContext();
joshualitte49109f2015-07-17 12:47:39 -0700156 draw(canvas, 1, blobs);
157}
joshualitt65e96b42015-07-31 11:45:22 -0700158
Brian Osman7c597742019-03-26 11:10:11 -0400159DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobCache, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700160 text_blob_cache_inner(reporter, ctxInfo.grContext(), 1024, 256, 30, true, false);
joshualitt7f9c9eb2015-08-21 11:08:00 -0700161}
162
Brian Osman7c597742019-03-26 11:10:11 -0400163DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobStressCache, reporter, ctxInfo) {
Herb Derby278b0672018-10-05 13:46:51 -0400164 text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, true, true);
joshualitt65e96b42015-07-31 11:45:22 -0700165}
166
Brian Osman7c597742019-03-26 11:10:11 -0400167DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobAbnormal, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700168 text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, false, false);
joshualitt7f9c9eb2015-08-21 11:08:00 -0700169}
170
Brian Osman7c597742019-03-26 11:10:11 -0400171DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobStressAbnormal, reporter, ctxInfo) {
Herb Derby278b0672018-10-05 13:46:51 -0400172 text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, false, true);
joshualitt65e96b42015-07-31 11:45:22 -0700173}