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 "SkCanvas.h"
10 #include "SkColorFilter.h"
11 #include "SkColorPriv.h"
12 #include "SkShader.h"
13
14 #include "SkBlurImageFilter.h"
15 #include "SkColorFilterImageFilter.h"
16 #include "SkMergeImageFilter.h"
17 #include "SkOffsetImageFilter.h"
18 #include "SkTestImageFilters.h"
19
20 #define FILTER_WIDTH SkIntToScalar(150)
21 #define FILTER_HEIGHT SkIntToScalar(200)
22
make0()23 static SkImageFilter* make0() { return new SkDownSampleImageFilter(SK_Scalar1 / 5); }
make1()24 static SkImageFilter* make1() { return new SkOffsetImageFilter(SkIntToScalar(16), SkIntToScalar(16)); }
make2()25 static SkImageFilter* make2() {
26 SkColorFilter* cf = SkColorFilter::CreateModeFilter(SK_ColorBLUE,
27 SkXfermode::kSrcIn_Mode);
28 SkAutoUnref aur(cf);
29 return SkColorFilterImageFilter::Create(cf);
30 }
make3()31 static SkImageFilter* make3() {
32 return new SkBlurImageFilter(8, 0);
33 }
34
make4()35 static SkImageFilter* make4() {
36 SkImageFilter* outer = new SkOffsetImageFilter(SkIntToScalar(16), SkIntToScalar(16));
37 SkImageFilter* inner = new SkDownSampleImageFilter(SK_Scalar1 / 5);
38 SkAutoUnref aur0(outer);
39 SkAutoUnref aur1(inner);
40 return new SkComposeImageFilter(outer, inner);
41 }
make5()42 static SkImageFilter* make5() {
43 SkImageFilter* first = new SkOffsetImageFilter(SkIntToScalar(16), SkIntToScalar(16));
44 SkImageFilter* second = new SkDownSampleImageFilter(SK_Scalar1 / 5);
45 SkAutoUnref aur0(first);
46 SkAutoUnref aur1(second);
47 return new SkMergeImageFilter(first, second);
48 }
49
make6()50 static SkImageFilter* make6() {
51 SkImageFilter* outer = new SkOffsetImageFilter(SkIntToScalar(16), SkIntToScalar(16));
52 SkImageFilter* inner = new SkDownSampleImageFilter(SK_Scalar1 / 5);
53 SkAutoUnref aur0(outer);
54 SkAutoUnref aur1(inner);
55 SkImageFilter* compose = new SkComposeImageFilter(outer, inner);
56 SkAutoUnref aur2(compose);
57
58 SkColorFilter* cf = SkColorFilter::CreateModeFilter(0x880000FF,
59 SkXfermode::kSrcIn_Mode);
60 SkAutoUnref aur3(cf);
61 SkImageFilter* blue = SkColorFilterImageFilter::Create(cf);
62 SkAutoUnref aur4(blue);
63
64 return new SkMergeImageFilter(compose, blue);
65 }
66
make7()67 static SkImageFilter* make7() {
68 SkImageFilter* outer = new SkOffsetImageFilter(SkIntToScalar(16), SkIntToScalar(16));
69 SkImageFilter* inner = make3();
70 SkAutoUnref aur0(outer);
71 SkAutoUnref aur1(inner);
72 SkImageFilter* compose = new SkComposeImageFilter(outer, inner);
73 SkAutoUnref aur2(compose);
74
75 SkColorFilter* cf = SkColorFilter::CreateModeFilter(0x880000FF,
76 SkXfermode::kSrcIn_Mode);
77 SkAutoUnref aur3(cf);
78 SkImageFilter* blue = SkColorFilterImageFilter::Create(cf);
79 SkAutoUnref aur4(blue);
80
81 return new SkMergeImageFilter(compose, blue);
82 }
83
draw0(SkCanvas * canvas)84 static void draw0(SkCanvas* canvas) {
85 SkPaint p;
86 p.setAntiAlias(true);
87 SkRect r = SkRect::MakeWH(FILTER_WIDTH, FILTER_HEIGHT);
88 r.inset(SK_Scalar1 * 12, SK_Scalar1 * 12);
89 p.setColor(SK_ColorRED);
90 canvas->drawOval(r, p);
91 }
92
93 class TestImageFiltersGM : public skiagm::GM {
94 public:
TestImageFiltersGM()95 TestImageFiltersGM () {}
96
97 protected:
98
onShortName()99 virtual SkString onShortName() {
100 return SkString("testimagefilters");
101 }
102
onISize()103 virtual SkISize onISize() { return SkISize::Make(700, 460); }
104
onDraw(SkCanvas * canvas)105 virtual void onDraw(SkCanvas* canvas) {
106 // this->drawSizeBounds(canvas, 0xFFCCCCCC);
107
108 static SkImageFilter* (*gFilterProc[])() = {
109 make0, make1, make2, make3, make4, make5, make6, make7
110 };
111
112 const SkRect bounds = SkRect::MakeWH(FILTER_WIDTH, FILTER_HEIGHT);
113
114 const SkScalar dx = bounds.width() * 8 / 7;
115 const SkScalar dy = bounds.height() * 8 / 7;
116
117 canvas->translate(SkIntToScalar(8), SkIntToScalar(8));
118
119 for (size_t i = 0; i < SK_ARRAY_COUNT(gFilterProc); ++i) {
120 int ix = i % 4;
121 int iy = i / 4;
122
123 SkAutoCanvasRestore acr(canvas, true);
124 canvas->translate(ix * dx, iy * dy);
125
126 SkPaint p;
127 p.setStyle(SkPaint::kStroke_Style);
128 canvas->drawRect(bounds, p);
129
130 SkPaint paint;
131 paint.setImageFilter(gFilterProc[i]())->unref();
132 canvas->saveLayer(&bounds, &paint);
133 draw0(canvas);
134 }
135 }
136
137 private:
138 typedef GM INHERITED;
139 };
140
141 //////////////////////////////////////////////////////////////////////////////
142
MyFactory(void *)143 static skiagm::GM* MyFactory(void*) { return new TestImageFiltersGM; }
144 static skiagm::GMRegistry reg(MyFactory);
145