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" |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 11 | #include "SkOffsetImageFilter.h" |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 12 | #include "SkXfermodeImageFilter.h" |
| 13 | #include "SkBitmapSource.h" |
| 14 | |
| 15 | #define WIDTH 600 |
| 16 | #define HEIGHT 600 |
| 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 | |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 36 | static void drawClippedBitmap(SkCanvas* canvas, const SkBitmap& bitmap, const SkPaint& paint, |
| 37 | int x, int y) { |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 38 | canvas->save(); |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 39 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 40 | canvas->clipRect(SkRect::MakeWH( |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 41 | SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()))); |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 42 | canvas->drawBitmap(bitmap, 0, 0, &paint); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 43 | canvas->restore(); |
| 44 | } |
| 45 | |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 46 | static void drawClippedPaint(SkCanvas* canvas, const SkRect& rect, const SkPaint& paint, |
| 47 | int x, int y) { |
| 48 | canvas->save(); |
| 49 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 50 | canvas->clipRect(rect); |
| 51 | canvas->drawPaint(paint); |
| 52 | canvas->restore(); |
| 53 | } |
| 54 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 55 | void onOnceBeforeDraw() override { |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame^] | 56 | 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] | 57 | |
robertphillips | 943a462 | 2015-09-03 13:32:33 -0700 | [diff] [blame^] | 58 | fCheckerboard = sk_tool_utils::create_checkerboard_bitmap( |
| 59 | 80, 80, |
| 60 | sk_tool_utils::color_to_565(0xFFA0A0A0), |
| 61 | sk_tool_utils::color_to_565(0xFF404040), |
| 62 | 8); |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 63 | } |
| 64 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 65 | void onDraw(SkCanvas* canvas) override { |
senorblanco | 16b254a | 2015-04-09 11:13:24 -0700 | [diff] [blame] | 66 | canvas->clear(SK_ColorBLACK); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 67 | SkPaint paint; |
| 68 | |
| 69 | const struct { |
| 70 | SkXfermode::Mode fMode; |
| 71 | const char* fLabel; |
| 72 | } gModes[] = { |
| 73 | { SkXfermode::kClear_Mode, "Clear" }, |
| 74 | { SkXfermode::kSrc_Mode, "Src" }, |
| 75 | { SkXfermode::kDst_Mode, "Dst" }, |
| 76 | { SkXfermode::kSrcOver_Mode, "SrcOver" }, |
| 77 | { SkXfermode::kDstOver_Mode, "DstOver" }, |
| 78 | { SkXfermode::kSrcIn_Mode, "SrcIn" }, |
| 79 | { SkXfermode::kDstIn_Mode, "DstIn" }, |
| 80 | { SkXfermode::kSrcOut_Mode, "SrcOut" }, |
| 81 | { SkXfermode::kDstOut_Mode, "DstOut" }, |
| 82 | { SkXfermode::kSrcATop_Mode, "SrcATop" }, |
| 83 | { SkXfermode::kDstATop_Mode, "DstATop" }, |
| 84 | { SkXfermode::kXor_Mode, "Xor" }, |
| 85 | |
| 86 | { SkXfermode::kPlus_Mode, "Plus" }, |
| 87 | { SkXfermode::kModulate_Mode, "Modulate" }, |
| 88 | { SkXfermode::kScreen_Mode, "Screen" }, |
| 89 | { SkXfermode::kOverlay_Mode, "Overlay" }, |
| 90 | { SkXfermode::kDarken_Mode, "Darken" }, |
| 91 | { SkXfermode::kLighten_Mode, "Lighten" }, |
| 92 | { SkXfermode::kColorDodge_Mode, "ColorDodge" }, |
| 93 | { SkXfermode::kColorBurn_Mode, "ColorBurn" }, |
| 94 | { SkXfermode::kHardLight_Mode, "HardLight" }, |
| 95 | { SkXfermode::kSoftLight_Mode, "SoftLight" }, |
| 96 | { SkXfermode::kDifference_Mode, "Difference" }, |
| 97 | { SkXfermode::kExclusion_Mode, "Exclusion" }, |
| 98 | { SkXfermode::kMultiply_Mode, "Multiply" }, |
| 99 | { SkXfermode::kHue_Mode, "Hue" }, |
| 100 | { SkXfermode::kSaturation_Mode, "Saturation" }, |
| 101 | { SkXfermode::kColor_Mode, "Color" }, |
| 102 | { SkXfermode::kLuminosity_Mode, "Luminosity" }, |
| 103 | }; |
| 104 | |
| 105 | int x = 0, y = 0; |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 106 | SkAutoTUnref<SkImageFilter> background(SkBitmapSource::Create(fCheckerboard)); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 107 | for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) { |
| 108 | SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(gModes[i].fMode)); |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 109 | SkAutoTUnref<SkImageFilter> filter(SkXfermodeImageFilter::Create(mode, background)); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 110 | paint.setImageFilter(filter); |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 111 | drawClippedBitmap(canvas, fBitmap, paint, x, y); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 112 | x += fBitmap.width() + MARGIN; |
| 113 | if (x + fBitmap.width() > WIDTH) { |
| 114 | x = 0; |
| 115 | y += fBitmap.height() + MARGIN; |
| 116 | } |
| 117 | } |
| 118 | // Test arithmetic mode as image filter |
| 119 | SkAutoTUnref<SkXfermode> mode(SkArithmeticMode::Create(0, SK_Scalar1, SK_Scalar1, 0)); |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 120 | SkAutoTUnref<SkImageFilter> filter(SkXfermodeImageFilter::Create(mode, background)); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 121 | paint.setImageFilter(filter); |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 122 | drawClippedBitmap(canvas, fBitmap, paint, x, y); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 123 | x += fBitmap.width() + MARGIN; |
| 124 | if (x + fBitmap.width() > WIDTH) { |
| 125 | x = 0; |
| 126 | y += fBitmap.height() + MARGIN; |
| 127 | } |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 128 | // Test nullptr mode |
| 129 | filter.reset(SkXfermodeImageFilter::Create(nullptr, background)); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 130 | paint.setImageFilter(filter); |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 131 | drawClippedBitmap(canvas, fBitmap, paint, x, y); |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 132 | x += fBitmap.width() + MARGIN; |
| 133 | if (x + fBitmap.width() > WIDTH) { |
| 134 | x = 0; |
| 135 | y += fBitmap.height() + MARGIN; |
| 136 | } |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 137 | SkRect clipRect = SkRect::MakeWH(SkIntToScalar(fBitmap.width() + 4), |
| 138 | SkIntToScalar(fBitmap.height() + 4)); |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 139 | // Test offsets on SrcMode (uses fixed-function blend) |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 140 | SkAutoTUnref<SkImageFilter> foreground(SkBitmapSource::Create(fBitmap)); |
| 141 | SkAutoTUnref<SkImageFilter> offsetForeground(SkOffsetImageFilter::Create( |
| 142 | SkIntToScalar(4), SkIntToScalar(-4), foreground)); |
| 143 | SkAutoTUnref<SkImageFilter> offsetBackground(SkOffsetImageFilter::Create( |
| 144 | SkIntToScalar(4), SkIntToScalar(4), background)); |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 145 | mode.reset(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 146 | filter.reset(SkXfermodeImageFilter::Create(mode, offsetBackground, offsetForeground)); |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 147 | paint.setImageFilter(filter); |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 148 | drawClippedPaint(canvas, clipRect, paint, x, y); |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 149 | x += fBitmap.width() + MARGIN; |
| 150 | if (x + fBitmap.width() > WIDTH) { |
| 151 | x = 0; |
| 152 | y += fBitmap.height() + MARGIN; |
| 153 | } |
| 154 | // Test offsets on Darken (uses shader blend) |
| 155 | mode.reset(SkXfermode::Create(SkXfermode::kDarken_Mode)); |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 156 | filter.reset(SkXfermodeImageFilter::Create(mode, offsetBackground, offsetForeground)); |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 157 | paint.setImageFilter(filter); |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 158 | drawClippedPaint(canvas, clipRect, paint, x, y); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 159 | x += fBitmap.width() + MARGIN; |
| 160 | if (x + fBitmap.width() > WIDTH) { |
| 161 | x = 0; |
| 162 | y += fBitmap.height() + MARGIN; |
| 163 | } |
| 164 | // Test cropping |
| 165 | static const size_t nbSamples = 3; |
| 166 | SkXfermode::Mode sampledModes[nbSamples] = {SkXfermode::kOverlay_Mode, |
| 167 | SkXfermode::kSrcOver_Mode, |
| 168 | SkXfermode::kPlus_Mode}; |
| 169 | int offsets[nbSamples][4] = {{ 10, 10, -16, -16}, |
| 170 | { 10, 10, 10, 10}, |
| 171 | {-10, -10, -6, -6}}; |
| 172 | for (size_t i = 0; i < nbSamples; ++i) { |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 173 | SkIRect cropRect = SkIRect::MakeXYWH(offsets[i][0], |
| 174 | offsets[i][1], |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 175 | fBitmap.width() + offsets[i][2], |
| 176 | fBitmap.height() + offsets[i][3]); |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 177 | SkImageFilter::CropRect rect(SkRect::Make(cropRect)); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 178 | mode.reset(SkXfermode::Create(sampledModes[i])); |
commit-bot@chromium.org | cac5fd5 | 2014-03-10 10:51:58 +0000 | [diff] [blame] | 179 | filter.reset(SkXfermodeImageFilter::Create( |
| 180 | mode, offsetBackground, offsetForeground, &rect)); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 181 | paint.setImageFilter(filter); |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 182 | drawClippedPaint(canvas, clipRect, paint, x, y); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 183 | x += fBitmap.width() + MARGIN; |
| 184 | if (x + fBitmap.width() > WIDTH) { |
| 185 | x = 0; |
| 186 | y += fBitmap.height() + MARGIN; |
| 187 | } |
| 188 | } |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 189 | } |
| 190 | private: |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 191 | SkBitmap fBitmap, fCheckerboard; |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 192 | typedef GM INHERITED; |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | ////////////////////////////////////////////////////////////////////////////// |
| 196 | |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame] | 197 | DEF_GM( return new XfermodeImageFilterGM; ); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 198 | |
| 199 | } |