Debian Codegraph @

src/gpu/ganesh/geometry/GrStyledShape.cpp

raw
1 /*
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 #include "src/gpu/ganesh/geometry/GrStyledShape.h"
8
9 #include "include/core/SkArc.h"
10 #include "include/core/SkPaint.h"
11 #include "include/core/SkPathEffect.h"
12 #include "include/core/SkPoint.h"
13 #include "include/core/SkStrokeRec.h"
14 #include "include/private/SkIDChangeListener.h"
15 #include "include/private/base/SkAlign.h"
16 #include "include/private/base/SkDebug.h"
17 #include "include/private/base/SkMalloc.h"
18 #include "include/private/base/SkTo.h"
19
20 #include <algorithm>
21 #include <cstring>
22 #include <limits>
23 #include <utility>
24
25
26 GrStyledShape& GrStyledShape::operator=(const GrStyledShape& that) {
27 fShape = that.fShape;
28 fStyle = that.fStyle;
29 fGenID = that.fGenID;
30 fSimplified = that.fSimplified;
31
32 fInheritedKey.reset(that.fInheritedKey.count());
33 sk_careful_memcpy(fInheritedKey.get(), that.fInheritedKey.get(),
34 sizeof(uint32_t) * fInheritedKey.count());
35 if (that.fInheritedPathForListeners.has_value()) {
36 fInheritedPathForListeners = *that.fInheritedPathForListeners;
37 } else {
38 fInheritedPathForListeners.reset();
39 }
40 return *this;
41 }
42
43 static bool is_inverted(bool originalIsInverted, GrStyledShape::FillInversion inversion) {
44 switch (inversion) {
45 case GrStyledShape::FillInversion::kPreserve:
46 return originalIsInverted;
47 case GrStyledShape::FillInversion::kFlip:
48 return !originalIsInverted;
49 case GrStyledShape::FillInversion::kForceInverted:
50 return true;
51 case GrStyledShape::FillInversion::kForceNoninverted:
52 return false;
53 }
54 return false;
55 }
56
57 GrStyledShape GrStyledShape::MakeFilled(const GrStyledShape& original, FillInversion inversion) {
58 bool newIsInverted = is_inverted(original.fShape.inverted(), inversion);
59 if (original.style().isSimpleFill() && newIsInverted == original.fShape.inverted()) {
60 // By returning the original rather than falling through we can preserve any inherited style
61 // key. Otherwise, we wipe it out below since the style change invalidates it.
62 return original;
63 }
64 GrStyledShape result;
65 SkASSERT(result.fStyle.isSimpleFill());
66 if (original.fInheritedPathForListeners.has_value()) {
67 result.fInheritedPathForListeners = *original.fInheritedPathForListeners;
68 }
69
70 result.fShape = original.fShape;
71 result.fGenID = original.fGenID;
72 result.fShape.setInverted(newIsInverted);
73
74 if (!original.style().isSimpleFill()) {
75 // Going from a non-filled style to fill may allow additional simplifications (e.g.
76 // closing an open rect that wasn't closed in the original shape because it had
77 // stroke style).
78 result.simplify();
79 // The above simplify() call only sets simplified to true if its geometry was changed,
80 // since it already sees its style as a simple fill. Since the original style was not a
81 // simple fill, MakeFilled always simplifies.
82 result.fSimplified = true;
83 }
84
85 // Verify that lines/points were converted to empty by the style change
86 SkASSERT((!original.fShape.isLine() && !original.fShape.isPoint()) || result.fShape.isEmpty());
87
88 // We don't copy the inherited key since it can contain path effect information that we just
89 // stripped.
90 return result;
91 }
92
93 SkRect GrStyledShape::styledBounds() const {
94 if (this->isEmpty() && !fStyle.hasNonDashPathEffect()) {
95 return SkRect::MakeEmpty();
96 }
97
98 SkRect bounds;
99 fStyle.adjustBounds(&bounds, this->bounds());
100 return bounds;
101 }
102
103 // If the path is small enough to be keyed from its data this returns key length, otherwise -1.
104 static int path_key_from_data_size(const SkPath& path) {
105 const int verbCnt = path.countVerbs();
106 if (verbCnt > GrStyledShape::kMaxKeyFromDataVerbCnt) {
107 return -1;
108 }
109 const size_t pointCnt = path.points().size();
110 const size_t conicWeightCnt = path.conicWeights().size();
111
112 static_assert(sizeof(SkPoint) == 2 * sizeof(uint32_t));
113 static_assert(sizeof(SkScalar) == sizeof(uint32_t));
114 // 1 is for the verb count. Each verb is a byte but we'll pad the verb data out to
115 // a uint32_t length.
116 return 1 + (SkAlign4(verbCnt) >> 2) + 2 * pointCnt + conicWeightCnt;
117 }
118
119 // Writes the path data key into the passed pointer.
120 static void write_path_key_from_data(const SkPath& path, uint32_t* origKey) {
121 uint32_t* key = origKey;
122 // The check below should take care of negative values casted positive.
123 SkSpan<const SkPathVerb> verbs = path.verbs();
124 SkSpan<const SkPoint> points = path.points();
125 SkSpan<const float> conics = path.conicWeights();
126 SkASSERT(verbs.size() <= GrStyledShape::kMaxKeyFromDataVerbCnt);
127 SkASSERT(points.size() && verbs.size());
128 *key++ = SkToInt(verbs.size());
129 memcpy(key, verbs.data(), verbs.size_bytes());
130 const size_t verbKeySize = SkAlign4(verbs.size());
131 // pad out to uint32_t alignment using value that will stand out when debugging.
132 uint8_t* pad = reinterpret_cast<uint8_t*>(key)+ verbs.size();
133 memset(pad, 0xDE, verbKeySize - verbs.size());
134 key += verbKeySize >> 2;
135
136 memcpy(key, points.data(), points.size_bytes());
137 static_assert(sizeof(SkPoint) == 2 * sizeof(uint32_t));
138 key += 2 * points.size();
139 sk_careful_memcpy(key, conics.data(), conics.size_bytes());
140 static_assert(sizeof(SkScalar) == sizeof(uint32_t));
141 SkDEBUGCODE(key += conics.size());
142 SkASSERT(key - origKey == path_key_from_data_size(path));
143 }
144
145 uint16_t GrStyledShape::unstyledKeySize() const {
146 if (fInheritedKey.count()) {
147 return SkTo<uint16_t>(fInheritedKey.count());
148 }
149
150 uint16_t count = 1; // Every key has the state flags from the GrShape
151 switch(fShape.type()) {
152 case GrShape::Type::kPoint:
153 static_assert(0 == sizeof(SkPoint) % sizeof(uint32_t));
154 count += sizeof(SkPoint) / sizeof(uint32_t);
155 break;
156 case GrShape::Type::kRect:
157 static_assert(0 == sizeof(SkRect) % sizeof(uint32_t));
158 count += sizeof(SkRect) / sizeof(uint32_t);
159 break;
160 case GrShape::Type::kRRect:
161 static_assert(0 == SkRRect::kSizeInMemory % sizeof(uint32_t));
162 count += SkRRect::kSizeInMemory / sizeof(uint32_t);
163 break;
164 case GrShape::Type::kArc:
165 static_assert(0 == sizeof(SkArc) % sizeof(uint32_t));
166 count += sizeof(SkArc) / sizeof(uint32_t);
167 break;
168 case GrShape::Type::kLine:
169 static_assert(0 == sizeof(GrLineSegment) % sizeof(uint32_t));
170 count += sizeof(GrLineSegment) / sizeof(uint32_t);
171 break;
172 case GrShape::Type::kPath: {
173 if (0 == fGenID) {
174 return 0; // volatile, so won't be keyed
175 }
176 // When >= 0, `dataKeySize` is a reasonably small number bounded by
177 // kMaxKeyFromDataVerbCnt since point count is derived from verb count.
178 int dataKeySize = path_key_from_data_size(fShape.path());
179 if (dataKeySize >= 0) {
180 count += SkTo<uint16_t>(dataKeySize);
181 } else {
182 count++; // Just adds the gen ID.
183 }
184 break; }
185 default:
186 // else it's empty, which just needs the state flags for its key
187 SkASSERT(fShape.isEmpty());
188 }
189 return count;
190 }
191
192 void GrStyledShape::writeUnstyledKey(uint32_t* key) const {
193 SkASSERT(this->unstyledKeySize());
194 SkDEBUGCODE(uint32_t* origKey = key;)
195 if (fInheritedKey.count()) {
196 memcpy(key, fInheritedKey.get(), sizeof(uint32_t) * fInheritedKey.count());
197 SkDEBUGCODE(key += fInheritedKey.count();)
198 } else {
199 // Dir and start are only used for rect and rrect shapes, so are not included in other
200 // shape type keys. Make sure that they are the defaults for other shapes so it doesn't
201 // matter that we universally include them in the flag key value.
202 SkASSERT((fShape.isRect() || fShape.isRRect()) ||
203 (fShape.dir() == GrShape::kDefaultDir &&
204 fShape.startIndex() == GrShape::kDefaultStart));
205
206 // Every key starts with the state from the GrShape (this includes path fill type,
207 // and any tracked winding, start, inversion, as well as the class of geometry).
208 *key++ = fShape.stateKey();
209
210 switch(fShape.type()) {
211 case GrShape::Type::kPath: {
212 SkASSERT(fGenID != 0);
213 // Ensure that the path's inversion matches our state so that the path's key suffices.
214 SkASSERT(fShape.inverted() == fShape.path().isInverseFillType());
215
216 int dataKeySize = path_key_from_data_size(fShape.path());
217 if (dataKeySize >= 0) {
218 write_path_key_from_data(fShape.path(), key);
219 return;
220 } else {
221 *key++ = fGenID;
222 }
223 break; }
224 case GrShape::Type::kPoint:
225 memcpy(key, &fShape.point(), sizeof(SkPoint));
226 key += sizeof(SkPoint) / sizeof(uint32_t);
227 break;
228 case GrShape::Type::kRect:
229 memcpy(key, &fShape.rect(), sizeof(SkRect));
230 key += sizeof(SkRect) / sizeof(uint32_t);
231 break;
232 case GrShape::Type::kRRect:
233 fShape.rrect().writeToMemory(key);
234 key += SkRRect::kSizeInMemory / sizeof(uint32_t);
235 break;
236 case GrShape::Type::kArc:
237 // Write dense floats first
238 memcpy(key, &fShape.arc(), sizeof(SkRect) + 2 * sizeof(float));
239 key += (sizeof(SkArc) / sizeof(uint32_t) - 1);
240 // Then write the final bool as an int, to make sure upper bits are set
241 *key++ = fShape.arc().isWedge() ? 1 : 0;
242 break;
243 case GrShape::Type::kLine:
244 memcpy(key, &fShape.line(), sizeof(GrLineSegment));
245 key += sizeof(GrLineSegment) / sizeof(uint32_t);
246 break;
247 default:
248 // Nothing other than the flag state is needed in the key for an empty shape
249 SkASSERT(fShape.isEmpty());
250 }
251 }
252 SkASSERT(key - origKey == this->unstyledKeySize());
253 }
254
255 void GrStyledShape::setInheritedKey(const GrStyledShape &parent, GrStyle::Apply apply,
256 SkScalar scale) {
257 static constexpr int kInheritedKeyLimit = 1024;
258 SkASSERT(!fInheritedKey.count());
259 // If the output shape turns out to be simple, then we will just use its geometric key
260 if (fShape.isPath()) {
261 // We want ApplyFullStyle(ApplyPathEffect(shape)) to have the same key as
262 // ApplyFullStyle(shape).
263 // The full key is structured as (geo,path_effect,stroke).
264 // If we do ApplyPathEffect we get geo,path_effect as the inherited key. If we then
265 // do ApplyFullStyle we'll memcpy geo,path_effect into the new inherited key
266 // and then append the style key (which should now be stroke only) at the end.
267 int parentCnt = parent.fInheritedKey.count();
268 bool useParentGeoKey = !parentCnt;
269 if (useParentGeoKey) {
270 parentCnt = parent.unstyledKeySize();
271 if (!parentCnt) {
272 // The parent's geometry has no key so we will have no key.
273 fGenID = 0;
274 return;
275 }
276 }
277 uint32_t styleKeyFlags = 0;
278 if (parent.knownToBeClosed()) {
279 styleKeyFlags |= GrStyle::kClosed_KeyFlag;
280 }
281 if (parent.asLine(nullptr, nullptr)) {
282 styleKeyFlags |= GrStyle::kNoJoins_KeyFlag;
283 }
284 int styleCnt = GrStyle::KeySize(parent.fStyle, apply, styleKeyFlags);
285 if (styleCnt < 0) {
286 // The style doesn't allow a key, set the path gen ID to 0 so that we fail when
287 // we try to get a key for the shape.
288 fGenID = 0;
289 return;
290 } else if (parentCnt + styleCnt > kInheritedKeyLimit) {
291 // Prevent chained path effects and styles from growing the key too large
292 fGenID = 0;
293 return;
294 }
295
296 fInheritedKey.reset(parentCnt + styleCnt);
297 if (useParentGeoKey) {
298 // This will be the geo key.
299 parent.writeUnstyledKey(fInheritedKey.get());
300 } else {
301 // This should be (geo,path_effect).
302 memcpy(fInheritedKey.get(), parent.fInheritedKey.get(),
303 parentCnt * sizeof(uint32_t));
304 }
305 // Now turn (geo,path_effect) or (geo) into (geo,path_effect,stroke)
306 GrStyle::WriteKey(fInheritedKey.get() + parentCnt, parent.fStyle, apply, scale,
307 styleKeyFlags);
308 }
309 }
310
311 const SkPath* GrStyledShape::originalPathForListeners() const {
312 if (fInheritedPathForListeners.has_value()) {
313 return &fInheritedPathForListeners.value();
314 } else if (fShape.isPath() && !fShape.path().isVolatile()) {
315 return &fShape.path();
316 }
317 return nullptr;
318 }
319
320 void GrStyledShape::addGenIDChangeListener(sk_sp<SkIDChangeListener> listener) const {
321 if (const auto* lp = this->originalPathForListeners()) {
322 SkPathPriv::AddGenIDChangeListener(*lp, std::move(listener));
323 }
324 }
325
326 GrStyledShape GrStyledShape::MakeArc(const SkArc& arc,
327 const GrStyle& style,
328 DoSimplify doSimplify) {
329 GrStyledShape result;
330 result.fShape.setArc(
331 SkArc::Make(arc.fOval.makeSorted(), arc.fStartAngle, arc.fSweepAngle, arc.fType));
332 result.fStyle = style;
333 if (doSimplify == DoSimplify::kYes) {
334 result.simplify();
335 }
336 return result;
337 }
338
339 GrStyledShape::GrStyledShape(const GrStyledShape& that)
340 : fShape(that.fShape)
341 , fStyle(that.fStyle)
342 , fGenID(that.fGenID)
343 , fSimplified(that.fSimplified) {
344 fInheritedKey.reset(that.fInheritedKey.count());
345 sk_careful_memcpy(fInheritedKey.get(), that.fInheritedKey.get(),
346 sizeof(uint32_t) * fInheritedKey.count());
347 if (that.fInheritedPathForListeners.has_value()) {
348 fInheritedPathForListeners = *that.fInheritedPathForListeners;
349 }
350 }
351
352 GrStyledShape::GrStyledShape(const GrStyledShape& parent, GrStyle::Apply apply, SkScalar scale) {
353 SkASSERT(scale > 0 && std::isfinite(scale));
354 // TODO: Add some quantization of scale for better cache performance here or leave that up
355 // to caller?
356 // TODO: For certain shapes and stroke params we could ignore the scale. (e.g. miter or bevel
357 // stroke of a rect).
358 if (!parent.style().applies() ||
359 (GrStyle::Apply::kPathEffectOnly == apply && !parent.style().pathEffect())) {
360 *this = parent;
361 return;
362 }
363
364 SkPathEffect* pe = parent.fStyle.pathEffect();
365 std::optional<SkPath> tmpPath;
366 const GrStyledShape* parentForKey = &parent;
367 std::optional<GrStyledShape> tmpParent;
368
369 // Start out as an empty path that is filled in by the applied style
370 fShape.setPath(SkPath());
371
372 if (pe) {
373 const SkPath* srcForPathEffect;
374 if (parent.fShape.isPath()) {
375 srcForPathEffect = &parent.fShape.path();
376 } else {
377 srcForPathEffect = &tmpPath.emplace(parent.asPath());
378 }
379 // Should we consider bounds? Would have to include in key, but it'd be nice to know
380 // if the bounds actually modified anything before including in key.
381 SkStrokeRec strokeRec = parent.fStyle.strokeRec();
382 if (!parent.fStyle.applyPathEffectToPath(&fShape.path(), &strokeRec, *srcForPathEffect,
383 scale)) {
384 tmpParent.emplace(*srcForPathEffect, GrStyle(strokeRec, nullptr));
385 *this = tmpParent->applyStyle(apply, scale);
386 return;
387 }
388 // A path effect has access to change the res scale but we aren't expecting it to and it
389 // would mess up our key computation.
390 SkASSERT(scale == strokeRec.getResScale());
391 if (GrStyle::Apply::kPathEffectAndStrokeRec == apply && strokeRec.needToApply()) {
392 // The intermediate shape may not be a general path. If we we're just applying
393 // the path effect then attemptToReduceFromPath would catch it. This means that
394 // when we subsequently applied the remaining strokeRec we would have a non-path
395 // parent shape that would be used to determine the the stroked path's key.
396 // We detect that case here and change parentForKey to a temporary that represents
397 // the simpler shape so that applying both path effect and the strokerec all at
398 // once produces the same key.
399 tmpParent.emplace(fShape.path(), GrStyle(strokeRec, nullptr));
400 tmpParent->setInheritedKey(parent, GrStyle::Apply::kPathEffectOnly, scale);
401 if (!tmpPath.has_value()) {
402 tmpPath.emplace();
403 }
404 tmpPath = tmpParent->asPath();
405 SkStrokeRec::InitStyle fillOrHairline;
406 // The parent shape may have simplified away the strokeRec, check for that here.
407 if (tmpParent->style().applies()) {
408 SkAssertResult(tmpParent->style().applyToPath(&fShape.path(), &fillOrHairline,
409 tmpPath.value(), scale));
410 } else if (tmpParent->style().isSimpleFill()) {
411 fillOrHairline = SkStrokeRec::kFill_InitStyle;
412 } else {
413 SkASSERT(tmpParent->style().isSimpleHairline());
414 fillOrHairline = SkStrokeRec::kHairline_InitStyle;
415 }
416 fStyle.resetToInitStyle(fillOrHairline);
417 parentForKey = &tmpParent.value();
418 } else {
419 fStyle = GrStyle(strokeRec, nullptr);
420 }
421 } else {
422 const SkPath* srcForParentStyle;
423 if (parent.fShape.isPath()) {
424 srcForParentStyle = &parent.fShape.path();
425 } else {
426 srcForParentStyle = &tmpPath.emplace(parent.asPath());
427 }
428 SkStrokeRec::InitStyle fillOrHairline;
429 SkASSERT(parent.fStyle.applies());
430 SkASSERT(!parent.fStyle.pathEffect());
431 SkAssertResult(parent.fStyle.applyToPath(&fShape.path(), &fillOrHairline,
432 *srcForParentStyle, scale));
433 fStyle.resetToInitStyle(fillOrHairline);
434 }
435
436 if (parent.fInheritedPathForListeners.has_value()) {
437 fInheritedPathForListeners = *parent.fInheritedPathForListeners;
438 } else if (parent.fShape.isPath() && !parent.fShape.path().isVolatile()) {
439 fInheritedPathForListeners = parent.fShape.path();
440 }
441 this->simplify();
442 this->setInheritedKey(*parentForKey, apply, scale);
443 }
444
445 bool GrStyledShape::asRRect(SkRRect* rrect, bool* inverted) const {
446 if (!fShape.isRRect() && !fShape.isRect()) {
447 return false;
448 }
449
450 if (rrect) {
451 *rrect = fShape.isRect() ? SkRRect::MakeRect(fShape.rect()) : fShape.rrect();
452 }
453
454 if (inverted) {
455 *inverted = fShape.inverted();
456 }
457
458 return true;
459 }
460
461 bool GrStyledShape::asLine(SkPoint pts[2], bool* inverted) const {
462 if (!fShape.isLine()) {
463 return false;
464 }
465
466 if (pts) {
467 pts[0] = fShape.line().fP1;
468 pts[1] = fShape.line().fP2;
469 }
470 if (inverted) {
471 *inverted = fShape.inverted();
472 }
473 return true;
474 }
475
476 bool GrStyledShape::asNestedRects(SkRect rects[2]) const {
477 if (!fShape.isPath()) {
478 return false;
479 }
480
481 // TODO: it would be better two store DRRects natively in the shape rather than converting
482 // them to a path and then reextracting the nested rects
483 if (fShape.path().isInverseFillType()) {
484 return false;
485 }
486
487 SkPathDirection dirs[2];
488 if (!SkPathPriv::IsNestedFillRects(fShape.path(), rects, dirs)) {
489 return false;
490 }
491
492 if (SkPathFillType::kWinding == fShape.path().getFillType() && dirs[0] == dirs[1]) {
493 // The two rects need to be wound opposite to each other
494 return false;
495 }
496
497 // Right now, nested rects where the margin is not the same width
498 // all around do not render correctly
499 const SkScalar* outer = rects[0].asScalars();
500 const SkScalar* inner = rects[1].asScalars();
501
502 bool allEq = true;
503
504 SkScalar margin = SkScalarAbs(outer[0] - inner[0]);
505 bool allGoE1 = margin >= SK_Scalar1;
506
507 for (int i = 1; i < 4; ++i) {
508 SkScalar temp = SkScalarAbs(outer[i] - inner[i]);
509 if (temp < SK_Scalar1) {
510 allGoE1 = false;
511 }
512 if (!SkScalarNearlyEqual(margin, temp)) {
513 allEq = false;
514 }
515 }
516
517 return allEq || allGoE1;
518 }
519
520 class AutoRestoreInverseness {
521 public:
522 AutoRestoreInverseness(GrShape* shape, const GrStyle& style)
523 // Dashing ignores inverseness skbug.com/40036591.
524 : fShape(shape), fInverted(!style.isDashed() && fShape->inverted()) {}
525
526 ~AutoRestoreInverseness() {
527 // Restore invertedness after any modifications were made to the shape type
528 fShape->setInverted(fInverted);
529 SkASSERT(!fShape->isPath() || fInverted == fShape->path().isInverseFillType());
530 }
531
532 private:
533 GrShape* fShape;
534 bool fInverted;
535 };
536
537 void GrStyledShape::simplify() {
538 AutoRestoreInverseness ari(&fShape, fStyle);
539
540 unsigned simplifyFlags = 0;
541 if (fStyle.isSimpleFill()) {
542 simplifyFlags = GrShape::kAll_Flags;
543 } else if (!fStyle.hasPathEffect()) {
544 // Everything but arcs with caps that might extend beyond the oval edge can ignore winding
545 if (!fShape.isArc() || fStyle.strokeRec().getCap() == SkPaint::kButt_Cap) {
546 simplifyFlags |= GrShape::kIgnoreWinding_Flag;
547 }
548 simplifyFlags |= GrShape::kMakeCanonical_Flag;
549 } // else if there's a path effect, every destructive simplification is disabledd
550
551 // Remember if the original shape was closed; in the event we simplify to a point or line
552 // because of degenerate geometry, we need to update joins and caps.
553 GrShape::Type oldType = fShape.type();
554 fClosed = fShape.simplify(simplifyFlags);
555 fSimplified = oldType != fShape.type();
556
557 if (fShape.isPath()) {
558 // The shape remains a path, so configure the gen ID and canonicalize fill type if possible
559 if (fInheritedKey.count() || fShape.path().isVolatile()) {
560 fGenID = 0;
561 } else {
562 fGenID = fShape.path().getGenerationID();
563 }
564 if (!fStyle.hasNonDashPathEffect() &&
565 (fStyle.strokeRec().getStyle() == SkStrokeRec::kStroke_Style ||
566 fStyle.strokeRec().getStyle() == SkStrokeRec::kHairline_Style ||
567 fShape.path().isConvex())) {
568 // Stroke styles don't differentiate between winding and even/odd. There is no
569 // distinction between even/odd and non-zero winding count for convex paths.
570 // Moreover, dashing ignores inverseness (skbug.com/40036591)
571 fShape.path().setFillType(GrShape::kDefaultFillType);
572 }
573 } else {
574 fInheritedKey.reset(0);
575 // Whenever we simplify to a non-path, break the chain so we no longer refer to the
576 // original path. This prevents attaching genID listeners to temporary paths created when
577 // drawing simple shapes.
578 fInheritedPathForListeners.reset();
579 // Further simplifications to the shape based on the style
580 this->simplifyStroke();
581 }
582 }
583
584 void GrStyledShape::simplifyStroke() {
585 AutoRestoreInverseness ari(&fShape, fStyle);
586
587 // For stroke+filled rects, a mitered shape becomes a larger rect and a rounded shape
588 // becomes a round rect.
589 if (!fStyle.hasPathEffect() && fShape.isRect() &&
590 fStyle.strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style) {
591 if (fStyle.strokeRec().getJoin() == SkPaint::kBevel_Join ||
592 (fStyle.strokeRec().getJoin() == SkPaint::kMiter_Join &&
593 fStyle.strokeRec().getMiter() < SK_ScalarSqrt2)) {
594 // Bevel-stroked rect needs path rendering
595 return;
596 }
597
598 SkScalar r = fStyle.strokeRec().getWidth() / 2;
599 fShape.rect().outset(r, r);
600 if (fStyle.strokeRec().getJoin() == SkPaint::kRound_Join) {
601 // There's no dashing to worry about if we got here, so it's okay that this resets
602 // winding parameters
603 fShape.setRRect(SkRRect::MakeRectXY(fShape.rect(), r, r));
604 }
605 fStyle = GrStyle::SimpleFill();
606 fSimplified = true;
607 return;
608 }
609
610 // Otherwise, if we're a point or a line, we might be able to explicitly apply some of the
611 // stroking (and even some of the dashing). Any other shape+style is too complicated to reduce.
612 if ((!fShape.isPoint() && !fShape.isLine()) || fStyle.hasNonDashPathEffect() ||
613 fStyle.strokeRec().isHairlineStyle()) {
614 return;
615 }
616
617 // Tracks style simplifications, even if the geometry can't be further simplified.
618 bool styleSimplified = false;
619 if (fStyle.isDashed()) {
620 // For dashing a point, if the first interval is on, we can drop the dash and just draw
621 // the caps. For dashing a line, if every off interval is 0 length, its a stroke.
622 bool dropDash = false;
623 if (fShape.isPoint()) {
624 dropDash = fStyle.dashIntervalCnt() > 0 &&
625 SkToBool(fStyle.dashIntervals()[0]);
626 } else {
627 dropDash = true;
628 for (int i = 1; i < fStyle.dashIntervalCnt(); i += 2) {
629 if (SkToBool(fStyle.dashIntervals()[i])) {
630 // An off interval has non-zero length so this won't convert to a simple line
631 dropDash = false;
632 break;
633 }
634 }
635 }
636
637 if (!dropDash) {
638 return;
639 }
640 // Fall through to modifying the shape to respect the new stroke geometry
641 fStyle = GrStyle(fStyle.strokeRec(), nullptr);
642 // Since the reduced the line or point after dashing is dependent on the caps of the dashes,
643 // we reset to be unclosed so we don't override the style based on joins later.
644 fClosed = false;
645 styleSimplified = true;
646 }
647
648 // At this point, we're a line or point with no path effects. Any fill portion of the style
649 // is empty, so a fill-only style can be empty, and a stroke+fill becomes a stroke.
650 if (fStyle.isSimpleFill()) {
651 fShape.reset();
652 fSimplified = true;
653 return;
654 } else if (fStyle.strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style) {
655 // Stroke only
656 SkStrokeRec rec = fStyle.strokeRec();
657 rec.setStrokeStyle(fStyle.strokeRec().getWidth(), false);
658 fStyle = GrStyle(rec, nullptr);
659 styleSimplified = true;
660 }
661
662 // A point or line that was formed by a degenerate closed shape needs its style updated to
663 // reflect the fact that it doesn't actually produce caps.
664 if (fClosed) {
665 SkPaint::Cap cap;
666 if (fShape.isLine() && fStyle.strokeRec().getJoin() == SkPaint::kRound_Join) {
667 // As a closed shape, the line moves from a to b and back to a, producing a 180 degree
668 // turn. With round joins, this would make a semi-circle at each end, which is visually
669 // identical to a round cap on the reduced line geometry.
670 cap = SkPaint::kRound_Cap;
671 } else {
672 // If this were a closed line, the 180 degree turn either is a miter join that exceeds
673 // the miter limit and becomes a bevel, or a bevel join. In either case, the bevel shape
674 // of a 180 degreen corner is equivalent to a butt cap.
675 // - to match the SVG spec, the 0-length sides of an empty rectangle are skipped, so
676 // it fits this closed line description (it is not two 90 degree turns that could
677 // produce miter geometry).
678 cap = SkPaint::kButt_Cap;
679 }
680
681 if (cap != fStyle.strokeRec().getCap() ||
682 SkPaint::kDefault_Join != fStyle.strokeRec().getJoin()) {
683 SkStrokeRec rec = fStyle.strokeRec();
684 rec.setStrokeParams(cap, SkPaint::kDefault_Join, fStyle.strokeRec().getMiter());
685 fStyle = GrStyle(rec, nullptr);
686 styleSimplified = true;
687 }
688 }
689
690 if (fShape.isPoint()) {
691 // The drawn geometry is entirely based on the cap style and stroke width. A butt cap point
692 // doesn't draw anything, a round cap is an oval and a square cap is a square.
693 if (fStyle.strokeRec().getCap() == SkPaint::kButt_Cap) {
694 fShape.reset();
695 } else {
696 SkScalar w = fStyle.strokeRec().getWidth() / 2.f;
697 SkRect r = {fShape.point().fX, fShape.point().fY, fShape.point().fX, fShape.point().fY};
698 r.outset(w, w);
699
700 if (fStyle.strokeRec().getCap() == SkPaint::kRound_Cap) {
701 fShape.setRRect(SkRRect::MakeOval(r));
702 } else {
703 fShape.setRect(r);
704 }
705 }
706 } else {
707 // Stroked lines reduce to rectangles or round rects when they are axis-aligned. If we
708 // allowed rotation angle, this would work for any lines.
709 SkRect rect;
710 SkVector outset;
711 if (fShape.line().fP1.fY == fShape.line().fP2.fY) {
712 rect.fLeft = std::min(fShape.line().fP1.fX, fShape.line().fP2.fX);
713 rect.fRight = std::max(fShape.line().fP1.fX, fShape.line().fP2.fX);
714 rect.fTop = rect.fBottom = fShape.line().fP1.fY;
715 outset.fY = fStyle.strokeRec().getWidth() / 2.f;
716 outset.fX = SkPaint::kButt_Cap == fStyle.strokeRec().getCap() ? 0.f : outset.fY;
717 } else if (fShape.line().fP1.fX == fShape.line().fP2.fX) {
718 rect.fTop = std::min(fShape.line().fP1.fY, fShape.line().fP2.fY);
719 rect.fBottom = std::max(fShape.line().fP1.fY, fShape.line().fP2.fY);
720 rect.fLeft = rect.fRight = fShape.line().fP1.fX;
721 outset.fX = fStyle.strokeRec().getWidth() / 2.f;
722 outset.fY = SkPaint::kButt_Cap == fStyle.strokeRec().getCap() ? 0.f : outset.fX;
723 } else {
724 // Geometrically can't apply the style and turn into a fill, but might still be simpler
725 // than before based solely on changes to fStyle.
726 fSimplified |= styleSimplified;
727 return;
728 }
729 rect.outset(outset.fX, outset.fY);
730 if (rect.isEmpty()) {
731 fShape.reset();
732 } else if (fStyle.strokeRec().getCap() == SkPaint::kRound_Cap) {
733 SkASSERT(outset.fX == outset.fY);
734 fShape.setRRect(SkRRect::MakeRectXY(rect, outset.fX, outset.fY));
735 } else {
736 fShape.setRect(rect);
737 }
738 }
739 // If we made it here, the stroke was fully applied to the new shape so we can become a fill.
740 fStyle = GrStyle::SimpleFill();
741 fSimplified = true;
742 }