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