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 "SkAlphaThresholdFilter.h"
10 #include "SkImageSource.h"
11 #include "SkOffsetImageFilter.h"
12 #include "SkRandom.h"
13 #include "SkRegion.h"
14 #include "SkSurface.h"
15 #include "sk_tool_utils.h"
16
17 #define WIDTH 500
18 #define HEIGHT 500
19
draw_rects(SkCanvas * canvas)20 static void draw_rects(SkCanvas* canvas) {
21 SkPaint rectPaint;
22 rectPaint.setColor(SK_ColorBLUE);
23 canvas->drawRect(SkRect::MakeXYWH(0, 0, WIDTH / 2, HEIGHT / 2), rectPaint);
24 rectPaint.setColor(0xBFFF0000);
25 canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, 0, WIDTH / 2, HEIGHT / 2), rectPaint);
26 rectPaint.setColor(0x3F00FF00);
27 canvas->drawRect(SkRect::MakeXYWH(0, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint);
28 rectPaint.setColor(SK_ColorTRANSPARENT);
29 canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint);
30 }
31
create_filter_paint(SkImageFilter::CropRect * cropRect=nullptr)32 static SkPaint create_filter_paint(SkImageFilter::CropRect* cropRect = nullptr) {
33 SkIRect rects[2];
34 rects[0] = SkIRect::MakeXYWH(0, 150, WIDTH, HEIGHT - 300);
35 rects[1] = SkIRect::MakeXYWH(150, 0, WIDTH - 300, HEIGHT);
36 SkRegion region;
37 region.setRects(rects, 2);
38
39 SkPaint paint;
40 sk_sp<SkImageFilter> offset(SkOffsetImageFilter::Make(25, 25, nullptr));
41 paint.setImageFilter(SkAlphaThresholdFilter::Make(region, 0.2f, 0.7f, std::move(offset), cropRect));
42 return paint;
43 }
44
45 class ImageAlphaThresholdGM : public skiagm::GM {
46 public:
ImageAlphaThresholdGM(bool useCropRect)47 ImageAlphaThresholdGM(bool useCropRect) : fUseCropRect(useCropRect) {
48 this->setBGColor(SK_ColorWHITE);
49 }
50
51 protected:
52
onShortName()53 SkString onShortName() override {
54 if (fUseCropRect) {
55 return SkString("imagealphathreshold_crop");
56 }
57
58 return SkString("imagealphathreshold");
59 }
60
onISize()61 SkISize onISize() override {
62 return SkISize::Make(WIDTH, HEIGHT);
63 }
64
onDraw(SkCanvas * canvas)65 void onDraw(SkCanvas* canvas) override {
66 SkMatrix matrix;
67 matrix.reset();
68 matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f);
69 matrix.postScale(.8f, .8f);
70
71 canvas->concat(matrix);
72
73 SkRect r = SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100);
74 SkImageFilter::CropRect cropRect(r);
75
76 SkPaint paint = create_filter_paint(fUseCropRect ? &cropRect : nullptr);
77 canvas->saveLayer(nullptr, &paint);
78 draw_rects(canvas);
79
80 canvas->restore();
81 }
82
83 private:
84 bool fUseCropRect;
85
86 typedef GM INHERITED;
87 };
88
89 // Create a 'width' x 'height' SkSurface that matches the colorType of 'canvas' as
90 // best we can
make_color_matching_surface(SkCanvas * canvas,int width,int height,SkAlphaType at)91 static sk_sp<SkSurface> make_color_matching_surface(SkCanvas* canvas, int width, int height,
92 SkAlphaType at) {
93
94 SkColorType ct = canvas->imageInfo().colorType();
95 sk_sp<SkColorSpace> cs(canvas->imageInfo().refColorSpace());
96
97 if (kUnknown_SkColorType == ct) {
98 // For backends that aren't yet color-space aware we just fallback to N32.
99 ct = kN32_SkColorType;
100 cs = nullptr;
101 } else if (SkColorTypeIsAlwaysOpaque(ct)) {
102 at = kOpaque_SkAlphaType;
103 }
104
105 SkImageInfo info = SkImageInfo::Make(width, height, ct, at, std::move(cs));
106
107 return sk_tool_utils::makeSurface(canvas, info);
108 }
109
110 class ImageAlphaThresholdSurfaceGM : public skiagm::GM {
111 public:
ImageAlphaThresholdSurfaceGM()112 ImageAlphaThresholdSurfaceGM() {
113 this->setBGColor(0xFFFFFFFF);
114 }
115
116 protected:
onShortName()117 SkString onShortName() override {
118 return SkString("imagealphathreshold_surface");
119 }
120
onISize()121 SkISize onISize() override {
122 return SkISize::Make(WIDTH, HEIGHT);
123 }
124
onDraw(SkCanvas * canvas,SkString * errorMsg)125 DrawResult onDraw(SkCanvas* canvas, SkString* errorMsg) override {
126 SkMatrix matrix;
127 matrix.reset();
128 matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f);
129 matrix.postScale(.8f, .8f);
130
131 canvas->concat(matrix);
132
133 sk_sp<SkSurface> surface(make_color_matching_surface(canvas, WIDTH, HEIGHT,
134 kPremul_SkAlphaType));
135 if (!surface) {
136 *errorMsg = "make_color_matching_surface failed";
137 return DrawResult::kFail;
138 }
139
140 surface->getCanvas()->clear(SK_ColorTRANSPARENT);
141 draw_rects(surface->getCanvas());
142
143 SkPaint paint = create_filter_paint();
144 canvas->clipRect(SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100));
145 canvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, &paint);
146 return DrawResult::kOk;
147 }
148
149 private:
150 typedef skiagm::GM INHERITED;
151 };
152
153 //////////////////////////////////////////////////////////////////////////////
154
155 DEF_GM(return new ImageAlphaThresholdGM(true);)
DEF_GM(return new ImageAlphaThresholdGM (false);)156 DEF_GM(return new ImageAlphaThresholdGM(false);)
157 DEF_GM(return new ImageAlphaThresholdSurfaceGM();)
158
159 //////////////////////////////////////////////////////////////////////////////
160
161 static sk_sp<SkImage> make_img() {
162 SkBitmap bitmap;
163 bitmap.allocPixels(SkImageInfo::MakeS32(WIDTH, HEIGHT, kPremul_SkAlphaType));
164 SkCanvas canvas(bitmap);
165
166 SkPaint paint;
167 SkRect rect = SkRect::MakeWH(WIDTH, HEIGHT);
168 SkRandom rnd;
169
170 while (!rect.isEmpty()) {
171 paint.setColor(rnd.nextU() | (0xFF << 24));
172 canvas.drawRect(rect, paint);
173 rect.inset(25, 25);
174 }
175
176 return SkImage::MakeFromBitmap(bitmap);
177 }
178
179 DEF_SIMPLE_GM_BG(imagealphathreshold_image, canvas, WIDTH * 2, HEIGHT, SK_ColorBLACK) {
180 sk_sp<SkImage> image(make_img());
181
182 SkIRect rects[2];
183 rects[0] = SkIRect::MakeXYWH(0, 150, WIDTH, HEIGHT - 300);
184 rects[1] = SkIRect::MakeXYWH(150, 0, WIDTH - 300, HEIGHT);
185 SkRegion region;
186 region.setRects(rects, 2);
187
188 SkPaint filterPaint;
189 sk_sp<SkImageFilter> imageSource(SkImageSource::Make(image));
190 filterPaint.setImageFilter(SkAlphaThresholdFilter::Make(region, 0.2f, 0.7f,
191 std::move(imageSource)));
192
193 canvas->saveLayer(nullptr, &filterPaint);
194 canvas->restore();
195 canvas->translate(WIDTH, 0);
196 canvas->drawImage(image, 0, 0);
197 }
198