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" |
| 17 | |
| 18 | #ifdef SK_BUILD_FOR_WIN |
| 19 | #include "SkTypeface_win.h" |
| 20 | #endif |
| 21 | |
| 22 | #include "Test.h" |
| 23 | |
| 24 | #if SK_SUPPORT_GPU |
| 25 | #include "GrContextFactory.h" |
| 26 | |
| 27 | struct TextBlobWrapper { |
joshualitt | bedf7e5 | 2015-07-23 08:09:35 -0700 | [diff] [blame] | 28 | // This class assumes it 'owns' the textblob it wraps, and thus does not need to take a ref |
| 29 | explicit TextBlobWrapper(const SkTextBlob* blob) : fBlob(blob) {} |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 30 | TextBlobWrapper(const TextBlobWrapper& blob) : fBlob(SkRef(blob.fBlob.get())) {} |
| 31 | |
| 32 | SkAutoTUnref<const SkTextBlob> fBlob; |
| 33 | }; |
| 34 | |
| 35 | static void draw(SkCanvas* canvas, int redraw, const SkTArray<TextBlobWrapper>& blobs) { |
joshualitt | 404d9d6 | 2015-07-22 11:00:32 -0700 | [diff] [blame] | 36 | int yOffset = 0; |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 37 | for (int r = 0; r < redraw; r++) { |
| 38 | for (int i = 0; i < blobs.count(); i++) { |
joshualitt | 404d9d6 | 2015-07-22 11:00:32 -0700 | [diff] [blame] | 39 | const SkTextBlob* blob = blobs[i].fBlob.get(); |
| 40 | const SkRect& bounds = blob->bounds(); |
| 41 | yOffset += SkScalarCeilToInt(bounds.height()); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 42 | SkPaint paint; |
joshualitt | 404d9d6 | 2015-07-22 11:00:32 -0700 | [diff] [blame] | 43 | canvas->drawTextBlob(blob, 0, SkIntToScalar(yOffset), paint); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 44 | } |
| 45 | } |
| 46 | } |
| 47 | |
| 48 | // limit this just so we don't take too long to draw |
| 49 | #define MAX_TOTAL_TEXT 4096 |
| 50 | #define MAX_CHAR 256 |
joshualitt | 11dfc8e | 2015-07-23 08:30:25 -0700 | [diff] [blame] | 51 | #define MAX_FAMILIES 30 |
| 52 | |
| 53 | static const int kWidth = 1024; |
| 54 | static const int kHeight = 768; |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 55 | |
| 56 | // This test hammers the GPU textblobcache and font atlas |
joshualitt | 404d9d6 | 2015-07-22 11:00:32 -0700 | [diff] [blame] | 57 | DEF_GPUTEST(TextBlobCache, reporter, factory) { |
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 | bdc91a7 | 2015-07-31 07:55:09 -0700 | [diff] [blame] | 62 | GrContext* ctx = factory->get(GrContextFactory::kNative_GLContextType); |
joshualitt | 11dfc8e | 2015-07-23 08:30:25 -0700 | [diff] [blame] | 63 | SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, kN32_SkColorType, kPremul_SkAlphaType); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 64 | SkAutoTUnref<SkSurface> surface(SkSurface::NewRenderTarget(ctx, SkSurface::kNo_Budgeted, info, |
| 65 | 0, &props)); |
| 66 | REPORTER_ASSERT(reporter, surface); |
| 67 | if (!surface) { |
| 68 | return; |
| 69 | } |
| 70 | |
| 71 | SkCanvas* canvas = surface->getCanvas(); |
| 72 | |
| 73 | SkAutoTUnref<SkFontMgr> fm(SkFontMgr::RefDefault()); |
| 74 | |
| 75 | int count = SkMin32(fm->countFamilies(), MAX_FAMILIES); |
| 76 | |
| 77 | // make a ton of text |
| 78 | uint16_t text[MAX_TOTAL_TEXT]; |
| 79 | for (int i = 0; i < MAX_TOTAL_TEXT; i++) { |
| 80 | text[i] = i % MAX_CHAR; |
| 81 | } |
| 82 | |
| 83 | // generate textblobs |
| 84 | SkTArray<TextBlobWrapper> blobs; |
| 85 | for (int i = 0; i < count; i++) { |
| 86 | SkPaint paint; |
| 87 | paint.setTextEncoding(SkPaint::kGlyphID_TextEncoding); |
joshualitt | 11dfc8e | 2015-07-23 08:30:25 -0700 | [diff] [blame] | 88 | paint.setTextSize(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); |
| 92 | SkAutoTUnref<SkFontStyleSet> set(fm->createStyleSet(i)); |
| 93 | for (int j = 0; j < set->count(); ++j) { |
| 94 | SkFontStyle fs; |
| 95 | set->getStyle(j, &fs, NULL); |
| 96 | |
joshualitt | bdc91a7 | 2015-07-31 07:55:09 -0700 | [diff] [blame] | 97 | SkSafeUnref(paint.setTypeface(set->createTypeface(j))); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 98 | |
| 99 | SkTextBlobBuilder builder; |
| 100 | for (int aa = 0; aa < 2; aa++) { |
| 101 | for (int subpixel = 0; subpixel < 2; subpixel++) { |
| 102 | for (int lcd = 0; lcd < 2; lcd++) { |
| 103 | paint.setAntiAlias(SkToBool(aa)); |
| 104 | paint.setSubpixelText(SkToBool(subpixel)); |
| 105 | paint.setLCDRenderText(SkToBool(lcd)); |
| 106 | const SkTextBlobBuilder::RunBuffer& run = builder.allocRun(paint, |
| 107 | MAX_TOTAL_TEXT, |
| 108 | 0, 0, |
| 109 | NULL); |
| 110 | memcpy(run.glyphs, text, MAX_TOTAL_TEXT * sizeof(uint16_t)); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | SkNEW_APPEND_TO_TARRAY(&blobs, TextBlobWrapper, (builder.build())); |
| 115 | } |
| 116 | } |
| 117 | |
joshualitt | 11dfc8e | 2015-07-23 08:30:25 -0700 | [diff] [blame] | 118 | // create surface where LCD is impossible |
| 119 | info = SkImageInfo::MakeN32Premul(kWidth, kHeight); |
| 120 | SkSurfaceProps propsNoLCD(0, kUnknown_SkPixelGeometry); |
| 121 | SkAutoTUnref<SkSurface> surfaceNoLCD(canvas->newSurface(info, &propsNoLCD)); |
| 122 | REPORTER_ASSERT(reporter, surface); |
| 123 | if (!surface) { |
| 124 | return; |
| 125 | } |
| 126 | |
| 127 | SkCanvas* canvasNoLCD = surfaceNoLCD->getCanvas(); |
| 128 | |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 129 | // test redraw |
| 130 | draw(canvas, 2, blobs); |
joshualitt | 11dfc8e | 2015-07-23 08:30:25 -0700 | [diff] [blame] | 131 | draw(canvasNoLCD, 2, blobs); |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 132 | |
| 133 | // test draw after free |
| 134 | ctx->freeGpuResources(); |
| 135 | draw(canvas, 1, blobs); |
| 136 | |
joshualitt | 11dfc8e | 2015-07-23 08:30:25 -0700 | [diff] [blame] | 137 | ctx->freeGpuResources(); |
| 138 | draw(canvasNoLCD, 1, blobs); |
| 139 | |
joshualitt | e49109f | 2015-07-17 12:47:39 -0700 | [diff] [blame] | 140 | // test draw after abandon |
| 141 | ctx->abandonContext(); |
| 142 | draw(canvas, 1, blobs); |
| 143 | } |
| 144 | #endif |