blob: cbc6b99f84d5a8df6f549c0f0ce500ec5d855f3f [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
30struct TextBlobWrapper {
joshualittbedf7e52015-07-23 08:09:35 -070031 // This class assumes it 'owns' the textblob it wraps, and thus does not need to take a ref
32 explicit TextBlobWrapper(const SkTextBlob* blob) : fBlob(blob) {}
joshualitte49109f2015-07-17 12:47:39 -070033 TextBlobWrapper(const TextBlobWrapper& blob) : fBlob(SkRef(blob.fBlob.get())) {}
34
35 SkAutoTUnref<const SkTextBlob> fBlob;
36};
37
38static void draw(SkCanvas* canvas, int redraw, const SkTArray<TextBlobWrapper>& blobs) {
joshualitt404d9d62015-07-22 11:00:32 -070039 int yOffset = 0;
joshualitte49109f2015-07-17 12:47:39 -070040 for (int r = 0; r < redraw; r++) {
41 for (int i = 0; i < blobs.count(); i++) {
joshualitt404d9d62015-07-22 11:00:32 -070042 const SkTextBlob* blob = blobs[i].fBlob.get();
43 const SkRect& bounds = blob->bounds();
44 yOffset += SkScalarCeilToInt(bounds.height());
joshualitte49109f2015-07-17 12:47:39 -070045 SkPaint paint;
joshualitt404d9d62015-07-22 11:00:32 -070046 canvas->drawTextBlob(blob, 0, SkIntToScalar(yOffset), paint);
joshualitte49109f2015-07-17 12:47:39 -070047 }
48 }
49}
50
joshualitt11dfc8e2015-07-23 08:30:25 -070051static const int kWidth = 1024;
52static const int kHeight = 768;
joshualitte49109f2015-07-17 12:47:39 -070053
54// This test hammers the GPU textblobcache and font atlas
kkinnunen15302832015-12-01 04:35:26 -080055static void text_blob_cache_inner(skiatest::Reporter* reporter, GrContext* context,
joshualitt7f9c9eb2015-08-21 11:08:00 -070056 int maxTotalText, int maxGlyphID, int maxFamilies, bool normal,
57 bool stressTest) {
joshualitte49109f2015-07-17 12:47:39 -070058 // setup surface
59 uint32_t flags = 0;
60 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
61
joshualitt7f9c9eb2015-08-21 11:08:00 -070062 // configure our context for maximum stressing of cache and atlas
63 if (stressTest) {
kkinnunen15302832015-12-01 04:35:26 -080064 GrTest::SetupAlwaysEvictAtlas(context);
65 context->setTextBlobCacheLimit_ForTesting(0);
joshualitt7f9c9eb2015-08-21 11:08:00 -070066 }
67
joshualitt11dfc8e2015-07-23 08:30:25 -070068 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, kN32_SkColorType, kPremul_SkAlphaType);
reede8f30622016-03-23 18:59:25 -070069 auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, &props));
joshualitte49109f2015-07-17 12:47:39 -070070 REPORTER_ASSERT(reporter, surface);
71 if (!surface) {
72 return;
73 }
74
75 SkCanvas* canvas = surface->getCanvas();
76
77 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
78
joshualitt65e96b42015-07-31 11:45:22 -070079 int count = SkMin32(fm->countFamilies(), maxFamilies);
joshualitte49109f2015-07-17 12:47:39 -070080
81 // make a ton of text
joshualitt65e96b42015-07-31 11:45:22 -070082 SkAutoTArray<uint16_t> text(maxTotalText);
83 for (int i = 0; i < maxTotalText; i++) {
84 text[i] = i % maxGlyphID;
joshualitte49109f2015-07-17 12:47:39 -070085 }
86
87 // generate textblobs
88 SkTArray<TextBlobWrapper> blobs;
89 for (int i = 0; i < count; i++) {
90 SkPaint paint;
91 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
joshualitt11dfc8e2015-07-23 08:30:25 -070092 paint.setTextSize(48); // draw big glyphs to really stress the atlas
joshualitte49109f2015-07-17 12:47:39 -070093
94 SkString familyName;
95 fm->getFamilyName(i, &familyName);
96 SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i));
97 for (int j = 0; j < set->count(); ++j) {
98 SkFontStyle fs;
halcanary96fcdcc2015-08-27 07:41:13 -070099 set->getStyle(j, &fs, nullptr);
joshualitte49109f2015-07-17 12:47:39 -0700100
joshualitt65e96b42015-07-31 11:45:22 -0700101 // We use a typeface which randomy returns unexpected mask formats to fuzz
bungeman13b9c952016-05-12 10:09:30 -0700102 sk_sp<SkTypeface> orig(set->createTypeface(j));
joshualitt65e96b42015-07-31 11:45:22 -0700103 if (normal) {
104 paint.setTypeface(orig);
105 } else {
bungeman13b9c952016-05-12 10:09:30 -0700106 paint.setTypeface(sk_make_sp<SkRandomTypeface>(orig, paint, true));
joshualitt65e96b42015-07-31 11:45:22 -0700107 }
joshualitte49109f2015-07-17 12:47:39 -0700108
109 SkTextBlobBuilder builder;
110 for (int aa = 0; aa < 2; aa++) {
111 for (int subpixel = 0; subpixel < 2; subpixel++) {
112 for (int lcd = 0; lcd < 2; lcd++) {
113 paint.setAntiAlias(SkToBool(aa));
114 paint.setSubpixelText(SkToBool(subpixel));
115 paint.setLCDRenderText(SkToBool(lcd));
joshualitt65e96b42015-07-31 11:45:22 -0700116 if (!SkToBool(lcd)) {
117 paint.setTextSize(160);
118 }
joshualitte49109f2015-07-17 12:47:39 -0700119 const SkTextBlobBuilder::RunBuffer& run = builder.allocRun(paint,
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 }
bungeman85dc3592016-02-09 11:32:56 -0800127 blobs.emplace_back(builder.build());
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) {
bsalomon8b7451a2016-05-11 06:33:06 -0700163 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) {
bsalomon8b7451a2016-05-11 06:33:06 -0700171 text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, false, true);
joshualitt65e96b42015-07-31 11:45:22 -0700172}
joshualitte49109f2015-07-17 12:47:39 -0700173#endif