blob: effd8c3b0ff935a6ba067b1efd1c2045fe9dc4c4 [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
bsalomon@google.comaeb21602011-08-30 18:13:44 +00008#include "GrAAHairLinePathRenderer.h"
9
bsalomon75398562015-08-17 12:55:38 -070010#include "GrBatchFlushState.h"
joshualitt40ded322015-05-02 07:07:17 -070011#include "GrBatchTest.h"
cdalton397536c2016-03-25 12:15:03 -070012#include "GrBuffer.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -070013#include "GrCaps.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000014#include "GrContext.h"
joshualitt5478d422014-11-14 16:00:38 -080015#include "GrDefaultGeoProcFactory.h"
bsalomon@google.comdbeeac32011-09-12 14:59:34 +000016#include "GrPathUtils.h"
egdaniel8dd688b2015-01-22 10:16:09 -080017#include "GrPipelineBuilder.h"
joshualitt5478d422014-11-14 16:00:38 -080018#include "GrProcessor.h"
bsalomoned0bcad2015-05-04 10:36:42 -070019#include "GrResourceProvider.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000020#include "SkGeometry.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000021#include "SkStroke.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000022#include "SkTemplates.h"
23
bsalomon16b99132015-08-13 14:55:50 -070024#include "batches/GrVertexBatch.h"
joshualitt74417822015-08-07 11:42:16 -070025
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000026#include "effects/GrBezierEffect.h"
bsalomon@google.com4647f902013-03-26 14:45:27 +000027
bsalomoned0bcad2015-05-04 10:36:42 -070028#define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true>
29
bsalomon@google.comaeb21602011-08-30 18:13:44 +000030// quadratics are rendered as 5-sided polys in order to bound the
31// AA stroke around the center-curve. See comments in push_quad_index_buffer and
egdaniel@google.com5383a752013-07-12 20:15:34 +000032// bloat_quad. Quadratics and conics share an index buffer
bsalomon@google.comaeb21602011-08-30 18:13:44 +000033
robertphillips@google.comada90da2013-09-18 22:14:49 +000034// lines are rendered as:
35// *______________*
36// |\ -_______ /|
37// | \ \ / |
38// | *--------* |
39// | / ______/ \ |
40// */_-__________\*
41// For: 6 vertices and 18 indices (for 6 triangles)
bsalomon@google.comaeb21602011-08-30 18:13:44 +000042
joshualitt5ead6da2014-10-22 16:00:29 -070043// Each quadratic is rendered as a five sided polygon. This poly bounds
44// the quadratic's bounding triangle but has been expanded so that the
45// 1-pixel wide area around the curve is inside the poly.
46// If a,b,c are the original control points then the poly a0,b0,c0,c1,a1
47// that is rendered would look like this:
48// b0
49// b
50//
51// a0 c0
52// a c
53// a1 c1
egdaniel14afb432014-12-22 10:57:08 -080054// Each is drawn as three triangles ((a0,a1,b0), (b0,c1,c0), (a1,c1,b0))
55// specified by these 9 indices:
joshualitt5ead6da2014-10-22 16:00:29 -070056static const uint16_t kQuadIdxBufPattern[] = {
57 0, 1, 2,
58 2, 4, 3,
59 1, 4, 2
60};
bsalomon@google.comaeb21602011-08-30 18:13:44 +000061
joshualitt5ead6da2014-10-22 16:00:29 -070062static const int kIdxsPerQuad = SK_ARRAY_COUNT(kQuadIdxBufPattern);
63static const int kQuadNumVertices = 5;
64static const int kQuadsNumInIdxBuffer = 256;
bsalomoned0bcad2015-05-04 10:36:42 -070065GR_DECLARE_STATIC_UNIQUE_KEY(gQuadsIndexBufferKey);
66
cdalton397536c2016-03-25 12:15:03 -070067static const GrBuffer* ref_quads_index_buffer(GrResourceProvider* resourceProvider) {
bsalomoned0bcad2015-05-04 10:36:42 -070068 GR_DEFINE_STATIC_UNIQUE_KEY(gQuadsIndexBufferKey);
bsalomoneae62002015-07-31 13:59:30 -070069 return resourceProvider->findOrCreateInstancedIndexBuffer(
bsalomoned0bcad2015-05-04 10:36:42 -070070 kQuadIdxBufPattern, kIdxsPerQuad, kQuadsNumInIdxBuffer, kQuadNumVertices,
71 gQuadsIndexBufferKey);
72}
jvanverth@google.com681ccf02013-08-16 14:51:51 +000073
bsalomon@google.comaeb21602011-08-30 18:13:44 +000074
joshualitt5ead6da2014-10-22 16:00:29 -070075// Each line segment is rendered as two quads and two triangles.
76// p0 and p1 have alpha = 1 while all other points have alpha = 0.
77// The four external points are offset 1 pixel perpendicular to the
78// line and half a pixel parallel to the line.
79//
80// p4 p5
81// p0 p1
82// p2 p3
83//
84// Each is drawn as six triangles specified by these 18 indices:
jvanverth@google.com681ccf02013-08-16 14:51:51 +000085
joshualitt5ead6da2014-10-22 16:00:29 -070086static const uint16_t kLineSegIdxBufPattern[] = {
87 0, 1, 3,
88 0, 3, 2,
89 0, 4, 5,
90 0, 5, 1,
91 0, 2, 4,
92 1, 5, 3
93};
skia.committer@gmail.com74758112013-08-17 07:01:54 +000094
joshualitt5ead6da2014-10-22 16:00:29 -070095static const int kIdxsPerLineSeg = SK_ARRAY_COUNT(kLineSegIdxBufPattern);
96static const int kLineSegNumVertices = 6;
97static const int kLineSegsNumInIdxBuffer = 256;
bsalomon@google.comaeb21602011-08-30 18:13:44 +000098
bsalomoned0bcad2015-05-04 10:36:42 -070099GR_DECLARE_STATIC_UNIQUE_KEY(gLinesIndexBufferKey);
100
cdalton397536c2016-03-25 12:15:03 -0700101static const GrBuffer* ref_lines_index_buffer(GrResourceProvider* resourceProvider) {
bsalomoned0bcad2015-05-04 10:36:42 -0700102 GR_DEFINE_STATIC_UNIQUE_KEY(gLinesIndexBufferKey);
bsalomoneae62002015-07-31 13:59:30 -0700103 return resourceProvider->findOrCreateInstancedIndexBuffer(
bsalomoned0bcad2015-05-04 10:36:42 -0700104 kLineSegIdxBufPattern, kIdxsPerLineSeg, kLineSegsNumInIdxBuffer, kLineSegNumVertices,
105 gLinesIndexBufferKey);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000106}
107
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000108// Takes 178th time of logf on Z600 / VC2010
bsalomoned0bcad2015-05-04 10:36:42 -0700109static int get_float_exp(float x) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000110 GR_STATIC_ASSERT(sizeof(int) == sizeof(float));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000111#ifdef SK_DEBUG
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000112 static bool tested;
113 if (!tested) {
114 tested = true;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000115 SkASSERT(get_float_exp(0.25f) == -2);
116 SkASSERT(get_float_exp(0.3f) == -2);
117 SkASSERT(get_float_exp(0.5f) == -1);
118 SkASSERT(get_float_exp(1.f) == 0);
119 SkASSERT(get_float_exp(2.f) == 1);
120 SkASSERT(get_float_exp(2.5f) == 1);
121 SkASSERT(get_float_exp(8.f) == 3);
122 SkASSERT(get_float_exp(100.f) == 6);
123 SkASSERT(get_float_exp(1000.f) == 9);
124 SkASSERT(get_float_exp(1024.f) == 10);
125 SkASSERT(get_float_exp(3000000.f) == 21);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000126 }
127#endif
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000128 const int* iptr = (const int*)&x;
129 return (((*iptr) & 0x7f800000) >> 23) - 127;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000130}
131
egdaniel@google.com5383a752013-07-12 20:15:34 +0000132// Uses the max curvature function for quads to estimate
133// where to chop the conic. If the max curvature is not
134// found along the curve segment it will return 1 and
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000135// dst[0] is the original conic. If it returns 2 the dst[0]
egdaniel@google.com5383a752013-07-12 20:15:34 +0000136// and dst[1] are the two new conics.
bsalomoned0bcad2015-05-04 10:36:42 -0700137static int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000138 SkScalar t = SkFindQuadMaxCurvature(src);
139 if (t == 0) {
140 if (dst) {
141 dst[0].set(src, weight);
142 }
143 return 1;
144 } else {
145 if (dst) {
146 SkConic conic;
147 conic.set(src, weight);
148 conic.chopAt(t, dst);
149 }
150 return 2;
151 }
152}
153
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000154// Calls split_conic on the entire conic and then once more on each subsection.
155// Most cases will result in either 1 conic (chop point is not within t range)
156// or 3 points (split once and then one subsection is split again).
bsalomoned0bcad2015-05-04 10:36:42 -0700157static int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000158 SkConic dstTemp[2];
159 int conicCnt = split_conic(src, dstTemp, weight);
160 if (2 == conicCnt) {
161 int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW);
162 conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW);
163 } else {
164 dst[0] = dstTemp[0];
165 }
166 return conicCnt;
167}
168
egdaniel@google.com5383a752013-07-12 20:15:34 +0000169// returns 0 if quad/conic is degen or close to it
170// in this case approx the path with lines
171// otherwise returns 1
bsalomoned0bcad2015-05-04 10:36:42 -0700172static int is_degen_quad_or_conic(const SkPoint p[3], SkScalar* dsqd) {
jvanverth876f48b2015-05-05 08:01:51 -0700173 static const SkScalar gDegenerateToLineTol = GrPathUtils::kDefaultTolerance;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000174 static const SkScalar gDegenerateToLineTolSqd =
175 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
176
177 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
178 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
179 return 1;
180 }
181
joshualitt63648072015-02-19 10:25:21 -0800182 *dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
183 if (*dsqd < gDegenerateToLineTolSqd) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000184 return 1;
185 }
186
187 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
188 return 1;
189 }
190 return 0;
191}
192
bsalomoned0bcad2015-05-04 10:36:42 -0700193static int is_degen_quad_or_conic(const SkPoint p[3]) {
joshualitt63648072015-02-19 10:25:21 -0800194 SkScalar dsqd;
195 return is_degen_quad_or_conic(p, &dsqd);
196}
197
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000198// we subdivide the quads to avoid huge overfill
199// if it returns -1 then should be drawn as lines
bsalomoned0bcad2015-05-04 10:36:42 -0700200static int num_quad_subdivs(const SkPoint p[3]) {
joshualitt63648072015-02-19 10:25:21 -0800201 SkScalar dsqd;
202 if (is_degen_quad_or_conic(p, &dsqd)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000203 return -1;
204 }
205
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000206 // tolerance of triangle height in pixels
207 // tuned on windows Quadro FX 380 / Z600
208 // trade off of fill vs cpu time on verts
209 // maybe different when do this using gpu (geo or tess shaders)
210 static const SkScalar gSubdivTol = 175 * SK_Scalar1;
211
robertphillips@google.com7460b372012-04-25 16:54:51 +0000212 if (dsqd <= SkScalarMul(gSubdivTol, gSubdivTol)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000213 return 0;
214 } else {
robertphillips@google.com87379e12013-03-29 12:11:10 +0000215 static const int kMaxSub = 4;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000216 // subdividing the quad reduces d by 4. so we want x = log4(d/tol)
217 // = log4(d*d/tol*tol)/2
218 // = log2(d*d/tol*tol)
219
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000220 // +1 since we're ignoring the mantissa contribution.
221 int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000222 log = SkTMin(SkTMax(0, log), kMaxSub);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000223 return log;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000224 }
225}
226
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000227/**
228 * Generates the lines and quads to be rendered. Lines are always recorded in
229 * device space. We will do a device space bloat to account for the 1pixel
230 * thickness.
231 * Quads are recorded in device space unless m contains
232 * perspective, then in they are in src space. We do this because we will
233 * subdivide large quads to reduce over-fill. This subdivision has to be
234 * performed before applying the perspective matrix.
235 */
bsalomoned0bcad2015-05-04 10:36:42 -0700236static int gather_lines_and_quads(const SkPath& path,
237 const SkMatrix& m,
238 const SkIRect& devClipBounds,
239 GrAAHairLinePathRenderer::PtArray* lines,
240 GrAAHairLinePathRenderer::PtArray* quads,
241 GrAAHairLinePathRenderer::PtArray* conics,
242 GrAAHairLinePathRenderer::IntArray* quadSubdivCnts,
243 GrAAHairLinePathRenderer::FloatArray* conicWeights) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000244 SkPath::Iter iter(path, false);
245
246 int totalQuadCount = 0;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000247 SkRect bounds;
248 SkIRect ibounds;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000249
250 bool persp = m.hasPerspective();
251
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000252 for (;;) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000253 SkPoint pathPts[4];
254 SkPoint devPts[4];
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000255 SkPath::Verb verb = iter.next(pathPts);
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000256 switch (verb) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000257 case SkPath::kConic_Verb: {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000258 SkConic dst[4];
259 // We chop the conics to create tighter clipping to hide error
260 // that appears near max curvature of very thin conics. Thin
261 // hyperbolas with high weight still show error.
egdaniel@google.com5383a752013-07-12 20:15:34 +0000262 int conicCnt = chop_conic(pathPts, dst, iter.conicWeight());
263 for (int i = 0; i < conicCnt; ++i) {
264 SkPoint* chopPnts = dst[i].fPts;
265 m.mapPoints(devPts, chopPnts, 3);
266 bounds.setBounds(devPts, 3);
267 bounds.outset(SK_Scalar1, SK_Scalar1);
268 bounds.roundOut(&ibounds);
269 if (SkIRect::Intersects(devClipBounds, ibounds)) {
270 if (is_degen_quad_or_conic(devPts)) {
271 SkPoint* pts = lines->push_back_n(4);
272 pts[0] = devPts[0];
273 pts[1] = devPts[1];
274 pts[2] = devPts[1];
275 pts[3] = devPts[2];
276 } else {
277 // when in perspective keep conics in src space
278 SkPoint* cPts = persp ? chopPnts : devPts;
279 SkPoint* pts = conics->push_back_n(3);
280 pts[0] = cPts[0];
281 pts[1] = cPts[1];
282 pts[2] = cPts[2];
283 conicWeights->push_back() = dst[i].fW;
284 }
285 }
286 }
reed@google.com277c3f82013-05-31 15:17:50 +0000287 break;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000288 }
289 case SkPath::kMove_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000290 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000291 case SkPath::kLine_Verb:
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000292 m.mapPoints(devPts, pathPts, 2);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000293 bounds.setBounds(devPts, 2);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000294 bounds.outset(SK_Scalar1, SK_Scalar1);
295 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000296 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000297 SkPoint* pts = lines->push_back_n(2);
298 pts[0] = devPts[0];
299 pts[1] = devPts[1];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000300 }
301 break;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000302 case SkPath::kQuad_Verb: {
303 SkPoint choppedPts[5];
304 // Chopping the quad helps when the quad is either degenerate or nearly degenerate.
305 // When it is degenerate it allows the approximation with lines to work since the
306 // chop point (if there is one) will be at the parabola's vertex. In the nearly
307 // degenerate the QuadUVMatrix computed for the points is almost singular which
308 // can cause rendering artifacts.
309 int n = SkChopQuadAtMaxCurvature(pathPts, choppedPts);
310 for (int i = 0; i < n; ++i) {
311 SkPoint* quadPts = choppedPts + i * 2;
312 m.mapPoints(devPts, quadPts, 3);
313 bounds.setBounds(devPts, 3);
314 bounds.outset(SK_Scalar1, SK_Scalar1);
315 bounds.roundOut(&ibounds);
316
317 if (SkIRect::Intersects(devClipBounds, ibounds)) {
318 int subdiv = num_quad_subdivs(devPts);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000319 SkASSERT(subdiv >= -1);
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000320 if (-1 == subdiv) {
321 SkPoint* pts = lines->push_back_n(4);
322 pts[0] = devPts[0];
323 pts[1] = devPts[1];
324 pts[2] = devPts[1];
325 pts[3] = devPts[2];
326 } else {
327 // when in perspective keep quads in src space
328 SkPoint* qPts = persp ? quadPts : devPts;
329 SkPoint* pts = quads->push_back_n(3);
330 pts[0] = qPts[0];
331 pts[1] = qPts[1];
332 pts[2] = qPts[2];
333 quadSubdivCnts->push_back() = subdiv;
334 totalQuadCount += 1 << subdiv;
335 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000336 }
337 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000338 break;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000339 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000340 case SkPath::kCubic_Verb:
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000341 m.mapPoints(devPts, pathPts, 4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000342 bounds.setBounds(devPts, 4);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000343 bounds.outset(SK_Scalar1, SK_Scalar1);
344 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000345 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000346 PREALLOC_PTARRAY(32) q;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000347 // We convert cubics to quadratics (for now).
348 // In perspective have to do conversion in src space.
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000349 if (persp) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000350 SkScalar tolScale =
bsalomon18fab302016-02-16 08:00:05 -0800351 GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m, path.getBounds());
352 GrPathUtils::convertCubicToQuads(pathPts, tolScale, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000353 } else {
bsalomon18fab302016-02-16 08:00:05 -0800354 GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000355 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000356 for (int i = 0; i < q.count(); i += 3) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000357 SkPoint* qInDevSpace;
358 // bounds has to be calculated in device space, but q is
359 // in src space when there is perspective.
360 if (persp) {
361 m.mapPoints(devPts, &q[i], 3);
362 bounds.setBounds(devPts, 3);
363 qInDevSpace = devPts;
364 } else {
365 bounds.setBounds(&q[i], 3);
366 qInDevSpace = &q[i];
367 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000368 bounds.outset(SK_Scalar1, SK_Scalar1);
369 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000370 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000371 int subdiv = num_quad_subdivs(qInDevSpace);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000372 SkASSERT(subdiv >= -1);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000373 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000374 SkPoint* pts = lines->push_back_n(4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000375 // lines should always be in device coords
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000376 pts[0] = qInDevSpace[0];
377 pts[1] = qInDevSpace[1];
378 pts[2] = qInDevSpace[1];
379 pts[3] = qInDevSpace[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000380 } else {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000381 SkPoint* pts = quads->push_back_n(3);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000382 // q is already in src space when there is no
383 // perspective and dev coords otherwise.
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000384 pts[0] = q[0 + i];
385 pts[1] = q[1 + i];
386 pts[2] = q[2 + i];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000387 quadSubdivCnts->push_back() = subdiv;
388 totalQuadCount += 1 << subdiv;
389 }
390 }
391 }
392 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000393 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000394 case SkPath::kClose_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000395 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000396 case SkPath::kDone_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000397 return totalQuadCount;
398 }
399 }
400}
401
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000402struct LineVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000403 SkPoint fPos;
egdaniele27065a2014-11-06 08:00:48 -0800404 float fCoverage;
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000405};
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000406
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000407struct BezierVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000408 SkPoint fPos;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000409 union {
410 struct {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000411 SkScalar fK;
412 SkScalar fL;
413 SkScalar fM;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000414 } fConic;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000415 SkVector fQuadCoord;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000416 struct {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000417 SkScalar fBogus[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000418 };
419 };
420};
egdaniel@google.com5383a752013-07-12 20:15:34 +0000421
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000422GR_STATIC_ASSERT(sizeof(BezierVertex) == 3 * sizeof(SkPoint));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000423
bsalomoned0bcad2015-05-04 10:36:42 -0700424static void intersect_lines(const SkPoint& ptA, const SkVector& normA,
425 const SkPoint& ptB, const SkVector& normB,
426 SkPoint* result) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000427
428 SkScalar lineAW = -normA.dot(ptA);
429 SkScalar lineBW = -normB.dot(ptB);
430
431 SkScalar wInv = SkScalarMul(normA.fX, normB.fY) -
egdaniel@google.com5383a752013-07-12 20:15:34 +0000432 SkScalarMul(normA.fY, normB.fX);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000433 wInv = SkScalarInvert(wInv);
434
435 result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY);
436 result->fX = SkScalarMul(result->fX, wInv);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000437
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000438 result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW);
439 result->fY = SkScalarMul(result->fY, wInv);
440}
441
bsalomoned0bcad2015-05-04 10:36:42 -0700442static void set_uv_quad(const SkPoint qpts[3], BezierVertex verts[kQuadNumVertices]) {
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000443 // this should be in the src space, not dev coords, when we have perspective
444 GrPathUtils::QuadUVMatrix DevToUV(qpts);
joshualitt5ead6da2014-10-22 16:00:29 -0700445 DevToUV.apply<kQuadNumVertices, sizeof(BezierVertex), sizeof(SkPoint)>(verts);
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000446}
447
bsalomoned0bcad2015-05-04 10:36:42 -0700448static void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice,
449 const SkMatrix* toSrc, BezierVertex verts[kQuadNumVertices]) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000450 SkASSERT(!toDevice == !toSrc);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000451 // original quad is specified by tri a,b,c
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000452 SkPoint a = qpts[0];
453 SkPoint b = qpts[1];
454 SkPoint c = qpts[2];
455
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000456 if (toDevice) {
457 toDevice->mapPoints(&a, 1);
458 toDevice->mapPoints(&b, 1);
459 toDevice->mapPoints(&c, 1);
460 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000461 // make a new poly where we replace a and c by a 1-pixel wide edges orthog
462 // to edges ab and bc:
463 //
464 // before | after
465 // | b0
466 // b |
467 // |
468 // | a0 c0
469 // a c | a1 c1
470 //
471 // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c,
472 // respectively.
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000473 BezierVertex& a0 = verts[0];
474 BezierVertex& a1 = verts[1];
475 BezierVertex& b0 = verts[2];
476 BezierVertex& c0 = verts[3];
477 BezierVertex& c1 = verts[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000478
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000479 SkVector ab = b;
480 ab -= a;
481 SkVector ac = c;
482 ac -= a;
483 SkVector cb = b;
484 cb -= c;
485
486 // We should have already handled degenerates
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000487 SkASSERT(ab.length() > 0 && cb.length() > 0);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000488
489 ab.normalize();
490 SkVector abN;
491 abN.setOrthog(ab, SkVector::kLeft_Side);
492 if (abN.dot(ac) > 0) {
493 abN.negate();
494 }
495
496 cb.normalize();
497 SkVector cbN;
498 cbN.setOrthog(cb, SkVector::kLeft_Side);
499 if (cbN.dot(ac) < 0) {
500 cbN.negate();
501 }
502
503 a0.fPos = a;
504 a0.fPos += abN;
505 a1.fPos = a;
506 a1.fPos -= abN;
507
508 c0.fPos = c;
509 c0.fPos += cbN;
510 c1.fPos = c;
511 c1.fPos -= cbN;
512
513 intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos);
514
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000515 if (toSrc) {
joshualitt5ead6da2014-10-22 16:00:29 -0700516 toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(BezierVertex), kQuadNumVertices);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000517 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000518}
519
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000520// Equations based off of Loop-Blinn Quadratic GPU Rendering
egdaniel@google.com5383a752013-07-12 20:15:34 +0000521// Input Parametric:
522// 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)
523// Output Implicit:
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000524// f(x, y, w) = f(P) = K^2 - LM
525// K = dot(k, P), L = dot(l, P), M = dot(m, P)
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000526// k, l, m are calculated in function GrPathUtils::getConicKLM
bsalomoned0bcad2015-05-04 10:36:42 -0700527static void set_conic_coeffs(const SkPoint p[3], BezierVertex verts[kQuadNumVertices],
528 const SkScalar weight) {
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000529 SkScalar klm[9];
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000530
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000531 GrPathUtils::getConicKLM(p, weight, klm);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000532
joshualitt5ead6da2014-10-22 16:00:29 -0700533 for (int i = 0; i < kQuadNumVertices; ++i) {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000534 const SkPoint pnt = verts[i].fPos;
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000535 verts[i].fConic.fK = pnt.fX * klm[0] + pnt.fY * klm[1] + klm[2];
536 verts[i].fConic.fL = pnt.fX * klm[3] + pnt.fY * klm[4] + klm[5];
537 verts[i].fConic.fM = pnt.fX * klm[6] + pnt.fY * klm[7] + klm[8];
egdaniel@google.com5383a752013-07-12 20:15:34 +0000538 }
539}
540
bsalomoned0bcad2015-05-04 10:36:42 -0700541static void add_conics(const SkPoint p[3],
542 const SkScalar weight,
543 const SkMatrix* toDevice,
544 const SkMatrix* toSrc,
545 BezierVertex** vert) {
joshualitt7bc18b72015-02-03 16:41:41 -0800546 bloat_quad(p, toDevice, toSrc, *vert);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000547 set_conic_coeffs(p, *vert, weight);
joshualitt5ead6da2014-10-22 16:00:29 -0700548 *vert += kQuadNumVertices;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000549}
550
bsalomoned0bcad2015-05-04 10:36:42 -0700551static void add_quads(const SkPoint p[3],
552 int subdiv,
553 const SkMatrix* toDevice,
554 const SkMatrix* toSrc,
555 BezierVertex** vert) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000556 SkASSERT(subdiv >= 0);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000557 if (subdiv) {
558 SkPoint newP[5];
559 SkChopQuadAtHalf(p, newP);
joshualitt7bc18b72015-02-03 16:41:41 -0800560 add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert);
561 add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000562 } else {
joshualitt7bc18b72015-02-03 16:41:41 -0800563 bloat_quad(p, toDevice, toSrc, *vert);
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000564 set_uv_quad(p, *vert);
joshualitt5ead6da2014-10-22 16:00:29 -0700565 *vert += kQuadNumVertices;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000566 }
567}
568
bsalomoned0bcad2015-05-04 10:36:42 -0700569static void add_line(const SkPoint p[2],
570 const SkMatrix* toSrc,
571 uint8_t coverage,
572 LineVertex** vert) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000573 const SkPoint& a = p[0];
574 const SkPoint& b = p[1];
575
robertphillips@google.comada90da2013-09-18 22:14:49 +0000576 SkVector ortho, vec = b;
577 vec -= a;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000578
robertphillips@google.comada90da2013-09-18 22:14:49 +0000579 if (vec.setLength(SK_ScalarHalf)) {
580 // Create a vector orthogonal to 'vec' and of unit length
581 ortho.fX = 2.0f * vec.fY;
582 ortho.fY = -2.0f * vec.fX;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000583
egdaniele27065a2014-11-06 08:00:48 -0800584 float floatCoverage = GrNormalizeByteToFloat(coverage);
585
robertphillips@google.comada90da2013-09-18 22:14:49 +0000586 (*vert)[0].fPos = a;
egdaniele27065a2014-11-06 08:00:48 -0800587 (*vert)[0].fCoverage = floatCoverage;
robertphillips@google.comada90da2013-09-18 22:14:49 +0000588 (*vert)[1].fPos = b;
egdaniele27065a2014-11-06 08:00:48 -0800589 (*vert)[1].fCoverage = floatCoverage;
robertphillips@google.comada90da2013-09-18 22:14:49 +0000590 (*vert)[2].fPos = a - vec + ortho;
591 (*vert)[2].fCoverage = 0;
592 (*vert)[3].fPos = b + vec + ortho;
593 (*vert)[3].fCoverage = 0;
594 (*vert)[4].fPos = a - vec - ortho;
595 (*vert)[4].fCoverage = 0;
596 (*vert)[5].fPos = b + vec - ortho;
597 (*vert)[5].fCoverage = 0;
598
bsalomon49f085d2014-09-05 13:34:00 -0700599 if (toSrc) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000600 toSrc->mapPointsWithStride(&(*vert)->fPos,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000601 sizeof(LineVertex),
joshualitt5ead6da2014-10-22 16:00:29 -0700602 kLineSegNumVertices);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000603 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000604 } else {
605 // just make it degenerate and likely offscreen
joshualitt5ead6da2014-10-22 16:00:29 -0700606 for (int i = 0; i < kLineSegNumVertices; ++i) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000607 (*vert)[i].fPos.set(SK_ScalarMax, SK_ScalarMax);
608 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000609 }
610
joshualitt5ead6da2014-10-22 16:00:29 -0700611 *vert += kLineSegNumVertices;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000612}
613
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000614///////////////////////////////////////////////////////////////////////////////
615
bsalomon0aff2fa2015-07-31 06:48:27 -0700616bool GrAAHairLinePathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
617 if (!args.fAntiAlias) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000618 return false;
619 }
620
halcanary96fcdcc2015-08-27 07:41:13 -0700621 if (!IsStrokeHairlineOrEquivalent(*args.fStroke, *args.fViewMatrix, nullptr)) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000622 return false;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000623 }
624
bsalomon0aff2fa2015-07-31 06:48:27 -0700625 if (SkPath::kLine_SegmentMask == args.fPath->getSegmentMasks() ||
robertphillipse7d4b2f2015-08-13 07:57:10 -0700626 args.fShaderCaps->shaderDerivativeSupport()) {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000627 return true;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000628 }
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000629 return false;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000630}
631
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000632template <class VertexType>
joshualitt8059eb92014-12-29 15:10:07 -0800633bool check_bounds(const SkMatrix& viewMatrix, const SkRect& devBounds, void* vertices, int vCount)
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000634{
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000635 SkRect tolDevBounds = devBounds;
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000636 // The bounds ought to be tight, but in perspective the below code runs the verts
637 // through the view matrix to get back to dev coords, which can introduce imprecision.
joshualitt8059eb92014-12-29 15:10:07 -0800638 if (viewMatrix.hasPerspective()) {
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000639 tolDevBounds.outset(SK_Scalar1 / 1000, SK_Scalar1 / 1000);
640 } else {
641 // Non-persp matrices cause this path renderer to draw in device space.
joshualitt8059eb92014-12-29 15:10:07 -0800642 SkASSERT(viewMatrix.isIdentity());
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000643 }
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000644 SkRect actualBounds;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000645
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000646 VertexType* verts = reinterpret_cast<VertexType*>(vertices);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000647 bool first = true;
648 for (int i = 0; i < vCount; ++i) {
649 SkPoint pos = verts[i].fPos;
650 // This is a hack to workaround the fact that we move some degenerate segments offscreen.
651 if (SK_ScalarMax == pos.fX) {
652 continue;
653 }
joshualitt8059eb92014-12-29 15:10:07 -0800654 viewMatrix.mapPoints(&pos, 1);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000655 if (first) {
656 actualBounds.set(pos.fX, pos.fY, pos.fX, pos.fY);
657 first = false;
658 } else {
659 actualBounds.growToInclude(pos.fX, pos.fY);
660 }
661 }
662 if (!first) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000663 return tolDevBounds.contains(actualBounds);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000664 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000665
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000666 return true;
667}
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000668
bsalomonabd30f52015-08-13 13:34:48 -0700669class AAHairlineBatch : public GrVertexBatch {
joshualitt7bc18b72015-02-03 16:41:41 -0800670public:
reed1b55a962015-09-17 20:16:13 -0700671 DEFINE_BATCH_CLASS_ID
672
joshualitt7bc18b72015-02-03 16:41:41 -0800673 struct Geometry {
674 GrColor fColor;
675 uint8_t fCoverage;
676 SkMatrix fViewMatrix;
677 SkPath fPath;
joshualitt7bc18b72015-02-03 16:41:41 -0800678 SkIRect fDevClipBounds;
679 };
680
halcanary385fe4d2015-08-26 13:07:48 -0700681 static GrDrawBatch* Create(const Geometry& geometry) { return new AAHairlineBatch(geometry); }
joshualitt7bc18b72015-02-03 16:41:41 -0800682
mtklein36352bf2015-03-25 18:17:31 -0700683 const char* name() const override { return "AAHairlineBatch"; }
joshualitt7bc18b72015-02-03 16:41:41 -0800684
halcanary9d524f22016-03-29 09:03:52 -0700685 void computePipelineOptimizations(GrInitInvariantOutput* color,
ethannicholasff210322015-11-24 12:10:10 -0800686 GrInitInvariantOutput* coverage,
687 GrBatchToXPOverrides* overrides) const override {
joshualitt7bc18b72015-02-03 16:41:41 -0800688 // When this is called on a batch, there is only one geometry bundle
ethannicholasff210322015-11-24 12:10:10 -0800689 color->setKnownFourComponents(fGeoData[0].fColor);
690 coverage->setUnknownSingleComponent();
joshualitt7bc18b72015-02-03 16:41:41 -0800691 }
692
bsalomone46f9fe2015-08-18 06:05:14 -0700693private:
ethannicholasff210322015-11-24 12:10:10 -0800694 void initBatchTracker(const GrXPOverridesForBatch& overrides) override {
joshualitt7bc18b72015-02-03 16:41:41 -0800695 // Handle any color overrides
ethannicholasff210322015-11-24 12:10:10 -0800696 if (!overrides.readsColor()) {
joshualitt7bc18b72015-02-03 16:41:41 -0800697 fGeoData[0].fColor = GrColor_ILLEGAL;
joshualitt7bc18b72015-02-03 16:41:41 -0800698 }
ethannicholasff210322015-11-24 12:10:10 -0800699 overrides.getOverrideColorIfSet(&fGeoData[0].fColor);
joshualitt7bc18b72015-02-03 16:41:41 -0800700
701 // setup batch properties
ethannicholasff210322015-11-24 12:10:10 -0800702 fBatch.fColorIgnored = !overrides.readsColor();
joshualitt7bc18b72015-02-03 16:41:41 -0800703 fBatch.fColor = fGeoData[0].fColor;
ethannicholasff210322015-11-24 12:10:10 -0800704 fBatch.fUsesLocalCoords = overrides.readsLocalCoords();
705 fBatch.fCoverageIgnored = !overrides.readsCoverage();
joshualitt7bc18b72015-02-03 16:41:41 -0800706 fBatch.fCoverage = fGeoData[0].fCoverage;
joshualitt7bc18b72015-02-03 16:41:41 -0800707 }
708
joshualitt7bc18b72015-02-03 16:41:41 -0800709 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
710
joshualitt144c3c82015-11-30 12:30:13 -0800711 void onPrepareDraws(Target*) const override;
bsalomon75398562015-08-17 12:55:38 -0700712
joshualitt7bc18b72015-02-03 16:41:41 -0800713 typedef SkTArray<SkPoint, true> PtArray;
714 typedef SkTArray<int, true> IntArray;
715 typedef SkTArray<float, true> FloatArray;
716
reed1b55a962015-09-17 20:16:13 -0700717 AAHairlineBatch(const Geometry& geometry) : INHERITED(ClassID()) {
joshualitt7bc18b72015-02-03 16:41:41 -0800718 fGeoData.push_back(geometry);
joshualitt99c7c072015-05-01 13:43:30 -0700719
720 // compute bounds
721 fBounds = geometry.fPath.getBounds();
722 geometry.fViewMatrix.mapRect(&fBounds);
joshualitt4c977862015-05-13 08:00:56 -0700723
724 // This is b.c. hairlines are notionally infinitely thin so without expansion
725 // two overlapping lines could be reordered even though they hit the same pixels.
726 fBounds.outset(0.5f, 0.5f);
joshualitt7bc18b72015-02-03 16:41:41 -0800727 }
728
bsalomoncb02b382015-08-12 11:14:50 -0700729 bool onCombineIfPossible(GrBatch* t, const GrCaps& caps) override {
bsalomonabd30f52015-08-13 13:34:48 -0700730 AAHairlineBatch* that = t->cast<AAHairlineBatch>();
731
732 if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(),
733 that->bounds(), caps)) {
joshualitt8cab9a72015-07-16 09:13:50 -0700734 return false;
735 }
736
joshualitt7bc18b72015-02-03 16:41:41 -0800737 if (this->viewMatrix().hasPerspective() != that->viewMatrix().hasPerspective()) {
738 return false;
739 }
740
741 // We go to identity if we don't have perspective
742 if (this->viewMatrix().hasPerspective() &&
743 !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
744 return false;
745 }
746
747 // TODO we can actually batch hairlines if they are the same color in a kind of bulk method
748 // but we haven't implemented this yet
749 // TODO investigate going to vertex color and coverage?
750 if (this->coverage() != that->coverage()) {
751 return false;
752 }
753
754 if (this->color() != that->color()) {
755 return false;
756 }
757
758 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
759 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
760 return false;
761 }
762
763 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
joshualitt99c7c072015-05-01 13:43:30 -0700764 this->joinBounds(that->bounds());
joshualitt7bc18b72015-02-03 16:41:41 -0800765 return true;
766 }
767
768 GrColor color() const { return fBatch.fColor; }
769 uint8_t coverage() const { return fBatch.fCoverage; }
770 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
771 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
joshualittb8c241a2015-05-19 08:23:30 -0700772 bool coverageIgnored() const { return fBatch.fCoverageIgnored; }
joshualitt7bc18b72015-02-03 16:41:41 -0800773
774 struct BatchTracker {
775 GrColor fColor;
776 uint8_t fCoverage;
777 SkRect fDevBounds;
778 bool fUsesLocalCoords;
779 bool fColorIgnored;
780 bool fCoverageIgnored;
781 };
782
joshualitt7bc18b72015-02-03 16:41:41 -0800783 BatchTracker fBatch;
784 SkSTArray<1, Geometry, true> fGeoData;
reed1b55a962015-09-17 20:16:13 -0700785
786 typedef GrVertexBatch INHERITED;
joshualitt7bc18b72015-02-03 16:41:41 -0800787};
788
joshualitt144c3c82015-11-30 12:30:13 -0800789void AAHairlineBatch::onPrepareDraws(Target* target) const {
joshualitt7bc18b72015-02-03 16:41:41 -0800790 // Setup the viewmatrix and localmatrix for the GrGeometryProcessor.
791 SkMatrix invert;
792 if (!this->viewMatrix().invert(&invert)) {
793 return;
794 }
795
796 // we will transform to identity space if the viewmatrix does not have perspective
797 bool hasPerspective = this->viewMatrix().hasPerspective();
798 const SkMatrix* geometryProcessorViewM = &SkMatrix::I();
799 const SkMatrix* geometryProcessorLocalM = &invert;
halcanary96fcdcc2015-08-27 07:41:13 -0700800 const SkMatrix* toDevice = nullptr;
801 const SkMatrix* toSrc = nullptr;
joshualitt7bc18b72015-02-03 16:41:41 -0800802 if (hasPerspective) {
803 geometryProcessorViewM = &this->viewMatrix();
804 geometryProcessorLocalM = &SkMatrix::I();
805 toDevice = &this->viewMatrix();
806 toSrc = &invert;
807 }
808
joshualitt7bc18b72015-02-03 16:41:41 -0800809 // This is hand inlined for maximum performance.
810 PREALLOC_PTARRAY(128) lines;
811 PREALLOC_PTARRAY(128) quads;
812 PREALLOC_PTARRAY(128) conics;
813 IntArray qSubdivs;
814 FloatArray cWeights;
joshualitt351ba1b2015-02-16 08:33:19 -0800815 int quadCount = 0;
joshualitt7bc18b72015-02-03 16:41:41 -0800816
817 int instanceCount = fGeoData.count();
818 for (int i = 0; i < instanceCount; i++) {
819 const Geometry& args = fGeoData[i];
joshualitt351ba1b2015-02-16 08:33:19 -0800820 quadCount += gather_lines_and_quads(args.fPath, args.fViewMatrix, args.fDevClipBounds,
821 &lines, &quads, &conics, &qSubdivs, &cWeights);
joshualitt7bc18b72015-02-03 16:41:41 -0800822 }
823
joshualitt7bc18b72015-02-03 16:41:41 -0800824 int lineCount = lines.count() / 2;
825 int conicCount = conics.count() / 3;
826
827 // do lines first
828 if (lineCount) {
bsalomon342bfc22016-04-01 06:06:20 -0700829 SkAutoTUnref<const GrGeometryProcessor> lineGP;
830 {
831 using namespace GrDefaultGeoProcFactory;
832
833 Color color(this->color());
834 Coverage coverage(Coverage::kAttribute_Type);
835 LocalCoords localCoords(this->usesLocalCoords() ? LocalCoords::kUsePosition_Type :
836 LocalCoords::kUnused_Type);
837 localCoords.fMatrix = geometryProcessorLocalM;
838 lineGP.reset(GrDefaultGeoProcFactory::Create(color, coverage, localCoords,
839 *geometryProcessorViewM));
840 }
841
cdalton397536c2016-03-25 12:15:03 -0700842 SkAutoTUnref<const GrBuffer> linesIndexBuffer(
bsalomon75398562015-08-17 12:55:38 -0700843 ref_lines_index_buffer(target->resourceProvider()));
joshualitt7bc18b72015-02-03 16:41:41 -0800844
cdalton397536c2016-03-25 12:15:03 -0700845 const GrBuffer* vertexBuffer;
joshualitt7bc18b72015-02-03 16:41:41 -0800846 int firstVertex;
847
848 size_t vertexStride = lineGP->getVertexStride();
849 int vertexCount = kLineSegNumVertices * lineCount;
bsalomone64eb572015-05-07 11:35:55 -0700850 LineVertex* verts = reinterpret_cast<LineVertex*>(
bsalomon75398562015-08-17 12:55:38 -0700851 target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, &firstVertex));
joshualitt7bc18b72015-02-03 16:41:41 -0800852
bsalomone64eb572015-05-07 11:35:55 -0700853 if (!verts|| !linesIndexBuffer) {
joshualitt4b31de82015-03-05 14:33:41 -0800854 SkDebugf("Could not allocate vertices\n");
855 return;
856 }
857
joshualitt7bc18b72015-02-03 16:41:41 -0800858 SkASSERT(lineGP->getVertexStride() == sizeof(LineVertex));
859
joshualitt7bc18b72015-02-03 16:41:41 -0800860 for (int i = 0; i < lineCount; ++i) {
861 add_line(&lines[2*i], toSrc, this->coverage(), &verts);
862 }
863
bsalomon342bfc22016-04-01 06:06:20 -0700864 GrMesh mesh;
865 mesh.initInstanced(kTriangles_GrPrimitiveType, vertexBuffer, linesIndexBuffer,
866 firstVertex, kLineSegNumVertices, kIdxsPerLineSeg, lineCount,
867 kLineSegsNumInIdxBuffer);
868 target->draw(lineGP, mesh);
joshualitt7bc18b72015-02-03 16:41:41 -0800869 }
870
871 if (quadCount || conicCount) {
bsalomon342bfc22016-04-01 06:06:20 -0700872 SkAutoTUnref<const GrGeometryProcessor> quadGP(
873 GrQuadEffect::Create(this->color(),
874 *geometryProcessorViewM,
875 kHairlineAA_GrProcessorEdgeType,
876 target->caps(),
877 *geometryProcessorLocalM,
878 this->usesLocalCoords(),
879 this->coverage()));
880
881 SkAutoTUnref<const GrGeometryProcessor> conicGP(
882 GrConicEffect::Create(this->color(),
883 *geometryProcessorViewM,
884 kHairlineAA_GrProcessorEdgeType,
885 target->caps(),
886 *geometryProcessorLocalM,
887 this->usesLocalCoords(),
888 this->coverage()));
889
cdalton397536c2016-03-25 12:15:03 -0700890 const GrBuffer* vertexBuffer;
joshualitt7bc18b72015-02-03 16:41:41 -0800891 int firstVertex;
892
cdalton397536c2016-03-25 12:15:03 -0700893 SkAutoTUnref<const GrBuffer> quadsIndexBuffer(
bsalomon75398562015-08-17 12:55:38 -0700894 ref_quads_index_buffer(target->resourceProvider()));
bsalomoned0bcad2015-05-04 10:36:42 -0700895
joshualitt7bc18b72015-02-03 16:41:41 -0800896 size_t vertexStride = sizeof(BezierVertex);
897 int vertexCount = kQuadNumVertices * quadCount + kQuadNumVertices * conicCount;
bsalomon75398562015-08-17 12:55:38 -0700898 void *vertices = target->makeVertexSpace(vertexStride, vertexCount,
899 &vertexBuffer, &firstVertex);
joshualitt7bc18b72015-02-03 16:41:41 -0800900
bsalomoned0bcad2015-05-04 10:36:42 -0700901 if (!vertices || !quadsIndexBuffer) {
joshualitt4b31de82015-03-05 14:33:41 -0800902 SkDebugf("Could not allocate vertices\n");
903 return;
904 }
905
joshualitt7bc18b72015-02-03 16:41:41 -0800906 // Setup vertices
robertphillips44c31282015-09-03 12:58:48 -0700907 BezierVertex* bezVerts = reinterpret_cast<BezierVertex*>(vertices);
joshualitt7bc18b72015-02-03 16:41:41 -0800908
joshualitt7bc18b72015-02-03 16:41:41 -0800909 int unsubdivQuadCnt = quads.count() / 3;
910 for (int i = 0; i < unsubdivQuadCnt; ++i) {
911 SkASSERT(qSubdivs[i] >= 0);
robertphillips44c31282015-09-03 12:58:48 -0700912 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &bezVerts);
joshualitt7bc18b72015-02-03 16:41:41 -0800913 }
914
915 // Start Conics
916 for (int i = 0; i < conicCount; ++i) {
robertphillips44c31282015-09-03 12:58:48 -0700917 add_conics(&conics[3*i], cWeights[i], toDevice, toSrc, &bezVerts);
joshualitt7bc18b72015-02-03 16:41:41 -0800918 }
919
920 if (quadCount > 0) {
bsalomon342bfc22016-04-01 06:06:20 -0700921 GrMesh mesh;
922 mesh.initInstanced(kTriangles_GrPrimitiveType, vertexBuffer, quadsIndexBuffer,
923 firstVertex, kQuadNumVertices, kIdxsPerQuad, quadCount,
924 kQuadsNumInIdxBuffer);
925 target->draw(quadGP, mesh);
926 firstVertex += quadCount * kQuadNumVertices;
joshualitt7bc18b72015-02-03 16:41:41 -0800927 }
928
929 if (conicCount > 0) {
bsalomon342bfc22016-04-01 06:06:20 -0700930 GrMesh mesh;
931 mesh.initInstanced(kTriangles_GrPrimitiveType, vertexBuffer, quadsIndexBuffer,
932 firstVertex, kQuadNumVertices, kIdxsPerQuad, conicCount,
933 kQuadsNumInIdxBuffer);
934 target->draw(conicGP, mesh);
joshualitt7bc18b72015-02-03 16:41:41 -0800935 }
936 }
937}
938
bsalomonabd30f52015-08-13 13:34:48 -0700939static GrDrawBatch* create_hairline_batch(GrColor color,
940 const SkMatrix& viewMatrix,
941 const SkPath& path,
942 const GrStrokeInfo& stroke,
943 const SkIRect& devClipBounds) {
joshualitt40ded322015-05-02 07:07:17 -0700944 SkScalar hairlineCoverage;
945 uint8_t newCoverage = 0xff;
946 if (GrPathRenderer::IsStrokeHairlineOrEquivalent(stroke, viewMatrix, &hairlineCoverage)) {
947 newCoverage = SkScalarRoundToInt(hairlineCoverage * 0xff);
948 }
949
950 AAHairlineBatch::Geometry geometry;
951 geometry.fColor = color;
952 geometry.fCoverage = newCoverage;
953 geometry.fViewMatrix = viewMatrix;
954 geometry.fPath = path;
955 geometry.fDevClipBounds = devClipBounds;
956
bsalomoned0bcad2015-05-04 10:36:42 -0700957 return AAHairlineBatch::Create(geometry);
joshualitt40ded322015-05-02 07:07:17 -0700958}
959
bsalomon0aff2fa2015-07-31 06:48:27 -0700960bool GrAAHairLinePathRenderer::onDrawPath(const DrawPathArgs& args) {
joshualittde83b412016-01-14 09:58:36 -0800961 GR_AUDIT_TRAIL_AUTO_FRAME(args.fTarget->getAuditTrail(),"GrAAHairlinePathRenderer::onDrawPath");
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000962 SkIRect devClipBounds;
robertphillips7bceedc2015-12-01 12:51:26 -0800963 GrRenderTarget* rt = args.fPipelineBuilder->getRenderTarget();
964 args.fPipelineBuilder->clip().getConservativeBounds(rt->width(), rt->height(), &devClipBounds);
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000965
bsalomonabd30f52015-08-13 13:34:48 -0700966 SkAutoTUnref<GrDrawBatch> batch(create_hairline_batch(args.fColor, *args.fViewMatrix, *args.fPath,
967 *args.fStroke, devClipBounds));
bsalomon0aff2fa2015-07-31 06:48:27 -0700968 args.fTarget->drawBatch(*args.fPipelineBuilder, batch);
bsalomon@google.com4647f902013-03-26 14:45:27 +0000969
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000970 return true;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000971}
joshualitt40ded322015-05-02 07:07:17 -0700972
973///////////////////////////////////////////////////////////////////////////////////////////////////
974
975#ifdef GR_TEST_UTILS
976
bsalomonabd30f52015-08-13 13:34:48 -0700977DRAW_BATCH_TEST_DEFINE(AAHairlineBatch) {
joshualitt40ded322015-05-02 07:07:17 -0700978 GrColor color = GrRandomColor(random);
979 SkMatrix viewMatrix = GrTest::TestMatrix(random);
980 GrStrokeInfo stroke(SkStrokeRec::kHairline_InitStyle);
981 SkPath path = GrTest::TestPath(random);
982 SkIRect devClipBounds;
983 devClipBounds.setEmpty();
bsalomoned0bcad2015-05-04 10:36:42 -0700984 return create_hairline_batch(color, viewMatrix, path, stroke, devClipBounds);
joshualitt40ded322015-05-02 07:07:17 -0700985}
986
987#endif