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 | |
| 8 | #include "sk_tool_utils.h" |
joshualitt | 7f9c9eb | 2015-08-21 11:08:00 -0700 | [diff] [blame] | 9 | |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 10 | #include "SkCanvas.h" |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 11 | #include "SkFontMgr.h" |
Herb Derby | d3895d8 | 2018-09-04 13:27:00 -0400 | [diff] [blame^] | 12 | #include "SkGlyphRun.h" |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 13 | #include "SkGraphics.h" |
Mike Klein | 10d66cc | 2017-11-10 11:33:43 -0500 | [diff] [blame] | 14 | #include "SkPaint.h" |
| 15 | #include "SkPoint.h" |
| 16 | #include "SkRandomScalerContext.h" |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 17 | #include "SkSurface.h" |
Mike Klein | 10d66cc | 2017-11-10 11:33:43 -0500 | [diff] [blame] | 18 | #include "SkTextBlob.h" |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 19 | #include "SkTypeface.h" |
| 20 | |
| 21 | #ifdef SK_BUILD_FOR_WIN |
| 22 | #include "SkTypeface_win.h" |
| 23 | #endif |
| 24 | |
| 25 | #include "Test.h" |
| 26 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 27 | #include "GrContext.h" |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 28 | #include "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) { |
| 47 | int dim = SkGlyphCacheCommon::kSkSideTooBigForAtlas; |
| 48 | // These sizes were selected because they allow each atlas to hold a single plot and will thus |
| 49 | // stress the atlas |
| 50 | GrDrawOpAtlasConfig configs[3]; |
| 51 | configs[kA8_GrMaskFormat].fWidth = dim; |
| 52 | configs[kA8_GrMaskFormat].fHeight = dim; |
| 53 | configs[kA8_GrMaskFormat].fPlotWidth = dim; |
| 54 | configs[kA8_GrMaskFormat].fPlotHeight = dim; |
| 55 | |
| 56 | configs[kA565_GrMaskFormat].fWidth = dim; |
| 57 | configs[kA565_GrMaskFormat].fHeight = dim; |
| 58 | configs[kA565_GrMaskFormat].fPlotWidth = dim; |
| 59 | configs[kA565_GrMaskFormat].fPlotHeight = dim; |
| 60 | |
| 61 | configs[kARGB_GrMaskFormat].fWidth = dim; |
| 62 | configs[kARGB_GrMaskFormat].fHeight = dim; |
| 63 | configs[kARGB_GrMaskFormat].fPlotWidth = dim; |
| 64 | configs[kARGB_GrMaskFormat].fPlotHeight = dim; |
| 65 | |
| 66 | context->contextPriv().setTextContextAtlasSizes_ForTesting(configs); |
| 67 | } |
| 68 | |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 69 | // This test hammers the GPU textblobcache and font atlas |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 70 | static void text_blob_cache_inner(skiatest::Reporter* reporter, GrContext* context, |
joshualitt | 7f9c9eb | 2015-08-21 11:08:00 -0700 | [diff] [blame] | 71 | int maxTotalText, int maxGlyphID, int maxFamilies, bool normal, |
| 72 | bool stressTest) { |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 73 | // setup surface |
| 74 | uint32_t flags = 0; |
| 75 | SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); |
| 76 | |
joshualitt | 7f9c9eb | 2015-08-21 11:08:00 -0700 | [diff] [blame] | 77 | // configure our context for maximum stressing of cache and atlas |
| 78 | if (stressTest) { |
Herb Derby | d3895d8 | 2018-09-04 13:27:00 -0400 | [diff] [blame^] | 79 | setup_always_evict_atlas(context); |
Robert Phillips | 0c4b7b1 | 2018-03-06 08:20:37 -0500 | [diff] [blame] | 80 | context->contextPriv().setTextBlobCacheLimit_ForTesting(0); |
joshualitt | 7f9c9eb | 2015-08-21 11:08:00 -0700 | [diff] [blame] | 81 | } |
| 82 | |
joshualitt | 11dfc8e | 2015-07-23 08:30:25 -0700 | [diff] [blame] | 83 | SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, kN32_SkColorType, kPremul_SkAlphaType); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 84 | auto surface(SkSurface::MakeRenderTarget(context, SkBudgeted::kNo, info, 0, &props)); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 85 | REPORTER_ASSERT(reporter, surface); |
| 86 | if (!surface) { |
| 87 | return; |
| 88 | } |
| 89 | |
| 90 | SkCanvas* canvas = surface->getCanvas(); |
| 91 | |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 92 | sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault()); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 93 | |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 94 | int count = SkMin32(fm->countFamilies(), maxFamilies); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 95 | |
| 96 | // make a ton of text |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 97 | SkAutoTArray<uint16_t> text(maxTotalText); |
| 98 | for (int i = 0; i < maxTotalText; i++) { |
| 99 | text[i] = i % maxGlyphID; |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // generate textblobs |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 103 | SkTArray<sk_sp<SkTextBlob>> blobs; |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 104 | for (int i = 0; i < count; i++) { |
| 105 | SkPaint paint; |
| 106 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
joshualitt | 11dfc8e | 2015-07-23 08:30:25 -0700 | [diff] [blame] | 107 | paint.setTextSize(48); // draw big glyphs to really stress the atlas |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 108 | |
| 109 | SkString familyName; |
| 110 | fm->getFamilyName(i, &familyName); |
Hal Canary | 342b7ac | 2016-11-04 11:49:42 -0400 | [diff] [blame] | 111 | sk_sp<SkFontStyleSet> set(fm->createStyleSet(i)); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 112 | for (int j = 0; j < set->count(); ++j) { |
| 113 | SkFontStyle fs; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 114 | set->getStyle(j, &fs, nullptr); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 115 | |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 116 | // We use a typeface which randomy returns unexpected mask formats to fuzz |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 117 | sk_sp<SkTypeface> orig(set->createTypeface(j)); |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 118 | if (normal) { |
| 119 | paint.setTypeface(orig); |
| 120 | } else { |
bungeman | 13b9c95 | 2016-05-12 10:09:30 -0700 | [diff] [blame] | 121 | paint.setTypeface(sk_make_sp<SkRandomTypeface>(orig, paint, true)); |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 122 | } |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 123 | |
| 124 | SkTextBlobBuilder builder; |
| 125 | for (int aa = 0; aa < 2; aa++) { |
| 126 | for (int subpixel = 0; subpixel < 2; subpixel++) { |
| 127 | for (int lcd = 0; lcd < 2; lcd++) { |
| 128 | paint.setAntiAlias(SkToBool(aa)); |
| 129 | paint.setSubpixelText(SkToBool(subpixel)); |
| 130 | paint.setLCDRenderText(SkToBool(lcd)); |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 131 | if (!SkToBool(lcd)) { |
| 132 | paint.setTextSize(160); |
| 133 | } |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 134 | const SkTextBlobBuilder::RunBuffer& run = builder.allocRun(paint, |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 135 | maxTotalText, |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 136 | 0, 0, |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 137 | nullptr); |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 138 | memcpy(run.glyphs, text.get(), maxTotalText * sizeof(uint16_t)); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 139 | } |
| 140 | } |
| 141 | } |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 142 | blobs.emplace_back(builder.make()); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | |
joshualitt | 11dfc8e | 2015-07-23 08:30:25 -0700 | [diff] [blame] | 146 | // create surface where LCD is impossible |
| 147 | info = SkImageInfo::MakeN32Premul(kWidth, kHeight); |
| 148 | SkSurfaceProps propsNoLCD(0, kUnknown_SkPixelGeometry); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 149 | auto surfaceNoLCD(canvas->makeSurface(info, &propsNoLCD)); |
joshualitt | 11dfc8e | 2015-07-23 08:30:25 -0700 | [diff] [blame] | 150 | REPORTER_ASSERT(reporter, surface); |
| 151 | if (!surface) { |
| 152 | return; |
| 153 | } |
| 154 | |
| 155 | SkCanvas* canvasNoLCD = surfaceNoLCD->getCanvas(); |
| 156 | |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 157 | // test redraw |
| 158 | draw(canvas, 2, blobs); |
joshualitt | 11dfc8e | 2015-07-23 08:30:25 -0700 | [diff] [blame] | 159 | draw(canvasNoLCD, 2, blobs); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 160 | |
| 161 | // test draw after free |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 162 | context->freeGpuResources(); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 163 | draw(canvas, 1, blobs); |
| 164 | |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 165 | context->freeGpuResources(); |
joshualitt | 11dfc8e | 2015-07-23 08:30:25 -0700 | [diff] [blame] | 166 | draw(canvasNoLCD, 1, blobs); |
| 167 | |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 168 | // test draw after abandon |
kkinnunen | 1530283 | 2015-12-01 04:35:26 -0800 | [diff] [blame] | 169 | context->abandonContext(); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 170 | draw(canvas, 1, blobs); |
| 171 | } |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 172 | |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 173 | DEF_GPUTEST_FOR_NULLGL_CONTEXT(TextBlobCache, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 174 | text_blob_cache_inner(reporter, ctxInfo.grContext(), 1024, 256, 30, true, false); |
joshualitt | 7f9c9eb | 2015-08-21 11:08:00 -0700 | [diff] [blame] | 175 | } |
| 176 | |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 177 | DEF_GPUTEST_FOR_NULLGL_CONTEXT(TextBlobStressCache, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 178 | text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, true, true); |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 179 | } |
| 180 | |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 181 | DEF_GPUTEST_FOR_NULLGL_CONTEXT(TextBlobAbnormal, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 182 | text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, false, false); |
joshualitt | 7f9c9eb | 2015-08-21 11:08:00 -0700 | [diff] [blame] | 183 | } |
| 184 | |
bsalomon | 758586c | 2016-04-06 14:02:39 -0700 | [diff] [blame] | 185 | DEF_GPUTEST_FOR_NULLGL_CONTEXT(TextBlobStressAbnormal, reporter, ctxInfo) { |
bsalomon | 8b7451a | 2016-05-11 06:33:06 -0700 | [diff] [blame] | 186 | text_blob_cache_inner(reporter, ctxInfo.grContext(), 256, 256, 10, false, true); |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 187 | } |