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