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" |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame^] | 10 | #include "SkOffsetImageFilter.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 | |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame^] | 16 | static void draw_rects(SkCanvas* canvas) { |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 17 | SkPaint rectPaint; |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame^] | 18 | rectPaint.setColor(SK_ColorBLUE); |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 19 | 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 Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame^] | 24 | rectPaint.setColor(SK_ColorTRANSPARENT); |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 25 | canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint); |
| 26 | } |
| 27 | |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame^] | 28 | static SkPaint create_filter_paint(SkImageFilter::CropRect* cropRect = nullptr) { |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 29 | 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 Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame^] | 36 | sk_sp<SkImageFilter> offset(SkOffsetImageFilter::Make(25, 25, nullptr)); |
| 37 | paint.setImageFilter(SkAlphaThresholdFilter::Make(region, 0.2f, 0.7f, std::move(offset), cropRect)); |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 38 | return paint; |
| 39 | } |
| 40 | |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame^] | 41 | class ImageAlphaThresholdGM : public skiagm::GM { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 42 | public: |
Brian Osman | 01015fb | 2016-10-03 11:06:45 -0400 | [diff] [blame] | 43 | ImageAlphaThresholdGM(bool useCropRect) : fUseCropRect(useCropRect) { |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame^] | 44 | this->setBGColor(SK_ColorWHITE); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 45 | } |
| 46 | |
| 47 | protected: |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 48 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 49 | SkString onShortName() override { |
robertphillips | af9b8c8 | 2016-04-12 11:02:25 -0700 | [diff] [blame] | 50 | if (fUseCropRect) { |
| 51 | return SkString("imagealphathreshold_crop"); |
| 52 | } |
| 53 | |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 54 | return SkString("imagealphathreshold"); |
| 55 | } |
| 56 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 57 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 58 | return SkISize::Make(WIDTH, HEIGHT); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 59 | } |
| 60 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 61 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 62 | SkMatrix matrix; |
| 63 | matrix.reset(); |
| 64 | matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f); |
commit-bot@chromium.org | 9109e18 | 2014-01-07 16:04:01 +0000 | [diff] [blame] | 65 | matrix.postScale(.8f, .8f); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 66 | |
| 67 | canvas->concat(matrix); |
| 68 | |
robertphillips | af9b8c8 | 2016-04-12 11:02:25 -0700 | [diff] [blame] | 69 | 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); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 73 | canvas->saveLayer(nullptr, &paint); |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 74 | draw_rects(canvas); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 75 | |
| 76 | canvas->restore(); |
| 77 | } |
| 78 | |
| 79 | private: |
robertphillips | af9b8c8 | 2016-04-12 11:02:25 -0700 | [diff] [blame] | 80 | bool fUseCropRect; |
| 81 | |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 82 | typedef GM INHERITED; |
| 83 | }; |
| 84 | |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame^] | 85 | // Create a 'width' x 'height' SkSurface that matches the colorType of 'canvas' as |
| 86 | // best we can |
| 87 | static sk_sp<SkSurface> make_color_matching_surface(SkCanvas* canvas, int width, int height, |
| 88 | SkAlphaType alphaType) { |
robertphillips | af9b8c8 | 2016-04-12 11:02:25 -0700 | [diff] [blame] | 89 | |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame^] | 90 | SkColorType ct = canvas->imageInfo().colorType(); |
| 91 | sk_sp<SkColorSpace> cs(sk_ref_sp(canvas->imageInfo().colorSpace())); |
| 92 | |
| 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 | |
| 109 | class ImageAlphaThresholdSurfaceGM : public skiagm::GM { |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 110 | public: |
| 111 | ImageAlphaThresholdSurfaceGM() { |
| 112 | this->setBGColor(0xFFFFFFFF); |
| 113 | } |
| 114 | |
| 115 | protected: |
| 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 Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame^] | 125 | 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)); |
| 134 | |
| 135 | surface->getCanvas()->clear(SK_ColorTRANSPARENT); |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 136 | draw_rects(surface->getCanvas()); |
| 137 | |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 138 | SkPaint paint = create_filter_paint(); |
| 139 | canvas->clipRect(SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100)); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 140 | canvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, &paint); |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | private: |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame^] | 144 | typedef skiagm::GM INHERITED; |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 145 | }; |
| 146 | |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 147 | ////////////////////////////////////////////////////////////////////////////// |
| 148 | |
robertphillips | af9b8c8 | 2016-04-12 11:02:25 -0700 | [diff] [blame] | 149 | DEF_GM(return new ImageAlphaThresholdGM(true);) |
| 150 | DEF_GM(return new ImageAlphaThresholdGM(false);) |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 151 | DEF_GM(return new ImageAlphaThresholdSurfaceGM();) |