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" |
Brian Osman | a4aa133 | 2017-10-19 12:54:28 -0400 | [diff] [blame] | 10 | #include "SkImageSource.h" |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame] | 11 | #include "SkOffsetImageFilter.h" |
Brian Osman | a4aa133 | 2017-10-19 12:54:28 -0400 | [diff] [blame] | 12 | #include "SkRandom.h" |
Mike Reed | 267be7f | 2017-02-13 09:32:54 -0500 | [diff] [blame] | 13 | #include "SkRegion.h" |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 14 | #include "SkSurface.h" |
Mike Reed | 46596ae | 2018-01-02 15:40:29 -0500 | [diff] [blame] | 15 | #include "sk_tool_utils.h" |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 16 | |
| 17 | #define WIDTH 500 |
| 18 | #define HEIGHT 500 |
| 19 | |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame] | 20 | static void draw_rects(SkCanvas* canvas) { |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 21 | SkPaint rectPaint; |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame] | 22 | rectPaint.setColor(SK_ColorBLUE); |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 23 | canvas->drawRect(SkRect::MakeXYWH(0, 0, WIDTH / 2, HEIGHT / 2), rectPaint); |
| 24 | rectPaint.setColor(0xBFFF0000); |
| 25 | canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, 0, WIDTH / 2, HEIGHT / 2), rectPaint); |
| 26 | rectPaint.setColor(0x3F00FF00); |
| 27 | canvas->drawRect(SkRect::MakeXYWH(0, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint); |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame] | 28 | rectPaint.setColor(SK_ColorTRANSPARENT); |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 29 | canvas->drawRect(SkRect::MakeXYWH(WIDTH / 2, HEIGHT / 2, WIDTH / 2, HEIGHT / 2), rectPaint); |
| 30 | } |
| 31 | |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame] | 32 | static SkPaint create_filter_paint(SkImageFilter::CropRect* cropRect = nullptr) { |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 33 | SkIRect rects[2]; |
| 34 | rects[0] = SkIRect::MakeXYWH(0, 150, WIDTH, HEIGHT - 300); |
| 35 | rects[1] = SkIRect::MakeXYWH(150, 0, WIDTH - 300, HEIGHT); |
| 36 | SkRegion region; |
| 37 | region.setRects(rects, 2); |
| 38 | |
| 39 | SkPaint paint; |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame] | 40 | sk_sp<SkImageFilter> offset(SkOffsetImageFilter::Make(25, 25, nullptr)); |
| 41 | paint.setImageFilter(SkAlphaThresholdFilter::Make(region, 0.2f, 0.7f, std::move(offset), cropRect)); |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 42 | return paint; |
| 43 | } |
| 44 | |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame] | 45 | class ImageAlphaThresholdGM : public skiagm::GM { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 46 | public: |
Brian Osman | 01015fb | 2016-10-03 11:06:45 -0400 | [diff] [blame] | 47 | ImageAlphaThresholdGM(bool useCropRect) : fUseCropRect(useCropRect) { |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame] | 48 | this->setBGColor(SK_ColorWHITE); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | protected: |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 52 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 53 | SkString onShortName() override { |
robertphillips | af9b8c8 | 2016-04-12 11:02:25 -0700 | [diff] [blame] | 54 | if (fUseCropRect) { |
| 55 | return SkString("imagealphathreshold_crop"); |
| 56 | } |
| 57 | |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 58 | return SkString("imagealphathreshold"); |
| 59 | } |
| 60 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 61 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 62 | return SkISize::Make(WIDTH, HEIGHT); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 63 | } |
| 64 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 65 | void onDraw(SkCanvas* canvas) override { |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 66 | SkMatrix matrix; |
| 67 | matrix.reset(); |
| 68 | matrix.setTranslate(WIDTH * .1f, HEIGHT * .1f); |
commit-bot@chromium.org | 9109e18 | 2014-01-07 16:04:01 +0000 | [diff] [blame] | 69 | matrix.postScale(.8f, .8f); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 70 | |
| 71 | canvas->concat(matrix); |
| 72 | |
robertphillips | af9b8c8 | 2016-04-12 11:02:25 -0700 | [diff] [blame] | 73 | SkRect r = SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100); |
| 74 | SkImageFilter::CropRect cropRect(r); |
| 75 | |
| 76 | SkPaint paint = create_filter_paint(fUseCropRect ? &cropRect : nullptr); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 77 | canvas->saveLayer(nullptr, &paint); |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 78 | draw_rects(canvas); |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 79 | |
| 80 | canvas->restore(); |
| 81 | } |
| 82 | |
| 83 | private: |
robertphillips | af9b8c8 | 2016-04-12 11:02:25 -0700 | [diff] [blame] | 84 | bool fUseCropRect; |
| 85 | |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 86 | typedef GM INHERITED; |
| 87 | }; |
| 88 | |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame] | 89 | // Create a 'width' x 'height' SkSurface that matches the colorType of 'canvas' as |
| 90 | // best we can |
| 91 | static sk_sp<SkSurface> make_color_matching_surface(SkCanvas* canvas, int width, int height, |
Mike Reed | 1617899 | 2018-02-09 17:35:29 -0500 | [diff] [blame] | 92 | SkAlphaType at) { |
robertphillips | af9b8c8 | 2016-04-12 11:02:25 -0700 | [diff] [blame] | 93 | |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame] | 94 | SkColorType ct = canvas->imageInfo().colorType(); |
Mike Reed | 693fdbd | 2017-01-12 10:13:40 -0500 | [diff] [blame] | 95 | sk_sp<SkColorSpace> cs(canvas->imageInfo().refColorSpace()); |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame] | 96 | |
| 97 | if (kUnknown_SkColorType == ct) { |
| 98 | // For backends that aren't yet color-space aware we just fallback to N32. |
| 99 | ct = kN32_SkColorType; |
| 100 | cs = nullptr; |
Mike Reed | 1617899 | 2018-02-09 17:35:29 -0500 | [diff] [blame] | 101 | } else if (SkColorTypeIsAlwaysOpaque(ct)) { |
| 102 | at = kOpaque_SkAlphaType; |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame] | 103 | } |
| 104 | |
Mike Reed | 1617899 | 2018-02-09 17:35:29 -0500 | [diff] [blame] | 105 | SkImageInfo info = SkImageInfo::Make(width, height, ct, at, std::move(cs)); |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame] | 106 | |
Mike Reed | 46596ae | 2018-01-02 15:40:29 -0500 | [diff] [blame] | 107 | return sk_tool_utils::makeSurface(canvas, info); |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | class ImageAlphaThresholdSurfaceGM : public skiagm::GM { |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 111 | public: |
| 112 | ImageAlphaThresholdSurfaceGM() { |
| 113 | this->setBGColor(0xFFFFFFFF); |
| 114 | } |
| 115 | |
| 116 | protected: |
| 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 Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame] | 126 | 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 Phillips | 253b4dd | 2016-12-20 19:05:09 -0500 | [diff] [blame] | 135 | if (!surface) { |
| 136 | return; |
| 137 | } |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame] | 138 | |
| 139 | surface->getCanvas()->clear(SK_ColorTRANSPARENT); |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 140 | draw_rects(surface->getCanvas()); |
| 141 | |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 142 | SkPaint paint = create_filter_paint(); |
| 143 | canvas->clipRect(SkRect::MakeLTRB(100, 100, WIDTH - 100, HEIGHT - 100)); |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 144 | canvas->drawImage(surface->makeImageSnapshot().get(), 0, 0, &paint); |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | private: |
Robert Phillips | 22c57ab | 2016-12-19 16:51:53 -0500 | [diff] [blame] | 148 | typedef skiagm::GM INHERITED; |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 149 | }; |
| 150 | |
commit-bot@chromium.org | 40eb3c1 | 2014-01-06 23:41:14 +0000 | [diff] [blame] | 151 | ////////////////////////////////////////////////////////////////////////////// |
| 152 | |
robertphillips | af9b8c8 | 2016-04-12 11:02:25 -0700 | [diff] [blame] | 153 | DEF_GM(return new ImageAlphaThresholdGM(true);) |
| 154 | DEF_GM(return new ImageAlphaThresholdGM(false);) |
senorblanco | 1ea67a3 | 2016-01-19 08:50:18 -0800 | [diff] [blame] | 155 | DEF_GM(return new ImageAlphaThresholdSurfaceGM();) |
Brian Osman | a4aa133 | 2017-10-19 12:54:28 -0400 | [diff] [blame] | 156 | |
| 157 | ////////////////////////////////////////////////////////////////////////////// |
| 158 | |
| 159 | static sk_sp<SkImage> make_img() { |
| 160 | SkBitmap bitmap; |
| 161 | bitmap.allocPixels(SkImageInfo::MakeS32(WIDTH, HEIGHT, kPremul_SkAlphaType)); |
| 162 | SkCanvas canvas(bitmap); |
| 163 | |
| 164 | SkPaint paint; |
| 165 | SkRect rect = SkRect::MakeWH(WIDTH, HEIGHT); |
| 166 | SkRandom rnd; |
| 167 | |
| 168 | while (!rect.isEmpty()) { |
| 169 | paint.setColor(rnd.nextU() | (0xFF << 24)); |
| 170 | canvas.drawRect(rect, paint); |
| 171 | rect.inset(25, 25); |
| 172 | } |
| 173 | |
| 174 | return SkImage::MakeFromBitmap(bitmap); |
| 175 | } |
| 176 | |
| 177 | DEF_SIMPLE_GM_BG(imagealphathreshold_image, canvas, WIDTH * 2, HEIGHT, SK_ColorBLACK) { |
| 178 | sk_sp<SkImage> image(make_img()); |
| 179 | |
| 180 | SkIRect rects[2]; |
| 181 | rects[0] = SkIRect::MakeXYWH(0, 150, WIDTH, HEIGHT - 300); |
| 182 | rects[1] = SkIRect::MakeXYWH(150, 0, WIDTH - 300, HEIGHT); |
| 183 | SkRegion region; |
| 184 | region.setRects(rects, 2); |
| 185 | |
| 186 | SkPaint filterPaint; |
| 187 | sk_sp<SkImageFilter> imageSource(SkImageSource::Make(image)); |
| 188 | filterPaint.setImageFilter(SkAlphaThresholdFilter::Make(region, 0.2f, 0.7f, |
| 189 | std::move(imageSource))); |
| 190 | |
| 191 | canvas->saveLayer(nullptr, &filterPaint); |
| 192 | canvas->restore(); |
| 193 | canvas->translate(WIDTH, 0); |
| 194 | canvas->drawImage(image, 0, 0); |
| 195 | } |