blob: 7ee1e8008614900370f92d44c2d46ba1e949d190 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@android.comdd0ac282009-06-20 02:38:16 +00008#include "gm.h"
9#include "SkBitmap.h"
10#include "SkShader.h"
11#include "SkXfermode.h"
12
13namespace skiagm {
14
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +000015static void make_bitmaps(int w, int h, SkBitmap* src, SkBitmap* dst,
16 SkBitmap* transparent) {
reed@android.comdd0ac282009-06-20 02:38:16 +000017 src->setConfig(SkBitmap::kARGB_8888_Config, w, h);
18 src->allocPixels();
junov@google.comdbfac8a2012-12-06 21:47:40 +000019 src->eraseColor(SK_ColorTRANSPARENT);
reed@android.comdd0ac282009-06-20 02:38:16 +000020
reed@android.comdd0ac282009-06-20 02:38:16 +000021 SkPaint p;
mike@reedtribe.org3bd21732012-09-26 02:45:10 +000022 p.setAntiAlias(true);
23
reed@android.comdd0ac282009-06-20 02:38:16 +000024 SkRect r;
25 SkScalar ww = SkIntToScalar(w);
26 SkScalar hh = SkIntToScalar(h);
27
mike@reedtribe.org3bd21732012-09-26 02:45:10 +000028 {
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 }
tomhudson@google.com86bf5c92011-04-26 18:15:03 +000034
reed@android.comdd0ac282009-06-20 02:38:16 +000035 dst->setConfig(SkBitmap::kARGB_8888_Config, w, h);
36 dst->allocPixels();
junov@google.comdbfac8a2012-12-06 21:47:40 +000037 dst->eraseColor(SK_ColorTRANSPARENT);
reed@android.comdd0ac282009-06-20 02:38:16 +000038
mike@reedtribe.org3bd21732012-09-26 02:45:10 +000039 {
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 }
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +000045
46 transparent->setConfig(SkBitmap::kARGB_8888_Config, w, h);
47 transparent->allocPixels();
48 transparent->eraseColor(SK_ColorTRANSPARENT);
reed@android.comdd0ac282009-06-20 02:38:16 +000049}
50
scroggo@google.com7312a182013-01-28 21:14:21 +000051static uint16_t gData[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
52
reed@android.comdd0ac282009-06-20 02:38:16 +000053class XfermodesGM : public GM {
vandebo@chromium.org3b416212013-10-30 20:48:05 +000054 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,
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +000067 //! A W/2xH/2 transparent image.
68 kSmallTransparentImage_SrcType = 0x40,
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +000069 //! kRectangleImage_SrcType drawn directly with a mask.
70 kRectangleWithMask_SrcType = 0x80,
vandebo@chromium.org3b416212013-10-30 20:48:05 +000071
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +000072 kAll_SrcType = 0xFF, //!< All the source types.
vandebo@chromium.org3b416212013-10-30 20:48:05 +000073 kBasic_SrcType = 0x03, //!< Just basic source types.
74 };
75
reed@android.comdd0ac282009-06-20 02:38:16 +000076 SkBitmap fBG;
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +000077 SkBitmap fSrcB, fDstB, fTransparent;
reed@android.comdd0ac282009-06-20 02:38:16 +000078
vandebo@chromium.org3b416212013-10-30 20:48:05 +000079 /* 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 */
83 void draw_mode(SkCanvas* canvas, SkXfermode* mode, SrcType srcType,
reed@google.comf3c1cc92010-12-23 16:45:33 +000084 SkScalar x, SkScalar y) {
reed@android.comdd0ac282009-06-20 02:38:16 +000085 SkPaint p;
vandebo@chromium.org3b416212013-10-30 20:48:05 +000086 SkMatrix m;
87 bool restoreNeeded = false;
88 m.setTranslate(x, y);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +000089
vandebo@chromium.org3b416212013-10-30 20:48:05 +000090 canvas->drawBitmapMatrix(fSrcB, m, &p);
reed@android.comdd0ac282009-06-20 02:38:16 +000091 p.setXfermode(mode);
vandebo@chromium.org3b416212013-10-30 20:48:05 +000092 switch (srcType) {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +000093 case kSmallTransparentImage_SrcType:
94 m.postScale(SK_ScalarHalf, SK_ScalarHalf, x, y);
95 canvas->drawBitmapMatrix(fTransparent, m, &p);
96 break;
vandebo@chromium.org3b416212013-10-30 20:48:05 +000097 case kQuarterClearInLayer_SrcType: {
vandebo@chromium.orgf5747da2013-10-30 21:29:47 +000098 SkRect bounds = SkRect::MakeXYWH(x, y, SkIntToScalar(W),
99 SkIntToScalar(H));
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000100 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);
vandebo@chromium.orgf5747da2013-10-30 21:29:47 +0000109 SkRect r = SkRect::MakeXYWH(x + halfW, y, halfW,
110 SkIntToScalar(H));
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000111 canvas->drawRect(r, p);
112 p.setColor(0xFFAA66FF);
vandebo@chromium.orgf5747da2013-10-30 21:29:47 +0000113 r = SkRect::MakeXYWH(x, y + halfH, SkIntToScalar(W), halfH);
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000114 canvas->drawRect(r, p);
115 break;
116 }
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +0000117 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 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000126 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 }
reed@android.comdd0ac282009-06-20 02:38:16 +0000151 }
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000152
scroggo@google.com7312a182013-01-28 21:14:21 +0000153 virtual void onOnceBeforeDraw() SK_OVERRIDE {
reed@google.com383a6972013-10-21 14:00:07 +0000154 fBG.setConfig(SkBitmap::kARGB_4444_Config, 2, 2, 4, kOpaque_SkAlphaType);
scroggo@google.com7312a182013-01-28 21:14:21 +0000155 fBG.setPixels(gData);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000156
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +0000157 make_bitmaps(W, H, &fSrcB, &fDstB, &fTransparent);
reed@google.com5213c042011-12-20 13:41:09 +0000158 }
159
reed@android.comdd0ac282009-06-20 02:38:16 +0000160public:
reed@google.comf3c1cc92010-12-23 16:45:33 +0000161 const static int W = 64;
162 const static int H = 64;
scroggo@google.com7312a182013-01-28 21:14:21 +0000163 XfermodesGM() {}
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000164
reed@android.comdd0ac282009-06-20 02:38:16 +0000165protected:
reed@android.com048522d2009-06-23 12:19:41 +0000166 virtual SkString onShortName() {
reed@android.com8015dd82009-06-21 00:49:18 +0000167 return SkString("xfermodes");
168 }
169
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000170 virtual SkISize onISize() {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +0000171 return make_isize(1990, 640);
reed@android.com048522d2009-06-23 12:19:41 +0000172 }
reed@android.comdd0ac282009-06-20 02:38:16 +0000173
reed@android.comdd0ac282009-06-20 02:38:16 +0000174 virtual void onDraw(SkCanvas* canvas) {
reed@google.comf3c1cc92010-12-23 16:45:33 +0000175 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000176
reed@android.comdd0ac282009-06-20 02:38:16 +0000177 const struct {
reed@google.comf3c1cc92010-12-23 16:45:33 +0000178 SkXfermode::Mode fMode;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000179 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.
reed@android.comdd0ac282009-06-20 02:38:16 +0000186 } gModes[] = {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000187 { 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 },
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000198
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000199 { 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 },
reed@android.comdd0ac282009-06-20 02:38:16 +0000217 };
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000218
reed@google.comf3c1cc92010-12-23 16:45:33 +0000219 const SkScalar w = SkIntToScalar(W);
220 const SkScalar h = SkIntToScalar(H);
reed@android.comdd0ac282009-06-20 02:38:16 +0000221 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);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000227
reed@android.comdd0ac282009-06-20 02:38:16 +0000228 SkPaint labelP;
229 labelP.setAntiAlias(true);
230 labelP.setTextAlign(SkPaint::kCenter_Align);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000231
reed@android.comc9c64222009-06-23 00:09:12 +0000232 const int W = 5;
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000233
reed@android.comdd0ac282009-06-20 02:38:16 +0000234 SkScalar x0 = 0;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000235 SkScalar y0 = 0;
236 for (int sourceType = 1; sourceType & kAll_SrcType; sourceType <<= 1) {
237 SkScalar x = x0, y = y0;
reed@android.comdd0ac282009-06-20 02:38:16 +0000238 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000239 if ((gModes[i].fSourceTypeMask & sourceType) == 0) {
240 continue;
241 }
reed@android.comc9c64222009-06-23 00:09:12 +0000242 SkXfermode* mode = SkXfermode::Create(gModes[i].fMode);
reed@google.comf3c1cc92010-12-23 16:45:33 +0000243 SkAutoUnref aur(mode);
reed@android.comdd0ac282009-06-20 02:38:16 +0000244 SkRect r;
245 r.set(x, y, x+w, y+h);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000246
reed@google.comf3c1cc92010-12-23 16:45:33 +0000247 SkPaint p;
reed@android.comdd0ac282009-06-20 02:38:16 +0000248 p.setStyle(SkPaint::kFill_Style);
249 p.setShader(s);
reed@android.comdd0ac282009-06-20 02:38:16 +0000250 canvas->drawRect(r, p);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000251
reed@google.comf3c1cc92010-12-23 16:45:33 +0000252 canvas->saveLayer(&r, NULL, SkCanvas::kARGB_ClipLayer_SaveFlag);
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000253 draw_mode(canvas, mode, static_cast<SrcType>(sourceType),
254 r.fLeft, r.fTop);
reed@google.comf3c1cc92010-12-23 16:45:33 +0000255 canvas->restore();
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000256
reed@google.comf3c1cc92010-12-23 16:45:33 +0000257 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
258 p.setStyle(SkPaint::kStroke_Style);
259 p.setShader(NULL);
260 canvas->drawRect(r, p);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000261
reed@google.comf3c1cc92010-12-23 16:45:33 +0000262#if 1
reed@android.comdd0ac282009-06-20 02:38:16 +0000263 canvas->drawText(gModes[i].fLabel, strlen(gModes[i].fLabel),
264 x + w/2, y - labelP.getTextSize()/2, labelP);
reed@google.comf3c1cc92010-12-23 16:45:33 +0000265#endif
reed@android.comdd0ac282009-06-20 02:38:16 +0000266 x += w + SkIntToScalar(10);
reed@android.comc9c64222009-06-23 00:09:12 +0000267 if ((i % W) == W - 1) {
reed@android.comdd0ac282009-06-20 02:38:16 +0000268 x = x0;
269 y += h + SkIntToScalar(30);
270 }
271 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000272 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 }
reed@android.comdd0ac282009-06-20 02:38:16 +0000281 }
282 s->unref();
283 }
284
285private:
286 typedef GM INHERITED;
287};
288
289//////////////////////////////////////////////////////////////////////////////
290
291static GM* MyFactory(void*) { return new XfermodesGM; }
292static GMRegistry reg(MyFactory);
293
294}