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         SkMatrix m;
137         bool restoreNeeded = false;
138         m.setTranslate(x, y);
139 
140         canvas->drawBitmap(fSrcB, x, y, &p);
141         p.setBlendMode(mode);
142         switch (srcType) {
143             case kSmallTransparentImage_SrcType: {
144                 m.postScale(SK_ScalarHalf, SK_ScalarHalf, x, y);
145 
146                 SkAutoCanvasRestore acr(canvas, true);
147                 canvas->concat(m);
148                 canvas->drawBitmap(fTransparent, 0, 0, &p);
149                 break;
150             }
151             case kQuarterClearInLayer_SrcType: {
152                 SkRect bounds = SkRect::MakeXYWH(x, y, SkIntToScalar(W),
153                                                  SkIntToScalar(H));
154                 canvas->saveLayer(&bounds, &p);
155                 restoreNeeded = true;
156                 p.setBlendMode(SkBlendMode::kSrcOver);
157                 [[fallthrough]];
158             }
159             case kQuarterClear_SrcType: {
160                 SkScalar halfW = SkIntToScalar(W) / 2;
161                 SkScalar halfH = SkIntToScalar(H) / 2;
162                 p.setColor(ToolUtils::color_to_565(0xFF66AAFF));
163                 SkRect r = SkRect::MakeXYWH(x + halfW, y, halfW,
164                                             SkIntToScalar(H));
165                 canvas->drawRect(r, p);
166                 p.setColor(ToolUtils::color_to_565(0xFFAA66FF));
167                 r = SkRect::MakeXYWH(x, y + halfH, SkIntToScalar(W), halfH);
168                 canvas->drawRect(r, p);
169                 break;
170             }
171             case kRectangleWithMask_SrcType: {
172                 canvas->save();
173                 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                 [[fallthrough]];
179             }
180             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);
185                 p.setColor(ToolUtils::color_to_565(0xFF66AAFF));
186                 canvas->drawRect(r, p);
187                 break;
188             }
189             case kSmallRectangleImageWithAlpha_SrcType:
190                 m.postScale(SK_ScalarHalf, SK_ScalarHalf, x, y);
191                 [[fallthrough]];
192             case kRectangleImageWithAlpha_SrcType:
193                 p.setAlpha(0x88);
194                 [[fallthrough]];
195             case kRectangleImage_SrcType: {
196                 SkAutoCanvasRestore acr(canvas, true);
197                 canvas->concat(m);
198                 canvas->drawBitmap(fDstB, 0, 0, &p);
199                 break;
200             }
201             default:
202                 break;
203         }
204 
205         if (restoreNeeded) {
206             canvas->restore();
207         }
208     }
209 
onOnceBeforeDraw()210     void onOnceBeforeDraw() override {
211         fBG.installPixels(SkImageInfo::Make(2, 2, kARGB_4444_SkColorType,
212                                             kOpaque_SkAlphaType),
213                           gData, 4);
214 
215         make_bitmaps(W, H, &fSrcB, &fDstB, &fTransparent);
216     }
217 
218 public:
219     const static int W = 64;
220     const static int H = 64;
XfermodesGM()221     XfermodesGM() {}
222 
223 protected:
onShortName()224     SkString onShortName() override {
225         return SkString("xfermodes");
226     }
227 
onISize()228     SkISize onISize() override {
229         return SkISize::Make(1990, 570);
230     }
231 
onDraw(SkCanvas * canvas)232     void onDraw(SkCanvas* canvas) override {
233         canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
234 
235         const SkScalar w = SkIntToScalar(W);
236         const SkScalar h = SkIntToScalar(H);
237         SkMatrix m;
238         m.setScale(SkIntToScalar(6), SkIntToScalar(6));
239         auto s = fBG.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat, &m);
240 
241         SkPaint labelP;
242         labelP.setAntiAlias(true);
243 
244         SkFont font(ToolUtils::create_portable_typeface());
245 
246         const int W = 5;
247 
248         SkScalar x0 = 0;
249         SkScalar y0 = 0;
250         for (int sourceType = 1; sourceType & kAll_SrcType; sourceType <<= 1) {
251             SkScalar x = x0, y = y0;
252             for (size_t i = 0; i < SK_ARRAY_COUNT(gModes); i++) {
253                 if ((gModes[i].fSourceTypeMask & sourceType) == 0) {
254                     continue;
255                 }
256                 SkRect r{ x, y, x+w, y+h };
257 
258                 SkPaint p;
259                 p.setStyle(SkPaint::kFill_Style);
260                 p.setShader(s);
261                 canvas->drawRect(r, p);
262 
263                 canvas->saveLayer(&r, nullptr);
264                 draw_mode(canvas, gModes[i].fMode, static_cast<SrcType>(sourceType),
265                           r.fLeft, r.fTop);
266                 canvas->restore();
267 
268                 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
269                 p.setStyle(SkPaint::kStroke_Style);
270                 p.setShader(nullptr);
271                 canvas->drawRect(r, p);
272 
273 #if 1
274                 const char* label = SkBlendMode_Name(gModes[i].fMode);
275                 SkTextUtils::DrawString(canvas, label, x + w/2, y - font.getSize()/2,
276                                         font, labelP, SkTextUtils::kCenter_Align);
277 #endif
278                 x += w + SkIntToScalar(10);
279                 if ((i % W) == W - 1) {
280                     x = x0;
281                     y += h + SkIntToScalar(30);
282                 }
283             }
284             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             }
293         }
294     }
295 
296 private:
297     using INHERITED = GM;
298 };
299 DEF_GM( return new XfermodesGM; )
300