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