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