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