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