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