blob: 49feef12f798a452897075814169654964f74f68 [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
8#include "GrShape.h"
9
10GrShape& GrShape::operator=(const GrShape& that) {
11 bool wasPath = Type::kPath == fType;
12 fStyle = that.fStyle;
13 fType = that.fType;
14 switch (fType) {
15 case Type::kEmpty:
16 if (wasPath) {
17 fPath.reset();
18 }
19 break;
20 case Type::kRRect:
21 if (wasPath) {
22 fPath.reset();
23 }
24 fRRect = that.fRRect;
25 break;
26 case Type::kPath:
27 if (wasPath) {
28 *fPath.get() = *that.fPath.get();
29 } else {
30 fPath.set(*that.fPath.get());
31 }
32 break;
33 }
34 fInheritedKey.reset(that.fInheritedKey.count());
35 memcpy(fInheritedKey.get(), that.fInheritedKey.get(),
36 sizeof(uint32_t) * fInheritedKey.count());
37 return *this;
38}
39
40int GrShape::unstyledKeySize() const {
41 if (fInheritedKey.count()) {
42 return fInheritedKey.count();
43 }
44 switch (fType) {
45 case Type::kEmpty:
46 return 1;
47 case Type::kRRect:
48 SkASSERT(!fInheritedKey.count());
49 SkASSERT(0 == SkRRect::kSizeInMemory % sizeof(uint32_t));
50 return SkRRect::kSizeInMemory / sizeof(uint32_t);
51 case Type::kPath:
52 if (fPath.get()->isVolatile()) {
53 return -1;
54 } else {
55 return 1;
56 }
57 }
58 SkFAIL("Should never get here.");
59 return 0;
60}
61
62void GrShape::writeUnstyledKey(uint32_t* key) const {
63 SkASSERT(this->unstyledKeySize());
64 SkDEBUGCODE(uint32_t* origKey = key;)
65 if (fInheritedKey.count()) {
66 memcpy(key, fInheritedKey.get(), sizeof(uint32_t) * fInheritedKey.count());
67 SkDEBUGCODE(key += fInheritedKey.count();)
68 } else {
69 switch (fType) {
70 case Type::kEmpty:
71 *key++ = 1;
72 break;
73 case Type::kRRect:
74 fRRect.writeToMemory(key);
75 key += SkRRect::kSizeInMemory / sizeof(uint32_t);
76 break;
77 case Type::kPath:
78 SkASSERT(!fPath.get()->isVolatile());
79 *key++ = fPath.get()->getGenerationID();
80 break;
81 }
82 }
83 SkASSERT(key - origKey == this->unstyledKeySize());
84}
85
86int GrShape::StyleKeySize(const GrStyle& style, bool stopAfterPE) {
87 GR_STATIC_ASSERT(sizeof(uint32_t) == sizeof(SkScalar));
88 int size = 0;
89 if (style.isDashed()) {
90 // One scalar for dash phase and one for each dash value.
91 size += 1 + style.dashIntervalCnt();
92 } else if (style.pathEffect()) {
93 // No key for a generic path effect.
94 return -1;
95 }
96
97 if (stopAfterPE) {
98 return size;
99 }
100
101 if (style.strokeRec().needToApply()) {
102 // One for style/cap/join, 2 for miter and width.
103 size += 3;
104 }
105 return size;
106}
107
108void GrShape::StyleKey(uint32_t* key, const GrStyle& style, bool stopAfterPE) {
109 SkASSERT(key);
110 SkASSERT(StyleKeySize(style, stopAfterPE) >= 0);
111 GR_STATIC_ASSERT(sizeof(uint32_t) == sizeof(SkScalar));
112
113 int i = 0;
114 if (style.isDashed()) {
115 GR_STATIC_ASSERT(sizeof(style.dashPhase()) == sizeof(uint32_t));
116 SkScalar phase = style.dashPhase();
117 memcpy(&key[i++], &phase, sizeof(SkScalar));
118
119 int32_t count = style.dashIntervalCnt();
120 // Dash count should always be even.
121 SkASSERT(0 == (count & 0x1));
122 const SkScalar* intervals = style.dashIntervals();
123 int intervalByteCnt = count * sizeof(SkScalar);
124 memcpy(&key[i], intervals, intervalByteCnt);
125 i += count;
126 } else {
127 SkASSERT(!style.pathEffect());
128 }
129
130 if (!stopAfterPE && style.strokeRec().needToApply()) {
131 enum {
132 kStyleBits = 2,
133 kJoinBits = 2,
134 kCapBits = 32 - kStyleBits - kJoinBits,
135
136 kJoinShift = kStyleBits,
137 kCapShift = kJoinShift + kJoinBits,
138 };
139 GR_STATIC_ASSERT(SkStrokeRec::kStyleCount <= (1 << kStyleBits));
140 GR_STATIC_ASSERT(SkPaint::kJoinCount <= (1 << kJoinBits));
141 GR_STATIC_ASSERT(SkPaint::kCapCount <= (1 << kCapBits));
142 key[i++] = style.strokeRec().getStyle() |
143 style.strokeRec().getJoin() << kJoinShift |
144 style.strokeRec().getCap() << kCapShift;
145
146 SkScalar scalar;
147 // Miter limit only affects miter joins
148 scalar = SkPaint::kMiter_Join == style.strokeRec().getJoin()
149 ? style.strokeRec().getMiter()
150 : -1.f;
151 memcpy(&key[i++], &scalar, sizeof(scalar));
152
153 scalar = style.strokeRec().getWidth();
154 memcpy(&key[i++], &scalar, sizeof(scalar));
155 }
156 SkASSERT(StyleKeySize(style, stopAfterPE) == i);
157}
158
bsalomon72dc51c2016-04-27 06:46:23 -0700159void GrShape::setInheritedKey(const GrShape &parent, bool stopAfterPE) {
bsalomon47cc7692016-04-26 12:56:00 -0700160 SkASSERT(!fInheritedKey.count());
161 // If the output shape turns out to be simple, then we will just use its geometric key
162 if (Type::kPath == fType) {
163 // We want ApplyFullStyle(ApplyPathEffect(shape)) to have the same key as
164 // ApplyFullStyle(shape).
165 // The full key is structured as (geo,path_effect,stroke).
166 // If we do ApplyPathEffect we get get,path_effect as the inherited key. If we then
167 // do ApplyFullStyle we'll memcpy geo,path_effect into the new inherited key
168 // and then append the style key (which should now be stroke only) at the end.
169 int parentCnt = parent.fInheritedKey.count();
170 bool useParentGeoKey = !parentCnt;
171 if (useParentGeoKey) {
172 parentCnt = parent.unstyledKeySize();
bsalomon72dc51c2016-04-27 06:46:23 -0700173 if (parentCnt < 0) {
174 // The parent's geometry has no key so we will have no key.
175 fPath.get()->setIsVolatile(true);
176 return;
177 }
bsalomon47cc7692016-04-26 12:56:00 -0700178 }
179 int styleCnt = StyleKeySize(parent.fStyle, stopAfterPE);
180 if (styleCnt < 0) {
181 // The style doesn't allow a key, set the path to volatile so that we fail when
182 // we try to get a key for the shape.
183 fPath.get()->setIsVolatile(true);
bsalomon72dc51c2016-04-27 06:46:23 -0700184 return;
bsalomon47cc7692016-04-26 12:56:00 -0700185 }
bsalomon72dc51c2016-04-27 06:46:23 -0700186 fInheritedKey.reset(parentCnt + styleCnt);
187 if (useParentGeoKey) {
188 // This will be the geo key.
189 parent.writeUnstyledKey(fInheritedKey.get());
190 } else {
191 // This should be (geo,path_effect).
192 memcpy(fInheritedKey.get(), parent.fInheritedKey.get(),
193 parentCnt * sizeof(uint32_t));
194 }
195 // Now turn (geo,path_effect) or (geo) into (geo,path_effect,stroke)
196 StyleKey(fInheritedKey.get() + parentCnt, parent.fStyle, stopAfterPE);
bsalomon47cc7692016-04-26 12:56:00 -0700197 }
198}
199
200GrShape::GrShape(const GrShape& that) : fType(that.fType), fStyle(that.fStyle) {
201 switch (fType) {
202 case Type::kEmpty:
203 return;
204 case Type::kRRect:
205 fRRect = that.fRRect;
206 return;
207 case Type::kPath:
208 fPath.set(*that.fPath.get());
209 return;
210 }
211 fInheritedKey.reset(that.fInheritedKey.count());
212 memcpy(fInheritedKey.get(), that.fInheritedKey.get(),
213 sizeof(uint32_t) * fInheritedKey.count());
214}
215
216GrShape::GrShape(const GrShape& parent, bool stopAfterPE) {
217 fType = Type::kEmpty;
218 SkPathEffect* pe = parent.fStyle.pathEffect();
219 const SkPath* inPath;
220 SkStrokeRec strokeRec = parent.fStyle.strokeRec();
bsalomon72dc51c2016-04-27 06:46:23 -0700221 bool appliedPE = false;
bsalomon47cc7692016-04-26 12:56:00 -0700222 if (pe) {
223 fType = Type::kPath;
224 fPath.init();
225 if (parent.fType == Type::kPath) {
226 inPath = parent.fPath.get();
227 } else {
228 inPath = fPath.get();
229 parent.asPath(fPath.get());
230 }
231 // Should we consider bounds? Would have to include in key, but it'd be nice to know
232 // if the bounds actually modified anything before including in key.
233 if (!pe->filterPath(fPath.get(), *inPath, &strokeRec, nullptr)) {
234 // Make an empty unstyled shape if filtering fails.
235 fType = Type::kEmpty;
236 fStyle = GrStyle();
237 fPath.reset();
238 return;
239 }
bsalomon72dc51c2016-04-27 06:46:23 -0700240 appliedPE = true;
bsalomon47cc7692016-04-26 12:56:00 -0700241 inPath = fPath.get();
242 } else if (stopAfterPE || !strokeRec.needToApply()) {
243 *this = parent;
244 return;
245 } else {
246 fType = Type::kPath;
247 fPath.init();
248 if (parent.fType == Type::kPath) {
249 inPath = parent.fPath.get();
250 } else {
251 inPath = fPath.get();
252 parent.asPath(fPath.get());
253 }
254 }
bsalomon72dc51c2016-04-27 06:46:23 -0700255 const GrShape* effectiveParent = &parent;
256 SkTLazy<GrShape> tmpParent;
bsalomon47cc7692016-04-26 12:56:00 -0700257 if (!stopAfterPE) {
bsalomon72dc51c2016-04-27 06:46:23 -0700258 if (appliedPE) {
259 // If the intermediate shape from just the PE is not a path then we capture that here
260 // so that we can pass the non-path parent to setInheritedKey.
261 SkRRect rrect;
262 Type parentType = AttemptToReduceFromPathImpl(*fPath.get(), &rrect, nullptr, strokeRec);
263 switch (parentType) {
264 case Type::kEmpty:
265 tmpParent.init();
266 effectiveParent = tmpParent.get();
267 break;
268 case Type::kRRect:
269 tmpParent.init(rrect, GrStyle(strokeRec, nullptr));
270 effectiveParent = tmpParent.get();
271 case Type::kPath:
272 break;
273 }
274 }
bsalomon47cc7692016-04-26 12:56:00 -0700275 strokeRec.applyToPath(fPath.get(), *inPath);
276 } else {
277 fStyle = GrStyle(strokeRec, nullptr);
278 }
bsalomon72dc51c2016-04-27 06:46:23 -0700279 this->attemptToReduceFromPath();
280 this->setInheritedKey(*effectiveParent, stopAfterPE);
bsalomon47cc7692016-04-26 12:56:00 -0700281}