blob: ea7185be1dbb8747d8cadcd9109f8ef3a602ed38 [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
30SkPaint create_filter_paint() {
31 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;
38 paint.setImageFilter(SkAlphaThresholdFilter::Create(region, 0.2f, 0.7f))->unref();
39 return paint;
40}
41
42};
43
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000044namespace skiagm {
45
46class ImageAlphaThresholdGM : public GM {
47public:
48 ImageAlphaThresholdGM() {
49 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 {
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
senorblanco1ea67a32016-01-19 08:50:18 -080070 SkPaint paint = create_filter_paint();
halcanary96fcdcc2015-08-27 07:41:13 -070071 canvas->saveLayer(nullptr, &paint);
senorblanco1ea67a32016-01-19 08:50:18 -080072 draw_rects(canvas);
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +000073
74 canvas->restore();
75 }
76
77private:
78 typedef GM INHERITED;
79};
80
senorblanco1ea67a32016-01-19 08:50:18 -080081class ImageAlphaThresholdSurfaceGM : public GM {
82public:
83 ImageAlphaThresholdSurfaceGM() {
84 this->setBGColor(0xFFFFFFFF);
85 }
86
87protected:
88 SkString onShortName() override {
89 return SkString("imagealphathreshold_surface");
90 }
91
92 SkISize onISize() override {
93 return SkISize::Make(WIDTH, HEIGHT);
94 }
95
96 void onDraw(SkCanvas* canvas) override {
97 SkImageInfo info = SkImageInfo::MakeN32(WIDTH, HEIGHT, kOpaque_SkAlphaType);
98 SkAutoTUnref<SkSurface> surface(canvas->newSurface(info));
99 if (nullptr == surface) {
100 surface.reset(SkSurface::NewRaster(info));
101 }
102 surface->getCanvas()->clear(SK_ColorWHITE);
103 draw_rects(surface->getCanvas());
104
105 SkAutoTUnref<SkImage> image(surface->newImageSnapshot());
106 SkPaint paint = create_filter_paint();
107 canvas->clipRect(SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100));
108 canvas->drawImage(image, 0, 0, &paint);
109 }
110
111private:
112 typedef GM INHERITED;
113};
114
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000115//////////////////////////////////////////////////////////////////////////////
116
senorblanco1ea67a32016-01-19 08:50:18 -0800117DEF_GM(return new ImageAlphaThresholdGM();)
118DEF_GM(return new ImageAlphaThresholdSurfaceGM();)
commit-bot@chromium.org40eb3c12014-01-06 23:41:14 +0000119
120}