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.h"
9 #include "sk_tool_utils.h"
10 #include "SkCanvas.h"
11 #include "SkColorFilter.h"
12 #include "SkColorPriv.h"
13 #include "SkShader.h"
14 
15 #include "SkBlurImageFilter.h"
16 #include "SkColorFilterImageFilter.h"
17 #include "SkDropShadowImageFilter.h"
18 #include "SkSpecialImage.h"
19 
20 class FailImageFilter : public SkImageFilter {
21 public:
22     class Registrar {
23     public:
Registrar()24         Registrar() {
25             SkFlattenable::Register("FailImageFilter",
26                                     FailImageFilter::CreateProc,
27                                     FailImageFilter::GetFlattenableType());
28         }
29     };
Make()30     static sk_sp<SkImageFilter> Make() {
31         return sk_sp<SkImageFilter>(new FailImageFilter);
32     }
33 
34     SK_TO_STRING_OVERRIDE()
35     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(FailImageFilter)
36 
37 protected:
FailImageFilter()38     FailImageFilter() : INHERITED(nullptr, 0, nullptr) {}
39 
onFilterImage(SkSpecialImage * source,const Context &,SkIPoint * offset) const40     sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&,
41                                         SkIPoint* offset) const override {
42         return nullptr;
43     }
44 
45 private:
46     typedef SkImageFilter INHERITED;
47 };
48 
49 static FailImageFilter::Registrar gReg0;
50 
CreateProc(SkReadBuffer & buffer)51 sk_sp<SkFlattenable> FailImageFilter::CreateProc(SkReadBuffer& buffer) {
52     SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 0);
53     return FailImageFilter::Make();
54 }
55 
56 #ifndef SK_IGNORE_TO_STRING
toString(SkString * str) const57 void FailImageFilter::toString(SkString* str) const {
58     str->appendf("FailImageFilter: (");
59     str->append(")");
60 }
61 #endif
62 
63 class IdentityImageFilter : public SkImageFilter {
64 public:
65     class Registrar {
66     public:
Registrar()67         Registrar() {
68             SkFlattenable::Register("IdentityImageFilter",
69                                     IdentityImageFilter::CreateProc,
70                                     IdentityImageFilter::GetFlattenableType());
71         }
72     };
Make(sk_sp<SkImageFilter> input)73     static sk_sp<SkImageFilter> Make(sk_sp<SkImageFilter> input) {
74         return sk_sp<SkImageFilter>(new IdentityImageFilter(std::move(input)));
75     }
76 
77     SK_TO_STRING_OVERRIDE()
78     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(IdentityImageFilter)
79 
80 protected:
onFilterImage(SkSpecialImage * source,const Context &,SkIPoint * offset) const81     sk_sp<SkSpecialImage> onFilterImage(SkSpecialImage* source, const Context&,
82                                         SkIPoint* offset) const override {
83         offset->set(0, 0);
84         return sk_ref_sp<SkSpecialImage>(source);
85     }
86 
87 private:
IdentityImageFilter(sk_sp<SkImageFilter> input)88     IdentityImageFilter(sk_sp<SkImageFilter> input) : INHERITED(&input, 1, nullptr) {}
89 
90     typedef SkImageFilter INHERITED;
91 };
92 
93 static IdentityImageFilter::Registrar gReg1;
94 
CreateProc(SkReadBuffer & buffer)95 sk_sp<SkFlattenable> IdentityImageFilter::CreateProc(SkReadBuffer& buffer) {
96     SK_IMAGEFILTER_UNFLATTEN_COMMON(common, 1);
97     return IdentityImageFilter::Make(common.getInput(0));
98 }
99 
100 #ifndef SK_IGNORE_TO_STRING
toString(SkString * str) const101 void IdentityImageFilter::toString(SkString* str) const {
102     str->appendf("IdentityImageFilter: (");
103     str->append(")");
104 }
105 #endif
106 
107 ///////////////////////////////////////////////////////////////////////////////
108 
draw_paint(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)109 static void draw_paint(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
110     SkPaint paint;
111     paint.setImageFilter(std::move(imf));
112     paint.setColor(SK_ColorGREEN);
113     canvas->save();
114     canvas->clipRect(r);
115     canvas->drawPaint(paint);
116     canvas->restore();
117 }
118 
draw_line(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)119 static void draw_line(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
120     SkPaint paint;
121     paint.setColor(SK_ColorBLUE);
122     paint.setImageFilter(imf);
123     paint.setStrokeWidth(r.width()/10);
124     canvas->drawLine(r.fLeft, r.fTop, r.fRight, r.fBottom, paint);
125 }
126 
draw_rect(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)127 static void draw_rect(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
128     SkPaint paint;
129     paint.setColor(SK_ColorYELLOW);
130     paint.setImageFilter(imf);
131     SkRect rr(r);
132     rr.inset(r.width()/10, r.height()/10);
133     canvas->drawRect(rr, paint);
134 }
135 
draw_path(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)136 static void draw_path(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
137     SkPaint paint;
138     paint.setColor(SK_ColorMAGENTA);
139     paint.setImageFilter(imf);
140     paint.setAntiAlias(true);
141     canvas->drawCircle(r.centerX(), r.centerY(), r.width()*2/5, paint);
142 }
143 
draw_text(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)144 static void draw_text(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
145     SkPaint paint;
146     paint.setImageFilter(imf);
147     paint.setColor(SK_ColorCYAN);
148     paint.setAntiAlias(true);
149     sk_tool_utils::set_portable_typeface(&paint);
150     paint.setTextSize(r.height()/2);
151     paint.setTextAlign(SkPaint::kCenter_Align);
152     canvas->drawText("Text", 4, r.centerX(), r.centerY(), paint);
153 }
154 
draw_bitmap(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)155 static void draw_bitmap(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
156     SkPaint paint;
157     paint.setImageFilter(std::move(imf));
158 
159     SkIRect bounds;
160     r.roundOut(&bounds);
161 
162     SkBitmap bm;
163     bm.allocN32Pixels(bounds.width(), bounds.height());
164     bm.eraseColor(SK_ColorTRANSPARENT);
165     SkCanvas c(bm);
166     draw_path(&c, r, nullptr);
167 
168     canvas->drawBitmap(bm, 0, 0, &paint);
169 }
170 
171 ///////////////////////////////////////////////////////////////////////////////
172 
173 class ImageFiltersBaseGM : public skiagm::GM {
174 public:
ImageFiltersBaseGM()175     ImageFiltersBaseGM () {}
176 
177 protected:
onShortName()178     SkString onShortName() override {
179         return SkString("imagefiltersbase");
180     }
181 
onISize()182     SkISize onISize() override { return SkISize::Make(700, 500); }
183 
draw_frame(SkCanvas * canvas,const SkRect & r)184     void draw_frame(SkCanvas* canvas, const SkRect& r) {
185         SkPaint paint;
186         paint.setStyle(SkPaint::kStroke_Style);
187         paint.setColor(SK_ColorRED);
188         canvas->drawRect(r, paint);
189     }
190 
onDraw(SkCanvas * canvas)191     void onDraw(SkCanvas* canvas) override {
192         void (*drawProc[])(SkCanvas*, const SkRect&, sk_sp<SkImageFilter>) = {
193             draw_paint,
194             draw_line, draw_rect, draw_path, draw_text,
195             draw_bitmap,
196         };
197 
198         auto cf = SkColorFilter::MakeModeFilter(SK_ColorRED, SkBlendMode::kSrcIn);
199         sk_sp<SkImageFilter> filters[] = {
200             nullptr,
201             IdentityImageFilter::Make(nullptr),
202             FailImageFilter::Make(),
203             SkColorFilterImageFilter::Make(std::move(cf), nullptr),
204             SkBlurImageFilter::Make(12.0f, 0.0f, nullptr),
205             SkDropShadowImageFilter::Make(
206                                     10.0f, 5.0f, 3.0f, 3.0f, SK_ColorBLUE,
207                                     SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode,
208                                     nullptr),
209         };
210 
211         SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64));
212         SkScalar MARGIN = SkIntToScalar(16);
213         SkScalar DX = r.width() + MARGIN;
214         SkScalar DY = r.height() + MARGIN;
215 
216         canvas->translate(MARGIN, MARGIN);
217         for (size_t i = 0; i < SK_ARRAY_COUNT(drawProc); ++i) {
218             canvas->save();
219             for (size_t j = 0; j < SK_ARRAY_COUNT(filters); ++j) {
220                 drawProc[i](canvas, r, filters[j]);
221 
222                 draw_frame(canvas, r);
223                 canvas->translate(0, DY);
224             }
225             canvas->restore();
226             canvas->translate(DX, 0);
227         }
228     }
229 
230 private:
231     typedef GM INHERITED;
232 };
233 DEF_GM( return new ImageFiltersBaseGM; )
234 
235 ///////////////////////////////////////////////////////////////////////////////
236 
237 /*
238  *  Want to test combos of filter and LCD text, to be sure we disable LCD in the presence of
239  *  a filter.
240  */
241 class ImageFiltersTextBaseGM : public skiagm::GM {
242     SkString fSuffix;
243 public:
ImageFiltersTextBaseGM(const char suffix[])244     ImageFiltersTextBaseGM(const char suffix[]) : fSuffix(suffix) {}
245 
246 protected:
onShortName()247     SkString onShortName() override {
248         SkString name;
249         name.printf("%s_%s", "textfilter", fSuffix.c_str());
250         return name;
251     }
252 
onISize()253     SkISize onISize() override { return SkISize::Make(512, 342); }
254 
drawWaterfall(SkCanvas * canvas,const SkPaint & origPaint)255     void drawWaterfall(SkCanvas* canvas, const SkPaint& origPaint) {
256         const uint32_t flags[] = {
257             0,
258             SkPaint::kAntiAlias_Flag,
259             SkPaint::kAntiAlias_Flag | SkPaint::kLCDRenderText_Flag,
260         };
261         SkPaint paint(origPaint);
262         sk_tool_utils::set_portable_typeface(&paint);
263         paint.setTextSize(30);
264 
265         SkAutoCanvasRestore acr(canvas, true);
266         for (size_t i = 0; i < SK_ARRAY_COUNT(flags); ++i) {
267             paint.setFlags(flags[i]);
268             canvas->drawText("Hamburgefons", 11, 0, 0, paint);
269             canvas->translate(0, 40);
270         }
271     }
272 
273     virtual void installFilter(SkPaint* paint) = 0;
274 
onDraw(SkCanvas * canvas)275     void onDraw(SkCanvas* canvas) override {
276         SkPaint paint;
277 
278         canvas->translate(20, 40);
279 
280         for (int doSaveLayer = 0; doSaveLayer <= 1; ++doSaveLayer) {
281             SkAutoCanvasRestore acr(canvas, true);
282             for (int useFilter = 0; useFilter <= 1; ++useFilter) {
283                 SkAutoCanvasRestore acr2(canvas, true);
284 
285                 SkPaint paint;
286                 if (useFilter) {
287                     this->installFilter(&paint);
288                 }
289                 if (doSaveLayer) {
290                     canvas->saveLayer(nullptr, &paint);
291                     paint.setImageFilter(nullptr);
292                 }
293                 this->drawWaterfall(canvas, paint);
294 
295                 acr2.restore();
296                 canvas->translate(250, 0);
297             }
298             acr.restore();
299             canvas->translate(0, 200);
300         }
301     }
302 
303 private:
304     typedef GM INHERITED;
305 };
306 
307 class ImageFiltersText_IF : public ImageFiltersTextBaseGM {
308 public:
ImageFiltersText_IF()309     ImageFiltersText_IF() : ImageFiltersTextBaseGM("image") {}
310 
installFilter(SkPaint * paint)311     void installFilter(SkPaint* paint) override {
312         paint->setImageFilter(SkBlurImageFilter::Make(1.5f, 1.5f, nullptr));
313     }
314 };
315 DEF_GM( return new ImageFiltersText_IF; )
316 
317 class ImageFiltersText_CF : public ImageFiltersTextBaseGM {
318 public:
ImageFiltersText_CF()319     ImageFiltersText_CF() : ImageFiltersTextBaseGM("color") {}
320 
installFilter(SkPaint * paint)321     void installFilter(SkPaint* paint) override {
322         paint->setColorFilter(SkColorFilter::MakeModeFilter(SK_ColorBLUE, SkBlendMode::kSrcIn));
323     }
324 };
325 DEF_GM( return new ImageFiltersText_CF; )
326