blob: 6ddabe75c1d876a3c2191c90e5c13532c95e7631 [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "gm/gm.h"
9#include "include/core/SkBitmap.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040010#include "include/core/SkBlendMode.h"
11#include "include/core/SkCanvas.h"
12#include "include/core/SkColor.h"
13#include "include/core/SkFont.h"
14#include "include/core/SkImageInfo.h"
15#include "include/core/SkMatrix.h"
16#include "include/core/SkPaint.h"
17#include "include/core/SkRect.h"
18#include "include/core/SkScalar.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "include/core/SkShader.h"
Ben Wagner7fde8e12019-05-01 17:28:53 -040020#include "include/core/SkSize.h"
21#include "include/core/SkString.h"
22#include "include/core/SkTileMode.h"
23#include "include/core/SkTypeface.h"
24#include "include/core/SkTypes.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "include/utils/SkTextUtils.h"
26#include "tools/ToolUtils.h"
reed@android.comdd0ac282009-06-20 02:38:16 +000027
reede7125322016-02-09 11:59:24 -080028enum SrcType {
29 //! A WxH image with a rectangle in the lower right.
30 kRectangleImage_SrcType = 0x01,
31 //! kRectangleImage_SrcType with an alpha of 34.5%.
32 kRectangleImageWithAlpha_SrcType = 0x02,
33 //! kRectnagleImageWithAlpha_SrcType scaled down by half.
34 kSmallRectangleImageWithAlpha_SrcType = 0x04,
35 //! kRectangleImage_SrcType drawn directly instead in an image.
36 kRectangle_SrcType = 0x08,
37 //! Two rectangles, first on the right half, second on the bottom half.
38 kQuarterClear_SrcType = 0x10,
39 //! kQuarterClear_SrcType in a layer.
40 kQuarterClearInLayer_SrcType = 0x20,
41 //! A W/2xH/2 transparent image.
42 kSmallTransparentImage_SrcType = 0x40,
43 //! kRectangleImage_SrcType drawn directly with a mask.
44 kRectangleWithMask_SrcType = 0x80,
halcanary9d524f22016-03-29 09:03:52 -070045
reede7125322016-02-09 11:59:24 -080046 kAll_SrcType = 0xFF, //!< All the source types.
47 kBasic_SrcType = 0x03, //!< Just basic source types.
48};
49
50const struct {
reed374772b2016-10-05 17:33:02 -070051 SkBlendMode fMode;
reed374772b2016-10-05 17:33:02 -070052 int fSourceTypeMask; // The source types to use this
reede7125322016-02-09 11:59:24 -080053 // mode with. See draw_mode for
54 // an explanation of each type.
55 // PDF has to play some tricks
56 // to support the base modes,
57 // test those more extensively.
58} gModes[] = {
Brian Osmand1e67e72017-03-15 12:19:37 -040059 { SkBlendMode::kClear, kAll_SrcType },
60 { SkBlendMode::kSrc, kAll_SrcType },
61 { SkBlendMode::kDst, kAll_SrcType },
62 { SkBlendMode::kSrcOver, kAll_SrcType },
63 { SkBlendMode::kDstOver, kAll_SrcType },
64 { SkBlendMode::kSrcIn, kAll_SrcType },
65 { SkBlendMode::kDstIn, kAll_SrcType },
66 { SkBlendMode::kSrcOut, kAll_SrcType },
67 { SkBlendMode::kDstOut, kAll_SrcType },
68 { SkBlendMode::kSrcATop, kAll_SrcType },
69 { SkBlendMode::kDstATop, kAll_SrcType },
halcanary9d524f22016-03-29 09:03:52 -070070
Brian Osmand1e67e72017-03-15 12:19:37 -040071 { SkBlendMode::kXor, kBasic_SrcType },
72 { SkBlendMode::kPlus, kBasic_SrcType },
73 { SkBlendMode::kModulate, kAll_SrcType },
74 { SkBlendMode::kScreen, kBasic_SrcType },
75 { SkBlendMode::kOverlay, kBasic_SrcType },
76 { SkBlendMode::kDarken, kBasic_SrcType },
77 { SkBlendMode::kLighten, kBasic_SrcType },
78 { SkBlendMode::kColorDodge, kBasic_SrcType },
79 { SkBlendMode::kColorBurn, kBasic_SrcType },
80 { SkBlendMode::kHardLight, kBasic_SrcType },
81 { SkBlendMode::kSoftLight, kBasic_SrcType },
82 { SkBlendMode::kDifference, kBasic_SrcType },
83 { SkBlendMode::kExclusion, kBasic_SrcType },
84 { SkBlendMode::kMultiply, kAll_SrcType },
85 { SkBlendMode::kHue, kBasic_SrcType },
86 { SkBlendMode::kSaturation, kBasic_SrcType },
87 { SkBlendMode::kColor, kBasic_SrcType },
88 { SkBlendMode::kLuminosity, kBasic_SrcType },
reede7125322016-02-09 11:59:24 -080089};
reed@android.comdd0ac282009-06-20 02:38:16 +000090
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +000091static void make_bitmaps(int w, int h, SkBitmap* src, SkBitmap* dst,
92 SkBitmap* transparent) {
reed@google.comeb9a46c2014-01-25 16:46:20 +000093 src->allocN32Pixels(w, h);
junov@google.comdbfac8a2012-12-06 21:47:40 +000094 src->eraseColor(SK_ColorTRANSPARENT);
reed@android.comdd0ac282009-06-20 02:38:16 +000095
reed@android.comdd0ac282009-06-20 02:38:16 +000096 SkPaint p;
mike@reedtribe.org3bd21732012-09-26 02:45:10 +000097 p.setAntiAlias(true);
98
reed@android.comdd0ac282009-06-20 02:38:16 +000099 SkRect r;
100 SkScalar ww = SkIntToScalar(w);
101 SkScalar hh = SkIntToScalar(h);
102
mike@reedtribe.org3bd21732012-09-26 02:45:10 +0000103 {
104 SkCanvas c(*src);
Mike Kleinea3f0142019-03-20 11:12:10 -0500105 p.setColor(ToolUtils::color_to_565(0xFFFFCC44));
Mike Reed92b33352019-08-24 19:39:13 -0400106 r.setWH(ww*3/4, hh*3/4);
mike@reedtribe.org3bd21732012-09-26 02:45:10 +0000107 c.drawOval(r, p);
108 }
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000109
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000110 dst->allocN32Pixels(w, h);
junov@google.comdbfac8a2012-12-06 21:47:40 +0000111 dst->eraseColor(SK_ColorTRANSPARENT);
reed@android.comdd0ac282009-06-20 02:38:16 +0000112
mike@reedtribe.org3bd21732012-09-26 02:45:10 +0000113 {
114 SkCanvas c(*dst);
Mike Kleinea3f0142019-03-20 11:12:10 -0500115 p.setColor(ToolUtils::color_to_565(0xFF66AAFF));
Mike Reed92b33352019-08-24 19:39:13 -0400116 r.setLTRB(ww/3, hh/3, ww*19/20, hh*19/20);
mike@reedtribe.org3bd21732012-09-26 02:45:10 +0000117 c.drawRect(r, p);
118 }
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +0000119
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000120 transparent->allocN32Pixels(w, h);
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +0000121 transparent->eraseColor(SK_ColorTRANSPARENT);
reed@android.comdd0ac282009-06-20 02:38:16 +0000122}
123
scroggo@google.com7312a182013-01-28 21:14:21 +0000124static uint16_t gData[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
125
reede7125322016-02-09 11:59:24 -0800126class XfermodesGM : public skiagm::GM {
reed@android.comdd0ac282009-06-20 02:38:16 +0000127 SkBitmap fBG;
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +0000128 SkBitmap fSrcB, fDstB, fTransparent;
reed@android.comdd0ac282009-06-20 02:38:16 +0000129
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000130 /* The srcType argument indicates what to draw for the source part. Skia
131 * uses the implied shape of the drawing command and these modes
132 * demonstrate that.
133 */
reed374772b2016-10-05 17:33:02 -0700134 void draw_mode(SkCanvas* canvas, SkBlendMode mode, SrcType srcType, SkScalar x, SkScalar y) {
reed@android.comdd0ac282009-06-20 02:38:16 +0000135 SkPaint p;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000136 SkMatrix m;
137 bool restoreNeeded = false;
138 m.setTranslate(x, y);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000139
Florin Malitac54d8db2014-12-10 12:02:16 -0500140 canvas->drawBitmap(fSrcB, x, y, &p);
reed374772b2016-10-05 17:33:02 -0700141 p.setBlendMode(mode);
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000142 switch (srcType) {
Florin Malitac54d8db2014-12-10 12:02:16 -0500143 case kSmallTransparentImage_SrcType: {
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +0000144 m.postScale(SK_ScalarHalf, SK_ScalarHalf, x, y);
Florin Malitac54d8db2014-12-10 12:02:16 -0500145
146 SkAutoCanvasRestore acr(canvas, true);
147 canvas->concat(m);
148 canvas->drawBitmap(fTransparent, 0, 0, &p);
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +0000149 break;
Florin Malitac54d8db2014-12-10 12:02:16 -0500150 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000151 case kQuarterClearInLayer_SrcType: {
vandebo@chromium.orgf5747da2013-10-30 21:29:47 +0000152 SkRect bounds = SkRect::MakeXYWH(x, y, SkIntToScalar(W),
153 SkIntToScalar(H));
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000154 canvas->saveLayer(&bounds, &p);
155 restoreNeeded = true;
reed374772b2016-10-05 17:33:02 -0700156 p.setBlendMode(SkBlendMode::kSrcOver);
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000157 // Fall through.
158 }
159 case kQuarterClear_SrcType: {
160 SkScalar halfW = SkIntToScalar(W) / 2;
161 SkScalar halfH = SkIntToScalar(H) / 2;
Mike Kleinea3f0142019-03-20 11:12:10 -0500162 p.setColor(ToolUtils::color_to_565(0xFF66AAFF));
vandebo@chromium.orgf5747da2013-10-30 21:29:47 +0000163 SkRect r = SkRect::MakeXYWH(x + halfW, y, halfW,
164 SkIntToScalar(H));
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000165 canvas->drawRect(r, p);
Mike Kleinea3f0142019-03-20 11:12:10 -0500166 p.setColor(ToolUtils::color_to_565(0xFFAA66FF));
vandebo@chromium.orgf5747da2013-10-30 21:29:47 +0000167 r = SkRect::MakeXYWH(x, y + halfH, SkIntToScalar(W), halfH);
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000168 canvas->drawRect(r, p);
169 break;
170 }
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +0000171 case kRectangleWithMask_SrcType: {
commit-bot@chromium.org091a5942014-04-18 14:19:31 +0000172 canvas->save();
commit-bot@chromium.org4e8f1e52013-12-17 23:38:28 +0000173 restoreNeeded = true;
174 SkScalar w = SkIntToScalar(W);
175 SkScalar h = SkIntToScalar(H);
176 SkRect r = SkRect::MakeXYWH(x, y + h / 4, w, h * 23 / 60);
177 canvas->clipRect(r);
178 // Fall through.
179 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000180 case kRectangle_SrcType: {
181 SkScalar w = SkIntToScalar(W);
182 SkScalar h = SkIntToScalar(H);
183 SkRect r = SkRect::MakeXYWH(x + w / 3, y + h / 3,
184 w * 37 / 60, h * 37 / 60);
Mike Kleinea3f0142019-03-20 11:12:10 -0500185 p.setColor(ToolUtils::color_to_565(0xFF66AAFF));
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000186 canvas->drawRect(r, p);
187 break;
188 }
189 case kSmallRectangleImageWithAlpha_SrcType:
190 m.postScale(SK_ScalarHalf, SK_ScalarHalf, x, y);
191 // Fall through.
192 case kRectangleImageWithAlpha_SrcType:
193 p.setAlpha(0x88);
194 // Fall through.
Florin Malitac54d8db2014-12-10 12:02:16 -0500195 case kRectangleImage_SrcType: {
196 SkAutoCanvasRestore acr(canvas, true);
197 canvas->concat(m);
198 canvas->drawBitmap(fDstB, 0, 0, &p);
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000199 break;
Florin Malitac54d8db2014-12-10 12:02:16 -0500200 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000201 default:
202 break;
203 }
204
205 if (restoreNeeded) {
206 canvas->restore();
207 }
reed@android.comdd0ac282009-06-20 02:38:16 +0000208 }
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000209
mtklein36352bf2015-03-25 18:17:31 -0700210 void onOnceBeforeDraw() override {
commit-bot@chromium.orgdac52252014-02-17 21:21:46 +0000211 fBG.installPixels(SkImageInfo::Make(2, 2, kARGB_4444_SkColorType,
212 kOpaque_SkAlphaType),
213 gData, 4);
rmistry@google.comae933ce2012-08-23 18:19:56 +0000214
commit-bot@chromium.org7542dc82013-12-03 21:08:46 +0000215 make_bitmaps(W, H, &fSrcB, &fDstB, &fTransparent);
reed@google.com5213c042011-12-20 13:41:09 +0000216 }
217
reed@android.comdd0ac282009-06-20 02:38:16 +0000218public:
reed@google.comf3c1cc92010-12-23 16:45:33 +0000219 const static int W = 64;
220 const static int H = 64;
scroggo@google.com7312a182013-01-28 21:14:21 +0000221 XfermodesGM() {}
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000222
reed@android.comdd0ac282009-06-20 02:38:16 +0000223protected:
mtklein36352bf2015-03-25 18:17:31 -0700224 SkString onShortName() override {
reed@android.com8015dd82009-06-21 00:49:18 +0000225 return SkString("xfermodes");
226 }
227
mtklein36352bf2015-03-25 18:17:31 -0700228 SkISize onISize() override {
Mike Reed403d1542016-09-26 18:51:24 -0400229 return SkISize::Make(1990, 570);
reed@android.com048522d2009-06-23 12:19:41 +0000230 }
reed@android.comdd0ac282009-06-20 02:38:16 +0000231
mtklein36352bf2015-03-25 18:17:31 -0700232 void onDraw(SkCanvas* canvas) override {
reed@google.comf3c1cc92010-12-23 16:45:33 +0000233 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000234
reed@google.comf3c1cc92010-12-23 16:45:33 +0000235 const SkScalar w = SkIntToScalar(W);
236 const SkScalar h = SkIntToScalar(H);
reed@android.comdd0ac282009-06-20 02:38:16 +0000237 SkMatrix m;
238 m.setScale(SkIntToScalar(6), SkIntToScalar(6));
Mike Reed50acf8f2019-04-08 13:20:23 -0400239 auto s = fBG.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &m);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000240
reed@android.comdd0ac282009-06-20 02:38:16 +0000241 SkPaint labelP;
242 labelP.setAntiAlias(true);
Mike Reedb579f072019-01-03 15:45:53 -0500243
Mike Kleinea3f0142019-03-20 11:12:10 -0500244 SkFont font(ToolUtils::create_portable_typeface());
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000245
reed@android.comc9c64222009-06-23 00:09:12 +0000246 const int W = 5;
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000247
reed@android.comdd0ac282009-06-20 02:38:16 +0000248 SkScalar x0 = 0;
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000249 SkScalar y0 = 0;
250 for (int sourceType = 1; sourceType & kAll_SrcType; sourceType <<= 1) {
251 SkScalar x = x0, y = y0;
reed@android.comdd0ac282009-06-20 02:38:16 +0000252 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000253 if ((gModes[i].fSourceTypeMask & sourceType) == 0) {
254 continue;
255 }
Mike Reed403d1542016-09-26 18:51:24 -0400256 SkRect r{ x, y, x+w, y+h };
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000257
reed@google.comf3c1cc92010-12-23 16:45:33 +0000258 SkPaint p;
reed@android.comdd0ac282009-06-20 02:38:16 +0000259 p.setStyle(SkPaint::kFill_Style);
260 p.setShader(s);
reed@android.comdd0ac282009-06-20 02:38:16 +0000261 canvas->drawRect(r, p);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000262
halcanary96fcdcc2015-08-27 07:41:13 -0700263 canvas->saveLayer(&r, nullptr);
Mike Reed403d1542016-09-26 18:51:24 -0400264 draw_mode(canvas, gModes[i].fMode, static_cast<SrcType>(sourceType),
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000265 r.fLeft, r.fTop);
reed@google.comf3c1cc92010-12-23 16:45:33 +0000266 canvas->restore();
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000267
reed@google.comf3c1cc92010-12-23 16:45:33 +0000268 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
269 p.setStyle(SkPaint::kStroke_Style);
halcanary96fcdcc2015-08-27 07:41:13 -0700270 p.setShader(nullptr);
reed@google.comf3c1cc92010-12-23 16:45:33 +0000271 canvas->drawRect(r, p);
tomhudson@google.com86bf5c92011-04-26 18:15:03 +0000272
reed@google.comf3c1cc92010-12-23 16:45:33 +0000273#if 1
Brian Osmand1e67e72017-03-15 12:19:37 -0400274 const char* label = SkBlendMode_Name(gModes[i].fMode);
Mike Reedb579f072019-01-03 15:45:53 -0500275 SkTextUtils::DrawString(canvas, label, x + w/2, y - font.getSize()/2,
276 font, labelP, SkTextUtils::kCenter_Align);
reed@google.comf3c1cc92010-12-23 16:45:33 +0000277#endif
reed@android.comdd0ac282009-06-20 02:38:16 +0000278 x += w + SkIntToScalar(10);
reed@android.comc9c64222009-06-23 00:09:12 +0000279 if ((i % W) == W - 1) {
reed@android.comdd0ac282009-06-20 02:38:16 +0000280 x = x0;
281 y += h + SkIntToScalar(30);
282 }
283 }
vandebo@chromium.org3b416212013-10-30 20:48:05 +0000284 if (y < 320) {
285 if (x > x0) {
286 y += h + SkIntToScalar(30);
287 }
288 y0 = y;
289 } else {
290 x0 += SkIntToScalar(400);
291 y0 = 0;
292 }
reed@android.comdd0ac282009-06-20 02:38:16 +0000293 }
reed@android.comdd0ac282009-06-20 02:38:16 +0000294 }
295
296private:
297 typedef GM INHERITED;
298};
reede7125322016-02-09 11:59:24 -0800299DEF_GM( return new XfermodesGM; )