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 #include "SkColorFilter.h"
10 
11 #include "SkColorFilterImageFilter.h"
12 #include "SkDropShadowImageFilter.h"
13 
14 ///////////////////////////////////////////////////////////////////////////////
15 
draw_paint(SkCanvas * canvas,const SkRect & r,SkImageFilter * imf)16 static void draw_paint(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
17     SkPaint paint;
18     paint.setImageFilter(imf);
19     paint.setColor(SK_ColorBLACK);
20     canvas->save();
21     canvas->clipRect(r);
22     canvas->drawPaint(paint);
23     canvas->restore();
24 }
25 
draw_path(SkCanvas * canvas,const SkRect & r,SkImageFilter * imf)26 static void draw_path(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
27     SkPaint paint;
28     paint.setColor(SK_ColorGREEN);
29     paint.setImageFilter(imf);
30     paint.setAntiAlias(true);
31     canvas->save();
32     canvas->clipRect(r);
33     canvas->drawCircle(r.centerX(), r.centerY(), r.width()/3, paint);
34     canvas->restore();
35 }
36 
draw_text(SkCanvas * canvas,const SkRect & r,SkImageFilter * imf)37 static void draw_text(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
38     SkPaint paint;
39     paint.setImageFilter(imf);
40     paint.setColor(SK_ColorGREEN);
41     paint.setAntiAlias(true);
42     sk_tool_utils::set_portable_typeface(&paint);
43     paint.setTextSize(r.height()/2);
44     paint.setTextAlign(SkPaint::kCenter_Align);
45     canvas->save();
46     canvas->clipRect(r);
47     canvas->drawText("Text", 4, r.centerX(), r.centerY(), paint);
48     canvas->restore();
49 }
50 
draw_bitmap(SkCanvas * canvas,const SkRect & r,SkImageFilter * imf)51 static void draw_bitmap(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
52     SkPaint paint;
53 
54     SkIRect bounds;
55     r.roundOut(&bounds);
56 
57     SkBitmap bm;
58     bm.allocN32Pixels(bounds.width(), bounds.height());
59     bm.eraseColor(SK_ColorTRANSPARENT);
60     SkCanvas c(bm);
61     draw_path(&c, r, NULL);
62 
63     paint.setImageFilter(imf);
64     canvas->save();
65     canvas->clipRect(r);
66     canvas->drawBitmap(bm, 0, 0, &paint);
67     canvas->restore();
68 }
69 
draw_sprite(SkCanvas * canvas,const SkRect & r,SkImageFilter * imf)70 static void draw_sprite(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
71     SkPaint paint;
72 
73     SkIRect bounds;
74     r.roundOut(&bounds);
75 
76     SkBitmap bm;
77     bm.allocN32Pixels(bounds.width(), bounds.height());
78     bm.eraseColor(SK_ColorRED);
79     SkCanvas c(bm);
80 
81     SkIRect cropRect = SkIRect::MakeXYWH(10, 10, 44, 44);
82     paint.setColor(SK_ColorGREEN);
83     c.drawRect(SkRect::Make(cropRect), paint);
84 
85     paint.setImageFilter(imf);
86     SkPoint loc = { r.fLeft, r.fTop };
87     canvas->getTotalMatrix().mapPoints(&loc, 1);
88     canvas->drawSprite(bm,
89                        SkScalarRoundToInt(loc.fX), SkScalarRoundToInt(loc.fY),
90                        &paint);
91 }
92 
93 ///////////////////////////////////////////////////////////////////////////////
94 
95 class DropShadowImageFilterGM : public skiagm::GM {
96 public:
DropShadowImageFilterGM()97     DropShadowImageFilterGM () {}
98 
99 protected:
100 
onShortName()101     virtual SkString onShortName() {
102         return SkString("dropshadowimagefilter");
103     }
104 
onISize()105     virtual SkISize onISize() { return SkISize::Make(400, 656); }
106 
draw_frame(SkCanvas * canvas,const SkRect & r)107     void draw_frame(SkCanvas* canvas, const SkRect& r) {
108         SkPaint paint;
109         paint.setStyle(SkPaint::kStroke_Style);
110         paint.setColor(SK_ColorRED);
111         canvas->drawRect(r, paint);
112     }
113 
onDraw(SkCanvas * canvas)114     virtual void onDraw(SkCanvas* canvas) {
115         void (*drawProc[])(SkCanvas*, const SkRect&, SkImageFilter*) = {
116             draw_sprite, draw_bitmap, draw_path, draw_paint, draw_text
117         };
118 
119         SkAutoTUnref<SkColorFilter> cf(
120             SkColorFilter::CreateModeFilter(SK_ColorMAGENTA, SkXfermode::kSrcIn_Mode));
121         SkAutoTUnref<SkImageFilter> cfif(SkColorFilterImageFilter::Create(cf.get()));
122         SkImageFilter::CropRect cropRect(SkRect::Make(SkIRect::MakeXYWH(10, 10, 44, 44)),
123                                          SkImageFilter::CropRect::kHasAll_CropEdge);
124         SkImageFilter::CropRect bogusRect(SkRect::Make(SkIRect::MakeXYWH(-100, -100, 10, 10)),
125                                           SkImageFilter::CropRect::kHasAll_CropEdge);
126 
127         SkImageFilter* filters[] = {
128             NULL,
129             SkDropShadowImageFilter::Create(7.0f, 0.0f, 0.0f, 3.0f, SK_ColorBLUE,
130                 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode),
131             SkDropShadowImageFilter::Create(0.0f, 7.0f, 3.0f, 0.0f, SK_ColorBLUE,
132                 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode),
133             SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE,
134                 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode),
135             SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE,
136                 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, cfif, NULL),
137             SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE,
138                 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, NULL, &cropRect),
139             SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE,
140                 SkDropShadowImageFilter::kDrawShadowAndForeground_ShadowMode, NULL, &bogusRect),
141             SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE,
142                 SkDropShadowImageFilter::kDrawShadowOnly_ShadowMode),
143         };
144 
145         SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64));
146         SkScalar MARGIN = SkIntToScalar(16);
147         SkScalar DX = r.width() + MARGIN;
148         SkScalar DY = r.height() + MARGIN;
149 
150         canvas->translate(MARGIN, MARGIN);
151         for (size_t j = 0; j < SK_ARRAY_COUNT(drawProc); ++j) {
152             canvas->save();
153             for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
154                 drawProc[j](canvas, r, filters[i]);
155                 canvas->translate(0, DY);
156             }
157             canvas->restore();
158             canvas->translate(DX, 0);
159         }
160 
161         for(size_t j = 0; j < SK_ARRAY_COUNT(filters); ++j) {
162             SkSafeUnref(filters[j]);
163         }
164     }
165 
166 private:
167     typedef GM INHERITED;
168 };
169 
170 ///////////////////////////////////////////////////////////////////////////////
171 
MyFactory(void *)172 static skiagm::GM* MyFactory(void*) { return new DropShadowImageFilterGM; }
173 static skiagm::GMRegistry reg(MyFactory);
174