blob: 4d606c9550512045898e1c2f76de2bcb7231cf51 [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"
12#include "SkGraphics.h"
Mike Klein10d66cc2017-11-10 11:33:43 -050013#include "SkPaint.h"
14#include "SkPoint.h"
15#include "SkRandomScalerContext.h"
joshualitte49109f2015-07-17 12:47:39 -070016#include "SkSurface.h"
Mike Klein10d66cc2017-11-10 11:33:43 -050017#include "SkTextBlob.h"
joshualitte49109f2015-07-17 12:47:39 -070018#include "SkTypeface.h"
19
20#ifdef SK_BUILD_FOR_WIN
21 #include "SkTypeface_win.h"
22#endif
23
24#include "Test.h"
25
26#if SK_SUPPORT_GPU
kkinnunen15302832015-12-01 04:35:26 -080027#include "GrContext.h"
Robert Phillips0c4b7b12018-03-06 08:20:37 -050028#include "GrContextPriv.h"
joshualitt7f9c9eb2015-08-21 11:08:00 -070029#include "GrTest.h"
joshualitte49109f2015-07-17 12:47:39 -070030
fmalita37283c22016-09-13 10:00:23 -070031static void draw(SkCanvas* canvas, int redraw, const SkTArray<sk_sp<SkTextBlob>>& blobs) {
joshualitt404d9d62015-07-22 11:00:32 -070032 int yOffset = 0;
joshualitte49109f2015-07-17 12:47:39 -070033 for (int r = 0; r < redraw; r++) {
34 for (int i = 0; i < blobs.count(); i++) {
fmalita37283c22016-09-13 10:00:23 -070035 const auto& blob = blobs[i];
joshualitt404d9d62015-07-22 11:00:32 -070036 const SkRect& bounds = blob->bounds();
37 yOffset += SkScalarCeilToInt(bounds.height());
joshualitte49109f2015-07-17 12:47:39 -070038 SkPaint paint;
joshualitt404d9d62015-07-22 11:00:32 -070039 canvas->drawTextBlob(blob, 0, SkIntToScalar(yOffset), paint);
joshualitte49109f2015-07-17 12:47:39 -070040 }
41 }
42}
43
joshualitt11dfc8e2015-07-23 08:30:25 -070044static const int kWidth = 1024;
45static const int kHeight = 768;
joshualitte49109f2015-07-17 12:47:39 -070046
47// This test hammers the GPU textblobcache and font atlas
kkinnunen15302832015-12-01 04:35:26 -080048static void text_blob_cache_inner(skiatest::Reporter* reporter, GrContext* context,
joshualitt7f9c9eb2015-08-21 11:08:00 -070049 int maxTotalText, int maxGlyphID, int maxFamilies, bool normal,
50 bool stressTest) {
joshualitte49109f2015-07-17 12:47:39 -070051 // setup surface
52 uint32_t flags = 0;
53 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
54
joshualitt7f9c9eb2015-08-21 11:08:00 -070055 // configure our context for maximum stressing of cache and atlas
56 if (stressTest) {
kkinnunen15302832015-12-01 04:35:26 -080057 GrTest::SetupAlwaysEvictAtlas(context);
Robert Phillips0c4b7b12018-03-06 08:20:37 -050058 context->contextPriv().setTextBlobCacheLimit_ForTesting(0);
joshualitt7f9c9eb2015-08-21 11:08:00 -070059 }
60
joshualitt11dfc8e2015-07-23 08:30:25 -070061 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, kN32_SkColorType, kPremul_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070062 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, &props));
joshualitte49109f2015-07-17 12:47:39 -070063 REPORTER_ASSERT(reporter, surface);
64 if (!surface) {
65 return;
66 }
67
68 SkCanvas* canvas = surface->getCanvas();
69
Hal Canary342b7ac2016-11-04 11:49:42 -040070 sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
joshualitte49109f2015-07-17 12:47:39 -070071
joshualitt65e96b42015-07-31 11:45:22 -070072 int count = SkMin32(fm->countFamilies(), maxFamilies);
joshualitte49109f2015-07-17 12:47:39 -070073
74 // make a ton of text
joshualitt65e96b42015-07-31 11:45:22 -070075 SkAutoTArray<uint16_t> text(maxTotalText);
76 for (int i = 0; i < maxTotalText; i++) {
77 text[i] = i % maxGlyphID;
joshualitte49109f2015-07-17 12:47:39 -070078 }
79
80 // generate textblobs
fmalita37283c22016-09-13 10:00:23 -070081 SkTArray<sk_sp<SkTextBlob>> blobs;
joshualitte49109f2015-07-17 12:47:39 -070082 for (int i = 0; i < count; i++) {
83 SkPaint paint;
84 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
joshualitt11dfc8e2015-07-23 08:30:25 -070085 paint.setTextSize(48); // draw big glyphs to really stress the atlas
joshualitte49109f2015-07-17 12:47:39 -070086
87 SkString familyName;
88 fm->getFamilyName(i, &familyName);
Hal Canary342b7ac2016-11-04 11:49:42 -040089 sk_sp<SkFontStyleSet> set(fm->createStyleSet(i));
joshualitte49109f2015-07-17 12:47:39 -070090 for (int j = 0; j < set->count(); ++j) {
91 SkFontStyle fs;
halcanary96fcdcc2015-08-27 07:41:13 -070092 set->getStyle(j, &fs, nullptr);
joshualitte49109f2015-07-17 12:47:39 -070093
joshualitt65e96b42015-07-31 11:45:22 -070094 // We use a typeface which randomy returns unexpected mask formats to fuzz
bungeman13b9c952016-05-12 10:09:30 -070095 sk_sp<SkTypeface> orig(set->createTypeface(j));
joshualitt65e96b42015-07-31 11:45:22 -070096 if (normal) {
97 paint.setTypeface(orig);
98 } else {
bungeman13b9c952016-05-12 10:09:30 -070099 paint.setTypeface(sk_make_sp<SkRandomTypeface>(orig, paint, true));
joshualitt65e96b42015-07-31 11:45:22 -0700100 }
joshualitte49109f2015-07-17 12:47:39 -0700101
102 SkTextBlobBuilder builder;
103 for (int aa = 0; aa < 2; aa++) {
104 for (int subpixel = 0; subpixel < 2; subpixel++) {
105 for (int lcd = 0; lcd < 2; lcd++) {
106 paint.setAntiAlias(SkToBool(aa));
107 paint.setSubpixelText(SkToBool(subpixel));
108 paint.setLCDRenderText(SkToBool(lcd));
joshualitt65e96b42015-07-31 11:45:22 -0700109 if (!SkToBool(lcd)) {
110 paint.setTextSize(160);
111 }
joshualitte49109f2015-07-17 12:47:39 -0700112 const SkTextBlobBuilder::RunBuffer& run = builder.allocRun(paint,
joshualitt65e96b42015-07-31 11:45:22 -0700113 maxTotalText,
joshualitte49109f2015-07-17 12:47:39 -0700114 0, 0,
halcanary96fcdcc2015-08-27 07:41:13 -0700115 nullptr);
joshualitt65e96b42015-07-31 11:45:22 -0700116 memcpy(run.glyphs, text.get(), maxTotalText * sizeof(uint16_t));
joshualitte49109f2015-07-17 12:47:39 -0700117 }
118 }
119 }
fmalita37283c22016-09-13 10:00:23 -0700120 blobs.emplace_back(builder.make());
joshualitte49109f2015-07-17 12:47:39 -0700121 }
122 }
123
joshualitt11dfc8e2015-07-23 08:30:25 -0700124 // create surface where LCD is impossible
125 info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
126 SkSurfaceProps propsNoLCD(0, kUnknown_SkPixelGeometry);
reede8f30622016-03-23 18:59:25 -0700127 auto surfaceNoLCD(canvas->makeSurface(info, &propsNoLCD));
joshualitt11dfc8e2015-07-23 08:30:25 -0700128 REPORTER_ASSERT(reporter, surface);
129 if (!surface) {
130 return;
131 }
132
133 SkCanvas* canvasNoLCD = surfaceNoLCD->getCanvas();
134
joshualitte49109f2015-07-17 12:47:39 -0700135 // test redraw
136 draw(canvas, 2, blobs);
joshualitt11dfc8e2015-07-23 08:30:25 -0700137 draw(canvasNoLCD, 2, blobs);
joshualitte49109f2015-07-17 12:47:39 -0700138
139 // test draw after free
kkinnunen15302832015-12-01 04:35:26 -0800140 context->freeGpuResources();
joshualitte49109f2015-07-17 12:47:39 -0700141 draw(canvas, 1, blobs);
142
kkinnunen15302832015-12-01 04:35:26 -0800143 context->freeGpuResources();
joshualitt11dfc8e2015-07-23 08:30:25 -0700144 draw(canvasNoLCD, 1, blobs);
145
joshualitte49109f2015-07-17 12:47:39 -0700146 // test draw after abandon
kkinnunen15302832015-12-01 04:35:26 -0800147 context->abandonContext();
joshualitte49109f2015-07-17 12:47:39 -0700148 draw(canvas, 1, blobs);
149}
joshualitt65e96b42015-07-31 11:45:22 -0700150
bsalomon758586c2016-04-06 14:02:39 -0700151DEF_GPUTEST_FOR_NULLGL_CONTEXT(TextBlobCache, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700152 text_blob_cache_inner(reporter, ctxInfo.grContext(), 1024, 256, 30, true, false);
joshualitt7f9c9eb2015-08-21 11:08:00 -0700153}
154
bsalomon758586c2016-04-06 14:02:39 -0700155DEF_GPUTEST_FOR_NULLGL_CONTEXT(TextBlobStressCache, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700156 text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, true, true);
joshualitt65e96b42015-07-31 11:45:22 -0700157}
158
bsalomon758586c2016-04-06 14:02:39 -0700159DEF_GPUTEST_FOR_NULLGL_CONTEXT(TextBlobAbnormal, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700160 text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, false, false);
joshualitt7f9c9eb2015-08-21 11:08:00 -0700161}
162
bsalomon758586c2016-04-06 14:02:39 -0700163DEF_GPUTEST_FOR_NULLGL_CONTEXT(TextBlobStressAbnormal, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700164 text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, false, true);
joshualitt65e96b42015-07-31 11:45:22 -0700165}
joshualitte49109f2015-07-17 12:47:39 -0700166#endif