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