• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
8 #include "gm.h"
9 #include "SkBitmap.h"
10 #include "SkShader.h"
11 #include "SkXfermode.h"
12 
13 namespace skiagm {
14 
make_bitmaps(int w,int h,SkBitmap * src,SkBitmap * dst,SkBitmap * transparent)15 static void make_bitmaps(int w, int h, SkBitmap* src, SkBitmap* dst,
16                          SkBitmap* transparent) {
17     src->allocN32Pixels(w, h);
18     src->eraseColor(SK_ColorTRANSPARENT);
19 
20     SkPaint p;
21     p.setAntiAlias(true);
22 
23     SkRect r;
24     SkScalar ww = SkIntToScalar(w);
25     SkScalar hh = SkIntToScalar(h);
26 
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     }
33 
34     dst->allocN32Pixels(w, h);
35     dst->eraseColor(SK_ColorTRANSPARENT);
36 
37     {
38         SkCanvas c(*dst);
39         p.setColor(0xFF66AAFF);
40         r.set(ww/3, hh/3, ww*19/20, hh*19/20);
41         c.drawRect(r, p);
42     }
43 
44     transparent->allocN32Pixels(w, h);
45     transparent->eraseColor(SK_ColorTRANSPARENT);
46 }
47 
48 static uint16_t gData[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
49 
50 class XfermodesGM : public GM {
51     enum SrcType {
52      //! A WxH image with a rectangle in the lower right.
53      kRectangleImage_SrcType               = 0x01,
54      //! kRectangleImage_SrcType with an alpha of 34.5%.
55      kRectangleImageWithAlpha_SrcType      = 0x02,
56      //! kRectnagleImageWithAlpha_SrcType scaled down by half.
57      kSmallRectangleImageWithAlpha_SrcType = 0x04,
58      //! kRectangleImage_SrcType drawn directly instead in an image.
59      kRectangle_SrcType                    = 0x08,
60      //! Two rectangles, first on the right half, second on the bottom half.
61      kQuarterClear_SrcType                 = 0x10,
62      //! kQuarterClear_SrcType in a layer.
63      kQuarterClearInLayer_SrcType          = 0x20,
64      //! A W/2xH/2 transparent image.
65      kSmallTransparentImage_SrcType        = 0x40,
66      //! kRectangleImage_SrcType drawn directly with a mask.
67      kRectangleWithMask_SrcType            = 0x80,
68 
69      kAll_SrcType                          = 0xFF, //!< All the source types.
70      kBasic_SrcType                        = 0x03, //!< Just basic source types.
71     };
72 
73     SkBitmap    fBG;
74     SkBitmap    fSrcB, fDstB, fTransparent;
75 
76     /* The srcType argument indicates what to draw for the source part. Skia
77      * uses the implied shape of the drawing command and these modes
78      * demonstrate that.
79      */
draw_mode(SkCanvas * canvas,SkXfermode * mode,SrcType srcType,SkScalar x,SkScalar y)80     void draw_mode(SkCanvas* canvas, SkXfermode* mode, SrcType srcType,
81                    SkScalar x, SkScalar y) {
82         SkPaint p;
83         SkMatrix m;
84         bool restoreNeeded = false;
85         m.setTranslate(x, y);
86 
87         canvas->drawBitmapMatrix(fSrcB, m, &p);
88         p.setXfermode(mode);
89         switch (srcType) {
90             case kSmallTransparentImage_SrcType:
91                 m.postScale(SK_ScalarHalf, SK_ScalarHalf, x, y);
92                 canvas->drawBitmapMatrix(fTransparent, m, &p);
93                 break;
94             case kQuarterClearInLayer_SrcType: {
95                 SkRect bounds = SkRect::MakeXYWH(x, y, SkIntToScalar(W),
96                                                  SkIntToScalar(H));
97                 canvas->saveLayer(&bounds, &p);
98                 restoreNeeded = true;
99                 p.setXfermodeMode(SkXfermode::kSrcOver_Mode);
100                 // Fall through.
101             }
102             case kQuarterClear_SrcType: {
103                 SkScalar halfW = SkIntToScalar(W) / 2;
104                 SkScalar halfH = SkIntToScalar(H) / 2;
105                 p.setColor(0xFF66AAFF);
106                 SkRect r = SkRect::MakeXYWH(x + halfW, y, halfW,
107                                             SkIntToScalar(H));
108                 canvas->drawRect(r, p);
109                 p.setColor(0xFFAA66FF);
110                 r = SkRect::MakeXYWH(x, y + halfH, SkIntToScalar(W), halfH);
111                 canvas->drawRect(r, p);
112                 break;
113             }
114             case kRectangleWithMask_SrcType: {
115                 canvas->save();
116                 restoreNeeded = true;
117                 SkScalar w = SkIntToScalar(W);
118                 SkScalar h = SkIntToScalar(H);
119                 SkRect r = SkRect::MakeXYWH(x, y + h / 4, w, h * 23 / 60);
120                 canvas->clipRect(r);
121                 // Fall through.
122             }
123             case kRectangle_SrcType: {
124                 SkScalar w = SkIntToScalar(W);
125                 SkScalar h = SkIntToScalar(H);
126                 SkRect r = SkRect::MakeXYWH(x + w / 3, y + h / 3,
127                                             w * 37 / 60, h * 37 / 60);
128                 p.setColor(0xFF66AAFF);
129                 canvas->drawRect(r, p);
130                 break;
131             }
132             case kSmallRectangleImageWithAlpha_SrcType:
133                 m.postScale(SK_ScalarHalf, SK_ScalarHalf, x, y);
134                 // Fall through.
135             case kRectangleImageWithAlpha_SrcType:
136                 p.setAlpha(0x88);
137                 // Fall through.
138             case kRectangleImage_SrcType:
139                 canvas->drawBitmapMatrix(fDstB, m, &p);
140                 break;
141             default:
142                 break;
143         }
144 
145         if (restoreNeeded) {
146             canvas->restore();
147         }
148     }
149 
onOnceBeforeDraw()150     virtual void onOnceBeforeDraw() SK_OVERRIDE {
151         fBG.installPixels(SkImageInfo::Make(2, 2, kARGB_4444_SkColorType,
152                                             kOpaque_SkAlphaType),
153                           gData, 4);
154 
155         make_bitmaps(W, H, &fSrcB, &fDstB, &fTransparent);
156     }
157 
158 public:
159     const static int W = 64;
160     const static int H = 64;
XfermodesGM()161     XfermodesGM() {}
162 
163 protected:
onShortName()164     virtual SkString onShortName() {
165         return SkString("xfermodes");
166     }
167 
onISize()168     virtual SkISize onISize() {
169         return SkISize::Make(1990, 640);
170     }
171 
onDraw(SkCanvas * canvas)172     virtual void onDraw(SkCanvas* canvas) {
173         canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
174 
175         const struct {
176             SkXfermode::Mode  fMode;
177             const char*       fLabel;
178             int               fSourceTypeMask;  // The source types to use this
179                                                 // mode with. See draw_mode for
180                                                 // an explanation of each type.
181                                                 // PDF has to play some tricks
182                                                 // to support the base modes,
183                                                 // test those more extensively.
184         } gModes[] = {
185             { SkXfermode::kClear_Mode,        "Clear",        kAll_SrcType   },
186             { SkXfermode::kSrc_Mode,          "Src",          kAll_SrcType   },
187             { SkXfermode::kDst_Mode,          "Dst",          kAll_SrcType   },
188             { SkXfermode::kSrcOver_Mode,      "SrcOver",      kAll_SrcType   },
189             { SkXfermode::kDstOver_Mode,      "DstOver",      kAll_SrcType   },
190             { SkXfermode::kSrcIn_Mode,        "SrcIn",        kAll_SrcType   },
191             { SkXfermode::kDstIn_Mode,        "DstIn",        kAll_SrcType   },
192             { SkXfermode::kSrcOut_Mode,       "SrcOut",       kAll_SrcType   },
193             { SkXfermode::kDstOut_Mode,       "DstOut",       kAll_SrcType   },
194             { SkXfermode::kSrcATop_Mode,      "SrcATop",      kAll_SrcType   },
195             { SkXfermode::kDstATop_Mode,      "DstATop",      kAll_SrcType   },
196 
197             { SkXfermode::kXor_Mode,          "Xor",          kBasic_SrcType },
198             { SkXfermode::kPlus_Mode,         "Plus",         kBasic_SrcType },
199             { SkXfermode::kModulate_Mode,     "Modulate",     kAll_SrcType   },
200             { SkXfermode::kScreen_Mode,       "Screen",       kBasic_SrcType },
201             { SkXfermode::kOverlay_Mode,      "Overlay",      kBasic_SrcType },
202             { SkXfermode::kDarken_Mode,       "Darken",       kBasic_SrcType },
203             { SkXfermode::kLighten_Mode,      "Lighten",      kBasic_SrcType },
204             { SkXfermode::kColorDodge_Mode,   "ColorDodge",   kBasic_SrcType },
205             { SkXfermode::kColorBurn_Mode,    "ColorBurn",    kBasic_SrcType },
206             { SkXfermode::kHardLight_Mode,    "HardLight",    kBasic_SrcType },
207             { SkXfermode::kSoftLight_Mode,    "SoftLight",    kBasic_SrcType },
208             { SkXfermode::kDifference_Mode,   "Difference",   kBasic_SrcType },
209             { SkXfermode::kExclusion_Mode,    "Exclusion",    kBasic_SrcType },
210             { SkXfermode::kMultiply_Mode,     "Multiply",     kAll_SrcType   },
211             { SkXfermode::kHue_Mode,          "Hue",          kBasic_SrcType },
212             { SkXfermode::kSaturation_Mode,   "Saturation",   kBasic_SrcType },
213             { SkXfermode::kColor_Mode,        "Color",        kBasic_SrcType },
214             { SkXfermode::kLuminosity_Mode,   "Luminosity",   kBasic_SrcType },
215         };
216 
217         const SkScalar w = SkIntToScalar(W);
218         const SkScalar h = SkIntToScalar(H);
219         SkMatrix m;
220         m.setScale(SkIntToScalar(6), SkIntToScalar(6));
221         SkShader* s = SkShader::CreateBitmapShader(fBG,
222                                                    SkShader::kRepeat_TileMode,
223                                                    SkShader::kRepeat_TileMode,
224                                                    &m);
225 
226         SkPaint labelP;
227         labelP.setAntiAlias(true);
228         labelP.setTextAlign(SkPaint::kCenter_Align);
229 
230         const int W = 5;
231 
232         SkScalar x0 = 0;
233         SkScalar y0 = 0;
234         for (int sourceType = 1; sourceType & kAll_SrcType; sourceType <<= 1) {
235             SkScalar x = x0, y = y0;
236             for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
237                 if ((gModes[i].fSourceTypeMask & sourceType) == 0) {
238                     continue;
239                 }
240                 SkXfermode* mode = SkXfermode::Create(gModes[i].fMode);
241                 SkAutoUnref aur(mode);
242                 SkRect r;
243                 r.set(x, y, x+w, y+h);
244 
245                 SkPaint p;
246                 p.setStyle(SkPaint::kFill_Style);
247                 p.setShader(s);
248                 canvas->drawRect(r, p);
249 
250                 canvas->saveLayer(&r, NULL);
251                 draw_mode(canvas, mode, static_cast<SrcType>(sourceType),
252                           r.fLeft, r.fTop);
253                 canvas->restore();
254 
255                 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
256                 p.setStyle(SkPaint::kStroke_Style);
257                 p.setShader(NULL);
258                 canvas->drawRect(r, p);
259 
260 #if 1
261                 canvas->drawText(gModes[i].fLabel, strlen(gModes[i].fLabel),
262                                  x + w/2, y - labelP.getTextSize()/2, labelP);
263 #endif
264                 x += w + SkIntToScalar(10);
265                 if ((i % W) == W - 1) {
266                     x = x0;
267                     y += h + SkIntToScalar(30);
268                 }
269             }
270             if (y < 320) {
271                 if (x > x0) {
272                     y += h + SkIntToScalar(30);
273                 }
274                 y0 = y;
275             } else {
276                 x0 += SkIntToScalar(400);
277                 y0 = 0;
278             }
279         }
280         s->unref();
281     }
282 
283 private:
284     typedef GM INHERITED;
285 };
286 
287 //////////////////////////////////////////////////////////////////////////////
288 
MyFactory(void *)289 static GM* MyFactory(void*) { return new XfermodesGM; }
290 static GMRegistry reg(MyFactory);
291 
292 }
293