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 #include "SkTextUtils.h"
15
16 #include "SkBlurImageFilter.h"
17 #include "SkMorphologyImageFilter.h"
18 #include "SkColorFilterImageFilter.h"
19 #include "SkMergeImageFilter.h"
20 #include "SkOffsetImageFilter.h"
21
22 ///////////////////////////////////////////////////////////////////////////////
23
draw_paint(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)24 static void draw_paint(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
25 SkPaint paint;
26 paint.setImageFilter(std::move(imf));
27 paint.setColor(SK_ColorBLACK);
28 canvas->save();
29 canvas->clipRect(r);
30 canvas->drawPaint(paint);
31 canvas->restore();
32 }
33
draw_path(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)34 static void draw_path(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
35 SkPaint paint;
36 paint.setColor(SK_ColorMAGENTA);
37 paint.setImageFilter(std::move(imf));
38 paint.setAntiAlias(true);
39 canvas->drawCircle(r.centerX(), r.centerY(), r.width()*2/5, paint);
40 }
41
draw_text(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)42 static void draw_text(SkCanvas* canvas, const SkRect& r, sk_sp<SkImageFilter> imf) {
43 SkPaint paint;
44 paint.setImageFilter(std::move(imf));
45 paint.setColor(SK_ColorGREEN);
46
47 SkFont font(sk_tool_utils::create_portable_typeface(), r.height()/2);
48 SkTextUtils::DrawString(canvas, "Text", r.centerX(), r.centerY(), font, paint, SkTextUtils::kCenter_Align);
49 }
50
draw_bitmap(SkCanvas * canvas,const SkRect & r,sk_sp<SkImageFilter> imf)51 static void draw_bitmap(SkCanvas* canvas, const SkRect& r, sk_sp<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, nullptr);
62
63 paint.setImageFilter(std::move(imf));
64 canvas->drawBitmap(bm, 0, 0, &paint);
65 }
66
67 ///////////////////////////////////////////////////////////////////////////////
68
69 class ImageFiltersCroppedGM : public skiagm::GM {
70 public:
ImageFiltersCroppedGM()71 ImageFiltersCroppedGM () {}
72
73 protected:
onShortName()74 SkString onShortName() override {
75 return SkString("imagefilterscropped");
76 }
77
onISize()78 SkISize onISize() override { return SkISize::Make(400, 960); }
79
make_checkerboard()80 void make_checkerboard() {
81 fCheckerboard.allocN32Pixels(80, 80);
82 SkCanvas canvas(fCheckerboard);
83 canvas.clear(SK_ColorTRANSPARENT);
84 SkPaint darkPaint;
85 darkPaint.setColor(0xFF404040);
86 SkPaint lightPaint;
87 lightPaint.setColor(0xFFA0A0A0);
88 for (int y = 0; y < 80; y += 16) {
89 for (int x = 0; x < 80; x += 16) {
90 canvas.save();
91 canvas.translate(SkIntToScalar(x), SkIntToScalar(y));
92 canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint);
93 canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint);
94 canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint);
95 canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint);
96 canvas.restore();
97 }
98 }
99 }
100
draw_frame(SkCanvas * canvas,const SkRect & r)101 void draw_frame(SkCanvas* canvas, const SkRect& r) {
102 SkPaint paint;
103 paint.setStyle(SkPaint::kStroke_Style);
104 paint.setColor(SK_ColorRED);
105 canvas->drawRect(r, paint);
106 }
107
onOnceBeforeDraw()108 void onOnceBeforeDraw() override{
109 make_checkerboard();
110 }
111
onDraw(SkCanvas * canvas)112 void onDraw(SkCanvas* canvas) override {
113 void (*drawProc[])(SkCanvas*, const SkRect&, sk_sp<SkImageFilter>) = {
114 draw_bitmap, draw_path, draw_paint, draw_text
115 };
116
117 sk_sp<SkColorFilter> cf(SkColorFilter::MakeModeFilter(SK_ColorBLUE,
118 SkBlendMode::kSrcIn));
119 SkImageFilter::CropRect cropRect(SkRect::Make(SkIRect::MakeXYWH(10, 10, 44, 44)),
120 SkImageFilter::CropRect::kHasAll_CropEdge);
121 SkImageFilter::CropRect bogusRect(SkRect::Make(SkIRect::MakeXYWH(-100, -100, 10, 10)),
122 SkImageFilter::CropRect::kHasAll_CropEdge);
123
124 sk_sp<SkImageFilter> offset(SkOffsetImageFilter::Make(SkIntToScalar(-10),
125 SkIntToScalar(-10),
126 nullptr));
127
128 sk_sp<SkImageFilter> cfOffset(SkColorFilterImageFilter::Make(cf, std::move(offset)));
129
130 sk_sp<SkImageFilter> erodeX(SkErodeImageFilter::Make(8, 0, nullptr, &cropRect));
131 sk_sp<SkImageFilter> erodeY(SkErodeImageFilter::Make(0, 8, nullptr, &cropRect));
132
133 sk_sp<SkImageFilter> filters[] = {
134 nullptr,
135 SkColorFilterImageFilter::Make(cf, nullptr, &cropRect),
136 SkBlurImageFilter::Make(0.0f, 0.0f, nullptr, &cropRect),
137 SkBlurImageFilter::Make(1.0f, 1.0f, nullptr, &cropRect),
138 SkBlurImageFilter::Make(8.0f, 0.0f, nullptr, &cropRect),
139 SkBlurImageFilter::Make(0.0f, 8.0f, nullptr, &cropRect),
140 SkBlurImageFilter::Make(8.0f, 8.0f, nullptr, &cropRect),
141 SkErodeImageFilter::Make(1, 1, nullptr, &cropRect),
142 SkErodeImageFilter::Make(8, 0, std::move(erodeY), &cropRect),
143 SkErodeImageFilter::Make(0, 8, std::move(erodeX), &cropRect),
144 SkErodeImageFilter::Make(8, 8, nullptr, &cropRect),
145 SkMergeImageFilter::Make(nullptr,
146 std::move(cfOffset),
147 &cropRect),
148 SkBlurImageFilter::Make(8.0f, 8.0f, nullptr, &bogusRect),
149 SkColorFilterImageFilter::Make(cf, nullptr, &bogusRect),
150 };
151
152 SkRect r = SkRect::MakeWH(SkIntToScalar(64), SkIntToScalar(64));
153 SkScalar MARGIN = SkIntToScalar(16);
154 SkScalar DX = r.width() + MARGIN;
155 SkScalar DY = r.height() + MARGIN;
156
157 canvas->translate(MARGIN, MARGIN);
158 for (size_t j = 0; j < SK_ARRAY_COUNT(drawProc); ++j) {
159 canvas->save();
160 for (size_t i = 0; i < SK_ARRAY_COUNT(filters); ++i) {
161 SkPaint paint;
162 canvas->drawBitmap(fCheckerboard, 0, 0);
163 drawProc[j](canvas, r, filters[i]);
164 canvas->translate(0, DY);
165 }
166 canvas->restore();
167 canvas->translate(DX, 0);
168 }
169 }
170
171 private:
172 SkBitmap fCheckerboard;
173 typedef GM INHERITED;
174 };
175
176 ///////////////////////////////////////////////////////////////////////////////
177
178 DEF_GM( return new ImageFiltersCroppedGM; )
179