blob: 2b65202c1067856228984d1507203aca5f97c344 [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"
10#include "SkRandom.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
senorblanco1ea67a32016-01-19 08:50:18 -080016namespace {
17
18void draw_rects(SkCanvas* canvas) {
19 SkPaint rectPaint;
20 rectPaint.setColor(0xFF0000FF);
21 canvas->drawRect(SkRect::MakeXYWH(0, 0, WIDTH / 2, HEIGHT / 2), rectPaint);
22 rectPaint.setColor(0xBFFF0000);
23 canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, 0, WIDTH / 2, HEIGHT / 2), rectPaint);
24 rectPaint.setColor(0x3F00FF00);
25 canvas->drawRect(SkRect::MakeXYWH(0, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint);
26 rectPaint.setColor(0x00000000);
27 canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint);
28}
29
robertphillipsaf9b8c82016-04-12 11:02:25 -070030SkPaint create_filter_paint(SkImageFilter::CropRect* cropRect = nullptr) {
senorblanco1ea67a32016-01-19 08:50:18 -080031 SkIRect rects[2];
32 rects[0] = SkIRect::MakeXYWH(0, 150, WIDTH, HEIGHT - 300);
33 rects[1] = SkIRect::MakeXYWH(150, 0, WIDTH - 300, HEIGHT);
34 SkRegion region;
35 region.setRects(rects, 2);
36
37 SkPaint paint;
robertphillipsaf9b8c82016-04-12 11:02:25 -070038 paint.setImageFilter(SkAlphaThresholdFilter::Make(region, 0.2f, 0.7f, nullptr, cropRect));
senorblanco1ea67a32016-01-19 08:50:18 -080039 return paint;
40}
41
42};
43
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000044namespace skiagm {
45
46class ImageAlphaThresholdGM : public GM {
47public:
robertphillipsaf9b8c82016-04-12 11:02:25 -070048 ImageAlphaThresholdGM(bool useCropRect) : fUseCropRect(true) {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000049 this->setBGColor(0xFFFFFFFF);
50 }
51
52protected:
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000053
mtklein36352bf2015-03-25 18:17:31 -070054 SkString onShortName() override {
robertphillipsaf9b8c82016-04-12 11:02:25 -070055 if (fUseCropRect) {
56 return SkString("imagealphathreshold_crop");
57 }
58
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000059 return SkString("imagealphathreshold");
60 }
61
mtklein36352bf2015-03-25 18:17:31 -070062 SkISize onISize() override {
tfarinaf5393182014-06-09 23:59:03 -070063 return SkISize::Make(WIDTH, HEIGHT);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000064 }
65
mtklein36352bf2015-03-25 18:17:31 -070066 void onDraw(SkCanvas* canvas) override {
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000067 SkMatrix matrix;
68 matrix.reset();
69 matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f);
commit-bot@chromium.org9109e182014-01-07 16:04:01 +000070 matrix.postScale(.8f, .8f);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000071
72 canvas->concat(matrix);
73
robertphillipsaf9b8c82016-04-12 11:02:25 -070074 SkRect r = SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100);
75 SkImageFilter::CropRect cropRect(r);
76
77 SkPaint paint = create_filter_paint(fUseCropRect ? &cropRect : nullptr);
halcanary96fcdcc2015-08-27 07:41:13 -070078 canvas->saveLayer(nullptr, &paint);
senorblanco1ea67a32016-01-19 08:50:18 -080079 draw_rects(canvas);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000080
81 canvas->restore();
82 }
83
84private:
robertphillipsaf9b8c82016-04-12 11:02:25 -070085 bool fUseCropRect;
86
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000087 typedef GM INHERITED;
88};
89
robertphillipsaf9b8c82016-04-12 11:02:25 -070090
senorblanco1ea67a32016-01-19 08:50:18 -080091class ImageAlphaThresholdSurfaceGM : public GM {
92public:
93 ImageAlphaThresholdSurfaceGM() {
94 this->setBGColor(0xFFFFFFFF);
95 }
96
97protected:
98 SkString onShortName() override {
99 return SkString("imagealphathreshold_surface");
100 }
101
102 SkISize onISize() override {
103 return SkISize::Make(WIDTH, HEIGHT);
104 }
105
106 void onDraw(SkCanvas* canvas) override {
107 SkImageInfo info = SkImageInfo::MakeN32(WIDTH, HEIGHT, kOpaque_SkAlphaType);
reede8f30622016-03-23 18:59:25 -0700108 auto surface(canvas->makeSurface(info));
senorblanco1ea67a32016-01-19 08:50:18 -0800109 if (nullptr == surface) {
reede8f30622016-03-23 18:59:25 -0700110 surface = SkSurface::MakeRaster(info);
senorblanco1ea67a32016-01-19 08:50:18 -0800111 }
112 surface->getCanvas()->clear(SK_ColorWHITE);
113 draw_rects(surface->getCanvas());
114
senorblanco1ea67a32016-01-19 08:50:18 -0800115 SkPaint paint = create_filter_paint();
116 canvas->clipRect(SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100));
reed9ce9d672016-03-17 10:51:11 -0700117 canvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, &paint);
senorblanco1ea67a32016-01-19 08:50:18 -0800118 }
119
120private:
121 typedef GM INHERITED;
122};
123
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000124//////////////////////////////////////////////////////////////////////////////
125
robertphillipsaf9b8c82016-04-12 11:02:25 -0700126DEF_GM(return new ImageAlphaThresholdGM(true);)
127DEF_GM(return new ImageAlphaThresholdGM(false);)
senorblanco1ea67a32016-01-19 08:50:18 -0800128DEF_GM(return new ImageAlphaThresholdSurfaceGM();)
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000129
130}