blob: 4a350184358b2fe10116b17aa2e30b8af51d3602 [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"
Mike Reed267be7f2017-02-13 09:32:54 -050011#include "SkRegion.h"
senorblanco1ea67a32016-01-19 08:50:18 -080012#include "SkSurface.h"
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000013
14#define WIDTH 500
15#define HEIGHT 500
16
Robert Phillips22c57ab2016-12-19 16:51:53 -050017static void draw_rects(SkCanvas* canvas) {
senorblanco1ea67a32016-01-19 08:50:18 -080018 SkPaint rectPaint;
Robert Phillips22c57ab2016-12-19 16:51:53 -050019 rectPaint.setColor(SK_ColorBLUE);
senorblanco1ea67a32016-01-19 08:50:18 -080020 canvas->drawRect(SkRect::MakeXYWH(0, 0, WIDTH / 2, HEIGHT / 2), rectPaint);
21 rectPaint.setColor(0xBFFF0000);
22 canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, 0, WIDTH / 2, HEIGHT / 2), rectPaint);
23 rectPaint.setColor(0x3F00FF00);
24 canvas->drawRect(SkRect::MakeXYWH(0, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint);
Robert Phillips22c57ab2016-12-19 16:51:53 -050025 rectPaint.setColor(SK_ColorTRANSPARENT);
senorblanco1ea67a32016-01-19 08:50:18 -080026 canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint);
27}
28
Robert Phillips22c57ab2016-12-19 16:51:53 -050029static SkPaint create_filter_paint(SkImageFilter::CropRect* cropRect = nullptr) {
senorblanco1ea67a32016-01-19 08:50:18 -080030 SkIRect rects[2];
31 rects[0] = SkIRect::MakeXYWH(0, 150, WIDTH, HEIGHT - 300);
32 rects[1] = SkIRect::MakeXYWH(150, 0, WIDTH - 300, HEIGHT);
33 SkRegion region;
34 region.setRects(rects, 2);
35
36 SkPaint paint;
Robert Phillips22c57ab2016-12-19 16:51:53 -050037 sk_sp<SkImageFilter> offset(SkOffsetImageFilter::Make(25, 25, nullptr));
38 paint.setImageFilter(SkAlphaThresholdFilter::Make(region, 0.2f, 0.7f, std::move(offset), cropRect));
senorblanco1ea67a32016-01-19 08:50:18 -080039 return paint;
40}
41
Robert Phillips22c57ab2016-12-19 16:51:53 -050042class ImageAlphaThresholdGM : public skiagm::GM {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000043public:
Brian Osman01015fb2016-10-03 11:06:45 -040044 ImageAlphaThresholdGM(bool useCropRect) : fUseCropRect(useCropRect) {
Robert Phillips22c57ab2016-12-19 16:51:53 -050045 this->setBGColor(SK_ColorWHITE);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000046 }
47
48protected:
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000049
mtklein36352bf2015-03-25 18:17:31 -070050 SkString onShortName() override {
robertphillipsaf9b8c82016-04-12 11:02:25 -070051 if (fUseCropRect) {
52 return SkString("imagealphathreshold_crop");
53 }
54
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000055 return SkString("imagealphathreshold");
56 }
57
mtklein36352bf2015-03-25 18:17:31 -070058 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070059 return SkISize::Make(WIDTH, HEIGHT);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000060 }
61
mtklein36352bf2015-03-25 18:17:31 -070062 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000063 SkMatrix matrix;
64 matrix.reset();
65 matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f);
commit-bot@chromium.org9109e182014-01-07 16:04:01 +000066 matrix.postScale(.8f, .8f);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000067
68 canvas->concat(matrix);
69
robertphillipsaf9b8c82016-04-12 11:02:25 -070070 SkRect r = SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100);
71 SkImageFilter::CropRect cropRect(r);
72
73 SkPaint paint = create_filter_paint(fUseCropRect ? &cropRect : nullptr);
halcanary96fcdcc2015-08-27 07:41:13 -070074 canvas->saveLayer(nullptr, &paint);
senorblanco1ea67a32016-01-19 08:50:18 -080075 draw_rects(canvas);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000076
77 canvas->restore();
78 }
79
80private:
robertphillipsaf9b8c82016-04-12 11:02:25 -070081 bool fUseCropRect;
82
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000083 typedef GM INHERITED;
84};
85
Robert Phillips22c57ab2016-12-19 16:51:53 -050086// Create a 'width' x 'height' SkSurface that matches the colorType of 'canvas' as
87// best we can
88static sk_sp<SkSurface> make_color_matching_surface(SkCanvas* canvas, int width, int height,
89 SkAlphaType alphaType) {
robertphillipsaf9b8c82016-04-12 11:02:25 -070090
Robert Phillips22c57ab2016-12-19 16:51:53 -050091 SkColorType ct = canvas->imageInfo().colorType();
Mike Reed693fdbd2017-01-12 10:13:40 -050092 sk_sp<SkColorSpace> cs(canvas->imageInfo().refColorSpace());
Robert Phillips22c57ab2016-12-19 16:51:53 -050093
94 if (kUnknown_SkColorType == ct) {
95 // For backends that aren't yet color-space aware we just fallback to N32.
96 ct = kN32_SkColorType;
97 cs = nullptr;
98 }
99
100 SkImageInfo info = SkImageInfo::Make(width, height, ct, alphaType, std::move(cs));
101
102 sk_sp<SkSurface> result = canvas->makeSurface(info);
103 if (!result) {
104 result = SkSurface::MakeRaster(info);
105 }
106
107 return result;
108}
109
110class ImageAlphaThresholdSurfaceGM : public skiagm::GM {
senorblanco1ea67a32016-01-19 08:50:18 -0800111public:
112 ImageAlphaThresholdSurfaceGM() {
113 this->setBGColor(0xFFFFFFFF);
114 }
115
116protected:
117 SkString onShortName() override {
118 return SkString("imagealphathreshold_surface");
119 }
120
121 SkISize onISize() override {
122 return SkISize::Make(WIDTH, HEIGHT);
123 }
124
125 void onDraw(SkCanvas* canvas) override {
Robert Phillips22c57ab2016-12-19 16:51:53 -0500126 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));
Robert Phillips253b4dd2016-12-20 19:05:09 -0500135 if (!surface) {
136 return;
137 }
Robert Phillips22c57ab2016-12-19 16:51:53 -0500138
139 surface->getCanvas()->clear(SK_ColorTRANSPARENT);
senorblanco1ea67a32016-01-19 08:50:18 -0800140 draw_rects(surface->getCanvas());
141
senorblanco1ea67a32016-01-19 08:50:18 -0800142 SkPaint paint = create_filter_paint();
143 canvas->clipRect(SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100));
reed9ce9d672016-03-17 10:51:11 -0700144 canvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, &paint);
senorblanco1ea67a32016-01-19 08:50:18 -0800145 }
146
147private:
Robert Phillips22c57ab2016-12-19 16:51:53 -0500148 typedef skiagm::GM INHERITED;
senorblanco1ea67a32016-01-19 08:50:18 -0800149};
150
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000151//////////////////////////////////////////////////////////////////////////////
152
robertphillipsaf9b8c82016-04-12 11:02:25 -0700153DEF_GM(return new ImageAlphaThresholdGM(true);)
154DEF_GM(return new ImageAlphaThresholdGM(false);)
senorblanco1ea67a32016-01-19 08:50:18 -0800155DEF_GM(return new ImageAlphaThresholdSurfaceGM();)