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