blob: 3783bfcb3abcbe4c31d944d5fd8d8074717ca599 [file] [log] [blame]
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +00001/*
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"
Brian Osmana4aa1332017-10-19 12:54:28 -040010#include "SkImageSource.h"
Robert Phillips22c57ab2016-12-19 16:51:53 -050011#include "SkOffsetImageFilter.h"
Brian Osmana4aa1332017-10-19 12:54:28 -040012#include "SkRandom.h"
Mike Reed267be7f2017-02-13 09:32:54 -050013#include "SkRegion.h"
senorblanco1ea67a32016-01-19 08:50:18 -080014#include "SkSurface.h"
Mike Reed46596ae2018-01-02 15:40:29 -050015#include "sk_tool_utils.h"
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000016
17#define WIDTH 500
18#define HEIGHT 500
19
Robert Phillips22c57ab2016-12-19 16:51:53 -050020static void draw_rects(SkCanvas* canvas) {
senorblanco1ea67a32016-01-19 08:50:18 -080021 SkPaint rectPaint;
Robert Phillips22c57ab2016-12-19 16:51:53 -050022 rectPaint.setColor(SK_ColorBLUE);
senorblanco1ea67a32016-01-19 08:50:18 -080023 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);
Robert Phillips22c57ab2016-12-19 16:51:53 -050028 rectPaint.setColor(SK_ColorTRANSPARENT);
senorblanco1ea67a32016-01-19 08:50:18 -080029 canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint);
30}
31
Robert Phillips22c57ab2016-12-19 16:51:53 -050032static SkPaint create_filter_paint(SkImageFilter::CropRect* cropRect = nullptr) {
senorblanco1ea67a32016-01-19 08:50:18 -080033 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;
Robert Phillips22c57ab2016-12-19 16:51:53 -050040 sk_sp<SkImageFilter> offset(SkOffsetImageFilter::Make(25, 25, nullptr));
41 paint.setImageFilter(SkAlphaThresholdFilter::Make(region, 0.2f, 0.7f, std::move(offset), cropRect));
senorblanco1ea67a32016-01-19 08:50:18 -080042 return paint;
43}
44
Robert Phillips22c57ab2016-12-19 16:51:53 -050045class ImageAlphaThresholdGM : public skiagm::GM {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000046public:
Brian Osman01015fb2016-10-03 11:06:45 -040047 ImageAlphaThresholdGM(bool useCropRect) : fUseCropRect(useCropRect) {
Robert Phillips22c57ab2016-12-19 16:51:53 -050048 this->setBGColor(SK_ColorWHITE);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000049 }
50
51protected:
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000052
mtklein36352bf2015-03-25 18:17:31 -070053 SkString onShortName() override {
robertphillipsaf9b8c82016-04-12 11:02:25 -070054 if (fUseCropRect) {
55 return SkString("imagealphathreshold_crop");
56 }
57
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000058 return SkString("imagealphathreshold");
59 }
60
mtklein36352bf2015-03-25 18:17:31 -070061 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070062 return SkISize::Make(WIDTH, HEIGHT);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000063 }
64
mtklein36352bf2015-03-25 18:17:31 -070065 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000066 SkMatrix matrix;
67 matrix.reset();
68 matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f);
commit-bot@chromium.org9109e182014-01-07 16:04:01 +000069 matrix.postScale(.8f, .8f);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000070
71 canvas->concat(matrix);
72
robertphillipsaf9b8c82016-04-12 11:02:25 -070073 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);
halcanary96fcdcc2015-08-27 07:41:13 -070077 canvas->saveLayer(nullptr, &paint);
senorblanco1ea67a32016-01-19 08:50:18 -080078 draw_rects(canvas);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000079
80 canvas->restore();
81 }
82
83private:
robertphillipsaf9b8c82016-04-12 11:02:25 -070084 bool fUseCropRect;
85
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000086 typedef GM INHERITED;
87};
88
Robert Phillips22c57ab2016-12-19 16:51:53 -050089// Create a 'width' x 'height' SkSurface that matches the colorType of 'canvas' as
90// best we can
91static sk_sp<SkSurface> make_color_matching_surface(SkCanvas* canvas, int width, int height,
92 SkAlphaType alphaType) {
robertphillipsaf9b8c82016-04-12 11:02:25 -070093
Robert Phillips22c57ab2016-12-19 16:51:53 -050094 SkColorType ct = canvas->imageInfo().colorType();
Mike Reed693fdbd2017-01-12 10:13:40 -050095 sk_sp<SkColorSpace> cs(canvas->imageInfo().refColorSpace());
Robert Phillips22c57ab2016-12-19 16:51:53 -050096
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 }
102
103 SkImageInfo info = SkImageInfo::Make(width, height, ct, alphaType, std::move(cs));
104
Mike Reed46596ae2018-01-02 15:40:29 -0500105 return sk_tool_utils::makeSurface(canvas, info);
Robert Phillips22c57ab2016-12-19 16:51:53 -0500106}
107
108class ImageAlphaThresholdSurfaceGM : public skiagm::GM {
senorblanco1ea67a32016-01-19 08:50:18 -0800109public:
110 ImageAlphaThresholdSurfaceGM() {
111 this->setBGColor(0xFFFFFFFF);
112 }
113
114protected:
115 SkString onShortName() override {
116 return SkString("imagealphathreshold_surface");
117 }
118
119 SkISize onISize() override {
120 return SkISize::Make(WIDTH, HEIGHT);
121 }
122
123 void onDraw(SkCanvas* canvas) override {
Robert Phillips22c57ab2016-12-19 16:51:53 -0500124 SkMatrix matrix;
125 matrix.reset();
126 matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f);
127 matrix.postScale(.8f, .8f);
128
129 canvas->concat(matrix);
130
131 sk_sp<SkSurface> surface(make_color_matching_surface(canvas, WIDTH, HEIGHT,
132 kPremul_SkAlphaType));
Robert Phillips253b4dd2016-12-20 19:05:09 -0500133 if (!surface) {
134 return;
135 }
Robert Phillips22c57ab2016-12-19 16:51:53 -0500136
137 surface->getCanvas()->clear(SK_ColorTRANSPARENT);
senorblanco1ea67a32016-01-19 08:50:18 -0800138 draw_rects(surface->getCanvas());
139
senorblanco1ea67a32016-01-19 08:50:18 -0800140 SkPaint paint = create_filter_paint();
141 canvas->clipRect(SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100));
reed9ce9d672016-03-17 10:51:11 -0700142 canvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, &paint);
senorblanco1ea67a32016-01-19 08:50:18 -0800143 }
144
145private:
Robert Phillips22c57ab2016-12-19 16:51:53 -0500146 typedef skiagm::GM INHERITED;
senorblanco1ea67a32016-01-19 08:50:18 -0800147};
148
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000149//////////////////////////////////////////////////////////////////////////////
150
robertphillipsaf9b8c82016-04-12 11:02:25 -0700151DEF_GM(return new ImageAlphaThresholdGM(true);)
152DEF_GM(return new ImageAlphaThresholdGM(false);)
senorblanco1ea67a32016-01-19 08:50:18 -0800153DEF_GM(return new ImageAlphaThresholdSurfaceGM();)
Brian Osmana4aa1332017-10-19 12:54:28 -0400154
155//////////////////////////////////////////////////////////////////////////////
156
157static sk_sp<SkImage> make_img() {
158 SkBitmap bitmap;
159 bitmap.allocPixels(SkImageInfo::MakeS32(WIDTH, HEIGHT, kPremul_SkAlphaType));
160 SkCanvas canvas(bitmap);
161
162 SkPaint paint;
163 SkRect rect = SkRect::MakeWH(WIDTH, HEIGHT);
164 SkRandom rnd;
165
166 while (!rect.isEmpty()) {
167 paint.setColor(rnd.nextU() | (0xFF << 24));
168 canvas.drawRect(rect, paint);
169 rect.inset(25, 25);
170 }
171
172 return SkImage::MakeFromBitmap(bitmap);
173}
174
175DEF_SIMPLE_GM_BG(imagealphathreshold_image, canvas, WIDTH * 2, HEIGHT, SK_ColorBLACK) {
176 sk_sp<SkImage> image(make_img());
177
178 SkIRect rects[2];
179 rects[0] = SkIRect::MakeXYWH(0, 150, WIDTH, HEIGHT - 300);
180 rects[1] = SkIRect::MakeXYWH(150, 0, WIDTH - 300, HEIGHT);
181 SkRegion region;
182 region.setRects(rects, 2);
183
184 SkPaint filterPaint;
185 sk_sp<SkImageFilter> imageSource(SkImageSource::Make(image));
186 filterPaint.setImageFilter(SkAlphaThresholdFilter::Make(region, 0.2f, 0.7f,
187 std::move(imageSource)));
188
189 canvas->saveLayer(nullptr, &filterPaint);
190 canvas->restore();
191 canvas->translate(WIDTH, 0);
192 canvas->drawImage(image, 0, 0);
193}