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_Side | |
16 * of a segment to the kLeft_Side, the winding score should | |
17 * add 1. | |
18 * And winding score should subtract 1 if the scanline cross | |
19 * from kLeft_Side to kRight_Side. | |
20 * Always return kNA_Side 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 { | |
bsalomon
2016/02/02 18:03:48
nit, we usually put the full name of the enum afte
Joel.Liang
2016/02/04 12:12:05
Done.
| |
37 kLeft_Side = -1, | |
38 kOn_Side = 0, | |
39 kRight_Side = 1, | |
40 kNA_Side = 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 DScalar, DPoint and DMatrix | |
52 */ | |
53 | |
54 // Scalar with double precision | |
55 typedef double DScalar; | |
mtklein
2016/02/04 14:32:59
We could probably just type "double". SkScalar on
Joel.Liang
2016/02/05 10:10:55
Done.
| |
56 | |
57 // Point with double precision | |
58 struct DPoint { | |
59 DScalar fX, fY; | |
60 | |
61 static DPoint Make(DScalar x, DScalar y) { | |
62 DPoint pt; | |
63 pt.set(x, y); | |
64 return pt; | |
65 } | |
66 | |
67 DScalar x() const { return fX; } | |
68 DScalar y() const { return fY; } | |
69 | |
70 void set(DScalar x, DScalar y) { fX = x; fY = y; } | |
71 | |
72 /** Returns the euclidian distance from (0,0) to (x,y) | |
73 */ | |
74 static DScalar Length(DScalar x, DScalar y) { | |
75 return sqrt(x * x + y * y); | |
76 } | |
77 | |
78 /** Returns the euclidian distance between a and b | |
79 */ | |
80 static DScalar Distance(const DPoint& a, const DPoint& b) { | |
81 return Length(a.fX - b.fX, a.fY - b.fY); | |
82 } | |
83 | |
84 DScalar distanceToSqd(const DPoint& pt) const { | |
85 DScalar dx = fX - pt.fX; | |
86 DScalar dy = fY - pt.fY; | |
87 return dx * dx + dy * dy; | |
88 } | |
89 }; | |
90 | |
91 // Matrix with double precision | |
92 class DMatrix { | |
bsalomon
2016/02/02 18:03:48
SkMatrix promotes itself to doubles for some opera
Joel.Liang
2016/02/04 12:12:04
I just tried SkMatrix, it didn't give sufficient p
| |
93 public: | |
94 DScalar operator[](int index) const { | |
95 SkASSERT((unsigned)index < 9); | |
96 return fMat[index]; | |
97 } | |
98 | |
99 DScalar& operator[](int index) { | |
100 SkASSERT((unsigned)index < 9); | |
101 return fMat[index]; | |
102 } | |
103 | |
104 enum { | |
105 kMScaleX, | |
106 kMSkewX, | |
107 kMTransX, | |
108 kMSkewY, | |
109 kMScaleY, | |
110 kMTransY, | |
111 kMPersp0, | |
112 kMPersp1, | |
113 kMPersp2 | |
114 }; | |
115 | |
116 void setAll(DScalar scaleX, DScalar skewX, DScalar transX, | |
117 DScalar skewY, DScalar scaleY, DScalar transY, | |
118 DScalar persp0, DScalar persp1, DScalar persp2) { | |
119 fMat[kMScaleX] = scaleX; | |
120 fMat[kMSkewX] = skewX; | |
121 fMat[kMTransX] = transX; | |
122 fMat[kMSkewY] = skewY; | |
123 fMat[kMScaleY] = scaleY; | |
124 fMat[kMTransY] = transY; | |
125 fMat[kMPersp0] = persp0; | |
126 fMat[kMPersp1] = persp1; | |
127 fMat[kMPersp2] = persp2; | |
128 } | |
129 | |
130 /** Set the matrix to identity | |
131 */ | |
132 void reset() { | |
133 fMat[kMScaleX] = fMat[kMScaleY] = fMat[kMPersp2] = 1.0; | |
134 fMat[kMSkewX] = fMat[kMSkewY] = | |
135 fMat[kMTransX] = fMat[kMTransY] = | |
136 fMat[kMPersp0] = fMat[kMPersp1] = 0.0; | |
137 } | |
138 | |
139 // alias for reset() | |
140 void setIdentity() { this->reset(); } | |
141 | |
142 DPoint mapPoint(const SkPoint& src) const { | |
143 DPoint pt = DPoint::Make(src.x(), src.y()); | |
144 return this->mapPoint(pt); | |
145 } | |
146 | |
147 DPoint mapPoint(const DPoint& src) const { | |
148 return DPoint::Make(fMat[kMScaleX] * src.x() + fMat[kMSkewY] * src.y() + fMat[kMPersp0], | |
mtklein
2016/02/04 14:32:59
If you only use this matrix for affine transformat
Wasim.Abbas
2016/02/04 14:48:38
In Patch Set 5 we have changed this matrix code. I
| |
149 fMat[kMSkewX] * src.x() + fMat[kMScaleY] * src.y() + fMat[kMPersp1]); | |
150 } | |
151 private: | |
152 DScalar fMat[9]; | |
153 }; | |
154 | |
155 /////////////////////////////////////////////////////////////////////////////// | |
156 | |
157 static const SkScalar kClose = (SK_Scalar1 / 16.0); | |
158 static const SkScalar kCloseSqd = SkScalarMul(kClose, kClose); | |
159 static const SkScalar kNearlyZero = (SK_Scalar1 / (1 << 15)); | |
160 | |
161 static inline bool between_closed_open(double a, double b, double c, | |
bsalomon
2016/02/02 18:03:48
Should these doubles be DScalars? (same for the co
Joel.Liang
2016/02/04 12:12:05
Done.
| |
162 SkScalar tolerance = kNearlyZero) { | |
163 SkASSERT(tolerance >= 0.f); | |
164 return b < c ? (a >= b - tolerance && a < c - tolerance) : | |
165 (a >= c - tolerance && a < b - tolerance); | |
166 } | |
167 | |
168 static inline bool between_closed(double a, double b, double c, | |
169 SkScalar tolerance = kNearlyZero) { | |
170 SkASSERT(tolerance >= 0.f); | |
171 return b < c ? (a >= b - tolerance && a <= c + tolerance) : | |
172 (a >= c - tolerance && a <= b + tolerance); | |
173 } | |
174 | |
175 static inline bool nearly_zero(double x, | |
176 SkScalar tolerance = kNearlyZero) { | |
177 SkASSERT(tolerance >= 0.f); | |
178 return fabs(x) <= tolerance; | |
179 } | |
180 | |
181 static inline bool nearly_equal(double x, double y, | |
182 SkScalar tolerance = kNearlyZero) { | |
183 SkASSERT(tolerance >= 0.f); | |
184 return fabs(x - y) <= tolerance; | |
185 } | |
186 | |
187 static inline float sign_of(const float &val) { | |
bsalomon
2016/02/02 18:03:48
Do we not have a helper for this already?
Joel.Liang
2016/02/04 12:12:04
For input value 0.0, we need 1.0 return here. The
| |
188 return (val < 0.f) ? -1.f : 1.f; | |
189 } | |
190 | |
191 static bool is_colinear(const SkPoint pts[3]) { | |
192 return nearly_zero((pts[1].y() - pts[0].y()) * (pts[1].x() - pts[2].x()) - | |
193 (pts[1].y() - pts[2].y()) * (pts[1].x() - pts[0].x())); | |
194 } | |
195 | |
196 class PathSegment { | |
197 public: | |
198 enum { | |
199 // These enum values are assumed in member functions below. | |
200 kLine = 0, | |
201 kQuad = 1, | |
202 } fType; | |
203 | |
204 // line uses 2 pts, quad uses 3 pts | |
205 SkPoint fPts[3]; | |
206 | |
207 DPoint fB0T, fB2T; | |
208 DMatrix fXformMatrix; | |
209 DScalar fScalingFactor; | |
210 SkRect fBoundingBox; | |
211 | |
212 void init(); | |
213 | |
214 int countPoints() { | |
215 GR_STATIC_ASSERT(0 == kLine && 1 == kQuad); | |
216 return fType + 2; | |
217 } | |
218 | |
219 const SkPoint& endPt() const { | |
220 GR_STATIC_ASSERT(0 == kLine && 1 == kQuad); | |
221 return fPts[fType + 1]; | |
222 }; | |
223 }; | |
224 | |
225 typedef SkTArray<PathSegment, true> PathSegmentArray; | |
226 | |
227 void PathSegment::init() { | |
228 const DPoint b0 = DPoint::Make(this->fPts[0].x(), this->fPts[0].y()); | |
229 const DPoint b2 = DPoint::Make(this->endPt().x(), this->endPt().y()); | |
230 const DScalar b0x = b0.x(); | |
231 const DScalar b0y = b0.y(); | |
232 const DScalar b2x = b2.x(); | |
233 const DScalar b2y = b2.y(); | |
234 | |
235 this->fBoundingBox.set(this->fPts[0], this->endPt()); | |
236 | |
237 if (this->fType == PathSegment::kLine) { | |
238 this->fScalingFactor = DPoint::Distance(b0, b2); | |
239 | |
240 const DScalar cosTheta = (b2x - b0x) / this->fScalingFactor; | |
241 const DScalar sinTheta = (b2y - b0y) / this->fScalingFactor; | |
242 | |
243 this->fXformMatrix.setAll( | |
244 cosTheta, -sinTheta, 0.0, | |
245 sinTheta, cosTheta, 0.0, | |
246 -(cosTheta * b0x) - (sinTheta * b0y), (sinTheta * b0x) - (cosTheta * b0y), 1.0 | |
247 ); | |
Joel.Liang
2016/02/04 12:12:04
This matrix is a transposed matrix of general Open
| |
248 } else { | |
249 SkASSERT(this->fType == PathSegment::kQuad); | |
250 | |
251 // Calculate bounding box | |
252 const SkPoint _P1mP0 = this->fPts[1] - this->fPts[0]; | |
253 SkPoint t = _P1mP0 - this->fPts[2] + this->fPts[1]; | |
254 t.fX = _P1mP0.x() / t.x(); | |
255 t.fY = _P1mP0.y() / t.y(); | |
256 t.fX = SkScalarClampMax(t.x(), 1.0); | |
257 t.fY = SkScalarClampMax(t.y(), 1.0); | |
258 t.fX = _P1mP0.x() * t.x(); | |
259 t.fY = _P1mP0.y() * t.y(); | |
260 const SkPoint b1 = this->fPts[0] + t; | |
261 this->fBoundingBox.growToInclude(&b1, 1); | |
262 | |
263 const DScalar cp1x = this->fPts[1].x(); | |
264 const DScalar cp1y = this->fPts[1].y(); | |
265 | |
266 | |
267 const double a = pow(b0y - (2.0 * cp1y) + b2y, 2.0); | |
bsalomon
2016/02/02 18:03:48
DScalars?
Joel.Liang
2016/02/04 12:12:05
Done.
| |
268 const double h = -1.0 * (b0y - (2.0 * cp1y) + b2y) * (b0x - (2.0 * cp1x) + b2x); | |
269 const double b = pow(b0x - (2.0 * cp1x) + b2x, 2.0); | |
bsalomon
2016/02/02 18:03:48
Are pow(<foo>, 2.0)s as fast as <foo>*<foo>?
Joel.Liang
2016/02/04 12:12:04
I done a performance test for pow and <foo>*<foo>,
| |
270 const double c = (pow(b0x, 2.0) * pow(b2y, 2.0)) - (4.0 * b0x * cp1x * c p1y * b2y) | |
271 - (2.0 * b0x * b2x * b0y * b2y) + (4.0 * b0x * b2x * pow(cp1y, 2 .0)) | |
272 + (4.0 * pow(cp1x, 2.0) * b0y * b2y) - (4.0 * cp1x * b2x * b0y * cp1y) | |
273 + (pow(b2x, 2.0) * pow(b0y, 2.0)); | |
274 const double g = (b0x * b0y * b2y) - (2.0 * b0x * pow(cp1y, 2.0)) | |
275 + (2.0 * b0x * cp1y * b2y) - (b0x * pow(b2y, 2.0)) | |
276 + (2.0 * cp1x * b0y * cp1y) - (4.0 * cp1x * b0y * b2y) | |
277 + (2.0 * cp1x * cp1y * b2y) - (b2x * pow(b0y, 2.0)) | |
278 + (2.0 * b2x * b0y * cp1y) + (b2x * b0y * b2y) | |
279 - (2.0 * b2x * pow(cp1y, 2.0)); | |
280 const double f = -((pow(b0x, 2.0) * b2y) - (2.0 * b0x * cp1x * cp1y) | |
281 - (2.0 * b0x * cp1x * b2y) - (b0x * b2x * b0y) | |
282 + (4.0 * b0x * b2x * cp1y) - (b0x * b2x * b2y) | |
283 + (2.0 * pow(cp1x, 2.0) * b0y) + (2.0 * pow(cp1x, 2.0) * b2y) | |
284 - (2.0 * cp1x * b2x * b0y) - (2.0 * cp1x * b2x * cp1y) | |
285 + (pow(b2x, 2.0) * b0y)); | |
286 | |
287 | |
288 const double cosTheta = sqrt(a / (a + b)); | |
289 const double sinTheta = -1.0 * sign_of((a + b) * h) * sqrt(b / (a + b)); | |
290 | |
291 const double gDef = cosTheta * g - sinTheta * f; | |
292 const double fDef = sinTheta * g + cosTheta * f; | |
293 | |
294 | |
295 const double x0 = gDef / (a + b); | |
296 const double y0 = (1.0 / (2.0 * fDef)) * (c - (pow(gDef, 2.0) / (a + b)) ); | |
297 | |
298 | |
299 const double lambda = -1.0 * ((a + b) / (2.0 * fDef)); | |
300 this->fScalingFactor = (1.0 / lambda); | |
301 this->fScalingFactor *= this->fScalingFactor; | |
302 | |
303 | |
304 this->fXformMatrix.setAll( | |
305 lambda * cosTheta, lambda * sinTheta, 0.0, | |
306 - lambda * sinTheta, lambda * cosTheta, 0.0, | |
307 lambda * x0, lambda * y0, 1.0 | |
308 ); | |
Joel.Liang
2016/02/04 12:12:04
Transpose the matrix in the next commit.
| |
309 } | |
310 | |
311 this->fB0T = this->fXformMatrix.mapPoint(b0); | |
312 this->fB2T = this->fXformMatrix.mapPoint(b2); | |
313 } | |
314 | |
315 static void init_distances(DFData* data, int size) { | |
316 DFData* currData = data; | |
317 | |
318 for (int i = 0; i < size; ++i) { | |
319 // init distance to "far away" | |
320 currData->fDistSq = SK_DistanceFieldMagnitude * SK_DistanceFieldMagnitud e; | |
321 currData->fDeltaWindingScore = 0; | |
322 ++currData; | |
323 } | |
324 } | |
325 | |
326 static inline bool get_direction(const SkPath& path, const SkMatrix& m, | |
327 SkPathPriv::FirstDirection* dir) { | |
328 if (!SkPathPriv::CheapComputeFirstDirection(path, dir)) { | |
329 return false; | |
330 } | |
331 | |
332 // check whether m reverses the orientation | |
333 SkASSERT(!m.hasPerspective()); | |
334 SkScalar det2x2 = SkScalarMul(m.get(SkMatrix::kMScaleX), m.get(SkMatrix::kMS caleY)) - | |
335 SkScalarMul(m.get(SkMatrix::kMSkewX), m.get(SkMatrix::kMSk ewY)); | |
336 | |
337 if (det2x2 < 0) { | |
338 *dir = SkPathPriv::OppositeFirstDirection(*dir); | |
339 } | |
340 return true; | |
341 } | |
342 | |
343 static inline void add_line_to_segment(const SkPoint pts[2], | |
344 PathSegmentArray* segments) { | |
345 segments->push_back(); | |
346 segments->back().fType = PathSegment::kLine; | |
347 segments->back().fPts[0] = pts[0]; | |
348 segments->back().fPts[1] = pts[1]; | |
349 | |
350 segments->back().init(); | |
351 } | |
352 | |
353 static inline void add_quad_segment(const SkPoint pts[3], | |
354 PathSegmentArray* segments) { | |
355 if (pts[0].distanceToSqd(pts[1]) < kCloseSqd || | |
356 pts[1].distanceToSqd(pts[2]) < kCloseSqd || | |
357 is_colinear(pts)) { | |
358 if (pts[0] != pts[2]) { | |
359 SkPoint line_pts[2]; | |
360 line_pts[0] = pts[0]; | |
361 line_pts[1] = pts[2]; | |
362 add_line_to_segment(line_pts, segments); | |
363 } | |
364 } else { | |
365 segments->push_back(); | |
366 segments->back().fType = PathSegment::kQuad; | |
367 segments->back().fPts[0] = pts[0]; | |
368 segments->back().fPts[1] = pts[1]; | |
369 segments->back().fPts[2] = pts[2]; | |
370 | |
371 segments->back().init(); | |
372 } | |
373 } | |
374 | |
375 static inline void add_cubic_segments(const SkPoint pts[4], | |
376 SkPathPriv::FirstDirection dir, | |
377 PathSegmentArray* segments) { | |
378 SkSTArray<15, SkPoint, true> quads; | |
379 GrPathUtils::convertCubicToQuads(pts, SK_Scalar1, true, dir, &quads); | |
380 int count = quads.count(); | |
381 for (int q = 0; q < count; q += 3) { | |
382 add_quad_segment(&quads[q], segments); | |
383 } | |
384 } | |
385 | |
386 static float calculate_nearest_point_for_quad( | |
387 const PathSegment& segment, | |
388 const DPoint &xFormPt) { | |
389 #define THIRD float(0.33333333333f) | |
bsalomon
2016/02/02 18:03:48
If you're only using these in this function maybe
Joel.Liang
2016/02/04 12:12:05
Done.
| |
390 #define TWENTYSEVENTH float(0.037037037f) | |
391 | |
392 const float a = 0.5f - xFormPt.y(); | |
393 const float b = -0.5f * xFormPt.x(); | |
394 | |
395 const float a3 = a * a * a; | |
396 const float b2 = b * b; | |
397 | |
398 const float c = (b2 * 0.25f) + (a3 * TWENTYSEVENTH); | |
399 | |
400 if (c >= 0.f) { | |
401 const float sqrtC = sqrt(c); | |
402 const float result = (float)cbrt((-b * 0.5f) + sqrtC) + (float)cbrt((-b * 0.5f) - sqrtC); | |
403 return result; | |
404 } else { | |
405 const float cosPhi = (float)sqrt((b2 * 0.25f) * (-27.f / a3)) * ((b > 0) ? -1.f : 1.f); | |
406 const float phi = (float)acos(cosPhi); | |
407 float result; | |
408 if (xFormPt.x() > 0.f) { | |
409 result = 2.f * (float)sqrt(-a * THIRD) * (float)cos(phi * THIRD); | |
410 if (!between_closed(result, segment.fB0T.x(), segment.fB2T.x())) { | |
411 result = 2.f * (float)sqrt(-a * THIRD) * (float)cos((phi * THIRD ) + (SK_ScalarPI * 2.f * THIRD)); | |
412 } | |
413 } else { | |
414 result = 2.f * (float)sqrt(-a * THIRD) * (float)cos((phi * THIRD) + (SK_ScalarPI * 2.f * THIRD)); | |
415 if (!between_closed(result, segment.fB0T.x(), segment.fB2T.x())) { | |
416 result = 2.f * (float)sqrt(-a * THIRD) * (float)cos(phi * THIRD) ; | |
417 } | |
418 } | |
419 return result; | |
420 } | |
421 } | |
422 | |
423 struct RowData | |
bsalomon
2016/02/02 18:03:48
Some comments on this struct might be useful (e.g.
Joel.Liang
2016/02/04 12:12:05
Done. I have changed some member variable name and
| |
424 { | |
bsalomon
2016/02/02 18:03:48
{ on prev line
Joel.Liang
2016/02/04 12:12:04
Done.
| |
425 enum IntersectionType { | |
426 kNoIntersection, | |
427 kVerticalLine, | |
428 kTangentLine, | |
429 kTwoPointsIntersect | |
430 } fIntersectionType; | |
431 int fSignB0B2; | |
432 double fPow2x; | |
bsalomon
2016/02/02 18:03:48
DScalars?
Joel.Liang
2016/02/04 12:12:04
Done.
| |
433 double fIntersectionPoint1; | |
434 double fIntersectionPoint2; | |
435 }; | |
436 | |
437 void precomputation_for_row( | |
438 RowData *rowData, | |
439 const PathSegment& segment, | |
440 const SkPoint& pointLeft, | |
441 const SkPoint& pointRight | |
442 ) { | |
443 if (segment.fType != PathSegment::kQuad) { | |
444 return; | |
445 } | |
446 | |
447 const DPoint& xFormPtLeft = segment.fXformMatrix.mapPoint(pointLeft); | |
448 const DPoint& xFormPtRight = segment.fXformMatrix.mapPoint(pointRight);; | |
449 | |
450 rowData->fSignB0B2 = sign_of(segment.fB2T.x() - segment.fB0T.x()); | |
451 | |
452 const double x1 = xFormPtLeft.x(); | |
bsalomon
2016/02/02 18:03:48
DScalars?
Joel.Liang
2016/02/04 12:12:04
Done.
| |
453 const double y1 = xFormPtLeft.y(); | |
454 const double x2 = xFormPtRight.x(); | |
455 const double y2 = xFormPtRight.y(); | |
456 | |
457 if (nearly_equal(x1, x2)) { | |
458 rowData->fIntersectionType = RowData::kVerticalLine; | |
459 rowData->fPow2x = pow(x1, 2.0); | |
460 return; | |
461 } | |
462 | |
463 // Line y = mx + b | |
464 const double m = (y2 - y1) / (x2 - x1); | |
465 const double b = -m * x1 + y1; | |
466 | |
467 const double c = pow(m, 2.0) + 4.0 * b; | |
468 | |
469 if (nearly_zero(c, 4.0 * kNearlyZero * kNearlyZero)) { | |
470 rowData->fIntersectionType = RowData::kTangentLine; | |
471 rowData->fIntersectionPoint1 = m / 2.0; | |
472 rowData->fIntersectionPoint2 = m / 2.0; | |
473 } else if (c < 0.0) { | |
474 rowData->fIntersectionType = RowData::kNoIntersection; | |
475 return; | |
476 } else { | |
477 rowData->fIntersectionType = RowData::kTwoPointsIntersect; | |
478 const double d = sqrt(c); | |
479 rowData->fIntersectionPoint1 = (m + d) / 2.0; | |
480 rowData->fIntersectionPoint2 = (m - d) / 2.0; | |
481 } | |
482 } | |
483 | |
484 SegSide calculate_side_of_quad( | |
485 const PathSegment& segment, | |
486 const SkPoint& point, | |
487 const DPoint& xFormPt, | |
488 const RowData& rowData) { | |
489 SegSide side = kNA_Side; | |
490 | |
491 if (RowData::kVerticalLine == rowData.fIntersectionType) { | |
492 side = (SegSide)(int)(sign_of(rowData.fPow2x - xFormPt.y()) * rowData.fS ignB0B2); | |
493 } | |
494 else if (RowData::kTwoPointsIntersect == rowData.fIntersectionType || | |
495 RowData::kTangentLine == rowData.fIntersectionType) { | |
496 const double p1 = rowData.fIntersectionPoint1; | |
497 const double p2 = rowData.fIntersectionPoint2; | |
498 | |
499 int signP1 = sign_of(p1 - xFormPt.x()); | |
500 if (between_closed(p1, segment.fB0T.x(), segment.fB2T.x())) { | |
501 side = (SegSide)((-signP1) * rowData.fSignB0B2); | |
502 } | |
503 if (between_closed(p2, segment.fB0T.x(), segment.fB2T.x())) { | |
504 int signP2 = sign_of(p2 - xFormPt.x()); | |
505 if (side == kNA_Side || signP2 == 1) { | |
506 side = (SegSide)(signP2 * rowData.fSignB0B2); | |
507 } | |
508 } | |
509 | |
510 // The scanline is the tangent line of current quadratic segment. | |
511 if (RowData::kTangentLine == rowData.fIntersectionType) { | |
512 // The path start at the tangent point. | |
513 if (nearly_equal(p1, segment.fB0T.x())) { | |
514 side = (SegSide)(side * (-signP1) * rowData.fSignB0B2); | |
515 } | |
516 | |
517 // The path end at the tangent point. | |
518 if (nearly_equal(p1, segment.fB2T.x())) { | |
519 side = (SegSide)(side * signP1 * rowData.fSignB0B2); | |
520 } | |
521 } | |
522 } | |
523 | |
524 return side; | |
525 } | |
526 | |
527 static float distance_to_segment(const SkPoint& point, | |
528 const PathSegment& segment, | |
529 const RowData& rowData, | |
530 SegSide* side) { | |
531 SkASSERT(side); | |
532 | |
533 const DPoint xformPt = segment.fXformMatrix.mapPoint(point); | |
534 | |
535 if (segment.fType == PathSegment::kLine) { | |
536 float result = SK_DistanceFieldPad * SK_DistanceFieldPad; | |
537 | |
538 if (between_closed(xformPt.x(), segment.fB0T.x(), segment.fB2T.x())) { | |
539 result = pow(xformPt.y(), 2.0); | |
540 } else if (xformPt.x() < segment.fB0T.x()) { | |
541 result = (pow(xformPt.x(), 2.0) + pow(xformPt.y(), 2.0)); | |
542 } else { | |
543 result = (pow((xformPt.x() - segment.fB2T.x()), 2.0) | |
544 + pow(xformPt.y(), 2.0)); | |
545 } | |
546 | |
547 if (between_closed_open(point.y(), segment.fBoundingBox.top(), | |
548 segment.fBoundingBox.bottom())) { | |
549 *side = (SegSide)(int)sign_of(-xformPt.y()); | |
550 } else { | |
551 *side = kNA_Side; | |
552 } | |
553 return result; | |
554 } else { | |
555 SkASSERT(segment.fType == PathSegment::kQuad); | |
556 | |
557 const DPoint& xformPt = segment.fXformMatrix.mapPoint(point); | |
558 const float nearestPoint = calculate_nearest_point_for_quad(segment, xfo rmPt); | |
559 | |
560 float dist; | |
561 | |
562 if (between_closed(nearestPoint, segment.fB0T.x(), segment.fB2T.x())) { | |
563 DPoint x = DPoint::Make(nearestPoint, nearestPoint * nearestPoint); | |
564 dist = xformPt.distanceToSqd(x); | |
565 } else { | |
566 const float distToB0T = xformPt.distanceToSqd(segment.fB0T); | |
567 const float distToB2T = xformPt.distanceToSqd(segment.fB2T); | |
568 | |
569 if (distToB0T < distToB2T) { | |
570 dist = distToB0T; | |
571 } else { | |
572 dist = distToB2T; | |
573 } | |
574 } | |
575 | |
576 if (between_closed_open(point.y(), segment.fBoundingBox.top(), | |
577 segment.fBoundingBox.bottom())) { | |
578 *side = calculate_side_of_quad(segment, point, xformPt, rowData); | |
579 } else { | |
580 *side = kNA_Side; | |
581 } | |
582 | |
583 return dist * segment.fScalingFactor; | |
584 } | |
585 } | |
586 | |
587 static void calculate_distance_field_data(PathSegmentArray* segments, | |
588 DFData* dataPtr, | |
589 int width, int height) { | |
590 int count = segments->count(); | |
591 for (int a = 0; a < count; ++a) { | |
592 PathSegment& segment = (*segments)[a]; | |
593 const SkRect& segBB = segment.fBoundingBox.makeOutset( | |
594 SK_DistanceFieldPad, SK_DistanceFieldPad); | |
595 int startColumn = segBB.left(); | |
596 int endColumn = segBB.right() + 1; | |
597 | |
598 int startRow = segBB.top(); | |
599 int endRow = segBB.bottom() + 1; | |
600 | |
601 SkASSERT((startColumn >= 0) && "StartColumn < 0!"); | |
602 SkASSERT((endColumn <= width) && "endColumn > width!"); | |
603 SkASSERT((startRow >= 0) && "StartRow < 0!"); | |
604 SkASSERT((endRow <= height) && "EndRow > height!"); | |
605 | |
606 for (int row = startRow; row < endRow; ++row) { | |
607 SegSide prevSide = kNA_Side; | |
608 const float pY = row + 0.5f; | |
609 RowData rowData; | |
610 | |
611 const SkPoint pointLeft = SkPoint::Make(startColumn, pY); | |
612 const SkPoint pointRight = SkPoint::Make(endColumn, pY); | |
613 | |
614 precomputation_for_row(&rowData, segment, pointLeft, pointRight); | |
615 | |
616 for (int col = startColumn; col < endColumn; ++col) { | |
617 int idx = (row * width) + col; | |
618 | |
619 const float pX = col + 0.5f; | |
620 const SkPoint point = SkPoint::Make(pX, pY); | |
621 | |
622 const float distSq = dataPtr[idx].fDistSq; | |
623 int dilation = distSq < 1.5 * 1.5 ? 1 : | |
624 distSq < 2.5 * 2.5 ? 2 : | |
625 distSq < 3.5 * 3.5 ? 3 : SK_DistanceFieldPad; | |
626 if (dilation > SK_DistanceFieldPad) { | |
627 dilation = SK_DistanceFieldPad; | |
628 } | |
629 | |
630 // Optimisation for not calculating some points. | |
631 if (dilation != SK_DistanceFieldPad | |
632 && !segment.fBoundingBox.roundOut() | |
bsalomon
2016/02/02 18:03:48
&& on prev line, looks like everything following &
Joel.Liang
2016/02/04 12:12:04
Not fits within 100 col. So I split it into 2 line
| |
633 .makeOutset(dilation, dilation) | |
634 .contains(col, row)) { | |
635 continue; | |
636 } | |
637 | |
638 SegSide side = kNA_Side; | |
639 int deltaWindingScore = 0; | |
640 float currDistSq = | |
641 distance_to_segment(point, segment, rowData, &side); | |
642 if (prevSide == kLeft_Side && side == kRight_Side) { | |
643 deltaWindingScore = -1; | |
644 } else if (prevSide == kRight_Side && side == kLeft_Side) { | |
645 deltaWindingScore = 1; | |
646 } | |
647 prevSide = side; | |
648 | |
649 if (currDistSq < distSq) { | |
650 dataPtr[idx].fDistSq = currDistSq; | |
651 } | |
652 dataPtr[idx].fDeltaWindingScore += deltaWindingScore; | |
653 } | |
654 } | |
655 } | |
656 } | |
657 | |
658 static unsigned char pack_distance_field_val(float dist, float distanceMagnitude ) { | |
659 if (dist <= -(distanceMagnitude * (1.0f - 1.0f / 128.0f))) { | |
660 return 255; | |
661 } else if (dist > distanceMagnitude) { | |
662 return 0; | |
663 } else { | |
664 return (unsigned char)((distanceMagnitude - dist) * 128.0f / distanceMag nitude); | |
665 } | |
666 } | |
667 | |
668 bool GrGenerateDistanceFieldFromPath(unsigned char* distanceField, | |
669 const SkPath& path, const SkMatrix& drawMat rix, | |
670 int width, int height, size_t rowBytes) { | |
671 SkASSERT(distanceField); | |
672 | |
673 SkMatrix m = drawMatrix; | |
674 m.postTranslate(SK_DistanceFieldPad, SK_DistanceFieldPad); | |
675 | |
676 // create temp data | |
677 size_t dataSize = width * height * sizeof(DFData); | |
678 SkAutoSMalloc<1024> dfStorage(dataSize); | |
679 DFData* dataPtr = (DFData*) dfStorage.get(); | |
680 | |
681 // create initial distance data | |
682 init_distances(dataPtr, width * height); | |
683 | |
684 SkPath::Iter iter(path, true); | |
685 SkSTArray<15, PathSegment, true> segments; | |
686 | |
687 SkPathPriv::FirstDirection dir; | |
688 // get_direction can fail for some degenerate paths. | |
689 if (path.getSegmentMasks() & SkPath::kCubic_SegmentMask && | |
690 !get_direction(path, m, &dir)) { | |
691 return false; | |
692 } | |
693 | |
694 for (;;) { | |
695 SkPoint pts[4]; | |
696 SkPath::Verb verb = iter.next(pts); | |
697 switch (verb) { | |
698 case SkPath::kMove_Verb: | |
699 // m.mapPoints(pts, 1); | |
700 break; | |
701 case SkPath::kLine_Verb: { | |
702 m.mapPoints(pts, 2); | |
703 add_line_to_segment(pts, &segments); | |
704 break; | |
705 } | |
706 case SkPath::kQuad_Verb: | |
707 m.mapPoints(pts, 3); | |
708 add_quad_segment(pts, &segments); | |
709 break; | |
710 case SkPath::kConic_Verb: { | |
711 m.mapPoints(pts, 3); | |
712 SkScalar weight = iter.conicWeight(); | |
713 SkAutoConicToQuads converter; | |
714 const SkPoint* quadPts = converter.computeQuads(pts, weight, 0.5 f); | |
715 for (int i = 0; i < converter.countQuads(); ++i) { | |
716 add_quad_segment(quadPts + 2*i, &segments); | |
717 } | |
718 break; | |
719 } | |
720 case SkPath::kCubic_Verb: { | |
721 m.mapPoints(pts, 4); | |
722 add_cubic_segments(pts, dir, &segments); | |
723 break; | |
724 }; | |
725 default: | |
726 break; | |
727 } | |
728 if (verb == SkPath::kDone_Verb) { | |
729 break; | |
730 } | |
731 } | |
732 | |
733 calculate_distance_field_data(&segments, dataPtr, width, height); | |
734 | |
735 for (int row = 0; row < height; ++row) { | |
736 int windingNumber = 0; // Winding number start from zero for each scanli ne | |
737 for (int col = 0; col < width; ++col) { | |
738 int idx = (row * width) + col; | |
739 windingNumber += dataPtr[idx].fDeltaWindingScore; | |
740 | |
741 enum DFSign { | |
742 kInside = -1, | |
743 kOutside = 1 | |
744 } dfSign; | |
745 | |
746 if (path.getFillType() == SkPath::kWinding_FillType) { | |
747 dfSign = windingNumber ? kInside : kOutside; | |
748 } else if (path.getFillType() == SkPath::kInverseWinding_FillType) { | |
749 dfSign = windingNumber ? kOutside : kInside; | |
750 } else if (path.getFillType() == SkPath::kEvenOdd_FillType) { | |
751 dfSign = (windingNumber % 2) ? kInside : kOutside; | |
752 } else { | |
753 SkASSERT(path.getFillType() == SkPath::kInverseEvenOdd_FillType) ; | |
754 dfSign = (windingNumber % 2) ? kOutside : kInside; | |
755 } | |
756 | |
757 // The winding number at the end of a scanline should be zero. | |
758 if ((col == width - 1) && (windingNumber != 0)) { | |
759 SkASSERT(0 && "Winding number should be zero at the end of a sca n line."); | |
760 return false; | |
761 } | |
762 | |
763 const float miniDist = sqrt(dataPtr[idx].fDistSq); | |
764 const float dist = dfSign * miniDist; | |
765 | |
766 unsigned char pixelVal = | |
767 pack_distance_field_val(dist, (float)SK_DistanceFieldMagnitude); | |
768 | |
769 distanceField[(row * rowBytes) + col] = pixelVal; | |
770 } | |
771 } | |
772 return true; | |
773 } | |
OLD | NEW |