blob: d0e55086c981a631d663f59241b28ca541eac9bc [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"
9
10#include "Sk2DPathEffect.h"
11#include "SkBlurMask.h"
12#include "SkBlurMaskFilter.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"
18#include "SkRandom.h"
19#include "SkTextBlob.h"
20
21namespace skiagm {
22
mtkleindbfd7ab2016-09-01 11:24:54 -070023constexpr int kWidth = 1250;
24constexpr int kHeight = 700;
joshualittd0b5c332015-04-10 06:17:26 -070025
joshualitt9e36c1a2015-04-14 12:17:27 -070026// Unlike the variant in sk_tool_utils, this version positions the glyphs on a diagonal
joshualittd0b5c332015-04-10 06:17:26 -070027static 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);
halcanary96fcdcc2015-08-27 07:41:13 -070033 glyphs.append(paint.textToGlyphs(text, len, nullptr));
joshualittd0b5c332015-04-10 06:17:26 -070034 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
51typedef void (*LooperProc)(SkPaint*);
52
53struct LooperSettings {
reed374772b2016-10-05 17:33:02 -070054 SkBlendMode fMode;
joshualittd0b5c332015-04-10 06:17:26 -070055 SkColor fColor;
56 SkPaint::Style fStyle;
57 SkScalar fWidth;
58 SkScalar fOffset;
59 SkScalar fSkewX;
60 bool fEffect;
61};
62
63static void mask_filter(SkPaint* paint) {
reedefdfd512016-04-04 10:02:58 -070064 paint->setMaskFilter(SkBlurMaskFilter::Make(kNormal_SkBlurStyle,
65 SkBlurMask::ConvertRadiusToSigma(3.f)));
joshualittd0b5c332015-04-10 06:17:26 -070066}
67
reeda4393342016-03-18 11:22:57 -070068static sk_sp<SkPathEffect> make_tile_effect() {
joshualittd0b5c332015-04-10 06:17:26 -070069 SkMatrix m;
70 m.setScale(1.f, 1.f);
71
72 SkPath path;
73 path.addCircle(0, 0, SkIntToScalar(5));
74
reeda4393342016-03-18 11:22:57 -070075 return SkPath2DPathEffect::Make(m, path);
joshualittd0b5c332015-04-10 06:17:26 -070076}
77
78static void path_effect(SkPaint* paint) {
reeda4393342016-03-18 11:22:57 -070079 paint->setPathEffect(make_tile_effect());
joshualittd0b5c332015-04-10 06:17:26 -070080}
81
reed1a9b9642016-03-13 14:13:58 -070082static sk_sp<SkShader> make_shader(const SkRect& bounds) {
joshualittd0b5c332015-04-10 06:17:26 -070083 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 };
reed1a9b9642016-03-13 14:13:58 -070091 return SkGradientShader::MakeLinear(pts, colors, nullptr, SK_ARRAY_COUNT(colors),
92 SkShader::kClamp_TileMode);
joshualittd0b5c332015-04-10 06:17:26 -070093}
94
95static void color_filter(SkPaint* paint) {
96 SkRect r;
97 r.setWH(SkIntToScalar(kWidth), 50);
reed1a9b9642016-03-13 14:13:58 -070098 paint->setShader(make_shader(r));
reedd053ce92016-03-22 10:17:23 -070099 paint->setColorFilter(SkColorMatrixFilter::MakeLightingFilter(0xF0F0F0, 0));
joshualittd0b5c332015-04-10 06:17:26 -0700100}
101
102static void kitchen_sink(SkPaint* paint) {
103 color_filter(paint);
104 path_effect(paint);
105 mask_filter(paint);
106
107}
108
reed7b380d02016-03-21 13:25:16 -0700109static sk_sp<SkDrawLooper> setupLooper(SkLayerDrawLooper::BitFlags bits,
110 LooperProc proc,
111 const LooperSettings settings[],
112 size_t size) {
joshualittd0b5c332015-04-10 06:17:26 -0700113 SkLayerDrawLooper::Builder looperBuilder;
114
115 SkLayerDrawLooper::LayerInfo info;
116 info.fPaintBits = bits;
117
Mike Reedfaba3712016-11-03 14:45:31 -0400118 info.fColorMode = SkBlendMode::kSrc;
joshualittd0b5c332015-04-10 06:17:26 -0700119
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);
reed374772b2016-10-05 17:33:02 -0700123 paint->setBlendMode(settings[i].fMode);
joshualittd0b5c332015-04-10 06:17:26 -0700124 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 }
reed7b380d02016-03-21 13:25:16 -0700131 return looperBuilder.detach();
joshualittd0b5c332015-04-10 06:17:26 -0700132}
133
134class TextBlobLooperGM : public GM {
135public:
136 TextBlobLooperGM() {}
137
138protected:
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);
caryclarkf597c422015-07-28 10:37:53 -0700149 sk_tool_utils::set_portable_typeface(&paint);
joshualittd0b5c332015-04-10 06:17:26 -0700150 add_to_text_blob(&builder, text, paint, 0, 0);
fmalita37283c22016-09-13 10:00:23 -0700151 fBlob = builder.make();
joshualittd0b5c332015-04-10 06:17:26 -0700152
153 // create a looper which sandwhiches an effect in two normal draws
154 LooperSettings looperSandwhich[] = {
reed374772b2016-10-05 17:33:02 -0700155 { SkBlendMode::kSrc, SK_ColorMAGENTA, SkPaint::kFill_Style, 0, 0, 0, false },
156 { SkBlendMode::kSrcOver, 0x88000000, SkPaint::kFill_Style, 0, 10.f, 0, true },
157 { SkBlendMode::kSrcOver, 0x50FF00FF, SkPaint::kFill_Style, 0, 20.f, 0, false },
joshualittd0b5c332015-04-10 06:17:26 -0700158 };
159
160 LooperSettings compound[] = {
reed374772b2016-10-05 17:33:02 -0700161 { SkBlendMode::kSrc, SK_ColorWHITE, SkPaint::kStroke_Style, 1.f * 3/4, 0, 0, false },
162 { SkBlendMode::kSrc, SK_ColorRED, SkPaint::kStroke_Style, 4.f, 0, 0, false },
163 { SkBlendMode::kSrc, SK_ColorBLUE, SkPaint::kFill_Style, 0, 0, 0, false },
164 { SkBlendMode::kSrcOver, 0x88000000, SkPaint::kFill_Style, 0, 10.f, 0, true }
joshualittd0b5c332015-04-10 06:17:26 -0700165 };
166
167 LooperSettings xfermode[] = {
reed374772b2016-10-05 17:33:02 -0700168 { SkBlendMode::kDifference, SK_ColorWHITE, SkPaint::kFill_Style, 0, 0, 0, false },
169 { SkBlendMode::kSrcOver, 0xFF000000, SkPaint::kFill_Style, 0, 1.f, 0, true },
170 { SkBlendMode::kSrcOver, 0x50FF00FF, SkPaint::kFill_Style, 0, 2.f, 0, false },
joshualittd0b5c332015-04-10 06:17:26 -0700171 };
172
173 // NOTE, this should be ignored by textblobs
174 LooperSettings skew[] = {
reed374772b2016-10-05 17:33:02 -0700175 { SkBlendMode::kSrc, SK_ColorRED, SkPaint::kFill_Style, 0, 0, -1.f, false },
176 { SkBlendMode::kSrc, SK_ColorGREEN, SkPaint::kFill_Style, 0, 10.f, -1.f, false },
177 { SkBlendMode::kSrc, SK_ColorBLUE, SkPaint::kFill_Style, 0, 20.f, -1.f, false },
joshualittd0b5c332015-04-10 06:17:26 -0700178 };
179
180 LooperSettings kitchenSink[] = {
reed374772b2016-10-05 17:33:02 -0700181 { SkBlendMode::kSrc, SK_ColorWHITE, SkPaint::kStroke_Style, 1.f * 3/4, 0, 0, false },
182 { SkBlendMode::kSrc, SK_ColorBLACK, SkPaint::kFill_Style, 0, 0, 0, false },
183 { SkBlendMode::kDifference, SK_ColorWHITE, SkPaint::kFill_Style, 1.f, 10.f, 0, false },
184 { SkBlendMode::kSrc, SK_ColorWHITE, SkPaint::kFill_Style, 0, 10.f, 0, true },
185 { SkBlendMode::kSrcOver, 0x50FF00FF, SkPaint::kFill_Style, 0, 20.f, 0, false },
joshualittd0b5c332015-04-10 06:17:26 -0700186 };
187
reed7b380d02016-03-21 13:25:16 -0700188 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)));
joshualittd0b5c332015-04-10 06:17:26 -0700211
212 // Test we respect overrides
reed7b380d02016-03-21 13:25:16 -0700213 fLoopers.push_back(setupLooper(0, &kitchen_sink,
214 kitchenSink, SK_ARRAY_COUNT(kitchenSink)));
joshualittd0b5c332015-04-10 06:17:26 -0700215 }
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
caryclarkf597c422015-07-28 10:37:53 -0700227 canvas->drawColor(sk_tool_utils::color_to_565(SK_ColorGRAY));
joshualittd0b5c332015-04-10 06:17:26 -0700228
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
247private:
fmalita37283c22016-09-13 10:00:23 -0700248 sk_sp<SkTextBlob> fBlob;
reed7b380d02016-03-21 13:25:16 -0700249 SkTArray<sk_sp<SkDrawLooper>, true> fLoopers;
joshualittd0b5c332015-04-10 06:17:26 -0700250
251 typedef GM INHERITED;
252};
253
254//////////////////////////////////////////////////////////////////////////////
255
halcanary385fe4d2015-08-26 13:07:48 -0700256DEF_GM(return new TextBlobLooperGM;)
joshualittd0b5c332015-04-10 06:17:26 -0700257}