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