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