blob: b5ce8b8ced8ad1ce2375d8b8a687c85bd1f17a3f [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@google.comeb9a46c2014-01-25 16:46:20 +000017 src->allocN32Pixels(w, h);
junov@google.comdbfac8a2012-12-06 21:47:40 +000018 src->eraseColor(SK_ColorTRANSPARENT);
reed@android.comdd0ac282009-06-20 02:38:16 +000019
reed@android.comdd0ac282009-06-20 02:38:16 +000020 SkPaint p;
mike@reedtribe.org3bd21732012-09-26 02:45:10 +000021 p.setAntiAlias(true);
22
reed@android.comdd0ac282009-06-20 02:38:16 +000023 SkRect r;
24 SkScalar ww = SkIntToScalar(w);
25 SkScalar hh = SkIntToScalar(h);
26
mike@reedtribe.org3bd21732012-09-26 02:45:10 +000027 {
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 }
tomhudson@google.com86bf5c92011-04-26 18:15:03 +000033
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000034 dst->allocN32Pixels(w, h);
junov@google.comdbfac8a2012-12-06 21:47:40 +000035 dst->eraseColor(SK_ColorTRANSPARENT);
reed@android.comdd0ac282009-06-20 02:38:16 +000036
mike@reedtribe.org3bd21732012-09-26 02:45:10 +000037 {
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 }
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +000043
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000044 transparent->allocN32Pixels(w, h);
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +000045 transparent->eraseColor(SK_ColorTRANSPARENT);
reed@android.comdd0ac282009-06-20 02:38:16 +000046}
47
scroggo@google.com7312a182013-01-28 21:14:21 +000048static uint16_t gData[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
49
reed@android.comdd0ac282009-06-20 02:38:16 +000050class XfermodesGM : public GM {
vandebo@chromium.org3b416212013-10-30 20:48:05 +000051 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,
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +000064 //! A W/2xH/2 transparent image.
65 kSmallTransparentImage_SrcType = 0x40,
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +000066 //! kRectangleImage_SrcType drawn directly with a mask.
67 kRectangleWithMask_SrcType = 0x80,
vandebo@chromium.org3b416212013-10-30 20:48:05 +000068
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +000069 kAll_SrcType = 0xFF, //!< All the source types.
vandebo@chromium.org3b416212013-10-30 20:48:05 +000070 kBasic_SrcType = 0x03, //!< Just basic source types.
71 };
72
reed@android.comdd0ac282009-06-20 02:38:16 +000073 SkBitmap fBG;
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +000074 SkBitmap fSrcB, fDstB, fTransparent;
reed@android.comdd0ac282009-06-20 02:38:16 +000075
vandebo@chromium.org3b416212013-10-30 20:48:05 +000076 /* 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 */
80 void draw_mode(SkCanvas* canvas, SkXfermode* mode, SrcType srcType,
reed@google.comf3c1cc92010-12-23 16:45:33 +000081 SkScalar x, SkScalar y) {
reed@android.comdd0ac282009-06-20 02:38:16 +000082 SkPaint p;
vandebo@chromium.org3b416212013-10-30 20:48:05 +000083 SkMatrix m;
84 bool restoreNeeded = false;
85 m.setTranslate(x, y);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +000086
vandebo@chromium.org3b416212013-10-30 20:48:05 +000087 canvas->drawBitmapMatrix(fSrcB, m, &p);
reed@android.comdd0ac282009-06-20 02:38:16 +000088 p.setXfermode(mode);
vandebo@chromium.org3b416212013-10-30 20:48:05 +000089 switch (srcType) {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +000090 case kSmallTransparentImage_SrcType:
91 m.postScale(SK_ScalarHalf, SK_ScalarHalf, x, y);
92 canvas->drawBitmapMatrix(fTransparent, m, &p);
93 break;
vandebo@chromium.org3b416212013-10-30 20:48:05 +000094 case kQuarterClearInLayer_SrcType: {
vandebo@chromium.orgf5747da2013-10-30 21:29:47 +000095 SkRect bounds = SkRect::MakeXYWH(x, y, SkIntToScalar(W),
96 SkIntToScalar(H));
vandebo@chromium.org3b416212013-10-30 20:48:05 +000097 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);
vandebo@chromium.orgf5747da2013-10-30 21:29:47 +0000106 SkRect r = SkRect::MakeXYWH(x + halfW, y, halfW,
107 SkIntToScalar(H));
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000108 canvas->drawRect(r, p);
109 p.setColor(0xFFAA66FF);
vandebo@chromium.orgf5747da2013-10-30 21:29:47 +0000110 r = SkRect::MakeXYWH(x, y + halfH, SkIntToScalar(W), halfH);
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000111 canvas->drawRect(r, p);
112 break;
113 }
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +0000114 case kRectangleWithMask_SrcType: {
115 canvas->save(SkCanvas::kClip_SaveFlag);
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 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000123 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 }
reed@android.comdd0ac282009-06-20 02:38:16 +0000148 }
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000149
scroggo@google.com7312a182013-01-28 21:14:21 +0000150 virtual void onOnceBeforeDraw() SK_OVERRIDE {
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000151 fBG.installPixels(SkImageInfo::Make(2, 2, kARGB_4444_SkColorType,
152 kOpaque_SkAlphaType),
153 gData, 4);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000154
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +0000155 make_bitmaps(W, H, &fSrcB, &fDstB, &fTransparent);
reed@google.com5213c042011-12-20 13:41:09 +0000156 }
157
reed@android.comdd0ac282009-06-20 02:38:16 +0000158public:
reed@google.comf3c1cc92010-12-23 16:45:33 +0000159 const static int W = 64;
160 const static int H = 64;
scroggo@google.com7312a182013-01-28 21:14:21 +0000161 XfermodesGM() {}
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000162
reed@android.comdd0ac282009-06-20 02:38:16 +0000163protected:
reed@android.com048522d2009-06-23 12:19:41 +0000164 virtual SkString onShortName() {
reed@android.com8015dd82009-06-21 00:49:18 +0000165 return SkString("xfermodes");
166 }
167
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000168 virtual SkISize onISize() {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +0000169 return make_isize(1990, 640);
reed@android.com048522d2009-06-23 12:19:41 +0000170 }
reed@android.comdd0ac282009-06-20 02:38:16 +0000171
reed@android.comdd0ac282009-06-20 02:38:16 +0000172 virtual void onDraw(SkCanvas* canvas) {
reed@google.comf3c1cc92010-12-23 16:45:33 +0000173 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000174
reed@android.comdd0ac282009-06-20 02:38:16 +0000175 const struct {
reed@google.comf3c1cc92010-12-23 16:45:33 +0000176 SkXfermode::Mode fMode;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000177 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.
reed@android.comdd0ac282009-06-20 02:38:16 +0000184 } gModes[] = {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000185 { 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 },
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000196
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000197 { 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 },
reed@android.comdd0ac282009-06-20 02:38:16 +0000215 };
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000216
reed@google.comf3c1cc92010-12-23 16:45:33 +0000217 const SkScalar w = SkIntToScalar(W);
218 const SkScalar h = SkIntToScalar(H);
reed@android.comdd0ac282009-06-20 02:38:16 +0000219 SkShader* s = SkShader::CreateBitmapShader(fBG,
220 SkShader::kRepeat_TileMode,
221 SkShader::kRepeat_TileMode);
222 SkMatrix m;
223 m.setScale(SkIntToScalar(6), SkIntToScalar(6));
224 s->setLocalMatrix(m);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000225
reed@android.comdd0ac282009-06-20 02:38:16 +0000226 SkPaint labelP;
227 labelP.setAntiAlias(true);
228 labelP.setTextAlign(SkPaint::kCenter_Align);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000229
reed@android.comc9c64222009-06-23 00:09:12 +0000230 const int W = 5;
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000231
reed@android.comdd0ac282009-06-20 02:38:16 +0000232 SkScalar x0 = 0;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000233 SkScalar y0 = 0;
234 for (int sourceType = 1; sourceType & kAll_SrcType; sourceType <<= 1) {
235 SkScalar x = x0, y = y0;
reed@android.comdd0ac282009-06-20 02:38:16 +0000236 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000237 if ((gModes[i].fSourceTypeMask & sourceType) == 0) {
238 continue;
239 }
reed@android.comc9c64222009-06-23 00:09:12 +0000240 SkXfermode* mode = SkXfermode::Create(gModes[i].fMode);
reed@google.comf3c1cc92010-12-23 16:45:33 +0000241 SkAutoUnref aur(mode);
reed@android.comdd0ac282009-06-20 02:38:16 +0000242 SkRect r;
243 r.set(x, y, x+w, y+h);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000244
reed@google.comf3c1cc92010-12-23 16:45:33 +0000245 SkPaint p;
reed@android.comdd0ac282009-06-20 02:38:16 +0000246 p.setStyle(SkPaint::kFill_Style);
247 p.setShader(s);
reed@android.comdd0ac282009-06-20 02:38:16 +0000248 canvas->drawRect(r, p);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000249
reed@google.comf3c1cc92010-12-23 16:45:33 +0000250 canvas->saveLayer(&r, NULL, SkCanvas::kARGB_ClipLayer_SaveFlag);
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000251 draw_mode(canvas, mode, static_cast<SrcType>(sourceType),
252 r.fLeft, r.fTop);
reed@google.comf3c1cc92010-12-23 16:45:33 +0000253 canvas->restore();
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000254
reed@google.comf3c1cc92010-12-23 16:45:33 +0000255 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
256 p.setStyle(SkPaint::kStroke_Style);
257 p.setShader(NULL);
258 canvas->drawRect(r, p);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000259
reed@google.comf3c1cc92010-12-23 16:45:33 +0000260#if 1
reed@android.comdd0ac282009-06-20 02:38:16 +0000261 canvas->drawText(gModes[i].fLabel, strlen(gModes[i].fLabel),
262 x + w/2, y - labelP.getTextSize()/2, labelP);
reed@google.comf3c1cc92010-12-23 16:45:33 +0000263#endif
reed@android.comdd0ac282009-06-20 02:38:16 +0000264 x += w + SkIntToScalar(10);
reed@android.comc9c64222009-06-23 00:09:12 +0000265 if ((i % W) == W - 1) {
reed@android.comdd0ac282009-06-20 02:38:16 +0000266 x = x0;
267 y += h + SkIntToScalar(30);
268 }
269 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000270 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 }
reed@android.comdd0ac282009-06-20 02:38:16 +0000279 }
280 s->unref();
281 }
282
283private:
284 typedef GM INHERITED;
285};
286
287//////////////////////////////////////////////////////////////////////////////
288
289static GM* MyFactory(void*) { return new XfermodesGM; }
290static GMRegistry reg(MyFactory);
291
292}