blob: 5ffd32d46de2f36b61f8093d614e8e408624f49d [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) {
bsalomon47cc7692016-04-26 12:56:00 -070011 fStyle = that.fStyle;
bsalomon728b0f72016-06-27 10:00:19 -070012 this->changeType(that.fType, Type::kPath == that.fType ? &that.path() : nullptr);
bsalomon47cc7692016-04-26 12:56:00 -070013 switch (fType) {
14 case Type::kEmpty:
bsalomon47cc7692016-04-26 12:56:00 -070015 break;
16 case Type::kRRect:
bsalomon0a0f67e2016-06-28 11:56:42 -070017 fRRectData = that.fRRectData;
18 break;
19 case Type::kLine:
20 fLineData = that.fLineData;
bsalomon47cc7692016-04-26 12:56:00 -070021 break;
22 case Type::kPath:
bsalomon728b0f72016-06-27 10:00:19 -070023 fPathData.fGenID = that.fPathData.fGenID;
bsalomon47cc7692016-04-26 12:56:00 -070024 break;
25 }
26 fInheritedKey.reset(that.fInheritedKey.count());
benjaminwagnerd9cca4a2016-05-04 11:06:19 -070027 sk_careful_memcpy(fInheritedKey.get(), that.fInheritedKey.get(),
28 sizeof(uint32_t) * fInheritedKey.count());
bsalomon47cc7692016-04-26 12:56:00 -070029 return *this;
30}
31
bsalomon0a0f67e2016-06-28 11:56:42 -070032SkRect GrShape::bounds() const {
bsalomon9fb42032016-05-13 09:23:38 -070033 static constexpr SkRect kEmpty = SkRect::MakeEmpty();
34 switch (fType) {
35 case Type::kEmpty:
36 return kEmpty;
bsalomon0a0f67e2016-06-28 11:56:42 -070037 case Type::kLine: {
38 SkRect bounds;
39 if (fLineData.fPts[0].fX < fLineData.fPts[1].fX) {
40 bounds.fLeft = fLineData.fPts[0].fX;
41 bounds.fRight = fLineData.fPts[1].fX;
42 } else {
43 bounds.fLeft = fLineData.fPts[1].fX;
44 bounds.fRight = fLineData.fPts[0].fX;
45 }
46 if (fLineData.fPts[0].fY < fLineData.fPts[1].fY) {
47 bounds.fTop = fLineData.fPts[0].fY;
48 bounds.fBottom = fLineData.fPts[1].fY;
49 } else {
50 bounds.fTop = fLineData.fPts[1].fY;
51 bounds.fBottom = fLineData.fPts[0].fY;
52 }
53 return bounds;
54 }
bsalomon9fb42032016-05-13 09:23:38 -070055 case Type::kRRect:
bsalomon728b0f72016-06-27 10:00:19 -070056 return fRRectData.fRRect.getBounds();
bsalomon9fb42032016-05-13 09:23:38 -070057 case Type::kPath:
bsalomon728b0f72016-06-27 10:00:19 -070058 return this->path().getBounds();
bsalomon9fb42032016-05-13 09:23:38 -070059 }
60 SkFAIL("Unknown shape type");
61 return kEmpty;
62}
63
bsalomon0a0f67e2016-06-28 11:56:42 -070064SkRect GrShape::styledBounds() const {
bsalomon9fb42032016-05-13 09:23:38 -070065 if (Type::kEmpty == fType && !fStyle.hasNonDashPathEffect()) {
bsalomon0a0f67e2016-06-28 11:56:42 -070066 return SkRect::MakeEmpty();
bsalomon9fb42032016-05-13 09:23:38 -070067 }
bsalomon0a0f67e2016-06-28 11:56:42 -070068 SkRect bounds;
69 fStyle.adjustBounds(&bounds, this->bounds());
70 return bounds;
bsalomon9fb42032016-05-13 09:23:38 -070071}
72
bsalomon47cc7692016-04-26 12:56:00 -070073int GrShape::unstyledKeySize() const {
74 if (fInheritedKey.count()) {
75 return fInheritedKey.count();
76 }
77 switch (fType) {
78 case Type::kEmpty:
79 return 1;
80 case Type::kRRect:
81 SkASSERT(!fInheritedKey.count());
82 SkASSERT(0 == SkRRect::kSizeInMemory % sizeof(uint32_t));
bsalomon70493962016-06-10 08:05:14 -070083 // + 1 for the direction, start index, and inverseness.
bsalomonee295642016-06-06 14:01:25 -070084 return SkRRect::kSizeInMemory / sizeof(uint32_t) + 1;
bsalomon0a0f67e2016-06-28 11:56:42 -070085 case Type::kLine:
86 GR_STATIC_ASSERT(2 * sizeof(uint32_t) == sizeof(SkPoint));
87 // 4 for the end points and 1 for the inverseness
88 return 5;
bsalomon47cc7692016-04-26 12:56:00 -070089 case Type::kPath:
bsalomon728b0f72016-06-27 10:00:19 -070090 if (0 == fPathData.fGenID) {
bsalomon47cc7692016-04-26 12:56:00 -070091 return -1;
92 } else {
bsalomon70493962016-06-10 08:05:14 -070093 // The key is the path ID and fill type.
94 return 2;
bsalomon47cc7692016-04-26 12:56:00 -070095 }
96 }
97 SkFAIL("Should never get here.");
98 return 0;
99}
100
101void GrShape::writeUnstyledKey(uint32_t* key) const {
102 SkASSERT(this->unstyledKeySize());
103 SkDEBUGCODE(uint32_t* origKey = key;)
104 if (fInheritedKey.count()) {
105 memcpy(key, fInheritedKey.get(), sizeof(uint32_t) * fInheritedKey.count());
106 SkDEBUGCODE(key += fInheritedKey.count();)
107 } else {
108 switch (fType) {
109 case Type::kEmpty:
110 *key++ = 1;
111 break;
112 case Type::kRRect:
bsalomon728b0f72016-06-27 10:00:19 -0700113 fRRectData.fRRect.writeToMemory(key);
bsalomon47cc7692016-04-26 12:56:00 -0700114 key += SkRRect::kSizeInMemory / sizeof(uint32_t);
bsalomon728b0f72016-06-27 10:00:19 -0700115 *key = (fRRectData.fDir == SkPath::kCCW_Direction) ? (1 << 31) : 0;
116 *key |= fRRectData.fInverted ? (1 << 30) : 0;
117 *key++ |= fRRectData.fStart;
118 SkASSERT(fRRectData.fStart < 8);
bsalomon47cc7692016-04-26 12:56:00 -0700119 break;
bsalomon0a0f67e2016-06-28 11:56:42 -0700120 case Type::kLine:
121 memcpy(key, fLineData.fPts, 2 * sizeof(SkPoint));
122 key += 4;
123 *key++ = fLineData.fInverted ? 1 : 0;
124 break;
bsalomon47cc7692016-04-26 12:56:00 -0700125 case Type::kPath:
bsalomon728b0f72016-06-27 10:00:19 -0700126 SkASSERT(fPathData.fGenID);
127 *key++ = fPathData.fGenID;
bsalomon70493962016-06-10 08:05:14 -0700128 // We could canonicalize the fill rule for paths that don't differentiate between
129 // even/odd or winding fill (e.g. convex).
bsalomon728b0f72016-06-27 10:00:19 -0700130 *key++ = this->path().getFillType();
bsalomon47cc7692016-04-26 12:56:00 -0700131 break;
132 }
133 }
134 SkASSERT(key - origKey == this->unstyledKeySize());
135}
136
bsalomon97fd2d42016-05-09 13:02:01 -0700137void GrShape::setInheritedKey(const GrShape &parent, GrStyle::Apply apply, SkScalar scale) {
bsalomon47cc7692016-04-26 12:56:00 -0700138 SkASSERT(!fInheritedKey.count());
139 // If the output shape turns out to be simple, then we will just use its geometric key
140 if (Type::kPath == fType) {
141 // We want ApplyFullStyle(ApplyPathEffect(shape)) to have the same key as
142 // ApplyFullStyle(shape).
143 // The full key is structured as (geo,path_effect,stroke).
144 // If we do ApplyPathEffect we get get,path_effect as the inherited key. If we then
145 // do ApplyFullStyle we'll memcpy geo,path_effect into the new inherited key
146 // and then append the style key (which should now be stroke only) at the end.
147 int parentCnt = parent.fInheritedKey.count();
148 bool useParentGeoKey = !parentCnt;
149 if (useParentGeoKey) {
150 parentCnt = parent.unstyledKeySize();
bsalomon72dc51c2016-04-27 06:46:23 -0700151 if (parentCnt < 0) {
152 // The parent's geometry has no key so we will have no key.
bsalomon728b0f72016-06-27 10:00:19 -0700153 fPathData.fGenID = 0;
bsalomon72dc51c2016-04-27 06:46:23 -0700154 return;
155 }
bsalomon47cc7692016-04-26 12:56:00 -0700156 }
bsalomon06077562016-05-04 13:50:29 -0700157 uint32_t styleKeyFlags = 0;
158 if (parent.knownToBeClosed()) {
159 styleKeyFlags |= GrStyle::kClosed_KeyFlag;
160 }
161 int styleCnt = GrStyle::KeySize(parent.fStyle, apply, styleKeyFlags);
bsalomon47cc7692016-04-26 12:56:00 -0700162 if (styleCnt < 0) {
bsalomon93f66bc2016-06-21 08:35:49 -0700163 // 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 -0700164 // we try to get a key for the shape.
bsalomon728b0f72016-06-27 10:00:19 -0700165 fPathData.fGenID = 0;
bsalomon72dc51c2016-04-27 06:46:23 -0700166 return;
bsalomon47cc7692016-04-26 12:56:00 -0700167 }
bsalomon72dc51c2016-04-27 06:46:23 -0700168 fInheritedKey.reset(parentCnt + styleCnt);
169 if (useParentGeoKey) {
170 // This will be the geo key.
171 parent.writeUnstyledKey(fInheritedKey.get());
172 } else {
173 // This should be (geo,path_effect).
174 memcpy(fInheritedKey.get(), parent.fInheritedKey.get(),
175 parentCnt * sizeof(uint32_t));
176 }
177 // Now turn (geo,path_effect) or (geo) into (geo,path_effect,stroke)
bsalomon97fd2d42016-05-09 13:02:01 -0700178 GrStyle::WriteKey(fInheritedKey.get() + parentCnt, parent.fStyle, apply, scale,
179 styleKeyFlags);
bsalomon47cc7692016-04-26 12:56:00 -0700180 }
181}
182
bsalomon728b0f72016-06-27 10:00:19 -0700183GrShape::GrShape(const GrShape& that) : fStyle(that.fStyle) {
184 const SkPath* thatPath = Type::kPath == that.fType ? &that.fPathData.fPath : nullptr;
185 this->initType(that.fType, thatPath);
bsalomon47cc7692016-04-26 12:56:00 -0700186 switch (fType) {
187 case Type::kEmpty:
bsalomon93f66bc2016-06-21 08:35:49 -0700188 break;
bsalomon47cc7692016-04-26 12:56:00 -0700189 case Type::kRRect:
bsalomon0a0f67e2016-06-28 11:56:42 -0700190 fRRectData = that.fRRectData;
191 break;
192 case Type::kLine:
193 fLineData = that.fLineData;
bsalomon93f66bc2016-06-21 08:35:49 -0700194 break;
bsalomon47cc7692016-04-26 12:56:00 -0700195 case Type::kPath:
bsalomon728b0f72016-06-27 10:00:19 -0700196 fPathData.fGenID = that.fPathData.fGenID;
bsalomon93f66bc2016-06-21 08:35:49 -0700197 break;
bsalomon47cc7692016-04-26 12:56:00 -0700198 }
199 fInheritedKey.reset(that.fInheritedKey.count());
bsalomon93f66bc2016-06-21 08:35:49 -0700200 sk_careful_memcpy(fInheritedKey.get(), that.fInheritedKey.get(),
201 sizeof(uint32_t) * fInheritedKey.count());
bsalomon47cc7692016-04-26 12:56:00 -0700202}
203
bsalomon97fd2d42016-05-09 13:02:01 -0700204GrShape::GrShape(const GrShape& parent, GrStyle::Apply apply, SkScalar scale) {
205 // TODO: Add some quantization of scale for better cache performance here or leave that up
206 // to caller?
207 // TODO: For certain shapes and stroke params we could ignore the scale. (e.g. miter or bevel
208 // stroke of a rect).
bsalomonfb083272016-05-04 08:27:41 -0700209 if (!parent.style().applies() ||
210 (GrStyle::Apply::kPathEffectOnly == apply && !parent.style().pathEffect())) {
bsalomon728b0f72016-06-27 10:00:19 -0700211 this->initType(Type::kEmpty);
bsalomonfb083272016-05-04 08:27:41 -0700212 *this = parent;
213 return;
214 }
215
bsalomon47cc7692016-04-26 12:56:00 -0700216 SkPathEffect* pe = parent.fStyle.pathEffect();
bsalomonfb083272016-05-04 08:27:41 -0700217 SkTLazy<SkPath> tmpPath;
218 const GrShape* parentForKey = &parent;
219 SkTLazy<GrShape> tmpParent;
bsalomon728b0f72016-06-27 10:00:19 -0700220 this->initType(Type::kPath);
221 fPathData.fGenID = 0;
bsalomon47cc7692016-04-26 12:56:00 -0700222 if (pe) {
bsalomon728b0f72016-06-27 10:00:19 -0700223 const SkPath* srcForPathEffect;
bsalomon47cc7692016-04-26 12:56:00 -0700224 if (parent.fType == Type::kPath) {
bsalomon728b0f72016-06-27 10:00:19 -0700225 srcForPathEffect = &parent.path();
bsalomon47cc7692016-04-26 12:56:00 -0700226 } else {
bsalomonfb083272016-05-04 08:27:41 -0700227 srcForPathEffect = tmpPath.init();
228 parent.asPath(tmpPath.get());
bsalomon47cc7692016-04-26 12:56:00 -0700229 }
230 // Should we consider bounds? Would have to include in key, but it'd be nice to know
231 // if the bounds actually modified anything before including in key.
bsalomonfb083272016-05-04 08:27:41 -0700232 SkStrokeRec strokeRec = parent.fStyle.strokeRec();
bsalomon728b0f72016-06-27 10:00:19 -0700233 if (!parent.fStyle.applyPathEffectToPath(&this->path(), &strokeRec, *srcForPathEffect,
bsalomon398e3f42016-06-13 10:22:48 -0700234 scale)) {
bsalomond6723842016-06-07 12:20:15 -0700235 // If the path effect fails then we continue as though there was no path effect.
236 // If the original was a rrect that we couldn't canonicalize because of the path
237 // effect, then do so now.
bsalomon728b0f72016-06-27 10:00:19 -0700238 if (parent.fType == Type::kRRect && (parent.fRRectData.fDir != kDefaultRRectDir ||
239 parent.fRRectData.fStart != kDefaultRRectStart)) {
bsalomond6723842016-06-07 12:20:15 -0700240 SkASSERT(srcForPathEffect == tmpPath.get());
241 tmpPath.get()->reset();
bsalomon728b0f72016-06-27 10:00:19 -0700242 tmpPath.get()->addRRect(parent.fRRectData.fRRect, kDefaultRRectDir,
243 kDefaultRRectDir);
bsalomond6723842016-06-07 12:20:15 -0700244 }
bsalomon728b0f72016-06-27 10:00:19 -0700245 this->path() = *srcForPathEffect;
bsalomon47cc7692016-04-26 12:56:00 -0700246 }
bsalomon97fd2d42016-05-09 13:02:01 -0700247 // A path effect has access to change the res scale but we aren't expecting it to and it
248 // would mess up our key computation.
249 SkASSERT(scale == strokeRec.getResScale());
bsalomon1b28c1a2016-06-20 12:28:17 -0700250 if (GrStyle::Apply::kPathEffectAndStrokeRec == apply && strokeRec.needToApply()) {
251 // The intermediate shape may not be a general path. If we we're just applying
252 // the path effect then attemptToReduceFromPath would catch it. This means that
253 // when we subsequently applied the remaining strokeRec we would have a non-path
254 // parent shape that would be used to determine the the stroked path's key.
255 // We detect that case here and change parentForKey to a temporary that represents
256 // the simpler shape so that applying both path effect and the strokerec all at
257 // once produces the same key.
bsalomon728b0f72016-06-27 10:00:19 -0700258 tmpParent.init(this->path(), GrStyle(strokeRec, nullptr));
bsalomon1b28c1a2016-06-20 12:28:17 -0700259 tmpParent.get()->setInheritedKey(parent, GrStyle::Apply::kPathEffectOnly, scale);
260 if (!tmpPath.isValid()) {
261 tmpPath.init();
bsalomon72dc51c2016-04-27 06:46:23 -0700262 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700263 tmpParent.get()->asPath(tmpPath.get());
264 SkStrokeRec::InitStyle fillOrHairline;
bsalomon728b0f72016-06-27 10:00:19 -0700265 SkAssertResult(tmpParent.get()->style().applyToPath(&this->path(), &fillOrHairline,
bsalomon1b28c1a2016-06-20 12:28:17 -0700266 *tmpPath.get(), scale));
267 fStyle.resetToInitStyle(fillOrHairline);
268 parentForKey = tmpParent.get();
bsalomonfb083272016-05-04 08:27:41 -0700269 } else {
270 fStyle = GrStyle(strokeRec, nullptr);
bsalomon72dc51c2016-04-27 06:46:23 -0700271 }
bsalomon47cc7692016-04-26 12:56:00 -0700272 } else {
bsalomon97fd2d42016-05-09 13:02:01 -0700273 const SkPath* srcForParentStyle;
bsalomonfb083272016-05-04 08:27:41 -0700274 if (parent.fType == Type::kPath) {
bsalomon728b0f72016-06-27 10:00:19 -0700275 srcForParentStyle = &parent.path();
bsalomonfb083272016-05-04 08:27:41 -0700276 } else {
bsalomon97fd2d42016-05-09 13:02:01 -0700277 srcForParentStyle = tmpPath.init();
bsalomonfb083272016-05-04 08:27:41 -0700278 parent.asPath(tmpPath.get());
279 }
bsalomon97fd2d42016-05-09 13:02:01 -0700280 SkStrokeRec::InitStyle fillOrHairline;
281 SkASSERT(parent.fStyle.applies());
282 SkASSERT(!parent.fStyle.pathEffect());
bsalomon728b0f72016-06-27 10:00:19 -0700283 SkAssertResult(parent.fStyle.applyToPath(&this->path(), &fillOrHairline, *srcForParentStyle,
bsalomon97fd2d42016-05-09 13:02:01 -0700284 scale));
285 fStyle.resetToInitStyle(fillOrHairline);
bsalomon47cc7692016-04-26 12:56:00 -0700286 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700287 this->attemptToSimplifyPath();
bsalomon97fd2d42016-05-09 13:02:01 -0700288 this->setInheritedKey(*parentForKey, apply, scale);
bsalomon47cc7692016-04-26 12:56:00 -0700289}
bsalomonee295642016-06-06 14:01:25 -0700290
bsalomon1b28c1a2016-06-20 12:28:17 -0700291void GrShape::attemptToSimplifyPath() {
bsalomonee295642016-06-06 14:01:25 -0700292 SkRect rect;
bsalomon728b0f72016-06-27 10:00:19 -0700293 SkRRect rrect;
294 SkPath::Direction rrectDir;
295 unsigned rrectStart;
296 bool inverted = this->path().isInverseFillType();
bsalomon0a0f67e2016-06-28 11:56:42 -0700297 SkPoint pts[2];
bsalomon728b0f72016-06-27 10:00:19 -0700298 if (this->path().isEmpty()) {
299 this->changeType(Type::kEmpty);
bsalomon0a0f67e2016-06-28 11:56:42 -0700300 } else if (this->path().isLine(pts)) {
301 this->changeType(Type::kLine);
302 fLineData.fPts[0] = pts[0];
303 fLineData.fPts[1] = pts[1];
304 fLineData.fInverted = inverted;
bsalomon728b0f72016-06-27 10:00:19 -0700305 } else if (this->path().isRRect(&rrect, &rrectDir, &rrectStart)) {
306 this->changeType(Type::kRRect);
307 fRRectData.fRRect = rrect;
308 fRRectData.fDir = rrectDir;
309 fRRectData.fStart = rrectStart;
310 fRRectData.fInverted = inverted;
bsalomon1b28c1a2016-06-20 12:28:17 -0700311 // Currently SkPath does not acknowledge that empty, rect, or oval subtypes as rrects.
bsalomon728b0f72016-06-27 10:00:19 -0700312 SkASSERT(!fRRectData.fRRect.isEmpty());
313 SkASSERT(fRRectData.fRRect.getType() != SkRRect::kRect_Type);
314 SkASSERT(fRRectData.fRRect.getType() != SkRRect::kOval_Type);
315 } else if (this->path().isOval(&rect, &rrectDir, &rrectStart)) {
316 this->changeType(Type::kRRect);
317 fRRectData.fRRect.setOval(rect);
318 fRRectData.fDir = rrectDir;
319 fRRectData.fInverted = inverted;
bsalomon1b28c1a2016-06-20 12:28:17 -0700320 // convert from oval indexing to rrect indexiing.
bsalomon728b0f72016-06-27 10:00:19 -0700321 fRRectData.fStart = 2 * rrectStart;
322 } else if (SkPathPriv::IsSimpleClosedRect(this->path(), &rect, &rrectDir, &rrectStart)) {
323 this->changeType(Type::kRRect);
bsalomon1b28c1a2016-06-20 12:28:17 -0700324 // When there is a path effect we restrict rect detection to the narrower API that
325 // gives us the starting position. Otherwise, we will retry with the more aggressive
326 // isRect().
bsalomon728b0f72016-06-27 10:00:19 -0700327 fRRectData.fRRect.setRect(rect);
328 fRRectData.fInverted = inverted;
329 fRRectData.fDir = rrectDir;
bsalomon1b28c1a2016-06-20 12:28:17 -0700330 // convert from rect indexing to rrect indexiing.
bsalomon728b0f72016-06-27 10:00:19 -0700331 fRRectData.fStart = 2 * rrectStart;
bsalomon1b28c1a2016-06-20 12:28:17 -0700332 } else if (!this->style().hasPathEffect()) {
bsalomonee295642016-06-06 14:01:25 -0700333 bool closed;
bsalomon728b0f72016-06-27 10:00:19 -0700334 if (this->path().isRect(&rect, &closed, nullptr)) {
bsalomon1b28c1a2016-06-20 12:28:17 -0700335 if (closed || this->style().isSimpleFill()) {
bsalomon728b0f72016-06-27 10:00:19 -0700336 this->changeType(Type::kRRect);
337 fRRectData.fRRect.setRect(rect);
bsalomonee295642016-06-06 14:01:25 -0700338 // Since there is no path effect the dir and start index is immaterial.
bsalomon728b0f72016-06-27 10:00:19 -0700339 fRRectData.fDir = kDefaultRRectDir;
340 fRRectData.fStart = kDefaultRRectStart;
bsalomon1b28c1a2016-06-20 12:28:17 -0700341 // There isn't dashing so we will have to preserver inverseness.
bsalomon728b0f72016-06-27 10:00:19 -0700342 fRRectData.fInverted = inverted;
bsalomonee295642016-06-06 14:01:25 -0700343 }
344 }
345 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700346 if (Type::kPath != fType) {
bsalomon1b28c1a2016-06-20 12:28:17 -0700347 fInheritedKey.reset(0);
348 if (Type::kRRect == fType) {
349 this->attemptToSimplifyRRect();
bsalomon0a0f67e2016-06-28 11:56:42 -0700350 } else if (Type::kLine == fType) {
351 this->attemptToSimplifyLine();
bsalomon1b28c1a2016-06-20 12:28:17 -0700352 }
bsalomon93f66bc2016-06-21 08:35:49 -0700353 } else {
bsalomon728b0f72016-06-27 10:00:19 -0700354 if (fInheritedKey.count() || this->path().isVolatile()) {
355 fPathData.fGenID = 0;
bsalomon93f66bc2016-06-21 08:35:49 -0700356 } else {
bsalomon728b0f72016-06-27 10:00:19 -0700357 fPathData.fGenID = this->path().getGenerationID();
bsalomon93f66bc2016-06-21 08:35:49 -0700358 }
359 if (this->style().isSimpleFill()) {
bsalomon0a0f67e2016-06-28 11:56:42 -0700360 this->path().close();
361 this->path().setIsVolatile(true);
bsalomon93f66bc2016-06-21 08:35:49 -0700362 }
bsalomona4817af2016-06-23 11:48:26 -0700363 if (!this->style().hasNonDashPathEffect()) {
364 if (this->style().strokeRec().getStyle() == SkStrokeRec::kStroke_Style ||
365 this->style().strokeRec().getStyle() == SkStrokeRec::kHairline_Style) {
366 // Stroke styles don't differentiate between winding and even/odd.
367 // Moreover, dashing ignores inverseness (skbug.com/5421)
bsalomon728b0f72016-06-27 10:00:19 -0700368 bool inverse = !this->style().isDashed() && this->path().isInverseFillType();
bsalomona4817af2016-06-23 11:48:26 -0700369 if (inverse) {
bsalomon728b0f72016-06-27 10:00:19 -0700370 this->path().setFillType(kDefaultPathInverseFillType);
bsalomona4817af2016-06-23 11:48:26 -0700371 } else {
bsalomon728b0f72016-06-27 10:00:19 -0700372 this->path().setFillType(kDefaultPathFillType);
bsalomona4817af2016-06-23 11:48:26 -0700373 }
bsalomon728b0f72016-06-27 10:00:19 -0700374 } else if (this->path().isConvex()) {
bsalomona4817af2016-06-23 11:48:26 -0700375 // There is no distinction between even/odd and non-zero winding count for convex
376 // paths.
bsalomon728b0f72016-06-27 10:00:19 -0700377 if (this->path().isInverseFillType()) {
378 this->path().setFillType(kDefaultPathInverseFillType);
bsalomona4817af2016-06-23 11:48:26 -0700379 } else {
bsalomon728b0f72016-06-27 10:00:19 -0700380 this->path().setFillType(kDefaultPathFillType);
bsalomona4817af2016-06-23 11:48:26 -0700381 }
bsalomon93f66bc2016-06-21 08:35:49 -0700382 }
383 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700384 }
385}
386
387void GrShape::attemptToSimplifyRRect() {
388 SkASSERT(Type::kRRect == fType);
389 SkASSERT(!fInheritedKey.count());
bsalomon728b0f72016-06-27 10:00:19 -0700390 if (fRRectData.fRRect.isEmpty()) {
bsalomon1b28c1a2016-06-20 12:28:17 -0700391 fType = Type::kEmpty;
392 return;
393 }
394 if (!this->style().hasPathEffect()) {
bsalomon728b0f72016-06-27 10:00:19 -0700395 fRRectData.fDir = kDefaultRRectDir;
396 fRRectData.fStart = kDefaultRRectStart;
bsalomon1b28c1a2016-06-20 12:28:17 -0700397 } else if (fStyle.isDashed()) {
398 // Dashing ignores the inverseness (currently). skbug.com/5421
bsalomon728b0f72016-06-27 10:00:19 -0700399 fRRectData.fInverted = false;
bsalomon1b28c1a2016-06-20 12:28:17 -0700400 }
bsalomonee295642016-06-06 14:01:25 -0700401}
bsalomon0a0f67e2016-06-28 11:56:42 -0700402
403void GrShape::attemptToSimplifyLine() {
404 if (fStyle.isSimpleFill() && !fLineData.fInverted) {
405 this->changeType(Type::kEmpty);
406 } else {
407 // Only path effects could care about the order of the points. Otherwise canonicalize
408 // the point order
409 if (!fStyle.hasPathEffect()) {
410 SkPoint* pts = fLineData.fPts;
411 if (pts[1].fY < pts[0].fY || (pts[1].fY == pts[0].fY && pts[1].fX < pts[0].fX)) {
412 SkTSwap(pts[0], pts[1]);
413 }
414 } else if (fStyle.isDashed()) {
415 // Dashing ignores inverseness.
416 fLineData.fInverted = false;
417 }
418 }
419}