blob: 59794d8edd67da30ea13dd96db7bfd94aaf56ac6 [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
Robert Phillips461c5392021-08-17 16:42:51 -04008#include "src/gpu/ops/AAHairLinePathRenderer.h"
Robert Phillips06273bc2021-08-11 15:43:50 -04009
Mike Kleinc0bd9f92019-04-23 12:05:21 -050010#include "include/core/SkPoint3.h"
11#include "include/private/SkTemplates.h"
12#include "src/core/SkGeometry.h"
13#include "src/core/SkMatrixPriv.h"
14#include "src/core/SkPointPriv.h"
15#include "src/core/SkRectPriv.h"
16#include "src/core/SkStroke.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040017#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrBuffer.h"
19#include "src/gpu/GrCaps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrDefaultGeoProcFactory.h"
21#include "src/gpu/GrDrawOpTest.h"
22#include "src/gpu/GrOpFlushState.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrProcessor.h"
Robert Phillips4f93c572020-03-18 08:13:53 -040024#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050025#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050026#include "src/gpu/GrStyle.h"
Robert Phillips62214f72021-06-15 10:12:51 -040027#include "src/gpu/GrUtil.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050028#include "src/gpu/effects/GrBezierEffect.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040029#include "src/gpu/geometry/GrPathUtils.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000030#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050031#include "src/gpu/ops/GrMeshDrawOp.h"
Robert Phillips55f681f2020-02-28 08:58:15 -050032#include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h"
Robert Phillips4dca8312021-07-28 15:13:20 -040033#include "src/gpu/v1/SurfaceDrawContext_v1.h"
bsalomon@google.com4647f902013-03-26 14:45:27 +000034
bsalomoned0bcad2015-05-04 10:36:42 -070035#define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true>
36
Robert Phillips461c5392021-08-17 16:42:51 -040037using PtArray = SkTArray<SkPoint, true>;
38using IntArray = SkTArray<int, true>;
39using FloatArray = SkTArray<float, true>;
40
41namespace {
42
bsalomon@google.comaeb21602011-08-30 18:13:44 +000043// quadratics are rendered as 5-sided polys in order to bound the
44// AA stroke around the center-curve. See comments in push_quad_index_buffer and
egdaniel@google.com5383a752013-07-12 20:15:34 +000045// bloat_quad. Quadratics and conics share an index buffer
bsalomon@google.comaeb21602011-08-30 18:13:44 +000046
robertphillips@google.comada90da2013-09-18 22:14:49 +000047// lines are rendered as:
48// *______________*
49// |\ -_______ /|
50// | \ \ / |
51// | *--------* |
52// | / ______/ \ |
53// */_-__________\*
54// For: 6 vertices and 18 indices (for 6 triangles)
bsalomon@google.comaeb21602011-08-30 18:13:44 +000055
joshualitt5ead6da2014-10-22 16:00:29 -070056// Each quadratic is rendered as a five sided polygon. This poly bounds
57// the quadratic's bounding triangle but has been expanded so that the
58// 1-pixel wide area around the curve is inside the poly.
59// If a,b,c are the original control points then the poly a0,b0,c0,c1,a1
60// that is rendered would look like this:
61// b0
62// b
63//
64// a0 c0
65// a c
66// a1 c1
egdaniel14afb432014-12-22 10:57:08 -080067// Each is drawn as three triangles ((a0,a1,b0), (b0,c1,c0), (a1,c1,b0))
68// specified by these 9 indices:
joshualitt5ead6da2014-10-22 16:00:29 -070069static const uint16_t kQuadIdxBufPattern[] = {
70 0, 1, 2,
71 2, 4, 3,
72 1, 4, 2
73};
bsalomon@google.comaeb21602011-08-30 18:13:44 +000074
joshualitt5ead6da2014-10-22 16:00:29 -070075static const int kIdxsPerQuad = SK_ARRAY_COUNT(kQuadIdxBufPattern);
76static const int kQuadNumVertices = 5;
77static const int kQuadsNumInIdxBuffer = 256;
bsalomoned0bcad2015-05-04 10:36:42 -070078GR_DECLARE_STATIC_UNIQUE_KEY(gQuadsIndexBufferKey);
79
Robert Phillips461c5392021-08-17 16:42:51 -040080sk_sp<const GrBuffer> get_quads_index_buffer(GrResourceProvider* resourceProvider) {
bsalomoned0bcad2015-05-04 10:36:42 -070081 GR_DEFINE_STATIC_UNIQUE_KEY(gQuadsIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -040082 return resourceProvider->findOrCreatePatternedIndexBuffer(
bsalomoned0bcad2015-05-04 10:36:42 -070083 kQuadIdxBufPattern, kIdxsPerQuad, kQuadsNumInIdxBuffer, kQuadNumVertices,
84 gQuadsIndexBufferKey);
85}
jvanverth@google.com681ccf02013-08-16 14:51:51 +000086
bsalomon@google.comaeb21602011-08-30 18:13:44 +000087
joshualitt5ead6da2014-10-22 16:00:29 -070088// Each line segment is rendered as two quads and two triangles.
89// p0 and p1 have alpha = 1 while all other points have alpha = 0.
90// The four external points are offset 1 pixel perpendicular to the
91// line and half a pixel parallel to the line.
92//
93// p4 p5
94// p0 p1
95// p2 p3
96//
97// Each is drawn as six triangles specified by these 18 indices:
jvanverth@google.com681ccf02013-08-16 14:51:51 +000098
joshualitt5ead6da2014-10-22 16:00:29 -070099static const uint16_t kLineSegIdxBufPattern[] = {
100 0, 1, 3,
101 0, 3, 2,
102 0, 4, 5,
103 0, 5, 1,
104 0, 2, 4,
105 1, 5, 3
106};
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000107
joshualitt5ead6da2014-10-22 16:00:29 -0700108static const int kIdxsPerLineSeg = SK_ARRAY_COUNT(kLineSegIdxBufPattern);
109static const int kLineSegNumVertices = 6;
110static const int kLineSegsNumInIdxBuffer = 256;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000111
bsalomoned0bcad2015-05-04 10:36:42 -0700112GR_DECLARE_STATIC_UNIQUE_KEY(gLinesIndexBufferKey);
113
Robert Phillips461c5392021-08-17 16:42:51 -0400114sk_sp<const GrBuffer> get_lines_index_buffer(GrResourceProvider* resourceProvider) {
bsalomoned0bcad2015-05-04 10:36:42 -0700115 GR_DEFINE_STATIC_UNIQUE_KEY(gLinesIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400116 return resourceProvider->findOrCreatePatternedIndexBuffer(
bsalomoned0bcad2015-05-04 10:36:42 -0700117 kLineSegIdxBufPattern, kIdxsPerLineSeg, kLineSegsNumInIdxBuffer, kLineSegNumVertices,
118 gLinesIndexBufferKey);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000119}
120
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000121// Takes 178th time of logf on Z600 / VC2010
Robert Phillips461c5392021-08-17 16:42:51 -0400122int get_float_exp(float x) {
Brian Salomon4dea72a2019-12-18 10:43:10 -0500123 static_assert(sizeof(int) == sizeof(float));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000124#ifdef SK_DEBUG
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000125 static bool tested;
126 if (!tested) {
127 tested = true;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000128 SkASSERT(get_float_exp(0.25f) == -2);
129 SkASSERT(get_float_exp(0.3f) == -2);
130 SkASSERT(get_float_exp(0.5f) == -1);
131 SkASSERT(get_float_exp(1.f) == 0);
132 SkASSERT(get_float_exp(2.f) == 1);
133 SkASSERT(get_float_exp(2.5f) == 1);
134 SkASSERT(get_float_exp(8.f) == 3);
135 SkASSERT(get_float_exp(100.f) == 6);
136 SkASSERT(get_float_exp(1000.f) == 9);
137 SkASSERT(get_float_exp(1024.f) == 10);
138 SkASSERT(get_float_exp(3000000.f) == 21);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000139 }
140#endif
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000141 const int* iptr = (const int*)&x;
142 return (((*iptr) & 0x7f800000) >> 23) - 127;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000143}
144
egdaniel@google.com5383a752013-07-12 20:15:34 +0000145// Uses the max curvature function for quads to estimate
146// where to chop the conic. If the max curvature is not
147// found along the curve segment it will return 1 and
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000148// dst[0] is the original conic. If it returns 2 the dst[0]
egdaniel@google.com5383a752013-07-12 20:15:34 +0000149// and dst[1] are the two new conics.
Robert Phillips461c5392021-08-17 16:42:51 -0400150int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000151 SkScalar t = SkFindQuadMaxCurvature(src);
Chris Dalton1d474dd2018-07-24 01:08:31 -0600152 if (t == 0 || t == 1) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000153 if (dst) {
154 dst[0].set(src, weight);
155 }
156 return 1;
157 } else {
158 if (dst) {
159 SkConic conic;
160 conic.set(src, weight);
caryclark414c4292016-09-26 11:03:54 -0700161 if (!conic.chopAt(t, dst)) {
162 dst[0].set(src, weight);
163 return 1;
164 }
egdaniel@google.com5383a752013-07-12 20:15:34 +0000165 }
166 return 2;
167 }
168}
169
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000170// Calls split_conic on the entire conic and then once more on each subsection.
171// Most cases will result in either 1 conic (chop point is not within t range)
172// or 3 points (split once and then one subsection is split again).
Robert Phillips461c5392021-08-17 16:42:51 -0400173int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000174 SkConic dstTemp[2];
175 int conicCnt = split_conic(src, dstTemp, weight);
176 if (2 == conicCnt) {
177 int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW);
178 conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW);
179 } else {
180 dst[0] = dstTemp[0];
181 }
182 return conicCnt;
183}
184
egdaniel@google.com5383a752013-07-12 20:15:34 +0000185// returns 0 if quad/conic is degen or close to it
186// in this case approx the path with lines
187// otherwise returns 1
Robert Phillips461c5392021-08-17 16:42:51 -0400188int is_degen_quad_or_conic(const SkPoint p[3], SkScalar* dsqd) {
jvanverthcfeb85f2016-04-29 07:38:10 -0700189 static const SkScalar gDegenerateToLineTol = GrPathUtils::kDefaultTolerance;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000190 static const SkScalar gDegenerateToLineTolSqd =
Mike Reed8be952a2017-02-13 20:44:33 -0500191 gDegenerateToLineTol * gDegenerateToLineTol;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000192
Cary Clarkdf429f32017-11-08 11:44:31 -0500193 if (SkPointPriv::DistanceToSqd(p[0], p[1]) < gDegenerateToLineTolSqd ||
194 SkPointPriv::DistanceToSqd(p[1], p[2]) < gDegenerateToLineTolSqd) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000195 return 1;
196 }
197
Cary Clarkdf429f32017-11-08 11:44:31 -0500198 *dsqd = SkPointPriv::DistanceToLineBetweenSqd(p[1], p[0], p[2]);
joshualitt63648072015-02-19 10:25:21 -0800199 if (*dsqd < gDegenerateToLineTolSqd) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000200 return 1;
201 }
202
Cary Clarkdf429f32017-11-08 11:44:31 -0500203 if (SkPointPriv::DistanceToLineBetweenSqd(p[2], p[1], p[0]) < gDegenerateToLineTolSqd) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000204 return 1;
205 }
206 return 0;
207}
208
Robert Phillips461c5392021-08-17 16:42:51 -0400209int is_degen_quad_or_conic(const SkPoint p[3]) {
joshualitt63648072015-02-19 10:25:21 -0800210 SkScalar dsqd;
211 return is_degen_quad_or_conic(p, &dsqd);
212}
213
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000214// we subdivide the quads to avoid huge overfill
215// if it returns -1 then should be drawn as lines
Robert Phillips461c5392021-08-17 16:42:51 -0400216int num_quad_subdivs(const SkPoint p[3]) {
joshualitt63648072015-02-19 10:25:21 -0800217 SkScalar dsqd;
218 if (is_degen_quad_or_conic(p, &dsqd)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000219 return -1;
220 }
221
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000222 // tolerance of triangle height in pixels
223 // tuned on windows Quadro FX 380 / Z600
224 // trade off of fill vs cpu time on verts
225 // maybe different when do this using gpu (geo or tess shaders)
226 static const SkScalar gSubdivTol = 175 * SK_Scalar1;
227
Mike Reed8be952a2017-02-13 20:44:33 -0500228 if (dsqd <= gSubdivTol * gSubdivTol) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000229 return 0;
230 } else {
robertphillips@google.com87379e12013-03-29 12:11:10 +0000231 static const int kMaxSub = 4;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000232 // subdividing the quad reduces d by 4. so we want x = log4(d/tol)
233 // = log4(d*d/tol*tol)/2
234 // = log2(d*d/tol*tol)
235
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000236 // +1 since we're ignoring the mantissa contribution.
237 int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
Brian Osman788b9162020-02-07 10:36:46 -0500238 log = std::min(std::max(0, log), kMaxSub);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000239 return log;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000240 }
241}
242
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000243/**
244 * Generates the lines and quads to be rendered. Lines are always recorded in
245 * device space. We will do a device space bloat to account for the 1pixel
246 * thickness.
247 * Quads are recorded in device space unless m contains
248 * perspective, then in they are in src space. We do this because we will
249 * subdivide large quads to reduce over-fill. This subdivision has to be
250 * performed before applying the perspective matrix.
251 */
Robert Phillips461c5392021-08-17 16:42:51 -0400252int gather_lines_and_quads(const SkPath& path,
253 const SkMatrix& m,
254 const SkIRect& devClipBounds,
255 SkScalar capLength,
256 bool convertConicsToQuads,
257 PtArray* lines,
258 PtArray* quads,
259 PtArray* conics,
260 IntArray* quadSubdivCnts,
261 FloatArray* conicWeights) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000262 SkPath::Iter iter(path, false);
263
264 int totalQuadCount = 0;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000265 SkRect bounds;
266 SkIRect ibounds;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000267
268 bool persp = m.hasPerspective();
269
Brian Osmancf3dc292017-06-28 16:45:32 -0400270 // Whenever a degenerate, zero-length contour is encountered, this code will insert a
271 // 'capLength' x-aligned line segment. Since this is rendering hairlines it is hoped this will
272 // suffice for AA square & circle capping.
273 int verbsInContour = 0; // Does not count moves
274 bool seenZeroLengthVerb = false;
275 SkPoint zeroVerbPt;
276
Brian Salomon969a7382018-05-09 13:28:44 -0400277 // Adds a quad that has already been chopped to the list and checks for quads that are close to
278 // lines. Also does a bounding box check. It takes points that are in src space and device
279 // space. The src points are only required if the view matrix has perspective.
280 auto addChoppedQuad = [&](const SkPoint srcPts[3], const SkPoint devPts[4],
281 bool isContourStart) {
282 SkRect bounds;
283 SkIRect ibounds;
284 bounds.setBounds(devPts, 3);
285 bounds.outset(SK_Scalar1, SK_Scalar1);
286 bounds.roundOut(&ibounds);
287 // We only need the src space space pts when not in perspective.
288 SkASSERT(srcPts || !persp);
289 if (SkIRect::Intersects(devClipBounds, ibounds)) {
290 int subdiv = num_quad_subdivs(devPts);
291 SkASSERT(subdiv >= -1);
292 if (-1 == subdiv) {
293 SkPoint* pts = lines->push_back_n(4);
294 pts[0] = devPts[0];
295 pts[1] = devPts[1];
296 pts[2] = devPts[1];
297 pts[3] = devPts[2];
298 if (isContourStart && pts[0] == pts[1] && pts[2] == pts[3]) {
299 seenZeroLengthVerb = true;
300 zeroVerbPt = pts[0];
301 }
302 } else {
303 // when in perspective keep quads in src space
304 const SkPoint* qPts = persp ? srcPts : devPts;
305 SkPoint* pts = quads->push_back_n(3);
306 pts[0] = qPts[0];
307 pts[1] = qPts[1];
308 pts[2] = qPts[2];
309 quadSubdivCnts->push_back() = subdiv;
310 totalQuadCount += 1 << subdiv;
311 }
312 }
313 };
314
315 // Applies the view matrix to quad src points and calls the above helper.
316 auto addSrcChoppedQuad = [&](const SkPoint srcSpaceQuadPts[3], bool isContourStart) {
317 SkPoint devPts[3];
318 m.mapPoints(devPts, srcSpaceQuadPts, 3);
319 addChoppedQuad(srcSpaceQuadPts, devPts, isContourStart);
320 };
321
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000322 for (;;) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000323 SkPoint pathPts[4];
Mike Reedba7e9a62019-08-16 13:30:34 -0400324 SkPath::Verb verb = iter.next(pathPts);
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000325 switch (verb) {
Brian Salomon969a7382018-05-09 13:28:44 -0400326 case SkPath::kConic_Verb:
327 if (convertConicsToQuads) {
328 SkScalar weight = iter.conicWeight();
329 SkAutoConicToQuads converter;
Brian Osmane3deee12018-11-20 11:10:15 -0500330 const SkPoint* quadPts = converter.computeQuads(pathPts, weight, 0.25f);
Brian Salomon969a7382018-05-09 13:28:44 -0400331 for (int i = 0; i < converter.countQuads(); ++i) {
332 addSrcChoppedQuad(quadPts + 2 * i, !verbsInContour && 0 == i);
333 }
334 } else {
335 SkConic dst[4];
336 // We chop the conics to create tighter clipping to hide error
337 // that appears near max curvature of very thin conics. Thin
338 // hyperbolas with high weight still show error.
339 int conicCnt = chop_conic(pathPts, dst, iter.conicWeight());
340 for (int i = 0; i < conicCnt; ++i) {
341 SkPoint devPts[4];
342 SkPoint* chopPnts = dst[i].fPts;
343 m.mapPoints(devPts, chopPnts, 3);
344 bounds.setBounds(devPts, 3);
345 bounds.outset(SK_Scalar1, SK_Scalar1);
346 bounds.roundOut(&ibounds);
347 if (SkIRect::Intersects(devClipBounds, ibounds)) {
348 if (is_degen_quad_or_conic(devPts)) {
349 SkPoint* pts = lines->push_back_n(4);
350 pts[0] = devPts[0];
351 pts[1] = devPts[1];
352 pts[2] = devPts[1];
353 pts[3] = devPts[2];
354 if (verbsInContour == 0 && i == 0 && pts[0] == pts[1] &&
355 pts[2] == pts[3]) {
356 seenZeroLengthVerb = true;
357 zeroVerbPt = pts[0];
358 }
359 } else {
360 // when in perspective keep conics in src space
361 SkPoint* cPts = persp ? chopPnts : devPts;
362 SkPoint* pts = conics->push_back_n(3);
363 pts[0] = cPts[0];
364 pts[1] = cPts[1];
365 pts[2] = cPts[2];
366 conicWeights->push_back() = dst[i].fW;
Brian Osmancf3dc292017-06-28 16:45:32 -0400367 }
egdaniel@google.com5383a752013-07-12 20:15:34 +0000368 }
369 }
370 }
Brian Osmancf3dc292017-06-28 16:45:32 -0400371 verbsInContour++;
reed@google.com277c3f82013-05-31 15:17:50 +0000372 break;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000373 case SkPath::kMove_Verb:
Brian Osmancf3dc292017-06-28 16:45:32 -0400374 // New contour (and last one was unclosed). If it was just a zero length drawing
375 // operation, and we're supposed to draw caps, then add a tiny line.
376 if (seenZeroLengthVerb && verbsInContour == 1 && capLength > 0) {
377 SkPoint* pts = lines->push_back_n(2);
378 pts[0] = SkPoint::Make(zeroVerbPt.fX - capLength, zeroVerbPt.fY);
379 pts[1] = SkPoint::Make(zeroVerbPt.fX + capLength, zeroVerbPt.fY);
380 }
381 verbsInContour = 0;
382 seenZeroLengthVerb = false;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000383 break;
Brian Salomon969a7382018-05-09 13:28:44 -0400384 case SkPath::kLine_Verb: {
385 SkPoint devPts[2];
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000386 m.mapPoints(devPts, pathPts, 2);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000387 bounds.setBounds(devPts, 2);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000388 bounds.outset(SK_Scalar1, SK_Scalar1);
389 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000390 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000391 SkPoint* pts = lines->push_back_n(2);
392 pts[0] = devPts[0];
393 pts[1] = devPts[1];
Brian Osmancf3dc292017-06-28 16:45:32 -0400394 if (verbsInContour == 0 && pts[0] == pts[1]) {
395 seenZeroLengthVerb = true;
396 zeroVerbPt = pts[0];
397 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000398 }
Brian Osmancf3dc292017-06-28 16:45:32 -0400399 verbsInContour++;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000400 break;
Brian Salomon969a7382018-05-09 13:28:44 -0400401 }
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000402 case SkPath::kQuad_Verb: {
403 SkPoint choppedPts[5];
404 // Chopping the quad helps when the quad is either degenerate or nearly degenerate.
405 // When it is degenerate it allows the approximation with lines to work since the
406 // chop point (if there is one) will be at the parabola's vertex. In the nearly
407 // degenerate the QuadUVMatrix computed for the points is almost singular which
408 // can cause rendering artifacts.
409 int n = SkChopQuadAtMaxCurvature(pathPts, choppedPts);
410 for (int i = 0; i < n; ++i) {
Brian Salomon969a7382018-05-09 13:28:44 -0400411 addSrcChoppedQuad(choppedPts + i * 2, !verbsInContour && 0 == i);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000412 }
Brian Osmancf3dc292017-06-28 16:45:32 -0400413 verbsInContour++;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000414 break;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000415 }
Brian Salomon969a7382018-05-09 13:28:44 -0400416 case SkPath::kCubic_Verb: {
417 SkPoint devPts[4];
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000418 m.mapPoints(devPts, pathPts, 4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000419 bounds.setBounds(devPts, 4);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000420 bounds.outset(SK_Scalar1, SK_Scalar1);
421 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000422 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000423 PREALLOC_PTARRAY(32) q;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000424 // We convert cubics to quadratics (for now).
425 // In perspective have to do conversion in src space.
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000426 if (persp) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000427 SkScalar tolScale =
bsalomon18fab302016-02-16 08:00:05 -0800428 GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m, path.getBounds());
429 GrPathUtils::convertCubicToQuads(pathPts, tolScale, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000430 } else {
bsalomon18fab302016-02-16 08:00:05 -0800431 GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000432 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000433 for (int i = 0; i < q.count(); i += 3) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000434 if (persp) {
Brian Salomon969a7382018-05-09 13:28:44 -0400435 addSrcChoppedQuad(&q[i], !verbsInContour && 0 == i);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000436 } else {
Brian Salomon969a7382018-05-09 13:28:44 -0400437 addChoppedQuad(nullptr, &q[i], !verbsInContour && 0 == i);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000438 }
439 }
440 }
Brian Osmancf3dc292017-06-28 16:45:32 -0400441 verbsInContour++;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000442 break;
Brian Salomon969a7382018-05-09 13:28:44 -0400443 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000444 case SkPath::kClose_Verb:
Brian Osmancf3dc292017-06-28 16:45:32 -0400445 // Contour is closed, so we don't need to grow the starting line, unless it's
446 // *just* a zero length subpath. (SVG Spec 11.4, 'stroke').
447 if (capLength > 0) {
448 if (seenZeroLengthVerb && verbsInContour == 1) {
449 SkPoint* pts = lines->push_back_n(2);
450 pts[0] = SkPoint::Make(zeroVerbPt.fX - capLength, zeroVerbPt.fY);
451 pts[1] = SkPoint::Make(zeroVerbPt.fX + capLength, zeroVerbPt.fY);
452 } else if (verbsInContour == 0) {
453 // Contour was (moveTo, close). Add a line.
Brian Salomon969a7382018-05-09 13:28:44 -0400454 SkPoint devPts[2];
Brian Osmancf3dc292017-06-28 16:45:32 -0400455 m.mapPoints(devPts, pathPts, 1);
456 devPts[1] = devPts[0];
457 bounds.setBounds(devPts, 2);
458 bounds.outset(SK_Scalar1, SK_Scalar1);
459 bounds.roundOut(&ibounds);
460 if (SkIRect::Intersects(devClipBounds, ibounds)) {
461 SkPoint* pts = lines->push_back_n(2);
462 pts[0] = SkPoint::Make(devPts[0].fX - capLength, devPts[0].fY);
463 pts[1] = SkPoint::Make(devPts[1].fX + capLength, devPts[1].fY);
464 }
465 }
466 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000467 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000468 case SkPath::kDone_Verb:
Brian Osmancf3dc292017-06-28 16:45:32 -0400469 if (seenZeroLengthVerb && verbsInContour == 1 && capLength > 0) {
470 // Path ended with a dangling (moveTo, line|quad|etc). If the final verb is
471 // degenerate, we need to draw a line.
472 SkPoint* pts = lines->push_back_n(2);
473 pts[0] = SkPoint::Make(zeroVerbPt.fX - capLength, zeroVerbPt.fY);
474 pts[1] = SkPoint::Make(zeroVerbPt.fX + capLength, zeroVerbPt.fY);
475 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000476 return totalQuadCount;
477 }
478 }
479}
480
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000481struct LineVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000482 SkPoint fPos;
egdaniele27065a2014-11-06 08:00:48 -0800483 float fCoverage;
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000484};
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000485
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000486struct BezierVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000487 SkPoint fPos;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000488 union {
489 struct {
csmartdaltoncc261272017-03-23 13:38:45 -0600490 SkScalar fKLM[3];
egdaniel@google.com5383a752013-07-12 20:15:34 +0000491 } fConic;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000492 SkVector fQuadCoord;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000493 struct {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000494 SkScalar fBogus[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000495 };
496 };
497};
egdaniel@google.com5383a752013-07-12 20:15:34 +0000498
Brian Salomon4dea72a2019-12-18 10:43:10 -0500499static_assert(sizeof(BezierVertex) == 3 * sizeof(SkPoint));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000500
Robert Phillips461c5392021-08-17 16:42:51 -0400501void intersect_lines(const SkPoint& ptA, const SkVector& normA,
502 const SkPoint& ptB, const SkVector& normB,
503 SkPoint* result) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000504
505 SkScalar lineAW = -normA.dot(ptA);
506 SkScalar lineBW = -normB.dot(ptB);
507
Mike Reed8be952a2017-02-13 20:44:33 -0500508 SkScalar wInv = normA.fX * normB.fY - normA.fY * normB.fX;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000509 wInv = SkScalarInvert(wInv);
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400510 if (!SkScalarIsFinite(wInv)) {
511 // lines are parallel, pick the point in between
512 *result = (ptA + ptB)*SK_ScalarHalf;
513 *result += normA;
514 } else {
515 result->fX = normA.fY * lineBW - lineAW * normB.fY;
516 result->fX *= wInv;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000517
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400518 result->fY = lineAW * normB.fX - normA.fX * lineBW;
519 result->fY *= wInv;
520 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000521}
522
Robert Phillips461c5392021-08-17 16:42:51 -0400523void set_uv_quad(const SkPoint qpts[3], BezierVertex verts[kQuadNumVertices]) {
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000524 // this should be in the src space, not dev coords, when we have perspective
525 GrPathUtils::QuadUVMatrix DevToUV(qpts);
Brian Osman568bec72018-12-26 16:48:25 -0500526 DevToUV.apply(verts, kQuadNumVertices, sizeof(BezierVertex), sizeof(SkPoint));
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000527}
528
Robert Phillips461c5392021-08-17 16:42:51 -0400529void bloat_quad(const SkPoint qpts[3],
530 const SkMatrix* toDevice,
531 const SkMatrix* toSrc,
532 BezierVertex verts[kQuadNumVertices]) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000533 SkASSERT(!toDevice == !toSrc);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000534 // original quad is specified by tri a,b,c
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000535 SkPoint a = qpts[0];
536 SkPoint b = qpts[1];
537 SkPoint c = qpts[2];
538
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000539 if (toDevice) {
540 toDevice->mapPoints(&a, 1);
541 toDevice->mapPoints(&b, 1);
542 toDevice->mapPoints(&c, 1);
543 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000544 // make a new poly where we replace a and c by a 1-pixel wide edges orthog
545 // to edges ab and bc:
546 //
547 // before | after
548 // | b0
549 // b |
550 // |
551 // | a0 c0
552 // a c | a1 c1
553 //
554 // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c,
555 // respectively.
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000556 BezierVertex& a0 = verts[0];
557 BezierVertex& a1 = verts[1];
558 BezierVertex& b0 = verts[2];
559 BezierVertex& c0 = verts[3];
560 BezierVertex& c1 = verts[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000561
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000562 SkVector ab = b;
563 ab -= a;
564 SkVector ac = c;
565 ac -= a;
566 SkVector cb = b;
567 cb -= c;
568
Robert Phillips2c2303e2021-02-04 11:04:01 -0500569 // After the transform (or due to floating point math) we might have a line,
570 // try to do something reasonable
571 if (SkPointPriv::LengthSqd(ab) <= SK_ScalarNearlyZero*SK_ScalarNearlyZero) {
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400572 ab = cb;
573 }
Robert Phillips2c2303e2021-02-04 11:04:01 -0500574 if (SkPointPriv::LengthSqd(cb) <= SK_ScalarNearlyZero*SK_ScalarNearlyZero) {
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400575 cb = ab;
576 }
577
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000578 // We should have already handled degenerates
Robert Phillips2c2303e2021-02-04 11:04:01 -0500579 SkASSERT(ab.length() > 0 && cb.length() > 0);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000580
581 ab.normalize();
Brian Salomon0235c642018-08-31 12:04:18 -0400582 SkVector abN = SkPointPriv::MakeOrthog(ab, SkPointPriv::kLeft_Side);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000583 if (abN.dot(ac) > 0) {
584 abN.negate();
585 }
586
587 cb.normalize();
Brian Salomon0235c642018-08-31 12:04:18 -0400588 SkVector cbN = SkPointPriv::MakeOrthog(cb, SkPointPriv::kLeft_Side);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000589 if (cbN.dot(ac) < 0) {
590 cbN.negate();
591 }
592
593 a0.fPos = a;
594 a0.fPos += abN;
595 a1.fPos = a;
596 a1.fPos -= abN;
597
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400598 if (toDevice && SkPointPriv::LengthSqd(ac) <= SK_ScalarNearlyZero*SK_ScalarNearlyZero) {
599 c = b;
600 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000601 c0.fPos = c;
602 c0.fPos += cbN;
603 c1.fPos = c;
604 c1.fPos -= cbN;
605
606 intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos);
607
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000608 if (toSrc) {
Brian Salomonfa3783f2018-01-05 13:49:07 -0500609 SkMatrixPriv::MapPointsWithStride(*toSrc, &verts[0].fPos, sizeof(BezierVertex),
610 kQuadNumVertices);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000611 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000612}
613
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000614// Equations based off of Loop-Blinn Quadratic GPU Rendering
egdaniel@google.com5383a752013-07-12 20:15:34 +0000615// Input Parametric:
616// 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)
617// Output Implicit:
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000618// f(x, y, w) = f(P) = K^2 - LM
619// K = dot(k, P), L = dot(l, P), M = dot(m, P)
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000620// k, l, m are calculated in function GrPathUtils::getConicKLM
Robert Phillips461c5392021-08-17 16:42:51 -0400621void set_conic_coeffs(const SkPoint p[3],
622 BezierVertex verts[kQuadNumVertices],
623 const SkScalar weight) {
csmartdaltoncc261272017-03-23 13:38:45 -0600624 SkMatrix klm;
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000625
csmartdaltoncc261272017-03-23 13:38:45 -0600626 GrPathUtils::getConicKLM(p, weight, &klm);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000627
joshualitt5ead6da2014-10-22 16:00:29 -0700628 for (int i = 0; i < kQuadNumVertices; ++i) {
Cary Clarke4442cb2017-10-18 11:46:18 -0400629 const SkPoint3 pt3 = {verts[i].fPos.x(), verts[i].fPos.y(), 1.f};
630 klm.mapHomogeneousPoints((SkPoint3* ) verts[i].fConic.fKLM, &pt3, 1);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000631 }
632}
633
Robert Phillips461c5392021-08-17 16:42:51 -0400634void add_conics(const SkPoint p[3],
635 const SkScalar weight,
636 const SkMatrix* toDevice,
637 const SkMatrix* toSrc,
638 BezierVertex** vert) {
joshualitt7bc18b72015-02-03 16:41:41 -0800639 bloat_quad(p, toDevice, toSrc, *vert);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000640 set_conic_coeffs(p, *vert, weight);
joshualitt5ead6da2014-10-22 16:00:29 -0700641 *vert += kQuadNumVertices;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000642}
643
Robert Phillips461c5392021-08-17 16:42:51 -0400644void add_quads(const SkPoint p[3],
645 int subdiv,
646 const SkMatrix* toDevice,
647 const SkMatrix* toSrc,
648 BezierVertex** vert) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000649 SkASSERT(subdiv >= 0);
Jim Van Verth14982c82020-07-15 15:42:19 -0400650 // temporary vertex storage to avoid reading the vertex buffer
Jim Van Verth3557ab42020-07-28 09:46:03 -0400651 BezierVertex outVerts[kQuadNumVertices] = {};
Jim Van Verth14982c82020-07-15 15:42:19 -0400652
653 // storage for the chopped quad
654 // pts 0,1,2 are the first quad, and 2,3,4 the second quad
655 SkPoint choppedQuadPts[5];
656 // start off with our original curve in the second quad slot
657 memcpy(&choppedQuadPts[2], p, 3*sizeof(SkPoint));
658
659 int stepCount = 1 << subdiv;
660 while (stepCount > 1) {
661 // The general idea is:
662 // * chop the quad using pts 2,3,4 as the input
663 // * write out verts using pts 0,1,2
664 // * now 2,3,4 is the remainder of the curve, chop again until all subdivisions are done
665 SkScalar h = 1.f / stepCount;
666 SkChopQuadAt(&choppedQuadPts[2], choppedQuadPts, h);
667
668 bloat_quad(choppedQuadPts, toDevice, toSrc, outVerts);
669 set_uv_quad(choppedQuadPts, outVerts);
670 memcpy(*vert, outVerts, kQuadNumVertices*sizeof(BezierVertex));
joshualitt5ead6da2014-10-22 16:00:29 -0700671 *vert += kQuadNumVertices;
Jim Van Verth14982c82020-07-15 15:42:19 -0400672 --stepCount;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000673 }
Jim Van Verth14982c82020-07-15 15:42:19 -0400674
675 // finish up, write out the final quad
676 bloat_quad(&choppedQuadPts[2], toDevice, toSrc, outVerts);
677 set_uv_quad(&choppedQuadPts[2], outVerts);
678 memcpy(*vert, outVerts, kQuadNumVertices * sizeof(BezierVertex));
679 *vert += kQuadNumVertices;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000680}
681
Robert Phillips461c5392021-08-17 16:42:51 -0400682void add_line(const SkPoint p[2],
683 const SkMatrix* toSrc,
684 uint8_t coverage,
685 LineVertex** vert) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000686 const SkPoint& a = p[0];
687 const SkPoint& b = p[1];
688
robertphillips@google.comada90da2013-09-18 22:14:49 +0000689 SkVector ortho, vec = b;
690 vec -= a;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000691
Cary Clarkdf429f32017-11-08 11:44:31 -0500692 SkScalar lengthSqd = SkPointPriv::LengthSqd(vec);
Greg Daniel2e67dea2017-10-12 15:14:41 -0400693
robertphillips@google.comada90da2013-09-18 22:14:49 +0000694 if (vec.setLength(SK_ScalarHalf)) {
695 // Create a vector orthogonal to 'vec' and of unit length
696 ortho.fX = 2.0f * vec.fY;
697 ortho.fY = -2.0f * vec.fX;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000698
egdaniele27065a2014-11-06 08:00:48 -0800699 float floatCoverage = GrNormalizeByteToFloat(coverage);
700
Greg Daniel2e67dea2017-10-12 15:14:41 -0400701 if (lengthSqd >= 1.0f) {
702 // Relative to points a and b:
703 // The inner vertices are inset half a pixel along the line a,b
704 (*vert)[0].fPos = a + vec;
705 (*vert)[0].fCoverage = floatCoverage;
706 (*vert)[1].fPos = b - vec;
707 (*vert)[1].fCoverage = floatCoverage;
708 } else {
709 // The inner vertices are inset a distance of length(a,b) from the outer edge of
710 // geometry. For the "a" inset this is the same as insetting from b by half a pixel.
711 // The coverage is then modulated by the length. This gives us the correct
712 // coverage for rects shorter than a pixel as they get translated subpixel amounts
713 // inside of a pixel.
714 SkScalar length = SkScalarSqrt(lengthSqd);
715 (*vert)[0].fPos = b - vec;
716 (*vert)[0].fCoverage = floatCoverage * length;
717 (*vert)[1].fPos = a + vec;
718 (*vert)[1].fCoverage = floatCoverage * length;
719 }
720 // Relative to points a and b:
721 // The outer vertices are outset half a pixel along the line a,b and then a whole pixel
722 // orthogonally.
robertphillips@google.comada90da2013-09-18 22:14:49 +0000723 (*vert)[2].fPos = a - vec + ortho;
724 (*vert)[2].fCoverage = 0;
725 (*vert)[3].fPos = b + vec + ortho;
726 (*vert)[3].fCoverage = 0;
727 (*vert)[4].fPos = a - vec - ortho;
728 (*vert)[4].fCoverage = 0;
729 (*vert)[5].fPos = b + vec - ortho;
730 (*vert)[5].fCoverage = 0;
731
bsalomon49f085d2014-09-05 13:34:00 -0700732 if (toSrc) {
Brian Salomonfa3783f2018-01-05 13:49:07 -0500733 SkMatrixPriv::MapPointsWithStride(*toSrc, &(*vert)->fPos, sizeof(LineVertex),
734 kLineSegNumVertices);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000735 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000736 } else {
737 // just make it degenerate and likely offscreen
joshualitt5ead6da2014-10-22 16:00:29 -0700738 for (int i = 0; i < kLineSegNumVertices; ++i) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000739 (*vert)[i].fPos.set(SK_ScalarMax, SK_ScalarMax);
740 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000741 }
742
joshualitt5ead6da2014-10-22 16:00:29 -0700743 *vert += kLineSegNumVertices;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000744}
745
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000746///////////////////////////////////////////////////////////////////////////////
747
Brian Salomona531f252017-07-07 13:29:28 -0400748class AAHairlineOp final : public GrMeshDrawOp {
749private:
750 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
751
joshualitt7bc18b72015-02-03 16:41:41 -0800752public:
Brian Salomon25a88092016-12-01 09:36:50 -0500753 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700754
Herb Derbyc76d4092020-10-07 16:46:15 -0400755 static GrOp::Owner Make(GrRecordingContext* context,
756 GrPaint&& paint,
757 const SkMatrix& viewMatrix,
758 const SkPath& path,
759 const GrStyle& style,
760 const SkIRect& devClipBounds,
761 const GrUserStencilSettings* stencilSettings) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500762 SkScalar hairlineCoverage;
763 uint8_t newCoverage = 0xff;
Robert Phillips62214f72021-06-15 10:12:51 -0400764 if (GrIsStrokeHairlineOrEquivalent(style, viewMatrix, &hairlineCoverage)) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500765 newCoverage = SkScalarRoundToInt(hairlineCoverage * 0xff);
766 }
joshualitt7bc18b72015-02-03 16:41:41 -0800767
Brian Osmancf3dc292017-06-28 16:45:32 -0400768 const SkStrokeRec& stroke = style.strokeRec();
769 SkScalar capLength = SkPaint::kButt_Cap != stroke.getCap() ? hairlineCoverage * 0.5f : 0.0f;
770
Robert Phillips7c525e62018-06-12 10:11:12 -0400771 return Helper::FactoryHelper<AAHairlineOp>(context, std::move(paint), newCoverage,
772 viewMatrix, path,
Brian Salomona531f252017-07-07 13:29:28 -0400773 devClipBounds, capLength, stencilSettings);
774 }
775
Herb Derbyc76d4092020-10-07 16:46:15 -0400776 AAHairlineOp(GrProcessorSet* processorSet,
Brian Osmancf860852018-10-31 14:04:39 -0400777 const SkPMColor4f& color,
Brian Salomona531f252017-07-07 13:29:28 -0400778 uint8_t coverage,
779 const SkMatrix& viewMatrix,
780 const SkPath& path,
781 SkIRect devClipBounds,
782 SkScalar capLength,
783 const GrUserStencilSettings* stencilSettings)
784 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -0400785 , fHelper(processorSet, GrAAType::kCoverage, stencilSettings)
Brian Salomona531f252017-07-07 13:29:28 -0400786 , fColor(color)
787 , fCoverage(coverage) {
788 fPaths.emplace_back(PathData{viewMatrix, path, devClipBounds, capLength});
789
790 this->setTransformedBounds(path.getBounds(), viewMatrix, HasAABloat::kYes,
Greg Daniel5faf4742019-10-01 15:14:44 -0400791 IsHairline::kYes);
bsalomonf1703092016-06-29 18:41:53 -0700792 }
joshualitt7bc18b72015-02-03 16:41:41 -0800793
Brian Salomond0a0a652016-12-15 15:25:22 -0500794 const char* name() const override { return "AAHairlineOp"; }
joshualitt7bc18b72015-02-03 16:41:41 -0800795
Robert Phillips294723d2021-06-17 09:23:58 -0400796 void visitProxies(const GrVisitProxyFunc& func) const override {
Robert Phillips4f93c572020-03-18 08:13:53 -0400797
798 bool visited = false;
799 for (int i = 0; i < 3; ++i) {
800 if (fProgramInfos[i]) {
801 fProgramInfos[i]->visitFPProxies(func);
802 visited = true;
803 }
804 }
805
806 if (!visited) {
807 fHelper.visitProxies(func);
808 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400809 }
810
Brian Salomona531f252017-07-07 13:29:28 -0400811 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
812
Chris Dalton57ab06c2021-04-22 12:57:28 -0600813 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
814 GrClampType clampType) override {
Brian Osman8fa7ab42019-03-18 10:22:42 -0400815 // This Op uses uniform (not vertex) color, so doesn't need to track wide color.
Chris Dalton57ab06c2021-04-22 12:57:28 -0600816 return fHelper.finalizeProcessors(caps, clip, clampType,
Brian Osman8fa7ab42019-03-18 10:22:42 -0400817 GrProcessorAnalysisCoverage::kSingleChannel, &fColor,
818 nullptr);
Brian Salomona531f252017-07-07 13:29:28 -0400819 }
820
Robert Phillips461c5392021-08-17 16:42:51 -0400821 enum class Program : uint8_t {
822 kNone = 0x0,
823 kLine = 0x1,
824 kQuad = 0x2,
825 kConic = 0x4,
Robert Phillips4f93c572020-03-18 08:13:53 -0400826 };
827
bsalomone46f9fe2015-08-18 06:05:14 -0700828private:
Robert Phillips4f93c572020-03-18 08:13:53 -0400829 void makeLineProgramInfo(const GrCaps&, SkArenaAlloc*, const GrPipeline*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500830 const GrSurfaceProxyView& writeView,
Chris Dalton2a26c502021-08-26 10:05:11 -0600831 bool usesMSAASurface,
Robert Phillips4f93c572020-03-18 08:13:53 -0400832 const SkMatrix* geometryProcessorViewM,
Greg Danield358cbe2020-09-11 09:33:54 -0400833 const SkMatrix* geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -0500834 GrXferBarrierFlags renderPassXferBarriers,
835 GrLoadOp colorLoadOp);
Robert Phillips4f93c572020-03-18 08:13:53 -0400836 void makeQuadProgramInfo(const GrCaps&, SkArenaAlloc*, const GrPipeline*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500837 const GrSurfaceProxyView& writeView,
Chris Dalton2a26c502021-08-26 10:05:11 -0600838 bool usesMSAASurface,
Robert Phillips4f93c572020-03-18 08:13:53 -0400839 const SkMatrix* geometryProcessorViewM,
Greg Danield358cbe2020-09-11 09:33:54 -0400840 const SkMatrix* geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -0500841 GrXferBarrierFlags renderPassXferBarriers,
842 GrLoadOp colorLoadOp);
Robert Phillips4f93c572020-03-18 08:13:53 -0400843 void makeConicProgramInfo(const GrCaps&, SkArenaAlloc*, const GrPipeline*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500844 const GrSurfaceProxyView& writeView,
Chris Dalton2a26c502021-08-26 10:05:11 -0600845 bool usesMSAASurface,
Robert Phillips4f93c572020-03-18 08:13:53 -0400846 const SkMatrix* geometryProcessorViewM,
Greg Danield358cbe2020-09-11 09:33:54 -0400847 const SkMatrix* geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -0500848 GrXferBarrierFlags renderPassXferBarriers,
849 GrLoadOp colorLoadOp);
Robert Phillips1b3c2672019-12-04 14:34:48 -0500850
Robert Phillips2669a7b2020-03-12 12:07:19 -0400851 GrProgramInfo* programInfo() override {
852 // This Op has 3 programInfos and implements its own onPrePrepareDraws so this entry point
853 // should really never be called.
854 SkASSERT(0);
855 return nullptr;
856 }
857
Robert Phillips4f93c572020-03-18 08:13:53 -0400858 Program predictPrograms(const GrCaps*) const;
859
Robert Phillips4133dc42020-03-11 15:55:55 -0400860 void onCreateProgramInfo(const GrCaps*,
861 SkArenaAlloc*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500862 const GrSurfaceProxyView& writeView,
Chris Dalton6aaf00f2021-07-13 13:26:39 -0600863 bool usesMSAASurface,
Robert Phillips4133dc42020-03-11 15:55:55 -0400864 GrAppliedClip&&,
John Stiles52cb1d02021-06-02 11:58:05 -0400865 const GrDstProxyView&,
Greg Daniel42dbca52020-11-20 10:22:43 -0500866 GrXferBarrierFlags renderPassXferBarriers,
867 GrLoadOp colorLoadOp) override;
Robert Phillips4133dc42020-03-11 15:55:55 -0400868
Robert Phillips2669a7b2020-03-12 12:07:19 -0400869 void onPrePrepareDraws(GrRecordingContext*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500870 const GrSurfaceProxyView& writeView,
Robert Phillips2669a7b2020-03-12 12:07:19 -0400871 GrAppliedClip*,
John Stiles52cb1d02021-06-02 11:58:05 -0400872 const GrDstProxyView&,
Greg Daniel42dbca52020-11-20 10:22:43 -0500873 GrXferBarrierFlags renderPassXferBarriers,
874 GrLoadOp colorLoadOp) override;
Robert Phillips2669a7b2020-03-12 12:07:19 -0400875
Robert Phillips71143952021-06-17 14:55:07 -0400876 void onPrepareDraws(GrMeshDrawTarget*) override;
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700877 void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
bsalomon75398562015-08-17 12:55:38 -0700878
Herb Derbye25c3002020-10-27 15:57:27 -0400879 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500880 AAHairlineOp* that = t->cast<AAHairlineOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700881
Brian Salomona531f252017-07-07 13:29:28 -0400882 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000883 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700884 }
885
joshualitt7bc18b72015-02-03 16:41:41 -0800886 if (this->viewMatrix().hasPerspective() != that->viewMatrix().hasPerspective()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000887 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800888 }
889
890 // We go to identity if we don't have perspective
891 if (this->viewMatrix().hasPerspective() &&
Mike Reed2c383152019-12-18 16:47:47 -0500892 !SkMatrixPriv::CheapEqual(this->viewMatrix(), that->viewMatrix())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000893 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800894 }
895
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500896 // TODO we can actually combine hairlines if they are the same color in a kind of bulk
897 // method but we haven't implemented this yet
joshualitt7bc18b72015-02-03 16:41:41 -0800898 // TODO investigate going to vertex color and coverage?
899 if (this->coverage() != that->coverage()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000900 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800901 }
902
903 if (this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000904 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800905 }
906
Mike Reed2c383152019-12-18 16:47:47 -0500907 if (fHelper.usesLocalCoords() && !SkMatrixPriv::CheapEqual(this->viewMatrix(),
908 that->viewMatrix())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000909 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800910 }
911
Brian Salomond0a0a652016-12-15 15:25:22 -0500912 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000913 return CombineResult::kMerged;
joshualitt7bc18b72015-02-03 16:41:41 -0800914 }
915
John Stilesaf366522020-08-13 09:57:34 -0400916#if GR_TEST_UTILS
917 SkString onDumpInfo() const override {
918 return SkStringPrintf("Color: 0x%08x Coverage: 0x%02x, Count: %d\n%s",
919 fColor.toBytes_RGBA(), fCoverage, fPaths.count(),
920 fHelper.dumpInfo().c_str());
921 }
922#endif
923
Brian Osmancf860852018-10-31 14:04:39 -0400924 const SkPMColor4f& color() const { return fColor; }
Brian Salomond0a0a652016-12-15 15:25:22 -0500925 uint8_t coverage() const { return fCoverage; }
Brian Salomond0a0a652016-12-15 15:25:22 -0500926 const SkMatrix& viewMatrix() const { return fPaths[0].fViewMatrix; }
joshualitt7bc18b72015-02-03 16:41:41 -0800927
Brian Salomond0a0a652016-12-15 15:25:22 -0500928 struct PathData {
bsalomonf1703092016-06-29 18:41:53 -0700929 SkMatrix fViewMatrix;
930 SkPath fPath;
931 SkIRect fDevClipBounds;
Brian Osmancf3dc292017-06-28 16:45:32 -0400932 SkScalar fCapLength;
bsalomonf1703092016-06-29 18:41:53 -0700933 };
934
Brian Salomona531f252017-07-07 13:29:28 -0400935 SkSTArray<1, PathData, true> fPaths;
936 Helper fHelper;
Brian Osmancf860852018-10-31 14:04:39 -0400937 SkPMColor4f fColor;
Brian Salomond0a0a652016-12-15 15:25:22 -0500938 uint8_t fCoverage;
joshualitt7bc18b72015-02-03 16:41:41 -0800939
Robert Phillips461c5392021-08-17 16:42:51 -0400940 Program fCharacterization = Program::kNone; // holds a mask of required programs
Robert Phillips4f93c572020-03-18 08:13:53 -0400941 GrSimpleMesh* fMeshes[3] = { nullptr };
942 GrProgramInfo* fProgramInfos[3] = { nullptr };
943
John Stiles7571f9e2020-09-02 22:42:33 -0400944 using INHERITED = GrMeshDrawOp;
joshualitt7bc18b72015-02-03 16:41:41 -0800945};
946
Robert Phillips461c5392021-08-17 16:42:51 -0400947GR_MAKE_BITFIELD_CLASS_OPS(AAHairlineOp::Program)
Brian Salomona531f252017-07-07 13:29:28 -0400948
Robert Phillips4f93c572020-03-18 08:13:53 -0400949void AAHairlineOp::makeLineProgramInfo(const GrCaps& caps, SkArenaAlloc* arena,
950 const GrPipeline* pipeline,
Adlai Hollere2296f72020-11-19 13:41:26 -0500951 const GrSurfaceProxyView& writeView,
Chris Dalton2a26c502021-08-26 10:05:11 -0600952 bool usesMSAASurface,
Robert Phillips4f93c572020-03-18 08:13:53 -0400953 const SkMatrix* geometryProcessorViewM,
Greg Danield358cbe2020-09-11 09:33:54 -0400954 const SkMatrix* geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -0500955 GrXferBarrierFlags renderPassXferBarriers,
956 GrLoadOp colorLoadOp) {
Robert Phillips4f93c572020-03-18 08:13:53 -0400957 if (fProgramInfos[0]) {
958 return;
959 }
Robert Phillips1b3c2672019-12-04 14:34:48 -0500960
Robert Phillips4f93c572020-03-18 08:13:53 -0400961 GrGeometryProcessor* lineGP;
962 {
963 using namespace GrDefaultGeoProcFactory;
Robert Phillips1b3c2672019-12-04 14:34:48 -0500964
Robert Phillips4f93c572020-03-18 08:13:53 -0400965 Color color(this->color());
966 LocalCoords localCoords(fHelper.usesLocalCoords() ? LocalCoords::kUsePosition_Type
967 : LocalCoords::kUnused_Type);
968 localCoords.fMatrix = geometryProcessorLocalM;
Robert Phillips1b3c2672019-12-04 14:34:48 -0500969
Robert Phillips4f93c572020-03-18 08:13:53 -0400970 lineGP = GrDefaultGeoProcFactory::Make(arena,
971 color,
972 Coverage::kAttribute_Type,
973 localCoords,
974 *geometryProcessorViewM);
975 SkASSERT(sizeof(LineVertex) == lineGP->vertexStride());
976 }
977
Greg Danield358cbe2020-09-11 09:33:54 -0400978 fProgramInfos[0] = GrSimpleMeshDrawOpHelper::CreateProgramInfo(
Chris Dalton2a26c502021-08-26 10:05:11 -0600979 &caps, arena, pipeline, writeView, usesMSAASurface, lineGP, GrPrimitiveType::kTriangles,
Greg Daniel42dbca52020-11-20 10:22:43 -0500980 renderPassXferBarriers, colorLoadOp, fHelper.stencilSettings());
Robert Phillips1b3c2672019-12-04 14:34:48 -0500981}
982
Robert Phillips4f93c572020-03-18 08:13:53 -0400983void AAHairlineOp::makeQuadProgramInfo(const GrCaps& caps, SkArenaAlloc* arena,
984 const GrPipeline* pipeline,
Adlai Hollere2296f72020-11-19 13:41:26 -0500985 const GrSurfaceProxyView& writeView,
Chris Dalton2a26c502021-08-26 10:05:11 -0600986 bool usesMSAASurface,
Robert Phillips4f93c572020-03-18 08:13:53 -0400987 const SkMatrix* geometryProcessorViewM,
Greg Danield358cbe2020-09-11 09:33:54 -0400988 const SkMatrix* geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -0500989 GrXferBarrierFlags renderPassXferBarriers,
990 GrLoadOp colorLoadOp) {
Robert Phillips4f93c572020-03-18 08:13:53 -0400991 if (fProgramInfos[1]) {
992 return;
993 }
994
Robert Phillips1b3c2672019-12-04 14:34:48 -0500995 GrGeometryProcessor* quadGP = GrQuadEffect::Make(arena,
996 this->color(),
997 *geometryProcessorViewM,
Robert Phillips1b3c2672019-12-04 14:34:48 -0500998 caps,
999 *geometryProcessorLocalM,
1000 fHelper.usesLocalCoords(),
1001 this->coverage());
1002 SkASSERT(sizeof(BezierVertex) == quadGP->vertexStride());
1003
Greg Danield358cbe2020-09-11 09:33:54 -04001004 fProgramInfos[1] = GrSimpleMeshDrawOpHelper::CreateProgramInfo(
Chris Dalton2a26c502021-08-26 10:05:11 -06001005 &caps, arena, pipeline, writeView, usesMSAASurface, quadGP, GrPrimitiveType::kTriangles,
Greg Daniel42dbca52020-11-20 10:22:43 -05001006 renderPassXferBarriers, colorLoadOp, fHelper.stencilSettings());
Robert Phillips1b3c2672019-12-04 14:34:48 -05001007}
1008
Robert Phillips4f93c572020-03-18 08:13:53 -04001009void AAHairlineOp::makeConicProgramInfo(const GrCaps& caps, SkArenaAlloc* arena,
1010 const GrPipeline* pipeline,
Adlai Hollere2296f72020-11-19 13:41:26 -05001011 const GrSurfaceProxyView& writeView,
Chris Dalton2a26c502021-08-26 10:05:11 -06001012 bool usesMSAASurface,
Robert Phillips4f93c572020-03-18 08:13:53 -04001013 const SkMatrix* geometryProcessorViewM,
Greg Danield358cbe2020-09-11 09:33:54 -04001014 const SkMatrix* geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -05001015 GrXferBarrierFlags renderPassXferBarriers,
1016 GrLoadOp colorLoadOp) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001017 if (fProgramInfos[2]) {
1018 return;
1019 }
1020
Robert Phillips1b3c2672019-12-04 14:34:48 -05001021 GrGeometryProcessor* conicGP = GrConicEffect::Make(arena,
1022 this->color(),
1023 *geometryProcessorViewM,
Robert Phillips1b3c2672019-12-04 14:34:48 -05001024 caps,
1025 *geometryProcessorLocalM,
1026 fHelper.usesLocalCoords(),
1027 this->coverage());
1028 SkASSERT(sizeof(BezierVertex) == conicGP->vertexStride());
1029
Greg Danield358cbe2020-09-11 09:33:54 -04001030 fProgramInfos[2] = GrSimpleMeshDrawOpHelper::CreateProgramInfo(
Chris Dalton2a26c502021-08-26 10:05:11 -06001031 &caps, arena, pipeline, writeView, usesMSAASurface, conicGP,
1032 GrPrimitiveType::kTriangles, renderPassXferBarriers, colorLoadOp,
1033 fHelper.stencilSettings());
Robert Phillips1b3c2672019-12-04 14:34:48 -05001034}
1035
Robert Phillips4f93c572020-03-18 08:13:53 -04001036AAHairlineOp::Program AAHairlineOp::predictPrograms(const GrCaps* caps) const {
1037 bool convertConicsToQuads = !caps->shaderCaps()->floatIs32Bits();
1038
1039 // When predicting the programs we always include the lineProgram bc it is used as a fallback
1040 // for quads and conics. In non-DDL mode there are cases where it sometimes isn't needed for a
1041 // given path.
Robert Phillips461c5392021-08-17 16:42:51 -04001042 Program neededPrograms = Program::kLine;
Robert Phillips4f93c572020-03-18 08:13:53 -04001043
1044 for (int i = 0; i < fPaths.count(); i++) {
1045 uint32_t mask = fPaths[i].fPath.getSegmentMasks();
1046
1047 if (mask & (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)) {
Robert Phillips461c5392021-08-17 16:42:51 -04001048 neededPrograms |= Program::kQuad;
Robert Phillips4f93c572020-03-18 08:13:53 -04001049 }
1050 if (mask & SkPath::kConic_SegmentMask) {
1051 if (convertConicsToQuads) {
Robert Phillips461c5392021-08-17 16:42:51 -04001052 neededPrograms |= Program::kQuad;
Robert Phillips4f93c572020-03-18 08:13:53 -04001053 } else {
Robert Phillips461c5392021-08-17 16:42:51 -04001054 neededPrograms |= Program::kConic;
Robert Phillips4f93c572020-03-18 08:13:53 -04001055 }
1056 }
1057 }
1058
1059 return neededPrograms;
1060}
1061
1062void AAHairlineOp::onCreateProgramInfo(const GrCaps* caps,
1063 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -05001064 const GrSurfaceProxyView& writeView,
Chris Dalton6aaf00f2021-07-13 13:26:39 -06001065 bool usesMSAASurface,
Robert Phillips4f93c572020-03-18 08:13:53 -04001066 GrAppliedClip&& appliedClip,
John Stiles52cb1d02021-06-02 11:58:05 -04001067 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -05001068 GrXferBarrierFlags renderPassXferBarriers,
1069 GrLoadOp colorLoadOp) {
joshualitt7bc18b72015-02-03 16:41:41 -08001070 // Setup the viewmatrix and localmatrix for the GrGeometryProcessor.
1071 SkMatrix invert;
1072 if (!this->viewMatrix().invert(&invert)) {
1073 return;
1074 }
1075
1076 // we will transform to identity space if the viewmatrix does not have perspective
1077 bool hasPerspective = this->viewMatrix().hasPerspective();
1078 const SkMatrix* geometryProcessorViewM = &SkMatrix::I();
1079 const SkMatrix* geometryProcessorLocalM = &invert;
joshualitt7bc18b72015-02-03 16:41:41 -08001080 if (hasPerspective) {
1081 geometryProcessorViewM = &this->viewMatrix();
1082 geometryProcessorLocalM = &SkMatrix::I();
Robert Phillips4f93c572020-03-18 08:13:53 -04001083 }
1084
Adlai Hollere2296f72020-11-19 13:41:26 -05001085 auto pipeline = fHelper.createPipeline(caps, arena, writeView.swizzle(),
Chris Dalton1b6a43c2020-09-25 12:21:18 -06001086 std::move(appliedClip), dstProxyView);
Robert Phillips4f93c572020-03-18 08:13:53 -04001087
Robert Phillips461c5392021-08-17 16:42:51 -04001088 if (fCharacterization & Program::kLine) {
Chris Dalton2a26c502021-08-26 10:05:11 -06001089 this->makeLineProgramInfo(*caps, arena, pipeline, writeView, usesMSAASurface,
Greg Danield358cbe2020-09-11 09:33:54 -04001090 geometryProcessorViewM, geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -05001091 renderPassXferBarriers, colorLoadOp);
Robert Phillips4f93c572020-03-18 08:13:53 -04001092 }
Robert Phillips461c5392021-08-17 16:42:51 -04001093 if (fCharacterization & Program::kQuad) {
Chris Dalton2a26c502021-08-26 10:05:11 -06001094 this->makeQuadProgramInfo(*caps, arena, pipeline, writeView, usesMSAASurface,
Greg Danield358cbe2020-09-11 09:33:54 -04001095 geometryProcessorViewM, geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -05001096 renderPassXferBarriers, colorLoadOp);
Robert Phillips4f93c572020-03-18 08:13:53 -04001097 }
Robert Phillips461c5392021-08-17 16:42:51 -04001098 if (fCharacterization & Program::kConic) {
Chris Dalton2a26c502021-08-26 10:05:11 -06001099 this->makeConicProgramInfo(*caps, arena, pipeline, writeView, usesMSAASurface,
Greg Danield358cbe2020-09-11 09:33:54 -04001100 geometryProcessorViewM, geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -05001101 renderPassXferBarriers, colorLoadOp);
Robert Phillips4f93c572020-03-18 08:13:53 -04001102
1103 }
1104}
1105
1106void AAHairlineOp::onPrePrepareDraws(GrRecordingContext* context,
Adlai Hollere2296f72020-11-19 13:41:26 -05001107 const GrSurfaceProxyView& writeView,
Robert Phillips4f93c572020-03-18 08:13:53 -04001108 GrAppliedClip* clip,
John Stiles52cb1d02021-06-02 11:58:05 -04001109 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -05001110 GrXferBarrierFlags renderPassXferBarriers,
1111 GrLoadOp colorLoadOp) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001112 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
1113 const GrCaps* caps = context->priv().caps();
1114
Chris Dalton6aaf00f2021-07-13 13:26:39 -06001115 // http://skbug.com/12201 -- DDL does not yet support DMSAA.
1116 bool usesMSAASurface = writeView.asRenderTargetProxy()->numSamples() > 1;
1117
Robert Phillips4f93c572020-03-18 08:13:53 -04001118 // This is equivalent to a GrOpFlushState::detachAppliedClip
Michael Ludwigd1d997e2020-06-04 15:52:44 -04001119 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip::Disabled();
Robert Phillips4f93c572020-03-18 08:13:53 -04001120
1121 // Conservatively predict which programs will be required
1122 fCharacterization = this->predictPrograms(caps);
1123
Chris Dalton6aaf00f2021-07-13 13:26:39 -06001124 this->createProgramInfo(caps, arena, writeView, usesMSAASurface, std::move(appliedClip),
1125 dstProxyView, renderPassXferBarriers, colorLoadOp);
Robert Phillips4f93c572020-03-18 08:13:53 -04001126
1127 context->priv().recordProgramInfo(fProgramInfos[0]);
1128 context->priv().recordProgramInfo(fProgramInfos[1]);
1129 context->priv().recordProgramInfo(fProgramInfos[2]);
1130}
1131
Robert Phillips71143952021-06-17 14:55:07 -04001132void AAHairlineOp::onPrepareDraws(GrMeshDrawTarget* target) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001133 // Setup the viewmatrix and localmatrix for the GrGeometryProcessor.
1134 SkMatrix invert;
1135 if (!this->viewMatrix().invert(&invert)) {
1136 return;
1137 }
1138
1139 // we will transform to identity space if the viewmatrix does not have perspective
1140 const SkMatrix* toDevice = nullptr;
1141 const SkMatrix* toSrc = nullptr;
1142 if (this->viewMatrix().hasPerspective()) {
joshualitt7bc18b72015-02-03 16:41:41 -08001143 toDevice = &this->viewMatrix();
1144 toSrc = &invert;
1145 }
1146
Robert Phillips4f93c572020-03-18 08:13:53 -04001147 SkDEBUGCODE(Program predictedPrograms = this->predictPrograms(&target->caps()));
Robert Phillips461c5392021-08-17 16:42:51 -04001148 Program actualPrograms = Program::kNone;
Robert Phillips4f93c572020-03-18 08:13:53 -04001149
joshualitt7bc18b72015-02-03 16:41:41 -08001150 // This is hand inlined for maximum performance.
1151 PREALLOC_PTARRAY(128) lines;
1152 PREALLOC_PTARRAY(128) quads;
1153 PREALLOC_PTARRAY(128) conics;
1154 IntArray qSubdivs;
1155 FloatArray cWeights;
joshualitt351ba1b2015-02-16 08:33:19 -08001156 int quadCount = 0;
joshualitt7bc18b72015-02-03 16:41:41 -08001157
Brian Salomond0a0a652016-12-15 15:25:22 -05001158 int instanceCount = fPaths.count();
Brian Salomon969a7382018-05-09 13:28:44 -04001159 bool convertConicsToQuads = !target->caps().shaderCaps()->floatIs32Bits();
joshualitt7bc18b72015-02-03 16:41:41 -08001160 for (int i = 0; i < instanceCount; i++) {
Brian Salomond0a0a652016-12-15 15:25:22 -05001161 const PathData& args = fPaths[i];
joshualitt351ba1b2015-02-16 08:33:19 -08001162 quadCount += gather_lines_and_quads(args.fPath, args.fViewMatrix, args.fDevClipBounds,
Brian Salomon969a7382018-05-09 13:28:44 -04001163 args.fCapLength, convertConicsToQuads, &lines, &quads,
1164 &conics, &qSubdivs, &cWeights);
joshualitt7bc18b72015-02-03 16:41:41 -08001165 }
1166
joshualitt7bc18b72015-02-03 16:41:41 -08001167 int lineCount = lines.count() / 2;
1168 int conicCount = conics.count() / 3;
Brian Salomon296de502018-03-13 12:26:55 -04001169 int quadAndConicCount = conicCount + quadCount;
1170
1171 static constexpr int kMaxLines = SK_MaxS32 / kLineSegNumVertices;
1172 static constexpr int kMaxQuadsAndConics = SK_MaxS32 / kQuadNumVertices;
1173 if (lineCount > kMaxLines || quadAndConicCount > kMaxQuadsAndConics) {
1174 return;
1175 }
joshualitt7bc18b72015-02-03 16:41:41 -08001176
1177 // do lines first
1178 if (lineCount) {
Robert Phillips461c5392021-08-17 16:42:51 -04001179 SkASSERT(predictedPrograms & Program::kLine);
1180 actualPrograms |= Program::kLine;
Robert Phillips4f93c572020-03-18 08:13:53 -04001181
Brian Salomond28a79d2017-10-16 13:01:07 -04001182 sk_sp<const GrBuffer> linesIndexBuffer = get_lines_index_buffer(target->resourceProvider());
joshualitt7bc18b72015-02-03 16:41:41 -08001183
Robert Phillipse94cdd22019-11-04 14:15:58 -05001184 GrMeshDrawOp::PatternHelper helper(target, GrPrimitiveType::kTriangles, sizeof(LineVertex),
1185 std::move(linesIndexBuffer), kLineSegNumVertices,
1186 kIdxsPerLineSeg, lineCount, kLineSegsNumInIdxBuffer);
1187
1188 LineVertex* verts = reinterpret_cast<LineVertex*>(helper.vertices());
1189 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -08001190 SkDebugf("Could not allocate vertices\n");
1191 return;
1192 }
1193
joshualitt7bc18b72015-02-03 16:41:41 -08001194 for (int i = 0; i < lineCount; ++i) {
1195 add_line(&lines[2*i], toSrc, this->coverage(), &verts);
1196 }
1197
Robert Phillips4f93c572020-03-18 08:13:53 -04001198 fMeshes[0] = helper.mesh();
joshualitt7bc18b72015-02-03 16:41:41 -08001199 }
1200
1201 if (quadCount || conicCount) {
Brian Salomon12d22642019-01-29 14:38:50 -05001202 sk_sp<const GrBuffer> vertexBuffer;
joshualitt7bc18b72015-02-03 16:41:41 -08001203 int firstVertex;
1204
Brian Salomond28a79d2017-10-16 13:01:07 -04001205 sk_sp<const GrBuffer> quadsIndexBuffer = get_quads_index_buffer(target->resourceProvider());
bsalomoned0bcad2015-05-04 10:36:42 -07001206
Brian Salomon296de502018-03-13 12:26:55 -04001207 int vertexCount = kQuadNumVertices * quadAndConicCount;
Brian Salomon92be2f72018-06-19 14:33:47 -04001208 void* vertices = target->makeVertexSpace(sizeof(BezierVertex), vertexCount, &vertexBuffer,
1209 &firstVertex);
joshualitt7bc18b72015-02-03 16:41:41 -08001210
bsalomoned0bcad2015-05-04 10:36:42 -07001211 if (!vertices || !quadsIndexBuffer) {
joshualitt4b31de82015-03-05 14:33:41 -08001212 SkDebugf("Could not allocate vertices\n");
1213 return;
1214 }
1215
joshualitt7bc18b72015-02-03 16:41:41 -08001216 // Setup vertices
robertphillips44c31282015-09-03 12:58:48 -07001217 BezierVertex* bezVerts = reinterpret_cast<BezierVertex*>(vertices);
joshualitt7bc18b72015-02-03 16:41:41 -08001218
joshualitt7bc18b72015-02-03 16:41:41 -08001219 int unsubdivQuadCnt = quads.count() / 3;
1220 for (int i = 0; i < unsubdivQuadCnt; ++i) {
1221 SkASSERT(qSubdivs[i] >= 0);
Brian Osmanae8e0632021-07-26 16:07:35 -04001222 if (!quads[3*i].isFinite() || !quads[3*i+1].isFinite() || !quads[3*i+2].isFinite()) {
1223 return;
1224 }
robertphillips44c31282015-09-03 12:58:48 -07001225 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &bezVerts);
joshualitt7bc18b72015-02-03 16:41:41 -08001226 }
1227
1228 // Start Conics
1229 for (int i = 0; i < conicCount; ++i) {
robertphillips44c31282015-09-03 12:58:48 -07001230 add_conics(&conics[3*i], cWeights[i], toDevice, toSrc, &bezVerts);
joshualitt7bc18b72015-02-03 16:41:41 -08001231 }
1232
1233 if (quadCount > 0) {
Robert Phillips461c5392021-08-17 16:42:51 -04001234 SkASSERT(predictedPrograms & Program::kQuad);
1235 actualPrograms |= Program::kQuad;
Robert Phillips1b3c2672019-12-04 14:34:48 -05001236
Robert Phillips4f93c572020-03-18 08:13:53 -04001237 fMeshes[1] = target->allocMesh();
1238 fMeshes[1]->setIndexedPatterned(quadsIndexBuffer, kIdxsPerQuad, quadCount,
1239 kQuadsNumInIdxBuffer, vertexBuffer, kQuadNumVertices,
1240 firstVertex);
bsalomon342bfc22016-04-01 06:06:20 -07001241 firstVertex += quadCount * kQuadNumVertices;
joshualitt7bc18b72015-02-03 16:41:41 -08001242 }
1243
1244 if (conicCount > 0) {
Robert Phillips461c5392021-08-17 16:42:51 -04001245 SkASSERT(predictedPrograms & Program::kConic);
1246 actualPrograms |= Program::kConic;
Robert Phillips1b3c2672019-12-04 14:34:48 -05001247
Robert Phillips4f93c572020-03-18 08:13:53 -04001248 fMeshes[2] = target->allocMesh();
1249 fMeshes[2]->setIndexedPatterned(std::move(quadsIndexBuffer), kIdxsPerQuad, conicCount,
1250 kQuadsNumInIdxBuffer, std::move(vertexBuffer),
1251 kQuadNumVertices, firstVertex);
joshualitt7bc18b72015-02-03 16:41:41 -08001252 }
1253 }
Robert Phillips4f93c572020-03-18 08:13:53 -04001254
1255 // In DDL mode this will replace the predicted program requirements with the actual ones.
1256 // However, we will already have surfaced the predicted programs to the DDL.
1257 fCharacterization = actualPrograms;
joshualitt7bc18b72015-02-03 16:41:41 -08001258}
1259
Chris Dalton07cdcfc92019-02-26 11:13:22 -07001260void AAHairlineOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001261 this->createProgramInfo(flushState);
Robert Phillips3968fcb2019-12-05 16:40:31 -05001262
Robert Phillips4f93c572020-03-18 08:13:53 -04001263 for (int i = 0; i < 3; ++i) {
1264 if (fProgramInfos[i] && fMeshes[i]) {
1265 flushState->bindPipelineAndScissorClip(*fProgramInfos[i], chainBounds);
Robert Phillips787fd9d2021-03-22 14:48:09 -04001266 flushState->bindTextures(fProgramInfos[i]->geomProc(), nullptr,
Robert Phillips4f93c572020-03-18 08:13:53 -04001267 fProgramInfos[i]->pipeline());
1268 flushState->drawMesh(*fMeshes[i]);
1269 }
1270 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -07001271}
1272
Robert Phillips461c5392021-08-17 16:42:51 -04001273} // anonymous namespace
joshualitt40ded322015-05-02 07:07:17 -07001274
1275///////////////////////////////////////////////////////////////////////////////////////////////////
1276
Hal Canary6f6961e2017-01-31 13:50:44 -05001277#if GR_TEST_UTILS
joshualitt40ded322015-05-02 07:07:17 -07001278
Brian Salomona531f252017-07-07 13:29:28 -04001279GR_DRAW_OP_TEST_DEFINE(AAHairlineOp) {
joshualitt40ded322015-05-02 07:07:17 -07001280 SkMatrix viewMatrix = GrTest::TestMatrix(random);
John Stiles31954bf2020-08-07 17:35:54 -04001281 const SkPath& path = GrTest::TestPath(random);
joshualitt40ded322015-05-02 07:07:17 -07001282 SkIRect devClipBounds;
1283 devClipBounds.setEmpty();
Robert Phillips7c525e62018-06-12 10:11:12 -04001284 return AAHairlineOp::Make(context, std::move(paint), viewMatrix, path,
1285 GrStyle::SimpleHairline(), devClipBounds,
1286 GrGetRandomStencil(random, context));
joshualitt40ded322015-05-02 07:07:17 -07001287}
1288
1289#endif
Robert Phillips461c5392021-08-17 16:42:51 -04001290
1291///////////////////////////////////////////////////////////////////////////////////////////////////
1292
1293namespace skgpu::v1 {
1294
Robert Phillipsdb0ec082021-08-19 12:30:12 -04001295PathRenderer::CanDrawPath AAHairLinePathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Robert Phillips461c5392021-08-17 16:42:51 -04001296 if (GrAAType::kCoverage != args.fAAType) {
1297 return CanDrawPath::kNo;
1298 }
1299
1300 if (!GrIsStrokeHairlineOrEquivalent(args.fShape->style(), *args.fViewMatrix, nullptr)) {
1301 return CanDrawPath::kNo;
1302 }
1303
1304 // We don't currently handle dashing in this class though perhaps we should.
1305 if (args.fShape->style().pathEffect()) {
1306 return CanDrawPath::kNo;
1307 }
1308
1309 if (SkPath::kLine_SegmentMask == args.fShape->segmentMask() ||
1310 args.fCaps->shaderCaps()->shaderDerivativeSupport()) {
1311 return CanDrawPath::kYes;
1312 }
1313
1314 return CanDrawPath::kNo;
1315}
1316
1317
1318bool AAHairLinePathRenderer::onDrawPath(const DrawPathArgs& args) {
1319 GR_AUDIT_TRAIL_AUTO_FRAME(args.fContext->priv().auditTrail(),
1320 "AAHairlinePathRenderer::onDrawPath");
1321 SkASSERT(args.fSurfaceDrawContext->numSamples() <= 1);
1322
1323 SkPath path;
1324 args.fShape->asPath(&path);
1325 GrOp::Owner op =
1326 AAHairlineOp::Make(args.fContext, std::move(args.fPaint), *args.fViewMatrix, path,
1327 args.fShape->style(), *args.fClipConservativeBounds,
1328 args.fUserStencilSettings);
1329 args.fSurfaceDrawContext->addDrawOp(args.fClip, std::move(op));
1330 return true;
1331}
1332
1333} // namespace skgpu::v1
1334