Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(88)

Side by Side Diff: src/gpu/GrDistanceFieldGenFromVector.cpp

Issue 2580373002: Revert of Generate Signed Distance Field directly from vector path (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « src/gpu/GrDistanceFieldGenFromVector.h ('k') | src/gpu/ops/GrAADistanceFieldPathRenderer.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 << 18));
142 static const double kTangentTolerance = (SK_Scalar1 / (1 << 11));
143 static const float kConicTolerance = 0.25f;
144
145 static inline bool between_closed_open(double a, double b, double c,
146 double tolerance = 0.0,
147 bool xformToleranceToX = false) {
148 SkASSERT(tolerance >= 0.0);
149 double tolB = tolerance;
150 double tolC = tolerance;
151
152 if (xformToleranceToX) {
153 // Canonical space is y = x^2 and the derivative of x^2 is 2x.
154 // So the slope of the tangent line at point (x, x^2) is 2x.
155 //
156 // /|
157 // sqrt(2x * 2x + 1 * 1) / | 2x
158 // /__|
159 // 1
160 tolB = tolerance / sqrt(4.0 * b * b + 1.0);
161 tolC = tolerance / sqrt(4.0 * c * c + 1.0);
162 }
163 return b < c ? (a >= b - tolB && a < c - tolC) :
164 (a >= c - tolC && a < b - tolB);
165 }
166
167 static inline bool between_closed(double a, double b, double c,
168 double tolerance = 0.0,
169 bool xformToleranceToX = false) {
170 SkASSERT(tolerance >= 0.0);
171 double tolB = tolerance;
172 double tolC = tolerance;
173
174 if (xformToleranceToX) {
175 tolB = tolerance / sqrt(4.0 * b * b + 1.0);
176 tolC = tolerance / sqrt(4.0 * c * c + 1.0);
177 }
178 return b < c ? (a >= b - tolB && a <= c + tolC) :
179 (a >= c - tolC && a <= b + tolB);
180 }
181
182 static inline bool nearly_zero(double x, double tolerance = kNearlyZero) {
183 SkASSERT(tolerance >= 0.0);
184 return fabs(x) <= tolerance;
185 }
186
187 static inline bool nearly_equal(double x, double y,
188 double tolerance = kNearlyZero,
189 bool xformToleranceToX = false) {
190 SkASSERT(tolerance >= 0.0);
191 if (xformToleranceToX) {
192 tolerance = tolerance / sqrt(4.0 * y * y + 1.0);
193 }
194 return fabs(x - y) <= tolerance;
195 }
196
197 static inline double sign_of(const double &val) {
198 return (val < 0.0) ? -1.0 : 1.0;
199 }
200
201 static bool is_colinear(const SkPoint pts[3]) {
202 return nearly_zero((pts[1].y() - pts[0].y()) * (pts[1].x() - pts[2].x()) -
203 (pts[1].y() - pts[2].y()) * (pts[1].x() - pts[0].x()), kC loseSqd);
204 }
205
206 class PathSegment {
207 public:
208 enum {
209 // These enum values are assumed in member functions below.
210 kLine = 0,
211 kQuad = 1,
212 } fType;
213
214 // line uses 2 pts, quad uses 3 pts
215 SkPoint fPts[3];
216
217 DPoint fP0T, fP2T;
218 DAffineMatrix fXformMatrix;
219 double fScalingFactor;
220 double fScalingFactorSqd;
221 double fNearlyZeroScaled;
222 double fTangentTolScaledSqd;
223 SkRect fBoundingBox;
224
225 void init();
226
227 int countPoints() {
228 GR_STATIC_ASSERT(0 == kLine && 1 == kQuad);
229 return fType + 2;
230 }
231
232 const SkPoint& endPt() const {
233 GR_STATIC_ASSERT(0 == kLine && 1 == kQuad);
234 return fPts[fType + 1];
235 }
236 };
237
238 typedef SkTArray<PathSegment, true> PathSegmentArray;
239
240 void PathSegment::init() {
241 const DPoint p0 = DPoint::Make(fPts[0].x(), fPts[0].y());
242 const DPoint p2 = DPoint::Make(this->endPt().x(), this->endPt().y());
243 const double p0x = p0.x();
244 const double p0y = p0.y();
245 const double p2x = p2.x();
246 const double p2y = p2.y();
247
248 fBoundingBox.set(fPts[0], this->endPt());
249
250 if (fType == PathSegment::kLine) {
251 fScalingFactorSqd = fScalingFactor = 1.0;
252 double hypotenuse = DPoint::Distance(p0, p2);
253
254 const double cosTheta = (p2x - p0x) / hypotenuse;
255 const double sinTheta = (p2y - p0y) / hypotenuse;
256
257 fXformMatrix.setAffine(
258 cosTheta, sinTheta, -(cosTheta * p0x) - (sinTheta * p0y),
259 -sinTheta, cosTheta, (sinTheta * p0x) - (cosTheta * p0y)
260 );
261 } else {
262 SkASSERT(fType == PathSegment::kQuad);
263
264 // Calculate bounding box
265 const SkPoint _P1mP0 = fPts[1] - fPts[0];
266 SkPoint t = _P1mP0 - fPts[2] + fPts[1];
267 t.fX = _P1mP0.x() / t.x();
268 t.fY = _P1mP0.y() / t.y();
269 t.fX = SkScalarClampMax(t.x(), 1.0);
270 t.fY = SkScalarClampMax(t.y(), 1.0);
271 t.fX = _P1mP0.x() * t.x();
272 t.fY = _P1mP0.y() * t.y();
273 const SkPoint m = fPts[0] + t;
274 fBoundingBox.growToInclude(&m, 1);
275
276 const double p1x = fPts[1].x();
277 const double p1y = fPts[1].y();
278
279 const double p0xSqd = p0x * p0x;
280 const double p0ySqd = p0y * p0y;
281 const double p2xSqd = p2x * p2x;
282 const double p2ySqd = p2y * p2y;
283 const double p1xSqd = p1x * p1x;
284 const double p1ySqd = p1y * p1y;
285
286 const double p01xProd = p0x * p1x;
287 const double p02xProd = p0x * p2x;
288 const double b12xProd = p1x * p2x;
289 const double p01yProd = p0y * p1y;
290 const double p02yProd = p0y * p2y;
291 const double b12yProd = p1y * p2y;
292
293 const double sqrtA = p0y - (2.0 * p1y) + p2y;
294 const double a = sqrtA * sqrtA;
295 const double h = -1.0 * (p0y - (2.0 * p1y) + p2y) * (p0x - (2.0 * p1x) + p2x);
296 const double sqrtB = p0x - (2.0 * p1x) + p2x;
297 const double b = sqrtB * sqrtB;
298 const double c = (p0xSqd * p2ySqd) - (4.0 * p01xProd * b12yProd)
299 - (2.0 * p02xProd * p02yProd) + (4.0 * p02xProd * p1ySqd)
300 + (4.0 * p1xSqd * p02yProd) - (4.0 * b12xProd * p01yProd)
301 + (p2xSqd * p0ySqd);
302 const double g = (p0x * p02yProd) - (2.0 * p0x * p1ySqd)
303 + (2.0 * p0x * b12yProd) - (p0x * p2ySqd)
304 + (2.0 * p1x * p01yProd) - (4.0 * p1x * p02yProd)
305 + (2.0 * p1x * b12yProd) - (p2x * p0ySqd)
306 + (2.0 * p2x * p01yProd) + (p2x * p02yProd)
307 - (2.0 * p2x * p1ySqd);
308 const double f = -((p0xSqd * p2y) - (2.0 * p01xProd * p1y)
309 - (2.0 * p01xProd * p2y) - (p02xProd * p0y)
310 + (4.0 * p02xProd * p1y) - (p02xProd * p2y)
311 + (2.0 * p1xSqd * p0y) + (2.0 * p1xSqd * p2y)
312 - (2.0 * b12xProd * p0y) - (2.0 * b12xProd * p1y)
313 + (p2xSqd * p0y));
314
315 const double cosTheta = sqrt(a / (a + b));
316 const double sinTheta = -1.0 * sign_of((a + b) * h) * sqrt(b / (a + b));
317
318 const double gDef = cosTheta * g - sinTheta * f;
319 const double fDef = sinTheta * g + cosTheta * f;
320
321
322 const double x0 = gDef / (a + b);
323 const double y0 = (1.0 / (2.0 * fDef)) * (c - (gDef * gDef / (a + b)));
324
325
326 const double lambda = -1.0 * ((a + b) / (2.0 * fDef));
327 fScalingFactor = fabs(1.0 / lambda);
328 fScalingFactorSqd = fScalingFactor * fScalingFactor;
329
330 const double lambda_cosTheta = lambda * cosTheta;
331 const double lambda_sinTheta = lambda * sinTheta;
332
333 fXformMatrix.setAffine(
334 lambda_cosTheta, -lambda_sinTheta, lambda * x0,
335 lambda_sinTheta, lambda_cosTheta, lambda * y0
336 );
337 }
338
339 fNearlyZeroScaled = kNearlyZero / fScalingFactor;
340 fTangentTolScaledSqd = kTangentTolerance * kTangentTolerance / fScalingFacto rSqd;
341
342 fP0T = fXformMatrix.mapPoint(p0);
343 fP2T = fXformMatrix.mapPoint(p2);
344 }
345
346 static void init_distances(DFData* data, int size) {
347 DFData* currData = data;
348
349 for (int i = 0; i < size; ++i) {
350 // init distance to "far away"
351 currData->fDistSq = SK_DistanceFieldMagnitude * SK_DistanceFieldMagnitud e;
352 currData->fDeltaWindingScore = 0;
353 ++currData;
354 }
355 }
356
357 static inline void add_line_to_segment(const SkPoint pts[2],
358 PathSegmentArray* segments) {
359 segments->push_back();
360 segments->back().fType = PathSegment::kLine;
361 segments->back().fPts[0] = pts[0];
362 segments->back().fPts[1] = pts[1];
363
364 segments->back().init();
365 }
366
367 static inline void add_quad_segment(const SkPoint pts[3],
368 PathSegmentArray* segments) {
369 if (pts[0].distanceToSqd(pts[1]) < kCloseSqd ||
370 pts[1].distanceToSqd(pts[2]) < kCloseSqd ||
371 is_colinear(pts)) {
372 if (pts[0] != pts[2]) {
373 SkPoint line_pts[2];
374 line_pts[0] = pts[0];
375 line_pts[1] = pts[2];
376 add_line_to_segment(line_pts, segments);
377 }
378 } else {
379 segments->push_back();
380 segments->back().fType = PathSegment::kQuad;
381 segments->back().fPts[0] = pts[0];
382 segments->back().fPts[1] = pts[1];
383 segments->back().fPts[2] = pts[2];
384
385 segments->back().init();
386 }
387 }
388
389 static inline void add_cubic_segments(const SkPoint pts[4],
390 PathSegmentArray* segments) {
391 SkSTArray<15, SkPoint, true> quads;
392 GrPathUtils::convertCubicToQuads(pts, SK_Scalar1, &quads);
393 int count = quads.count();
394 for (int q = 0; q < count; q += 3) {
395 add_quad_segment(&quads[q], segments);
396 }
397 }
398
399 static float calculate_nearest_point_for_quad(
400 const PathSegment& segment,
401 const DPoint &xFormPt) {
402 static const float kThird = 0.33333333333f;
403 static const float kTwentySeventh = 0.037037037f;
404
405 const float a = 0.5f - (float)xFormPt.y();
406 const float b = -0.5f * (float)xFormPt.x();
407
408 const float a3 = a * a * a;
409 const float b2 = b * b;
410
411 const float c = (b2 * 0.25f) + (a3 * kTwentySeventh);
412
413 if (c >= 0.f) {
414 const float sqrtC = sqrt(c);
415 const float result = (float)cbrt((-b * 0.5f) + sqrtC) + (float)cbrt((-b * 0.5f) - sqrtC);
416 return result;
417 } else {
418 const float cosPhi = (float)sqrt((b2 * 0.25f) * (-27.f / a3)) * ((b > 0) ? -1.f : 1.f);
419 const float phi = (float)acos(cosPhi);
420 float result;
421 if (xFormPt.x() > 0.f) {
422 result = 2.f * (float)sqrt(-a * kThird) * (float)cos(phi * kThird);
423 if (!between_closed(result, segment.fP0T.x(), segment.fP2T.x())) {
424 result = 2.f * (float)sqrt(-a * kThird) * (float)cos((phi * kThi rd) + (SK_ScalarPI * 2.f * kThird));
425 }
426 } else {
427 result = 2.f * (float)sqrt(-a * kThird) * (float)cos((phi * kThird) + (SK_ScalarPI * 2.f * kThird));
428 if (!between_closed(result, segment.fP0T.x(), segment.fP2T.x())) {
429 result = 2.f * (float)sqrt(-a * kThird) * (float)cos(phi * kThir d);
430 }
431 }
432 return result;
433 }
434 }
435
436 // This structure contains some intermediate values shared by the same row.
437 // It is used to calculate segment side of a quadratic bezier.
438 struct RowData {
439 // The intersection type of a scanline and y = x * x parabola in canonical s pace.
440 enum IntersectionType {
441 kNoIntersection,
442 kVerticalLine,
443 kTangentLine,
444 kTwoPointsIntersect
445 } fIntersectionType;
446
447 // The direction of the quadratic segment/scanline in the canonical space.
448 // 1: The quadratic segment/scanline going from negative x-axis to positive x-axis.
449 // 0: The scanline is a vertical line in the canonical space.
450 // -1: The quadratic segment/scanline going from positive x-axis to negative x-axis.
451 int fQuadXDirection;
452 int fScanlineXDirection;
453
454 // The y-value(equal to x*x) of intersection point for the kVerticalLine int ersection type.
455 double fYAtIntersection;
456
457 // The x-value for two intersection points.
458 double fXAtIntersection1;
459 double fXAtIntersection2;
460 };
461
462 void precomputation_for_row(
463 RowData *rowData,
464 const PathSegment& segment,
465 const SkPoint& pointLeft,
466 const SkPoint& pointRight
467 ) {
468 if (segment.fType != PathSegment::kQuad) {
469 return;
470 }
471
472 const DPoint& xFormPtLeft = segment.fXformMatrix.mapPoint(pointLeft);
473 const DPoint& xFormPtRight = segment.fXformMatrix.mapPoint(pointRight);;
474
475 rowData->fQuadXDirection = (int)sign_of(segment.fP2T.x() - segment.fP0T.x()) ;
476 rowData->fScanlineXDirection = (int)sign_of(xFormPtRight.x() - xFormPtLeft.x ());
477
478 const double x1 = xFormPtLeft.x();
479 const double y1 = xFormPtLeft.y();
480 const double x2 = xFormPtRight.x();
481 const double y2 = xFormPtRight.y();
482
483 if (nearly_equal(x1, x2, segment.fNearlyZeroScaled, true)) {
484 rowData->fIntersectionType = RowData::kVerticalLine;
485 rowData->fYAtIntersection = x1 * x1;
486 rowData->fScanlineXDirection = 0;
487 return;
488 }
489
490 // Line y = mx + b
491 const double m = (y2 - y1) / (x2 - x1);
492 const double b = -m * x1 + y1;
493
494 const double m2 = m * m;
495 const double c = m2 + 4.0 * b;
496
497 const double tol = 4.0 * segment.fTangentTolScaledSqd / (m2 + 1.0);
498
499 // Check if the scanline is the tangent line of the curve,
500 // and the curve start or end at the same y-coordinate of the scanline
501 if ((rowData->fScanlineXDirection == 1 &&
502 (segment.fPts[0].y() == pointLeft.y() ||
503 segment.fPts[2].y() == pointLeft.y())) &&
504 nearly_zero(c, tol)) {
505 rowData->fIntersectionType = RowData::kTangentLine;
506 rowData->fXAtIntersection1 = m / 2.0;
507 rowData->fXAtIntersection2 = m / 2.0;
508 } else if (c <= 0.0) {
509 rowData->fIntersectionType = RowData::kNoIntersection;
510 return;
511 } else {
512 rowData->fIntersectionType = RowData::kTwoPointsIntersect;
513 const double d = sqrt(c);
514 rowData->fXAtIntersection1 = (m + d) / 2.0;
515 rowData->fXAtIntersection2 = (m - d) / 2.0;
516 }
517 }
518
519 SegSide calculate_side_of_quad(
520 const PathSegment& segment,
521 const SkPoint& point,
522 const DPoint& xFormPt,
523 const RowData& rowData) {
524 SegSide side = kNA_SegSide;
525
526 if (RowData::kVerticalLine == rowData.fIntersectionType) {
527 side = (SegSide)(int)(sign_of(xFormPt.y() - rowData.fYAtIntersection) * rowData.fQuadXDirection);
528 }
529 else if (RowData::kTwoPointsIntersect == rowData.fIntersectionType) {
530 const double p1 = rowData.fXAtIntersection1;
531 const double p2 = rowData.fXAtIntersection2;
532
533 int signP1 = (int)sign_of(p1 - xFormPt.x());
534 bool includeP1 = true;
535 bool includeP2 = true;
536
537 if (rowData.fScanlineXDirection == 1) {
538 if ((rowData.fQuadXDirection == -1 && segment.fPts[0].y() <= point.y () &&
539 nearly_equal(segment.fP0T.x(), p1, segment.fNearlyZeroScaled, t rue)) ||
540 (rowData.fQuadXDirection == 1 && segment.fPts[2].y() <= point.y () &&
541 nearly_equal(segment.fP2T.x(), p1, segment.fNearlyZeroScaled, t rue))) {
542 includeP1 = false;
543 }
544 if ((rowData.fQuadXDirection == -1 && segment.fPts[2].y() <= point.y () &&
545 nearly_equal(segment.fP2T.x(), p2, segment.fNearlyZeroScaled, t rue)) ||
546 (rowData.fQuadXDirection == 1 && segment.fPts[0].y() <= point.y () &&
547 nearly_equal(segment.fP0T.x(), p2, segment.fNearlyZeroScaled, t rue))) {
548 includeP2 = false;
549 }
550 }
551
552 if (includeP1 && between_closed(p1, segment.fP0T.x(), segment.fP2T.x(),
553 segment.fNearlyZeroScaled, true)) {
554 side = (SegSide)(signP1 * rowData.fQuadXDirection);
555 }
556 if (includeP2 && between_closed(p2, segment.fP0T.x(), segment.fP2T.x(),
557 segment.fNearlyZeroScaled, true)) {
558 int signP2 = (int)sign_of(p2 - xFormPt.x());
559 if (side == kNA_SegSide || signP2 == 1) {
560 side = (SegSide)(-signP2 * rowData.fQuadXDirection);
561 }
562 }
563 } else if (RowData::kTangentLine == rowData.fIntersectionType) {
564 // The scanline is the tangent line of current quadratic segment.
565
566 const double p = rowData.fXAtIntersection1;
567 int signP = (int)sign_of(p - xFormPt.x());
568 if (rowData.fScanlineXDirection == 1) {
569 // The path start or end at the tangent point.
570 if (segment.fPts[0].y() == point.y()) {
571 side = (SegSide)(signP);
572 } else if (segment.fPts[2].y() == point.y()) {
573 side = (SegSide)(-signP);
574 }
575 }
576 }
577
578 return side;
579 }
580
581 static float distance_to_segment(const SkPoint& point,
582 const PathSegment& segment,
583 const RowData& rowData,
584 SegSide* side) {
585 SkASSERT(side);
586
587 const DPoint xformPt = segment.fXformMatrix.mapPoint(point);
588
589 if (segment.fType == PathSegment::kLine) {
590 float result = SK_DistanceFieldPad * SK_DistanceFieldPad;
591
592 if (between_closed(xformPt.x(), segment.fP0T.x(), segment.fP2T.x())) {
593 result = (float)(xformPt.y() * xformPt.y());
594 } else if (xformPt.x() < segment.fP0T.x()) {
595 result = (float)(xformPt.x() * xformPt.x() + xformPt.y() * xformPt.y ());
596 } else {
597 result = (float)((xformPt.x() - segment.fP2T.x()) * (xformPt.x() - s egment.fP2T.x())
598 + xformPt.y() * xformPt.y());
599 }
600
601 if (between_closed_open(point.y(), segment.fBoundingBox.top(),
602 segment.fBoundingBox.bottom())) {
603 *side = (SegSide)(int)sign_of(xformPt.y());
604 } else {
605 *side = kNA_SegSide;
606 }
607 return result;
608 } else {
609 SkASSERT(segment.fType == PathSegment::kQuad);
610
611 const float nearestPoint = calculate_nearest_point_for_quad(segment, xfo rmPt);
612
613 float dist;
614
615 if (between_closed(nearestPoint, segment.fP0T.x(), segment.fP2T.x())) {
616 DPoint x = DPoint::Make(nearestPoint, nearestPoint * nearestPoint);
617 dist = (float)xformPt.distanceToSqd(x);
618 } else {
619 const float distToB0T = (float)xformPt.distanceToSqd(segment.fP0T);
620 const float distToB2T = (float)xformPt.distanceToSqd(segment.fP2T);
621
622 if (distToB0T < distToB2T) {
623 dist = distToB0T;
624 } else {
625 dist = distToB2T;
626 }
627 }
628
629 if (between_closed_open(point.y(), segment.fBoundingBox.top(),
630 segment.fBoundingBox.bottom())) {
631 *side = calculate_side_of_quad(segment, point, xformPt, rowData);
632 } else {
633 *side = kNA_SegSide;
634 }
635
636 return (float)(dist * segment.fScalingFactorSqd);
637 }
638 }
639
640 static void calculate_distance_field_data(PathSegmentArray* segments,
641 DFData* dataPtr,
642 int width, int height) {
643 int count = segments->count();
644 for (int a = 0; a < count; ++a) {
645 PathSegment& segment = (*segments)[a];
646 const SkRect& segBB = segment.fBoundingBox.makeOutset(
647 SK_DistanceFieldPad, SK_DistanceFieldPad);
648 int startColumn = (int)segBB.left();
649 int endColumn = SkScalarCeilToInt(segBB.right());
650
651 int startRow = (int)segBB.top();
652 int endRow = SkScalarCeilToInt(segBB.bottom());
653
654 SkASSERT((startColumn >= 0) && "StartColumn < 0!");
655 SkASSERT((endColumn <= width) && "endColumn > width!");
656 SkASSERT((startRow >= 0) && "StartRow < 0!");
657 SkASSERT((endRow <= height) && "EndRow > height!");
658
659 for (int row = startRow; row < endRow; ++row) {
660 SegSide prevSide = kNA_SegSide;
661 const float pY = row + 0.5f;
662 RowData rowData;
663
664 const SkPoint pointLeft = SkPoint::Make((SkScalar)startColumn, pY);
665 const SkPoint pointRight = SkPoint::Make((SkScalar)endColumn, pY);
666
667 if (between_closed_open(pY, segment.fBoundingBox.top(),
668 segment.fBoundingBox.bottom())) {
669 precomputation_for_row(&rowData, segment, pointLeft, pointRight) ;
670 }
671
672 for (int col = startColumn; col < endColumn; ++col) {
673 int idx = (row * width) + col;
674
675 const float pX = col + 0.5f;
676 const SkPoint point = SkPoint::Make(pX, pY);
677
678 const float distSq = dataPtr[idx].fDistSq;
679 int dilation = distSq < 1.5 * 1.5 ? 1 :
680 distSq < 2.5 * 2.5 ? 2 :
681 distSq < 3.5 * 3.5 ? 3 : SK_DistanceFieldPad;
682 if (dilation > SK_DistanceFieldPad) {
683 dilation = SK_DistanceFieldPad;
684 }
685
686 // Optimisation for not calculating some points.
687 if (dilation != SK_DistanceFieldPad && !segment.fBoundingBox.rou ndOut()
688 .makeOutset(dilation, dilation).contains(col, row)) {
689 continue;
690 }
691
692 SegSide side = kNA_SegSide;
693 int deltaWindingScore = 0;
694 float currDistSq = distance_to_segment(point, segment, rowData , &side);
695 if (prevSide == kLeft_SegSide && side == kRight_SegSide) {
696 deltaWindingScore = -1;
697 } else if (prevSide == kRight_SegSide && side == kLeft_SegSide) {
698 deltaWindingScore = 1;
699 }
700
701 prevSide = side;
702
703 if (currDistSq < distSq) {
704 dataPtr[idx].fDistSq = currDistSq;
705 }
706
707 dataPtr[idx].fDeltaWindingScore += deltaWindingScore;
708 }
709 }
710 }
711 }
712
713 template <int distanceMagnitude>
714 static unsigned char pack_distance_field_val(float dist) {
715 // The distance field is constructed as unsigned char values, so that the ze ro value is at 128,
716 // Beside 128, we have 128 values in range [0, 128), but only 127 values in range (128, 255].
717 // So we multiply distanceMagnitude by 127/128 at the latter range to avoid overflow.
718 dist = SkScalarPin(-dist, -distanceMagnitude, distanceMagnitude * 127.0f / 1 28.0f);
719
720 // Scale into the positive range for unsigned distance.
721 dist += distanceMagnitude;
722
723 // Scale into unsigned char range.
724 // Round to place negative and positive values as equally as possible around 128
725 // (which represents zero).
726 return (unsigned char)SkScalarRoundToInt(dist / (2 * distanceMagnitude) * 25 6.0f);
727 }
728
729 bool GrGenerateDistanceFieldFromPath(unsigned char* distanceField,
730 const SkPath& path, const SkMatrix& drawMat rix,
731 int width, int height, size_t rowBytes) {
732 SkASSERT(distanceField);
733
734 SkPath simplifiedPath;
735 SkPath workingPath;
736 if (Simplify(path, &simplifiedPath)) {
737 workingPath = simplifiedPath;
738 } else {
739 workingPath = path;
740 }
741
742 if (!IsDistanceFieldSupportedFillType(workingPath.getFillType())) {
743 return false;
744 }
745
746 workingPath.transform(drawMatrix);
747
748 // translate path to offset (SK_DistanceFieldPad, SK_DistanceFieldPad)
749 SkMatrix dfMatrix;
750 dfMatrix.setTranslate(SK_DistanceFieldPad, SK_DistanceFieldPad);
751 workingPath.transform(dfMatrix);
752
753 // create temp data
754 size_t dataSize = width * height * sizeof(DFData);
755 SkAutoSMalloc<1024> dfStorage(dataSize);
756 DFData* dataPtr = (DFData*) dfStorage.get();
757
758 // create initial distance data
759 init_distances(dataPtr, width * height);
760
761 SkPath::Iter iter(workingPath, true);
762 SkSTArray<15, PathSegment, true> segments;
763
764 for (;;) {
765 SkPoint pts[4];
766 SkPath::Verb verb = iter.next(pts);
767 switch (verb) {
768 case SkPath::kMove_Verb:
769 break;
770 case SkPath::kLine_Verb: {
771 add_line_to_segment(pts, &segments);
772 break;
773 }
774 case SkPath::kQuad_Verb:
775 add_quad_segment(pts, &segments);
776 break;
777 case SkPath::kConic_Verb: {
778 SkScalar weight = iter.conicWeight();
779 SkAutoConicToQuads converter;
780 const SkPoint* quadPts = converter.computeQuads(pts, weight, kCo nicTolerance);
781 for (int i = 0; i < converter.countQuads(); ++i) {
782 add_quad_segment(quadPts + 2*i, &segments);
783 }
784 break;
785 }
786 case SkPath::kCubic_Verb: {
787 add_cubic_segments(pts, &segments);
788 break;
789 };
790 default:
791 break;
792 }
793 if (verb == SkPath::kDone_Verb) {
794 break;
795 }
796 }
797
798 calculate_distance_field_data(&segments, dataPtr, width, height);
799
800 for (int row = 0; row < height; ++row) {
801 int windingNumber = 0; // Winding number start from zero for each scanli ne
802 for (int col = 0; col < width; ++col) {
803 int idx = (row * width) + col;
804 windingNumber += dataPtr[idx].fDeltaWindingScore;
805
806 enum DFSign {
807 kInside = -1,
808 kOutside = 1
809 } dfSign;
810
811 if (workingPath.getFillType() == SkPath::kWinding_FillType) {
812 dfSign = windingNumber ? kInside : kOutside;
813 } else if (workingPath.getFillType() == SkPath::kInverseWinding_Fill Type) {
814 dfSign = windingNumber ? kOutside : kInside;
815 } else if (workingPath.getFillType() == SkPath::kEvenOdd_FillType) {
816 dfSign = (windingNumber % 2) ? kInside : kOutside;
817 } else {
818 SkASSERT(workingPath.getFillType() == SkPath::kInverseEvenOdd_Fi llType);
819 dfSign = (windingNumber % 2) ? kOutside : kInside;
820 }
821
822 // The winding number at the end of a scanline should be zero.
823 // SkASSERT(((col != width - 1) || (windingNumber == 0)) &&
824 // "Winding number should be zero at the end of a scan line. ");
825 // Fallback to use SkPath::contains to determine the sign of pixel i nstead of assertion.
826 if (col == width - 1 && windingNumber != 0) {
827 for (int col = 0; col < width; ++col) {
828 int idx = (row * width) + col;
829 dfSign = workingPath.contains(col + 0.5, row + 0.5) ? kInsid e : kOutside;
830 const float miniDist = sqrt(dataPtr[idx].fDistSq);
831 const float dist = dfSign * miniDist;
832
833 unsigned char pixelVal = pack_distance_field_val<SK_Distance FieldMagnitude>(dist);
834
835 distanceField[(row * rowBytes) + col] = pixelVal;
836 }
837 continue;
838 }
839
840 const float miniDist = sqrt(dataPtr[idx].fDistSq);
841 const float dist = dfSign * miniDist;
842
843 unsigned char pixelVal = pack_distance_field_val<SK_DistanceFieldMag nitude>(dist);
844
845 distanceField[(row * rowBytes) + col] = pixelVal;
846 }
847 }
848 return true;
849 }
OLDNEW
« no previous file with comments | « src/gpu/GrDistanceFieldGenFromVector.h ('k') | src/gpu/ops/GrAADistanceFieldPathRenderer.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698