blob: 884eaef535c0d6f43729288e9f8717ddfdc1262b [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 Phillips06273bc2021-08-11 15:43:50 -04008#include "src/gpu/ops/GrAAHairLinePathRenderer.h"
9
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
bsalomon@google.comaeb21602011-08-30 18:13:44 +000037// quadratics are rendered as 5-sided polys in order to bound the
38// AA stroke around the center-curve. See comments in push_quad_index_buffer and
egdaniel@google.com5383a752013-07-12 20:15:34 +000039// bloat_quad. Quadratics and conics share an index buffer
bsalomon@google.comaeb21602011-08-30 18:13:44 +000040
robertphillips@google.comada90da2013-09-18 22:14:49 +000041// lines are rendered as:
42// *______________*
43// |\ -_______ /|
44// | \ \ / |
45// | *--------* |
46// | / ______/ \ |
47// */_-__________\*
48// For: 6 vertices and 18 indices (for 6 triangles)
bsalomon@google.comaeb21602011-08-30 18:13:44 +000049
joshualitt5ead6da2014-10-22 16:00:29 -070050// Each quadratic is rendered as a five sided polygon. This poly bounds
51// the quadratic's bounding triangle but has been expanded so that the
52// 1-pixel wide area around the curve is inside the poly.
53// If a,b,c are the original control points then the poly a0,b0,c0,c1,a1
54// that is rendered would look like this:
55// b0
56// b
57//
58// a0 c0
59// a c
60// a1 c1
egdaniel14afb432014-12-22 10:57:08 -080061// Each is drawn as three triangles ((a0,a1,b0), (b0,c1,c0), (a1,c1,b0))
62// specified by these 9 indices:
joshualitt5ead6da2014-10-22 16:00:29 -070063static const uint16_t kQuadIdxBufPattern[] = {
64 0, 1, 2,
65 2, 4, 3,
66 1, 4, 2
67};
bsalomon@google.comaeb21602011-08-30 18:13:44 +000068
joshualitt5ead6da2014-10-22 16:00:29 -070069static const int kIdxsPerQuad = SK_ARRAY_COUNT(kQuadIdxBufPattern);
70static const int kQuadNumVertices = 5;
71static const int kQuadsNumInIdxBuffer = 256;
bsalomoned0bcad2015-05-04 10:36:42 -070072GR_DECLARE_STATIC_UNIQUE_KEY(gQuadsIndexBufferKey);
73
Brian Salomond28a79d2017-10-16 13:01:07 -040074static sk_sp<const GrBuffer> get_quads_index_buffer(GrResourceProvider* resourceProvider) {
bsalomoned0bcad2015-05-04 10:36:42 -070075 GR_DEFINE_STATIC_UNIQUE_KEY(gQuadsIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -040076 return resourceProvider->findOrCreatePatternedIndexBuffer(
bsalomoned0bcad2015-05-04 10:36:42 -070077 kQuadIdxBufPattern, kIdxsPerQuad, kQuadsNumInIdxBuffer, kQuadNumVertices,
78 gQuadsIndexBufferKey);
79}
jvanverth@google.com681ccf02013-08-16 14:51:51 +000080
bsalomon@google.comaeb21602011-08-30 18:13:44 +000081
joshualitt5ead6da2014-10-22 16:00:29 -070082// Each line segment is rendered as two quads and two triangles.
83// p0 and p1 have alpha = 1 while all other points have alpha = 0.
84// The four external points are offset 1 pixel perpendicular to the
85// line and half a pixel parallel to the line.
86//
87// p4 p5
88// p0 p1
89// p2 p3
90//
91// Each is drawn as six triangles specified by these 18 indices:
jvanverth@google.com681ccf02013-08-16 14:51:51 +000092
joshualitt5ead6da2014-10-22 16:00:29 -070093static const uint16_t kLineSegIdxBufPattern[] = {
94 0, 1, 3,
95 0, 3, 2,
96 0, 4, 5,
97 0, 5, 1,
98 0, 2, 4,
99 1, 5, 3
100};
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000101
joshualitt5ead6da2014-10-22 16:00:29 -0700102static const int kIdxsPerLineSeg = SK_ARRAY_COUNT(kLineSegIdxBufPattern);
103static const int kLineSegNumVertices = 6;
104static const int kLineSegsNumInIdxBuffer = 256;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000105
bsalomoned0bcad2015-05-04 10:36:42 -0700106GR_DECLARE_STATIC_UNIQUE_KEY(gLinesIndexBufferKey);
107
Brian Salomond28a79d2017-10-16 13:01:07 -0400108static sk_sp<const GrBuffer> get_lines_index_buffer(GrResourceProvider* resourceProvider) {
bsalomoned0bcad2015-05-04 10:36:42 -0700109 GR_DEFINE_STATIC_UNIQUE_KEY(gLinesIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400110 return resourceProvider->findOrCreatePatternedIndexBuffer(
bsalomoned0bcad2015-05-04 10:36:42 -0700111 kLineSegIdxBufPattern, kIdxsPerLineSeg, kLineSegsNumInIdxBuffer, kLineSegNumVertices,
112 gLinesIndexBufferKey);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000113}
114
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000115// Takes 178th time of logf on Z600 / VC2010
bsalomoned0bcad2015-05-04 10:36:42 -0700116static int get_float_exp(float x) {
Brian Salomon4dea72a2019-12-18 10:43:10 -0500117 static_assert(sizeof(int) == sizeof(float));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000118#ifdef SK_DEBUG
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000119 static bool tested;
120 if (!tested) {
121 tested = true;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000122 SkASSERT(get_float_exp(0.25f) == -2);
123 SkASSERT(get_float_exp(0.3f) == -2);
124 SkASSERT(get_float_exp(0.5f) == -1);
125 SkASSERT(get_float_exp(1.f) == 0);
126 SkASSERT(get_float_exp(2.f) == 1);
127 SkASSERT(get_float_exp(2.5f) == 1);
128 SkASSERT(get_float_exp(8.f) == 3);
129 SkASSERT(get_float_exp(100.f) == 6);
130 SkASSERT(get_float_exp(1000.f) == 9);
131 SkASSERT(get_float_exp(1024.f) == 10);
132 SkASSERT(get_float_exp(3000000.f) == 21);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000133 }
134#endif
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000135 const int* iptr = (const int*)&x;
136 return (((*iptr) & 0x7f800000) >> 23) - 127;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000137}
138
egdaniel@google.com5383a752013-07-12 20:15:34 +0000139// Uses the max curvature function for quads to estimate
140// where to chop the conic. If the max curvature is not
141// found along the curve segment it will return 1 and
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000142// dst[0] is the original conic. If it returns 2 the dst[0]
egdaniel@google.com5383a752013-07-12 20:15:34 +0000143// and dst[1] are the two new conics.
bsalomoned0bcad2015-05-04 10:36:42 -0700144static int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000145 SkScalar t = SkFindQuadMaxCurvature(src);
Chris Dalton1d474dd2018-07-24 01:08:31 -0600146 if (t == 0 || t == 1) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000147 if (dst) {
148 dst[0].set(src, weight);
149 }
150 return 1;
151 } else {
152 if (dst) {
153 SkConic conic;
154 conic.set(src, weight);
caryclark414c4292016-09-26 11:03:54 -0700155 if (!conic.chopAt(t, dst)) {
156 dst[0].set(src, weight);
157 return 1;
158 }
egdaniel@google.com5383a752013-07-12 20:15:34 +0000159 }
160 return 2;
161 }
162}
163
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000164// Calls split_conic on the entire conic and then once more on each subsection.
165// Most cases will result in either 1 conic (chop point is not within t range)
166// or 3 points (split once and then one subsection is split again).
bsalomoned0bcad2015-05-04 10:36:42 -0700167static int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000168 SkConic dstTemp[2];
169 int conicCnt = split_conic(src, dstTemp, weight);
170 if (2 == conicCnt) {
171 int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW);
172 conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW);
173 } else {
174 dst[0] = dstTemp[0];
175 }
176 return conicCnt;
177}
178
egdaniel@google.com5383a752013-07-12 20:15:34 +0000179// returns 0 if quad/conic is degen or close to it
180// in this case approx the path with lines
181// otherwise returns 1
bsalomoned0bcad2015-05-04 10:36:42 -0700182static int is_degen_quad_or_conic(const SkPoint p[3], SkScalar* dsqd) {
jvanverthcfeb85f2016-04-29 07:38:10 -0700183 static const SkScalar gDegenerateToLineTol = GrPathUtils::kDefaultTolerance;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000184 static const SkScalar gDegenerateToLineTolSqd =
Mike Reed8be952a2017-02-13 20:44:33 -0500185 gDegenerateToLineTol * gDegenerateToLineTol;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000186
Cary Clarkdf429f32017-11-08 11:44:31 -0500187 if (SkPointPriv::DistanceToSqd(p[0], p[1]) < gDegenerateToLineTolSqd ||
188 SkPointPriv::DistanceToSqd(p[1], p[2]) < gDegenerateToLineTolSqd) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000189 return 1;
190 }
191
Cary Clarkdf429f32017-11-08 11:44:31 -0500192 *dsqd = SkPointPriv::DistanceToLineBetweenSqd(p[1], p[0], p[2]);
joshualitt63648072015-02-19 10:25:21 -0800193 if (*dsqd < gDegenerateToLineTolSqd) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000194 return 1;
195 }
196
Cary Clarkdf429f32017-11-08 11:44:31 -0500197 if (SkPointPriv::DistanceToLineBetweenSqd(p[2], p[1], p[0]) < gDegenerateToLineTolSqd) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000198 return 1;
199 }
200 return 0;
201}
202
bsalomoned0bcad2015-05-04 10:36:42 -0700203static int is_degen_quad_or_conic(const SkPoint p[3]) {
joshualitt63648072015-02-19 10:25:21 -0800204 SkScalar dsqd;
205 return is_degen_quad_or_conic(p, &dsqd);
206}
207
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000208// we subdivide the quads to avoid huge overfill
209// if it returns -1 then should be drawn as lines
bsalomoned0bcad2015-05-04 10:36:42 -0700210static int num_quad_subdivs(const SkPoint p[3]) {
joshualitt63648072015-02-19 10:25:21 -0800211 SkScalar dsqd;
212 if (is_degen_quad_or_conic(p, &dsqd)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000213 return -1;
214 }
215
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000216 // tolerance of triangle height in pixels
217 // tuned on windows Quadro FX 380 / Z600
218 // trade off of fill vs cpu time on verts
219 // maybe different when do this using gpu (geo or tess shaders)
220 static const SkScalar gSubdivTol = 175 * SK_Scalar1;
221
Mike Reed8be952a2017-02-13 20:44:33 -0500222 if (dsqd <= gSubdivTol * gSubdivTol) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000223 return 0;
224 } else {
robertphillips@google.com87379e12013-03-29 12:11:10 +0000225 static const int kMaxSub = 4;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000226 // subdividing the quad reduces d by 4. so we want x = log4(d/tol)
227 // = log4(d*d/tol*tol)/2
228 // = log2(d*d/tol*tol)
229
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000230 // +1 since we're ignoring the mantissa contribution.
231 int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
Brian Osman788b9162020-02-07 10:36:46 -0500232 log = std::min(std::max(0, log), kMaxSub);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000233 return log;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000234 }
235}
236
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000237/**
238 * Generates the lines and quads to be rendered. Lines are always recorded in
239 * device space. We will do a device space bloat to account for the 1pixel
240 * thickness.
241 * Quads are recorded in device space unless m contains
242 * perspective, then in they are in src space. We do this because we will
243 * subdivide large quads to reduce over-fill. This subdivision has to be
244 * performed before applying the perspective matrix.
245 */
bsalomoned0bcad2015-05-04 10:36:42 -0700246static int gather_lines_and_quads(const SkPath& path,
247 const SkMatrix& m,
248 const SkIRect& devClipBounds,
Brian Osmancf3dc292017-06-28 16:45:32 -0400249 SkScalar capLength,
Brian Salomon969a7382018-05-09 13:28:44 -0400250 bool convertConicsToQuads,
bsalomoned0bcad2015-05-04 10:36:42 -0700251 GrAAHairLinePathRenderer::PtArray* lines,
252 GrAAHairLinePathRenderer::PtArray* quads,
253 GrAAHairLinePathRenderer::PtArray* conics,
254 GrAAHairLinePathRenderer::IntArray* quadSubdivCnts,
255 GrAAHairLinePathRenderer::FloatArray* conicWeights) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000256 SkPath::Iter iter(path, false);
257
258 int totalQuadCount = 0;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000259 SkRect bounds;
260 SkIRect ibounds;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000261
262 bool persp = m.hasPerspective();
263
Brian Osmancf3dc292017-06-28 16:45:32 -0400264 // Whenever a degenerate, zero-length contour is encountered, this code will insert a
265 // 'capLength' x-aligned line segment. Since this is rendering hairlines it is hoped this will
266 // suffice for AA square & circle capping.
267 int verbsInContour = 0; // Does not count moves
268 bool seenZeroLengthVerb = false;
269 SkPoint zeroVerbPt;
270
Brian Salomon969a7382018-05-09 13:28:44 -0400271 // Adds a quad that has already been chopped to the list and checks for quads that are close to
272 // lines. Also does a bounding box check. It takes points that are in src space and device
273 // space. The src points are only required if the view matrix has perspective.
274 auto addChoppedQuad = [&](const SkPoint srcPts[3], const SkPoint devPts[4],
275 bool isContourStart) {
276 SkRect bounds;
277 SkIRect ibounds;
278 bounds.setBounds(devPts, 3);
279 bounds.outset(SK_Scalar1, SK_Scalar1);
280 bounds.roundOut(&ibounds);
281 // We only need the src space space pts when not in perspective.
282 SkASSERT(srcPts || !persp);
283 if (SkIRect::Intersects(devClipBounds, ibounds)) {
284 int subdiv = num_quad_subdivs(devPts);
285 SkASSERT(subdiv >= -1);
286 if (-1 == subdiv) {
287 SkPoint* pts = lines->push_back_n(4);
288 pts[0] = devPts[0];
289 pts[1] = devPts[1];
290 pts[2] = devPts[1];
291 pts[3] = devPts[2];
292 if (isContourStart && pts[0] == pts[1] && pts[2] == pts[3]) {
293 seenZeroLengthVerb = true;
294 zeroVerbPt = pts[0];
295 }
296 } else {
297 // when in perspective keep quads in src space
298 const SkPoint* qPts = persp ? srcPts : devPts;
299 SkPoint* pts = quads->push_back_n(3);
300 pts[0] = qPts[0];
301 pts[1] = qPts[1];
302 pts[2] = qPts[2];
303 quadSubdivCnts->push_back() = subdiv;
304 totalQuadCount += 1 << subdiv;
305 }
306 }
307 };
308
309 // Applies the view matrix to quad src points and calls the above helper.
310 auto addSrcChoppedQuad = [&](const SkPoint srcSpaceQuadPts[3], bool isContourStart) {
311 SkPoint devPts[3];
312 m.mapPoints(devPts, srcSpaceQuadPts, 3);
313 addChoppedQuad(srcSpaceQuadPts, devPts, isContourStart);
314 };
315
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000316 for (;;) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000317 SkPoint pathPts[4];
Mike Reedba7e9a62019-08-16 13:30:34 -0400318 SkPath::Verb verb = iter.next(pathPts);
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000319 switch (verb) {
Brian Salomon969a7382018-05-09 13:28:44 -0400320 case SkPath::kConic_Verb:
321 if (convertConicsToQuads) {
322 SkScalar weight = iter.conicWeight();
323 SkAutoConicToQuads converter;
Brian Osmane3deee12018-11-20 11:10:15 -0500324 const SkPoint* quadPts = converter.computeQuads(pathPts, weight, 0.25f);
Brian Salomon969a7382018-05-09 13:28:44 -0400325 for (int i = 0; i < converter.countQuads(); ++i) {
326 addSrcChoppedQuad(quadPts + 2 * i, !verbsInContour && 0 == i);
327 }
328 } else {
329 SkConic dst[4];
330 // We chop the conics to create tighter clipping to hide error
331 // that appears near max curvature of very thin conics. Thin
332 // hyperbolas with high weight still show error.
333 int conicCnt = chop_conic(pathPts, dst, iter.conicWeight());
334 for (int i = 0; i < conicCnt; ++i) {
335 SkPoint devPts[4];
336 SkPoint* chopPnts = dst[i].fPts;
337 m.mapPoints(devPts, chopPnts, 3);
338 bounds.setBounds(devPts, 3);
339 bounds.outset(SK_Scalar1, SK_Scalar1);
340 bounds.roundOut(&ibounds);
341 if (SkIRect::Intersects(devClipBounds, ibounds)) {
342 if (is_degen_quad_or_conic(devPts)) {
343 SkPoint* pts = lines->push_back_n(4);
344 pts[0] = devPts[0];
345 pts[1] = devPts[1];
346 pts[2] = devPts[1];
347 pts[3] = devPts[2];
348 if (verbsInContour == 0 && i == 0 && pts[0] == pts[1] &&
349 pts[2] == pts[3]) {
350 seenZeroLengthVerb = true;
351 zeroVerbPt = pts[0];
352 }
353 } else {
354 // when in perspective keep conics in src space
355 SkPoint* cPts = persp ? chopPnts : devPts;
356 SkPoint* pts = conics->push_back_n(3);
357 pts[0] = cPts[0];
358 pts[1] = cPts[1];
359 pts[2] = cPts[2];
360 conicWeights->push_back() = dst[i].fW;
Brian Osmancf3dc292017-06-28 16:45:32 -0400361 }
egdaniel@google.com5383a752013-07-12 20:15:34 +0000362 }
363 }
364 }
Brian Osmancf3dc292017-06-28 16:45:32 -0400365 verbsInContour++;
reed@google.com277c3f82013-05-31 15:17:50 +0000366 break;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000367 case SkPath::kMove_Verb:
Brian Osmancf3dc292017-06-28 16:45:32 -0400368 // New contour (and last one was unclosed). If it was just a zero length drawing
369 // operation, and we're supposed to draw caps, then add a tiny line.
370 if (seenZeroLengthVerb && verbsInContour == 1 && capLength > 0) {
371 SkPoint* pts = lines->push_back_n(2);
372 pts[0] = SkPoint::Make(zeroVerbPt.fX - capLength, zeroVerbPt.fY);
373 pts[1] = SkPoint::Make(zeroVerbPt.fX + capLength, zeroVerbPt.fY);
374 }
375 verbsInContour = 0;
376 seenZeroLengthVerb = false;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000377 break;
Brian Salomon969a7382018-05-09 13:28:44 -0400378 case SkPath::kLine_Verb: {
379 SkPoint devPts[2];
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000380 m.mapPoints(devPts, pathPts, 2);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000381 bounds.setBounds(devPts, 2);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000382 bounds.outset(SK_Scalar1, SK_Scalar1);
383 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000384 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000385 SkPoint* pts = lines->push_back_n(2);
386 pts[0] = devPts[0];
387 pts[1] = devPts[1];
Brian Osmancf3dc292017-06-28 16:45:32 -0400388 if (verbsInContour == 0 && pts[0] == pts[1]) {
389 seenZeroLengthVerb = true;
390 zeroVerbPt = pts[0];
391 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000392 }
Brian Osmancf3dc292017-06-28 16:45:32 -0400393 verbsInContour++;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000394 break;
Brian Salomon969a7382018-05-09 13:28:44 -0400395 }
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000396 case SkPath::kQuad_Verb: {
397 SkPoint choppedPts[5];
398 // Chopping the quad helps when the quad is either degenerate or nearly degenerate.
399 // When it is degenerate it allows the approximation with lines to work since the
400 // chop point (if there is one) will be at the parabola's vertex. In the nearly
401 // degenerate the QuadUVMatrix computed for the points is almost singular which
402 // can cause rendering artifacts.
403 int n = SkChopQuadAtMaxCurvature(pathPts, choppedPts);
404 for (int i = 0; i < n; ++i) {
Brian Salomon969a7382018-05-09 13:28:44 -0400405 addSrcChoppedQuad(choppedPts + i * 2, !verbsInContour && 0 == i);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000406 }
Brian Osmancf3dc292017-06-28 16:45:32 -0400407 verbsInContour++;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000408 break;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000409 }
Brian Salomon969a7382018-05-09 13:28:44 -0400410 case SkPath::kCubic_Verb: {
411 SkPoint devPts[4];
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000412 m.mapPoints(devPts, pathPts, 4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000413 bounds.setBounds(devPts, 4);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000414 bounds.outset(SK_Scalar1, SK_Scalar1);
415 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000416 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000417 PREALLOC_PTARRAY(32) q;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000418 // We convert cubics to quadratics (for now).
419 // In perspective have to do conversion in src space.
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000420 if (persp) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000421 SkScalar tolScale =
bsalomon18fab302016-02-16 08:00:05 -0800422 GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m, path.getBounds());
423 GrPathUtils::convertCubicToQuads(pathPts, tolScale, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000424 } else {
bsalomon18fab302016-02-16 08:00:05 -0800425 GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000426 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000427 for (int i = 0; i < q.count(); i += 3) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000428 if (persp) {
Brian Salomon969a7382018-05-09 13:28:44 -0400429 addSrcChoppedQuad(&q[i], !verbsInContour && 0 == i);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000430 } else {
Brian Salomon969a7382018-05-09 13:28:44 -0400431 addChoppedQuad(nullptr, &q[i], !verbsInContour && 0 == i);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000432 }
433 }
434 }
Brian Osmancf3dc292017-06-28 16:45:32 -0400435 verbsInContour++;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000436 break;
Brian Salomon969a7382018-05-09 13:28:44 -0400437 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000438 case SkPath::kClose_Verb:
Brian Osmancf3dc292017-06-28 16:45:32 -0400439 // Contour is closed, so we don't need to grow the starting line, unless it's
440 // *just* a zero length subpath. (SVG Spec 11.4, 'stroke').
441 if (capLength > 0) {
442 if (seenZeroLengthVerb && verbsInContour == 1) {
443 SkPoint* pts = lines->push_back_n(2);
444 pts[0] = SkPoint::Make(zeroVerbPt.fX - capLength, zeroVerbPt.fY);
445 pts[1] = SkPoint::Make(zeroVerbPt.fX + capLength, zeroVerbPt.fY);
446 } else if (verbsInContour == 0) {
447 // Contour was (moveTo, close). Add a line.
Brian Salomon969a7382018-05-09 13:28:44 -0400448 SkPoint devPts[2];
Brian Osmancf3dc292017-06-28 16:45:32 -0400449 m.mapPoints(devPts, pathPts, 1);
450 devPts[1] = devPts[0];
451 bounds.setBounds(devPts, 2);
452 bounds.outset(SK_Scalar1, SK_Scalar1);
453 bounds.roundOut(&ibounds);
454 if (SkIRect::Intersects(devClipBounds, ibounds)) {
455 SkPoint* pts = lines->push_back_n(2);
456 pts[0] = SkPoint::Make(devPts[0].fX - capLength, devPts[0].fY);
457 pts[1] = SkPoint::Make(devPts[1].fX + capLength, devPts[1].fY);
458 }
459 }
460 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000461 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000462 case SkPath::kDone_Verb:
Brian Osmancf3dc292017-06-28 16:45:32 -0400463 if (seenZeroLengthVerb && verbsInContour == 1 && capLength > 0) {
464 // Path ended with a dangling (moveTo, line|quad|etc). If the final verb is
465 // degenerate, we need to draw a line.
466 SkPoint* pts = lines->push_back_n(2);
467 pts[0] = SkPoint::Make(zeroVerbPt.fX - capLength, zeroVerbPt.fY);
468 pts[1] = SkPoint::Make(zeroVerbPt.fX + capLength, zeroVerbPt.fY);
469 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000470 return totalQuadCount;
471 }
472 }
473}
474
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000475struct LineVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000476 SkPoint fPos;
egdaniele27065a2014-11-06 08:00:48 -0800477 float fCoverage;
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000478};
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000479
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000480struct BezierVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000481 SkPoint fPos;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000482 union {
483 struct {
csmartdaltoncc261272017-03-23 13:38:45 -0600484 SkScalar fKLM[3];
egdaniel@google.com5383a752013-07-12 20:15:34 +0000485 } fConic;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000486 SkVector fQuadCoord;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000487 struct {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000488 SkScalar fBogus[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000489 };
490 };
491};
egdaniel@google.com5383a752013-07-12 20:15:34 +0000492
Brian Salomon4dea72a2019-12-18 10:43:10 -0500493static_assert(sizeof(BezierVertex) == 3 * sizeof(SkPoint));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000494
bsalomoned0bcad2015-05-04 10:36:42 -0700495static void intersect_lines(const SkPoint& ptA, const SkVector& normA,
496 const SkPoint& ptB, const SkVector& normB,
497 SkPoint* result) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000498
499 SkScalar lineAW = -normA.dot(ptA);
500 SkScalar lineBW = -normB.dot(ptB);
501
Mike Reed8be952a2017-02-13 20:44:33 -0500502 SkScalar wInv = normA.fX * normB.fY - normA.fY * normB.fX;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000503 wInv = SkScalarInvert(wInv);
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400504 if (!SkScalarIsFinite(wInv)) {
505 // lines are parallel, pick the point in between
506 *result = (ptA + ptB)*SK_ScalarHalf;
507 *result += normA;
508 } else {
509 result->fX = normA.fY * lineBW - lineAW * normB.fY;
510 result->fX *= wInv;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000511
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400512 result->fY = lineAW * normB.fX - normA.fX * lineBW;
513 result->fY *= wInv;
514 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000515}
516
bsalomoned0bcad2015-05-04 10:36:42 -0700517static void set_uv_quad(const SkPoint qpts[3], BezierVertex verts[kQuadNumVertices]) {
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000518 // this should be in the src space, not dev coords, when we have perspective
519 GrPathUtils::QuadUVMatrix DevToUV(qpts);
Brian Osman568bec72018-12-26 16:48:25 -0500520 DevToUV.apply(verts, kQuadNumVertices, sizeof(BezierVertex), sizeof(SkPoint));
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000521}
522
bsalomoned0bcad2015-05-04 10:36:42 -0700523static void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice,
524 const SkMatrix* toSrc, BezierVertex verts[kQuadNumVertices]) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000525 SkASSERT(!toDevice == !toSrc);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000526 // original quad is specified by tri a,b,c
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000527 SkPoint a = qpts[0];
528 SkPoint b = qpts[1];
529 SkPoint c = qpts[2];
530
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000531 if (toDevice) {
532 toDevice->mapPoints(&a, 1);
533 toDevice->mapPoints(&b, 1);
534 toDevice->mapPoints(&c, 1);
535 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000536 // make a new poly where we replace a and c by a 1-pixel wide edges orthog
537 // to edges ab and bc:
538 //
539 // before | after
540 // | b0
541 // b |
542 // |
543 // | a0 c0
544 // a c | a1 c1
545 //
546 // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c,
547 // respectively.
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000548 BezierVertex& a0 = verts[0];
549 BezierVertex& a1 = verts[1];
550 BezierVertex& b0 = verts[2];
551 BezierVertex& c0 = verts[3];
552 BezierVertex& c1 = verts[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000553
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000554 SkVector ab = b;
555 ab -= a;
556 SkVector ac = c;
557 ac -= a;
558 SkVector cb = b;
559 cb -= c;
560
Robert Phillips2c2303e2021-02-04 11:04:01 -0500561 // After the transform (or due to floating point math) we might have a line,
562 // try to do something reasonable
563 if (SkPointPriv::LengthSqd(ab) <= SK_ScalarNearlyZero*SK_ScalarNearlyZero) {
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400564 ab = cb;
565 }
Robert Phillips2c2303e2021-02-04 11:04:01 -0500566 if (SkPointPriv::LengthSqd(cb) <= SK_ScalarNearlyZero*SK_ScalarNearlyZero) {
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400567 cb = ab;
568 }
569
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000570 // We should have already handled degenerates
Robert Phillips2c2303e2021-02-04 11:04:01 -0500571 SkASSERT(ab.length() > 0 && cb.length() > 0);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000572
573 ab.normalize();
Brian Salomon0235c642018-08-31 12:04:18 -0400574 SkVector abN = SkPointPriv::MakeOrthog(ab, SkPointPriv::kLeft_Side);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000575 if (abN.dot(ac) > 0) {
576 abN.negate();
577 }
578
579 cb.normalize();
Brian Salomon0235c642018-08-31 12:04:18 -0400580 SkVector cbN = SkPointPriv::MakeOrthog(cb, SkPointPriv::kLeft_Side);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000581 if (cbN.dot(ac) < 0) {
582 cbN.negate();
583 }
584
585 a0.fPos = a;
586 a0.fPos += abN;
587 a1.fPos = a;
588 a1.fPos -= abN;
589
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400590 if (toDevice && SkPointPriv::LengthSqd(ac) <= SK_ScalarNearlyZero*SK_ScalarNearlyZero) {
591 c = b;
592 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000593 c0.fPos = c;
594 c0.fPos += cbN;
595 c1.fPos = c;
596 c1.fPos -= cbN;
597
598 intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos);
599
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000600 if (toSrc) {
Brian Salomonfa3783f2018-01-05 13:49:07 -0500601 SkMatrixPriv::MapPointsWithStride(*toSrc, &verts[0].fPos, sizeof(BezierVertex),
602 kQuadNumVertices);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000603 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000604}
605
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000606// Equations based off of Loop-Blinn Quadratic GPU Rendering
egdaniel@google.com5383a752013-07-12 20:15:34 +0000607// Input Parametric:
608// 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)
609// Output Implicit:
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000610// f(x, y, w) = f(P) = K^2 - LM
611// K = dot(k, P), L = dot(l, P), M = dot(m, P)
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000612// k, l, m are calculated in function GrPathUtils::getConicKLM
bsalomoned0bcad2015-05-04 10:36:42 -0700613static void set_conic_coeffs(const SkPoint p[3], BezierVertex verts[kQuadNumVertices],
614 const SkScalar weight) {
csmartdaltoncc261272017-03-23 13:38:45 -0600615 SkMatrix klm;
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000616
csmartdaltoncc261272017-03-23 13:38:45 -0600617 GrPathUtils::getConicKLM(p, weight, &klm);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000618
joshualitt5ead6da2014-10-22 16:00:29 -0700619 for (int i = 0; i < kQuadNumVertices; ++i) {
Cary Clarke4442cb2017-10-18 11:46:18 -0400620 const SkPoint3 pt3 = {verts[i].fPos.x(), verts[i].fPos.y(), 1.f};
621 klm.mapHomogeneousPoints((SkPoint3* ) verts[i].fConic.fKLM, &pt3, 1);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000622 }
623}
624
bsalomoned0bcad2015-05-04 10:36:42 -0700625static void add_conics(const SkPoint p[3],
626 const SkScalar weight,
627 const SkMatrix* toDevice,
628 const SkMatrix* toSrc,
629 BezierVertex** vert) {
joshualitt7bc18b72015-02-03 16:41:41 -0800630 bloat_quad(p, toDevice, toSrc, *vert);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000631 set_conic_coeffs(p, *vert, weight);
joshualitt5ead6da2014-10-22 16:00:29 -0700632 *vert += kQuadNumVertices;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000633}
634
bsalomoned0bcad2015-05-04 10:36:42 -0700635static void add_quads(const SkPoint p[3],
636 int subdiv,
637 const SkMatrix* toDevice,
638 const SkMatrix* toSrc,
639 BezierVertex** vert) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000640 SkASSERT(subdiv >= 0);
Jim Van Verth14982c82020-07-15 15:42:19 -0400641 // temporary vertex storage to avoid reading the vertex buffer
Jim Van Verth3557ab42020-07-28 09:46:03 -0400642 BezierVertex outVerts[kQuadNumVertices] = {};
Jim Van Verth14982c82020-07-15 15:42:19 -0400643
644 // storage for the chopped quad
645 // pts 0,1,2 are the first quad, and 2,3,4 the second quad
646 SkPoint choppedQuadPts[5];
647 // start off with our original curve in the second quad slot
648 memcpy(&choppedQuadPts[2], p, 3*sizeof(SkPoint));
649
650 int stepCount = 1 << subdiv;
651 while (stepCount > 1) {
652 // The general idea is:
653 // * chop the quad using pts 2,3,4 as the input
654 // * write out verts using pts 0,1,2
655 // * now 2,3,4 is the remainder of the curve, chop again until all subdivisions are done
656 SkScalar h = 1.f / stepCount;
657 SkChopQuadAt(&choppedQuadPts[2], choppedQuadPts, h);
658
659 bloat_quad(choppedQuadPts, toDevice, toSrc, outVerts);
660 set_uv_quad(choppedQuadPts, outVerts);
661 memcpy(*vert, outVerts, kQuadNumVertices*sizeof(BezierVertex));
joshualitt5ead6da2014-10-22 16:00:29 -0700662 *vert += kQuadNumVertices;
Jim Van Verth14982c82020-07-15 15:42:19 -0400663 --stepCount;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000664 }
Jim Van Verth14982c82020-07-15 15:42:19 -0400665
666 // finish up, write out the final quad
667 bloat_quad(&choppedQuadPts[2], toDevice, toSrc, outVerts);
668 set_uv_quad(&choppedQuadPts[2], outVerts);
669 memcpy(*vert, outVerts, kQuadNumVertices * sizeof(BezierVertex));
670 *vert += kQuadNumVertices;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000671}
672
bsalomoned0bcad2015-05-04 10:36:42 -0700673static void add_line(const SkPoint p[2],
674 const SkMatrix* toSrc,
675 uint8_t coverage,
676 LineVertex** vert) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000677 const SkPoint& a = p[0];
678 const SkPoint& b = p[1];
679
robertphillips@google.comada90da2013-09-18 22:14:49 +0000680 SkVector ortho, vec = b;
681 vec -= a;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000682
Cary Clarkdf429f32017-11-08 11:44:31 -0500683 SkScalar lengthSqd = SkPointPriv::LengthSqd(vec);
Greg Daniel2e67dea2017-10-12 15:14:41 -0400684
robertphillips@google.comada90da2013-09-18 22:14:49 +0000685 if (vec.setLength(SK_ScalarHalf)) {
686 // Create a vector orthogonal to 'vec' and of unit length
687 ortho.fX = 2.0f * vec.fY;
688 ortho.fY = -2.0f * vec.fX;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000689
egdaniele27065a2014-11-06 08:00:48 -0800690 float floatCoverage = GrNormalizeByteToFloat(coverage);
691
Greg Daniel2e67dea2017-10-12 15:14:41 -0400692 if (lengthSqd >= 1.0f) {
693 // Relative to points a and b:
694 // The inner vertices are inset half a pixel along the line a,b
695 (*vert)[0].fPos = a + vec;
696 (*vert)[0].fCoverage = floatCoverage;
697 (*vert)[1].fPos = b - vec;
698 (*vert)[1].fCoverage = floatCoverage;
699 } else {
700 // The inner vertices are inset a distance of length(a,b) from the outer edge of
701 // geometry. For the "a" inset this is the same as insetting from b by half a pixel.
702 // The coverage is then modulated by the length. This gives us the correct
703 // coverage for rects shorter than a pixel as they get translated subpixel amounts
704 // inside of a pixel.
705 SkScalar length = SkScalarSqrt(lengthSqd);
706 (*vert)[0].fPos = b - vec;
707 (*vert)[0].fCoverage = floatCoverage * length;
708 (*vert)[1].fPos = a + vec;
709 (*vert)[1].fCoverage = floatCoverage * length;
710 }
711 // Relative to points a and b:
712 // The outer vertices are outset half a pixel along the line a,b and then a whole pixel
713 // orthogonally.
robertphillips@google.comada90da2013-09-18 22:14:49 +0000714 (*vert)[2].fPos = a - vec + ortho;
715 (*vert)[2].fCoverage = 0;
716 (*vert)[3].fPos = b + vec + ortho;
717 (*vert)[3].fCoverage = 0;
718 (*vert)[4].fPos = a - vec - ortho;
719 (*vert)[4].fCoverage = 0;
720 (*vert)[5].fPos = b + vec - ortho;
721 (*vert)[5].fCoverage = 0;
722
bsalomon49f085d2014-09-05 13:34:00 -0700723 if (toSrc) {
Brian Salomonfa3783f2018-01-05 13:49:07 -0500724 SkMatrixPriv::MapPointsWithStride(*toSrc, &(*vert)->fPos, sizeof(LineVertex),
725 kLineSegNumVertices);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000726 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000727 } else {
728 // just make it degenerate and likely offscreen
joshualitt5ead6da2014-10-22 16:00:29 -0700729 for (int i = 0; i < kLineSegNumVertices; ++i) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000730 (*vert)[i].fPos.set(SK_ScalarMax, SK_ScalarMax);
731 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000732 }
733
joshualitt5ead6da2014-10-22 16:00:29 -0700734 *vert += kLineSegNumVertices;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000735}
736
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000737///////////////////////////////////////////////////////////////////////////////
738
Chris Dalton5ed44232017-09-07 13:22:46 -0600739GrPathRenderer::CanDrawPath
740GrAAHairLinePathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600741 if (GrAAType::kCoverage != args.fAAType) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600742 return CanDrawPath::kNo;
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000743 }
744
Robert Phillips62214f72021-06-15 10:12:51 -0400745 if (!GrIsStrokeHairlineOrEquivalent(args.fShape->style(), *args.fViewMatrix, nullptr)) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600746 return CanDrawPath::kNo;
bsalomon6663acf2016-05-10 09:14:17 -0700747 }
748
749 // We don't currently handle dashing in this class though perhaps we should.
bsalomon8acedde2016-06-24 10:42:16 -0700750 if (args.fShape->style().pathEffect()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600751 return CanDrawPath::kNo;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000752 }
753
bsalomon8acedde2016-06-24 10:42:16 -0700754 if (SkPath::kLine_SegmentMask == args.fShape->segmentMask() ||
Eric Karl5c779752017-05-08 12:02:07 -0700755 args.fCaps->shaderCaps()->shaderDerivativeSupport()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600756 return CanDrawPath::kYes;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000757 }
bsalomon8acedde2016-06-24 10:42:16 -0700758
Chris Dalton5ed44232017-09-07 13:22:46 -0600759 return CanDrawPath::kNo;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000760}
761
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000762template <class VertexType>
joshualitt8059eb92014-12-29 15:10:07 -0800763bool check_bounds(const SkMatrix& viewMatrix, const SkRect& devBounds, void* vertices, int vCount)
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000764{
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000765 SkRect tolDevBounds = devBounds;
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000766 // The bounds ought to be tight, but in perspective the below code runs the verts
767 // through the view matrix to get back to dev coords, which can introduce imprecision.
joshualitt8059eb92014-12-29 15:10:07 -0800768 if (viewMatrix.hasPerspective()) {
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000769 tolDevBounds.outset(SK_Scalar1 / 1000, SK_Scalar1 / 1000);
770 } else {
771 // Non-persp matrices cause this path renderer to draw in device space.
joshualitt8059eb92014-12-29 15:10:07 -0800772 SkASSERT(viewMatrix.isIdentity());
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000773 }
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000774 SkRect actualBounds;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000775
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000776 VertexType* verts = reinterpret_cast<VertexType*>(vertices);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000777 bool first = true;
778 for (int i = 0; i < vCount; ++i) {
779 SkPoint pos = verts[i].fPos;
780 // This is a hack to workaround the fact that we move some degenerate segments offscreen.
781 if (SK_ScalarMax == pos.fX) {
782 continue;
783 }
joshualitt8059eb92014-12-29 15:10:07 -0800784 viewMatrix.mapPoints(&pos, 1);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000785 if (first) {
Mike Reed92b33352019-08-24 19:39:13 -0400786 actualBounds.setLTRB(pos.fX, pos.fY, pos.fX, pos.fY);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000787 first = false;
788 } else {
Mike Reed185ffe92018-01-08 17:09:54 -0500789 SkRectPriv::GrowToInclude(&actualBounds, pos);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000790 }
791 }
792 if (!first) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000793 return tolDevBounds.contains(actualBounds);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000794 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000795
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000796 return true;
797}
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000798
Brian Salomona531f252017-07-07 13:29:28 -0400799class AAHairlineOp final : public GrMeshDrawOp {
800private:
801 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
802
joshualitt7bc18b72015-02-03 16:41:41 -0800803public:
Brian Salomon25a88092016-12-01 09:36:50 -0500804 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700805
Herb Derbyc76d4092020-10-07 16:46:15 -0400806 static GrOp::Owner Make(GrRecordingContext* context,
807 GrPaint&& paint,
808 const SkMatrix& viewMatrix,
809 const SkPath& path,
810 const GrStyle& style,
811 const SkIRect& devClipBounds,
812 const GrUserStencilSettings* stencilSettings) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500813 SkScalar hairlineCoverage;
814 uint8_t newCoverage = 0xff;
Robert Phillips62214f72021-06-15 10:12:51 -0400815 if (GrIsStrokeHairlineOrEquivalent(style, viewMatrix, &hairlineCoverage)) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500816 newCoverage = SkScalarRoundToInt(hairlineCoverage * 0xff);
817 }
joshualitt7bc18b72015-02-03 16:41:41 -0800818
Brian Osmancf3dc292017-06-28 16:45:32 -0400819 const SkStrokeRec& stroke = style.strokeRec();
820 SkScalar capLength = SkPaint::kButt_Cap != stroke.getCap() ? hairlineCoverage * 0.5f : 0.0f;
821
Robert Phillips7c525e62018-06-12 10:11:12 -0400822 return Helper::FactoryHelper<AAHairlineOp>(context, std::move(paint), newCoverage,
823 viewMatrix, path,
Brian Salomona531f252017-07-07 13:29:28 -0400824 devClipBounds, capLength, stencilSettings);
825 }
826
Herb Derbyc76d4092020-10-07 16:46:15 -0400827 AAHairlineOp(GrProcessorSet* processorSet,
Brian Osmancf860852018-10-31 14:04:39 -0400828 const SkPMColor4f& color,
Brian Salomona531f252017-07-07 13:29:28 -0400829 uint8_t coverage,
830 const SkMatrix& viewMatrix,
831 const SkPath& path,
832 SkIRect devClipBounds,
833 SkScalar capLength,
834 const GrUserStencilSettings* stencilSettings)
835 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -0400836 , fHelper(processorSet, GrAAType::kCoverage, stencilSettings)
Brian Salomona531f252017-07-07 13:29:28 -0400837 , fColor(color)
838 , fCoverage(coverage) {
839 fPaths.emplace_back(PathData{viewMatrix, path, devClipBounds, capLength});
840
841 this->setTransformedBounds(path.getBounds(), viewMatrix, HasAABloat::kYes,
Greg Daniel5faf4742019-10-01 15:14:44 -0400842 IsHairline::kYes);
bsalomonf1703092016-06-29 18:41:53 -0700843 }
joshualitt7bc18b72015-02-03 16:41:41 -0800844
Brian Salomond0a0a652016-12-15 15:25:22 -0500845 const char* name() const override { return "AAHairlineOp"; }
joshualitt7bc18b72015-02-03 16:41:41 -0800846
Robert Phillips294723d2021-06-17 09:23:58 -0400847 void visitProxies(const GrVisitProxyFunc& func) const override {
Robert Phillips4f93c572020-03-18 08:13:53 -0400848
849 bool visited = false;
850 for (int i = 0; i < 3; ++i) {
851 if (fProgramInfos[i]) {
852 fProgramInfos[i]->visitFPProxies(func);
853 visited = true;
854 }
855 }
856
857 if (!visited) {
858 fHelper.visitProxies(func);
859 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400860 }
861
Brian Salomona531f252017-07-07 13:29:28 -0400862 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
863
Chris Dalton57ab06c2021-04-22 12:57:28 -0600864 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
865 GrClampType clampType) override {
Brian Osman8fa7ab42019-03-18 10:22:42 -0400866 // This Op uses uniform (not vertex) color, so doesn't need to track wide color.
Chris Dalton57ab06c2021-04-22 12:57:28 -0600867 return fHelper.finalizeProcessors(caps, clip, clampType,
Brian Osman8fa7ab42019-03-18 10:22:42 -0400868 GrProcessorAnalysisCoverage::kSingleChannel, &fColor,
869 nullptr);
Brian Salomona531f252017-07-07 13:29:28 -0400870 }
871
Robert Phillips4f93c572020-03-18 08:13:53 -0400872 enum Program : uint8_t {
873 kNone_Program = 0x0,
874 kLine_Program = 0x1,
875 kQuad_Program = 0x2,
876 kConic_Program = 0x4,
877 };
878
bsalomone46f9fe2015-08-18 06:05:14 -0700879private:
Robert Phillips4f93c572020-03-18 08:13:53 -0400880 void makeLineProgramInfo(const GrCaps&, SkArenaAlloc*, const GrPipeline*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500881 const GrSurfaceProxyView& writeView,
Robert Phillips4f93c572020-03-18 08:13:53 -0400882 const SkMatrix* geometryProcessorViewM,
Greg Danield358cbe2020-09-11 09:33:54 -0400883 const SkMatrix* geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -0500884 GrXferBarrierFlags renderPassXferBarriers,
885 GrLoadOp colorLoadOp);
Robert Phillips4f93c572020-03-18 08:13:53 -0400886 void makeQuadProgramInfo(const GrCaps&, SkArenaAlloc*, const GrPipeline*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500887 const GrSurfaceProxyView& writeView,
Robert Phillips4f93c572020-03-18 08:13:53 -0400888 const SkMatrix* geometryProcessorViewM,
Greg Danield358cbe2020-09-11 09:33:54 -0400889 const SkMatrix* geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -0500890 GrXferBarrierFlags renderPassXferBarriers,
891 GrLoadOp colorLoadOp);
Robert Phillips4f93c572020-03-18 08:13:53 -0400892 void makeConicProgramInfo(const GrCaps&, SkArenaAlloc*, const GrPipeline*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500893 const GrSurfaceProxyView& writeView,
Robert Phillips4f93c572020-03-18 08:13:53 -0400894 const SkMatrix* geometryProcessorViewM,
Greg Danield358cbe2020-09-11 09:33:54 -0400895 const SkMatrix* geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -0500896 GrXferBarrierFlags renderPassXferBarriers,
897 GrLoadOp colorLoadOp);
Robert Phillips1b3c2672019-12-04 14:34:48 -0500898
Robert Phillips2669a7b2020-03-12 12:07:19 -0400899 GrProgramInfo* programInfo() override {
900 // This Op has 3 programInfos and implements its own onPrePrepareDraws so this entry point
901 // should really never be called.
902 SkASSERT(0);
903 return nullptr;
904 }
905
Robert Phillips4f93c572020-03-18 08:13:53 -0400906 Program predictPrograms(const GrCaps*) const;
907
Robert Phillips4133dc42020-03-11 15:55:55 -0400908 void onCreateProgramInfo(const GrCaps*,
909 SkArenaAlloc*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500910 const GrSurfaceProxyView& writeView,
Chris Dalton6aaf00f2021-07-13 13:26:39 -0600911 bool usesMSAASurface,
Robert Phillips4133dc42020-03-11 15:55:55 -0400912 GrAppliedClip&&,
John Stiles52cb1d02021-06-02 11:58:05 -0400913 const GrDstProxyView&,
Greg Daniel42dbca52020-11-20 10:22:43 -0500914 GrXferBarrierFlags renderPassXferBarriers,
915 GrLoadOp colorLoadOp) override;
Robert Phillips4133dc42020-03-11 15:55:55 -0400916
Robert Phillips2669a7b2020-03-12 12:07:19 -0400917 void onPrePrepareDraws(GrRecordingContext*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500918 const GrSurfaceProxyView& writeView,
Robert Phillips2669a7b2020-03-12 12:07:19 -0400919 GrAppliedClip*,
John Stiles52cb1d02021-06-02 11:58:05 -0400920 const GrDstProxyView&,
Greg Daniel42dbca52020-11-20 10:22:43 -0500921 GrXferBarrierFlags renderPassXferBarriers,
922 GrLoadOp colorLoadOp) override;
Robert Phillips2669a7b2020-03-12 12:07:19 -0400923
Robert Phillips71143952021-06-17 14:55:07 -0400924 void onPrepareDraws(GrMeshDrawTarget*) override;
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700925 void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
bsalomon75398562015-08-17 12:55:38 -0700926
joshualitt7bc18b72015-02-03 16:41:41 -0800927 typedef SkTArray<SkPoint, true> PtArray;
928 typedef SkTArray<int, true> IntArray;
929 typedef SkTArray<float, true> FloatArray;
930
Herb Derbye25c3002020-10-27 15:57:27 -0400931 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500932 AAHairlineOp* that = t->cast<AAHairlineOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700933
Brian Salomona531f252017-07-07 13:29:28 -0400934 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000935 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700936 }
937
joshualitt7bc18b72015-02-03 16:41:41 -0800938 if (this->viewMatrix().hasPerspective() != that->viewMatrix().hasPerspective()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000939 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800940 }
941
942 // We go to identity if we don't have perspective
943 if (this->viewMatrix().hasPerspective() &&
Mike Reed2c383152019-12-18 16:47:47 -0500944 !SkMatrixPriv::CheapEqual(this->viewMatrix(), that->viewMatrix())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000945 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800946 }
947
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500948 // TODO we can actually combine hairlines if they are the same color in a kind of bulk
949 // method but we haven't implemented this yet
joshualitt7bc18b72015-02-03 16:41:41 -0800950 // TODO investigate going to vertex color and coverage?
951 if (this->coverage() != that->coverage()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000952 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800953 }
954
955 if (this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000956 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800957 }
958
Mike Reed2c383152019-12-18 16:47:47 -0500959 if (fHelper.usesLocalCoords() && !SkMatrixPriv::CheapEqual(this->viewMatrix(),
960 that->viewMatrix())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000961 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800962 }
963
Brian Salomond0a0a652016-12-15 15:25:22 -0500964 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000965 return CombineResult::kMerged;
joshualitt7bc18b72015-02-03 16:41:41 -0800966 }
967
John Stilesaf366522020-08-13 09:57:34 -0400968#if GR_TEST_UTILS
969 SkString onDumpInfo() const override {
970 return SkStringPrintf("Color: 0x%08x Coverage: 0x%02x, Count: %d\n%s",
971 fColor.toBytes_RGBA(), fCoverage, fPaths.count(),
972 fHelper.dumpInfo().c_str());
973 }
974#endif
975
Brian Osmancf860852018-10-31 14:04:39 -0400976 const SkPMColor4f& color() const { return fColor; }
Brian Salomond0a0a652016-12-15 15:25:22 -0500977 uint8_t coverage() const { return fCoverage; }
Brian Salomond0a0a652016-12-15 15:25:22 -0500978 const SkMatrix& viewMatrix() const { return fPaths[0].fViewMatrix; }
joshualitt7bc18b72015-02-03 16:41:41 -0800979
Brian Salomond0a0a652016-12-15 15:25:22 -0500980 struct PathData {
bsalomonf1703092016-06-29 18:41:53 -0700981 SkMatrix fViewMatrix;
982 SkPath fPath;
983 SkIRect fDevClipBounds;
Brian Osmancf3dc292017-06-28 16:45:32 -0400984 SkScalar fCapLength;
bsalomonf1703092016-06-29 18:41:53 -0700985 };
986
Brian Salomona531f252017-07-07 13:29:28 -0400987 SkSTArray<1, PathData, true> fPaths;
988 Helper fHelper;
Brian Osmancf860852018-10-31 14:04:39 -0400989 SkPMColor4f fColor;
Brian Salomond0a0a652016-12-15 15:25:22 -0500990 uint8_t fCoverage;
joshualitt7bc18b72015-02-03 16:41:41 -0800991
Robert Phillips4f93c572020-03-18 08:13:53 -0400992 Program fCharacterization = kNone_Program; // holds a mask of required programs
993 GrSimpleMesh* fMeshes[3] = { nullptr };
994 GrProgramInfo* fProgramInfos[3] = { nullptr };
995
John Stiles7571f9e2020-09-02 22:42:33 -0400996 using INHERITED = GrMeshDrawOp;
joshualitt7bc18b72015-02-03 16:41:41 -0800997};
998
Robert Phillips4f93c572020-03-18 08:13:53 -0400999GR_MAKE_BITFIELD_OPS(AAHairlineOp::Program)
Brian Salomona531f252017-07-07 13:29:28 -04001000
Robert Phillips4f93c572020-03-18 08:13:53 -04001001void AAHairlineOp::makeLineProgramInfo(const GrCaps& caps, SkArenaAlloc* arena,
1002 const GrPipeline* pipeline,
Adlai Hollere2296f72020-11-19 13:41:26 -05001003 const GrSurfaceProxyView& writeView,
Robert Phillips4f93c572020-03-18 08:13:53 -04001004 const SkMatrix* geometryProcessorViewM,
Greg Danield358cbe2020-09-11 09:33:54 -04001005 const SkMatrix* geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -05001006 GrXferBarrierFlags renderPassXferBarriers,
1007 GrLoadOp colorLoadOp) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001008 if (fProgramInfos[0]) {
1009 return;
1010 }
Robert Phillips1b3c2672019-12-04 14:34:48 -05001011
Robert Phillips4f93c572020-03-18 08:13:53 -04001012 GrGeometryProcessor* lineGP;
1013 {
1014 using namespace GrDefaultGeoProcFactory;
Robert Phillips1b3c2672019-12-04 14:34:48 -05001015
Robert Phillips4f93c572020-03-18 08:13:53 -04001016 Color color(this->color());
1017 LocalCoords localCoords(fHelper.usesLocalCoords() ? LocalCoords::kUsePosition_Type
1018 : LocalCoords::kUnused_Type);
1019 localCoords.fMatrix = geometryProcessorLocalM;
Robert Phillips1b3c2672019-12-04 14:34:48 -05001020
Robert Phillips4f93c572020-03-18 08:13:53 -04001021 lineGP = GrDefaultGeoProcFactory::Make(arena,
1022 color,
1023 Coverage::kAttribute_Type,
1024 localCoords,
1025 *geometryProcessorViewM);
1026 SkASSERT(sizeof(LineVertex) == lineGP->vertexStride());
1027 }
1028
Greg Danield358cbe2020-09-11 09:33:54 -04001029 fProgramInfos[0] = GrSimpleMeshDrawOpHelper::CreateProgramInfo(
1030 arena, pipeline, writeView, lineGP, GrPrimitiveType::kTriangles,
Greg Daniel42dbca52020-11-20 10:22:43 -05001031 renderPassXferBarriers, colorLoadOp, fHelper.stencilSettings());
Robert Phillips1b3c2672019-12-04 14:34:48 -05001032}
1033
Robert Phillips4f93c572020-03-18 08:13:53 -04001034void AAHairlineOp::makeQuadProgramInfo(const GrCaps& caps, SkArenaAlloc* arena,
1035 const GrPipeline* pipeline,
Adlai Hollere2296f72020-11-19 13:41:26 -05001036 const GrSurfaceProxyView& writeView,
Robert Phillips4f93c572020-03-18 08:13:53 -04001037 const SkMatrix* geometryProcessorViewM,
Greg Danield358cbe2020-09-11 09:33:54 -04001038 const SkMatrix* geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -05001039 GrXferBarrierFlags renderPassXferBarriers,
1040 GrLoadOp colorLoadOp) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001041 if (fProgramInfos[1]) {
1042 return;
1043 }
1044
Robert Phillips1b3c2672019-12-04 14:34:48 -05001045 GrGeometryProcessor* quadGP = GrQuadEffect::Make(arena,
1046 this->color(),
1047 *geometryProcessorViewM,
Robert Phillips1b3c2672019-12-04 14:34:48 -05001048 caps,
1049 *geometryProcessorLocalM,
1050 fHelper.usesLocalCoords(),
1051 this->coverage());
1052 SkASSERT(sizeof(BezierVertex) == quadGP->vertexStride());
1053
Greg Danield358cbe2020-09-11 09:33:54 -04001054 fProgramInfos[1] = GrSimpleMeshDrawOpHelper::CreateProgramInfo(
1055 arena, pipeline, writeView, quadGP, GrPrimitiveType::kTriangles,
Greg Daniel42dbca52020-11-20 10:22:43 -05001056 renderPassXferBarriers, colorLoadOp, fHelper.stencilSettings());
Robert Phillips1b3c2672019-12-04 14:34:48 -05001057}
1058
Robert Phillips4f93c572020-03-18 08:13:53 -04001059void AAHairlineOp::makeConicProgramInfo(const GrCaps& caps, SkArenaAlloc* arena,
1060 const GrPipeline* pipeline,
Adlai Hollere2296f72020-11-19 13:41:26 -05001061 const GrSurfaceProxyView& writeView,
Robert Phillips4f93c572020-03-18 08:13:53 -04001062 const SkMatrix* geometryProcessorViewM,
Greg Danield358cbe2020-09-11 09:33:54 -04001063 const SkMatrix* geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -05001064 GrXferBarrierFlags renderPassXferBarriers,
1065 GrLoadOp colorLoadOp) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001066 if (fProgramInfos[2]) {
1067 return;
1068 }
1069
Robert Phillips1b3c2672019-12-04 14:34:48 -05001070 GrGeometryProcessor* conicGP = GrConicEffect::Make(arena,
1071 this->color(),
1072 *geometryProcessorViewM,
Robert Phillips1b3c2672019-12-04 14:34:48 -05001073 caps,
1074 *geometryProcessorLocalM,
1075 fHelper.usesLocalCoords(),
1076 this->coverage());
1077 SkASSERT(sizeof(BezierVertex) == conicGP->vertexStride());
1078
Greg Danield358cbe2020-09-11 09:33:54 -04001079 fProgramInfos[2] = GrSimpleMeshDrawOpHelper::CreateProgramInfo(
1080 arena, pipeline, writeView, conicGP, GrPrimitiveType::kTriangles,
Greg Daniel42dbca52020-11-20 10:22:43 -05001081 renderPassXferBarriers, colorLoadOp, fHelper.stencilSettings());
Robert Phillips1b3c2672019-12-04 14:34:48 -05001082}
1083
Robert Phillips4f93c572020-03-18 08:13:53 -04001084AAHairlineOp::Program AAHairlineOp::predictPrograms(const GrCaps* caps) const {
1085 bool convertConicsToQuads = !caps->shaderCaps()->floatIs32Bits();
1086
1087 // When predicting the programs we always include the lineProgram bc it is used as a fallback
1088 // for quads and conics. In non-DDL mode there are cases where it sometimes isn't needed for a
1089 // given path.
1090 Program neededPrograms = kLine_Program;
1091
1092 for (int i = 0; i < fPaths.count(); i++) {
1093 uint32_t mask = fPaths[i].fPath.getSegmentMasks();
1094
1095 if (mask & (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)) {
1096 neededPrograms |= kQuad_Program;
1097 }
1098 if (mask & SkPath::kConic_SegmentMask) {
1099 if (convertConicsToQuads) {
1100 neededPrograms |= kQuad_Program;
1101 } else {
1102 neededPrograms |= kConic_Program;
1103 }
1104 }
1105 }
1106
1107 return neededPrograms;
1108}
1109
1110void AAHairlineOp::onCreateProgramInfo(const GrCaps* caps,
1111 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -05001112 const GrSurfaceProxyView& writeView,
Chris Dalton6aaf00f2021-07-13 13:26:39 -06001113 bool usesMSAASurface,
Robert Phillips4f93c572020-03-18 08:13:53 -04001114 GrAppliedClip&& appliedClip,
John Stiles52cb1d02021-06-02 11:58:05 -04001115 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -05001116 GrXferBarrierFlags renderPassXferBarriers,
1117 GrLoadOp colorLoadOp) {
joshualitt7bc18b72015-02-03 16:41:41 -08001118 // Setup the viewmatrix and localmatrix for the GrGeometryProcessor.
1119 SkMatrix invert;
1120 if (!this->viewMatrix().invert(&invert)) {
1121 return;
1122 }
1123
1124 // we will transform to identity space if the viewmatrix does not have perspective
1125 bool hasPerspective = this->viewMatrix().hasPerspective();
1126 const SkMatrix* geometryProcessorViewM = &SkMatrix::I();
1127 const SkMatrix* geometryProcessorLocalM = &invert;
joshualitt7bc18b72015-02-03 16:41:41 -08001128 if (hasPerspective) {
1129 geometryProcessorViewM = &this->viewMatrix();
1130 geometryProcessorLocalM = &SkMatrix::I();
Robert Phillips4f93c572020-03-18 08:13:53 -04001131 }
1132
Adlai Hollere2296f72020-11-19 13:41:26 -05001133 auto pipeline = fHelper.createPipeline(caps, arena, writeView.swizzle(),
Chris Dalton1b6a43c2020-09-25 12:21:18 -06001134 std::move(appliedClip), dstProxyView);
Robert Phillips4f93c572020-03-18 08:13:53 -04001135
1136 if (fCharacterization & kLine_Program) {
Brian Salomon8afde5f2020-04-01 16:22:00 -04001137 this->makeLineProgramInfo(*caps, arena, pipeline, writeView,
Greg Danield358cbe2020-09-11 09:33:54 -04001138 geometryProcessorViewM, geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -05001139 renderPassXferBarriers, colorLoadOp);
Robert Phillips4f93c572020-03-18 08:13:53 -04001140 }
1141 if (fCharacterization & kQuad_Program) {
Brian Salomon8afde5f2020-04-01 16:22:00 -04001142 this->makeQuadProgramInfo(*caps, arena, pipeline, writeView,
Greg Danield358cbe2020-09-11 09:33:54 -04001143 geometryProcessorViewM, geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -05001144 renderPassXferBarriers, colorLoadOp);
Robert Phillips4f93c572020-03-18 08:13:53 -04001145 }
1146 if (fCharacterization & kConic_Program) {
Brian Salomon8afde5f2020-04-01 16:22:00 -04001147 this->makeConicProgramInfo(*caps, arena, pipeline, writeView,
Greg Danield358cbe2020-09-11 09:33:54 -04001148 geometryProcessorViewM, geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -05001149 renderPassXferBarriers, colorLoadOp);
Robert Phillips4f93c572020-03-18 08:13:53 -04001150
1151 }
1152}
1153
1154void AAHairlineOp::onPrePrepareDraws(GrRecordingContext* context,
Adlai Hollere2296f72020-11-19 13:41:26 -05001155 const GrSurfaceProxyView& writeView,
Robert Phillips4f93c572020-03-18 08:13:53 -04001156 GrAppliedClip* clip,
John Stiles52cb1d02021-06-02 11:58:05 -04001157 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -05001158 GrXferBarrierFlags renderPassXferBarriers,
1159 GrLoadOp colorLoadOp) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001160 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
1161 const GrCaps* caps = context->priv().caps();
1162
Chris Dalton6aaf00f2021-07-13 13:26:39 -06001163 // http://skbug.com/12201 -- DDL does not yet support DMSAA.
1164 bool usesMSAASurface = writeView.asRenderTargetProxy()->numSamples() > 1;
1165
Robert Phillips4f93c572020-03-18 08:13:53 -04001166 // This is equivalent to a GrOpFlushState::detachAppliedClip
Michael Ludwigd1d997e2020-06-04 15:52:44 -04001167 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip::Disabled();
Robert Phillips4f93c572020-03-18 08:13:53 -04001168
1169 // Conservatively predict which programs will be required
1170 fCharacterization = this->predictPrograms(caps);
1171
Chris Dalton6aaf00f2021-07-13 13:26:39 -06001172 this->createProgramInfo(caps, arena, writeView, usesMSAASurface, std::move(appliedClip),
1173 dstProxyView, renderPassXferBarriers, colorLoadOp);
Robert Phillips4f93c572020-03-18 08:13:53 -04001174
1175 context->priv().recordProgramInfo(fProgramInfos[0]);
1176 context->priv().recordProgramInfo(fProgramInfos[1]);
1177 context->priv().recordProgramInfo(fProgramInfos[2]);
1178}
1179
Robert Phillips71143952021-06-17 14:55:07 -04001180void AAHairlineOp::onPrepareDraws(GrMeshDrawTarget* target) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001181 // Setup the viewmatrix and localmatrix for the GrGeometryProcessor.
1182 SkMatrix invert;
1183 if (!this->viewMatrix().invert(&invert)) {
1184 return;
1185 }
1186
1187 // we will transform to identity space if the viewmatrix does not have perspective
1188 const SkMatrix* toDevice = nullptr;
1189 const SkMatrix* toSrc = nullptr;
1190 if (this->viewMatrix().hasPerspective()) {
joshualitt7bc18b72015-02-03 16:41:41 -08001191 toDevice = &this->viewMatrix();
1192 toSrc = &invert;
1193 }
1194
Robert Phillips4f93c572020-03-18 08:13:53 -04001195 SkDEBUGCODE(Program predictedPrograms = this->predictPrograms(&target->caps()));
1196 Program actualPrograms = kNone_Program;
1197
joshualitt7bc18b72015-02-03 16:41:41 -08001198 // This is hand inlined for maximum performance.
1199 PREALLOC_PTARRAY(128) lines;
1200 PREALLOC_PTARRAY(128) quads;
1201 PREALLOC_PTARRAY(128) conics;
1202 IntArray qSubdivs;
1203 FloatArray cWeights;
joshualitt351ba1b2015-02-16 08:33:19 -08001204 int quadCount = 0;
joshualitt7bc18b72015-02-03 16:41:41 -08001205
Brian Salomond0a0a652016-12-15 15:25:22 -05001206 int instanceCount = fPaths.count();
Brian Salomon969a7382018-05-09 13:28:44 -04001207 bool convertConicsToQuads = !target->caps().shaderCaps()->floatIs32Bits();
joshualitt7bc18b72015-02-03 16:41:41 -08001208 for (int i = 0; i < instanceCount; i++) {
Brian Salomond0a0a652016-12-15 15:25:22 -05001209 const PathData& args = fPaths[i];
joshualitt351ba1b2015-02-16 08:33:19 -08001210 quadCount += gather_lines_and_quads(args.fPath, args.fViewMatrix, args.fDevClipBounds,
Brian Salomon969a7382018-05-09 13:28:44 -04001211 args.fCapLength, convertConicsToQuads, &lines, &quads,
1212 &conics, &qSubdivs, &cWeights);
joshualitt7bc18b72015-02-03 16:41:41 -08001213 }
1214
joshualitt7bc18b72015-02-03 16:41:41 -08001215 int lineCount = lines.count() / 2;
1216 int conicCount = conics.count() / 3;
Brian Salomon296de502018-03-13 12:26:55 -04001217 int quadAndConicCount = conicCount + quadCount;
1218
1219 static constexpr int kMaxLines = SK_MaxS32 / kLineSegNumVertices;
1220 static constexpr int kMaxQuadsAndConics = SK_MaxS32 / kQuadNumVertices;
1221 if (lineCount > kMaxLines || quadAndConicCount > kMaxQuadsAndConics) {
1222 return;
1223 }
joshualitt7bc18b72015-02-03 16:41:41 -08001224
1225 // do lines first
1226 if (lineCount) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001227 SkASSERT(predictedPrograms & kLine_Program);
1228 actualPrograms |= kLine_Program;
1229
Brian Salomond28a79d2017-10-16 13:01:07 -04001230 sk_sp<const GrBuffer> linesIndexBuffer = get_lines_index_buffer(target->resourceProvider());
joshualitt7bc18b72015-02-03 16:41:41 -08001231
Robert Phillipse94cdd22019-11-04 14:15:58 -05001232 GrMeshDrawOp::PatternHelper helper(target, GrPrimitiveType::kTriangles, sizeof(LineVertex),
1233 std::move(linesIndexBuffer), kLineSegNumVertices,
1234 kIdxsPerLineSeg, lineCount, kLineSegsNumInIdxBuffer);
1235
1236 LineVertex* verts = reinterpret_cast<LineVertex*>(helper.vertices());
1237 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -08001238 SkDebugf("Could not allocate vertices\n");
1239 return;
1240 }
1241
joshualitt7bc18b72015-02-03 16:41:41 -08001242 for (int i = 0; i < lineCount; ++i) {
1243 add_line(&lines[2*i], toSrc, this->coverage(), &verts);
1244 }
1245
Robert Phillips4f93c572020-03-18 08:13:53 -04001246 fMeshes[0] = helper.mesh();
joshualitt7bc18b72015-02-03 16:41:41 -08001247 }
1248
1249 if (quadCount || conicCount) {
Brian Salomon12d22642019-01-29 14:38:50 -05001250 sk_sp<const GrBuffer> vertexBuffer;
joshualitt7bc18b72015-02-03 16:41:41 -08001251 int firstVertex;
1252
Brian Salomond28a79d2017-10-16 13:01:07 -04001253 sk_sp<const GrBuffer> quadsIndexBuffer = get_quads_index_buffer(target->resourceProvider());
bsalomoned0bcad2015-05-04 10:36:42 -07001254
Brian Salomon296de502018-03-13 12:26:55 -04001255 int vertexCount = kQuadNumVertices * quadAndConicCount;
Brian Salomon92be2f72018-06-19 14:33:47 -04001256 void* vertices = target->makeVertexSpace(sizeof(BezierVertex), vertexCount, &vertexBuffer,
1257 &firstVertex);
joshualitt7bc18b72015-02-03 16:41:41 -08001258
bsalomoned0bcad2015-05-04 10:36:42 -07001259 if (!vertices || !quadsIndexBuffer) {
joshualitt4b31de82015-03-05 14:33:41 -08001260 SkDebugf("Could not allocate vertices\n");
1261 return;
1262 }
1263
joshualitt7bc18b72015-02-03 16:41:41 -08001264 // Setup vertices
robertphillips44c31282015-09-03 12:58:48 -07001265 BezierVertex* bezVerts = reinterpret_cast<BezierVertex*>(vertices);
joshualitt7bc18b72015-02-03 16:41:41 -08001266
joshualitt7bc18b72015-02-03 16:41:41 -08001267 int unsubdivQuadCnt = quads.count() / 3;
1268 for (int i = 0; i < unsubdivQuadCnt; ++i) {
1269 SkASSERT(qSubdivs[i] >= 0);
Brian Osmanae8e0632021-07-26 16:07:35 -04001270 if (!quads[3*i].isFinite() || !quads[3*i+1].isFinite() || !quads[3*i+2].isFinite()) {
1271 return;
1272 }
robertphillips44c31282015-09-03 12:58:48 -07001273 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &bezVerts);
joshualitt7bc18b72015-02-03 16:41:41 -08001274 }
1275
1276 // Start Conics
1277 for (int i = 0; i < conicCount; ++i) {
robertphillips44c31282015-09-03 12:58:48 -07001278 add_conics(&conics[3*i], cWeights[i], toDevice, toSrc, &bezVerts);
joshualitt7bc18b72015-02-03 16:41:41 -08001279 }
1280
1281 if (quadCount > 0) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001282 SkASSERT(predictedPrograms & kQuad_Program);
1283 actualPrograms |= kQuad_Program;
Robert Phillips1b3c2672019-12-04 14:34:48 -05001284
Robert Phillips4f93c572020-03-18 08:13:53 -04001285 fMeshes[1] = target->allocMesh();
1286 fMeshes[1]->setIndexedPatterned(quadsIndexBuffer, kIdxsPerQuad, quadCount,
1287 kQuadsNumInIdxBuffer, vertexBuffer, kQuadNumVertices,
1288 firstVertex);
bsalomon342bfc22016-04-01 06:06:20 -07001289 firstVertex += quadCount * kQuadNumVertices;
joshualitt7bc18b72015-02-03 16:41:41 -08001290 }
1291
1292 if (conicCount > 0) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001293 SkASSERT(predictedPrograms & kConic_Program);
1294 actualPrograms |= kConic_Program;
Robert Phillips1b3c2672019-12-04 14:34:48 -05001295
Robert Phillips4f93c572020-03-18 08:13:53 -04001296 fMeshes[2] = target->allocMesh();
1297 fMeshes[2]->setIndexedPatterned(std::move(quadsIndexBuffer), kIdxsPerQuad, conicCount,
1298 kQuadsNumInIdxBuffer, std::move(vertexBuffer),
1299 kQuadNumVertices, firstVertex);
joshualitt7bc18b72015-02-03 16:41:41 -08001300 }
1301 }
Robert Phillips4f93c572020-03-18 08:13:53 -04001302
1303 // In DDL mode this will replace the predicted program requirements with the actual ones.
1304 // However, we will already have surfaced the predicted programs to the DDL.
1305 fCharacterization = actualPrograms;
joshualitt7bc18b72015-02-03 16:41:41 -08001306}
1307
Chris Dalton07cdcfc92019-02-26 11:13:22 -07001308void AAHairlineOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001309 this->createProgramInfo(flushState);
Robert Phillips3968fcb2019-12-05 16:40:31 -05001310
Robert Phillips4f93c572020-03-18 08:13:53 -04001311 for (int i = 0; i < 3; ++i) {
1312 if (fProgramInfos[i] && fMeshes[i]) {
1313 flushState->bindPipelineAndScissorClip(*fProgramInfos[i], chainBounds);
Robert Phillips787fd9d2021-03-22 14:48:09 -04001314 flushState->bindTextures(fProgramInfos[i]->geomProc(), nullptr,
Robert Phillips4f93c572020-03-18 08:13:53 -04001315 fProgramInfos[i]->pipeline());
1316 flushState->drawMesh(*fMeshes[i]);
1317 }
1318 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -07001319}
1320
bsalomon0aff2fa2015-07-31 06:48:27 -07001321bool GrAAHairLinePathRenderer::onDrawPath(const DrawPathArgs& args) {
Robert Phillipsa92913e2021-07-12 16:31:52 -04001322 GR_AUDIT_TRAIL_AUTO_FRAME(args.fContext->priv().auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -07001323 "GrAAHairlinePathRenderer::onDrawPath");
John Stiles0fbc6a32021-06-04 14:40:57 -04001324 SkASSERT(args.fSurfaceDrawContext->numSamples() <= 1);
csmartdaltonecbc12b2016-06-08 10:08:43 -07001325
bsalomon8acedde2016-06-24 10:42:16 -07001326 SkPath path;
1327 args.fShape->asPath(&path);
Herb Derbyc76d4092020-10-07 16:46:15 -04001328 GrOp::Owner op =
Robert Phillips7c525e62018-06-12 10:11:12 -04001329 AAHairlineOp::Make(args.fContext, std::move(args.fPaint), *args.fViewMatrix, path,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001330 args.fShape->style(), *args.fClipConservativeBounds,
1331 args.fUserStencilSettings);
John Stiles0fbc6a32021-06-04 14:40:57 -04001332 args.fSurfaceDrawContext->addDrawOp(args.fClip, std::move(op));
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001333 return true;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001334}
joshualitt40ded322015-05-02 07:07:17 -07001335
1336///////////////////////////////////////////////////////////////////////////////////////////////////
1337
Hal Canary6f6961e2017-01-31 13:50:44 -05001338#if GR_TEST_UTILS
joshualitt40ded322015-05-02 07:07:17 -07001339
Brian Salomona531f252017-07-07 13:29:28 -04001340GR_DRAW_OP_TEST_DEFINE(AAHairlineOp) {
joshualitt40ded322015-05-02 07:07:17 -07001341 SkMatrix viewMatrix = GrTest::TestMatrix(random);
John Stiles31954bf2020-08-07 17:35:54 -04001342 const SkPath& path = GrTest::TestPath(random);
joshualitt40ded322015-05-02 07:07:17 -07001343 SkIRect devClipBounds;
1344 devClipBounds.setEmpty();
Robert Phillips7c525e62018-06-12 10:11:12 -04001345 return AAHairlineOp::Make(context, std::move(paint), viewMatrix, path,
1346 GrStyle::SimpleHairline(), devClipBounds,
1347 GrGetRandomStencil(random, context));
joshualitt40ded322015-05-02 07:07:17 -07001348}
1349
1350#endif