blob: 51e815524f39c4b955d493a9cfcd9d3413bbcb97 [file] [log] [blame]
joshualittd0b5c332015-04-10 06:17:26 -07001/*
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"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
joshualittd0b5c332015-04-10 06:17:26 -070010
11#include "Sk2DPathEffect.h"
12#include "SkBlurMask.h"
reed0daf5dd2016-01-11 12:34:04 -080013#include "SkColorMatrixFilter.h"
joshualittd0b5c332015-04-10 06:17:26 -070014#include "SkCanvas.h"
15#include "SkGradientShader.h"
16#include "SkGraphics.h"
17#include "SkLayerDrawLooper.h"
Mike Reed1be1f8d2018-03-14 13:01:17 -040018#include "SkMaskFilter.h"
joshualittd0b5c332015-04-10 06:17:26 -070019#include "SkRandom.h"
20#include "SkTextBlob.h"
21
22namespace skiagm {
23
mtkleindbfd7ab2016-09-01 11:24:54 -070024constexpr int kWidth = 1250;
25constexpr int kHeight = 700;
joshualittd0b5c332015-04-10 06:17:26 -070026
joshualitt9e36c1a2015-04-14 12:17:27 -070027// Unlike the variant in sk_tool_utils, this version positions the glyphs on a diagonal
Mike Reed2ed78202018-11-21 15:10:08 -050028static void add_to_text_blob(SkTextBlobBuilder* builder, const char* text, const SkFont& font,
joshualittd0b5c332015-04-10 06:17:26 -070029 SkScalar x, SkScalar y) {
joshualittd0b5c332015-04-10 06:17:26 -070030 SkTDArray<uint16_t> glyphs;
31
32 size_t len = strlen(text);
Mike Reed2ed78202018-11-21 15:10:08 -050033 glyphs.append(font.countText(text, len, kUTF8_SkTextEncoding));
34 font.textToGlyphs(text, len, kUTF8_SkTextEncoding, glyphs.begin(), glyphs.count());
joshualittd0b5c332015-04-10 06:17:26 -070035
Mike Reed2ed78202018-11-21 15:10:08 -050036 const SkScalar advanceX = font.getSize() * 0.85f;
37 const SkScalar advanceY = font.getSize() * 1.5f;
joshualittd0b5c332015-04-10 06:17:26 -070038
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 }
Mike Reed2ed78202018-11-21 15:10:08 -050044 const SkTextBlobBuilder::RunBuffer& run = builder->allocRunPos(font, glyphs.count());
joshualittd0b5c332015-04-10 06:17:26 -070045 memcpy(run.glyphs, glyphs.begin(), glyphs.count() * sizeof(uint16_t));
46 memcpy(run.pos, pos.begin(), len * sizeof(SkScalar) * 2);
47}
48
49typedef void (*LooperProc)(SkPaint*);
50
51struct LooperSettings {
reed374772b2016-10-05 17:33:02 -070052 SkBlendMode fMode;
joshualittd0b5c332015-04-10 06:17:26 -070053 SkColor fColor;
54 SkPaint::Style fStyle;
55 SkScalar fWidth;
56 SkScalar fOffset;
57 SkScalar fSkewX;
58 bool fEffect;
59};
60
61static void mask_filter(SkPaint* paint) {
Mike Reed1be1f8d2018-03-14 13:01:17 -040062 paint->setMaskFilter(SkMaskFilter::MakeBlur(kNormal_SkBlurStyle,
reedefdfd512016-04-04 10:02:58 -070063 SkBlurMask::ConvertRadiusToSigma(3.f)));
joshualittd0b5c332015-04-10 06:17:26 -070064}
65
reeda4393342016-03-18 11:22:57 -070066static sk_sp<SkPathEffect> make_tile_effect() {
joshualittd0b5c332015-04-10 06:17:26 -070067 SkMatrix m;
68 m.setScale(1.f, 1.f);
69
70 SkPath path;
71 path.addCircle(0, 0, SkIntToScalar(5));
72
reeda4393342016-03-18 11:22:57 -070073 return SkPath2DPathEffect::Make(m, path);
joshualittd0b5c332015-04-10 06:17:26 -070074}
75
76static void path_effect(SkPaint* paint) {
reeda4393342016-03-18 11:22:57 -070077 paint->setPathEffect(make_tile_effect());
joshualittd0b5c332015-04-10 06:17:26 -070078}
79
reed1a9b9642016-03-13 14:13:58 -070080static sk_sp<SkShader> make_shader(const SkRect& bounds) {
joshualittd0b5c332015-04-10 06:17:26 -070081 const SkPoint pts[] = {
82 { bounds.left(), bounds.top() },
83 { bounds.right(), bounds.bottom() },
84 };
85 const SkColor colors[] = {
86 SK_ColorRED, SK_ColorGREEN, SK_ColorBLUE, SK_ColorBLACK,
87 SK_ColorCYAN, SK_ColorMAGENTA, SK_ColorYELLOW,
88 };
reed1a9b9642016-03-13 14:13:58 -070089 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
90 SkShader::kClamp_TileMode);
joshualittd0b5c332015-04-10 06:17:26 -070091}
92
93static void color_filter(SkPaint* paint) {
94 SkRect r;
95 r.setWH(SkIntToScalar(kWidth), 50);
reed1a9b9642016-03-13 14:13:58 -070096 paint->setShader(make_shader(r));
reedd053ce92016-03-22 10:17:23 -070097 paint->setColorFilter(SkColorMatrixFilter::MakeLightingFilter(0xF0F0F0, 0));
joshualittd0b5c332015-04-10 06:17:26 -070098}
99
100static void kitchen_sink(SkPaint* paint) {
101 color_filter(paint);
102 path_effect(paint);
103 mask_filter(paint);
104
105}
106
reed7b380d02016-03-21 13:25:16 -0700107static sk_sp<SkDrawLooper> setupLooper(SkLayerDrawLooper::BitFlags bits,
108 LooperProc proc,
109 const LooperSettings settings[],
110 size_t size) {
joshualittd0b5c332015-04-10 06:17:26 -0700111 SkLayerDrawLooper::Builder looperBuilder;
112
113 SkLayerDrawLooper::LayerInfo info;
114 info.fPaintBits = bits;
115
Mike Reedfaba3712016-11-03 14:45:31 -0400116 info.fColorMode = SkBlendMode::kSrc;
joshualittd0b5c332015-04-10 06:17:26 -0700117
118 for (size_t i = 0; i < size; i++) {
119 info.fOffset.set(settings[i].fOffset, settings[i].fOffset);
120 SkPaint* paint = looperBuilder.addLayer(info);
reed374772b2016-10-05 17:33:02 -0700121 paint->setBlendMode(settings[i].fMode);
joshualittd0b5c332015-04-10 06:17:26 -0700122 paint->setColor(settings[i].fColor);
123 paint->setStyle(settings[i].fStyle);
124 paint->setStrokeWidth(settings[i].fWidth);
125 if (settings[i].fEffect) {
126 (*proc)(paint);
127 }
128 }
reed7b380d02016-03-21 13:25:16 -0700129 return looperBuilder.detach();
joshualittd0b5c332015-04-10 06:17:26 -0700130}
131
132class TextBlobLooperGM : public GM {
133public:
134 TextBlobLooperGM() {}
135
136protected:
137 void onOnceBeforeDraw() override {
138 SkTextBlobBuilder builder;
139
140 // LCD
Mike Reed2ed78202018-11-21 15:10:08 -0500141 SkFont font;
142 font.setSize(32);
joshualittd0b5c332015-04-10 06:17:26 -0700143 const char* text = "The quick brown fox jumps over the lazy dog";
Mike Reed2ed78202018-11-21 15:10:08 -0500144 font.setSubpixel(true);
145 font.setEdging(SkFont::Edging::kSubpixelAntiAlias);
146 font.setTypeface(sk_tool_utils::create_portable_typeface());
147 add_to_text_blob(&builder, text, font, 0, 0);
fmalita37283c22016-09-13 10:00:23 -0700148 fBlob = builder.make();
joshualittd0b5c332015-04-10 06:17:26 -0700149
150 // create a looper which sandwhiches an effect in two normal draws
151 LooperSettings looperSandwhich[] = {
reed374772b2016-10-05 17:33:02 -0700152 { SkBlendMode::kSrc, SK_ColorMAGENTA, SkPaint::kFill_Style, 0, 0, 0, false },
153 { SkBlendMode::kSrcOver, 0x88000000, SkPaint::kFill_Style, 0, 10.f, 0, true },
154 { SkBlendMode::kSrcOver, 0x50FF00FF, SkPaint::kFill_Style, 0, 20.f, 0, false },
joshualittd0b5c332015-04-10 06:17:26 -0700155 };
156
157 LooperSettings compound[] = {
reed374772b2016-10-05 17:33:02 -0700158 { SkBlendMode::kSrc, SK_ColorWHITE, SkPaint::kStroke_Style, 1.f * 3/4, 0, 0, false },
159 { SkBlendMode::kSrc, SK_ColorRED, SkPaint::kStroke_Style, 4.f, 0, 0, false },
160 { SkBlendMode::kSrc, SK_ColorBLUE, SkPaint::kFill_Style, 0, 0, 0, false },
161 { SkBlendMode::kSrcOver, 0x88000000, SkPaint::kFill_Style, 0, 10.f, 0, true }
joshualittd0b5c332015-04-10 06:17:26 -0700162 };
163
164 LooperSettings xfermode[] = {
reed374772b2016-10-05 17:33:02 -0700165 { SkBlendMode::kDifference, SK_ColorWHITE, SkPaint::kFill_Style, 0, 0, 0, false },
166 { SkBlendMode::kSrcOver, 0xFF000000, SkPaint::kFill_Style, 0, 1.f, 0, true },
167 { SkBlendMode::kSrcOver, 0x50FF00FF, SkPaint::kFill_Style, 0, 2.f, 0, false },
joshualittd0b5c332015-04-10 06:17:26 -0700168 };
169
170 // NOTE, this should be ignored by textblobs
171 LooperSettings skew[] = {
reed374772b2016-10-05 17:33:02 -0700172 { SkBlendMode::kSrc, SK_ColorRED, SkPaint::kFill_Style, 0, 0, -1.f, false },
173 { SkBlendMode::kSrc, SK_ColorGREEN, SkPaint::kFill_Style, 0, 10.f, -1.f, false },
174 { SkBlendMode::kSrc, SK_ColorBLUE, SkPaint::kFill_Style, 0, 20.f, -1.f, false },
joshualittd0b5c332015-04-10 06:17:26 -0700175 };
176
177 LooperSettings kitchenSink[] = {
reed374772b2016-10-05 17:33:02 -0700178 { SkBlendMode::kSrc, SK_ColorWHITE, SkPaint::kStroke_Style, 1.f * 3/4, 0, 0, false },
179 { SkBlendMode::kSrc, SK_ColorBLACK, SkPaint::kFill_Style, 0, 0, 0, false },
180 { SkBlendMode::kDifference, SK_ColorWHITE, SkPaint::kFill_Style, 1.f, 10.f, 0, false },
181 { SkBlendMode::kSrc, SK_ColorWHITE, SkPaint::kFill_Style, 0, 10.f, 0, true },
182 { SkBlendMode::kSrcOver, 0x50FF00FF, SkPaint::kFill_Style, 0, 20.f, 0, false },
joshualittd0b5c332015-04-10 06:17:26 -0700183 };
184
reed7b380d02016-03-21 13:25:16 -0700185 fLoopers.push_back(setupLooper(SkLayerDrawLooper::kMaskFilter_Bit |
186 SkLayerDrawLooper::kXfermode_Bit |
187 SkLayerDrawLooper::kStyle_Bit, &mask_filter,
188 compound, SK_ARRAY_COUNT(compound)));
189 fLoopers.push_back(setupLooper(SkLayerDrawLooper::kPathEffect_Bit |
190 SkLayerDrawLooper::kXfermode_Bit, &path_effect,
191 looperSandwhich, SK_ARRAY_COUNT(looperSandwhich)));
192 fLoopers.push_back(setupLooper(SkLayerDrawLooper::kShader_Bit |
193 SkLayerDrawLooper::kColorFilter_Bit |
194 SkLayerDrawLooper::kXfermode_Bit, &color_filter,
195 looperSandwhich, SK_ARRAY_COUNT(looperSandwhich)));
196 fLoopers.push_back(setupLooper(SkLayerDrawLooper::kShader_Bit |
197 SkLayerDrawLooper::kColorFilter_Bit |
198 SkLayerDrawLooper::kXfermode_Bit, &color_filter,
199 xfermode, SK_ARRAY_COUNT(xfermode)));
200 fLoopers.push_back(setupLooper(0, nullptr, skew, SK_ARRAY_COUNT(skew)));
201 fLoopers.push_back(setupLooper(SkLayerDrawLooper::kMaskFilter_Bit |
202 SkLayerDrawLooper::kShader_Bit |
203 SkLayerDrawLooper::kColorFilter_Bit |
204 SkLayerDrawLooper::kPathEffect_Bit |
205 SkLayerDrawLooper::kStyle_Bit |
206 SkLayerDrawLooper::kXfermode_Bit, &kitchen_sink,
207 kitchenSink, SK_ARRAY_COUNT(kitchenSink)));
joshualittd0b5c332015-04-10 06:17:26 -0700208
209 // Test we respect overrides
reed7b380d02016-03-21 13:25:16 -0700210 fLoopers.push_back(setupLooper(0, &kitchen_sink,
211 kitchenSink, SK_ARRAY_COUNT(kitchenSink)));
joshualittd0b5c332015-04-10 06:17:26 -0700212 }
213
214 SkString onShortName() override {
215 return SkString("textbloblooper");
216 }
217
218 SkISize onISize() override {
219 return SkISize::Make(kWidth, kHeight);
220 }
221
222 void onDraw(SkCanvas* canvas) override {
223
Mike Kleind46dce32018-08-16 10:17:03 -0400224 canvas->drawColor(SK_ColorGRAY);
joshualittd0b5c332015-04-10 06:17:26 -0700225
226 SkPaint paint;
227 canvas->translate(10, 40);
228
joshualittd0b5c332015-04-10 06:17:26 -0700229 SkRect bounds = fBlob->bounds();
230
231 int y = 0;
232 for (int looper = 0; looper < fLoopers.count(); looper++) {
233 paint.setLooper(fLoopers[looper]);
234 canvas->save();
235 canvas->translate(0, SkIntToScalar(y));
236 canvas->drawTextBlob(fBlob, 0, 0, paint);
237 canvas->restore();
238 y += SkScalarFloorToInt(bounds.height());
239 }
240 }
241
242private:
fmalita37283c22016-09-13 10:00:23 -0700243 sk_sp<SkTextBlob> fBlob;
Brian Salomon343553a2018-09-05 15:41:23 -0400244 SkTArray<sk_sp<SkDrawLooper>> fLoopers;
joshualittd0b5c332015-04-10 06:17:26 -0700245
246 typedef GM INHERITED;
247};
248
249//////////////////////////////////////////////////////////////////////////////
250
halcanary385fe4d2015-08-26 13:07:48 -0700251DEF_GM(return new TextBlobLooperGM;)
joshualittd0b5c332015-04-10 06:17:26 -0700252}