blob: 6aa6c20ed6e8e5796ee595216af1335f333ae5eb [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);
kkinnunen15302832015-12-01 04:35:26 -080069 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(context, SkSurface::kNo_Budgeted, info,
joshualitte49109f2015-07-17 12:47:39 -070070 0, &props));
71 REPORTER_ASSERT(reporter, surface);
72 if (!surface) {
73 return;
74 }
75
76 SkCanvas* canvas = surface->getCanvas();
77
78 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
79
joshualitt65e96b42015-07-31 11:45:22 -070080 int count = SkMin32(fm->countFamilies(), maxFamilies);
joshualitte49109f2015-07-17 12:47:39 -070081
82 // make a ton of text
joshualitt65e96b42015-07-31 11:45:22 -070083 SkAutoTArray<uint16_t> text(maxTotalText);
84 for (int i = 0; i < maxTotalText; i++) {
85 text[i] = i % maxGlyphID;
joshualitte49109f2015-07-17 12:47:39 -070086 }
87
88 // generate textblobs
89 SkTArray<TextBlobWrapper> blobs;
90 for (int i = 0; i < count; i++) {
91 SkPaint paint;
92 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
joshualitt11dfc8e2015-07-23 08:30:25 -070093 paint.setTextSize(48); // draw big glyphs to really stress the atlas
joshualitte49109f2015-07-17 12:47:39 -070094
95 SkString familyName;
96 fm->getFamilyName(i, &familyName);
97 SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i));
98 for (int j = 0; j < set->count(); ++j) {
99 SkFontStyle fs;
halcanary96fcdcc2015-08-27 07:41:13 -0700100 set->getStyle(j, &fs, nullptr);
joshualitte49109f2015-07-17 12:47:39 -0700101
joshualitt65e96b42015-07-31 11:45:22 -0700102 // We use a typeface which randomy returns unexpected mask formats to fuzz
103 SkAutoTUnref<SkTypeface> orig(set->createTypeface(j));
104 if (normal) {
105 paint.setTypeface(orig);
106 } else {
halcanary385fe4d2015-08-26 13:07:48 -0700107 SkAutoTUnref<SkTypeface> typeface(new SkRandomTypeface(orig, paint, true));
joshualitt65e96b42015-07-31 11:45:22 -0700108 paint.setTypeface(typeface);
109 }
joshualitte49109f2015-07-17 12:47:39 -0700110
111 SkTextBlobBuilder builder;
112 for (int aa = 0; aa < 2; aa++) {
113 for (int subpixel = 0; subpixel < 2; subpixel++) {
114 for (int lcd = 0; lcd < 2; lcd++) {
115 paint.setAntiAlias(SkToBool(aa));
116 paint.setSubpixelText(SkToBool(subpixel));
117 paint.setLCDRenderText(SkToBool(lcd));
joshualitt65e96b42015-07-31 11:45:22 -0700118 if (!SkToBool(lcd)) {
119 paint.setTextSize(160);
120 }
joshualitte49109f2015-07-17 12:47:39 -0700121 const SkTextBlobBuilder::RunBuffer& run = builder.allocRun(paint,
joshualitt65e96b42015-07-31 11:45:22 -0700122 maxTotalText,
joshualitte49109f2015-07-17 12:47:39 -0700123 0, 0,
halcanary96fcdcc2015-08-27 07:41:13 -0700124 nullptr);
joshualitt65e96b42015-07-31 11:45:22 -0700125 memcpy(run.glyphs, text.get(), maxTotalText * sizeof(uint16_t));
joshualitte49109f2015-07-17 12:47:39 -0700126 }
127 }
128 }
129 SkNEW_APPEND_TO_TARRAY(&blobs, TextBlobWrapper, (builder.build()));
130 }
131 }
132
joshualitt11dfc8e2015-07-23 08:30:25 -0700133 // create surface where LCD is impossible
134 info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
135 SkSurfaceProps propsNoLCD(0, kUnknown_SkPixelGeometry);
136 SkAutoTUnref<SkSurface> surfaceNoLCD(canvas->newSurface(info, &propsNoLCD));
137 REPORTER_ASSERT(reporter, surface);
138 if (!surface) {
139 return;
140 }
141
142 SkCanvas* canvasNoLCD = surfaceNoLCD->getCanvas();
143
joshualitte49109f2015-07-17 12:47:39 -0700144 // test redraw
145 draw(canvas, 2, blobs);
joshualitt11dfc8e2015-07-23 08:30:25 -0700146 draw(canvasNoLCD, 2, blobs);
joshualitte49109f2015-07-17 12:47:39 -0700147
148 // test draw after free
kkinnunen15302832015-12-01 04:35:26 -0800149 context->freeGpuResources();
joshualitte49109f2015-07-17 12:47:39 -0700150 draw(canvas, 1, blobs);
151
kkinnunen15302832015-12-01 04:35:26 -0800152 context->freeGpuResources();
joshualitt11dfc8e2015-07-23 08:30:25 -0700153 draw(canvasNoLCD, 1, blobs);
154
joshualitte49109f2015-07-17 12:47:39 -0700155 // test draw after abandon
kkinnunen15302832015-12-01 04:35:26 -0800156 context->abandonContext();
joshualitte49109f2015-07-17 12:47:39 -0700157 draw(canvas, 1, blobs);
158}
joshualitt65e96b42015-07-31 11:45:22 -0700159
kkinnunen15302832015-12-01 04:35:26 -0800160DEF_GPUTEST_FOR_NULL_CONTEXT(TextBlobCache, reporter, context) {
161 text_blob_cache_inner(reporter, context, 1024, 256, 30, true, false);
joshualitt7f9c9eb2015-08-21 11:08:00 -0700162}
163
kkinnunen15302832015-12-01 04:35:26 -0800164DEF_GPUTEST_FOR_NULL_CONTEXT(TextBlobStressCache, reporter, context) {
165 text_blob_cache_inner(reporter, context, 256, 256, 10, true, true);
joshualitt65e96b42015-07-31 11:45:22 -0700166}
167
kkinnunen15302832015-12-01 04:35:26 -0800168DEF_GPUTEST_FOR_NULL_CONTEXT(TextBlobAbnormal, reporter, context) {
169 text_blob_cache_inner(reporter, context, 256, 256, 10, false, false);
joshualitt7f9c9eb2015-08-21 11:08:00 -0700170}
171
kkinnunen15302832015-12-01 04:35:26 -0800172DEF_GPUTEST_FOR_NULL_CONTEXT(TextBlobStressAbnormal, reporter, context) {
173 text_blob_cache_inner(reporter, context, 256, 256, 10, false, true);
joshualitt65e96b42015-07-31 11:45:22 -0700174}
joshualitte49109f2015-07-17 12:47:39 -0700175#endif