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" |
| 9 | #include "SkArithmeticMode.h" |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 10 | #include "SkOffsetImageFilter.h" |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 11 | #include "SkXfermodeImageFilter.h" |
| 12 | #include "SkBitmapSource.h" |
| 13 | |
| 14 | #define WIDTH 600 |
| 15 | #define HEIGHT 600 |
| 16 | #define MARGIN 12 |
| 17 | |
| 18 | namespace skiagm { |
| 19 | |
| 20 | class XfermodeImageFilterGM : public GM { |
| 21 | public: |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame^] | 22 | XfermodeImageFilterGM(){ |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 23 | this->setBGColor(0xFF000000); |
| 24 | } |
| 25 | |
| 26 | protected: |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame^] | 27 | virtual SkString onShortName() SK_OVERRIDE { |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 28 | return SkString("xfermodeimagefilter"); |
| 29 | } |
| 30 | |
| 31 | void make_bitmap() { |
| 32 | fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 80, 80); |
| 33 | fBitmap.allocPixels(); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 34 | SkBitmapDevice device(fBitmap); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 35 | SkCanvas canvas(&device); |
| 36 | canvas.clear(0x00000000); |
| 37 | SkPaint paint; |
| 38 | paint.setAntiAlias(true); |
| 39 | paint.setColor(0xD000D000); |
| 40 | paint.setTextSize(SkIntToScalar(96)); |
| 41 | const char* str = "e"; |
| 42 | canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(65), paint); |
| 43 | } |
| 44 | |
| 45 | void make_checkerboard() { |
| 46 | fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, 80, 80); |
| 47 | fCheckerboard.allocPixels(); |
robertphillips@google.com | 1f2f338 | 2013-08-29 11:54:56 +0000 | [diff] [blame] | 48 | SkBitmapDevice device(fCheckerboard); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 49 | SkCanvas canvas(&device); |
| 50 | canvas.clear(0x00000000); |
| 51 | SkPaint darkPaint; |
| 52 | darkPaint.setColor(0xFF404040); |
| 53 | SkPaint lightPaint; |
| 54 | lightPaint.setColor(0xFFA0A0A0); |
| 55 | for (int y = 0; y < 80; y += 16) { |
| 56 | for (int x = 0; x < 80; x += 16) { |
| 57 | canvas.save(); |
| 58 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 59 | canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint); |
| 60 | canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint); |
| 61 | canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint); |
| 62 | canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint); |
| 63 | canvas.restore(); |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame^] | 68 | virtual SkISize onISize() SK_OVERRIDE { |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 69 | return make_isize(WIDTH, HEIGHT); |
| 70 | } |
| 71 | |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame^] | 72 | static void drawClippedBitmap(SkCanvas* canvas, const SkBitmap& bitmap, const SkPaint& paint, |
| 73 | int x, int y) { |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 74 | canvas->save(); |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame^] | 75 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 76 | canvas->clipRect(SkRect::MakeWH( |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 77 | SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()))); |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame^] | 78 | canvas->drawBitmap(bitmap, 0, 0, &paint); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 79 | canvas->restore(); |
| 80 | } |
| 81 | |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame^] | 82 | static void drawClippedPaint(SkCanvas* canvas, const SkRect& rect, const SkPaint& paint, |
| 83 | int x, int y) { |
| 84 | canvas->save(); |
| 85 | canvas->translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 86 | canvas->clipRect(rect); |
| 87 | canvas->drawPaint(paint); |
| 88 | canvas->restore(); |
| 89 | } |
| 90 | |
| 91 | virtual void onOnceBeforeDraw() SK_OVERRIDE { |
| 92 | make_bitmap(); |
| 93 | make_checkerboard(); |
| 94 | } |
| 95 | |
| 96 | virtual void onDraw(SkCanvas* canvas) SK_OVERRIDE { |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 97 | canvas->clear(0x00000000); |
| 98 | SkPaint paint; |
| 99 | |
| 100 | const struct { |
| 101 | SkXfermode::Mode fMode; |
| 102 | const char* fLabel; |
| 103 | } gModes[] = { |
| 104 | { SkXfermode::kClear_Mode, "Clear" }, |
| 105 | { SkXfermode::kSrc_Mode, "Src" }, |
| 106 | { SkXfermode::kDst_Mode, "Dst" }, |
| 107 | { SkXfermode::kSrcOver_Mode, "SrcOver" }, |
| 108 | { SkXfermode::kDstOver_Mode, "DstOver" }, |
| 109 | { SkXfermode::kSrcIn_Mode, "SrcIn" }, |
| 110 | { SkXfermode::kDstIn_Mode, "DstIn" }, |
| 111 | { SkXfermode::kSrcOut_Mode, "SrcOut" }, |
| 112 | { SkXfermode::kDstOut_Mode, "DstOut" }, |
| 113 | { SkXfermode::kSrcATop_Mode, "SrcATop" }, |
| 114 | { SkXfermode::kDstATop_Mode, "DstATop" }, |
| 115 | { SkXfermode::kXor_Mode, "Xor" }, |
| 116 | |
| 117 | { SkXfermode::kPlus_Mode, "Plus" }, |
| 118 | { SkXfermode::kModulate_Mode, "Modulate" }, |
| 119 | { SkXfermode::kScreen_Mode, "Screen" }, |
| 120 | { SkXfermode::kOverlay_Mode, "Overlay" }, |
| 121 | { SkXfermode::kDarken_Mode, "Darken" }, |
| 122 | { SkXfermode::kLighten_Mode, "Lighten" }, |
| 123 | { SkXfermode::kColorDodge_Mode, "ColorDodge" }, |
| 124 | { SkXfermode::kColorBurn_Mode, "ColorBurn" }, |
| 125 | { SkXfermode::kHardLight_Mode, "HardLight" }, |
| 126 | { SkXfermode::kSoftLight_Mode, "SoftLight" }, |
| 127 | { SkXfermode::kDifference_Mode, "Difference" }, |
| 128 | { SkXfermode::kExclusion_Mode, "Exclusion" }, |
| 129 | { SkXfermode::kMultiply_Mode, "Multiply" }, |
| 130 | { SkXfermode::kHue_Mode, "Hue" }, |
| 131 | { SkXfermode::kSaturation_Mode, "Saturation" }, |
| 132 | { SkXfermode::kColor_Mode, "Color" }, |
| 133 | { SkXfermode::kLuminosity_Mode, "Luminosity" }, |
| 134 | }; |
| 135 | |
| 136 | int x = 0, y = 0; |
| 137 | SkAutoTUnref<SkImageFilter> background(SkNEW_ARGS(SkBitmapSource, (fCheckerboard))); |
| 138 | for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) { |
| 139 | SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(gModes[i].fMode)); |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 140 | SkAutoTUnref<SkImageFilter> filter(SkNEW_ARGS( |
| 141 | SkXfermodeImageFilter, (mode, background))); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 142 | paint.setImageFilter(filter); |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame^] | 143 | drawClippedBitmap(canvas, fBitmap, paint, x, y); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 144 | x += fBitmap.width() + MARGIN; |
| 145 | if (x + fBitmap.width() > WIDTH) { |
| 146 | x = 0; |
| 147 | y += fBitmap.height() + MARGIN; |
| 148 | } |
| 149 | } |
| 150 | // Test arithmetic mode as image filter |
| 151 | SkAutoTUnref<SkXfermode> mode(SkArithmeticMode::Create(0, SK_Scalar1, SK_Scalar1, 0)); |
| 152 | SkAutoTUnref<SkImageFilter> filter(SkNEW_ARGS(SkXfermodeImageFilter, (mode, background))); |
| 153 | paint.setImageFilter(filter); |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame^] | 154 | drawClippedBitmap(canvas, fBitmap, paint, x, y); |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 155 | x += fBitmap.width() + MARGIN; |
| 156 | if (x + fBitmap.width() > WIDTH) { |
| 157 | x = 0; |
| 158 | y += fBitmap.height() + MARGIN; |
| 159 | } |
| 160 | // Test NULL mode |
| 161 | filter.reset(SkNEW_ARGS(SkXfermodeImageFilter, (NULL, background))); |
| 162 | paint.setImageFilter(filter); |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame^] | 163 | drawClippedBitmap(canvas, fBitmap, paint, x, y); |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 164 | x += fBitmap.width() + MARGIN; |
| 165 | if (x + fBitmap.width() > WIDTH) { |
| 166 | x = 0; |
| 167 | y += fBitmap.height() + MARGIN; |
| 168 | } |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame^] | 169 | SkRect clipRect = SkRect::MakeWH(SkIntToScalar(fBitmap.width() + 4), |
| 170 | SkIntToScalar(fBitmap.height() + 4)); |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 171 | // Test offsets on SrcMode (uses fixed-function blend) |
| 172 | SkAutoTUnref<SkImageFilter> foreground(SkNEW_ARGS(SkBitmapSource, (fBitmap))); |
| 173 | SkAutoTUnref<SkImageFilter> offsetForeground(SkNEW_ARGS(SkOffsetImageFilter, |
| 174 | (SkIntToScalar(4), SkIntToScalar(-4), foreground))); |
| 175 | SkAutoTUnref<SkImageFilter> offsetBackground(SkNEW_ARGS(SkOffsetImageFilter, |
| 176 | (SkIntToScalar(4), SkIntToScalar(4), background))); |
| 177 | mode.reset(SkXfermode::Create(SkXfermode::kSrcOver_Mode)); |
| 178 | filter.reset(SkNEW_ARGS(SkXfermodeImageFilter, |
| 179 | (mode, offsetBackground, offsetForeground))); |
| 180 | paint.setImageFilter(filter); |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame^] | 181 | drawClippedPaint(canvas, clipRect, paint, x, y); |
commit-bot@chromium.org | 7b32070 | 2013-07-10 21:22:18 +0000 | [diff] [blame] | 182 | x += fBitmap.width() + MARGIN; |
| 183 | if (x + fBitmap.width() > WIDTH) { |
| 184 | x = 0; |
| 185 | y += fBitmap.height() + MARGIN; |
| 186 | } |
| 187 | // Test offsets on Darken (uses shader blend) |
| 188 | mode.reset(SkXfermode::Create(SkXfermode::kDarken_Mode)); |
| 189 | filter.reset(SkNEW_ARGS(SkXfermodeImageFilter, (mode, offsetBackground, offsetForeground))); |
| 190 | paint.setImageFilter(filter); |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame^] | 191 | drawClippedPaint(canvas, clipRect, paint, x, y); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 192 | x += fBitmap.width() + MARGIN; |
| 193 | if (x + fBitmap.width() > WIDTH) { |
| 194 | x = 0; |
| 195 | y += fBitmap.height() + MARGIN; |
| 196 | } |
| 197 | // Test cropping |
| 198 | static const size_t nbSamples = 3; |
| 199 | SkXfermode::Mode sampledModes[nbSamples] = {SkXfermode::kOverlay_Mode, |
| 200 | SkXfermode::kSrcOver_Mode, |
| 201 | SkXfermode::kPlus_Mode}; |
| 202 | int offsets[nbSamples][4] = {{ 10, 10, -16, -16}, |
| 203 | { 10, 10, 10, 10}, |
| 204 | {-10, -10, -6, -6}}; |
| 205 | for (size_t i = 0; i < nbSamples; ++i) { |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame^] | 206 | SkIRect cropRect = SkIRect::MakeXYWH(offsets[i][0], |
| 207 | offsets[i][1], |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 208 | fBitmap.width() + offsets[i][2], |
| 209 | fBitmap.height() + offsets[i][3]); |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 210 | SkImageFilter::CropRect rect(SkRect::Make(cropRect)); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 211 | mode.reset(SkXfermode::Create(sampledModes[i])); |
| 212 | filter.reset(SkNEW_ARGS(SkXfermodeImageFilter, |
senorblanco@chromium.org | b295fb6 | 2013-10-10 13:51:19 +0000 | [diff] [blame] | 213 | (mode, offsetBackground, offsetForeground, &rect))); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 214 | paint.setImageFilter(filter); |
senorblanco@chromium.org | e93e1db | 2013-12-09 18:31:42 +0000 | [diff] [blame^] | 215 | drawClippedPaint(canvas, clipRect, paint, x, y); |
commit-bot@chromium.org | 1a4fb70 | 2013-09-26 16:09:28 +0000 | [diff] [blame] | 216 | x += fBitmap.width() + MARGIN; |
| 217 | if (x + fBitmap.width() > WIDTH) { |
| 218 | x = 0; |
| 219 | y += fBitmap.height() + MARGIN; |
| 220 | } |
| 221 | } |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 222 | } |
| 223 | private: |
senorblanco@chromium.org | 86fc266 | 2013-05-31 17:49:12 +0000 | [diff] [blame] | 224 | SkBitmap fBitmap, fCheckerboard; |
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 | } |