blob: 547e812e1170764fdf2b761d02654ac4878d1fcf [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"
Robert Phillips22c57ab2016-12-19 16:51:53 -050010#include "SkOffsetImageFilter.h"
senorblanco1ea67a32016-01-19 08:50:18 -080011#include "SkSurface.h"
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000012
13#define WIDTH 500
14#define HEIGHT 500
15
Robert Phillips22c57ab2016-12-19 16:51:53 -050016static void draw_rects(SkCanvas* canvas) {
senorblanco1ea67a32016-01-19 08:50:18 -080017 SkPaint rectPaint;
Robert Phillips22c57ab2016-12-19 16:51:53 -050018 rectPaint.setColor(SK_ColorBLUE);
senorblanco1ea67a32016-01-19 08:50:18 -080019 canvas->drawRect(SkRect::MakeXYWH(0, 0, WIDTH / 2, HEIGHT / 2), rectPaint);
20 rectPaint.setColor(0xBFFF0000);
21 canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, 0, WIDTH / 2, HEIGHT / 2), rectPaint);
22 rectPaint.setColor(0x3F00FF00);
23 canvas->drawRect(SkRect::MakeXYWH(0, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint);
Robert Phillips22c57ab2016-12-19 16:51:53 -050024 rectPaint.setColor(SK_ColorTRANSPARENT);
senorblanco1ea67a32016-01-19 08:50:18 -080025 canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint);
26}
27
Robert Phillips22c57ab2016-12-19 16:51:53 -050028static SkPaint create_filter_paint(SkImageFilter::CropRect* cropRect = nullptr) {
senorblanco1ea67a32016-01-19 08:50:18 -080029 SkIRect rects[2];
30 rects[0] = SkIRect::MakeXYWH(0, 150, WIDTH, HEIGHT - 300);
31 rects[1] = SkIRect::MakeXYWH(150, 0, WIDTH - 300, HEIGHT);
32 SkRegion region;
33 region.setRects(rects, 2);
34
35 SkPaint paint;
Robert Phillips22c57ab2016-12-19 16:51:53 -050036 sk_sp<SkImageFilter> offset(SkOffsetImageFilter::Make(25, 25, nullptr));
37 paint.setImageFilter(SkAlphaThresholdFilter::Make(region, 0.2f, 0.7f, std::move(offset), cropRect));
senorblanco1ea67a32016-01-19 08:50:18 -080038 return paint;
39}
40
Robert Phillips22c57ab2016-12-19 16:51:53 -050041class ImageAlphaThresholdGM : public skiagm::GM {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000042public:
Brian Osman01015fb2016-10-03 11:06:45 -040043 ImageAlphaThresholdGM(bool useCropRect) : fUseCropRect(useCropRect) {
Robert Phillips22c57ab2016-12-19 16:51:53 -050044 this->setBGColor(SK_ColorWHITE);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000045 }
46
47protected:
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000048
mtklein36352bf2015-03-25 18:17:31 -070049 SkString onShortName() override {
robertphillipsaf9b8c82016-04-12 11:02:25 -070050 if (fUseCropRect) {
51 return SkString("imagealphathreshold_crop");
52 }
53
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000054 return SkString("imagealphathreshold");
55 }
56
mtklein36352bf2015-03-25 18:17:31 -070057 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070058 return SkISize::Make(WIDTH, HEIGHT);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000059 }
60
mtklein36352bf2015-03-25 18:17:31 -070061 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000062 SkMatrix matrix;
63 matrix.reset();
64 matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f);
commit-bot@chromium.org9109e182014-01-07 16:04:01 +000065 matrix.postScale(.8f, .8f);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000066
67 canvas->concat(matrix);
68
robertphillipsaf9b8c82016-04-12 11:02:25 -070069 SkRect r = SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100);
70 SkImageFilter::CropRect cropRect(r);
71
72 SkPaint paint = create_filter_paint(fUseCropRect ? &cropRect : nullptr);
halcanary96fcdcc2015-08-27 07:41:13 -070073 canvas->saveLayer(nullptr, &paint);
senorblanco1ea67a32016-01-19 08:50:18 -080074 draw_rects(canvas);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000075
76 canvas->restore();
77 }
78
79private:
robertphillipsaf9b8c82016-04-12 11:02:25 -070080 bool fUseCropRect;
81
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000082 typedef GM INHERITED;
83};
84
Robert Phillips22c57ab2016-12-19 16:51:53 -050085// Create a 'width' x 'height' SkSurface that matches the colorType of 'canvas' as
86// best we can
87static sk_sp<SkSurface> make_color_matching_surface(SkCanvas* canvas, int width, int height,
88 SkAlphaType alphaType) {
robertphillipsaf9b8c82016-04-12 11:02:25 -070089
Robert Phillips22c57ab2016-12-19 16:51:53 -050090 SkColorType ct = canvas->imageInfo().colorType();
Mike Reed693fdbd2017-01-12 10:13:40 -050091 sk_sp<SkColorSpace> cs(canvas->imageInfo().refColorSpace());
Robert Phillips22c57ab2016-12-19 16:51:53 -050092
93 if (kUnknown_SkColorType == ct) {
94 // For backends that aren't yet color-space aware we just fallback to N32.
95 ct = kN32_SkColorType;
96 cs = nullptr;
97 }
98
99 SkImageInfo info = SkImageInfo::Make(width, height, ct, alphaType, std::move(cs));
100
101 sk_sp<SkSurface> result = canvas->makeSurface(info);
102 if (!result) {
103 result = SkSurface::MakeRaster(info);
104 }
105
106 return result;
107}
108
109class ImageAlphaThresholdSurfaceGM : public skiagm::GM {
senorblanco1ea67a32016-01-19 08:50:18 -0800110public:
111 ImageAlphaThresholdSurfaceGM() {
112 this->setBGColor(0xFFFFFFFF);
113 }
114
115protected:
116 SkString onShortName() override {
117 return SkString("imagealphathreshold_surface");
118 }
119
120 SkISize onISize() override {
121 return SkISize::Make(WIDTH, HEIGHT);
122 }
123
124 void onDraw(SkCanvas* canvas) override {
Robert Phillips22c57ab2016-12-19 16:51:53 -0500125 SkMatrix matrix;
126 matrix.reset();
127 matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f);
128 matrix.postScale(.8f, .8f);
129
130 canvas->concat(matrix);
131
132 sk_sp<SkSurface> surface(make_color_matching_surface(canvas, WIDTH, HEIGHT,
133 kPremul_SkAlphaType));
Robert Phillips253b4dd2016-12-20 19:05:09 -0500134 if (!surface) {
135 return;
136 }
Robert Phillips22c57ab2016-12-19 16:51:53 -0500137
138 surface->getCanvas()->clear(SK_ColorTRANSPARENT);
senorblanco1ea67a32016-01-19 08:50:18 -0800139 draw_rects(surface->getCanvas());
140
senorblanco1ea67a32016-01-19 08:50:18 -0800141 SkPaint paint = create_filter_paint();
142 canvas->clipRect(SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100));
reed9ce9d672016-03-17 10:51:11 -0700143 canvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, &paint);
senorblanco1ea67a32016-01-19 08:50:18 -0800144 }
145
146private:
Robert Phillips22c57ab2016-12-19 16:51:53 -0500147 typedef skiagm::GM INHERITED;
senorblanco1ea67a32016-01-19 08:50:18 -0800148};
149
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000150//////////////////////////////////////////////////////////////////////////////
151
robertphillipsaf9b8c82016-04-12 11:02:25 -0700152DEF_GM(return new ImageAlphaThresholdGM(true);)
153DEF_GM(return new ImageAlphaThresholdGM(false);)
senorblanco1ea67a32016-01-19 08:50:18 -0800154DEF_GM(return new ImageAlphaThresholdSurfaceGM();)