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 paint.setTextSize(r.height()/2);
43 paint.setTextAlign(SkPaint::kCenter_Align);
44 canvas->save();
45 canvas->clipRect(r);
46 canvas->drawText("Text", 4, r.centerX(), r.centerY(), paint);
47 canvas->restore();
48 }
49
draw_bitmap(SkCanvas * canvas,const SkRect & r,SkImageFilter * imf)50 static void draw_bitmap(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
51 SkPaint paint;
52
53 SkIRect bounds;
54 r.roundOut(&bounds);
55
56 SkBitmap bm;
57 bm.allocN32Pixels(bounds.width(), bounds.height());
58 bm.eraseColor(SK_ColorTRANSPARENT);
59 SkCanvas c(bm);
60 draw_path(&c, r, NULL);
61
62 paint.setImageFilter(imf);
63 canvas->save();
64 canvas->clipRect(r);
65 canvas->drawBitmap(bm, 0, 0, &paint);
66 canvas->restore();
67 }
68
draw_sprite(SkCanvas * canvas,const SkRect & r,SkImageFilter * imf)69 static void draw_sprite(SkCanvas* canvas, const SkRect& r, SkImageFilter* imf) {
70 SkPaint paint;
71
72 SkIRect bounds;
73 r.roundOut(&bounds);
74
75 SkBitmap bm;
76 bm.allocN32Pixels(bounds.width(), bounds.height());
77 bm.eraseColor(SK_ColorRED);
78 SkCanvas c(bm);
79
80 SkIRect cropRect = SkIRect::MakeXYWH(10, 10, 44, 44);
81 paint.setColor(SK_ColorGREEN);
82 c.drawRect(SkRect::Make(cropRect), paint);
83
84 paint.setImageFilter(imf);
85 SkPoint loc = { r.fLeft, r.fTop };
86 canvas->getTotalMatrix().mapPoints(&loc, 1);
87 canvas->drawSprite(bm,
88 SkScalarRoundToInt(loc.fX), SkScalarRoundToInt(loc.fY),
89 &paint);
90 }
91
92 ///////////////////////////////////////////////////////////////////////////////
93
94 class DropShadowImageFilterGM : public skiagm::GM {
95 public:
DropShadowImageFilterGM()96 DropShadowImageFilterGM () {}
97
98 protected:
99
onShortName()100 virtual SkString onShortName() {
101 return SkString("dropshadowimagefilter");
102 }
103
onISize()104 virtual SkISize onISize() { return SkISize::Make(400, 700); }
105
draw_frame(SkCanvas * canvas,const SkRect & r)106 void draw_frame(SkCanvas* canvas, const SkRect& r) {
107 SkPaint paint;
108 paint.setStyle(SkPaint::kStroke_Style);
109 paint.setColor(SK_ColorRED);
110 canvas->drawRect(r, paint);
111 }
112
onGetFlags() const113 virtual uint32_t onGetFlags() const {
114 // Because of the use of drawSprite, this test is excluded
115 // from scaled replay tests because drawSprite ignores the
116 // reciprocal scale that is applied at record time, which is
117 // the intended behavior of drawSprite.
118 return kSkipScaledReplay_Flag | kSkipTiled_Flag;
119 }
120
onDraw(SkCanvas * canvas)121 virtual void onDraw(SkCanvas* canvas) {
122 void (*drawProc[])(SkCanvas*, const SkRect&, SkImageFilter*) = {
123 draw_sprite, draw_bitmap, draw_path, draw_paint, draw_text
124 };
125
126 SkAutoTUnref<SkColorFilter> cf(
127 SkColorFilter::CreateModeFilter(SK_ColorMAGENTA, SkXfermode::kSrcIn_Mode));
128 SkAutoTUnref<SkImageFilter> cfif(SkColorFilterImageFilter::Create(cf.get()));
129 SkImageFilter::CropRect cropRect(SkRect::Make(SkIRect::MakeXYWH(10, 10, 44, 44)),
130 SkImageFilter::CropRect::kHasAll_CropEdge);
131 SkImageFilter::CropRect bogusRect(SkRect::Make(SkIRect::MakeXYWH(-100, -100, 10, 10)),
132 SkImageFilter::CropRect::kHasAll_CropEdge);
133
134 SkImageFilter* filters[] = {
135 NULL,
136 SkDropShadowImageFilter::Create(7.0f, 0.0f, 0.0f, 3.0f, SK_ColorBLUE),
137 SkDropShadowImageFilter::Create(0.0f, 7.0f, 3.0f, 0.0f, SK_ColorBLUE),
138 SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE),
139 SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, cfif),
140 SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, NULL, &cropRect),
141 SkDropShadowImageFilter::Create(7.0f, 7.0f, 3.0f, 3.0f, SK_ColorBLUE, NULL, &bogusRect),
142 };
143
144 SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64));
145 SkScalar MARGIN = SkIntToScalar(16);
146 SkScalar DX = r.width() + MARGIN;
147 SkScalar DY = r.height() + MARGIN;
148
149 canvas->translate(MARGIN, MARGIN);
150 for (size_t j = 0; j < SK_ARRAY_COUNT(drawProc); ++j) {
151 canvas->save();
152 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
153 drawProc[j](canvas, r, filters[i]);
154 canvas->translate(0, DY);
155 }
156 canvas->restore();
157 canvas->translate(DX, 0);
158 }
159
160 for(size_t j = 0; j < SK_ARRAY_COUNT(filters); ++j) {
161 SkSafeUnref(filters[j]);
162 }
163 }
164
165 private:
166 typedef GM INHERITED;
167 };
168
169 ///////////////////////////////////////////////////////////////////////////////
170
MyFactory(void *)171 static skiagm::GM* MyFactory(void*) { return new DropShadowImageFilterGM; }
172 static skiagm::GMRegistry reg(MyFactory);
173