blob: 7912194c2ccc1ab187bccd7021c384023169a9cf [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "tools/ToolUtils.h"
joshualitt7f9c9eb2015-08-21 11:08:00 -07009
Herb Derby9830fc42019-09-12 10:58:26 -040010#include <string>
11
Mike Reedac9f0c92020-12-23 10:11:33 -050012#include "include/core/SkBitmap.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050013#include "include/core/SkCanvas.h"
14#include "include/core/SkFontMgr.h"
15#include "include/core/SkGraphics.h"
16#include "include/core/SkPaint.h"
17#include "include/core/SkPoint.h"
18#include "include/core/SkSurface.h"
19#include "include/core/SkTextBlob.h"
20#include "include/core/SkTypeface.h"
Robert Phillips6d344c32020-07-06 10:56:46 -040021#include "include/gpu/GrDirectContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/core/SkGlyphRun.h"
Adlai Hollera0693042020-10-14 11:23:11 -040023#include "src/gpu/GrDirectContextPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "tools/fonts/RandomScalerContext.h"
joshualitte49109f2015-07-17 12:47:39 -070025
26#ifdef SK_BUILD_FOR_WIN
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027 #include "include/ports/SkTypeface_win.h"
joshualitte49109f2015-07-17 12:47:39 -070028#endif
29
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "tests/Test.h"
joshualitte49109f2015-07-17 12:47:39 -070031
Adlai Hollera0693042020-10-14 11:23:11 -040032#include "src/gpu/GrDirectContextPriv.h"
Robert Phillipse19babf2020-04-06 13:57:30 -040033#include "src/gpu/text/GrAtlasManager.h"
Herb Derby1ca54d42020-06-26 12:50:38 -040034#include "src/gpu/text/GrTextBlobCache.h"
joshualitte49109f2015-07-17 12:47:39 -070035
fmalita37283c22016-09-13 10:00:23 -070036static void draw(SkCanvas* canvas, int redraw, const SkTArray<sk_sp<SkTextBlob>>& 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++) {
fmalita37283c22016-09-13 10:00:23 -070040 const auto& blob = blobs[i];
joshualitt404d9d62015-07-22 11:00:32 -070041 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
Robert Phillips0c5bb2f2020-07-17 15:40:13 -040052static void setup_always_evict_atlas(GrDirectContext* dContext) {
53 dContext->priv().getAtlasManager()->setAtlasDimensionsToMinimum_ForTesting();
Herb Derbyd3895d82018-09-04 13:27:00 -040054}
55
Herb Derby1ca54d42020-06-26 12:50:38 -040056class GrTextBlobTestingPeer {
57public:
58 static void SetBudget(GrTextBlobCache* cache, size_t budget) {
Herb Derby13e3fae2020-07-23 13:24:53 -040059 SkAutoSpinlock lock{cache->fSpinLock};
Herb Derby1ca54d42020-06-26 12:50:38 -040060 cache->fSizeBudget = budget;
61 cache->internalCheckPurge();
62 }
63};
64
joshualitte49109f2015-07-17 12:47:39 -070065// This test hammers the GPU textblobcache and font atlas
Robert Phillips0c5bb2f2020-07-17 15:40:13 -040066static void text_blob_cache_inner(skiatest::Reporter* reporter, GrDirectContext* dContext,
joshualitt7f9c9eb2015-08-21 11:08:00 -070067 int maxTotalText, int maxGlyphID, int maxFamilies, bool normal,
68 bool stressTest) {
joshualitte49109f2015-07-17 12:47:39 -070069 // setup surface
70 uint32_t flags = 0;
Ben Wagnerae4bb982020-09-24 14:49:00 -040071 SkSurfaceProps props(flags, kRGB_H_SkPixelGeometry);
joshualitte49109f2015-07-17 12:47:39 -070072
joshualitt7f9c9eb2015-08-21 11:08:00 -070073 // configure our context for maximum stressing of cache and atlas
74 if (stressTest) {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -040075 setup_always_evict_atlas(dContext);
76 GrTextBlobTestingPeer::SetBudget(dContext->priv().getTextBlobCache(), 0);
joshualitt7f9c9eb2015-08-21 11:08:00 -070077 }
78
Brian Osman7c597742019-03-26 11:10:11 -040079 SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, kRGBA_8888_SkColorType,
80 kPremul_SkAlphaType);
Robert Phillips0c5bb2f2020-07-17 15:40:13 -040081 auto surface(SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info, 0, &props));
joshualitte49109f2015-07-17 12:47:39 -070082 REPORTER_ASSERT(reporter, surface);
83 if (!surface) {
84 return;
85 }
86
87 SkCanvas* canvas = surface->getCanvas();
88
Hal Canary342b7ac2016-11-04 11:49:42 -040089 sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
joshualitte49109f2015-07-17 12:47:39 -070090
Brian Osman7f364052020-02-06 11:25:43 -050091 int count = std::min(fm->countFamilies(), maxFamilies);
joshualitte49109f2015-07-17 12:47:39 -070092
93 // make a ton of text
joshualitt65e96b42015-07-31 11:45:22 -070094 SkAutoTArray<uint16_t> text(maxTotalText);
95 for (int i = 0; i < maxTotalText; i++) {
96 text[i] = i % maxGlyphID;
joshualitte49109f2015-07-17 12:47:39 -070097 }
98
99 // generate textblobs
fmalita37283c22016-09-13 10:00:23 -0700100 SkTArray<sk_sp<SkTextBlob>> blobs;
joshualitte49109f2015-07-17 12:47:39 -0700101 for (int i = 0; i < count; i++) {
Mike Reed70914f52018-11-23 13:08:33 -0500102 SkFont font;
103 font.setSize(48); // draw big glyphs to really stress the atlas
joshualitte49109f2015-07-17 12:47:39 -0700104
105 SkString familyName;
106 fm->getFamilyName(i, &familyName);
Hal Canary342b7ac2016-11-04 11:49:42 -0400107 sk_sp<SkFontStyleSet> set(fm->createStyleSet(i));
joshualitte49109f2015-07-17 12:47:39 -0700108 for (int j = 0; j < set->count(); ++j) {
109 SkFontStyle fs;
halcanary96fcdcc2015-08-27 07:41:13 -0700110 set->getStyle(j, &fs, nullptr);
joshualitte49109f2015-07-17 12:47:39 -0700111
joshualitt65e96b42015-07-31 11:45:22 -0700112 // We use a typeface which randomy returns unexpected mask formats to fuzz
bungeman13b9c952016-05-12 10:09:30 -0700113 sk_sp<SkTypeface> orig(set->createTypeface(j));
joshualitt65e96b42015-07-31 11:45:22 -0700114 if (normal) {
Mike Reed70914f52018-11-23 13:08:33 -0500115 font.setTypeface(orig);
joshualitt65e96b42015-07-31 11:45:22 -0700116 } else {
Mike Reed70914f52018-11-23 13:08:33 -0500117 font.setTypeface(sk_make_sp<SkRandomTypeface>(orig, SkPaint(), true));
joshualitt65e96b42015-07-31 11:45:22 -0700118 }
joshualitte49109f2015-07-17 12:47:39 -0700119
120 SkTextBlobBuilder builder;
121 for (int aa = 0; aa < 2; aa++) {
122 for (int subpixel = 0; subpixel < 2; subpixel++) {
123 for (int lcd = 0; lcd < 2; lcd++) {
Mike Reed70914f52018-11-23 13:08:33 -0500124 font.setEdging(SkFont::Edging::kAlias);
125 if (aa) {
126 font.setEdging(SkFont::Edging::kAntiAlias);
127 if (lcd) {
128 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
129 }
joshualitt65e96b42015-07-31 11:45:22 -0700130 }
Mike Reed70914f52018-11-23 13:08:33 -0500131 font.setSubpixel(SkToBool(subpixel));
132 if (!SkToBool(lcd)) {
133 font.setSize(160);
134 }
135 const SkTextBlobBuilder::RunBuffer& run = builder.allocRun(font,
joshualitt65e96b42015-07-31 11:45:22 -0700136 maxTotalText,
joshualitte49109f2015-07-17 12:47:39 -0700137 0, 0,
halcanary96fcdcc2015-08-27 07:41:13 -0700138 nullptr);
joshualitt65e96b42015-07-31 11:45:22 -0700139 memcpy(run.glyphs, text.get(), maxTotalText * sizeof(uint16_t));
joshualitte49109f2015-07-17 12:47:39 -0700140 }
141 }
142 }
fmalita37283c22016-09-13 10:00:23 -0700143 blobs.emplace_back(builder.make());
joshualitte49109f2015-07-17 12:47:39 -0700144 }
145 }
146
joshualitt11dfc8e2015-07-23 08:30:25 -0700147 // create surface where LCD is impossible
Brian Osman7c597742019-03-26 11:10:11 -0400148 info = SkImageInfo::Make(kWidth, kHeight, kRGBA_8888_SkColorType, kPremul_SkAlphaType);
joshualitt11dfc8e2015-07-23 08:30:25 -0700149 SkSurfaceProps propsNoLCD(0, kUnknown_SkPixelGeometry);
reede8f30622016-03-23 18:59:25 -0700150 auto surfaceNoLCD(canvas->makeSurface(info, &propsNoLCD));
joshualitt11dfc8e2015-07-23 08:30:25 -0700151 REPORTER_ASSERT(reporter, surface);
152 if (!surface) {
153 return;
154 }
155
156 SkCanvas* canvasNoLCD = surfaceNoLCD->getCanvas();
157
joshualitte49109f2015-07-17 12:47:39 -0700158 // test redraw
159 draw(canvas, 2, blobs);
joshualitt11dfc8e2015-07-23 08:30:25 -0700160 draw(canvasNoLCD, 2, blobs);
joshualitte49109f2015-07-17 12:47:39 -0700161
162 // test draw after free
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400163 dContext->freeGpuResources();
joshualitte49109f2015-07-17 12:47:39 -0700164 draw(canvas, 1, blobs);
165
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400166 dContext->freeGpuResources();
joshualitt11dfc8e2015-07-23 08:30:25 -0700167 draw(canvasNoLCD, 1, blobs);
168
joshualitte49109f2015-07-17 12:47:39 -0700169 // test draw after abandon
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400170 dContext->abandonContext();
joshualitte49109f2015-07-17 12:47:39 -0700171 draw(canvas, 1, blobs);
172}
joshualitt65e96b42015-07-31 11:45:22 -0700173
Brian Osman7c597742019-03-26 11:10:11 -0400174DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobCache, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400175 text_blob_cache_inner(reporter, ctxInfo.directContext(), 1024, 256, 30, true, false);
joshualitt7f9c9eb2015-08-21 11:08:00 -0700176}
177
Brian Osman7c597742019-03-26 11:10:11 -0400178DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobStressCache, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400179 text_blob_cache_inner(reporter, ctxInfo.directContext(), 256, 256, 10, true, true);
joshualitt65e96b42015-07-31 11:45:22 -0700180}
181
Brian Osman7c597742019-03-26 11:10:11 -0400182DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobAbnormal, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400183 text_blob_cache_inner(reporter, ctxInfo.directContext(), 256, 256, 10, false, false);
joshualitt7f9c9eb2015-08-21 11:08:00 -0700184}
185
Brian Osman7c597742019-03-26 11:10:11 -0400186DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobStressAbnormal, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400187 text_blob_cache_inner(reporter, ctxInfo.directContext(), 256, 256, 10, false, true);
joshualitt65e96b42015-07-31 11:45:22 -0700188}
Herb Derby9830fc42019-09-12 10:58:26 -0400189
190static const int kScreenDim = 160;
191
192static SkBitmap draw_blob(SkTextBlob* blob, SkSurface* surface, SkPoint offset) {
193
194 SkPaint paint;
195
196 SkCanvas* canvas = surface->getCanvas();
197 canvas->save();
198 canvas->drawColor(SK_ColorWHITE, SkBlendMode::kSrc);
199 canvas->translate(offset.fX, offset.fY);
200 canvas->drawTextBlob(blob, 0, 0, paint);
201 SkBitmap bitmap;
202 bitmap.allocN32Pixels(kScreenDim, kScreenDim);
203 surface->readPixels(bitmap, 0, 0);
204 canvas->restore();
205 return bitmap;
206}
207
208static bool compare_bitmaps(const SkBitmap& expected, const SkBitmap& actual) {
209 SkASSERT(expected.width() == actual.width());
210 SkASSERT(expected.height() == actual.height());
211 for (int i = 0; i < expected.width(); ++i) {
212 for (int j = 0; j < expected.height(); ++j) {
213 SkColor expectedColor = expected.getColor(i, j);
214 SkColor actualColor = actual.getColor(i, j);
215 if (expectedColor != actualColor) {
216 return false;
217 }
218 }
219 }
220 return true;
221}
222
223static sk_sp<SkTextBlob> make_blob() {
224 auto tf = SkTypeface::MakeFromName("Roboto2-Regular", SkFontStyle());
225 SkFont font;
226 font.setTypeface(tf);
227 font.setSubpixel(false);
228 font.setEdging(SkFont::Edging::kAlias);
229 font.setSize(24);
230
231 static char text[] = "HekpqB";
232 static const int maxGlyphLen = sizeof(text) * 4;
233 SkGlyphID glyphs[maxGlyphLen];
234 int glyphCount =
235 font.textToGlyphs(text, sizeof(text), SkTextEncoding::kUTF8, glyphs, maxGlyphLen);
236
237 SkTextBlobBuilder builder;
238 const auto& runBuffer = builder.allocRun(font, glyphCount, 0, 0);
239 for (int i = 0; i < glyphCount; i++) {
240 runBuffer.glyphs[i] = glyphs[i];
241 }
242 return builder.make();
243}
244
Herb Derby88a3b462020-05-22 11:35:02 -0400245// Turned off to pass on android and ios devices, which were running out of memory..
246#if 0
Herb Derby317dce52020-05-21 14:46:06 -0400247static sk_sp<SkTextBlob> make_large_blob() {
248 auto tf = SkTypeface::MakeFromName("Roboto2-Regular", SkFontStyle());
249 SkFont font;
250 font.setTypeface(tf);
251 font.setSubpixel(false);
252 font.setEdging(SkFont::Edging::kAlias);
253 font.setSize(24);
254
255 const int mallocSize = 0x3c3c3bd; // x86 size
256 std::unique_ptr<char[]> text{new char[mallocSize + 1]};
257 if (text == nullptr) {
258 return nullptr;
259 }
260 for (int i = 0; i < mallocSize; i++) {
261 text[i] = 'x';
262 }
263 text[mallocSize] = 0;
264
265 static const int maxGlyphLen = mallocSize;
266 std::unique_ptr<SkGlyphID[]> glyphs{new SkGlyphID[maxGlyphLen]};
267 int glyphCount =
268 font.textToGlyphs(
269 text.get(), mallocSize, SkTextEncoding::kUTF8, glyphs.get(), maxGlyphLen);
270 SkTextBlobBuilder builder;
271 const auto& runBuffer = builder.allocRun(font, glyphCount, 0, 0);
272 for (int i = 0; i < glyphCount; i++) {
273 runBuffer.glyphs[i] = glyphs[i];
274 }
275 return builder.make();
276}
277
278DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TextBlobIntegerOverflowTest, reporter, ctxInfo) {
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400279 auto dContext = ctxInfo.directContext();
Herb Derby317dce52020-05-21 14:46:06 -0400280 const SkImageInfo info =
281 SkImageInfo::Make(kScreenDim, kScreenDim, kN32_SkColorType, kPremul_SkAlphaType);
Robert Phillips0c5bb2f2020-07-17 15:40:13 -0400282 auto surface = SkSurface::MakeRenderTarget(dContext, SkBudgeted::kNo, info);
Herb Derby317dce52020-05-21 14:46:06 -0400283
284 auto blob = make_large_blob();
285 int y = 40;
286 SkBitmap base = draw_blob(blob.get(), surface.get(), {40, y + 0.0f});
287}
Herb Derby88a3b462020-05-22 11:35:02 -0400288#endif
Herb Derby317dce52020-05-21 14:46:06 -0400289
Herb Derby9830fc42019-09-12 10:58:26 -0400290static const bool kDumpPngs = true;
291// dump pngs needs a "good" and a "bad" directory to put the results in. This allows the use of the
292// skdiff tool to visualize the differences.
293
294void write_png(const std::string& filename, const SkBitmap& bitmap) {
295 auto data = SkEncodeBitmap(bitmap, SkEncodedImageFormat::kPNG, 0);
296 SkFILEWStream w{filename.c_str()};
297 w.write(data->data(), data->size());
298 w.fsync();
299}
300
301DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TextBlobJaggedGlyph, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400302 auto direct = ctxInfo.directContext();
Herb Derby9830fc42019-09-12 10:58:26 -0400303 const SkImageInfo info =
304 SkImageInfo::Make(kScreenDim, kScreenDim, kN32_SkColorType, kPremul_SkAlphaType);
Robert Phillips6d344c32020-07-06 10:56:46 -0400305 auto surface = SkSurface::MakeRenderTarget(direct, SkBudgeted::kNo, info);
Herb Derby9830fc42019-09-12 10:58:26 -0400306
307 auto blob = make_blob();
308
309 for (int y = 40; y < kScreenDim - 40; y++) {
310 SkBitmap base = draw_blob(blob.get(), surface.get(), {40, y + 0.0f});
311 SkBitmap half = draw_blob(blob.get(), surface.get(), {40, y + 0.5f});
312 SkBitmap unit = draw_blob(blob.get(), surface.get(), {40, y + 1.0f});
313 bool isOk = compare_bitmaps(base, half) || compare_bitmaps(unit, half);
314 REPORTER_ASSERT(reporter, isOk);
315 if (!isOk) {
316 if (kDumpPngs) {
317 {
318 std::string filename = "bad/half-y" + std::to_string(y) + ".png";
319 write_png(filename, half);
320 }
321 {
322 std::string filename = "good/half-y" + std::to_string(y) + ".png";
323 write_png(filename, base);
324 }
325 }
326 break;
327 }
328 }
329
330 // Testing the x direction across all platforms does not workout, because letter spacing can
331 // change based on non-integer advance widths, but this has been useful for diagnosing problems.
332#if 0
333 blob = make_blob();
334 for (int x = 40; x < kScreenDim - 40; x++) {
335 SkBitmap base = draw_blob(blob.get(), surface.get(), {x + 0.0f, 40});
336 SkBitmap half = draw_blob(blob.get(), surface.get(), {x + 0.5f, 40});
337 SkBitmap unit = draw_blob(blob.get(), surface.get(), {x + 1.0f, 40});
338 bool isOk = compare_bitmaps(base, half) || compare_bitmaps(unit, half);
339 REPORTER_ASSERT(reporter, isOk);
340 if (!isOk) {
341 if (kDumpPngs) {
342 {
343 std::string filename = "bad/half-x" + std::to_string(x) + ".png";
344 write_png(filename, half);
345 }
346 {
347 std::string filename = "good/half-x" + std::to_string(x) + ".png";
348 write_png(filename, base);
349 }
350 }
351 break;
352 }
353 }
354#endif
355}
356
357DEF_GPUTEST_FOR_RENDERING_CONTEXTS(TextBlobSmoothScroll, reporter, ctxInfo) {
Robert Phillips6d344c32020-07-06 10:56:46 -0400358 auto direct = ctxInfo.directContext();
Herb Derby9830fc42019-09-12 10:58:26 -0400359 const SkImageInfo info =
360 SkImageInfo::Make(kScreenDim, kScreenDim, kN32_SkColorType, kPremul_SkAlphaType);
Robert Phillips6d344c32020-07-06 10:56:46 -0400361 auto surface = SkSurface::MakeRenderTarget(direct, SkBudgeted::kNo, info);
Herb Derby9830fc42019-09-12 10:58:26 -0400362
363 auto movingBlob = make_blob();
364
365 for (SkScalar y = 40; y < 50; y += 1.0/8.0) {
366 auto expectedBlob = make_blob();
367 auto expectedBitMap = draw_blob(expectedBlob.get(), surface.get(), {40, y});
368 auto movingBitmap = draw_blob(movingBlob.get(), surface.get(), {40, y});
369 bool isOk = compare_bitmaps(expectedBitMap, movingBitmap);
370 REPORTER_ASSERT(reporter, isOk);
371 if (!isOk) {
372 if (kDumpPngs) {
373 {
374 std::string filename = "bad/scroll-y" + std::to_string(y) + ".png";
375 write_png(filename, movingBitmap);
376 }
377 {
378 std::string filename = "good/scroll-y" + std::to_string(y) + ".png";
379 write_png(filename, expectedBitMap);
380 }
381 }
382 break;
383 }
384 }
385}