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