blob: 65c982571d48bf7110bcce1a2d91883402703655 [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 {
bsalomon0ae36a22016-07-18 07:31:13 -070033 // Bounds where left == bottom or top == right can indicate a line or point shape. We return
34 // inverted bounds for a truly empty shape.
35 static constexpr SkRect kInverted = SkRect::MakeLTRB(1, 1, -1, -1);
bsalomon9fb42032016-05-13 09:23:38 -070036 switch (fType) {
37 case Type::kEmpty:
bsalomon0ae36a22016-07-18 07:31:13 -070038 return kInverted;
bsalomon0a0f67e2016-06-28 11:56:42 -070039 case Type::kLine: {
40 SkRect bounds;
41 if (fLineData.fPts[0].fX < fLineData.fPts[1].fX) {
42 bounds.fLeft = fLineData.fPts[0].fX;
43 bounds.fRight = fLineData.fPts[1].fX;
44 } else {
45 bounds.fLeft = fLineData.fPts[1].fX;
46 bounds.fRight = fLineData.fPts[0].fX;
47 }
48 if (fLineData.fPts[0].fY < fLineData.fPts[1].fY) {
49 bounds.fTop = fLineData.fPts[0].fY;
50 bounds.fBottom = fLineData.fPts[1].fY;
51 } else {
52 bounds.fTop = fLineData.fPts[1].fY;
53 bounds.fBottom = fLineData.fPts[0].fY;
54 }
55 return bounds;
56 }
bsalomon9fb42032016-05-13 09:23:38 -070057 case Type::kRRect:
bsalomon728b0f72016-06-27 10:00:19 -070058 return fRRectData.fRRect.getBounds();
bsalomon9fb42032016-05-13 09:23:38 -070059 case Type::kPath:
bsalomon728b0f72016-06-27 10:00:19 -070060 return this->path().getBounds();
bsalomon9fb42032016-05-13 09:23:38 -070061 }
62 SkFAIL("Unknown shape type");
bsalomon0ae36a22016-07-18 07:31:13 -070063 return kInverted;
bsalomon9fb42032016-05-13 09:23:38 -070064}
65
bsalomon0a0f67e2016-06-28 11:56:42 -070066SkRect GrShape::styledBounds() const {
bsalomon9fb42032016-05-13 09:23:38 -070067 if (Type::kEmpty == fType && !fStyle.hasNonDashPathEffect()) {
bsalomon0a0f67e2016-06-28 11:56:42 -070068 return SkRect::MakeEmpty();
bsalomon9fb42032016-05-13 09:23:38 -070069 }
bsalomon0a0f67e2016-06-28 11:56:42 -070070 SkRect bounds;
71 fStyle.adjustBounds(&bounds, this->bounds());
72 return bounds;
bsalomon9fb42032016-05-13 09:23:38 -070073}
74
bsalomon47cc7692016-04-26 12:56:00 -070075int GrShape::unstyledKeySize() const {
76 if (fInheritedKey.count()) {
77 return fInheritedKey.count();
78 }
79 switch (fType) {
80 case Type::kEmpty:
81 return 1;
82 case Type::kRRect:
83 SkASSERT(!fInheritedKey.count());
84 SkASSERT(0 == SkRRect::kSizeInMemory % sizeof(uint32_t));
bsalomon70493962016-06-10 08:05:14 -070085 // + 1 for the direction, start index, and inverseness.
bsalomonee295642016-06-06 14:01:25 -070086 return SkRRect::kSizeInMemory / sizeof(uint32_t) + 1;
bsalomon0a0f67e2016-06-28 11:56:42 -070087 case Type::kLine:
88 GR_STATIC_ASSERT(2 * sizeof(uint32_t) == sizeof(SkPoint));
89 // 4 for the end points and 1 for the inverseness
90 return 5;
bsalomon47cc7692016-04-26 12:56:00 -070091 case Type::kPath:
bsalomon728b0f72016-06-27 10:00:19 -070092 if (0 == fPathData.fGenID) {
bsalomon47cc7692016-04-26 12:56:00 -070093 return -1;
94 } else {
bsalomon70493962016-06-10 08:05:14 -070095 // The key is the path ID and fill type.
96 return 2;
bsalomon47cc7692016-04-26 12:56:00 -070097 }
98 }
99 SkFAIL("Should never get here.");
100 return 0;
101}
102
103void GrShape::writeUnstyledKey(uint32_t* key) const {
104 SkASSERT(this->unstyledKeySize());
105 SkDEBUGCODE(uint32_t* origKey = key;)
106 if (fInheritedKey.count()) {
107 memcpy(key, fInheritedKey.get(), sizeof(uint32_t) * fInheritedKey.count());
108 SkDEBUGCODE(key += fInheritedKey.count();)
109 } else {
110 switch (fType) {
111 case Type::kEmpty:
112 *key++ = 1;
113 break;
114 case Type::kRRect:
bsalomon728b0f72016-06-27 10:00:19 -0700115 fRRectData.fRRect.writeToMemory(key);
bsalomon47cc7692016-04-26 12:56:00 -0700116 key += SkRRect::kSizeInMemory / sizeof(uint32_t);
bsalomon728b0f72016-06-27 10:00:19 -0700117 *key = (fRRectData.fDir == SkPath::kCCW_Direction) ? (1 << 31) : 0;
118 *key |= fRRectData.fInverted ? (1 << 30) : 0;
119 *key++ |= fRRectData.fStart;
120 SkASSERT(fRRectData.fStart < 8);
bsalomon47cc7692016-04-26 12:56:00 -0700121 break;
bsalomon0a0f67e2016-06-28 11:56:42 -0700122 case Type::kLine:
123 memcpy(key, fLineData.fPts, 2 * sizeof(SkPoint));
124 key += 4;
125 *key++ = fLineData.fInverted ? 1 : 0;
126 break;
bsalomon47cc7692016-04-26 12:56:00 -0700127 case Type::kPath:
bsalomon728b0f72016-06-27 10:00:19 -0700128 SkASSERT(fPathData.fGenID);
129 *key++ = fPathData.fGenID;
bsalomon70493962016-06-10 08:05:14 -0700130 // We could canonicalize the fill rule for paths that don't differentiate between
131 // even/odd or winding fill (e.g. convex).
bsalomon728b0f72016-06-27 10:00:19 -0700132 *key++ = this->path().getFillType();
bsalomon47cc7692016-04-26 12:56:00 -0700133 break;
134 }
135 }
136 SkASSERT(key - origKey == this->unstyledKeySize());
137}
138
bsalomon97fd2d42016-05-09 13:02:01 -0700139void GrShape::setInheritedKey(const GrShape &parent, GrStyle::Apply apply, SkScalar scale) {
bsalomon47cc7692016-04-26 12:56:00 -0700140 SkASSERT(!fInheritedKey.count());
141 // If the output shape turns out to be simple, then we will just use its geometric key
142 if (Type::kPath == fType) {
143 // We want ApplyFullStyle(ApplyPathEffect(shape)) to have the same key as
144 // ApplyFullStyle(shape).
145 // The full key is structured as (geo,path_effect,stroke).
146 // If we do ApplyPathEffect we get get,path_effect as the inherited key. If we then
147 // do ApplyFullStyle we'll memcpy geo,path_effect into the new inherited key
148 // and then append the style key (which should now be stroke only) at the end.
149 int parentCnt = parent.fInheritedKey.count();
150 bool useParentGeoKey = !parentCnt;
151 if (useParentGeoKey) {
152 parentCnt = parent.unstyledKeySize();
bsalomon72dc51c2016-04-27 06:46:23 -0700153 if (parentCnt < 0) {
154 // The parent's geometry has no key so we will have no key.
bsalomon728b0f72016-06-27 10:00:19 -0700155 fPathData.fGenID = 0;
bsalomon72dc51c2016-04-27 06:46:23 -0700156 return;
157 }
bsalomon47cc7692016-04-26 12:56:00 -0700158 }
bsalomon06077562016-05-04 13:50:29 -0700159 uint32_t styleKeyFlags = 0;
160 if (parent.knownToBeClosed()) {
161 styleKeyFlags |= GrStyle::kClosed_KeyFlag;
162 }
bsalomon0ae36a22016-07-18 07:31:13 -0700163 if (parent.asLine(nullptr, nullptr)) {
164 styleKeyFlags |= GrStyle::kNoJoins_KeyFlag;
165 }
bsalomon06077562016-05-04 13:50:29 -0700166 int styleCnt = GrStyle::KeySize(parent.fStyle, apply, styleKeyFlags);
bsalomon47cc7692016-04-26 12:56:00 -0700167 if (styleCnt < 0) {
bsalomon93f66bc2016-06-21 08:35:49 -0700168 // 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 -0700169 // we try to get a key for the shape.
bsalomon728b0f72016-06-27 10:00:19 -0700170 fPathData.fGenID = 0;
bsalomon72dc51c2016-04-27 06:46:23 -0700171 return;
bsalomon47cc7692016-04-26 12:56:00 -0700172 }
bsalomon72dc51c2016-04-27 06:46:23 -0700173 fInheritedKey.reset(parentCnt + styleCnt);
174 if (useParentGeoKey) {
175 // This will be the geo key.
176 parent.writeUnstyledKey(fInheritedKey.get());
177 } else {
178 // This should be (geo,path_effect).
179 memcpy(fInheritedKey.get(), parent.fInheritedKey.get(),
180 parentCnt * sizeof(uint32_t));
181 }
182 // Now turn (geo,path_effect) or (geo) into (geo,path_effect,stroke)
bsalomon97fd2d42016-05-09 13:02:01 -0700183 GrStyle::WriteKey(fInheritedKey.get() + parentCnt, parent.fStyle, apply, scale,
184 styleKeyFlags);
bsalomon47cc7692016-04-26 12:56:00 -0700185 }
186}
187
bsalomon728b0f72016-06-27 10:00:19 -0700188GrShape::GrShape(const GrShape& that) : fStyle(that.fStyle) {
189 const SkPath* thatPath = Type::kPath == that.fType ? &that.fPathData.fPath : nullptr;
190 this->initType(that.fType, thatPath);
bsalomon47cc7692016-04-26 12:56:00 -0700191 switch (fType) {
192 case Type::kEmpty:
bsalomon93f66bc2016-06-21 08:35:49 -0700193 break;
bsalomon47cc7692016-04-26 12:56:00 -0700194 case Type::kRRect:
bsalomon0a0f67e2016-06-28 11:56:42 -0700195 fRRectData = that.fRRectData;
196 break;
197 case Type::kLine:
198 fLineData = that.fLineData;
bsalomon93f66bc2016-06-21 08:35:49 -0700199 break;
bsalomon47cc7692016-04-26 12:56:00 -0700200 case Type::kPath:
bsalomon728b0f72016-06-27 10:00:19 -0700201 fPathData.fGenID = that.fPathData.fGenID;
bsalomon93f66bc2016-06-21 08:35:49 -0700202 break;
bsalomon47cc7692016-04-26 12:56:00 -0700203 }
204 fInheritedKey.reset(that.fInheritedKey.count());
bsalomon93f66bc2016-06-21 08:35:49 -0700205 sk_careful_memcpy(fInheritedKey.get(), that.fInheritedKey.get(),
206 sizeof(uint32_t) * fInheritedKey.count());
bsalomon47cc7692016-04-26 12:56:00 -0700207}
208
bsalomon97fd2d42016-05-09 13:02:01 -0700209GrShape::GrShape(const GrShape& parent, GrStyle::Apply apply, SkScalar scale) {
210 // TODO: Add some quantization of scale for better cache performance here or leave that up
211 // to caller?
212 // TODO: For certain shapes and stroke params we could ignore the scale. (e.g. miter or bevel
213 // stroke of a rect).
bsalomonfb083272016-05-04 08:27:41 -0700214 if (!parent.style().applies() ||
215 (GrStyle::Apply::kPathEffectOnly == apply && !parent.style().pathEffect())) {
bsalomon728b0f72016-06-27 10:00:19 -0700216 this->initType(Type::kEmpty);
bsalomonfb083272016-05-04 08:27:41 -0700217 *this = parent;
218 return;
219 }
220
bsalomon47cc7692016-04-26 12:56:00 -0700221 SkPathEffect* pe = parent.fStyle.pathEffect();
bsalomonfb083272016-05-04 08:27:41 -0700222 SkTLazy<SkPath> tmpPath;
223 const GrShape* parentForKey = &parent;
224 SkTLazy<GrShape> tmpParent;
bsalomon728b0f72016-06-27 10:00:19 -0700225 this->initType(Type::kPath);
226 fPathData.fGenID = 0;
bsalomon47cc7692016-04-26 12:56:00 -0700227 if (pe) {
bsalomon728b0f72016-06-27 10:00:19 -0700228 const SkPath* srcForPathEffect;
bsalomon47cc7692016-04-26 12:56:00 -0700229 if (parent.fType == Type::kPath) {
bsalomon728b0f72016-06-27 10:00:19 -0700230 srcForPathEffect = &parent.path();
bsalomon47cc7692016-04-26 12:56:00 -0700231 } else {
bsalomonfb083272016-05-04 08:27:41 -0700232 srcForPathEffect = tmpPath.init();
233 parent.asPath(tmpPath.get());
bsalomon47cc7692016-04-26 12:56:00 -0700234 }
235 // Should we consider bounds? Would have to include in key, but it'd be nice to know
236 // if the bounds actually modified anything before including in key.
bsalomonfb083272016-05-04 08:27:41 -0700237 SkStrokeRec strokeRec = parent.fStyle.strokeRec();
bsalomon728b0f72016-06-27 10:00:19 -0700238 if (!parent.fStyle.applyPathEffectToPath(&this->path(), &strokeRec, *srcForPathEffect,
bsalomon398e3f42016-06-13 10:22:48 -0700239 scale)) {
bsalomon0ae36a22016-07-18 07:31:13 -0700240 tmpParent.init(*srcForPathEffect, GrStyle(strokeRec, nullptr));
241 *this = tmpParent.get()->applyStyle(apply, scale);
242 return;
bsalomon47cc7692016-04-26 12:56:00 -0700243 }
bsalomon97fd2d42016-05-09 13:02:01 -0700244 // A path effect has access to change the res scale but we aren't expecting it to and it
245 // would mess up our key computation.
246 SkASSERT(scale == strokeRec.getResScale());
bsalomon1b28c1a2016-06-20 12:28:17 -0700247 if (GrStyle::Apply::kPathEffectAndStrokeRec == apply && strokeRec.needToApply()) {
248 // The intermediate shape may not be a general path. If we we're just applying
249 // the path effect then attemptToReduceFromPath would catch it. This means that
250 // when we subsequently applied the remaining strokeRec we would have a non-path
251 // parent shape that would be used to determine the the stroked path's key.
252 // We detect that case here and change parentForKey to a temporary that represents
253 // the simpler shape so that applying both path effect and the strokerec all at
254 // once produces the same key.
bsalomon728b0f72016-06-27 10:00:19 -0700255 tmpParent.init(this->path(), GrStyle(strokeRec, nullptr));
bsalomon1b28c1a2016-06-20 12:28:17 -0700256 tmpParent.get()->setInheritedKey(parent, GrStyle::Apply::kPathEffectOnly, scale);
257 if (!tmpPath.isValid()) {
258 tmpPath.init();
bsalomon72dc51c2016-04-27 06:46:23 -0700259 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700260 tmpParent.get()->asPath(tmpPath.get());
261 SkStrokeRec::InitStyle fillOrHairline;
bsalomon0ae36a22016-07-18 07:31:13 -0700262 // The parent shape may have simplified away the strokeRec, check for that here.
263 if (tmpParent.get()->style().applies()) {
264 SkAssertResult(tmpParent.get()->style().applyToPath(&this->path(), &fillOrHairline,
265 *tmpPath.get(), scale));
266 } else if (tmpParent.get()->style().isSimpleFill()) {
267 fillOrHairline = SkStrokeRec::kFill_InitStyle;
268 } else {
269 SkASSERT(tmpParent.get()->style().isSimpleHairline());
270 fillOrHairline = SkStrokeRec::kHairline_InitStyle;
271 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700272 fStyle.resetToInitStyle(fillOrHairline);
273 parentForKey = tmpParent.get();
bsalomonfb083272016-05-04 08:27:41 -0700274 } else {
275 fStyle = GrStyle(strokeRec, nullptr);
bsalomon72dc51c2016-04-27 06:46:23 -0700276 }
bsalomon47cc7692016-04-26 12:56:00 -0700277 } else {
bsalomon97fd2d42016-05-09 13:02:01 -0700278 const SkPath* srcForParentStyle;
bsalomonfb083272016-05-04 08:27:41 -0700279 if (parent.fType == Type::kPath) {
bsalomon728b0f72016-06-27 10:00:19 -0700280 srcForParentStyle = &parent.path();
bsalomonfb083272016-05-04 08:27:41 -0700281 } else {
bsalomon97fd2d42016-05-09 13:02:01 -0700282 srcForParentStyle = tmpPath.init();
bsalomonfb083272016-05-04 08:27:41 -0700283 parent.asPath(tmpPath.get());
284 }
bsalomon97fd2d42016-05-09 13:02:01 -0700285 SkStrokeRec::InitStyle fillOrHairline;
286 SkASSERT(parent.fStyle.applies());
287 SkASSERT(!parent.fStyle.pathEffect());
bsalomon728b0f72016-06-27 10:00:19 -0700288 SkAssertResult(parent.fStyle.applyToPath(&this->path(), &fillOrHairline, *srcForParentStyle,
bsalomon97fd2d42016-05-09 13:02:01 -0700289 scale));
290 fStyle.resetToInitStyle(fillOrHairline);
bsalomon47cc7692016-04-26 12:56:00 -0700291 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700292 this->attemptToSimplifyPath();
bsalomon97fd2d42016-05-09 13:02:01 -0700293 this->setInheritedKey(*parentForKey, apply, scale);
bsalomon47cc7692016-04-26 12:56:00 -0700294}
bsalomonee295642016-06-06 14:01:25 -0700295
bsalomon1b28c1a2016-06-20 12:28:17 -0700296void GrShape::attemptToSimplifyPath() {
bsalomonee295642016-06-06 14:01:25 -0700297 SkRect rect;
bsalomon728b0f72016-06-27 10:00:19 -0700298 SkRRect rrect;
299 SkPath::Direction rrectDir;
300 unsigned rrectStart;
301 bool inverted = this->path().isInverseFillType();
bsalomon0a0f67e2016-06-28 11:56:42 -0700302 SkPoint pts[2];
bsalomon728b0f72016-06-27 10:00:19 -0700303 if (this->path().isEmpty()) {
304 this->changeType(Type::kEmpty);
bsalomon0a0f67e2016-06-28 11:56:42 -0700305 } else if (this->path().isLine(pts)) {
306 this->changeType(Type::kLine);
307 fLineData.fPts[0] = pts[0];
308 fLineData.fPts[1] = pts[1];
309 fLineData.fInverted = inverted;
bsalomon728b0f72016-06-27 10:00:19 -0700310 } else if (this->path().isRRect(&rrect, &rrectDir, &rrectStart)) {
311 this->changeType(Type::kRRect);
312 fRRectData.fRRect = rrect;
313 fRRectData.fDir = rrectDir;
314 fRRectData.fStart = rrectStart;
315 fRRectData.fInverted = inverted;
bsalomon1b28c1a2016-06-20 12:28:17 -0700316 // Currently SkPath does not acknowledge that empty, rect, or oval subtypes as rrects.
bsalomon728b0f72016-06-27 10:00:19 -0700317 SkASSERT(!fRRectData.fRRect.isEmpty());
318 SkASSERT(fRRectData.fRRect.getType() != SkRRect::kRect_Type);
319 SkASSERT(fRRectData.fRRect.getType() != SkRRect::kOval_Type);
320 } else if (this->path().isOval(&rect, &rrectDir, &rrectStart)) {
321 this->changeType(Type::kRRect);
322 fRRectData.fRRect.setOval(rect);
323 fRRectData.fDir = rrectDir;
324 fRRectData.fInverted = inverted;
bsalomon1b28c1a2016-06-20 12:28:17 -0700325 // convert from oval indexing to rrect indexiing.
bsalomon728b0f72016-06-27 10:00:19 -0700326 fRRectData.fStart = 2 * rrectStart;
327 } else if (SkPathPriv::IsSimpleClosedRect(this->path(), &rect, &rrectDir, &rrectStart)) {
328 this->changeType(Type::kRRect);
bsalomon1b28c1a2016-06-20 12:28:17 -0700329 // When there is a path effect we restrict rect detection to the narrower API that
330 // gives us the starting position. Otherwise, we will retry with the more aggressive
331 // isRect().
bsalomon728b0f72016-06-27 10:00:19 -0700332 fRRectData.fRRect.setRect(rect);
333 fRRectData.fInverted = inverted;
334 fRRectData.fDir = rrectDir;
bsalomon1b28c1a2016-06-20 12:28:17 -0700335 // convert from rect indexing to rrect indexiing.
bsalomon728b0f72016-06-27 10:00:19 -0700336 fRRectData.fStart = 2 * rrectStart;
bsalomon1b28c1a2016-06-20 12:28:17 -0700337 } else if (!this->style().hasPathEffect()) {
bsalomonee295642016-06-06 14:01:25 -0700338 bool closed;
bsalomon728b0f72016-06-27 10:00:19 -0700339 if (this->path().isRect(&rect, &closed, nullptr)) {
bsalomon1b28c1a2016-06-20 12:28:17 -0700340 if (closed || this->style().isSimpleFill()) {
bsalomon728b0f72016-06-27 10:00:19 -0700341 this->changeType(Type::kRRect);
342 fRRectData.fRRect.setRect(rect);
bsalomonee295642016-06-06 14:01:25 -0700343 // Since there is no path effect the dir and start index is immaterial.
bsalomon728b0f72016-06-27 10:00:19 -0700344 fRRectData.fDir = kDefaultRRectDir;
345 fRRectData.fStart = kDefaultRRectStart;
bsalomon1b28c1a2016-06-20 12:28:17 -0700346 // There isn't dashing so we will have to preserver inverseness.
bsalomon728b0f72016-06-27 10:00:19 -0700347 fRRectData.fInverted = inverted;
bsalomonee295642016-06-06 14:01:25 -0700348 }
349 }
350 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700351 if (Type::kPath != fType) {
bsalomon1b28c1a2016-06-20 12:28:17 -0700352 fInheritedKey.reset(0);
353 if (Type::kRRect == fType) {
354 this->attemptToSimplifyRRect();
bsalomon0a0f67e2016-06-28 11:56:42 -0700355 } else if (Type::kLine == fType) {
356 this->attemptToSimplifyLine();
bsalomon1b28c1a2016-06-20 12:28:17 -0700357 }
bsalomon93f66bc2016-06-21 08:35:49 -0700358 } else {
bsalomon728b0f72016-06-27 10:00:19 -0700359 if (fInheritedKey.count() || this->path().isVolatile()) {
360 fPathData.fGenID = 0;
bsalomon93f66bc2016-06-21 08:35:49 -0700361 } else {
bsalomon728b0f72016-06-27 10:00:19 -0700362 fPathData.fGenID = this->path().getGenerationID();
bsalomon93f66bc2016-06-21 08:35:49 -0700363 }
364 if (this->style().isSimpleFill()) {
bsalomon0a0f67e2016-06-28 11:56:42 -0700365 this->path().close();
366 this->path().setIsVolatile(true);
bsalomon93f66bc2016-06-21 08:35:49 -0700367 }
bsalomona4817af2016-06-23 11:48:26 -0700368 if (!this->style().hasNonDashPathEffect()) {
369 if (this->style().strokeRec().getStyle() == SkStrokeRec::kStroke_Style ||
370 this->style().strokeRec().getStyle() == SkStrokeRec::kHairline_Style) {
371 // Stroke styles don't differentiate between winding and even/odd.
372 // Moreover, dashing ignores inverseness (skbug.com/5421)
bsalomon728b0f72016-06-27 10:00:19 -0700373 bool inverse = !this->style().isDashed() && this->path().isInverseFillType();
bsalomona4817af2016-06-23 11:48:26 -0700374 if (inverse) {
bsalomon728b0f72016-06-27 10:00:19 -0700375 this->path().setFillType(kDefaultPathInverseFillType);
bsalomona4817af2016-06-23 11:48:26 -0700376 } else {
bsalomon728b0f72016-06-27 10:00:19 -0700377 this->path().setFillType(kDefaultPathFillType);
bsalomona4817af2016-06-23 11:48:26 -0700378 }
bsalomon728b0f72016-06-27 10:00:19 -0700379 } else if (this->path().isConvex()) {
bsalomona4817af2016-06-23 11:48:26 -0700380 // There is no distinction between even/odd and non-zero winding count for convex
381 // paths.
bsalomon728b0f72016-06-27 10:00:19 -0700382 if (this->path().isInverseFillType()) {
383 this->path().setFillType(kDefaultPathInverseFillType);
bsalomona4817af2016-06-23 11:48:26 -0700384 } else {
bsalomon728b0f72016-06-27 10:00:19 -0700385 this->path().setFillType(kDefaultPathFillType);
bsalomona4817af2016-06-23 11:48:26 -0700386 }
bsalomon93f66bc2016-06-21 08:35:49 -0700387 }
388 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700389 }
390}
391
392void GrShape::attemptToSimplifyRRect() {
393 SkASSERT(Type::kRRect == fType);
394 SkASSERT(!fInheritedKey.count());
bsalomon728b0f72016-06-27 10:00:19 -0700395 if (fRRectData.fRRect.isEmpty()) {
bsalomon1b28c1a2016-06-20 12:28:17 -0700396 fType = Type::kEmpty;
397 return;
398 }
399 if (!this->style().hasPathEffect()) {
bsalomon728b0f72016-06-27 10:00:19 -0700400 fRRectData.fDir = kDefaultRRectDir;
401 fRRectData.fStart = kDefaultRRectStart;
bsalomon1b28c1a2016-06-20 12:28:17 -0700402 } else if (fStyle.isDashed()) {
403 // Dashing ignores the inverseness (currently). skbug.com/5421
bsalomon728b0f72016-06-27 10:00:19 -0700404 fRRectData.fInverted = false;
bsalomon1b28c1a2016-06-20 12:28:17 -0700405 }
bsalomon487f8d32016-07-20 07:15:44 -0700406 // Turn a stroke-and-filled miter rect into a filled rect. TODO: more rrect stroke shortcuts.
407 if (!fStyle.hasPathEffect() &&
408 fStyle.strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style &&
409 fStyle.strokeRec().getJoin() == SkPaint::kMiter_Join &&
410 fStyle.strokeRec().getMiter() >= SK_ScalarSqrt2 &&
411 fRRectData.fRRect.isRect()) {
412 SkScalar r = fStyle.strokeRec().getWidth() / 2;
413 fRRectData.fRRect = SkRRect::MakeRect(fRRectData.fRRect.rect().makeOutset(r, r));
414 fStyle = GrStyle::SimpleFill();
415 }
bsalomonee295642016-06-06 14:01:25 -0700416}
bsalomon0a0f67e2016-06-28 11:56:42 -0700417
418void GrShape::attemptToSimplifyLine() {
bsalomon0ae36a22016-07-18 07:31:13 -0700419 SkASSERT(Type::kLine == fType);
420 SkASSERT(!fInheritedKey.count());
421 if (fStyle.isDashed()) {
422 // Dashing ignores inverseness.
423 fLineData.fInverted = false;
424 return;
425 } else if (fStyle.hasPathEffect()) {
426 return;
427 }
428 if (fStyle.strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style) {
429 // Make stroke + fill be stroke since the fill is empty.
430 SkStrokeRec rec = fStyle.strokeRec();
431 rec.setStrokeStyle(fStyle.strokeRec().getWidth(), false);
432 fStyle = GrStyle(rec, nullptr);
433 }
bsalomon0a0f67e2016-06-28 11:56:42 -0700434 if (fStyle.isSimpleFill() && !fLineData.fInverted) {
435 this->changeType(Type::kEmpty);
bsalomon0ae36a22016-07-18 07:31:13 -0700436 return;
437 }
438 SkPoint* pts = fLineData.fPts;
439 if (fStyle.strokeRec().getStyle() == SkStrokeRec::kStroke_Style) {
440 // If it is horizontal or vertical we will turn it into a filled rrect.
441 SkRect rect;
442 rect.fLeft = SkTMin(pts[0].fX, pts[1].fX);
443 rect.fRight = SkTMax(pts[0].fX, pts[1].fX);
444 rect.fTop = SkTMin(pts[0].fY, pts[1].fY);
445 rect.fBottom = SkTMax(pts[0].fY, pts[1].fY);
446 bool eqX = rect.fLeft == rect.fRight;
447 bool eqY = rect.fTop == rect.fBottom;
448 if (eqX || eqY) {
449 SkScalar r = fStyle.strokeRec().getWidth() / 2;
450 bool inverted = fLineData.fInverted;
451 this->changeType(Type::kRRect);
452 switch (fStyle.strokeRec().getCap()) {
453 case SkPaint::kButt_Cap:
454 if (eqX && eqY) {
455 this->changeType(Type::kEmpty);
456 return;
457 }
458 if (eqX) {
459 rect.outset(r, 0);
460 } else {
461 rect.outset(0, r);
462 }
463 fRRectData.fRRect = SkRRect::MakeRect(rect);
464 break;
465 case SkPaint::kSquare_Cap:
466 rect.outset(r, r);
467 fRRectData.fRRect = SkRRect::MakeRect(rect);
468 break;
469 case SkPaint::kRound_Cap:
470 rect.outset(r, r);
471 fRRectData.fRRect = SkRRect::MakeRectXY(rect, r, r);
472 break;
bsalomon0a0f67e2016-06-28 11:56:42 -0700473 }
bsalomon0ae36a22016-07-18 07:31:13 -0700474 fRRectData.fInverted = inverted;
475 fRRectData.fDir = kDefaultRRectDir;
476 fRRectData.fStart = kDefaultRRectStart;
477 if (fRRectData.fRRect.isEmpty()) {
478 // This can happen when r is very small relative to the rect edges.
479 this->changeType(Type::kEmpty);
480 return;
481 }
482 fStyle = GrStyle::SimpleFill();
483 return;
bsalomon0a0f67e2016-06-28 11:56:42 -0700484 }
485 }
bsalomon0ae36a22016-07-18 07:31:13 -0700486 // Only path effects could care about the order of the points. Otherwise canonicalize
487 // the point order.
488 if (pts[1].fY < pts[0].fY || (pts[1].fY == pts[0].fY && pts[1].fX < pts[0].fX)) {
489 SkTSwap(pts[0], pts[1]);
490 }
bsalomon0a0f67e2016-06-28 11:56:42 -0700491}