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