blob: 9636427c8eb7145d973aaec7cb6305b4fcd37481 [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;
Brian Osmanf6f7cf62017-09-25 16:49:55 -040012 fOriginalPath = that.fOriginalPath;
bsalomon728b0f72016-06-27 10:00:19 -070013 this->changeType(that.fType, Type::kPath == that.fType ? &that.path() : nullptr);
bsalomon47cc7692016-04-26 12:56:00 -070014 switch (fType) {
15 case Type::kEmpty:
bsalomon47cc7692016-04-26 12:56:00 -070016 break;
Brian Salomon085c0862017-08-31 15:44:51 -040017 case Type::kInvertedEmpty:
18 break;
bsalomon47cc7692016-04-26 12:56:00 -070019 case Type::kRRect:
bsalomon0a0f67e2016-06-28 11:56:42 -070020 fRRectData = that.fRRectData;
21 break;
22 case Type::kLine:
23 fLineData = that.fLineData;
bsalomon47cc7692016-04-26 12:56:00 -070024 break;
25 case Type::kPath:
bsalomon728b0f72016-06-27 10:00:19 -070026 fPathData.fGenID = that.fPathData.fGenID;
bsalomon47cc7692016-04-26 12:56:00 -070027 break;
28 }
29 fInheritedKey.reset(that.fInheritedKey.count());
benjaminwagnerd9cca4a2016-05-04 11:06:19 -070030 sk_careful_memcpy(fInheritedKey.get(), that.fInheritedKey.get(),
31 sizeof(uint32_t) * fInheritedKey.count());
bsalomon47cc7692016-04-26 12:56:00 -070032 return *this;
33}
34
Brian Salomon4f40caf2017-09-01 09:00:45 -040035static bool flip_inversion(bool originalIsInverted, GrShape::FillInversion inversion) {
36 switch (inversion) {
37 case GrShape::FillInversion::kPreserve:
38 return false;
39 case GrShape::FillInversion::kFlip:
40 return true;
41 case GrShape::FillInversion::kForceInverted:
42 return !originalIsInverted;
43 case GrShape::FillInversion::kForceNoninverted:
44 return originalIsInverted;
45 }
46 return false;
47}
48
49static bool is_inverted(bool originalIsInverted, GrShape::FillInversion inversion) {
50 switch (inversion) {
51 case GrShape::FillInversion::kPreserve:
52 return originalIsInverted;
53 case GrShape::FillInversion::kFlip:
54 return !originalIsInverted;
55 case GrShape::FillInversion::kForceInverted:
56 return true;
57 case GrShape::FillInversion::kForceNoninverted:
58 return false;
59 }
60 return false;
61}
62
63GrShape GrShape::MakeFilled(const GrShape& original, FillInversion inversion) {
64 if (original.style().isSimpleFill() && !flip_inversion(original.inverseFilled(), inversion)) {
65 // By returning the original rather than falling through we can preserve any inherited style
66 // key. Otherwise, we wipe it out below since the style change invalidates it.
67 return original;
68 }
69 GrShape result;
Brian Osmanf6f7cf62017-09-25 16:49:55 -040070 result.fOriginalPath = original.fOriginalPath;
Brian Salomon4f40caf2017-09-01 09:00:45 -040071 switch (original.fType) {
72 case Type::kRRect:
73 result.fType = original.fType;
74 result.fRRectData.fRRect = original.fRRectData.fRRect;
75 result.fRRectData.fDir = kDefaultRRectDir;
76 result.fRRectData.fStart = kDefaultRRectStart;
77 result.fRRectData.fInverted = is_inverted(original.fRRectData.fInverted, inversion);
78 break;
79 case Type::kLine:
80 // Lines don't fill.
81 if (is_inverted(original.fLineData.fInverted, inversion)) {
82 result.fType = Type::kInvertedEmpty;
83 } else {
84 result.fType = Type::kEmpty;
85 }
86 break;
87 case Type::kEmpty:
88 result.fType = is_inverted(false, inversion) ? Type::kInvertedEmpty : Type::kEmpty;
89 break;
90 case Type::kInvertedEmpty:
91 result.fType = is_inverted(true, inversion) ? Type::kInvertedEmpty : Type::kEmpty;
92 break;
93 case Type::kPath:
94 result.initType(Type::kPath, &original.fPathData.fPath);
95 result.fPathData.fGenID = original.fPathData.fGenID;
96 if (flip_inversion(original.fPathData.fPath.isInverseFillType(), inversion)) {
97 result.fPathData.fPath.toggleInverseFillType();
98 }
99 if (!original.style().isSimpleFill()) {
100 // Going from a non-filled style to fill may allow additional simplifications (e.g.
101 // closing an open rect that wasn't closed in the original shape because it had
102 // stroke style).
103 result.attemptToSimplifyPath();
104 }
105 break;
106 }
107 // We don't copy the inherited key since it can contain path effect information that we just
108 // stripped.
109 return result;
110}
111
bsalomon0a0f67e2016-06-28 11:56:42 -0700112SkRect GrShape::bounds() const {
bsalomon0ae36a22016-07-18 07:31:13 -0700113 // Bounds where left == bottom or top == right can indicate a line or point shape. We return
114 // inverted bounds for a truly empty shape.
115 static constexpr SkRect kInverted = SkRect::MakeLTRB(1, 1, -1, -1);
bsalomon9fb42032016-05-13 09:23:38 -0700116 switch (fType) {
117 case Type::kEmpty:
bsalomon0ae36a22016-07-18 07:31:13 -0700118 return kInverted;
Brian Salomon085c0862017-08-31 15:44:51 -0400119 case Type::kInvertedEmpty:
120 return kInverted;
bsalomon0a0f67e2016-06-28 11:56:42 -0700121 case Type::kLine: {
122 SkRect bounds;
123 if (fLineData.fPts[0].fX < fLineData.fPts[1].fX) {
124 bounds.fLeft = fLineData.fPts[0].fX;
125 bounds.fRight = fLineData.fPts[1].fX;
126 } else {
127 bounds.fLeft = fLineData.fPts[1].fX;
128 bounds.fRight = fLineData.fPts[0].fX;
129 }
130 if (fLineData.fPts[0].fY < fLineData.fPts[1].fY) {
131 bounds.fTop = fLineData.fPts[0].fY;
132 bounds.fBottom = fLineData.fPts[1].fY;
133 } else {
134 bounds.fTop = fLineData.fPts[1].fY;
135 bounds.fBottom = fLineData.fPts[0].fY;
136 }
137 return bounds;
138 }
bsalomon9fb42032016-05-13 09:23:38 -0700139 case Type::kRRect:
bsalomon728b0f72016-06-27 10:00:19 -0700140 return fRRectData.fRRect.getBounds();
bsalomon9fb42032016-05-13 09:23:38 -0700141 case Type::kPath:
bsalomon728b0f72016-06-27 10:00:19 -0700142 return this->path().getBounds();
bsalomon9fb42032016-05-13 09:23:38 -0700143 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400144 SK_ABORT("Unknown shape type");
bsalomon0ae36a22016-07-18 07:31:13 -0700145 return kInverted;
bsalomon9fb42032016-05-13 09:23:38 -0700146}
147
bsalomon0a0f67e2016-06-28 11:56:42 -0700148SkRect GrShape::styledBounds() const {
Brian Salomon085c0862017-08-31 15:44:51 -0400149 if (this->isEmpty() && !fStyle.hasNonDashPathEffect()) {
bsalomon0a0f67e2016-06-28 11:56:42 -0700150 return SkRect::MakeEmpty();
bsalomon9fb42032016-05-13 09:23:38 -0700151 }
Brian Salomon085c0862017-08-31 15:44:51 -0400152
bsalomon0a0f67e2016-06-28 11:56:42 -0700153 SkRect bounds;
154 fStyle.adjustBounds(&bounds, this->bounds());
155 return bounds;
bsalomon9fb42032016-05-13 09:23:38 -0700156}
157
bsalomon67fa4e32016-09-21 08:26:57 -0700158// If the path is small enough to be keyed from its data this returns key length, otherwise -1.
159static int path_key_from_data_size(const SkPath& path) {
160 const int verbCnt = path.countVerbs();
161 if (verbCnt > GrShape::kMaxKeyFromDataVerbCnt) {
162 return -1;
163 }
164 const int pointCnt = path.countPoints();
165 const int conicWeightCnt = SkPathPriv::ConicWeightCnt(path);
166
167 GR_STATIC_ASSERT(sizeof(SkPoint) == 2 * sizeof(uint32_t));
168 GR_STATIC_ASSERT(sizeof(SkScalar) == sizeof(uint32_t));
169 // 2 is for the verb cnt and a fill type. Each verb is a byte but we'll pad the verb data out to
170 // a uint32_t length.
171 return 2 + (SkAlign4(verbCnt) >> 2) + 2 * pointCnt + conicWeightCnt;
172}
173
174// Writes the path data key into the passed pointer.
bsalomon0e4a4662016-09-21 11:23:46 -0700175static void write_path_key_from_data(const SkPath& path, uint32_t* origKey) {
bsalomon67fa4e32016-09-21 08:26:57 -0700176 uint32_t* key = origKey;
177 // The check below should take care of negative values casted positive.
178 const int verbCnt = path.countVerbs();
179 const int pointCnt = path.countPoints();
180 const int conicWeightCnt = SkPathPriv::ConicWeightCnt(path);
181 SkASSERT(verbCnt <= GrShape::kMaxKeyFromDataVerbCnt);
182 SkASSERT(pointCnt && verbCnt);
183 *key++ = path.getFillType();
184 *key++ = verbCnt;
185 memcpy(key, SkPathPriv::VerbData(path), verbCnt * sizeof(uint8_t));
186 int verbKeySize = SkAlign4(verbCnt);
187 // pad out to uint32_t alignment using value that will stand out when debugging.
188 uint8_t* pad = reinterpret_cast<uint8_t*>(key)+ verbCnt;
189 memset(pad, 0xDE, verbKeySize - verbCnt);
190 key += verbKeySize >> 2;
191
192 memcpy(key, SkPathPriv::PointData(path), sizeof(SkPoint) * pointCnt);
193 GR_STATIC_ASSERT(sizeof(SkPoint) == 2 * sizeof(uint32_t));
194 key += 2 * pointCnt;
bsalomon0e4a4662016-09-21 11:23:46 -0700195 sk_careful_memcpy(key, SkPathPriv::ConicWeightData(path), sizeof(SkScalar) * conicWeightCnt);
bsalomon67fa4e32016-09-21 08:26:57 -0700196 GR_STATIC_ASSERT(sizeof(SkScalar) == sizeof(uint32_t));
197 SkDEBUGCODE(key += conicWeightCnt);
198 SkASSERT(key - origKey == path_key_from_data_size(path));
199}
200
bsalomon47cc7692016-04-26 12:56:00 -0700201int GrShape::unstyledKeySize() const {
202 if (fInheritedKey.count()) {
203 return fInheritedKey.count();
204 }
205 switch (fType) {
206 case Type::kEmpty:
207 return 1;
Brian Salomon085c0862017-08-31 15:44:51 -0400208 case Type::kInvertedEmpty:
209 return 1;
bsalomon47cc7692016-04-26 12:56:00 -0700210 case Type::kRRect:
211 SkASSERT(!fInheritedKey.count());
212 SkASSERT(0 == SkRRect::kSizeInMemory % sizeof(uint32_t));
bsalomon70493962016-06-10 08:05:14 -0700213 // + 1 for the direction, start index, and inverseness.
bsalomonee295642016-06-06 14:01:25 -0700214 return SkRRect::kSizeInMemory / sizeof(uint32_t) + 1;
bsalomon0a0f67e2016-06-28 11:56:42 -0700215 case Type::kLine:
216 GR_STATIC_ASSERT(2 * sizeof(uint32_t) == sizeof(SkPoint));
217 // 4 for the end points and 1 for the inverseness
218 return 5;
bsalomon67fa4e32016-09-21 08:26:57 -0700219 case Type::kPath: {
bsalomonaa840642016-09-23 12:09:16 -0700220 if (0 == fPathData.fGenID) {
221 return -1;
222 }
bsalomon67fa4e32016-09-21 08:26:57 -0700223 int dataKeySize = path_key_from_data_size(fPathData.fPath);
224 if (dataKeySize >= 0) {
225 return dataKeySize;
226 }
bsalomon67fa4e32016-09-21 08:26:57 -0700227 // The key is the path ID and fill type.
228 return 2;
229 }
bsalomon47cc7692016-04-26 12:56:00 -0700230 }
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400231 SK_ABORT("Should never get here.");
bsalomon47cc7692016-04-26 12:56:00 -0700232 return 0;
233}
234
235void GrShape::writeUnstyledKey(uint32_t* key) const {
236 SkASSERT(this->unstyledKeySize());
237 SkDEBUGCODE(uint32_t* origKey = key;)
238 if (fInheritedKey.count()) {
239 memcpy(key, fInheritedKey.get(), sizeof(uint32_t) * fInheritedKey.count());
240 SkDEBUGCODE(key += fInheritedKey.count();)
241 } else {
242 switch (fType) {
243 case Type::kEmpty:
244 *key++ = 1;
245 break;
Brian Salomon085c0862017-08-31 15:44:51 -0400246 case Type::kInvertedEmpty:
247 *key++ = 2;
248 break;
bsalomon47cc7692016-04-26 12:56:00 -0700249 case Type::kRRect:
bsalomon728b0f72016-06-27 10:00:19 -0700250 fRRectData.fRRect.writeToMemory(key);
bsalomon47cc7692016-04-26 12:56:00 -0700251 key += SkRRect::kSizeInMemory / sizeof(uint32_t);
bsalomon728b0f72016-06-27 10:00:19 -0700252 *key = (fRRectData.fDir == SkPath::kCCW_Direction) ? (1 << 31) : 0;
253 *key |= fRRectData.fInverted ? (1 << 30) : 0;
254 *key++ |= fRRectData.fStart;
255 SkASSERT(fRRectData.fStart < 8);
bsalomon47cc7692016-04-26 12:56:00 -0700256 break;
bsalomon0a0f67e2016-06-28 11:56:42 -0700257 case Type::kLine:
258 memcpy(key, fLineData.fPts, 2 * sizeof(SkPoint));
259 key += 4;
260 *key++ = fLineData.fInverted ? 1 : 0;
261 break;
bsalomon67fa4e32016-09-21 08:26:57 -0700262 case Type::kPath: {
bsalomonaa840642016-09-23 12:09:16 -0700263 SkASSERT(fPathData.fGenID);
bsalomon67fa4e32016-09-21 08:26:57 -0700264 int dataKeySize = path_key_from_data_size(fPathData.fPath);
265 if (dataKeySize >= 0) {
bsalomon0e4a4662016-09-21 11:23:46 -0700266 write_path_key_from_data(fPathData.fPath, key);
bsalomon67fa4e32016-09-21 08:26:57 -0700267 return;
268 }
bsalomon728b0f72016-06-27 10:00:19 -0700269 *key++ = fPathData.fGenID;
bsalomon70493962016-06-10 08:05:14 -0700270 // We could canonicalize the fill rule for paths that don't differentiate between
271 // even/odd or winding fill (e.g. convex).
bsalomon728b0f72016-06-27 10:00:19 -0700272 *key++ = this->path().getFillType();
bsalomon47cc7692016-04-26 12:56:00 -0700273 break;
bsalomon67fa4e32016-09-21 08:26:57 -0700274 }
bsalomon47cc7692016-04-26 12:56:00 -0700275 }
276 }
277 SkASSERT(key - origKey == this->unstyledKeySize());
278}
279
bsalomon97fd2d42016-05-09 13:02:01 -0700280void GrShape::setInheritedKey(const GrShape &parent, GrStyle::Apply apply, SkScalar scale) {
bsalomon47cc7692016-04-26 12:56:00 -0700281 SkASSERT(!fInheritedKey.count());
282 // If the output shape turns out to be simple, then we will just use its geometric key
283 if (Type::kPath == fType) {
284 // We want ApplyFullStyle(ApplyPathEffect(shape)) to have the same key as
285 // ApplyFullStyle(shape).
286 // The full key is structured as (geo,path_effect,stroke).
Brian Salomon4f40caf2017-09-01 09:00:45 -0400287 // If we do ApplyPathEffect we get geo,path_effect as the inherited key. If we then
bsalomon47cc7692016-04-26 12:56:00 -0700288 // do ApplyFullStyle we'll memcpy geo,path_effect into the new inherited key
289 // and then append the style key (which should now be stroke only) at the end.
290 int parentCnt = parent.fInheritedKey.count();
291 bool useParentGeoKey = !parentCnt;
292 if (useParentGeoKey) {
293 parentCnt = parent.unstyledKeySize();
bsalomon72dc51c2016-04-27 06:46:23 -0700294 if (parentCnt < 0) {
295 // The parent's geometry has no key so we will have no key.
bsalomon728b0f72016-06-27 10:00:19 -0700296 fPathData.fGenID = 0;
bsalomon72dc51c2016-04-27 06:46:23 -0700297 return;
298 }
bsalomon47cc7692016-04-26 12:56:00 -0700299 }
bsalomon06077562016-05-04 13:50:29 -0700300 uint32_t styleKeyFlags = 0;
301 if (parent.knownToBeClosed()) {
302 styleKeyFlags |= GrStyle::kClosed_KeyFlag;
303 }
bsalomon0ae36a22016-07-18 07:31:13 -0700304 if (parent.asLine(nullptr, nullptr)) {
305 styleKeyFlags |= GrStyle::kNoJoins_KeyFlag;
306 }
bsalomon06077562016-05-04 13:50:29 -0700307 int styleCnt = GrStyle::KeySize(parent.fStyle, apply, styleKeyFlags);
bsalomon47cc7692016-04-26 12:56:00 -0700308 if (styleCnt < 0) {
bsalomon93f66bc2016-06-21 08:35:49 -0700309 // 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 -0700310 // we try to get a key for the shape.
bsalomon728b0f72016-06-27 10:00:19 -0700311 fPathData.fGenID = 0;
bsalomon72dc51c2016-04-27 06:46:23 -0700312 return;
bsalomon47cc7692016-04-26 12:56:00 -0700313 }
bsalomon72dc51c2016-04-27 06:46:23 -0700314 fInheritedKey.reset(parentCnt + styleCnt);
315 if (useParentGeoKey) {
316 // This will be the geo key.
317 parent.writeUnstyledKey(fInheritedKey.get());
318 } else {
319 // This should be (geo,path_effect).
320 memcpy(fInheritedKey.get(), parent.fInheritedKey.get(),
321 parentCnt * sizeof(uint32_t));
322 }
323 // Now turn (geo,path_effect) or (geo) into (geo,path_effect,stroke)
bsalomon97fd2d42016-05-09 13:02:01 -0700324 GrStyle::WriteKey(fInheritedKey.get() + parentCnt, parent.fStyle, apply, scale,
325 styleKeyFlags);
bsalomon47cc7692016-04-26 12:56:00 -0700326 }
327}
328
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400329void GrShape::addGenIDChangeListener(SkPathRef::GenIDChangeListener* listener) const {
330 SkPathPriv::AddGenIDChangeListener(fOriginalPath, listener);
331}
332
333GrShape::GrShape(const GrShape& that) : fStyle(that.fStyle), fOriginalPath(that.fOriginalPath) {
bsalomon728b0f72016-06-27 10:00:19 -0700334 const SkPath* thatPath = Type::kPath == that.fType ? &that.fPathData.fPath : nullptr;
335 this->initType(that.fType, thatPath);
bsalomon47cc7692016-04-26 12:56:00 -0700336 switch (fType) {
337 case Type::kEmpty:
bsalomon93f66bc2016-06-21 08:35:49 -0700338 break;
Brian Salomon085c0862017-08-31 15:44:51 -0400339 case Type::kInvertedEmpty:
340 break;
bsalomon47cc7692016-04-26 12:56:00 -0700341 case Type::kRRect:
bsalomon0a0f67e2016-06-28 11:56:42 -0700342 fRRectData = that.fRRectData;
343 break;
344 case Type::kLine:
345 fLineData = that.fLineData;
bsalomon93f66bc2016-06-21 08:35:49 -0700346 break;
bsalomon47cc7692016-04-26 12:56:00 -0700347 case Type::kPath:
bsalomon728b0f72016-06-27 10:00:19 -0700348 fPathData.fGenID = that.fPathData.fGenID;
bsalomon93f66bc2016-06-21 08:35:49 -0700349 break;
bsalomon47cc7692016-04-26 12:56:00 -0700350 }
351 fInheritedKey.reset(that.fInheritedKey.count());
bsalomon93f66bc2016-06-21 08:35:49 -0700352 sk_careful_memcpy(fInheritedKey.get(), that.fInheritedKey.get(),
353 sizeof(uint32_t) * fInheritedKey.count());
bsalomon47cc7692016-04-26 12:56:00 -0700354}
355
bsalomon97fd2d42016-05-09 13:02:01 -0700356GrShape::GrShape(const GrShape& parent, GrStyle::Apply apply, SkScalar scale) {
357 // TODO: Add some quantization of scale for better cache performance here or leave that up
358 // to caller?
359 // TODO: For certain shapes and stroke params we could ignore the scale. (e.g. miter or bevel
360 // stroke of a rect).
bsalomonfb083272016-05-04 08:27:41 -0700361 if (!parent.style().applies() ||
362 (GrStyle::Apply::kPathEffectOnly == apply && !parent.style().pathEffect())) {
bsalomon728b0f72016-06-27 10:00:19 -0700363 this->initType(Type::kEmpty);
bsalomonfb083272016-05-04 08:27:41 -0700364 *this = parent;
365 return;
366 }
367
bsalomon47cc7692016-04-26 12:56:00 -0700368 SkPathEffect* pe = parent.fStyle.pathEffect();
bsalomonfb083272016-05-04 08:27:41 -0700369 SkTLazy<SkPath> tmpPath;
370 const GrShape* parentForKey = &parent;
371 SkTLazy<GrShape> tmpParent;
bsalomon728b0f72016-06-27 10:00:19 -0700372 this->initType(Type::kPath);
373 fPathData.fGenID = 0;
bsalomon47cc7692016-04-26 12:56:00 -0700374 if (pe) {
bsalomon728b0f72016-06-27 10:00:19 -0700375 const SkPath* srcForPathEffect;
bsalomon47cc7692016-04-26 12:56:00 -0700376 if (parent.fType == Type::kPath) {
bsalomon728b0f72016-06-27 10:00:19 -0700377 srcForPathEffect = &parent.path();
bsalomon47cc7692016-04-26 12:56:00 -0700378 } else {
bsalomonfb083272016-05-04 08:27:41 -0700379 srcForPathEffect = tmpPath.init();
380 parent.asPath(tmpPath.get());
bsalomon47cc7692016-04-26 12:56:00 -0700381 }
382 // Should we consider bounds? Would have to include in key, but it'd be nice to know
383 // if the bounds actually modified anything before including in key.
bsalomonfb083272016-05-04 08:27:41 -0700384 SkStrokeRec strokeRec = parent.fStyle.strokeRec();
bsalomon728b0f72016-06-27 10:00:19 -0700385 if (!parent.fStyle.applyPathEffectToPath(&this->path(), &strokeRec, *srcForPathEffect,
bsalomon398e3f42016-06-13 10:22:48 -0700386 scale)) {
bsalomon0ae36a22016-07-18 07:31:13 -0700387 tmpParent.init(*srcForPathEffect, GrStyle(strokeRec, nullptr));
388 *this = tmpParent.get()->applyStyle(apply, scale);
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400389 fOriginalPath = parent.fOriginalPath;
bsalomon0ae36a22016-07-18 07:31:13 -0700390 return;
bsalomon47cc7692016-04-26 12:56:00 -0700391 }
bsalomon97fd2d42016-05-09 13:02:01 -0700392 // A path effect has access to change the res scale but we aren't expecting it to and it
393 // would mess up our key computation.
394 SkASSERT(scale == strokeRec.getResScale());
bsalomon1b28c1a2016-06-20 12:28:17 -0700395 if (GrStyle::Apply::kPathEffectAndStrokeRec == apply && strokeRec.needToApply()) {
396 // The intermediate shape may not be a general path. If we we're just applying
397 // the path effect then attemptToReduceFromPath would catch it. This means that
398 // when we subsequently applied the remaining strokeRec we would have a non-path
399 // parent shape that would be used to determine the the stroked path's key.
400 // We detect that case here and change parentForKey to a temporary that represents
401 // the simpler shape so that applying both path effect and the strokerec all at
402 // once produces the same key.
bsalomon728b0f72016-06-27 10:00:19 -0700403 tmpParent.init(this->path(), GrStyle(strokeRec, nullptr));
bsalomon1b28c1a2016-06-20 12:28:17 -0700404 tmpParent.get()->setInheritedKey(parent, GrStyle::Apply::kPathEffectOnly, scale);
405 if (!tmpPath.isValid()) {
406 tmpPath.init();
bsalomon72dc51c2016-04-27 06:46:23 -0700407 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700408 tmpParent.get()->asPath(tmpPath.get());
409 SkStrokeRec::InitStyle fillOrHairline;
bsalomon0ae36a22016-07-18 07:31:13 -0700410 // The parent shape may have simplified away the strokeRec, check for that here.
411 if (tmpParent.get()->style().applies()) {
412 SkAssertResult(tmpParent.get()->style().applyToPath(&this->path(), &fillOrHairline,
413 *tmpPath.get(), scale));
414 } else if (tmpParent.get()->style().isSimpleFill()) {
415 fillOrHairline = SkStrokeRec::kFill_InitStyle;
416 } else {
417 SkASSERT(tmpParent.get()->style().isSimpleHairline());
418 fillOrHairline = SkStrokeRec::kHairline_InitStyle;
419 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700420 fStyle.resetToInitStyle(fillOrHairline);
421 parentForKey = tmpParent.get();
bsalomonfb083272016-05-04 08:27:41 -0700422 } else {
423 fStyle = GrStyle(strokeRec, nullptr);
bsalomon72dc51c2016-04-27 06:46:23 -0700424 }
bsalomon47cc7692016-04-26 12:56:00 -0700425 } else {
bsalomon97fd2d42016-05-09 13:02:01 -0700426 const SkPath* srcForParentStyle;
bsalomonfb083272016-05-04 08:27:41 -0700427 if (parent.fType == Type::kPath) {
bsalomon728b0f72016-06-27 10:00:19 -0700428 srcForParentStyle = &parent.path();
bsalomonfb083272016-05-04 08:27:41 -0700429 } else {
bsalomon97fd2d42016-05-09 13:02:01 -0700430 srcForParentStyle = tmpPath.init();
bsalomonfb083272016-05-04 08:27:41 -0700431 parent.asPath(tmpPath.get());
432 }
bsalomon97fd2d42016-05-09 13:02:01 -0700433 SkStrokeRec::InitStyle fillOrHairline;
434 SkASSERT(parent.fStyle.applies());
435 SkASSERT(!parent.fStyle.pathEffect());
bsalomon728b0f72016-06-27 10:00:19 -0700436 SkAssertResult(parent.fStyle.applyToPath(&this->path(), &fillOrHairline, *srcForParentStyle,
bsalomon97fd2d42016-05-09 13:02:01 -0700437 scale));
438 fStyle.resetToInitStyle(fillOrHairline);
bsalomon47cc7692016-04-26 12:56:00 -0700439 }
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400440 fOriginalPath = parent.fOriginalPath;
bsalomon1b28c1a2016-06-20 12:28:17 -0700441 this->attemptToSimplifyPath();
bsalomon97fd2d42016-05-09 13:02:01 -0700442 this->setInheritedKey(*parentForKey, apply, scale);
bsalomon47cc7692016-04-26 12:56:00 -0700443}
bsalomonee295642016-06-06 14:01:25 -0700444
bsalomon1b28c1a2016-06-20 12:28:17 -0700445void GrShape::attemptToSimplifyPath() {
bsalomonee295642016-06-06 14:01:25 -0700446 SkRect rect;
bsalomon728b0f72016-06-27 10:00:19 -0700447 SkRRect rrect;
448 SkPath::Direction rrectDir;
449 unsigned rrectStart;
450 bool inverted = this->path().isInverseFillType();
bsalomon0a0f67e2016-06-28 11:56:42 -0700451 SkPoint pts[2];
bsalomon728b0f72016-06-27 10:00:19 -0700452 if (this->path().isEmpty()) {
Brian Salomon085c0862017-08-31 15:44:51 -0400453 // Dashing ignores inverseness skbug.com/5421.
454 this->changeType(inverted && !this->style().isDashed() ? Type::kInvertedEmpty
455 : Type::kEmpty);
bsalomon0a0f67e2016-06-28 11:56:42 -0700456 } else if (this->path().isLine(pts)) {
457 this->changeType(Type::kLine);
458 fLineData.fPts[0] = pts[0];
459 fLineData.fPts[1] = pts[1];
460 fLineData.fInverted = inverted;
bsalomon728b0f72016-06-27 10:00:19 -0700461 } else if (this->path().isRRect(&rrect, &rrectDir, &rrectStart)) {
462 this->changeType(Type::kRRect);
463 fRRectData.fRRect = rrect;
464 fRRectData.fDir = rrectDir;
465 fRRectData.fStart = rrectStart;
466 fRRectData.fInverted = inverted;
bsalomon728b0f72016-06-27 10:00:19 -0700467 SkASSERT(!fRRectData.fRRect.isEmpty());
bsalomon728b0f72016-06-27 10:00:19 -0700468 } else if (this->path().isOval(&rect, &rrectDir, &rrectStart)) {
469 this->changeType(Type::kRRect);
470 fRRectData.fRRect.setOval(rect);
471 fRRectData.fDir = rrectDir;
472 fRRectData.fInverted = inverted;
bsalomon1b28c1a2016-06-20 12:28:17 -0700473 // convert from oval indexing to rrect indexiing.
bsalomon728b0f72016-06-27 10:00:19 -0700474 fRRectData.fStart = 2 * rrectStart;
475 } else if (SkPathPriv::IsSimpleClosedRect(this->path(), &rect, &rrectDir, &rrectStart)) {
476 this->changeType(Type::kRRect);
bsalomon1b28c1a2016-06-20 12:28:17 -0700477 // When there is a path effect we restrict rect detection to the narrower API that
478 // gives us the starting position. Otherwise, we will retry with the more aggressive
479 // isRect().
bsalomon728b0f72016-06-27 10:00:19 -0700480 fRRectData.fRRect.setRect(rect);
481 fRRectData.fInverted = inverted;
482 fRRectData.fDir = rrectDir;
bsalomon1b28c1a2016-06-20 12:28:17 -0700483 // convert from rect indexing to rrect indexiing.
bsalomon728b0f72016-06-27 10:00:19 -0700484 fRRectData.fStart = 2 * rrectStart;
bsalomon1b28c1a2016-06-20 12:28:17 -0700485 } else if (!this->style().hasPathEffect()) {
bsalomonee295642016-06-06 14:01:25 -0700486 bool closed;
bsalomon728b0f72016-06-27 10:00:19 -0700487 if (this->path().isRect(&rect, &closed, nullptr)) {
bsalomon1b28c1a2016-06-20 12:28:17 -0700488 if (closed || this->style().isSimpleFill()) {
bsalomon728b0f72016-06-27 10:00:19 -0700489 this->changeType(Type::kRRect);
490 fRRectData.fRRect.setRect(rect);
bsalomonee295642016-06-06 14:01:25 -0700491 // Since there is no path effect the dir and start index is immaterial.
bsalomon728b0f72016-06-27 10:00:19 -0700492 fRRectData.fDir = kDefaultRRectDir;
493 fRRectData.fStart = kDefaultRRectStart;
bsalomon1b28c1a2016-06-20 12:28:17 -0700494 // There isn't dashing so we will have to preserver inverseness.
bsalomon728b0f72016-06-27 10:00:19 -0700495 fRRectData.fInverted = inverted;
bsalomonee295642016-06-06 14:01:25 -0700496 }
497 }
498 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700499 if (Type::kPath != fType) {
bsalomon1b28c1a2016-06-20 12:28:17 -0700500 fInheritedKey.reset(0);
501 if (Type::kRRect == fType) {
502 this->attemptToSimplifyRRect();
bsalomon0a0f67e2016-06-28 11:56:42 -0700503 } else if (Type::kLine == fType) {
504 this->attemptToSimplifyLine();
bsalomon1b28c1a2016-06-20 12:28:17 -0700505 }
bsalomon93f66bc2016-06-21 08:35:49 -0700506 } else {
bsalomon728b0f72016-06-27 10:00:19 -0700507 if (fInheritedKey.count() || this->path().isVolatile()) {
508 fPathData.fGenID = 0;
bsalomon93f66bc2016-06-21 08:35:49 -0700509 } else {
bsalomon728b0f72016-06-27 10:00:19 -0700510 fPathData.fGenID = this->path().getGenerationID();
bsalomon93f66bc2016-06-21 08:35:49 -0700511 }
bsalomona4817af2016-06-23 11:48:26 -0700512 if (!this->style().hasNonDashPathEffect()) {
513 if (this->style().strokeRec().getStyle() == SkStrokeRec::kStroke_Style ||
514 this->style().strokeRec().getStyle() == SkStrokeRec::kHairline_Style) {
515 // Stroke styles don't differentiate between winding and even/odd.
516 // Moreover, dashing ignores inverseness (skbug.com/5421)
bsalomon728b0f72016-06-27 10:00:19 -0700517 bool inverse = !this->style().isDashed() && this->path().isInverseFillType();
bsalomona4817af2016-06-23 11:48:26 -0700518 if (inverse) {
bsalomon728b0f72016-06-27 10:00:19 -0700519 this->path().setFillType(kDefaultPathInverseFillType);
bsalomona4817af2016-06-23 11:48:26 -0700520 } else {
bsalomon728b0f72016-06-27 10:00:19 -0700521 this->path().setFillType(kDefaultPathFillType);
bsalomona4817af2016-06-23 11:48:26 -0700522 }
bsalomon728b0f72016-06-27 10:00:19 -0700523 } else if (this->path().isConvex()) {
bsalomona4817af2016-06-23 11:48:26 -0700524 // There is no distinction between even/odd and non-zero winding count for convex
525 // paths.
bsalomon728b0f72016-06-27 10:00:19 -0700526 if (this->path().isInverseFillType()) {
527 this->path().setFillType(kDefaultPathInverseFillType);
bsalomona4817af2016-06-23 11:48:26 -0700528 } else {
bsalomon728b0f72016-06-27 10:00:19 -0700529 this->path().setFillType(kDefaultPathFillType);
bsalomona4817af2016-06-23 11:48:26 -0700530 }
bsalomon93f66bc2016-06-21 08:35:49 -0700531 }
532 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700533 }
534}
535
536void GrShape::attemptToSimplifyRRect() {
537 SkASSERT(Type::kRRect == fType);
538 SkASSERT(!fInheritedKey.count());
bsalomon728b0f72016-06-27 10:00:19 -0700539 if (fRRectData.fRRect.isEmpty()) {
Brian Salomon085c0862017-08-31 15:44:51 -0400540 // Dashing ignores the inverseness currently. skbug.com/5421
541 fType = fRRectData.fInverted && !fStyle.isDashed() ? Type::kInvertedEmpty : Type::kEmpty;
bsalomon1b28c1a2016-06-20 12:28:17 -0700542 return;
543 }
544 if (!this->style().hasPathEffect()) {
bsalomon728b0f72016-06-27 10:00:19 -0700545 fRRectData.fDir = kDefaultRRectDir;
546 fRRectData.fStart = kDefaultRRectStart;
bsalomon1b28c1a2016-06-20 12:28:17 -0700547 } else if (fStyle.isDashed()) {
548 // Dashing ignores the inverseness (currently). skbug.com/5421
bsalomon728b0f72016-06-27 10:00:19 -0700549 fRRectData.fInverted = false;
bsalomon1b28c1a2016-06-20 12:28:17 -0700550 }
bsalomon487f8d32016-07-20 07:15:44 -0700551 // Turn a stroke-and-filled miter rect into a filled rect. TODO: more rrect stroke shortcuts.
552 if (!fStyle.hasPathEffect() &&
553 fStyle.strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style &&
554 fStyle.strokeRec().getJoin() == SkPaint::kMiter_Join &&
555 fStyle.strokeRec().getMiter() >= SK_ScalarSqrt2 &&
556 fRRectData.fRRect.isRect()) {
557 SkScalar r = fStyle.strokeRec().getWidth() / 2;
558 fRRectData.fRRect = SkRRect::MakeRect(fRRectData.fRRect.rect().makeOutset(r, r));
559 fStyle = GrStyle::SimpleFill();
560 }
bsalomonee295642016-06-06 14:01:25 -0700561}
bsalomon0a0f67e2016-06-28 11:56:42 -0700562
563void GrShape::attemptToSimplifyLine() {
bsalomon0ae36a22016-07-18 07:31:13 -0700564 SkASSERT(Type::kLine == fType);
565 SkASSERT(!fInheritedKey.count());
566 if (fStyle.isDashed()) {
567 // Dashing ignores inverseness.
568 fLineData.fInverted = false;
569 return;
570 } else if (fStyle.hasPathEffect()) {
571 return;
572 }
573 if (fStyle.strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style) {
574 // Make stroke + fill be stroke since the fill is empty.
575 SkStrokeRec rec = fStyle.strokeRec();
576 rec.setStrokeStyle(fStyle.strokeRec().getWidth(), false);
577 fStyle = GrStyle(rec, nullptr);
578 }
Brian Salomon085c0862017-08-31 15:44:51 -0400579 if (fStyle.isSimpleFill()) {
580 this->changeType(fLineData.fInverted ? Type::kInvertedEmpty : Type::kEmpty);
bsalomon0ae36a22016-07-18 07:31:13 -0700581 return;
582 }
583 SkPoint* pts = fLineData.fPts;
584 if (fStyle.strokeRec().getStyle() == SkStrokeRec::kStroke_Style) {
585 // If it is horizontal or vertical we will turn it into a filled rrect.
586 SkRect rect;
587 rect.fLeft = SkTMin(pts[0].fX, pts[1].fX);
588 rect.fRight = SkTMax(pts[0].fX, pts[1].fX);
589 rect.fTop = SkTMin(pts[0].fY, pts[1].fY);
590 rect.fBottom = SkTMax(pts[0].fY, pts[1].fY);
591 bool eqX = rect.fLeft == rect.fRight;
592 bool eqY = rect.fTop == rect.fBottom;
593 if (eqX || eqY) {
594 SkScalar r = fStyle.strokeRec().getWidth() / 2;
595 bool inverted = fLineData.fInverted;
596 this->changeType(Type::kRRect);
597 switch (fStyle.strokeRec().getCap()) {
598 case SkPaint::kButt_Cap:
599 if (eqX && eqY) {
600 this->changeType(Type::kEmpty);
601 return;
602 }
603 if (eqX) {
604 rect.outset(r, 0);
605 } else {
606 rect.outset(0, r);
607 }
608 fRRectData.fRRect = SkRRect::MakeRect(rect);
609 break;
610 case SkPaint::kSquare_Cap:
611 rect.outset(r, r);
612 fRRectData.fRRect = SkRRect::MakeRect(rect);
613 break;
614 case SkPaint::kRound_Cap:
615 rect.outset(r, r);
616 fRRectData.fRRect = SkRRect::MakeRectXY(rect, r, r);
617 break;
bsalomon0a0f67e2016-06-28 11:56:42 -0700618 }
bsalomon0ae36a22016-07-18 07:31:13 -0700619 fRRectData.fInverted = inverted;
620 fRRectData.fDir = kDefaultRRectDir;
621 fRRectData.fStart = kDefaultRRectStart;
622 if (fRRectData.fRRect.isEmpty()) {
623 // This can happen when r is very small relative to the rect edges.
Brian Salomon085c0862017-08-31 15:44:51 -0400624 this->changeType(inverted ? Type::kInvertedEmpty : Type::kEmpty);
bsalomon0ae36a22016-07-18 07:31:13 -0700625 return;
626 }
627 fStyle = GrStyle::SimpleFill();
628 return;
bsalomon0a0f67e2016-06-28 11:56:42 -0700629 }
630 }
bsalomon0ae36a22016-07-18 07:31:13 -0700631 // Only path effects could care about the order of the points. Otherwise canonicalize
632 // the point order.
633 if (pts[1].fY < pts[0].fY || (pts[1].fY == pts[0].fY && pts[1].fX < pts[0].fX)) {
634 SkTSwap(pts[0], pts[1]);
635 }
bsalomon0a0f67e2016-06-28 11:56:42 -0700636}