blob: 34e290f47b649db181a82f1cfe33cf91c14a084d [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"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrDefaultGeoProcFactory.h"
19#include "src/gpu/GrDrawOpTest.h"
20#include "src/gpu/GrOpFlushState.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrProcessor.h"
Robert Phillips4f93c572020-03-18 08:13:53 -040022#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrStyle.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050025#include "src/gpu/GrSurfaceDrawContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/effects/GrBezierEffect.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040027#include "src/gpu/geometry/GrPathUtils.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000028#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050029#include "src/gpu/ops/GrAAHairLinePathRenderer.h"
30#include "src/gpu/ops/GrMeshDrawOp.h"
Robert Phillips55f681f2020-02-28 08:58:15 -050031#include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h"
bsalomon@google.com4647f902013-03-26 14:45:27 +000032
bsalomoned0bcad2015-05-04 10:36:42 -070033#define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true>
34
bsalomon@google.comaeb21602011-08-30 18:13:44 +000035// quadratics are rendered as 5-sided polys in order to bound the
36// AA stroke around the center-curve. See comments in push_quad_index_buffer and
egdaniel@google.com5383a752013-07-12 20:15:34 +000037// bloat_quad. Quadratics and conics share an index buffer
bsalomon@google.comaeb21602011-08-30 18:13:44 +000038
robertphillips@google.comada90da2013-09-18 22:14:49 +000039// lines are rendered as:
40// *______________*
41// |\ -_______ /|
42// | \ \ / |
43// | *--------* |
44// | / ______/ \ |
45// */_-__________\*
46// For: 6 vertices and 18 indices (for 6 triangles)
bsalomon@google.comaeb21602011-08-30 18:13:44 +000047
joshualitt5ead6da2014-10-22 16:00:29 -070048// Each quadratic is rendered as a five sided polygon. This poly bounds
49// the quadratic's bounding triangle but has been expanded so that the
50// 1-pixel wide area around the curve is inside the poly.
51// If a,b,c are the original control points then the poly a0,b0,c0,c1,a1
52// that is rendered would look like this:
53// b0
54// b
55//
56// a0 c0
57// a c
58// a1 c1
egdaniel14afb432014-12-22 10:57:08 -080059// Each is drawn as three triangles ((a0,a1,b0), (b0,c1,c0), (a1,c1,b0))
60// specified by these 9 indices:
joshualitt5ead6da2014-10-22 16:00:29 -070061static const uint16_t kQuadIdxBufPattern[] = {
62 0, 1, 2,
63 2, 4, 3,
64 1, 4, 2
65};
bsalomon@google.comaeb21602011-08-30 18:13:44 +000066
joshualitt5ead6da2014-10-22 16:00:29 -070067static const int kIdxsPerQuad = SK_ARRAY_COUNT(kQuadIdxBufPattern);
68static const int kQuadNumVertices = 5;
69static const int kQuadsNumInIdxBuffer = 256;
bsalomoned0bcad2015-05-04 10:36:42 -070070GR_DECLARE_STATIC_UNIQUE_KEY(gQuadsIndexBufferKey);
71
Brian Salomond28a79d2017-10-16 13:01:07 -040072static sk_sp<const GrBuffer> get_quads_index_buffer(GrResourceProvider* resourceProvider) {
bsalomoned0bcad2015-05-04 10:36:42 -070073 GR_DEFINE_STATIC_UNIQUE_KEY(gQuadsIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -040074 return resourceProvider->findOrCreatePatternedIndexBuffer(
bsalomoned0bcad2015-05-04 10:36:42 -070075 kQuadIdxBufPattern, kIdxsPerQuad, kQuadsNumInIdxBuffer, kQuadNumVertices,
76 gQuadsIndexBufferKey);
77}
jvanverth@google.com681ccf02013-08-16 14:51:51 +000078
bsalomon@google.comaeb21602011-08-30 18:13:44 +000079
joshualitt5ead6da2014-10-22 16:00:29 -070080// Each line segment is rendered as two quads and two triangles.
81// p0 and p1 have alpha = 1 while all other points have alpha = 0.
82// The four external points are offset 1 pixel perpendicular to the
83// line and half a pixel parallel to the line.
84//
85// p4 p5
86// p0 p1
87// p2 p3
88//
89// Each is drawn as six triangles specified by these 18 indices:
jvanverth@google.com681ccf02013-08-16 14:51:51 +000090
joshualitt5ead6da2014-10-22 16:00:29 -070091static const uint16_t kLineSegIdxBufPattern[] = {
92 0, 1, 3,
93 0, 3, 2,
94 0, 4, 5,
95 0, 5, 1,
96 0, 2, 4,
97 1, 5, 3
98};
skia.committer@gmail.com74758112013-08-17 07:01:54 +000099
joshualitt5ead6da2014-10-22 16:00:29 -0700100static const int kIdxsPerLineSeg = SK_ARRAY_COUNT(kLineSegIdxBufPattern);
101static const int kLineSegNumVertices = 6;
102static const int kLineSegsNumInIdxBuffer = 256;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000103
bsalomoned0bcad2015-05-04 10:36:42 -0700104GR_DECLARE_STATIC_UNIQUE_KEY(gLinesIndexBufferKey);
105
Brian Salomond28a79d2017-10-16 13:01:07 -0400106static sk_sp<const GrBuffer> get_lines_index_buffer(GrResourceProvider* resourceProvider) {
bsalomoned0bcad2015-05-04 10:36:42 -0700107 GR_DEFINE_STATIC_UNIQUE_KEY(gLinesIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400108 return resourceProvider->findOrCreatePatternedIndexBuffer(
bsalomoned0bcad2015-05-04 10:36:42 -0700109 kLineSegIdxBufPattern, kIdxsPerLineSeg, kLineSegsNumInIdxBuffer, kLineSegNumVertices,
110 gLinesIndexBufferKey);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000111}
112
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000113// Takes 178th time of logf on Z600 / VC2010
bsalomoned0bcad2015-05-04 10:36:42 -0700114static int get_float_exp(float x) {
Brian Salomon4dea72a2019-12-18 10:43:10 -0500115 static_assert(sizeof(int) == sizeof(float));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000116#ifdef SK_DEBUG
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000117 static bool tested;
118 if (!tested) {
119 tested = true;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000120 SkASSERT(get_float_exp(0.25f) == -2);
121 SkASSERT(get_float_exp(0.3f) == -2);
122 SkASSERT(get_float_exp(0.5f) == -1);
123 SkASSERT(get_float_exp(1.f) == 0);
124 SkASSERT(get_float_exp(2.f) == 1);
125 SkASSERT(get_float_exp(2.5f) == 1);
126 SkASSERT(get_float_exp(8.f) == 3);
127 SkASSERT(get_float_exp(100.f) == 6);
128 SkASSERT(get_float_exp(1000.f) == 9);
129 SkASSERT(get_float_exp(1024.f) == 10);
130 SkASSERT(get_float_exp(3000000.f) == 21);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000131 }
132#endif
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000133 const int* iptr = (const int*)&x;
134 return (((*iptr) & 0x7f800000) >> 23) - 127;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000135}
136
egdaniel@google.com5383a752013-07-12 20:15:34 +0000137// Uses the max curvature function for quads to estimate
138// where to chop the conic. If the max curvature is not
139// found along the curve segment it will return 1 and
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000140// dst[0] is the original conic. If it returns 2 the dst[0]
egdaniel@google.com5383a752013-07-12 20:15:34 +0000141// and dst[1] are the two new conics.
bsalomoned0bcad2015-05-04 10:36:42 -0700142static int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000143 SkScalar t = SkFindQuadMaxCurvature(src);
Chris Dalton1d474dd2018-07-24 01:08:31 -0600144 if (t == 0 || t == 1) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000145 if (dst) {
146 dst[0].set(src, weight);
147 }
148 return 1;
149 } else {
150 if (dst) {
151 SkConic conic;
152 conic.set(src, weight);
caryclark414c4292016-09-26 11:03:54 -0700153 if (!conic.chopAt(t, dst)) {
154 dst[0].set(src, weight);
155 return 1;
156 }
egdaniel@google.com5383a752013-07-12 20:15:34 +0000157 }
158 return 2;
159 }
160}
161
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000162// Calls split_conic on the entire conic and then once more on each subsection.
163// Most cases will result in either 1 conic (chop point is not within t range)
164// or 3 points (split once and then one subsection is split again).
bsalomoned0bcad2015-05-04 10:36:42 -0700165static int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000166 SkConic dstTemp[2];
167 int conicCnt = split_conic(src, dstTemp, weight);
168 if (2 == conicCnt) {
169 int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW);
170 conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW);
171 } else {
172 dst[0] = dstTemp[0];
173 }
174 return conicCnt;
175}
176
egdaniel@google.com5383a752013-07-12 20:15:34 +0000177// returns 0 if quad/conic is degen or close to it
178// in this case approx the path with lines
179// otherwise returns 1
bsalomoned0bcad2015-05-04 10:36:42 -0700180static int is_degen_quad_or_conic(const SkPoint p[3], SkScalar* dsqd) {
jvanverthcfeb85f2016-04-29 07:38:10 -0700181 static const SkScalar gDegenerateToLineTol = GrPathUtils::kDefaultTolerance;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000182 static const SkScalar gDegenerateToLineTolSqd =
Mike Reed8be952a2017-02-13 20:44:33 -0500183 gDegenerateToLineTol * gDegenerateToLineTol;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000184
Cary Clarkdf429f32017-11-08 11:44:31 -0500185 if (SkPointPriv::DistanceToSqd(p[0], p[1]) < gDegenerateToLineTolSqd ||
186 SkPointPriv::DistanceToSqd(p[1], p[2]) < gDegenerateToLineTolSqd) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000187 return 1;
188 }
189
Cary Clarkdf429f32017-11-08 11:44:31 -0500190 *dsqd = SkPointPriv::DistanceToLineBetweenSqd(p[1], p[0], p[2]);
joshualitt63648072015-02-19 10:25:21 -0800191 if (*dsqd < gDegenerateToLineTolSqd) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000192 return 1;
193 }
194
Cary Clarkdf429f32017-11-08 11:44:31 -0500195 if (SkPointPriv::DistanceToLineBetweenSqd(p[2], p[1], p[0]) < gDegenerateToLineTolSqd) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000196 return 1;
197 }
198 return 0;
199}
200
bsalomoned0bcad2015-05-04 10:36:42 -0700201static int is_degen_quad_or_conic(const SkPoint p[3]) {
joshualitt63648072015-02-19 10:25:21 -0800202 SkScalar dsqd;
203 return is_degen_quad_or_conic(p, &dsqd);
204}
205
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000206// we subdivide the quads to avoid huge overfill
207// if it returns -1 then should be drawn as lines
bsalomoned0bcad2015-05-04 10:36:42 -0700208static int num_quad_subdivs(const SkPoint p[3]) {
joshualitt63648072015-02-19 10:25:21 -0800209 SkScalar dsqd;
210 if (is_degen_quad_or_conic(p, &dsqd)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000211 return -1;
212 }
213
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000214 // tolerance of triangle height in pixels
215 // tuned on windows Quadro FX 380 / Z600
216 // trade off of fill vs cpu time on verts
217 // maybe different when do this using gpu (geo or tess shaders)
218 static const SkScalar gSubdivTol = 175 * SK_Scalar1;
219
Mike Reed8be952a2017-02-13 20:44:33 -0500220 if (dsqd <= gSubdivTol * gSubdivTol) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000221 return 0;
222 } else {
robertphillips@google.com87379e12013-03-29 12:11:10 +0000223 static const int kMaxSub = 4;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000224 // subdividing the quad reduces d by 4. so we want x = log4(d/tol)
225 // = log4(d*d/tol*tol)/2
226 // = log2(d*d/tol*tol)
227
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000228 // +1 since we're ignoring the mantissa contribution.
229 int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
Brian Osman788b9162020-02-07 10:36:46 -0500230 log = std::min(std::max(0, log), kMaxSub);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000231 return log;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000232 }
233}
234
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000235/**
236 * Generates the lines and quads to be rendered. Lines are always recorded in
237 * device space. We will do a device space bloat to account for the 1pixel
238 * thickness.
239 * Quads are recorded in device space unless m contains
240 * perspective, then in they are in src space. We do this because we will
241 * subdivide large quads to reduce over-fill. This subdivision has to be
242 * performed before applying the perspective matrix.
243 */
bsalomoned0bcad2015-05-04 10:36:42 -0700244static int gather_lines_and_quads(const SkPath& path,
245 const SkMatrix& m,
246 const SkIRect& devClipBounds,
Brian Osmancf3dc292017-06-28 16:45:32 -0400247 SkScalar capLength,
Brian Salomon969a7382018-05-09 13:28:44 -0400248 bool convertConicsToQuads,
bsalomoned0bcad2015-05-04 10:36:42 -0700249 GrAAHairLinePathRenderer::PtArray* lines,
250 GrAAHairLinePathRenderer::PtArray* quads,
251 GrAAHairLinePathRenderer::PtArray* conics,
252 GrAAHairLinePathRenderer::IntArray* quadSubdivCnts,
253 GrAAHairLinePathRenderer::FloatArray* conicWeights) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000254 SkPath::Iter iter(path, false);
255
256 int totalQuadCount = 0;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000257 SkRect bounds;
258 SkIRect ibounds;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000259
260 bool persp = m.hasPerspective();
261
Brian Osmancf3dc292017-06-28 16:45:32 -0400262 // Whenever a degenerate, zero-length contour is encountered, this code will insert a
263 // 'capLength' x-aligned line segment. Since this is rendering hairlines it is hoped this will
264 // suffice for AA square & circle capping.
265 int verbsInContour = 0; // Does not count moves
266 bool seenZeroLengthVerb = false;
267 SkPoint zeroVerbPt;
268
Brian Salomon969a7382018-05-09 13:28:44 -0400269 // Adds a quad that has already been chopped to the list and checks for quads that are close to
270 // lines. Also does a bounding box check. It takes points that are in src space and device
271 // space. The src points are only required if the view matrix has perspective.
272 auto addChoppedQuad = [&](const SkPoint srcPts[3], const SkPoint devPts[4],
273 bool isContourStart) {
274 SkRect bounds;
275 SkIRect ibounds;
276 bounds.setBounds(devPts, 3);
277 bounds.outset(SK_Scalar1, SK_Scalar1);
278 bounds.roundOut(&ibounds);
279 // We only need the src space space pts when not in perspective.
280 SkASSERT(srcPts || !persp);
281 if (SkIRect::Intersects(devClipBounds, ibounds)) {
282 int subdiv = num_quad_subdivs(devPts);
283 SkASSERT(subdiv >= -1);
284 if (-1 == subdiv) {
285 SkPoint* pts = lines->push_back_n(4);
286 pts[0] = devPts[0];
287 pts[1] = devPts[1];
288 pts[2] = devPts[1];
289 pts[3] = devPts[2];
290 if (isContourStart && pts[0] == pts[1] && pts[2] == pts[3]) {
291 seenZeroLengthVerb = true;
292 zeroVerbPt = pts[0];
293 }
294 } else {
295 // when in perspective keep quads in src space
296 const SkPoint* qPts = persp ? srcPts : devPts;
297 SkPoint* pts = quads->push_back_n(3);
298 pts[0] = qPts[0];
299 pts[1] = qPts[1];
300 pts[2] = qPts[2];
301 quadSubdivCnts->push_back() = subdiv;
302 totalQuadCount += 1 << subdiv;
303 }
304 }
305 };
306
307 // Applies the view matrix to quad src points and calls the above helper.
308 auto addSrcChoppedQuad = [&](const SkPoint srcSpaceQuadPts[3], bool isContourStart) {
309 SkPoint devPts[3];
310 m.mapPoints(devPts, srcSpaceQuadPts, 3);
311 addChoppedQuad(srcSpaceQuadPts, devPts, isContourStart);
312 };
313
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000314 for (;;) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000315 SkPoint pathPts[4];
Mike Reedba7e9a62019-08-16 13:30:34 -0400316 SkPath::Verb verb = iter.next(pathPts);
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000317 switch (verb) {
Brian Salomon969a7382018-05-09 13:28:44 -0400318 case SkPath::kConic_Verb:
319 if (convertConicsToQuads) {
320 SkScalar weight = iter.conicWeight();
321 SkAutoConicToQuads converter;
Brian Osmane3deee12018-11-20 11:10:15 -0500322 const SkPoint* quadPts = converter.computeQuads(pathPts, weight, 0.25f);
Brian Salomon969a7382018-05-09 13:28:44 -0400323 for (int i = 0; i < converter.countQuads(); ++i) {
324 addSrcChoppedQuad(quadPts + 2 * i, !verbsInContour && 0 == i);
325 }
326 } else {
327 SkConic dst[4];
328 // We chop the conics to create tighter clipping to hide error
329 // that appears near max curvature of very thin conics. Thin
330 // hyperbolas with high weight still show error.
331 int conicCnt = chop_conic(pathPts, dst, iter.conicWeight());
332 for (int i = 0; i < conicCnt; ++i) {
333 SkPoint devPts[4];
334 SkPoint* chopPnts = dst[i].fPts;
335 m.mapPoints(devPts, chopPnts, 3);
336 bounds.setBounds(devPts, 3);
337 bounds.outset(SK_Scalar1, SK_Scalar1);
338 bounds.roundOut(&ibounds);
339 if (SkIRect::Intersects(devClipBounds, ibounds)) {
340 if (is_degen_quad_or_conic(devPts)) {
341 SkPoint* pts = lines->push_back_n(4);
342 pts[0] = devPts[0];
343 pts[1] = devPts[1];
344 pts[2] = devPts[1];
345 pts[3] = devPts[2];
346 if (verbsInContour == 0 && i == 0 && pts[0] == pts[1] &&
347 pts[2] == pts[3]) {
348 seenZeroLengthVerb = true;
349 zeroVerbPt = pts[0];
350 }
351 } else {
352 // when in perspective keep conics in src space
353 SkPoint* cPts = persp ? chopPnts : devPts;
354 SkPoint* pts = conics->push_back_n(3);
355 pts[0] = cPts[0];
356 pts[1] = cPts[1];
357 pts[2] = cPts[2];
358 conicWeights->push_back() = dst[i].fW;
Brian Osmancf3dc292017-06-28 16:45:32 -0400359 }
egdaniel@google.com5383a752013-07-12 20:15:34 +0000360 }
361 }
362 }
Brian Osmancf3dc292017-06-28 16:45:32 -0400363 verbsInContour++;
reed@google.com277c3f82013-05-31 15:17:50 +0000364 break;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000365 case SkPath::kMove_Verb:
Brian Osmancf3dc292017-06-28 16:45:32 -0400366 // New contour (and last one was unclosed). If it was just a zero length drawing
367 // operation, and we're supposed to draw caps, then add a tiny line.
368 if (seenZeroLengthVerb && verbsInContour == 1 && capLength > 0) {
369 SkPoint* pts = lines->push_back_n(2);
370 pts[0] = SkPoint::Make(zeroVerbPt.fX - capLength, zeroVerbPt.fY);
371 pts[1] = SkPoint::Make(zeroVerbPt.fX + capLength, zeroVerbPt.fY);
372 }
373 verbsInContour = 0;
374 seenZeroLengthVerb = false;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000375 break;
Brian Salomon969a7382018-05-09 13:28:44 -0400376 case SkPath::kLine_Verb: {
377 SkPoint devPts[2];
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000378 m.mapPoints(devPts, pathPts, 2);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000379 bounds.setBounds(devPts, 2);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000380 bounds.outset(SK_Scalar1, SK_Scalar1);
381 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000382 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000383 SkPoint* pts = lines->push_back_n(2);
384 pts[0] = devPts[0];
385 pts[1] = devPts[1];
Brian Osmancf3dc292017-06-28 16:45:32 -0400386 if (verbsInContour == 0 && pts[0] == pts[1]) {
387 seenZeroLengthVerb = true;
388 zeroVerbPt = pts[0];
389 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000390 }
Brian Osmancf3dc292017-06-28 16:45:32 -0400391 verbsInContour++;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000392 break;
Brian Salomon969a7382018-05-09 13:28:44 -0400393 }
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000394 case SkPath::kQuad_Verb: {
395 SkPoint choppedPts[5];
396 // Chopping the quad helps when the quad is either degenerate or nearly degenerate.
397 // When it is degenerate it allows the approximation with lines to work since the
398 // chop point (if there is one) will be at the parabola's vertex. In the nearly
399 // degenerate the QuadUVMatrix computed for the points is almost singular which
400 // can cause rendering artifacts.
401 int n = SkChopQuadAtMaxCurvature(pathPts, choppedPts);
402 for (int i = 0; i < n; ++i) {
Brian Salomon969a7382018-05-09 13:28:44 -0400403 addSrcChoppedQuad(choppedPts + i * 2, !verbsInContour && 0 == i);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000404 }
Brian Osmancf3dc292017-06-28 16:45:32 -0400405 verbsInContour++;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000406 break;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000407 }
Brian Salomon969a7382018-05-09 13:28:44 -0400408 case SkPath::kCubic_Verb: {
409 SkPoint devPts[4];
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000410 m.mapPoints(devPts, pathPts, 4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000411 bounds.setBounds(devPts, 4);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000412 bounds.outset(SK_Scalar1, SK_Scalar1);
413 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000414 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000415 PREALLOC_PTARRAY(32) q;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000416 // We convert cubics to quadratics (for now).
417 // In perspective have to do conversion in src space.
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000418 if (persp) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000419 SkScalar tolScale =
bsalomon18fab302016-02-16 08:00:05 -0800420 GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m, path.getBounds());
421 GrPathUtils::convertCubicToQuads(pathPts, tolScale, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000422 } else {
bsalomon18fab302016-02-16 08:00:05 -0800423 GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000424 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000425 for (int i = 0; i < q.count(); i += 3) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000426 if (persp) {
Brian Salomon969a7382018-05-09 13:28:44 -0400427 addSrcChoppedQuad(&q[i], !verbsInContour && 0 == i);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000428 } else {
Brian Salomon969a7382018-05-09 13:28:44 -0400429 addChoppedQuad(nullptr, &q[i], !verbsInContour && 0 == i);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000430 }
431 }
432 }
Brian Osmancf3dc292017-06-28 16:45:32 -0400433 verbsInContour++;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000434 break;
Brian Salomon969a7382018-05-09 13:28:44 -0400435 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000436 case SkPath::kClose_Verb:
Brian Osmancf3dc292017-06-28 16:45:32 -0400437 // Contour is closed, so we don't need to grow the starting line, unless it's
438 // *just* a zero length subpath. (SVG Spec 11.4, 'stroke').
439 if (capLength > 0) {
440 if (seenZeroLengthVerb && verbsInContour == 1) {
441 SkPoint* pts = lines->push_back_n(2);
442 pts[0] = SkPoint::Make(zeroVerbPt.fX - capLength, zeroVerbPt.fY);
443 pts[1] = SkPoint::Make(zeroVerbPt.fX + capLength, zeroVerbPt.fY);
444 } else if (verbsInContour == 0) {
445 // Contour was (moveTo, close). Add a line.
Brian Salomon969a7382018-05-09 13:28:44 -0400446 SkPoint devPts[2];
Brian Osmancf3dc292017-06-28 16:45:32 -0400447 m.mapPoints(devPts, pathPts, 1);
448 devPts[1] = devPts[0];
449 bounds.setBounds(devPts, 2);
450 bounds.outset(SK_Scalar1, SK_Scalar1);
451 bounds.roundOut(&ibounds);
452 if (SkIRect::Intersects(devClipBounds, ibounds)) {
453 SkPoint* pts = lines->push_back_n(2);
454 pts[0] = SkPoint::Make(devPts[0].fX - capLength, devPts[0].fY);
455 pts[1] = SkPoint::Make(devPts[1].fX + capLength, devPts[1].fY);
456 }
457 }
458 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000459 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000460 case SkPath::kDone_Verb:
Brian Osmancf3dc292017-06-28 16:45:32 -0400461 if (seenZeroLengthVerb && verbsInContour == 1 && capLength > 0) {
462 // Path ended with a dangling (moveTo, line|quad|etc). If the final verb is
463 // degenerate, we need to draw a line.
464 SkPoint* pts = lines->push_back_n(2);
465 pts[0] = SkPoint::Make(zeroVerbPt.fX - capLength, zeroVerbPt.fY);
466 pts[1] = SkPoint::Make(zeroVerbPt.fX + capLength, zeroVerbPt.fY);
467 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000468 return totalQuadCount;
469 }
470 }
471}
472
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000473struct LineVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000474 SkPoint fPos;
egdaniele27065a2014-11-06 08:00:48 -0800475 float fCoverage;
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000476};
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000477
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000478struct BezierVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000479 SkPoint fPos;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000480 union {
481 struct {
csmartdaltoncc261272017-03-23 13:38:45 -0600482 SkScalar fKLM[3];
egdaniel@google.com5383a752013-07-12 20:15:34 +0000483 } fConic;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000484 SkVector fQuadCoord;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000485 struct {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000486 SkScalar fBogus[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000487 };
488 };
489};
egdaniel@google.com5383a752013-07-12 20:15:34 +0000490
Brian Salomon4dea72a2019-12-18 10:43:10 -0500491static_assert(sizeof(BezierVertex) == 3 * sizeof(SkPoint));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000492
bsalomoned0bcad2015-05-04 10:36:42 -0700493static void intersect_lines(const SkPoint& ptA, const SkVector& normA,
494 const SkPoint& ptB, const SkVector& normB,
495 SkPoint* result) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000496
497 SkScalar lineAW = -normA.dot(ptA);
498 SkScalar lineBW = -normB.dot(ptB);
499
Mike Reed8be952a2017-02-13 20:44:33 -0500500 SkScalar wInv = normA.fX * normB.fY - normA.fY * normB.fX;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000501 wInv = SkScalarInvert(wInv);
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400502 if (!SkScalarIsFinite(wInv)) {
503 // lines are parallel, pick the point in between
504 *result = (ptA + ptB)*SK_ScalarHalf;
505 *result += normA;
506 } else {
507 result->fX = normA.fY * lineBW - lineAW * normB.fY;
508 result->fX *= wInv;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000509
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400510 result->fY = lineAW * normB.fX - normA.fX * lineBW;
511 result->fY *= wInv;
512 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000513}
514
bsalomoned0bcad2015-05-04 10:36:42 -0700515static void set_uv_quad(const SkPoint qpts[3], BezierVertex verts[kQuadNumVertices]) {
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000516 // this should be in the src space, not dev coords, when we have perspective
517 GrPathUtils::QuadUVMatrix DevToUV(qpts);
Brian Osman568bec72018-12-26 16:48:25 -0500518 DevToUV.apply(verts, kQuadNumVertices, sizeof(BezierVertex), sizeof(SkPoint));
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000519}
520
bsalomoned0bcad2015-05-04 10:36:42 -0700521static void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice,
522 const SkMatrix* toSrc, BezierVertex verts[kQuadNumVertices]) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000523 SkASSERT(!toDevice == !toSrc);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000524 // original quad is specified by tri a,b,c
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000525 SkPoint a = qpts[0];
526 SkPoint b = qpts[1];
527 SkPoint c = qpts[2];
528
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000529 if (toDevice) {
530 toDevice->mapPoints(&a, 1);
531 toDevice->mapPoints(&b, 1);
532 toDevice->mapPoints(&c, 1);
533 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000534 // make a new poly where we replace a and c by a 1-pixel wide edges orthog
535 // to edges ab and bc:
536 //
537 // before | after
538 // | b0
539 // b |
540 // |
541 // | a0 c0
542 // a c | a1 c1
543 //
544 // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c,
545 // respectively.
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000546 BezierVertex& a0 = verts[0];
547 BezierVertex& a1 = verts[1];
548 BezierVertex& b0 = verts[2];
549 BezierVertex& c0 = verts[3];
550 BezierVertex& c1 = verts[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000551
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000552 SkVector ab = b;
553 ab -= a;
554 SkVector ac = c;
555 ac -= a;
556 SkVector cb = b;
557 cb -= c;
558
Robert Phillips2c2303e2021-02-04 11:04:01 -0500559 // After the transform (or due to floating point math) we might have a line,
560 // try to do something reasonable
561 if (SkPointPriv::LengthSqd(ab) <= SK_ScalarNearlyZero*SK_ScalarNearlyZero) {
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400562 ab = cb;
563 }
Robert Phillips2c2303e2021-02-04 11:04:01 -0500564 if (SkPointPriv::LengthSqd(cb) <= SK_ScalarNearlyZero*SK_ScalarNearlyZero) {
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400565 cb = ab;
566 }
567
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000568 // We should have already handled degenerates
Robert Phillips2c2303e2021-02-04 11:04:01 -0500569 SkASSERT(ab.length() > 0 && cb.length() > 0);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000570
571 ab.normalize();
Brian Salomon0235c642018-08-31 12:04:18 -0400572 SkVector abN = SkPointPriv::MakeOrthog(ab, SkPointPriv::kLeft_Side);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000573 if (abN.dot(ac) > 0) {
574 abN.negate();
575 }
576
577 cb.normalize();
Brian Salomon0235c642018-08-31 12:04:18 -0400578 SkVector cbN = SkPointPriv::MakeOrthog(cb, SkPointPriv::kLeft_Side);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000579 if (cbN.dot(ac) < 0) {
580 cbN.negate();
581 }
582
583 a0.fPos = a;
584 a0.fPos += abN;
585 a1.fPos = a;
586 a1.fPos -= abN;
587
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400588 if (toDevice && SkPointPriv::LengthSqd(ac) <= SK_ScalarNearlyZero*SK_ScalarNearlyZero) {
589 c = b;
590 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000591 c0.fPos = c;
592 c0.fPos += cbN;
593 c1.fPos = c;
594 c1.fPos -= cbN;
595
596 intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos);
597
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000598 if (toSrc) {
Brian Salomonfa3783f2018-01-05 13:49:07 -0500599 SkMatrixPriv::MapPointsWithStride(*toSrc, &verts[0].fPos, sizeof(BezierVertex),
600 kQuadNumVertices);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000601 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000602}
603
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000604// Equations based off of Loop-Blinn Quadratic GPU Rendering
egdaniel@google.com5383a752013-07-12 20:15:34 +0000605// Input Parametric:
606// 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)
607// Output Implicit:
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000608// f(x, y, w) = f(P) = K^2 - LM
609// K = dot(k, P), L = dot(l, P), M = dot(m, P)
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000610// k, l, m are calculated in function GrPathUtils::getConicKLM
bsalomoned0bcad2015-05-04 10:36:42 -0700611static void set_conic_coeffs(const SkPoint p[3], BezierVertex verts[kQuadNumVertices],
612 const SkScalar weight) {
csmartdaltoncc261272017-03-23 13:38:45 -0600613 SkMatrix klm;
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000614
csmartdaltoncc261272017-03-23 13:38:45 -0600615 GrPathUtils::getConicKLM(p, weight, &klm);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000616
joshualitt5ead6da2014-10-22 16:00:29 -0700617 for (int i = 0; i < kQuadNumVertices; ++i) {
Cary Clarke4442cb2017-10-18 11:46:18 -0400618 const SkPoint3 pt3 = {verts[i].fPos.x(), verts[i].fPos.y(), 1.f};
619 klm.mapHomogeneousPoints((SkPoint3* ) verts[i].fConic.fKLM, &pt3, 1);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000620 }
621}
622
bsalomoned0bcad2015-05-04 10:36:42 -0700623static void add_conics(const SkPoint p[3],
624 const SkScalar weight,
625 const SkMatrix* toDevice,
626 const SkMatrix* toSrc,
627 BezierVertex** vert) {
joshualitt7bc18b72015-02-03 16:41:41 -0800628 bloat_quad(p, toDevice, toSrc, *vert);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000629 set_conic_coeffs(p, *vert, weight);
joshualitt5ead6da2014-10-22 16:00:29 -0700630 *vert += kQuadNumVertices;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000631}
632
bsalomoned0bcad2015-05-04 10:36:42 -0700633static void add_quads(const SkPoint p[3],
634 int subdiv,
635 const SkMatrix* toDevice,
636 const SkMatrix* toSrc,
637 BezierVertex** vert) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000638 SkASSERT(subdiv >= 0);
Jim Van Verth14982c82020-07-15 15:42:19 -0400639 // temporary vertex storage to avoid reading the vertex buffer
Jim Van Verth3557ab42020-07-28 09:46:03 -0400640 BezierVertex outVerts[kQuadNumVertices] = {};
Jim Van Verth14982c82020-07-15 15:42:19 -0400641
642 // storage for the chopped quad
643 // pts 0,1,2 are the first quad, and 2,3,4 the second quad
644 SkPoint choppedQuadPts[5];
645 // start off with our original curve in the second quad slot
646 memcpy(&choppedQuadPts[2], p, 3*sizeof(SkPoint));
647
648 int stepCount = 1 << subdiv;
649 while (stepCount > 1) {
650 // The general idea is:
651 // * chop the quad using pts 2,3,4 as the input
652 // * write out verts using pts 0,1,2
653 // * now 2,3,4 is the remainder of the curve, chop again until all subdivisions are done
654 SkScalar h = 1.f / stepCount;
655 SkChopQuadAt(&choppedQuadPts[2], choppedQuadPts, h);
656
657 bloat_quad(choppedQuadPts, toDevice, toSrc, outVerts);
658 set_uv_quad(choppedQuadPts, outVerts);
659 memcpy(*vert, outVerts, kQuadNumVertices*sizeof(BezierVertex));
joshualitt5ead6da2014-10-22 16:00:29 -0700660 *vert += kQuadNumVertices;
Jim Van Verth14982c82020-07-15 15:42:19 -0400661 --stepCount;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000662 }
Jim Van Verth14982c82020-07-15 15:42:19 -0400663
664 // finish up, write out the final quad
665 bloat_quad(&choppedQuadPts[2], toDevice, toSrc, outVerts);
666 set_uv_quad(&choppedQuadPts[2], outVerts);
667 memcpy(*vert, outVerts, kQuadNumVertices * sizeof(BezierVertex));
668 *vert += kQuadNumVertices;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000669}
670
bsalomoned0bcad2015-05-04 10:36:42 -0700671static void add_line(const SkPoint p[2],
672 const SkMatrix* toSrc,
673 uint8_t coverage,
674 LineVertex** vert) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000675 const SkPoint& a = p[0];
676 const SkPoint& b = p[1];
677
robertphillips@google.comada90da2013-09-18 22:14:49 +0000678 SkVector ortho, vec = b;
679 vec -= a;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000680
Cary Clarkdf429f32017-11-08 11:44:31 -0500681 SkScalar lengthSqd = SkPointPriv::LengthSqd(vec);
Greg Daniel2e67dea2017-10-12 15:14:41 -0400682
robertphillips@google.comada90da2013-09-18 22:14:49 +0000683 if (vec.setLength(SK_ScalarHalf)) {
684 // Create a vector orthogonal to 'vec' and of unit length
685 ortho.fX = 2.0f * vec.fY;
686 ortho.fY = -2.0f * vec.fX;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000687
egdaniele27065a2014-11-06 08:00:48 -0800688 float floatCoverage = GrNormalizeByteToFloat(coverage);
689
Greg Daniel2e67dea2017-10-12 15:14:41 -0400690 if (lengthSqd >= 1.0f) {
691 // Relative to points a and b:
692 // The inner vertices are inset half a pixel along the line a,b
693 (*vert)[0].fPos = a + vec;
694 (*vert)[0].fCoverage = floatCoverage;
695 (*vert)[1].fPos = b - vec;
696 (*vert)[1].fCoverage = floatCoverage;
697 } else {
698 // The inner vertices are inset a distance of length(a,b) from the outer edge of
699 // geometry. For the "a" inset this is the same as insetting from b by half a pixel.
700 // The coverage is then modulated by the length. This gives us the correct
701 // coverage for rects shorter than a pixel as they get translated subpixel amounts
702 // inside of a pixel.
703 SkScalar length = SkScalarSqrt(lengthSqd);
704 (*vert)[0].fPos = b - vec;
705 (*vert)[0].fCoverage = floatCoverage * length;
706 (*vert)[1].fPos = a + vec;
707 (*vert)[1].fCoverage = floatCoverage * length;
708 }
709 // Relative to points a and b:
710 // The outer vertices are outset half a pixel along the line a,b and then a whole pixel
711 // orthogonally.
robertphillips@google.comada90da2013-09-18 22:14:49 +0000712 (*vert)[2].fPos = a - vec + ortho;
713 (*vert)[2].fCoverage = 0;
714 (*vert)[3].fPos = b + vec + ortho;
715 (*vert)[3].fCoverage = 0;
716 (*vert)[4].fPos = a - vec - ortho;
717 (*vert)[4].fCoverage = 0;
718 (*vert)[5].fPos = b + vec - ortho;
719 (*vert)[5].fCoverage = 0;
720
bsalomon49f085d2014-09-05 13:34:00 -0700721 if (toSrc) {
Brian Salomonfa3783f2018-01-05 13:49:07 -0500722 SkMatrixPriv::MapPointsWithStride(*toSrc, &(*vert)->fPos, sizeof(LineVertex),
723 kLineSegNumVertices);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000724 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000725 } else {
726 // just make it degenerate and likely offscreen
joshualitt5ead6da2014-10-22 16:00:29 -0700727 for (int i = 0; i < kLineSegNumVertices; ++i) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000728 (*vert)[i].fPos.set(SK_ScalarMax, SK_ScalarMax);
729 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000730 }
731
joshualitt5ead6da2014-10-22 16:00:29 -0700732 *vert += kLineSegNumVertices;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000733}
734
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000735///////////////////////////////////////////////////////////////////////////////
736
Chris Dalton5ed44232017-09-07 13:22:46 -0600737GrPathRenderer::CanDrawPath
738GrAAHairLinePathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600739 if (GrAAType::kCoverage != args.fAAType) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600740 return CanDrawPath::kNo;
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000741 }
742
bsalomon8acedde2016-06-24 10:42:16 -0700743 if (!IsStrokeHairlineOrEquivalent(args.fShape->style(), *args.fViewMatrix, nullptr)) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600744 return CanDrawPath::kNo;
bsalomon6663acf2016-05-10 09:14:17 -0700745 }
746
747 // We don't currently handle dashing in this class though perhaps we should.
bsalomon8acedde2016-06-24 10:42:16 -0700748 if (args.fShape->style().pathEffect()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600749 return CanDrawPath::kNo;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000750 }
751
bsalomon8acedde2016-06-24 10:42:16 -0700752 if (SkPath::kLine_SegmentMask == args.fShape->segmentMask() ||
Eric Karl5c779752017-05-08 12:02:07 -0700753 args.fCaps->shaderCaps()->shaderDerivativeSupport()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600754 return CanDrawPath::kYes;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000755 }
bsalomon8acedde2016-06-24 10:42:16 -0700756
Chris Dalton5ed44232017-09-07 13:22:46 -0600757 return CanDrawPath::kNo;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000758}
759
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000760template <class VertexType>
joshualitt8059eb92014-12-29 15:10:07 -0800761bool check_bounds(const SkMatrix& viewMatrix, const SkRect& devBounds, void* vertices, int vCount)
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000762{
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000763 SkRect tolDevBounds = devBounds;
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000764 // The bounds ought to be tight, but in perspective the below code runs the verts
765 // through the view matrix to get back to dev coords, which can introduce imprecision.
joshualitt8059eb92014-12-29 15:10:07 -0800766 if (viewMatrix.hasPerspective()) {
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000767 tolDevBounds.outset(SK_Scalar1 / 1000, SK_Scalar1 / 1000);
768 } else {
769 // Non-persp matrices cause this path renderer to draw in device space.
joshualitt8059eb92014-12-29 15:10:07 -0800770 SkASSERT(viewMatrix.isIdentity());
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000771 }
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000772 SkRect actualBounds;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000773
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000774 VertexType* verts = reinterpret_cast<VertexType*>(vertices);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000775 bool first = true;
776 for (int i = 0; i < vCount; ++i) {
777 SkPoint pos = verts[i].fPos;
778 // This is a hack to workaround the fact that we move some degenerate segments offscreen.
779 if (SK_ScalarMax == pos.fX) {
780 continue;
781 }
joshualitt8059eb92014-12-29 15:10:07 -0800782 viewMatrix.mapPoints(&pos, 1);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000783 if (first) {
Mike Reed92b33352019-08-24 19:39:13 -0400784 actualBounds.setLTRB(pos.fX, pos.fY, pos.fX, pos.fY);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000785 first = false;
786 } else {
Mike Reed185ffe92018-01-08 17:09:54 -0500787 SkRectPriv::GrowToInclude(&actualBounds, pos);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000788 }
789 }
790 if (!first) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000791 return tolDevBounds.contains(actualBounds);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000792 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000793
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000794 return true;
795}
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000796
Brian Salomona531f252017-07-07 13:29:28 -0400797class AAHairlineOp final : public GrMeshDrawOp {
798private:
799 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
800
joshualitt7bc18b72015-02-03 16:41:41 -0800801public:
Brian Salomon25a88092016-12-01 09:36:50 -0500802 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700803
Herb Derbyc76d4092020-10-07 16:46:15 -0400804 static GrOp::Owner Make(GrRecordingContext* context,
805 GrPaint&& paint,
806 const SkMatrix& viewMatrix,
807 const SkPath& path,
808 const GrStyle& style,
809 const SkIRect& devClipBounds,
810 const GrUserStencilSettings* stencilSettings) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500811 SkScalar hairlineCoverage;
812 uint8_t newCoverage = 0xff;
813 if (GrPathRenderer::IsStrokeHairlineOrEquivalent(style, viewMatrix, &hairlineCoverage)) {
814 newCoverage = SkScalarRoundToInt(hairlineCoverage * 0xff);
815 }
joshualitt7bc18b72015-02-03 16:41:41 -0800816
Brian Osmancf3dc292017-06-28 16:45:32 -0400817 const SkStrokeRec& stroke = style.strokeRec();
818 SkScalar capLength = SkPaint::kButt_Cap != stroke.getCap() ? hairlineCoverage * 0.5f : 0.0f;
819
Robert Phillips7c525e62018-06-12 10:11:12 -0400820 return Helper::FactoryHelper<AAHairlineOp>(context, std::move(paint), newCoverage,
821 viewMatrix, path,
Brian Salomona531f252017-07-07 13:29:28 -0400822 devClipBounds, capLength, stencilSettings);
823 }
824
Herb Derbyc76d4092020-10-07 16:46:15 -0400825 AAHairlineOp(GrProcessorSet* processorSet,
Brian Osmancf860852018-10-31 14:04:39 -0400826 const SkPMColor4f& color,
Brian Salomona531f252017-07-07 13:29:28 -0400827 uint8_t coverage,
828 const SkMatrix& viewMatrix,
829 const SkPath& path,
830 SkIRect devClipBounds,
831 SkScalar capLength,
832 const GrUserStencilSettings* stencilSettings)
833 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -0400834 , fHelper(processorSet, GrAAType::kCoverage, stencilSettings)
Brian Salomona531f252017-07-07 13:29:28 -0400835 , fColor(color)
836 , fCoverage(coverage) {
837 fPaths.emplace_back(PathData{viewMatrix, path, devClipBounds, capLength});
838
839 this->setTransformedBounds(path.getBounds(), viewMatrix, HasAABloat::kYes,
Greg Daniel5faf4742019-10-01 15:14:44 -0400840 IsHairline::kYes);
bsalomonf1703092016-06-29 18:41:53 -0700841 }
joshualitt7bc18b72015-02-03 16:41:41 -0800842
Brian Salomond0a0a652016-12-15 15:25:22 -0500843 const char* name() const override { return "AAHairlineOp"; }
joshualitt7bc18b72015-02-03 16:41:41 -0800844
Chris Dalton1706cbf2019-05-21 19:35:29 -0600845 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillips4f93c572020-03-18 08:13:53 -0400846
847 bool visited = false;
848 for (int i = 0; i < 3; ++i) {
849 if (fProgramInfos[i]) {
850 fProgramInfos[i]->visitFPProxies(func);
851 visited = true;
852 }
853 }
854
855 if (!visited) {
856 fHelper.visitProxies(func);
857 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400858 }
859
Brian Salomona531f252017-07-07 13:29:28 -0400860 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
861
Chris Dalton57ab06c2021-04-22 12:57:28 -0600862 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
863 GrClampType clampType) override {
Brian Osman8fa7ab42019-03-18 10:22:42 -0400864 // This Op uses uniform (not vertex) color, so doesn't need to track wide color.
Chris Dalton57ab06c2021-04-22 12:57:28 -0600865 return fHelper.finalizeProcessors(caps, clip, clampType,
Brian Osman8fa7ab42019-03-18 10:22:42 -0400866 GrProcessorAnalysisCoverage::kSingleChannel, &fColor,
867 nullptr);
Brian Salomona531f252017-07-07 13:29:28 -0400868 }
869
Robert Phillips4f93c572020-03-18 08:13:53 -0400870 enum Program : uint8_t {
871 kNone_Program = 0x0,
872 kLine_Program = 0x1,
873 kQuad_Program = 0x2,
874 kConic_Program = 0x4,
875 };
876
bsalomone46f9fe2015-08-18 06:05:14 -0700877private:
Robert Phillips4f93c572020-03-18 08:13:53 -0400878 void makeLineProgramInfo(const GrCaps&, SkArenaAlloc*, const GrPipeline*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500879 const GrSurfaceProxyView& writeView,
Robert Phillips4f93c572020-03-18 08:13:53 -0400880 const SkMatrix* geometryProcessorViewM,
Greg Danield358cbe2020-09-11 09:33:54 -0400881 const SkMatrix* geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -0500882 GrXferBarrierFlags renderPassXferBarriers,
883 GrLoadOp colorLoadOp);
Robert Phillips4f93c572020-03-18 08:13:53 -0400884 void makeQuadProgramInfo(const GrCaps&, SkArenaAlloc*, const GrPipeline*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500885 const GrSurfaceProxyView& writeView,
Robert Phillips4f93c572020-03-18 08:13:53 -0400886 const SkMatrix* geometryProcessorViewM,
Greg Danield358cbe2020-09-11 09:33:54 -0400887 const SkMatrix* geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -0500888 GrXferBarrierFlags renderPassXferBarriers,
889 GrLoadOp colorLoadOp);
Robert Phillips4f93c572020-03-18 08:13:53 -0400890 void makeConicProgramInfo(const GrCaps&, SkArenaAlloc*, const GrPipeline*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500891 const GrSurfaceProxyView& writeView,
Robert Phillips4f93c572020-03-18 08:13:53 -0400892 const SkMatrix* geometryProcessorViewM,
Greg Danield358cbe2020-09-11 09:33:54 -0400893 const SkMatrix* geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -0500894 GrXferBarrierFlags renderPassXferBarriers,
895 GrLoadOp colorLoadOp);
Robert Phillips1b3c2672019-12-04 14:34:48 -0500896
Robert Phillips2669a7b2020-03-12 12:07:19 -0400897 GrProgramInfo* programInfo() override {
898 // This Op has 3 programInfos and implements its own onPrePrepareDraws so this entry point
899 // should really never be called.
900 SkASSERT(0);
901 return nullptr;
902 }
903
Robert Phillips4f93c572020-03-18 08:13:53 -0400904 Program predictPrograms(const GrCaps*) const;
905
Robert Phillips4133dc42020-03-11 15:55:55 -0400906 void onCreateProgramInfo(const GrCaps*,
907 SkArenaAlloc*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500908 const GrSurfaceProxyView& writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400909 GrAppliedClip&&,
John Stiles52cb1d02021-06-02 11:58:05 -0400910 const GrDstProxyView&,
Greg Daniel42dbca52020-11-20 10:22:43 -0500911 GrXferBarrierFlags renderPassXferBarriers,
912 GrLoadOp colorLoadOp) override;
Robert Phillips4133dc42020-03-11 15:55:55 -0400913
Robert Phillips2669a7b2020-03-12 12:07:19 -0400914 void onPrePrepareDraws(GrRecordingContext*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500915 const GrSurfaceProxyView& writeView,
Robert Phillips2669a7b2020-03-12 12:07:19 -0400916 GrAppliedClip*,
John Stiles52cb1d02021-06-02 11:58:05 -0400917 const GrDstProxyView&,
Greg Daniel42dbca52020-11-20 10:22:43 -0500918 GrXferBarrierFlags renderPassXferBarriers,
919 GrLoadOp colorLoadOp) override;
Robert Phillips2669a7b2020-03-12 12:07:19 -0400920
Brian Salomon91326c32017-08-09 16:02:19 -0400921 void onPrepareDraws(Target*) override;
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700922 void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
bsalomon75398562015-08-17 12:55:38 -0700923
joshualitt7bc18b72015-02-03 16:41:41 -0800924 typedef SkTArray<SkPoint, true> PtArray;
925 typedef SkTArray<int, true> IntArray;
926 typedef SkTArray<float, true> FloatArray;
927
Herb Derbye25c3002020-10-27 15:57:27 -0400928 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500929 AAHairlineOp* that = t->cast<AAHairlineOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700930
Brian Salomona531f252017-07-07 13:29:28 -0400931 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000932 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700933 }
934
joshualitt7bc18b72015-02-03 16:41:41 -0800935 if (this->viewMatrix().hasPerspective() != that->viewMatrix().hasPerspective()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000936 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800937 }
938
939 // We go to identity if we don't have perspective
940 if (this->viewMatrix().hasPerspective() &&
Mike Reed2c383152019-12-18 16:47:47 -0500941 !SkMatrixPriv::CheapEqual(this->viewMatrix(), that->viewMatrix())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000942 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800943 }
944
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500945 // TODO we can actually combine hairlines if they are the same color in a kind of bulk
946 // method but we haven't implemented this yet
joshualitt7bc18b72015-02-03 16:41:41 -0800947 // TODO investigate going to vertex color and coverage?
948 if (this->coverage() != that->coverage()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000949 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800950 }
951
952 if (this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000953 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800954 }
955
Mike Reed2c383152019-12-18 16:47:47 -0500956 if (fHelper.usesLocalCoords() && !SkMatrixPriv::CheapEqual(this->viewMatrix(),
957 that->viewMatrix())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000958 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800959 }
960
Brian Salomond0a0a652016-12-15 15:25:22 -0500961 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000962 return CombineResult::kMerged;
joshualitt7bc18b72015-02-03 16:41:41 -0800963 }
964
John Stilesaf366522020-08-13 09:57:34 -0400965#if GR_TEST_UTILS
966 SkString onDumpInfo() const override {
967 return SkStringPrintf("Color: 0x%08x Coverage: 0x%02x, Count: %d\n%s",
968 fColor.toBytes_RGBA(), fCoverage, fPaths.count(),
969 fHelper.dumpInfo().c_str());
970 }
971#endif
972
Brian Osmancf860852018-10-31 14:04:39 -0400973 const SkPMColor4f& color() const { return fColor; }
Brian Salomond0a0a652016-12-15 15:25:22 -0500974 uint8_t coverage() const { return fCoverage; }
Brian Salomond0a0a652016-12-15 15:25:22 -0500975 const SkMatrix& viewMatrix() const { return fPaths[0].fViewMatrix; }
joshualitt7bc18b72015-02-03 16:41:41 -0800976
Brian Salomond0a0a652016-12-15 15:25:22 -0500977 struct PathData {
bsalomonf1703092016-06-29 18:41:53 -0700978 SkMatrix fViewMatrix;
979 SkPath fPath;
980 SkIRect fDevClipBounds;
Brian Osmancf3dc292017-06-28 16:45:32 -0400981 SkScalar fCapLength;
bsalomonf1703092016-06-29 18:41:53 -0700982 };
983
Brian Salomona531f252017-07-07 13:29:28 -0400984 SkSTArray<1, PathData, true> fPaths;
985 Helper fHelper;
Brian Osmancf860852018-10-31 14:04:39 -0400986 SkPMColor4f fColor;
Brian Salomond0a0a652016-12-15 15:25:22 -0500987 uint8_t fCoverage;
joshualitt7bc18b72015-02-03 16:41:41 -0800988
Robert Phillips4f93c572020-03-18 08:13:53 -0400989 Program fCharacterization = kNone_Program; // holds a mask of required programs
990 GrSimpleMesh* fMeshes[3] = { nullptr };
991 GrProgramInfo* fProgramInfos[3] = { nullptr };
992
John Stiles7571f9e2020-09-02 22:42:33 -0400993 using INHERITED = GrMeshDrawOp;
joshualitt7bc18b72015-02-03 16:41:41 -0800994};
995
Robert Phillips4f93c572020-03-18 08:13:53 -0400996GR_MAKE_BITFIELD_OPS(AAHairlineOp::Program)
Brian Salomona531f252017-07-07 13:29:28 -0400997
Robert Phillips4f93c572020-03-18 08:13:53 -0400998void AAHairlineOp::makeLineProgramInfo(const GrCaps& caps, SkArenaAlloc* arena,
999 const GrPipeline* pipeline,
Adlai Hollere2296f72020-11-19 13:41:26 -05001000 const GrSurfaceProxyView& writeView,
Robert Phillips4f93c572020-03-18 08:13:53 -04001001 const SkMatrix* geometryProcessorViewM,
Greg Danield358cbe2020-09-11 09:33:54 -04001002 const SkMatrix* geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -05001003 GrXferBarrierFlags renderPassXferBarriers,
1004 GrLoadOp colorLoadOp) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001005 if (fProgramInfos[0]) {
1006 return;
1007 }
Robert Phillips1b3c2672019-12-04 14:34:48 -05001008
Robert Phillips4f93c572020-03-18 08:13:53 -04001009 GrGeometryProcessor* lineGP;
1010 {
1011 using namespace GrDefaultGeoProcFactory;
Robert Phillips1b3c2672019-12-04 14:34:48 -05001012
Robert Phillips4f93c572020-03-18 08:13:53 -04001013 Color color(this->color());
1014 LocalCoords localCoords(fHelper.usesLocalCoords() ? LocalCoords::kUsePosition_Type
1015 : LocalCoords::kUnused_Type);
1016 localCoords.fMatrix = geometryProcessorLocalM;
Robert Phillips1b3c2672019-12-04 14:34:48 -05001017
Robert Phillips4f93c572020-03-18 08:13:53 -04001018 lineGP = GrDefaultGeoProcFactory::Make(arena,
1019 color,
1020 Coverage::kAttribute_Type,
1021 localCoords,
1022 *geometryProcessorViewM);
1023 SkASSERT(sizeof(LineVertex) == lineGP->vertexStride());
1024 }
1025
Greg Danield358cbe2020-09-11 09:33:54 -04001026 fProgramInfos[0] = GrSimpleMeshDrawOpHelper::CreateProgramInfo(
1027 arena, pipeline, writeView, lineGP, GrPrimitiveType::kTriangles,
Greg Daniel42dbca52020-11-20 10:22:43 -05001028 renderPassXferBarriers, colorLoadOp, fHelper.stencilSettings());
Robert Phillips1b3c2672019-12-04 14:34:48 -05001029}
1030
Robert Phillips4f93c572020-03-18 08:13:53 -04001031void AAHairlineOp::makeQuadProgramInfo(const GrCaps& caps, SkArenaAlloc* arena,
1032 const GrPipeline* pipeline,
Adlai Hollere2296f72020-11-19 13:41:26 -05001033 const GrSurfaceProxyView& writeView,
Robert Phillips4f93c572020-03-18 08:13:53 -04001034 const SkMatrix* geometryProcessorViewM,
Greg Danield358cbe2020-09-11 09:33:54 -04001035 const SkMatrix* geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -05001036 GrXferBarrierFlags renderPassXferBarriers,
1037 GrLoadOp colorLoadOp) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001038 if (fProgramInfos[1]) {
1039 return;
1040 }
1041
Robert Phillips1b3c2672019-12-04 14:34:48 -05001042 GrGeometryProcessor* quadGP = GrQuadEffect::Make(arena,
1043 this->color(),
1044 *geometryProcessorViewM,
Robert Phillips1b3c2672019-12-04 14:34:48 -05001045 caps,
1046 *geometryProcessorLocalM,
1047 fHelper.usesLocalCoords(),
1048 this->coverage());
1049 SkASSERT(sizeof(BezierVertex) == quadGP->vertexStride());
1050
Greg Danield358cbe2020-09-11 09:33:54 -04001051 fProgramInfos[1] = GrSimpleMeshDrawOpHelper::CreateProgramInfo(
1052 arena, pipeline, writeView, quadGP, GrPrimitiveType::kTriangles,
Greg Daniel42dbca52020-11-20 10:22:43 -05001053 renderPassXferBarriers, colorLoadOp, fHelper.stencilSettings());
Robert Phillips1b3c2672019-12-04 14:34:48 -05001054}
1055
Robert Phillips4f93c572020-03-18 08:13:53 -04001056void AAHairlineOp::makeConicProgramInfo(const GrCaps& caps, SkArenaAlloc* arena,
1057 const GrPipeline* pipeline,
Adlai Hollere2296f72020-11-19 13:41:26 -05001058 const GrSurfaceProxyView& writeView,
Robert Phillips4f93c572020-03-18 08:13:53 -04001059 const SkMatrix* geometryProcessorViewM,
Greg Danield358cbe2020-09-11 09:33:54 -04001060 const SkMatrix* geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -05001061 GrXferBarrierFlags renderPassXferBarriers,
1062 GrLoadOp colorLoadOp) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001063 if (fProgramInfos[2]) {
1064 return;
1065 }
1066
Robert Phillips1b3c2672019-12-04 14:34:48 -05001067 GrGeometryProcessor* conicGP = GrConicEffect::Make(arena,
1068 this->color(),
1069 *geometryProcessorViewM,
Robert Phillips1b3c2672019-12-04 14:34:48 -05001070 caps,
1071 *geometryProcessorLocalM,
1072 fHelper.usesLocalCoords(),
1073 this->coverage());
1074 SkASSERT(sizeof(BezierVertex) == conicGP->vertexStride());
1075
Greg Danield358cbe2020-09-11 09:33:54 -04001076 fProgramInfos[2] = GrSimpleMeshDrawOpHelper::CreateProgramInfo(
1077 arena, pipeline, writeView, conicGP, GrPrimitiveType::kTriangles,
Greg Daniel42dbca52020-11-20 10:22:43 -05001078 renderPassXferBarriers, colorLoadOp, fHelper.stencilSettings());
Robert Phillips1b3c2672019-12-04 14:34:48 -05001079}
1080
Robert Phillips4f93c572020-03-18 08:13:53 -04001081AAHairlineOp::Program AAHairlineOp::predictPrograms(const GrCaps* caps) const {
1082 bool convertConicsToQuads = !caps->shaderCaps()->floatIs32Bits();
1083
1084 // When predicting the programs we always include the lineProgram bc it is used as a fallback
1085 // for quads and conics. In non-DDL mode there are cases where it sometimes isn't needed for a
1086 // given path.
1087 Program neededPrograms = kLine_Program;
1088
1089 for (int i = 0; i < fPaths.count(); i++) {
1090 uint32_t mask = fPaths[i].fPath.getSegmentMasks();
1091
1092 if (mask & (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)) {
1093 neededPrograms |= kQuad_Program;
1094 }
1095 if (mask & SkPath::kConic_SegmentMask) {
1096 if (convertConicsToQuads) {
1097 neededPrograms |= kQuad_Program;
1098 } else {
1099 neededPrograms |= kConic_Program;
1100 }
1101 }
1102 }
1103
1104 return neededPrograms;
1105}
1106
1107void AAHairlineOp::onCreateProgramInfo(const GrCaps* caps,
1108 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -05001109 const GrSurfaceProxyView& writeView,
Robert Phillips4f93c572020-03-18 08:13:53 -04001110 GrAppliedClip&& appliedClip,
John Stiles52cb1d02021-06-02 11:58:05 -04001111 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -05001112 GrXferBarrierFlags renderPassXferBarriers,
1113 GrLoadOp colorLoadOp) {
joshualitt7bc18b72015-02-03 16:41:41 -08001114 // Setup the viewmatrix and localmatrix for the GrGeometryProcessor.
1115 SkMatrix invert;
1116 if (!this->viewMatrix().invert(&invert)) {
1117 return;
1118 }
1119
1120 // we will transform to identity space if the viewmatrix does not have perspective
1121 bool hasPerspective = this->viewMatrix().hasPerspective();
1122 const SkMatrix* geometryProcessorViewM = &SkMatrix::I();
1123 const SkMatrix* geometryProcessorLocalM = &invert;
joshualitt7bc18b72015-02-03 16:41:41 -08001124 if (hasPerspective) {
1125 geometryProcessorViewM = &this->viewMatrix();
1126 geometryProcessorLocalM = &SkMatrix::I();
Robert Phillips4f93c572020-03-18 08:13:53 -04001127 }
1128
Adlai Hollere2296f72020-11-19 13:41:26 -05001129 auto pipeline = fHelper.createPipeline(caps, arena, writeView.swizzle(),
Chris Dalton1b6a43c2020-09-25 12:21:18 -06001130 std::move(appliedClip), dstProxyView);
Robert Phillips4f93c572020-03-18 08:13:53 -04001131
1132 if (fCharacterization & kLine_Program) {
Brian Salomon8afde5f2020-04-01 16:22:00 -04001133 this->makeLineProgramInfo(*caps, arena, pipeline, writeView,
Greg Danield358cbe2020-09-11 09:33:54 -04001134 geometryProcessorViewM, geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -05001135 renderPassXferBarriers, colorLoadOp);
Robert Phillips4f93c572020-03-18 08:13:53 -04001136 }
1137 if (fCharacterization & kQuad_Program) {
Brian Salomon8afde5f2020-04-01 16:22:00 -04001138 this->makeQuadProgramInfo(*caps, arena, pipeline, writeView,
Greg Danield358cbe2020-09-11 09:33:54 -04001139 geometryProcessorViewM, geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -05001140 renderPassXferBarriers, colorLoadOp);
Robert Phillips4f93c572020-03-18 08:13:53 -04001141 }
1142 if (fCharacterization & kConic_Program) {
Brian Salomon8afde5f2020-04-01 16:22:00 -04001143 this->makeConicProgramInfo(*caps, arena, pipeline, writeView,
Greg Danield358cbe2020-09-11 09:33:54 -04001144 geometryProcessorViewM, geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -05001145 renderPassXferBarriers, colorLoadOp);
Robert Phillips4f93c572020-03-18 08:13:53 -04001146
1147 }
1148}
1149
1150void AAHairlineOp::onPrePrepareDraws(GrRecordingContext* context,
Adlai Hollere2296f72020-11-19 13:41:26 -05001151 const GrSurfaceProxyView& writeView,
Robert Phillips4f93c572020-03-18 08:13:53 -04001152 GrAppliedClip* clip,
John Stiles52cb1d02021-06-02 11:58:05 -04001153 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -05001154 GrXferBarrierFlags renderPassXferBarriers,
1155 GrLoadOp colorLoadOp) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001156 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
1157 const GrCaps* caps = context->priv().caps();
1158
1159 // This is equivalent to a GrOpFlushState::detachAppliedClip
Michael Ludwigd1d997e2020-06-04 15:52:44 -04001160 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip::Disabled();
Robert Phillips4f93c572020-03-18 08:13:53 -04001161
1162 // Conservatively predict which programs will be required
1163 fCharacterization = this->predictPrograms(caps);
1164
Greg Danield358cbe2020-09-11 09:33:54 -04001165 this->createProgramInfo(caps, arena, writeView, std::move(appliedClip), dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -05001166 renderPassXferBarriers, colorLoadOp);
Robert Phillips4f93c572020-03-18 08:13:53 -04001167
1168 context->priv().recordProgramInfo(fProgramInfos[0]);
1169 context->priv().recordProgramInfo(fProgramInfos[1]);
1170 context->priv().recordProgramInfo(fProgramInfos[2]);
1171}
1172
1173void AAHairlineOp::onPrepareDraws(Target* target) {
1174 // Setup the viewmatrix and localmatrix for the GrGeometryProcessor.
1175 SkMatrix invert;
1176 if (!this->viewMatrix().invert(&invert)) {
1177 return;
1178 }
1179
1180 // we will transform to identity space if the viewmatrix does not have perspective
1181 const SkMatrix* toDevice = nullptr;
1182 const SkMatrix* toSrc = nullptr;
1183 if (this->viewMatrix().hasPerspective()) {
joshualitt7bc18b72015-02-03 16:41:41 -08001184 toDevice = &this->viewMatrix();
1185 toSrc = &invert;
1186 }
1187
Robert Phillips4f93c572020-03-18 08:13:53 -04001188 SkDEBUGCODE(Program predictedPrograms = this->predictPrograms(&target->caps()));
1189 Program actualPrograms = kNone_Program;
1190
joshualitt7bc18b72015-02-03 16:41:41 -08001191 // This is hand inlined for maximum performance.
1192 PREALLOC_PTARRAY(128) lines;
1193 PREALLOC_PTARRAY(128) quads;
1194 PREALLOC_PTARRAY(128) conics;
1195 IntArray qSubdivs;
1196 FloatArray cWeights;
joshualitt351ba1b2015-02-16 08:33:19 -08001197 int quadCount = 0;
joshualitt7bc18b72015-02-03 16:41:41 -08001198
Brian Salomond0a0a652016-12-15 15:25:22 -05001199 int instanceCount = fPaths.count();
Brian Salomon969a7382018-05-09 13:28:44 -04001200 bool convertConicsToQuads = !target->caps().shaderCaps()->floatIs32Bits();
joshualitt7bc18b72015-02-03 16:41:41 -08001201 for (int i = 0; i < instanceCount; i++) {
Brian Salomond0a0a652016-12-15 15:25:22 -05001202 const PathData& args = fPaths[i];
joshualitt351ba1b2015-02-16 08:33:19 -08001203 quadCount += gather_lines_and_quads(args.fPath, args.fViewMatrix, args.fDevClipBounds,
Brian Salomon969a7382018-05-09 13:28:44 -04001204 args.fCapLength, convertConicsToQuads, &lines, &quads,
1205 &conics, &qSubdivs, &cWeights);
joshualitt7bc18b72015-02-03 16:41:41 -08001206 }
1207
joshualitt7bc18b72015-02-03 16:41:41 -08001208 int lineCount = lines.count() / 2;
1209 int conicCount = conics.count() / 3;
Brian Salomon296de502018-03-13 12:26:55 -04001210 int quadAndConicCount = conicCount + quadCount;
1211
1212 static constexpr int kMaxLines = SK_MaxS32 / kLineSegNumVertices;
1213 static constexpr int kMaxQuadsAndConics = SK_MaxS32 / kQuadNumVertices;
1214 if (lineCount > kMaxLines || quadAndConicCount > kMaxQuadsAndConics) {
1215 return;
1216 }
joshualitt7bc18b72015-02-03 16:41:41 -08001217
1218 // do lines first
1219 if (lineCount) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001220 SkASSERT(predictedPrograms & kLine_Program);
1221 actualPrograms |= kLine_Program;
1222
Brian Salomond28a79d2017-10-16 13:01:07 -04001223 sk_sp<const GrBuffer> linesIndexBuffer = get_lines_index_buffer(target->resourceProvider());
joshualitt7bc18b72015-02-03 16:41:41 -08001224
Robert Phillipse94cdd22019-11-04 14:15:58 -05001225 GrMeshDrawOp::PatternHelper helper(target, GrPrimitiveType::kTriangles, sizeof(LineVertex),
1226 std::move(linesIndexBuffer), kLineSegNumVertices,
1227 kIdxsPerLineSeg, lineCount, kLineSegsNumInIdxBuffer);
1228
1229 LineVertex* verts = reinterpret_cast<LineVertex*>(helper.vertices());
1230 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -08001231 SkDebugf("Could not allocate vertices\n");
1232 return;
1233 }
1234
joshualitt7bc18b72015-02-03 16:41:41 -08001235 for (int i = 0; i < lineCount; ++i) {
1236 add_line(&lines[2*i], toSrc, this->coverage(), &verts);
1237 }
1238
Robert Phillips4f93c572020-03-18 08:13:53 -04001239 fMeshes[0] = helper.mesh();
joshualitt7bc18b72015-02-03 16:41:41 -08001240 }
1241
1242 if (quadCount || conicCount) {
Brian Salomon12d22642019-01-29 14:38:50 -05001243 sk_sp<const GrBuffer> vertexBuffer;
joshualitt7bc18b72015-02-03 16:41:41 -08001244 int firstVertex;
1245
Brian Salomond28a79d2017-10-16 13:01:07 -04001246 sk_sp<const GrBuffer> quadsIndexBuffer = get_quads_index_buffer(target->resourceProvider());
bsalomoned0bcad2015-05-04 10:36:42 -07001247
Brian Salomon296de502018-03-13 12:26:55 -04001248 int vertexCount = kQuadNumVertices * quadAndConicCount;
Brian Salomon92be2f72018-06-19 14:33:47 -04001249 void* vertices = target->makeVertexSpace(sizeof(BezierVertex), vertexCount, &vertexBuffer,
1250 &firstVertex);
joshualitt7bc18b72015-02-03 16:41:41 -08001251
bsalomoned0bcad2015-05-04 10:36:42 -07001252 if (!vertices || !quadsIndexBuffer) {
joshualitt4b31de82015-03-05 14:33:41 -08001253 SkDebugf("Could not allocate vertices\n");
1254 return;
1255 }
1256
joshualitt7bc18b72015-02-03 16:41:41 -08001257 // Setup vertices
robertphillips44c31282015-09-03 12:58:48 -07001258 BezierVertex* bezVerts = reinterpret_cast<BezierVertex*>(vertices);
joshualitt7bc18b72015-02-03 16:41:41 -08001259
joshualitt7bc18b72015-02-03 16:41:41 -08001260 int unsubdivQuadCnt = quads.count() / 3;
1261 for (int i = 0; i < unsubdivQuadCnt; ++i) {
1262 SkASSERT(qSubdivs[i] >= 0);
robertphillips44c31282015-09-03 12:58:48 -07001263 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &bezVerts);
joshualitt7bc18b72015-02-03 16:41:41 -08001264 }
1265
1266 // Start Conics
1267 for (int i = 0; i < conicCount; ++i) {
robertphillips44c31282015-09-03 12:58:48 -07001268 add_conics(&conics[3*i], cWeights[i], toDevice, toSrc, &bezVerts);
joshualitt7bc18b72015-02-03 16:41:41 -08001269 }
1270
1271 if (quadCount > 0) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001272 SkASSERT(predictedPrograms & kQuad_Program);
1273 actualPrograms |= kQuad_Program;
Robert Phillips1b3c2672019-12-04 14:34:48 -05001274
Robert Phillips4f93c572020-03-18 08:13:53 -04001275 fMeshes[1] = target->allocMesh();
1276 fMeshes[1]->setIndexedPatterned(quadsIndexBuffer, kIdxsPerQuad, quadCount,
1277 kQuadsNumInIdxBuffer, vertexBuffer, kQuadNumVertices,
1278 firstVertex);
bsalomon342bfc22016-04-01 06:06:20 -07001279 firstVertex += quadCount * kQuadNumVertices;
joshualitt7bc18b72015-02-03 16:41:41 -08001280 }
1281
1282 if (conicCount > 0) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001283 SkASSERT(predictedPrograms & kConic_Program);
1284 actualPrograms |= kConic_Program;
Robert Phillips1b3c2672019-12-04 14:34:48 -05001285
Robert Phillips4f93c572020-03-18 08:13:53 -04001286 fMeshes[2] = target->allocMesh();
1287 fMeshes[2]->setIndexedPatterned(std::move(quadsIndexBuffer), kIdxsPerQuad, conicCount,
1288 kQuadsNumInIdxBuffer, std::move(vertexBuffer),
1289 kQuadNumVertices, firstVertex);
joshualitt7bc18b72015-02-03 16:41:41 -08001290 }
1291 }
Robert Phillips4f93c572020-03-18 08:13:53 -04001292
1293 // In DDL mode this will replace the predicted program requirements with the actual ones.
1294 // However, we will already have surfaced the predicted programs to the DDL.
1295 fCharacterization = actualPrograms;
joshualitt7bc18b72015-02-03 16:41:41 -08001296}
1297
Chris Dalton07cdcfc92019-02-26 11:13:22 -07001298void AAHairlineOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001299 this->createProgramInfo(flushState);
Robert Phillips3968fcb2019-12-05 16:40:31 -05001300
Robert Phillips4f93c572020-03-18 08:13:53 -04001301 for (int i = 0; i < 3; ++i) {
1302 if (fProgramInfos[i] && fMeshes[i]) {
1303 flushState->bindPipelineAndScissorClip(*fProgramInfos[i], chainBounds);
Robert Phillips787fd9d2021-03-22 14:48:09 -04001304 flushState->bindTextures(fProgramInfos[i]->geomProc(), nullptr,
Robert Phillips4f93c572020-03-18 08:13:53 -04001305 fProgramInfos[i]->pipeline());
1306 flushState->drawMesh(*fMeshes[i]);
1307 }
1308 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -07001309}
1310
bsalomon0aff2fa2015-07-31 06:48:27 -07001311bool GrAAHairLinePathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -04001312 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -07001313 "GrAAHairlinePathRenderer::onDrawPath");
Chris Dalton6ce447a2019-06-23 18:07:38 -06001314 SkASSERT(args.fRenderTargetContext->numSamples() <= 1);
csmartdaltonecbc12b2016-06-08 10:08:43 -07001315
bsalomon8acedde2016-06-24 10:42:16 -07001316 SkPath path;
1317 args.fShape->asPath(&path);
Herb Derbyc76d4092020-10-07 16:46:15 -04001318 GrOp::Owner op =
Robert Phillips7c525e62018-06-12 10:11:12 -04001319 AAHairlineOp::Make(args.fContext, std::move(args.fPaint), *args.fViewMatrix, path,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001320 args.fShape->style(), *args.fClipConservativeBounds,
1321 args.fUserStencilSettings);
1322 args.fRenderTargetContext->addDrawOp(args.fClip, std::move(op));
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001323 return true;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001324}
joshualitt40ded322015-05-02 07:07:17 -07001325
1326///////////////////////////////////////////////////////////////////////////////////////////////////
1327
Hal Canary6f6961e2017-01-31 13:50:44 -05001328#if GR_TEST_UTILS
joshualitt40ded322015-05-02 07:07:17 -07001329
Brian Salomona531f252017-07-07 13:29:28 -04001330GR_DRAW_OP_TEST_DEFINE(AAHairlineOp) {
joshualitt40ded322015-05-02 07:07:17 -07001331 SkMatrix viewMatrix = GrTest::TestMatrix(random);
John Stiles31954bf2020-08-07 17:35:54 -04001332 const SkPath& path = GrTest::TestPath(random);
joshualitt40ded322015-05-02 07:07:17 -07001333 SkIRect devClipBounds;
1334 devClipBounds.setEmpty();
Robert Phillips7c525e62018-06-12 10:11:12 -04001335 return AAHairlineOp::Make(context, std::move(paint), viewMatrix, path,
1336 GrStyle::SimpleHairline(), devClipBounds,
1337 GrGetRandomStencil(random, context));
joshualitt40ded322015-05-02 07:07:17 -07001338}
1339
1340#endif