blob: 0fb339555ecf0dc4f6609c012adbbcaf33517f02 [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;
bsalomonee295642016-06-06 14:01:25 -070025 fRRectDir = that.fRRectDir;
26 fRRectStart = that.fRRectStart;
bsalomon70493962016-06-10 08:05:14 -070027 fRRectIsInverted = that.fRRectIsInverted;
bsalomon47cc7692016-04-26 12:56:00 -070028 break;
29 case Type::kPath:
30 if (wasPath) {
31 *fPath.get() = *that.fPath.get();
32 } else {
33 fPath.set(*that.fPath.get());
34 }
bsalomon93f66bc2016-06-21 08:35:49 -070035 fPathGenID = that.fPathGenID;
bsalomon47cc7692016-04-26 12:56:00 -070036 break;
37 }
38 fInheritedKey.reset(that.fInheritedKey.count());
benjaminwagnerd9cca4a2016-05-04 11:06:19 -070039 sk_careful_memcpy(fInheritedKey.get(), that.fInheritedKey.get(),
40 sizeof(uint32_t) * fInheritedKey.count());
bsalomon47cc7692016-04-26 12:56:00 -070041 return *this;
42}
43
bsalomon9fb42032016-05-13 09:23:38 -070044const SkRect& GrShape::bounds() const {
45 static constexpr SkRect kEmpty = SkRect::MakeEmpty();
46 switch (fType) {
47 case Type::kEmpty:
48 return kEmpty;
49 case Type::kRRect:
50 return fRRect.getBounds();
51 case Type::kPath:
52 return fPath.get()->getBounds();
53 }
54 SkFAIL("Unknown shape type");
55 return kEmpty;
56}
57
58void GrShape::styledBounds(SkRect* bounds) const {
59 if (Type::kEmpty == fType && !fStyle.hasNonDashPathEffect()) {
60 *bounds = SkRect::MakeEmpty();
61 } else {
62 fStyle.adjustBounds(bounds, this->bounds());
63 }
64}
65
bsalomon47cc7692016-04-26 12:56:00 -070066int GrShape::unstyledKeySize() const {
67 if (fInheritedKey.count()) {
68 return fInheritedKey.count();
69 }
70 switch (fType) {
71 case Type::kEmpty:
72 return 1;
73 case Type::kRRect:
74 SkASSERT(!fInheritedKey.count());
75 SkASSERT(0 == SkRRect::kSizeInMemory % sizeof(uint32_t));
bsalomon70493962016-06-10 08:05:14 -070076 // + 1 for the direction, start index, and inverseness.
bsalomonee295642016-06-06 14:01:25 -070077 return SkRRect::kSizeInMemory / sizeof(uint32_t) + 1;
bsalomon47cc7692016-04-26 12:56:00 -070078 case Type::kPath:
bsalomon93f66bc2016-06-21 08:35:49 -070079 if (0 == fPathGenID) {
bsalomon47cc7692016-04-26 12:56:00 -070080 return -1;
81 } else {
bsalomon70493962016-06-10 08:05:14 -070082 // The key is the path ID and fill type.
83 return 2;
bsalomon47cc7692016-04-26 12:56:00 -070084 }
85 }
86 SkFAIL("Should never get here.");
87 return 0;
88}
89
90void GrShape::writeUnstyledKey(uint32_t* key) const {
91 SkASSERT(this->unstyledKeySize());
92 SkDEBUGCODE(uint32_t* origKey = key;)
93 if (fInheritedKey.count()) {
94 memcpy(key, fInheritedKey.get(), sizeof(uint32_t) * fInheritedKey.count());
95 SkDEBUGCODE(key += fInheritedKey.count();)
96 } else {
97 switch (fType) {
98 case Type::kEmpty:
99 *key++ = 1;
100 break;
101 case Type::kRRect:
102 fRRect.writeToMemory(key);
103 key += SkRRect::kSizeInMemory / sizeof(uint32_t);
bsalomonee295642016-06-06 14:01:25 -0700104 *key = (fRRectDir == SkPath::kCCW_Direction) ? (1 << 31) : 0;
bsalomon70493962016-06-10 08:05:14 -0700105 *key |= fRRectIsInverted ? (1 << 30) : 0;
bsalomonee295642016-06-06 14:01:25 -0700106 *key++ |= fRRectStart;
107 SkASSERT(fRRectStart < 8);
bsalomon47cc7692016-04-26 12:56:00 -0700108 break;
109 case Type::kPath:
bsalomon93f66bc2016-06-21 08:35:49 -0700110 SkASSERT(fPathGenID);
111 *key++ = fPathGenID;
bsalomon70493962016-06-10 08:05:14 -0700112 // We could canonicalize the fill rule for paths that don't differentiate between
113 // even/odd or winding fill (e.g. convex).
114 *key++ = fPath.get()->getFillType();
bsalomon47cc7692016-04-26 12:56:00 -0700115 break;
116 }
117 }
118 SkASSERT(key - origKey == this->unstyledKeySize());
119}
120
bsalomon97fd2d42016-05-09 13:02:01 -0700121void GrShape::setInheritedKey(const GrShape &parent, GrStyle::Apply apply, SkScalar scale) {
bsalomon47cc7692016-04-26 12:56:00 -0700122 SkASSERT(!fInheritedKey.count());
123 // If the output shape turns out to be simple, then we will just use its geometric key
124 if (Type::kPath == fType) {
125 // We want ApplyFullStyle(ApplyPathEffect(shape)) to have the same key as
126 // ApplyFullStyle(shape).
127 // The full key is structured as (geo,path_effect,stroke).
128 // If we do ApplyPathEffect we get get,path_effect as the inherited key. If we then
129 // do ApplyFullStyle we'll memcpy geo,path_effect into the new inherited key
130 // and then append the style key (which should now be stroke only) at the end.
131 int parentCnt = parent.fInheritedKey.count();
132 bool useParentGeoKey = !parentCnt;
133 if (useParentGeoKey) {
134 parentCnt = parent.unstyledKeySize();
bsalomon72dc51c2016-04-27 06:46:23 -0700135 if (parentCnt < 0) {
136 // The parent's geometry has no key so we will have no key.
bsalomon93f66bc2016-06-21 08:35:49 -0700137 fPathGenID = 0;
bsalomon72dc51c2016-04-27 06:46:23 -0700138 return;
139 }
bsalomon47cc7692016-04-26 12:56:00 -0700140 }
bsalomon06077562016-05-04 13:50:29 -0700141 uint32_t styleKeyFlags = 0;
142 if (parent.knownToBeClosed()) {
143 styleKeyFlags |= GrStyle::kClosed_KeyFlag;
144 }
145 int styleCnt = GrStyle::KeySize(parent.fStyle, apply, styleKeyFlags);
bsalomon47cc7692016-04-26 12:56:00 -0700146 if (styleCnt < 0) {
bsalomon93f66bc2016-06-21 08:35:49 -0700147 // The style doesn't allow a key, set the path gen ID to 0 so that we fail when
bsalomon47cc7692016-04-26 12:56:00 -0700148 // we try to get a key for the shape.
bsalomon93f66bc2016-06-21 08:35:49 -0700149 fPathGenID = 0;
bsalomon72dc51c2016-04-27 06:46:23 -0700150 return;
bsalomon47cc7692016-04-26 12:56:00 -0700151 }
bsalomon72dc51c2016-04-27 06:46:23 -0700152 fInheritedKey.reset(parentCnt + styleCnt);
153 if (useParentGeoKey) {
154 // This will be the geo key.
155 parent.writeUnstyledKey(fInheritedKey.get());
156 } else {
157 // This should be (geo,path_effect).
158 memcpy(fInheritedKey.get(), parent.fInheritedKey.get(),
159 parentCnt * sizeof(uint32_t));
160 }
161 // Now turn (geo,path_effect) or (geo) into (geo,path_effect,stroke)
bsalomon97fd2d42016-05-09 13:02:01 -0700162 GrStyle::WriteKey(fInheritedKey.get() + parentCnt, parent.fStyle, apply, scale,
163 styleKeyFlags);
bsalomon47cc7692016-04-26 12:56:00 -0700164 }
165}
166
167GrShape::GrShape(const GrShape& that) : fType(that.fType), fStyle(that.fStyle) {
168 switch (fType) {
169 case Type::kEmpty:
bsalomon93f66bc2016-06-21 08:35:49 -0700170 break;
bsalomon47cc7692016-04-26 12:56:00 -0700171 case Type::kRRect:
172 fRRect = that.fRRect;
bsalomon70493962016-06-10 08:05:14 -0700173 fRRectDir = that.fRRectDir;
174 fRRectStart = that.fRRectStart;
175 fRRectIsInverted = that.fRRectIsInverted;
bsalomon93f66bc2016-06-21 08:35:49 -0700176 break;
bsalomon47cc7692016-04-26 12:56:00 -0700177 case Type::kPath:
178 fPath.set(*that.fPath.get());
bsalomon93f66bc2016-06-21 08:35:49 -0700179 fPathGenID = that.fPathGenID;
180 break;
bsalomon47cc7692016-04-26 12:56:00 -0700181 }
182 fInheritedKey.reset(that.fInheritedKey.count());
bsalomon93f66bc2016-06-21 08:35:49 -0700183 sk_careful_memcpy(fInheritedKey.get(), that.fInheritedKey.get(),
184 sizeof(uint32_t) * fInheritedKey.count());
bsalomon47cc7692016-04-26 12:56:00 -0700185}
186
bsalomon97fd2d42016-05-09 13:02:01 -0700187GrShape::GrShape(const GrShape& parent, GrStyle::Apply apply, SkScalar scale) {
188 // TODO: Add some quantization of scale for better cache performance here or leave that up
189 // to caller?
190 // TODO: For certain shapes and stroke params we could ignore the scale. (e.g. miter or bevel
191 // stroke of a rect).
bsalomonfb083272016-05-04 08:27:41 -0700192 if (!parent.style().applies() ||
193 (GrStyle::Apply::kPathEffectOnly == apply && !parent.style().pathEffect())) {
194 fType = Type::kEmpty;
195 *this = parent;
196 return;
197 }
198
bsalomon47cc7692016-04-26 12:56:00 -0700199 SkPathEffect* pe = parent.fStyle.pathEffect();
bsalomonfb083272016-05-04 08:27:41 -0700200 SkTLazy<SkPath> tmpPath;
201 const GrShape* parentForKey = &parent;
202 SkTLazy<GrShape> tmpParent;
203 fType = Type::kPath;
204 fPath.init();
bsalomon47cc7692016-04-26 12:56:00 -0700205 if (pe) {
bsalomonfb083272016-05-04 08:27:41 -0700206 SkPath* srcForPathEffect;
bsalomon47cc7692016-04-26 12:56:00 -0700207 if (parent.fType == Type::kPath) {
bsalomonfb083272016-05-04 08:27:41 -0700208 srcForPathEffect = parent.fPath.get();
bsalomon47cc7692016-04-26 12:56:00 -0700209 } else {
bsalomonfb083272016-05-04 08:27:41 -0700210 srcForPathEffect = tmpPath.init();
211 parent.asPath(tmpPath.get());
bsalomon47cc7692016-04-26 12:56:00 -0700212 }
213 // Should we consider bounds? Would have to include in key, but it'd be nice to know
214 // if the bounds actually modified anything before including in key.
bsalomonfb083272016-05-04 08:27:41 -0700215 SkStrokeRec strokeRec = parent.fStyle.strokeRec();
bsalomon398e3f42016-06-13 10:22:48 -0700216 if (!parent.fStyle.applyPathEffectToPath(fPath.get(), &strokeRec, *srcForPathEffect,
217 scale)) {
bsalomond6723842016-06-07 12:20:15 -0700218 // If the path effect fails then we continue as though there was no path effect.
219 // If the original was a rrect that we couldn't canonicalize because of the path
220 // effect, then do so now.
221 if (parent.fType == Type::kRRect && (parent.fRRectDir != kDefaultRRectDir ||
222 parent.fRRectStart != kDefaultRRectStart)) {
223 SkASSERT(srcForPathEffect == tmpPath.get());
224 tmpPath.get()->reset();
225 tmpPath.get()->addRRect(parent.fRRect, kDefaultRRectDir, kDefaultRRectDir);
226 }
227 *fPath.get() = *srcForPathEffect;
bsalomon47cc7692016-04-26 12:56:00 -0700228 }
bsalomon97fd2d42016-05-09 13:02:01 -0700229 // A path effect has access to change the res scale but we aren't expecting it to and it
230 // would mess up our key computation.
231 SkASSERT(scale == strokeRec.getResScale());
bsalomon1b28c1a2016-06-20 12:28:17 -0700232 if (GrStyle::Apply::kPathEffectAndStrokeRec == apply && strokeRec.needToApply()) {
233 // The intermediate shape may not be a general path. If we we're just applying
234 // the path effect then attemptToReduceFromPath would catch it. This means that
235 // when we subsequently applied the remaining strokeRec we would have a non-path
236 // parent shape that would be used to determine the the stroked path's key.
237 // We detect that case here and change parentForKey to a temporary that represents
238 // the simpler shape so that applying both path effect and the strokerec all at
239 // once produces the same key.
240 tmpParent.init(*fPath.get(), GrStyle(strokeRec, nullptr));
241 tmpParent.get()->setInheritedKey(parent, GrStyle::Apply::kPathEffectOnly, scale);
242 if (!tmpPath.isValid()) {
243 tmpPath.init();
bsalomon72dc51c2016-04-27 06:46:23 -0700244 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700245 tmpParent.get()->asPath(tmpPath.get());
246 SkStrokeRec::InitStyle fillOrHairline;
247 SkAssertResult(tmpParent.get()->style().applyToPath(fPath.get(), &fillOrHairline,
248 *tmpPath.get(), scale));
249 fStyle.resetToInitStyle(fillOrHairline);
250 parentForKey = tmpParent.get();
bsalomonfb083272016-05-04 08:27:41 -0700251 } else {
252 fStyle = GrStyle(strokeRec, nullptr);
bsalomon72dc51c2016-04-27 06:46:23 -0700253 }
bsalomon47cc7692016-04-26 12:56:00 -0700254 } else {
bsalomon97fd2d42016-05-09 13:02:01 -0700255 const SkPath* srcForParentStyle;
bsalomonfb083272016-05-04 08:27:41 -0700256 if (parent.fType == Type::kPath) {
bsalomon97fd2d42016-05-09 13:02:01 -0700257 srcForParentStyle = parent.fPath.get();
bsalomonfb083272016-05-04 08:27:41 -0700258 } else {
bsalomon97fd2d42016-05-09 13:02:01 -0700259 srcForParentStyle = tmpPath.init();
bsalomonfb083272016-05-04 08:27:41 -0700260 parent.asPath(tmpPath.get());
261 }
bsalomon97fd2d42016-05-09 13:02:01 -0700262 SkStrokeRec::InitStyle fillOrHairline;
263 SkASSERT(parent.fStyle.applies());
264 SkASSERT(!parent.fStyle.pathEffect());
265 SkAssertResult(parent.fStyle.applyToPath(fPath.get(), &fillOrHairline, *srcForParentStyle,
266 scale));
267 fStyle.resetToInitStyle(fillOrHairline);
bsalomon47cc7692016-04-26 12:56:00 -0700268 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700269 this->attemptToSimplifyPath();
bsalomon97fd2d42016-05-09 13:02:01 -0700270 this->setInheritedKey(*parentForKey, apply, scale);
bsalomon47cc7692016-04-26 12:56:00 -0700271}
bsalomonee295642016-06-06 14:01:25 -0700272
bsalomon1b28c1a2016-06-20 12:28:17 -0700273void GrShape::attemptToSimplifyPath() {
274 SkASSERT(Type::kPath == fType);
bsalomonee295642016-06-06 14:01:25 -0700275 SkRect rect;
bsalomon1b28c1a2016-06-20 12:28:17 -0700276 if (fPath.get()->isEmpty()) {
277 fType = Type::kEmpty;
278 } else if (fPath.get()->isRRect(&fRRect, &fRRectDir, &fRRectStart)) {
279 // Currently SkPath does not acknowledge that empty, rect, or oval subtypes as rrects.
280 SkASSERT(!fRRect.isEmpty());
281 SkASSERT(fRRect.getType() != SkRRect::kRect_Type);
282 SkASSERT(fRRect.getType() != SkRRect::kOval_Type);
283 fRRectIsInverted = fPath.get()->isInverseFillType();
284 fType = Type::kRRect;
285 } else if (fPath.get()->isOval(&rect, &fRRectDir, &fRRectStart)) {
286 fRRect.setOval(rect);
287 fRRectIsInverted = fPath.get()->isInverseFillType();
288 // convert from oval indexing to rrect indexiing.
289 fRRectStart *= 2;
290 fType = Type::kRRect;
291 } else if (SkPathPriv::IsSimpleClosedRect(*fPath.get(), &rect, &fRRectDir, &fRRectStart)) {
292 // When there is a path effect we restrict rect detection to the narrower API that
293 // gives us the starting position. Otherwise, we will retry with the more aggressive
294 // isRect().
295 fRRect.setRect(rect);
296 fRRectIsInverted = fPath.get()->isInverseFillType();
297 // convert from rect indexing to rrect indexiing.
298 fRRectStart *= 2;
299 fType = Type::kRRect;
300 } else if (!this->style().hasPathEffect()) {
bsalomonee295642016-06-06 14:01:25 -0700301 bool closed;
bsalomon1b28c1a2016-06-20 12:28:17 -0700302 if (fPath.get()->isRect(&rect, &closed, nullptr)) {
303 if (closed || this->style().isSimpleFill()) {
304 fRRect.setRect(rect);
bsalomonee295642016-06-06 14:01:25 -0700305 // Since there is no path effect the dir and start index is immaterial.
bsalomon1b28c1a2016-06-20 12:28:17 -0700306 fRRectDir = kDefaultRRectDir;
307 fRRectStart = kDefaultRRectStart;
308 // There isn't dashing so we will have to preserver inverseness.
309 fRRectIsInverted = fPath.get()->isInverseFillType();
310 fType = Type::kRRect;
bsalomonee295642016-06-06 14:01:25 -0700311 }
312 }
313 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700314 if (Type::kPath != fType) {
315 fPath.reset();
316 fInheritedKey.reset(0);
317 if (Type::kRRect == fType) {
318 this->attemptToSimplifyRRect();
319 }
bsalomon93f66bc2016-06-21 08:35:49 -0700320 } else {
321 if (fInheritedKey.count() || fPath.get()->isVolatile()) {
322 fPathGenID = 0;
323 } else {
324 fPathGenID = fPath.get()->getGenerationID();
325 }
326 if (this->style().isSimpleFill()) {
327 // Filled paths are treated as though all their contours were closed.
328 // Since SkPath doesn't track individual contours, this will only close the last. :(
329 // There is no point in closing lines, though, since they loose their line-ness.
330 if (!fPath.get()->isLine(nullptr)) {
331 fPath.get()->close();
332 fPath.get()->setIsVolatile(true);
333 }
334 }
335 if (fPath.get()->isConvex()) {
336 // There is no distinction between even/odd and non-zero winding count for convex
337 // paths.
338 if (fPath.get()->isInverseFillType()) {
339 fPath.get()->setFillType(SkPath::kInverseEvenOdd_FillType);
340 } else {
341 fPath.get()->setFillType(SkPath::kEvenOdd_FillType);
342 }
343 }
344 if (this->style().isDashed()) {
345 // Dashing ignores inverseness (skbug.com/5421)
346 switch (fPath.get()->getFillType()) {
347 case SkPath::kWinding_FillType:
348 case SkPath::kEvenOdd_FillType:
349 break;
350 case SkPath::kInverseWinding_FillType:
351 fPath.get()->setFillType(SkPath::kWinding_FillType);
352 break;
353 case SkPath::kInverseEvenOdd_FillType:
354 fPath.get()->setFillType(SkPath::kEvenOdd_FillType);
355 break;
356 }
357 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700358 }
359}
360
361void GrShape::attemptToSimplifyRRect() {
362 SkASSERT(Type::kRRect == fType);
363 SkASSERT(!fInheritedKey.count());
364 if (fRRect.isEmpty()) {
365 fType = Type::kEmpty;
366 return;
367 }
368 if (!this->style().hasPathEffect()) {
369 fRRectDir = kDefaultRRectDir;
370 fRRectStart = kDefaultRRectStart;
371 } else if (fStyle.isDashed()) {
372 // Dashing ignores the inverseness (currently). skbug.com/5421
373 fRRectIsInverted = false;
374 }
bsalomonee295642016-06-06 14:01:25 -0700375}