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