blob: 6f67382a0f6b4f3c22089348496fbd3801f61a3d [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
27#include "GrContextFactory.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
joshualitt65e96b42015-07-31 11:45:22 -070055static void text_blob_cache_inner(skiatest::Reporter* reporter, GrContextFactory* factory,
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
joshualitt65e96b42015-07-31 11:45:22 -070062 // We don't typically actually draw with this unittest
63 GrContext* ctx = factory->get(GrContextFactory::kNull_GLContextType);
joshualitt7f9c9eb2015-08-21 11:08:00 -070064
65 // configure our context for maximum stressing of cache and atlas
66 if (stressTest) {
67 GrTest::SetupAlwaysEvictAtlas(ctx);
68 ctx->setTextBlobCacheLimit_ForTesting(0);
69 }
70
joshualitt11dfc8e2015-07-23 08:30:25 -070071 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, kN32_SkColorType, kPremul_SkAlphaType);
joshualitte49109f2015-07-17 12:47:39 -070072 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, info,
73 0, &props));
74 REPORTER_ASSERT(reporter, surface);
75 if (!surface) {
76 return;
77 }
78
79 SkCanvas* canvas = surface->getCanvas();
80
81 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
82
joshualitt65e96b42015-07-31 11:45:22 -070083 int count = SkMin32(fm->countFamilies(), maxFamilies);
joshualitte49109f2015-07-17 12:47:39 -070084
85 // make a ton of text
joshualitt65e96b42015-07-31 11:45:22 -070086 SkAutoTArray<uint16_t> text(maxTotalText);
87 for (int i = 0; i < maxTotalText; i++) {
88 text[i] = i % maxGlyphID;
joshualitte49109f2015-07-17 12:47:39 -070089 }
90
91 // generate textblobs
92 SkTArray<TextBlobWrapper> blobs;
93 for (int i = 0; i < count; i++) {
94 SkPaint paint;
95 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
joshualitt11dfc8e2015-07-23 08:30:25 -070096 paint.setTextSize(48); // draw big glyphs to really stress the atlas
joshualitte49109f2015-07-17 12:47:39 -070097
98 SkString familyName;
99 fm->getFamilyName(i, &familyName);
100 SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i));
101 for (int j = 0; j < set->count(); ++j) {
102 SkFontStyle fs;
103 set->getStyle(j, &fs, NULL);
104
joshualitt65e96b42015-07-31 11:45:22 -0700105 // We use a typeface which randomy returns unexpected mask formats to fuzz
106 SkAutoTUnref<SkTypeface> orig(set->createTypeface(j));
107 if (normal) {
108 paint.setTypeface(orig);
109 } else {
110 SkAutoTUnref<SkTypeface> typeface(SkNEW_ARGS(SkRandomTypeface, (orig, paint, true)));
111 paint.setTypeface(typeface);
112 }
joshualitte49109f2015-07-17 12:47:39 -0700113
114 SkTextBlobBuilder builder;
115 for (int aa = 0; aa < 2; aa++) {
116 for (int subpixel = 0; subpixel < 2; subpixel++) {
117 for (int lcd = 0; lcd < 2; lcd++) {
118 paint.setAntiAlias(SkToBool(aa));
119 paint.setSubpixelText(SkToBool(subpixel));
120 paint.setLCDRenderText(SkToBool(lcd));
joshualitt65e96b42015-07-31 11:45:22 -0700121 if (!SkToBool(lcd)) {
122 paint.setTextSize(160);
123 }
joshualitte49109f2015-07-17 12:47:39 -0700124 const SkTextBlobBuilder::RunBuffer& run = builder.allocRun(paint,
joshualitt65e96b42015-07-31 11:45:22 -0700125 maxTotalText,
joshualitte49109f2015-07-17 12:47:39 -0700126 0, 0,
127 NULL);
joshualitt65e96b42015-07-31 11:45:22 -0700128 memcpy(run.glyphs, text.get(), maxTotalText * sizeof(uint16_t));
joshualitte49109f2015-07-17 12:47:39 -0700129 }
130 }
131 }
132 SkNEW_APPEND_TO_TARRAY(&blobs, TextBlobWrapper, (builder.build()));
133 }
134 }
135
joshualitt11dfc8e2015-07-23 08:30:25 -0700136 // create surface where LCD is impossible
137 info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
138 SkSurfaceProps propsNoLCD(0, kUnknown_SkPixelGeometry);
139 SkAutoTUnref<SkSurface> surfaceNoLCD(canvas->newSurface(info, &propsNoLCD));
140 REPORTER_ASSERT(reporter, surface);
141 if (!surface) {
142 return;
143 }
144
145 SkCanvas* canvasNoLCD = surfaceNoLCD->getCanvas();
146
joshualitte49109f2015-07-17 12:47:39 -0700147 // test redraw
148 draw(canvas, 2, blobs);
joshualitt11dfc8e2015-07-23 08:30:25 -0700149 draw(canvasNoLCD, 2, blobs);
joshualitte49109f2015-07-17 12:47:39 -0700150
151 // test draw after free
152 ctx->freeGpuResources();
153 draw(canvas, 1, blobs);
154
joshualitt11dfc8e2015-07-23 08:30:25 -0700155 ctx->freeGpuResources();
156 draw(canvasNoLCD, 1, blobs);
157
joshualitte49109f2015-07-17 12:47:39 -0700158 // test draw after abandon
159 ctx->abandonContext();
160 draw(canvas, 1, blobs);
161}
joshualitt65e96b42015-07-31 11:45:22 -0700162
163DEF_GPUTEST(TextBlobCache, reporter, factory) {
joshualitt7f9c9eb2015-08-21 11:08:00 -0700164 text_blob_cache_inner(reporter, factory, 1024, 256, 30, true, false);
165}
166
167DEF_GPUTEST(TextBlobStressCache, reporter, factory) {
168 text_blob_cache_inner(reporter, factory, 256, 256, 10, true, true);
joshualitt65e96b42015-07-31 11:45:22 -0700169}
170
171DEF_GPUTEST(TextBlobAbnormal, reporter, factory) {
joshualitt7f9c9eb2015-08-21 11:08:00 -0700172 text_blob_cache_inner(reporter, factory, 256, 256, 10, false, false);
173}
174
175DEF_GPUTEST(TextBlobStressAbnormal, reporter, factory) {
176 text_blob_cache_inner(reporter, factory, 256, 256, 10, false, true);
joshualitt65e96b42015-07-31 11:45:22 -0700177}
joshualitte49109f2015-07-17 12:47:39 -0700178#endif