blob: 5ddeaf259586e519db55061376c44f26361c30ec [file] [log] [blame]
bsalomon@google.comf75b84e2011-09-29 14:58:28 +00001/*
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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#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"
Greg Danielf91aeb22019-06-18 09:58:02 -040015#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050022#include "src/gpu/GrProcessor.h"
23#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrStyle.h"
25#include "src/gpu/effects/GrBezierEffect.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040026#include "src/gpu/geometry/GrPathUtils.h"
27#include "src/gpu/geometry/GrShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/ops/GrAAHairLinePathRenderer.h"
29#include "src/gpu/ops/GrMeshDrawOp.h"
30#include "src/gpu/ops/GrSimpleMeshDrawOpHelper.h"
bsalomon@google.com4647f902013-03-26 14:45:27 +000031
bsalomoned0bcad2015-05-04 10:36:42 -070032#define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true>
33
bsalomon@google.comaeb21602011-08-30 18:13:44 +000034// 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
egdaniel@google.com5383a752013-07-12 20:15:34 +000036// bloat_quad. Quadratics and conics share an index buffer
bsalomon@google.comaeb21602011-08-30 18:13:44 +000037
robertphillips@google.comada90da2013-09-18 22:14:49 +000038// lines are rendered as:
39// *______________*
40// |\ -_______ /|
41// | \ \ / |
42// | *--------* |
43// | / ______/ \ |
44// */_-__________\*
45// For: 6 vertices and 18 indices (for 6 triangles)
bsalomon@google.comaeb21602011-08-30 18:13:44 +000046
joshualitt5ead6da2014-10-22 16:00:29 -070047// 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
egdaniel14afb432014-12-22 10:57:08 -080058// Each is drawn as three triangles ((a0,a1,b0), (b0,c1,c0), (a1,c1,b0))
59// specified by these 9 indices:
joshualitt5ead6da2014-10-22 16:00:29 -070060static const uint16_t kQuadIdxBufPattern[] = {
61 0, 1, 2,
62 2, 4, 3,
63 1, 4, 2
64};
bsalomon@google.comaeb21602011-08-30 18:13:44 +000065
joshualitt5ead6da2014-10-22 16:00:29 -070066static const int kIdxsPerQuad = SK_ARRAY_COUNT(kQuadIdxBufPattern);
67static const int kQuadNumVertices = 5;
68static const int kQuadsNumInIdxBuffer = 256;
bsalomoned0bcad2015-05-04 10:36:42 -070069GR_DECLARE_STATIC_UNIQUE_KEY(gQuadsIndexBufferKey);
70
Brian Salomond28a79d2017-10-16 13:01:07 -040071static sk_sp<const GrBuffer> get_quads_index_buffer(GrResourceProvider* resourceProvider) {
bsalomoned0bcad2015-05-04 10:36:42 -070072 GR_DEFINE_STATIC_UNIQUE_KEY(gQuadsIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -040073 return resourceProvider->findOrCreatePatternedIndexBuffer(
bsalomoned0bcad2015-05-04 10:36:42 -070074 kQuadIdxBufPattern, kIdxsPerQuad, kQuadsNumInIdxBuffer, kQuadNumVertices,
75 gQuadsIndexBufferKey);
76}
jvanverth@google.com681ccf02013-08-16 14:51:51 +000077
bsalomon@google.comaeb21602011-08-30 18:13:44 +000078
joshualitt5ead6da2014-10-22 16:00:29 -070079// 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:
jvanverth@google.com681ccf02013-08-16 14:51:51 +000089
joshualitt5ead6da2014-10-22 16:00:29 -070090static 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};
skia.committer@gmail.com74758112013-08-17 07:01:54 +000098
joshualitt5ead6da2014-10-22 16:00:29 -070099static const int kIdxsPerLineSeg = SK_ARRAY_COUNT(kLineSegIdxBufPattern);
100static const int kLineSegNumVertices = 6;
101static const int kLineSegsNumInIdxBuffer = 256;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000102
bsalomoned0bcad2015-05-04 10:36:42 -0700103GR_DECLARE_STATIC_UNIQUE_KEY(gLinesIndexBufferKey);
104
Brian Salomond28a79d2017-10-16 13:01:07 -0400105static sk_sp<const GrBuffer> get_lines_index_buffer(GrResourceProvider* resourceProvider) {
bsalomoned0bcad2015-05-04 10:36:42 -0700106 GR_DEFINE_STATIC_UNIQUE_KEY(gLinesIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400107 return resourceProvider->findOrCreatePatternedIndexBuffer(
bsalomoned0bcad2015-05-04 10:36:42 -0700108 kLineSegIdxBufPattern, kIdxsPerLineSeg, kLineSegsNumInIdxBuffer, kLineSegNumVertices,
109 gLinesIndexBufferKey);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000110}
111
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000112// Takes 178th time of logf on Z600 / VC2010
bsalomoned0bcad2015-05-04 10:36:42 -0700113static int get_float_exp(float x) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000114 GR_STATIC_ASSERT(sizeof(int) == sizeof(float));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000115#ifdef SK_DEBUG
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000116 static bool tested;
117 if (!tested) {
118 tested = true;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000119 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);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000130 }
131#endif
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000132 const int* iptr = (const int*)&x;
133 return (((*iptr) & 0x7f800000) >> 23) - 127;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000134}
135
egdaniel@google.com5383a752013-07-12 20:15:34 +0000136// 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
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000139// dst[0] is the original conic. If it returns 2 the dst[0]
egdaniel@google.com5383a752013-07-12 20:15:34 +0000140// and dst[1] are the two new conics.
bsalomoned0bcad2015-05-04 10:36:42 -0700141static int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000142 SkScalar t = SkFindQuadMaxCurvature(src);
Chris Dalton1d474dd2018-07-24 01:08:31 -0600143 if (t == 0 || t == 1) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000144 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);
caryclark414c4292016-09-26 11:03:54 -0700152 if (!conic.chopAt(t, dst)) {
153 dst[0].set(src, weight);
154 return 1;
155 }
egdaniel@google.com5383a752013-07-12 20:15:34 +0000156 }
157 return 2;
158 }
159}
160
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000161// 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).
bsalomoned0bcad2015-05-04 10:36:42 -0700164static int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000165 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
egdaniel@google.com5383a752013-07-12 20:15:34 +0000176// returns 0 if quad/conic is degen or close to it
177// in this case approx the path with lines
178// otherwise returns 1
bsalomoned0bcad2015-05-04 10:36:42 -0700179static int is_degen_quad_or_conic(const SkPoint p[3], SkScalar* dsqd) {
jvanverthcfeb85f2016-04-29 07:38:10 -0700180 static const SkScalar gDegenerateToLineTol = GrPathUtils::kDefaultTolerance;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000181 static const SkScalar gDegenerateToLineTolSqd =
Mike Reed8be952a2017-02-13 20:44:33 -0500182 gDegenerateToLineTol * gDegenerateToLineTol;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000183
Cary Clarkdf429f32017-11-08 11:44:31 -0500184 if (SkPointPriv::DistanceToSqd(p[0], p[1]) < gDegenerateToLineTolSqd ||
185 SkPointPriv::DistanceToSqd(p[1], p[2]) < gDegenerateToLineTolSqd) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000186 return 1;
187 }
188
Cary Clarkdf429f32017-11-08 11:44:31 -0500189 *dsqd = SkPointPriv::DistanceToLineBetweenSqd(p[1], p[0], p[2]);
joshualitt63648072015-02-19 10:25:21 -0800190 if (*dsqd < gDegenerateToLineTolSqd) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000191 return 1;
192 }
193
Cary Clarkdf429f32017-11-08 11:44:31 -0500194 if (SkPointPriv::DistanceToLineBetweenSqd(p[2], p[1], p[0]) < gDegenerateToLineTolSqd) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000195 return 1;
196 }
197 return 0;
198}
199
bsalomoned0bcad2015-05-04 10:36:42 -0700200static int is_degen_quad_or_conic(const SkPoint p[3]) {
joshualitt63648072015-02-19 10:25:21 -0800201 SkScalar dsqd;
202 return is_degen_quad_or_conic(p, &dsqd);
203}
204
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000205// we subdivide the quads to avoid huge overfill
206// if it returns -1 then should be drawn as lines
bsalomoned0bcad2015-05-04 10:36:42 -0700207static int num_quad_subdivs(const SkPoint p[3]) {
joshualitt63648072015-02-19 10:25:21 -0800208 SkScalar dsqd;
209 if (is_degen_quad_or_conic(p, &dsqd)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000210 return -1;
211 }
212
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000213 // 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
Mike Reed8be952a2017-02-13 20:44:33 -0500219 if (dsqd <= gSubdivTol * gSubdivTol) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000220 return 0;
221 } else {
robertphillips@google.com87379e12013-03-29 12:11:10 +0000222 static const int kMaxSub = 4;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000223 // 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
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000227 // +1 since we're ignoring the mantissa contribution.
228 int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000229 log = SkTMin(SkTMax(0, log), kMaxSub);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000230 return log;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000231 }
232}
233
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000234/**
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 */
bsalomoned0bcad2015-05-04 10:36:42 -0700243static int gather_lines_and_quads(const SkPath& path,
244 const SkMatrix& m,
245 const SkIRect& devClipBounds,
Brian Osmancf3dc292017-06-28 16:45:32 -0400246 SkScalar capLength,
Brian Salomon969a7382018-05-09 13:28:44 -0400247 bool convertConicsToQuads,
bsalomoned0bcad2015-05-04 10:36:42 -0700248 GrAAHairLinePathRenderer::PtArray* lines,
249 GrAAHairLinePathRenderer::PtArray* quads,
250 GrAAHairLinePathRenderer::PtArray* conics,
251 GrAAHairLinePathRenderer::IntArray* quadSubdivCnts,
252 GrAAHairLinePathRenderer::FloatArray* conicWeights) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000253 SkPath::Iter iter(path, false);
254
255 int totalQuadCount = 0;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000256 SkRect bounds;
257 SkIRect ibounds;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000258
259 bool persp = m.hasPerspective();
260
Brian Osmancf3dc292017-06-28 16:45:32 -0400261 // 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
Brian Salomon969a7382018-05-09 13:28:44 -0400268 // 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
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000313 for (;;) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000314 SkPoint pathPts[4];
Mike Reedba7e9a62019-08-16 13:30:34 -0400315 SkPath::Verb verb = iter.next(pathPts);
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000316 switch (verb) {
Brian Salomon969a7382018-05-09 13:28:44 -0400317 case SkPath::kConic_Verb:
318 if (convertConicsToQuads) {
319 SkScalar weight = iter.conicWeight();
320 SkAutoConicToQuads converter;
Brian Osmane3deee12018-11-20 11:10:15 -0500321 const SkPoint* quadPts = converter.computeQuads(pathPts, weight, 0.25f);
Brian Salomon969a7382018-05-09 13:28:44 -0400322 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;
Brian Osmancf3dc292017-06-28 16:45:32 -0400358 }
egdaniel@google.com5383a752013-07-12 20:15:34 +0000359 }
360 }
361 }
Brian Osmancf3dc292017-06-28 16:45:32 -0400362 verbsInContour++;
reed@google.com277c3f82013-05-31 15:17:50 +0000363 break;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000364 case SkPath::kMove_Verb:
Brian Osmancf3dc292017-06-28 16:45:32 -0400365 // 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;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000374 break;
Brian Salomon969a7382018-05-09 13:28:44 -0400375 case SkPath::kLine_Verb: {
376 SkPoint devPts[2];
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000377 m.mapPoints(devPts, pathPts, 2);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000378 bounds.setBounds(devPts, 2);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000379 bounds.outset(SK_Scalar1, SK_Scalar1);
380 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000381 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000382 SkPoint* pts = lines->push_back_n(2);
383 pts[0] = devPts[0];
384 pts[1] = devPts[1];
Brian Osmancf3dc292017-06-28 16:45:32 -0400385 if (verbsInContour == 0 && pts[0] == pts[1]) {
386 seenZeroLengthVerb = true;
387 zeroVerbPt = pts[0];
388 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000389 }
Brian Osmancf3dc292017-06-28 16:45:32 -0400390 verbsInContour++;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000391 break;
Brian Salomon969a7382018-05-09 13:28:44 -0400392 }
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000393 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) {
Brian Salomon969a7382018-05-09 13:28:44 -0400402 addSrcChoppedQuad(choppedPts + i * 2, !verbsInContour && 0 == i);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000403 }
Brian Osmancf3dc292017-06-28 16:45:32 -0400404 verbsInContour++;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000405 break;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000406 }
Brian Salomon969a7382018-05-09 13:28:44 -0400407 case SkPath::kCubic_Verb: {
408 SkPoint devPts[4];
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000409 m.mapPoints(devPts, pathPts, 4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000410 bounds.setBounds(devPts, 4);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000411 bounds.outset(SK_Scalar1, SK_Scalar1);
412 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000413 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000414 PREALLOC_PTARRAY(32) q;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000415 // We convert cubics to quadratics (for now).
416 // In perspective have to do conversion in src space.
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000417 if (persp) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000418 SkScalar tolScale =
bsalomon18fab302016-02-16 08:00:05 -0800419 GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m, path.getBounds());
420 GrPathUtils::convertCubicToQuads(pathPts, tolScale, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000421 } else {
bsalomon18fab302016-02-16 08:00:05 -0800422 GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000423 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000424 for (int i = 0; i < q.count(); i += 3) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000425 if (persp) {
Brian Salomon969a7382018-05-09 13:28:44 -0400426 addSrcChoppedQuad(&q[i], !verbsInContour && 0 == i);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000427 } else {
Brian Salomon969a7382018-05-09 13:28:44 -0400428 addChoppedQuad(nullptr, &q[i], !verbsInContour && 0 == i);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000429 }
430 }
431 }
Brian Osmancf3dc292017-06-28 16:45:32 -0400432 verbsInContour++;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000433 break;
Brian Salomon969a7382018-05-09 13:28:44 -0400434 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000435 case SkPath::kClose_Verb:
Brian Osmancf3dc292017-06-28 16:45:32 -0400436 // 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.
Brian Salomon969a7382018-05-09 13:28:44 -0400445 SkPoint devPts[2];
Brian Osmancf3dc292017-06-28 16:45:32 -0400446 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 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000458 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000459 case SkPath::kDone_Verb:
Brian Osmancf3dc292017-06-28 16:45:32 -0400460 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 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000467 return totalQuadCount;
468 }
469 }
470}
471
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000472struct LineVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000473 SkPoint fPos;
egdaniele27065a2014-11-06 08:00:48 -0800474 float fCoverage;
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000475};
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000476
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000477struct BezierVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000478 SkPoint fPos;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000479 union {
480 struct {
csmartdaltoncc261272017-03-23 13:38:45 -0600481 SkScalar fKLM[3];
egdaniel@google.com5383a752013-07-12 20:15:34 +0000482 } fConic;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000483 SkVector fQuadCoord;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000484 struct {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000485 SkScalar fBogus[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000486 };
487 };
488};
egdaniel@google.com5383a752013-07-12 20:15:34 +0000489
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000490GR_STATIC_ASSERT(sizeof(BezierVertex) == 3 * sizeof(SkPoint));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000491
bsalomoned0bcad2015-05-04 10:36:42 -0700492static void intersect_lines(const SkPoint& ptA, const SkVector& normA,
493 const SkPoint& ptB, const SkVector& normB,
494 SkPoint* result) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000495
496 SkScalar lineAW = -normA.dot(ptA);
497 SkScalar lineBW = -normB.dot(ptB);
498
Mike Reed8be952a2017-02-13 20:44:33 -0500499 SkScalar wInv = normA.fX * normB.fY - normA.fY * normB.fX;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000500 wInv = SkScalarInvert(wInv);
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400501 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;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000508
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400509 result->fY = lineAW * normB.fX - normA.fX * lineBW;
510 result->fY *= wInv;
511 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000512}
513
bsalomoned0bcad2015-05-04 10:36:42 -0700514static void set_uv_quad(const SkPoint qpts[3], BezierVertex verts[kQuadNumVertices]) {
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000515 // this should be in the src space, not dev coords, when we have perspective
516 GrPathUtils::QuadUVMatrix DevToUV(qpts);
Brian Osman568bec72018-12-26 16:48:25 -0500517 DevToUV.apply(verts, kQuadNumVertices, sizeof(BezierVertex), sizeof(SkPoint));
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000518}
519
bsalomoned0bcad2015-05-04 10:36:42 -0700520static void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice,
521 const SkMatrix* toSrc, BezierVertex verts[kQuadNumVertices]) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000522 SkASSERT(!toDevice == !toSrc);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000523 // original quad is specified by tri a,b,c
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000524 SkPoint a = qpts[0];
525 SkPoint b = qpts[1];
526 SkPoint c = qpts[2];
527
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000528 if (toDevice) {
529 toDevice->mapPoints(&a, 1);
530 toDevice->mapPoints(&b, 1);
531 toDevice->mapPoints(&c, 1);
532 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000533 // 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.
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000545 BezierVertex& a0 = verts[0];
546 BezierVertex& a1 = verts[1];
547 BezierVertex& b0 = verts[2];
548 BezierVertex& c0 = verts[3];
549 BezierVertex& c1 = verts[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000550
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000551 SkVector ab = b;
552 ab -= a;
553 SkVector ac = c;
554 ac -= a;
555 SkVector cb = b;
556 cb -= c;
557
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400558 // 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
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000566 // We should have already handled degenerates
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400567 SkASSERT(toDevice || (ab.length() > 0 && cb.length() > 0));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000568
569 ab.normalize();
Brian Salomon0235c642018-08-31 12:04:18 -0400570 SkVector abN = SkPointPriv::MakeOrthog(ab, SkPointPriv::kLeft_Side);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000571 if (abN.dot(ac) > 0) {
572 abN.negate();
573 }
574
575 cb.normalize();
Brian Salomon0235c642018-08-31 12:04:18 -0400576 SkVector cbN = SkPointPriv::MakeOrthog(cb, SkPointPriv::kLeft_Side);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000577 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
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400586 if (toDevice && SkPointPriv::LengthSqd(ac) <= SK_ScalarNearlyZero*SK_ScalarNearlyZero) {
587 c = b;
588 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000589 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
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000596 if (toSrc) {
Brian Salomonfa3783f2018-01-05 13:49:07 -0500597 SkMatrixPriv::MapPointsWithStride(*toSrc, &verts[0].fPos, sizeof(BezierVertex),
598 kQuadNumVertices);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000599 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000600}
601
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000602// Equations based off of Loop-Blinn Quadratic GPU Rendering
egdaniel@google.com5383a752013-07-12 20:15:34 +0000603// 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:
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000606// f(x, y, w) = f(P) = K^2 - LM
607// K = dot(k, P), L = dot(l, P), M = dot(m, P)
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000608// k, l, m are calculated in function GrPathUtils::getConicKLM
bsalomoned0bcad2015-05-04 10:36:42 -0700609static void set_conic_coeffs(const SkPoint p[3], BezierVertex verts[kQuadNumVertices],
610 const SkScalar weight) {
csmartdaltoncc261272017-03-23 13:38:45 -0600611 SkMatrix klm;
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000612
csmartdaltoncc261272017-03-23 13:38:45 -0600613 GrPathUtils::getConicKLM(p, weight, &klm);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000614
joshualitt5ead6da2014-10-22 16:00:29 -0700615 for (int i = 0; i < kQuadNumVertices; ++i) {
Cary Clarke4442cb2017-10-18 11:46:18 -0400616 const SkPoint3 pt3 = {verts[i].fPos.x(), verts[i].fPos.y(), 1.f};
617 klm.mapHomogeneousPoints((SkPoint3* ) verts[i].fConic.fKLM, &pt3, 1);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000618 }
619}
620
bsalomoned0bcad2015-05-04 10:36:42 -0700621static void add_conics(const SkPoint p[3],
622 const SkScalar weight,
623 const SkMatrix* toDevice,
624 const SkMatrix* toSrc,
625 BezierVertex** vert) {
joshualitt7bc18b72015-02-03 16:41:41 -0800626 bloat_quad(p, toDevice, toSrc, *vert);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000627 set_conic_coeffs(p, *vert, weight);
joshualitt5ead6da2014-10-22 16:00:29 -0700628 *vert += kQuadNumVertices;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000629}
630
bsalomoned0bcad2015-05-04 10:36:42 -0700631static void add_quads(const SkPoint p[3],
632 int subdiv,
633 const SkMatrix* toDevice,
634 const SkMatrix* toSrc,
635 BezierVertex** vert) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000636 SkASSERT(subdiv >= 0);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000637 if (subdiv) {
638 SkPoint newP[5];
639 SkChopQuadAtHalf(p, newP);
joshualitt7bc18b72015-02-03 16:41:41 -0800640 add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert);
641 add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000642 } else {
joshualitt7bc18b72015-02-03 16:41:41 -0800643 bloat_quad(p, toDevice, toSrc, *vert);
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000644 set_uv_quad(p, *vert);
joshualitt5ead6da2014-10-22 16:00:29 -0700645 *vert += kQuadNumVertices;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000646 }
647}
648
bsalomoned0bcad2015-05-04 10:36:42 -0700649static void add_line(const SkPoint p[2],
650 const SkMatrix* toSrc,
651 uint8_t coverage,
652 LineVertex** vert) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000653 const SkPoint& a = p[0];
654 const SkPoint& b = p[1];
655
robertphillips@google.comada90da2013-09-18 22:14:49 +0000656 SkVector ortho, vec = b;
657 vec -= a;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000658
Cary Clarkdf429f32017-11-08 11:44:31 -0500659 SkScalar lengthSqd = SkPointPriv::LengthSqd(vec);
Greg Daniel2e67dea2017-10-12 15:14:41 -0400660
robertphillips@google.comada90da2013-09-18 22:14:49 +0000661 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;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000665
egdaniele27065a2014-11-06 08:00:48 -0800666 float floatCoverage = GrNormalizeByteToFloat(coverage);
667
Greg Daniel2e67dea2017-10-12 15:14:41 -0400668 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.
robertphillips@google.comada90da2013-09-18 22:14:49 +0000690 (*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
bsalomon49f085d2014-09-05 13:34:00 -0700699 if (toSrc) {
Brian Salomonfa3783f2018-01-05 13:49:07 -0500700 SkMatrixPriv::MapPointsWithStride(*toSrc, &(*vert)->fPos, sizeof(LineVertex),
701 kLineSegNumVertices);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000702 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000703 } else {
704 // just make it degenerate and likely offscreen
joshualitt5ead6da2014-10-22 16:00:29 -0700705 for (int i = 0; i < kLineSegNumVertices; ++i) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000706 (*vert)[i].fPos.set(SK_ScalarMax, SK_ScalarMax);
707 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000708 }
709
joshualitt5ead6da2014-10-22 16:00:29 -0700710 *vert += kLineSegNumVertices;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000711}
712
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000713///////////////////////////////////////////////////////////////////////////////
714
Chris Dalton5ed44232017-09-07 13:22:46 -0600715GrPathRenderer::CanDrawPath
716GrAAHairLinePathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600717 if (GrAAType::kCoverage != args.fAAType) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600718 return CanDrawPath::kNo;
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000719 }
720
bsalomon8acedde2016-06-24 10:42:16 -0700721 if (!IsStrokeHairlineOrEquivalent(args.fShape->style(), *args.fViewMatrix, nullptr)) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600722 return CanDrawPath::kNo;
bsalomon6663acf2016-05-10 09:14:17 -0700723 }
724
725 // We don't currently handle dashing in this class though perhaps we should.
bsalomon8acedde2016-06-24 10:42:16 -0700726 if (args.fShape->style().pathEffect()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600727 return CanDrawPath::kNo;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000728 }
729
bsalomon8acedde2016-06-24 10:42:16 -0700730 if (SkPath::kLine_SegmentMask == args.fShape->segmentMask() ||
Eric Karl5c779752017-05-08 12:02:07 -0700731 args.fCaps->shaderCaps()->shaderDerivativeSupport()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600732 return CanDrawPath::kYes;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000733 }
bsalomon8acedde2016-06-24 10:42:16 -0700734
Chris Dalton5ed44232017-09-07 13:22:46 -0600735 return CanDrawPath::kNo;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000736}
737
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000738template <class VertexType>
joshualitt8059eb92014-12-29 15:10:07 -0800739bool check_bounds(const SkMatrix& viewMatrix, const SkRect& devBounds, void* vertices, int vCount)
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000740{
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000741 SkRect tolDevBounds = devBounds;
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000742 // 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.
joshualitt8059eb92014-12-29 15:10:07 -0800744 if (viewMatrix.hasPerspective()) {
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000745 tolDevBounds.outset(SK_Scalar1 / 1000, SK_Scalar1 / 1000);
746 } else {
747 // Non-persp matrices cause this path renderer to draw in device space.
joshualitt8059eb92014-12-29 15:10:07 -0800748 SkASSERT(viewMatrix.isIdentity());
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000749 }
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000750 SkRect actualBounds;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000751
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000752 VertexType* verts = reinterpret_cast<VertexType*>(vertices);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000753 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 }
joshualitt8059eb92014-12-29 15:10:07 -0800760 viewMatrix.mapPoints(&pos, 1);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000761 if (first) {
Mike Reed92b33352019-08-24 19:39:13 -0400762 actualBounds.setLTRB(pos.fX, pos.fY, pos.fX, pos.fY);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000763 first = false;
764 } else {
Mike Reed185ffe92018-01-08 17:09:54 -0500765 SkRectPriv::GrowToInclude(&actualBounds, pos);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000766 }
767 }
768 if (!first) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000769 return tolDevBounds.contains(actualBounds);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000770 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000771
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000772 return true;
773}
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000774
Brian Salomona531f252017-07-07 13:29:28 -0400775namespace {
776
777class AAHairlineOp final : public GrMeshDrawOp {
778private:
779 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
780
joshualitt7bc18b72015-02-03 16:41:41 -0800781public:
Brian Salomon25a88092016-12-01 09:36:50 -0500782 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700783
Robert Phillipsb97da532019-02-12 15:24:12 -0500784 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400785 GrPaint&& paint,
Brian Salomona531f252017-07-07 13:29:28 -0400786 const SkMatrix& viewMatrix,
787 const SkPath& path,
788 const GrStyle& style,
789 const SkIRect& devClipBounds,
790 const GrUserStencilSettings* stencilSettings) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500791 SkScalar hairlineCoverage;
792 uint8_t newCoverage = 0xff;
793 if (GrPathRenderer::IsStrokeHairlineOrEquivalent(style, viewMatrix, &hairlineCoverage)) {
794 newCoverage = SkScalarRoundToInt(hairlineCoverage * 0xff);
795 }
joshualitt7bc18b72015-02-03 16:41:41 -0800796
Brian Osmancf3dc292017-06-28 16:45:32 -0400797 const SkStrokeRec& stroke = style.strokeRec();
798 SkScalar capLength = SkPaint::kButt_Cap != stroke.getCap() ? hairlineCoverage * 0.5f : 0.0f;
799
Robert Phillips7c525e62018-06-12 10:11:12 -0400800 return Helper::FactoryHelper<AAHairlineOp>(context, std::move(paint), newCoverage,
801 viewMatrix, path,
Brian Salomona531f252017-07-07 13:29:28 -0400802 devClipBounds, capLength, stencilSettings);
803 }
804
805 AAHairlineOp(const Helper::MakeArgs& helperArgs,
Brian Osmancf860852018-10-31 14:04:39 -0400806 const SkPMColor4f& color,
Brian Salomona531f252017-07-07 13:29:28 -0400807 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 IsZeroArea::kYes);
bsalomonf1703092016-06-29 18:41:53 -0700821 }
joshualitt7bc18b72015-02-03 16:41:41 -0800822
Brian Salomond0a0a652016-12-15 15:25:22 -0500823 const char* name() const override { return "AAHairlineOp"; }
joshualitt7bc18b72015-02-03 16:41:41 -0800824
Chris Dalton1706cbf2019-05-21 19:35:29 -0600825 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400826 fHelper.visitProxies(func);
827 }
828
Brian Osman9a390ac2018-11-12 09:47:48 -0500829#ifdef SK_DEBUG
Brian Salomon7c3e7182016-12-01 09:35:30 -0500830 SkString dumpInfo() const override {
831 SkString string;
Brian Osmancf860852018-10-31 14:04:39 -0400832 string.appendf("Color: 0x%08x Coverage: 0x%02x, Count: %d\n", fColor.toBytes_RGBA(),
Brian Osman1be2b7c2018-10-29 16:07:15 -0400833 fCoverage, fPaths.count());
Brian Salomona531f252017-07-07 13:29:28 -0400834 string += INHERITED::dumpInfo();
835 string += fHelper.dumpInfo();
Brian Salomon7c3e7182016-12-01 09:35:30 -0500836 return string;
837 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500838#endif
Brian Salomon7c3e7182016-12-01 09:35:30 -0500839
Brian Salomona531f252017-07-07 13:29:28 -0400840 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
841
Chris Dalton6ce447a2019-06-23 18:07:38 -0600842 GrProcessorSet::Analysis finalize(
843 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
844 GrClampType clampType) override {
Brian Osman8fa7ab42019-03-18 10:22:42 -0400845 // This Op uses uniform (not vertex) color, so doesn't need to track wide color.
Chris Dalton6ce447a2019-06-23 18:07:38 -0600846 return fHelper.finalizeProcessors(caps, clip, hasMixedSampledCoverage, clampType,
Brian Osman8fa7ab42019-03-18 10:22:42 -0400847 GrProcessorAnalysisCoverage::kSingleChannel, &fColor,
848 nullptr);
Brian Salomona531f252017-07-07 13:29:28 -0400849 }
850
bsalomone46f9fe2015-08-18 06:05:14 -0700851private:
Brian Salomon91326c32017-08-09 16:02:19 -0400852 void onPrepareDraws(Target*) override;
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700853 void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
bsalomon75398562015-08-17 12:55:38 -0700854
joshualitt7bc18b72015-02-03 16:41:41 -0800855 typedef SkTArray<SkPoint, true> PtArray;
856 typedef SkTArray<int, true> IntArray;
857 typedef SkTArray<float, true> FloatArray;
858
Brian Salomon7eae3e02018-08-07 14:02:38 +0000859 CombineResult onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500860 AAHairlineOp* that = t->cast<AAHairlineOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700861
Brian Salomona531f252017-07-07 13:29:28 -0400862 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000863 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700864 }
865
joshualitt7bc18b72015-02-03 16:41:41 -0800866 if (this->viewMatrix().hasPerspective() != that->viewMatrix().hasPerspective()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000867 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800868 }
869
870 // We go to identity if we don't have perspective
871 if (this->viewMatrix().hasPerspective() &&
872 !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000873 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800874 }
875
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500876 // 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
joshualitt7bc18b72015-02-03 16:41:41 -0800878 // TODO investigate going to vertex color and coverage?
879 if (this->coverage() != that->coverage()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000880 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800881 }
882
883 if (this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000884 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800885 }
886
Brian Salomona531f252017-07-07 13:29:28 -0400887 if (fHelper.usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000888 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800889 }
890
Brian Salomond0a0a652016-12-15 15:25:22 -0500891 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000892 return CombineResult::kMerged;
joshualitt7bc18b72015-02-03 16:41:41 -0800893 }
894
Brian Osmancf860852018-10-31 14:04:39 -0400895 const SkPMColor4f& color() const { return fColor; }
Brian Salomond0a0a652016-12-15 15:25:22 -0500896 uint8_t coverage() const { return fCoverage; }
Brian Salomond0a0a652016-12-15 15:25:22 -0500897 const SkMatrix& viewMatrix() const { return fPaths[0].fViewMatrix; }
joshualitt7bc18b72015-02-03 16:41:41 -0800898
Brian Salomond0a0a652016-12-15 15:25:22 -0500899 struct PathData {
bsalomonf1703092016-06-29 18:41:53 -0700900 SkMatrix fViewMatrix;
901 SkPath fPath;
902 SkIRect fDevClipBounds;
Brian Osmancf3dc292017-06-28 16:45:32 -0400903 SkScalar fCapLength;
bsalomonf1703092016-06-29 18:41:53 -0700904 };
905
Brian Salomona531f252017-07-07 13:29:28 -0400906 SkSTArray<1, PathData, true> fPaths;
907 Helper fHelper;
Brian Osmancf860852018-10-31 14:04:39 -0400908 SkPMColor4f fColor;
Brian Salomond0a0a652016-12-15 15:25:22 -0500909 uint8_t fCoverage;
joshualitt7bc18b72015-02-03 16:41:41 -0800910
Brian Salomona531f252017-07-07 13:29:28 -0400911 typedef GrMeshDrawOp INHERITED;
joshualitt7bc18b72015-02-03 16:41:41 -0800912};
913
Brian Salomona531f252017-07-07 13:29:28 -0400914} // anonymous namespace
915
Brian Salomon91326c32017-08-09 16:02:19 -0400916void AAHairlineOp::onPrepareDraws(Target* target) {
joshualitt7bc18b72015-02-03 16:41:41 -0800917 // 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;
halcanary96fcdcc2015-08-27 07:41:13 -0700927 const SkMatrix* toDevice = nullptr;
928 const SkMatrix* toSrc = nullptr;
joshualitt7bc18b72015-02-03 16:41:41 -0800929 if (hasPerspective) {
930 geometryProcessorViewM = &this->viewMatrix();
931 geometryProcessorLocalM = &SkMatrix::I();
932 toDevice = &this->viewMatrix();
933 toSrc = &invert;
934 }
935
joshualitt7bc18b72015-02-03 16:41:41 -0800936 // 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;
joshualitt351ba1b2015-02-16 08:33:19 -0800942 int quadCount = 0;
joshualitt7bc18b72015-02-03 16:41:41 -0800943
Brian Salomond0a0a652016-12-15 15:25:22 -0500944 int instanceCount = fPaths.count();
Brian Salomon969a7382018-05-09 13:28:44 -0400945 bool convertConicsToQuads = !target->caps().shaderCaps()->floatIs32Bits();
joshualitt7bc18b72015-02-03 16:41:41 -0800946 for (int i = 0; i < instanceCount; i++) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500947 const PathData& args = fPaths[i];
joshualitt351ba1b2015-02-16 08:33:19 -0800948 quadCount += gather_lines_and_quads(args.fPath, args.fViewMatrix, args.fDevClipBounds,
Brian Salomon969a7382018-05-09 13:28:44 -0400949 args.fCapLength, convertConicsToQuads, &lines, &quads,
950 &conics, &qSubdivs, &cWeights);
joshualitt7bc18b72015-02-03 16:41:41 -0800951 }
952
joshualitt7bc18b72015-02-03 16:41:41 -0800953 int lineCount = lines.count() / 2;
954 int conicCount = conics.count() / 3;
Brian Salomon296de502018-03-13 12:26:55 -0400955 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 }
joshualitt7bc18b72015-02-03 16:41:41 -0800962
963 // do lines first
964 if (lineCount) {
bungeman06ca8ec2016-06-09 08:01:03 -0700965 sk_sp<GrGeometryProcessor> lineGP;
bsalomon342bfc22016-04-01 06:06:20 -0700966 {
967 using namespace GrDefaultGeoProcFactory;
968
969 Color color(this->color());
Brian Salomona531f252017-07-07 13:29:28 -0400970 LocalCoords localCoords(fHelper.usesLocalCoords() ? LocalCoords::kUsePosition_Type
971 : LocalCoords::kUnused_Type);
bsalomon342bfc22016-04-01 06:06:20 -0700972 localCoords.fMatrix = geometryProcessorLocalM;
Ruiqi Maob609e6d2018-07-17 10:19:38 -0400973 lineGP = GrDefaultGeoProcFactory::Make(target->caps().shaderCaps(),
974 color, Coverage::kAttribute_Type, localCoords,
bungeman06ca8ec2016-06-09 08:01:03 -0700975 *geometryProcessorViewM);
bsalomon342bfc22016-04-01 06:06:20 -0700976 }
977
Brian Salomond28a79d2017-10-16 13:01:07 -0400978 sk_sp<const GrBuffer> linesIndexBuffer = get_lines_index_buffer(target->resourceProvider());
joshualitt7bc18b72015-02-03 16:41:41 -0800979
Brian Salomon12d22642019-01-29 14:38:50 -0500980 sk_sp<const GrBuffer> vertexBuffer;
joshualitt7bc18b72015-02-03 16:41:41 -0800981 int firstVertex;
982
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500983 SkASSERT(sizeof(LineVertex) == lineGP->vertexStride());
joshualitt7bc18b72015-02-03 16:41:41 -0800984 int vertexCount = kLineSegNumVertices * lineCount;
Brian Salomon92be2f72018-06-19 14:33:47 -0400985 LineVertex* verts = reinterpret_cast<LineVertex*>(target->makeVertexSpace(
986 sizeof(LineVertex), vertexCount, &vertexBuffer, &firstVertex));
joshualitt7bc18b72015-02-03 16:41:41 -0800987
bsalomone64eb572015-05-07 11:35:55 -0700988 if (!verts|| !linesIndexBuffer) {
joshualitt4b31de82015-03-05 14:33:41 -0800989 SkDebugf("Could not allocate vertices\n");
990 return;
991 }
992
joshualitt7bc18b72015-02-03 16:41:41 -0800993 for (int i = 0; i < lineCount; ++i) {
994 add_line(&lines[2*i], toSrc, this->coverage(), &verts);
995 }
996
Brian Salomon7eae3e02018-08-07 14:02:38 +0000997 GrMesh* mesh = target->allocMesh(GrPrimitiveType::kTriangles);
Brian Salomon12d22642019-01-29 14:38:50 -0500998 mesh->setIndexedPatterned(std::move(linesIndexBuffer), kIdxsPerLineSeg, kLineSegNumVertices,
Brian Salomon7eae3e02018-08-07 14:02:38 +0000999 lineCount, kLineSegsNumInIdxBuffer);
Brian Salomon12d22642019-01-29 14:38:50 -05001000 mesh->setVertexData(std::move(vertexBuffer), firstVertex);
Chris Dalton07cdcfc92019-02-26 11:13:22 -07001001 target->recordDraw(std::move(lineGP), mesh);
joshualitt7bc18b72015-02-03 16:41:41 -08001002 }
1003
1004 if (quadCount || conicCount) {
Brian Salomona531f252017-07-07 13:29:28 -04001005 sk_sp<GrGeometryProcessor> quadGP(GrQuadEffect::Make(this->color(),
1006 *geometryProcessorViewM,
Ethan Nicholas1706f842017-11-10 11:58:19 -05001007 GrClipEdgeType::kHairlineAA,
Brian Salomona531f252017-07-07 13:29:28 -04001008 target->caps(),
1009 *geometryProcessorLocalM,
1010 fHelper.usesLocalCoords(),
1011 this->coverage()));
bsalomon342bfc22016-04-01 06:06:20 -07001012
Brian Salomona531f252017-07-07 13:29:28 -04001013 sk_sp<GrGeometryProcessor> conicGP(GrConicEffect::Make(this->color(),
1014 *geometryProcessorViewM,
Ethan Nicholas1706f842017-11-10 11:58:19 -05001015 GrClipEdgeType::kHairlineAA,
Brian Salomona531f252017-07-07 13:29:28 -04001016 target->caps(),
1017 *geometryProcessorLocalM,
1018 fHelper.usesLocalCoords(),
1019 this->coverage()));
bsalomon342bfc22016-04-01 06:06:20 -07001020
Brian Salomon12d22642019-01-29 14:38:50 -05001021 sk_sp<const GrBuffer> vertexBuffer;
joshualitt7bc18b72015-02-03 16:41:41 -08001022 int firstVertex;
1023
Brian Salomond28a79d2017-10-16 13:01:07 -04001024 sk_sp<const GrBuffer> quadsIndexBuffer = get_quads_index_buffer(target->resourceProvider());
bsalomoned0bcad2015-05-04 10:36:42 -07001025
Brian Osmanf04fb3c2018-11-12 15:34:00 -05001026 SkASSERT(sizeof(BezierVertex) == quadGP->vertexStride());
1027 SkASSERT(sizeof(BezierVertex) == conicGP->vertexStride());
Brian Salomon296de502018-03-13 12:26:55 -04001028 int vertexCount = kQuadNumVertices * quadAndConicCount;
Brian Salomon92be2f72018-06-19 14:33:47 -04001029 void* vertices = target->makeVertexSpace(sizeof(BezierVertex), vertexCount, &vertexBuffer,
1030 &firstVertex);
joshualitt7bc18b72015-02-03 16:41:41 -08001031
bsalomoned0bcad2015-05-04 10:36:42 -07001032 if (!vertices || !quadsIndexBuffer) {
joshualitt4b31de82015-03-05 14:33:41 -08001033 SkDebugf("Could not allocate vertices\n");
1034 return;
1035 }
1036
joshualitt7bc18b72015-02-03 16:41:41 -08001037 // Setup vertices
robertphillips44c31282015-09-03 12:58:48 -07001038 BezierVertex* bezVerts = reinterpret_cast<BezierVertex*>(vertices);
joshualitt7bc18b72015-02-03 16:41:41 -08001039
joshualitt7bc18b72015-02-03 16:41:41 -08001040 int unsubdivQuadCnt = quads.count() / 3;
1041 for (int i = 0; i < unsubdivQuadCnt; ++i) {
1042 SkASSERT(qSubdivs[i] >= 0);
robertphillips44c31282015-09-03 12:58:48 -07001043 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &bezVerts);
joshualitt7bc18b72015-02-03 16:41:41 -08001044 }
1045
1046 // Start Conics
1047 for (int i = 0; i < conicCount; ++i) {
robertphillips44c31282015-09-03 12:58:48 -07001048 add_conics(&conics[3*i], cWeights[i], toDevice, toSrc, &bezVerts);
joshualitt7bc18b72015-02-03 16:41:41 -08001049 }
1050
1051 if (quadCount > 0) {
Brian Salomon7eae3e02018-08-07 14:02:38 +00001052 GrMesh* mesh = target->allocMesh(GrPrimitiveType::kTriangles);
Brian Salomon12d22642019-01-29 14:38:50 -05001053 mesh->setIndexedPatterned(quadsIndexBuffer, kIdxsPerQuad, kQuadNumVertices, quadCount,
1054 kQuadsNumInIdxBuffer);
Brian Salomon7eae3e02018-08-07 14:02:38 +00001055 mesh->setVertexData(vertexBuffer, firstVertex);
Chris Dalton07cdcfc92019-02-26 11:13:22 -07001056 target->recordDraw(std::move(quadGP), mesh);
bsalomon342bfc22016-04-01 06:06:20 -07001057 firstVertex += quadCount * kQuadNumVertices;
joshualitt7bc18b72015-02-03 16:41:41 -08001058 }
1059
1060 if (conicCount > 0) {
Brian Salomon7eae3e02018-08-07 14:02:38 +00001061 GrMesh* mesh = target->allocMesh(GrPrimitiveType::kTriangles);
Brian Salomon12d22642019-01-29 14:38:50 -05001062 mesh->setIndexedPatterned(std::move(quadsIndexBuffer), kIdxsPerQuad, kQuadNumVertices,
Brian Salomon7eae3e02018-08-07 14:02:38 +00001063 conicCount, kQuadsNumInIdxBuffer);
Brian Salomon12d22642019-01-29 14:38:50 -05001064 mesh->setVertexData(std::move(vertexBuffer), firstVertex);
Chris Dalton07cdcfc92019-02-26 11:13:22 -07001065 target->recordDraw(std::move(conicGP), mesh);
joshualitt7bc18b72015-02-03 16:41:41 -08001066 }
1067 }
1068}
1069
Chris Dalton07cdcfc92019-02-26 11:13:22 -07001070void AAHairlineOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
1071 fHelper.executeDrawsAndUploads(this, flushState, chainBounds);
1072}
1073
bsalomon0aff2fa2015-07-31 06:48:27 -07001074bool GrAAHairLinePathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -04001075 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -07001076 "GrAAHairlinePathRenderer::onDrawPath");
Chris Dalton6ce447a2019-06-23 18:07:38 -06001077 SkASSERT(args.fRenderTargetContext->numSamples() <= 1);
csmartdaltonecbc12b2016-06-08 10:08:43 -07001078
jvanverth@google.com681ccf02013-08-16 14:51:51 +00001079 SkIRect devClipBounds;
Robert Phillips784b7bf2016-12-09 13:35:02 -05001080 args.fClip->getConservativeBounds(args.fRenderTargetContext->width(),
1081 args.fRenderTargetContext->height(),
robertphillips976f5f02016-06-03 10:59:20 -07001082 &devClipBounds);
bsalomon8acedde2016-06-24 10:42:16 -07001083 SkPath path;
1084 args.fShape->asPath(&path);
Brian Salomona531f252017-07-07 13:29:28 -04001085 std::unique_ptr<GrDrawOp> op =
Robert Phillips7c525e62018-06-12 10:11:12 -04001086 AAHairlineOp::Make(args.fContext, std::move(args.fPaint), *args.fViewMatrix, path,
Brian Salomona531f252017-07-07 13:29:28 -04001087 args.fShape->style(), devClipBounds, args.fUserStencilSettings);
1088 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001089 return true;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001090}
joshualitt40ded322015-05-02 07:07:17 -07001091
1092///////////////////////////////////////////////////////////////////////////////////////////////////
1093
Hal Canary6f6961e2017-01-31 13:50:44 -05001094#if GR_TEST_UTILS
joshualitt40ded322015-05-02 07:07:17 -07001095
Brian Salomona531f252017-07-07 13:29:28 -04001096GR_DRAW_OP_TEST_DEFINE(AAHairlineOp) {
joshualitt40ded322015-05-02 07:07:17 -07001097 SkMatrix viewMatrix = GrTest::TestMatrix(random);
joshualitt40ded322015-05-02 07:07:17 -07001098 SkPath path = GrTest::TestPath(random);
1099 SkIRect devClipBounds;
1100 devClipBounds.setEmpty();
Robert Phillips7c525e62018-06-12 10:11:12 -04001101 return AAHairlineOp::Make(context, std::move(paint), viewMatrix, path,
1102 GrStyle::SimpleHairline(), devClipBounds,
1103 GrGetRandomStencil(random, context));
joshualitt40ded322015-05-02 07:07:17 -07001104}
1105
1106#endif