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" |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 10 | #include "SkArithmeticMode.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 { |
| 52 | SkXfermode::Mode fMode; |
| 53 | const char* fLabel; |
| 54 | } gModes[] = { |
| 55 | { SkXfermode::kClear_Mode, "Clear" }, |
| 56 | { SkXfermode::kSrc_Mode, "Src" }, |
| 57 | { SkXfermode::kDst_Mode, "Dst" }, |
| 58 | { SkXfermode::kSrcOver_Mode, "SrcOver" }, |
| 59 | { SkXfermode::kDstOver_Mode, "DstOver" }, |
| 60 | { SkXfermode::kSrcIn_Mode, "SrcIn" }, |
| 61 | { SkXfermode::kDstIn_Mode, "DstIn" }, |
| 62 | { SkXfermode::kSrcOut_Mode, "SrcOut" }, |
| 63 | { SkXfermode::kDstOut_Mode, "DstOut" }, |
| 64 | { SkXfermode::kSrcATop_Mode, "SrcATop" }, |
| 65 | { SkXfermode::kDstATop_Mode, "DstATop" }, |
| 66 | { SkXfermode::kXor_Mode, "Xor" }, |
| 67 | |
| 68 | { SkXfermode::kPlus_Mode, "Plus" }, |
| 69 | { SkXfermode::kModulate_Mode, "Modulate" }, |
| 70 | { SkXfermode::kScreen_Mode, "Screen" }, |
| 71 | { SkXfermode::kOverlay_Mode, "Overlay" }, |
| 72 | { SkXfermode::kDarken_Mode, "Darken" }, |
| 73 | { SkXfermode::kLighten_Mode, "Lighten" }, |
| 74 | { SkXfermode::kColorDodge_Mode, "ColorDodge" }, |
| 75 | { SkXfermode::kColorBurn_Mode, "ColorBurn" }, |
| 76 | { SkXfermode::kHardLight_Mode, "HardLight" }, |
| 77 | { SkXfermode::kSoftLight_Mode, "SoftLight" }, |
| 78 | { SkXfermode::kDifference_Mode, "Difference" }, |
| 79 | { SkXfermode::kExclusion_Mode, "Exclusion" }, |
| 80 | { SkXfermode::kMultiply_Mode, "Multiply" }, |
| 81 | { SkXfermode::kHue_Mode, "Hue" }, |
| 82 | { SkXfermode::kSaturation_Mode, "Saturation" }, |
| 83 | { SkXfermode::kColor_Mode, "Color" }, |
| 84 | { SkXfermode::kLuminosity_Mode, "Luminosity" }, |
| 85 | }; |
| 86 | |
| 87 | int x = 0, y = 0; |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame^] | 88 | sk_sp<SkImageFilter> background(SkImageSource::Create(fCheckerboard.get())); |
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 | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 90 | paint.setImageFilter(SkXfermodeImageFilter::Make(SkXfermode::Make(gModes[i].fMode), |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame^] | 91 | background.get())); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 92 | DrawClippedBitmap(canvas, fBitmap, paint, x, y); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 93 | x += fBitmap.width() + MARGIN; |
| 94 | if (x + fBitmap.width() > WIDTH) { |
| 95 | x = 0; |
| 96 | y += fBitmap.height() + MARGIN; |
| 97 | } |
| 98 | } |
| 99 | // Test arithmetic mode as image filter |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 100 | paint.setImageFilter(SkXfermodeImageFilter::Make( |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame^] | 101 | SkArithmeticMode::Make(0, SK_Scalar1, SK_Scalar1, 0), |
| 102 | background.get())); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 103 | DrawClippedBitmap(canvas, fBitmap, paint, x, y); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 104 | x += fBitmap.width() + MARGIN; |
| 105 | if (x + fBitmap.width() > WIDTH) { |
| 106 | x = 0; |
| 107 | y += fBitmap.height() + MARGIN; |
| 108 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 109 | // Test nullptr mode |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame^] | 110 | paint.setImageFilter(SkXfermodeImageFilter::Make(nullptr, background.get())); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 111 | DrawClippedBitmap(canvas, fBitmap, paint, x, y); |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 112 | x += fBitmap.width() + MARGIN; |
| 113 | if (x + fBitmap.width() > WIDTH) { |
| 114 | x = 0; |
| 115 | y += fBitmap.height() + MARGIN; |
| 116 | } |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 117 | SkRect clipRect = SkRect::MakeWH(SkIntToScalar(fBitmap.width() + 4), |
| 118 | SkIntToScalar(fBitmap.height() + 4)); |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 119 | // Test offsets on SrcMode (uses fixed-function blend) |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 120 | sk_sp<SkImage> bitmapImage(SkImage::MakeFromBitmap(fBitmap)); |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame^] | 121 | sk_sp<SkImageFilter> foreground(SkImageSource::Create(bitmapImage.get())); |
| 122 | sk_sp<SkImageFilter> offsetForeground(SkOffsetImageFilter::Make(SkIntToScalar(4), |
| 123 | SkIntToScalar(-4), |
| 124 | foreground)); |
| 125 | sk_sp<SkImageFilter> offsetBackground(SkOffsetImageFilter::Make(SkIntToScalar(4), |
| 126 | SkIntToScalar(4), |
| 127 | background)); |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 128 | paint.setImageFilter(SkXfermodeImageFilter::Make( |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame^] | 129 | SkXfermode::Make(SkXfermode::kSrcOver_Mode), |
| 130 | offsetBackground.get(), |
| 131 | offsetForeground.get(), |
| 132 | nullptr)); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 133 | DrawClippedPaint(canvas, clipRect, paint, x, y); |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 134 | x += fBitmap.width() + MARGIN; |
| 135 | if (x + fBitmap.width() > WIDTH) { |
| 136 | x = 0; |
| 137 | y += fBitmap.height() + MARGIN; |
| 138 | } |
| 139 | // Test offsets on Darken (uses shader blend) |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 140 | paint.setImageFilter(SkXfermodeImageFilter::Make(SkXfermode::Make(SkXfermode::kDarken_Mode), |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame^] | 141 | offsetBackground.get(), |
| 142 | offsetForeground.get(), |
| 143 | nullptr)); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 144 | DrawClippedPaint(canvas, clipRect, paint, x, y); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 145 | x += fBitmap.width() + MARGIN; |
| 146 | if (x + fBitmap.width() > WIDTH) { |
| 147 | x = 0; |
| 148 | y += fBitmap.height() + MARGIN; |
| 149 | } |
| 150 | // Test cropping |
| 151 | static const size_t nbSamples = 3; |
| 152 | SkXfermode::Mode sampledModes[nbSamples] = {SkXfermode::kOverlay_Mode, |
| 153 | SkXfermode::kSrcOver_Mode, |
| 154 | SkXfermode::kPlus_Mode}; |
| 155 | int offsets[nbSamples][4] = {{ 10, 10, -16, -16}, |
| 156 | { 10, 10, 10, 10}, |
| 157 | {-10, -10, -6, -6}}; |
| 158 | for (size_t i = 0; i < nbSamples; ++i) { |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 159 | SkIRect cropRect = SkIRect::MakeXYWH(offsets[i][0], |
| 160 | offsets[i][1], |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 161 | fBitmap.width() + offsets[i][2], |
| 162 | fBitmap.height() + offsets[i][3]); |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 163 | SkImageFilter::CropRect rect(SkRect::Make(cropRect)); |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame^] | 164 | paint.setImageFilter(SkXfermodeImageFilter::Make(SkXfermode::Make(sampledModes[i]), |
| 165 | offsetBackground.get(), |
| 166 | offsetForeground.get(), |
| 167 | &rect)); |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 168 | DrawClippedPaint(canvas, clipRect, paint, x, y); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 169 | x += fBitmap.width() + MARGIN; |
| 170 | if (x + fBitmap.width() > WIDTH) { |
| 171 | x = 0; |
| 172 | y += fBitmap.height() + MARGIN; |
| 173 | } |
| 174 | } |
senorblanco | 06d54ad | 2016-01-13 13:48:54 -0800 | [diff] [blame] | 175 | // Test small bg, large fg with Screen (uses shader blend) |
reed | cfb6bdf | 2016-03-29 11:32:50 -0700 | [diff] [blame] | 176 | auto mode = SkXfermode::Make(SkXfermode::kScreen_Mode); |
senorblanco | 06d54ad | 2016-01-13 13:48:54 -0800 | [diff] [blame] | 177 | SkImageFilter::CropRect cropRect(SkRect::MakeXYWH(10, 10, 60, 60)); |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame^] | 178 | sk_sp<SkImageFilter> cropped(SkOffsetImageFilter::Make(0, 0, foreground, &cropRect)); |
| 179 | paint.setImageFilter(SkXfermodeImageFilter::Make(mode, cropped.get(), |
| 180 | background.get(), nullptr)); |
senorblanco | 06d54ad | 2016-01-13 13:48:54 -0800 | [diff] [blame] | 181 | DrawClippedPaint(canvas, clipRect, paint, x, y); |
| 182 | x += fBitmap.width() + MARGIN; |
| 183 | if (x + fBitmap.width() > WIDTH) { |
| 184 | x = 0; |
| 185 | y += fBitmap.height() + MARGIN; |
| 186 | } |
| 187 | // Test small fg, large bg with Screen (uses shader blend) |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame^] | 188 | paint.setImageFilter(SkXfermodeImageFilter::Make(mode, background.get(), |
| 189 | cropped.get(), nullptr)); |
senorblanco | 06d54ad | 2016-01-13 13:48:54 -0800 | [diff] [blame] | 190 | DrawClippedPaint(canvas, clipRect, paint, x, y); |
| 191 | x += fBitmap.width() + MARGIN; |
| 192 | if (x + fBitmap.width() > WIDTH) { |
| 193 | x = 0; |
| 194 | y += fBitmap.height() + MARGIN; |
| 195 | } |
senorblanco | 9db0427 | 2016-03-31 08:24:29 -0700 | [diff] [blame] | 196 | // Test small fg, large bg with SrcIn with a crop that forces it to full size. |
| 197 | // This tests that SkXfermodeImageFilter correctly applies the compositing mode to |
| 198 | // the region outside the foreground. |
| 199 | mode = SkXfermode::Make(SkXfermode::kSrcIn_Mode); |
| 200 | SkImageFilter::CropRect cropRectFull(SkRect::MakeXYWH(0, 0, 80, 80)); |
robertphillips | 51a315e | 2016-03-31 09:05:49 -0700 | [diff] [blame^] | 201 | paint.setImageFilter(SkXfermodeImageFilter::Make(mode, background.get(), |
| 202 | cropped.get(), &cropRectFull)); |
senorblanco | 9db0427 | 2016-03-31 08:24:29 -0700 | [diff] [blame] | 203 | DrawClippedPaint(canvas, clipRect, paint, x, y); |
| 204 | x += fBitmap.width() + MARGIN; |
| 205 | if (x + fBitmap.width() > WIDTH) { |
| 206 | x = 0; |
| 207 | y += fBitmap.height() + MARGIN; |
| 208 | } |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 209 | } |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 210 | |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 211 | private: |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 212 | static void DrawClippedBitmap(SkCanvas* canvas, const SkBitmap& bitmap, const SkPaint& paint, |
| 213 | int x, int y) { |
| 214 | canvas->save(); |
| 215 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 216 | canvas->clipRect(SkRect::MakeWH( |
| 217 | SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()))); |
| 218 | canvas->drawBitmap(bitmap, 0, 0, &paint); |
| 219 | canvas->restore(); |
| 220 | } |
| 221 | |
| 222 | static void DrawClippedPaint(SkCanvas* canvas, const SkRect& rect, const SkPaint& paint, |
| 223 | int x, int y) { |
| 224 | canvas->save(); |
| 225 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 226 | canvas->clipRect(rect); |
| 227 | canvas->drawPaint(paint); |
| 228 | canvas->restore(); |
| 229 | } |
| 230 | |
reed | 9ce9d67 | 2016-03-17 10:51:11 -0700 | [diff] [blame] | 231 | SkBitmap fBitmap; |
| 232 | sk_sp<SkImage> fCheckerboard; |
fmalita | 5598b63 | 2015-09-15 11:26:13 -0700 | [diff] [blame] | 233 | |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 234 | typedef GM INHERITED; |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 235 | }; |
| 236 | |
| 237 | ////////////////////////////////////////////////////////////////////////////// |
| 238 | |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 239 | DEF_GM( return new XfermodeImageFilterGM; ); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 240 | |
| 241 | } |