joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "tools/ToolUtils.h" |
joshualitt | 7f9c9eb | 2015-08-21 11:08:00 -0700 | [diff] [blame] | 9 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 10 | #include "include/core/SkCanvas.h" |
| 11 | #include "include/core/SkFontMgr.h" |
| 12 | #include "include/core/SkGraphics.h" |
| 13 | #include "include/core/SkPaint.h" |
| 14 | #include "include/core/SkPoint.h" |
| 15 | #include "include/core/SkSurface.h" |
| 16 | #include "include/core/SkTextBlob.h" |
| 17 | #include "include/core/SkTypeface.h" |
| 18 | #include "src/core/SkGlyphRun.h" |
| 19 | #include "tools/fonts/RandomScalerContext.h" |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 20 | |
| 21 | #ifdef SK_BUILD_FOR_WIN |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "include/ports/SkTypeface_win.h" |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 23 | #endif |
| 24 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "tests/Test.h" |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 26 | |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 27 | #include "include/gpu/GrContext.h" |
| 28 | #include "src/gpu/GrContextPriv.h" |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 29 | |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 30 | static void draw(SkCanvas* canvas, int redraw, const SkTArray<sk_sp<SkTextBlob>>& blobs) { |
joshualitt | 404d9d6 | 2015-07-22 11:00:32 -0700 | [diff] [blame] | 31 | int yOffset = 0; |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 32 | for (int r = 0; r < redraw; r++) { |
| 33 | for (int i = 0; i < blobs.count(); i++) { |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 34 | const auto& blob = blobs[i]; |
joshualitt | 404d9d6 | 2015-07-22 11:00:32 -0700 | [diff] [blame] | 35 | const SkRect& bounds = blob->bounds(); |
| 36 | yOffset += SkScalarCeilToInt(bounds.height()); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 37 | SkPaint paint; |
joshualitt | 404d9d6 | 2015-07-22 11:00:32 -0700 | [diff] [blame] | 38 | canvas->drawTextBlob(blob, 0, SkIntToScalar(yOffset), paint); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 39 | } |
| 40 | } |
| 41 | } |
| 42 | |
joshualitt | 11dfc8e | 2015-07-23 08:30:25 -0700 | [diff] [blame] | 43 | static const int kWidth = 1024; |
| 44 | static const int kHeight = 768; |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 45 | |
Herb Derby | d3895d8 | 2018-09-04 13:27:00 -0400 | [diff] [blame] | 46 | static void setup_always_evict_atlas(GrContext* context) { |
Robert Phillips | 9da87e0 | 2019-02-04 13:26:26 -0500 | [diff] [blame] | 47 | context->priv().getAtlasManager()->setAtlasSizesToMinimum_ForTesting(); |
Herb Derby | d3895d8 | 2018-09-04 13:27:00 -0400 | [diff] [blame] | 48 | } |
| 49 | |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 50 | // This test hammers the GPU textblobcache and font atlas |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 51 | static void text_blob_cache_inner(skiatest::Reporter* reporter, GrContext* context, |
joshualitt | 7f9c9eb | 2015-08-21 11:08:00 -0700 | [diff] [blame] | 52 | int maxTotalText, int maxGlyphID, int maxFamilies, bool normal, |
| 53 | bool stressTest) { |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 54 | // setup surface |
| 55 | uint32_t flags = 0; |
| 56 | SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); |
| 57 | |
joshualitt | 7f9c9eb | 2015-08-21 11:08:00 -0700 | [diff] [blame] | 58 | // configure our context for maximum stressing of cache and atlas |
| 59 | if (stressTest) { |
Herb Derby | d3895d8 | 2018-09-04 13:27:00 -0400 | [diff] [blame] | 60 | setup_always_evict_atlas(context); |
Robert Phillips | dbaf317 | 2019-02-06 15:12:53 -0500 | [diff] [blame] | 61 | context->priv().testingOnly_setTextBlobCacheLimit(0); |
joshualitt | 7f9c9eb | 2015-08-21 11:08:00 -0700 | [diff] [blame] | 62 | } |
| 63 | |
Brian Osman | 7c59774 | 2019-03-26 11:10:11 -0400 | [diff] [blame] | 64 | SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, kRGBA_8888_SkColorType, |
| 65 | kPremul_SkAlphaType); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 66 | auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, &props)); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 67 | REPORTER_ASSERT(reporter, surface); |
| 68 | if (!surface) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | SkCanvas* canvas = surface->getCanvas(); |
| 73 | |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 74 | sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault()); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 75 | |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 76 | int count = SkMin32(fm->countFamilies(), maxFamilies); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 77 | |
| 78 | // make a ton of text |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 79 | SkAutoTArray<uint16_t> text(maxTotalText); |
| 80 | for (int i = 0; i < maxTotalText; i++) { |
| 81 | text[i] = i % maxGlyphID; |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | // generate textblobs |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 85 | SkTArray<sk_sp<SkTextBlob>> blobs; |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 86 | for (int i = 0; i < count; i++) { |
Mike Reed | 70914f5 | 2018-11-23 13:08:33 -0500 | [diff] [blame] | 87 | SkFont font; |
| 88 | font.setSize(48); // draw big glyphs to really stress the atlas |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 89 | |
| 90 | SkString familyName; |
| 91 | fm->getFamilyName(i, &familyName); |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 92 | sk_sp<SkFontStyleSet> set(fm->createStyleSet(i)); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 93 | for (int j = 0; j < set->count(); ++j) { |
| 94 | SkFontStyle fs; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 95 | set->getStyle(j, &fs, nullptr); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 96 | |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 97 | // We use a typeface which randomy returns unexpected mask formats to fuzz |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 98 | sk_sp<SkTypeface> orig(set->createTypeface(j)); |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 99 | if (normal) { |
Mike Reed | 70914f5 | 2018-11-23 13:08:33 -0500 | [diff] [blame] | 100 | font.setTypeface(orig); |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 101 | } else { |
Mike Reed | 70914f5 | 2018-11-23 13:08:33 -0500 | [diff] [blame] | 102 | font.setTypeface(sk_make_sp<SkRandomTypeface>(orig, SkPaint(), true)); |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 103 | } |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 104 | |
| 105 | SkTextBlobBuilder builder; |
| 106 | for (int aa = 0; aa < 2; aa++) { |
| 107 | for (int subpixel = 0; subpixel < 2; subpixel++) { |
| 108 | for (int lcd = 0; lcd < 2; lcd++) { |
Mike Reed | 70914f5 | 2018-11-23 13:08:33 -0500 | [diff] [blame] | 109 | font.setEdging(SkFont::Edging::kAlias); |
| 110 | if (aa) { |
| 111 | font.setEdging(SkFont::Edging::kAntiAlias); |
| 112 | if (lcd) { |
| 113 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
| 114 | } |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 115 | } |
Mike Reed | 70914f5 | 2018-11-23 13:08:33 -0500 | [diff] [blame] | 116 | font.setSubpixel(SkToBool(subpixel)); |
| 117 | if (!SkToBool(lcd)) { |
| 118 | font.setSize(160); |
| 119 | } |
| 120 | const SkTextBlobBuilder::RunBuffer& run = builder.allocRun(font, |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 121 | maxTotalText, |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 122 | 0, 0, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 123 | nullptr); |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 124 | memcpy(run.glyphs, text.get(), maxTotalText * sizeof(uint16_t)); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 125 | } |
| 126 | } |
| 127 | } |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 128 | blobs.emplace_back(builder.make()); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
joshualitt | 11dfc8e | 2015-07-23 08:30:25 -0700 | [diff] [blame] | 132 | // create surface where LCD is impossible |
Brian Osman | 7c59774 | 2019-03-26 11:10:11 -0400 | [diff] [blame] | 133 | info = SkImageInfo::Make(kWidth, kHeight, kRGBA_8888_SkColorType, kPremul_SkAlphaType); |
joshualitt | 11dfc8e | 2015-07-23 08:30:25 -0700 | [diff] [blame] | 134 | SkSurfaceProps propsNoLCD(0, kUnknown_SkPixelGeometry); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 135 | auto surfaceNoLCD(canvas->makeSurface(info, &propsNoLCD)); |
joshualitt | 11dfc8e | 2015-07-23 08:30:25 -0700 | [diff] [blame] | 136 | REPORTER_ASSERT(reporter, surface); |
| 137 | if (!surface) { |
| 138 | return; |
| 139 | } |
| 140 | |
| 141 | SkCanvas* canvasNoLCD = surfaceNoLCD->getCanvas(); |
| 142 | |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 143 | // test redraw |
| 144 | draw(canvas, 2, blobs); |
joshualitt | 11dfc8e | 2015-07-23 08:30:25 -0700 | [diff] [blame] | 145 | draw(canvasNoLCD, 2, blobs); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 146 | |
| 147 | // test draw after free |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 148 | context->freeGpuResources(); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 149 | draw(canvas, 1, blobs); |
| 150 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 151 | context->freeGpuResources(); |
joshualitt | 11dfc8e | 2015-07-23 08:30:25 -0700 | [diff] [blame] | 152 | draw(canvasNoLCD, 1, blobs); |
| 153 | |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 154 | // test draw after abandon |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 155 | context->abandonContext(); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 156 | draw(canvas, 1, blobs); |
| 157 | } |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 158 | |
Brian Osman | 7c59774 | 2019-03-26 11:10:11 -0400 | [diff] [blame] | 159 | DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobCache, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 160 | text_blob_cache_inner(reporter, ctxInfo.grContext(), 1024, 256, 30, true, false); |
joshualitt | 7f9c9eb | 2015-08-21 11:08:00 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Brian Osman | 7c59774 | 2019-03-26 11:10:11 -0400 | [diff] [blame] | 163 | DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobStressCache, reporter, ctxInfo) { |
Herb Derby | 278b067 | 2018-10-05 13:46:51 -0400 | [diff] [blame] | 164 | text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, true, true); |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Brian Osman | 7c59774 | 2019-03-26 11:10:11 -0400 | [diff] [blame] | 167 | DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobAbnormal, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 168 | text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, false, false); |
joshualitt | 7f9c9eb | 2015-08-21 11:08:00 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Brian Osman | 7c59774 | 2019-03-26 11:10:11 -0400 | [diff] [blame] | 171 | DEF_GPUTEST_FOR_MOCK_CONTEXT(TextBlobStressAbnormal, reporter, ctxInfo) { |
Herb Derby | 278b067 | 2018-10-05 13:46:51 -0400 | [diff] [blame] | 172 | text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, false, true); |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 173 | } |