OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 ARM Ltd. |
| 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 "GrDistanceFieldGenFromVector.h" |
| 9 #include "SkPoint.h" |
| 10 #include "SkGeometry.h" |
| 11 #include "SkPathOps.h" |
| 12 #include "GrPathUtils.h" |
| 13 #include "GrConfig.h" |
| 14 |
| 15 /** |
| 16 * If a scanline (a row of texel) cross from the kRight_SegSide |
| 17 * of a segment to the kLeft_SegSide, the winding score should |
| 18 * add 1. |
| 19 * And winding score should subtract 1 if the scanline cross |
| 20 * from kLeft_SegSide to kRight_SegSide. |
| 21 * Always return kNA_SegSide if the scanline does not cross over |
| 22 * the segment. Winding score should be zero in this case. |
| 23 * You can get the winding number for each texel of the scanline |
| 24 * by adding the winding score from left to right. |
| 25 * Assuming we always start from outside, so the winding number |
| 26 * should always start from zero. |
| 27 * ________ ________ |
| 28 * | | | | |
| 29 * ...R|L......L|R.....L|R......R|L..... <= Scanline & side of segment |
| 30 * |+1 |-1 |-1 |+1 <= Winding score |
| 31 * 0 | 1 ^ 0 ^ -1 |0 <= Winding number |
| 32 * |________| |________| |
| 33 * |
| 34 * .......NA................NA.......... |
| 35 * 0 0 |
| 36 */ |
| 37 enum SegSide { |
| 38 kLeft_SegSide = -1, |
| 39 kOn_SegSide = 0, |
| 40 kRight_SegSide = 1, |
| 41 kNA_SegSide = 2, |
| 42 }; |
| 43 |
| 44 struct DFData { |
| 45 float fDistSq; // distance squared to nearest (so far) edge |
| 46 int fDeltaWindingScore; // +1 or -1 whenever a scanline cross over a segme
nt |
| 47 }; |
| 48 |
| 49 /////////////////////////////////////////////////////////////////////////////// |
| 50 |
| 51 /* |
| 52 * Type definition for double precision DPoint and DAffineMatrix |
| 53 */ |
| 54 |
| 55 // Point with double precision |
| 56 struct DPoint { |
| 57 double fX, fY; |
| 58 |
| 59 static DPoint Make(double x, double y) { |
| 60 DPoint pt; |
| 61 pt.set(x, y); |
| 62 return pt; |
| 63 } |
| 64 |
| 65 double x() const { return fX; } |
| 66 double y() const { return fY; } |
| 67 |
| 68 void set(double x, double y) { fX = x; fY = y; } |
| 69 |
| 70 /** Returns the euclidian distance from (0,0) to (x,y) |
| 71 */ |
| 72 static double Length(double x, double y) { |
| 73 return sqrt(x * x + y * y); |
| 74 } |
| 75 |
| 76 /** Returns the euclidian distance between a and b |
| 77 */ |
| 78 static double Distance(const DPoint& a, const DPoint& b) { |
| 79 return Length(a.fX - b.fX, a.fY - b.fY); |
| 80 } |
| 81 |
| 82 double distanceToSqd(const DPoint& pt) const { |
| 83 double dx = fX - pt.fX; |
| 84 double dy = fY - pt.fY; |
| 85 return dx * dx + dy * dy; |
| 86 } |
| 87 }; |
| 88 |
| 89 // Matrix with double precision for affine transformation. |
| 90 // We don't store row 3 because its always (0, 0, 1). |
| 91 class DAffineMatrix { |
| 92 public: |
| 93 double operator[](int index) const { |
| 94 SkASSERT((unsigned)index < 6); |
| 95 return fMat[index]; |
| 96 } |
| 97 |
| 98 double& operator[](int index) { |
| 99 SkASSERT((unsigned)index < 6); |
| 100 return fMat[index]; |
| 101 } |
| 102 |
| 103 void setAffine(double m11, double m12, double m13, |
| 104 double m21, double m22, double m23) { |
| 105 fMat[0] = m11; |
| 106 fMat[1] = m12; |
| 107 fMat[2] = m13; |
| 108 fMat[3] = m21; |
| 109 fMat[4] = m22; |
| 110 fMat[5] = m23; |
| 111 } |
| 112 |
| 113 /** Set the matrix to identity |
| 114 */ |
| 115 void reset() { |
| 116 fMat[0] = fMat[4] = 1.0; |
| 117 fMat[1] = fMat[3] = |
| 118 fMat[2] = fMat[5] = 0.0; |
| 119 } |
| 120 |
| 121 // alias for reset() |
| 122 void setIdentity() { this->reset(); } |
| 123 |
| 124 DPoint mapPoint(const SkPoint& src) const { |
| 125 DPoint pt = DPoint::Make(src.x(), src.y()); |
| 126 return this->mapPoint(pt); |
| 127 } |
| 128 |
| 129 DPoint mapPoint(const DPoint& src) const { |
| 130 return DPoint::Make(fMat[0] * src.x() + fMat[1] * src.y() + fMat[2], |
| 131 fMat[3] * src.x() + fMat[4] * src.y() + fMat[5]); |
| 132 } |
| 133 private: |
| 134 double fMat[6]; |
| 135 }; |
| 136 |
| 137 /////////////////////////////////////////////////////////////////////////////// |
| 138 |
| 139 static const double kClose = (SK_Scalar1 / 16.0); |
| 140 static const double kCloseSqd = SkScalarMul(kClose, kClose); |
| 141 static const double kNearlyZero = (SK_Scalar1 / (1 << 15)); |
| 142 |
| 143 static inline bool between_closed_open(double a, double b, double c, |
| 144 double tolerance = kNearlyZero) { |
| 145 SkASSERT(tolerance >= 0.f); |
| 146 return b < c ? (a >= b - tolerance && a < c - tolerance) : |
| 147 (a >= c - tolerance && a < b - tolerance); |
| 148 } |
| 149 |
| 150 static inline bool between_closed(double a, double b, double c, |
| 151 double tolerance = kNearlyZero) { |
| 152 SkASSERT(tolerance >= 0.f); |
| 153 return b < c ? (a >= b - tolerance && a <= c + tolerance) : |
| 154 (a >= c - tolerance && a <= b + tolerance); |
| 155 } |
| 156 |
| 157 static inline bool nearly_zero(double x, double tolerance = kNearlyZero) { |
| 158 SkASSERT(tolerance >= 0.f); |
| 159 return fabs(x) <= tolerance; |
| 160 } |
| 161 |
| 162 static inline bool nearly_equal(double x, double y, double tolerance = kNearlyZe
ro) { |
| 163 SkASSERT(tolerance >= 0.f); |
| 164 return fabs(x - y) <= tolerance; |
| 165 } |
| 166 |
| 167 static inline float sign_of(const float &val) { |
| 168 return (val < 0.f) ? -1.f : 1.f; |
| 169 } |
| 170 |
| 171 static bool is_colinear(const SkPoint pts[3]) { |
| 172 return nearly_zero((pts[1].y() - pts[0].y()) * (pts[1].x() - pts[2].x()) - |
| 173 (pts[1].y() - pts[2].y()) * (pts[1].x() - pts[0].x())); |
| 174 } |
| 175 |
| 176 class PathSegment { |
| 177 public: |
| 178 enum { |
| 179 // These enum values are assumed in member functions below. |
| 180 kLine = 0, |
| 181 kQuad = 1, |
| 182 } fType; |
| 183 |
| 184 // line uses 2 pts, quad uses 3 pts |
| 185 SkPoint fPts[3]; |
| 186 |
| 187 DPoint fP0T, fP2T; |
| 188 DAffineMatrix fXformMatrix; |
| 189 double fScalingFactor; |
| 190 SkRect fBoundingBox; |
| 191 |
| 192 void init(); |
| 193 |
| 194 int countPoints() { |
| 195 GR_STATIC_ASSERT(0 == kLine && 1 == kQuad); |
| 196 return fType + 2; |
| 197 } |
| 198 |
| 199 const SkPoint& endPt() const { |
| 200 GR_STATIC_ASSERT(0 == kLine && 1 == kQuad); |
| 201 return fPts[fType + 1]; |
| 202 }; |
| 203 }; |
| 204 |
| 205 typedef SkTArray<PathSegment, true> PathSegmentArray; |
| 206 |
| 207 void PathSegment::init() { |
| 208 const DPoint p0 = DPoint::Make(fPts[0].x(), fPts[0].y()); |
| 209 const DPoint p2 = DPoint::Make(this->endPt().x(), this->endPt().y()); |
| 210 const double p0x = p0.x(); |
| 211 const double p0y = p0.y(); |
| 212 const double p2x = p2.x(); |
| 213 const double p2y = p2.y(); |
| 214 |
| 215 fBoundingBox.set(fPts[0], this->endPt()); |
| 216 |
| 217 if (fType == PathSegment::kLine) { |
| 218 fScalingFactor = DPoint::Distance(p0, p2); |
| 219 |
| 220 const double cosTheta = (p2x - p0x) / fScalingFactor; |
| 221 const double sinTheta = (p2y - p0y) / fScalingFactor; |
| 222 |
| 223 fXformMatrix.setAffine( |
| 224 cosTheta, sinTheta, -(cosTheta * p0x) - (sinTheta * p0y), |
| 225 -sinTheta, cosTheta, (sinTheta * p0x) - (cosTheta * p0y) |
| 226 ); |
| 227 } else { |
| 228 SkASSERT(fType == PathSegment::kQuad); |
| 229 |
| 230 // Calculate bounding box |
| 231 const SkPoint _P1mP0 = fPts[1] - fPts[0]; |
| 232 SkPoint t = _P1mP0 - fPts[2] + fPts[1]; |
| 233 t.fX = _P1mP0.x() / t.x(); |
| 234 t.fY = _P1mP0.y() / t.y(); |
| 235 t.fX = SkScalarClampMax(t.x(), 1.0); |
| 236 t.fY = SkScalarClampMax(t.y(), 1.0); |
| 237 t.fX = _P1mP0.x() * t.x(); |
| 238 t.fY = _P1mP0.y() * t.y(); |
| 239 const SkPoint m = fPts[0] + t; |
| 240 fBoundingBox.growToInclude(&m, 1); |
| 241 |
| 242 const double p1x = fPts[1].x(); |
| 243 const double p1y = fPts[1].y(); |
| 244 |
| 245 const double p0xSqd = p0x * p0x; |
| 246 const double p0ySqd = p0y * p0y; |
| 247 const double p2xSqd = p2x * p2x; |
| 248 const double p2ySqd = p2y * p2y; |
| 249 const double p1xSqd = p1x * p1x; |
| 250 const double p1ySqd = p1y * p1y; |
| 251 |
| 252 const double p01xProd = p0x * p1x; |
| 253 const double p02xProd = p0x * p2x; |
| 254 const double b12xProd = p1x * p2x; |
| 255 const double p01yProd = p0y * p1y; |
| 256 const double p02yProd = p0y * p2y; |
| 257 const double b12yProd = p1y * p2y; |
| 258 |
| 259 const double sqrtA = p0y - (2.0 * p1y) + p2y; |
| 260 const double a = sqrtA * sqrtA; |
| 261 const double h = -1.0 * (p0y - (2.0 * p1y) + p2y) * (p0x - (2.0 * p1x) +
p2x); |
| 262 const double sqrtB = p0x - (2.0 * p1x) + p2x; |
| 263 const double b = sqrtB * sqrtB; |
| 264 const double c = (p0xSqd * p2ySqd) - (4.0 * p01xProd * b12yProd) |
| 265 - (2.0 * p02xProd * p02yProd) + (4.0 * p02xProd * p1ySqd) |
| 266 + (4.0 * p1xSqd * p02yProd) - (4.0 * b12xProd * p01yProd) |
| 267 + (p2xSqd * p0ySqd); |
| 268 const double g = (p0x * p02yProd) - (2.0 * p0x * p1ySqd) |
| 269 + (2.0 * p0x * b12yProd) - (p0x * p2ySqd) |
| 270 + (2.0 * p1x * p01yProd) - (4.0 * p1x * p02yProd) |
| 271 + (2.0 * p1x * b12yProd) - (p2x * p0ySqd) |
| 272 + (2.0 * p2x * p01yProd) + (p2x * p02yProd) |
| 273 - (2.0 * p2x * p1ySqd); |
| 274 const double f = -((p0xSqd * p2y) - (2.0 * p01xProd * p1y) |
| 275 - (2.0 * p01xProd * p2y) - (p02xProd * p0y) |
| 276 + (4.0 * p02xProd * p1y) - (p02xProd * p2y) |
| 277 + (2.0 * p1xSqd * p0y) + (2.0 * p1xSqd * p2y) |
| 278 - (2.0 * b12xProd * p0y) - (2.0 * b12xProd * p1y) |
| 279 + (p2xSqd * p0y)); |
| 280 |
| 281 const double cosTheta = sqrt(a / (a + b)); |
| 282 const double sinTheta = -1.0 * sign_of((a + b) * h) * sqrt(b / (a + b)); |
| 283 |
| 284 const double gDef = cosTheta * g - sinTheta * f; |
| 285 const double fDef = sinTheta * g + cosTheta * f; |
| 286 |
| 287 |
| 288 const double x0 = gDef / (a + b); |
| 289 const double y0 = (1.0 / (2.0 * fDef)) * (c - (gDef * gDef / (a + b))); |
| 290 |
| 291 |
| 292 const double lambda = -1.0 * ((a + b) / (2.0 * fDef)); |
| 293 fScalingFactor = (1.0 / lambda); |
| 294 fScalingFactor *= fScalingFactor; |
| 295 |
| 296 const double lambda_cosTheta = lambda * cosTheta; |
| 297 const double lambda_sinTheta = lambda * sinTheta; |
| 298 |
| 299 fXformMatrix.setAffine( |
| 300 lambda_cosTheta, -lambda_sinTheta, lambda * x0, |
| 301 lambda_sinTheta, lambda_cosTheta, lambda * y0 |
| 302 ); |
| 303 } |
| 304 |
| 305 fP0T = fXformMatrix.mapPoint(p0); |
| 306 fP2T = fXformMatrix.mapPoint(p2); |
| 307 } |
| 308 |
| 309 static void init_distances(DFData* data, int size) { |
| 310 DFData* currData = data; |
| 311 |
| 312 for (int i = 0; i < size; ++i) { |
| 313 // init distance to "far away" |
| 314 currData->fDistSq = SK_DistanceFieldMagnitude * SK_DistanceFieldMagnitud
e; |
| 315 currData->fDeltaWindingScore = 0; |
| 316 ++currData; |
| 317 } |
| 318 } |
| 319 |
| 320 static inline bool get_direction(const SkPath& path, const SkMatrix& m, |
| 321 SkPathPriv::FirstDirection* dir) { |
| 322 if (!SkPathPriv::CheapComputeFirstDirection(path, dir)) { |
| 323 return false; |
| 324 } |
| 325 |
| 326 // check whether m reverses the orientation |
| 327 SkASSERT(!m.hasPerspective()); |
| 328 SkScalar det2x2 = SkScalarMul(m.get(SkMatrix::kMScaleX), m.get(SkMatrix::kMS
caleY)) - |
| 329 SkScalarMul(m.get(SkMatrix::kMSkewX), m.get(SkMatrix::kMSk
ewY)); |
| 330 |
| 331 if (det2x2 < 0) { |
| 332 *dir = SkPathPriv::OppositeFirstDirection(*dir); |
| 333 } |
| 334 return true; |
| 335 } |
| 336 |
| 337 static inline void add_line_to_segment(const SkPoint pts[2], |
| 338 PathSegmentArray* segments) { |
| 339 segments->push_back(); |
| 340 segments->back().fType = PathSegment::kLine; |
| 341 segments->back().fPts[0] = pts[0]; |
| 342 segments->back().fPts[1] = pts[1]; |
| 343 |
| 344 segments->back().init(); |
| 345 } |
| 346 |
| 347 static inline void add_quad_segment(const SkPoint pts[3], |
| 348 PathSegmentArray* segments) { |
| 349 if (pts[0].distanceToSqd(pts[1]) < kCloseSqd || |
| 350 pts[1].distanceToSqd(pts[2]) < kCloseSqd || |
| 351 is_colinear(pts)) { |
| 352 if (pts[0] != pts[2]) { |
| 353 SkPoint line_pts[2]; |
| 354 line_pts[0] = pts[0]; |
| 355 line_pts[1] = pts[2]; |
| 356 add_line_to_segment(line_pts, segments); |
| 357 } |
| 358 } else { |
| 359 segments->push_back(); |
| 360 segments->back().fType = PathSegment::kQuad; |
| 361 segments->back().fPts[0] = pts[0]; |
| 362 segments->back().fPts[1] = pts[1]; |
| 363 segments->back().fPts[2] = pts[2]; |
| 364 |
| 365 segments->back().init(); |
| 366 } |
| 367 } |
| 368 |
| 369 static inline void add_cubic_segments(const SkPoint pts[4], |
| 370 PathSegmentArray* segments) { |
| 371 SkSTArray<15, SkPoint, true> quads; |
| 372 SkPathPriv::FirstDirection dir = SkPathPriv::kUnknown_FirstDirection; |
| 373 GrPathUtils::convertCubicToQuads(pts, SK_Scalar1, false, dir, &quads); |
| 374 int count = quads.count(); |
| 375 for (int q = 0; q < count; q += 3) { |
| 376 add_quad_segment(&quads[q], segments); |
| 377 } |
| 378 } |
| 379 |
| 380 static float calculate_nearest_point_for_quad( |
| 381 const PathSegment& segment, |
| 382 const DPoint &xFormPt) { |
| 383 static const float kThird = 0.33333333333f; |
| 384 static const float kTwentySeventh = 0.037037037f; |
| 385 |
| 386 const float a = 0.5f - xFormPt.y(); |
| 387 const float b = -0.5f * xFormPt.x(); |
| 388 |
| 389 const float a3 = a * a * a; |
| 390 const float b2 = b * b; |
| 391 |
| 392 const float c = (b2 * 0.25f) + (a3 * kTwentySeventh); |
| 393 |
| 394 if (c >= 0.f) { |
| 395 const float sqrtC = sqrt(c); |
| 396 const float result = (float)cbrt((-b * 0.5f) + sqrtC) + (float)cbrt((-b
* 0.5f) - sqrtC); |
| 397 return result; |
| 398 } else { |
| 399 const float cosPhi = (float)sqrt((b2 * 0.25f) * (-27.f / a3)) * ((b > 0)
? -1.f : 1.f); |
| 400 const float phi = (float)acos(cosPhi); |
| 401 float result; |
| 402 if (xFormPt.x() > 0.f) { |
| 403 result = 2.f * (float)sqrt(-a * kThird) * (float)cos(phi * kThird); |
| 404 if (!between_closed(result, segment.fP0T.x(), segment.fP2T.x())) { |
| 405 result = 2.f * (float)sqrt(-a * kThird) * (float)cos((phi * kThi
rd) + (SK_ScalarPI * 2.f * kThird)); |
| 406 } |
| 407 } else { |
| 408 result = 2.f * (float)sqrt(-a * kThird) * (float)cos((phi * kThird)
+ (SK_ScalarPI * 2.f * kThird)); |
| 409 if (!between_closed(result, segment.fP0T.x(), segment.fP2T.x())) { |
| 410 result = 2.f * (float)sqrt(-a * kThird) * (float)cos(phi * kThir
d); |
| 411 } |
| 412 } |
| 413 return result; |
| 414 } |
| 415 } |
| 416 |
| 417 // This structure contains some intermediate values shared by the same row. |
| 418 // It is used to calculate segment side of a quadratic bezier. |
| 419 struct RowData { |
| 420 // The intersection type of a scanline and y = x * x parabola in canonical s
pace. |
| 421 enum IntersectionType { |
| 422 kNoIntersection, |
| 423 kVerticalLine, |
| 424 kTangentLine, |
| 425 kTwoPointsIntersect |
| 426 } fIntersectionType; |
| 427 |
| 428 // The direction of the quadratic segment in the canonical space. |
| 429 // 1: The quadratic segment going from negative x-axis to positive x-axis. |
| 430 // -1: The quadratic segment going from positive x-axis to negative x-axis. |
| 431 int fQuadXDirection; |
| 432 |
| 433 // The y-value(equal to x*x) of intersection point for the kVerticalLine int
ersection type. |
| 434 double fYAtIntersection; |
| 435 |
| 436 // The x-value for two intersection points. |
| 437 double fXAtIntersection1; |
| 438 double fXAtIntersection2; |
| 439 }; |
| 440 |
| 441 void precomputation_for_row( |
| 442 RowData *rowData, |
| 443 const PathSegment& segment, |
| 444 const SkPoint& pointLeft, |
| 445 const SkPoint& pointRight |
| 446 ) { |
| 447 if (segment.fType != PathSegment::kQuad) { |
| 448 return; |
| 449 } |
| 450 |
| 451 const DPoint& xFormPtLeft = segment.fXformMatrix.mapPoint(pointLeft); |
| 452 const DPoint& xFormPtRight = segment.fXformMatrix.mapPoint(pointRight);; |
| 453 |
| 454 rowData->fQuadXDirection = sign_of(segment.fP2T.x() - segment.fP0T.x()); |
| 455 |
| 456 const double x1 = xFormPtLeft.x(); |
| 457 const double y1 = xFormPtLeft.y(); |
| 458 const double x2 = xFormPtRight.x(); |
| 459 const double y2 = xFormPtRight.y(); |
| 460 |
| 461 if (nearly_equal(x1, x2)) { |
| 462 rowData->fIntersectionType = RowData::kVerticalLine; |
| 463 rowData->fYAtIntersection = x1 * x1; |
| 464 return; |
| 465 } |
| 466 |
| 467 // Line y = mx + b |
| 468 const double m = (y2 - y1) / (x2 - x1); |
| 469 const double b = -m * x1 + y1; |
| 470 |
| 471 const double c = m * m + 4.0 * b; |
| 472 |
| 473 if (nearly_zero(c, 4.0 * kNearlyZero * kNearlyZero)) { |
| 474 rowData->fIntersectionType = RowData::kTangentLine; |
| 475 rowData->fXAtIntersection1 = m / 2.0; |
| 476 rowData->fXAtIntersection2 = m / 2.0; |
| 477 } else if (c < 0.0) { |
| 478 rowData->fIntersectionType = RowData::kNoIntersection; |
| 479 return; |
| 480 } else { |
| 481 rowData->fIntersectionType = RowData::kTwoPointsIntersect; |
| 482 const double d = sqrt(c); |
| 483 rowData->fXAtIntersection1 = (m + d) / 2.0; |
| 484 rowData->fXAtIntersection2 = (m - d) / 2.0; |
| 485 } |
| 486 } |
| 487 |
| 488 SegSide calculate_side_of_quad( |
| 489 const PathSegment& segment, |
| 490 const SkPoint& point, |
| 491 const DPoint& xFormPt, |
| 492 const RowData& rowData) { |
| 493 SegSide side = kNA_SegSide; |
| 494 |
| 495 if (RowData::kVerticalLine == rowData.fIntersectionType) { |
| 496 side = (SegSide)(int)(sign_of(rowData.fYAtIntersection - xFormPt.y()) *
rowData.fQuadXDirection); |
| 497 } |
| 498 else if (RowData::kTwoPointsIntersect == rowData.fIntersectionType || |
| 499 RowData::kTangentLine == rowData.fIntersectionType) { |
| 500 const double p1 = rowData.fXAtIntersection1; |
| 501 const double p2 = rowData.fXAtIntersection2; |
| 502 |
| 503 int signP1 = sign_of(p1 - xFormPt.x()); |
| 504 if (between_closed(p1, segment.fP0T.x(), segment.fP2T.x())) { |
| 505 side = (SegSide)((-signP1) * rowData.fQuadXDirection); |
| 506 } |
| 507 if (between_closed(p2, segment.fP0T.x(), segment.fP2T.x())) { |
| 508 int signP2 = sign_of(p2 - xFormPt.x()); |
| 509 if (side == kNA_SegSide || signP2 == 1) { |
| 510 side = (SegSide)(signP2 * rowData.fQuadXDirection); |
| 511 } |
| 512 } |
| 513 |
| 514 // The scanline is the tangent line of current quadratic segment. |
| 515 if (RowData::kTangentLine == rowData.fIntersectionType) { |
| 516 // The path start at the tangent point. |
| 517 if (nearly_equal(p1, segment.fP0T.x())) { |
| 518 side = (SegSide)(side * (-signP1) * rowData.fQuadXDirection); |
| 519 } |
| 520 |
| 521 // The path end at the tangent point. |
| 522 if (nearly_equal(p1, segment.fP2T.x())) { |
| 523 side = (SegSide)(side * signP1 * rowData.fQuadXDirection); |
| 524 } |
| 525 } |
| 526 } |
| 527 |
| 528 return side; |
| 529 } |
| 530 |
| 531 static float distance_to_segment(const SkPoint& point, |
| 532 const PathSegment& segment, |
| 533 const RowData& rowData, |
| 534 SegSide* side) { |
| 535 SkASSERT(side); |
| 536 |
| 537 const DPoint xformPt = segment.fXformMatrix.mapPoint(point); |
| 538 |
| 539 if (segment.fType == PathSegment::kLine) { |
| 540 float result = SK_DistanceFieldPad * SK_DistanceFieldPad; |
| 541 |
| 542 if (between_closed(xformPt.x(), segment.fP0T.x(), segment.fP2T.x())) { |
| 543 result = xformPt.y() * xformPt.y(); |
| 544 } else if (xformPt.x() < segment.fP0T.x()) { |
| 545 result = (xformPt.x() * xformPt.x() + xformPt.y() * xformPt.y()); |
| 546 } else { |
| 547 result = ((xformPt.x() - segment.fP2T.x()) * (xformPt.x() - segment.
fP2T.x()) |
| 548 + xformPt.y() * xformPt.y()); |
| 549 } |
| 550 |
| 551 if (between_closed_open(point.y(), segment.fBoundingBox.top(), |
| 552 segment.fBoundingBox.bottom())) { |
| 553 *side = (SegSide)(int)sign_of(-xformPt.y()); |
| 554 } else { |
| 555 *side = kNA_SegSide; |
| 556 } |
| 557 return result; |
| 558 } else { |
| 559 SkASSERT(segment.fType == PathSegment::kQuad); |
| 560 |
| 561 const float nearestPoint = calculate_nearest_point_for_quad(segment, xfo
rmPt); |
| 562 |
| 563 float dist; |
| 564 |
| 565 if (between_closed(nearestPoint, segment.fP0T.x(), segment.fP2T.x())) { |
| 566 DPoint x = DPoint::Make(nearestPoint, nearestPoint * nearestPoint); |
| 567 dist = xformPt.distanceToSqd(x); |
| 568 } else { |
| 569 const float distToB0T = xformPt.distanceToSqd(segment.fP0T); |
| 570 const float distToB2T = xformPt.distanceToSqd(segment.fP2T); |
| 571 |
| 572 if (distToB0T < distToB2T) { |
| 573 dist = distToB0T; |
| 574 } else { |
| 575 dist = distToB2T; |
| 576 } |
| 577 } |
| 578 |
| 579 if (between_closed_open(point.y(), segment.fBoundingBox.top(), |
| 580 segment.fBoundingBox.bottom())) { |
| 581 *side = calculate_side_of_quad(segment, point, xformPt, rowData); |
| 582 } else { |
| 583 *side = kNA_SegSide; |
| 584 } |
| 585 |
| 586 return dist * segment.fScalingFactor; |
| 587 } |
| 588 } |
| 589 |
| 590 static void calculate_distance_field_data(PathSegmentArray* segments, |
| 591 DFData* dataPtr, |
| 592 int width, int height) { |
| 593 int count = segments->count(); |
| 594 for (int a = 0; a < count; ++a) { |
| 595 PathSegment& segment = (*segments)[a]; |
| 596 const SkRect& segBB = segment.fBoundingBox.makeOutset( |
| 597 SK_DistanceFieldPad, SK_DistanceFieldPad); |
| 598 int startColumn = segBB.left(); |
| 599 int endColumn = SkScalarCeilToInt(segBB.right()); |
| 600 |
| 601 int startRow = segBB.top(); |
| 602 int endRow = SkScalarCeilToInt(segBB.bottom()); |
| 603 |
| 604 SkASSERT((startColumn >= 0) && "StartColumn < 0!"); |
| 605 SkASSERT((endColumn <= width) && "endColumn > width!"); |
| 606 SkASSERT((startRow >= 0) && "StartRow < 0!"); |
| 607 SkASSERT((endRow <= height) && "EndRow > height!"); |
| 608 |
| 609 for (int row = startRow; row < endRow; ++row) { |
| 610 SegSide prevSide = kNA_SegSide; |
| 611 const float pY = row + 0.5f; |
| 612 RowData rowData; |
| 613 |
| 614 const SkPoint pointLeft = SkPoint::Make(startColumn, pY); |
| 615 const SkPoint pointRight = SkPoint::Make(endColumn, pY); |
| 616 |
| 617 precomputation_for_row(&rowData, segment, pointLeft, pointRight); |
| 618 |
| 619 for (int col = startColumn; col < endColumn; ++col) { |
| 620 int idx = (row * width) + col; |
| 621 |
| 622 const float pX = col + 0.5f; |
| 623 const SkPoint point = SkPoint::Make(pX, pY); |
| 624 |
| 625 const float distSq = dataPtr[idx].fDistSq; |
| 626 int dilation = distSq < 1.5 * 1.5 ? 1 : |
| 627 distSq < 2.5 * 2.5 ? 2 : |
| 628 distSq < 3.5 * 3.5 ? 3 : SK_DistanceFieldPad; |
| 629 if (dilation > SK_DistanceFieldPad) { |
| 630 dilation = SK_DistanceFieldPad; |
| 631 } |
| 632 |
| 633 // Optimisation for not calculating some points. |
| 634 if (dilation != SK_DistanceFieldPad && !segment.fBoundingBox.rou
ndOut() |
| 635 .makeOutset(dilation, dilation).contains(col, row)) { |
| 636 continue; |
| 637 } |
| 638 |
| 639 SegSide side = kNA_SegSide; |
| 640 int deltaWindingScore = 0; |
| 641 float currDistSq = distance_to_segment(point, segment, rowData
, &side); |
| 642 if (prevSide == kLeft_SegSide && side == kRight_SegSide) { |
| 643 deltaWindingScore = -1; |
| 644 } else if (prevSide == kRight_SegSide && side == kLeft_SegSide)
{ |
| 645 deltaWindingScore = 1; |
| 646 } |
| 647 |
| 648 prevSide = side; |
| 649 |
| 650 if (currDistSq < distSq) { |
| 651 dataPtr[idx].fDistSq = currDistSq; |
| 652 } |
| 653 |
| 654 dataPtr[idx].fDeltaWindingScore += deltaWindingScore; |
| 655 } |
| 656 } |
| 657 } |
| 658 } |
| 659 |
| 660 static unsigned char pack_distance_field_val(float dist, float distanceMagnitude
) { |
| 661 // The distance field is constructed as unsigned char values, so that the ze
ro value is at 128, |
| 662 // Beside 128, we have 128 values in range [0, 128), but only 127 values in
range (128, 255]. |
| 663 // So we multiply distanceMagnitude by 127/128 at the latter range to avoid
overflow. |
| 664 dist = SkScalarPin(-dist, -distanceMagnitude, distanceMagnitude * 127.0f / 1
28.0f); |
| 665 |
| 666 // Scale into the positive range for unsigned distance |
| 667 dist += distanceMagnitude; |
| 668 |
| 669 // Scale into unsigned char range |
| 670 return (unsigned char)(dist / (2 * distanceMagnitude) * 256.0f); |
| 671 } |
| 672 |
| 673 bool GrGenerateDistanceFieldFromPath(unsigned char* distanceField, |
| 674 const SkPath& path, const SkMatrix& drawMat
rix, |
| 675 int width, int height, size_t rowBytes) { |
| 676 SkASSERT(distanceField); |
| 677 |
| 678 SkPath simplifiedPath; |
| 679 Simplify(path, &simplifiedPath); |
| 680 |
| 681 SkASSERT(SkPath::kEvenOdd_FillType == simplifiedPath.getFillType() || |
| 682 SkPath::kInverseEvenOdd_FillType == simplifiedPath.getFillType()); |
| 683 |
| 684 SkMatrix m = drawMatrix; |
| 685 m.postTranslate(SK_DistanceFieldPad, SK_DistanceFieldPad); |
| 686 |
| 687 // create temp data |
| 688 size_t dataSize = width * height * sizeof(DFData); |
| 689 SkAutoSMalloc<1024> dfStorage(dataSize); |
| 690 DFData* dataPtr = (DFData*) dfStorage.get(); |
| 691 |
| 692 // create initial distance data |
| 693 init_distances(dataPtr, width * height); |
| 694 |
| 695 SkPath::Iter iter(simplifiedPath, true); |
| 696 SkSTArray<15, PathSegment, true> segments; |
| 697 |
| 698 for (;;) { |
| 699 SkPoint pts[4]; |
| 700 SkPath::Verb verb = iter.next(pts); |
| 701 switch (verb) { |
| 702 case SkPath::kMove_Verb: |
| 703 // m.mapPoints(pts, 1); |
| 704 break; |
| 705 case SkPath::kLine_Verb: { |
| 706 m.mapPoints(pts, 2); |
| 707 add_line_to_segment(pts, &segments); |
| 708 break; |
| 709 } |
| 710 case SkPath::kQuad_Verb: |
| 711 m.mapPoints(pts, 3); |
| 712 add_quad_segment(pts, &segments); |
| 713 break; |
| 714 case SkPath::kConic_Verb: { |
| 715 m.mapPoints(pts, 3); |
| 716 SkScalar weight = iter.conicWeight(); |
| 717 SkAutoConicToQuads converter; |
| 718 const SkPoint* quadPts = converter.computeQuads(pts, weight, 0.5
f); |
| 719 for (int i = 0; i < converter.countQuads(); ++i) { |
| 720 add_quad_segment(quadPts + 2*i, &segments); |
| 721 } |
| 722 break; |
| 723 } |
| 724 case SkPath::kCubic_Verb: { |
| 725 m.mapPoints(pts, 4); |
| 726 add_cubic_segments(pts, &segments); |
| 727 break; |
| 728 }; |
| 729 default: |
| 730 break; |
| 731 } |
| 732 if (verb == SkPath::kDone_Verb) { |
| 733 break; |
| 734 } |
| 735 } |
| 736 |
| 737 calculate_distance_field_data(&segments, dataPtr, width, height); |
| 738 |
| 739 for (int row = 0; row < height; ++row) { |
| 740 int windingNumber = 0; // Winding number start from zero for each scanli
ne |
| 741 for (int col = 0; col < width; ++col) { |
| 742 int idx = (row * width) + col; |
| 743 windingNumber += dataPtr[idx].fDeltaWindingScore; |
| 744 |
| 745 enum DFSign { |
| 746 kInside = -1, |
| 747 kOutside = 1 |
| 748 } dfSign; |
| 749 |
| 750 if (simplifiedPath.getFillType() == SkPath::kWinding_FillType) { |
| 751 dfSign = windingNumber ? kInside : kOutside; |
| 752 } else if (simplifiedPath.getFillType() == SkPath::kInverseWinding_F
illType) { |
| 753 dfSign = windingNumber ? kOutside : kInside; |
| 754 } else if (simplifiedPath.getFillType() == SkPath::kEvenOdd_FillType
) { |
| 755 dfSign = (windingNumber % 2) ? kInside : kOutside; |
| 756 } else { |
| 757 SkASSERT(simplifiedPath.getFillType() == SkPath::kInverseEvenOdd
_FillType); |
| 758 dfSign = (windingNumber % 2) ? kOutside : kInside; |
| 759 } |
| 760 |
| 761 // The winding number at the end of a scanline should be zero. |
| 762 if ((col == width - 1) && (windingNumber != 0)) { |
| 763 SkASSERT(0 && "Winding number should be zero at the end of a sca
n line."); |
| 764 return false; |
| 765 } |
| 766 |
| 767 const float miniDist = sqrt(dataPtr[idx].fDistSq); |
| 768 const float dist = dfSign * miniDist; |
| 769 |
| 770 unsigned char pixelVal = |
| 771 pack_distance_field_val(dist, (float)SK_DistanceFieldMagnitude); |
| 772 |
| 773 distanceField[(row * rowBytes) + col] = pixelVal; |
| 774 } |
| 775 } |
| 776 return true; |
| 777 } |
OLD | NEW |