epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 8 | #include "gm.h" |
| 9 | #include "SkBitmap.h" |
| 10 | #include "SkShader.h" |
| 11 | #include "SkXfermode.h" |
| 12 | |
| 13 | namespace skiagm { |
| 14 | |
tomhudson@google.com | 86bf5c9 | 2011-04-26 18:15:03 +0000 | [diff] [blame] | 15 | static void make_bitmaps(int w, int h, SkBitmap* src, SkBitmap* dst) { |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 16 | src->setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| 17 | src->allocPixels(); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 18 | src->eraseColor(SK_ColorTRANSPARENT); |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 19 | |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 20 | SkPaint p; |
mike@reedtribe.org | 3bd2173 | 2012-09-26 02:45:10 +0000 | [diff] [blame] | 21 | p.setAntiAlias(true); |
| 22 | |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 23 | SkRect r; |
| 24 | SkScalar ww = SkIntToScalar(w); |
| 25 | SkScalar hh = SkIntToScalar(h); |
| 26 | |
mike@reedtribe.org | 3bd2173 | 2012-09-26 02:45:10 +0000 | [diff] [blame] | 27 | { |
| 28 | SkCanvas c(*src); |
| 29 | p.setColor(0xFFFFCC44); |
| 30 | r.set(0, 0, ww*3/4, hh*3/4); |
| 31 | c.drawOval(r, p); |
| 32 | } |
tomhudson@google.com | 86bf5c9 | 2011-04-26 18:15:03 +0000 | [diff] [blame] | 33 | |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 34 | dst->setConfig(SkBitmap::kARGB_8888_Config, w, h); |
| 35 | dst->allocPixels(); |
junov@google.com | dbfac8a | 2012-12-06 21:47:40 +0000 | [diff] [blame] | 36 | dst->eraseColor(SK_ColorTRANSPARENT); |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 37 | |
mike@reedtribe.org | 3bd2173 | 2012-09-26 02:45:10 +0000 | [diff] [blame] | 38 | { |
| 39 | SkCanvas c(*dst); |
| 40 | p.setColor(0xFF66AAFF); |
| 41 | r.set(ww/3, hh/3, ww*19/20, hh*19/20); |
| 42 | c.drawRect(r, p); |
| 43 | } |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 44 | } |
| 45 | |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 46 | class XfermodesGM : public GM { |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 47 | SkBitmap fBG; |
| 48 | SkBitmap fSrcB, fDstB; |
reed@google.com | 5213c04 | 2011-12-20 13:41:09 +0000 | [diff] [blame] | 49 | bool fOnce; |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 50 | |
epoger@google.com | 4eed340 | 2011-06-24 19:14:42 +0000 | [diff] [blame] | 51 | void draw_mode(SkCanvas* canvas, SkXfermode* mode, int alpha, |
reed@google.com | f3c1cc9 | 2010-12-23 16:45:33 +0000 | [diff] [blame] | 52 | SkScalar x, SkScalar y) { |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 53 | SkPaint p; |
tomhudson@google.com | 86bf5c9 | 2011-04-26 18:15:03 +0000 | [diff] [blame] | 54 | |
epoger@google.com | 4eed340 | 2011-06-24 19:14:42 +0000 | [diff] [blame] | 55 | canvas->drawBitmap(fSrcB, x, y, &p); |
| 56 | p.setAlpha(alpha); |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 57 | p.setXfermode(mode); |
epoger@google.com | 4eed340 | 2011-06-24 19:14:42 +0000 | [diff] [blame] | 58 | canvas->drawBitmap(fDstB, x, y, &p); |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 59 | } |
tomhudson@google.com | 86bf5c9 | 2011-04-26 18:15:03 +0000 | [diff] [blame] | 60 | |
reed@google.com | 5213c04 | 2011-12-20 13:41:09 +0000 | [diff] [blame] | 61 | void init() { |
| 62 | if (!fOnce) { |
reed@google.com | 263cb3a | 2011-12-20 14:35:32 +0000 | [diff] [blame] | 63 | // Do all this work in a temporary so we get a deep copy |
| 64 | uint16_t localData[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF }; |
reed@google.com | 5213c04 | 2011-12-20 13:41:09 +0000 | [diff] [blame] | 65 | SkBitmap scratchBitmap; |
| 66 | scratchBitmap.setConfig(SkBitmap::kARGB_4444_Config, 2, 2, 4); |
reed@google.com | 263cb3a | 2011-12-20 14:35:32 +0000 | [diff] [blame] | 67 | scratchBitmap.setPixels(localData); |
reed@google.com | 5213c04 | 2011-12-20 13:41:09 +0000 | [diff] [blame] | 68 | scratchBitmap.setIsOpaque(true); |
| 69 | scratchBitmap.copyTo(&fBG, SkBitmap::kARGB_4444_Config); |
rmistry@google.com | ae933ce | 2012-08-23 18:19:56 +0000 | [diff] [blame] | 70 | |
reed@google.com | 5213c04 | 2011-12-20 13:41:09 +0000 | [diff] [blame] | 71 | make_bitmaps(W, H, &fSrcB, &fDstB); |
| 72 | fOnce = true; |
| 73 | } |
| 74 | } |
| 75 | |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 76 | public: |
reed@google.com | f3c1cc9 | 2010-12-23 16:45:33 +0000 | [diff] [blame] | 77 | const static int W = 64; |
| 78 | const static int H = 64; |
reed@google.com | 5213c04 | 2011-12-20 13:41:09 +0000 | [diff] [blame] | 79 | XfermodesGM() : fOnce(false) {} |
tomhudson@google.com | 86bf5c9 | 2011-04-26 18:15:03 +0000 | [diff] [blame] | 80 | |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 81 | protected: |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 82 | virtual SkString onShortName() { |
reed@android.com | 8015dd8 | 2009-06-21 00:49:18 +0000 | [diff] [blame] | 83 | return SkString("xfermodes"); |
| 84 | } |
| 85 | |
tomhudson@google.com | 86bf5c9 | 2011-04-26 18:15:03 +0000 | [diff] [blame] | 86 | virtual SkISize onISize() { |
epoger@google.com | 4eed340 | 2011-06-24 19:14:42 +0000 | [diff] [blame] | 87 | return make_isize(790, 640); |
reed@android.com | 048522d | 2009-06-23 12:19:41 +0000 | [diff] [blame] | 88 | } |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 89 | |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 90 | virtual void onDraw(SkCanvas* canvas) { |
reed@google.com | 5213c04 | 2011-12-20 13:41:09 +0000 | [diff] [blame] | 91 | this->init(); |
| 92 | |
reed@google.com | f3c1cc9 | 2010-12-23 16:45:33 +0000 | [diff] [blame] | 93 | canvas->translate(SkIntToScalar(10), SkIntToScalar(20)); |
tomhudson@google.com | 86bf5c9 | 2011-04-26 18:15:03 +0000 | [diff] [blame] | 94 | |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 95 | const struct { |
reed@google.com | f3c1cc9 | 2010-12-23 16:45:33 +0000 | [diff] [blame] | 96 | SkXfermode::Mode fMode; |
epoger@google.com | 4eed340 | 2011-06-24 19:14:42 +0000 | [diff] [blame] | 97 | const char* fLabel; |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 98 | } gModes[] = { |
epoger@google.com | 4eed340 | 2011-06-24 19:14:42 +0000 | [diff] [blame] | 99 | { SkXfermode::kClear_Mode, "Clear" }, |
| 100 | { SkXfermode::kSrc_Mode, "Src" }, |
| 101 | { SkXfermode::kDst_Mode, "Dst" }, |
| 102 | { SkXfermode::kSrcOver_Mode, "SrcOver" }, |
| 103 | { SkXfermode::kDstOver_Mode, "DstOver" }, |
| 104 | { SkXfermode::kSrcIn_Mode, "SrcIn" }, |
| 105 | { SkXfermode::kDstIn_Mode, "DstIn" }, |
| 106 | { SkXfermode::kSrcOut_Mode, "SrcOut" }, |
| 107 | { SkXfermode::kDstOut_Mode, "DstOut" }, |
| 108 | { SkXfermode::kSrcATop_Mode, "SrcATop" }, |
| 109 | { SkXfermode::kDstATop_Mode, "DstATop" }, |
| 110 | { SkXfermode::kXor_Mode, "Xor" }, |
tomhudson@google.com | 86bf5c9 | 2011-04-26 18:15:03 +0000 | [diff] [blame] | 111 | |
epoger@google.com | 4eed340 | 2011-06-24 19:14:42 +0000 | [diff] [blame] | 112 | { SkXfermode::kPlus_Mode, "Plus" }, |
| 113 | { SkXfermode::kMultiply_Mode, "Multiply" }, |
| 114 | { SkXfermode::kScreen_Mode, "Screen" }, |
| 115 | { SkXfermode::kOverlay_Mode, "Overlay" }, |
| 116 | { SkXfermode::kDarken_Mode, "Darken" }, |
| 117 | { SkXfermode::kLighten_Mode, "Lighten" }, |
| 118 | { SkXfermode::kColorDodge_Mode, "ColorDodge" }, |
| 119 | { SkXfermode::kColorBurn_Mode, "ColorBurn" }, |
| 120 | { SkXfermode::kHardLight_Mode, "HardLight" }, |
| 121 | { SkXfermode::kSoftLight_Mode, "SoftLight" }, |
| 122 | { SkXfermode::kDifference_Mode, "Difference" }, |
| 123 | { SkXfermode::kExclusion_Mode, "Exclusion" }, |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 124 | }; |
tomhudson@google.com | 86bf5c9 | 2011-04-26 18:15:03 +0000 | [diff] [blame] | 125 | |
reed@google.com | f3c1cc9 | 2010-12-23 16:45:33 +0000 | [diff] [blame] | 126 | const SkScalar w = SkIntToScalar(W); |
| 127 | const SkScalar h = SkIntToScalar(H); |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 128 | SkShader* s = SkShader::CreateBitmapShader(fBG, |
| 129 | SkShader::kRepeat_TileMode, |
| 130 | SkShader::kRepeat_TileMode); |
| 131 | SkMatrix m; |
| 132 | m.setScale(SkIntToScalar(6), SkIntToScalar(6)); |
| 133 | s->setLocalMatrix(m); |
tomhudson@google.com | 86bf5c9 | 2011-04-26 18:15:03 +0000 | [diff] [blame] | 134 | |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 135 | SkPaint labelP; |
| 136 | labelP.setAntiAlias(true); |
| 137 | labelP.setTextAlign(SkPaint::kCenter_Align); |
tomhudson@google.com | 86bf5c9 | 2011-04-26 18:15:03 +0000 | [diff] [blame] | 138 | |
reed@android.com | c9c6422 | 2009-06-23 00:09:12 +0000 | [diff] [blame] | 139 | const int W = 5; |
tomhudson@google.com | 86bf5c9 | 2011-04-26 18:15:03 +0000 | [diff] [blame] | 140 | |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 141 | SkScalar x0 = 0; |
epoger@google.com | 4eed340 | 2011-06-24 19:14:42 +0000 | [diff] [blame] | 142 | for (int twice = 0; twice < 2; twice++) { |
| 143 | SkScalar x = x0, y = 0; |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 144 | for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) { |
reed@android.com | c9c6422 | 2009-06-23 00:09:12 +0000 | [diff] [blame] | 145 | SkXfermode* mode = SkXfermode::Create(gModes[i].fMode); |
reed@google.com | f3c1cc9 | 2010-12-23 16:45:33 +0000 | [diff] [blame] | 146 | SkAutoUnref aur(mode); |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 147 | SkRect r; |
| 148 | r.set(x, y, x+w, y+h); |
tomhudson@google.com | 86bf5c9 | 2011-04-26 18:15:03 +0000 | [diff] [blame] | 149 | |
reed@google.com | f3c1cc9 | 2010-12-23 16:45:33 +0000 | [diff] [blame] | 150 | SkPaint p; |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 151 | p.setStyle(SkPaint::kFill_Style); |
| 152 | p.setShader(s); |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 153 | canvas->drawRect(r, p); |
tomhudson@google.com | 86bf5c9 | 2011-04-26 18:15:03 +0000 | [diff] [blame] | 154 | |
reed@google.com | f3c1cc9 | 2010-12-23 16:45:33 +0000 | [diff] [blame] | 155 | canvas->saveLayer(&r, NULL, SkCanvas::kARGB_ClipLayer_SaveFlag); |
epoger@google.com | 4eed340 | 2011-06-24 19:14:42 +0000 | [diff] [blame] | 156 | draw_mode(canvas, mode, twice ? 0x88 : 0xFF, r.fLeft, r.fTop); |
reed@google.com | f3c1cc9 | 2010-12-23 16:45:33 +0000 | [diff] [blame] | 157 | canvas->restore(); |
tomhudson@google.com | 86bf5c9 | 2011-04-26 18:15:03 +0000 | [diff] [blame] | 158 | |
reed@google.com | f3c1cc9 | 2010-12-23 16:45:33 +0000 | [diff] [blame] | 159 | r.inset(-SK_ScalarHalf, -SK_ScalarHalf); |
| 160 | p.setStyle(SkPaint::kStroke_Style); |
| 161 | p.setShader(NULL); |
| 162 | canvas->drawRect(r, p); |
tomhudson@google.com | 86bf5c9 | 2011-04-26 18:15:03 +0000 | [diff] [blame] | 163 | |
reed@google.com | f3c1cc9 | 2010-12-23 16:45:33 +0000 | [diff] [blame] | 164 | #if 1 |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 165 | canvas->drawText(gModes[i].fLabel, strlen(gModes[i].fLabel), |
| 166 | x + w/2, y - labelP.getTextSize()/2, labelP); |
reed@google.com | f3c1cc9 | 2010-12-23 16:45:33 +0000 | [diff] [blame] | 167 | #endif |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 168 | x += w + SkIntToScalar(10); |
reed@android.com | c9c6422 | 2009-06-23 00:09:12 +0000 | [diff] [blame] | 169 | if ((i % W) == W - 1) { |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 170 | x = x0; |
| 171 | y += h + SkIntToScalar(30); |
| 172 | } |
| 173 | } |
epoger@google.com | 4eed340 | 2011-06-24 19:14:42 +0000 | [diff] [blame] | 174 | x0 += SkIntToScalar(400); |
reed@android.com | dd0ac28 | 2009-06-20 02:38:16 +0000 | [diff] [blame] | 175 | } |
| 176 | s->unref(); |
| 177 | } |
| 178 | |
| 179 | private: |
| 180 | typedef GM INHERITED; |
| 181 | }; |
| 182 | |
| 183 | ////////////////////////////////////////////////////////////////////////////// |
| 184 | |
| 185 | static GM* MyFactory(void*) { return new XfermodesGM; } |
| 186 | static GMRegistry reg(MyFactory); |
| 187 | |
| 188 | } |