| 1 |
/* |
| 2 |
* Copyright 2017 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 |
|
| 9 |
#include "src/utils/SkShadowTessellator.h" |
| 10 |
|
| 11 |
#include "include/core/SkColor.h" |
| 12 |
#include "include/core/SkMatrix.h" |
| 13 |
#include "include/core/SkPath.h" |
| 14 |
#include "include/core/SkPoint.h" |
| 15 |
#include "include/core/SkPoint3.h" |
| 16 |
#include "include/core/SkRect.h" |
| 17 |
#include "include/core/SkTypes.h" |
| 18 |
#include "include/core/SkVertices.h" |
| 19 |
#include "include/private/base/SkFloatingPoint.h" |
| 20 |
#include "include/private/base/SkTDArray.h" |
| 21 |
#include "include/private/base/SkTemplates.h" |
| 22 |
#include "src/core/SkColorData.h" |
| 23 |
#include "src/core/SkDrawShadowInfo.h" |
| 24 |
#include "src/core/SkGeometry.h" |
| 25 |
#include "src/core/SkPointPriv.h" |
| 26 |
#include "src/core/SkRectPriv.h" |
| 27 |
#include "src/utils/SkPolyUtils.h" |
| 28 |
|
| 29 |
#include <algorithm> |
| 30 |
#include <cstdint> |
| 31 |
|
| 32 |
|
| 33 |
#if defined(SK_GANESH) |
| 34 |
#include "src/gpu/ganesh/geometry/GrPathUtils.h" |
| 35 |
#endif |
| 36 |
|
| 37 |
using namespace skia_private; |
| 38 |
|
| 39 |
#if !defined(SK_ENABLE_OPTIMIZE_SIZE) |
| 40 |
|
| 41 |
/** |
| 42 |
* Base class |
| 43 |
*/ |
| 44 |
class SkBaseShadowTessellator { |
| 45 |
public: |
| 46 |
SkBaseShadowTessellator(const SkPoint3& zPlaneParams, const SkRect& bounds, bool transparent); |
| 47 |
virtual ~SkBaseShadowTessellator() {} |
| 48 |
|
| 49 |
sk_sp<SkVertices> releaseVertices() { |
| 50 |
if (!fSucceeded) { |
| 51 |
return nullptr; |
| 52 |
} |
| 53 |
return SkVertices::MakeCopy(SkVertices::kTriangles_VertexMode, this->vertexCount(), |
| 54 |
fPositions.begin(), nullptr, fColors.begin(), |
| 55 |
this->indexCount(), fIndices.begin()); |
| 56 |
} |
| 57 |
|
| 58 |
protected: |
| 59 |
inline static constexpr auto kMinHeight = 0.1f; |
| 60 |
inline static constexpr auto kPenumbraColor = SK_ColorTRANSPARENT; |
| 61 |
inline static constexpr auto kUmbraColor = SK_ColorBLACK; |
| 62 |
|
| 63 |
int vertexCount() const { return fPositions.size(); } |
| 64 |
int indexCount() const { return fIndices.size(); } |
| 65 |
|
| 66 |
// initialization methods |
| 67 |
bool accumulateCentroid(const SkPoint& c, const SkPoint& n); |
| 68 |
bool checkConvexity(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2); |
| 69 |
void finishPathPolygon(); |
| 70 |
|
| 71 |
// convex shadow methods |
| 72 |
bool computeConvexShadow(SkScalar inset, SkScalar outset, bool doClip); |
| 73 |
void computeClipVectorsAndTestCentroid(); |
| 74 |
bool clipUmbraPoint(const SkPoint& umbraPoint, const SkPoint& centroid, SkPoint* clipPoint); |
| 75 |
void addEdge(const SkVector& nextPoint, const SkVector& nextNormal, SkColor umbraColor, |
| 76 |
const SkTDArray<SkPoint>& umbraPolygon, bool lastEdge, bool doClip); |
| 77 |
bool addInnerPoint(const SkPoint& pathPoint, SkColor umbraColor, |
| 78 |
const SkTDArray<SkPoint>& umbraPolygon, int* currUmbraIndex); |
| 79 |
int getClosestUmbraIndex(const SkPoint& point, const SkTDArray<SkPoint>& umbraPolygon); |
| 80 |
|
| 81 |
// concave shadow methods |
| 82 |
bool computeConcaveShadow(SkScalar inset, SkScalar outset); |
| 83 |
void stitchConcaveRings(const SkTDArray<SkPoint>& umbraPolygon, |
| 84 |
SkTDArray<int>* umbraIndices, |
| 85 |
const SkTDArray<SkPoint>& penumbraPolygon, |
| 86 |
SkTDArray<int>* penumbraIndices); |
| 87 |
|
| 88 |
void handleLine(const SkPoint& p); |
| 89 |
void handleLine(const SkMatrix& m, SkPoint* p); |
| 90 |
|
| 91 |
void handleQuad(const SkPoint pts[3]); |
| 92 |
void handleQuad(const SkMatrix& m, SkPoint pts[3]); |
| 93 |
|
| 94 |
void handleCubic(const SkMatrix& m, SkPoint pts[4]); |
| 95 |
|
| 96 |
void handleConic(const SkMatrix& m, SkPoint pts[3], SkScalar w); |
| 97 |
|
| 98 |
bool addArc(const SkVector& nextNormal, SkScalar offset, bool finishArc); |
| 99 |
|
| 100 |
void appendTriangle(uint16_t index0, uint16_t index1, uint16_t index2); |
| 101 |
void appendQuad(uint16_t index0, uint16_t index1, uint16_t index2, uint16_t index3); |
| 102 |
|
| 103 |
SkScalar heightFunc(SkScalar x, SkScalar y) { |
| 104 |
return fZPlaneParams.fX*x + fZPlaneParams.fY*y + fZPlaneParams.fZ; |
| 105 |
} |
| 106 |
|
| 107 |
SkPoint3 fZPlaneParams; |
| 108 |
|
| 109 |
// temporary buffer |
| 110 |
SkTDArray<SkPoint> fPointBuffer; |
| 111 |
|
| 112 |
SkTDArray<SkPoint> fPositions; |
| 113 |
SkTDArray<SkColor> fColors; |
| 114 |
SkTDArray<uint16_t> fIndices; |
| 115 |
|
| 116 |
SkTDArray<SkPoint> fPathPolygon; |
| 117 |
SkTDArray<SkPoint> fClipPolygon; |
| 118 |
SkTDArray<SkVector> fClipVectors; |
| 119 |
|
| 120 |
SkRect fPathBounds; |
| 121 |
SkPoint fCentroid; |
| 122 |
SkScalar fArea; |
| 123 |
SkScalar fLastArea; |
| 124 |
SkScalar fLastCross; |
| 125 |
|
| 126 |
int fFirstVertexIndex; |
| 127 |
SkVector fFirstOutset; |
| 128 |
SkPoint fFirstPoint; |
| 129 |
|
| 130 |
bool fSucceeded; |
| 131 |
bool fTransparent; |
| 132 |
bool fIsConvex; |
| 133 |
bool fValidUmbra; |
| 134 |
|
| 135 |
SkScalar fDirection; |
| 136 |
int fPrevUmbraIndex; |
| 137 |
int fCurrUmbraIndex; |
| 138 |
int fCurrClipIndex; |
| 139 |
bool fPrevUmbraOutside; |
| 140 |
bool fFirstUmbraOutside; |
| 141 |
SkVector fPrevOutset; |
| 142 |
SkPoint fPrevPoint; |
| 143 |
}; |
| 144 |
|
| 145 |
static bool compute_normal(const SkPoint& p0, const SkPoint& p1, SkScalar dir, |
| 146 |
SkVector* newNormal) { |
| 147 |
SkVector normal; |
| 148 |
// compute perpendicular |
| 149 |
normal.fX = p0.fY - p1.fY; |
| 150 |
normal.fY = p1.fX - p0.fX; |
| 151 |
normal *= dir; |
| 152 |
if (!normal.normalize()) { |
| 153 |
return false; |
| 154 |
} |
| 155 |
*newNormal = normal; |
| 156 |
return true; |
| 157 |
} |
| 158 |
|
| 159 |
static bool duplicate_pt(const SkPoint& p0, const SkPoint& p1) { |
| 160 |
static constexpr SkScalar kClose = (SK_Scalar1 / 16); |
| 161 |
static constexpr SkScalar kCloseSqd = kClose * kClose; |
| 162 |
|
| 163 |
SkScalar distSq = SkPointPriv::DistanceToSqd(p0, p1); |
| 164 |
return distSq < kCloseSqd; |
| 165 |
} |
| 166 |
|
| 167 |
static SkScalar perp_dot(const SkPoint& p0, const SkPoint& p1, const SkPoint& p2) { |
| 168 |
SkVector v0 = p1 - p0; |
| 169 |
SkVector v1 = p2 - p1; |
| 170 |
return v0.cross(v1); |
| 171 |
} |
| 172 |
|
| 173 |
SkBaseShadowTessellator::SkBaseShadowTessellator(const SkPoint3& zPlaneParams, const SkRect& bounds, |
| 174 |
bool transparent) |
| 175 |
: fZPlaneParams(zPlaneParams) |
| 176 |
, fPathBounds(bounds) |
| 177 |
, fCentroid({0, 0}) |
| 178 |
, fArea(0) |
| 179 |
, fLastArea(0) |
| 180 |
, fLastCross(0) |
| 181 |
, fFirstVertexIndex(-1) |
| 182 |
, fSucceeded(false) |
| 183 |
, fTransparent(transparent) |
| 184 |
, fIsConvex(true) |
| 185 |
, fValidUmbra(true) |
| 186 |
, fDirection(1) |
| 187 |
, fPrevUmbraIndex(-1) |
| 188 |
, fCurrUmbraIndex(0) |
| 189 |
, fCurrClipIndex(0) |
| 190 |
, fPrevUmbraOutside(false) |
| 191 |
, fFirstUmbraOutside(false) { |
| 192 |
// child classes will set reserve for positions, colors and indices |
| 193 |
} |
| 194 |
|
| 195 |
bool SkBaseShadowTessellator::accumulateCentroid(const SkPoint& curr, const SkPoint& next) { |
| 196 |
if (duplicate_pt(curr, next)) { |
| 197 |
return false; |
| 198 |
} |
| 199 |
|
| 200 |
SkASSERT(!fPathPolygon.empty()); |
| 201 |
SkVector v0 = curr - fPathPolygon[0]; |
| 202 |
SkVector v1 = next - fPathPolygon[0]; |
| 203 |
SkScalar quadArea = v0.cross(v1); |
| 204 |
fCentroid.fX += (v0.fX + v1.fX) * quadArea; |
| 205 |
fCentroid.fY += (v0.fY + v1.fY) * quadArea; |
| 206 |
fArea += quadArea; |
| 207 |
// convexity check |
| 208 |
if (quadArea*fLastArea < 0) { |
| 209 |
fIsConvex = false; |
| 210 |
} |
| 211 |
if (0 != quadArea) { |
| 212 |
fLastArea = quadArea; |
| 213 |
} |
| 214 |
|
| 215 |
return true; |
| 216 |
} |
| 217 |
|
| 218 |
bool SkBaseShadowTessellator::checkConvexity(const SkPoint& p0, |
| 219 |
const SkPoint& p1, |
| 220 |
const SkPoint& p2) { |
| 221 |
SkScalar cross = perp_dot(p0, p1, p2); |
| 222 |
// skip collinear point |
| 223 |
if (SkScalarNearlyZero(cross)) { |
| 224 |
return false; |
| 225 |
} |
| 226 |
|
| 227 |
// check for convexity |
| 228 |
if (fLastCross*cross < 0) { |
| 229 |
fIsConvex = false; |
| 230 |
} |
| 231 |
if (0 != cross) { |
| 232 |
fLastCross = cross; |
| 233 |
} |
| 234 |
|
| 235 |
return true; |
| 236 |
} |
| 237 |
|
| 238 |
void SkBaseShadowTessellator::finishPathPolygon() { |
| 239 |
if (fPathPolygon.size() > 1) { |
| 240 |
if (!this->accumulateCentroid(fPathPolygon[fPathPolygon.size() - 1], fPathPolygon[0])) { |
| 241 |
// remove coincident point |
| 242 |
fPathPolygon.pop_back(); |
| 243 |
} |
| 244 |
} |
| 245 |
|
| 246 |
if (fPathPolygon.size() > 2) { |
| 247 |
// do this before the final convexity check, so we use the correct fPathPolygon[0] |
| 248 |
fCentroid *= sk_ieee_float_divide(1, 3 * fArea); |
| 249 |
fCentroid += fPathPolygon[0]; |
| 250 |
if (!checkConvexity(fPathPolygon[fPathPolygon.size() - 2], |
| 251 |
fPathPolygon[fPathPolygon.size() - 1], |
| 252 |
fPathPolygon[0])) { |
| 253 |
// remove collinear point |
| 254 |
fPathPolygon[0] = fPathPolygon[fPathPolygon.size() - 1]; |
| 255 |
fPathPolygon.pop_back(); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
// if area is positive, winding is ccw |
| 260 |
fDirection = fArea > 0 ? -1 : 1; |
| 261 |
} |
| 262 |
|
| 263 |
bool SkBaseShadowTessellator::computeConvexShadow(SkScalar inset, SkScalar outset, bool doClip) { |
| 264 |
if (doClip) { |
| 265 |
this->computeClipVectorsAndTestCentroid(); |
| 266 |
} |
| 267 |
|
| 268 |
// adjust inset distance and umbra color if necessary |
| 269 |
auto umbraColor = kUmbraColor; |
| 270 |
SkScalar minDistSq = SkPointPriv::DistanceToLineSegmentBetweenSqd(fCentroid, |
| 271 |
fPathPolygon[0], |
| 272 |
fPathPolygon[1]); |
| 273 |
for (int i = 1; i < fPathPolygon.size(); ++i) { |
| 274 |
int j = i + 1; |
| 275 |
if (i == fPathPolygon.size() - 1) { |
| 276 |
j = 0; |
| 277 |
} |
| 278 |
SkPoint currPoint = fPathPolygon[i]; |
| 279 |
SkPoint nextPoint = fPathPolygon[j]; |
| 280 |
SkScalar distSq = SkPointPriv::DistanceToLineSegmentBetweenSqd(fCentroid, currPoint, |
| 281 |
nextPoint); |
| 282 |
if (distSq < minDistSq) { |
| 283 |
minDistSq = distSq; |
| 284 |
} |
| 285 |
} |
| 286 |
|
| 287 |
SkTDArray<SkPoint> insetPolygon; |
| 288 |
if (inset > SK_ScalarNearlyZero) { |
| 289 |
static constexpr auto kTolerance = 1.0e-2f; |
| 290 |
if (minDistSq < (inset + kTolerance)*(inset + kTolerance)) { |
| 291 |
// if the umbra would collapse, we back off a bit on inner blur and adjust the alpha |
| 292 |
auto newInset = SkScalarSqrt(minDistSq) - kTolerance; |
| 293 |
auto ratio = 128 * (newInset / inset + 1); |
| 294 |
SkASSERT(SkIsFinite(ratio)); |
| 295 |
// they aren't PMColors, but the interpolation algorithm is the same |
| 296 |
umbraColor = SkPMLerp(kUmbraColor, kPenumbraColor, (unsigned)ratio); |
| 297 |
inset = newInset; |
| 298 |
} |
| 299 |
|
| 300 |
// generate inner ring |
| 301 |
if (!SkInsetConvexPolygon(&fPathPolygon[0], fPathPolygon.size(), inset, |
| 302 |
&insetPolygon)) { |
| 303 |
// not ideal, but in this case we'll inset using the centroid |
| 304 |
fValidUmbra = false; |
| 305 |
} |
| 306 |
} |
| 307 |
const SkTDArray<SkPoint>& umbraPolygon = (inset > SK_ScalarNearlyZero) ? insetPolygon |
| 308 |
: fPathPolygon; |
| 309 |
|
| 310 |
// walk around the path polygon, generate outer ring and connect to inner ring |
| 311 |
if (fTransparent) { |
| 312 |
fPositions.push_back(fCentroid); |
| 313 |
fColors.push_back(umbraColor); |
| 314 |
} |
| 315 |
fCurrUmbraIndex = 0; |
| 316 |
|
| 317 |
// initial setup |
| 318 |
// add first quad |
| 319 |
int polyCount = fPathPolygon.size(); |
| 320 |
if (!compute_normal(fPathPolygon[polyCount - 1], fPathPolygon[0], fDirection, &fFirstOutset)) { |
| 321 |
// polygon should be sanitized by this point, so this is unrecoverable |
| 322 |
return false; |
| 323 |
} |
| 324 |
|
| 325 |
fFirstOutset *= outset; |
| 326 |
fFirstPoint = fPathPolygon[polyCount - 1]; |
| 327 |
fFirstVertexIndex = fPositions.size(); |
| 328 |
fPrevOutset = fFirstOutset; |
| 329 |
fPrevPoint = fFirstPoint; |
| 330 |
fPrevUmbraIndex = -1; |
| 331 |
|
| 332 |
this->addInnerPoint(fFirstPoint, umbraColor, umbraPolygon, &fPrevUmbraIndex); |
| 333 |
|
| 334 |
if (!fTransparent && doClip) { |
| 335 |
SkPoint clipPoint; |
| 336 |
bool isOutside = this->clipUmbraPoint(fPositions[fFirstVertexIndex], |
| 337 |
fCentroid, &clipPoint); |
| 338 |
if (isOutside) { |
| 339 |
fPositions.push_back(clipPoint); |
| 340 |
fColors.push_back(umbraColor); |
| 341 |
} |
| 342 |
fPrevUmbraOutside = isOutside; |
| 343 |
fFirstUmbraOutside = isOutside; |
| 344 |
} |
| 345 |
|
| 346 |
SkPoint newPoint = fFirstPoint + fFirstOutset; |
| 347 |
fPositions.push_back(newPoint); |
| 348 |
fColors.push_back(kPenumbraColor); |
| 349 |
this->addEdge(fPathPolygon[0], fFirstOutset, umbraColor, umbraPolygon, false, doClip); |
| 350 |
|
| 351 |
for (int i = 1; i < polyCount; ++i) { |
| 352 |
SkVector normal; |
| 353 |
if (!compute_normal(fPrevPoint, fPathPolygon[i], fDirection, &normal)) { |
| 354 |
return false; |
| 355 |
} |
| 356 |
normal *= outset; |
| 357 |
this->addArc(normal, outset, true); |
| 358 |
this->addEdge(fPathPolygon[i], normal, umbraColor, umbraPolygon, |
| 359 |
i == polyCount - 1, doClip); |
| 360 |
} |
| 361 |
SkASSERT(this->indexCount()); |
| 362 |
|
| 363 |
// final fan |
| 364 |
SkASSERT(fPositions.size() >= 3); |
| 365 |
if (this->addArc(fFirstOutset, outset, false)) { |
| 366 |
if (fFirstUmbraOutside) { |
| 367 |
this->appendTriangle(fFirstVertexIndex, fPositions.size() - 1, |
| 368 |
fFirstVertexIndex + 2); |
| 369 |
} else { |
| 370 |
this->appendTriangle(fFirstVertexIndex, fPositions.size() - 1, |
| 371 |
fFirstVertexIndex + 1); |
| 372 |
} |
| 373 |
} else { |
| 374 |
// no arc added, fix up by setting first penumbra point position to last one |
| 375 |
if (fFirstUmbraOutside) { |
| 376 |
fPositions[fFirstVertexIndex + 2] = fPositions[fPositions.size() - 1]; |
| 377 |
} else { |
| 378 |
fPositions[fFirstVertexIndex + 1] = fPositions[fPositions.size() - 1]; |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
return true; |
| 383 |
} |
| 384 |
|
| 385 |
void SkBaseShadowTessellator::computeClipVectorsAndTestCentroid() { |
| 386 |
SkASSERT(fClipPolygon.size() >= 3); |
| 387 |
fCurrClipIndex = fClipPolygon.size() - 1; |
| 388 |
|
| 389 |
// init clip vectors |
| 390 |
SkVector v0 = fClipPolygon[1] - fClipPolygon[0]; |
| 391 |
SkVector v1 = fClipPolygon[2] - fClipPolygon[0]; |
| 392 |
fClipVectors.push_back(v0); |
| 393 |
|
| 394 |
// init centroid check |
| 395 |
bool hiddenCentroid = true; |
| 396 |
v1 = fCentroid - fClipPolygon[0]; |
| 397 |
SkScalar initCross = v0.cross(v1); |
| 398 |
|
| 399 |
for (int p = 1; p < fClipPolygon.size(); ++p) { |
| 400 |
// add to clip vectors |
| 401 |
v0 = fClipPolygon[(p + 1) % fClipPolygon.size()] - fClipPolygon[p]; |
| 402 |
fClipVectors.push_back(v0); |
| 403 |
// Determine if transformed centroid is inside clipPolygon. |
| 404 |
v1 = fCentroid - fClipPolygon[p]; |
| 405 |
if (initCross*v0.cross(v1) <= 0) { |
| 406 |
hiddenCentroid = false; |
| 407 |
} |
| 408 |
} |
| 409 |
SkASSERT(fClipVectors.size() == fClipPolygon.size()); |
| 410 |
|
| 411 |
fTransparent = fTransparent || !hiddenCentroid; |
| 412 |
} |
| 413 |
|
| 414 |
void SkBaseShadowTessellator::addEdge(const SkPoint& nextPoint, const SkVector& nextNormal, |
| 415 |
SkColor umbraColor, const SkTDArray<SkPoint>& umbraPolygon, |
| 416 |
bool lastEdge, bool doClip) { |
| 417 |
// add next umbra point |
| 418 |
int currUmbraIndex; |
| 419 |
bool duplicate; |
| 420 |
if (lastEdge) { |
| 421 |
duplicate = false; |
| 422 |
currUmbraIndex = fFirstVertexIndex; |
| 423 |
fPrevPoint = nextPoint; |
| 424 |
} else { |
| 425 |
duplicate = this->addInnerPoint(nextPoint, umbraColor, umbraPolygon, &currUmbraIndex); |
| 426 |
} |
| 427 |
int prevPenumbraIndex = duplicate || (currUmbraIndex == fFirstVertexIndex) |
| 428 |
? fPositions.size() - 1 |
| 429 |
: fPositions.size() - 2; |
| 430 |
if (!duplicate) { |
| 431 |
// add to center fan if transparent or centroid showing |
| 432 |
if (fTransparent) { |
| 433 |
this->appendTriangle(0, fPrevUmbraIndex, currUmbraIndex); |
| 434 |
// otherwise add to clip ring |
| 435 |
} else if (doClip) { |
| 436 |
SkPoint clipPoint; |
| 437 |
bool isOutside = lastEdge ? fFirstUmbraOutside |
| 438 |
: this->clipUmbraPoint(fPositions[currUmbraIndex], fCentroid, |
| 439 |
&clipPoint); |
| 440 |
if (isOutside) { |
| 441 |
if (!lastEdge) { |
| 442 |
fPositions.push_back(clipPoint); |
| 443 |
fColors.push_back(umbraColor); |
| 444 |
} |
| 445 |
this->appendTriangle(fPrevUmbraIndex, currUmbraIndex, currUmbraIndex + 1); |
| 446 |
if (fPrevUmbraOutside) { |
| 447 |
// fill out quad |
| 448 |
this->appendTriangle(fPrevUmbraIndex, currUmbraIndex + 1, |
| 449 |
fPrevUmbraIndex + 1); |
| 450 |
} |
| 451 |
} else if (fPrevUmbraOutside) { |
| 452 |
// add tri |
| 453 |
this->appendTriangle(fPrevUmbraIndex, currUmbraIndex, fPrevUmbraIndex + 1); |
| 454 |
} |
| 455 |
|
| 456 |
fPrevUmbraOutside = isOutside; |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
// add next penumbra point and quad |
| 461 |
SkPoint newPoint = nextPoint + nextNormal; |
| 462 |
fPositions.push_back(newPoint); |
| 463 |
fColors.push_back(kPenumbraColor); |
| 464 |
|
| 465 |
if (!duplicate) { |
| 466 |
this->appendTriangle(fPrevUmbraIndex, prevPenumbraIndex, currUmbraIndex); |
| 467 |
} |
| 468 |
this->appendTriangle(prevPenumbraIndex, fPositions.size() - 1, currUmbraIndex); |
| 469 |
|
| 470 |
fPrevUmbraIndex = currUmbraIndex; |
| 471 |
fPrevOutset = nextNormal; |
| 472 |
} |
| 473 |
|
| 474 |
bool SkBaseShadowTessellator::clipUmbraPoint(const SkPoint& umbraPoint, const SkPoint& centroid, |
| 475 |
SkPoint* clipPoint) { |
| 476 |
SkVector segmentVector = centroid - umbraPoint; |
| 477 |
|
| 478 |
int startClipPoint = fCurrClipIndex; |
| 479 |
do { |
| 480 |
SkVector dp = umbraPoint - fClipPolygon[fCurrClipIndex]; |
| 481 |
SkScalar denom = fClipVectors[fCurrClipIndex].cross(segmentVector); |
| 482 |
SkScalar t_num = dp.cross(segmentVector); |
| 483 |
// if line segments are nearly parallel |
| 484 |
if (SkScalarNearlyZero(denom)) { |
| 485 |
// and collinear |
| 486 |
if (SkScalarNearlyZero(t_num)) { |
| 487 |
return false; |
| 488 |
} |
| 489 |
// otherwise are separate, will try the next poly segment |
| 490 |
// else if crossing lies within poly segment |
| 491 |
} else if (t_num >= 0 && t_num <= denom) { |
| 492 |
SkScalar s_num = dp.cross(fClipVectors[fCurrClipIndex]); |
| 493 |
// if umbra point is inside the clip polygon |
| 494 |
if (s_num >= 0 && s_num <= denom) { |
| 495 |
segmentVector *= s_num / denom; |
| 496 |
*clipPoint = umbraPoint + segmentVector; |
| 497 |
return true; |
| 498 |
} |
| 499 |
} |
| 500 |
fCurrClipIndex = (fCurrClipIndex + 1) % fClipPolygon.size(); |
| 501 |
} while (fCurrClipIndex != startClipPoint); |
| 502 |
|
| 503 |
return false; |
| 504 |
} |
| 505 |
|
| 506 |
bool SkBaseShadowTessellator::addInnerPoint(const SkPoint& pathPoint, SkColor umbraColor, |
| 507 |
const SkTDArray<SkPoint>& umbraPolygon, |
| 508 |
int* currUmbraIndex) { |
| 509 |
SkPoint umbraPoint; |
| 510 |
if (!fValidUmbra) { |
| 511 |
SkVector v = fCentroid - pathPoint; |
| 512 |
v *= 0.95f; |
| 513 |
umbraPoint = pathPoint + v; |
| 514 |
} else { |
| 515 |
umbraPoint = umbraPolygon[this->getClosestUmbraIndex(pathPoint, umbraPolygon)]; |
| 516 |
} |
| 517 |
|
| 518 |
fPrevPoint = pathPoint; |
| 519 |
|
| 520 |
// merge "close" points |
| 521 |
if (fPrevUmbraIndex == -1 || |
| 522 |
!duplicate_pt(umbraPoint, fPositions[fPrevUmbraIndex])) { |
| 523 |
// if we've wrapped around, don't add a new point |
| 524 |
if (fPrevUmbraIndex >= 0 && duplicate_pt(umbraPoint, fPositions[fFirstVertexIndex])) { |
| 525 |
*currUmbraIndex = fFirstVertexIndex; |
| 526 |
} else { |
| 527 |
*currUmbraIndex = fPositions.size(); |
| 528 |
fPositions.push_back(umbraPoint); |
| 529 |
fColors.push_back(umbraColor); |
| 530 |
} |
| 531 |
return false; |
| 532 |
} else { |
| 533 |
*currUmbraIndex = fPrevUmbraIndex; |
| 534 |
return true; |
| 535 |
} |
| 536 |
} |
| 537 |
|
| 538 |
int SkBaseShadowTessellator::getClosestUmbraIndex(const SkPoint& p, |
| 539 |
const SkTDArray<SkPoint>& umbraPolygon) { |
| 540 |
SkScalar minDistance = SkPointPriv::DistanceToSqd(p, umbraPolygon[fCurrUmbraIndex]); |
| 541 |
int index = fCurrUmbraIndex; |
| 542 |
int dir = 1; |
| 543 |
int next = (index + dir) % umbraPolygon.size(); |
| 544 |
|
| 545 |
// init travel direction |
| 546 |
SkScalar distance = SkPointPriv::DistanceToSqd(p, umbraPolygon[next]); |
| 547 |
if (distance < minDistance) { |
| 548 |
index = next; |
| 549 |
minDistance = distance; |
| 550 |
} else { |
| 551 |
dir = umbraPolygon.size() - 1; |
| 552 |
} |
| 553 |
|
| 554 |
// iterate until we find a point that increases the distance |
| 555 |
next = (index + dir) % umbraPolygon.size(); |
| 556 |
distance = SkPointPriv::DistanceToSqd(p, umbraPolygon[next]); |
| 557 |
while (distance < minDistance) { |
| 558 |
index = next; |
| 559 |
minDistance = distance; |
| 560 |
next = (index + dir) % umbraPolygon.size(); |
| 561 |
distance = SkPointPriv::DistanceToSqd(p, umbraPolygon[next]); |
| 562 |
} |
| 563 |
|
| 564 |
fCurrUmbraIndex = index; |
| 565 |
return index; |
| 566 |
} |
| 567 |
|
| 568 |
bool SkBaseShadowTessellator::computeConcaveShadow(SkScalar inset, SkScalar outset) { |
| 569 |
if (!SkIsSimplePolygon(&fPathPolygon[0], fPathPolygon.size())) { |
| 570 |
return false; |
| 571 |
} |
| 572 |
|
| 573 |
// shouldn't inset more than the half bounds of the polygon |
| 574 |
inset = std::min(inset, std::min(SkTAbs(SkRectPriv::HalfWidth(fPathBounds)), |
| 575 |
SkTAbs(SkRectPriv::HalfHeight(fPathBounds)))); |
| 576 |
// generate inner ring |
| 577 |
SkTDArray<SkPoint> umbraPolygon; |
| 578 |
SkTDArray<int> umbraIndices; |
| 579 |
umbraIndices.reserve(fPathPolygon.size()); |
| 580 |
if (!SkOffsetSimplePolygon(&fPathPolygon[0], fPathPolygon.size(), fPathBounds, inset, |
| 581 |
&umbraPolygon, &umbraIndices)) { |
| 582 |
// TODO: figure out how to handle this case |
| 583 |
return false; |
| 584 |
} |
| 585 |
|
| 586 |
// generate outer ring |
| 587 |
SkTDArray<SkPoint> penumbraPolygon; |
| 588 |
SkTDArray<int> penumbraIndices; |
| 589 |
penumbraPolygon.reserve(umbraPolygon.size()); |
| 590 |
penumbraIndices.reserve(umbraPolygon.size()); |
| 591 |
if (!SkOffsetSimplePolygon(&fPathPolygon[0], fPathPolygon.size(), fPathBounds, -outset, |
| 592 |
&penumbraPolygon, &penumbraIndices)) { |
| 593 |
// TODO: figure out how to handle this case |
| 594 |
return false; |
| 595 |
} |
| 596 |
|
| 597 |
if (umbraPolygon.empty() || penumbraPolygon.empty()) { |
| 598 |
return false; |
| 599 |
} |
| 600 |
|
| 601 |
// attach the rings together |
| 602 |
this->stitchConcaveRings(umbraPolygon, &umbraIndices, penumbraPolygon, &penumbraIndices); |
| 603 |
|
| 604 |
return true; |
| 605 |
} |
| 606 |
|
| 607 |
void SkBaseShadowTessellator::stitchConcaveRings(const SkTDArray<SkPoint>& umbraPolygon, |
| 608 |
SkTDArray<int>* umbraIndices, |
| 609 |
const SkTDArray<SkPoint>& penumbraPolygon, |
| 610 |
SkTDArray<int>* penumbraIndices) { |
| 611 |
// TODO: only create and fill indexMap when fTransparent is true? |
| 612 |
AutoSTMalloc<64, uint16_t> indexMap(umbraPolygon.size()); |
| 613 |
|
| 614 |
// find minimum indices |
| 615 |
int minIndex = 0; |
| 616 |
int min = (*penumbraIndices)[0]; |
| 617 |
for (int i = 1; i < (*penumbraIndices).size(); ++i) { |
| 618 |
if ((*penumbraIndices)[i] < min) { |
| 619 |
min = (*penumbraIndices)[i]; |
| 620 |
minIndex = i; |
| 621 |
} |
| 622 |
} |
| 623 |
int currPenumbra = minIndex; |
| 624 |
|
| 625 |
minIndex = 0; |
| 626 |
min = (*umbraIndices)[0]; |
| 627 |
for (int i = 1; i < (*umbraIndices).size(); ++i) { |
| 628 |
if ((*umbraIndices)[i] < min) { |
| 629 |
min = (*umbraIndices)[i]; |
| 630 |
minIndex = i; |
| 631 |
} |
| 632 |
} |
| 633 |
int currUmbra = minIndex; |
| 634 |
|
| 635 |
// now find a case where the indices are equal (there should be at least one) |
| 636 |
int maxPenumbraIndex = fPathPolygon.size() - 1; |
| 637 |
int maxUmbraIndex = fPathPolygon.size() - 1; |
| 638 |
while ((*penumbraIndices)[currPenumbra] != (*umbraIndices)[currUmbra]) { |
| 639 |
if ((*penumbraIndices)[currPenumbra] < (*umbraIndices)[currUmbra]) { |
| 640 |
(*penumbraIndices)[currPenumbra] += fPathPolygon.size(); |
| 641 |
maxPenumbraIndex = (*penumbraIndices)[currPenumbra]; |
| 642 |
currPenumbra = (currPenumbra + 1) % penumbraPolygon.size(); |
| 643 |
} else { |
| 644 |
(*umbraIndices)[currUmbra] += fPathPolygon.size(); |
| 645 |
maxUmbraIndex = (*umbraIndices)[currUmbra]; |
| 646 |
currUmbra = (currUmbra + 1) % umbraPolygon.size(); |
| 647 |
} |
| 648 |
} |
| 649 |
|
| 650 |
fPositions.push_back(penumbraPolygon[currPenumbra]); |
| 651 |
fColors.push_back(kPenumbraColor); |
| 652 |
int prevPenumbraIndex = 0; |
| 653 |
fPositions.push_back(umbraPolygon[currUmbra]); |
| 654 |
fColors.push_back(kUmbraColor); |
| 655 |
fPrevUmbraIndex = 1; |
| 656 |
indexMap[currUmbra] = 1; |
| 657 |
|
| 658 |
int nextPenumbra = (currPenumbra + 1) % penumbraPolygon.size(); |
| 659 |
int nextUmbra = (currUmbra + 1) % umbraPolygon.size(); |
| 660 |
while ((*penumbraIndices)[nextPenumbra] <= maxPenumbraIndex || |
| 661 |
(*umbraIndices)[nextUmbra] <= maxUmbraIndex) { |
| 662 |
|
| 663 |
if ((*umbraIndices)[nextUmbra] == (*penumbraIndices)[nextPenumbra]) { |
| 664 |
// advance both one step |
| 665 |
fPositions.push_back(penumbraPolygon[nextPenumbra]); |
| 666 |
fColors.push_back(kPenumbraColor); |
| 667 |
int currPenumbraIndex = fPositions.size() - 1; |
| 668 |
|
| 669 |
fPositions.push_back(umbraPolygon[nextUmbra]); |
| 670 |
fColors.push_back(kUmbraColor); |
| 671 |
int currUmbraIndex = fPositions.size() - 1; |
| 672 |
indexMap[nextUmbra] = currUmbraIndex; |
| 673 |
|
| 674 |
this->appendQuad(prevPenumbraIndex, currPenumbraIndex, |
| 675 |
fPrevUmbraIndex, currUmbraIndex); |
| 676 |
|
| 677 |
prevPenumbraIndex = currPenumbraIndex; |
| 678 |
(*penumbraIndices)[currPenumbra] += fPathPolygon.size(); |
| 679 |
currPenumbra = nextPenumbra; |
| 680 |
nextPenumbra = (currPenumbra + 1) % penumbraPolygon.size(); |
| 681 |
|
| 682 |
fPrevUmbraIndex = currUmbraIndex; |
| 683 |
(*umbraIndices)[currUmbra] += fPathPolygon.size(); |
| 684 |
currUmbra = nextUmbra; |
| 685 |
nextUmbra = (currUmbra + 1) % umbraPolygon.size(); |
| 686 |
} |
| 687 |
|
| 688 |
while ((*penumbraIndices)[nextPenumbra] < (*umbraIndices)[nextUmbra] && |
| 689 |
(*penumbraIndices)[nextPenumbra] <= maxPenumbraIndex) { |
| 690 |
// fill out penumbra arc |
| 691 |
fPositions.push_back(penumbraPolygon[nextPenumbra]); |
| 692 |
fColors.push_back(kPenumbraColor); |
| 693 |
int currPenumbraIndex = fPositions.size() - 1; |
| 694 |
|
| 695 |
this->appendTriangle(prevPenumbraIndex, currPenumbraIndex, fPrevUmbraIndex); |
| 696 |
|
| 697 |
prevPenumbraIndex = currPenumbraIndex; |
| 698 |
// this ensures the ordering when we wrap around |
| 699 |
(*penumbraIndices)[currPenumbra] += fPathPolygon.size(); |
| 700 |
currPenumbra = nextPenumbra; |
| 701 |
nextPenumbra = (currPenumbra + 1) % penumbraPolygon.size(); |
| 702 |
} |
| 703 |
|
| 704 |
while ((*umbraIndices)[nextUmbra] < (*penumbraIndices)[nextPenumbra] && |
| 705 |
(*umbraIndices)[nextUmbra] <= maxUmbraIndex) { |
| 706 |
// fill out umbra arc |
| 707 |
fPositions.push_back(umbraPolygon[nextUmbra]); |
| 708 |
fColors.push_back(kUmbraColor); |
| 709 |
int currUmbraIndex = fPositions.size() - 1; |
| 710 |
indexMap[nextUmbra] = currUmbraIndex; |
| 711 |
|
| 712 |
this->appendTriangle(fPrevUmbraIndex, prevPenumbraIndex, currUmbraIndex); |
| 713 |
|
| 714 |
fPrevUmbraIndex = currUmbraIndex; |
| 715 |
// this ensures the ordering when we wrap around |
| 716 |
(*umbraIndices)[currUmbra] += fPathPolygon.size(); |
| 717 |
currUmbra = nextUmbra; |
| 718 |
nextUmbra = (currUmbra + 1) % umbraPolygon.size(); |
| 719 |
} |
| 720 |
} |
| 721 |
// finish up by advancing both one step |
| 722 |
fPositions.push_back(penumbraPolygon[nextPenumbra]); |
| 723 |
fColors.push_back(kPenumbraColor); |
| 724 |
int currPenumbraIndex = fPositions.size() - 1; |
| 725 |
|
| 726 |
fPositions.push_back(umbraPolygon[nextUmbra]); |
| 727 |
fColors.push_back(kUmbraColor); |
| 728 |
int currUmbraIndex = fPositions.size() - 1; |
| 729 |
indexMap[nextUmbra] = currUmbraIndex; |
| 730 |
|
| 731 |
this->appendQuad(prevPenumbraIndex, currPenumbraIndex, |
| 732 |
fPrevUmbraIndex, currUmbraIndex); |
| 733 |
|
| 734 |
if (fTransparent) { |
| 735 |
SkTriangulateSimplePolygon(umbraPolygon.begin(), indexMap, umbraPolygon.size(), |
| 736 |
&fIndices); |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
|
| 741 |
// tesselation tolerance values, in device space pixels |
| 742 |
#if defined(SK_GANESH) |
| 743 |
static constexpr SkScalar kQuadTolerance = 0.2f; |
| 744 |
static constexpr SkScalar kCubicTolerance = 0.2f; |
| 745 |
static constexpr SkScalar kQuadToleranceSqd = kQuadTolerance * kQuadTolerance; |
| 746 |
static constexpr SkScalar kCubicToleranceSqd = kCubicTolerance * kCubicTolerance; |
| 747 |
#endif |
| 748 |
static constexpr SkScalar kConicTolerance = 0.25f; |
| 749 |
|
| 750 |
// clamps the point to the nearest 16th of a pixel |
| 751 |
static void sanitize_point(const SkPoint& in, SkPoint* out) { |
| 752 |
out->fX = SkScalarRoundToScalar(16.f*in.fX)*0.0625f; |
| 753 |
out->fY = SkScalarRoundToScalar(16.f*in.fY)*0.0625f; |
| 754 |
} |
| 755 |
|
| 756 |
void SkBaseShadowTessellator::handleLine(const SkPoint& p) { |
| 757 |
SkPoint pSanitized; |
| 758 |
sanitize_point(p, &pSanitized); |
| 759 |
|
| 760 |
if (!fPathPolygon.empty()) { |
| 761 |
if (!this->accumulateCentroid(fPathPolygon[fPathPolygon.size() - 1], pSanitized)) { |
| 762 |
// skip coincident point |
| 763 |
return; |
| 764 |
} |
| 765 |
} |
| 766 |
|
| 767 |
if (fPathPolygon.size() > 1) { |
| 768 |
if (!checkConvexity(fPathPolygon[fPathPolygon.size() - 2], |
| 769 |
fPathPolygon[fPathPolygon.size() - 1], |
| 770 |
pSanitized)) { |
| 771 |
// remove collinear point |
| 772 |
fPathPolygon.pop_back(); |
| 773 |
// it's possible that the previous point is coincident with the new one now |
| 774 |
if (duplicate_pt(fPathPolygon[fPathPolygon.size() - 1], pSanitized)) { |
| 775 |
fPathPolygon.pop_back(); |
| 776 |
} |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
fPathPolygon.push_back(pSanitized); |
| 781 |
} |
| 782 |
|
| 783 |
void SkBaseShadowTessellator::handleLine(const SkMatrix& m, SkPoint* p) { |
| 784 |
*p = m.mapPoint(*p); |
| 785 |
|
| 786 |
this->handleLine(*p); |
| 787 |
} |
| 788 |
|
| 789 |
void SkBaseShadowTessellator::handleQuad(const SkPoint pts[3]) { |
| 790 |
#if defined(SK_GANESH) |
| 791 |
// check for degeneracy |
| 792 |
SkVector v0 = pts[1] - pts[0]; |
| 793 |
SkVector v1 = pts[2] - pts[0]; |
| 794 |
if (SkScalarNearlyZero(v0.cross(v1))) { |
| 795 |
return; |
| 796 |
} |
| 797 |
// TODO: Pull PathUtils out of Ganesh? |
| 798 |
int maxCount = GrPathUtils::quadraticPointCount(pts, kQuadTolerance); |
| 799 |
fPointBuffer.resize(maxCount); |
| 800 |
SkPoint* target = fPointBuffer.begin(); |
| 801 |
int count = GrPathUtils::generateQuadraticPoints(pts[0], pts[1], pts[2], |
| 802 |
kQuadToleranceSqd, &target, maxCount); |
| 803 |
fPointBuffer.resize(count); |
| 804 |
for (int i = 0; i < count; i++) { |
| 805 |
this->handleLine(fPointBuffer[i]); |
| 806 |
} |
| 807 |
#else |
| 808 |
// for now, just to draw something |
| 809 |
this->handleLine(pts[1]); |
| 810 |
this->handleLine(pts[2]); |
| 811 |
#endif |
| 812 |
} |
| 813 |
|
| 814 |
void SkBaseShadowTessellator::handleQuad(const SkMatrix& m, SkPoint pts[3]) { |
| 815 |
m.mapPoints({pts, 3}); |
| 816 |
this->handleQuad(pts); |
| 817 |
} |
| 818 |
|
| 819 |
void SkBaseShadowTessellator::handleCubic(const SkMatrix& m, SkPoint pts[4]) { |
| 820 |
m.mapPoints({pts, 4}); |
| 821 |
#if defined(SK_GANESH) |
| 822 |
// TODO: Pull PathUtils out of Ganesh? |
| 823 |
int maxCount = GrPathUtils::cubicPointCount(pts, kCubicTolerance); |
| 824 |
fPointBuffer.resize(maxCount); |
| 825 |
SkPoint* target = fPointBuffer.begin(); |
| 826 |
int count = GrPathUtils::generateCubicPoints(pts[0], pts[1], pts[2], pts[3], |
| 827 |
kCubicToleranceSqd, &target, maxCount); |
| 828 |
fPointBuffer.resize(count); |
| 829 |
for (int i = 0; i < count; i++) { |
| 830 |
this->handleLine(fPointBuffer[i]); |
| 831 |
} |
| 832 |
#else |
| 833 |
// for now, just to draw something |
| 834 |
this->handleLine(pts[1]); |
| 835 |
this->handleLine(pts[2]); |
| 836 |
this->handleLine(pts[3]); |
| 837 |
#endif |
| 838 |
} |
| 839 |
|
| 840 |
void SkBaseShadowTessellator::handleConic(const SkMatrix& m, SkPoint pts[3], SkScalar w) { |
| 841 |
if (m.hasPerspective()) { |
| 842 |
w = SkConic::TransformW(pts, w, m); |
| 843 |
} |
| 844 |
m.mapPoints({pts, 3}); |
| 845 |
SkAutoConicToQuads quadder; |
| 846 |
const SkPoint* quads = quadder.computeQuads(pts, w, kConicTolerance); |
| 847 |
SkPoint lastPoint = *(quads++); |
| 848 |
int count = quadder.countQuads(); |
| 849 |
for (int i = 0; i < count; ++i) { |
| 850 |
SkPoint quadPts[3]; |
| 851 |
quadPts[0] = lastPoint; |
| 852 |
quadPts[1] = quads[0]; |
| 853 |
quadPts[2] = i == count - 1 ? pts[2] : quads[1]; |
| 854 |
this->handleQuad(quadPts); |
| 855 |
lastPoint = quadPts[2]; |
| 856 |
quads += 2; |
| 857 |
} |
| 858 |
} |
| 859 |
|
| 860 |
bool SkBaseShadowTessellator::addArc(const SkVector& nextNormal, SkScalar offset, bool finishArc) { |
| 861 |
// fill in fan from previous quad |
| 862 |
SkScalar rotSin, rotCos; |
| 863 |
int numSteps; |
| 864 |
if (!SkComputeRadialSteps(fPrevOutset, nextNormal, offset, &rotSin, &rotCos, &numSteps)) { |
| 865 |
// recover as best we can |
| 866 |
numSteps = 0; |
| 867 |
} |
| 868 |
SkVector prevNormal = fPrevOutset; |
| 869 |
for (int i = 0; i < numSteps-1; ++i) { |
| 870 |
SkVector currNormal; |
| 871 |
currNormal.fX = prevNormal.fX*rotCos - prevNormal.fY*rotSin; |
| 872 |
currNormal.fY = prevNormal.fY*rotCos + prevNormal.fX*rotSin; |
| 873 |
fPositions.push_back(fPrevPoint + currNormal); |
| 874 |
fColors.push_back(kPenumbraColor); |
| 875 |
this->appendTriangle(fPrevUmbraIndex, fPositions.size() - 1, fPositions.size() - 2); |
| 876 |
|
| 877 |
prevNormal = currNormal; |
| 878 |
} |
| 879 |
if (finishArc && numSteps) { |
| 880 |
fPositions.push_back(fPrevPoint + nextNormal); |
| 881 |
fColors.push_back(kPenumbraColor); |
| 882 |
this->appendTriangle(fPrevUmbraIndex, fPositions.size() - 1, fPositions.size() - 2); |
| 883 |
} |
| 884 |
fPrevOutset = nextNormal; |
| 885 |
|
| 886 |
return (numSteps > 0); |
| 887 |
} |
| 888 |
|
| 889 |
void SkBaseShadowTessellator::appendTriangle(uint16_t index0, uint16_t index1, uint16_t index2) { |
| 890 |
auto indices = fIndices.append(3); |
| 891 |
|
| 892 |
indices[0] = index0; |
| 893 |
indices[1] = index1; |
| 894 |
indices[2] = index2; |
| 895 |
} |
| 896 |
|
| 897 |
void SkBaseShadowTessellator::appendQuad(uint16_t index0, uint16_t index1, |
| 898 |
uint16_t index2, uint16_t index3) { |
| 899 |
auto indices = fIndices.append(6); |
| 900 |
|
| 901 |
indices[0] = index0; |
| 902 |
indices[1] = index1; |
| 903 |
indices[2] = index2; |
| 904 |
|
| 905 |
indices[3] = index2; |
| 906 |
indices[4] = index1; |
| 907 |
indices[5] = index3; |
| 908 |
} |
| 909 |
|
| 910 |
////////////////////////////////////////////////////////////////////////////////////////////////// |
| 911 |
|
| 912 |
class SkAmbientShadowTessellator : public SkBaseShadowTessellator { |
| 913 |
public: |
| 914 |
SkAmbientShadowTessellator(const SkPath& path, const SkMatrix& ctm, |
| 915 |
const SkPoint3& zPlaneParams, bool transparent); |
| 916 |
|
| 917 |
private: |
| 918 |
bool computePathPolygon(const SkPath& path, const SkMatrix& ctm); |
| 919 |
|
| 920 |
using INHERITED = SkBaseShadowTessellator; |
| 921 |
}; |
| 922 |
|
| 923 |
SkAmbientShadowTessellator::SkAmbientShadowTessellator(const SkPath& path, |
| 924 |
const SkMatrix& ctm, |
| 925 |
const SkPoint3& zPlaneParams, |
| 926 |
bool transparent) |
| 927 |
: INHERITED(zPlaneParams, path.getBounds(), transparent) { |
| 928 |
// Set base colors |
| 929 |
auto baseZ = heightFunc(fPathBounds.centerX(), fPathBounds.centerY()); |
| 930 |
// umbraColor is the interior value, penumbraColor the exterior value. |
| 931 |
auto outset = SkDrawShadowMetrics::AmbientBlurRadius(baseZ); |
| 932 |
auto inset = outset * SkDrawShadowMetrics::AmbientRecipAlpha(baseZ) - outset; |
| 933 |
|
| 934 |
if (!this->computePathPolygon(path, ctm)) { |
| 935 |
return; |
| 936 |
} |
| 937 |
if (fPathPolygon.size() < 3 || !SkIsFinite(fArea)) { |
| 938 |
fSucceeded = true; // We don't want to try to blur these cases, so we will |
| 939 |
// return an empty SkVertices instead. |
| 940 |
return; |
| 941 |
} |
| 942 |
|
| 943 |
// Outer ring: 3*numPts |
| 944 |
// Middle ring: numPts |
| 945 |
fPositions.reserve(4 * path.countPoints()); |
| 946 |
fColors.reserve(4 * path.countPoints()); |
| 947 |
// Outer ring: 12*numPts |
| 948 |
// Middle ring: 0 |
| 949 |
fIndices.reserve(12 * path.countPoints()); |
| 950 |
|
| 951 |
if (fIsConvex) { |
| 952 |
fSucceeded = this->computeConvexShadow(inset, outset, false); |
| 953 |
} else { |
| 954 |
fSucceeded = this->computeConcaveShadow(inset, outset); |
| 955 |
} |
| 956 |
} |
| 957 |
|
| 958 |
static inline void spancpy(SkSpan<SkPoint> dst, SkSpan<const SkPoint> src) { |
| 959 |
SkASSERT(dst.size() >= src.size()); |
| 960 |
for (size_t i = 0; i < src.size(); ++i) { |
| 961 |
dst[i] = src[i]; |
| 962 |
} |
| 963 |
} |
| 964 |
|
| 965 |
bool SkAmbientShadowTessellator::computePathPolygon(const SkPath& path, const SkMatrix& ctm) { |
| 966 |
fPathPolygon.reserve(path.countPoints()); |
| 967 |
|
| 968 |
// walk around the path, tessellate and generate outer ring |
| 969 |
// if original path is transparent, will accumulate sum of points for centroid |
| 970 |
SkPath::Iter iter(path, true); |
| 971 |
bool verbSeen = false; |
| 972 |
bool closeSeen = false; |
| 973 |
while (auto rec = iter.next()) { |
| 974 |
if (closeSeen) { |
| 975 |
return false; |
| 976 |
} |
| 977 |
SkPoint pts[4]; // max needed |
| 978 |
spancpy(pts, rec->fPoints); // need a writable copy |
| 979 |
switch (rec->fVerb) { |
| 980 |
case SkPathVerb::kLine: |
| 981 |
this->handleLine(ctm, &pts[1]); |
| 982 |
break; |
| 983 |
case SkPathVerb::kQuad: |
| 984 |
this->handleQuad(ctm, pts); |
| 985 |
break; |
| 986 |
case SkPathVerb::kCubic: |
| 987 |
this->handleCubic(ctm, pts); |
| 988 |
break; |
| 989 |
case SkPathVerb::kConic: |
| 990 |
this->handleConic(ctm, pts, rec->conicWeight()); |
| 991 |
break; |
| 992 |
case SkPathVerb::kMove: |
| 993 |
if (verbSeen) { |
| 994 |
return false; |
| 995 |
} |
| 996 |
break; |
| 997 |
case SkPathVerb::kClose: |
| 998 |
closeSeen = true; |
| 999 |
break; |
| 1000 |
} |
| 1001 |
verbSeen = true; |
| 1002 |
} |
| 1003 |
|
| 1004 |
this->finishPathPolygon(); |
| 1005 |
return true; |
| 1006 |
} |
| 1007 |
|
| 1008 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1009 |
|
| 1010 |
class SkSpotShadowTessellator : public SkBaseShadowTessellator { |
| 1011 |
public: |
| 1012 |
SkSpotShadowTessellator(const SkPath& path, const SkMatrix& ctm, |
| 1013 |
const SkPoint3& zPlaneParams, const SkPoint3& lightPos, |
| 1014 |
SkScalar lightRadius, bool transparent, bool directional); |
| 1015 |
|
| 1016 |
private: |
| 1017 |
bool computeClipAndPathPolygons(const SkPath& path, const SkMatrix& ctm, |
| 1018 |
const SkMatrix& shadowTransform); |
| 1019 |
void addToClip(const SkVector& nextPoint); |
| 1020 |
|
| 1021 |
using INHERITED = SkBaseShadowTessellator; |
| 1022 |
}; |
| 1023 |
|
| 1024 |
SkSpotShadowTessellator::SkSpotShadowTessellator(const SkPath& path, const SkMatrix& ctm, |
| 1025 |
const SkPoint3& zPlaneParams, |
| 1026 |
const SkPoint3& lightPos, SkScalar lightRadius, |
| 1027 |
bool transparent, bool directional) |
| 1028 |
: INHERITED(zPlaneParams, path.getBounds(), transparent) { |
| 1029 |
|
| 1030 |
// Compute the blur radius, scale and translation for the spot shadow. |
| 1031 |
SkMatrix shadowTransform; |
| 1032 |
SkScalar outset; |
| 1033 |
if (!SkDrawShadowMetrics::GetSpotShadowTransform(lightPos, lightRadius, ctm, zPlaneParams, |
| 1034 |
path.getBounds(), directional, |
| 1035 |
&shadowTransform, &outset)) { |
| 1036 |
return; |
| 1037 |
} |
| 1038 |
SkScalar inset = outset; |
| 1039 |
|
| 1040 |
// compute rough clip bounds for umbra, plus offset polygon, plus centroid |
| 1041 |
if (!this->computeClipAndPathPolygons(path, ctm, shadowTransform)) { |
| 1042 |
return; |
| 1043 |
} |
| 1044 |
if (fClipPolygon.size() < 3 || fPathPolygon.size() < 3 || !SkIsFinite(fArea)) { |
| 1045 |
fSucceeded = true; // We don't want to try to blur these cases, so we will |
| 1046 |
// return an empty SkVertices instead. |
| 1047 |
return; |
| 1048 |
} |
| 1049 |
|
| 1050 |
// TODO: calculate these reserves better |
| 1051 |
// Penumbra ring: 3*numPts |
| 1052 |
// Umbra ring: numPts |
| 1053 |
// Inner ring: numPts |
| 1054 |
fPositions.reserve(5 * path.countPoints()); |
| 1055 |
fColors.reserve(5 * path.countPoints()); |
| 1056 |
// Penumbra ring: 12*numPts |
| 1057 |
// Umbra ring: 3*numPts |
| 1058 |
fIndices.reserve(15 * path.countPoints()); |
| 1059 |
|
| 1060 |
if (fIsConvex) { |
| 1061 |
fSucceeded = this->computeConvexShadow(inset, outset, true); |
| 1062 |
} else { |
| 1063 |
fSucceeded = this->computeConcaveShadow(inset, outset); |
| 1064 |
} |
| 1065 |
|
| 1066 |
if (!fSucceeded) { |
| 1067 |
return; |
| 1068 |
} |
| 1069 |
|
| 1070 |
fSucceeded = true; |
| 1071 |
} |
| 1072 |
|
| 1073 |
bool SkSpotShadowTessellator::computeClipAndPathPolygons(const SkPath& path, const SkMatrix& ctm, |
| 1074 |
const SkMatrix& shadowTransform) { |
| 1075 |
|
| 1076 |
fPathPolygon.reserve(path.countPoints()); |
| 1077 |
fClipPolygon.reserve(path.countPoints()); |
| 1078 |
|
| 1079 |
// Walk around the path and compute clip polygon and path polygon. |
| 1080 |
// Will also accumulate sum of areas for centroid. |
| 1081 |
// For Bezier curves, we compute additional interior points on curve. |
| 1082 |
SkPath::Iter iter(path, true); |
| 1083 |
SkPoint clipPts[4]; |
| 1084 |
|
| 1085 |
// coefficients to compute cubic Bezier at t = 5/16 |
| 1086 |
static constexpr SkScalar kA = 0.32495117187f; |
| 1087 |
static constexpr SkScalar kB = 0.44311523437f; |
| 1088 |
static constexpr SkScalar kC = 0.20141601562f; |
| 1089 |
static constexpr SkScalar kD = 0.03051757812f; |
| 1090 |
|
| 1091 |
SkPoint curvePoint; |
| 1092 |
bool closeSeen = false; |
| 1093 |
bool verbSeen = false; |
| 1094 |
while (auto rec = iter.next()) { |
| 1095 |
if (closeSeen) { |
| 1096 |
return false; |
| 1097 |
} |
| 1098 |
SkPoint pts[4]; // max needed |
| 1099 |
spancpy(pts, rec->fPoints); // need a writable copy |
| 1100 |
switch (rec->fVerb) { |
| 1101 |
case SkPathVerb::kLine: |
| 1102 |
clipPts[0] = ctm.mapPoint(pts[1]); |
| 1103 |
this->addToClip(clipPts[0]); |
| 1104 |
this->handleLine(shadowTransform, &pts[1]); |
| 1105 |
break; |
| 1106 |
case SkPathVerb::kQuad: |
| 1107 |
ctm.mapPoints({clipPts, 3}, {pts, 3}); |
| 1108 |
// point at t = 1/2 |
| 1109 |
curvePoint.fX = 0.25f*clipPts[0].fX + 0.5f*clipPts[1].fX + 0.25f*clipPts[2].fX; |
| 1110 |
curvePoint.fY = 0.25f*clipPts[0].fY + 0.5f*clipPts[1].fY + 0.25f*clipPts[2].fY; |
| 1111 |
this->addToClip(curvePoint); |
| 1112 |
this->addToClip(clipPts[2]); |
| 1113 |
this->handleQuad(shadowTransform, pts); |
| 1114 |
break; |
| 1115 |
case SkPathVerb::kConic: { |
| 1116 |
ctm.mapPoints({clipPts, 3}, {pts, 3}); |
| 1117 |
const float w = rec->conicWeight(); |
| 1118 |
// point at t = 1/2 |
| 1119 |
curvePoint.fX = 0.25f*clipPts[0].fX + w*0.5f*clipPts[1].fX + 0.25f*clipPts[2].fX; |
| 1120 |
curvePoint.fY = 0.25f*clipPts[0].fY + w*0.5f*clipPts[1].fY + 0.25f*clipPts[2].fY; |
| 1121 |
curvePoint *= SkScalarInvert(0.5f + 0.5f*w); |
| 1122 |
this->addToClip(curvePoint); |
| 1123 |
this->addToClip(clipPts[2]); |
| 1124 |
this->handleConic(shadowTransform, pts, w); |
| 1125 |
} break; |
| 1126 |
case SkPathVerb::kCubic: |
| 1127 |
ctm.mapPoints({clipPts, 4}, {pts, 4}); |
| 1128 |
// point at t = 5/16 |
| 1129 |
curvePoint.fX = kA*clipPts[0].fX + kB*clipPts[1].fX |
| 1130 |
+ kC*clipPts[2].fX + kD*clipPts[3].fX; |
| 1131 |
curvePoint.fY = kA*clipPts[0].fY + kB*clipPts[1].fY |
| 1132 |
+ kC*clipPts[2].fY + kD*clipPts[3].fY; |
| 1133 |
this->addToClip(curvePoint); |
| 1134 |
// point at t = 11/16 |
| 1135 |
curvePoint.fX = kD*clipPts[0].fX + kC*clipPts[1].fX |
| 1136 |
+ kB*clipPts[2].fX + kA*clipPts[3].fX; |
| 1137 |
curvePoint.fY = kD*clipPts[0].fY + kC*clipPts[1].fY |
| 1138 |
+ kB*clipPts[2].fY + kA*clipPts[3].fY; |
| 1139 |
this->addToClip(curvePoint); |
| 1140 |
this->addToClip(clipPts[3]); |
| 1141 |
this->handleCubic(shadowTransform, pts); |
| 1142 |
break; |
| 1143 |
case SkPathVerb::kMove: |
| 1144 |
if (verbSeen) { |
| 1145 |
return false; |
| 1146 |
} |
| 1147 |
break; |
| 1148 |
case SkPathVerb::kClose: |
| 1149 |
closeSeen = true; |
| 1150 |
break; |
| 1151 |
} |
| 1152 |
verbSeen = true; |
| 1153 |
} |
| 1154 |
|
| 1155 |
this->finishPathPolygon(); |
| 1156 |
return true; |
| 1157 |
} |
| 1158 |
|
| 1159 |
void SkSpotShadowTessellator::addToClip(const SkPoint& point) { |
| 1160 |
if (fClipPolygon.empty() || !duplicate_pt(point, fClipPolygon[fClipPolygon.size() - 1])) { |
| 1161 |
fClipPolygon.push_back(point); |
| 1162 |
} |
| 1163 |
} |
| 1164 |
|
| 1165 |
/////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1166 |
|
| 1167 |
sk_sp<SkVertices> SkShadowTessellator::MakeAmbient(const SkPath& path, const SkMatrix& ctm, |
| 1168 |
const SkPoint3& zPlane, bool transparent) { |
| 1169 |
if (!ctm.mapRect(path.getBounds()).isFinite() || !zPlane.isFinite()) { |
| 1170 |
return nullptr; |
| 1171 |
} |
| 1172 |
SkAmbientShadowTessellator ambientTess(path, ctm, zPlane, transparent); |
| 1173 |
return ambientTess.releaseVertices(); |
| 1174 |
} |
| 1175 |
|
| 1176 |
sk_sp<SkVertices> SkShadowTessellator::MakeSpot(const SkPath& path, const SkMatrix& ctm, |
| 1177 |
const SkPoint3& zPlane, const SkPoint3& lightPos, |
| 1178 |
SkScalar lightRadius, bool transparent, |
| 1179 |
bool directional) { |
| 1180 |
if (!ctm.mapRect(path.getBounds()).isFinite() || !zPlane.isFinite() || |
| 1181 |
!lightPos.isFinite() || !(lightPos.fZ >= SK_ScalarNearlyZero) || |
| 1182 |
!SkIsFinite(lightRadius) || !(lightRadius >= SK_ScalarNearlyZero)) { |
| 1183 |
return nullptr; |
| 1184 |
} |
| 1185 |
SkSpotShadowTessellator spotTess(path, ctm, zPlane, lightPos, lightRadius, transparent, |
| 1186 |
directional); |
| 1187 |
return spotTess.releaseVertices(); |
| 1188 |
} |
| 1189 |
|
| 1190 |
#endif // !defined(SK_ENABLE_OPTIMIZE_SIZE) |
| 1191 |
|