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" |
| 10 | #include "SkXfermodeImageFilter.h" |
| 11 | #include "SkBitmapSource.h" |
| 12 | |
| 13 | #define WIDTH 600 |
| 14 | #define HEIGHT 600 |
| 15 | #define MARGIN 12 |
| 16 | |
| 17 | namespace skiagm { |
| 18 | |
| 19 | class XfermodeImageFilterGM : public GM { |
| 20 | public: |
| 21 | XfermodeImageFilterGM() : fInitialized(false) { |
| 22 | this->setBGColor(0xFF000000); |
| 23 | } |
| 24 | |
| 25 | protected: |
| 26 | virtual SkString onShortName() { |
| 27 | return SkString("xfermodeimagefilter"); |
| 28 | } |
| 29 | |
| 30 | void make_bitmap() { |
| 31 | fBitmap.setConfig(SkBitmap::kARGB_8888_Config, 80, 80); |
| 32 | fBitmap.allocPixels(); |
| 33 | SkDevice device(fBitmap); |
| 34 | SkCanvas canvas(&device); |
| 35 | canvas.clear(0x00000000); |
| 36 | SkPaint paint; |
| 37 | paint.setAntiAlias(true); |
| 38 | paint.setColor(0xD000D000); |
| 39 | paint.setTextSize(SkIntToScalar(96)); |
| 40 | const char* str = "e"; |
| 41 | canvas.drawText(str, strlen(str), SkIntToScalar(15), SkIntToScalar(65), paint); |
| 42 | } |
| 43 | |
| 44 | void make_checkerboard() { |
| 45 | fCheckerboard.setConfig(SkBitmap::kARGB_8888_Config, 80, 80); |
| 46 | fCheckerboard.allocPixels(); |
| 47 | SkDevice device(fCheckerboard); |
| 48 | SkCanvas canvas(&device); |
| 49 | canvas.clear(0x00000000); |
| 50 | SkPaint darkPaint; |
| 51 | darkPaint.setColor(0xFF404040); |
| 52 | SkPaint lightPaint; |
| 53 | lightPaint.setColor(0xFFA0A0A0); |
| 54 | for (int y = 0; y < 80; y += 16) { |
| 55 | for (int x = 0; x < 80; x += 16) { |
| 56 | canvas.save(); |
| 57 | canvas.translate(SkIntToScalar(x), SkIntToScalar(y)); |
| 58 | canvas.drawRect(SkRect::MakeXYWH(0, 0, 8, 8), darkPaint); |
| 59 | canvas.drawRect(SkRect::MakeXYWH(8, 0, 8, 8), lightPaint); |
| 60 | canvas.drawRect(SkRect::MakeXYWH(0, 8, 8, 8), lightPaint); |
| 61 | canvas.drawRect(SkRect::MakeXYWH(8, 8, 8, 8), darkPaint); |
| 62 | canvas.restore(); |
| 63 | } |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | virtual SkISize onISize() { |
| 68 | return make_isize(WIDTH, HEIGHT); |
| 69 | } |
| 70 | |
| 71 | void drawClippedBitmap(SkCanvas* canvas, const SkBitmap& bitmap, const SkPaint& paint, SkScalar x, SkScalar y) { |
| 72 | canvas->save(); |
| 73 | canvas->clipRect(SkRect::MakeXYWH(x, y, |
| 74 | SkIntToScalar(bitmap.width()), SkIntToScalar(bitmap.height()))); |
| 75 | canvas->drawBitmap(bitmap, x, y, &paint); |
| 76 | canvas->restore(); |
| 77 | } |
| 78 | |
| 79 | virtual void onDraw(SkCanvas* canvas) { |
| 80 | if (!fInitialized) { |
| 81 | make_bitmap(); |
| 82 | make_checkerboard(); |
| 83 | fInitialized = true; |
| 84 | } |
| 85 | canvas->clear(0x00000000); |
| 86 | SkPaint paint; |
| 87 | |
| 88 | const struct { |
| 89 | SkXfermode::Mode fMode; |
| 90 | const char* fLabel; |
| 91 | } gModes[] = { |
| 92 | { SkXfermode::kClear_Mode, "Clear" }, |
| 93 | { SkXfermode::kSrc_Mode, "Src" }, |
| 94 | { SkXfermode::kDst_Mode, "Dst" }, |
| 95 | { SkXfermode::kSrcOver_Mode, "SrcOver" }, |
| 96 | { SkXfermode::kDstOver_Mode, "DstOver" }, |
| 97 | { SkXfermode::kSrcIn_Mode, "SrcIn" }, |
| 98 | { SkXfermode::kDstIn_Mode, "DstIn" }, |
| 99 | { SkXfermode::kSrcOut_Mode, "SrcOut" }, |
| 100 | { SkXfermode::kDstOut_Mode, "DstOut" }, |
| 101 | { SkXfermode::kSrcATop_Mode, "SrcATop" }, |
| 102 | { SkXfermode::kDstATop_Mode, "DstATop" }, |
| 103 | { SkXfermode::kXor_Mode, "Xor" }, |
| 104 | |
| 105 | { SkXfermode::kPlus_Mode, "Plus" }, |
| 106 | { SkXfermode::kModulate_Mode, "Modulate" }, |
| 107 | { SkXfermode::kScreen_Mode, "Screen" }, |
| 108 | { SkXfermode::kOverlay_Mode, "Overlay" }, |
| 109 | { SkXfermode::kDarken_Mode, "Darken" }, |
| 110 | { SkXfermode::kLighten_Mode, "Lighten" }, |
| 111 | { SkXfermode::kColorDodge_Mode, "ColorDodge" }, |
| 112 | { SkXfermode::kColorBurn_Mode, "ColorBurn" }, |
| 113 | { SkXfermode::kHardLight_Mode, "HardLight" }, |
| 114 | { SkXfermode::kSoftLight_Mode, "SoftLight" }, |
| 115 | { SkXfermode::kDifference_Mode, "Difference" }, |
| 116 | { SkXfermode::kExclusion_Mode, "Exclusion" }, |
| 117 | { SkXfermode::kMultiply_Mode, "Multiply" }, |
| 118 | { SkXfermode::kHue_Mode, "Hue" }, |
| 119 | { SkXfermode::kSaturation_Mode, "Saturation" }, |
| 120 | { SkXfermode::kColor_Mode, "Color" }, |
| 121 | { SkXfermode::kLuminosity_Mode, "Luminosity" }, |
| 122 | }; |
| 123 | |
| 124 | int x = 0, y = 0; |
| 125 | SkAutoTUnref<SkImageFilter> background(SkNEW_ARGS(SkBitmapSource, (fCheckerboard))); |
| 126 | for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) { |
| 127 | SkAutoTUnref<SkXfermode> mode(SkXfermode::Create(gModes[i].fMode)); |
| 128 | SkAutoTUnref<SkImageFilter> filter(SkNEW_ARGS(SkXfermodeImageFilter, (mode, background))); |
| 129 | paint.setImageFilter(filter); |
| 130 | drawClippedBitmap(canvas, fBitmap, paint, SkIntToScalar(x), SkIntToScalar(y)); |
| 131 | x += fBitmap.width() + MARGIN; |
| 132 | if (x + fBitmap.width() > WIDTH) { |
| 133 | x = 0; |
| 134 | y += fBitmap.height() + MARGIN; |
| 135 | } |
| 136 | } |
| 137 | // Test arithmetic mode as image filter |
| 138 | SkAutoTUnref<SkXfermode> mode(SkArithmeticMode::Create(0, SK_Scalar1, SK_Scalar1, 0)); |
| 139 | SkAutoTUnref<SkImageFilter> filter(SkNEW_ARGS(SkXfermodeImageFilter, (mode, background))); |
| 140 | paint.setImageFilter(filter); |
| 141 | drawClippedBitmap(canvas, fBitmap, paint, SkIntToScalar(x), SkIntToScalar(y)); |
| 142 | x += fBitmap.width() + MARGIN; |
| 143 | if (x + fBitmap.width() > WIDTH) { |
| 144 | x = 0; |
| 145 | y += fBitmap.height() + MARGIN; |
| 146 | } |
| 147 | // Test NULL mode |
| 148 | filter.reset(SkNEW_ARGS(SkXfermodeImageFilter, (NULL, background))); |
| 149 | paint.setImageFilter(filter); |
| 150 | drawClippedBitmap(canvas, fBitmap, paint, SkIntToScalar(x), SkIntToScalar(y)); |
| 151 | } |
| 152 | private: |
| 153 | typedef GM INHERITED; |
| 154 | SkBitmap fBitmap, fCheckerboard; |
| 155 | bool fInitialized; |
| 156 | }; |
| 157 | |
| 158 | ////////////////////////////////////////////////////////////////////////////// |
| 159 | |
| 160 | static GM* MyFactory(void*) { return new XfermodeImageFilterGM; } |
| 161 | static GMRegistry reg(MyFactory); |
| 162 | |
| 163 | } |