blob: 4a1d49c71acc1288cf14f870ab90bf5ad767cd45 [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
8#include "sk_tool_utils.h"
joshualitt7f9c9eb2015-08-21 11:08:00 -07009
joshualitte49109f2015-07-17 12:47:39 -070010#include "SkCanvas.h"
joshualitte49109f2015-07-17 12:47:39 -070011#include "SkFontMgr.h"
Herb Derbyd3895d82018-09-04 13:27:00 -040012#include "SkGlyphRun.h"
joshualitte49109f2015-07-17 12:47:39 -070013#include "SkGraphics.h"
Mike Klein10d66cc2017-11-10 11:33:43 -050014#include "SkPaint.h"
15#include "SkPoint.h"
16#include "SkRandomScalerContext.h"
joshualitte49109f2015-07-17 12:47:39 -070017#include "SkSurface.h"
Mike Klein10d66cc2017-11-10 11:33:43 -050018#include "SkTextBlob.h"
joshualitte49109f2015-07-17 12:47:39 -070019#include "SkTypeface.h"
20
21#ifdef SK_BUILD_FOR_WIN
22 #include "SkTypeface_win.h"
23#endif
24
25#include "Test.h"
26
kkinnunen15302832015-12-01 04:35:26 -080027#include "GrContext.h"
Robert Phillips0c4b7b12018-03-06 08:20:37 -050028#include "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) {
Herb Derby3c4d5332018-09-07 15:27:57 -040047 context->contextPriv().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 Phillips0c4b7b12018-03-06 08:20:37 -050061 context->contextPriv().setTextBlobCacheLimit_ForTesting(0);
joshualitt7f9c9eb2015-08-21 11:08:00 -070062 }
63
joshualitt11dfc8e2015-07-23 08:30:25 -070064 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, kN32_SkColorType, kPremul_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070065 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, &props));
joshualitte49109f2015-07-17 12:47:39 -070066 REPORTER_ASSERT(reporter, surface);
67 if (!surface) {
68 return;
69 }
70
71 SkCanvas* canvas = surface->getCanvas();
72
Hal Canary342b7ac2016-11-04 11:49:42 -040073 sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
joshualitte49109f2015-07-17 12:47:39 -070074
joshualitt65e96b42015-07-31 11:45:22 -070075 int count = SkMin32(fm->countFamilies(), maxFamilies);
joshualitte49109f2015-07-17 12:47:39 -070076
77 // make a ton of text
joshualitt65e96b42015-07-31 11:45:22 -070078 SkAutoTArray<uint16_t> text(maxTotalText);
79 for (int i = 0; i < maxTotalText; i++) {
80 text[i] = i % maxGlyphID;
joshualitte49109f2015-07-17 12:47:39 -070081 }
82
83 // generate textblobs
fmalita37283c22016-09-13 10:00:23 -070084 SkTArray<sk_sp<SkTextBlob>> blobs;
joshualitte49109f2015-07-17 12:47:39 -070085 for (int i = 0; i < count; i++) {
Mike Reed70914f52018-11-23 13:08:33 -050086 SkFont font;
87 font.setSize(48); // draw big glyphs to really stress the atlas
joshualitte49109f2015-07-17 12:47:39 -070088
89 SkString familyName;
90 fm->getFamilyName(i, &familyName);
Hal Canary342b7ac2016-11-04 11:49:42 -040091 sk_sp<SkFontStyleSet> set(fm->createStyleSet(i));
joshualitte49109f2015-07-17 12:47:39 -070092 for (int j = 0; j < set->count(); ++j) {
93 SkFontStyle fs;
halcanary96fcdcc2015-08-27 07:41:13 -070094 set->getStyle(j, &fs, nullptr);
joshualitte49109f2015-07-17 12:47:39 -070095
joshualitt65e96b42015-07-31 11:45:22 -070096 // We use a typeface which randomy returns unexpected mask formats to fuzz
bungeman13b9c952016-05-12 10:09:30 -070097 sk_sp<SkTypeface> orig(set->createTypeface(j));
joshualitt65e96b42015-07-31 11:45:22 -070098 if (normal) {
Mike Reed70914f52018-11-23 13:08:33 -050099 font.setTypeface(orig);
joshualitt65e96b42015-07-31 11:45:22 -0700100 } else {
Mike Reed70914f52018-11-23 13:08:33 -0500101 font.setTypeface(sk_make_sp<SkRandomTypeface>(orig, SkPaint(), true));
joshualitt65e96b42015-07-31 11:45:22 -0700102 }
joshualitte49109f2015-07-17 12:47:39 -0700103
104 SkTextBlobBuilder builder;
105 for (int aa = 0; aa < 2; aa++) {
106 for (int subpixel = 0; subpixel < 2; subpixel++) {
107 for (int lcd = 0; lcd < 2; lcd++) {
Mike Reed70914f52018-11-23 13:08:33 -0500108 font.setEdging(SkFont::Edging::kAlias);
109 if (aa) {
110 font.setEdging(SkFont::Edging::kAntiAlias);
111 if (lcd) {
112 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
113 }
joshualitt65e96b42015-07-31 11:45:22 -0700114 }
Mike Reed70914f52018-11-23 13:08:33 -0500115 font.setSubpixel(SkToBool(subpixel));
116 if (!SkToBool(lcd)) {
117 font.setSize(160);
118 }
119 const SkTextBlobBuilder::RunBuffer& run = builder.allocRun(font,
joshualitt65e96b42015-07-31 11:45:22 -0700120 maxTotalText,
joshualitte49109f2015-07-17 12:47:39 -0700121 0, 0,
halcanary96fcdcc2015-08-27 07:41:13 -0700122 nullptr);
joshualitt65e96b42015-07-31 11:45:22 -0700123 memcpy(run.glyphs, text.get(), maxTotalText * sizeof(uint16_t));
joshualitte49109f2015-07-17 12:47:39 -0700124 }
125 }
126 }
fmalita37283c22016-09-13 10:00:23 -0700127 blobs.emplace_back(builder.make());
joshualitte49109f2015-07-17 12:47:39 -0700128 }
129 }
130
joshualitt11dfc8e2015-07-23 08:30:25 -0700131 // create surface where LCD is impossible
132 info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
133 SkSurfaceProps propsNoLCD(0, kUnknown_SkPixelGeometry);
reede8f30622016-03-23 18:59:25 -0700134 auto surfaceNoLCD(canvas->makeSurface(info, &propsNoLCD));
joshualitt11dfc8e2015-07-23 08:30:25 -0700135 REPORTER_ASSERT(reporter, surface);
136 if (!surface) {
137 return;
138 }
139
140 SkCanvas* canvasNoLCD = surfaceNoLCD->getCanvas();
141
joshualitte49109f2015-07-17 12:47:39 -0700142 // test redraw
143 draw(canvas, 2, blobs);
joshualitt11dfc8e2015-07-23 08:30:25 -0700144 draw(canvasNoLCD, 2, blobs);
joshualitte49109f2015-07-17 12:47:39 -0700145
146 // test draw after free
kkinnunen15302832015-12-01 04:35:26 -0800147 context->freeGpuResources();
joshualitte49109f2015-07-17 12:47:39 -0700148 draw(canvas, 1, blobs);
149
kkinnunen15302832015-12-01 04:35:26 -0800150 context->freeGpuResources();
joshualitt11dfc8e2015-07-23 08:30:25 -0700151 draw(canvasNoLCD, 1, blobs);
152
joshualitte49109f2015-07-17 12:47:39 -0700153 // test draw after abandon
kkinnunen15302832015-12-01 04:35:26 -0800154 context->abandonContext();
joshualitte49109f2015-07-17 12:47:39 -0700155 draw(canvas, 1, blobs);
156}
joshualitt65e96b42015-07-31 11:45:22 -0700157
bsalomon758586c2016-04-06 14:02:39 -0700158DEF_GPUTEST_FOR_NULLGL_CONTEXT(TextBlobCache, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700159 text_blob_cache_inner(reporter, ctxInfo.grContext(), 1024, 256, 30, true, false);
joshualitt7f9c9eb2015-08-21 11:08:00 -0700160}
161
bsalomon758586c2016-04-06 14:02:39 -0700162DEF_GPUTEST_FOR_NULLGL_CONTEXT(TextBlobStressCache, reporter, ctxInfo) {
Herb Derby278b0672018-10-05 13:46:51 -0400163 text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, true, true);
joshualitt65e96b42015-07-31 11:45:22 -0700164}
165
bsalomon758586c2016-04-06 14:02:39 -0700166DEF_GPUTEST_FOR_NULLGL_CONTEXT(TextBlobAbnormal, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700167 text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, false, false);
joshualitt7f9c9eb2015-08-21 11:08:00 -0700168}
169
bsalomon758586c2016-04-06 14:02:39 -0700170DEF_GPUTEST_FOR_NULLGL_CONTEXT(TextBlobStressAbnormal, reporter, ctxInfo) {
Herb Derby278b0672018-10-05 13:46:51 -0400171 text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, false, true);
joshualitt65e96b42015-07-31 11:45:22 -0700172}