OLD | NEW |
---|---|
(Empty) | |
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 #include "GrShape.h" | |
9 | |
10 GrShape& 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.~SkPath(); | |
18 } | |
19 break; | |
20 case Type::kRRect: | |
21 if (wasPath) { | |
22 fPath.~SkPath(); | |
23 } | |
24 fRRect = that.fRRect; | |
25 break; | |
26 case Type::kPath: | |
27 if (wasPath) { | |
28 fPath = that.fPath; | |
29 } else { | |
30 new (&fPath) SkPath(that.fPath); | |
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 | |
40 int 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.isVolatile()) { | |
53 return -1; | |
54 } else { | |
55 return 1; | |
56 } | |
57 } | |
58 SkFAIL(); | |
59 return 0; | |
60 } | |
61 | |
62 void 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.isVolatile()); | |
79 *key++ = fPath.getGenerationID(); | |
80 break; | |
81 } | |
82 } | |
83 SkASSERT(key - origKey == this->unstyledKeySize()); | |
84 } | |
85 | |
86 int 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 | |
108 void 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); | |
robertphillips
2016/04/24 23:00:44
I'm not sure what this comment is saying
bsalomon
2016/04/25 13:32:26
I'll just rm it. I think it came in when I lifted
| |
125 // Enable the line below if fields are added after dashing. | |
126 SkDEBUGCODE(i += count); | |
127 } else { | |
128 SkASSERT(!style.pathEffect()); | |
129 } | |
130 | |
131 if (!stopAfterPE && style.strokeRec().needToApply()) { | |
132 enum { | |
133 kStyleBits = 2, | |
134 kJoinBits = 2, | |
135 kCapBits = 32 - kStyleBits - kJoinBits, | |
136 | |
137 kJoinShift = kStyleBits, | |
138 kCapShift = kJoinShift + kJoinBits, | |
139 | |
robertphillips
2016/04/24 23:00:44
rm this space ?
bsalomon
2016/04/25 13:32:26
Done.
| |
140 }; | |
141 GR_STATIC_ASSERT(SkStrokeRec::kStyleCount <= (1 << kStyleBits)); | |
142 GR_STATIC_ASSERT(SkPaint::kJoinCount <= (1 << kJoinBits)); | |
143 GR_STATIC_ASSERT(SkPaint::kCapCount <= (1 << kCapBits)); | |
144 key[i++] = style.strokeRec().getStyle() | | |
145 style.strokeRec().getJoin() << kJoinShift | | |
146 style.strokeRec().getCap() << kCapShift; | |
147 | |
148 SkScalar scalar; | |
149 // Miter limit only affects miter joins | |
150 scalar = SkPaint::kMiter_Join == style.strokeRec().getJoin() | |
151 ? style.strokeRec().getMiter() | |
152 : -1.f; | |
153 memcpy(&key[i++], &scalar, sizeof(scalar)); | |
154 | |
155 scalar = style.strokeRec().getWidth(); | |
156 memcpy(&key[i++], &scalar, sizeof(scalar)); | |
157 } | |
158 SkASSERT(StyleKeySize(style, stopAfterPE) == i); | |
159 } | |
160 | |
161 void GrShape::setInheritedKey(const GrShape &parent, bool stopAfterPE){ | |
162 SkASSERT(!fInheritedKey.count()); | |
163 // If the output shape turns out to be simple, then we will just use its geo metric key | |
164 if (Type::kPath == fType) { | |
165 // We want ApplyFullStyle(ApplyPathEffect(shape)) to have the same key a s | |
166 // ApplyFullStyle(shape). | |
167 // The full key is structured as geo,path_effect,stroke. | |
robertphillips
2016/04/25 12:07:06
get get, -> get geo, ?
Maybe a paren or "" delimi
bsalomon
2016/04/25 13:32:26
Done.
| |
168 // If we do ApplyPathEffect we get get,path_effect as the inherited key. If we then | |
robertphillips
2016/04/25 12:07:06
as into -> into ?
bsalomon
2016/04/25 13:32:26
Done.
| |
169 // do ApplyFullStyle we'll memcpy geo,path_effect as into the new inheri ted key | |
170 // and then append the style (which should now be stroke only) key at th e end. | |
171 int parentCnt = parent.fInheritedKey.count(); | |
robertphillips
2016/04/25 12:07:06
rm extra ' ' ?
bsalomon
2016/04/25 13:32:26
Done.
| |
172 bool useParentGeoKey = !parentCnt ; | |
173 if (useParentGeoKey) { | |
174 parentCnt = parent.unstyledKeySize(); | |
175 } | |
176 int styleCnt = StyleKeySize(parent.fStyle, stopAfterPE); | |
177 if (styleCnt < 0) { | |
178 // The style doesn't allow a key, set the path to volatile so that w e fail when | |
179 // we try to get a key for the shape. | |
180 fPath.setIsVolatile(true); | |
181 } else { | |
182 fInheritedKey.reset(parentCnt + styleCnt); | |
183 if (useParentGeoKey) { | |
184 // This will be the geo key. | |
185 parent.writeUnstyledKey(fInheritedKey.get()); | |
186 } else { | |
187 // This should be geo,path_effect | |
188 memcpy(fInheritedKey.get(), parent.fInheritedKey.get(), | |
189 parentCnt * sizeof(uint32_t)); | |
190 } | |
191 // Now turn (geo,path_effect) or (geo) into (geo,path_effect,stroke) | |
192 StyleKey(fInheritedKey.get() + parentCnt, parent.fStyle, stopAfterPE ); | |
193 } | |
194 } | |
195 } | |
196 | |
197 GrShape::GrShape(const GrShape& that) : fType(that.fType), fStyle(that.fStyle) { | |
198 switch (fType) { | |
199 case Type::kEmpty: | |
200 return; | |
201 case Type::kRRect: | |
202 fRRect = that.fRRect; | |
203 return; | |
204 case Type::kPath: | |
205 new (&fPath)SkPath(that.fPath); | |
206 return; | |
207 } | |
208 fInheritedKey.reset(that.fInheritedKey.count()); | |
209 memcpy(fInheritedKey.get(), that.fInheritedKey.get(), | |
210 sizeof(uint32_t) * fInheritedKey.count()); | |
211 } | |
212 | |
213 GrShape::GrShape(const GrShape& parent, bool stopAfterPE) { | |
214 fType = Type::kEmpty; | |
215 SkPathEffect* pe = parent.fStyle.pathEffect(); | |
216 const SkPath* inPath; | |
217 SkStrokeRec strokeRec = parent.fStyle.strokeRec(); | |
218 if (pe) { | |
219 fType = Type::kPath; | |
220 new (&fPath) SkPath(); | |
221 if (parent.fType == Type::kPath) { | |
222 inPath = &parent.fPath; | |
223 } else { | |
224 inPath = &fPath; | |
225 parent.asPath(&fPath); | |
226 } | |
227 // Should we consider bounds? Would have to include in key, but it'd be nice to know | |
228 // if the bounds actually modified anything before including in key. | |
229 if (!pe->filterPath(&fPath, *inPath, &strokeRec, nullptr)) { | |
230 // Make an empty unstyled shape if filtering fails. | |
231 fType = Type::kEmpty; | |
232 fStyle = GrStyle(); | |
233 fPath.~SkPath(); | |
robertphillips
2016/04/25 12:07:06
won't falling through here be bad ?
bsalomon
2016/04/25 13:32:26
Yes, added return. I'll add a unit test for this i
| |
234 } | |
235 inPath = &fPath; | |
236 } else if (stopAfterPE || !strokeRec.needToApply()) { | |
237 *this = parent; | |
238 return; | |
239 } else { | |
240 fType = Type::kPath; | |
241 new (&fPath) SkPath(); | |
242 if (parent.fType == Type::kPath) { | |
243 inPath = &parent.fPath; | |
244 } else { | |
245 inPath = &fPath; | |
246 parent.asPath(&fPath); | |
247 } | |
248 } | |
249 if (!stopAfterPE) { | |
250 strokeRec.applyToPath(&fPath, *inPath); | |
251 } else { | |
252 fStyle = GrStyle(strokeRec, nullptr); | |
253 } | |
254 this->setInheritedKey(parent, stopAfterPE); | |
255 } | |
OLD | NEW |