joshualitt | 44c4851 | 2015-08-01 07:33:41 -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 "gm.h" |
| 9 | |
| 10 | #include "Resources.h" |
| 11 | #include "SkCanvas.h" |
| 12 | #include "SkGradientShader.h" |
| 13 | #include "SkStream.h" |
| 14 | #include "SkSurface.h" |
| 15 | #include "SkTextBlob.h" |
| 16 | #include "SkTypeface.h" |
| 17 | #include "../src/fonts/SkRandomScalerContext.h" |
| 18 | |
| 19 | #if SK_SUPPORT_GPU |
| 20 | |
| 21 | #include "GrContext.h" |
| 22 | |
| 23 | namespace skiagm { |
| 24 | class TextBlobRandomFont : public GM { |
| 25 | public: |
| 26 | // This gm tests that textblobs can be translated and scaled with a font that returns random |
| 27 | // but deterministic masks |
| 28 | TextBlobRandomFont() { } |
| 29 | |
| 30 | protected: |
| 31 | void onOnceBeforeDraw() override { |
| 32 | SkTextBlobBuilder builder; |
| 33 | |
| 34 | const char* text = "The quick brown fox jumps over the lazy dog."; |
| 35 | |
| 36 | // make textbloben |
| 37 | SkPaint paint; |
| 38 | paint.setTextSize(32); |
| 39 | paint.setLCDRenderText(true); |
| 40 | |
| 41 | // Setup our random scaler context |
| 42 | SkAutoTUnref<SkTypeface> orig(sk_tool_utils::create_portable_typeface("sans-serif", |
| 43 | SkTypeface::kBold)); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 44 | if (nullptr == orig) { |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 45 | orig.reset(SkTypeface::RefDefault()); |
| 46 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 47 | SkAutoTUnref<SkTypeface> random(new SkRandomTypeface(orig, paint, false)); |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 48 | paint.setTypeface(random); |
| 49 | |
| 50 | SkRect bounds; |
| 51 | paint.measureText(text, strlen(text), &bounds); |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 52 | sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, 0); |
| 53 | |
| 54 | // A8 |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 55 | const char* bigtext1 = "The quick brown fox"; |
| 56 | const char* bigtext2 = "jumps over the lazy dog."; |
| 57 | paint.setTextSize(160); |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 58 | paint.setSubpixelText(false); |
| 59 | paint.setLCDRenderText(false); |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 60 | paint.measureText(bigtext1, strlen(bigtext1), &bounds); |
| 61 | SkScalar offset = bounds.height(); |
| 62 | sk_tool_utils::add_to_text_blob(&builder, bigtext1, paint, 0, offset); |
| 63 | |
| 64 | paint.measureText(bigtext2, strlen(bigtext2), &bounds); |
| 65 | offset += bounds.height(); |
| 66 | sk_tool_utils::add_to_text_blob(&builder, bigtext2, paint, 0, offset); |
| 67 | |
| 68 | // color emoji |
| 69 | SkAutoTUnref<SkTypeface> origEmoji; |
| 70 | sk_tool_utils::emoji_typeface(&origEmoji); |
| 71 | const char* osName = sk_tool_utils::platform_os_name(); |
| 72 | // The mac emoji string will break us |
| 73 | if (origEmoji && (!strcmp(osName, "Android") || !strcmp(osName, "Ubuntu"))) { |
| 74 | const char* emojiText = sk_tool_utils::emoji_sample_text(); |
| 75 | paint.measureText(emojiText, strlen(emojiText), &bounds); |
| 76 | offset += bounds.height(); |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 77 | SkAutoTUnref<SkTypeface> randomEmoji(new SkRandomTypeface(orig, paint, false)); |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 78 | paint.setTypeface(randomEmoji); |
| 79 | sk_tool_utils::add_to_text_blob(&builder, emojiText, paint, 0, offset); |
| 80 | } |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 81 | |
| 82 | // build |
| 83 | fBlob.reset(builder.build()); |
| 84 | } |
| 85 | |
| 86 | SkString onShortName() override { |
| 87 | return SkString("textblobrandomfont"); |
| 88 | } |
| 89 | |
| 90 | SkISize onISize() override { |
| 91 | return SkISize::Make(kWidth, kHeight); |
| 92 | } |
| 93 | |
| 94 | void onDraw(SkCanvas* canvas) override { |
| 95 | // This GM exists to test a specific feature of the GPU backend. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 96 | if (nullptr == canvas->getGrContext()) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 97 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 98 | return; |
| 99 | } |
| 100 | |
| 101 | canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorWHITE)); |
| 102 | |
brianosman | 898235c | 2016-04-06 07:38:23 -0700 | [diff] [blame] | 103 | SkImageInfo info = SkImageInfo::MakeN32Premul(kWidth, kHeight, |
| 104 | canvas->imageInfo().profileType()); |
| 105 | SkSurfaceProps canvasProps(SkSurfaceProps::kLegacyFontHost_InitType); |
brianosman | b461d34 | 2016-04-13 13:10:14 -0700 | [diff] [blame] | 106 | uint32_t gammaCorrect = canvas->getProps(&canvasProps) |
| 107 | ? canvasProps.flags() & SkSurfaceProps::kGammaCorrect_Flag : 0; |
| 108 | SkSurfaceProps props(gammaCorrect, kUnknown_SkPixelGeometry); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 109 | auto surface(canvas->makeSurface(info, &props)); |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 110 | if (surface) { |
| 111 | SkPaint paint; |
| 112 | paint.setAntiAlias(true); |
| 113 | |
| 114 | SkCanvas* c = surface->getCanvas(); |
| 115 | |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 116 | int stride = SkScalarCeilToInt(fBlob->bounds().height()); |
| 117 | int yOffset = stride / 8; |
| 118 | for (int i = 0; i < 1; i++) { |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 119 | // fiddle the canvas to force regen of textblobs |
| 120 | canvas->rotate(i % 2 ? 0.0f : -0.05f); |
| 121 | canvas->drawTextBlob(fBlob, 10.0f, SkIntToScalar(yOffset), paint); |
| 122 | yOffset += stride; |
| 123 | |
| 124 | // This will draw as black boxes |
| 125 | c->drawTextBlob(fBlob, 10, SkIntToScalar(yOffset), paint); |
| 126 | surface->draw(canvas, 0, 0, nullptr); |
| 127 | |
| 128 | // free gpu resources and verify |
| 129 | yOffset += stride; |
| 130 | canvas->getGrContext()->freeGpuResources(); |
| 131 | canvas->drawTextBlob(fBlob, 10, SkIntToScalar(yOffset), paint); |
| 132 | |
| 133 | yOffset += stride; |
| 134 | } |
| 135 | |
| 136 | } else { |
| 137 | const char* text = "This test requires a surface"; |
| 138 | size_t len = strlen(text); |
| 139 | SkPaint paint; |
| 140 | canvas->drawText(text, len, 10, 100, paint); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | private: |
| 145 | SkAutoTUnref<const SkTextBlob> fBlob; |
| 146 | |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 147 | static const int kWidth = 2000; |
| 148 | static const int kHeight = 1600; |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 149 | |
| 150 | typedef GM INHERITED; |
| 151 | }; |
| 152 | |
| 153 | ////////////////////////////////////////////////////////////////////////////// |
| 154 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 155 | DEF_GM(return new TextBlobRandomFont;) |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 156 | } |
| 157 | #endif |