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