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