blob: 1d5a48b8b41c442d1731c6c149748de63e23277b [file] [log] [blame]
bsalomon47cc7692016-04-26 12:56:00 -07001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "src/gpu/GrStyle.h"
9#include "src/utils/SkDashPathPriv.h"
bsalomon47cc7692016-04-26 12:56:00 -070010
bsalomon06077562016-05-04 13:50:29 -070011int GrStyle::KeySize(const GrStyle &style, Apply apply, uint32_t flags) {
Brian Salomon4dea72a2019-12-18 10:43:10 -050012 static_assert(sizeof(uint32_t) == sizeof(SkScalar));
bsalomonfb083272016-05-04 08:27:41 -070013 int size = 0;
14 if (style.isDashed()) {
bsalomon97fd2d42016-05-09 13:02:01 -070015 // One scalar for scale, one for dash phase, and one for each dash value.
16 size += 2 + style.dashIntervalCnt();
bsalomonfb083272016-05-04 08:27:41 -070017 } else if (style.pathEffect()) {
18 // No key for a generic path effect.
19 return -1;
20 }
21
22 if (Apply::kPathEffectOnly == apply) {
23 return size;
24 }
25
26 if (style.strokeRec().needToApply()) {
bsalomon97fd2d42016-05-09 13:02:01 -070027 // One for res scale, one for style/cap/join, one for miter limit, and one for width.
28 size += 4;
bsalomonfb083272016-05-04 08:27:41 -070029 }
30 return size;
31}
32
bsalomon97fd2d42016-05-09 13:02:01 -070033void GrStyle::WriteKey(uint32_t *key, const GrStyle &style, Apply apply, SkScalar scale,
34 uint32_t flags) {
bsalomonfb083272016-05-04 08:27:41 -070035 SkASSERT(key);
36 SkASSERT(KeySize(style, apply) >= 0);
Brian Salomon4dea72a2019-12-18 10:43:10 -050037 static_assert(sizeof(uint32_t) == sizeof(SkScalar));
bsalomonfb083272016-05-04 08:27:41 -070038
39 int i = 0;
bsalomon97fd2d42016-05-09 13:02:01 -070040 // The scale can influence both the path effect and stroking. We want to preserve the
41 // property that the following two are equal:
42 // 1. WriteKey with apply == kPathEffectAndStrokeRec
43 // 2. WriteKey with apply == kPathEffectOnly followed by WriteKey of a GrStyle made
44 // from SkStrokeRec output by the the path effect (and no additional path effect).
45 // Since the scale can affect both parts of 2 we write it into the key twice.
bsalomonfb083272016-05-04 08:27:41 -070046 if (style.isDashed()) {
Brian Salomon4dea72a2019-12-18 10:43:10 -050047 static_assert(sizeof(style.dashPhase()) == sizeof(uint32_t));
bsalomonfb083272016-05-04 08:27:41 -070048 SkScalar phase = style.dashPhase();
bsalomon97fd2d42016-05-09 13:02:01 -070049 memcpy(&key[i++], &scale, sizeof(SkScalar));
bsalomonfb083272016-05-04 08:27:41 -070050 memcpy(&key[i++], &phase, sizeof(SkScalar));
51
52 int32_t count = style.dashIntervalCnt();
53 // Dash count should always be even.
54 SkASSERT(0 == (count & 0x1));
55 const SkScalar *intervals = style.dashIntervals();
56 int intervalByteCnt = count * sizeof(SkScalar);
57 memcpy(&key[i], intervals, intervalByteCnt);
58 i += count;
59 } else {
60 SkASSERT(!style.pathEffect());
61 }
62
63 if (Apply::kPathEffectAndStrokeRec == apply && style.strokeRec().needToApply()) {
bsalomon97fd2d42016-05-09 13:02:01 -070064 memcpy(&key[i++], &scale, sizeof(SkScalar));
bsalomonfb083272016-05-04 08:27:41 -070065 enum {
66 kStyleBits = 2,
67 kJoinBits = 2,
68 kCapBits = 32 - kStyleBits - kJoinBits,
69
70 kJoinShift = kStyleBits,
71 kCapShift = kJoinShift + kJoinBits,
72 };
Brian Salomon4dea72a2019-12-18 10:43:10 -050073 static_assert(SkStrokeRec::kStyleCount <= (1 << kStyleBits));
74 static_assert(SkPaint::kJoinCount <= (1 << kJoinBits));
75 static_assert(SkPaint::kCapCount <= (1 << kCapBits));
bsalomon06077562016-05-04 13:50:29 -070076 // The cap type only matters for unclosed shapes. However, a path effect could unclose
77 // the shape before it is stroked.
bsalomon0ae36a22016-07-18 07:31:13 -070078 SkPaint::Cap cap = SkPaint::kDefault_Cap;
79 if (!(flags & kClosed_KeyFlag) || style.pathEffect()) {
bsalomon06077562016-05-04 13:50:29 -070080 cap = style.strokeRec().getCap();
81 }
bsalomon0ae36a22016-07-18 07:31:13 -070082 SkScalar miter = -1.f;
83 SkPaint::Join join = SkPaint::kDefault_Join;
84
85 // Dashing will not insert joins but other path effects may.
86 if (!(flags & kNoJoins_KeyFlag) || style.hasNonDashPathEffect()) {
87 join = style.strokeRec().getJoin();
88 // Miter limit only affects miter joins
89 if (SkPaint::kMiter_Join == join) {
90 miter = style.strokeRec().getMiter();
91 }
92 }
93
bsalomonfb083272016-05-04 08:27:41 -070094 key[i++] = style.strokeRec().getStyle() |
bsalomon0ae36a22016-07-18 07:31:13 -070095 join << kJoinShift |
bsalomon06077562016-05-04 13:50:29 -070096 cap << kCapShift;
bsalomonfb083272016-05-04 08:27:41 -070097
bsalomon0ae36a22016-07-18 07:31:13 -070098 memcpy(&key[i++], &miter, sizeof(miter));
bsalomonfb083272016-05-04 08:27:41 -070099
bsalomon0ae36a22016-07-18 07:31:13 -0700100 SkScalar width = style.strokeRec().getWidth();
101 memcpy(&key[i++], &width, sizeof(width));
bsalomonfb083272016-05-04 08:27:41 -0700102 }
103 SkASSERT(KeySize(style, apply) == i);
104}
105
Robert Phillipsf809c1e2017-01-13 11:02:42 -0500106void GrStyle::initPathEffect(sk_sp<SkPathEffect> pe) {
caryclarkd6562002016-07-27 12:02:07 -0700107 SkASSERT(!fPathEffect);
bsalomonfb083272016-05-04 08:27:41 -0700108 SkASSERT(SkPathEffect::kNone_DashType == fDashInfo.fType);
109 SkASSERT(0 == fDashInfo.fIntervals.count());
bsalomon47cc7692016-04-26 12:56:00 -0700110 if (!pe) {
bsalomon47cc7692016-04-26 12:56:00 -0700111 return;
112 }
113 SkPathEffect::DashInfo info;
114 if (SkPathEffect::kDash_DashType == pe->asADash(&info)) {
bsalomona0587862016-06-09 06:03:38 -0700115 SkStrokeRec::Style recStyle = fStrokeRec.getStyle();
116 if (recStyle != SkStrokeRec::kFill_Style && recStyle != SkStrokeRec::kStrokeAndFill_Style) {
bsalomon47cc7692016-04-26 12:56:00 -0700117 fDashInfo.fType = SkPathEffect::kDash_DashType;
118 fDashInfo.fIntervals.reset(info.fCount);
119 fDashInfo.fPhase = info.fPhase;
120 info.fIntervals = fDashInfo.fIntervals.get();
121 pe->asADash(&info);
Robert Phillipsf809c1e2017-01-13 11:02:42 -0500122 fPathEffect = std::move(pe);
bsalomon47cc7692016-04-26 12:56:00 -0700123 }
124 } else {
Robert Phillipsf809c1e2017-01-13 11:02:42 -0500125 fPathEffect = std::move(pe);
bsalomon47cc7692016-04-26 12:56:00 -0700126 }
bsalomonfb083272016-05-04 08:27:41 -0700127}
128
bsalomon398e3f42016-06-13 10:22:48 -0700129bool GrStyle::applyPathEffect(SkPath* dst, SkStrokeRec* strokeRec, const SkPath& src) const {
130 if (!fPathEffect) {
bsalomonfb083272016-05-04 08:27:41 -0700131 return false;
132 }
Tyler Dennistonf8b7c1a2021-07-13 13:22:19 -0400133
134 // TODO: [skbug.com/11957] Plumb CTM callers and pass it to filterPath().
135 SkASSERT(!fPathEffect->needsCTM());
136
bsalomon398e3f42016-06-13 10:22:48 -0700137 if (SkPathEffect::kDash_DashType == fDashInfo.fType) {
138 // We apply the dash ourselves here rather than using the path effect. This is so that
139 // we can control whether the dasher applies the strokeRec for special cases. Our keying
140 // depends on the strokeRec being applied separately.
Tyler Dennistonf8b7c1a2021-07-13 13:22:19 -0400141 SkASSERT(!fPathEffect->needsCTM()); // Make sure specified PE doesn't need CTM
bsalomon398e3f42016-06-13 10:22:48 -0700142 SkScalar phase = fDashInfo.fPhase;
143 const SkScalar* intervals = fDashInfo.fIntervals.get();
144 int intervalCnt = fDashInfo.fIntervals.count();
145 SkScalar initialLength;
146 int initialIndex;
147 SkScalar intervalLength;
148 SkDashPath::CalcDashParameters(phase, intervals, intervalCnt, &initialLength,
149 &initialIndex, &intervalLength);
150 if (!SkDashPath::InternalFilter(dst, src, strokeRec,
151 nullptr, intervals, intervalCnt,
152 initialLength, initialIndex, intervalLength,
153 SkDashPath::StrokeRecApplication::kDisallow)) {
154 return false;
155 }
156 } else if (!fPathEffect->filterPath(dst, src, strokeRec, nullptr)) {
bsalomonfb083272016-05-04 08:27:41 -0700157 return false;
158 }
159 dst->setIsVolatile(true);
160 return true;
161}
162
163bool GrStyle::applyPathEffectToPath(SkPath *dst, SkStrokeRec *remainingStroke,
bsalomon97fd2d42016-05-09 13:02:01 -0700164 const SkPath &src, SkScalar resScale) const {
bsalomonfb083272016-05-04 08:27:41 -0700165 SkASSERT(dst);
166 SkStrokeRec strokeRec = fStrokeRec;
bsalomon97fd2d42016-05-09 13:02:01 -0700167 strokeRec.setResScale(resScale);
bsalomon398e3f42016-06-13 10:22:48 -0700168 if (!this->applyPathEffect(dst, &strokeRec, src)) {
bsalomonfb083272016-05-04 08:27:41 -0700169 return false;
170 }
171 *remainingStroke = strokeRec;
172 return true;
173}
174
bsalomon97fd2d42016-05-09 13:02:01 -0700175bool GrStyle::applyToPath(SkPath* dst, SkStrokeRec::InitStyle* style, const SkPath& src,
176 SkScalar resScale) const {
bsalomonfb083272016-05-04 08:27:41 -0700177 SkASSERT(style);
178 SkASSERT(dst);
179 SkStrokeRec strokeRec = fStrokeRec;
bsalomon97fd2d42016-05-09 13:02:01 -0700180 strokeRec.setResScale(resScale);
bsalomon1a0b9ed2016-05-06 11:07:03 -0700181 const SkPath* pathForStrokeRec = &src;
bsalomon398e3f42016-06-13 10:22:48 -0700182 if (this->applyPathEffect(dst, &strokeRec, src)) {
bsalomon1a0b9ed2016-05-06 11:07:03 -0700183 pathForStrokeRec = dst;
184 } else if (fPathEffect) {
bsalomonfb083272016-05-04 08:27:41 -0700185 return false;
186 }
187 if (strokeRec.needToApply()) {
bsalomon1a0b9ed2016-05-06 11:07:03 -0700188 if (!strokeRec.applyToPath(dst, *pathForStrokeRec)) {
bsalomonfb083272016-05-04 08:27:41 -0700189 return false;
190 }
bsalomonb85e63d2016-06-27 07:59:12 -0700191 dst->setIsVolatile(true);
bsalomonfb083272016-05-04 08:27:41 -0700192 *style = SkStrokeRec::kFill_InitStyle;
bsalomon1a0b9ed2016-05-06 11:07:03 -0700193 } else if (!fPathEffect) {
194 // Nothing to do for path effect or stroke, fail.
195 return false;
bsalomonfb083272016-05-04 08:27:41 -0700196 } else {
197 SkASSERT(SkStrokeRec::kFill_Style == strokeRec.getStyle() ||
198 SkStrokeRec::kHairline_Style == strokeRec.getStyle());
199 *style = strokeRec.getStyle() == SkStrokeRec::kFill_Style
200 ? SkStrokeRec::kFill_InitStyle
201 : SkStrokeRec::kHairline_InitStyle;
202 }
203 return true;
bsalomon47cc7692016-04-26 12:56:00 -0700204}