Flutter Engine
The Flutter Engine
Loading...
Searching...
No Matches
xfermodes.cpp
Go to the documentation of this file.
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"
13#include "include/core/SkFont.h"
17#include "include/core/SkRect.h"
20#include "include/core/SkSize.h"
26#include "tools/ToolUtils.h"
28
29enum SrcType {
30 //! A WxH image with a rectangle in the lower right.
32 //! kRectangleImage_SrcType with an alpha of 34.5%.
34 //! kRectnagleImageWithAlpha_SrcType scaled down by half.
36 //! kRectangleImage_SrcType drawn directly instead in an image.
38 //! Two rectangles, first on the right half, second on the bottom half.
40 //! kQuarterClear_SrcType in a layer.
42 //! A W/2xH/2 transparent image.
44 //! kRectangleImage_SrcType drawn directly with a mask.
46
47 kAll_SrcType = 0xFF, //!< All the source types.
48 kBasic_SrcType = 0x03, //!< Just basic source types.
49};
50
51const struct {
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[] = {
71
90};
91
92static 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;
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
125static uint16_t gData[] = { 0xFFFF, 0xCCCF, 0xCCCF, 0xFFFF };
126
127class 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 */
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) {
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 }
156 canvas->saveLayer(&bounds, &p);
157 restoreNeeded = true;
158 p.setBlendMode(SkBlendMode::kSrcOver);
159 [[fallthrough]];
160 }
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,
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 }
174 canvas->save();
175 restoreNeeded = true;
178 SkRect r = SkRect::MakeXYWH(x, y + h / 4, w, h * 23 / 60);
179 canvas->clipRect(r);
180 [[fallthrough]];
181 }
182 case kRectangle_SrcType: {
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 }
192 m.postScale(SK_ScalarHalf, SK_ScalarHalf, x, y);
193 [[fallthrough]];
195 p.setAlpha(0x88);
196 [[fallthrough]];
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
212 void onOnceBeforeDraw() override {
215 gData, 4);
216
217 make_bitmaps(W, H, &fSrcB, &fDstB, &fTransparent);
218 }
219
220public:
221 const static int W = 64;
222 const static int H = 64;
224
225protected:
226 SkString getName() const override { return SkString("xfermodes"); }
227
228 SkISize getISize() override { return SkISize::Make(1990, 570); }
229
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));
238 SkSamplingOptions(), m);
239
240 SkPaint labelP;
241 labelP.setAntiAlias(true);
242
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
295private:
296 using INHERITED = GM;
297};
298DEF_GM( return new XfermodesGM; )
@ kOpaque_SkAlphaType
pixel is opaque
Definition SkAlphaType.h:28
SK_API const char * SkBlendMode_Name(SkBlendMode blendMode)
SkBlendMode
Definition SkBlendMode.h:38
@ kSrcOut
r = s * (1-da)
@ kExclusion
rc = s + d - two(s*d), ra = kSrcOver
@ kSaturation
saturation of source with hue and luminosity of destination
@ kColorBurn
darken destination to reflect source
@ kPlus
r = min(s + d, 1)
@ kLighten
rc = s + d - min(s*da, d*sa), ra = kSrcOver
@ kHue
hue of source with saturation and luminosity of destination
@ kDstIn
r = d * sa
@ kModulate
r = s*d
@ kMultiply
r = s*(1-da) + d*(1-sa) + s*d
@ kColorDodge
brighten destination to reflect source
@ kScreen
r = s + d - s*d
@ kSrcOver
r = s + (1-sa)*d
@ kXor
r = s*(1-da) + d*(1-sa)
@ kLuminosity
luminosity of source with hue and saturation of destination
@ kSoftLight
lighten or darken, depending on source
@ kDifference
rc = s + d - 2*(min(s*da, d*sa)), ra = kSrcOver
@ kOverlay
multiply or screen, depending on destination
@ kSrcATop
r = s*da + d*(1-sa)
@ kDstATop
r = d*sa + s*(1-da)
@ kDstOver
r = d + (1-da)*s
@ kColor
hue and saturation of source with luminosity of destination
@ kHardLight
multiply or screen, depending on source
@ kDstOut
r = d * (1-sa)
@ kDarken
rc = s + d - max(s*da, d*sa), ra = kSrcOver
@ kSrcIn
r = s * da
@ kClear
r = 0
@ kARGB_4444_SkColorType
pixel with 4 bits for alpha, red, green, blue; in 16-bit word
Definition SkColorType.h:23
constexpr SkColor SK_ColorTRANSPARENT
Definition SkColor.h:99
#define SK_ScalarHalf
Definition SkScalar.h:19
#define SkIntToScalar(x)
Definition SkScalar.h:57
sk_sp< SkImage > asImage() const
Definition SkBitmap.cpp:645
bool installPixels(const SkImageInfo &info, void *pixels, size_t rowBytes, void(*releaseProc)(void *addr, void *context), void *context)
Definition SkBitmap.cpp:323
sk_sp< SkShader > makeShader(SkTileMode tmx, SkTileMode tmy, const SkSamplingOptions &, const SkMatrix *localMatrix=nullptr) const
Definition SkBitmap.cpp:669
void allocN32Pixels(int width, int height, bool isOpaque=false)
Definition SkBitmap.cpp:232
void eraseColor(SkColor4f) const
Definition SkBitmap.cpp:442
int saveLayer(const SkRect *bounds, const SkPaint *paint)
Definition SkCanvas.cpp:500
void drawRect(const SkRect &rect, const SkPaint &paint)
void drawOval(const SkRect &oval, const SkPaint &paint)
void clipRect(const SkRect &rect, SkClipOp op, bool doAntiAlias)
void restore()
Definition SkCanvas.cpp:465
void translate(SkScalar dx, SkScalar dy)
int save()
Definition SkCanvas.cpp:451
void concat(const SkMatrix &matrix)
void drawImage(const SkImage *image, SkScalar left, SkScalar top)
Definition SkCanvas.h:1528
void setAntiAlias(bool aa)
Definition SkPaint.h:170
@ kStroke_Style
set to stroke geometry
Definition SkPaint.h:194
@ kFill_Style
set to fill geometry
Definition SkPaint.h:193
static void DrawString(SkCanvas *canvas, const char text[], SkScalar x, SkScalar y, const SkFont &font, const SkPaint &paint, Align align=kLeft_Align)
Definition SkTextUtils.h:34
static const int W
SkISize getISize() override
SkString getName() const override
void onOnceBeforeDraw() override
void onDraw(SkCanvas *canvas) override
GM(SkColor backgroundColor=SK_ColorWHITE)
Definition gm.cpp:81
float SkScalar
Definition extension.cpp:12
struct MyStruct s
#define DEF_GM(CODE)
Definition gm.h:40
double y
double x
SkFont DefaultPortableFont()
SkColor color_to_565(SkColor color)
SkScalar w
SkScalar h
Definition SkMD5.cpp:130
static constexpr SkISize Make(int32_t w, int32_t h)
Definition SkSize.h:20
static SkImageInfo Make(int width, int height, SkColorType ct, SkAlphaType at)
void setWH(float width, float height)
Definition SkRect.h:944
static constexpr SkRect MakeXYWH(float x, float y, float w, float h)
Definition SkRect.h:659
void setLTRB(float left, float top, float right, float bottom)
Definition SkRect.h:865
static uint16_t gData[]
const struct @274 gModes[]
SrcType
Definition xfermodes.cpp:29
@ kBasic_SrcType
Just basic source types.
Definition xfermodes.cpp:48
@ kRectangleImageWithAlpha_SrcType
kRectangleImage_SrcType with an alpha of 34.5%.
Definition xfermodes.cpp:33
@ kRectangleImage_SrcType
A WxH image with a rectangle in the lower right.
Definition xfermodes.cpp:31
@ kSmallRectangleImageWithAlpha_SrcType
kRectnagleImageWithAlpha_SrcType scaled down by half.
Definition xfermodes.cpp:35
@ kAll_SrcType
All the source types.
Definition xfermodes.cpp:47
@ kSmallTransparentImage_SrcType
A W/2xH/2 transparent image.
Definition xfermodes.cpp:43
@ kQuarterClear_SrcType
Two rectangles, first on the right half, second on the bottom half.
Definition xfermodes.cpp:39
@ kRectangle_SrcType
kRectangleImage_SrcType drawn directly instead in an image.
Definition xfermodes.cpp:37
@ kRectangleWithMask_SrcType
kRectangleImage_SrcType drawn directly with a mask.
Definition xfermodes.cpp:45
@ kQuarterClearInLayer_SrcType
kQuarterClear_SrcType in a layer.
Definition xfermodes.cpp:41
static void make_bitmaps(int w, int h, SkBitmap *src, SkBitmap *dst, SkBitmap *transparent)
Definition xfermodes.cpp:92
SkBlendMode fMode
Definition xfermodes.cpp:52
int fSourceTypeMask
Definition xfermodes.cpp:53