blob: e60558e1e8a6d7a41500e59311dfa61b68d39855 [file] [log] [blame]
bsalomon@google.comf75b84e2011-09-29 14:58:28 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkPoint3.h"
9#include "include/private/SkTemplates.h"
10#include "src/core/SkGeometry.h"
11#include "src/core/SkMatrixPriv.h"
12#include "src/core/SkPointPriv.h"
13#include "src/core/SkRectPriv.h"
14#include "src/core/SkStroke.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040015#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050016#include "src/gpu/GrBuffer.h"
17#include "src/gpu/GrCaps.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrDefaultGeoProcFactory.h"
19#include "src/gpu/GrDrawOpTest.h"
20#include "src/gpu/GrOpFlushState.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrProcessor.h"
Robert Phillips4f93c572020-03-18 08:13:53 -040022#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/GrResourceProvider.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/GrStyle.h"
Brian Salomoneebe7352020-12-09 16:37:04 -050025#include "src/gpu/GrSurfaceDrawContext.h"
Robert Phillips62214f72021-06-15 10:12:51 -040026#include "src/gpu/GrUtil.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050027#include "src/gpu/effects/GrBezierEffect.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040028#include "src/gpu/geometry/GrPathUtils.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000029#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050030#include "src/gpu/ops/GrAAHairLinePathRenderer.h"
31#include "src/gpu/ops/GrMeshDrawOp.h"
Robert Phillips55f681f2020-02-28 08:58:15 -050032#include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h"
bsalomon@google.com4647f902013-03-26 14:45:27 +000033
bsalomoned0bcad2015-05-04 10:36:42 -070034#define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true>
35
bsalomon@google.comaeb21602011-08-30 18:13:44 +000036// quadratics are rendered as 5-sided polys in order to bound the
37// AA stroke around the center-curve. See comments in push_quad_index_buffer and
egdaniel@google.com5383a752013-07-12 20:15:34 +000038// bloat_quad. Quadratics and conics share an index buffer
bsalomon@google.comaeb21602011-08-30 18:13:44 +000039
robertphillips@google.comada90da2013-09-18 22:14:49 +000040// lines are rendered as:
41// *______________*
42// |\ -_______ /|
43// | \ \ / |
44// | *--------* |
45// | / ______/ \ |
46// */_-__________\*
47// For: 6 vertices and 18 indices (for 6 triangles)
bsalomon@google.comaeb21602011-08-30 18:13:44 +000048
joshualitt5ead6da2014-10-22 16:00:29 -070049// Each quadratic is rendered as a five sided polygon. This poly bounds
50// the quadratic's bounding triangle but has been expanded so that the
51// 1-pixel wide area around the curve is inside the poly.
52// If a,b,c are the original control points then the poly a0,b0,c0,c1,a1
53// that is rendered would look like this:
54// b0
55// b
56//
57// a0 c0
58// a c
59// a1 c1
egdaniel14afb432014-12-22 10:57:08 -080060// Each is drawn as three triangles ((a0,a1,b0), (b0,c1,c0), (a1,c1,b0))
61// specified by these 9 indices:
joshualitt5ead6da2014-10-22 16:00:29 -070062static const uint16_t kQuadIdxBufPattern[] = {
63 0, 1, 2,
64 2, 4, 3,
65 1, 4, 2
66};
bsalomon@google.comaeb21602011-08-30 18:13:44 +000067
joshualitt5ead6da2014-10-22 16:00:29 -070068static const int kIdxsPerQuad = SK_ARRAY_COUNT(kQuadIdxBufPattern);
69static const int kQuadNumVertices = 5;
70static const int kQuadsNumInIdxBuffer = 256;
bsalomoned0bcad2015-05-04 10:36:42 -070071GR_DECLARE_STATIC_UNIQUE_KEY(gQuadsIndexBufferKey);
72
Brian Salomond28a79d2017-10-16 13:01:07 -040073static sk_sp<const GrBuffer> get_quads_index_buffer(GrResourceProvider* resourceProvider) {
bsalomoned0bcad2015-05-04 10:36:42 -070074 GR_DEFINE_STATIC_UNIQUE_KEY(gQuadsIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -040075 return resourceProvider->findOrCreatePatternedIndexBuffer(
bsalomoned0bcad2015-05-04 10:36:42 -070076 kQuadIdxBufPattern, kIdxsPerQuad, kQuadsNumInIdxBuffer, kQuadNumVertices,
77 gQuadsIndexBufferKey);
78}
jvanverth@google.com681ccf02013-08-16 14:51:51 +000079
bsalomon@google.comaeb21602011-08-30 18:13:44 +000080
joshualitt5ead6da2014-10-22 16:00:29 -070081// Each line segment is rendered as two quads and two triangles.
82// p0 and p1 have alpha = 1 while all other points have alpha = 0.
83// The four external points are offset 1 pixel perpendicular to the
84// line and half a pixel parallel to the line.
85//
86// p4 p5
87// p0 p1
88// p2 p3
89//
90// Each is drawn as six triangles specified by these 18 indices:
jvanverth@google.com681ccf02013-08-16 14:51:51 +000091
joshualitt5ead6da2014-10-22 16:00:29 -070092static const uint16_t kLineSegIdxBufPattern[] = {
93 0, 1, 3,
94 0, 3, 2,
95 0, 4, 5,
96 0, 5, 1,
97 0, 2, 4,
98 1, 5, 3
99};
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000100
joshualitt5ead6da2014-10-22 16:00:29 -0700101static const int kIdxsPerLineSeg = SK_ARRAY_COUNT(kLineSegIdxBufPattern);
102static const int kLineSegNumVertices = 6;
103static const int kLineSegsNumInIdxBuffer = 256;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000104
bsalomoned0bcad2015-05-04 10:36:42 -0700105GR_DECLARE_STATIC_UNIQUE_KEY(gLinesIndexBufferKey);
106
Brian Salomond28a79d2017-10-16 13:01:07 -0400107static sk_sp<const GrBuffer> get_lines_index_buffer(GrResourceProvider* resourceProvider) {
bsalomoned0bcad2015-05-04 10:36:42 -0700108 GR_DEFINE_STATIC_UNIQUE_KEY(gLinesIndexBufferKey);
Chris Daltonff926502017-05-03 14:36:54 -0400109 return resourceProvider->findOrCreatePatternedIndexBuffer(
bsalomoned0bcad2015-05-04 10:36:42 -0700110 kLineSegIdxBufPattern, kIdxsPerLineSeg, kLineSegsNumInIdxBuffer, kLineSegNumVertices,
111 gLinesIndexBufferKey);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000112}
113
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000114// Takes 178th time of logf on Z600 / VC2010
bsalomoned0bcad2015-05-04 10:36:42 -0700115static int get_float_exp(float x) {
Brian Salomon4dea72a2019-12-18 10:43:10 -0500116 static_assert(sizeof(int) == sizeof(float));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000117#ifdef SK_DEBUG
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000118 static bool tested;
119 if (!tested) {
120 tested = true;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000121 SkASSERT(get_float_exp(0.25f) == -2);
122 SkASSERT(get_float_exp(0.3f) == -2);
123 SkASSERT(get_float_exp(0.5f) == -1);
124 SkASSERT(get_float_exp(1.f) == 0);
125 SkASSERT(get_float_exp(2.f) == 1);
126 SkASSERT(get_float_exp(2.5f) == 1);
127 SkASSERT(get_float_exp(8.f) == 3);
128 SkASSERT(get_float_exp(100.f) == 6);
129 SkASSERT(get_float_exp(1000.f) == 9);
130 SkASSERT(get_float_exp(1024.f) == 10);
131 SkASSERT(get_float_exp(3000000.f) == 21);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000132 }
133#endif
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000134 const int* iptr = (const int*)&x;
135 return (((*iptr) & 0x7f800000) >> 23) - 127;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000136}
137
egdaniel@google.com5383a752013-07-12 20:15:34 +0000138// Uses the max curvature function for quads to estimate
139// where to chop the conic. If the max curvature is not
140// found along the curve segment it will return 1 and
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000141// dst[0] is the original conic. If it returns 2 the dst[0]
egdaniel@google.com5383a752013-07-12 20:15:34 +0000142// and dst[1] are the two new conics.
bsalomoned0bcad2015-05-04 10:36:42 -0700143static int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000144 SkScalar t = SkFindQuadMaxCurvature(src);
Chris Dalton1d474dd2018-07-24 01:08:31 -0600145 if (t == 0 || t == 1) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000146 if (dst) {
147 dst[0].set(src, weight);
148 }
149 return 1;
150 } else {
151 if (dst) {
152 SkConic conic;
153 conic.set(src, weight);
caryclark414c4292016-09-26 11:03:54 -0700154 if (!conic.chopAt(t, dst)) {
155 dst[0].set(src, weight);
156 return 1;
157 }
egdaniel@google.com5383a752013-07-12 20:15:34 +0000158 }
159 return 2;
160 }
161}
162
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000163// Calls split_conic on the entire conic and then once more on each subsection.
164// Most cases will result in either 1 conic (chop point is not within t range)
165// or 3 points (split once and then one subsection is split again).
bsalomoned0bcad2015-05-04 10:36:42 -0700166static int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000167 SkConic dstTemp[2];
168 int conicCnt = split_conic(src, dstTemp, weight);
169 if (2 == conicCnt) {
170 int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW);
171 conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW);
172 } else {
173 dst[0] = dstTemp[0];
174 }
175 return conicCnt;
176}
177
egdaniel@google.com5383a752013-07-12 20:15:34 +0000178// returns 0 if quad/conic is degen or close to it
179// in this case approx the path with lines
180// otherwise returns 1
bsalomoned0bcad2015-05-04 10:36:42 -0700181static int is_degen_quad_or_conic(const SkPoint p[3], SkScalar* dsqd) {
jvanverthcfeb85f2016-04-29 07:38:10 -0700182 static const SkScalar gDegenerateToLineTol = GrPathUtils::kDefaultTolerance;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000183 static const SkScalar gDegenerateToLineTolSqd =
Mike Reed8be952a2017-02-13 20:44:33 -0500184 gDegenerateToLineTol * gDegenerateToLineTol;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000185
Cary Clarkdf429f32017-11-08 11:44:31 -0500186 if (SkPointPriv::DistanceToSqd(p[0], p[1]) < gDegenerateToLineTolSqd ||
187 SkPointPriv::DistanceToSqd(p[1], p[2]) < gDegenerateToLineTolSqd) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000188 return 1;
189 }
190
Cary Clarkdf429f32017-11-08 11:44:31 -0500191 *dsqd = SkPointPriv::DistanceToLineBetweenSqd(p[1], p[0], p[2]);
joshualitt63648072015-02-19 10:25:21 -0800192 if (*dsqd < gDegenerateToLineTolSqd) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000193 return 1;
194 }
195
Cary Clarkdf429f32017-11-08 11:44:31 -0500196 if (SkPointPriv::DistanceToLineBetweenSqd(p[2], p[1], p[0]) < gDegenerateToLineTolSqd) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000197 return 1;
198 }
199 return 0;
200}
201
bsalomoned0bcad2015-05-04 10:36:42 -0700202static int is_degen_quad_or_conic(const SkPoint p[3]) {
joshualitt63648072015-02-19 10:25:21 -0800203 SkScalar dsqd;
204 return is_degen_quad_or_conic(p, &dsqd);
205}
206
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000207// we subdivide the quads to avoid huge overfill
208// if it returns -1 then should be drawn as lines
bsalomoned0bcad2015-05-04 10:36:42 -0700209static int num_quad_subdivs(const SkPoint p[3]) {
joshualitt63648072015-02-19 10:25:21 -0800210 SkScalar dsqd;
211 if (is_degen_quad_or_conic(p, &dsqd)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000212 return -1;
213 }
214
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000215 // tolerance of triangle height in pixels
216 // tuned on windows Quadro FX 380 / Z600
217 // trade off of fill vs cpu time on verts
218 // maybe different when do this using gpu (geo or tess shaders)
219 static const SkScalar gSubdivTol = 175 * SK_Scalar1;
220
Mike Reed8be952a2017-02-13 20:44:33 -0500221 if (dsqd <= gSubdivTol * gSubdivTol) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000222 return 0;
223 } else {
robertphillips@google.com87379e12013-03-29 12:11:10 +0000224 static const int kMaxSub = 4;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000225 // subdividing the quad reduces d by 4. so we want x = log4(d/tol)
226 // = log4(d*d/tol*tol)/2
227 // = log2(d*d/tol*tol)
228
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000229 // +1 since we're ignoring the mantissa contribution.
230 int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
Brian Osman788b9162020-02-07 10:36:46 -0500231 log = std::min(std::max(0, log), kMaxSub);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000232 return log;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000233 }
234}
235
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000236/**
237 * Generates the lines and quads to be rendered. Lines are always recorded in
238 * device space. We will do a device space bloat to account for the 1pixel
239 * thickness.
240 * Quads are recorded in device space unless m contains
241 * perspective, then in they are in src space. We do this because we will
242 * subdivide large quads to reduce over-fill. This subdivision has to be
243 * performed before applying the perspective matrix.
244 */
bsalomoned0bcad2015-05-04 10:36:42 -0700245static int gather_lines_and_quads(const SkPath& path,
246 const SkMatrix& m,
247 const SkIRect& devClipBounds,
Brian Osmancf3dc292017-06-28 16:45:32 -0400248 SkScalar capLength,
Brian Salomon969a7382018-05-09 13:28:44 -0400249 bool convertConicsToQuads,
bsalomoned0bcad2015-05-04 10:36:42 -0700250 GrAAHairLinePathRenderer::PtArray* lines,
251 GrAAHairLinePathRenderer::PtArray* quads,
252 GrAAHairLinePathRenderer::PtArray* conics,
253 GrAAHairLinePathRenderer::IntArray* quadSubdivCnts,
254 GrAAHairLinePathRenderer::FloatArray* conicWeights) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000255 SkPath::Iter iter(path, false);
256
257 int totalQuadCount = 0;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000258 SkRect bounds;
259 SkIRect ibounds;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000260
261 bool persp = m.hasPerspective();
262
Brian Osmancf3dc292017-06-28 16:45:32 -0400263 // Whenever a degenerate, zero-length contour is encountered, this code will insert a
264 // 'capLength' x-aligned line segment. Since this is rendering hairlines it is hoped this will
265 // suffice for AA square & circle capping.
266 int verbsInContour = 0; // Does not count moves
267 bool seenZeroLengthVerb = false;
268 SkPoint zeroVerbPt;
269
Brian Salomon969a7382018-05-09 13:28:44 -0400270 // Adds a quad that has already been chopped to the list and checks for quads that are close to
271 // lines. Also does a bounding box check. It takes points that are in src space and device
272 // space. The src points are only required if the view matrix has perspective.
273 auto addChoppedQuad = [&](const SkPoint srcPts[3], const SkPoint devPts[4],
274 bool isContourStart) {
275 SkRect bounds;
276 SkIRect ibounds;
277 bounds.setBounds(devPts, 3);
278 bounds.outset(SK_Scalar1, SK_Scalar1);
279 bounds.roundOut(&ibounds);
280 // We only need the src space space pts when not in perspective.
281 SkASSERT(srcPts || !persp);
282 if (SkIRect::Intersects(devClipBounds, ibounds)) {
283 int subdiv = num_quad_subdivs(devPts);
284 SkASSERT(subdiv >= -1);
285 if (-1 == subdiv) {
286 SkPoint* pts = lines->push_back_n(4);
287 pts[0] = devPts[0];
288 pts[1] = devPts[1];
289 pts[2] = devPts[1];
290 pts[3] = devPts[2];
291 if (isContourStart && pts[0] == pts[1] && pts[2] == pts[3]) {
292 seenZeroLengthVerb = true;
293 zeroVerbPt = pts[0];
294 }
295 } else {
296 // when in perspective keep quads in src space
297 const SkPoint* qPts = persp ? srcPts : devPts;
298 SkPoint* pts = quads->push_back_n(3);
299 pts[0] = qPts[0];
300 pts[1] = qPts[1];
301 pts[2] = qPts[2];
302 quadSubdivCnts->push_back() = subdiv;
303 totalQuadCount += 1 << subdiv;
304 }
305 }
306 };
307
308 // Applies the view matrix to quad src points and calls the above helper.
309 auto addSrcChoppedQuad = [&](const SkPoint srcSpaceQuadPts[3], bool isContourStart) {
310 SkPoint devPts[3];
311 m.mapPoints(devPts, srcSpaceQuadPts, 3);
312 addChoppedQuad(srcSpaceQuadPts, devPts, isContourStart);
313 };
314
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000315 for (;;) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000316 SkPoint pathPts[4];
Mike Reedba7e9a62019-08-16 13:30:34 -0400317 SkPath::Verb verb = iter.next(pathPts);
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000318 switch (verb) {
Brian Salomon969a7382018-05-09 13:28:44 -0400319 case SkPath::kConic_Verb:
320 if (convertConicsToQuads) {
321 SkScalar weight = iter.conicWeight();
322 SkAutoConicToQuads converter;
Brian Osmane3deee12018-11-20 11:10:15 -0500323 const SkPoint* quadPts = converter.computeQuads(pathPts, weight, 0.25f);
Brian Salomon969a7382018-05-09 13:28:44 -0400324 for (int i = 0; i < converter.countQuads(); ++i) {
325 addSrcChoppedQuad(quadPts + 2 * i, !verbsInContour && 0 == i);
326 }
327 } else {
328 SkConic dst[4];
329 // We chop the conics to create tighter clipping to hide error
330 // that appears near max curvature of very thin conics. Thin
331 // hyperbolas with high weight still show error.
332 int conicCnt = chop_conic(pathPts, dst, iter.conicWeight());
333 for (int i = 0; i < conicCnt; ++i) {
334 SkPoint devPts[4];
335 SkPoint* chopPnts = dst[i].fPts;
336 m.mapPoints(devPts, chopPnts, 3);
337 bounds.setBounds(devPts, 3);
338 bounds.outset(SK_Scalar1, SK_Scalar1);
339 bounds.roundOut(&ibounds);
340 if (SkIRect::Intersects(devClipBounds, ibounds)) {
341 if (is_degen_quad_or_conic(devPts)) {
342 SkPoint* pts = lines->push_back_n(4);
343 pts[0] = devPts[0];
344 pts[1] = devPts[1];
345 pts[2] = devPts[1];
346 pts[3] = devPts[2];
347 if (verbsInContour == 0 && i == 0 && pts[0] == pts[1] &&
348 pts[2] == pts[3]) {
349 seenZeroLengthVerb = true;
350 zeroVerbPt = pts[0];
351 }
352 } else {
353 // when in perspective keep conics in src space
354 SkPoint* cPts = persp ? chopPnts : devPts;
355 SkPoint* pts = conics->push_back_n(3);
356 pts[0] = cPts[0];
357 pts[1] = cPts[1];
358 pts[2] = cPts[2];
359 conicWeights->push_back() = dst[i].fW;
Brian Osmancf3dc292017-06-28 16:45:32 -0400360 }
egdaniel@google.com5383a752013-07-12 20:15:34 +0000361 }
362 }
363 }
Brian Osmancf3dc292017-06-28 16:45:32 -0400364 verbsInContour++;
reed@google.com277c3f82013-05-31 15:17:50 +0000365 break;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000366 case SkPath::kMove_Verb:
Brian Osmancf3dc292017-06-28 16:45:32 -0400367 // New contour (and last one was unclosed). If it was just a zero length drawing
368 // operation, and we're supposed to draw caps, then add a tiny line.
369 if (seenZeroLengthVerb && verbsInContour == 1 && capLength > 0) {
370 SkPoint* pts = lines->push_back_n(2);
371 pts[0] = SkPoint::Make(zeroVerbPt.fX - capLength, zeroVerbPt.fY);
372 pts[1] = SkPoint::Make(zeroVerbPt.fX + capLength, zeroVerbPt.fY);
373 }
374 verbsInContour = 0;
375 seenZeroLengthVerb = false;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000376 break;
Brian Salomon969a7382018-05-09 13:28:44 -0400377 case SkPath::kLine_Verb: {
378 SkPoint devPts[2];
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000379 m.mapPoints(devPts, pathPts, 2);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000380 bounds.setBounds(devPts, 2);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000381 bounds.outset(SK_Scalar1, SK_Scalar1);
382 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000383 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000384 SkPoint* pts = lines->push_back_n(2);
385 pts[0] = devPts[0];
386 pts[1] = devPts[1];
Brian Osmancf3dc292017-06-28 16:45:32 -0400387 if (verbsInContour == 0 && pts[0] == pts[1]) {
388 seenZeroLengthVerb = true;
389 zeroVerbPt = pts[0];
390 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000391 }
Brian Osmancf3dc292017-06-28 16:45:32 -0400392 verbsInContour++;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000393 break;
Brian Salomon969a7382018-05-09 13:28:44 -0400394 }
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000395 case SkPath::kQuad_Verb: {
396 SkPoint choppedPts[5];
397 // Chopping the quad helps when the quad is either degenerate or nearly degenerate.
398 // When it is degenerate it allows the approximation with lines to work since the
399 // chop point (if there is one) will be at the parabola's vertex. In the nearly
400 // degenerate the QuadUVMatrix computed for the points is almost singular which
401 // can cause rendering artifacts.
402 int n = SkChopQuadAtMaxCurvature(pathPts, choppedPts);
403 for (int i = 0; i < n; ++i) {
Brian Salomon969a7382018-05-09 13:28:44 -0400404 addSrcChoppedQuad(choppedPts + i * 2, !verbsInContour && 0 == i);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000405 }
Brian Osmancf3dc292017-06-28 16:45:32 -0400406 verbsInContour++;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000407 break;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000408 }
Brian Salomon969a7382018-05-09 13:28:44 -0400409 case SkPath::kCubic_Verb: {
410 SkPoint devPts[4];
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000411 m.mapPoints(devPts, pathPts, 4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000412 bounds.setBounds(devPts, 4);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000413 bounds.outset(SK_Scalar1, SK_Scalar1);
414 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000415 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000416 PREALLOC_PTARRAY(32) q;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000417 // We convert cubics to quadratics (for now).
418 // In perspective have to do conversion in src space.
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000419 if (persp) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000420 SkScalar tolScale =
bsalomon18fab302016-02-16 08:00:05 -0800421 GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m, path.getBounds());
422 GrPathUtils::convertCubicToQuads(pathPts, tolScale, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000423 } else {
bsalomon18fab302016-02-16 08:00:05 -0800424 GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000425 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000426 for (int i = 0; i < q.count(); i += 3) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000427 if (persp) {
Brian Salomon969a7382018-05-09 13:28:44 -0400428 addSrcChoppedQuad(&q[i], !verbsInContour && 0 == i);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000429 } else {
Brian Salomon969a7382018-05-09 13:28:44 -0400430 addChoppedQuad(nullptr, &q[i], !verbsInContour && 0 == i);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000431 }
432 }
433 }
Brian Osmancf3dc292017-06-28 16:45:32 -0400434 verbsInContour++;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000435 break;
Brian Salomon969a7382018-05-09 13:28:44 -0400436 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000437 case SkPath::kClose_Verb:
Brian Osmancf3dc292017-06-28 16:45:32 -0400438 // Contour is closed, so we don't need to grow the starting line, unless it's
439 // *just* a zero length subpath. (SVG Spec 11.4, 'stroke').
440 if (capLength > 0) {
441 if (seenZeroLengthVerb && verbsInContour == 1) {
442 SkPoint* pts = lines->push_back_n(2);
443 pts[0] = SkPoint::Make(zeroVerbPt.fX - capLength, zeroVerbPt.fY);
444 pts[1] = SkPoint::Make(zeroVerbPt.fX + capLength, zeroVerbPt.fY);
445 } else if (verbsInContour == 0) {
446 // Contour was (moveTo, close). Add a line.
Brian Salomon969a7382018-05-09 13:28:44 -0400447 SkPoint devPts[2];
Brian Osmancf3dc292017-06-28 16:45:32 -0400448 m.mapPoints(devPts, pathPts, 1);
449 devPts[1] = devPts[0];
450 bounds.setBounds(devPts, 2);
451 bounds.outset(SK_Scalar1, SK_Scalar1);
452 bounds.roundOut(&ibounds);
453 if (SkIRect::Intersects(devClipBounds, ibounds)) {
454 SkPoint* pts = lines->push_back_n(2);
455 pts[0] = SkPoint::Make(devPts[0].fX - capLength, devPts[0].fY);
456 pts[1] = SkPoint::Make(devPts[1].fX + capLength, devPts[1].fY);
457 }
458 }
459 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000460 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000461 case SkPath::kDone_Verb:
Brian Osmancf3dc292017-06-28 16:45:32 -0400462 if (seenZeroLengthVerb && verbsInContour == 1 && capLength > 0) {
463 // Path ended with a dangling (moveTo, line|quad|etc). If the final verb is
464 // degenerate, we need to draw a line.
465 SkPoint* pts = lines->push_back_n(2);
466 pts[0] = SkPoint::Make(zeroVerbPt.fX - capLength, zeroVerbPt.fY);
467 pts[1] = SkPoint::Make(zeroVerbPt.fX + capLength, zeroVerbPt.fY);
468 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000469 return totalQuadCount;
470 }
471 }
472}
473
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000474struct LineVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000475 SkPoint fPos;
egdaniele27065a2014-11-06 08:00:48 -0800476 float fCoverage;
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000477};
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000478
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000479struct BezierVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000480 SkPoint fPos;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000481 union {
482 struct {
csmartdaltoncc261272017-03-23 13:38:45 -0600483 SkScalar fKLM[3];
egdaniel@google.com5383a752013-07-12 20:15:34 +0000484 } fConic;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000485 SkVector fQuadCoord;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000486 struct {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000487 SkScalar fBogus[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000488 };
489 };
490};
egdaniel@google.com5383a752013-07-12 20:15:34 +0000491
Brian Salomon4dea72a2019-12-18 10:43:10 -0500492static_assert(sizeof(BezierVertex) == 3 * sizeof(SkPoint));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000493
bsalomoned0bcad2015-05-04 10:36:42 -0700494static void intersect_lines(const SkPoint& ptA, const SkVector& normA,
495 const SkPoint& ptB, const SkVector& normB,
496 SkPoint* result) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000497
498 SkScalar lineAW = -normA.dot(ptA);
499 SkScalar lineBW = -normB.dot(ptB);
500
Mike Reed8be952a2017-02-13 20:44:33 -0500501 SkScalar wInv = normA.fX * normB.fY - normA.fY * normB.fX;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000502 wInv = SkScalarInvert(wInv);
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400503 if (!SkScalarIsFinite(wInv)) {
504 // lines are parallel, pick the point in between
505 *result = (ptA + ptB)*SK_ScalarHalf;
506 *result += normA;
507 } else {
508 result->fX = normA.fY * lineBW - lineAW * normB.fY;
509 result->fX *= wInv;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000510
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400511 result->fY = lineAW * normB.fX - normA.fX * lineBW;
512 result->fY *= wInv;
513 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000514}
515
bsalomoned0bcad2015-05-04 10:36:42 -0700516static void set_uv_quad(const SkPoint qpts[3], BezierVertex verts[kQuadNumVertices]) {
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000517 // this should be in the src space, not dev coords, when we have perspective
518 GrPathUtils::QuadUVMatrix DevToUV(qpts);
Brian Osman568bec72018-12-26 16:48:25 -0500519 DevToUV.apply(verts, kQuadNumVertices, sizeof(BezierVertex), sizeof(SkPoint));
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000520}
521
bsalomoned0bcad2015-05-04 10:36:42 -0700522static void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice,
523 const SkMatrix* toSrc, BezierVertex verts[kQuadNumVertices]) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000524 SkASSERT(!toDevice == !toSrc);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000525 // original quad is specified by tri a,b,c
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000526 SkPoint a = qpts[0];
527 SkPoint b = qpts[1];
528 SkPoint c = qpts[2];
529
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000530 if (toDevice) {
531 toDevice->mapPoints(&a, 1);
532 toDevice->mapPoints(&b, 1);
533 toDevice->mapPoints(&c, 1);
534 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000535 // make a new poly where we replace a and c by a 1-pixel wide edges orthog
536 // to edges ab and bc:
537 //
538 // before | after
539 // | b0
540 // b |
541 // |
542 // | a0 c0
543 // a c | a1 c1
544 //
545 // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c,
546 // respectively.
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000547 BezierVertex& a0 = verts[0];
548 BezierVertex& a1 = verts[1];
549 BezierVertex& b0 = verts[2];
550 BezierVertex& c0 = verts[3];
551 BezierVertex& c1 = verts[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000552
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000553 SkVector ab = b;
554 ab -= a;
555 SkVector ac = c;
556 ac -= a;
557 SkVector cb = b;
558 cb -= c;
559
Robert Phillips2c2303e2021-02-04 11:04:01 -0500560 // After the transform (or due to floating point math) we might have a line,
561 // try to do something reasonable
562 if (SkPointPriv::LengthSqd(ab) <= SK_ScalarNearlyZero*SK_ScalarNearlyZero) {
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400563 ab = cb;
564 }
Robert Phillips2c2303e2021-02-04 11:04:01 -0500565 if (SkPointPriv::LengthSqd(cb) <= SK_ScalarNearlyZero*SK_ScalarNearlyZero) {
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400566 cb = ab;
567 }
568
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000569 // We should have already handled degenerates
Robert Phillips2c2303e2021-02-04 11:04:01 -0500570 SkASSERT(ab.length() > 0 && cb.length() > 0);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000571
572 ab.normalize();
Brian Salomon0235c642018-08-31 12:04:18 -0400573 SkVector abN = SkPointPriv::MakeOrthog(ab, SkPointPriv::kLeft_Side);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000574 if (abN.dot(ac) > 0) {
575 abN.negate();
576 }
577
578 cb.normalize();
Brian Salomon0235c642018-08-31 12:04:18 -0400579 SkVector cbN = SkPointPriv::MakeOrthog(cb, SkPointPriv::kLeft_Side);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000580 if (cbN.dot(ac) < 0) {
581 cbN.negate();
582 }
583
584 a0.fPos = a;
585 a0.fPos += abN;
586 a1.fPos = a;
587 a1.fPos -= abN;
588
Jim Van Verth1499d9e2018-10-09 16:30:52 -0400589 if (toDevice && SkPointPriv::LengthSqd(ac) <= SK_ScalarNearlyZero*SK_ScalarNearlyZero) {
590 c = b;
591 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000592 c0.fPos = c;
593 c0.fPos += cbN;
594 c1.fPos = c;
595 c1.fPos -= cbN;
596
597 intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos);
598
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000599 if (toSrc) {
Brian Salomonfa3783f2018-01-05 13:49:07 -0500600 SkMatrixPriv::MapPointsWithStride(*toSrc, &verts[0].fPos, sizeof(BezierVertex),
601 kQuadNumVertices);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000602 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000603}
604
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000605// Equations based off of Loop-Blinn Quadratic GPU Rendering
egdaniel@google.com5383a752013-07-12 20:15:34 +0000606// Input Parametric:
607// 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)
608// Output Implicit:
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000609// f(x, y, w) = f(P) = K^2 - LM
610// K = dot(k, P), L = dot(l, P), M = dot(m, P)
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000611// k, l, m are calculated in function GrPathUtils::getConicKLM
bsalomoned0bcad2015-05-04 10:36:42 -0700612static void set_conic_coeffs(const SkPoint p[3], BezierVertex verts[kQuadNumVertices],
613 const SkScalar weight) {
csmartdaltoncc261272017-03-23 13:38:45 -0600614 SkMatrix klm;
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000615
csmartdaltoncc261272017-03-23 13:38:45 -0600616 GrPathUtils::getConicKLM(p, weight, &klm);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000617
joshualitt5ead6da2014-10-22 16:00:29 -0700618 for (int i = 0; i < kQuadNumVertices; ++i) {
Cary Clarke4442cb2017-10-18 11:46:18 -0400619 const SkPoint3 pt3 = {verts[i].fPos.x(), verts[i].fPos.y(), 1.f};
620 klm.mapHomogeneousPoints((SkPoint3* ) verts[i].fConic.fKLM, &pt3, 1);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000621 }
622}
623
bsalomoned0bcad2015-05-04 10:36:42 -0700624static void add_conics(const SkPoint p[3],
625 const SkScalar weight,
626 const SkMatrix* toDevice,
627 const SkMatrix* toSrc,
628 BezierVertex** vert) {
joshualitt7bc18b72015-02-03 16:41:41 -0800629 bloat_quad(p, toDevice, toSrc, *vert);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000630 set_conic_coeffs(p, *vert, weight);
joshualitt5ead6da2014-10-22 16:00:29 -0700631 *vert += kQuadNumVertices;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000632}
633
bsalomoned0bcad2015-05-04 10:36:42 -0700634static void add_quads(const SkPoint p[3],
635 int subdiv,
636 const SkMatrix* toDevice,
637 const SkMatrix* toSrc,
638 BezierVertex** vert) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000639 SkASSERT(subdiv >= 0);
Jim Van Verth14982c82020-07-15 15:42:19 -0400640 // temporary vertex storage to avoid reading the vertex buffer
Jim Van Verth3557ab42020-07-28 09:46:03 -0400641 BezierVertex outVerts[kQuadNumVertices] = {};
Jim Van Verth14982c82020-07-15 15:42:19 -0400642
643 // storage for the chopped quad
644 // pts 0,1,2 are the first quad, and 2,3,4 the second quad
645 SkPoint choppedQuadPts[5];
646 // start off with our original curve in the second quad slot
647 memcpy(&choppedQuadPts[2], p, 3*sizeof(SkPoint));
648
649 int stepCount = 1 << subdiv;
650 while (stepCount > 1) {
651 // The general idea is:
652 // * chop the quad using pts 2,3,4 as the input
653 // * write out verts using pts 0,1,2
654 // * now 2,3,4 is the remainder of the curve, chop again until all subdivisions are done
655 SkScalar h = 1.f / stepCount;
656 SkChopQuadAt(&choppedQuadPts[2], choppedQuadPts, h);
657
658 bloat_quad(choppedQuadPts, toDevice, toSrc, outVerts);
659 set_uv_quad(choppedQuadPts, outVerts);
660 memcpy(*vert, outVerts, kQuadNumVertices*sizeof(BezierVertex));
joshualitt5ead6da2014-10-22 16:00:29 -0700661 *vert += kQuadNumVertices;
Jim Van Verth14982c82020-07-15 15:42:19 -0400662 --stepCount;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000663 }
Jim Van Verth14982c82020-07-15 15:42:19 -0400664
665 // finish up, write out the final quad
666 bloat_quad(&choppedQuadPts[2], toDevice, toSrc, outVerts);
667 set_uv_quad(&choppedQuadPts[2], outVerts);
668 memcpy(*vert, outVerts, kQuadNumVertices * sizeof(BezierVertex));
669 *vert += kQuadNumVertices;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000670}
671
bsalomoned0bcad2015-05-04 10:36:42 -0700672static void add_line(const SkPoint p[2],
673 const SkMatrix* toSrc,
674 uint8_t coverage,
675 LineVertex** vert) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000676 const SkPoint& a = p[0];
677 const SkPoint& b = p[1];
678
robertphillips@google.comada90da2013-09-18 22:14:49 +0000679 SkVector ortho, vec = b;
680 vec -= a;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000681
Cary Clarkdf429f32017-11-08 11:44:31 -0500682 SkScalar lengthSqd = SkPointPriv::LengthSqd(vec);
Greg Daniel2e67dea2017-10-12 15:14:41 -0400683
robertphillips@google.comada90da2013-09-18 22:14:49 +0000684 if (vec.setLength(SK_ScalarHalf)) {
685 // Create a vector orthogonal to 'vec' and of unit length
686 ortho.fX = 2.0f * vec.fY;
687 ortho.fY = -2.0f * vec.fX;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000688
egdaniele27065a2014-11-06 08:00:48 -0800689 float floatCoverage = GrNormalizeByteToFloat(coverage);
690
Greg Daniel2e67dea2017-10-12 15:14:41 -0400691 if (lengthSqd >= 1.0f) {
692 // Relative to points a and b:
693 // The inner vertices are inset half a pixel along the line a,b
694 (*vert)[0].fPos = a + vec;
695 (*vert)[0].fCoverage = floatCoverage;
696 (*vert)[1].fPos = b - vec;
697 (*vert)[1].fCoverage = floatCoverage;
698 } else {
699 // The inner vertices are inset a distance of length(a,b) from the outer edge of
700 // geometry. For the "a" inset this is the same as insetting from b by half a pixel.
701 // The coverage is then modulated by the length. This gives us the correct
702 // coverage for rects shorter than a pixel as they get translated subpixel amounts
703 // inside of a pixel.
704 SkScalar length = SkScalarSqrt(lengthSqd);
705 (*vert)[0].fPos = b - vec;
706 (*vert)[0].fCoverage = floatCoverage * length;
707 (*vert)[1].fPos = a + vec;
708 (*vert)[1].fCoverage = floatCoverage * length;
709 }
710 // Relative to points a and b:
711 // The outer vertices are outset half a pixel along the line a,b and then a whole pixel
712 // orthogonally.
robertphillips@google.comada90da2013-09-18 22:14:49 +0000713 (*vert)[2].fPos = a - vec + ortho;
714 (*vert)[2].fCoverage = 0;
715 (*vert)[3].fPos = b + vec + ortho;
716 (*vert)[3].fCoverage = 0;
717 (*vert)[4].fPos = a - vec - ortho;
718 (*vert)[4].fCoverage = 0;
719 (*vert)[5].fPos = b + vec - ortho;
720 (*vert)[5].fCoverage = 0;
721
bsalomon49f085d2014-09-05 13:34:00 -0700722 if (toSrc) {
Brian Salomonfa3783f2018-01-05 13:49:07 -0500723 SkMatrixPriv::MapPointsWithStride(*toSrc, &(*vert)->fPos, sizeof(LineVertex),
724 kLineSegNumVertices);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000725 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000726 } else {
727 // just make it degenerate and likely offscreen
joshualitt5ead6da2014-10-22 16:00:29 -0700728 for (int i = 0; i < kLineSegNumVertices; ++i) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000729 (*vert)[i].fPos.set(SK_ScalarMax, SK_ScalarMax);
730 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000731 }
732
joshualitt5ead6da2014-10-22 16:00:29 -0700733 *vert += kLineSegNumVertices;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000734}
735
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000736///////////////////////////////////////////////////////////////////////////////
737
Chris Dalton5ed44232017-09-07 13:22:46 -0600738GrPathRenderer::CanDrawPath
739GrAAHairLinePathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Chris Dalton6ce447a2019-06-23 18:07:38 -0600740 if (GrAAType::kCoverage != args.fAAType) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600741 return CanDrawPath::kNo;
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000742 }
743
Robert Phillips62214f72021-06-15 10:12:51 -0400744 if (!GrIsStrokeHairlineOrEquivalent(args.fShape->style(), *args.fViewMatrix, nullptr)) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600745 return CanDrawPath::kNo;
bsalomon6663acf2016-05-10 09:14:17 -0700746 }
747
748 // We don't currently handle dashing in this class though perhaps we should.
bsalomon8acedde2016-06-24 10:42:16 -0700749 if (args.fShape->style().pathEffect()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600750 return CanDrawPath::kNo;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000751 }
752
bsalomon8acedde2016-06-24 10:42:16 -0700753 if (SkPath::kLine_SegmentMask == args.fShape->segmentMask() ||
Eric Karl5c779752017-05-08 12:02:07 -0700754 args.fCaps->shaderCaps()->shaderDerivativeSupport()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600755 return CanDrawPath::kYes;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000756 }
bsalomon8acedde2016-06-24 10:42:16 -0700757
Chris Dalton5ed44232017-09-07 13:22:46 -0600758 return CanDrawPath::kNo;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000759}
760
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000761template <class VertexType>
joshualitt8059eb92014-12-29 15:10:07 -0800762bool check_bounds(const SkMatrix& viewMatrix, const SkRect& devBounds, void* vertices, int vCount)
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000763{
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000764 SkRect tolDevBounds = devBounds;
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000765 // The bounds ought to be tight, but in perspective the below code runs the verts
766 // through the view matrix to get back to dev coords, which can introduce imprecision.
joshualitt8059eb92014-12-29 15:10:07 -0800767 if (viewMatrix.hasPerspective()) {
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000768 tolDevBounds.outset(SK_Scalar1 / 1000, SK_Scalar1 / 1000);
769 } else {
770 // Non-persp matrices cause this path renderer to draw in device space.
joshualitt8059eb92014-12-29 15:10:07 -0800771 SkASSERT(viewMatrix.isIdentity());
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000772 }
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000773 SkRect actualBounds;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000774
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000775 VertexType* verts = reinterpret_cast<VertexType*>(vertices);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000776 bool first = true;
777 for (int i = 0; i < vCount; ++i) {
778 SkPoint pos = verts[i].fPos;
779 // This is a hack to workaround the fact that we move some degenerate segments offscreen.
780 if (SK_ScalarMax == pos.fX) {
781 continue;
782 }
joshualitt8059eb92014-12-29 15:10:07 -0800783 viewMatrix.mapPoints(&pos, 1);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000784 if (first) {
Mike Reed92b33352019-08-24 19:39:13 -0400785 actualBounds.setLTRB(pos.fX, pos.fY, pos.fX, pos.fY);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000786 first = false;
787 } else {
Mike Reed185ffe92018-01-08 17:09:54 -0500788 SkRectPriv::GrowToInclude(&actualBounds, pos);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000789 }
790 }
791 if (!first) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000792 return tolDevBounds.contains(actualBounds);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000793 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000794
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000795 return true;
796}
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000797
Brian Salomona531f252017-07-07 13:29:28 -0400798class AAHairlineOp final : public GrMeshDrawOp {
799private:
800 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
801
joshualitt7bc18b72015-02-03 16:41:41 -0800802public:
Brian Salomon25a88092016-12-01 09:36:50 -0500803 DEFINE_OP_CLASS_ID
reed1b55a962015-09-17 20:16:13 -0700804
Herb Derbyc76d4092020-10-07 16:46:15 -0400805 static GrOp::Owner Make(GrRecordingContext* context,
806 GrPaint&& paint,
807 const SkMatrix& viewMatrix,
808 const SkPath& path,
809 const GrStyle& style,
810 const SkIRect& devClipBounds,
811 const GrUserStencilSettings* stencilSettings) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500812 SkScalar hairlineCoverage;
813 uint8_t newCoverage = 0xff;
Robert Phillips62214f72021-06-15 10:12:51 -0400814 if (GrIsStrokeHairlineOrEquivalent(style, viewMatrix, &hairlineCoverage)) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500815 newCoverage = SkScalarRoundToInt(hairlineCoverage * 0xff);
816 }
joshualitt7bc18b72015-02-03 16:41:41 -0800817
Brian Osmancf3dc292017-06-28 16:45:32 -0400818 const SkStrokeRec& stroke = style.strokeRec();
819 SkScalar capLength = SkPaint::kButt_Cap != stroke.getCap() ? hairlineCoverage * 0.5f : 0.0f;
820
Robert Phillips7c525e62018-06-12 10:11:12 -0400821 return Helper::FactoryHelper<AAHairlineOp>(context, std::move(paint), newCoverage,
822 viewMatrix, path,
Brian Salomona531f252017-07-07 13:29:28 -0400823 devClipBounds, capLength, stencilSettings);
824 }
825
Herb Derbyc76d4092020-10-07 16:46:15 -0400826 AAHairlineOp(GrProcessorSet* processorSet,
Brian Osmancf860852018-10-31 14:04:39 -0400827 const SkPMColor4f& color,
Brian Salomona531f252017-07-07 13:29:28 -0400828 uint8_t coverage,
829 const SkMatrix& viewMatrix,
830 const SkPath& path,
831 SkIRect devClipBounds,
832 SkScalar capLength,
833 const GrUserStencilSettings* stencilSettings)
834 : INHERITED(ClassID())
Herb Derbyc76d4092020-10-07 16:46:15 -0400835 , fHelper(processorSet, GrAAType::kCoverage, stencilSettings)
Brian Salomona531f252017-07-07 13:29:28 -0400836 , fColor(color)
837 , fCoverage(coverage) {
838 fPaths.emplace_back(PathData{viewMatrix, path, devClipBounds, capLength});
839
840 this->setTransformedBounds(path.getBounds(), viewMatrix, HasAABloat::kYes,
Greg Daniel5faf4742019-10-01 15:14:44 -0400841 IsHairline::kYes);
bsalomonf1703092016-06-29 18:41:53 -0700842 }
joshualitt7bc18b72015-02-03 16:41:41 -0800843
Brian Salomond0a0a652016-12-15 15:25:22 -0500844 const char* name() const override { return "AAHairlineOp"; }
joshualitt7bc18b72015-02-03 16:41:41 -0800845
Robert Phillips294723d2021-06-17 09:23:58 -0400846 void visitProxies(const GrVisitProxyFunc& func) const override {
Robert Phillips4f93c572020-03-18 08:13:53 -0400847
848 bool visited = false;
849 for (int i = 0; i < 3; ++i) {
850 if (fProgramInfos[i]) {
851 fProgramInfos[i]->visitFPProxies(func);
852 visited = true;
853 }
854 }
855
856 if (!visited) {
857 fHelper.visitProxies(func);
858 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400859 }
860
Brian Salomona531f252017-07-07 13:29:28 -0400861 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
862
Chris Dalton57ab06c2021-04-22 12:57:28 -0600863 GrProcessorSet::Analysis finalize(const GrCaps& caps, const GrAppliedClip* clip,
864 GrClampType clampType) override {
Brian Osman8fa7ab42019-03-18 10:22:42 -0400865 // This Op uses uniform (not vertex) color, so doesn't need to track wide color.
Chris Dalton57ab06c2021-04-22 12:57:28 -0600866 return fHelper.finalizeProcessors(caps, clip, clampType,
Brian Osman8fa7ab42019-03-18 10:22:42 -0400867 GrProcessorAnalysisCoverage::kSingleChannel, &fColor,
868 nullptr);
Brian Salomona531f252017-07-07 13:29:28 -0400869 }
870
Robert Phillips4f93c572020-03-18 08:13:53 -0400871 enum Program : uint8_t {
872 kNone_Program = 0x0,
873 kLine_Program = 0x1,
874 kQuad_Program = 0x2,
875 kConic_Program = 0x4,
876 };
877
bsalomone46f9fe2015-08-18 06:05:14 -0700878private:
Robert Phillips4f93c572020-03-18 08:13:53 -0400879 void makeLineProgramInfo(const GrCaps&, SkArenaAlloc*, const GrPipeline*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500880 const GrSurfaceProxyView& writeView,
Robert Phillips4f93c572020-03-18 08:13:53 -0400881 const SkMatrix* geometryProcessorViewM,
Greg Danield358cbe2020-09-11 09:33:54 -0400882 const SkMatrix* geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -0500883 GrXferBarrierFlags renderPassXferBarriers,
884 GrLoadOp colorLoadOp);
Robert Phillips4f93c572020-03-18 08:13:53 -0400885 void makeQuadProgramInfo(const GrCaps&, SkArenaAlloc*, const GrPipeline*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500886 const GrSurfaceProxyView& writeView,
Robert Phillips4f93c572020-03-18 08:13:53 -0400887 const SkMatrix* geometryProcessorViewM,
Greg Danield358cbe2020-09-11 09:33:54 -0400888 const SkMatrix* geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -0500889 GrXferBarrierFlags renderPassXferBarriers,
890 GrLoadOp colorLoadOp);
Robert Phillips4f93c572020-03-18 08:13:53 -0400891 void makeConicProgramInfo(const GrCaps&, SkArenaAlloc*, const GrPipeline*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500892 const GrSurfaceProxyView& writeView,
Robert Phillips4f93c572020-03-18 08:13:53 -0400893 const SkMatrix* geometryProcessorViewM,
Greg Danield358cbe2020-09-11 09:33:54 -0400894 const SkMatrix* geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -0500895 GrXferBarrierFlags renderPassXferBarriers,
896 GrLoadOp colorLoadOp);
Robert Phillips1b3c2672019-12-04 14:34:48 -0500897
Robert Phillips2669a7b2020-03-12 12:07:19 -0400898 GrProgramInfo* programInfo() override {
899 // This Op has 3 programInfos and implements its own onPrePrepareDraws so this entry point
900 // should really never be called.
901 SkASSERT(0);
902 return nullptr;
903 }
904
Robert Phillips4f93c572020-03-18 08:13:53 -0400905 Program predictPrograms(const GrCaps*) const;
906
Robert Phillips4133dc42020-03-11 15:55:55 -0400907 void onCreateProgramInfo(const GrCaps*,
908 SkArenaAlloc*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500909 const GrSurfaceProxyView& writeView,
Chris Dalton6aaf00f2021-07-13 13:26:39 -0600910 bool usesMSAASurface,
Robert Phillips4133dc42020-03-11 15:55:55 -0400911 GrAppliedClip&&,
John Stiles52cb1d02021-06-02 11:58:05 -0400912 const GrDstProxyView&,
Greg Daniel42dbca52020-11-20 10:22:43 -0500913 GrXferBarrierFlags renderPassXferBarriers,
914 GrLoadOp colorLoadOp) override;
Robert Phillips4133dc42020-03-11 15:55:55 -0400915
Robert Phillips2669a7b2020-03-12 12:07:19 -0400916 void onPrePrepareDraws(GrRecordingContext*,
Adlai Hollere2296f72020-11-19 13:41:26 -0500917 const GrSurfaceProxyView& writeView,
Robert Phillips2669a7b2020-03-12 12:07:19 -0400918 GrAppliedClip*,
John Stiles52cb1d02021-06-02 11:58:05 -0400919 const GrDstProxyView&,
Greg Daniel42dbca52020-11-20 10:22:43 -0500920 GrXferBarrierFlags renderPassXferBarriers,
921 GrLoadOp colorLoadOp) override;
Robert Phillips2669a7b2020-03-12 12:07:19 -0400922
Robert Phillips71143952021-06-17 14:55:07 -0400923 void onPrepareDraws(GrMeshDrawTarget*) override;
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700924 void onExecute(GrOpFlushState*, const SkRect& chainBounds) override;
bsalomon75398562015-08-17 12:55:38 -0700925
joshualitt7bc18b72015-02-03 16:41:41 -0800926 typedef SkTArray<SkPoint, true> PtArray;
927 typedef SkTArray<int, true> IntArray;
928 typedef SkTArray<float, true> FloatArray;
929
Herb Derbye25c3002020-10-27 15:57:27 -0400930 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500931 AAHairlineOp* that = t->cast<AAHairlineOp>();
bsalomonabd30f52015-08-13 13:34:48 -0700932
Brian Salomona531f252017-07-07 13:29:28 -0400933 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000934 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700935 }
936
joshualitt7bc18b72015-02-03 16:41:41 -0800937 if (this->viewMatrix().hasPerspective() != that->viewMatrix().hasPerspective()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000938 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800939 }
940
941 // We go to identity if we don't have perspective
942 if (this->viewMatrix().hasPerspective() &&
Mike Reed2c383152019-12-18 16:47:47 -0500943 !SkMatrixPriv::CheapEqual(this->viewMatrix(), that->viewMatrix())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000944 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800945 }
946
Brian Salomon53e4c3c2016-12-21 11:38:53 -0500947 // TODO we can actually combine hairlines if they are the same color in a kind of bulk
948 // method but we haven't implemented this yet
joshualitt7bc18b72015-02-03 16:41:41 -0800949 // TODO investigate going to vertex color and coverage?
950 if (this->coverage() != that->coverage()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000951 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800952 }
953
954 if (this->color() != that->color()) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000955 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800956 }
957
Mike Reed2c383152019-12-18 16:47:47 -0500958 if (fHelper.usesLocalCoords() && !SkMatrixPriv::CheapEqual(this->viewMatrix(),
959 that->viewMatrix())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000960 return CombineResult::kCannotCombine;
joshualitt7bc18b72015-02-03 16:41:41 -0800961 }
962
Brian Salomond0a0a652016-12-15 15:25:22 -0500963 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
Brian Salomon7eae3e02018-08-07 14:02:38 +0000964 return CombineResult::kMerged;
joshualitt7bc18b72015-02-03 16:41:41 -0800965 }
966
John Stilesaf366522020-08-13 09:57:34 -0400967#if GR_TEST_UTILS
968 SkString onDumpInfo() const override {
969 return SkStringPrintf("Color: 0x%08x Coverage: 0x%02x, Count: %d\n%s",
970 fColor.toBytes_RGBA(), fCoverage, fPaths.count(),
971 fHelper.dumpInfo().c_str());
972 }
973#endif
974
Brian Osmancf860852018-10-31 14:04:39 -0400975 const SkPMColor4f& color() const { return fColor; }
Brian Salomond0a0a652016-12-15 15:25:22 -0500976 uint8_t coverage() const { return fCoverage; }
Brian Salomond0a0a652016-12-15 15:25:22 -0500977 const SkMatrix& viewMatrix() const { return fPaths[0].fViewMatrix; }
joshualitt7bc18b72015-02-03 16:41:41 -0800978
Brian Salomond0a0a652016-12-15 15:25:22 -0500979 struct PathData {
bsalomonf1703092016-06-29 18:41:53 -0700980 SkMatrix fViewMatrix;
981 SkPath fPath;
982 SkIRect fDevClipBounds;
Brian Osmancf3dc292017-06-28 16:45:32 -0400983 SkScalar fCapLength;
bsalomonf1703092016-06-29 18:41:53 -0700984 };
985
Brian Salomona531f252017-07-07 13:29:28 -0400986 SkSTArray<1, PathData, true> fPaths;
987 Helper fHelper;
Brian Osmancf860852018-10-31 14:04:39 -0400988 SkPMColor4f fColor;
Brian Salomond0a0a652016-12-15 15:25:22 -0500989 uint8_t fCoverage;
joshualitt7bc18b72015-02-03 16:41:41 -0800990
Robert Phillips4f93c572020-03-18 08:13:53 -0400991 Program fCharacterization = kNone_Program; // holds a mask of required programs
992 GrSimpleMesh* fMeshes[3] = { nullptr };
993 GrProgramInfo* fProgramInfos[3] = { nullptr };
994
John Stiles7571f9e2020-09-02 22:42:33 -0400995 using INHERITED = GrMeshDrawOp;
joshualitt7bc18b72015-02-03 16:41:41 -0800996};
997
Robert Phillips4f93c572020-03-18 08:13:53 -0400998GR_MAKE_BITFIELD_OPS(AAHairlineOp::Program)
Brian Salomona531f252017-07-07 13:29:28 -0400999
Robert Phillips4f93c572020-03-18 08:13:53 -04001000void AAHairlineOp::makeLineProgramInfo(const GrCaps& caps, SkArenaAlloc* arena,
1001 const GrPipeline* pipeline,
Adlai Hollere2296f72020-11-19 13:41:26 -05001002 const GrSurfaceProxyView& writeView,
Robert Phillips4f93c572020-03-18 08:13:53 -04001003 const SkMatrix* geometryProcessorViewM,
Greg Danield358cbe2020-09-11 09:33:54 -04001004 const SkMatrix* geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -05001005 GrXferBarrierFlags renderPassXferBarriers,
1006 GrLoadOp colorLoadOp) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001007 if (fProgramInfos[0]) {
1008 return;
1009 }
Robert Phillips1b3c2672019-12-04 14:34:48 -05001010
Robert Phillips4f93c572020-03-18 08:13:53 -04001011 GrGeometryProcessor* lineGP;
1012 {
1013 using namespace GrDefaultGeoProcFactory;
Robert Phillips1b3c2672019-12-04 14:34:48 -05001014
Robert Phillips4f93c572020-03-18 08:13:53 -04001015 Color color(this->color());
1016 LocalCoords localCoords(fHelper.usesLocalCoords() ? LocalCoords::kUsePosition_Type
1017 : LocalCoords::kUnused_Type);
1018 localCoords.fMatrix = geometryProcessorLocalM;
Robert Phillips1b3c2672019-12-04 14:34:48 -05001019
Robert Phillips4f93c572020-03-18 08:13:53 -04001020 lineGP = GrDefaultGeoProcFactory::Make(arena,
1021 color,
1022 Coverage::kAttribute_Type,
1023 localCoords,
1024 *geometryProcessorViewM);
1025 SkASSERT(sizeof(LineVertex) == lineGP->vertexStride());
1026 }
1027
Greg Danield358cbe2020-09-11 09:33:54 -04001028 fProgramInfos[0] = GrSimpleMeshDrawOpHelper::CreateProgramInfo(
1029 arena, pipeline, writeView, lineGP, GrPrimitiveType::kTriangles,
Greg Daniel42dbca52020-11-20 10:22:43 -05001030 renderPassXferBarriers, colorLoadOp, fHelper.stencilSettings());
Robert Phillips1b3c2672019-12-04 14:34:48 -05001031}
1032
Robert Phillips4f93c572020-03-18 08:13:53 -04001033void AAHairlineOp::makeQuadProgramInfo(const GrCaps& caps, SkArenaAlloc* arena,
1034 const GrPipeline* pipeline,
Adlai Hollere2296f72020-11-19 13:41:26 -05001035 const GrSurfaceProxyView& writeView,
Robert Phillips4f93c572020-03-18 08:13:53 -04001036 const SkMatrix* geometryProcessorViewM,
Greg Danield358cbe2020-09-11 09:33:54 -04001037 const SkMatrix* geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -05001038 GrXferBarrierFlags renderPassXferBarriers,
1039 GrLoadOp colorLoadOp) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001040 if (fProgramInfos[1]) {
1041 return;
1042 }
1043
Robert Phillips1b3c2672019-12-04 14:34:48 -05001044 GrGeometryProcessor* quadGP = GrQuadEffect::Make(arena,
1045 this->color(),
1046 *geometryProcessorViewM,
Robert Phillips1b3c2672019-12-04 14:34:48 -05001047 caps,
1048 *geometryProcessorLocalM,
1049 fHelper.usesLocalCoords(),
1050 this->coverage());
1051 SkASSERT(sizeof(BezierVertex) == quadGP->vertexStride());
1052
Greg Danield358cbe2020-09-11 09:33:54 -04001053 fProgramInfos[1] = GrSimpleMeshDrawOpHelper::CreateProgramInfo(
1054 arena, pipeline, writeView, quadGP, GrPrimitiveType::kTriangles,
Greg Daniel42dbca52020-11-20 10:22:43 -05001055 renderPassXferBarriers, colorLoadOp, fHelper.stencilSettings());
Robert Phillips1b3c2672019-12-04 14:34:48 -05001056}
1057
Robert Phillips4f93c572020-03-18 08:13:53 -04001058void AAHairlineOp::makeConicProgramInfo(const GrCaps& caps, SkArenaAlloc* arena,
1059 const GrPipeline* pipeline,
Adlai Hollere2296f72020-11-19 13:41:26 -05001060 const GrSurfaceProxyView& writeView,
Robert Phillips4f93c572020-03-18 08:13:53 -04001061 const SkMatrix* geometryProcessorViewM,
Greg Danield358cbe2020-09-11 09:33:54 -04001062 const SkMatrix* geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -05001063 GrXferBarrierFlags renderPassXferBarriers,
1064 GrLoadOp colorLoadOp) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001065 if (fProgramInfos[2]) {
1066 return;
1067 }
1068
Robert Phillips1b3c2672019-12-04 14:34:48 -05001069 GrGeometryProcessor* conicGP = GrConicEffect::Make(arena,
1070 this->color(),
1071 *geometryProcessorViewM,
Robert Phillips1b3c2672019-12-04 14:34:48 -05001072 caps,
1073 *geometryProcessorLocalM,
1074 fHelper.usesLocalCoords(),
1075 this->coverage());
1076 SkASSERT(sizeof(BezierVertex) == conicGP->vertexStride());
1077
Greg Danield358cbe2020-09-11 09:33:54 -04001078 fProgramInfos[2] = GrSimpleMeshDrawOpHelper::CreateProgramInfo(
1079 arena, pipeline, writeView, conicGP, GrPrimitiveType::kTriangles,
Greg Daniel42dbca52020-11-20 10:22:43 -05001080 renderPassXferBarriers, colorLoadOp, fHelper.stencilSettings());
Robert Phillips1b3c2672019-12-04 14:34:48 -05001081}
1082
Robert Phillips4f93c572020-03-18 08:13:53 -04001083AAHairlineOp::Program AAHairlineOp::predictPrograms(const GrCaps* caps) const {
1084 bool convertConicsToQuads = !caps->shaderCaps()->floatIs32Bits();
1085
1086 // When predicting the programs we always include the lineProgram bc it is used as a fallback
1087 // for quads and conics. In non-DDL mode there are cases where it sometimes isn't needed for a
1088 // given path.
1089 Program neededPrograms = kLine_Program;
1090
1091 for (int i = 0; i < fPaths.count(); i++) {
1092 uint32_t mask = fPaths[i].fPath.getSegmentMasks();
1093
1094 if (mask & (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)) {
1095 neededPrograms |= kQuad_Program;
1096 }
1097 if (mask & SkPath::kConic_SegmentMask) {
1098 if (convertConicsToQuads) {
1099 neededPrograms |= kQuad_Program;
1100 } else {
1101 neededPrograms |= kConic_Program;
1102 }
1103 }
1104 }
1105
1106 return neededPrograms;
1107}
1108
1109void AAHairlineOp::onCreateProgramInfo(const GrCaps* caps,
1110 SkArenaAlloc* arena,
Adlai Hollere2296f72020-11-19 13:41:26 -05001111 const GrSurfaceProxyView& writeView,
Chris Dalton6aaf00f2021-07-13 13:26:39 -06001112 bool usesMSAASurface,
Robert Phillips4f93c572020-03-18 08:13:53 -04001113 GrAppliedClip&& appliedClip,
John Stiles52cb1d02021-06-02 11:58:05 -04001114 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -05001115 GrXferBarrierFlags renderPassXferBarriers,
1116 GrLoadOp colorLoadOp) {
joshualitt7bc18b72015-02-03 16:41:41 -08001117 // Setup the viewmatrix and localmatrix for the GrGeometryProcessor.
1118 SkMatrix invert;
1119 if (!this->viewMatrix().invert(&invert)) {
1120 return;
1121 }
1122
1123 // we will transform to identity space if the viewmatrix does not have perspective
1124 bool hasPerspective = this->viewMatrix().hasPerspective();
1125 const SkMatrix* geometryProcessorViewM = &SkMatrix::I();
1126 const SkMatrix* geometryProcessorLocalM = &invert;
joshualitt7bc18b72015-02-03 16:41:41 -08001127 if (hasPerspective) {
1128 geometryProcessorViewM = &this->viewMatrix();
1129 geometryProcessorLocalM = &SkMatrix::I();
Robert Phillips4f93c572020-03-18 08:13:53 -04001130 }
1131
Adlai Hollere2296f72020-11-19 13:41:26 -05001132 auto pipeline = fHelper.createPipeline(caps, arena, writeView.swizzle(),
Chris Dalton1b6a43c2020-09-25 12:21:18 -06001133 std::move(appliedClip), dstProxyView);
Robert Phillips4f93c572020-03-18 08:13:53 -04001134
1135 if (fCharacterization & kLine_Program) {
Brian Salomon8afde5f2020-04-01 16:22:00 -04001136 this->makeLineProgramInfo(*caps, arena, pipeline, writeView,
Greg Danield358cbe2020-09-11 09:33:54 -04001137 geometryProcessorViewM, geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -05001138 renderPassXferBarriers, colorLoadOp);
Robert Phillips4f93c572020-03-18 08:13:53 -04001139 }
1140 if (fCharacterization & kQuad_Program) {
Brian Salomon8afde5f2020-04-01 16:22:00 -04001141 this->makeQuadProgramInfo(*caps, arena, pipeline, writeView,
Greg Danield358cbe2020-09-11 09:33:54 -04001142 geometryProcessorViewM, geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -05001143 renderPassXferBarriers, colorLoadOp);
Robert Phillips4f93c572020-03-18 08:13:53 -04001144 }
1145 if (fCharacterization & kConic_Program) {
Brian Salomon8afde5f2020-04-01 16:22:00 -04001146 this->makeConicProgramInfo(*caps, arena, pipeline, writeView,
Greg Danield358cbe2020-09-11 09:33:54 -04001147 geometryProcessorViewM, geometryProcessorLocalM,
Greg Daniel42dbca52020-11-20 10:22:43 -05001148 renderPassXferBarriers, colorLoadOp);
Robert Phillips4f93c572020-03-18 08:13:53 -04001149
1150 }
1151}
1152
1153void AAHairlineOp::onPrePrepareDraws(GrRecordingContext* context,
Adlai Hollere2296f72020-11-19 13:41:26 -05001154 const GrSurfaceProxyView& writeView,
Robert Phillips4f93c572020-03-18 08:13:53 -04001155 GrAppliedClip* clip,
John Stiles52cb1d02021-06-02 11:58:05 -04001156 const GrDstProxyView& dstProxyView,
Greg Daniel42dbca52020-11-20 10:22:43 -05001157 GrXferBarrierFlags renderPassXferBarriers,
1158 GrLoadOp colorLoadOp) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001159 SkArenaAlloc* arena = context->priv().recordTimeAllocator();
1160 const GrCaps* caps = context->priv().caps();
1161
Chris Dalton6aaf00f2021-07-13 13:26:39 -06001162 // http://skbug.com/12201 -- DDL does not yet support DMSAA.
1163 bool usesMSAASurface = writeView.asRenderTargetProxy()->numSamples() > 1;
1164
Robert Phillips4f93c572020-03-18 08:13:53 -04001165 // This is equivalent to a GrOpFlushState::detachAppliedClip
Michael Ludwigd1d997e2020-06-04 15:52:44 -04001166 GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip::Disabled();
Robert Phillips4f93c572020-03-18 08:13:53 -04001167
1168 // Conservatively predict which programs will be required
1169 fCharacterization = this->predictPrograms(caps);
1170
Chris Dalton6aaf00f2021-07-13 13:26:39 -06001171 this->createProgramInfo(caps, arena, writeView, usesMSAASurface, std::move(appliedClip),
1172 dstProxyView, renderPassXferBarriers, colorLoadOp);
Robert Phillips4f93c572020-03-18 08:13:53 -04001173
1174 context->priv().recordProgramInfo(fProgramInfos[0]);
1175 context->priv().recordProgramInfo(fProgramInfos[1]);
1176 context->priv().recordProgramInfo(fProgramInfos[2]);
1177}
1178
Robert Phillips71143952021-06-17 14:55:07 -04001179void AAHairlineOp::onPrepareDraws(GrMeshDrawTarget* target) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001180 // Setup the viewmatrix and localmatrix for the GrGeometryProcessor.
1181 SkMatrix invert;
1182 if (!this->viewMatrix().invert(&invert)) {
1183 return;
1184 }
1185
1186 // we will transform to identity space if the viewmatrix does not have perspective
1187 const SkMatrix* toDevice = nullptr;
1188 const SkMatrix* toSrc = nullptr;
1189 if (this->viewMatrix().hasPerspective()) {
joshualitt7bc18b72015-02-03 16:41:41 -08001190 toDevice = &this->viewMatrix();
1191 toSrc = &invert;
1192 }
1193
Robert Phillips4f93c572020-03-18 08:13:53 -04001194 SkDEBUGCODE(Program predictedPrograms = this->predictPrograms(&target->caps()));
1195 Program actualPrograms = kNone_Program;
1196
joshualitt7bc18b72015-02-03 16:41:41 -08001197 // This is hand inlined for maximum performance.
1198 PREALLOC_PTARRAY(128) lines;
1199 PREALLOC_PTARRAY(128) quads;
1200 PREALLOC_PTARRAY(128) conics;
1201 IntArray qSubdivs;
1202 FloatArray cWeights;
joshualitt351ba1b2015-02-16 08:33:19 -08001203 int quadCount = 0;
joshualitt7bc18b72015-02-03 16:41:41 -08001204
Brian Salomond0a0a652016-12-15 15:25:22 -05001205 int instanceCount = fPaths.count();
Brian Salomon969a7382018-05-09 13:28:44 -04001206 bool convertConicsToQuads = !target->caps().shaderCaps()->floatIs32Bits();
joshualitt7bc18b72015-02-03 16:41:41 -08001207 for (int i = 0; i < instanceCount; i++) {
Brian Salomond0a0a652016-12-15 15:25:22 -05001208 const PathData& args = fPaths[i];
joshualitt351ba1b2015-02-16 08:33:19 -08001209 quadCount += gather_lines_and_quads(args.fPath, args.fViewMatrix, args.fDevClipBounds,
Brian Salomon969a7382018-05-09 13:28:44 -04001210 args.fCapLength, convertConicsToQuads, &lines, &quads,
1211 &conics, &qSubdivs, &cWeights);
joshualitt7bc18b72015-02-03 16:41:41 -08001212 }
1213
joshualitt7bc18b72015-02-03 16:41:41 -08001214 int lineCount = lines.count() / 2;
1215 int conicCount = conics.count() / 3;
Brian Salomon296de502018-03-13 12:26:55 -04001216 int quadAndConicCount = conicCount + quadCount;
1217
1218 static constexpr int kMaxLines = SK_MaxS32 / kLineSegNumVertices;
1219 static constexpr int kMaxQuadsAndConics = SK_MaxS32 / kQuadNumVertices;
1220 if (lineCount > kMaxLines || quadAndConicCount > kMaxQuadsAndConics) {
1221 return;
1222 }
joshualitt7bc18b72015-02-03 16:41:41 -08001223
1224 // do lines first
1225 if (lineCount) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001226 SkASSERT(predictedPrograms & kLine_Program);
1227 actualPrograms |= kLine_Program;
1228
Brian Salomond28a79d2017-10-16 13:01:07 -04001229 sk_sp<const GrBuffer> linesIndexBuffer = get_lines_index_buffer(target->resourceProvider());
joshualitt7bc18b72015-02-03 16:41:41 -08001230
Robert Phillipse94cdd22019-11-04 14:15:58 -05001231 GrMeshDrawOp::PatternHelper helper(target, GrPrimitiveType::kTriangles, sizeof(LineVertex),
1232 std::move(linesIndexBuffer), kLineSegNumVertices,
1233 kIdxsPerLineSeg, lineCount, kLineSegsNumInIdxBuffer);
1234
1235 LineVertex* verts = reinterpret_cast<LineVertex*>(helper.vertices());
1236 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -08001237 SkDebugf("Could not allocate vertices\n");
1238 return;
1239 }
1240
joshualitt7bc18b72015-02-03 16:41:41 -08001241 for (int i = 0; i < lineCount; ++i) {
1242 add_line(&lines[2*i], toSrc, this->coverage(), &verts);
1243 }
1244
Robert Phillips4f93c572020-03-18 08:13:53 -04001245 fMeshes[0] = helper.mesh();
joshualitt7bc18b72015-02-03 16:41:41 -08001246 }
1247
1248 if (quadCount || conicCount) {
Brian Salomon12d22642019-01-29 14:38:50 -05001249 sk_sp<const GrBuffer> vertexBuffer;
joshualitt7bc18b72015-02-03 16:41:41 -08001250 int firstVertex;
1251
Brian Salomond28a79d2017-10-16 13:01:07 -04001252 sk_sp<const GrBuffer> quadsIndexBuffer = get_quads_index_buffer(target->resourceProvider());
bsalomoned0bcad2015-05-04 10:36:42 -07001253
Brian Salomon296de502018-03-13 12:26:55 -04001254 int vertexCount = kQuadNumVertices * quadAndConicCount;
Brian Salomon92be2f72018-06-19 14:33:47 -04001255 void* vertices = target->makeVertexSpace(sizeof(BezierVertex), vertexCount, &vertexBuffer,
1256 &firstVertex);
joshualitt7bc18b72015-02-03 16:41:41 -08001257
bsalomoned0bcad2015-05-04 10:36:42 -07001258 if (!vertices || !quadsIndexBuffer) {
joshualitt4b31de82015-03-05 14:33:41 -08001259 SkDebugf("Could not allocate vertices\n");
1260 return;
1261 }
1262
joshualitt7bc18b72015-02-03 16:41:41 -08001263 // Setup vertices
robertphillips44c31282015-09-03 12:58:48 -07001264 BezierVertex* bezVerts = reinterpret_cast<BezierVertex*>(vertices);
joshualitt7bc18b72015-02-03 16:41:41 -08001265
joshualitt7bc18b72015-02-03 16:41:41 -08001266 int unsubdivQuadCnt = quads.count() / 3;
1267 for (int i = 0; i < unsubdivQuadCnt; ++i) {
1268 SkASSERT(qSubdivs[i] >= 0);
robertphillips44c31282015-09-03 12:58:48 -07001269 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &bezVerts);
joshualitt7bc18b72015-02-03 16:41:41 -08001270 }
1271
1272 // Start Conics
1273 for (int i = 0; i < conicCount; ++i) {
robertphillips44c31282015-09-03 12:58:48 -07001274 add_conics(&conics[3*i], cWeights[i], toDevice, toSrc, &bezVerts);
joshualitt7bc18b72015-02-03 16:41:41 -08001275 }
1276
1277 if (quadCount > 0) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001278 SkASSERT(predictedPrograms & kQuad_Program);
1279 actualPrograms |= kQuad_Program;
Robert Phillips1b3c2672019-12-04 14:34:48 -05001280
Robert Phillips4f93c572020-03-18 08:13:53 -04001281 fMeshes[1] = target->allocMesh();
1282 fMeshes[1]->setIndexedPatterned(quadsIndexBuffer, kIdxsPerQuad, quadCount,
1283 kQuadsNumInIdxBuffer, vertexBuffer, kQuadNumVertices,
1284 firstVertex);
bsalomon342bfc22016-04-01 06:06:20 -07001285 firstVertex += quadCount * kQuadNumVertices;
joshualitt7bc18b72015-02-03 16:41:41 -08001286 }
1287
1288 if (conicCount > 0) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001289 SkASSERT(predictedPrograms & kConic_Program);
1290 actualPrograms |= kConic_Program;
Robert Phillips1b3c2672019-12-04 14:34:48 -05001291
Robert Phillips4f93c572020-03-18 08:13:53 -04001292 fMeshes[2] = target->allocMesh();
1293 fMeshes[2]->setIndexedPatterned(std::move(quadsIndexBuffer), kIdxsPerQuad, conicCount,
1294 kQuadsNumInIdxBuffer, std::move(vertexBuffer),
1295 kQuadNumVertices, firstVertex);
joshualitt7bc18b72015-02-03 16:41:41 -08001296 }
1297 }
Robert Phillips4f93c572020-03-18 08:13:53 -04001298
1299 // In DDL mode this will replace the predicted program requirements with the actual ones.
1300 // However, we will already have surfaced the predicted programs to the DDL.
1301 fCharacterization = actualPrograms;
joshualitt7bc18b72015-02-03 16:41:41 -08001302}
1303
Chris Dalton07cdcfc92019-02-26 11:13:22 -07001304void AAHairlineOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) {
Robert Phillips4f93c572020-03-18 08:13:53 -04001305 this->createProgramInfo(flushState);
Robert Phillips3968fcb2019-12-05 16:40:31 -05001306
Robert Phillips4f93c572020-03-18 08:13:53 -04001307 for (int i = 0; i < 3; ++i) {
1308 if (fProgramInfos[i] && fMeshes[i]) {
1309 flushState->bindPipelineAndScissorClip(*fProgramInfos[i], chainBounds);
Robert Phillips787fd9d2021-03-22 14:48:09 -04001310 flushState->bindTextures(fProgramInfos[i]->geomProc(), nullptr,
Robert Phillips4f93c572020-03-18 08:13:53 -04001311 fProgramInfos[i]->pipeline());
1312 flushState->drawMesh(*fMeshes[i]);
1313 }
1314 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -07001315}
1316
bsalomon0aff2fa2015-07-31 06:48:27 -07001317bool GrAAHairLinePathRenderer::onDrawPath(const DrawPathArgs& args) {
Robert Phillipsa92913e2021-07-12 16:31:52 -04001318 GR_AUDIT_TRAIL_AUTO_FRAME(args.fContext->priv().auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -07001319 "GrAAHairlinePathRenderer::onDrawPath");
John Stiles0fbc6a32021-06-04 14:40:57 -04001320 SkASSERT(args.fSurfaceDrawContext->numSamples() <= 1);
csmartdaltonecbc12b2016-06-08 10:08:43 -07001321
bsalomon8acedde2016-06-24 10:42:16 -07001322 SkPath path;
1323 args.fShape->asPath(&path);
Herb Derbyc76d4092020-10-07 16:46:15 -04001324 GrOp::Owner op =
Robert Phillips7c525e62018-06-12 10:11:12 -04001325 AAHairlineOp::Make(args.fContext, std::move(args.fPaint), *args.fViewMatrix, path,
Michael Ludwig7c12e282020-05-29 09:54:07 -04001326 args.fShape->style(), *args.fClipConservativeBounds,
1327 args.fUserStencilSettings);
John Stiles0fbc6a32021-06-04 14:40:57 -04001328 args.fSurfaceDrawContext->addDrawOp(args.fClip, std::move(op));
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001329 return true;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001330}
joshualitt40ded322015-05-02 07:07:17 -07001331
1332///////////////////////////////////////////////////////////////////////////////////////////////////
1333
Hal Canary6f6961e2017-01-31 13:50:44 -05001334#if GR_TEST_UTILS
joshualitt40ded322015-05-02 07:07:17 -07001335
Brian Salomona531f252017-07-07 13:29:28 -04001336GR_DRAW_OP_TEST_DEFINE(AAHairlineOp) {
joshualitt40ded322015-05-02 07:07:17 -07001337 SkMatrix viewMatrix = GrTest::TestMatrix(random);
John Stiles31954bf2020-08-07 17:35:54 -04001338 const SkPath& path = GrTest::TestPath(random);
joshualitt40ded322015-05-02 07:07:17 -07001339 SkIRect devClipBounds;
1340 devClipBounds.setEmpty();
Robert Phillips7c525e62018-06-12 10:11:12 -04001341 return AAHairlineOp::Make(context, std::move(paint), viewMatrix, path,
1342 GrStyle::SimpleHairline(), devClipBounds,
1343 GrGetRandomStencil(random, context));
joshualitt40ded322015-05-02 07:07:17 -07001344}
1345
1346#endif