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