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" |
Mike Klein | 33d2055 | 2017-03-22 13:47:51 -0400 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 10 | |
| 11 | #include "Resources.h" |
| 12 | #include "SkCanvas.h" |
| 13 | #include "SkGradientShader.h" |
Mike Klein | 10d66cc | 2017-11-10 11:33:43 -0500 | [diff] [blame] | 14 | #include "SkRandomScalerContext.h" |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 15 | #include "SkStream.h" |
| 16 | #include "SkSurface.h" |
| 17 | #include "SkTextBlob.h" |
| 18 | #include "SkTypeface.h" |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 19 | |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 20 | #include "GrContext.h" |
| 21 | |
| 22 | namespace skiagm { |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 23 | class TextBlobRandomFont : public GpuGM { |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 24 | public: |
| 25 | // This gm tests that textblobs can be translated and scaled with a font that returns random |
| 26 | // but deterministic masks |
| 27 | TextBlobRandomFont() { } |
| 28 | |
| 29 | protected: |
| 30 | void onOnceBeforeDraw() override { |
| 31 | SkTextBlobBuilder builder; |
| 32 | |
| 33 | const char* text = "The quick brown fox jumps over the lazy dog."; |
| 34 | |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 35 | SkPaint paint; |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 36 | paint.setAntiAlias(true); |
Mike Reed | ea8900e | 2018-12-22 17:37:30 -0500 | [diff] [blame] | 37 | paint.setColor(SK_ColorMAGENTA); |
| 38 | |
| 39 | // make textbloben |
| 40 | SkFont font; |
| 41 | font.setSize(32); |
| 42 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 43 | |
| 44 | // Setup our random scaler context |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 45 | auto typeface = sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Bold()); |
| 46 | if (!typeface) { |
| 47 | typeface = SkTypeface::MakeDefault(); |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 48 | } |
Mike Reed | ea8900e | 2018-12-22 17:37:30 -0500 | [diff] [blame] | 49 | font.setTypeface(sk_make_sp<SkRandomTypeface>(std::move(typeface), paint, false)); |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 50 | |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 51 | SkScalar y = 0; |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 52 | SkRect bounds; |
Mike Reed | ea8900e | 2018-12-22 17:37:30 -0500 | [diff] [blame] | 53 | font.measureText(text, strlen(text), kUTF8_SkTextEncoding, &bounds); |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 54 | y -= bounds.fTop; |
Mike Reed | ea8900e | 2018-12-22 17:37:30 -0500 | [diff] [blame] | 55 | sk_tool_utils::add_to_text_blob(&builder, text, font, 0, y); |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 56 | y += bounds.fBottom; |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 57 | |
| 58 | // A8 |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 59 | const char* bigtext1 = "The quick brown fox"; |
| 60 | const char* bigtext2 = "jumps over the lazy dog."; |
Mike Reed | ea8900e | 2018-12-22 17:37:30 -0500 | [diff] [blame] | 61 | font.setSize(160); |
| 62 | font.setSubpixel(false); |
| 63 | font.setEdging(SkFont::Edging::kAntiAlias); |
| 64 | font.measureText(bigtext1, strlen(bigtext1), kUTF8_SkTextEncoding, &bounds); |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 65 | y -= bounds.fTop; |
Mike Reed | ea8900e | 2018-12-22 17:37:30 -0500 | [diff] [blame] | 66 | sk_tool_utils::add_to_text_blob(&builder, bigtext1, font, 0, y); |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 67 | y += bounds.fBottom; |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 68 | |
Mike Reed | ea8900e | 2018-12-22 17:37:30 -0500 | [diff] [blame] | 69 | font.measureText(bigtext2, strlen(bigtext2), kUTF8_SkTextEncoding, &bounds); |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 70 | y -= bounds.fTop; |
Mike Reed | ea8900e | 2018-12-22 17:37:30 -0500 | [diff] [blame] | 71 | sk_tool_utils::add_to_text_blob(&builder, bigtext2, font, 0, y); |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 72 | y += bounds.fBottom; |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 73 | |
| 74 | // color emoji |
Mike Klein | b251b72 | 2017-11-13 11:03:16 -0500 | [diff] [blame] | 75 | if (sk_sp<SkTypeface> origEmoji = sk_tool_utils::emoji_typeface()) { |
Mike Reed | ea8900e | 2018-12-22 17:37:30 -0500 | [diff] [blame] | 76 | font.setTypeface(sk_make_sp<SkRandomTypeface>(origEmoji, paint, false)); |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 77 | const char* emojiText = sk_tool_utils::emoji_sample_text(); |
Mike Reed | ea8900e | 2018-12-22 17:37:30 -0500 | [diff] [blame] | 78 | font.measureText(emojiText, strlen(emojiText), kUTF8_SkTextEncoding, &bounds); |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 79 | y -= bounds.fTop; |
Mike Reed | ea8900e | 2018-12-22 17:37:30 -0500 | [diff] [blame] | 80 | sk_tool_utils::add_to_text_blob(&builder, emojiText, font, 0, y); |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 81 | y += bounds.fBottom; |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 82 | } |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 83 | |
| 84 | // build |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 85 | fBlob = builder.make(); |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | SkString onShortName() override { |
| 89 | return SkString("textblobrandomfont"); |
| 90 | } |
| 91 | |
| 92 | SkISize onISize() override { |
| 93 | return SkISize::Make(kWidth, kHeight); |
| 94 | } |
| 95 | |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 96 | DrawResult onDraw(GrContext* context, GrRenderTargetContext*, SkCanvas* canvas, |
| 97 | SkString* errorMsg) override { |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 98 | // This GM exists to test a specific feature of the GPU backend. |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 99 | // This GM uses sk_tool_utils::makeSurface which doesn't work well with vias. |
| 100 | // This GM uses SkRandomTypeface which doesn't work well with serialization. |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 101 | canvas->drawColor(SK_ColorWHITE); |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 102 | |
Brian Osman | f941280 | 2016-12-02 12:07:17 -0500 | [diff] [blame] | 103 | SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, canvas->imageInfo().colorType(), |
| 104 | kPremul_SkAlphaType, |
Mike Reed | 693fdbd | 2017-01-12 10:13:40 -0500 | [diff] [blame] | 105 | canvas->imageInfo().refColorSpace()); |
brianosman | 3a0dbde | 2016-07-26 11:36:05 -0700 | [diff] [blame] | 106 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
Cary Clark | a24712e | 2018-09-05 18:41:40 +0000 | [diff] [blame] | 107 | auto surface(sk_tool_utils::makeSurface(canvas, info, &props)); |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 108 | if (!surface) { |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 109 | *errorMsg = "This test requires a surface"; |
| 110 | return DrawResult::kFail; |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | SkPaint paint; |
| 114 | paint.setAntiAlias(true); |
| 115 | |
Jim Van Verth | 30f64e4 | 2018-05-24 14:32:26 -0400 | [diff] [blame] | 116 | SkCanvas* surfaceCanvas = surface->getCanvas(); |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 117 | |
| 118 | SkScalar stride = SkScalarCeilToScalar(fBlob->bounds().height()); |
| 119 | SkScalar yOffset = 5; |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 120 | |
Jim Van Verth | 30f64e4 | 2018-05-24 14:32:26 -0400 | [diff] [blame] | 121 | canvas->save(); |
| 122 | // Originally we would alternate between rotating and not to force blob regeneration, |
| 123 | // but that code seems to have rotted. Keeping the rotate to match the old GM as |
| 124 | // much as possible, and it seems like a reasonable stress test for transformed |
| 125 | // color emoji. |
| 126 | canvas->rotate(-0.05f); |
| 127 | canvas->drawTextBlob(fBlob, 10, yOffset, paint); |
| 128 | yOffset += stride; |
| 129 | canvas->restore(); |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 130 | |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 131 | // Rotate in the surface canvas, not the final canvas, to avoid aliasing |
| 132 | surfaceCanvas->rotate(-0.05f); |
| 133 | surfaceCanvas->drawTextBlob(fBlob, 10, yOffset, paint); |
| 134 | surface->draw(canvas, 0, 0, nullptr); |
Jim Van Verth | 30f64e4 | 2018-05-24 14:32:26 -0400 | [diff] [blame] | 135 | yOffset += stride; |
| 136 | |
| 137 | // free gpu resources and verify |
Chris Dalton | 3a77837 | 2019-02-07 15:23:36 -0700 | [diff] [blame] | 138 | context->freeGpuResources(); |
Jim Van Verth | 30f64e4 | 2018-05-24 14:32:26 -0400 | [diff] [blame] | 139 | |
| 140 | canvas->rotate(-0.05f); |
| 141 | canvas->drawTextBlob(fBlob, 10, yOffset, paint); |
| 142 | yOffset += stride; |
Chris Dalton | 50e24d7 | 2019-02-07 16:20:09 -0700 | [diff] [blame] | 143 | return DrawResult::kOk; |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | private: |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 147 | sk_sp<SkTextBlob> fBlob; |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 148 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 149 | static constexpr int kWidth = 2000; |
| 150 | static constexpr int kHeight = 1600; |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 151 | |
| 152 | typedef GM INHERITED; |
| 153 | }; |
| 154 | |
| 155 | ////////////////////////////////////////////////////////////////////////////// |
| 156 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 157 | DEF_GM(return new TextBlobRandomFont;) |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 158 | } |