blob: 6d01722d0ad5e471ccd63500291915d6f6be115d [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"
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000015
16#define WIDTH 500
17#define HEIGHT 500
18
Robert Phillips22c57ab2016-12-19 16:51:53 -050019static void draw_rects(SkCanvas* canvas) {
senorblanco1ea67a32016-01-19 08:50:18 -080020 SkPaint rectPaint;
Robert Phillips22c57ab2016-12-19 16:51:53 -050021 rectPaint.setColor(SK_ColorBLUE);
senorblanco1ea67a32016-01-19 08:50:18 -080022 canvas->drawRect(SkRect::MakeXYWH(0, 0, WIDTH / 2, HEIGHT / 2), rectPaint);
23 rectPaint.setColor(0xBFFF0000);
24 canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, 0, WIDTH / 2, HEIGHT / 2), rectPaint);
25 rectPaint.setColor(0x3F00FF00);
26 canvas->drawRect(SkRect::MakeXYWH(0, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint);
Robert Phillips22c57ab2016-12-19 16:51:53 -050027 rectPaint.setColor(SK_ColorTRANSPARENT);
senorblanco1ea67a32016-01-19 08:50:18 -080028 canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint);
29}
30
Robert Phillips22c57ab2016-12-19 16:51:53 -050031static SkPaint create_filter_paint(SkImageFilter::CropRect* cropRect = nullptr) {
senorblanco1ea67a32016-01-19 08:50:18 -080032 SkIRect rects[2];
33 rects[0] = SkIRect::MakeXYWH(0, 150, WIDTH, HEIGHT - 300);
34 rects[1] = SkIRect::MakeXYWH(150, 0, WIDTH - 300, HEIGHT);
35 SkRegion region;
36 region.setRects(rects, 2);
37
38 SkPaint paint;
Robert Phillips22c57ab2016-12-19 16:51:53 -050039 sk_sp<SkImageFilter> offset(SkOffsetImageFilter::Make(25, 25, nullptr));
40 paint.setImageFilter(SkAlphaThresholdFilter::Make(region, 0.2f, 0.7f, std::move(offset), cropRect));
senorblanco1ea67a32016-01-19 08:50:18 -080041 return paint;
42}
43
Robert Phillips22c57ab2016-12-19 16:51:53 -050044class ImageAlphaThresholdGM : public skiagm::GM {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000045public:
Brian Osman01015fb2016-10-03 11:06:45 -040046 ImageAlphaThresholdGM(bool useCropRect) : fUseCropRect(useCropRect) {
Robert Phillips22c57ab2016-12-19 16:51:53 -050047 this->setBGColor(SK_ColorWHITE);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000048 }
49
50protected:
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000051
mtklein36352bf2015-03-25 18:17:31 -070052 SkString onShortName() override {
robertphillipsaf9b8c82016-04-12 11:02:25 -070053 if (fUseCropRect) {
54 return SkString("imagealphathreshold_crop");
55 }
56
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000057 return SkString("imagealphathreshold");
58 }
59
mtklein36352bf2015-03-25 18:17:31 -070060 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070061 return SkISize::Make(WIDTH, HEIGHT);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000062 }
63
mtklein36352bf2015-03-25 18:17:31 -070064 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000065 SkMatrix matrix;
66 matrix.reset();
67 matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f);
commit-bot@chromium.org9109e182014-01-07 16:04:01 +000068 matrix.postScale(.8f, .8f);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000069
70 canvas->concat(matrix);
71
robertphillipsaf9b8c82016-04-12 11:02:25 -070072 SkRect r = SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100);
73 SkImageFilter::CropRect cropRect(r);
74
75 SkPaint paint = create_filter_paint(fUseCropRect ? &cropRect : nullptr);
halcanary96fcdcc2015-08-27 07:41:13 -070076 canvas->saveLayer(nullptr, &paint);
senorblanco1ea67a32016-01-19 08:50:18 -080077 draw_rects(canvas);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000078
79 canvas->restore();
80 }
81
82private:
robertphillipsaf9b8c82016-04-12 11:02:25 -070083 bool fUseCropRect;
84
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000085 typedef GM INHERITED;
86};
87
Robert Phillips22c57ab2016-12-19 16:51:53 -050088// Create a 'width' x 'height' SkSurface that matches the colorType of 'canvas' as
89// best we can
90static sk_sp<SkSurface> make_color_matching_surface(SkCanvas* canvas, int width, int height,
91 SkAlphaType alphaType) {
robertphillipsaf9b8c82016-04-12 11:02:25 -070092
Robert Phillips22c57ab2016-12-19 16:51:53 -050093 SkColorType ct = canvas->imageInfo().colorType();
Mike Reed693fdbd2017-01-12 10:13:40 -050094 sk_sp<SkColorSpace> cs(canvas->imageInfo().refColorSpace());
Robert Phillips22c57ab2016-12-19 16:51:53 -050095
96 if (kUnknown_SkColorType == ct) {
97 // For backends that aren't yet color-space aware we just fallback to N32.
98 ct = kN32_SkColorType;
99 cs = nullptr;
100 }
101
102 SkImageInfo info = SkImageInfo::Make(width, height, ct, alphaType, std::move(cs));
103
104 sk_sp<SkSurface> result = canvas->makeSurface(info);
105 if (!result) {
106 result = SkSurface::MakeRaster(info);
107 }
108
109 return result;
110}
111
112class ImageAlphaThresholdSurfaceGM : public skiagm::GM {
senorblanco1ea67a32016-01-19 08:50:18 -0800113public:
114 ImageAlphaThresholdSurfaceGM() {
115 this->setBGColor(0xFFFFFFFF);
116 }
117
118protected:
119 SkString onShortName() override {
120 return SkString("imagealphathreshold_surface");
121 }
122
123 SkISize onISize() override {
124 return SkISize::Make(WIDTH, HEIGHT);
125 }
126
127 void onDraw(SkCanvas* canvas) override {
Robert Phillips22c57ab2016-12-19 16:51:53 -0500128 SkMatrix matrix;
129 matrix.reset();
130 matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f);
131 matrix.postScale(.8f, .8f);
132
133 canvas->concat(matrix);
134
135 sk_sp<SkSurface> surface(make_color_matching_surface(canvas, WIDTH, HEIGHT,
136 kPremul_SkAlphaType));
Robert Phillips253b4dd2016-12-20 19:05:09 -0500137 if (!surface) {
138 return;
139 }
Robert Phillips22c57ab2016-12-19 16:51:53 -0500140
141 surface->getCanvas()->clear(SK_ColorTRANSPARENT);
senorblanco1ea67a32016-01-19 08:50:18 -0800142 draw_rects(surface->getCanvas());
143
senorblanco1ea67a32016-01-19 08:50:18 -0800144 SkPaint paint = create_filter_paint();
145 canvas->clipRect(SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100));
reed9ce9d672016-03-17 10:51:11 -0700146 canvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, &paint);
senorblanco1ea67a32016-01-19 08:50:18 -0800147 }
148
149private:
Robert Phillips22c57ab2016-12-19 16:51:53 -0500150 typedef skiagm::GM INHERITED;
senorblanco1ea67a32016-01-19 08:50:18 -0800151};
152
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000153//////////////////////////////////////////////////////////////////////////////
154
robertphillipsaf9b8c82016-04-12 11:02:25 -0700155DEF_GM(return new ImageAlphaThresholdGM(true);)
156DEF_GM(return new ImageAlphaThresholdGM(false);)
senorblanco1ea67a32016-01-19 08:50:18 -0800157DEF_GM(return new ImageAlphaThresholdSurfaceGM();)
Brian Osmana4aa1332017-10-19 12:54:28 -0400158
159//////////////////////////////////////////////////////////////////////////////
160
161static 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
179DEF_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}