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