blob: 9bf46f6eb5789df339328a62aa36c97a64a09699 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
reeddd9ffea2016-02-18 12:39:14 -08007
reed@android.comdd0ac282009-06-20 02:38:16 +00008#include "gm.h"
Mike Klein33d20552017-03-22 13:47:51 -04009#include "sk_tool_utils.h"
reed@android.comdd0ac282009-06-20 02:38:16 +000010#include "SkBitmap.h"
11#include "SkShader.h"
reeddd9ffea2016-02-18 12:39:14 -080012#include "SkPM4f.h"
reed@android.comdd0ac282009-06-20 02:38:16 +000013
reede7125322016-02-09 11:59:24 -080014enum SrcType {
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,
halcanary9d524f22016-03-29 09:03:52 -070031
reede7125322016-02-09 11:59:24 -080032 kAll_SrcType = 0xFF, //!< All the source types.
33 kBasic_SrcType = 0x03, //!< Just basic source types.
34};
35
36const struct {
reed374772b2016-10-05 17:33:02 -070037 SkBlendMode fMode;
reed374772b2016-10-05 17:33:02 -070038 int fSourceTypeMask; // The source types to use this
reede7125322016-02-09 11:59:24 -080039 // mode with. See draw_mode for
40 // an explanation of each type.
41 // PDF has to play some tricks
42 // to support the base modes,
43 // test those more extensively.
44} gModes[] = {
Brian Osmand1e67e72017-03-15 12:19:37 -040045 { SkBlendMode::kClear, kAll_SrcType },
46 { SkBlendMode::kSrc, kAll_SrcType },
47 { SkBlendMode::kDst, kAll_SrcType },
48 { SkBlendMode::kSrcOver, kAll_SrcType },
49 { SkBlendMode::kDstOver, kAll_SrcType },
50 { SkBlendMode::kSrcIn, kAll_SrcType },
51 { SkBlendMode::kDstIn, kAll_SrcType },
52 { SkBlendMode::kSrcOut, kAll_SrcType },
53 { SkBlendMode::kDstOut, kAll_SrcType },
54 { SkBlendMode::kSrcATop, kAll_SrcType },
55 { SkBlendMode::kDstATop, kAll_SrcType },
halcanary9d524f22016-03-29 09:03:52 -070056
Brian Osmand1e67e72017-03-15 12:19:37 -040057 { SkBlendMode::kXor, kBasic_SrcType },
58 { SkBlendMode::kPlus, kBasic_SrcType },
59 { SkBlendMode::kModulate, kAll_SrcType },
60 { SkBlendMode::kScreen, kBasic_SrcType },
61 { SkBlendMode::kOverlay, kBasic_SrcType },
62 { SkBlendMode::kDarken, kBasic_SrcType },
63 { SkBlendMode::kLighten, kBasic_SrcType },
64 { SkBlendMode::kColorDodge, kBasic_SrcType },
65 { SkBlendMode::kColorBurn, kBasic_SrcType },
66 { SkBlendMode::kHardLight, kBasic_SrcType },
67 { SkBlendMode::kSoftLight, kBasic_SrcType },
68 { SkBlendMode::kDifference, kBasic_SrcType },
69 { SkBlendMode::kExclusion, kBasic_SrcType },
70 { SkBlendMode::kMultiply, kAll_SrcType },
71 { SkBlendMode::kHue, kBasic_SrcType },
72 { SkBlendMode::kSaturation, kBasic_SrcType },
73 { SkBlendMode::kColor, kBasic_SrcType },
74 { SkBlendMode::kLuminosity, kBasic_SrcType },
reede7125322016-02-09 11:59:24 -080075};
reed@android.comdd0ac282009-06-20 02:38:16 +000076
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +000077static void make_bitmaps(int w, int h, SkBitmap* src, SkBitmap* dst,
78 SkBitmap* transparent) {
reed@google.comeb9a46c2014-01-25 16:46:20 +000079 src->allocN32Pixels(w, h);
junov@google.comdbfac8a2012-12-06 21:47:40 +000080 src->eraseColor(SK_ColorTRANSPARENT);
reed@android.comdd0ac282009-06-20 02:38:16 +000081
reed@android.comdd0ac282009-06-20 02:38:16 +000082 SkPaint p;
mike@reedtribe.org3bd21732012-09-26 02:45:10 +000083 p.setAntiAlias(true);
84
reed@android.comdd0ac282009-06-20 02:38:16 +000085 SkRect r;
86 SkScalar ww = SkIntToScalar(w);
87 SkScalar hh = SkIntToScalar(h);
88
mike@reedtribe.org3bd21732012-09-26 02:45:10 +000089 {
90 SkCanvas c(*src);
caryclark4ec1ac62015-07-21 07:42:45 -070091 p.setColor(sk_tool_utils::color_to_565(0xFFFFCC44));
mike@reedtribe.org3bd21732012-09-26 02:45:10 +000092 r.set(0, 0, ww*3/4, hh*3/4);
93 c.drawOval(r, p);
94 }
tomhudson@google.com86bf5c92011-04-26 18:15:03 +000095
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +000096 dst->allocN32Pixels(w, h);
junov@google.comdbfac8a2012-12-06 21:47:40 +000097 dst->eraseColor(SK_ColorTRANSPARENT);
reed@android.comdd0ac282009-06-20 02:38:16 +000098
mike@reedtribe.org3bd21732012-09-26 02:45:10 +000099 {
100 SkCanvas c(*dst);
caryclark4ec1ac62015-07-21 07:42:45 -0700101 p.setColor(sk_tool_utils::color_to_565(0xFF66AAFF));
mike@reedtribe.org3bd21732012-09-26 02:45:10 +0000102 r.set(ww/3, hh/3, ww*19/20, hh*19/20);
103 c.drawRect(r, p);
104 }
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +0000105
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000106 transparent->allocN32Pixels(w, h);
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +0000107 transparent->eraseColor(SK_ColorTRANSPARENT);
reed@android.comdd0ac282009-06-20 02:38:16 +0000108}
109
scroggo@google.com7312a182013-01-28 21:14:21 +0000110static uint16_t gData[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
111
reede7125322016-02-09 11:59:24 -0800112class XfermodesGM : public skiagm::GM {
reed@android.comdd0ac282009-06-20 02:38:16 +0000113 SkBitmap fBG;
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +0000114 SkBitmap fSrcB, fDstB, fTransparent;
reed@android.comdd0ac282009-06-20 02:38:16 +0000115
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000116 /* The srcType argument indicates what to draw for the source part. Skia
117 * uses the implied shape of the drawing command and these modes
118 * demonstrate that.
119 */
reed374772b2016-10-05 17:33:02 -0700120 void draw_mode(SkCanvas* canvas, SkBlendMode mode, SrcType srcType, SkScalar x, SkScalar y) {
reed@android.comdd0ac282009-06-20 02:38:16 +0000121 SkPaint p;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000122 SkMatrix m;
123 bool restoreNeeded = false;
124 m.setTranslate(x, y);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000125
Florin Malitac54d8db2014-12-10 12:02:16 -0500126 canvas->drawBitmap(fSrcB, x, y, &p);
reed374772b2016-10-05 17:33:02 -0700127 p.setBlendMode(mode);
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000128 switch (srcType) {
Florin Malitac54d8db2014-12-10 12:02:16 -0500129 case kSmallTransparentImage_SrcType: {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +0000130 m.postScale(SK_ScalarHalf, SK_ScalarHalf, x, y);
Florin Malitac54d8db2014-12-10 12:02:16 -0500131
132 SkAutoCanvasRestore acr(canvas, true);
133 canvas->concat(m);
134 canvas->drawBitmap(fTransparent, 0, 0, &p);
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +0000135 break;
Florin Malitac54d8db2014-12-10 12:02:16 -0500136 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000137 case kQuarterClearInLayer_SrcType: {
vandebo@chromium.orgf5747da2013-10-30 21:29:47 +0000138 SkRect bounds = SkRect::MakeXYWH(x, y, SkIntToScalar(W),
139 SkIntToScalar(H));
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000140 canvas->saveLayer(&bounds, &p);
141 restoreNeeded = true;
reed374772b2016-10-05 17:33:02 -0700142 p.setBlendMode(SkBlendMode::kSrcOver);
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000143 // Fall through.
144 }
145 case kQuarterClear_SrcType: {
146 SkScalar halfW = SkIntToScalar(W) / 2;
147 SkScalar halfH = SkIntToScalar(H) / 2;
caryclark4ec1ac62015-07-21 07:42:45 -0700148 p.setColor(sk_tool_utils::color_to_565(0xFF66AAFF));
vandebo@chromium.orgf5747da2013-10-30 21:29:47 +0000149 SkRect r = SkRect::MakeXYWH(x + halfW, y, halfW,
150 SkIntToScalar(H));
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000151 canvas->drawRect(r, p);
caryclark4ec1ac62015-07-21 07:42:45 -0700152 p.setColor(sk_tool_utils::color_to_565(0xFFAA66FF));
vandebo@chromium.orgf5747da2013-10-30 21:29:47 +0000153 r = SkRect::MakeXYWH(x, y + halfH, SkIntToScalar(W), halfH);
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000154 canvas->drawRect(r, p);
155 break;
156 }
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +0000157 case kRectangleWithMask_SrcType: {
commit-bot@chromium.org091a5942014-04-18 14:19:31 +0000158 canvas->save();
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +0000159 restoreNeeded = true;
160 SkScalar w = SkIntToScalar(W);
161 SkScalar h = SkIntToScalar(H);
162 SkRect r = SkRect::MakeXYWH(x, y + h / 4, w, h * 23 / 60);
163 canvas->clipRect(r);
164 // Fall through.
165 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000166 case kRectangle_SrcType: {
167 SkScalar w = SkIntToScalar(W);
168 SkScalar h = SkIntToScalar(H);
169 SkRect r = SkRect::MakeXYWH(x + w / 3, y + h / 3,
170 w * 37 / 60, h * 37 / 60);
caryclark4ec1ac62015-07-21 07:42:45 -0700171 p.setColor(sk_tool_utils::color_to_565(0xFF66AAFF));
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000172 canvas->drawRect(r, p);
173 break;
174 }
175 case kSmallRectangleImageWithAlpha_SrcType:
176 m.postScale(SK_ScalarHalf, SK_ScalarHalf, x, y);
177 // Fall through.
178 case kRectangleImageWithAlpha_SrcType:
179 p.setAlpha(0x88);
180 // Fall through.
Florin Malitac54d8db2014-12-10 12:02:16 -0500181 case kRectangleImage_SrcType: {
182 SkAutoCanvasRestore acr(canvas, true);
183 canvas->concat(m);
184 canvas->drawBitmap(fDstB, 0, 0, &p);
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000185 break;
Florin Malitac54d8db2014-12-10 12:02:16 -0500186 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000187 default:
188 break;
189 }
190
191 if (restoreNeeded) {
192 canvas->restore();
193 }
reed@android.comdd0ac282009-06-20 02:38:16 +0000194 }
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000195
mtklein36352bf2015-03-25 18:17:31 -0700196 void onOnceBeforeDraw() override {
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000197 fBG.installPixels(SkImageInfo::Make(2, 2, kARGB_4444_SkColorType,
198 kOpaque_SkAlphaType),
199 gData, 4);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000200
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +0000201 make_bitmaps(W, H, &fSrcB, &fDstB, &fTransparent);
reed@google.com5213c042011-12-20 13:41:09 +0000202 }
203
reed@android.comdd0ac282009-06-20 02:38:16 +0000204public:
reed@google.comf3c1cc92010-12-23 16:45:33 +0000205 const static int W = 64;
206 const static int H = 64;
scroggo@google.com7312a182013-01-28 21:14:21 +0000207 XfermodesGM() {}
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000208
reed@android.comdd0ac282009-06-20 02:38:16 +0000209protected:
mtklein36352bf2015-03-25 18:17:31 -0700210 SkString onShortName() override {
reed@android.com8015dd82009-06-21 00:49:18 +0000211 return SkString("xfermodes");
212 }
213
mtklein36352bf2015-03-25 18:17:31 -0700214 SkISize onISize() override {
Mike Reed403d1542016-09-26 18:51:24 -0400215 return SkISize::Make(1990, 570);
reed@android.com048522d2009-06-23 12:19:41 +0000216 }
reed@android.comdd0ac282009-06-20 02:38:16 +0000217
mtklein36352bf2015-03-25 18:17:31 -0700218 void onDraw(SkCanvas* canvas) override {
reed@google.comf3c1cc92010-12-23 16:45:33 +0000219 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000220
reed@google.comf3c1cc92010-12-23 16:45:33 +0000221 const SkScalar w = SkIntToScalar(W);
222 const SkScalar h = SkIntToScalar(H);
reed@android.comdd0ac282009-06-20 02:38:16 +0000223 SkMatrix m;
224 m.setScale(SkIntToScalar(6), SkIntToScalar(6));
reed1a9b9642016-03-13 14:13:58 -0700225 auto s = SkShader::MakeBitmapShader(fBG, SkShader::kRepeat_TileMode,
226 SkShader::kRepeat_TileMode, &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);
caryclark1818acb2015-07-24 12:09:25 -0700230 sk_tool_utils::set_portable_typeface(&labelP);
reed@android.comdd0ac282009-06-20 02:38:16 +0000231 labelP.setTextAlign(SkPaint::kCenter_Align);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000232
reed@android.comc9c64222009-06-23 00:09:12 +0000233 const int W = 5;
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000234
reed@android.comdd0ac282009-06-20 02:38:16 +0000235 SkScalar x0 = 0;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000236 SkScalar y0 = 0;
237 for (int sourceType = 1; sourceType & kAll_SrcType; sourceType <<= 1) {
238 SkScalar x = x0, y = y0;
reed@android.comdd0ac282009-06-20 02:38:16 +0000239 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000240 if ((gModes[i].fSourceTypeMask & sourceType) == 0) {
241 continue;
242 }
Mike Reed403d1542016-09-26 18:51:24 -0400243 SkRect r{ 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
halcanary96fcdcc2015-08-27 07:41:13 -0700250 canvas->saveLayer(&r, nullptr);
Mike Reed403d1542016-09-26 18:51:24 -0400251 draw_mode(canvas, gModes[i].fMode, static_cast<SrcType>(sourceType),
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000252 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);
halcanary96fcdcc2015-08-27 07:41:13 -0700257 p.setShader(nullptr);
reed@google.comf3c1cc92010-12-23 16:45:33 +0000258 canvas->drawRect(r, p);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000259
reed@google.comf3c1cc92010-12-23 16:45:33 +0000260#if 1
Brian Osmand1e67e72017-03-15 12:19:37 -0400261 const char* label = SkBlendMode_Name(gModes[i].fMode);
Cary Clark2a475ea2017-04-28 15:35:12 -0400262 canvas->drawString(label,
reed@android.comdd0ac282009-06-20 02:38:16 +0000263 x + w/2, y - labelP.getTextSize()/2, labelP);
reed@google.comf3c1cc92010-12-23 16:45:33 +0000264#endif
reed@android.comdd0ac282009-06-20 02:38:16 +0000265 x += w + SkIntToScalar(10);
reed@android.comc9c64222009-06-23 00:09:12 +0000266 if ((i % W) == W - 1) {
reed@android.comdd0ac282009-06-20 02:38:16 +0000267 x = x0;
268 y += h + SkIntToScalar(30);
269 }
270 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000271 if (y < 320) {
272 if (x > x0) {
273 y += h + SkIntToScalar(30);
274 }
275 y0 = y;
276 } else {
277 x0 += SkIntToScalar(400);
278 y0 = 0;
279 }
reed@android.comdd0ac282009-06-20 02:38:16 +0000280 }
reed@android.comdd0ac282009-06-20 02:38:16 +0000281 }
282
283private:
284 typedef GM INHERITED;
285};
reede7125322016-02-09 11:59:24 -0800286DEF_GM( return new XfermodesGM; )