1 /*
2  * Copyright 2011 Google Inc.
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #include "include/core/SkPoint3.h"
9 #include "include/private/SkTemplates.h"
10 #include "src/core/SkGeometry.h"
11 #include "src/core/SkMatrixPriv.h"
12 #include "src/core/SkPointPriv.h"
13 #include "src/core/SkRectPriv.h"
14 #include "src/core/SkStroke.h"
15 #include "src/gpu/GrAuditTrail.h"
16 #include "src/gpu/GrBuffer.h"
17 #include "src/gpu/GrCaps.h"
18 #include "src/gpu/GrClip.h"
19 #include "src/gpu/GrDefaultGeoProcFactory.h"
20 #include "src/gpu/GrDrawOpTest.h"
21 #include "src/gpu/GrOpFlushState.h"
22 #include "src/gpu/GrProcessor.h"
23 #include "src/gpu/GrResourceProvider.h"
24 #include "src/gpu/GrStyle.h"
25 #include "src/gpu/effects/GrBezierEffect.h"
26 #include "src/gpu/geometry/GrPathUtils.h"
27 #include "src/gpu/geometry/GrShape.h"
28 #include "src/gpu/ops/GrAAHairLinePathRenderer.h"
29 #include "src/gpu/ops/GrMeshDrawOp.h"
30 #include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
31 
32 #define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true>
33 
34 // quadratics are rendered as 5-sided polys in order to bound the
35 // AA stroke around the center-curve. See comments in push_quad_index_buffer and
36 // bloat_quad. Quadratics and conics share an index buffer
37 
38 // lines are rendered as:
39 //      *______________*
40 //      |\ -_______   /|
41 //      | \        \ / |
42 //      |  *--------*  |
43 //      | /  ______/ \ |
44 //      */_-__________\*
45 // For: 6 vertices and 18 indices (for 6 triangles)
46 
47 // Each quadratic is rendered as a five sided polygon. This poly bounds
48 // the quadratic's bounding triangle but has been expanded so that the
49 // 1-pixel wide area around the curve is inside the poly.
50 // If a,b,c are the original control points then the poly a0,b0,c0,c1,a1
51 // that is rendered would look like this:
52 //              b0
53 //              b
54 //
55 //     a0              c0
56 //      a            c
57 //       a1       c1
58 // Each is drawn as three triangles ((a0,a1,b0), (b0,c1,c0), (a1,c1,b0))
59 // specified by these 9 indices:
60 static const uint16_t kQuadIdxBufPattern[] = {
61     0, 1, 2,
62     2, 4, 3,
63     1, 4, 2
64 };
65 
66 static const int kIdxsPerQuad = SK_ARRAY_COUNT(kQuadIdxBufPattern);
67 static const int kQuadNumVertices = 5;
68 static const int kQuadsNumInIdxBuffer = 256;
69 GR_DECLARE_STATIC_UNIQUE_KEY(gQuadsIndexBufferKey);
70 
get_quads_index_buffer(GrResourceProvider * resourceProvider)71 static sk_sp<const GrBuffer> get_quads_index_buffer(GrResourceProvider* resourceProvider) {
72     GR_DEFINE_STATIC_UNIQUE_KEY(gQuadsIndexBufferKey);
73     return resourceProvider->findOrCreatePatternedIndexBuffer(
74         kQuadIdxBufPattern, kIdxsPerQuad, kQuadsNumInIdxBuffer, kQuadNumVertices,
75         gQuadsIndexBufferKey);
76 }
77 
78 
79 // Each line segment is rendered as two quads and two triangles.
80 // p0 and p1 have alpha = 1 while all other points have alpha = 0.
81 // The four external points are offset 1 pixel perpendicular to the
82 // line and half a pixel parallel to the line.
83 //
84 // p4                  p5
85 //      p0         p1
86 // p2                  p3
87 //
88 // Each is drawn as six triangles specified by these 18 indices:
89 
90 static const uint16_t kLineSegIdxBufPattern[] = {
91     0, 1, 3,
92     0, 3, 2,
93     0, 4, 5,
94     0, 5, 1,
95     0, 2, 4,
96     1, 5, 3
97 };
98 
99 static const int kIdxsPerLineSeg = SK_ARRAY_COUNT(kLineSegIdxBufPattern);
100 static const int kLineSegNumVertices = 6;
101 static const int kLineSegsNumInIdxBuffer = 256;
102 
103 GR_DECLARE_STATIC_UNIQUE_KEY(gLinesIndexBufferKey);
104 
get_lines_index_buffer(GrResourceProvider * resourceProvider)105 static sk_sp<const GrBuffer> get_lines_index_buffer(GrResourceProvider* resourceProvider) {
106     GR_DEFINE_STATIC_UNIQUE_KEY(gLinesIndexBufferKey);
107     return resourceProvider->findOrCreatePatternedIndexBuffer(
108         kLineSegIdxBufPattern, kIdxsPerLineSeg,  kLineSegsNumInIdxBuffer, kLineSegNumVertices,
109         gLinesIndexBufferKey);
110 }
111 
112 // Takes 178th time of logf on Z600 / VC2010
get_float_exp(float x)113 static int get_float_exp(float x) {
114     GR_STATIC_ASSERT(sizeof(int) == sizeof(float));
115 #ifdef SK_DEBUG
116     static bool tested;
117     if (!tested) {
118         tested = true;
119         SkASSERT(get_float_exp(0.25f) == -2);
120         SkASSERT(get_float_exp(0.3f) == -2);
121         SkASSERT(get_float_exp(0.5f) == -1);
122         SkASSERT(get_float_exp(1.f) == 0);
123         SkASSERT(get_float_exp(2.f) == 1);
124         SkASSERT(get_float_exp(2.5f) == 1);
125         SkASSERT(get_float_exp(8.f) == 3);
126         SkASSERT(get_float_exp(100.f) == 6);
127         SkASSERT(get_float_exp(1000.f) == 9);
128         SkASSERT(get_float_exp(1024.f) == 10);
129         SkASSERT(get_float_exp(3000000.f) == 21);
130     }
131 #endif
132     const int* iptr = (const int*)&x;
133     return (((*iptr) & 0x7f800000) >> 23) - 127;
134 }
135 
136 // Uses the max curvature function for quads to estimate
137 // where to chop the conic. If the max curvature is not
138 // found along the curve segment it will return 1 and
139 // dst[0] is the original conic. If it returns 2 the dst[0]
140 // and dst[1] are the two new conics.
split_conic(const SkPoint src[3],SkConic dst[2],const SkScalar weight)141 static int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
142     SkScalar t = SkFindQuadMaxCurvature(src);
143     if (t == 0 || t == 1) {
144         if (dst) {
145             dst[0].set(src, weight);
146         }
147         return 1;
148     } else {
149         if (dst) {
150             SkConic conic;
151             conic.set(src, weight);
152             if (!conic.chopAt(t, dst)) {
153                 dst[0].set(src, weight);
154                 return 1;
155             }
156         }
157         return 2;
158     }
159 }
160 
161 // Calls split_conic on the entire conic and then once more on each subsection.
162 // Most cases will result in either 1 conic (chop point is not within t range)
163 // or 3 points (split once and then one subsection is split again).
chop_conic(const SkPoint src[3],SkConic dst[4],const SkScalar weight)164 static int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) {
165     SkConic dstTemp[2];
166     int conicCnt = split_conic(src, dstTemp, weight);
167     if (2 == conicCnt) {
168         int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW);
169         conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW);
170     } else {
171         dst[0] = dstTemp[0];
172     }
173     return conicCnt;
174 }
175 
176 // returns 0 if quad/conic is degen or close to it
177 // in this case approx the path with lines
178 // otherwise returns 1
is_degen_quad_or_conic(const SkPoint p[3],SkScalar * dsqd)179 static int is_degen_quad_or_conic(const SkPoint p[3], SkScalar* dsqd) {
180     static const SkScalar gDegenerateToLineTol = GrPathUtils::kDefaultTolerance;
181     static const SkScalar gDegenerateToLineTolSqd =
182         gDegenerateToLineTol * gDegenerateToLineTol;
183 
184     if (SkPointPriv::DistanceToSqd(p[0], p[1]) < gDegenerateToLineTolSqd ||
185         SkPointPriv::DistanceToSqd(p[1], p[2]) < gDegenerateToLineTolSqd) {
186         return 1;
187     }
188 
189     *dsqd = SkPointPriv::DistanceToLineBetweenSqd(p[1], p[0], p[2]);
190     if (*dsqd < gDegenerateToLineTolSqd) {
191         return 1;
192     }
193 
194     if (SkPointPriv::DistanceToLineBetweenSqd(p[2], p[1], p[0]) < gDegenerateToLineTolSqd) {
195         return 1;
196     }
197     return 0;
198 }
199 
is_degen_quad_or_conic(const SkPoint p[3])200 static int is_degen_quad_or_conic(const SkPoint p[3]) {
201     SkScalar dsqd;
202     return is_degen_quad_or_conic(p, &dsqd);
203 }
204 
205 // we subdivide the quads to avoid huge overfill
206 // if it returns -1 then should be drawn as lines
num_quad_subdivs(const SkPoint p[3])207 static int num_quad_subdivs(const SkPoint p[3]) {
208     SkScalar dsqd;
209     if (is_degen_quad_or_conic(p, &dsqd)) {
210         return -1;
211     }
212 
213     // tolerance of triangle height in pixels
214     // tuned on windows  Quadro FX 380 / Z600
215     // trade off of fill vs cpu time on verts
216     // maybe different when do this using gpu (geo or tess shaders)
217     static const SkScalar gSubdivTol = 175 * SK_Scalar1;
218 
219     if (dsqd <= gSubdivTol * gSubdivTol) {
220         return 0;
221     } else {
222         static const int kMaxSub = 4;
223         // subdividing the quad reduces d by 4. so we want x = log4(d/tol)
224         // = log4(d*d/tol*tol)/2
225         // = log2(d*d/tol*tol)
226 
227         // +1 since we're ignoring the mantissa contribution.
228         int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
229         log = SkTMin(SkTMax(0, log), kMaxSub);
230         return log;
231     }
232 }
233 
234 /**
235  * Generates the lines and quads to be rendered. Lines are always recorded in
236  * device space. We will do a device space bloat to account for the 1pixel
237  * thickness.
238  * Quads are recorded in device space unless m contains
239  * perspective, then in they are in src space. We do this because we will
240  * subdivide large quads to reduce over-fill. This subdivision has to be
241  * performed before applying the perspective matrix.
242  */
gather_lines_and_quads(const SkPath & path,const SkMatrix & m,const SkIRect & devClipBounds,SkScalar capLength,bool convertConicsToQuads,GrAAHairLinePathRenderer::PtArray * lines,GrAAHairLinePathRenderer::PtArray * quads,GrAAHairLinePathRenderer::PtArray * conics,GrAAHairLinePathRenderer::IntArray * quadSubdivCnts,GrAAHairLinePathRenderer::FloatArray * conicWeights)243 static int gather_lines_and_quads(const SkPath& path,
244                                   const SkMatrix& m,
245                                   const SkIRect& devClipBounds,
246                                   SkScalar capLength,
247                                   bool convertConicsToQuads,
248                                   GrAAHairLinePathRenderer::PtArray* lines,
249                                   GrAAHairLinePathRenderer::PtArray* quads,
250                                   GrAAHairLinePathRenderer::PtArray* conics,
251                                   GrAAHairLinePathRenderer::IntArray* quadSubdivCnts,
252                                   GrAAHairLinePathRenderer::FloatArray* conicWeights) {
253     SkPath::Iter iter(path, false);
254 
255     int totalQuadCount = 0;
256     SkRect bounds;
257     SkIRect ibounds;
258 
259     bool persp = m.hasPerspective();
260 
261     // Whenever a degenerate, zero-length contour is encountered, this code will insert a
262     // 'capLength' x-aligned line segment. Since this is rendering hairlines it is hoped this will
263     // suffice for AA square & circle capping.
264     int verbsInContour = 0; // Does not count moves
265     bool seenZeroLengthVerb = false;
266     SkPoint zeroVerbPt;
267 
268     // Adds a quad that has already been chopped to the list and checks for quads that are close to
269     // lines. Also does a bounding box check. It takes points that are in src space and device
270     // space. The src points are only required if the view matrix has perspective.
271     auto addChoppedQuad = [&](const SkPoint srcPts[3], const SkPoint devPts[4],
272                               bool isContourStart) {
273         SkRect bounds;
274         SkIRect ibounds;
275         bounds.setBounds(devPts, 3);
276         bounds.outset(SK_Scalar1, SK_Scalar1);
277         bounds.roundOut(&ibounds);
278         // We only need the src space space pts when not in perspective.
279         SkASSERT(srcPts || !persp);
280         if (SkIRect::Intersects(devClipBounds, ibounds)) {
281             int subdiv = num_quad_subdivs(devPts);
282             SkASSERT(subdiv >= -1);
283             if (-1 == subdiv) {
284                 SkPoint* pts = lines->push_back_n(4);
285                 pts[0] = devPts[0];
286                 pts[1] = devPts[1];
287                 pts[2] = devPts[1];
288                 pts[3] = devPts[2];
289                 if (isContourStart && pts[0] == pts[1] && pts[2] == pts[3]) {
290                     seenZeroLengthVerb = true;
291                     zeroVerbPt = pts[0];
292                 }
293             } else {
294                 // when in perspective keep quads in src space
295                 const SkPoint* qPts = persp ? srcPts : devPts;
296                 SkPoint* pts = quads->push_back_n(3);
297                 pts[0] = qPts[0];
298                 pts[1] = qPts[1];
299                 pts[2] = qPts[2];
300                 quadSubdivCnts->push_back() = subdiv;
301                 totalQuadCount += 1 << subdiv;
302             }
303         }
304     };
305 
306     // Applies the view matrix to quad src points and calls the above helper.
307     auto addSrcChoppedQuad = [&](const SkPoint srcSpaceQuadPts[3], bool isContourStart) {
308         SkPoint devPts[3];
309         m.mapPoints(devPts, srcSpaceQuadPts, 3);
310         addChoppedQuad(srcSpaceQuadPts, devPts, isContourStart);
311     };
312 
313     for (;;) {
314         SkPoint pathPts[4];
315         SkPath::Verb verb = iter.next(pathPts);
316         switch (verb) {
317             case SkPath::kConic_Verb:
318                 if (convertConicsToQuads) {
319                     SkScalar weight = iter.conicWeight();
320                     SkAutoConicToQuads converter;
321                     const SkPoint* quadPts = converter.computeQuads(pathPts, weight, 0.25f);
322                     for (int i = 0; i < converter.countQuads(); ++i) {
323                         addSrcChoppedQuad(quadPts + 2 * i, !verbsInContour && 0 == i);
324                     }
325                 } else {
326                     SkConic dst[4];
327                     // We chop the conics to create tighter clipping to hide error
328                     // that appears near max curvature of very thin conics. Thin
329                     // hyperbolas with high weight still show error.
330                     int conicCnt = chop_conic(pathPts, dst, iter.conicWeight());
331                     for (int i = 0; i < conicCnt; ++i) {
332                         SkPoint devPts[4];
333                         SkPoint* chopPnts = dst[i].fPts;
334                         m.mapPoints(devPts, chopPnts, 3);
335                         bounds.setBounds(devPts, 3);
336                         bounds.outset(SK_Scalar1, SK_Scalar1);
337                         bounds.roundOut(&ibounds);
338                         if (SkIRect::Intersects(devClipBounds, ibounds)) {
339                             if (is_degen_quad_or_conic(devPts)) {
340                                 SkPoint* pts = lines->push_back_n(4);
341                                 pts[0] = devPts[0];
342                                 pts[1] = devPts[1];
343                                 pts[2] = devPts[1];
344                                 pts[3] = devPts[2];
345                                 if (verbsInContour == 0 && i == 0 && pts[0] == pts[1] &&
346                                     pts[2] == pts[3]) {
347                                     seenZeroLengthVerb = true;
348                                     zeroVerbPt = pts[0];
349                                 }
350                             } else {
351                                 // when in perspective keep conics in src space
352                                 SkPoint* cPts = persp ? chopPnts : devPts;
353                                 SkPoint* pts = conics->push_back_n(3);
354                                 pts[0] = cPts[0];
355                                 pts[1] = cPts[1];
356                                 pts[2] = cPts[2];
357                                 conicWeights->push_back() = dst[i].fW;
358                             }
359                         }
360                     }
361                 }
362                 verbsInContour++;
363                 break;
364             case SkPath::kMove_Verb:
365                 // New contour (and last one was unclosed). If it was just a zero length drawing
366                 // operation, and we're supposed to draw caps, then add a tiny line.
367                 if (seenZeroLengthVerb && verbsInContour == 1 && capLength > 0) {
368                     SkPoint* pts = lines->push_back_n(2);
369                     pts[0] = SkPoint::Make(zeroVerbPt.fX - capLength, zeroVerbPt.fY);
370                     pts[1] = SkPoint::Make(zeroVerbPt.fX + capLength, zeroVerbPt.fY);
371                 }
372                 verbsInContour = 0;
373                 seenZeroLengthVerb = false;
374                 break;
375             case SkPath::kLine_Verb: {
376                 SkPoint devPts[2];
377                 m.mapPoints(devPts, pathPts, 2);
378                 bounds.setBounds(devPts, 2);
379                 bounds.outset(SK_Scalar1, SK_Scalar1);
380                 bounds.roundOut(&ibounds);
381                 if (SkIRect::Intersects(devClipBounds, ibounds)) {
382                     SkPoint* pts = lines->push_back_n(2);
383                     pts[0] = devPts[0];
384                     pts[1] = devPts[1];
385                     if (verbsInContour == 0 && pts[0] == pts[1]) {
386                         seenZeroLengthVerb = true;
387                         zeroVerbPt = pts[0];
388                     }
389                 }
390                 verbsInContour++;
391                 break;
392             }
393             case SkPath::kQuad_Verb: {
394                 SkPoint choppedPts[5];
395                 // Chopping the quad helps when the quad is either degenerate or nearly degenerate.
396                 // When it is degenerate it allows the approximation with lines to work since the
397                 // chop point (if there is one) will be at the parabola's vertex. In the nearly
398                 // degenerate the QuadUVMatrix computed for the points is almost singular which
399                 // can cause rendering artifacts.
400                 int n = SkChopQuadAtMaxCurvature(pathPts, choppedPts);
401                 for (int i = 0; i < n; ++i) {
402                     addSrcChoppedQuad(choppedPts + i * 2, !verbsInContour && 0 == i);
403                 }
404                 verbsInContour++;
405                 break;
406             }
407             case SkPath::kCubic_Verb: {
408                 SkPoint devPts[4];
409                 m.mapPoints(devPts, pathPts, 4);
410                 bounds.setBounds(devPts, 4);
411                 bounds.outset(SK_Scalar1, SK_Scalar1);
412                 bounds.roundOut(&ibounds);
413                 if (SkIRect::Intersects(devClipBounds, ibounds)) {
414                     PREALLOC_PTARRAY(32) q;
415                     // We convert cubics to quadratics (for now).
416                     // In perspective have to do conversion in src space.
417                     if (persp) {
418                         SkScalar tolScale =
419                             GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m, path.getBounds());
420                         GrPathUtils::convertCubicToQuads(pathPts, tolScale, &q);
421                     } else {
422                         GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, &q);
423                     }
424                     for (int i = 0; i < q.count(); i += 3) {
425                         if (persp) {
426                             addSrcChoppedQuad(&q[i], !verbsInContour && 0 == i);
427                         } else {
428                             addChoppedQuad(nullptr, &q[i], !verbsInContour && 0 == i);
429                         }
430                     }
431                 }
432                 verbsInContour++;
433                 break;
434             }
435             case SkPath::kClose_Verb:
436                 // Contour is closed, so we don't need to grow the starting line, unless it's
437                 // *just* a zero length subpath. (SVG Spec 11.4, 'stroke').
438                 if (capLength > 0) {
439                     if (seenZeroLengthVerb && verbsInContour == 1) {
440                         SkPoint* pts = lines->push_back_n(2);
441                         pts[0] = SkPoint::Make(zeroVerbPt.fX - capLength, zeroVerbPt.fY);
442                         pts[1] = SkPoint::Make(zeroVerbPt.fX + capLength, zeroVerbPt.fY);
443                     } else if (verbsInContour == 0) {
444                         // Contour was (moveTo, close). Add a line.
445                         SkPoint devPts[2];
446                         m.mapPoints(devPts, pathPts, 1);
447                         devPts[1] = devPts[0];
448                         bounds.setBounds(devPts, 2);
449                         bounds.outset(SK_Scalar1, SK_Scalar1);
450                         bounds.roundOut(&ibounds);
451                         if (SkIRect::Intersects(devClipBounds, ibounds)) {
452                             SkPoint* pts = lines->push_back_n(2);
453                             pts[0] = SkPoint::Make(devPts[0].fX - capLength, devPts[0].fY);
454                             pts[1] = SkPoint::Make(devPts[1].fX + capLength, devPts[1].fY);
455                         }
456                     }
457                 }
458                 break;
459             case SkPath::kDone_Verb:
460                 if (seenZeroLengthVerb && verbsInContour == 1 && capLength > 0) {
461                     // Path ended with a dangling (moveTo, line|quad|etc). If the final verb is
462                     // degenerate, we need to draw a line.
463                     SkPoint* pts = lines->push_back_n(2);
464                     pts[0] = SkPoint::Make(zeroVerbPt.fX - capLength, zeroVerbPt.fY);
465                     pts[1] = SkPoint::Make(zeroVerbPt.fX + capLength, zeroVerbPt.fY);
466                 }
467                 return totalQuadCount;
468         }
469     }
470 }
471 
472 struct LineVertex {
473     SkPoint fPos;
474     float fCoverage;
475 };
476 
477 struct BezierVertex {
478     SkPoint fPos;
479     union {
480         struct {
481             SkScalar fKLM[3];
482         } fConic;
483         SkVector   fQuadCoord;
484         struct {
485             SkScalar fBogus[4];
486         };
487     };
488 };
489 
490 GR_STATIC_ASSERT(sizeof(BezierVertex) == 3 * sizeof(SkPoint));
491 
intersect_lines(const SkPoint & ptA,const SkVector & normA,const SkPoint & ptB,const SkVector & normB,SkPoint * result)492 static void intersect_lines(const SkPoint& ptA, const SkVector& normA,
493                             const SkPoint& ptB, const SkVector& normB,
494                             SkPoint* result) {
495 
496     SkScalar lineAW = -normA.dot(ptA);
497     SkScalar lineBW = -normB.dot(ptB);
498 
499     SkScalar wInv = normA.fX * normB.fY - normA.fY * normB.fX;
500     wInv = SkScalarInvert(wInv);
501     if (!SkScalarIsFinite(wInv)) {
502         // lines are parallel, pick the point in between
503         *result = (ptA + ptB)*SK_ScalarHalf;
504         *result += normA;
505     } else {
506         result->fX = normA.fY * lineBW - lineAW * normB.fY;
507         result->fX *= wInv;
508 
509         result->fY = lineAW * normB.fX - normA.fX * lineBW;
510         result->fY *= wInv;
511     }
512 }
513 
set_uv_quad(const SkPoint qpts[3],BezierVertex verts[kQuadNumVertices])514 static void set_uv_quad(const SkPoint qpts[3], BezierVertex verts[kQuadNumVertices]) {
515     // this should be in the src space, not dev coords, when we have perspective
516     GrPathUtils::QuadUVMatrix DevToUV(qpts);
517     DevToUV.apply(verts, kQuadNumVertices, sizeof(BezierVertex), sizeof(SkPoint));
518 }
519 
bloat_quad(const SkPoint qpts[3],const SkMatrix * toDevice,const SkMatrix * toSrc,BezierVertex verts[kQuadNumVertices])520 static void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice,
521                        const SkMatrix* toSrc, BezierVertex verts[kQuadNumVertices]) {
522     SkASSERT(!toDevice == !toSrc);
523     // original quad is specified by tri a,b,c
524     SkPoint a = qpts[0];
525     SkPoint b = qpts[1];
526     SkPoint c = qpts[2];
527 
528     if (toDevice) {
529         toDevice->mapPoints(&a, 1);
530         toDevice->mapPoints(&b, 1);
531         toDevice->mapPoints(&c, 1);
532     }
533     // make a new poly where we replace a and c by a 1-pixel wide edges orthog
534     // to edges ab and bc:
535     //
536     //   before       |        after
537     //                |              b0
538     //         b      |
539     //                |
540     //                |     a0            c0
541     // a         c    |        a1       c1
542     //
543     // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c,
544     // respectively.
545     BezierVertex& a0 = verts[0];
546     BezierVertex& a1 = verts[1];
547     BezierVertex& b0 = verts[2];
548     BezierVertex& c0 = verts[3];
549     BezierVertex& c1 = verts[4];
550 
551     SkVector ab = b;
552     ab -= a;
553     SkVector ac = c;
554     ac -= a;
555     SkVector cb = b;
556     cb -= c;
557 
558     // After the transform we might have a line, try to do something reasonable
559     if (toDevice && SkPointPriv::LengthSqd(ab) <= SK_ScalarNearlyZero*SK_ScalarNearlyZero) {
560         ab = cb;
561     }
562     if (toDevice && SkPointPriv::LengthSqd(cb) <= SK_ScalarNearlyZero*SK_ScalarNearlyZero) {
563         cb = ab;
564     }
565 
566     // We should have already handled degenerates
567     SkASSERT(toDevice || (ab.length() > 0 && cb.length() > 0));
568 
569     ab.normalize();
570     SkVector abN = SkPointPriv::MakeOrthog(ab, SkPointPriv::kLeft_Side);
571     if (abN.dot(ac) > 0) {
572         abN.negate();
573     }
574 
575     cb.normalize();
576     SkVector cbN = SkPointPriv::MakeOrthog(cb, SkPointPriv::kLeft_Side);
577     if (cbN.dot(ac) < 0) {
578         cbN.negate();
579     }
580 
581     a0.fPos = a;
582     a0.fPos += abN;
583     a1.fPos = a;
584     a1.fPos -= abN;
585 
586     if (toDevice && SkPointPriv::LengthSqd(ac) <= SK_ScalarNearlyZero*SK_ScalarNearlyZero) {
587         c = b;
588     }
589     c0.fPos = c;
590     c0.fPos += cbN;
591     c1.fPos = c;
592     c1.fPos -= cbN;
593 
594     intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos);
595 
596     if (toSrc) {
597         SkMatrixPriv::MapPointsWithStride(*toSrc, &verts[0].fPos, sizeof(BezierVertex),
598                                           kQuadNumVertices);
599     }
600 }
601 
602 // Equations based off of Loop-Blinn Quadratic GPU Rendering
603 // Input Parametric:
604 // P(t) = (P0*(1-t)^2 + 2*w*P1*t*(1-t) + P2*t^2) / (1-t)^2 + 2*w*t*(1-t) + t^2)
605 // Output Implicit:
606 // f(x, y, w) = f(P) = K^2 - LM
607 // K = dot(k, P), L = dot(l, P), M = dot(m, P)
608 // k, l, m are calculated in function GrPathUtils::getConicKLM
set_conic_coeffs(const SkPoint p[3],BezierVertex verts[kQuadNumVertices],const SkScalar weight)609 static void set_conic_coeffs(const SkPoint p[3], BezierVertex verts[kQuadNumVertices],
610                              const SkScalar weight) {
611     SkMatrix klm;
612 
613     GrPathUtils::getConicKLM(p, weight, &klm);
614 
615     for (int i = 0; i < kQuadNumVertices; ++i) {
616         const SkPoint3 pt3 = {verts[i].fPos.x(), verts[i].fPos.y(), 1.f};
617         klm.mapHomogeneousPoints((SkPoint3* ) verts[i].fConic.fKLM, &pt3, 1);
618     }
619 }
620 
add_conics(const SkPoint p[3],const SkScalar weight,const SkMatrix * toDevice,const SkMatrix * toSrc,BezierVertex ** vert)621 static void add_conics(const SkPoint p[3],
622                        const SkScalar weight,
623                        const SkMatrix* toDevice,
624                        const SkMatrix* toSrc,
625                        BezierVertex** vert) {
626     bloat_quad(p, toDevice, toSrc, *vert);
627     set_conic_coeffs(p, *vert, weight);
628     *vert += kQuadNumVertices;
629 }
630 
add_quads(const SkPoint p[3],int subdiv,const SkMatrix * toDevice,const SkMatrix * toSrc,BezierVertex ** vert)631 static void add_quads(const SkPoint p[3],
632                       int subdiv,
633                       const SkMatrix* toDevice,
634                       const SkMatrix* toSrc,
635                       BezierVertex** vert) {
636     SkASSERT(subdiv >= 0);
637     if (subdiv) {
638         SkPoint newP[5];
639         SkChopQuadAtHalf(p, newP);
640         add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert);
641         add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert);
642     } else {
643         bloat_quad(p, toDevice, toSrc, *vert);
644         set_uv_quad(p, *vert);
645         *vert += kQuadNumVertices;
646     }
647 }
648 
add_line(const SkPoint p[2],const SkMatrix * toSrc,uint8_t coverage,LineVertex ** vert)649 static void add_line(const SkPoint p[2],
650                      const SkMatrix* toSrc,
651                      uint8_t coverage,
652                      LineVertex** vert) {
653     const SkPoint& a = p[0];
654     const SkPoint& b = p[1];
655 
656     SkVector ortho, vec = b;
657     vec -= a;
658 
659     SkScalar lengthSqd = SkPointPriv::LengthSqd(vec);
660 
661     if (vec.setLength(SK_ScalarHalf)) {
662         // Create a vector orthogonal to 'vec' and of unit length
663         ortho.fX = 2.0f * vec.fY;
664         ortho.fY = -2.0f * vec.fX;
665 
666         float floatCoverage = GrNormalizeByteToFloat(coverage);
667 
668         if (lengthSqd >= 1.0f) {
669             // Relative to points a and b:
670             // The inner vertices are inset half a pixel along the line a,b
671             (*vert)[0].fPos = a + vec;
672             (*vert)[0].fCoverage = floatCoverage;
673             (*vert)[1].fPos = b - vec;
674             (*vert)[1].fCoverage = floatCoverage;
675         } else {
676             // The inner vertices are inset a distance of length(a,b) from the outer edge of
677             // geometry. For the "a" inset this is the same as insetting from b by half a pixel.
678             // The coverage is then modulated by the length. This gives us the correct
679             // coverage for rects shorter than a pixel as they get translated subpixel amounts
680             // inside of a pixel.
681             SkScalar length = SkScalarSqrt(lengthSqd);
682             (*vert)[0].fPos = b - vec;
683             (*vert)[0].fCoverage = floatCoverage * length;
684             (*vert)[1].fPos = a + vec;
685             (*vert)[1].fCoverage = floatCoverage * length;
686         }
687         // Relative to points a and b:
688         // The outer vertices are outset half a pixel along the line a,b and then a whole pixel
689         // orthogonally.
690         (*vert)[2].fPos = a - vec + ortho;
691         (*vert)[2].fCoverage = 0;
692         (*vert)[3].fPos = b + vec + ortho;
693         (*vert)[3].fCoverage = 0;
694         (*vert)[4].fPos = a - vec - ortho;
695         (*vert)[4].fCoverage = 0;
696         (*vert)[5].fPos = b + vec - ortho;
697         (*vert)[5].fCoverage = 0;
698 
699         if (toSrc) {
700             SkMatrixPriv::MapPointsWithStride(*toSrc, &(*vert)->fPos, sizeof(LineVertex),
701                                               kLineSegNumVertices);
702         }
703     } else {
704         // just make it degenerate and likely offscreen
705         for (int i = 0; i < kLineSegNumVertices; ++i) {
706             (*vert)[i].fPos.set(SK_ScalarMax, SK_ScalarMax);
707         }
708     }
709 
710     *vert += kLineSegNumVertices;
711 }
712 
713 ///////////////////////////////////////////////////////////////////////////////
714 
715 GrPathRenderer::CanDrawPath
onCanDrawPath(const CanDrawPathArgs & args) const716 GrAAHairLinePathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
717     if (GrAAType::kCoverage != args.fAAType) {
718         return CanDrawPath::kNo;
719     }
720 
721     if (!IsStrokeHairlineOrEquivalent(args.fShape->style(), *args.fViewMatrix, nullptr)) {
722         return CanDrawPath::kNo;
723     }
724 
725     // We don't currently handle dashing in this class though perhaps we should.
726     if (args.fShape->style().pathEffect()) {
727         return CanDrawPath::kNo;
728     }
729 
730     if (SkPath::kLine_SegmentMask == args.fShape->segmentMask() ||
731         args.fCaps->shaderCaps()->shaderDerivativeSupport()) {
732         return CanDrawPath::kYes;
733     }
734 
735     return CanDrawPath::kNo;
736 }
737 
738 template <class VertexType>
check_bounds(const SkMatrix & viewMatrix,const SkRect & devBounds,void * vertices,int vCount)739 bool check_bounds(const SkMatrix& viewMatrix, const SkRect& devBounds, void* vertices, int vCount)
740 {
741     SkRect tolDevBounds = devBounds;
742     // The bounds ought to be tight, but in perspective the below code runs the verts
743     // through the view matrix to get back to dev coords, which can introduce imprecision.
744     if (viewMatrix.hasPerspective()) {
745         tolDevBounds.outset(SK_Scalar1 / 1000, SK_Scalar1 / 1000);
746     } else {
747         // Non-persp matrices cause this path renderer to draw in device space.
748         SkASSERT(viewMatrix.isIdentity());
749     }
750     SkRect actualBounds;
751 
752     VertexType* verts = reinterpret_cast<VertexType*>(vertices);
753     bool first = true;
754     for (int i = 0; i < vCount; ++i) {
755         SkPoint pos = verts[i].fPos;
756         // This is a hack to workaround the fact that we move some degenerate segments offscreen.
757         if (SK_ScalarMax == pos.fX) {
758             continue;
759         }
760         viewMatrix.mapPoints(&pos, 1);
761         if (first) {
762             actualBounds.setLTRB(pos.fX, pos.fY, pos.fX, pos.fY);
763             first = false;
764         } else {
765             SkRectPriv::GrowToInclude(&actualBounds, pos);
766         }
767     }
768     if (!first) {
769         return tolDevBounds.contains(actualBounds);
770     }
771 
772     return true;
773 }
774 
775 namespace {
776 
777 class AAHairlineOp final : public GrMeshDrawOp {
778 private:
779     using Helper = GrSimpleMeshDrawOpHelperWithStencil;
780 
781 public:
782     DEFINE_OP_CLASS_ID
783 
Make(GrRecordingContext * context,GrPaint && paint,const SkMatrix & viewMatrix,const SkPath & path,const GrStyle & style,const SkIRect & devClipBounds,const GrUserStencilSettings * stencilSettings)784     static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
785                                           GrPaint&& paint,
786                                           const SkMatrix& viewMatrix,
787                                           const SkPath& path,
788                                           const GrStyle& style,
789                                           const SkIRect& devClipBounds,
790                                           const GrUserStencilSettings* stencilSettings) {
791         SkScalar hairlineCoverage;
792         uint8_t newCoverage = 0xff;
793         if (GrPathRenderer::IsStrokeHairlineOrEquivalent(style, viewMatrix, &hairlineCoverage)) {
794             newCoverage = SkScalarRoundToInt(hairlineCoverage * 0xff);
795         }
796 
797         const SkStrokeRec& stroke = style.strokeRec();
798         SkScalar capLength = SkPaint::kButt_Cap != stroke.getCap() ? hairlineCoverage * 0.5f : 0.0f;
799 
800         return Helper::FactoryHelper<AAHairlineOp>(context, std::move(paint), newCoverage,
801                                                    viewMatrix, path,
802                                                    devClipBounds, capLength, stencilSettings);
803     }
804 
AAHairlineOp(const Helper::MakeArgs & helperArgs,const SkPMColor4f & color,uint8_t coverage,const SkMatrix & viewMatrix,const SkPath & path,SkIRect devClipBounds,SkScalar capLength,const GrUserStencilSettings * stencilSettings)805     AAHairlineOp(const Helper::MakeArgs& helperArgs,
806                  const SkPMColor4f& color,
807                  uint8_t coverage,
808                  const SkMatrix& viewMatrix,
809                  const SkPath& path,
810                  SkIRect devClipBounds,
811                  SkScalar capLength,
812                  const GrUserStencilSettings* stencilSettings)
813             : INHERITED(ClassID())
814             , fHelper(helperArgs, GrAAType::kCoverage, stencilSettings)
815             , fColor(color)
816             , fCoverage(coverage) {
817         fPaths.emplace_back(PathData{viewMatrix, path, devClipBounds, capLength});
818 
819         this->setTransformedBounds(path.getBounds(), viewMatrix, HasAABloat::kYes,
820                                    IsHairline::kYes);
821     }
822 
name() const823     const char* name() const override { return "AAHairlineOp"; }
824 
visitProxies(const VisitProxyFunc & func) const825     void visitProxies(const VisitProxyFunc& func) const override {
826         fHelper.visitProxies(func);
827     }
828 
829 #ifdef SK_DEBUG
dumpInfo() const830     SkString dumpInfo() const override {
831         SkString string;
832         string.appendf("Color: 0x%08x Coverage: 0x%02x, Count: %d\n", fColor.toBytes_RGBA(),
833                        fCoverage, fPaths.count());
834         string += INHERITED::dumpInfo();
835         string += fHelper.dumpInfo();
836         return string;
837     }
838 #endif
839 
fixedFunctionFlags() const840     FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
841 
finalize(const GrCaps & caps,const GrAppliedClip * clip,bool hasMixedSampledCoverage,GrClampType clampType)842     GrProcessorSet::Analysis finalize(
843             const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
844             GrClampType clampType) override {
845         // This Op uses uniform (not vertex) color, so doesn't need to track wide color.
846         return fHelper.finalizeProcessors(caps, clip, hasMixedSampledCoverage, clampType,
847                                           GrProcessorAnalysisCoverage::kSingleChannel, &fColor,
848                                           nullptr);
849     }
850 
851 private:
852     void onPrepareDraws(Target*) override;
853     void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
854 
855     typedef SkTArray<SkPoint, true> PtArray;
856     typedef SkTArray<int, true> IntArray;
857     typedef SkTArray<float, true> FloatArray;
858 
onCombineIfPossible(GrOp * t,const GrCaps & caps)859     CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
860         AAHairlineOp* that = t->cast<AAHairlineOp>();
861 
862         if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
863             return CombineResult::kCannotCombine;
864         }
865 
866         if (this->viewMatrix().hasPerspective() != that->viewMatrix().hasPerspective()) {
867             return CombineResult::kCannotCombine;
868         }
869 
870         // We go to identity if we don't have perspective
871         if (this->viewMatrix().hasPerspective() &&
872             !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
873             return CombineResult::kCannotCombine;
874         }
875 
876         // TODO we can actually combine hairlines if they are the same color in a kind of bulk
877         // method but we haven't implemented this yet
878         // TODO investigate going to vertex color and coverage?
879         if (this->coverage() != that->coverage()) {
880             return CombineResult::kCannotCombine;
881         }
882 
883         if (this->color() != that->color()) {
884             return CombineResult::kCannotCombine;
885         }
886 
887         if (fHelper.usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
888             return CombineResult::kCannotCombine;
889         }
890 
891         fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
892         return CombineResult::kMerged;
893     }
894 
color() const895     const SkPMColor4f& color() const { return fColor; }
coverage() const896     uint8_t coverage() const { return fCoverage; }
viewMatrix() const897     const SkMatrix& viewMatrix() const { return fPaths[0].fViewMatrix; }
898 
899     struct PathData {
900         SkMatrix fViewMatrix;
901         SkPath fPath;
902         SkIRect fDevClipBounds;
903         SkScalar fCapLength;
904     };
905 
906     SkSTArray<1, PathData, true> fPaths;
907     Helper fHelper;
908     SkPMColor4f fColor;
909     uint8_t fCoverage;
910 
911     typedef GrMeshDrawOp INHERITED;
912 };
913 
914 }  // anonymous namespace
915 
onPrepareDraws(Target * target)916 void AAHairlineOp::onPrepareDraws(Target* target) {
917     // Setup the viewmatrix and localmatrix for the GrGeometryProcessor.
918     SkMatrix invert;
919     if (!this->viewMatrix().invert(&invert)) {
920         return;
921     }
922 
923     // we will transform to identity space if the viewmatrix does not have perspective
924     bool hasPerspective = this->viewMatrix().hasPerspective();
925     const SkMatrix* geometryProcessorViewM = &SkMatrix::I();
926     const SkMatrix* geometryProcessorLocalM = &invert;
927     const SkMatrix* toDevice = nullptr;
928     const SkMatrix* toSrc = nullptr;
929     if (hasPerspective) {
930         geometryProcessorViewM = &this->viewMatrix();
931         geometryProcessorLocalM = &SkMatrix::I();
932         toDevice = &this->viewMatrix();
933         toSrc = &invert;
934     }
935 
936     // This is hand inlined for maximum performance.
937     PREALLOC_PTARRAY(128) lines;
938     PREALLOC_PTARRAY(128) quads;
939     PREALLOC_PTARRAY(128) conics;
940     IntArray qSubdivs;
941     FloatArray cWeights;
942     int quadCount = 0;
943 
944     int instanceCount = fPaths.count();
945     bool convertConicsToQuads = !target->caps().shaderCaps()->floatIs32Bits();
946     for (int i = 0; i < instanceCount; i++) {
947         const PathData& args = fPaths[i];
948         quadCount += gather_lines_and_quads(args.fPath, args.fViewMatrix, args.fDevClipBounds,
949                                             args.fCapLength, convertConicsToQuads, &lines, &quads,
950                                             &conics, &qSubdivs, &cWeights);
951     }
952 
953     int lineCount = lines.count() / 2;
954     int conicCount = conics.count() / 3;
955     int quadAndConicCount = conicCount + quadCount;
956 
957     static constexpr int kMaxLines = SK_MaxS32 / kLineSegNumVertices;
958     static constexpr int kMaxQuadsAndConics = SK_MaxS32 / kQuadNumVertices;
959     if (lineCount > kMaxLines || quadAndConicCount > kMaxQuadsAndConics) {
960         return;
961     }
962 
963     // do lines first
964     if (lineCount) {
965         sk_sp<GrGeometryProcessor> lineGP;
966         {
967             using namespace GrDefaultGeoProcFactory;
968 
969             Color color(this->color());
970             LocalCoords localCoords(fHelper.usesLocalCoords() ? LocalCoords::kUsePosition_Type
971                                                               : LocalCoords::kUnused_Type);
972             localCoords.fMatrix = geometryProcessorLocalM;
973             lineGP = GrDefaultGeoProcFactory::Make(target->caps().shaderCaps(),
974                                                    color, Coverage::kAttribute_Type, localCoords,
975                                                    *geometryProcessorViewM);
976         }
977 
978         sk_sp<const GrBuffer> linesIndexBuffer = get_lines_index_buffer(target->resourceProvider());
979 
980         sk_sp<const GrBuffer> vertexBuffer;
981         int firstVertex;
982 
983         SkASSERT(sizeof(LineVertex) == lineGP->vertexStride());
984         int vertexCount = kLineSegNumVertices * lineCount;
985         LineVertex* verts = reinterpret_cast<LineVertex*>(target->makeVertexSpace(
986                 sizeof(LineVertex), vertexCount, &vertexBuffer, &firstVertex));
987 
988         if (!verts|| !linesIndexBuffer) {
989             SkDebugf("Could not allocate vertices\n");
990             return;
991         }
992 
993         for (int i = 0; i < lineCount; ++i) {
994             add_line(&lines[2*i], toSrc, this->coverage(), &verts);
995         }
996 
997         GrMesh* mesh = target->allocMesh(GrPrimitiveType::kTriangles);
998         mesh->setIndexedPatterned(std::move(linesIndexBuffer), kIdxsPerLineSeg, kLineSegNumVertices,
999                                   lineCount, kLineSegsNumInIdxBuffer);
1000         mesh->setVertexData(std::move(vertexBuffer), firstVertex);
1001         target->recordDraw(std::move(lineGP), mesh);
1002     }
1003 
1004     if (quadCount || conicCount) {
1005         sk_sp<GrGeometryProcessor> quadGP(GrQuadEffect::Make(this->color(),
1006                                                              *geometryProcessorViewM,
1007                                                              GrClipEdgeType::kHairlineAA,
1008                                                              target->caps(),
1009                                                              *geometryProcessorLocalM,
1010                                                              fHelper.usesLocalCoords(),
1011                                                              this->coverage()));
1012 
1013         sk_sp<GrGeometryProcessor> conicGP(GrConicEffect::Make(this->color(),
1014                                                                *geometryProcessorViewM,
1015                                                                GrClipEdgeType::kHairlineAA,
1016                                                                target->caps(),
1017                                                                *geometryProcessorLocalM,
1018                                                                fHelper.usesLocalCoords(),
1019                                                                this->coverage()));
1020 
1021         sk_sp<const GrBuffer> vertexBuffer;
1022         int firstVertex;
1023 
1024         sk_sp<const GrBuffer> quadsIndexBuffer = get_quads_index_buffer(target->resourceProvider());
1025 
1026         SkASSERT(sizeof(BezierVertex) == quadGP->vertexStride());
1027         SkASSERT(sizeof(BezierVertex) == conicGP->vertexStride());
1028         int vertexCount = kQuadNumVertices * quadAndConicCount;
1029         void* vertices = target->makeVertexSpace(sizeof(BezierVertex), vertexCount, &vertexBuffer,
1030                                                  &firstVertex);
1031 
1032         if (!vertices || !quadsIndexBuffer) {
1033             SkDebugf("Could not allocate vertices\n");
1034             return;
1035         }
1036 
1037         // Setup vertices
1038         BezierVertex* bezVerts = reinterpret_cast<BezierVertex*>(vertices);
1039 
1040         int unsubdivQuadCnt = quads.count() / 3;
1041         for (int i = 0; i < unsubdivQuadCnt; ++i) {
1042             SkASSERT(qSubdivs[i] >= 0);
1043             add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &bezVerts);
1044         }
1045 
1046         // Start Conics
1047         for (int i = 0; i < conicCount; ++i) {
1048             add_conics(&conics[3*i], cWeights[i], toDevice, toSrc, &bezVerts);
1049         }
1050 
1051         if (quadCount > 0) {
1052             GrMesh* mesh = target->allocMesh(GrPrimitiveType::kTriangles);
1053             mesh->setIndexedPatterned(quadsIndexBuffer, kIdxsPerQuad, kQuadNumVertices, quadCount,
1054                                       kQuadsNumInIdxBuffer);
1055             mesh->setVertexData(vertexBuffer, firstVertex);
1056             target->recordDraw(std::move(quadGP), mesh);
1057             firstVertex += quadCount * kQuadNumVertices;
1058         }
1059 
1060         if (conicCount > 0) {
1061             GrMesh* mesh = target->allocMesh(GrPrimitiveType::kTriangles);
1062             mesh->setIndexedPatterned(std::move(quadsIndexBuffer), kIdxsPerQuad, kQuadNumVertices,
1063                                       conicCount, kQuadsNumInIdxBuffer);
1064             mesh->setVertexData(std::move(vertexBuffer), firstVertex);
1065             target->recordDraw(std::move(conicGP), mesh);
1066         }
1067     }
1068 }
1069 
onExecute(GrOpFlushState * flushState,const SkRect & chainBounds)1070 void AAHairlineOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
1071     fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
1072 }
1073 
onDrawPath(const DrawPathArgs & args)1074 bool GrAAHairLinePathRenderer::onDrawPath(const DrawPathArgs& args) {
1075     GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
1076                               "GrAAHairlinePathRenderer::onDrawPath");
1077     SkASSERT(args.fRenderTargetContext->numSamples() <= 1);
1078 
1079     SkIRect devClipBounds;
1080     args.fClip->getConservativeBounds(args.fRenderTargetContext->width(),
1081                                       args.fRenderTargetContext->height(),
1082                                       &devClipBounds);
1083     SkPath path;
1084     args.fShape->asPath(&path);
1085     std::unique_ptr<GrDrawOp> op =
1086             AAHairlineOp::Make(args.fContext, std::move(args.fPaint), *args.fViewMatrix, path,
1087                                args.fShape->style(), devClipBounds, args.fUserStencilSettings);
1088     args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
1089     return true;
1090 }
1091 
1092 ///////////////////////////////////////////////////////////////////////////////////////////////////
1093 
1094 #if GR_TEST_UTILS
1095 
GR_DRAW_OP_TEST_DEFINE(AAHairlineOp)1096 GR_DRAW_OP_TEST_DEFINE(AAHairlineOp) {
1097     SkMatrix viewMatrix = GrTest::TestMatrix(random);
1098     SkPath path = GrTest::TestPath(random);
1099     SkIRect devClipBounds;
1100     devClipBounds.setEmpty();
1101     return AAHairlineOp::Make(context, std::move(paint), viewMatrix, path,
1102                               GrStyle::SimpleHairline(), devClipBounds,
1103                               GrGetRandomStencil(random, context));
1104 }
1105 
1106 #endif
1107