blob: 7e0a3a4449e5a7b3745f178e5dbea42ee448beb0 [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
bsalomon67fa4e32016-09-21 08:26:57 -070075// If the path is small enough to be keyed from its data this returns key length, otherwise -1.
76static int path_key_from_data_size(const SkPath& path) {
77 const int verbCnt = path.countVerbs();
78 if (verbCnt > GrShape::kMaxKeyFromDataVerbCnt) {
79 return -1;
80 }
81 const int pointCnt = path.countPoints();
82 const int conicWeightCnt = SkPathPriv::ConicWeightCnt(path);
83
84 GR_STATIC_ASSERT(sizeof(SkPoint) == 2 * sizeof(uint32_t));
85 GR_STATIC_ASSERT(sizeof(SkScalar) == sizeof(uint32_t));
86 // 2 is for the verb cnt and a fill type. Each verb is a byte but we'll pad the verb data out to
87 // a uint32_t length.
88 return 2 + (SkAlign4(verbCnt) >> 2) + 2 * pointCnt + conicWeightCnt;
89}
90
91// Writes the path data key into the passed pointer.
bsalomon0e4a4662016-09-21 11:23:46 -070092static void write_path_key_from_data(const SkPath& path, uint32_t* origKey) {
bsalomon67fa4e32016-09-21 08:26:57 -070093 uint32_t* key = origKey;
94 // The check below should take care of negative values casted positive.
95 const int verbCnt = path.countVerbs();
96 const int pointCnt = path.countPoints();
97 const int conicWeightCnt = SkPathPriv::ConicWeightCnt(path);
98 SkASSERT(verbCnt <= GrShape::kMaxKeyFromDataVerbCnt);
99 SkASSERT(pointCnt && verbCnt);
100 *key++ = path.getFillType();
101 *key++ = verbCnt;
102 memcpy(key, SkPathPriv::VerbData(path), verbCnt * sizeof(uint8_t));
103 int verbKeySize = SkAlign4(verbCnt);
104 // pad out to uint32_t alignment using value that will stand out when debugging.
105 uint8_t* pad = reinterpret_cast<uint8_t*>(key)+ verbCnt;
106 memset(pad, 0xDE, verbKeySize - verbCnt);
107 key += verbKeySize >> 2;
108
109 memcpy(key, SkPathPriv::PointData(path), sizeof(SkPoint) * pointCnt);
110 GR_STATIC_ASSERT(sizeof(SkPoint) == 2 * sizeof(uint32_t));
111 key += 2 * pointCnt;
bsalomon0e4a4662016-09-21 11:23:46 -0700112 sk_careful_memcpy(key, SkPathPriv::ConicWeightData(path), sizeof(SkScalar) * conicWeightCnt);
bsalomon67fa4e32016-09-21 08:26:57 -0700113 GR_STATIC_ASSERT(sizeof(SkScalar) == sizeof(uint32_t));
114 SkDEBUGCODE(key += conicWeightCnt);
115 SkASSERT(key - origKey == path_key_from_data_size(path));
116}
117
bsalomon47cc7692016-04-26 12:56:00 -0700118int GrShape::unstyledKeySize() const {
119 if (fInheritedKey.count()) {
120 return fInheritedKey.count();
121 }
122 switch (fType) {
123 case Type::kEmpty:
124 return 1;
125 case Type::kRRect:
126 SkASSERT(!fInheritedKey.count());
127 SkASSERT(0 == SkRRect::kSizeInMemory % sizeof(uint32_t));
bsalomon70493962016-06-10 08:05:14 -0700128 // + 1 for the direction, start index, and inverseness.
bsalomonee295642016-06-06 14:01:25 -0700129 return SkRRect::kSizeInMemory / sizeof(uint32_t) + 1;
bsalomon0a0f67e2016-06-28 11:56:42 -0700130 case Type::kLine:
131 GR_STATIC_ASSERT(2 * sizeof(uint32_t) == sizeof(SkPoint));
132 // 4 for the end points and 1 for the inverseness
133 return 5;
bsalomon67fa4e32016-09-21 08:26:57 -0700134 case Type::kPath: {
bsalomonaa840642016-09-23 12:09:16 -0700135 if (0 == fPathData.fGenID) {
136 return -1;
137 }
bsalomon67fa4e32016-09-21 08:26:57 -0700138 int dataKeySize = path_key_from_data_size(fPathData.fPath);
139 if (dataKeySize >= 0) {
140 return dataKeySize;
141 }
bsalomon67fa4e32016-09-21 08:26:57 -0700142 // The key is the path ID and fill type.
143 return 2;
144 }
bsalomon47cc7692016-04-26 12:56:00 -0700145 }
146 SkFAIL("Should never get here.");
147 return 0;
148}
149
150void GrShape::writeUnstyledKey(uint32_t* key) const {
151 SkASSERT(this->unstyledKeySize());
152 SkDEBUGCODE(uint32_t* origKey = key;)
153 if (fInheritedKey.count()) {
154 memcpy(key, fInheritedKey.get(), sizeof(uint32_t) * fInheritedKey.count());
155 SkDEBUGCODE(key += fInheritedKey.count();)
156 } else {
157 switch (fType) {
158 case Type::kEmpty:
159 *key++ = 1;
160 break;
161 case Type::kRRect:
bsalomon728b0f72016-06-27 10:00:19 -0700162 fRRectData.fRRect.writeToMemory(key);
bsalomon47cc7692016-04-26 12:56:00 -0700163 key += SkRRect::kSizeInMemory / sizeof(uint32_t);
bsalomon728b0f72016-06-27 10:00:19 -0700164 *key = (fRRectData.fDir == SkPath::kCCW_Direction) ? (1 << 31) : 0;
165 *key |= fRRectData.fInverted ? (1 << 30) : 0;
166 *key++ |= fRRectData.fStart;
167 SkASSERT(fRRectData.fStart < 8);
bsalomon47cc7692016-04-26 12:56:00 -0700168 break;
bsalomon0a0f67e2016-06-28 11:56:42 -0700169 case Type::kLine:
170 memcpy(key, fLineData.fPts, 2 * sizeof(SkPoint));
171 key += 4;
172 *key++ = fLineData.fInverted ? 1 : 0;
173 break;
bsalomon67fa4e32016-09-21 08:26:57 -0700174 case Type::kPath: {
bsalomonaa840642016-09-23 12:09:16 -0700175 SkASSERT(fPathData.fGenID);
bsalomon67fa4e32016-09-21 08:26:57 -0700176 int dataKeySize = path_key_from_data_size(fPathData.fPath);
177 if (dataKeySize >= 0) {
bsalomon0e4a4662016-09-21 11:23:46 -0700178 write_path_key_from_data(fPathData.fPath, key);
bsalomon67fa4e32016-09-21 08:26:57 -0700179 return;
180 }
bsalomon728b0f72016-06-27 10:00:19 -0700181 *key++ = fPathData.fGenID;
bsalomon70493962016-06-10 08:05:14 -0700182 // We could canonicalize the fill rule for paths that don't differentiate between
183 // even/odd or winding fill (e.g. convex).
bsalomon728b0f72016-06-27 10:00:19 -0700184 *key++ = this->path().getFillType();
bsalomon47cc7692016-04-26 12:56:00 -0700185 break;
bsalomon67fa4e32016-09-21 08:26:57 -0700186 }
bsalomon47cc7692016-04-26 12:56:00 -0700187 }
188 }
189 SkASSERT(key - origKey == this->unstyledKeySize());
190}
191
bsalomon97fd2d42016-05-09 13:02:01 -0700192void GrShape::setInheritedKey(const GrShape &parent, GrStyle::Apply apply, SkScalar scale) {
bsalomon47cc7692016-04-26 12:56:00 -0700193 SkASSERT(!fInheritedKey.count());
194 // If the output shape turns out to be simple, then we will just use its geometric key
195 if (Type::kPath == fType) {
196 // We want ApplyFullStyle(ApplyPathEffect(shape)) to have the same key as
197 // ApplyFullStyle(shape).
198 // The full key is structured as (geo,path_effect,stroke).
199 // If we do ApplyPathEffect we get get,path_effect as the inherited key. If we then
200 // do ApplyFullStyle we'll memcpy geo,path_effect into the new inherited key
201 // and then append the style key (which should now be stroke only) at the end.
202 int parentCnt = parent.fInheritedKey.count();
203 bool useParentGeoKey = !parentCnt;
204 if (useParentGeoKey) {
205 parentCnt = parent.unstyledKeySize();
bsalomon72dc51c2016-04-27 06:46:23 -0700206 if (parentCnt < 0) {
207 // The parent's geometry has no key so we will have no key.
bsalomon728b0f72016-06-27 10:00:19 -0700208 fPathData.fGenID = 0;
bsalomon72dc51c2016-04-27 06:46:23 -0700209 return;
210 }
bsalomon47cc7692016-04-26 12:56:00 -0700211 }
bsalomon06077562016-05-04 13:50:29 -0700212 uint32_t styleKeyFlags = 0;
213 if (parent.knownToBeClosed()) {
214 styleKeyFlags |= GrStyle::kClosed_KeyFlag;
215 }
bsalomon0ae36a22016-07-18 07:31:13 -0700216 if (parent.asLine(nullptr, nullptr)) {
217 styleKeyFlags |= GrStyle::kNoJoins_KeyFlag;
218 }
bsalomon06077562016-05-04 13:50:29 -0700219 int styleCnt = GrStyle::KeySize(parent.fStyle, apply, styleKeyFlags);
bsalomon47cc7692016-04-26 12:56:00 -0700220 if (styleCnt < 0) {
bsalomon93f66bc2016-06-21 08:35:49 -0700221 // 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 -0700222 // we try to get a key for the shape.
bsalomon728b0f72016-06-27 10:00:19 -0700223 fPathData.fGenID = 0;
bsalomon72dc51c2016-04-27 06:46:23 -0700224 return;
bsalomon47cc7692016-04-26 12:56:00 -0700225 }
bsalomon72dc51c2016-04-27 06:46:23 -0700226 fInheritedKey.reset(parentCnt + styleCnt);
227 if (useParentGeoKey) {
228 // This will be the geo key.
229 parent.writeUnstyledKey(fInheritedKey.get());
230 } else {
231 // This should be (geo,path_effect).
232 memcpy(fInheritedKey.get(), parent.fInheritedKey.get(),
233 parentCnt * sizeof(uint32_t));
234 }
235 // Now turn (geo,path_effect) or (geo) into (geo,path_effect,stroke)
bsalomon97fd2d42016-05-09 13:02:01 -0700236 GrStyle::WriteKey(fInheritedKey.get() + parentCnt, parent.fStyle, apply, scale,
237 styleKeyFlags);
bsalomon47cc7692016-04-26 12:56:00 -0700238 }
239}
240
bsalomon728b0f72016-06-27 10:00:19 -0700241GrShape::GrShape(const GrShape& that) : fStyle(that.fStyle) {
242 const SkPath* thatPath = Type::kPath == that.fType ? &that.fPathData.fPath : nullptr;
243 this->initType(that.fType, thatPath);
bsalomon47cc7692016-04-26 12:56:00 -0700244 switch (fType) {
245 case Type::kEmpty:
bsalomon93f66bc2016-06-21 08:35:49 -0700246 break;
bsalomon47cc7692016-04-26 12:56:00 -0700247 case Type::kRRect:
bsalomon0a0f67e2016-06-28 11:56:42 -0700248 fRRectData = that.fRRectData;
249 break;
250 case Type::kLine:
251 fLineData = that.fLineData;
bsalomon93f66bc2016-06-21 08:35:49 -0700252 break;
bsalomon47cc7692016-04-26 12:56:00 -0700253 case Type::kPath:
bsalomon728b0f72016-06-27 10:00:19 -0700254 fPathData.fGenID = that.fPathData.fGenID;
bsalomon93f66bc2016-06-21 08:35:49 -0700255 break;
bsalomon47cc7692016-04-26 12:56:00 -0700256 }
257 fInheritedKey.reset(that.fInheritedKey.count());
bsalomon93f66bc2016-06-21 08:35:49 -0700258 sk_careful_memcpy(fInheritedKey.get(), that.fInheritedKey.get(),
259 sizeof(uint32_t) * fInheritedKey.count());
bsalomon47cc7692016-04-26 12:56:00 -0700260}
261
bsalomon97fd2d42016-05-09 13:02:01 -0700262GrShape::GrShape(const GrShape& parent, GrStyle::Apply apply, SkScalar scale) {
263 // TODO: Add some quantization of scale for better cache performance here or leave that up
264 // to caller?
265 // TODO: For certain shapes and stroke params we could ignore the scale. (e.g. miter or bevel
266 // stroke of a rect).
bsalomonfb083272016-05-04 08:27:41 -0700267 if (!parent.style().applies() ||
268 (GrStyle::Apply::kPathEffectOnly == apply && !parent.style().pathEffect())) {
bsalomon728b0f72016-06-27 10:00:19 -0700269 this->initType(Type::kEmpty);
bsalomonfb083272016-05-04 08:27:41 -0700270 *this = parent;
271 return;
272 }
273
bsalomon47cc7692016-04-26 12:56:00 -0700274 SkPathEffect* pe = parent.fStyle.pathEffect();
bsalomonfb083272016-05-04 08:27:41 -0700275 SkTLazy<SkPath> tmpPath;
276 const GrShape* parentForKey = &parent;
277 SkTLazy<GrShape> tmpParent;
bsalomon728b0f72016-06-27 10:00:19 -0700278 this->initType(Type::kPath);
279 fPathData.fGenID = 0;
bsalomon47cc7692016-04-26 12:56:00 -0700280 if (pe) {
bsalomon728b0f72016-06-27 10:00:19 -0700281 const SkPath* srcForPathEffect;
bsalomon47cc7692016-04-26 12:56:00 -0700282 if (parent.fType == Type::kPath) {
bsalomon728b0f72016-06-27 10:00:19 -0700283 srcForPathEffect = &parent.path();
bsalomon47cc7692016-04-26 12:56:00 -0700284 } else {
bsalomonfb083272016-05-04 08:27:41 -0700285 srcForPathEffect = tmpPath.init();
286 parent.asPath(tmpPath.get());
bsalomon47cc7692016-04-26 12:56:00 -0700287 }
288 // Should we consider bounds? Would have to include in key, but it'd be nice to know
289 // if the bounds actually modified anything before including in key.
bsalomonfb083272016-05-04 08:27:41 -0700290 SkStrokeRec strokeRec = parent.fStyle.strokeRec();
bsalomon728b0f72016-06-27 10:00:19 -0700291 if (!parent.fStyle.applyPathEffectToPath(&this->path(), &strokeRec, *srcForPathEffect,
bsalomon398e3f42016-06-13 10:22:48 -0700292 scale)) {
bsalomon0ae36a22016-07-18 07:31:13 -0700293 tmpParent.init(*srcForPathEffect, GrStyle(strokeRec, nullptr));
294 *this = tmpParent.get()->applyStyle(apply, scale);
295 return;
bsalomon47cc7692016-04-26 12:56:00 -0700296 }
bsalomon97fd2d42016-05-09 13:02:01 -0700297 // A path effect has access to change the res scale but we aren't expecting it to and it
298 // would mess up our key computation.
299 SkASSERT(scale == strokeRec.getResScale());
bsalomon1b28c1a2016-06-20 12:28:17 -0700300 if (GrStyle::Apply::kPathEffectAndStrokeRec == apply && strokeRec.needToApply()) {
301 // The intermediate shape may not be a general path. If we we're just applying
302 // the path effect then attemptToReduceFromPath would catch it. This means that
303 // when we subsequently applied the remaining strokeRec we would have a non-path
304 // parent shape that would be used to determine the the stroked path's key.
305 // We detect that case here and change parentForKey to a temporary that represents
306 // the simpler shape so that applying both path effect and the strokerec all at
307 // once produces the same key.
bsalomon728b0f72016-06-27 10:00:19 -0700308 tmpParent.init(this->path(), GrStyle(strokeRec, nullptr));
bsalomon1b28c1a2016-06-20 12:28:17 -0700309 tmpParent.get()->setInheritedKey(parent, GrStyle::Apply::kPathEffectOnly, scale);
310 if (!tmpPath.isValid()) {
311 tmpPath.init();
bsalomon72dc51c2016-04-27 06:46:23 -0700312 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700313 tmpParent.get()->asPath(tmpPath.get());
314 SkStrokeRec::InitStyle fillOrHairline;
bsalomon0ae36a22016-07-18 07:31:13 -0700315 // The parent shape may have simplified away the strokeRec, check for that here.
316 if (tmpParent.get()->style().applies()) {
317 SkAssertResult(tmpParent.get()->style().applyToPath(&this->path(), &fillOrHairline,
318 *tmpPath.get(), scale));
319 } else if (tmpParent.get()->style().isSimpleFill()) {
320 fillOrHairline = SkStrokeRec::kFill_InitStyle;
321 } else {
322 SkASSERT(tmpParent.get()->style().isSimpleHairline());
323 fillOrHairline = SkStrokeRec::kHairline_InitStyle;
324 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700325 fStyle.resetToInitStyle(fillOrHairline);
326 parentForKey = tmpParent.get();
bsalomonfb083272016-05-04 08:27:41 -0700327 } else {
328 fStyle = GrStyle(strokeRec, nullptr);
bsalomon72dc51c2016-04-27 06:46:23 -0700329 }
bsalomon47cc7692016-04-26 12:56:00 -0700330 } else {
bsalomon97fd2d42016-05-09 13:02:01 -0700331 const SkPath* srcForParentStyle;
bsalomonfb083272016-05-04 08:27:41 -0700332 if (parent.fType == Type::kPath) {
bsalomon728b0f72016-06-27 10:00:19 -0700333 srcForParentStyle = &parent.path();
bsalomonfb083272016-05-04 08:27:41 -0700334 } else {
bsalomon97fd2d42016-05-09 13:02:01 -0700335 srcForParentStyle = tmpPath.init();
bsalomonfb083272016-05-04 08:27:41 -0700336 parent.asPath(tmpPath.get());
337 }
bsalomon97fd2d42016-05-09 13:02:01 -0700338 SkStrokeRec::InitStyle fillOrHairline;
339 SkASSERT(parent.fStyle.applies());
340 SkASSERT(!parent.fStyle.pathEffect());
bsalomon728b0f72016-06-27 10:00:19 -0700341 SkAssertResult(parent.fStyle.applyToPath(&this->path(), &fillOrHairline, *srcForParentStyle,
bsalomon97fd2d42016-05-09 13:02:01 -0700342 scale));
343 fStyle.resetToInitStyle(fillOrHairline);
bsalomon47cc7692016-04-26 12:56:00 -0700344 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700345 this->attemptToSimplifyPath();
bsalomon97fd2d42016-05-09 13:02:01 -0700346 this->setInheritedKey(*parentForKey, apply, scale);
bsalomon47cc7692016-04-26 12:56:00 -0700347}
bsalomonee295642016-06-06 14:01:25 -0700348
bsalomon1b28c1a2016-06-20 12:28:17 -0700349void GrShape::attemptToSimplifyPath() {
bsalomonee295642016-06-06 14:01:25 -0700350 SkRect rect;
bsalomon728b0f72016-06-27 10:00:19 -0700351 SkRRect rrect;
352 SkPath::Direction rrectDir;
353 unsigned rrectStart;
354 bool inverted = this->path().isInverseFillType();
bsalomon0a0f67e2016-06-28 11:56:42 -0700355 SkPoint pts[2];
bsalomon728b0f72016-06-27 10:00:19 -0700356 if (this->path().isEmpty()) {
357 this->changeType(Type::kEmpty);
bsalomon0a0f67e2016-06-28 11:56:42 -0700358 } else if (this->path().isLine(pts)) {
359 this->changeType(Type::kLine);
360 fLineData.fPts[0] = pts[0];
361 fLineData.fPts[1] = pts[1];
362 fLineData.fInverted = inverted;
bsalomon728b0f72016-06-27 10:00:19 -0700363 } else if (this->path().isRRect(&rrect, &rrectDir, &rrectStart)) {
364 this->changeType(Type::kRRect);
365 fRRectData.fRRect = rrect;
366 fRRectData.fDir = rrectDir;
367 fRRectData.fStart = rrectStart;
368 fRRectData.fInverted = inverted;
bsalomon1b28c1a2016-06-20 12:28:17 -0700369 // Currently SkPath does not acknowledge that empty, rect, or oval subtypes as rrects.
bsalomon728b0f72016-06-27 10:00:19 -0700370 SkASSERT(!fRRectData.fRRect.isEmpty());
371 SkASSERT(fRRectData.fRRect.getType() != SkRRect::kRect_Type);
372 SkASSERT(fRRectData.fRRect.getType() != SkRRect::kOval_Type);
373 } else if (this->path().isOval(&rect, &rrectDir, &rrectStart)) {
374 this->changeType(Type::kRRect);
375 fRRectData.fRRect.setOval(rect);
376 fRRectData.fDir = rrectDir;
377 fRRectData.fInverted = inverted;
bsalomon1b28c1a2016-06-20 12:28:17 -0700378 // convert from oval indexing to rrect indexiing.
bsalomon728b0f72016-06-27 10:00:19 -0700379 fRRectData.fStart = 2 * rrectStart;
380 } else if (SkPathPriv::IsSimpleClosedRect(this->path(), &rect, &rrectDir, &rrectStart)) {
381 this->changeType(Type::kRRect);
bsalomon1b28c1a2016-06-20 12:28:17 -0700382 // When there is a path effect we restrict rect detection to the narrower API that
383 // gives us the starting position. Otherwise, we will retry with the more aggressive
384 // isRect().
bsalomon728b0f72016-06-27 10:00:19 -0700385 fRRectData.fRRect.setRect(rect);
386 fRRectData.fInverted = inverted;
387 fRRectData.fDir = rrectDir;
bsalomon1b28c1a2016-06-20 12:28:17 -0700388 // convert from rect indexing to rrect indexiing.
bsalomon728b0f72016-06-27 10:00:19 -0700389 fRRectData.fStart = 2 * rrectStart;
bsalomon1b28c1a2016-06-20 12:28:17 -0700390 } else if (!this->style().hasPathEffect()) {
bsalomonee295642016-06-06 14:01:25 -0700391 bool closed;
bsalomon728b0f72016-06-27 10:00:19 -0700392 if (this->path().isRect(&rect, &closed, nullptr)) {
bsalomon1b28c1a2016-06-20 12:28:17 -0700393 if (closed || this->style().isSimpleFill()) {
bsalomon728b0f72016-06-27 10:00:19 -0700394 this->changeType(Type::kRRect);
395 fRRectData.fRRect.setRect(rect);
bsalomonee295642016-06-06 14:01:25 -0700396 // Since there is no path effect the dir and start index is immaterial.
bsalomon728b0f72016-06-27 10:00:19 -0700397 fRRectData.fDir = kDefaultRRectDir;
398 fRRectData.fStart = kDefaultRRectStart;
bsalomon1b28c1a2016-06-20 12:28:17 -0700399 // There isn't dashing so we will have to preserver inverseness.
bsalomon728b0f72016-06-27 10:00:19 -0700400 fRRectData.fInverted = inverted;
bsalomonee295642016-06-06 14:01:25 -0700401 }
402 }
403 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700404 if (Type::kPath != fType) {
bsalomon1b28c1a2016-06-20 12:28:17 -0700405 fInheritedKey.reset(0);
406 if (Type::kRRect == fType) {
407 this->attemptToSimplifyRRect();
bsalomon0a0f67e2016-06-28 11:56:42 -0700408 } else if (Type::kLine == fType) {
409 this->attemptToSimplifyLine();
bsalomon1b28c1a2016-06-20 12:28:17 -0700410 }
bsalomon93f66bc2016-06-21 08:35:49 -0700411 } else {
bsalomon728b0f72016-06-27 10:00:19 -0700412 if (fInheritedKey.count() || this->path().isVolatile()) {
413 fPathData.fGenID = 0;
bsalomon93f66bc2016-06-21 08:35:49 -0700414 } else {
bsalomon728b0f72016-06-27 10:00:19 -0700415 fPathData.fGenID = this->path().getGenerationID();
bsalomon93f66bc2016-06-21 08:35:49 -0700416 }
bsalomona4817af2016-06-23 11:48:26 -0700417 if (!this->style().hasNonDashPathEffect()) {
418 if (this->style().strokeRec().getStyle() == SkStrokeRec::kStroke_Style ||
419 this->style().strokeRec().getStyle() == SkStrokeRec::kHairline_Style) {
420 // Stroke styles don't differentiate between winding and even/odd.
421 // Moreover, dashing ignores inverseness (skbug.com/5421)
bsalomon728b0f72016-06-27 10:00:19 -0700422 bool inverse = !this->style().isDashed() && this->path().isInverseFillType();
bsalomona4817af2016-06-23 11:48:26 -0700423 if (inverse) {
bsalomon728b0f72016-06-27 10:00:19 -0700424 this->path().setFillType(kDefaultPathInverseFillType);
bsalomona4817af2016-06-23 11:48:26 -0700425 } else {
bsalomon728b0f72016-06-27 10:00:19 -0700426 this->path().setFillType(kDefaultPathFillType);
bsalomona4817af2016-06-23 11:48:26 -0700427 }
bsalomon728b0f72016-06-27 10:00:19 -0700428 } else if (this->path().isConvex()) {
bsalomona4817af2016-06-23 11:48:26 -0700429 // There is no distinction between even/odd and non-zero winding count for convex
430 // paths.
bsalomon728b0f72016-06-27 10:00:19 -0700431 if (this->path().isInverseFillType()) {
432 this->path().setFillType(kDefaultPathInverseFillType);
bsalomona4817af2016-06-23 11:48:26 -0700433 } else {
bsalomon728b0f72016-06-27 10:00:19 -0700434 this->path().setFillType(kDefaultPathFillType);
bsalomona4817af2016-06-23 11:48:26 -0700435 }
bsalomon93f66bc2016-06-21 08:35:49 -0700436 }
437 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700438 }
439}
440
441void GrShape::attemptToSimplifyRRect() {
442 SkASSERT(Type::kRRect == fType);
443 SkASSERT(!fInheritedKey.count());
bsalomon728b0f72016-06-27 10:00:19 -0700444 if (fRRectData.fRRect.isEmpty()) {
bsalomon1b28c1a2016-06-20 12:28:17 -0700445 fType = Type::kEmpty;
446 return;
447 }
448 if (!this->style().hasPathEffect()) {
bsalomon728b0f72016-06-27 10:00:19 -0700449 fRRectData.fDir = kDefaultRRectDir;
450 fRRectData.fStart = kDefaultRRectStart;
bsalomon1b28c1a2016-06-20 12:28:17 -0700451 } else if (fStyle.isDashed()) {
452 // Dashing ignores the inverseness (currently). skbug.com/5421
bsalomon728b0f72016-06-27 10:00:19 -0700453 fRRectData.fInverted = false;
bsalomon1b28c1a2016-06-20 12:28:17 -0700454 }
bsalomon487f8d32016-07-20 07:15:44 -0700455 // Turn a stroke-and-filled miter rect into a filled rect. TODO: more rrect stroke shortcuts.
456 if (!fStyle.hasPathEffect() &&
457 fStyle.strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style &&
458 fStyle.strokeRec().getJoin() == SkPaint::kMiter_Join &&
459 fStyle.strokeRec().getMiter() >= SK_ScalarSqrt2 &&
460 fRRectData.fRRect.isRect()) {
461 SkScalar r = fStyle.strokeRec().getWidth() / 2;
462 fRRectData.fRRect = SkRRect::MakeRect(fRRectData.fRRect.rect().makeOutset(r, r));
463 fStyle = GrStyle::SimpleFill();
464 }
bsalomonee295642016-06-06 14:01:25 -0700465}
bsalomon0a0f67e2016-06-28 11:56:42 -0700466
467void GrShape::attemptToSimplifyLine() {
bsalomon0ae36a22016-07-18 07:31:13 -0700468 SkASSERT(Type::kLine == fType);
469 SkASSERT(!fInheritedKey.count());
470 if (fStyle.isDashed()) {
471 // Dashing ignores inverseness.
472 fLineData.fInverted = false;
473 return;
474 } else if (fStyle.hasPathEffect()) {
475 return;
476 }
477 if (fStyle.strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style) {
478 // Make stroke + fill be stroke since the fill is empty.
479 SkStrokeRec rec = fStyle.strokeRec();
480 rec.setStrokeStyle(fStyle.strokeRec().getWidth(), false);
481 fStyle = GrStyle(rec, nullptr);
482 }
bsalomon0a0f67e2016-06-28 11:56:42 -0700483 if (fStyle.isSimpleFill() && !fLineData.fInverted) {
484 this->changeType(Type::kEmpty);
bsalomon0ae36a22016-07-18 07:31:13 -0700485 return;
486 }
487 SkPoint* pts = fLineData.fPts;
488 if (fStyle.strokeRec().getStyle() == SkStrokeRec::kStroke_Style) {
489 // If it is horizontal or vertical we will turn it into a filled rrect.
490 SkRect rect;
491 rect.fLeft = SkTMin(pts[0].fX, pts[1].fX);
492 rect.fRight = SkTMax(pts[0].fX, pts[1].fX);
493 rect.fTop = SkTMin(pts[0].fY, pts[1].fY);
494 rect.fBottom = SkTMax(pts[0].fY, pts[1].fY);
495 bool eqX = rect.fLeft == rect.fRight;
496 bool eqY = rect.fTop == rect.fBottom;
497 if (eqX || eqY) {
498 SkScalar r = fStyle.strokeRec().getWidth() / 2;
499 bool inverted = fLineData.fInverted;
500 this->changeType(Type::kRRect);
501 switch (fStyle.strokeRec().getCap()) {
502 case SkPaint::kButt_Cap:
503 if (eqX && eqY) {
504 this->changeType(Type::kEmpty);
505 return;
506 }
507 if (eqX) {
508 rect.outset(r, 0);
509 } else {
510 rect.outset(0, r);
511 }
512 fRRectData.fRRect = SkRRect::MakeRect(rect);
513 break;
514 case SkPaint::kSquare_Cap:
515 rect.outset(r, r);
516 fRRectData.fRRect = SkRRect::MakeRect(rect);
517 break;
518 case SkPaint::kRound_Cap:
519 rect.outset(r, r);
520 fRRectData.fRRect = SkRRect::MakeRectXY(rect, r, r);
521 break;
bsalomon0a0f67e2016-06-28 11:56:42 -0700522 }
bsalomon0ae36a22016-07-18 07:31:13 -0700523 fRRectData.fInverted = inverted;
524 fRRectData.fDir = kDefaultRRectDir;
525 fRRectData.fStart = kDefaultRRectStart;
526 if (fRRectData.fRRect.isEmpty()) {
527 // This can happen when r is very small relative to the rect edges.
528 this->changeType(Type::kEmpty);
529 return;
530 }
531 fStyle = GrStyle::SimpleFill();
532 return;
bsalomon0a0f67e2016-06-28 11:56:42 -0700533 }
534 }
bsalomon0ae36a22016-07-18 07:31:13 -0700535 // Only path effects could care about the order of the points. Otherwise canonicalize
536 // the point order.
537 if (pts[1].fY < pts[0].fY || (pts[1].fY == pts[0].fY && pts[1].fX < pts[0].fX)) {
538 SkTSwap(pts[0], pts[1]);
539 }
bsalomon0a0f67e2016-06-28 11:56:42 -0700540}