blob: 995328d26de016a028e5c6a5938c5996883f8400 [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"
11#include "SkPaint.h"
12#include "SkPoint.h"
13#include "SkTextBlob.h"
14#include "SkFontMgr.h"
15#include "SkGraphics.h"
16#include "SkSurface.h"
17#include "SkTypeface.h"
joshualitt65e96b42015-07-31 11:45:22 -070018#include "../src/fonts/SkRandomScalerContext.h"
joshualitte49109f2015-07-17 12:47:39 -070019
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"
joshualitt7f9c9eb2015-08-21 11:08:00 -070028#include "GrTest.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
46// This test hammers the GPU textblobcache and font atlas
kkinnunen15302832015-12-01 04:35:26 -080047static void text_blob_cache_inner(skiatest::Reporter* reporter, GrContext* context,
joshualitt7f9c9eb2015-08-21 11:08:00 -070048 int maxTotalText, int maxGlyphID, int maxFamilies, bool normal,
49 bool stressTest) {
joshualitte49109f2015-07-17 12:47:39 -070050 // setup surface
51 uint32_t flags = 0;
52 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
53
joshualitt7f9c9eb2015-08-21 11:08:00 -070054 // configure our context for maximum stressing of cache and atlas
55 if (stressTest) {
kkinnunen15302832015-12-01 04:35:26 -080056 GrTest::SetupAlwaysEvictAtlas(context);
57 context->setTextBlobCacheLimit_ForTesting(0);
joshualitt7f9c9eb2015-08-21 11:08:00 -070058 }
59
joshualitt11dfc8e2015-07-23 08:30:25 -070060 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, kN32_SkColorType, kPremul_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070061 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, &props));
joshualitte49109f2015-07-17 12:47:39 -070062 REPORTER_ASSERT(reporter, surface);
63 if (!surface) {
64 return;
65 }
66
67 SkCanvas* canvas = surface->getCanvas();
68
Hal Canary342b7ac2016-11-04 11:49:42 -040069 sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
joshualitte49109f2015-07-17 12:47:39 -070070
joshualitt65e96b42015-07-31 11:45:22 -070071 int count = SkMin32(fm->countFamilies(), maxFamilies);
joshualitte49109f2015-07-17 12:47:39 -070072
73 // make a ton of text
joshualitt65e96b42015-07-31 11:45:22 -070074 SkAutoTArray<uint16_t> text(maxTotalText);
75 for (int i = 0; i < maxTotalText; i++) {
76 text[i] = i % maxGlyphID;
joshualitte49109f2015-07-17 12:47:39 -070077 }
78
79 // generate textblobs
fmalita37283c22016-09-13 10:00:23 -070080 SkTArray<sk_sp<SkTextBlob>> blobs;
joshualitte49109f2015-07-17 12:47:39 -070081 for (int i = 0; i < count; i++) {
82 SkPaint paint;
83 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
joshualitt11dfc8e2015-07-23 08:30:25 -070084 paint.setTextSize(48); // draw big glyphs to really stress the atlas
joshualitte49109f2015-07-17 12:47:39 -070085
86 SkString familyName;
87 fm->getFamilyName(i, &familyName);
Hal Canary342b7ac2016-11-04 11:49:42 -040088 sk_sp<SkFontStyleSet> set(fm->createStyleSet(i));
joshualitte49109f2015-07-17 12:47:39 -070089 for (int j = 0; j < set->count(); ++j) {
90 SkFontStyle fs;
halcanary96fcdcc2015-08-27 07:41:13 -070091 set->getStyle(j, &fs, nullptr);
joshualitte49109f2015-07-17 12:47:39 -070092
joshualitt65e96b42015-07-31 11:45:22 -070093 // We use a typeface which randomy returns unexpected mask formats to fuzz
bungeman13b9c952016-05-12 10:09:30 -070094 sk_sp<SkTypeface> orig(set->createTypeface(j));
joshualitt65e96b42015-07-31 11:45:22 -070095 if (normal) {
96 paint.setTypeface(orig);
97 } else {
bungeman13b9c952016-05-12 10:09:30 -070098 paint.setTypeface(sk_make_sp<SkRandomTypeface>(orig, paint, true));
joshualitt65e96b42015-07-31 11:45:22 -070099 }
joshualitte49109f2015-07-17 12:47:39 -0700100
101 SkTextBlobBuilder builder;
102 for (int aa = 0; aa < 2; aa++) {
103 for (int subpixel = 0; subpixel < 2; subpixel++) {
104 for (int lcd = 0; lcd < 2; lcd++) {
105 paint.setAntiAlias(SkToBool(aa));
106 paint.setSubpixelText(SkToBool(subpixel));
107 paint.setLCDRenderText(SkToBool(lcd));
joshualitt65e96b42015-07-31 11:45:22 -0700108 if (!SkToBool(lcd)) {
109 paint.setTextSize(160);
110 }
joshualitte49109f2015-07-17 12:47:39 -0700111 const SkTextBlobBuilder::RunBuffer& run = builder.allocRun(paint,
joshualitt65e96b42015-07-31 11:45:22 -0700112 maxTotalText,
joshualitte49109f2015-07-17 12:47:39 -0700113 0, 0,
halcanary96fcdcc2015-08-27 07:41:13 -0700114 nullptr);
joshualitt65e96b42015-07-31 11:45:22 -0700115 memcpy(run.glyphs, text.get(), maxTotalText * sizeof(uint16_t));
joshualitte49109f2015-07-17 12:47:39 -0700116 }
117 }
118 }
fmalita37283c22016-09-13 10:00:23 -0700119 blobs.emplace_back(builder.make());
joshualitte49109f2015-07-17 12:47:39 -0700120 }
121 }
122
joshualitt11dfc8e2015-07-23 08:30:25 -0700123 // create surface where LCD is impossible
124 info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
125 SkSurfaceProps propsNoLCD(0, kUnknown_SkPixelGeometry);
reede8f30622016-03-23 18:59:25 -0700126 auto surfaceNoLCD(canvas->makeSurface(info, &propsNoLCD));
joshualitt11dfc8e2015-07-23 08:30:25 -0700127 REPORTER_ASSERT(reporter, surface);
128 if (!surface) {
129 return;
130 }
131
132 SkCanvas* canvasNoLCD = surfaceNoLCD->getCanvas();
133
joshualitte49109f2015-07-17 12:47:39 -0700134 // test redraw
135 draw(canvas, 2, blobs);
joshualitt11dfc8e2015-07-23 08:30:25 -0700136 draw(canvasNoLCD, 2, blobs);
joshualitte49109f2015-07-17 12:47:39 -0700137
138 // test draw after free
kkinnunen15302832015-12-01 04:35:26 -0800139 context->freeGpuResources();
joshualitte49109f2015-07-17 12:47:39 -0700140 draw(canvas, 1, blobs);
141
kkinnunen15302832015-12-01 04:35:26 -0800142 context->freeGpuResources();
joshualitt11dfc8e2015-07-23 08:30:25 -0700143 draw(canvasNoLCD, 1, blobs);
144
joshualitte49109f2015-07-17 12:47:39 -0700145 // test draw after abandon
kkinnunen15302832015-12-01 04:35:26 -0800146 context->abandonContext();
joshualitte49109f2015-07-17 12:47:39 -0700147 draw(canvas, 1, blobs);
148}
joshualitt65e96b42015-07-31 11:45:22 -0700149
bsalomon758586c2016-04-06 14:02:39 -0700150DEF_GPUTEST_FOR_NULLGL_CONTEXT(TextBlobCache, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700151 text_blob_cache_inner(reporter, ctxInfo.grContext(), 1024, 256, 30, true, false);
joshualitt7f9c9eb2015-08-21 11:08:00 -0700152}
153
bsalomon758586c2016-04-06 14:02:39 -0700154DEF_GPUTEST_FOR_NULLGL_CONTEXT(TextBlobStressCache, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700155 text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, true, true);
joshualitt65e96b42015-07-31 11:45:22 -0700156}
157
bsalomon758586c2016-04-06 14:02:39 -0700158DEF_GPUTEST_FOR_NULLGL_CONTEXT(TextBlobAbnormal, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700159 text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, false, false);
joshualitt7f9c9eb2015-08-21 11:08:00 -0700160}
161
bsalomon758586c2016-04-06 14:02:39 -0700162DEF_GPUTEST_FOR_NULLGL_CONTEXT(TextBlobStressAbnormal, reporter, ctxInfo) {
bsalomon8b7451a2016-05-11 06:33:06 -0700163 text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, false, true);
joshualitt65e96b42015-07-31 11:45:22 -0700164}
joshualitte49109f2015-07-17 12:47:39 -0700165#endif