Debian Codegraph @

gm/pathopsblend.cpp

raw
1 /*
2 * Copyright 2024 Google LLC
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/SkPaint.h"
14 #include "include/core/SkPath.h"
15 #include "include/core/SkPathBuilder.h"
16 #include "include/core/SkRect.h"
17 #include "include/pathops/SkPathOps.h"
18 #include "src/core/SkStroke.h"
19 #include "tools/ToolUtils.h"
20
21 static SkPath cross() {
22 SkPathBuilder builder;
23 builder.addRect({15, 0, 35, 50});
24 builder.addRect({0, 15, 50, 35});
25 return builder.detach();
26 }
27
28 static SkPath circle() { return SkPath::Circle(25, 25, 20); }
29
30 // We implement every op except ReverseDifference: That one can be handled by swapping the paths
31 // and using the Difference logic.
32 static constexpr SkPathOp kOps[] = {
33 kDifference_SkPathOp,
34 kIntersect_SkPathOp,
35 kUnion_SkPathOp,
36 kXOR_SkPathOp,
37 };
38
39 struct OpAsBlend {
40 SkBlendMode fMode;
41 bool fInverse = false;
42 };
43
44 static OpAsBlend op_blend_mode(SkPathOp op) {
45 switch (op) {
46 case kDifference_SkPathOp:
47 return {SkBlendMode::kClear};
48 case kIntersect_SkPathOp:
49 return {SkBlendMode::kClear, /*fInverse=*/true};
50 case kUnion_SkPathOp:
51 return {SkBlendMode::kPlus};
52 case kXOR_SkPathOp:
53 return {SkBlendMode::kXor};
54 default:
55 // We don't implement kReverseDifference (see note above)
56 SkASSERT(op == kReverseDifference_SkPathOp);
57 return {SkBlendMode::kSrcOver};
58 }
59 }
60
61 DEF_SIMPLE_GM(pathops_blend, canvas, 130, 60 * std::size(kOps) + 60 + 10) {
62 // Checkerboard background to demonstrate that we're only covering the pixels we want:
63 ToolUtils::draw_checkerboard(canvas);
64
65 // Two paths that overlap in interesting ways:
66 SkPath p1 = cross();
67 SkPath p2 = circle();
68 // One path op (intersect) requires one path be drawn using inverse-fill:
69 SkPath p2inv = p2;
70 p2inv.toggleInverseFillType();
71
72 SkPaint paint;
73 paint.setAntiAlias(true);
74
75 canvas->translate(10, 10);
76
77 // Draw the two paths by themselves:
78 {
79 canvas->save();
80 canvas->drawPath(p1, paint);
81 canvas->translate(60, 0);
82 canvas->drawPath(p2, paint);
83 canvas->restore();
84 canvas->translate(0, 60);
85 }
86
87 for (SkPathOp op : kOps) {
88 canvas->save();
89
90 // Use PathOps to compute new path, then draw it:
91 if (auto opPath = Op(p1, p2, op)) {
92 canvas->drawPath(*opPath, paint);
93 }
94
95 canvas->translate(60, 0);
96
97 // Do raster version of op
98 {
99 auto blend = op_blend_mode(op);
100 // Create a layer. We will use blending to build a mask of the shape we want here.
101 // Note that we're always going to get a SrcOver blend of the final shape when this
102 // layer is restored. The math doesn't work out for most blend modes, because we're
103 // turning the coverage of the resulting shape into the layer's alpha.
104 canvas->saveLayer(SkRect::MakeWH(50, 50), nullptr);
105
106 // We reuse this paint to apply various blend modes:
107 SkPaint p;
108 p.setAntiAlias(true);
109
110 // Draw the first shape, using SrcOver. This fills the layer with a mask of that path:
111 p.setBlendMode(SkBlendMode::kSrcOver);
112 canvas->drawPath(p1, p);
113
114 // Based on the PathOp we're emulating, we set a specific blend mode, and then fill
115 // either the second path -- or its inverse.
116 p.setBlendMode(blend.fMode);
117 canvas->drawPath(blend.fInverse ? p2inv : p2, p);
118
119 // The layer's alpha channel now contains a mask of the desired shape. Cover the entire
120 // rectangle with whatever paint we ACTUALLY want to draw (eg, blue), using kSrcIn.
121 // This will only draw where the mask was present:
122 p.setBlendMode(SkBlendMode::kSrcIn);
123 p.setColor(SK_ColorBLUE);
124 canvas->drawPaint(p);
125 canvas->restore();
126 }
127
128 canvas->restore();
129 canvas->translate(0, 60);
130 }
131 }