joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 9 | #include "include/core/SkBlendMode.h" |
| 10 | #include "include/core/SkBlurTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 11 | #include "include/core/SkCanvas.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 12 | #include "include/core/SkColor.h" |
| 13 | #include "include/core/SkColorFilter.h" |
| 14 | #include "include/core/SkDrawLooper.h" |
| 15 | #include "include/core/SkFont.h" |
| 16 | #include "include/core/SkFontTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "include/core/SkMaskFilter.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 18 | #include "include/core/SkMatrix.h" |
| 19 | #include "include/core/SkPaint.h" |
| 20 | #include "include/core/SkPath.h" |
| 21 | #include "include/core/SkPathEffect.h" |
| 22 | #include "include/core/SkPoint.h" |
| 23 | #include "include/core/SkRect.h" |
| 24 | #include "include/core/SkRefCnt.h" |
| 25 | #include "include/core/SkScalar.h" |
| 26 | #include "include/core/SkShader.h" |
| 27 | #include "include/core/SkSize.h" |
| 28 | #include "include/core/SkString.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 29 | #include "include/core/SkTextBlob.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 30 | #include "include/core/SkTileMode.h" |
| 31 | #include "include/core/SkTypeface.h" |
| 32 | #include "include/core/SkTypes.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 33 | #include "include/effects/Sk2DPathEffect.h" |
| 34 | #include "include/effects/SkColorMatrixFilter.h" |
| 35 | #include "include/effects/SkGradientShader.h" |
| 36 | #include "include/effects/SkLayerDrawLooper.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 37 | #include "include/private/SkTArray.h" |
| 38 | #include "include/private/SkTDArray.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 39 | #include "src/core/SkBlurMask.h" |
Ben Wagner | 7fde8e1 | 2019-05-01 17:28:53 -0400 | [diff] [blame] | 40 | #include "tools/ToolUtils.h" |
| 41 | |
| 42 | #include <string.h> |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 43 | |
| 44 | namespace skiagm { |
| 45 | |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 46 | constexpr int kWidth = 1250; |
| 47 | constexpr int kHeight = 700; |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 48 | |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 49 | // Unlike the variant in ToolUtils, this version positions the glyphs on a diagonal |
Mike Reed | 2ed7820 | 2018-11-21 15:10:08 -0500 | [diff] [blame] | 50 | static void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkFont& font, |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 51 | SkScalar x, SkScalar y) { |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 52 | SkTDArray<uint16_t> glyphs; |
| 53 | |
| 54 | size_t len = strlen(text); |
Ben Wagner | 51e15a6 | 2019-05-07 15:38:46 -0400 | [diff] [blame] | 55 | glyphs.append(font.countText(text, len, SkTextEncoding::kUTF8)); |
| 56 | font.textToGlyphs(text, len, SkTextEncoding::kUTF8, glyphs.begin(), glyphs.count()); |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 57 | |
Mike Reed | 2ed7820 | 2018-11-21 15:10:08 -0500 | [diff] [blame] | 58 | const SkScalar advanceX = font.getSize() * 0.85f; |
| 59 | const SkScalar advanceY = font.getSize() * 1.5f; |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 60 | |
| 61 | SkTDArray<SkScalar> pos; |
| 62 | for (unsigned i = 0; i < len; ++i) { |
| 63 | *pos.append() = x + i * advanceX; |
| 64 | *pos.append() = y + i * (advanceY / len); |
| 65 | } |
Mike Reed | 2ed7820 | 2018-11-21 15:10:08 -0500 | [diff] [blame] | 66 | const SkTextBlobBuilder::RunBuffer& run = builder->allocRunPos(font, glyphs.count()); |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 67 | memcpy(run.glyphs, glyphs.begin(), glyphs.count() * sizeof(uint16_t)); |
| 68 | memcpy(run.pos, pos.begin(), len * sizeof(SkScalar) * 2); |
| 69 | } |
| 70 | |
| 71 | typedef void (*LooperProc)(SkPaint*); |
| 72 | |
| 73 | struct LooperSettings { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 74 | SkBlendMode fMode; |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 75 | SkColor fColor; |
| 76 | SkPaint::Style fStyle; |
| 77 | SkScalar fWidth; |
| 78 | SkScalar fOffset; |
| 79 | SkScalar fSkewX; |
| 80 | bool fEffect; |
| 81 | }; |
| 82 | |
| 83 | static void mask_filter(SkPaint* paint) { |
Mike Reed | 1be1f8d | 2018-03-14 13:01:17 -0400 | [diff] [blame] | 84 | paint->setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle, |
reed | efdfd51 | 2016-04-04 10:02:58 -0700 | [diff] [blame] | 85 | SkBlurMask::ConvertRadiusToSigma(3.f))); |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 86 | } |
| 87 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 88 | static sk_sp<SkPathEffect> make_tile_effect() { |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 89 | SkMatrix m; |
| 90 | m.setScale(1.f, 1.f); |
| 91 | |
| 92 | SkPath path; |
| 93 | path.addCircle(0, 0, SkIntToScalar(5)); |
| 94 | |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 95 | return SkPath2DPathEffect::Make(m, path); |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static void path_effect(SkPaint* paint) { |
reed | a439334 | 2016-03-18 11:22:57 -0700 | [diff] [blame] | 99 | paint->setPathEffect(make_tile_effect()); |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 100 | } |
| 101 | |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 102 | static sk_sp<SkShader> make_shader(const SkRect& bounds) { |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 103 | const SkPoint pts[] = { |
| 104 | { bounds.left(), bounds.top() }, |
| 105 | { bounds.right(), bounds.bottom() }, |
| 106 | }; |
| 107 | const SkColor colors[] = { |
| 108 | SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorBLACK, |
| 109 | SK_ColorCYAN, SK_ColorMAGENTA, SK_ColorYELLOW, |
| 110 | }; |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 111 | return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors), |
Mike Reed | fae8fce | 2019-04-03 10:27:45 -0400 | [diff] [blame] | 112 | SkTileMode::kClamp); |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | static void color_filter(SkPaint* paint) { |
| 116 | SkRect r; |
| 117 | r.setWH(SkIntToScalar(kWidth), 50); |
reed | 1a9b964 | 2016-03-13 14:13:58 -0700 | [diff] [blame] | 118 | paint->setShader(make_shader(r)); |
reed | d053ce9 | 2016-03-22 10:17:23 -0700 | [diff] [blame] | 119 | paint->setColorFilter(SkColorMatrixFilter::MakeLightingFilter(0xF0F0F0, 0)); |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | static void kitchen_sink(SkPaint* paint) { |
| 123 | color_filter(paint); |
| 124 | path_effect(paint); |
| 125 | mask_filter(paint); |
| 126 | |
| 127 | } |
| 128 | |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 129 | static sk_sp<SkDrawLooper> setupLooper(SkLayerDrawLooper::BitFlags bits, |
| 130 | LooperProc proc, |
| 131 | const LooperSettings settings[], |
| 132 | size_t size) { |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 133 | SkLayerDrawLooper::Builder looperBuilder; |
| 134 | |
| 135 | SkLayerDrawLooper::LayerInfo info; |
| 136 | info.fPaintBits = bits; |
| 137 | |
Mike Reed | faba371 | 2016-11-03 14:45:31 -0400 | [diff] [blame] | 138 | info.fColorMode = SkBlendMode::kSrc; |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 139 | |
| 140 | for (size_t i = 0; i < size; i++) { |
| 141 | info.fOffset.set(settings[i].fOffset, settings[i].fOffset); |
| 142 | SkPaint* paint = looperBuilder.addLayer(info); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 143 | paint->setBlendMode(settings[i].fMode); |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 144 | paint->setColor(settings[i].fColor); |
| 145 | paint->setStyle(settings[i].fStyle); |
| 146 | paint->setStrokeWidth(settings[i].fWidth); |
| 147 | if (settings[i].fEffect) { |
| 148 | (*proc)(paint); |
| 149 | } |
| 150 | } |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 151 | return looperBuilder.detach(); |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 152 | } |
| 153 | |
| 154 | class TextBlobLooperGM : public GM { |
| 155 | public: |
| 156 | TextBlobLooperGM() {} |
| 157 | |
| 158 | protected: |
| 159 | void onOnceBeforeDraw() override { |
| 160 | SkTextBlobBuilder builder; |
| 161 | |
| 162 | // LCD |
Mike Reed | 2ed7820 | 2018-11-21 15:10:08 -0500 | [diff] [blame] | 163 | SkFont font; |
| 164 | font.setSize(32); |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 165 | const char* text = "The quick brown fox jumps over the lazy dog"; |
Mike Reed | 2ed7820 | 2018-11-21 15:10:08 -0500 | [diff] [blame] | 166 | font.setSubpixel(true); |
| 167 | font.setEdging(SkFont::Edging::kSubpixelAntiAlias); |
Mike Klein | ea3f014 | 2019-03-20 11:12:10 -0500 | [diff] [blame] | 168 | font.setTypeface(ToolUtils::create_portable_typeface()); |
Mike Reed | 2ed7820 | 2018-11-21 15:10:08 -0500 | [diff] [blame] | 169 | add_to_text_blob(&builder, text, font, 0, 0); |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 170 | fBlob = builder.make(); |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 171 | |
| 172 | // create a looper which sandwhiches an effect in two normal draws |
| 173 | LooperSettings looperSandwhich[] = { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 174 | { SkBlendMode::kSrc, SK_ColorMAGENTA, SkPaint::kFill_Style, 0, 0, 0, false }, |
| 175 | { SkBlendMode::kSrcOver, 0x88000000, SkPaint::kFill_Style, 0, 10.f, 0, true }, |
| 176 | { SkBlendMode::kSrcOver, 0x50FF00FF, SkPaint::kFill_Style, 0, 20.f, 0, false }, |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | LooperSettings compound[] = { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 180 | { SkBlendMode::kSrc, SK_ColorWHITE, SkPaint::kStroke_Style, 1.f * 3/4, 0, 0, false }, |
| 181 | { SkBlendMode::kSrc, SK_ColorRED, SkPaint::kStroke_Style, 4.f, 0, 0, false }, |
| 182 | { SkBlendMode::kSrc, SK_ColorBLUE, SkPaint::kFill_Style, 0, 0, 0, false }, |
| 183 | { SkBlendMode::kSrcOver, 0x88000000, SkPaint::kFill_Style, 0, 10.f, 0, true } |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 184 | }; |
| 185 | |
| 186 | LooperSettings xfermode[] = { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 187 | { SkBlendMode::kDifference, SK_ColorWHITE, SkPaint::kFill_Style, 0, 0, 0, false }, |
| 188 | { SkBlendMode::kSrcOver, 0xFF000000, SkPaint::kFill_Style, 0, 1.f, 0, true }, |
| 189 | { SkBlendMode::kSrcOver, 0x50FF00FF, SkPaint::kFill_Style, 0, 2.f, 0, false }, |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 190 | }; |
| 191 | |
| 192 | // NOTE, this should be ignored by textblobs |
| 193 | LooperSettings skew[] = { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 194 | { SkBlendMode::kSrc, SK_ColorRED, SkPaint::kFill_Style, 0, 0, -1.f, false }, |
| 195 | { SkBlendMode::kSrc, SK_ColorGREEN, SkPaint::kFill_Style, 0, 10.f, -1.f, false }, |
| 196 | { SkBlendMode::kSrc, SK_ColorBLUE, SkPaint::kFill_Style, 0, 20.f, -1.f, false }, |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 197 | }; |
| 198 | |
| 199 | LooperSettings kitchenSink[] = { |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 200 | { SkBlendMode::kSrc, SK_ColorWHITE, SkPaint::kStroke_Style, 1.f * 3/4, 0, 0, false }, |
| 201 | { SkBlendMode::kSrc, SK_ColorBLACK, SkPaint::kFill_Style, 0, 0, 0, false }, |
| 202 | { SkBlendMode::kDifference, SK_ColorWHITE, SkPaint::kFill_Style, 1.f, 10.f, 0, false }, |
| 203 | { SkBlendMode::kSrc, SK_ColorWHITE, SkPaint::kFill_Style, 0, 10.f, 0, true }, |
| 204 | { SkBlendMode::kSrcOver, 0x50FF00FF, SkPaint::kFill_Style, 0, 20.f, 0, false }, |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 205 | }; |
| 206 | |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 207 | fLoopers.push_back(setupLooper(SkLayerDrawLooper::kMaskFilter_Bit | |
| 208 | SkLayerDrawLooper::kXfermode_Bit | |
| 209 | SkLayerDrawLooper::kStyle_Bit, &mask_filter, |
| 210 | compound, SK_ARRAY_COUNT(compound))); |
| 211 | fLoopers.push_back(setupLooper(SkLayerDrawLooper::kPathEffect_Bit | |
| 212 | SkLayerDrawLooper::kXfermode_Bit, &path_effect, |
| 213 | looperSandwhich, SK_ARRAY_COUNT(looperSandwhich))); |
| 214 | fLoopers.push_back(setupLooper(SkLayerDrawLooper::kShader_Bit | |
| 215 | SkLayerDrawLooper::kColorFilter_Bit | |
| 216 | SkLayerDrawLooper::kXfermode_Bit, &color_filter, |
| 217 | looperSandwhich, SK_ARRAY_COUNT(looperSandwhich))); |
| 218 | fLoopers.push_back(setupLooper(SkLayerDrawLooper::kShader_Bit | |
| 219 | SkLayerDrawLooper::kColorFilter_Bit | |
| 220 | SkLayerDrawLooper::kXfermode_Bit, &color_filter, |
| 221 | xfermode, SK_ARRAY_COUNT(xfermode))); |
| 222 | fLoopers.push_back(setupLooper(0, nullptr, skew, SK_ARRAY_COUNT(skew))); |
| 223 | fLoopers.push_back(setupLooper(SkLayerDrawLooper::kMaskFilter_Bit | |
| 224 | SkLayerDrawLooper::kShader_Bit | |
| 225 | SkLayerDrawLooper::kColorFilter_Bit | |
| 226 | SkLayerDrawLooper::kPathEffect_Bit | |
| 227 | SkLayerDrawLooper::kStyle_Bit | |
| 228 | SkLayerDrawLooper::kXfermode_Bit, &kitchen_sink, |
| 229 | kitchenSink, SK_ARRAY_COUNT(kitchenSink))); |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 230 | |
| 231 | // Test we respect overrides |
reed | 7b380d0 | 2016-03-21 13:25:16 -0700 | [diff] [blame] | 232 | fLoopers.push_back(setupLooper(0, &kitchen_sink, |
| 233 | kitchenSink, SK_ARRAY_COUNT(kitchenSink))); |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | SkString onShortName() override { |
| 237 | return SkString("textbloblooper"); |
| 238 | } |
| 239 | |
| 240 | SkISize onISize() override { |
| 241 | return SkISize::Make(kWidth, kHeight); |
| 242 | } |
| 243 | |
| 244 | void onDraw(SkCanvas* canvas) override { |
| 245 | |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 246 | canvas->drawColor(SK_ColorGRAY); |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 247 | |
| 248 | SkPaint paint; |
| 249 | canvas->translate(10, 40); |
| 250 | |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 251 | SkRect bounds = fBlob->bounds(); |
| 252 | |
| 253 | int y = 0; |
| 254 | for (int looper = 0; looper < fLoopers.count(); looper++) { |
Mike Reed | c9f0c59 | 2019-07-20 20:14:56 -0400 | [diff] [blame] | 255 | SkTextBlob* b = fBlob.get(); |
| 256 | canvas->save(); |
| 257 | canvas->translate(0, SkIntToScalar(y)); |
| 258 | fLoopers[looper]->apply(canvas, paint, [b](SkCanvas* c, const SkPaint& p) { |
| 259 | c->drawTextBlob(b, 0, 0, p); |
| 260 | }); |
| 261 | canvas->restore(); |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 262 | y += SkScalarFloorToInt(bounds.height()); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | private: |
fmalita | 37283c2 | 2016-09-13 10:00:23 -0700 | [diff] [blame] | 267 | sk_sp<SkTextBlob> fBlob; |
Brian Salomon | 343553a | 2018-09-05 15:41:23 -0400 | [diff] [blame] | 268 | SkTArray<sk_sp<SkDrawLooper>> fLoopers; |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 269 | |
| 270 | typedef GM INHERITED; |
| 271 | }; |
| 272 | |
| 273 | ////////////////////////////////////////////////////////////////////////////// |
| 274 | |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 275 | DEF_GM(return new TextBlobLooperGM;) |
joshualitt | d0b5c33 | 2015-04-10 06:17:26 -0700 | [diff] [blame] | 276 | } |