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