Debian Codegraph @

src/gpu/ganesh/GrStyle.h

raw
1 /*
2 * Copyright 2016 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 #ifndef GrStyle_DEFINED
9 #define GrStyle_DEFINED
10
11 #include "include/core/SkMatrix.h"
12 #include "include/core/SkPaint.h"
13 #include "include/core/SkPathEffect.h"
14 #include "include/core/SkRect.h"
15 #include "include/core/SkRefCnt.h"
16 #include "include/core/SkScalar.h"
17 #include "include/core/SkStrokeRec.h"
18 #include "include/private/base/SkAssert.h"
19 #include "include/private/base/SkMalloc.h"
20 #include "include/private/base/SkTemplates.h"
21 #include "include/private/base/SkTo.h"
22 #include "src/core/SkPathEffectBase.h"
23
24 #include <cstdint>
25 #include <utility>
26
27 class SkPath;
28
29 /**
30 * Represents the various ways that a GrStyledShape can be styled. It has fill/stroking information
31 * as well as an optional path effect. If the path effect represents dashing, the dashing
32 * information is extracted from the path effect and stored explicitly.
33 *
34 * This will replace GrStrokeInfo as GrStyledShape is deployed.
35 */
36 class GrStyle {
37 public:
38 /**
39 * A style object that represents a fill with no path effect.
40 * TODO: constexpr with C++14
41 */
42 static const GrStyle& SimpleFill() {
43 static const GrStyle kFill(SkStrokeRec::kFill_InitStyle);
44 return kFill;
45 }
46
47 /**
48 * A style object that represents a hairline stroke with no path effect.
49 * TODO: constexpr with C++14
50 */
51 static const GrStyle& SimpleHairline() {
52 static const GrStyle kHairline(SkStrokeRec::kHairline_InitStyle);
53 return kHairline;
54 }
55
56 enum class Apply {
57 kPathEffectOnly,
58 kPathEffectAndStrokeRec
59 };
60
61 /**
62 * Optional flags for computing keys that may remove unnecessary variation in the key due to
63 * style settings that don't affect particular classes of geometry.
64 */
65 enum KeyFlags {
66 // The shape being styled has no open contours.
67 kClosed_KeyFlag = 0x1,
68 // The shape being styled doesn't have any joins and so isn't affected by join type.
69 kNoJoins_KeyFlag = 0x2
70 };
71
72 /**
73 * Computes the key length for a GrStyle. The return will be negative if it cannot be turned
74 * into a key. This occurs when there is a path effect that is not a dash. The key can
75 * either reflect just the path effect (if one) or the path effect and the strokerec. Note
76 * that a simple fill has a zero sized key.
77 *
78 * If a positive value is returned, it will fit in a uint16_t.
79 */
80 static int KeySize(const GrStyle&, Apply, uint32_t flags = 0);
81
82 /**
83 * Writes a unique key for the style into the provided buffer. This function assumes the buffer
84 * has room for at least KeySize() values. It assumes that KeySize() returns a non-negative
85 * value for the combination of GrStyle, Apply and flags params. This is written so that the key
86 * for just dash application followed by the key for the remaining SkStrokeRec is the same as
87 * the key for applying dashing and SkStrokeRec all at once.
88 */
89 static void WriteKey(uint32_t*, const GrStyle&, Apply, SkScalar scale, uint32_t flags = 0);
90
91 GrStyle() : GrStyle(SkStrokeRec::kFill_InitStyle) {}
92
93 explicit GrStyle(SkStrokeRec::InitStyle initStyle) : fStrokeRec(initStyle) {}
94
95 GrStyle(const SkStrokeRec& strokeRec, sk_sp<SkPathEffect> pe) : fStrokeRec(strokeRec) {
96 this->initPathEffect(std::move(pe));
97 }
98
99 GrStyle(const GrStyle& that) = default;
100
101 explicit GrStyle(const SkPaint& paint) : fStrokeRec(paint) {
102 this->initPathEffect(paint.refPathEffect());
103 }
104
105 explicit GrStyle(const SkPaint& paint, SkPaint::Style overrideStyle)
106 : fStrokeRec(paint, overrideStyle) {
107 this->initPathEffect(paint.refPathEffect());
108 }
109
110 GrStyle& operator=(const GrStyle& that) {
111 fPathEffect = that.fPathEffect;
112 fDashInfo = that.fDashInfo;
113 fStrokeRec = that.fStrokeRec;
114 return *this;
115 }
116
117 void resetToInitStyle(SkStrokeRec::InitStyle fillOrHairline) {
118 fDashInfo.reset();
119 fPathEffect.reset(nullptr);
120 if (SkStrokeRec::kFill_InitStyle == fillOrHairline) {
121 fStrokeRec.setFillStyle();
122 } else {
123 fStrokeRec.setHairlineStyle();
124 }
125 }
126
127 /** Is this style a fill with no path effect? */
128 bool isSimpleFill() const { return fStrokeRec.isFillStyle() && !fPathEffect; }
129
130 /** Is this style a hairline with no path effect? */
131 bool isSimpleHairline() const { return fStrokeRec.isHairlineStyle() && !fPathEffect; }
132
133 SkPathEffect* pathEffect() const { return fPathEffect.get(); }
134 sk_sp<SkPathEffect> refPathEffect() const { return fPathEffect; }
135
136 bool hasPathEffect() const { return SkToBool(fPathEffect.get()); }
137
138 bool hasNonDashPathEffect() const { return fPathEffect.get() && !this->isDashed(); }
139
140 bool isDashed() const { return DashType::kDash == fDashInfo.fType; }
141 SkScalar dashPhase() const {
142 SkASSERT(this->isDashed());
143 return fDashInfo.fPhase;
144 }
145 int dashIntervalCnt() const {
146 SkASSERT(this->isDashed());
147 return fDashInfo.fIntervals.count();
148 }
149 const SkScalar* dashIntervals() const {
150 SkASSERT(this->isDashed());
151 return fDashInfo.fIntervals.get();
152 }
153
154 const SkStrokeRec& strokeRec() const { return fStrokeRec; }
155
156 /** Hairline or fill styles without path effects make no alterations to a geometry. */
157 bool applies() const {
158 return this->pathEffect() || (!fStrokeRec.isFillStyle() && !fStrokeRec.isHairlineStyle());
159 }
160
161 static SkScalar MatrixToScaleFactor(const SkMatrix& matrix) {
162 // getMaxScale will return -1 if the matrix has perspective. In that case we can use a scale
163 // factor of 1. This isn't necessarily a good choice and in the future we might consider
164 // taking a bounds here for the perspective case.
165 return SkScalarAbs(matrix.getMaxScale());
166 }
167 /**
168 * Applies just the path effect and returns remaining stroke information. This will fail if
169 * there is no path effect. dst may or may not have been overwritten on failure. Scale controls
170 * geometric approximations made by the path effect. It is typically computed from the view
171 * matrix.
172 */
173 [[nodiscard]] bool applyPathEffectToPath(SkPath* dst, SkStrokeRec* remainingStoke,
174 const SkPath& src, SkScalar scale) const;
175
176 /**
177 * If this succeeds then the result path should be filled or hairlined as indicated by the
178 * returned SkStrokeRec::InitStyle value. Will fail if there is no path effect and the
179 * strokerec doesn't change the geometry. When this fails the outputs may or may not have
180 * been overwritten. Scale controls geometric approximations made by the path effect and
181 * stroker. It is typically computed from the view matrix.
182 */
183 [[nodiscard]] bool applyToPath(SkPath* dst, SkStrokeRec::InitStyle* fillOrHairline,
184 const SkPath& src, SkScalar scale) const;
185
186 /** Given bounds of a path compute the bounds of path with the style applied. */
187 void adjustBounds(SkRect* dst, const SkRect& src) const {
188 *dst = src;
189 auto pe = as_PEB(this->pathEffect());
190 if (pe && !pe->computeFastBounds(dst)) {
191 // Restore dst == src since ComputeFastBounds leaves it undefined when returning false
192 *dst = src;
193 }
194
195 // This may not be the correct SkStrokeRec to use if there's a path effect: skbug.com/40036474
196 // It happens to work for dashing.
197 SkScalar radius = fStrokeRec.getInflationRadius();
198 dst->outset(radius, radius);
199 }
200
201 private:
202 void initPathEffect(sk_sp<SkPathEffect> pe);
203
204 enum class DashType {
205 kNone,
206 kDash,
207 };
208
209 struct DashInfo {
210 DashInfo() : fType(DashType::kNone) {}
211 DashInfo(const DashInfo& that) { *this = that; }
212 DashInfo& operator=(const DashInfo& that) {
213 fType = that.fType;
214 fPhase = that.fPhase;
215 fIntervals.reset(that.fIntervals.count());
216 sk_careful_memcpy(fIntervals.get(), that.fIntervals.get(),
217 sizeof(SkScalar) * that.fIntervals.count());
218 return *this;
219 }
220 void reset() {
221 fType = DashType::kNone;
222 fIntervals.reset(0);
223 }
224 DashType fType;
225 SkScalar fPhase{0};
226 skia_private::AutoSTArray<4, SkScalar> fIntervals;
227 };
228
229 bool applyPathEffect(SkPath* dst, SkStrokeRec* strokeRec, const SkPath& src) const;
230
231 SkStrokeRec fStrokeRec;
232 sk_sp<SkPathEffect> fPathEffect;
233 DashInfo fDashInfo;
234 };
235
236 #endif