senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +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" |
halcanary | b0cce2c | 2015-01-26 12:49:00 -0800 | [diff] [blame] | 9 | #include "sk_tool_utils.h" |
Brian Salomon | 89cb821 | 2017-01-09 10:48:23 -0500 | [diff] [blame] | 10 | #include "SkArithmeticImageFilter.h" |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 11 | #include "SkImage.h" |
| 12 | #include "SkImageSource.h" |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 13 | #include "SkOffsetImageFilter.h" |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 14 | #include "SkXfermodeImageFilter.h" |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 15 | |
| 16 | #define WIDTH 600 |
senorblanco | 06d54ad | 2016-01-13 13:48:54 -0800 | [diff] [blame] | 17 | #define HEIGHT 700 |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 18 | #define MARGIN 12 |
| 19 | |
| 20 | namespace skiagm { |
| 21 | |
| 22 | class XfermodeImageFilterGM : public GM { |
| 23 | public: |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 24 | XfermodeImageFilterGM(){ |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 25 | this->setBGColor(0xFF000000); |
| 26 | } |
| 27 | |
| 28 | protected: |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 29 | SkString onShortName() override { |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 30 | return SkString("xfermodeimagefilter"); |
| 31 | } |
| 32 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 33 | SkISize onISize() override { |
tfarina | f539318 | 2014-06-09 23:59:03 -0700 | [diff] [blame] | 34 | return SkISize::Make(WIDTH, HEIGHT); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 35 | } |
| 36 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 37 | void onOnceBeforeDraw() override { |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame] | 38 | fBitmap = sk_tool_utils::create_string_bitmap(80, 80, 0xD000D000, 15, 65, 96, "e"); |
halcanary | 878fa02 | 2015-01-26 11:24:32 -0800 | [diff] [blame] | 39 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 40 | fCheckerboard = SkImage::MakeFromBitmap( |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 41 | sk_tool_utils::create_checkerboard_bitmap(80, 80, |
Mike Klein | d46dce3 | 2018-08-16 10:17:03 -0400 | [diff] [blame] | 42 | 0xFFA0A0A0, |
| 43 | 0xFF404040, |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 44 | 8)); |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 45 | } |
| 46 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 47 | void onDraw(SkCanvas* canvas) override { |
senorblanco | 16b254a | 2015-04-09 11:13:24 -0700 | [diff] [blame] | 48 | canvas->clear(SK_ColorBLACK); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 49 | SkPaint paint; |
| 50 | |
Brian Osman | d1e67e7 | 2017-03-15 12:19:37 -0400 | [diff] [blame] | 51 | const SkBlendMode gModes[] = { |
| 52 | SkBlendMode::kClear, |
| 53 | SkBlendMode::kSrc, |
| 54 | SkBlendMode::kDst, |
| 55 | SkBlendMode::kSrcOver, |
| 56 | SkBlendMode::kDstOver, |
| 57 | SkBlendMode::kSrcIn, |
| 58 | SkBlendMode::kDstIn, |
| 59 | SkBlendMode::kSrcOut, |
| 60 | SkBlendMode::kDstOut, |
| 61 | SkBlendMode::kSrcATop, |
| 62 | SkBlendMode::kDstATop, |
| 63 | SkBlendMode::kXor, |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 64 | |
Brian Osman | d1e67e7 | 2017-03-15 12:19:37 -0400 | [diff] [blame] | 65 | SkBlendMode::kPlus, |
| 66 | SkBlendMode::kModulate, |
| 67 | SkBlendMode::kScreen, |
| 68 | SkBlendMode::kOverlay, |
| 69 | SkBlendMode::kDarken, |
| 70 | SkBlendMode::kLighten, |
| 71 | SkBlendMode::kColorDodge, |
| 72 | SkBlendMode::kColorBurn, |
| 73 | SkBlendMode::kHardLight, |
| 74 | SkBlendMode::kSoftLight, |
| 75 | SkBlendMode::kDifference, |
| 76 | SkBlendMode::kExclusion, |
| 77 | SkBlendMode::kMultiply, |
| 78 | SkBlendMode::kHue, |
| 79 | SkBlendMode::kSaturation, |
| 80 | SkBlendMode::kColor, |
| 81 | SkBlendMode::kLuminosity, |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | int x = 0, y = 0; |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 85 | sk_sp<SkImageFilter> background(SkImageSource::Make(fCheckerboard)); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 86 | for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) { |
Brian Osman | d1e67e7 | 2017-03-15 12:19:37 -0400 | [diff] [blame] | 87 | paint.setImageFilter(SkXfermodeImageFilter::Make(gModes[i], background)); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 88 | DrawClippedBitmap(canvas, fBitmap, paint, x, y); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 89 | x += fBitmap.width() + MARGIN; |
| 90 | if (x + fBitmap.width() > WIDTH) { |
| 91 | x = 0; |
| 92 | y += fBitmap.height() + MARGIN; |
| 93 | } |
| 94 | } |
| 95 | // Test arithmetic mode as image filter |
Mike Reed | 6b9cd05 | 2017-06-18 21:32:48 -0400 | [diff] [blame] | 96 | paint.setImageFilter(SkArithmeticImageFilter::Make(0, 1, 1, 0, true, background, |
| 97 | nullptr, nullptr)); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 98 | DrawClippedBitmap(canvas, fBitmap, paint, x, y); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 99 | x += fBitmap.width() + MARGIN; |
| 100 | if (x + fBitmap.width() > WIDTH) { |
| 101 | x = 0; |
| 102 | y += fBitmap.height() + MARGIN; |
| 103 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 104 | // Test nullptr mode |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 105 | paint.setImageFilter(SkXfermodeImageFilter::Make(SkBlendMode::kSrcOver, background)); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 106 | DrawClippedBitmap(canvas, fBitmap, paint, x, y); |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 107 | x += fBitmap.width() + MARGIN; |
| 108 | if (x + fBitmap.width() > WIDTH) { |
| 109 | x = 0; |
| 110 | y += fBitmap.height() + MARGIN; |
| 111 | } |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 112 | SkRect clipRect = SkRect::MakeWH(SkIntToScalar(fBitmap.width() + 4), |
| 113 | SkIntToScalar(fBitmap.height() + 4)); |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 114 | // Test offsets on SrcMode (uses fixed-function blend) |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 115 | sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(fBitmap)); |
robertphillips | 549c899 | 2016-04-01 09:28:51 -0700 | [diff] [blame] | 116 | sk_sp<SkImageFilter> foreground(SkImageSource::Make(std::move(bitmapImage))); |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame] | 117 | sk_sp<SkImageFilter> offsetForeground(SkOffsetImageFilter::Make(SkIntToScalar(4), |
| 118 | SkIntToScalar(-4), |
| 119 | foreground)); |
| 120 | sk_sp<SkImageFilter> offsetBackground(SkOffsetImageFilter::Make(SkIntToScalar(4), |
| 121 | SkIntToScalar(4), |
| 122 | background)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 123 | paint.setImageFilter(SkXfermodeImageFilter::Make(SkBlendMode::kSrcOver, |
| 124 | offsetBackground, |
| 125 | offsetForeground, |
| 126 | nullptr)); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 127 | DrawClippedPaint(canvas, clipRect, paint, x, y); |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 128 | x += fBitmap.width() + MARGIN; |
| 129 | if (x + fBitmap.width() > WIDTH) { |
| 130 | x = 0; |
| 131 | y += fBitmap.height() + MARGIN; |
| 132 | } |
| 133 | // Test offsets on Darken (uses shader blend) |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 134 | paint.setImageFilter(SkXfermodeImageFilter::Make(SkBlendMode::kDarken, |
robertphillips | 8c0326d | 2016-04-05 12:48:34 -0700 | [diff] [blame] | 135 | offsetBackground, |
| 136 | offsetForeground, |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame] | 137 | nullptr)); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 138 | DrawClippedPaint(canvas, clipRect, paint, x, y); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 139 | x += fBitmap.width() + MARGIN; |
| 140 | if (x + fBitmap.width() > WIDTH) { |
| 141 | x = 0; |
| 142 | y += fBitmap.height() + MARGIN; |
| 143 | } |
| 144 | // Test cropping |
mtklein | dbfd7ab | 2016-09-01 11:24:54 -0700 | [diff] [blame] | 145 | constexpr size_t nbSamples = 3; |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 146 | const SkBlendMode sampledModes[nbSamples] = { |
| 147 | SkBlendMode::kOverlay, SkBlendMode::kSrcOver, SkBlendMode::kPlus |
| 148 | }; |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 149 | int offsets[nbSamples][4] = {{ 10, 10, -16, -16}, |
| 150 | { 10, 10, 10, 10}, |
| 151 | {-10, -10, -6, -6}}; |
| 152 | for (size_t i = 0; i < nbSamples; ++i) { |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 153 | SkIRect cropRect = SkIRect::MakeXYWH(offsets[i][0], |
| 154 | offsets[i][1], |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 155 | fBitmap.width() + offsets[i][2], |
| 156 | fBitmap.height() + offsets[i][3]); |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 157 | SkImageFilter::CropRect rect(SkRect::Make(cropRect)); |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 158 | paint.setImageFilter(SkXfermodeImageFilter::Make(sampledModes[i], |
robertphillips | 8c0326d | 2016-04-05 12:48:34 -0700 | [diff] [blame] | 159 | offsetBackground, |
| 160 | offsetForeground, |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame] | 161 | &rect)); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 162 | DrawClippedPaint(canvas, clipRect, paint, x, y); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 163 | x += fBitmap.width() + MARGIN; |
| 164 | if (x + fBitmap.width() > WIDTH) { |
| 165 | x = 0; |
| 166 | y += fBitmap.height() + MARGIN; |
| 167 | } |
| 168 | } |
senorblanco | 06d54ad | 2016-01-13 13:48:54 -0800 | [diff] [blame] | 169 | // Test small bg, large fg with Screen (uses shader blend) |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 170 | SkBlendMode mode = SkBlendMode::kScreen; |
senorblanco | 06d54ad | 2016-01-13 13:48:54 -0800 | [diff] [blame] | 171 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(10, 10, 60, 60)); |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame] | 172 | sk_sp<SkImageFilter> cropped(SkOffsetImageFilter::Make(0, 0, foreground, &cropRect)); |
robertphillips | 8c0326d | 2016-04-05 12:48:34 -0700 | [diff] [blame] | 173 | paint.setImageFilter(SkXfermodeImageFilter::Make(mode, cropped, background, nullptr)); |
senorblanco | 06d54ad | 2016-01-13 13:48:54 -0800 | [diff] [blame] | 174 | DrawClippedPaint(canvas, clipRect, paint, x, y); |
| 175 | x += fBitmap.width() + MARGIN; |
| 176 | if (x + fBitmap.width() > WIDTH) { |
| 177 | x = 0; |
| 178 | y += fBitmap.height() + MARGIN; |
| 179 | } |
| 180 | // Test small fg, large bg with Screen (uses shader blend) |
robertphillips | 8c0326d | 2016-04-05 12:48:34 -0700 | [diff] [blame] | 181 | paint.setImageFilter(SkXfermodeImageFilter::Make(mode, background, cropped, nullptr)); |
senorblanco | 06d54ad | 2016-01-13 13:48:54 -0800 | [diff] [blame] | 182 | DrawClippedPaint(canvas, clipRect, paint, x, y); |
| 183 | x += fBitmap.width() + MARGIN; |
| 184 | if (x + fBitmap.width() > WIDTH) { |
| 185 | x = 0; |
| 186 | y += fBitmap.height() + MARGIN; |
| 187 | } |
senorblanco | 9db0427 | 2016-03-31 08:24:29 -0700 | [diff] [blame] | 188 | // Test small fg, large bg with SrcIn with a crop that forces it to full size. |
| 189 | // This tests that SkXfermodeImageFilter correctly applies the compositing mode to |
| 190 | // the region outside the foreground. |
reed | 374772b | 2016-10-05 17:33:02 -0700 | [diff] [blame] | 191 | mode = SkBlendMode::kSrcIn; |
senorblanco | 9db0427 | 2016-03-31 08:24:29 -0700 | [diff] [blame] | 192 | SkImageFilter::CropRect cropRectFull(SkRect::MakeXYWH(0, 0, 80, 80)); |
robertphillips | 8c0326d | 2016-04-05 12:48:34 -0700 | [diff] [blame] | 193 | paint.setImageFilter(SkXfermodeImageFilter::Make(mode, background, |
| 194 | cropped, &cropRectFull)); |
senorblanco | 9db0427 | 2016-03-31 08:24:29 -0700 | [diff] [blame] | 195 | DrawClippedPaint(canvas, clipRect, paint, x, y); |
| 196 | x += fBitmap.width() + MARGIN; |
| 197 | if (x + fBitmap.width() > WIDTH) { |
| 198 | x = 0; |
| 199 | y += fBitmap.height() + MARGIN; |
| 200 | } |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 201 | } |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 202 | |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 203 | private: |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 204 | static void DrawClippedBitmap(SkCanvas* canvas, const SkBitmap& bitmap, const SkPaint& paint, |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 205 | int x, int y) { |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 206 | canvas->save(); |
| 207 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 208 | canvas->clipRect(SkRect::MakeIWH(bitmap.width(), bitmap.height())); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 209 | canvas->drawBitmap(bitmap, 0, 0, &paint); |
| 210 | canvas->restore(); |
| 211 | } |
| 212 | |
| 213 | static void DrawClippedPaint(SkCanvas* canvas, const SkRect& rect, const SkPaint& paint, |
Robert Phillips | 1119dc3 | 2017-04-11 12:54:57 -0400 | [diff] [blame] | 214 | int x, int y) { |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 215 | canvas->save(); |
| 216 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 217 | canvas->clipRect(rect); |
| 218 | canvas->drawPaint(paint); |
| 219 | canvas->restore(); |
| 220 | } |
| 221 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 222 | SkBitmap fBitmap; |
| 223 | sk_sp<SkImage> fCheckerboard; |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 224 | |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 225 | typedef GM INHERITED; |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 226 | }; |
| 227 | |
| 228 | ////////////////////////////////////////////////////////////////////////////// |
| 229 | |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 230 | DEF_GM( return new XfermodeImageFilterGM; ); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 231 | |
| 232 | } |