| 1 |
/* |
| 2 |
* Copyright 2020 Google LLC |
| 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 "src/gpu/ganesh/geometry/GrShape.h" |
| 9 |
|
| 10 |
#include "include/core/SkArc.h" |
| 11 |
#include "include/core/SkScalar.h" |
| 12 |
#include "src/core/SkPathPriv.h" |
| 13 |
#include "src/core/SkRRectPriv.h" |
| 14 |
|
| 15 |
#include <algorithm> |
| 16 |
|
| 17 |
GrShape& GrShape::operator=(const GrShape& shape) { |
| 18 |
switch (shape.type()) { |
| 19 |
case Type::kEmpty: |
| 20 |
this->reset(); |
| 21 |
break; |
| 22 |
case Type::kPoint: |
| 23 |
this->setPoint(shape.fPoint); |
| 24 |
break; |
| 25 |
case Type::kRect: |
| 26 |
this->setRect(shape.fRect); |
| 27 |
break; |
| 28 |
case Type::kRRect: |
| 29 |
this->setRRect(shape.fRRect); |
| 30 |
break; |
| 31 |
case Type::kPath: |
| 32 |
this->setPath(shape.fPath); |
| 33 |
break; |
| 34 |
case Type::kArc: |
| 35 |
this->setArc(shape.fArc); |
| 36 |
break; |
| 37 |
case Type::kLine: |
| 38 |
this->setLine(shape.fLine); |
| 39 |
break; |
| 40 |
} |
| 41 |
|
| 42 |
fStart = shape.fStart; |
| 43 |
fCW = shape.fCW; |
| 44 |
fInverted = shape.fInverted; |
| 45 |
|
| 46 |
return *this; |
| 47 |
} |
| 48 |
|
| 49 |
uint32_t GrShape::stateKey() const { |
| 50 |
// Use the path's full fill type instead of just whether or not it's inverted. |
| 51 |
uint32_t key = this->isPath() ? static_cast<uint32_t>(fPath.getFillType()) |
| 52 |
: (fInverted ? 1 : 0); |
| 53 |
key |= ((uint32_t) fType) << 2; // fill type was 2 bits |
| 54 |
key |= fStart << 5; // type was 3 bits, total 5 bits so far |
| 55 |
key |= (fCW ? 1 : 0) << 8; // start was 3 bits, total 8 bits so far |
| 56 |
return key; |
| 57 |
} |
| 58 |
|
| 59 |
bool GrShape::simplifyPath(unsigned flags) { |
| 60 |
SkASSERT(this->isPath()); |
| 61 |
|
| 62 |
SkPoint pts[2]; |
| 63 |
|
| 64 |
if (fPath.isEmpty()) { |
| 65 |
this->setType(Type::kEmpty); |
| 66 |
return false; |
| 67 |
} |
| 68 |
if (fPath.isLine(pts)) { |
| 69 |
this->simplifyLine(pts[0], pts[1], flags); |
| 70 |
return false; |
| 71 |
} |
| 72 |
if (auto info = SkPathPriv::IsRRect(fPath)) { |
| 73 |
this->simplifyRRect(info->fRRect, info->fDirection, info->fStartIndex, flags); |
| 74 |
return true; |
| 75 |
} |
| 76 |
if (auto info = SkPathPriv::IsOval(fPath)) { |
| 77 |
// Convert to rrect indexing since oval is not represented explicitly |
| 78 |
this->simplifyRRect(SkRRect::MakeOval(info->fBounds), |
| 79 |
info->fDirection, info->fStartIndex * 2, flags); |
| 80 |
return true; |
| 81 |
} |
| 82 |
if (auto info = SkPathPriv::IsSimpleRect(fPath, (flags & kSimpleFill_Flag))) { |
| 83 |
// When there is a path effect we restrict rect detection to the narrower API that |
| 84 |
// gives us the starting position. Otherwise, we will retry with the more aggressive |
| 85 |
// isRect(). |
| 86 |
this->simplifyRect(info->fRect, info->fDirection, info->fStartIndex, flags); |
| 87 |
return true; |
| 88 |
} |
| 89 |
if (flags & kIgnoreWinding_Flag) { |
| 90 |
SkRect rect; |
| 91 |
// Attempt isRect() since we don't have to preserve any winding info |
| 92 |
bool closed; |
| 93 |
if (fPath.isRect(&rect, &closed) && (closed || (flags & kSimpleFill_Flag))) { |
| 94 |
this->simplifyRect(rect, kDefaultDir, kDefaultStart, flags); |
| 95 |
return true; |
| 96 |
} |
| 97 |
} |
| 98 |
// No further simplification for a path. For performance reasons, we don't query the path to |
| 99 |
// determine it was closed, as whether or not it was closed when it remains a path type is not |
| 100 |
// important for styling. |
| 101 |
return false; |
| 102 |
} |
| 103 |
|
| 104 |
bool GrShape::simplifyArc(unsigned flags) { |
| 105 |
SkASSERT(this->isArc()); |
| 106 |
|
| 107 |
// Arcs can simplify to rrects, lines, points, or empty; regardless of what it simplifies to |
| 108 |
// it was closed if went through the center point. |
| 109 |
bool wasClosed = fArc.isWedge(); |
| 110 |
if (fArc.fOval.isEmpty() || !fArc.fSweepAngle) { |
| 111 |
if (flags & kSimpleFill_Flag) { |
| 112 |
// Go straight to empty, since the other degenerate shapes all have 0 area anyway. |
| 113 |
this->setType(Type::kEmpty); |
| 114 |
} else if (!fArc.fSweepAngle) { |
| 115 |
SkPoint center = {fArc.fOval.centerX(), fArc.fOval.centerY()}; |
| 116 |
SkScalar startRad = SkDegreesToRadians(fArc.fStartAngle); |
| 117 |
SkPoint start = {center.fX + 0.5f * fArc.fOval.width() * SkScalarCos(startRad), |
| 118 |
center.fY + 0.5f * fArc.fOval.height() * SkScalarSin(startRad)}; |
| 119 |
// Either just the starting point, or a line from the center to the start |
| 120 |
if (fArc.isWedge()) { |
| 121 |
this->simplifyLine(center, start, flags); |
| 122 |
} else { |
| 123 |
this->simplifyPoint(start, flags); |
| 124 |
} |
| 125 |
} else { |
| 126 |
// TODO: Theoretically, we could analyze the arc projected into the empty bounds to |
| 127 |
// determine a line, but that is somewhat complex for little value (since the arc |
| 128 |
// can backtrack on itself if the sweep angle is large enough). |
| 129 |
this->setType(Type::kEmpty); |
| 130 |
} |
| 131 |
} else { |
| 132 |
if ((flags & kSimpleFill_Flag) || |
| 133 |
((flags & kIgnoreWinding_Flag) && !fArc.isWedge())) { |
| 134 |
// Eligible to turn into an oval if it sweeps a full circle |
| 135 |
if (fArc.fSweepAngle <= -360.f || fArc.fSweepAngle >= 360.f) { |
| 136 |
this->simplifyRRect(SkRRect::MakeOval(fArc.fOval), |
| 137 |
kDefaultDir, kDefaultStart, flags); |
| 138 |
return true; |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
if (flags & kMakeCanonical_Flag) { |
| 143 |
// Map start to 0 to 360, sweep is always positive |
| 144 |
if (fArc.fSweepAngle < 0) { |
| 145 |
fArc.fStartAngle = fArc.fStartAngle + fArc.fSweepAngle; |
| 146 |
fArc.fSweepAngle = -fArc.fSweepAngle; |
| 147 |
} |
| 148 |
|
| 149 |
if (fArc.fStartAngle < 0 || fArc.fStartAngle >= 360.f) { |
| 150 |
fArc.fStartAngle = SkScalarMod(fArc.fStartAngle, 360.f); |
| 151 |
} |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
return wasClosed; |
| 156 |
} |
| 157 |
|
| 158 |
void GrShape::simplifyRRect(const SkRRect& rrect, SkPathDirection dir, unsigned start, |
| 159 |
unsigned flags) { |
| 160 |
if (rrect.isEmpty() || rrect.isRect()) { |
| 161 |
// Change index from rrect to rect |
| 162 |
start = ((start + 1) / 2) % 4; |
| 163 |
this->simplifyRect(rrect.rect(), dir, start, flags); |
| 164 |
} else if (!this->isRRect()) { |
| 165 |
this->setType(Type::kRRect); |
| 166 |
fRRect = rrect; |
| 167 |
this->setPathWindingParams(dir, start); |
| 168 |
// A round rect is already canonical, so there's nothing more to do |
| 169 |
} else { |
| 170 |
// If starting as a round rect, the provided rrect/winding params should be already set |
| 171 |
SkASSERT(fRRect == rrect && this->dir() == dir && this->startIndex() == start); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
void GrShape::simplifyRect(const SkRect& rect, SkPathDirection dir, unsigned start, |
| 176 |
unsigned flags) { |
| 177 |
if (!rect.width() || !rect.height()) { |
| 178 |
if (flags & kSimpleFill_Flag) { |
| 179 |
// A zero area, filled shape so go straight to empty |
| 180 |
this->setType(Type::kEmpty); |
| 181 |
} else if (!rect.width() ^ !rect.height()) { |
| 182 |
// A line, choose the first point that best matches the starting index |
| 183 |
SkPoint p1 = {rect.fLeft, rect.fTop}; |
| 184 |
SkPoint p2 = {rect.fRight, rect.fBottom}; |
| 185 |
if (start >= 2 && !(flags & kIgnoreWinding_Flag)) { |
| 186 |
using std::swap; |
| 187 |
swap(p1, p2); |
| 188 |
} |
| 189 |
this->simplifyLine(p1, p2, flags); |
| 190 |
} else { |
| 191 |
// A point (all edges are equal, so start+dir doesn't affect choice) |
| 192 |
this->simplifyPoint({rect.fLeft, rect.fTop}, flags); |
| 193 |
} |
| 194 |
} else { |
| 195 |
if (!this->isRect()) { |
| 196 |
this->setType(Type::kRect); |
| 197 |
fRect = rect; |
| 198 |
this->setPathWindingParams(dir, start); |
| 199 |
} else { |
| 200 |
// If starting as a rect, the provided rect/winding params should already be set |
| 201 |
SkASSERT(fRect == rect); |
| 202 |
SkASSERT(this->dir() == dir); |
| 203 |
SkASSERT(this->startIndex() == start); |
| 204 |
} |
| 205 |
if (flags & kMakeCanonical_Flag) { |
| 206 |
fRect.sort(); |
| 207 |
} |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
void GrShape::simplifyLine(const SkPoint& p1, const SkPoint& p2, unsigned flags) { |
| 212 |
if (flags & kSimpleFill_Flag) { |
| 213 |
this->setType(Type::kEmpty); |
| 214 |
} else if (p1 == p2) { |
| 215 |
this->simplifyPoint(p1, false); |
| 216 |
} else { |
| 217 |
if (!this->isLine()) { |
| 218 |
this->setType(Type::kLine); |
| 219 |
fLine.fP1 = p1; |
| 220 |
fLine.fP2 = p2; |
| 221 |
} else { |
| 222 |
// If starting as a line, the provided points should already be set |
| 223 |
SkASSERT(fLine.fP1 == p1 && fLine.fP2 == p2); |
| 224 |
} |
| 225 |
if (flags & kMakeCanonical_Flag) { |
| 226 |
// Sort the end points |
| 227 |
if (fLine.fP2.fY < fLine.fP1.fY || |
| 228 |
(fLine.fP2.fY == fLine.fP1.fY && fLine.fP2.fX < fLine.fP1.fX)) { |
| 229 |
using std::swap; |
| 230 |
swap(fLine.fP1, fLine.fP2); |
| 231 |
} |
| 232 |
} |
| 233 |
} |
| 234 |
} |
| 235 |
|
| 236 |
void GrShape::simplifyPoint(const SkPoint& point, unsigned flags) { |
| 237 |
if (flags & kSimpleFill_Flag) { |
| 238 |
this->setType(Type::kEmpty); |
| 239 |
} else if (!this->isPoint()) { |
| 240 |
this->setType(Type::kPoint); |
| 241 |
fPoint = point; |
| 242 |
} else { |
| 243 |
// If starting as a point, the provided position should already be set |
| 244 |
SkASSERT(point == fPoint); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
bool GrShape::simplify(unsigned flags) { |
| 249 |
// Verify that winding parameters are valid for the current type. |
| 250 |
SkASSERT((fType == Type::kRect || fType == Type::kRRect) || |
| 251 |
(this->dir() == kDefaultDir && this->startIndex() == kDefaultStart)); |
| 252 |
|
| 253 |
// The type specific functions automatically fall through to the simpler shapes, so |
| 254 |
// we only need to start in the right place. |
| 255 |
bool wasClosed = false; |
| 256 |
switch (fType) { |
| 257 |
case Type::kEmpty: |
| 258 |
// do nothing |
| 259 |
break; |
| 260 |
case Type::kPoint: |
| 261 |
this->simplifyPoint(fPoint, flags); |
| 262 |
break; |
| 263 |
case Type::kLine: |
| 264 |
this->simplifyLine(fLine.fP1, fLine.fP2, flags); |
| 265 |
break; |
| 266 |
case Type::kRect: |
| 267 |
this->simplifyRect(fRect, this->dir(), this->startIndex(), flags); |
| 268 |
wasClosed = true; |
| 269 |
break; |
| 270 |
case Type::kRRect: |
| 271 |
this->simplifyRRect(fRRect, this->dir(), this->startIndex(), flags); |
| 272 |
wasClosed = true; |
| 273 |
break; |
| 274 |
case Type::kPath: |
| 275 |
wasClosed = this->simplifyPath(flags); |
| 276 |
break; |
| 277 |
case Type::kArc: |
| 278 |
wasClosed = this->simplifyArc(flags); |
| 279 |
break; |
| 280 |
|
| 281 |
default: |
| 282 |
SkUNREACHABLE; |
| 283 |
} |
| 284 |
|
| 285 |
if (((flags & kIgnoreWinding_Flag) || (fType != Type::kRect && fType != Type::kRRect))) { |
| 286 |
// Reset winding parameters if we don't need them anymore |
| 287 |
this->setPathWindingParams(kDefaultDir, kDefaultStart); |
| 288 |
} |
| 289 |
|
| 290 |
return wasClosed; |
| 291 |
} |
| 292 |
|
| 293 |
bool GrShape::conservativeContains(const SkRect& rect) const { |
| 294 |
switch (this->type()) { |
| 295 |
case Type::kEmpty: |
| 296 |
case Type::kPoint: // fall through since a point has 0 area |
| 297 |
case Type::kLine: // fall through, "" (currently choosing not to test if 'rect' == line) |
| 298 |
return false; |
| 299 |
case Type::kRect: |
| 300 |
return fRect.contains(rect); |
| 301 |
case Type::kRRect: |
| 302 |
return fRRect.contains(rect); |
| 303 |
case Type::kPath: |
| 304 |
return fPath.conservativelyContainsRect(rect); |
| 305 |
case Type::kArc: |
| 306 |
if (fArc.fType == SkArc::Type::kWedge) { |
| 307 |
SkPath arc = this->asPath(); |
| 308 |
return arc.conservativelyContainsRect(rect); |
| 309 |
} else { |
| 310 |
return false; |
| 311 |
} |
| 312 |
} |
| 313 |
SkUNREACHABLE; |
| 314 |
} |
| 315 |
|
| 316 |
bool GrShape::conservativeContains(const SkPoint& point) const { |
| 317 |
switch (this->type()) { |
| 318 |
case Type::kEmpty: |
| 319 |
case Type::kPoint: // fall through, currently choosing not to test if shape == point |
| 320 |
case Type::kLine: // fall through, "" |
| 321 |
case Type::kArc: |
| 322 |
return false; |
| 323 |
case Type::kRect: |
| 324 |
return fRect.contains(point.fX, point.fY); |
| 325 |
case Type::kRRect: |
| 326 |
return SkRRectPriv::ContainsPoint(fRRect, point); |
| 327 |
case Type::kPath: |
| 328 |
return fPath.contains(point.fX, point.fY); |
| 329 |
} |
| 330 |
SkUNREACHABLE; |
| 331 |
} |
| 332 |
|
| 333 |
bool GrShape::closed() const { |
| 334 |
switch (this->type()) { |
| 335 |
case Type::kEmpty: // fall through |
| 336 |
case Type::kRect: // fall through |
| 337 |
case Type::kRRect: |
| 338 |
return true; |
| 339 |
case Type::kPath: |
| 340 |
// SkPath doesn't keep track of the closed status of each contour. |
| 341 |
return SkPathPriv::IsClosedSingleContour(fPath); |
| 342 |
case Type::kArc: |
| 343 |
return fArc.fType == SkArc::Type::kWedge; |
| 344 |
case Type::kPoint: // fall through |
| 345 |
case Type::kLine: |
| 346 |
return false; |
| 347 |
} |
| 348 |
SkUNREACHABLE; |
| 349 |
} |
| 350 |
|
| 351 |
bool GrShape::convex(bool simpleFill) const { |
| 352 |
switch (this->type()) { |
| 353 |
case Type::kEmpty: // fall through |
| 354 |
case Type::kRect: // fall through |
| 355 |
case Type::kRRect: |
| 356 |
return true; |
| 357 |
case Type::kPath: |
| 358 |
// SkPath.isConvex() really means "is this path convex were it to be closed". |
| 359 |
// Convex paths may only have one contour hence isLastContourClosed() is sufficient. |
| 360 |
return (simpleFill || fPath.isLastContourClosed()) && fPath.isConvex(); |
| 361 |
case Type::kArc: |
| 362 |
return SkPathPriv::DrawArcIsConvex(fArc.fSweepAngle, fArc.fType, simpleFill); |
| 363 |
case Type::kPoint: // fall through |
| 364 |
case Type::kLine: |
| 365 |
return false; |
| 366 |
} |
| 367 |
SkUNREACHABLE; |
| 368 |
} |
| 369 |
|
| 370 |
SkRect GrShape::bounds() const { |
| 371 |
// Bounds where left == bottom or top == right can indicate a line or point shape. We return |
| 372 |
// inverted bounds for a truly empty shape. |
| 373 |
static constexpr SkRect kInverted = SkRect::MakeLTRB(1, 1, -1, -1); |
| 374 |
switch (this->type()) { |
| 375 |
case Type::kEmpty: |
| 376 |
return kInverted; |
| 377 |
case Type::kPoint: |
| 378 |
return {fPoint.fX, fPoint.fY, fPoint.fX, fPoint.fY}; |
| 379 |
case Type::kRect: |
| 380 |
return fRect.makeSorted(); |
| 381 |
case Type::kRRect: |
| 382 |
return fRRect.getBounds(); |
| 383 |
case Type::kPath: |
| 384 |
return fPath.getBounds(); |
| 385 |
case Type::kArc: |
| 386 |
return fArc.fOval; |
| 387 |
case Type::kLine: { |
| 388 |
SkRect b = SkRect::MakeLTRB(fLine.fP1.fX, fLine.fP1.fY, |
| 389 |
fLine.fP2.fX, fLine.fP2.fY); |
| 390 |
b.sort(); |
| 391 |
return b; } |
| 392 |
} |
| 393 |
SkUNREACHABLE; |
| 394 |
} |
| 395 |
|
| 396 |
uint32_t GrShape::segmentMask() const { |
| 397 |
// In order to match what a path would report, this has to inspect the shapes slightly |
| 398 |
// to reflect what they might simplify to. |
| 399 |
switch (this->type()) { |
| 400 |
case Type::kEmpty: |
| 401 |
return 0; |
| 402 |
case Type::kRRect: |
| 403 |
if (fRRect.isEmpty() || fRRect.isRect()) { |
| 404 |
return SkPath::kLine_SegmentMask; |
| 405 |
} else if (fRRect.isOval()) { |
| 406 |
return SkPath::kConic_SegmentMask; |
| 407 |
} else { |
| 408 |
return SkPath::kConic_SegmentMask | SkPath::kLine_SegmentMask; |
| 409 |
} |
| 410 |
case Type::kPath: |
| 411 |
return fPath.getSegmentMasks(); |
| 412 |
case Type::kArc: |
| 413 |
if (fArc.fType == SkArc::Type::kWedge) { |
| 414 |
return SkPath::kConic_SegmentMask | SkPath::kLine_SegmentMask; |
| 415 |
} else { |
| 416 |
return SkPath::kConic_SegmentMask; |
| 417 |
} |
| 418 |
case Type::kPoint: // fall through |
| 419 |
case Type::kLine: // "" |
| 420 |
case Type::kRect: |
| 421 |
return SkPath::kLine_SegmentMask; |
| 422 |
} |
| 423 |
SkUNREACHABLE; |
| 424 |
} |
| 425 |
|
| 426 |
SkPath GrShape::asPath(bool simpleFill) const { |
| 427 |
SkPathBuilder builder; |
| 428 |
if (!this->isPath() && !this->isArc()) { |
| 429 |
// When not a path, we need to set fill type on the path to match invertedness. |
| 430 |
// All the non-path geometries produce equivalent shapes with either even-odd or winding |
| 431 |
// so we can use the default fill type. |
| 432 |
builder.setFillType(kDefaultFillType); |
| 433 |
if (fInverted) { |
| 434 |
builder.toggleInverseFillType(); |
| 435 |
} |
| 436 |
} // Else when we're already a path, that will assign the fill type directly to 'out'. |
| 437 |
|
| 438 |
switch (this->type()) { |
| 439 |
case Type::kEmpty: |
| 440 |
break; |
| 441 |
case Type::kPoint: |
| 442 |
// A plain moveTo() or moveTo+close() does not match the expected path for a |
| 443 |
// point that is being dashed (see SkDashPath's handling of zero-length segments). |
| 444 |
builder.moveTo(fPoint); |
| 445 |
builder.lineTo(fPoint); |
| 446 |
break; |
| 447 |
case Type::kRect: |
| 448 |
builder.addRect(fRect, this->dir(), this->startIndex()); |
| 449 |
break; |
| 450 |
case Type::kRRect: |
| 451 |
builder.addRRect(fRRect, this->dir(), this->startIndex()); |
| 452 |
break; |
| 453 |
case Type::kPath: |
| 454 |
return fPath; |
| 455 |
case Type::kArc: { |
| 456 |
SkPath path = SkPathPriv::CreateDrawArcPath(fArc, simpleFill); |
| 457 |
// CreateDrawArcPath configures its fill type, so we just |
| 458 |
// have to ensure invertedness is correct. |
| 459 |
if (fInverted) { |
| 460 |
path.toggleInverseFillType(); |
| 461 |
} |
| 462 |
return path; |
| 463 |
} |
| 464 |
case Type::kLine: |
| 465 |
builder.moveTo(fLine.fP1); |
| 466 |
builder.lineTo(fLine.fP2); |
| 467 |
break; |
| 468 |
} |
| 469 |
return builder.detach(); |
| 470 |
} |