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 { |
| 23 | class TextBlobRandomFont : public GM { |
| 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 | |
| 35 | // make textbloben |
| 36 | SkPaint paint; |
| 37 | paint.setTextSize(32); |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 38 | paint.setAntiAlias(true); |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 39 | paint.setLCDRenderText(true); |
| 40 | |
| 41 | // Setup our random scaler context |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 42 | auto typeface = sk_tool_utils::create_portable_typeface("sans-serif", SkFontStyle::Bold()); |
| 43 | if (!typeface) { |
| 44 | typeface = SkTypeface::MakeDefault(); |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 45 | } |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 46 | paint.setColor(SK_ColorMAGENTA); |
| 47 | paint.setTypeface(sk_make_sp<SkRandomTypeface>(std::move(typeface), paint, false)); |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 48 | |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 49 | SkScalar y = 0; |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 50 | SkRect bounds; |
| 51 | paint.measureText(text, strlen(text), &bounds); |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 52 | y -= bounds.fTop; |
| 53 | sk_tool_utils::add_to_text_blob(&builder, text, paint, 0, y); |
| 54 | y += bounds.fBottom; |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 55 | |
| 56 | // A8 |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 57 | const char* bigtext1 = "The quick brown fox"; |
| 58 | const char* bigtext2 = "jumps over the lazy dog."; |
| 59 | paint.setTextSize(160); |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 60 | paint.setSubpixelText(false); |
| 61 | paint.setLCDRenderText(false); |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 62 | paint.measureText(bigtext1, strlen(bigtext1), &bounds); |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 63 | y -= bounds.fTop; |
| 64 | sk_tool_utils::add_to_text_blob(&builder, bigtext1, paint, 0, y); |
| 65 | y += bounds.fBottom; |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 66 | |
| 67 | paint.measureText(bigtext2, strlen(bigtext2), &bounds); |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 68 | y -= bounds.fTop; |
| 69 | sk_tool_utils::add_to_text_blob(&builder, bigtext2, paint, 0, y); |
| 70 | y += bounds.fBottom; |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 71 | |
| 72 | // color emoji |
Mike Klein | b251b72 | 2017-11-13 11:03:16 -0500 | [diff] [blame] | 73 | if (sk_sp<SkTypeface> origEmoji = sk_tool_utils::emoji_typeface()) { |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 74 | paint.setTypeface(sk_make_sp<SkRandomTypeface>(origEmoji, paint, false)); |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 75 | const char* emojiText = sk_tool_utils::emoji_sample_text(); |
| 76 | paint.measureText(emojiText, strlen(emojiText), &bounds); |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 77 | y -= bounds.fTop; |
| 78 | sk_tool_utils::add_to_text_blob(&builder, emojiText, paint, 0, y); |
| 79 | y += bounds.fBottom; |
joshualitt | d45fb5a | 2015-08-01 10:33:40 -0700 | [diff] [blame] | 80 | } |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 81 | |
| 82 | // build |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 83 | fBlob = builder.make(); |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 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. |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 96 | // This GM uses sk_tool_utils::makeSurface which doesn't work well with vias. |
| 97 | // This GM uses SkRandomTypeface which doesn't work well with serialization. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 98 | if (nullptr == canvas->getGrContext()) { |
halcanary | 2a24338 | 2015-09-09 08:16:41 -0700 | [diff] [blame] | 99 | skiagm::GM::DrawGpuOnlyMessage(canvas); |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 100 | return; |
| 101 | } |
| 102 | |
| 103 | canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorWHITE)); |
| 104 | |
Brian Osman | f941280 | 2016-12-02 12:07:17 -0500 | [diff] [blame] | 105 | SkImageInfo info = SkImageInfo::Make(kWidth, kHeight, canvas->imageInfo().colorType(), |
| 106 | kPremul_SkAlphaType, |
Mike Reed | 693fdbd | 2017-01-12 10:13:40 -0500 | [diff] [blame] | 107 | canvas->imageInfo().refColorSpace()); |
brianosman | 3a0dbde | 2016-07-26 11:36:05 -0700 | [diff] [blame] | 108 | SkSurfaceProps props(0, kUnknown_SkPixelGeometry); |
Mike Reed | 46596ae | 2018-01-02 15:40:29 -0500 | [diff] [blame] | 109 | auto surface(sk_tool_utils::makeSurface(canvas, info, &props)); |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 110 | if (!surface) { |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 111 | const char* text = "This test requires a surface"; |
| 112 | size_t len = strlen(text); |
| 113 | SkPaint paint; |
| 114 | canvas->drawText(text, len, 10, 100, paint); |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 115 | return; |
| 116 | } |
| 117 | |
| 118 | SkPaint paint; |
| 119 | paint.setAntiAlias(true); |
| 120 | |
Jim Van Verth | 30f64e4 | 2018-05-24 14:32:26 -0400 | [diff] [blame] | 121 | SkCanvas* surfaceCanvas = surface->getCanvas(); |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 122 | |
| 123 | SkScalar stride = SkScalarCeilToScalar(fBlob->bounds().height()); |
| 124 | SkScalar yOffset = 5; |
Ben Wagner | 20fa1e9 | 2018-04-30 15:39:15 -0400 | [diff] [blame] | 125 | |
Jim Van Verth | 30f64e4 | 2018-05-24 14:32:26 -0400 | [diff] [blame] | 126 | canvas->save(); |
| 127 | // Originally we would alternate between rotating and not to force blob regeneration, |
| 128 | // but that code seems to have rotted. Keeping the rotate to match the old GM as |
| 129 | // much as possible, and it seems like a reasonable stress test for transformed |
| 130 | // color emoji. |
| 131 | canvas->rotate(-0.05f); |
| 132 | canvas->drawTextBlob(fBlob, 10, yOffset, paint); |
| 133 | yOffset += stride; |
| 134 | canvas->restore(); |
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 | // this will test lcd masks when not requested |
| 137 | // on cpu this currently causes unspecified behavior, so avoid until it is fixed |
| 138 | if (canvas->getGrContext()) { |
| 139 | // Rotate in the surface canvas, not the final canvas, to avoid aliasing |
| 140 | surfaceCanvas->rotate(-0.05f); |
| 141 | surfaceCanvas->drawTextBlob(fBlob, 10, yOffset, paint); |
| 142 | surface->draw(canvas, 0, 0, nullptr); |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 143 | } |
Jim Van Verth | 30f64e4 | 2018-05-24 14:32:26 -0400 | [diff] [blame] | 144 | yOffset += stride; |
| 145 | |
| 146 | // free gpu resources and verify |
| 147 | if (canvas->getGrContext()) { |
| 148 | canvas->getGrContext()->freeGpuResources(); |
| 149 | } |
| 150 | |
| 151 | canvas->rotate(-0.05f); |
| 152 | canvas->drawTextBlob(fBlob, 10, yOffset, paint); |
| 153 | yOffset += stride; |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | private: |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 157 | sk_sp<SkTextBlob> fBlob; |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 158 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 159 | static constexpr int kWidth = 2000; |
| 160 | static constexpr int kHeight = 1600; |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 161 | |
| 162 | typedef GM INHERITED; |
| 163 | }; |
| 164 | |
| 165 | ////////////////////////////////////////////////////////////////////////////// |
| 166 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 167 | DEF_GM(return new TextBlobRandomFont;) |
joshualitt | 44c4851 | 2015-08-01 07:33:41 -0700 | [diff] [blame] | 168 | } |