blob: 60a6a8a24b777d65df078dd874526036bb8a3415 [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"
9#include "SkCanvas.h"
10#include "SkPaint.h"
11#include "SkPoint.h"
12#include "SkTextBlob.h"
13#include "SkFontMgr.h"
14#include "SkGraphics.h"
15#include "SkSurface.h"
16#include "SkTypeface.h"
joshualitt65e96b42015-07-31 11:45:22 -070017#include "../src/fonts/SkRandomScalerContext.h"
joshualitte49109f2015-07-17 12:47:39 -070018
19#ifdef SK_BUILD_FOR_WIN
20 #include "SkTypeface_win.h"
21#endif
22
23#include "Test.h"
24
25#if SK_SUPPORT_GPU
26#include "GrContextFactory.h"
27
28struct TextBlobWrapper {
joshualittbedf7e52015-07-23 08:09:35 -070029 // This class assumes it 'owns' the textblob it wraps, and thus does not need to take a ref
30 explicit TextBlobWrapper(const SkTextBlob* blob) : fBlob(blob) {}
joshualitte49109f2015-07-17 12:47:39 -070031 TextBlobWrapper(const TextBlobWrapper& blob) : fBlob(SkRef(blob.fBlob.get())) {}
32
33 SkAutoTUnref<const SkTextBlob> fBlob;
34};
35
36static void draw(SkCanvas* canvas, int redraw, const SkTArray<TextBlobWrapper>& blobs) {
joshualitt404d9d62015-07-22 11:00:32 -070037 int yOffset = 0;
joshualitte49109f2015-07-17 12:47:39 -070038 for (int r = 0; r < redraw; r++) {
39 for (int i = 0; i < blobs.count(); i++) {
joshualitt404d9d62015-07-22 11:00:32 -070040 const SkTextBlob* blob = blobs[i].fBlob.get();
41 const SkRect& bounds = blob->bounds();
42 yOffset += SkScalarCeilToInt(bounds.height());
joshualitte49109f2015-07-17 12:47:39 -070043 SkPaint paint;
joshualitt404d9d62015-07-22 11:00:32 -070044 canvas->drawTextBlob(blob, 0, SkIntToScalar(yOffset), paint);
joshualitte49109f2015-07-17 12:47:39 -070045 }
46 }
47}
48
joshualitt11dfc8e2015-07-23 08:30:25 -070049static const int kWidth = 1024;
50static const int kHeight = 768;
joshualitte49109f2015-07-17 12:47:39 -070051
52// This test hammers the GPU textblobcache and font atlas
joshualitt65e96b42015-07-31 11:45:22 -070053static void text_blob_cache_inner(skiatest::Reporter* reporter, GrContextFactory* factory,
54 int maxTotalText, int maxGlyphID, int maxFamilies, bool normal) {
joshualitte49109f2015-07-17 12:47:39 -070055 // setup surface
56 uint32_t flags = 0;
57 SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType);
58
joshualitt65e96b42015-07-31 11:45:22 -070059 // We don't typically actually draw with this unittest
60 GrContext* ctx = factory->get(GrContextFactory::kNull_GLContextType);
joshualitt11dfc8e2015-07-23 08:30:25 -070061 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, kN32_SkColorType, kPremul_SkAlphaType);
joshualitte49109f2015-07-17 12:47:39 -070062 SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, info,
63 0, &props));
64 REPORTER_ASSERT(reporter, surface);
65 if (!surface) {
66 return;
67 }
68
69 SkCanvas* canvas = surface->getCanvas();
70
71 SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault());
72
joshualitt65e96b42015-07-31 11:45:22 -070073 int count = SkMin32(fm->countFamilies(), maxFamilies);
joshualitte49109f2015-07-17 12:47:39 -070074
75 // make a ton of text
joshualitt65e96b42015-07-31 11:45:22 -070076 SkAutoTArray<uint16_t> text(maxTotalText);
77 for (int i = 0; i < maxTotalText; i++) {
78 text[i] = i % maxGlyphID;
joshualitte49109f2015-07-17 12:47:39 -070079 }
80
81 // generate textblobs
82 SkTArray<TextBlobWrapper> blobs;
83 for (int i = 0; i < count; i++) {
84 SkPaint paint;
85 paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding);
joshualitt11dfc8e2015-07-23 08:30:25 -070086 paint.setTextSize(48); // draw big glyphs to really stress the atlas
joshualitte49109f2015-07-17 12:47:39 -070087
88 SkString familyName;
89 fm->getFamilyName(i, &familyName);
90 SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i));
91 for (int j = 0; j < set->count(); ++j) {
92 SkFontStyle fs;
93 set->getStyle(j, &fs, NULL);
94
joshualitt65e96b42015-07-31 11:45:22 -070095 // We use a typeface which randomy returns unexpected mask formats to fuzz
96 SkAutoTUnref<SkTypeface> orig(set->createTypeface(j));
97 if (normal) {
98 paint.setTypeface(orig);
99 } else {
100 SkAutoTUnref<SkTypeface> typeface(SkNEW_ARGS(SkRandomTypeface, (orig, paint, true)));
101 paint.setTypeface(typeface);
102 }
joshualitte49109f2015-07-17 12:47:39 -0700103
104 SkTextBlobBuilder builder;
105 for (int aa = 0; aa < 2; aa++) {
106 for (int subpixel = 0; subpixel < 2; subpixel++) {
107 for (int lcd = 0; lcd < 2; lcd++) {
108 paint.setAntiAlias(SkToBool(aa));
109 paint.setSubpixelText(SkToBool(subpixel));
110 paint.setLCDRenderText(SkToBool(lcd));
joshualitt65e96b42015-07-31 11:45:22 -0700111 if (!SkToBool(lcd)) {
112 paint.setTextSize(160);
113 }
joshualitte49109f2015-07-17 12:47:39 -0700114 const SkTextBlobBuilder::RunBuffer& run = builder.allocRun(paint,
joshualitt65e96b42015-07-31 11:45:22 -0700115 maxTotalText,
joshualitte49109f2015-07-17 12:47:39 -0700116 0, 0,
117 NULL);
joshualitt65e96b42015-07-31 11:45:22 -0700118 memcpy(run.glyphs, text.get(), maxTotalText * sizeof(uint16_t));
joshualitte49109f2015-07-17 12:47:39 -0700119 }
120 }
121 }
122 SkNEW_APPEND_TO_TARRAY(&blobs, TextBlobWrapper, (builder.build()));
123 }
124 }
125
joshualitt11dfc8e2015-07-23 08:30:25 -0700126 // create surface where LCD is impossible
127 info = SkImageInfo::MakeN32Premul(kWidth, kHeight);
128 SkSurfaceProps propsNoLCD(0, kUnknown_SkPixelGeometry);
129 SkAutoTUnref<SkSurface> surfaceNoLCD(canvas->newSurface(info, &propsNoLCD));
130 REPORTER_ASSERT(reporter, surface);
131 if (!surface) {
132 return;
133 }
134
135 SkCanvas* canvasNoLCD = surfaceNoLCD->getCanvas();
136
joshualitte49109f2015-07-17 12:47:39 -0700137 // test redraw
138 draw(canvas, 2, blobs);
joshualitt11dfc8e2015-07-23 08:30:25 -0700139 draw(canvasNoLCD, 2, blobs);
joshualitte49109f2015-07-17 12:47:39 -0700140
141 // test draw after free
142 ctx->freeGpuResources();
143 draw(canvas, 1, blobs);
144
joshualitt11dfc8e2015-07-23 08:30:25 -0700145 ctx->freeGpuResources();
146 draw(canvasNoLCD, 1, blobs);
147
joshualitte49109f2015-07-17 12:47:39 -0700148 // test draw after abandon
149 ctx->abandonContext();
150 draw(canvas, 1, blobs);
151}
joshualitt65e96b42015-07-31 11:45:22 -0700152
153DEF_GPUTEST(TextBlobCache, reporter, factory) {
154 text_blob_cache_inner(reporter, factory, 4096, 256, 30, true);
155}
156
157DEF_GPUTEST(TextBlobAbnormal, reporter, factory) {
158#ifdef SK_BUILD_FOR_ANDROID
159 text_blob_cache_inner(reporter, factory, 32, 32, 1, false);
160#else
161 text_blob_cache_inner(reporter, factory, 256, 256, 1, false);
162#endif
163}
joshualitte49109f2015-07-17 12:47:39 -0700164#endif