GrShape.cpp revision 487f8d385be2e0dcc7e46339d7bb12e4820b91c8
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 8#include "GrShape.h" 9 10GrShape& GrShape::operator=(const GrShape& that) { 11 fStyle = that.fStyle; 12 this->changeType(that.fType, Type::kPath == that.fType ? &that.path() : nullptr); 13 switch (fType) { 14 case Type::kEmpty: 15 break; 16 case Type::kRRect: 17 fRRectData = that.fRRectData; 18 break; 19 case Type::kLine: 20 fLineData = that.fLineData; 21 break; 22 case Type::kPath: 23 fPathData.fGenID = that.fPathData.fGenID; 24 break; 25 } 26 fInheritedKey.reset(that.fInheritedKey.count()); 27 sk_careful_memcpy(fInheritedKey.get(), that.fInheritedKey.get(), 28 sizeof(uint32_t) * fInheritedKey.count()); 29 return *this; 30} 31 32SkRect GrShape::bounds() const { 33 // Bounds where left == bottom or top == right can indicate a line or point shape. We return 34 // inverted bounds for a truly empty shape. 35 static constexpr SkRect kInverted = SkRect::MakeLTRB(1, 1, -1, -1); 36 switch (fType) { 37 case Type::kEmpty: 38 return kInverted; 39 case Type::kLine: { 40 SkRect bounds; 41 if (fLineData.fPts[0].fX < fLineData.fPts[1].fX) { 42 bounds.fLeft = fLineData.fPts[0].fX; 43 bounds.fRight = fLineData.fPts[1].fX; 44 } else { 45 bounds.fLeft = fLineData.fPts[1].fX; 46 bounds.fRight = fLineData.fPts[0].fX; 47 } 48 if (fLineData.fPts[0].fY < fLineData.fPts[1].fY) { 49 bounds.fTop = fLineData.fPts[0].fY; 50 bounds.fBottom = fLineData.fPts[1].fY; 51 } else { 52 bounds.fTop = fLineData.fPts[1].fY; 53 bounds.fBottom = fLineData.fPts[0].fY; 54 } 55 return bounds; 56 } 57 case Type::kRRect: 58 return fRRectData.fRRect.getBounds(); 59 case Type::kPath: 60 return this->path().getBounds(); 61 } 62 SkFAIL("Unknown shape type"); 63 return kInverted; 64} 65 66SkRect GrShape::styledBounds() const { 67 if (Type::kEmpty == fType && !fStyle.hasNonDashPathEffect()) { 68 return SkRect::MakeEmpty(); 69 } 70 SkRect bounds; 71 fStyle.adjustBounds(&bounds, this->bounds()); 72 return bounds; 73} 74 75int GrShape::unstyledKeySize() const { 76 if (fInheritedKey.count()) { 77 return fInheritedKey.count(); 78 } 79 switch (fType) { 80 case Type::kEmpty: 81 return 1; 82 case Type::kRRect: 83 SkASSERT(!fInheritedKey.count()); 84 SkASSERT(0 == SkRRect::kSizeInMemory % sizeof(uint32_t)); 85 // + 1 for the direction, start index, and inverseness. 86 return SkRRect::kSizeInMemory / sizeof(uint32_t) + 1; 87 case Type::kLine: 88 GR_STATIC_ASSERT(2 * sizeof(uint32_t) == sizeof(SkPoint)); 89 // 4 for the end points and 1 for the inverseness 90 return 5; 91 case Type::kPath: 92 if (0 == fPathData.fGenID) { 93 return -1; 94 } else { 95 // The key is the path ID and fill type. 96 return 2; 97 } 98 } 99 SkFAIL("Should never get here."); 100 return 0; 101} 102 103void GrShape::writeUnstyledKey(uint32_t* key) const { 104 SkASSERT(this->unstyledKeySize()); 105 SkDEBUGCODE(uint32_t* origKey = key;) 106 if (fInheritedKey.count()) { 107 memcpy(key, fInheritedKey.get(), sizeof(uint32_t) * fInheritedKey.count()); 108 SkDEBUGCODE(key += fInheritedKey.count();) 109 } else { 110 switch (fType) { 111 case Type::kEmpty: 112 *key++ = 1; 113 break; 114 case Type::kRRect: 115 fRRectData.fRRect.writeToMemory(key); 116 key += SkRRect::kSizeInMemory / sizeof(uint32_t); 117 *key = (fRRectData.fDir == SkPath::kCCW_Direction) ? (1 << 31) : 0; 118 *key |= fRRectData.fInverted ? (1 << 30) : 0; 119 *key++ |= fRRectData.fStart; 120 SkASSERT(fRRectData.fStart < 8); 121 break; 122 case Type::kLine: 123 memcpy(key, fLineData.fPts, 2 * sizeof(SkPoint)); 124 key += 4; 125 *key++ = fLineData.fInverted ? 1 : 0; 126 break; 127 case Type::kPath: 128 SkASSERT(fPathData.fGenID); 129 *key++ = fPathData.fGenID; 130 // We could canonicalize the fill rule for paths that don't differentiate between 131 // even/odd or winding fill (e.g. convex). 132 *key++ = this->path().getFillType(); 133 break; 134 } 135 } 136 SkASSERT(key - origKey == this->unstyledKeySize()); 137} 138 139void GrShape::setInheritedKey(const GrShape &parent, GrStyle::Apply apply, SkScalar scale) { 140 SkASSERT(!fInheritedKey.count()); 141 // If the output shape turns out to be simple, then we will just use its geometric key 142 if (Type::kPath == fType) { 143 // We want ApplyFullStyle(ApplyPathEffect(shape)) to have the same key as 144 // ApplyFullStyle(shape). 145 // The full key is structured as (geo,path_effect,stroke). 146 // If we do ApplyPathEffect we get get,path_effect as the inherited key. If we then 147 // do ApplyFullStyle we'll memcpy geo,path_effect into the new inherited key 148 // and then append the style key (which should now be stroke only) at the end. 149 int parentCnt = parent.fInheritedKey.count(); 150 bool useParentGeoKey = !parentCnt; 151 if (useParentGeoKey) { 152 parentCnt = parent.unstyledKeySize(); 153 if (parentCnt < 0) { 154 // The parent's geometry has no key so we will have no key. 155 fPathData.fGenID = 0; 156 return; 157 } 158 } 159 uint32_t styleKeyFlags = 0; 160 if (parent.knownToBeClosed()) { 161 styleKeyFlags |= GrStyle::kClosed_KeyFlag; 162 } 163 if (parent.asLine(nullptr, nullptr)) { 164 styleKeyFlags |= GrStyle::kNoJoins_KeyFlag; 165 } 166 int styleCnt = GrStyle::KeySize(parent.fStyle, apply, styleKeyFlags); 167 if (styleCnt < 0) { 168 // The style doesn't allow a key, set the path gen ID to 0 so that we fail when 169 // we try to get a key for the shape. 170 fPathData.fGenID = 0; 171 return; 172 } 173 fInheritedKey.reset(parentCnt + styleCnt); 174 if (useParentGeoKey) { 175 // This will be the geo key. 176 parent.writeUnstyledKey(fInheritedKey.get()); 177 } else { 178 // This should be (geo,path_effect). 179 memcpy(fInheritedKey.get(), parent.fInheritedKey.get(), 180 parentCnt * sizeof(uint32_t)); 181 } 182 // Now turn (geo,path_effect) or (geo) into (geo,path_effect,stroke) 183 GrStyle::WriteKey(fInheritedKey.get() + parentCnt, parent.fStyle, apply, scale, 184 styleKeyFlags); 185 } 186} 187 188GrShape::GrShape(const GrShape& that) : fStyle(that.fStyle) { 189 const SkPath* thatPath = Type::kPath == that.fType ? &that.fPathData.fPath : nullptr; 190 this->initType(that.fType, thatPath); 191 switch (fType) { 192 case Type::kEmpty: 193 break; 194 case Type::kRRect: 195 fRRectData = that.fRRectData; 196 break; 197 case Type::kLine: 198 fLineData = that.fLineData; 199 break; 200 case Type::kPath: 201 fPathData.fGenID = that.fPathData.fGenID; 202 break; 203 } 204 fInheritedKey.reset(that.fInheritedKey.count()); 205 sk_careful_memcpy(fInheritedKey.get(), that.fInheritedKey.get(), 206 sizeof(uint32_t) * fInheritedKey.count()); 207} 208 209GrShape::GrShape(const GrShape& parent, GrStyle::Apply apply, SkScalar scale) { 210 // TODO: Add some quantization of scale for better cache performance here or leave that up 211 // to caller? 212 // TODO: For certain shapes and stroke params we could ignore the scale. (e.g. miter or bevel 213 // stroke of a rect). 214 if (!parent.style().applies() || 215 (GrStyle::Apply::kPathEffectOnly == apply && !parent.style().pathEffect())) { 216 this->initType(Type::kEmpty); 217 *this = parent; 218 return; 219 } 220 221 SkPathEffect* pe = parent.fStyle.pathEffect(); 222 SkTLazy<SkPath> tmpPath; 223 const GrShape* parentForKey = &parent; 224 SkTLazy<GrShape> tmpParent; 225 this->initType(Type::kPath); 226 fPathData.fGenID = 0; 227 if (pe) { 228 const SkPath* srcForPathEffect; 229 if (parent.fType == Type::kPath) { 230 srcForPathEffect = &parent.path(); 231 } else { 232 srcForPathEffect = tmpPath.init(); 233 parent.asPath(tmpPath.get()); 234 } 235 // Should we consider bounds? Would have to include in key, but it'd be nice to know 236 // if the bounds actually modified anything before including in key. 237 SkStrokeRec strokeRec = parent.fStyle.strokeRec(); 238 if (!parent.fStyle.applyPathEffectToPath(&this->path(), &strokeRec, *srcForPathEffect, 239 scale)) { 240 tmpParent.init(*srcForPathEffect, GrStyle(strokeRec, nullptr)); 241 *this = tmpParent.get()->applyStyle(apply, scale); 242 return; 243 } 244 // A path effect has access to change the res scale but we aren't expecting it to and it 245 // would mess up our key computation. 246 SkASSERT(scale == strokeRec.getResScale()); 247 if (GrStyle::Apply::kPathEffectAndStrokeRec == apply && strokeRec.needToApply()) { 248 // The intermediate shape may not be a general path. If we we're just applying 249 // the path effect then attemptToReduceFromPath would catch it. This means that 250 // when we subsequently applied the remaining strokeRec we would have a non-path 251 // parent shape that would be used to determine the the stroked path's key. 252 // We detect that case here and change parentForKey to a temporary that represents 253 // the simpler shape so that applying both path effect and the strokerec all at 254 // once produces the same key. 255 tmpParent.init(this->path(), GrStyle(strokeRec, nullptr)); 256 tmpParent.get()->setInheritedKey(parent, GrStyle::Apply::kPathEffectOnly, scale); 257 if (!tmpPath.isValid()) { 258 tmpPath.init(); 259 } 260 tmpParent.get()->asPath(tmpPath.get()); 261 SkStrokeRec::InitStyle fillOrHairline; 262 // The parent shape may have simplified away the strokeRec, check for that here. 263 if (tmpParent.get()->style().applies()) { 264 SkAssertResult(tmpParent.get()->style().applyToPath(&this->path(), &fillOrHairline, 265 *tmpPath.get(), scale)); 266 } else if (tmpParent.get()->style().isSimpleFill()) { 267 fillOrHairline = SkStrokeRec::kFill_InitStyle; 268 } else { 269 SkASSERT(tmpParent.get()->style().isSimpleHairline()); 270 fillOrHairline = SkStrokeRec::kHairline_InitStyle; 271 } 272 fStyle.resetToInitStyle(fillOrHairline); 273 parentForKey = tmpParent.get(); 274 } else { 275 fStyle = GrStyle(strokeRec, nullptr); 276 } 277 } else { 278 const SkPath* srcForParentStyle; 279 if (parent.fType == Type::kPath) { 280 srcForParentStyle = &parent.path(); 281 } else { 282 srcForParentStyle = tmpPath.init(); 283 parent.asPath(tmpPath.get()); 284 } 285 SkStrokeRec::InitStyle fillOrHairline; 286 SkASSERT(parent.fStyle.applies()); 287 SkASSERT(!parent.fStyle.pathEffect()); 288 SkAssertResult(parent.fStyle.applyToPath(&this->path(), &fillOrHairline, *srcForParentStyle, 289 scale)); 290 fStyle.resetToInitStyle(fillOrHairline); 291 } 292 this->attemptToSimplifyPath(); 293 this->setInheritedKey(*parentForKey, apply, scale); 294} 295 296void GrShape::attemptToSimplifyPath() { 297 SkRect rect; 298 SkRRect rrect; 299 SkPath::Direction rrectDir; 300 unsigned rrectStart; 301 bool inverted = this->path().isInverseFillType(); 302 SkPoint pts[2]; 303 if (this->path().isEmpty()) { 304 this->changeType(Type::kEmpty); 305 } else if (this->path().isLine(pts)) { 306 this->changeType(Type::kLine); 307 fLineData.fPts[0] = pts[0]; 308 fLineData.fPts[1] = pts[1]; 309 fLineData.fInverted = inverted; 310 } else if (this->path().isRRect(&rrect, &rrectDir, &rrectStart)) { 311 this->changeType(Type::kRRect); 312 fRRectData.fRRect = rrect; 313 fRRectData.fDir = rrectDir; 314 fRRectData.fStart = rrectStart; 315 fRRectData.fInverted = inverted; 316 // Currently SkPath does not acknowledge that empty, rect, or oval subtypes as rrects. 317 SkASSERT(!fRRectData.fRRect.isEmpty()); 318 SkASSERT(fRRectData.fRRect.getType() != SkRRect::kRect_Type); 319 SkASSERT(fRRectData.fRRect.getType() != SkRRect::kOval_Type); 320 } else if (this->path().isOval(&rect, &rrectDir, &rrectStart)) { 321 this->changeType(Type::kRRect); 322 fRRectData.fRRect.setOval(rect); 323 fRRectData.fDir = rrectDir; 324 fRRectData.fInverted = inverted; 325 // convert from oval indexing to rrect indexiing. 326 fRRectData.fStart = 2 * rrectStart; 327 } else if (SkPathPriv::IsSimpleClosedRect(this->path(), &rect, &rrectDir, &rrectStart)) { 328 this->changeType(Type::kRRect); 329 // When there is a path effect we restrict rect detection to the narrower API that 330 // gives us the starting position. Otherwise, we will retry with the more aggressive 331 // isRect(). 332 fRRectData.fRRect.setRect(rect); 333 fRRectData.fInverted = inverted; 334 fRRectData.fDir = rrectDir; 335 // convert from rect indexing to rrect indexiing. 336 fRRectData.fStart = 2 * rrectStart; 337 } else if (!this->style().hasPathEffect()) { 338 bool closed; 339 if (this->path().isRect(&rect, &closed, nullptr)) { 340 if (closed || this->style().isSimpleFill()) { 341 this->changeType(Type::kRRect); 342 fRRectData.fRRect.setRect(rect); 343 // Since there is no path effect the dir and start index is immaterial. 344 fRRectData.fDir = kDefaultRRectDir; 345 fRRectData.fStart = kDefaultRRectStart; 346 // There isn't dashing so we will have to preserver inverseness. 347 fRRectData.fInverted = inverted; 348 } 349 } 350 } 351 if (Type::kPath != fType) { 352 fInheritedKey.reset(0); 353 if (Type::kRRect == fType) { 354 this->attemptToSimplifyRRect(); 355 } else if (Type::kLine == fType) { 356 this->attemptToSimplifyLine(); 357 } 358 } else { 359 if (fInheritedKey.count() || this->path().isVolatile()) { 360 fPathData.fGenID = 0; 361 } else { 362 fPathData.fGenID = this->path().getGenerationID(); 363 } 364 if (this->style().isSimpleFill()) { 365 this->path().close(); 366 this->path().setIsVolatile(true); 367 } 368 if (!this->style().hasNonDashPathEffect()) { 369 if (this->style().strokeRec().getStyle() == SkStrokeRec::kStroke_Style || 370 this->style().strokeRec().getStyle() == SkStrokeRec::kHairline_Style) { 371 // Stroke styles don't differentiate between winding and even/odd. 372 // Moreover, dashing ignores inverseness (skbug.com/5421) 373 bool inverse = !this->style().isDashed() && this->path().isInverseFillType(); 374 if (inverse) { 375 this->path().setFillType(kDefaultPathInverseFillType); 376 } else { 377 this->path().setFillType(kDefaultPathFillType); 378 } 379 } else if (this->path().isConvex()) { 380 // There is no distinction between even/odd and non-zero winding count for convex 381 // paths. 382 if (this->path().isInverseFillType()) { 383 this->path().setFillType(kDefaultPathInverseFillType); 384 } else { 385 this->path().setFillType(kDefaultPathFillType); 386 } 387 } 388 } 389 } 390} 391 392void GrShape::attemptToSimplifyRRect() { 393 SkASSERT(Type::kRRect == fType); 394 SkASSERT(!fInheritedKey.count()); 395 if (fRRectData.fRRect.isEmpty()) { 396 fType = Type::kEmpty; 397 return; 398 } 399 if (!this->style().hasPathEffect()) { 400 fRRectData.fDir = kDefaultRRectDir; 401 fRRectData.fStart = kDefaultRRectStart; 402 } else if (fStyle.isDashed()) { 403 // Dashing ignores the inverseness (currently). skbug.com/5421 404 fRRectData.fInverted = false; 405 } 406 // Turn a stroke-and-filled miter rect into a filled rect. TODO: more rrect stroke shortcuts. 407 if (!fStyle.hasPathEffect() && 408 fStyle.strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style && 409 fStyle.strokeRec().getJoin() == SkPaint::kMiter_Join && 410 fStyle.strokeRec().getMiter() >= SK_ScalarSqrt2 && 411 fRRectData.fRRect.isRect()) { 412 SkScalar r = fStyle.strokeRec().getWidth() / 2; 413 fRRectData.fRRect = SkRRect::MakeRect(fRRectData.fRRect.rect().makeOutset(r, r)); 414 fStyle = GrStyle::SimpleFill(); 415 } 416} 417 418void GrShape::attemptToSimplifyLine() { 419 SkASSERT(Type::kLine == fType); 420 SkASSERT(!fInheritedKey.count()); 421 if (fStyle.isDashed()) { 422 // Dashing ignores inverseness. 423 fLineData.fInverted = false; 424 return; 425 } else if (fStyle.hasPathEffect()) { 426 return; 427 } 428 if (fStyle.strokeRec().getStyle() == SkStrokeRec::kStrokeAndFill_Style) { 429 // Make stroke + fill be stroke since the fill is empty. 430 SkStrokeRec rec = fStyle.strokeRec(); 431 rec.setStrokeStyle(fStyle.strokeRec().getWidth(), false); 432 fStyle = GrStyle(rec, nullptr); 433 } 434 if (fStyle.isSimpleFill() && !fLineData.fInverted) { 435 this->changeType(Type::kEmpty); 436 return; 437 } 438 SkPoint* pts = fLineData.fPts; 439 if (fStyle.strokeRec().getStyle() == SkStrokeRec::kStroke_Style) { 440 // If it is horizontal or vertical we will turn it into a filled rrect. 441 SkRect rect; 442 rect.fLeft = SkTMin(pts[0].fX, pts[1].fX); 443 rect.fRight = SkTMax(pts[0].fX, pts[1].fX); 444 rect.fTop = SkTMin(pts[0].fY, pts[1].fY); 445 rect.fBottom = SkTMax(pts[0].fY, pts[1].fY); 446 bool eqX = rect.fLeft == rect.fRight; 447 bool eqY = rect.fTop == rect.fBottom; 448 if (eqX || eqY) { 449 SkScalar r = fStyle.strokeRec().getWidth() / 2; 450 bool inverted = fLineData.fInverted; 451 this->changeType(Type::kRRect); 452 switch (fStyle.strokeRec().getCap()) { 453 case SkPaint::kButt_Cap: 454 if (eqX && eqY) { 455 this->changeType(Type::kEmpty); 456 return; 457 } 458 if (eqX) { 459 rect.outset(r, 0); 460 } else { 461 rect.outset(0, r); 462 } 463 fRRectData.fRRect = SkRRect::MakeRect(rect); 464 break; 465 case SkPaint::kSquare_Cap: 466 rect.outset(r, r); 467 fRRectData.fRRect = SkRRect::MakeRect(rect); 468 break; 469 case SkPaint::kRound_Cap: 470 rect.outset(r, r); 471 fRRectData.fRRect = SkRRect::MakeRectXY(rect, r, r); 472 break; 473 } 474 fRRectData.fInverted = inverted; 475 fRRectData.fDir = kDefaultRRectDir; 476 fRRectData.fStart = kDefaultRRectStart; 477 if (fRRectData.fRRect.isEmpty()) { 478 // This can happen when r is very small relative to the rect edges. 479 this->changeType(Type::kEmpty); 480 return; 481 } 482 fStyle = GrStyle::SimpleFill(); 483 return; 484 } 485 } 486 // Only path effects could care about the order of the points. Otherwise canonicalize 487 // the point order. 488 if (pts[1].fY < pts[0].fY || (pts[1].fY == pts[0].fY && pts[1].fX < pts[0].fX)) { 489 SkTSwap(pts[0], pts[1]); 490 } 491} 492