1 /*
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 */
7
8 #include "gm/gm.h"
9 #include "include/core/SkBitmap.h"
10 #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"
19 #include "include/core/SkShader.h"
20 #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"
25 #include "include/utils/SkTextUtils.h"
26 #include "tools/ToolUtils.h"
27
28 enum 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,
45
46 kAll_SrcType = 0xFF, //!< All the source types.
47 kBasic_SrcType = 0x03, //!< Just basic source types.
48 };
49
50 const struct {
51 SkBlendMode fMode;
52 int fSourceTypeMask; // The source types to use this
53 // 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[] = {
59 { 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 },
70
71 { 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 },
89 };
90
make_bitmaps(int w,int h,SkBitmap * src,SkBitmap * dst,SkBitmap * transparent)91 static void make_bitmaps(int w, int h, SkBitmap* src, SkBitmap* dst,
92 SkBitmap* transparent) {
93 src->allocN32Pixels(w, h);
94 src->eraseColor(SK_ColorTRANSPARENT);
95
96 SkPaint p;
97 p.setAntiAlias(true);
98
99 SkRect r;
100 SkScalar ww = SkIntToScalar(w);
101 SkScalar hh = SkIntToScalar(h);
102
103 {
104 SkCanvas c(*src);
105 p.setColor(ToolUtils::color_to_565(0xFFFFCC44));
106 r.setWH(ww*3/4, hh*3/4);
107 c.drawOval(r, p);
108 }
109
110 dst->allocN32Pixels(w, h);
111 dst->eraseColor(SK_ColorTRANSPARENT);
112
113 {
114 SkCanvas c(*dst);
115 p.setColor(ToolUtils::color_to_565(0xFF66AAFF));
116 r.setLTRB(ww/3, hh/3, ww*19/20, hh*19/20);
117 c.drawRect(r, p);
118 }
119
120 transparent->allocN32Pixels(w, h);
121 transparent->eraseColor(SK_ColorTRANSPARENT);
122 }
123
124 static uint16_t gData[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
125
126 class XfermodesGM : public skiagm::GM {
127 SkBitmap fBG;
128 SkBitmap fSrcB, fDstB, fTransparent;
129
130 /* 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 */
draw_mode(SkCanvas * canvas,SkBlendMode mode,SrcType srcType,SkScalar x,SkScalar y)134 void draw_mode(SkCanvas* canvas, SkBlendMode mode, SrcType srcType, SkScalar x, SkScalar y) {
135 SkPaint p;
136 SkSamplingOptions sampling;
137 SkMatrix m;
138 bool restoreNeeded = false;
139 m.setTranslate(x, y);
140
141 canvas->drawImage(fSrcB.asImage(), x, y, sampling, &p);
142 p.setBlendMode(mode);
143 switch (srcType) {
144 case kSmallTransparentImage_SrcType: {
145 m.postScale(SK_ScalarHalf, SK_ScalarHalf, x, y);
146
147 SkAutoCanvasRestore acr(canvas, true);
148 canvas->concat(m);
149 canvas->drawImage(fTransparent.asImage(), 0, 0, sampling, &p);
150 break;
151 }
152 case kQuarterClearInLayer_SrcType: {
153 SkRect bounds = SkRect::MakeXYWH(x, y, SkIntToScalar(W),
154 SkIntToScalar(H));
155 canvas->saveLayer(&bounds, &p);
156 restoreNeeded = true;
157 p.setBlendMode(SkBlendMode::kSrcOver);
158 [[fallthrough]];
159 }
160 case kQuarterClear_SrcType: {
161 SkScalar halfW = SkIntToScalar(W) / 2;
162 SkScalar halfH = SkIntToScalar(H) / 2;
163 p.setColor(ToolUtils::color_to_565(0xFF66AAFF));
164 SkRect r = SkRect::MakeXYWH(x + halfW, y, halfW,
165 SkIntToScalar(H));
166 canvas->drawRect(r, p);
167 p.setColor(ToolUtils::color_to_565(0xFFAA66FF));
168 r = SkRect::MakeXYWH(x, y + halfH, SkIntToScalar(W), halfH);
169 canvas->drawRect(r, p);
170 break;
171 }
172 case kRectangleWithMask_SrcType: {
173 canvas->save();
174 restoreNeeded = true;
175 SkScalar w = SkIntToScalar(W);
176 SkScalar h = SkIntToScalar(H);
177 SkRect r = SkRect::MakeXYWH(x, y + h / 4, w, h * 23 / 60);
178 canvas->clipRect(r);
179 [[fallthrough]];
180 }
181 case kRectangle_SrcType: {
182 SkScalar w = SkIntToScalar(W);
183 SkScalar h = SkIntToScalar(H);
184 SkRect r = SkRect::MakeXYWH(x + w / 3, y + h / 3,
185 w * 37 / 60, h * 37 / 60);
186 p.setColor(ToolUtils::color_to_565(0xFF66AAFF));
187 canvas->drawRect(r, p);
188 break;
189 }
190 case kSmallRectangleImageWithAlpha_SrcType:
191 m.postScale(SK_ScalarHalf, SK_ScalarHalf, x, y);
192 [[fallthrough]];
193 case kRectangleImageWithAlpha_SrcType:
194 p.setAlpha(0x88);
195 [[fallthrough]];
196 case kRectangleImage_SrcType: {
197 SkAutoCanvasRestore acr(canvas, true);
198 canvas->concat(m);
199 canvas->drawImage(fDstB.asImage(), 0, 0, sampling, &p);
200 break;
201 }
202 default:
203 break;
204 }
205
206 if (restoreNeeded) {
207 canvas->restore();
208 }
209 }
210
onOnceBeforeDraw()211 void onOnceBeforeDraw() override {
212 fBG.installPixels(SkImageInfo::Make(2, 2, kARGB_4444_SkColorType,
213 kOpaque_SkAlphaType),
214 gData, 4);
215
216 make_bitmaps(W, H, &fSrcB, &fDstB, &fTransparent);
217 }
218
219 public:
220 const static int W = 64;
221 const static int H = 64;
XfermodesGM()222 XfermodesGM() {}
223
224 protected:
onShortName()225 SkString onShortName() override {
226 return SkString("xfermodes");
227 }
228
onISize()229 SkISize onISize() override {
230 return SkISize::Make(1990, 570);
231 }
232
onDraw(SkCanvas * canvas)233 void onDraw(SkCanvas* canvas) override {
234 canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
235
236 const SkScalar w = SkIntToScalar(W);
237 const SkScalar h = SkIntToScalar(H);
238 SkMatrix m;
239 m.setScale(SkIntToScalar(6), SkIntToScalar(6));
240 auto s = fBG.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
241 SkSamplingOptions(), m);
242
243 SkPaint labelP;
244 labelP.setAntiAlias(true);
245
246 SkFont font(ToolUtils::create_portable_typeface());
247
248 const int kWrap = 5;
249
250 SkScalar x0 = 0;
251 SkScalar y0 = 0;
252 for (int sourceType = 1; sourceType & kAll_SrcType; sourceType <<= 1) {
253 SkScalar x = x0, y = y0;
254 for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
255 if ((gModes[i].fSourceTypeMask & sourceType) == 0) {
256 continue;
257 }
258 SkRect r{ x, y, x+w, y+h };
259
260 SkPaint p;
261 p.setStyle(SkPaint::kFill_Style);
262 p.setShader(s);
263 canvas->drawRect(r, p);
264
265 canvas->saveLayer(&r, nullptr);
266 draw_mode(canvas, gModes[i].fMode, static_cast<SrcType>(sourceType),
267 r.fLeft, r.fTop);
268 canvas->restore();
269
270 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
271 p.setStyle(SkPaint::kStroke_Style);
272 p.setShader(nullptr);
273 canvas->drawRect(r, p);
274
275 #if 1
276 const char* label = SkBlendMode_Name(gModes[i].fMode);
277 SkTextUtils::DrawString(canvas, label, x + w/2, y - font.getSize()/2,
278 font, labelP, SkTextUtils::kCenter_Align);
279 #endif
280 x += w + SkIntToScalar(10);
281 if ((i % kWrap) == kWrap - 1) {
282 x = x0;
283 y += h + SkIntToScalar(30);
284 }
285 }
286 if (y < 320) {
287 if (x > x0) {
288 y += h + SkIntToScalar(30);
289 }
290 y0 = y;
291 } else {
292 x0 += SkIntToScalar(400);
293 y0 = 0;
294 }
295 }
296 }
297
298 private:
299 using INHERITED = GM;
300 };
301 DEF_GM( return new XfermodesGM; )
302