• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
operator =(const GrShape & that)10 GrShape& GrShape::operator=(const GrShape& that) {
11     fStyle = that.fStyle;
12     this->changeType(that.fType, Type::kPath == that.fType ? &that.path() : nullptr);
13     switch (fType) {
14         case Type::kEmpty:
15             break;
16         case Type::kInvertedEmpty:
17             break;
18         case Type::kRRect:
19             fRRectData = that.fRRectData;
20             break;
21         case Type::kArc:
22             fArcData = that.fArcData;
23             break;
24         case Type::kLine:
25             fLineData = that.fLineData;
26             break;
27         case Type::kPath:
28             fPathData.fGenID = that.fPathData.fGenID;
29             break;
30     }
31     fInheritedKey.reset(that.fInheritedKey.count());
32     sk_careful_memcpy(fInheritedKey.get(), that.fInheritedKey.get(),
33                       sizeof(uint32_t) * fInheritedKey.count());
34     if (that.fInheritedPathForListeners.isValid()) {
35         fInheritedPathForListeners.set(*that.fInheritedPathForListeners.get());
36     } else {
37         fInheritedPathForListeners.reset();
38     }
39     return *this;
40 }
41 
flip_inversion(bool originalIsInverted,GrShape::FillInversion inversion)42 static bool flip_inversion(bool originalIsInverted, GrShape::FillInversion inversion) {
43     switch (inversion) {
44         case GrShape::FillInversion::kPreserve:
45             return false;
46         case GrShape::FillInversion::kFlip:
47             return true;
48         case GrShape::FillInversion::kForceInverted:
49             return !originalIsInverted;
50         case GrShape::FillInversion::kForceNoninverted:
51             return originalIsInverted;
52     }
53     return false;
54 }
55 
is_inverted(bool originalIsInverted,GrShape::FillInversion inversion)56 static bool is_inverted(bool originalIsInverted, GrShape::FillInversion inversion) {
57     switch (inversion) {
58         case GrShape::FillInversion::kPreserve:
59             return originalIsInverted;
60         case GrShape::FillInversion::kFlip:
61             return !originalIsInverted;
62         case GrShape::FillInversion::kForceInverted:
63             return true;
64         case GrShape::FillInversion::kForceNoninverted:
65             return false;
66     }
67     return false;
68 }
69 
MakeFilled(const GrShape & original,FillInversion inversion)70 GrShape GrShape::MakeFilled(const GrShape& original, FillInversion inversion) {
71     if (original.style().isSimpleFill() && !flip_inversion(original.inverseFilled(), inversion)) {
72         // By returning the original rather than falling through we can preserve any inherited style
73         // key. Otherwise, we wipe it out below since the style change invalidates it.
74         return original;
75     }
76     GrShape result;
77     if (original.fInheritedPathForListeners.isValid()) {
78         result.fInheritedPathForListeners.set(*original.fInheritedPathForListeners.get());
79     }
80     switch (original.fType) {
81         case Type::kRRect:
82             result.fType = original.fType;
83             result.fRRectData.fRRect = original.fRRectData.fRRect;
84             result.fRRectData.fDir = kDefaultRRectDir;
85             result.fRRectData.fStart = kDefaultRRectStart;
86             result.fRRectData.fInverted = is_inverted(original.fRRectData.fInverted, inversion);
87             break;
88         case Type::kArc:
89             result.fType = original.fType;
90             result.fArcData.fOval = original.fArcData.fOval;
91             result.fArcData.fStartAngleDegrees = original.fArcData.fStartAngleDegrees;
92             result.fArcData.fSweepAngleDegrees = original.fArcData.fSweepAngleDegrees;
93             result.fArcData.fUseCenter = original.fArcData.fUseCenter;
94             result.fArcData.fInverted = is_inverted(original.fArcData.fInverted, inversion);
95             break;
96         case Type::kLine:
97             // Lines don't fill.
98             if (is_inverted(original.fLineData.fInverted, inversion)) {
99                 result.fType = Type::kInvertedEmpty;
100             } else {
101                 result.fType = Type::kEmpty;
102             }
103             break;
104         case Type::kEmpty:
105             result.fType = is_inverted(false, inversion) ? Type::kInvertedEmpty :  Type::kEmpty;
106             break;
107         case Type::kInvertedEmpty:
108             result.fType = is_inverted(true, inversion) ? Type::kInvertedEmpty :  Type::kEmpty;
109             break;
110         case Type::kPath:
111             result.initType(Type::kPath, &original.fPathData.fPath);
112             result.fPathData.fGenID = original.fPathData.fGenID;
113             if (flip_inversion(original.fPathData.fPath.isInverseFillType(), inversion)) {
114                 result.fPathData.fPath.toggleInverseFillType();
115             }
116             if (!original.style().isSimpleFill()) {
117                 // Going from a non-filled style to fill may allow additional simplifications (e.g.
118                 // closing an open rect that wasn't closed in the original shape because it had
119                 // stroke style).
120                 result.attemptToSimplifyPath();
121             }
122             break;
123     }
124     // We don't copy the inherited key since it can contain path effect information that we just
125     // stripped.
126     return result;
127 }
128 
bounds() const129 SkRect GrShape::bounds() const {
130     // Bounds where left == bottom or top == right can indicate a line or point shape. We return
131     // inverted bounds for a truly empty shape.
132     static constexpr SkRect kInverted = SkRect::MakeLTRB(1, 1, -1, -1);
133     switch (fType) {
134         case Type::kEmpty:
135             return kInverted;
136         case Type::kInvertedEmpty:
137             return kInverted;
138         case Type::kLine: {
139             SkRect bounds;
140             if (fLineData.fPts[0].fX < fLineData.fPts[1].fX) {
141                 bounds.fLeft = fLineData.fPts[0].fX;
142                 bounds.fRight = fLineData.fPts[1].fX;
143             } else {
144                 bounds.fLeft = fLineData.fPts[1].fX;
145                 bounds.fRight = fLineData.fPts[0].fX;
146             }
147             if (fLineData.fPts[0].fY < fLineData.fPts[1].fY) {
148                 bounds.fTop = fLineData.fPts[0].fY;
149                 bounds.fBottom = fLineData.fPts[1].fY;
150             } else {
151                 bounds.fTop = fLineData.fPts[1].fY;
152                 bounds.fBottom = fLineData.fPts[0].fY;
153             }
154             return bounds;
155         }
156         case Type::kRRect:
157             return fRRectData.fRRect.getBounds();
158         case Type::kArc:
159             // Could make this less conservative by looking at angles.
160             return fArcData.fOval;
161         case Type::kPath:
162             return this->path().getBounds();
163     }
164     SK_ABORT("Unknown shape type");
165     return kInverted;
166 }
167 
styledBounds() const168 SkRect GrShape::styledBounds() const {
169     if (this->isEmpty() && !fStyle.hasNonDashPathEffect()) {
170         return SkRect::MakeEmpty();
171     }
172 
173     SkRect bounds;
174     fStyle.adjustBounds(&bounds, this->bounds());
175     return bounds;
176 }
177 
178 // If the path is small enough to be keyed from its data this returns key length, otherwise -1.
path_key_from_data_size(const SkPath & path)179 static int path_key_from_data_size(const SkPath& path) {
180     const int verbCnt = path.countVerbs();
181     if (verbCnt > GrShape::kMaxKeyFromDataVerbCnt) {
182         return -1;
183     }
184     const int pointCnt = path.countPoints();
185     const int conicWeightCnt = SkPathPriv::ConicWeightCnt(path);
186 
187     GR_STATIC_ASSERT(sizeof(SkPoint) == 2 * sizeof(uint32_t));
188     GR_STATIC_ASSERT(sizeof(SkScalar) == sizeof(uint32_t));
189     // 2 is for the verb cnt and a fill type. Each verb is a byte but we'll pad the verb data out to
190     // a uint32_t length.
191     return 2 + (SkAlign4(verbCnt) >> 2) + 2 * pointCnt + conicWeightCnt;
192 }
193 
194 // Writes the path data key into the passed pointer.
write_path_key_from_data(const SkPath & path,uint32_t * origKey)195 static void write_path_key_from_data(const SkPath& path, uint32_t* origKey) {
196     uint32_t* key = origKey;
197     // The check below should take care of negative values casted positive.
198     const int verbCnt = path.countVerbs();
199     const int pointCnt = path.countPoints();
200     const int conicWeightCnt = SkPathPriv::ConicWeightCnt(path);
201     SkASSERT(verbCnt <= GrShape::kMaxKeyFromDataVerbCnt);
202     SkASSERT(pointCnt && verbCnt);
203     *key++ = path.getFillType();
204     *key++ = verbCnt;
205     memcpy(key, SkPathPriv::VerbData(path), verbCnt * sizeof(uint8_t));
206     int verbKeySize = SkAlign4(verbCnt);
207     // pad out to uint32_t alignment using value that will stand out when debugging.
208     uint8_t* pad = reinterpret_cast<uint8_t*>(key)+ verbCnt;
209     memset(pad, 0xDE, verbKeySize - verbCnt);
210     key += verbKeySize >> 2;
211 
212     memcpy(key, SkPathPriv::PointData(path), sizeof(SkPoint) * pointCnt);
213     GR_STATIC_ASSERT(sizeof(SkPoint) == 2 * sizeof(uint32_t));
214     key += 2 * pointCnt;
215     sk_careful_memcpy(key, SkPathPriv::ConicWeightData(path), sizeof(SkScalar) * conicWeightCnt);
216     GR_STATIC_ASSERT(sizeof(SkScalar) == sizeof(uint32_t));
217     SkDEBUGCODE(key += conicWeightCnt);
218     SkASSERT(key - origKey == path_key_from_data_size(path));
219 }
220 
unstyledKeySize() const221 int GrShape::unstyledKeySize() const {
222     if (fInheritedKey.count()) {
223         return fInheritedKey.count();
224     }
225     switch (fType) {
226         case Type::kEmpty:
227             return 1;
228         case Type::kInvertedEmpty:
229             return 1;
230         case Type::kRRect:
231             SkASSERT(!fInheritedKey.count());
232             GR_STATIC_ASSERT(0 == SkRRect::kSizeInMemory % sizeof(uint32_t));
233             // + 1 for the direction, start index, and inverseness.
234             return SkRRect::kSizeInMemory / sizeof(uint32_t) + 1;
235         case Type::kArc:
236             SkASSERT(!fInheritedKey.count());
237             GR_STATIC_ASSERT(0 == sizeof(fArcData) % sizeof(uint32_t));
238             return sizeof(fArcData) / sizeof(uint32_t);
239         case Type::kLine:
240             GR_STATIC_ASSERT(2 * sizeof(uint32_t) == sizeof(SkPoint));
241             // 4 for the end points and 1 for the inverseness
242             return 5;
243         case Type::kPath: {
244             if (0 == fPathData.fGenID) {
245                 return -1;
246             }
247             int dataKeySize = path_key_from_data_size(fPathData.fPath);
248             if (dataKeySize >= 0) {
249                 return dataKeySize;
250             }
251             // The key is the path ID and fill type.
252             return 2;
253         }
254     }
255     SK_ABORT("Should never get here.");
256     return 0;
257 }
258 
writeUnstyledKey(uint32_t * key) const259 void GrShape::writeUnstyledKey(uint32_t* key) const {
260     SkASSERT(this->unstyledKeySize());
261     SkDEBUGCODE(uint32_t* origKey = key;)
262     if (fInheritedKey.count()) {
263         memcpy(key, fInheritedKey.get(), sizeof(uint32_t) * fInheritedKey.count());
264         SkDEBUGCODE(key += fInheritedKey.count();)
265     } else {
266         switch (fType) {
267             case Type::kEmpty:
268                 *key++ = 1;
269                 break;
270             case Type::kInvertedEmpty:
271                 *key++ = 2;
272                 break;
273             case Type::kRRect:
274                 fRRectData.fRRect.writeToMemory(key);
275                 key += SkRRect::kSizeInMemory / sizeof(uint32_t);
276                 *key = (fRRectData.fDir == SkPath::kCCW_Direction) ? (1 << 31) : 0;
277                 *key |= fRRectData.fInverted ? (1 << 30) : 0;
278                 *key++ |= fRRectData.fStart;
279                 SkASSERT(fRRectData.fStart < 8);
280                 break;
281             case Type::kArc:
282                 memcpy(key, &fArcData, sizeof(fArcData));
283                 key += sizeof(fArcData) / sizeof(uint32_t);
284                 break;
285             case Type::kLine:
286                 memcpy(key, fLineData.fPts, 2 * sizeof(SkPoint));
287                 key += 4;
288                 *key++ = fLineData.fInverted ? 1 : 0;
289                 break;
290             case Type::kPath: {
291                 SkASSERT(fPathData.fGenID);
292                 int dataKeySize = path_key_from_data_size(fPathData.fPath);
293                 if (dataKeySize >= 0) {
294                     write_path_key_from_data(fPathData.fPath, key);
295                     return;
296                 }
297                 *key++ = fPathData.fGenID;
298                 // We could canonicalize the fill rule for paths that don't differentiate between
299                 // even/odd or winding fill (e.g. convex).
300                 *key++ = this->path().getFillType();
301                 break;
302             }
303         }
304     }
305     SkASSERT(key - origKey == this->unstyledKeySize());
306 }
307 
setInheritedKey(const GrShape & parent,GrStyle::Apply apply,SkScalar scale)308 void GrShape::setInheritedKey(const GrShape &parent, GrStyle::Apply apply, SkScalar scale) {
309     SkASSERT(!fInheritedKey.count());
310     // If the output shape turns out to be simple, then we will just use its geometric key
311     if (Type::kPath == fType) {
312         // We want ApplyFullStyle(ApplyPathEffect(shape)) to have the same key as
313         // ApplyFullStyle(shape).
314         // The full key is structured as (geo,path_effect,stroke).
315         // If we do ApplyPathEffect we get geo,path_effect as the inherited key. If we then
316         // do ApplyFullStyle we'll memcpy geo,path_effect into the new inherited key
317         // and then append the style key (which should now be stroke only) at the end.
318         int parentCnt = parent.fInheritedKey.count();
319         bool useParentGeoKey = !parentCnt;
320         if (useParentGeoKey) {
321             parentCnt = parent.unstyledKeySize();
322             if (parentCnt < 0) {
323                 // The parent's geometry has no key so we will have no key.
324                 fPathData.fGenID = 0;
325                 return;
326             }
327         }
328         uint32_t styleKeyFlags = 0;
329         if (parent.knownToBeClosed()) {
330             styleKeyFlags |= GrStyle::kClosed_KeyFlag;
331         }
332         if (parent.asLine(nullptr, nullptr)) {
333             styleKeyFlags |= GrStyle::kNoJoins_KeyFlag;
334         }
335         int styleCnt = GrStyle::KeySize(parent.fStyle, apply, styleKeyFlags);
336         if (styleCnt < 0) {
337             // The style doesn't allow a key, set the path gen ID to 0 so that we fail when
338             // we try to get a key for the shape.
339             fPathData.fGenID = 0;
340             return;
341         }
342         fInheritedKey.reset(parentCnt + styleCnt);
343         if (useParentGeoKey) {
344             // This will be the geo key.
345             parent.writeUnstyledKey(fInheritedKey.get());
346         } else {
347             // This should be (geo,path_effect).
348             memcpy(fInheritedKey.get(), parent.fInheritedKey.get(),
349                    parentCnt * sizeof(uint32_t));
350         }
351         // Now turn (geo,path_effect) or (geo) into (geo,path_effect,stroke)
352         GrStyle::WriteKey(fInheritedKey.get() + parentCnt, parent.fStyle, apply, scale,
353                           styleKeyFlags);
354     }
355 }
356 
originalPathForListeners() const357 const SkPath* GrShape::originalPathForListeners() const {
358     if (fInheritedPathForListeners.isValid()) {
359         return fInheritedPathForListeners.get();
360     } else if (Type::kPath == fType && !fPathData.fPath.isVolatile()) {
361         return &fPathData.fPath;
362     }
363     return nullptr;
364 }
365 
addGenIDChangeListener(SkPathRef::GenIDChangeListener * listener) const366 void GrShape::addGenIDChangeListener(SkPathRef::GenIDChangeListener* listener) const {
367     if (const auto* lp = this->originalPathForListeners()) {
368         SkPathPriv::AddGenIDChangeListener(*lp, listener);
369     } else {
370         delete listener;
371     }
372 }
373 
MakeArc(const SkRect & oval,SkScalar startAngleDegrees,SkScalar sweepAngleDegrees,bool useCenter,const GrStyle & style)374 GrShape GrShape::MakeArc(const SkRect& oval, SkScalar startAngleDegrees, SkScalar sweepAngleDegrees,
375                          bool useCenter, const GrStyle& style) {
376 #ifdef SK_DISABLE_ARC_TO_LINE_TO_CHECK
377     // When this flag is set the segment mask of the path won't match GrShape's segment mask for
378     // paths. Represent this shape as a path.
379     SkPath path;
380     SkPathPriv::CreateDrawArcPath(&path, oval, startAngleDegrees, sweepAngleDegrees, useCenter,
381                                   style.isSimpleFill());
382     return GrShape(path, style);
383 #else
384     GrShape result;
385     result.changeType(Type::kArc);
386     result.fArcData.fOval = oval;
387     result.fArcData.fStartAngleDegrees = startAngleDegrees;
388     result.fArcData.fSweepAngleDegrees = sweepAngleDegrees;
389     result.fArcData.fUseCenter = useCenter;
390     result.fArcData.fInverted = false;
391     result.fStyle = style;
392     result.attemptToSimplifyArc();
393     return result;
394 #endif
395 }
396 
GrShape(const GrShape & that)397 GrShape::GrShape(const GrShape& that) : fStyle(that.fStyle) {
398     const SkPath* thatPath = Type::kPath == that.fType ? &that.fPathData.fPath : nullptr;
399     this->initType(that.fType, thatPath);
400     switch (fType) {
401         case Type::kEmpty:
402             break;
403         case Type::kInvertedEmpty:
404             break;
405         case Type::kRRect:
406             fRRectData = that.fRRectData;
407             break;
408         case Type::kArc:
409             fArcData = that.fArcData;
410             break;
411         case Type::kLine:
412             fLineData = that.fLineData;
413             break;
414         case Type::kPath:
415             fPathData.fGenID = that.fPathData.fGenID;
416             break;
417     }
418     fInheritedKey.reset(that.fInheritedKey.count());
419     sk_careful_memcpy(fInheritedKey.get(), that.fInheritedKey.get(),
420                       sizeof(uint32_t) * fInheritedKey.count());
421     if (that.fInheritedPathForListeners.isValid()) {
422         fInheritedPathForListeners.set(*that.fInheritedPathForListeners.get());
423     }
424 }
425 
GrShape(const GrShape & parent,GrStyle::Apply apply,SkScalar scale)426 GrShape::GrShape(const GrShape& parent, GrStyle::Apply apply, SkScalar scale) {
427     // TODO: Add some quantization of scale for better cache performance here or leave that up
428     // to caller?
429     // TODO: For certain shapes and stroke params we could ignore the scale. (e.g. miter or bevel
430     // stroke of a rect).
431     if (!parent.style().applies() ||
432         (GrStyle::Apply::kPathEffectOnly == apply && !parent.style().pathEffect())) {
433         this->initType(Type::kEmpty);
434         *this = parent;
435         return;
436     }
437 
438     SkPathEffect* pe = parent.fStyle.pathEffect();
439     SkTLazy<SkPath> tmpPath;
440     const GrShape* parentForKey = &parent;
441     SkTLazy<GrShape> tmpParent;
442     this->initType(Type::kPath);
443     fPathData.fGenID = 0;
444     if (pe) {
445         const SkPath* srcForPathEffect;
446         if (parent.fType == Type::kPath) {
447             srcForPathEffect = &parent.path();
448         } else {
449             srcForPathEffect = tmpPath.init();
450             parent.asPath(tmpPath.get());
451         }
452         // Should we consider bounds? Would have to include in key, but it'd be nice to know
453         // if the bounds actually modified anything before including in key.
454         SkStrokeRec strokeRec = parent.fStyle.strokeRec();
455         if (!parent.fStyle.applyPathEffectToPath(&this->path(), &strokeRec, *srcForPathEffect,
456                                                  scale)) {
457             tmpParent.init(*srcForPathEffect, GrStyle(strokeRec, nullptr));
458             *this = tmpParent.get()->applyStyle(apply, scale);
459             return;
460         }
461         // A path effect has access to change the res scale but we aren't expecting it to and it
462         // would mess up our key computation.
463         SkASSERT(scale == strokeRec.getResScale());
464         if (GrStyle::Apply::kPathEffectAndStrokeRec == apply && strokeRec.needToApply()) {
465             // The intermediate shape may not be a general path. If we we're just applying
466             // the path effect then attemptToReduceFromPath would catch it. This means that
467             // when we subsequently applied the remaining strokeRec we would have a non-path
468             // parent shape that would be used to determine the the stroked path's key.
469             // We detect that case here and change parentForKey to a temporary that represents
470             // the simpler shape so that applying both path effect and the strokerec all at
471             // once produces the same key.
472             tmpParent.init(this->path(), GrStyle(strokeRec, nullptr));
473             tmpParent.get()->setInheritedKey(parent, GrStyle::Apply::kPathEffectOnly, scale);
474             if (!tmpPath.isValid()) {
475                 tmpPath.init();
476             }
477             tmpParent.get()->asPath(tmpPath.get());
478             SkStrokeRec::InitStyle fillOrHairline;
479             // The parent shape may have simplified away the strokeRec, check for that here.
480             if (tmpParent.get()->style().applies()) {
481                 SkAssertResult(tmpParent.get()->style().applyToPath(&this->path(), &fillOrHairline,
482                                                                     *tmpPath.get(), scale));
483             } else if (tmpParent.get()->style().isSimpleFill()) {
484                 fillOrHairline = SkStrokeRec::kFill_InitStyle;
485             } else {
486                 SkASSERT(tmpParent.get()->style().isSimpleHairline());
487                 fillOrHairline = SkStrokeRec::kHairline_InitStyle;
488             }
489             fStyle.resetToInitStyle(fillOrHairline);
490             parentForKey = tmpParent.get();
491         } else {
492             fStyle = GrStyle(strokeRec, nullptr);
493         }
494     } else {
495         const SkPath* srcForParentStyle;
496         if (parent.fType == Type::kPath) {
497             srcForParentStyle = &parent.path();
498         } else {
499             srcForParentStyle = tmpPath.init();
500             parent.asPath(tmpPath.get());
501         }
502         SkStrokeRec::InitStyle fillOrHairline;
503         SkASSERT(parent.fStyle.applies());
504         SkASSERT(!parent.fStyle.pathEffect());
505         SkAssertResult(parent.fStyle.applyToPath(&this->path(), &fillOrHairline, *srcForParentStyle,
506                                                  scale));
507         fStyle.resetToInitStyle(fillOrHairline);
508     }
509     if (parent.fInheritedPathForListeners.isValid()) {
510         fInheritedPathForListeners.set(*parent.fInheritedPathForListeners.get());
511     } else if (Type::kPath == parent.fType && !parent.fPathData.fPath.isVolatile()) {
512         fInheritedPathForListeners.set(parent.fPathData.fPath);
513     }
514     this->attemptToSimplifyPath();
515     this->setInheritedKey(*parentForKey, apply, scale);
516 }
517 
attemptToSimplifyPath()518 void GrShape::attemptToSimplifyPath() {
519     SkRect rect;
520     SkRRect rrect;
521     SkPath::Direction rrectDir;
522     unsigned rrectStart;
523     bool inverted = this->path().isInverseFillType();
524     SkPoint pts[2];
525     if (this->path().isEmpty()) {
526         // Dashing ignores inverseness skbug.com/5421.
527         this->changeType(inverted && !this->style().isDashed() ? Type::kInvertedEmpty
528                                                                : Type::kEmpty);
529     } else if (this->path().isLine(pts)) {
530         this->changeType(Type::kLine);
531         fLineData.fPts[0] = pts[0];
532         fLineData.fPts[1] = pts[1];
533         fLineData.fInverted = inverted;
534     } else if (SkPathPriv::IsRRect(this->path(), &rrect, &rrectDir, &rrectStart)) {
535         this->changeType(Type::kRRect);
536         fRRectData.fRRect = rrect;
537         fRRectData.fDir = rrectDir;
538         fRRectData.fStart = rrectStart;
539         fRRectData.fInverted = inverted;
540         SkASSERT(!fRRectData.fRRect.isEmpty());
541     } else if (SkPathPriv::IsOval(this->path(), &rect, &rrectDir, &rrectStart)) {
542         this->changeType(Type::kRRect);
543         fRRectData.fRRect.setOval(rect);
544         fRRectData.fDir = rrectDir;
545         fRRectData.fInverted = inverted;
546         // convert from oval indexing to rrect indexiing.
547         fRRectData.fStart = 2 * rrectStart;
548     } else if (SkPathPriv::IsSimpleClosedRect(this->path(), &rect, &rrectDir, &rrectStart)) {
549         this->changeType(Type::kRRect);
550         // When there is a path effect we restrict rect detection to the narrower API that
551         // gives us the starting position. Otherwise, we will retry with the more aggressive
552         // isRect().
553         fRRectData.fRRect.setRect(rect);
554         fRRectData.fInverted = inverted;
555         fRRectData.fDir = rrectDir;
556         // convert from rect indexing to rrect indexiing.
557         fRRectData.fStart = 2 * rrectStart;
558     } else if (!this->style().hasPathEffect()) {
559         bool closed;
560         if (this->path().isRect(&rect, &closed, nullptr)) {
561             if (closed || this->style().isSimpleFill()) {
562                 this->changeType(Type::kRRect);
563                 fRRectData.fRRect.setRect(rect);
564                 // Since there is no path effect the dir and start index is immaterial.
565                 fRRectData.fDir = kDefaultRRectDir;
566                 fRRectData.fStart = kDefaultRRectStart;
567                 // There isn't dashing so we will have to preserver inverseness.
568                 fRRectData.fInverted = inverted;
569             }
570         }
571     }
572     if (Type::kPath != fType) {
573         fInheritedKey.reset(0);
574         // Whenever we simplify to a non-path, break the chain so we no longer refer to the
575         // original path. This prevents attaching genID listeners to temporary paths created when
576         // drawing simple shapes.
577         fInheritedPathForListeners.reset();
578         if (Type::kRRect == fType) {
579             this->attemptToSimplifyRRect();
580         } else if (Type::kLine == fType) {
581             this->attemptToSimplifyLine();
582         }
583     } else {
584         if (fInheritedKey.count() || this->path().isVolatile()) {
585             fPathData.fGenID = 0;
586         } else {
587             fPathData.fGenID = this->path().getGenerationID();
588         }
589         if (!this->style().hasNonDashPathEffect()) {
590             if (this->style().strokeRec().getStyle() == SkStrokeRec::kStroke_Style ||
591                 this->style().strokeRec().getStyle() == SkStrokeRec::kHairline_Style) {
592                 // Stroke styles don't differentiate between winding and even/odd.
593                 // Moreover, dashing ignores inverseness (skbug.com/5421)
594                 bool inverse = !this->style().isDashed() && this->path().isInverseFillType();
595                 if (inverse) {
596                     this->path().setFillType(kDefaultPathInverseFillType);
597                 } else {
598                     this->path().setFillType(kDefaultPathFillType);
599                 }
600             } else if (this->path().isConvex()) {
601                 // There is no distinction between even/odd and non-zero winding count for convex
602                 // paths.
603                 if (this->path().isInverseFillType()) {
604                     this->path().setFillType(kDefaultPathInverseFillType);
605                 } else {
606                     this->path().setFillType(kDefaultPathFillType);
607                 }
608             }
609         }
610     }
611 }
612 
attemptToSimplifyRRect()613 void GrShape::attemptToSimplifyRRect() {
614     SkASSERT(Type::kRRect == fType);
615     SkASSERT(!fInheritedKey.count());
616     if (fRRectData.fRRect.isEmpty()) {
617         // An empty filled rrect is equivalent to a filled empty path with inversion preserved.
618         if (fStyle.isSimpleFill()) {
619             fType = fRRectData.fInverted ? Type::kInvertedEmpty : Type::kEmpty;
620             fStyle = GrStyle::SimpleFill();
621             return;
622         }
623         // Dashing a rrect with no width or height is equivalent to filling an emtpy path.
624         // When skbug.com/7387 is fixed this should be modified or removed as a dashed zero length
625         // line  will produce cap geometry if the effect begins in an "on" interval.
626         if (fStyle.isDashed() && !fRRectData.fRRect.width() && !fRRectData.fRRect.height()) {
627             // Dashing ignores the inverseness (currently). skbug.com/5421.
628             fType = Type::kEmpty;
629             fStyle = GrStyle::SimpleFill();
630             return;
631         }
632     }
633     if (!this->style().hasPathEffect()) {
634         fRRectData.fDir = kDefaultRRectDir;
635         fRRectData.fStart = kDefaultRRectStart;
636     } else if (fStyle.isDashed()) {
637         // Dashing ignores the inverseness (currently). skbug.com/5421
638         fRRectData.fInverted = false;
639         // Possible TODO here: Check whether the dash results in a single arc or line.
640     }
641     // Turn a stroke-and-filled miter rect into a filled rect. TODO: more rrect stroke shortcuts.
642     if (!fStyle.hasPathEffect() &&
643         fStyle.strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style &&
644         fStyle.strokeRec().getJoin() == SkPaint::kMiter_Join &&
645         fStyle.strokeRec().getMiter() >= SK_ScalarSqrt2 &&
646         fRRectData.fRRect.isRect()) {
647         SkScalar r = fStyle.strokeRec().getWidth() / 2;
648         fRRectData.fRRect = SkRRect::MakeRect(fRRectData.fRRect.rect().makeOutset(r, r));
649         fStyle = GrStyle::SimpleFill();
650     }
651 }
652 
attemptToSimplifyLine()653 void GrShape::attemptToSimplifyLine() {
654     SkASSERT(Type::kLine == fType);
655     SkASSERT(!fInheritedKey.count());
656     if (fStyle.isDashed()) {
657         bool allOffsZero = true;
658         for (int i = 1; i < fStyle.dashIntervalCnt() && allOffsZero; i += 2) {
659             allOffsZero = !fStyle.dashIntervals()[i];
660         }
661         if (allOffsZero && this->attemptToSimplifyStrokedLineToRRect()) {
662             return;
663         }
664         // Dashing ignores inverseness.
665         fLineData.fInverted = false;
666         return;
667     } else if (fStyle.hasPathEffect()) {
668         return;
669     }
670     if (fStyle.strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style) {
671         // Make stroke + fill be stroke since the fill is empty.
672         SkStrokeRec rec = fStyle.strokeRec();
673         rec.setStrokeStyle(fStyle.strokeRec().getWidth(), false);
674         fStyle = GrStyle(rec, nullptr);
675     }
676     if (fStyle.isSimpleFill()) {
677         this->changeType(fLineData.fInverted ? Type::kInvertedEmpty : Type::kEmpty);
678         return;
679     }
680     if (fStyle.strokeRec().getStyle() == SkStrokeRec::kStroke_Style &&
681         this->attemptToSimplifyStrokedLineToRRect()) {
682         return;
683     }
684     // Only path effects could care about the order of the points. Otherwise canonicalize
685     // the point order.
686     SkPoint* pts = fLineData.fPts;
687     if (pts[1].fY < pts[0].fY || (pts[1].fY == pts[0].fY && pts[1].fX < pts[0].fX)) {
688         SkTSwap(pts[0], pts[1]);
689     }
690 }
691 
attemptToSimplifyArc()692 void GrShape::attemptToSimplifyArc() {
693     SkASSERT(fType == Type::kArc);
694     SkASSERT(!fArcData.fInverted);
695     if (fArcData.fOval.isEmpty() || !fArcData.fSweepAngleDegrees) {
696         this->changeType(Type::kEmpty);
697         return;
698     }
699 
700     // Assuming no path effect, a filled, stroked, hairline, or stroke-and-filled arc that traverses
701     // the full circle and doesn't use the center point is an oval. Unless it has square or round
702     // caps. They may protrude out of the oval. Round caps can't protrude out of a circle but we're
703     // ignoring that for now.
704     if (fStyle.isSimpleFill() || (!fStyle.pathEffect() && !fArcData.fUseCenter &&
705                                   fStyle.strokeRec().getCap() == SkPaint::kButt_Cap)) {
706         if (fArcData.fSweepAngleDegrees >= 360.f || fArcData.fSweepAngleDegrees <= -360.f) {
707             auto oval = fArcData.fOval;
708             this->changeType(Type::kRRect);
709             this->fRRectData.fRRect.setOval(oval);
710             this->fRRectData.fDir = kDefaultRRectDir;
711             this->fRRectData.fStart = kDefaultRRectStart;
712             this->fRRectData.fInverted = false;
713             return;
714         }
715     }
716     if (!fStyle.pathEffect()) {
717         // Canonicalize the arc such that the start is always in [0, 360) and the sweep is always
718         // positive.
719         if (fArcData.fSweepAngleDegrees < 0) {
720             fArcData.fStartAngleDegrees = fArcData.fStartAngleDegrees + fArcData.fSweepAngleDegrees;
721             fArcData.fSweepAngleDegrees = -fArcData.fSweepAngleDegrees;
722         }
723     }
724     if (this->fArcData.fStartAngleDegrees < 0 || this->fArcData.fStartAngleDegrees >= 360.f) {
725         this->fArcData.fStartAngleDegrees = SkScalarMod(this->fArcData.fStartAngleDegrees, 360.f);
726     }
727     // Possible TODOs here: Look at whether dash pattern results in a single dash and convert to
728     // non-dashed stroke. Stroke and fill can be fill if circular and no path effect. Just stroke
729     // could as well if the stroke fills the center.
730 }
731 
attemptToSimplifyStrokedLineToRRect()732 bool GrShape::attemptToSimplifyStrokedLineToRRect() {
733     SkASSERT(Type::kLine == fType);
734     SkASSERT(fStyle.strokeRec().getStyle() == SkStrokeRec::kStroke_Style);
735 
736     SkRect rect;
737     SkVector outset;
738     // If we allowed a rotation angle for rrects we could capture all cases here.
739     if (fLineData.fPts[0].fY == fLineData.fPts[1].fY) {
740         rect.fLeft = SkTMin(fLineData.fPts[0].fX, fLineData.fPts[1].fX);
741         rect.fRight = SkTMax(fLineData.fPts[0].fX, fLineData.fPts[1].fX);
742         rect.fTop = rect.fBottom = fLineData.fPts[0].fY;
743         outset.fY = fStyle.strokeRec().getWidth() / 2.f;
744         outset.fX = SkPaint::kButt_Cap == fStyle.strokeRec().getCap() ? 0.f : outset.fY;
745     } else if (fLineData.fPts[0].fX == fLineData.fPts[1].fX) {
746         rect.fTop = SkTMin(fLineData.fPts[0].fY, fLineData.fPts[1].fY);
747         rect.fBottom = SkTMax(fLineData.fPts[0].fY, fLineData.fPts[1].fY);
748         rect.fLeft = rect.fRight = fLineData.fPts[0].fX;
749         outset.fX = fStyle.strokeRec().getWidth() / 2.f;
750         outset.fY = SkPaint::kButt_Cap == fStyle.strokeRec().getCap() ? 0.f : outset.fX;
751     } else {
752         return false;
753     }
754     rect.outset(outset.fX, outset.fY);
755     if (rect.isEmpty()) {
756         this->changeType(Type::kEmpty);
757         fStyle = GrStyle::SimpleFill();
758         return true;
759     }
760     SkRRect rrect;
761     if (fStyle.strokeRec().getCap() == SkPaint::kRound_Cap) {
762         SkASSERT(outset.fX == outset.fY);
763         rrect = SkRRect::MakeRectXY(rect, outset.fX, outset.fY);
764     } else {
765         rrect = SkRRect::MakeRect(rect);
766     }
767     bool inverted = fLineData.fInverted && !fStyle.hasPathEffect();
768     this->changeType(Type::kRRect);
769     fRRectData.fRRect = rrect;
770     fRRectData.fInverted = inverted;
771     fRRectData.fDir = kDefaultRRectDir;
772     fRRectData.fStart = kDefaultRRectStart;
773     fStyle = GrStyle::SimpleFill();
774     return true;
775 }
776