blob: 91cd3c8eb1e88c264c4c1c6ff01618824582bfc4 [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);
389 return;
bsalomon47cc7692016-04-26 12:56:00 -0700390 }
bsalomon97fd2d42016-05-09 13:02:01 -0700391 // A path effect has access to change the res scale but we aren't expecting it to and it
392 // would mess up our key computation.
393 SkASSERT(scale == strokeRec.getResScale());
bsalomon1b28c1a2016-06-20 12:28:17 -0700394 if (GrStyle::Apply::kPathEffectAndStrokeRec == apply && strokeRec.needToApply()) {
395 // The intermediate shape may not be a general path. If we we're just applying
396 // the path effect then attemptToReduceFromPath would catch it. This means that
397 // when we subsequently applied the remaining strokeRec we would have a non-path
398 // parent shape that would be used to determine the the stroked path's key.
399 // We detect that case here and change parentForKey to a temporary that represents
400 // the simpler shape so that applying both path effect and the strokerec all at
401 // once produces the same key.
bsalomon728b0f72016-06-27 10:00:19 -0700402 tmpParent.init(this->path(), GrStyle(strokeRec, nullptr));
bsalomon1b28c1a2016-06-20 12:28:17 -0700403 tmpParent.get()->setInheritedKey(parent, GrStyle::Apply::kPathEffectOnly, scale);
404 if (!tmpPath.isValid()) {
405 tmpPath.init();
bsalomon72dc51c2016-04-27 06:46:23 -0700406 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700407 tmpParent.get()->asPath(tmpPath.get());
408 SkStrokeRec::InitStyle fillOrHairline;
bsalomon0ae36a22016-07-18 07:31:13 -0700409 // The parent shape may have simplified away the strokeRec, check for that here.
410 if (tmpParent.get()->style().applies()) {
411 SkAssertResult(tmpParent.get()->style().applyToPath(&this->path(), &fillOrHairline,
412 *tmpPath.get(), scale));
413 } else if (tmpParent.get()->style().isSimpleFill()) {
414 fillOrHairline = SkStrokeRec::kFill_InitStyle;
415 } else {
416 SkASSERT(tmpParent.get()->style().isSimpleHairline());
417 fillOrHairline = SkStrokeRec::kHairline_InitStyle;
418 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700419 fStyle.resetToInitStyle(fillOrHairline);
420 parentForKey = tmpParent.get();
bsalomonfb083272016-05-04 08:27:41 -0700421 } else {
422 fStyle = GrStyle(strokeRec, nullptr);
bsalomon72dc51c2016-04-27 06:46:23 -0700423 }
bsalomon47cc7692016-04-26 12:56:00 -0700424 } else {
bsalomon97fd2d42016-05-09 13:02:01 -0700425 const SkPath* srcForParentStyle;
bsalomonfb083272016-05-04 08:27:41 -0700426 if (parent.fType == Type::kPath) {
bsalomon728b0f72016-06-27 10:00:19 -0700427 srcForParentStyle = &parent.path();
bsalomonfb083272016-05-04 08:27:41 -0700428 } else {
bsalomon97fd2d42016-05-09 13:02:01 -0700429 srcForParentStyle = tmpPath.init();
bsalomonfb083272016-05-04 08:27:41 -0700430 parent.asPath(tmpPath.get());
431 }
bsalomon97fd2d42016-05-09 13:02:01 -0700432 SkStrokeRec::InitStyle fillOrHairline;
433 SkASSERT(parent.fStyle.applies());
434 SkASSERT(!parent.fStyle.pathEffect());
bsalomon728b0f72016-06-27 10:00:19 -0700435 SkAssertResult(parent.fStyle.applyToPath(&this->path(), &fillOrHairline, *srcForParentStyle,
bsalomon97fd2d42016-05-09 13:02:01 -0700436 scale));
437 fStyle.resetToInitStyle(fillOrHairline);
bsalomon47cc7692016-04-26 12:56:00 -0700438 }
Brian Osmanf6f7cf62017-09-25 16:49:55 -0400439 fOriginalPath = parent.fOriginalPath;
bsalomon1b28c1a2016-06-20 12:28:17 -0700440 this->attemptToSimplifyPath();
bsalomon97fd2d42016-05-09 13:02:01 -0700441 this->setInheritedKey(*parentForKey, apply, scale);
bsalomon47cc7692016-04-26 12:56:00 -0700442}
bsalomonee295642016-06-06 14:01:25 -0700443
bsalomon1b28c1a2016-06-20 12:28:17 -0700444void GrShape::attemptToSimplifyPath() {
bsalomonee295642016-06-06 14:01:25 -0700445 SkRect rect;
bsalomon728b0f72016-06-27 10:00:19 -0700446 SkRRect rrect;
447 SkPath::Direction rrectDir;
448 unsigned rrectStart;
449 bool inverted = this->path().isInverseFillType();
bsalomon0a0f67e2016-06-28 11:56:42 -0700450 SkPoint pts[2];
bsalomon728b0f72016-06-27 10:00:19 -0700451 if (this->path().isEmpty()) {
Brian Salomon085c0862017-08-31 15:44:51 -0400452 // Dashing ignores inverseness skbug.com/5421.
453 this->changeType(inverted && !this->style().isDashed() ? Type::kInvertedEmpty
454 : Type::kEmpty);
bsalomon0a0f67e2016-06-28 11:56:42 -0700455 } else if (this->path().isLine(pts)) {
456 this->changeType(Type::kLine);
457 fLineData.fPts[0] = pts[0];
458 fLineData.fPts[1] = pts[1];
459 fLineData.fInverted = inverted;
bsalomon728b0f72016-06-27 10:00:19 -0700460 } else if (this->path().isRRect(&rrect, &rrectDir, &rrectStart)) {
461 this->changeType(Type::kRRect);
462 fRRectData.fRRect = rrect;
463 fRRectData.fDir = rrectDir;
464 fRRectData.fStart = rrectStart;
465 fRRectData.fInverted = inverted;
bsalomon728b0f72016-06-27 10:00:19 -0700466 SkASSERT(!fRRectData.fRRect.isEmpty());
bsalomon728b0f72016-06-27 10:00:19 -0700467 } else if (this->path().isOval(&rect, &rrectDir, &rrectStart)) {
468 this->changeType(Type::kRRect);
469 fRRectData.fRRect.setOval(rect);
470 fRRectData.fDir = rrectDir;
471 fRRectData.fInverted = inverted;
bsalomon1b28c1a2016-06-20 12:28:17 -0700472 // convert from oval indexing to rrect indexiing.
bsalomon728b0f72016-06-27 10:00:19 -0700473 fRRectData.fStart = 2 * rrectStart;
474 } else if (SkPathPriv::IsSimpleClosedRect(this->path(), &rect, &rrectDir, &rrectStart)) {
475 this->changeType(Type::kRRect);
bsalomon1b28c1a2016-06-20 12:28:17 -0700476 // When there is a path effect we restrict rect detection to the narrower API that
477 // gives us the starting position. Otherwise, we will retry with the more aggressive
478 // isRect().
bsalomon728b0f72016-06-27 10:00:19 -0700479 fRRectData.fRRect.setRect(rect);
480 fRRectData.fInverted = inverted;
481 fRRectData.fDir = rrectDir;
bsalomon1b28c1a2016-06-20 12:28:17 -0700482 // convert from rect indexing to rrect indexiing.
bsalomon728b0f72016-06-27 10:00:19 -0700483 fRRectData.fStart = 2 * rrectStart;
bsalomon1b28c1a2016-06-20 12:28:17 -0700484 } else if (!this->style().hasPathEffect()) {
bsalomonee295642016-06-06 14:01:25 -0700485 bool closed;
bsalomon728b0f72016-06-27 10:00:19 -0700486 if (this->path().isRect(&rect, &closed, nullptr)) {
bsalomon1b28c1a2016-06-20 12:28:17 -0700487 if (closed || this->style().isSimpleFill()) {
bsalomon728b0f72016-06-27 10:00:19 -0700488 this->changeType(Type::kRRect);
489 fRRectData.fRRect.setRect(rect);
bsalomonee295642016-06-06 14:01:25 -0700490 // Since there is no path effect the dir and start index is immaterial.
bsalomon728b0f72016-06-27 10:00:19 -0700491 fRRectData.fDir = kDefaultRRectDir;
492 fRRectData.fStart = kDefaultRRectStart;
bsalomon1b28c1a2016-06-20 12:28:17 -0700493 // There isn't dashing so we will have to preserver inverseness.
bsalomon728b0f72016-06-27 10:00:19 -0700494 fRRectData.fInverted = inverted;
bsalomonee295642016-06-06 14:01:25 -0700495 }
496 }
497 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700498 if (Type::kPath != fType) {
bsalomon1b28c1a2016-06-20 12:28:17 -0700499 fInheritedKey.reset(0);
Brian Osmanb379dcd2017-10-04 15:44:05 -0400500 // Whenever we simplify to a non-path, break the chain so we no longer refer to the
501 // original path. This prevents attaching genID listeners to temporary paths created when
502 // drawing simple shapes.
503 fOriginalPath.reset();
bsalomon1b28c1a2016-06-20 12:28:17 -0700504 if (Type::kRRect == fType) {
505 this->attemptToSimplifyRRect();
bsalomon0a0f67e2016-06-28 11:56:42 -0700506 } else if (Type::kLine == fType) {
507 this->attemptToSimplifyLine();
bsalomon1b28c1a2016-06-20 12:28:17 -0700508 }
bsalomon93f66bc2016-06-21 08:35:49 -0700509 } else {
bsalomon728b0f72016-06-27 10:00:19 -0700510 if (fInheritedKey.count() || this->path().isVolatile()) {
511 fPathData.fGenID = 0;
bsalomon93f66bc2016-06-21 08:35:49 -0700512 } else {
bsalomon728b0f72016-06-27 10:00:19 -0700513 fPathData.fGenID = this->path().getGenerationID();
bsalomon93f66bc2016-06-21 08:35:49 -0700514 }
bsalomona4817af2016-06-23 11:48:26 -0700515 if (!this->style().hasNonDashPathEffect()) {
516 if (this->style().strokeRec().getStyle() == SkStrokeRec::kStroke_Style ||
517 this->style().strokeRec().getStyle() == SkStrokeRec::kHairline_Style) {
518 // Stroke styles don't differentiate between winding and even/odd.
519 // Moreover, dashing ignores inverseness (skbug.com/5421)
bsalomon728b0f72016-06-27 10:00:19 -0700520 bool inverse = !this->style().isDashed() && this->path().isInverseFillType();
bsalomona4817af2016-06-23 11:48:26 -0700521 if (inverse) {
bsalomon728b0f72016-06-27 10:00:19 -0700522 this->path().setFillType(kDefaultPathInverseFillType);
bsalomona4817af2016-06-23 11:48:26 -0700523 } else {
bsalomon728b0f72016-06-27 10:00:19 -0700524 this->path().setFillType(kDefaultPathFillType);
bsalomona4817af2016-06-23 11:48:26 -0700525 }
bsalomon728b0f72016-06-27 10:00:19 -0700526 } else if (this->path().isConvex()) {
bsalomona4817af2016-06-23 11:48:26 -0700527 // There is no distinction between even/odd and non-zero winding count for convex
528 // paths.
bsalomon728b0f72016-06-27 10:00:19 -0700529 if (this->path().isInverseFillType()) {
530 this->path().setFillType(kDefaultPathInverseFillType);
bsalomona4817af2016-06-23 11:48:26 -0700531 } else {
bsalomon728b0f72016-06-27 10:00:19 -0700532 this->path().setFillType(kDefaultPathFillType);
bsalomona4817af2016-06-23 11:48:26 -0700533 }
bsalomon93f66bc2016-06-21 08:35:49 -0700534 }
535 }
bsalomon1b28c1a2016-06-20 12:28:17 -0700536 }
537}
538
539void GrShape::attemptToSimplifyRRect() {
540 SkASSERT(Type::kRRect == fType);
541 SkASSERT(!fInheritedKey.count());
bsalomon728b0f72016-06-27 10:00:19 -0700542 if (fRRectData.fRRect.isEmpty()) {
Brian Salomon085c0862017-08-31 15:44:51 -0400543 // Dashing ignores the inverseness currently. skbug.com/5421
544 fType = fRRectData.fInverted && !fStyle.isDashed() ? Type::kInvertedEmpty : Type::kEmpty;
bsalomon1b28c1a2016-06-20 12:28:17 -0700545 return;
546 }
547 if (!this->style().hasPathEffect()) {
bsalomon728b0f72016-06-27 10:00:19 -0700548 fRRectData.fDir = kDefaultRRectDir;
549 fRRectData.fStart = kDefaultRRectStart;
bsalomon1b28c1a2016-06-20 12:28:17 -0700550 } else if (fStyle.isDashed()) {
551 // Dashing ignores the inverseness (currently). skbug.com/5421
bsalomon728b0f72016-06-27 10:00:19 -0700552 fRRectData.fInverted = false;
bsalomon1b28c1a2016-06-20 12:28:17 -0700553 }
bsalomon487f8d32016-07-20 07:15:44 -0700554 // Turn a stroke-and-filled miter rect into a filled rect. TODO: more rrect stroke shortcuts.
555 if (!fStyle.hasPathEffect() &&
556 fStyle.strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style &&
557 fStyle.strokeRec().getJoin() == SkPaint::kMiter_Join &&
558 fStyle.strokeRec().getMiter() >= SK_ScalarSqrt2 &&
559 fRRectData.fRRect.isRect()) {
560 SkScalar r = fStyle.strokeRec().getWidth() / 2;
561 fRRectData.fRRect = SkRRect::MakeRect(fRRectData.fRRect.rect().makeOutset(r, r));
562 fStyle = GrStyle::SimpleFill();
563 }
bsalomonee295642016-06-06 14:01:25 -0700564}
bsalomon0a0f67e2016-06-28 11:56:42 -0700565
566void GrShape::attemptToSimplifyLine() {
bsalomon0ae36a22016-07-18 07:31:13 -0700567 SkASSERT(Type::kLine == fType);
568 SkASSERT(!fInheritedKey.count());
569 if (fStyle.isDashed()) {
570 // Dashing ignores inverseness.
571 fLineData.fInverted = false;
572 return;
573 } else if (fStyle.hasPathEffect()) {
574 return;
575 }
576 if (fStyle.strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style) {
577 // Make stroke + fill be stroke since the fill is empty.
578 SkStrokeRec rec = fStyle.strokeRec();
579 rec.setStrokeStyle(fStyle.strokeRec().getWidth(), false);
580 fStyle = GrStyle(rec, nullptr);
581 }
Brian Salomon085c0862017-08-31 15:44:51 -0400582 if (fStyle.isSimpleFill()) {
583 this->changeType(fLineData.fInverted ? Type::kInvertedEmpty : Type::kEmpty);
bsalomon0ae36a22016-07-18 07:31:13 -0700584 return;
585 }
586 SkPoint* pts = fLineData.fPts;
587 if (fStyle.strokeRec().getStyle() == SkStrokeRec::kStroke_Style) {
588 // If it is horizontal or vertical we will turn it into a filled rrect.
589 SkRect rect;
590 rect.fLeft = SkTMin(pts[0].fX, pts[1].fX);
591 rect.fRight = SkTMax(pts[0].fX, pts[1].fX);
592 rect.fTop = SkTMin(pts[0].fY, pts[1].fY);
593 rect.fBottom = SkTMax(pts[0].fY, pts[1].fY);
594 bool eqX = rect.fLeft == rect.fRight;
595 bool eqY = rect.fTop == rect.fBottom;
596 if (eqX || eqY) {
597 SkScalar r = fStyle.strokeRec().getWidth() / 2;
598 bool inverted = fLineData.fInverted;
599 this->changeType(Type::kRRect);
600 switch (fStyle.strokeRec().getCap()) {
601 case SkPaint::kButt_Cap:
602 if (eqX && eqY) {
603 this->changeType(Type::kEmpty);
604 return;
605 }
606 if (eqX) {
607 rect.outset(r, 0);
608 } else {
609 rect.outset(0, r);
610 }
611 fRRectData.fRRect = SkRRect::MakeRect(rect);
612 break;
613 case SkPaint::kSquare_Cap:
614 rect.outset(r, r);
615 fRRectData.fRRect = SkRRect::MakeRect(rect);
616 break;
617 case SkPaint::kRound_Cap:
618 rect.outset(r, r);
619 fRRectData.fRRect = SkRRect::MakeRectXY(rect, r, r);
620 break;
bsalomon0a0f67e2016-06-28 11:56:42 -0700621 }
bsalomon0ae36a22016-07-18 07:31:13 -0700622 fRRectData.fInverted = inverted;
623 fRRectData.fDir = kDefaultRRectDir;
624 fRRectData.fStart = kDefaultRRectStart;
625 if (fRRectData.fRRect.isEmpty()) {
626 // This can happen when r is very small relative to the rect edges.
Brian Salomon085c0862017-08-31 15:44:51 -0400627 this->changeType(inverted ? Type::kInvertedEmpty : Type::kEmpty);
bsalomon0ae36a22016-07-18 07:31:13 -0700628 return;
629 }
630 fStyle = GrStyle::SimpleFill();
631 return;
bsalomon0a0f67e2016-06-28 11:56:42 -0700632 }
633 }
bsalomon0ae36a22016-07-18 07:31:13 -0700634 // Only path effects could care about the order of the points. Otherwise canonicalize
635 // the point order.
636 if (pts[1].fY < pts[0].fY || (pts[1].fY == pts[0].fY && pts[1].fX < pts[0].fX)) {
637 SkTSwap(pts[0], pts[1]);
638 }
bsalomon0a0f67e2016-06-28 11:56:42 -0700639}