1 /*
2 * Copyright 2011 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/gm.h"
9 #include "include/core/SkBitmap.h"
10 #include "include/core/SkBlendMode.h"
11 #include "include/core/SkCanvas.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkColorFilter.h"
14 #include "include/core/SkFlattenable.h"
15 #include "include/core/SkFont.h"
16 #include "include/core/SkImageFilter.h"
17 #include "include/core/SkPaint.h"
18 #include "include/core/SkPoint.h"
19 #include "include/core/SkRect.h"
20 #include "include/core/SkRefCnt.h"
21 #include "include/core/SkScalar.h"
22 #include "include/core/SkSize.h"
23 #include "include/core/SkString.h"
24 #include "include/core/SkTypeface.h"
25 #include "include/core/SkTypes.h"
26 #include "include/effects/SkImageFilters.h"
27 #include "include/utils/SkTextUtils.h"
28 #include "src/core/SkImageFilter_Base.h"
29 #include "src/core/SkSpecialImage.h"
30 #include "tools/ToolUtils.h"
31
32 #include <utility>
33
34 class SkReadBuffer;
35
36 class FailImageFilter : public SkImageFilter_Base {
37 public:
Make()38 static sk_sp<SkImageFilter> Make() {
39 return sk_sp<SkImageFilter>(new FailImageFilter);
40 }
41
42 SK_FLATTENABLE_HOOKS(FailImageFilter)
43 protected:
FailImageFilter()44 FailImageFilter() : INHERITED(nullptr, 0, nullptr) {}
45
onFilterImage(const Context &,SkIPoint * offset) const46 sk_sp<SkSpecialImage> onFilterImage(const Context&, SkIPoint* offset) const override {
47 return nullptr;
48 }
49
50 private:
51
52 typedef SkImageFilter_Base INHERITED;
53 };
54
CreateProc(SkReadBuffer & buffer)55 sk_sp<SkFlattenable> FailImageFilter::CreateProc(SkReadBuffer& buffer) {
56 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0);
57 return FailImageFilter::Make();
58 }
59
60 class IdentityImageFilter : public SkImageFilter_Base {
61 public:
Make(sk_sp<SkImageFilter> input)62 static sk_sp<SkImageFilter> Make(sk_sp<SkImageFilter> input) {
63 return sk_sp<SkImageFilter>(new IdentityImageFilter(std::move(input)));
64 }
65
66
67 SK_FLATTENABLE_HOOKS(IdentityImageFilter)
68 protected:
onFilterImage(const Context & ctx,SkIPoint * offset) const69 sk_sp<SkSpecialImage> onFilterImage(const Context& ctx, SkIPoint* offset) const override {
70 offset->set(0, 0);
71 return sk_ref_sp<SkSpecialImage>(ctx.sourceImage());
72 }
73
74 private:
IdentityImageFilter(sk_sp<SkImageFilter> input)75 IdentityImageFilter(sk_sp<SkImageFilter> input) : INHERITED(&input, 1, nullptr) {}
76
77 typedef SkImageFilter_Base INHERITED;
78 };
79
80 // Register these image filters as deserializable before main().
81 namespace {
82 static struct Initializer {
Initializer__anonb2c5d4be0111::Initializer83 Initializer() {
84 SK_REGISTER_FLATTENABLE(IdentityImageFilter);
85 SK_REGISTER_FLATTENABLE(FailImageFilter);
86 }
87 } initializer;
88 }
89
CreateProc(SkReadBuffer & buffer)90 sk_sp<SkFlattenable> IdentityImageFilter::CreateProc(SkReadBuffer& buffer) {
91 SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
92 return IdentityImageFilter::Make(common.getInput(0));
93 }
94
95 ///////////////////////////////////////////////////////////////////////////////
96
draw_paint(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)97 static void draw_paint(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
98 SkPaint paint;
99 paint.setImageFilter(std::move(imf));
100 paint.setColor(SK_ColorGREEN);
101 canvas->save();
102 canvas->clipRect(r);
103 canvas->drawPaint(paint);
104 canvas->restore();
105 }
106
draw_line(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)107 static void draw_line(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
108 SkPaint paint;
109 paint.setColor(SK_ColorBLUE);
110 paint.setImageFilter(imf);
111 paint.setStrokeWidth(r.width()/10);
112 canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, paint);
113 }
114
draw_rect(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)115 static void draw_rect(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
116 SkPaint paint;
117 paint.setColor(SK_ColorYELLOW);
118 paint.setImageFilter(imf);
119 SkRect rr(r);
120 rr.inset(r.width()/10, r.height()/10);
121 canvas->drawRect(rr, paint);
122 }
123
draw_path(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)124 static void draw_path(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
125 SkPaint paint;
126 paint.setColor(SK_ColorMAGENTA);
127 paint.setImageFilter(imf);
128 paint.setAntiAlias(true);
129 canvas->drawCircle(r.centerX(), r.centerY(), r.width()*2/5, paint);
130 }
131
draw_text(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)132 static void draw_text(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
133 SkPaint paint;
134 paint.setImageFilter(imf);
135 paint.setColor(SK_ColorCYAN);
136 SkFont font(ToolUtils::create_portable_typeface(), r.height() / 2);
137 SkTextUtils::DrawString(canvas, "Text", r.centerX(), r.centerY(), font, paint,
138 SkTextUtils::kCenter_Align);
139 }
140
draw_bitmap(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)141 static void draw_bitmap(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
142 SkPaint paint;
143 paint.setImageFilter(std::move(imf));
144
145 SkIRect bounds;
146 r.roundOut(&bounds);
147
148 SkBitmap bm;
149 bm.allocN32Pixels(bounds.width(), bounds.height());
150 bm.eraseColor(SK_ColorTRANSPARENT);
151 SkCanvas c(bm);
152 draw_path(&c, r, nullptr);
153
154 canvas->drawBitmap(bm, 0, 0, &paint);
155 }
156
157 ///////////////////////////////////////////////////////////////////////////////
158
159 class ImageFiltersBaseGM : public skiagm::GM {
160 public:
ImageFiltersBaseGM()161 ImageFiltersBaseGM () {}
162
163 protected:
onShortName()164 SkString onShortName() override {
165 return SkString("imagefiltersbase");
166 }
167
onISize()168 SkISize onISize() override { return SkISize::Make(700, 500); }
169
draw_frame(SkCanvas * canvas,const SkRect & r)170 void draw_frame(SkCanvas* canvas, const SkRect& r) {
171 SkPaint paint;
172 paint.setStyle(SkPaint::kStroke_Style);
173 paint.setColor(SK_ColorRED);
174 canvas->drawRect(r, paint);
175 }
176
onDraw(SkCanvas * canvas)177 void onDraw(SkCanvas* canvas) override {
178 void (*drawProc[])(SkCanvas*, const SkRect&, sk_sp<SkImageFilter>) = {
179 draw_paint,
180 draw_line, draw_rect, draw_path, draw_text,
181 draw_bitmap,
182 };
183
184 auto cf = SkColorFilters::Blend(SK_ColorRED, SkBlendMode::kSrcIn);
185 sk_sp<SkImageFilter> filters[] = {
186 nullptr,
187 IdentityImageFilter::Make(nullptr),
188 FailImageFilter::Make(),
189 SkImageFilters::ColorFilter(std::move(cf), nullptr),
190 // The strange 0.29 value tickles an edge case where crop rect calculates
191 // a small border, but the blur really needs no border. This tickles
192 // an msan uninitialized value bug.
193 SkImageFilters::Blur(12.0f, 0.29f, nullptr),
194 SkImageFilters::DropShadow(10.0f, 5.0f, 3.0f, 3.0f, SK_ColorBLUE, nullptr),
195 };
196
197 SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64));
198 SkScalar MARGIN = SkIntToScalar(16);
199 SkScalar DX = r.width() + MARGIN;
200 SkScalar DY = r.height() + MARGIN;
201
202 canvas->translate(MARGIN, MARGIN);
203 for (size_t i = 0; i < SK_ARRAY_COUNT(drawProc); ++i) {
204 canvas->save();
205 for (size_t j = 0; j < SK_ARRAY_COUNT(filters); ++j) {
206 drawProc[i](canvas, r, filters[j]);
207
208 draw_frame(canvas, r);
209 canvas->translate(0, DY);
210 }
211 canvas->restore();
212 canvas->translate(DX, 0);
213 }
214 }
215
216 private:
217 typedef GM INHERITED;
218 };
219 DEF_GM( return new ImageFiltersBaseGM; )
220
221 ///////////////////////////////////////////////////////////////////////////////
222
223 /*
224 * Want to test combos of filter and LCD text, to be sure we disable LCD in the presence of
225 * a filter.
226 */
227 class ImageFiltersTextBaseGM : public skiagm::GM {
228 SkString fSuffix;
229 public:
ImageFiltersTextBaseGM(const char suffix[])230 ImageFiltersTextBaseGM(const char suffix[]) : fSuffix(suffix) {}
231
232 protected:
onShortName()233 SkString onShortName() override {
234 SkString name;
235 name.printf("%s_%s", "textfilter", fSuffix.c_str());
236 return name;
237 }
238
onISize()239 SkISize onISize() override { return SkISize::Make(512, 342); }
240
drawWaterfall(SkCanvas * canvas,const SkPaint & paint)241 void drawWaterfall(SkCanvas* canvas, const SkPaint& paint) {
242 static const SkFont::Edging kEdgings[3] = {
243 SkFont::Edging::kAlias,
244 SkFont::Edging::kAntiAlias,
245 SkFont::Edging::kSubpixelAntiAlias,
246 };
247 SkFont font(ToolUtils::create_portable_typeface(), 30);
248
249 SkAutoCanvasRestore acr(canvas, true);
250 for (SkFont::Edging edging : kEdgings) {
251 font.setEdging(edging);
252 canvas->drawString("Hamburgefon", 0, 0, font, paint);
253 canvas->translate(0, 40);
254 }
255 }
256
257 virtual void installFilter(SkPaint* paint) = 0;
258
onDraw(SkCanvas * canvas)259 void onDraw(SkCanvas* canvas) override {
260 SkPaint paint;
261
262 canvas->translate(20, 40);
263
264 for (int doSaveLayer = 0; doSaveLayer <= 1; ++doSaveLayer) {
265 SkAutoCanvasRestore acr(canvas, true);
266 for (int useFilter = 0; useFilter <= 1; ++useFilter) {
267 SkAutoCanvasRestore acr2(canvas, true);
268
269 SkPaint paint;
270 if (useFilter) {
271 this->installFilter(&paint);
272 }
273 if (doSaveLayer) {
274 canvas->saveLayer(nullptr, &paint);
275 paint.setImageFilter(nullptr);
276 }
277 this->drawWaterfall(canvas, paint);
278
279 acr2.restore();
280 canvas->translate(250, 0);
281 }
282 acr.restore();
283 canvas->translate(0, 200);
284 }
285 }
286
287 private:
288 typedef GM INHERITED;
289 };
290
291 class ImageFiltersText_IF : public ImageFiltersTextBaseGM {
292 public:
ImageFiltersText_IF()293 ImageFiltersText_IF() : ImageFiltersTextBaseGM("image") {}
294
installFilter(SkPaint * paint)295 void installFilter(SkPaint* paint) override {
296 paint->setImageFilter(SkImageFilters::Blur(1.5f, 1.5f, nullptr));
297 }
298 };
299 DEF_GM( return new ImageFiltersText_IF; )
300
301 class ImageFiltersText_CF : public ImageFiltersTextBaseGM {
302 public:
ImageFiltersText_CF()303 ImageFiltersText_CF() : ImageFiltersTextBaseGM("color") {}
304
installFilter(SkPaint * paint)305 void installFilter(SkPaint* paint) override {
306 paint->setColorFilter(SkColorFilters::Blend(SK_ColorBLUE, SkBlendMode::kSrcIn));
307 }
308 };
309 DEF_GM( return new ImageFiltersText_CF; )
310