xref: /aosp_15_r20/external/skia/gm/xfermodes.cpp (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 #include "tools/fonts/FontToolUtils.h"
28 
29 enum SrcType {
30     //! A WxH image with a rectangle in the lower right.
31     kRectangleImage_SrcType               = 0x01,
32     //! kRectangleImage_SrcType with an alpha of 34.5%.
33     kRectangleImageWithAlpha_SrcType      = 0x02,
34     //! kRectnagleImageWithAlpha_SrcType scaled down by half.
35     kSmallRectangleImageWithAlpha_SrcType = 0x04,
36     //! kRectangleImage_SrcType drawn directly instead in an image.
37     kRectangle_SrcType                    = 0x08,
38     //! Two rectangles, first on the right half, second on the bottom half.
39     kQuarterClear_SrcType                 = 0x10,
40     //! kQuarterClear_SrcType in a layer.
41     kQuarterClearInLayer_SrcType          = 0x20,
42     //! A W/2xH/2 transparent image.
43     kSmallTransparentImage_SrcType        = 0x40,
44     //! kRectangleImage_SrcType drawn directly with a mask.
45     kRectangleWithMask_SrcType            = 0x80,
46 
47     kAll_SrcType                          = 0xFF, //!< All the source types.
48     kBasic_SrcType                        = 0x03, //!< Just basic source types.
49 };
50 
51 const struct {
52     SkBlendMode fMode;
53     int         fSourceTypeMask;  // The source types to use this
54                                   // mode with. See draw_mode for
55                                   // an explanation of each type.
56                                   // PDF has to play some tricks
57                                   // to support the base modes,
58                                   // test those more extensively.
59 } gModes[] = {
60     { SkBlendMode::kClear,        kAll_SrcType   },
61     { SkBlendMode::kSrc,          kAll_SrcType   },
62     { SkBlendMode::kDst,          kAll_SrcType   },
63     { SkBlendMode::kSrcOver,      kAll_SrcType   },
64     { SkBlendMode::kDstOver,      kAll_SrcType   },
65     { SkBlendMode::kSrcIn,        kAll_SrcType   },
66     { SkBlendMode::kDstIn,        kAll_SrcType   },
67     { SkBlendMode::kSrcOut,       kAll_SrcType   },
68     { SkBlendMode::kDstOut,       kAll_SrcType   },
69     { SkBlendMode::kSrcATop,      kAll_SrcType   },
70     { SkBlendMode::kDstATop,      kAll_SrcType   },
71 
72     { SkBlendMode::kXor,          kBasic_SrcType },
73     { SkBlendMode::kPlus,         kBasic_SrcType },
74     { SkBlendMode::kModulate,     kAll_SrcType   },
75     { SkBlendMode::kScreen,       kBasic_SrcType },
76     { SkBlendMode::kOverlay,      kBasic_SrcType },
77     { SkBlendMode::kDarken,       kBasic_SrcType },
78     { SkBlendMode::kLighten,      kBasic_SrcType },
79     { SkBlendMode::kColorDodge,   kBasic_SrcType },
80     { SkBlendMode::kColorBurn,    kBasic_SrcType },
81     { SkBlendMode::kHardLight,    kBasic_SrcType },
82     { SkBlendMode::kSoftLight,    kBasic_SrcType },
83     { SkBlendMode::kDifference,   kBasic_SrcType },
84     { SkBlendMode::kExclusion,    kBasic_SrcType },
85     { SkBlendMode::kMultiply,     kAll_SrcType   },
86     { SkBlendMode::kHue,          kBasic_SrcType },
87     { SkBlendMode::kSaturation,   kBasic_SrcType },
88     { SkBlendMode::kColor,        kBasic_SrcType },
89     { SkBlendMode::kLuminosity,   kBasic_SrcType },
90 };
91 
make_bitmaps(int w,int h,SkBitmap * src,SkBitmap * dst,SkBitmap * transparent)92 static void make_bitmaps(int w, int h, SkBitmap* src, SkBitmap* dst,
93                          SkBitmap* transparent) {
94     src->allocN32Pixels(w, h);
95     src->eraseColor(SK_ColorTRANSPARENT);
96 
97     SkPaint p;
98     p.setAntiAlias(true);
99 
100     SkRect r;
101     SkScalar ww = SkIntToScalar(w);
102     SkScalar hh = SkIntToScalar(h);
103 
104     {
105         SkCanvas c(*src);
106         p.setColor(ToolUtils::color_to_565(0xFFFFCC44));
107         r.setWH(ww*3/4, hh*3/4);
108         c.drawOval(r, p);
109     }
110 
111     dst->allocN32Pixels(w, h);
112     dst->eraseColor(SK_ColorTRANSPARENT);
113 
114     {
115         SkCanvas c(*dst);
116         p.setColor(ToolUtils::color_to_565(0xFF66AAFF));
117         r.setLTRB(ww/3, hh/3, ww*19/20, hh*19/20);
118         c.drawRect(r, p);
119     }
120 
121     transparent->allocN32Pixels(w, h);
122     transparent->eraseColor(SK_ColorTRANSPARENT);
123 }
124 
125 static uint16_t gData[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
126 
127 class XfermodesGM : public skiagm::GM {
128     SkBitmap    fBG;
129     SkBitmap    fSrcB, fDstB, fTransparent;
130 
131     /* The srcType argument indicates what to draw for the source part. Skia
132      * uses the implied shape of the drawing command and these modes
133      * demonstrate that.
134      */
draw_mode(SkCanvas * canvas,SkBlendMode mode,SrcType srcType,SkScalar x,SkScalar y)135     void draw_mode(SkCanvas* canvas, SkBlendMode mode, SrcType srcType, SkScalar x, SkScalar y) {
136         SkPaint p;
137         SkSamplingOptions sampling;
138         SkMatrix m;
139         bool restoreNeeded = false;
140         m.setTranslate(x, y);
141 
142         canvas->drawImage(fSrcB.asImage(), x, y, sampling, &p);
143         p.setBlendMode(mode);
144         switch (srcType) {
145             case kSmallTransparentImage_SrcType: {
146                 m.postScale(SK_ScalarHalf, SK_ScalarHalf, x, y);
147 
148                 SkAutoCanvasRestore acr(canvas, true);
149                 canvas->concat(m);
150                 canvas->drawImage(fTransparent.asImage(), 0, 0, sampling, &p);
151                 break;
152             }
153             case kQuarterClearInLayer_SrcType: {
154                 SkRect bounds = SkRect::MakeXYWH(x, y, SkIntToScalar(W),
155                                                  SkIntToScalar(H));
156                 canvas->saveLayer(&bounds, &p);
157                 restoreNeeded = true;
158                 p.setBlendMode(SkBlendMode::kSrcOver);
159                 [[fallthrough]];
160             }
161             case kQuarterClear_SrcType: {
162                 SkScalar halfW = SkIntToScalar(W) / 2;
163                 SkScalar halfH = SkIntToScalar(H) / 2;
164                 p.setColor(ToolUtils::color_to_565(0xFF66AAFF));
165                 SkRect r = SkRect::MakeXYWH(x + halfW, y, halfW,
166                                             SkIntToScalar(H));
167                 canvas->drawRect(r, p);
168                 p.setColor(ToolUtils::color_to_565(0xFFAA66FF));
169                 r = SkRect::MakeXYWH(x, y + halfH, SkIntToScalar(W), halfH);
170                 canvas->drawRect(r, p);
171                 break;
172             }
173             case kRectangleWithMask_SrcType: {
174                 canvas->save();
175                 restoreNeeded = true;
176                 SkScalar w = SkIntToScalar(W);
177                 SkScalar h = SkIntToScalar(H);
178                 SkRect r = SkRect::MakeXYWH(x, y + h / 4, w, h * 23 / 60);
179                 canvas->clipRect(r);
180                 [[fallthrough]];
181             }
182             case kRectangle_SrcType: {
183                 SkScalar w = SkIntToScalar(W);
184                 SkScalar h = SkIntToScalar(H);
185                 SkRect r = SkRect::MakeXYWH(x + w / 3, y + h / 3,
186                                             w * 37 / 60, h * 37 / 60);
187                 p.setColor(ToolUtils::color_to_565(0xFF66AAFF));
188                 canvas->drawRect(r, p);
189                 break;
190             }
191             case kSmallRectangleImageWithAlpha_SrcType:
192                 m.postScale(SK_ScalarHalf, SK_ScalarHalf, x, y);
193                 [[fallthrough]];
194             case kRectangleImageWithAlpha_SrcType:
195                 p.setAlpha(0x88);
196                 [[fallthrough]];
197             case kRectangleImage_SrcType: {
198                 SkAutoCanvasRestore acr(canvas, true);
199                 canvas->concat(m);
200                 canvas->drawImage(fDstB.asImage(), 0, 0, sampling, &p);
201                 break;
202             }
203             default:
204                 break;
205         }
206 
207         if (restoreNeeded) {
208             canvas->restore();
209         }
210     }
211 
onOnceBeforeDraw()212     void onOnceBeforeDraw() override {
213         fBG.installPixels(SkImageInfo::Make(2, 2, kARGB_4444_SkColorType,
214                                             kOpaque_SkAlphaType),
215                           gData, 4);
216 
217         make_bitmaps(W, H, &fSrcB, &fDstB, &fTransparent);
218     }
219 
220 public:
221     const static int W = 64;
222     const static int H = 64;
XfermodesGM()223     XfermodesGM() {}
224 
225 protected:
getName() const226     SkString getName() const override { return SkString("xfermodes"); }
227 
getISize()228     SkISize getISize() override { return SkISize::Make(1990, 570); }
229 
onDraw(SkCanvas * canvas)230     void onDraw(SkCanvas* canvas) override {
231         canvas->translate(SkIntToScalar(10), SkIntToScalar(20));
232 
233         const SkScalar w = SkIntToScalar(W);
234         const SkScalar h = SkIntToScalar(H);
235         SkMatrix m;
236         m.setScale(SkIntToScalar(6), SkIntToScalar(6));
237         auto s = fBG.makeShader(SkTileMode::kRepeat, SkTileMode::kRepeat,
238                                 SkSamplingOptions(), m);
239 
240         SkPaint labelP;
241         labelP.setAntiAlias(true);
242 
243         SkFont font = ToolUtils::DefaultPortableFont();
244 
245         const int kWrap = 5;
246 
247         SkScalar x0 = 0;
248         SkScalar y0 = 0;
249         for (int sourceType = 1; sourceType & kAll_SrcType; sourceType <<= 1) {
250             SkScalar x = x0, y = y0;
251             for (size_t i = 0; i < std::size(gModes); i++) {
252                 if ((gModes[i].fSourceTypeMask & sourceType) == 0) {
253                     continue;
254                 }
255                 SkRect r{ x, y, x+w, y+h };
256 
257                 SkPaint p;
258                 p.setStyle(SkPaint::kFill_Style);
259                 p.setShader(s);
260                 canvas->drawRect(r, p);
261 
262                 canvas->saveLayer(&r, nullptr);
263                 draw_mode(canvas, gModes[i].fMode, static_cast<SrcType>(sourceType),
264                           r.fLeft, r.fTop);
265                 canvas->restore();
266 
267                 r.inset(-SK_ScalarHalf, -SK_ScalarHalf);
268                 p.setStyle(SkPaint::kStroke_Style);
269                 p.setShader(nullptr);
270                 canvas->drawRect(r, p);
271 
272 #if 1
273                 const char* label = SkBlendMode_Name(gModes[i].fMode);
274                 SkTextUtils::DrawString(canvas, label, x + w/2, y - font.getSize()/2,
275                                         font, labelP, SkTextUtils::kCenter_Align);
276 #endif
277                 x += w + SkIntToScalar(10);
278                 if ((i % kWrap) == kWrap - 1) {
279                     x = x0;
280                     y += h + SkIntToScalar(30);
281                 }
282             }
283             if (y < 320) {
284                 if (x > x0) {
285                     y += h + SkIntToScalar(30);
286                 }
287                 y0 = y;
288             } else {
289                 x0 += SkIntToScalar(400);
290                 y0 = 0;
291             }
292         }
293     }
294 
295 private:
296     using INHERITED = GM;
297 };
298 DEF_GM( return new XfermodesGM; )
299