blob: 6ecaa2eba4653f99d81aaff087c07b4faaee56c8 [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
joshualitt6eff8702015-02-02 17:19:40 -080010#include "GrBatch.h"
11#include "GrBatchTarget.h"
12#include "GrBufferAllocPool.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000013#include "GrContext.h"
joshualitt5478d422014-11-14 16:00:38 -080014#include "GrDefaultGeoProcFactory.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000015#include "GrDrawTargetCaps.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000016#include "GrGpu.h"
17#include "GrIndexBuffer.h"
bsalomon@google.comdbeeac32011-09-12 14:59:34 +000018#include "GrPathUtils.h"
egdaniel8dd688b2015-01-22 10:16:09 -080019#include "GrPipelineBuilder.h"
joshualitt5478d422014-11-14 16:00:38 -080020#include "GrProcessor.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000021#include "SkGeometry.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000022#include "SkStroke.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000023#include "SkTemplates.h"
24
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000025#include "effects/GrBezierEffect.h"
bsalomon@google.com4647f902013-03-26 14:45:27 +000026
bsalomon@google.comaeb21602011-08-30 18:13:44 +000027// quadratics are rendered as 5-sided polys in order to bound the
28// AA stroke around the center-curve. See comments in push_quad_index_buffer and
egdaniel@google.com5383a752013-07-12 20:15:34 +000029// bloat_quad. Quadratics and conics share an index buffer
bsalomon@google.comaeb21602011-08-30 18:13:44 +000030
robertphillips@google.comada90da2013-09-18 22:14:49 +000031// lines are rendered as:
32// *______________*
33// |\ -_______ /|
34// | \ \ / |
35// | *--------* |
36// | / ______/ \ |
37// */_-__________\*
38// For: 6 vertices and 18 indices (for 6 triangles)
bsalomon@google.comaeb21602011-08-30 18:13:44 +000039
joshualitt5ead6da2014-10-22 16:00:29 -070040// Each quadratic is rendered as a five sided polygon. This poly bounds
41// the quadratic's bounding triangle but has been expanded so that the
42// 1-pixel wide area around the curve is inside the poly.
43// If a,b,c are the original control points then the poly a0,b0,c0,c1,a1
44// that is rendered would look like this:
45// b0
46// b
47//
48// a0 c0
49// a c
50// a1 c1
egdaniel14afb432014-12-22 10:57:08 -080051// Each is drawn as three triangles ((a0,a1,b0), (b0,c1,c0), (a1,c1,b0))
52// specified by these 9 indices:
joshualitt5ead6da2014-10-22 16:00:29 -070053static const uint16_t kQuadIdxBufPattern[] = {
54 0, 1, 2,
55 2, 4, 3,
56 1, 4, 2
57};
bsalomon@google.comaeb21602011-08-30 18:13:44 +000058
joshualitt5ead6da2014-10-22 16:00:29 -070059static const int kIdxsPerQuad = SK_ARRAY_COUNT(kQuadIdxBufPattern);
60static const int kQuadNumVertices = 5;
61static const int kQuadsNumInIdxBuffer = 256;
jvanverth@google.com681ccf02013-08-16 14:51:51 +000062
bsalomon@google.comaeb21602011-08-30 18:13:44 +000063
joshualitt5ead6da2014-10-22 16:00:29 -070064// Each line segment is rendered as two quads and two triangles.
65// p0 and p1 have alpha = 1 while all other points have alpha = 0.
66// The four external points are offset 1 pixel perpendicular to the
67// line and half a pixel parallel to the line.
68//
69// p4 p5
70// p0 p1
71// p2 p3
72//
73// Each is drawn as six triangles specified by these 18 indices:
jvanverth@google.com681ccf02013-08-16 14:51:51 +000074
joshualitt5ead6da2014-10-22 16:00:29 -070075static const uint16_t kLineSegIdxBufPattern[] = {
76 0, 1, 3,
77 0, 3, 2,
78 0, 4, 5,
79 0, 5, 1,
80 0, 2, 4,
81 1, 5, 3
82};
skia.committer@gmail.com74758112013-08-17 07:01:54 +000083
joshualitt5ead6da2014-10-22 16:00:29 -070084static const int kIdxsPerLineSeg = SK_ARRAY_COUNT(kLineSegIdxBufPattern);
85static const int kLineSegNumVertices = 6;
86static const int kLineSegsNumInIdxBuffer = 256;
bsalomon@google.comaeb21602011-08-30 18:13:44 +000087
88GrPathRenderer* GrAAHairLinePathRenderer::Create(GrContext* context) {
bsalomon@google.coma8a6a322011-09-23 14:19:58 +000089 GrGpu* gpu = context->getGpu();
joshualitt5ead6da2014-10-22 16:00:29 -070090 GrIndexBuffer* qIdxBuf = gpu->createInstancedIndexBuffer(kQuadIdxBufPattern,
91 kIdxsPerQuad,
92 kQuadsNumInIdxBuffer,
93 kQuadNumVertices);
bsalomon@google.coma8a6a322011-09-23 14:19:58 +000094 SkAutoTUnref<GrIndexBuffer> qIdxBuffer(qIdxBuf);
joshualitt5ead6da2014-10-22 16:00:29 -070095 GrIndexBuffer* lIdxBuf = gpu->createInstancedIndexBuffer(kLineSegIdxBufPattern,
96 kIdxsPerLineSeg,
97 kLineSegsNumInIdxBuffer,
98 kLineSegNumVertices);
jvanverth@google.com681ccf02013-08-16 14:51:51 +000099 SkAutoTUnref<GrIndexBuffer> lIdxBuffer(lIdxBuf);
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000100 return SkNEW_ARGS(GrAAHairLinePathRenderer,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000101 (context, lIdxBuf, qIdxBuf));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000102}
103
104GrAAHairLinePathRenderer::GrAAHairLinePathRenderer(
105 const GrContext* context,
106 const GrIndexBuffer* linesIndexBuffer,
107 const GrIndexBuffer* quadsIndexBuffer) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000108 fLinesIndexBuffer = linesIndexBuffer;
109 linesIndexBuffer->ref();
110 fQuadsIndexBuffer = quadsIndexBuffer;
111 quadsIndexBuffer->ref();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000112}
113
114GrAAHairLinePathRenderer::~GrAAHairLinePathRenderer() {
115 fLinesIndexBuffer->unref();
116 fQuadsIndexBuffer->unref();
117}
118
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000119namespace {
120
bsalomon@google.com92669012011-09-27 19:10:05 +0000121#define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true>
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000122
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000123// Takes 178th time of logf on Z600 / VC2010
124int get_float_exp(float x) {
125 GR_STATIC_ASSERT(sizeof(int) == sizeof(float));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000126#ifdef SK_DEBUG
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000127 static bool tested;
128 if (!tested) {
129 tested = true;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000130 SkASSERT(get_float_exp(0.25f) == -2);
131 SkASSERT(get_float_exp(0.3f) == -2);
132 SkASSERT(get_float_exp(0.5f) == -1);
133 SkASSERT(get_float_exp(1.f) == 0);
134 SkASSERT(get_float_exp(2.f) == 1);
135 SkASSERT(get_float_exp(2.5f) == 1);
136 SkASSERT(get_float_exp(8.f) == 3);
137 SkASSERT(get_float_exp(100.f) == 6);
138 SkASSERT(get_float_exp(1000.f) == 9);
139 SkASSERT(get_float_exp(1024.f) == 10);
140 SkASSERT(get_float_exp(3000000.f) == 21);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000141 }
142#endif
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000143 const int* iptr = (const int*)&x;
144 return (((*iptr) & 0x7f800000) >> 23) - 127;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000145}
146
egdaniel@google.com5383a752013-07-12 20:15:34 +0000147// Uses the max curvature function for quads to estimate
148// where to chop the conic. If the max curvature is not
149// found along the curve segment it will return 1 and
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000150// dst[0] is the original conic. If it returns 2 the dst[0]
egdaniel@google.com5383a752013-07-12 20:15:34 +0000151// and dst[1] are the two new conics.
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000152int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000153 SkScalar t = SkFindQuadMaxCurvature(src);
154 if (t == 0) {
155 if (dst) {
156 dst[0].set(src, weight);
157 }
158 return 1;
159 } else {
160 if (dst) {
161 SkConic conic;
162 conic.set(src, weight);
163 conic.chopAt(t, dst);
164 }
165 return 2;
166 }
167}
168
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000169// Calls split_conic on the entire conic and then once more on each subsection.
170// Most cases will result in either 1 conic (chop point is not within t range)
171// or 3 points (split once and then one subsection is split again).
172int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) {
173 SkConic dstTemp[2];
174 int conicCnt = split_conic(src, dstTemp, weight);
175 if (2 == conicCnt) {
176 int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW);
177 conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW);
178 } else {
179 dst[0] = dstTemp[0];
180 }
181 return conicCnt;
182}
183
egdaniel@google.com5383a752013-07-12 20:15:34 +0000184// returns 0 if quad/conic is degen or close to it
185// in this case approx the path with lines
186// otherwise returns 1
187int is_degen_quad_or_conic(const SkPoint p[3]) {
188 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
189 static const SkScalar gDegenerateToLineTolSqd =
190 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
191
192 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
193 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
194 return 1;
195 }
196
197 SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
198 if (dsqd < gDegenerateToLineTolSqd) {
199 return 1;
200 }
201
202 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
203 return 1;
204 }
205 return 0;
206}
207
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000208// we subdivide the quads to avoid huge overfill
209// if it returns -1 then should be drawn as lines
210int num_quad_subdivs(const SkPoint p[3]) {
211 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000212 static const SkScalar gDegenerateToLineTolSqd =
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000213 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000214
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000215 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
216 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000217 return -1;
218 }
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000219
bsalomon@google.com81712882012-11-01 17:12:34 +0000220 SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000221 if (dsqd < gDegenerateToLineTolSqd) {
222 return -1;
223 }
224
225 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000226 return -1;
227 }
228
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000229 // tolerance of triangle height in pixels
230 // tuned on windows Quadro FX 380 / Z600
231 // trade off of fill vs cpu time on verts
232 // maybe different when do this using gpu (geo or tess shaders)
233 static const SkScalar gSubdivTol = 175 * SK_Scalar1;
234
robertphillips@google.com7460b372012-04-25 16:54:51 +0000235 if (dsqd <= SkScalarMul(gSubdivTol, gSubdivTol)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000236 return 0;
237 } else {
robertphillips@google.com87379e12013-03-29 12:11:10 +0000238 static const int kMaxSub = 4;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000239 // subdividing the quad reduces d by 4. so we want x = log4(d/tol)
240 // = log4(d*d/tol*tol)/2
241 // = log2(d*d/tol*tol)
242
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000243 // +1 since we're ignoring the mantissa contribution.
244 int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000245 log = SkTMin(SkTMax(0, log), kMaxSub);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000246 return log;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000247 }
248}
249
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000250/**
251 * Generates the lines and quads to be rendered. Lines are always recorded in
252 * device space. We will do a device space bloat to account for the 1pixel
253 * thickness.
254 * Quads are recorded in device space unless m contains
255 * perspective, then in they are in src space. We do this because we will
256 * subdivide large quads to reduce over-fill. This subdivision has to be
257 * performed before applying the perspective matrix.
258 */
joshualitt6eff8702015-02-02 17:19:40 -0800259int gather_lines_and_quads(const SkPath& path,
260 const SkMatrix& m,
261 const SkIRect& devClipBounds,
262 GrAAHairLinePathRenderer::PtArray* lines,
263 GrAAHairLinePathRenderer::PtArray* quads,
264 GrAAHairLinePathRenderer::PtArray* conics,
265 GrAAHairLinePathRenderer::IntArray* quadSubdivCnts,
266 GrAAHairLinePathRenderer::FloatArray* conicWeights) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000267 SkPath::Iter iter(path, false);
268
269 int totalQuadCount = 0;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000270 SkRect bounds;
271 SkIRect ibounds;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000272
273 bool persp = m.hasPerspective();
274
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000275 for (;;) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000276 SkPoint pathPts[4];
277 SkPoint devPts[4];
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000278 SkPath::Verb verb = iter.next(pathPts);
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000279 switch (verb) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000280 case SkPath::kConic_Verb: {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000281 SkConic dst[4];
282 // We chop the conics to create tighter clipping to hide error
283 // that appears near max curvature of very thin conics. Thin
284 // hyperbolas with high weight still show error.
egdaniel@google.com5383a752013-07-12 20:15:34 +0000285 int conicCnt = chop_conic(pathPts, dst, iter.conicWeight());
286 for (int i = 0; i < conicCnt; ++i) {
287 SkPoint* chopPnts = dst[i].fPts;
288 m.mapPoints(devPts, chopPnts, 3);
289 bounds.setBounds(devPts, 3);
290 bounds.outset(SK_Scalar1, SK_Scalar1);
291 bounds.roundOut(&ibounds);
292 if (SkIRect::Intersects(devClipBounds, ibounds)) {
293 if (is_degen_quad_or_conic(devPts)) {
294 SkPoint* pts = lines->push_back_n(4);
295 pts[0] = devPts[0];
296 pts[1] = devPts[1];
297 pts[2] = devPts[1];
298 pts[3] = devPts[2];
299 } else {
300 // when in perspective keep conics in src space
301 SkPoint* cPts = persp ? chopPnts : devPts;
302 SkPoint* pts = conics->push_back_n(3);
303 pts[0] = cPts[0];
304 pts[1] = cPts[1];
305 pts[2] = cPts[2];
306 conicWeights->push_back() = dst[i].fW;
307 }
308 }
309 }
reed@google.com277c3f82013-05-31 15:17:50 +0000310 break;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000311 }
312 case SkPath::kMove_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000313 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000314 case SkPath::kLine_Verb:
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000315 m.mapPoints(devPts, pathPts, 2);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000316 bounds.setBounds(devPts, 2);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000317 bounds.outset(SK_Scalar1, SK_Scalar1);
318 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000319 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000320 SkPoint* pts = lines->push_back_n(2);
321 pts[0] = devPts[0];
322 pts[1] = devPts[1];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000323 }
324 break;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000325 case SkPath::kQuad_Verb: {
326 SkPoint choppedPts[5];
327 // Chopping the quad helps when the quad is either degenerate or nearly degenerate.
328 // When it is degenerate it allows the approximation with lines to work since the
329 // chop point (if there is one) will be at the parabola's vertex. In the nearly
330 // degenerate the QuadUVMatrix computed for the points is almost singular which
331 // can cause rendering artifacts.
332 int n = SkChopQuadAtMaxCurvature(pathPts, choppedPts);
333 for (int i = 0; i < n; ++i) {
334 SkPoint* quadPts = choppedPts + i * 2;
335 m.mapPoints(devPts, quadPts, 3);
336 bounds.setBounds(devPts, 3);
337 bounds.outset(SK_Scalar1, SK_Scalar1);
338 bounds.roundOut(&ibounds);
339
340 if (SkIRect::Intersects(devClipBounds, ibounds)) {
341 int subdiv = num_quad_subdivs(devPts);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000342 SkASSERT(subdiv >= -1);
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000343 if (-1 == subdiv) {
344 SkPoint* pts = lines->push_back_n(4);
345 pts[0] = devPts[0];
346 pts[1] = devPts[1];
347 pts[2] = devPts[1];
348 pts[3] = devPts[2];
349 } else {
350 // when in perspective keep quads in src space
351 SkPoint* qPts = persp ? quadPts : devPts;
352 SkPoint* pts = quads->push_back_n(3);
353 pts[0] = qPts[0];
354 pts[1] = qPts[1];
355 pts[2] = qPts[2];
356 quadSubdivCnts->push_back() = subdiv;
357 totalQuadCount += 1 << subdiv;
358 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000359 }
360 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000361 break;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000362 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000363 case SkPath::kCubic_Verb:
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000364 m.mapPoints(devPts, pathPts, 4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000365 bounds.setBounds(devPts, 4);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000366 bounds.outset(SK_Scalar1, SK_Scalar1);
367 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000368 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000369 PREALLOC_PTARRAY(32) q;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000370 // we don't need a direction if we aren't constraining the subdivision
371 static const SkPath::Direction kDummyDir = SkPath::kCCW_Direction;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000372 // We convert cubics to quadratics (for now).
373 // In perspective have to do conversion in src space.
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000374 if (persp) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000375 SkScalar tolScale =
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000376 GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m,
377 path.getBounds());
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000378 GrPathUtils::convertCubicToQuads(pathPts, tolScale, false, kDummyDir, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000379 } else {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000380 GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, false, kDummyDir, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000381 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000382 for (int i = 0; i < q.count(); i += 3) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000383 SkPoint* qInDevSpace;
384 // bounds has to be calculated in device space, but q is
385 // in src space when there is perspective.
386 if (persp) {
387 m.mapPoints(devPts, &q[i], 3);
388 bounds.setBounds(devPts, 3);
389 qInDevSpace = devPts;
390 } else {
391 bounds.setBounds(&q[i], 3);
392 qInDevSpace = &q[i];
393 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000394 bounds.outset(SK_Scalar1, SK_Scalar1);
395 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000396 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000397 int subdiv = num_quad_subdivs(qInDevSpace);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000398 SkASSERT(subdiv >= -1);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000399 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000400 SkPoint* pts = lines->push_back_n(4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000401 // lines should always be in device coords
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000402 pts[0] = qInDevSpace[0];
403 pts[1] = qInDevSpace[1];
404 pts[2] = qInDevSpace[1];
405 pts[3] = qInDevSpace[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000406 } else {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000407 SkPoint* pts = quads->push_back_n(3);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000408 // q is already in src space when there is no
409 // perspective and dev coords otherwise.
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000410 pts[0] = q[0 + i];
411 pts[1] = q[1 + i];
412 pts[2] = q[2 + i];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000413 quadSubdivCnts->push_back() = subdiv;
414 totalQuadCount += 1 << subdiv;
415 }
416 }
417 }
418 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000419 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000420 case SkPath::kClose_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000421 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000422 case SkPath::kDone_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000423 return totalQuadCount;
424 }
425 }
426}
427
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000428struct LineVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000429 SkPoint fPos;
egdaniele27065a2014-11-06 08:00:48 -0800430 float fCoverage;
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000431};
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000432
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000433struct BezierVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000434 SkPoint fPos;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000435 union {
436 struct {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000437 SkScalar fK;
438 SkScalar fL;
439 SkScalar fM;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000440 } fConic;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000441 SkVector fQuadCoord;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000442 struct {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000443 SkScalar fBogus[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000444 };
445 };
446};
egdaniel@google.com5383a752013-07-12 20:15:34 +0000447
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000448GR_STATIC_ASSERT(sizeof(BezierVertex) == 3 * sizeof(SkPoint));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000449
450void intersect_lines(const SkPoint& ptA, const SkVector& normA,
451 const SkPoint& ptB, const SkVector& normB,
452 SkPoint* result) {
453
454 SkScalar lineAW = -normA.dot(ptA);
455 SkScalar lineBW = -normB.dot(ptB);
456
457 SkScalar wInv = SkScalarMul(normA.fX, normB.fY) -
egdaniel@google.com5383a752013-07-12 20:15:34 +0000458 SkScalarMul(normA.fY, normB.fX);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000459 wInv = SkScalarInvert(wInv);
460
461 result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY);
462 result->fX = SkScalarMul(result->fX, wInv);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000463
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000464 result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW);
465 result->fY = SkScalarMul(result->fY, wInv);
466}
467
joshualitt5ead6da2014-10-22 16:00:29 -0700468void set_uv_quad(const SkPoint qpts[3], BezierVertex verts[kQuadNumVertices]) {
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000469 // this should be in the src space, not dev coords, when we have perspective
470 GrPathUtils::QuadUVMatrix DevToUV(qpts);
joshualitt5ead6da2014-10-22 16:00:29 -0700471 DevToUV.apply<kQuadNumVertices, sizeof(BezierVertex), sizeof(SkPoint)>(verts);
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000472}
473
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000474void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice,
joshualitt6eff8702015-02-02 17:19:40 -0800475 const SkMatrix* toSrc, BezierVertex verts[kQuadNumVertices]) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000476 SkASSERT(!toDevice == !toSrc);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000477 // original quad is specified by tri a,b,c
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000478 SkPoint a = qpts[0];
479 SkPoint b = qpts[1];
480 SkPoint c = qpts[2];
481
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000482 if (toDevice) {
483 toDevice->mapPoints(&a, 1);
484 toDevice->mapPoints(&b, 1);
485 toDevice->mapPoints(&c, 1);
486 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000487 // make a new poly where we replace a and c by a 1-pixel wide edges orthog
488 // to edges ab and bc:
489 //
490 // before | after
491 // | b0
492 // b |
493 // |
494 // | a0 c0
495 // a c | a1 c1
496 //
497 // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c,
498 // respectively.
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000499 BezierVertex& a0 = verts[0];
500 BezierVertex& a1 = verts[1];
501 BezierVertex& b0 = verts[2];
502 BezierVertex& c0 = verts[3];
503 BezierVertex& c1 = verts[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000504
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000505 SkVector ab = b;
506 ab -= a;
507 SkVector ac = c;
508 ac -= a;
509 SkVector cb = b;
510 cb -= c;
511
512 // We should have already handled degenerates
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000513 SkASSERT(ab.length() > 0 && cb.length() > 0);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000514
515 ab.normalize();
516 SkVector abN;
517 abN.setOrthog(ab, SkVector::kLeft_Side);
518 if (abN.dot(ac) > 0) {
519 abN.negate();
520 }
521
522 cb.normalize();
523 SkVector cbN;
524 cbN.setOrthog(cb, SkVector::kLeft_Side);
525 if (cbN.dot(ac) < 0) {
526 cbN.negate();
527 }
528
529 a0.fPos = a;
530 a0.fPos += abN;
531 a1.fPos = a;
532 a1.fPos -= abN;
533
534 c0.fPos = c;
535 c0.fPos += cbN;
536 c1.fPos = c;
537 c1.fPos -= cbN;
538
539 intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos);
540
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000541 if (toSrc) {
joshualitt5ead6da2014-10-22 16:00:29 -0700542 toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(BezierVertex), kQuadNumVertices);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000543 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000544}
545
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000546// Equations based off of Loop-Blinn Quadratic GPU Rendering
egdaniel@google.com5383a752013-07-12 20:15:34 +0000547// Input Parametric:
548// 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)
549// Output Implicit:
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000550// f(x, y, w) = f(P) = K^2 - LM
551// K = dot(k, P), L = dot(l, P), M = dot(m, P)
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000552// k, l, m are calculated in function GrPathUtils::getConicKLM
joshualitt5ead6da2014-10-22 16:00:29 -0700553void set_conic_coeffs(const SkPoint p[3], BezierVertex verts[kQuadNumVertices],
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000554 const SkScalar weight) {
555 SkScalar klm[9];
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000556
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000557 GrPathUtils::getConicKLM(p, weight, klm);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000558
joshualitt5ead6da2014-10-22 16:00:29 -0700559 for (int i = 0; i < kQuadNumVertices; ++i) {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000560 const SkPoint pnt = verts[i].fPos;
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000561 verts[i].fConic.fK = pnt.fX * klm[0] + pnt.fY * klm[1] + klm[2];
562 verts[i].fConic.fL = pnt.fX * klm[3] + pnt.fY * klm[4] + klm[5];
563 verts[i].fConic.fM = pnt.fX * klm[6] + pnt.fY * klm[7] + klm[8];
egdaniel@google.com5383a752013-07-12 20:15:34 +0000564 }
565}
566
567void add_conics(const SkPoint p[3],
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000568 const SkScalar weight,
egdaniel@google.com5383a752013-07-12 20:15:34 +0000569 const SkMatrix* toDevice,
570 const SkMatrix* toSrc,
joshualitt6eff8702015-02-02 17:19:40 -0800571 BezierVertex** vert) {
572 bloat_quad(p, toDevice, toSrc, *vert);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000573 set_conic_coeffs(p, *vert, weight);
joshualitt5ead6da2014-10-22 16:00:29 -0700574 *vert += kQuadNumVertices;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000575}
576
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000577void add_quads(const SkPoint p[3],
578 int subdiv,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000579 const SkMatrix* toDevice,
580 const SkMatrix* toSrc,
joshualitt6eff8702015-02-02 17:19:40 -0800581 BezierVertex** vert) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000582 SkASSERT(subdiv >= 0);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000583 if (subdiv) {
584 SkPoint newP[5];
585 SkChopQuadAtHalf(p, newP);
joshualitt6eff8702015-02-02 17:19:40 -0800586 add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert);
587 add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000588 } else {
joshualitt6eff8702015-02-02 17:19:40 -0800589 bloat_quad(p, toDevice, toSrc, *vert);
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000590 set_uv_quad(p, *vert);
joshualitt5ead6da2014-10-22 16:00:29 -0700591 *vert += kQuadNumVertices;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000592 }
593}
594
595void add_line(const SkPoint p[2],
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000596 const SkMatrix* toSrc,
egdaniele27065a2014-11-06 08:00:48 -0800597 uint8_t coverage,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000598 LineVertex** vert) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000599 const SkPoint& a = p[0];
600 const SkPoint& b = p[1];
601
robertphillips@google.comada90da2013-09-18 22:14:49 +0000602 SkVector ortho, vec = b;
603 vec -= a;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000604
robertphillips@google.comada90da2013-09-18 22:14:49 +0000605 if (vec.setLength(SK_ScalarHalf)) {
606 // Create a vector orthogonal to 'vec' and of unit length
607 ortho.fX = 2.0f * vec.fY;
608 ortho.fY = -2.0f * vec.fX;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000609
egdaniele27065a2014-11-06 08:00:48 -0800610 float floatCoverage = GrNormalizeByteToFloat(coverage);
611
robertphillips@google.comada90da2013-09-18 22:14:49 +0000612 (*vert)[0].fPos = a;
egdaniele27065a2014-11-06 08:00:48 -0800613 (*vert)[0].fCoverage = floatCoverage;
robertphillips@google.comada90da2013-09-18 22:14:49 +0000614 (*vert)[1].fPos = b;
egdaniele27065a2014-11-06 08:00:48 -0800615 (*vert)[1].fCoverage = floatCoverage;
robertphillips@google.comada90da2013-09-18 22:14:49 +0000616 (*vert)[2].fPos = a - vec + ortho;
617 (*vert)[2].fCoverage = 0;
618 (*vert)[3].fPos = b + vec + ortho;
619 (*vert)[3].fCoverage = 0;
620 (*vert)[4].fPos = a - vec - ortho;
621 (*vert)[4].fCoverage = 0;
622 (*vert)[5].fPos = b + vec - ortho;
623 (*vert)[5].fCoverage = 0;
624
bsalomon49f085d2014-09-05 13:34:00 -0700625 if (toSrc) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000626 toSrc->mapPointsWithStride(&(*vert)->fPos,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000627 sizeof(LineVertex),
joshualitt5ead6da2014-10-22 16:00:29 -0700628 kLineSegNumVertices);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000629 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000630 } else {
631 // just make it degenerate and likely offscreen
joshualitt5ead6da2014-10-22 16:00:29 -0700632 for (int i = 0; i < kLineSegNumVertices; ++i) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000633 (*vert)[i].fPos.set(SK_ScalarMax, SK_ScalarMax);
634 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000635 }
636
joshualitt5ead6da2014-10-22 16:00:29 -0700637 *vert += kLineSegNumVertices;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000638}
639
640}
641
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000642///////////////////////////////////////////////////////////////////////////////
643
joshualitt9853cce2014-11-17 14:22:48 -0800644bool GrAAHairLinePathRenderer::canDrawPath(const GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -0800645 const GrPipelineBuilder* pipelineBuilder,
joshualitt8059eb92014-12-29 15:10:07 -0800646 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -0800647 const SkPath& path,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000648 const SkStrokeRec& stroke,
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000649 bool antiAlias) const {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000650 if (!antiAlias) {
651 return false;
652 }
653
joshualitt8059eb92014-12-29 15:10:07 -0800654 if (!IsStrokeHairlineOrEquivalent(stroke, viewMatrix, NULL)) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000655 return false;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000656 }
657
robertphillips@google.come79f3202014-02-11 16:30:21 +0000658 if (SkPath::kLine_SegmentMask == path.getSegmentMasks() ||
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000659 target->caps()->shaderDerivativeSupport()) {
660 return true;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000661 }
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000662 return false;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000663}
664
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000665template <class VertexType>
joshualitt8059eb92014-12-29 15:10:07 -0800666bool check_bounds(const SkMatrix& viewMatrix, const SkRect& devBounds, void* vertices, int vCount)
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000667{
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000668 SkRect tolDevBounds = devBounds;
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000669 // The bounds ought to be tight, but in perspective the below code runs the verts
670 // through the view matrix to get back to dev coords, which can introduce imprecision.
joshualitt8059eb92014-12-29 15:10:07 -0800671 if (viewMatrix.hasPerspective()) {
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000672 tolDevBounds.outset(SK_Scalar1 / 1000, SK_Scalar1 / 1000);
673 } else {
674 // Non-persp matrices cause this path renderer to draw in device space.
joshualitt8059eb92014-12-29 15:10:07 -0800675 SkASSERT(viewMatrix.isIdentity());
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000676 }
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000677 SkRect actualBounds;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000678
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000679 VertexType* verts = reinterpret_cast<VertexType*>(vertices);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000680 bool first = true;
681 for (int i = 0; i < vCount; ++i) {
682 SkPoint pos = verts[i].fPos;
683 // This is a hack to workaround the fact that we move some degenerate segments offscreen.
684 if (SK_ScalarMax == pos.fX) {
685 continue;
686 }
joshualitt8059eb92014-12-29 15:10:07 -0800687 viewMatrix.mapPoints(&pos, 1);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000688 if (first) {
689 actualBounds.set(pos.fX, pos.fY, pos.fX, pos.fY);
690 first = false;
691 } else {
692 actualBounds.growToInclude(pos.fX, pos.fY);
693 }
694 }
695 if (!first) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000696 return tolDevBounds.contains(actualBounds);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000697 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000698
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000699 return true;
700}
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000701
joshualitt6eff8702015-02-02 17:19:40 -0800702class AAHairlineBatch : public GrBatch {
703public:
704 struct Geometry {
705 GrColor fColor;
706 uint8_t fCoverage;
707 SkMatrix fViewMatrix;
708 SkPath fPath;
709 SkDEBUGCODE(SkRect fDevBounds;)
710 SkIRect fDevClipBounds;
711 };
712
713 // TODO Batch itself should not hold on to index buffers. Instead, these should live in the
714 // cache.
715 static GrBatch* Create(const Geometry& geometry, const GrIndexBuffer* linesIndexBuffer,
716 const GrIndexBuffer* quadsIndexBuffer) {
717 return SkNEW_ARGS(AAHairlineBatch, (geometry, linesIndexBuffer, quadsIndexBuffer));
718 }
719
720 const char* name() const SK_OVERRIDE { return "AAHairlineBatch"; }
721
722 void getInvariantOutputColor(GrInitInvariantOutput* out) const SK_OVERRIDE {
723 // When this is called on a batch, there is only one geometry bundle
724 out->setKnownFourComponents(fGeoData[0].fColor);
725 }
726 void getInvariantOutputCoverage(GrInitInvariantOutput* out) const SK_OVERRIDE {
727 out->setUnknownSingleComponent();
728 }
729
730 void initBatchOpt(const GrBatchOpt& batchOpt) {
731 fBatchOpt = batchOpt;
732 }
733
734 void initBatchTracker(const GrPipelineInfo& init) SK_OVERRIDE {
735 // Handle any color overrides
736 if (init.fColorIgnored) {
737 fGeoData[0].fColor = GrColor_ILLEGAL;
738 } else if (GrColor_ILLEGAL != init.fOverrideColor) {
739 fGeoData[0].fColor = init.fOverrideColor;
740 }
741
742 // setup batch properties
743 fBatch.fColorIgnored = init.fColorIgnored;
744 fBatch.fColor = fGeoData[0].fColor;
745 fBatch.fUsesLocalCoords = init.fUsesLocalCoords;
746 fBatch.fCoverageIgnored = init.fCoverageIgnored;
747 fBatch.fCoverage = fGeoData[0].fCoverage;
748 SkDEBUGCODE(fBatch.fDevBounds = fGeoData[0].fDevBounds;)
749 }
750
751 void generateGeometry(GrBatchTarget* batchTarget, const GrPipeline* pipeline) SK_OVERRIDE {
752 int instanceCount = fGeoData.count();
753 for (int i = 0; i < instanceCount; i++) {
754 const Geometry& args = fGeoData[i];
755
756 // createGeom transforms the geometry to device space when the matrix does not have
757 // perspective.
758 SkMatrix vm = args.fViewMatrix;
759 SkMatrix invert = SkMatrix::I();
760 if (!args.fViewMatrix.hasPerspective()) {
761 vm = SkMatrix::I();
762 if (!args.fViewMatrix.invert(&invert)) {
763 return;
764 }
765 }
766
767 int lineCount;
768 int quadCount;
769 int conicCount;
770 PREALLOC_PTARRAY(128) lines;
771 PREALLOC_PTARRAY(128) quads;
772 PREALLOC_PTARRAY(128) conics;
773 IntArray qSubdivs;
774 FloatArray cWeights;
775 quadCount = gather_lines_and_quads(args.fPath, args.fViewMatrix, args.fDevClipBounds,
776 &lines, &quads, &conics, &qSubdivs, &cWeights);
777
778 lineCount = lines.count() / 2;
779 conicCount = conics.count() / 3;
780
781 // do lines first
782 if (lineCount) {
783 this->generateLines(batchTarget, pipeline, args, vm, invert, lines, lineCount);
784 }
785
786 if (quadCount || conicCount) {
787 this->generateQuadsAndConics(batchTarget,
788 pipeline,
789 args,
790 quads,
791 quadCount,
792 conics,
793 conicCount,
794 qSubdivs,
795 cWeights,
796 vm,
797 invert);
798 }
799 }
800 }
801
802 SkSTArray<1, Geometry, true>* geoData() { return &fGeoData; }
803
804private:
805 typedef SkTArray<SkPoint, true> PtArray;
806 typedef SkTArray<int, true> IntArray;
807 typedef SkTArray<float, true> FloatArray;
808
809 AAHairlineBatch(const Geometry& geometry, const GrIndexBuffer* linesIndexBuffer,
810 const GrIndexBuffer* quadsIndexBuffer)
811 : fLinesIndexBuffer(linesIndexBuffer)
812 , fQuadsIndexBuffer(quadsIndexBuffer) {
813 SkASSERT(linesIndexBuffer && quadsIndexBuffer);
814 this->initClassID<AAHairlineBatch>();
815 fGeoData.push_back(geometry);
816 }
817
818 bool onCombineIfPossible(GrBatch* t) SK_OVERRIDE {
819 AAHairlineBatch* that = t->cast<AAHairlineBatch>();
820
821 // We go to identity if we don't have perspective
822 if (this->viewMatrix().hasPerspective() &&
823 !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
824 return false;
825 }
826
827 // TODO we can actually batch hairlines if they are the same color in a kind of bulk method
828 // but we haven't implemented this yet
829 // TODO investigate going to vertex color and coverage?
830 if (this->coverage() != that->coverage()) {
831 return false;
832 }
833
834 if (this->color() != that->color()) {
835 return false;
836 }
837
838 SkASSERT(this->usesLocalCoords() == that->usesLocalCoords());
839 if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) {
840 return false;
841 }
842
843 fGeoData.push_back_n(that->geoData()->count(), that->geoData()->begin());
844 return true;
845 }
846
847 GrColor color() const { return fBatch.fColor; }
848 uint8_t coverage() const { return fBatch.fCoverage; }
849 bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; }
850 const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; }
851
852 void generateLines(GrBatchTarget* batchTarget,
853 const GrPipeline* pipeline,
854 const Geometry& args,
855 const SkMatrix& viewMatrix,
856 const SkMatrix& invert,
857 const PtArray& lines,
858 int lineCnt) {
859 uint32_t gpFlags = GrDefaultGeoProcFactory::kPosition_GPType |
860 GrDefaultGeoProcFactory::kCoverage_GPType;
861 SkAutoTUnref<const GrGeometryProcessor> gp(GrDefaultGeoProcFactory::Create(gpFlags,
862 args.fColor,
863 viewMatrix,
864 invert,
865 false,
866 args.fCoverage));
867
868 batchTarget->initDraw(gp, pipeline);
869
870 // TODO remove this when batch is everywhere
871 GrPipelineInfo init;
872 init.fColorIgnored = fBatch.fColorIgnored;
873 init.fOverrideColor = GrColor_ILLEGAL;
874 init.fCoverageIgnored = fBatch.fCoverageIgnored;
875 init.fUsesLocalCoords = this->usesLocalCoords();
876 gp->initBatchTracker(batchTarget->currentBatchTracker(), init);
877
878 const GrVertexBuffer* vertexBuffer;
879 int firstVertex;
880
881 size_t vertexStride = gp->getVertexStride();
882 int vertexCount = kLineSegNumVertices * lineCnt;
883 void *vertices = batchTarget->vertexPool()->makeSpace(vertexStride,
884 vertexCount,
885 &vertexBuffer,
886 &firstVertex);
887
888 SkASSERT(gp->getVertexStride() == sizeof(LineVertex));
889
890 // generate lines
891 const SkMatrix* toSrc = NULL;
892 if (args.fViewMatrix.hasPerspective()) {
893 SkMatrix perspectiveInvert;
894 if (!args.fViewMatrix.invert(&perspectiveInvert)) {
895 return;
896 }
897 toSrc = &perspectiveInvert;
898 }
899
900 LineVertex* verts = reinterpret_cast<LineVertex*>(vertices);
901 for (int i = 0; i < lineCnt; ++i) {
902 add_line(&lines[2*i], toSrc, args.fCoverage, &verts);
903 }
904
905 // Check devBounds
906 SkASSERT(check_bounds<LineVertex>(viewMatrix.hasPerspective() ? viewMatrix : SkMatrix::I(),
907 args.fDevBounds,
908 vertices,
909 kLineSegNumVertices * lineCnt));
910
911 {
912 GrDrawTarget::DrawInfo info;
913 info.setVertexBuffer(vertexBuffer);
914 info.setIndexBuffer(fLinesIndexBuffer);
915 info.setPrimitiveType(kTriangles_GrPrimitiveType);
916 info.setStartIndex(0);
917
918 int lines = 0;
919 while (lines < lineCnt) {
920 int n = SkTMin(lineCnt - lines, kLineSegsNumInIdxBuffer);
921
922 info.setStartVertex(kLineSegNumVertices*lines + firstVertex);
923 info.setVertexCount(kLineSegNumVertices*n);
924 info.setIndexCount(kIdxsPerLineSeg*n);
925 batchTarget->draw(info);
926
927 lines += n;
928 }
929 }
930 }
931
932 void generateQuadsAndConics(GrBatchTarget* batchTarget,
933 const GrPipeline* pipeline,
934 const Geometry& args,
935 const PREALLOC_PTARRAY(128)& quads,
936 int quadCount,
937 const PREALLOC_PTARRAY(128)& conics,
938 int conicCount,
939 const IntArray& qSubdivs,
940 const FloatArray& cWeights,
941 const SkMatrix& vm,
942 const SkMatrix& invert) {
943 const GrVertexBuffer* vertexBuffer;
944 int firstVertex;
945
946 size_t vertexStride = sizeof(BezierVertex);
947 int vertexCount = kQuadNumVertices * quadCount + kQuadNumVertices * conicCount;
948 void *vertices = batchTarget->vertexPool()->makeSpace(vertexStride,
949 vertexCount,
950 &vertexBuffer,
951 &firstVertex);
952
953 if (!this->createBezierGeom(vertices,
954 args.fViewMatrix,
955 args.fPath,
956 quads,
957 quadCount,
958 conics,
959 conicCount,
960 qSubdivs,
961 cWeights,
962 vertexStride)) {
963 SkDebugf("Couldn't create bezier geometry\n");
964 return;
965 }
966
967 // Check devBounds
968 SkASSERT(check_bounds<BezierVertex>(vm,
969 args.fDevBounds,
970 vertices,
971 kQuadNumVertices * quadCount +
972 kQuadNumVertices * conicCount));
973
974 if (quadCount > 0) {
975 SkAutoTUnref<GrGeometryProcessor> hairQuadProcessor(
976 GrQuadEffect::Create(args.fColor,
977 vm,
978 kHairlineAA_GrProcessorEdgeType,
979 batchTarget->caps(),
980 invert,
981 args.fCoverage));
982
983 batchTarget->initDraw(hairQuadProcessor, pipeline);
984
985 // TODO remove this when batch is everywhere
986 GrPipelineInfo init;
987 init.fColorIgnored = fBatch.fColorIgnored;
988 init.fOverrideColor = GrColor_ILLEGAL;
989 init.fCoverageIgnored = fBatch.fCoverageIgnored;
990 init.fUsesLocalCoords = this->usesLocalCoords();
991 hairQuadProcessor->initBatchTracker(batchTarget->currentBatchTracker(), init);
992
993 this->drawBeziers(batchTarget,
994 hairQuadProcessor,
995 pipeline,
996 vertexBuffer,
997 firstVertex,
998 quadCount);
999 }
1000
1001 if (conicCount > 0) {
1002 SkAutoTUnref<GrGeometryProcessor> hairConicProcessor(
1003 GrConicEffect::Create(args.fColor,
1004 vm,
1005 kHairlineAA_GrProcessorEdgeType,
1006 batchTarget->caps(),
1007 invert,
1008 args.fCoverage));
1009
1010 batchTarget->initDraw(hairConicProcessor, pipeline);
1011
1012 // TODO remove this when batch is everywhere
1013 GrPipelineInfo init;
1014 init.fColorIgnored = fBatch.fColorIgnored;
1015 init.fOverrideColor = GrColor_ILLEGAL;
1016 init.fCoverageIgnored = fBatch.fCoverageIgnored;
1017 init.fUsesLocalCoords = this->usesLocalCoords();
1018 hairConicProcessor->initBatchTracker(batchTarget->currentBatchTracker(), init);
1019
1020 this->drawConics(batchTarget,
1021 hairConicProcessor,
1022 pipeline,
1023 vertexBuffer,
1024 firstVertex,
1025 conicCount,
1026 quadCount);
1027 }
1028 }
1029
1030 bool createBezierGeom(void* vertices,
1031 const SkMatrix& viewMatrix,
1032 const SkPath& path,
1033 const PtArray& quads,
1034 int quadCnt,
1035 const PtArray& conics,
1036 int conicCnt,
1037 const IntArray& qSubdivs,
1038 const FloatArray& cWeights,
1039 size_t vertexStride) {
1040 BezierVertex* verts = reinterpret_cast<BezierVertex*>(vertices);
1041
1042 const SkMatrix* toDevice = NULL;
1043 const SkMatrix* toSrc = NULL;
1044 SkMatrix ivm;
1045
1046 if (viewMatrix.hasPerspective()) {
1047 if (viewMatrix.invert(&ivm)) {
1048 toDevice = &viewMatrix;
1049 toSrc = &ivm;
1050 }
1051 }
1052
1053 int unsubdivQuadCnt = quads.count() / 3;
1054 for (int i = 0; i < unsubdivQuadCnt; ++i) {
1055 SkASSERT(qSubdivs[i] >= 0);
1056 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts);
1057 }
1058
1059 // Start Conics
1060 for (int i = 0; i < conicCnt; ++i) {
1061 add_conics(&conics[3*i], cWeights[i], toDevice, toSrc, &verts);
1062 }
1063 return true;
1064 }
1065
1066 void drawBeziers(GrBatchTarget* batchTarget,
1067 const GrGeometryProcessor* hairQuadProcessor,
1068 const GrPipeline* pipeline,
1069 const GrVertexBuffer* vertexBuffer,
1070 int firstVertex,
1071 int quadCount) {
1072 GrDrawTarget::DrawInfo info;
1073 info.setVertexBuffer(vertexBuffer);
1074 info.setIndexBuffer(fQuadsIndexBuffer);
1075 info.setPrimitiveType(kTriangles_GrPrimitiveType);
1076 info.setStartIndex(0);
1077
1078 int quads = 0;
1079 while (quads < quadCount) {
1080 int n = SkTMin(quadCount - quads, kQuadsNumInIdxBuffer);
1081
1082 info.setStartVertex(kQuadNumVertices*quads + firstVertex);
1083 info.setVertexCount(kQuadNumVertices*n);
1084 info.setIndexCount(kIdxsPerQuad*n);
1085 batchTarget->draw(info);
1086
1087 quads += n;
1088 }
1089 }
1090
1091 void drawConics(GrBatchTarget* batchTarget,
1092 const GrGeometryProcessor* hairConicProcessor,
1093 const GrPipeline* pipeline,
1094 const GrVertexBuffer* vertexBuffer,
1095 int firstVertex,
1096 int conicCount,
1097 int quadCount) {
1098 GrDrawTarget::DrawInfo info;
1099 info.setVertexBuffer(vertexBuffer);
1100 info.setIndexBuffer(fQuadsIndexBuffer);
1101 info.setPrimitiveType(kTriangles_GrPrimitiveType);
1102 info.setStartIndex(0);
1103
1104 int conics = 0;
1105 while (conics < conicCount) {
1106 int n = SkTMin(conicCount - conics, kQuadsNumInIdxBuffer);
1107
1108 info.setStartVertex(kQuadNumVertices*(quadCount + conics) + firstVertex);
1109 info.setVertexCount(kQuadNumVertices*n);
1110 info.setIndexCount(kIdxsPerQuad*n);
1111 batchTarget->draw(info);
1112
1113 conics += n;
1114 }
1115 }
1116
1117 struct BatchTracker {
1118 GrColor fColor;
1119 uint8_t fCoverage;
1120 SkRect fDevBounds;
1121 bool fUsesLocalCoords;
1122 bool fColorIgnored;
1123 bool fCoverageIgnored;
1124 };
1125
1126 GrBatchOpt fBatchOpt;
1127 BatchTracker fBatch;
1128 SkSTArray<1, Geometry, true> fGeoData;
1129 const GrIndexBuffer* fLinesIndexBuffer;
1130 const GrIndexBuffer* fQuadsIndexBuffer;
1131};
1132
joshualitt9853cce2014-11-17 14:22:48 -08001133bool GrAAHairLinePathRenderer::onDrawPath(GrDrawTarget* target,
egdaniel8dd688b2015-01-22 10:16:09 -08001134 GrPipelineBuilder* pipelineBuilder,
joshualitt2e3b3e32014-12-09 13:31:14 -08001135 GrColor color,
joshualitt8059eb92014-12-29 15:10:07 -08001136 const SkMatrix& viewMatrix,
joshualitt9853cce2014-11-17 14:22:48 -08001137 const SkPath& path,
robertphillips@google.come79f3202014-02-11 16:30:21 +00001138 const SkStrokeRec& stroke,
joshualitt6eff8702015-02-02 17:19:40 -08001139 bool) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001140 SkScalar hairlineCoverage;
joshualitt2e3b3e32014-12-09 13:31:14 -08001141 uint8_t newCoverage = 0xff;
joshualitt8059eb92014-12-29 15:10:07 -08001142 if (IsStrokeHairlineOrEquivalent(stroke, viewMatrix, &hairlineCoverage)) {
joshualitt2e3b3e32014-12-09 13:31:14 -08001143 newCoverage = SkScalarRoundToInt(hairlineCoverage * 0xff);
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +00001144 }
1145
jvanverth@google.com681ccf02013-08-16 14:51:51 +00001146 SkIRect devClipBounds;
egdaniel8dd688b2015-01-22 10:16:09 -08001147 target->getClip()->getConservativeBounds(pipelineBuilder->getRenderTarget(), &devClipBounds);
skia.committer@gmail.com74758112013-08-17 07:01:54 +00001148
joshualitt6eff8702015-02-02 17:19:40 -08001149 // This outset was determined experimentally by running skps and gms. It probably could be a
1150 // bit tighter
1151 SkRect devRect = path.getBounds();
1152 devRect.outset(7, 7);
1153 viewMatrix.mapRect(&devRect);
skia.committer@gmail.com74758112013-08-17 07:01:54 +00001154
joshualitt6eff8702015-02-02 17:19:40 -08001155 AAHairlineBatch::Geometry geometry;
1156 geometry.fColor = color;
1157 geometry.fCoverage = newCoverage;
1158 geometry.fViewMatrix = viewMatrix;
1159 geometry.fPath = path;
1160 SkDEBUGCODE(geometry.fDevBounds = devRect;)
1161 geometry.fDevClipBounds = devClipBounds;
joshualittd27f73e2014-12-29 07:43:36 -08001162
joshualitt6eff8702015-02-02 17:19:40 -08001163 GrBatch* batch = AAHairlineBatch::Create(geometry, fLinesIndexBuffer, fQuadsIndexBuffer);
1164 target->drawBatch(pipelineBuilder, batch, &devRect);
bsalomon@google.com4647f902013-03-26 14:45:27 +00001165
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001166 return true;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001167}