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 |
| 27 | #include "GrContextFactory.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 | |
| 30 | struct TextBlobWrapper { |
joshualitt | bedf7e5 | 2015-07-23 08:09:35 -0700 | [diff] [blame] | 31 | // This class assumes it 'owns' the textblob it wraps, and thus does not need to take a ref |
| 32 | explicit TextBlobWrapper(const SkTextBlob* blob) : fBlob(blob) {} |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 33 | TextBlobWrapper(const TextBlobWrapper& blob) : fBlob(SkRef(blob.fBlob.get())) {} |
| 34 | |
| 35 | SkAutoTUnref<const SkTextBlob> fBlob; |
| 36 | }; |
| 37 | |
| 38 | static void draw(SkCanvas* canvas, int redraw, const SkTArray<TextBlobWrapper>& blobs) { |
joshualitt | 404d9d6 | 2015-07-22 11:00:32 -0700 | [diff] [blame] | 39 | int yOffset = 0; |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 40 | for (int r = 0; r < redraw; r++) { |
| 41 | for (int i = 0; i < blobs.count(); i++) { |
joshualitt | 404d9d6 | 2015-07-22 11:00:32 -0700 | [diff] [blame] | 42 | const SkTextBlob* blob = blobs[i].fBlob.get(); |
| 43 | const SkRect& bounds = blob->bounds(); |
| 44 | yOffset += SkScalarCeilToInt(bounds.height()); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 45 | SkPaint paint; |
joshualitt | 404d9d6 | 2015-07-22 11:00:32 -0700 | [diff] [blame] | 46 | canvas->drawTextBlob(blob, 0, SkIntToScalar(yOffset), paint); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | } |
| 50 | |
joshualitt | 11dfc8e | 2015-07-23 08:30:25 -0700 | [diff] [blame] | 51 | static const int kWidth = 1024; |
| 52 | static const int kHeight = 768; |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 53 | |
| 54 | // This test hammers the GPU textblobcache and font atlas |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 55 | static void text_blob_cache_inner(skiatest::Reporter* reporter, GrContextFactory* factory, |
joshualitt | 7f9c9eb | 2015-08-21 11:08:00 -0700 | [diff] [blame] | 56 | int maxTotalText, int maxGlyphID, int maxFamilies, bool normal, |
| 57 | bool stressTest) { |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 58 | // setup surface |
| 59 | uint32_t flags = 0; |
| 60 | SkSurfaceProps props(flags, SkSurfaceProps::kLegacyFontHost_InitType); |
| 61 | |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 62 | // We don't typically actually draw with this unittest |
| 63 | GrContext* ctx = factory->get(GrContextFactory::kNull_GLContextType); |
joshualitt | 7f9c9eb | 2015-08-21 11:08:00 -0700 | [diff] [blame] | 64 | |
| 65 | // configure our context for maximum stressing of cache and atlas |
| 66 | if (stressTest) { |
| 67 | GrTest::SetupAlwaysEvictAtlas(ctx); |
| 68 | ctx->setTextBlobCacheLimit_ForTesting(0); |
| 69 | } |
| 70 | |
joshualitt | 11dfc8e | 2015-07-23 08:30:25 -0700 | [diff] [blame] | 71 | SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, kN32_SkColorType, kPremul_SkAlphaType); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 72 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, info, |
| 73 | 0, &props)); |
| 74 | REPORTER_ASSERT(reporter, surface); |
| 75 | if (!surface) { |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | SkCanvas* canvas = surface->getCanvas(); |
| 80 | |
| 81 | SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
| 82 | |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 83 | int count = SkMin32(fm->countFamilies(), maxFamilies); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 84 | |
| 85 | // make a ton of text |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 86 | SkAutoTArray<uint16_t> text(maxTotalText); |
| 87 | for (int i = 0; i < maxTotalText; i++) { |
| 88 | text[i] = i % maxGlyphID; |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | // generate textblobs |
| 92 | SkTArray<TextBlobWrapper> blobs; |
| 93 | for (int i = 0; i < count; i++) { |
| 94 | SkPaint paint; |
| 95 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
joshualitt | 11dfc8e | 2015-07-23 08:30:25 -0700 | [diff] [blame] | 96 | paint.setTextSize(48); // draw big glyphs to really stress the atlas |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 97 | |
| 98 | SkString familyName; |
| 99 | fm->getFamilyName(i, &familyName); |
| 100 | SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i)); |
| 101 | for (int j = 0; j < set->count(); ++j) { |
| 102 | SkFontStyle fs; |
| 103 | set->getStyle(j, &fs, NULL); |
| 104 | |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 105 | // We use a typeface which randomy returns unexpected mask formats to fuzz |
| 106 | SkAutoTUnref<SkTypeface> orig(set->createTypeface(j)); |
| 107 | if (normal) { |
| 108 | paint.setTypeface(orig); |
| 109 | } else { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 110 | SkAutoTUnref<SkTypeface> typeface(new SkRandomTypeface(orig, paint, true)); |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 111 | paint.setTypeface(typeface); |
| 112 | } |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 113 | |
| 114 | SkTextBlobBuilder builder; |
| 115 | for (int aa = 0; aa < 2; aa++) { |
| 116 | for (int subpixel = 0; subpixel < 2; subpixel++) { |
| 117 | for (int lcd = 0; lcd < 2; lcd++) { |
| 118 | paint.setAntiAlias(SkToBool(aa)); |
| 119 | paint.setSubpixelText(SkToBool(subpixel)); |
| 120 | paint.setLCDRenderText(SkToBool(lcd)); |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 121 | if (!SkToBool(lcd)) { |
| 122 | paint.setTextSize(160); |
| 123 | } |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 124 | const SkTextBlobBuilder::RunBuffer& run = builder.allocRun(paint, |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 125 | maxTotalText, |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 126 | 0, 0, |
| 127 | NULL); |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 128 | memcpy(run.glyphs, text.get(), maxTotalText * sizeof(uint16_t)); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | } |
| 132 | SkNEW_APPEND_TO_TARRAY(&blobs, TextBlobWrapper, (builder.build())); |
| 133 | } |
| 134 | } |
| 135 | |
joshualitt | 11dfc8e | 2015-07-23 08:30:25 -0700 | [diff] [blame] | 136 | // create surface where LCD is impossible |
| 137 | info = SkImageInfo::MakeN32Premul(kWidth, kHeight); |
| 138 | SkSurfaceProps propsNoLCD(0, kUnknown_SkPixelGeometry); |
| 139 | SkAutoTUnref<SkSurface> surfaceNoLCD(canvas->newSurface(info, &propsNoLCD)); |
| 140 | REPORTER_ASSERT(reporter, surface); |
| 141 | if (!surface) { |
| 142 | return; |
| 143 | } |
| 144 | |
| 145 | SkCanvas* canvasNoLCD = surfaceNoLCD->getCanvas(); |
| 146 | |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 147 | // test redraw |
| 148 | draw(canvas, 2, blobs); |
joshualitt | 11dfc8e | 2015-07-23 08:30:25 -0700 | [diff] [blame] | 149 | draw(canvasNoLCD, 2, blobs); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 150 | |
| 151 | // test draw after free |
| 152 | ctx->freeGpuResources(); |
| 153 | draw(canvas, 1, blobs); |
| 154 | |
joshualitt | 11dfc8e | 2015-07-23 08:30:25 -0700 | [diff] [blame] | 155 | ctx->freeGpuResources(); |
| 156 | draw(canvasNoLCD, 1, blobs); |
| 157 | |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 158 | // test draw after abandon |
| 159 | ctx->abandonContext(); |
| 160 | draw(canvas, 1, blobs); |
| 161 | } |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 162 | |
| 163 | DEF_GPUTEST(TextBlobCache, reporter, factory) { |
joshualitt | 7f9c9eb | 2015-08-21 11:08:00 -0700 | [diff] [blame] | 164 | text_blob_cache_inner(reporter, factory, 1024, 256, 30, true, false); |
| 165 | } |
| 166 | |
| 167 | DEF_GPUTEST(TextBlobStressCache, reporter, factory) { |
| 168 | text_blob_cache_inner(reporter, factory, 256, 256, 10, true, true); |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | DEF_GPUTEST(TextBlobAbnormal, reporter, factory) { |
joshualitt | 7f9c9eb | 2015-08-21 11:08:00 -0700 | [diff] [blame] | 172 | text_blob_cache_inner(reporter, factory, 256, 256, 10, false, false); |
| 173 | } |
| 174 | |
| 175 | DEF_GPUTEST(TextBlobStressAbnormal, reporter, factory) { |
| 176 | text_blob_cache_inner(reporter, factory, 256, 256, 10, false, true); |
joshualitt | 65e96b4 | 2015-07-31 11:45:22 -0700 | [diff] [blame] | 177 | } |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 178 | #endif |