commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 1 | /* |
| 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" |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 11 | #include "SkSurface.h" |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 12 | |
| 13 | #define WIDTH 500 |
| 14 | #define HEIGHT 500 |
| 15 | |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | void 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 | |
robertphillips | af9b8c8 | 2016-04-12 11:02:25 -0700 | [diff] [blame] | 30 | SkPaint create_filter_paint(SkImageFilter::CropRect* cropRect = nullptr) { |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 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; |
robertphillips | af9b8c8 | 2016-04-12 11:02:25 -0700 | [diff] [blame] | 38 | paint.setImageFilter(SkAlphaThresholdFilter::Make(region, 0.2f, 0.7f, nullptr, cropRect)); |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 39 | return paint; |
| 40 | } |
| 41 | |
| 42 | }; |
| 43 | |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 44 | namespace skiagm { |
| 45 | |
| 46 | class ImageAlphaThresholdGM : public GM { |
| 47 | public: |
Brian Osman | 01015fb | 2016-10-03 11:06:45 -0400 | [diff] [blame] | 48 | ImageAlphaThresholdGM(bool useCropRect) : fUseCropRect(useCropRect) { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 49 | this->setBGColor(0xFFFFFFFF); |
| 50 | } |
| 51 | |
| 52 | protected: |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 53 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 54 | SkString onShortName() override { |
robertphillips | af9b8c8 | 2016-04-12 11:02:25 -0700 | [diff] [blame] | 55 | if (fUseCropRect) { |
| 56 | return SkString("imagealphathreshold_crop"); |
| 57 | } |
| 58 | |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 59 | return SkString("imagealphathreshold"); |
| 60 | } |
| 61 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 62 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 63 | return SkISize::Make(WIDTH, HEIGHT); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 64 | } |
| 65 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 66 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 67 | SkMatrix matrix; |
| 68 | matrix.reset(); |
| 69 | matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f); |
commit-bot@chromium.org | 9109e18 | 2014-01-07 16:04:01 +0000 | [diff] [blame] | 70 | matrix.postScale(.8f, .8f); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 71 | |
| 72 | canvas->concat(matrix); |
| 73 | |
robertphillips | af9b8c8 | 2016-04-12 11:02:25 -0700 | [diff] [blame] | 74 | 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); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 78 | canvas->saveLayer(nullptr, &paint); |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 79 | draw_rects(canvas); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 80 | |
| 81 | canvas->restore(); |
| 82 | } |
| 83 | |
| 84 | private: |
robertphillips | af9b8c8 | 2016-04-12 11:02:25 -0700 | [diff] [blame] | 85 | bool fUseCropRect; |
| 86 | |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 87 | typedef GM INHERITED; |
| 88 | }; |
| 89 | |
robertphillips | af9b8c8 | 2016-04-12 11:02:25 -0700 | [diff] [blame] | 90 | |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 91 | class ImageAlphaThresholdSurfaceGM : public GM { |
| 92 | public: |
| 93 | ImageAlphaThresholdSurfaceGM() { |
| 94 | this->setBGColor(0xFFFFFFFF); |
| 95 | } |
| 96 | |
| 97 | protected: |
| 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 { |
Brian Osman | 6fb592e | 2016-10-03 13:15:12 -0400 | [diff] [blame] | 107 | SkImageInfo info = SkImageInfo::MakeS32(WIDTH, HEIGHT, kOpaque_SkAlphaType); |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 108 | auto surface(canvas->makeSurface(info)); |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 109 | if (nullptr == surface) { |
reed | e8f3062 | 2016-03-23 18:59:25 -0700 | [diff] [blame] | 110 | surface = SkSurface::MakeRaster(info); |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 111 | } |
| 112 | surface->getCanvas()->clear(SK_ColorWHITE); |
| 113 | draw_rects(surface->getCanvas()); |
| 114 | |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 115 | SkPaint paint = create_filter_paint(); |
| 116 | canvas->clipRect(SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100)); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 117 | canvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, &paint); |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | private: |
| 121 | typedef GM INHERITED; |
| 122 | }; |
| 123 | |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 124 | ////////////////////////////////////////////////////////////////////////////// |
| 125 | |
robertphillips | af9b8c8 | 2016-04-12 11:02:25 -0700 | [diff] [blame] | 126 | DEF_GM(return new ImageAlphaThresholdGM(true);) |
| 127 | DEF_GM(return new ImageAlphaThresholdGM(false);) |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 128 | DEF_GM(return new ImageAlphaThresholdSurfaceGM();) |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 129 | |
| 130 | } |