2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
11 #include "SkXfermode.h"
15 //! A WxH image with a rectangle in the lower right.
16 kRectangleImage_SrcType = 0x01,
17 //! kRectangleImage_SrcType with an alpha of 34.5%.
18 kRectangleImageWithAlpha_SrcType = 0x02,
19 //! kRectnagleImageWithAlpha_SrcType scaled down by half.
20 kSmallRectangleImageWithAlpha_SrcType = 0x04,
21 //! kRectangleImage_SrcType drawn directly instead in an image.
22 kRectangle_SrcType = 0x08,
23 //! Two rectangles, first on the right half, second on the bottom half.
24 kQuarterClear_SrcType = 0x10,
25 //! kQuarterClear_SrcType in a layer.
26 kQuarterClearInLayer_SrcType = 0x20,
27 //! A W/2xH/2 transparent image.
28 kSmallTransparentImage_SrcType = 0x40,
29 //! kRectangleImage_SrcType drawn directly with a mask.
30 kRectangleWithMask_SrcType = 0x80,
32 kAll_SrcType = 0xFF, //!< All the source types.
33 kBasic_SrcType = 0x03, //!< Just basic source types.
39 int fSourceTypeMask; // The source types to use this
40 // mode with. See draw_mode for
41 // an explanation of each type.
42 // PDF has to play some tricks
43 // to support the base modes,
44 // test those more extensively.
46 { SkBlendMode::kClear, "Clear", kAll_SrcType },
47 { SkBlendMode::kSrc, "Src", kAll_SrcType },
48 { SkBlendMode::kDst, "Dst", kAll_SrcType },
49 { SkBlendMode::kSrcOver, "SrcOver", kAll_SrcType },
50 { SkBlendMode::kDstOver, "DstOver", kAll_SrcType },
51 { SkBlendMode::kSrcIn, "SrcIn", kAll_SrcType },
52 { SkBlendMode::kDstIn, "DstIn", kAll_SrcType },
53 { SkBlendMode::kSrcOut, "SrcOut", kAll_SrcType },
54 { SkBlendMode::kDstOut, "DstOut", kAll_SrcType },
55 { SkBlendMode::kSrcATop, "SrcATop", kAll_SrcType },
56 { SkBlendMode::kDstATop, "DstATop", kAll_SrcType },
58 { SkBlendMode::kXor, "Xor", kBasic_SrcType },
59 { SkBlendMode::kPlus, "Plus", kBasic_SrcType },
60 { SkBlendMode::kModulate, "Modulate", kAll_SrcType },
61 { SkBlendMode::kScreen, "Screen", kBasic_SrcType },
62 { SkBlendMode::kOverlay, "Overlay", kBasic_SrcType },
63 { SkBlendMode::kDarken, "Darken", kBasic_SrcType },
64 { SkBlendMode::kLighten, "Lighten", kBasic_SrcType },
65 { SkBlendMode::kColorDodge, "ColorDodge", kBasic_SrcType },
66 { SkBlendMode::kColorBurn, "ColorBurn", kBasic_SrcType },
67 { SkBlendMode::kHardLight, "HardLight", kBasic_SrcType },
68 { SkBlendMode::kSoftLight, "SoftLight", kBasic_SrcType },
69 { SkBlendMode::kDifference, "Difference", kBasic_SrcType },
70 { SkBlendMode::kExclusion, "Exclusion", kBasic_SrcType },
71 { SkBlendMode::kMultiply, "Multiply", kAll_SrcType },
72 { SkBlendMode::kHue, "Hue", kBasic_SrcType },
73 { SkBlendMode::kSaturation, "Saturation", kBasic_SrcType },
74 { SkBlendMode::kColor, "Color", kBasic_SrcType },
75 { SkBlendMode::kLuminosity, "Luminosity", kBasic_SrcType },
78 static void make_bitmaps(int w, int h, SkBitmap* src, SkBitmap* dst,
79 SkBitmap* transparent) {
80 src->allocN32Pixels(w, h);
81 src->eraseColor(SK_ColorTRANSPARENT);
87 SkScalar ww = SkIntToScalar(w);
88 SkScalar hh = SkIntToScalar(h);
92 p.setColor(sk_tool_utils::color_to_565(0xFFFFCC44));
93 r.set(0, 0, ww*3/4, hh*3/4);
97 dst->allocN32Pixels(w, h);
98 dst->eraseColor(SK_ColorTRANSPARENT);
102 p.setColor(sk_tool_utils::color_to_565(0xFF66AAFF));
103 r.set(ww/3, hh/3, ww*19/20, hh*19/20);
107 transparent->allocN32Pixels(w, h);
108 transparent->eraseColor(SK_ColorTRANSPARENT);
111 static uint16_t gData[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
113 class XfermodesGM : public skiagm::GM {
115 SkBitmap fSrcB, fDstB, fTransparent;
117 /* The srcType argument indicates what to draw for the source part. Skia
118 * uses the implied shape of the drawing command and these modes
121 void draw_mode(SkCanvas* canvas, SkBlendMode mode, SrcType srcType, SkScalar x, SkScalar y) {
124 bool restoreNeeded = false;
125 m.setTranslate(x, y);
127 canvas->drawBitmap(fSrcB, x, y, &p);
128 p.setBlendMode(mode);
130 case kSmallTransparentImage_SrcType: {
131 m.postScale(SK_ScalarHalf, SK_ScalarHalf, x, y);
133 SkAutoCanvasRestore acr(canvas, true);
135 canvas->drawBitmap(fTransparent, 0, 0, &p);
138 case kQuarterClearInLayer_SrcType: {
139 SkRect bounds = SkRect::MakeXYWH(x, y, SkIntToScalar(W),
141 canvas->saveLayer(&bounds, &p);
142 restoreNeeded = true;
143 p.setBlendMode(SkBlendMode::kSrcOver);
146 case kQuarterClear_SrcType: {
147 SkScalar halfW = SkIntToScalar(W) / 2;
148 SkScalar halfH = SkIntToScalar(H) / 2;
149 p.setColor(sk_tool_utils::color_to_565(0xFF66AAFF));
150 SkRect r = SkRect::MakeXYWH(x + halfW, y, halfW,
152 canvas->drawRect(r, p);
153 p.setColor(sk_tool_utils::color_to_565(0xFFAA66FF));
154 r = SkRect::MakeXYWH(x, y + halfH, SkIntToScalar(W), halfH);
155 canvas->drawRect(r, p);
158 case kRectangleWithMask_SrcType: {
160 restoreNeeded = true;
161 SkScalar w = SkIntToScalar(W);
162 SkScalar h = SkIntToScalar(H);
163 SkRect r = SkRect::MakeXYWH(x, y + h / 4, w, h * 23 / 60);
167 case kRectangle_SrcType: {
168 SkScalar w = SkIntToScalar(W);
169 SkScalar h = SkIntToScalar(H);
170 SkRect r = SkRect::MakeXYWH(x + w / 3, y + h / 3,
171 w * 37 / 60, h * 37 / 60);
172 p.setColor(sk_tool_utils::color_to_565(0xFF66AAFF));
173 canvas->drawRect(r, p);
176 case kSmallRectangleImageWithAlpha_SrcType:
177 m.postScale(SK_ScalarHalf, SK_ScalarHalf, x, y);
179 case kRectangleImageWithAlpha_SrcType:
182 case kRectangleImage_SrcType: {
183 SkAutoCanvasRestore acr(canvas, true);
185 canvas->drawBitmap(fDstB, 0, 0, &p);
197 void onOnceBeforeDraw() override {
198 fBG.installPixels(SkImageInfo::Make(2, 2, kARGB_4444_SkColorType,
199 kOpaque_SkAlphaType),
202 make_bitmaps(W, H, &fSrcB, &fDstB, &fTransparent);
206 const static int W = 64;
207 const static int H = 64;
211 SkString onShortName() override {
212 return SkString("xfermodes");
215 SkISize onISize() override {
216 return SkISize::Make(1990, 570);
219 void onDraw(SkCanvas* canvas) override {
220 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
222 const SkScalar w = SkIntToScalar(W);
223 const SkScalar h = SkIntToScalar(H);
225 m.setScale(SkIntToScalar(6), SkIntToScalar(6));
226 auto s = SkShader::MakeBitmapShader(fBG, SkShader::kRepeat_TileMode,
227 SkShader::kRepeat_TileMode, &m);
230 labelP.setAntiAlias(true);
231 sk_tool_utils::set_portable_typeface(&labelP);
232 labelP.setTextAlign(SkPaint::kCenter_Align);
238 for (int sourceType = 1; sourceType & kAll_SrcType; sourceType <<= 1) {
239 SkScalar x = x0, y = y0;
240 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
241 if ((gModes[i].fSourceTypeMask & sourceType) == 0) {
244 SkRect r{ x, y, x+w, y+h };
247 p.setStyle(SkPaint::kFill_Style);
249 canvas->drawRect(r, p);
251 canvas->saveLayer(&r, nullptr);
252 draw_mode(canvas, gModes[i].fMode, static_cast<SrcType>(sourceType),
256 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
257 p.setStyle(SkPaint::kStroke_Style);
258 p.setShader(nullptr);
259 canvas->drawRect(r, p);
262 canvas->drawText(gModes[i].fLabel, strlen(gModes[i].fLabel),
263 x + w/2, y - labelP.getTextSize()/2, labelP);
265 x += w + SkIntToScalar(10);
266 if ((i % W) == W - 1) {
268 y += h + SkIntToScalar(30);
273 y += h + SkIntToScalar(30);
277 x0 += SkIntToScalar(400);
284 typedef GM INHERITED;
286 DEF_GM( return new XfermodesGM; )