blob: 7168c8388477a52705dc0798160c93f931a354f8 [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
10#include "GrContext.h"
joshualitt5478d422014-11-14 16:00:38 -080011#include "GrDefaultGeoProcFactory.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000012#include "GrDrawState.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000013#include "GrDrawTargetCaps.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000014#include "GrGpu.h"
15#include "GrIndexBuffer.h"
bsalomon@google.comdbeeac32011-09-12 14:59:34 +000016#include "GrPathUtils.h"
joshualitt5478d422014-11-14 16:00:38 -080017#include "GrProcessor.h"
joshualittb0a8a372014-09-23 09:50:21 -070018#include "GrTBackendProcessorFactory.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000019#include "SkGeometry.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000020#include "SkStroke.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000021#include "SkTemplates.h"
22
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000023#include "effects/GrBezierEffect.h"
bsalomon@google.com4647f902013-03-26 14:45:27 +000024
bsalomon@google.comaeb21602011-08-30 18:13:44 +000025// quadratics are rendered as 5-sided polys in order to bound the
26// AA stroke around the center-curve. See comments in push_quad_index_buffer and
egdaniel@google.com5383a752013-07-12 20:15:34 +000027// bloat_quad. Quadratics and conics share an index buffer
bsalomon@google.comaeb21602011-08-30 18:13:44 +000028
robertphillips@google.comada90da2013-09-18 22:14:49 +000029// lines are rendered as:
30// *______________*
31// |\ -_______ /|
32// | \ \ / |
33// | *--------* |
34// | / ______/ \ |
35// */_-__________\*
36// For: 6 vertices and 18 indices (for 6 triangles)
bsalomon@google.comaeb21602011-08-30 18:13:44 +000037
joshualitt5ead6da2014-10-22 16:00:29 -070038// Each quadratic is rendered as a five sided polygon. This poly bounds
39// the quadratic's bounding triangle but has been expanded so that the
40// 1-pixel wide area around the curve is inside the poly.
41// If a,b,c are the original control points then the poly a0,b0,c0,c1,a1
42// that is rendered would look like this:
43// b0
44// b
45//
46// a0 c0
47// a c
48// a1 c1
49// Each is drawn as three triangles specified by these 9 indices:
50static const uint16_t kQuadIdxBufPattern[] = {
51 0, 1, 2,
52 2, 4, 3,
53 1, 4, 2
54};
bsalomon@google.comaeb21602011-08-30 18:13:44 +000055
joshualitt5ead6da2014-10-22 16:00:29 -070056static const int kIdxsPerQuad = SK_ARRAY_COUNT(kQuadIdxBufPattern);
57static const int kQuadNumVertices = 5;
58static const int kQuadsNumInIdxBuffer = 256;
jvanverth@google.com681ccf02013-08-16 14:51:51 +000059
bsalomon@google.comaeb21602011-08-30 18:13:44 +000060
joshualitt5ead6da2014-10-22 16:00:29 -070061// Each line segment is rendered as two quads and two triangles.
62// p0 and p1 have alpha = 1 while all other points have alpha = 0.
63// The four external points are offset 1 pixel perpendicular to the
64// line and half a pixel parallel to the line.
65//
66// p4 p5
67// p0 p1
68// p2 p3
69//
70// Each is drawn as six triangles specified by these 18 indices:
jvanverth@google.com681ccf02013-08-16 14:51:51 +000071
joshualitt5ead6da2014-10-22 16:00:29 -070072static const uint16_t kLineSegIdxBufPattern[] = {
73 0, 1, 3,
74 0, 3, 2,
75 0, 4, 5,
76 0, 5, 1,
77 0, 2, 4,
78 1, 5, 3
79};
skia.committer@gmail.com74758112013-08-17 07:01:54 +000080
joshualitt5ead6da2014-10-22 16:00:29 -070081static const int kIdxsPerLineSeg = SK_ARRAY_COUNT(kLineSegIdxBufPattern);
82static const int kLineSegNumVertices = 6;
83static const int kLineSegsNumInIdxBuffer = 256;
bsalomon@google.comaeb21602011-08-30 18:13:44 +000084
85GrPathRenderer* GrAAHairLinePathRenderer::Create(GrContext* context) {
bsalomon@google.coma8a6a322011-09-23 14:19:58 +000086 GrGpu* gpu = context->getGpu();
joshualitt5ead6da2014-10-22 16:00:29 -070087 GrIndexBuffer* qIdxBuf = gpu->createInstancedIndexBuffer(kQuadIdxBufPattern,
88 kIdxsPerQuad,
89 kQuadsNumInIdxBuffer,
90 kQuadNumVertices);
bsalomon@google.coma8a6a322011-09-23 14:19:58 +000091 SkAutoTUnref<GrIndexBuffer> qIdxBuffer(qIdxBuf);
joshualitt5ead6da2014-10-22 16:00:29 -070092 GrIndexBuffer* lIdxBuf = gpu->createInstancedIndexBuffer(kLineSegIdxBufPattern,
93 kIdxsPerLineSeg,
94 kLineSegsNumInIdxBuffer,
95 kLineSegNumVertices);
jvanverth@google.com681ccf02013-08-16 14:51:51 +000096 SkAutoTUnref<GrIndexBuffer> lIdxBuffer(lIdxBuf);
tomhudson@google.comc377baf2012-07-09 20:17:56 +000097 return SkNEW_ARGS(GrAAHairLinePathRenderer,
jvanverth@google.com681ccf02013-08-16 14:51:51 +000098 (context, lIdxBuf, qIdxBuf));
bsalomon@google.comaeb21602011-08-30 18:13:44 +000099}
100
101GrAAHairLinePathRenderer::GrAAHairLinePathRenderer(
102 const GrContext* context,
103 const GrIndexBuffer* linesIndexBuffer,
104 const GrIndexBuffer* quadsIndexBuffer) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000105 fLinesIndexBuffer = linesIndexBuffer;
106 linesIndexBuffer->ref();
107 fQuadsIndexBuffer = quadsIndexBuffer;
108 quadsIndexBuffer->ref();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000109}
110
111GrAAHairLinePathRenderer::~GrAAHairLinePathRenderer() {
112 fLinesIndexBuffer->unref();
113 fQuadsIndexBuffer->unref();
114}
115
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000116namespace {
117
bsalomon@google.com92669012011-09-27 19:10:05 +0000118#define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true>
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000119
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000120// Takes 178th time of logf on Z600 / VC2010
121int get_float_exp(float x) {
122 GR_STATIC_ASSERT(sizeof(int) == sizeof(float));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000123#ifdef SK_DEBUG
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000124 static bool tested;
125 if (!tested) {
126 tested = true;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000127 SkASSERT(get_float_exp(0.25f) == -2);
128 SkASSERT(get_float_exp(0.3f) == -2);
129 SkASSERT(get_float_exp(0.5f) == -1);
130 SkASSERT(get_float_exp(1.f) == 0);
131 SkASSERT(get_float_exp(2.f) == 1);
132 SkASSERT(get_float_exp(2.5f) == 1);
133 SkASSERT(get_float_exp(8.f) == 3);
134 SkASSERT(get_float_exp(100.f) == 6);
135 SkASSERT(get_float_exp(1000.f) == 9);
136 SkASSERT(get_float_exp(1024.f) == 10);
137 SkASSERT(get_float_exp(3000000.f) == 21);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000138 }
139#endif
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000140 const int* iptr = (const int*)&x;
141 return (((*iptr) & 0x7f800000) >> 23) - 127;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000142}
143
egdaniel@google.com5383a752013-07-12 20:15:34 +0000144// Uses the max curvature function for quads to estimate
145// where to chop the conic. If the max curvature is not
146// found along the curve segment it will return 1 and
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000147// dst[0] is the original conic. If it returns 2 the dst[0]
egdaniel@google.com5383a752013-07-12 20:15:34 +0000148// and dst[1] are the two new conics.
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000149int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000150 SkScalar t = SkFindQuadMaxCurvature(src);
151 if (t == 0) {
152 if (dst) {
153 dst[0].set(src, weight);
154 }
155 return 1;
156 } else {
157 if (dst) {
158 SkConic conic;
159 conic.set(src, weight);
160 conic.chopAt(t, dst);
161 }
162 return 2;
163 }
164}
165
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000166// Calls split_conic on the entire conic and then once more on each subsection.
167// Most cases will result in either 1 conic (chop point is not within t range)
168// or 3 points (split once and then one subsection is split again).
169int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) {
170 SkConic dstTemp[2];
171 int conicCnt = split_conic(src, dstTemp, weight);
172 if (2 == conicCnt) {
173 int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW);
174 conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW);
175 } else {
176 dst[0] = dstTemp[0];
177 }
178 return conicCnt;
179}
180
egdaniel@google.com5383a752013-07-12 20:15:34 +0000181// returns 0 if quad/conic is degen or close to it
182// in this case approx the path with lines
183// otherwise returns 1
184int is_degen_quad_or_conic(const SkPoint p[3]) {
185 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
186 static const SkScalar gDegenerateToLineTolSqd =
187 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
188
189 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
190 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
191 return 1;
192 }
193
194 SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
195 if (dsqd < gDegenerateToLineTolSqd) {
196 return 1;
197 }
198
199 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
200 return 1;
201 }
202 return 0;
203}
204
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000205// we subdivide the quads to avoid huge overfill
206// if it returns -1 then should be drawn as lines
207int num_quad_subdivs(const SkPoint p[3]) {
208 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000209 static const SkScalar gDegenerateToLineTolSqd =
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000210 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000211
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000212 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
213 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000214 return -1;
215 }
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000216
bsalomon@google.com81712882012-11-01 17:12:34 +0000217 SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000218 if (dsqd < gDegenerateToLineTolSqd) {
219 return -1;
220 }
221
222 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000223 return -1;
224 }
225
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000226 // tolerance of triangle height in pixels
227 // tuned on windows Quadro FX 380 / Z600
228 // trade off of fill vs cpu time on verts
229 // maybe different when do this using gpu (geo or tess shaders)
230 static const SkScalar gSubdivTol = 175 * SK_Scalar1;
231
robertphillips@google.com7460b372012-04-25 16:54:51 +0000232 if (dsqd <= SkScalarMul(gSubdivTol, gSubdivTol)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000233 return 0;
234 } else {
robertphillips@google.com87379e12013-03-29 12:11:10 +0000235 static const int kMaxSub = 4;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000236 // subdividing the quad reduces d by 4. so we want x = log4(d/tol)
237 // = log4(d*d/tol*tol)/2
238 // = log2(d*d/tol*tol)
239
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000240 // +1 since we're ignoring the mantissa contribution.
241 int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000242 log = SkTMin(SkTMax(0, log), kMaxSub);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000243 return log;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000244 }
245}
246
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000247/**
248 * Generates the lines and quads to be rendered. Lines are always recorded in
249 * device space. We will do a device space bloat to account for the 1pixel
250 * thickness.
251 * Quads are recorded in device space unless m contains
252 * perspective, then in they are in src space. We do this because we will
253 * subdivide large quads to reduce over-fill. This subdivision has to be
254 * performed before applying the perspective matrix.
255 */
256int generate_lines_and_quads(const SkPath& path,
257 const SkMatrix& m,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000258 const SkIRect& devClipBounds,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000259 GrAAHairLinePathRenderer::PtArray* lines,
260 GrAAHairLinePathRenderer::PtArray* quads,
261 GrAAHairLinePathRenderer::PtArray* conics,
262 GrAAHairLinePathRenderer::IntArray* quadSubdivCnts,
263 GrAAHairLinePathRenderer::FloatArray* conicWeights) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000264 SkPath::Iter iter(path, false);
265
266 int totalQuadCount = 0;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000267 SkRect bounds;
268 SkIRect ibounds;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000269
270 bool persp = m.hasPerspective();
271
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000272 for (;;) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000273 SkPoint pathPts[4];
274 SkPoint devPts[4];
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000275 SkPath::Verb verb = iter.next(pathPts);
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000276 switch (verb) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000277 case SkPath::kConic_Verb: {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000278 SkConic dst[4];
279 // We chop the conics to create tighter clipping to hide error
280 // that appears near max curvature of very thin conics. Thin
281 // hyperbolas with high weight still show error.
egdaniel@google.com5383a752013-07-12 20:15:34 +0000282 int conicCnt = chop_conic(pathPts, dst, iter.conicWeight());
283 for (int i = 0; i < conicCnt; ++i) {
284 SkPoint* chopPnts = dst[i].fPts;
285 m.mapPoints(devPts, chopPnts, 3);
286 bounds.setBounds(devPts, 3);
287 bounds.outset(SK_Scalar1, SK_Scalar1);
288 bounds.roundOut(&ibounds);
289 if (SkIRect::Intersects(devClipBounds, ibounds)) {
290 if (is_degen_quad_or_conic(devPts)) {
291 SkPoint* pts = lines->push_back_n(4);
292 pts[0] = devPts[0];
293 pts[1] = devPts[1];
294 pts[2] = devPts[1];
295 pts[3] = devPts[2];
296 } else {
297 // when in perspective keep conics in src space
298 SkPoint* cPts = persp ? chopPnts : devPts;
299 SkPoint* pts = conics->push_back_n(3);
300 pts[0] = cPts[0];
301 pts[1] = cPts[1];
302 pts[2] = cPts[2];
303 conicWeights->push_back() = dst[i].fW;
304 }
305 }
306 }
reed@google.com277c3f82013-05-31 15:17:50 +0000307 break;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000308 }
309 case SkPath::kMove_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000310 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000311 case SkPath::kLine_Verb:
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000312 m.mapPoints(devPts, pathPts, 2);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000313 bounds.setBounds(devPts, 2);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000314 bounds.outset(SK_Scalar1, SK_Scalar1);
315 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000316 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000317 SkPoint* pts = lines->push_back_n(2);
318 pts[0] = devPts[0];
319 pts[1] = devPts[1];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000320 }
321 break;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000322 case SkPath::kQuad_Verb: {
323 SkPoint choppedPts[5];
324 // Chopping the quad helps when the quad is either degenerate or nearly degenerate.
325 // When it is degenerate it allows the approximation with lines to work since the
326 // chop point (if there is one) will be at the parabola's vertex. In the nearly
327 // degenerate the QuadUVMatrix computed for the points is almost singular which
328 // can cause rendering artifacts.
329 int n = SkChopQuadAtMaxCurvature(pathPts, choppedPts);
330 for (int i = 0; i < n; ++i) {
331 SkPoint* quadPts = choppedPts + i * 2;
332 m.mapPoints(devPts, quadPts, 3);
333 bounds.setBounds(devPts, 3);
334 bounds.outset(SK_Scalar1, SK_Scalar1);
335 bounds.roundOut(&ibounds);
336
337 if (SkIRect::Intersects(devClipBounds, ibounds)) {
338 int subdiv = num_quad_subdivs(devPts);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000339 SkASSERT(subdiv >= -1);
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000340 if (-1 == subdiv) {
341 SkPoint* pts = lines->push_back_n(4);
342 pts[0] = devPts[0];
343 pts[1] = devPts[1];
344 pts[2] = devPts[1];
345 pts[3] = devPts[2];
346 } else {
347 // when in perspective keep quads in src space
348 SkPoint* qPts = persp ? quadPts : devPts;
349 SkPoint* pts = quads->push_back_n(3);
350 pts[0] = qPts[0];
351 pts[1] = qPts[1];
352 pts[2] = qPts[2];
353 quadSubdivCnts->push_back() = subdiv;
354 totalQuadCount += 1 << subdiv;
355 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000356 }
357 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000358 break;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000359 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000360 case SkPath::kCubic_Verb:
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000361 m.mapPoints(devPts, pathPts, 4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000362 bounds.setBounds(devPts, 4);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000363 bounds.outset(SK_Scalar1, SK_Scalar1);
364 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000365 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000366 PREALLOC_PTARRAY(32) q;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000367 // we don't need a direction if we aren't constraining the subdivision
368 static const SkPath::Direction kDummyDir = SkPath::kCCW_Direction;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000369 // We convert cubics to quadratics (for now).
370 // In perspective have to do conversion in src space.
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000371 if (persp) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000372 SkScalar tolScale =
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000373 GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m,
374 path.getBounds());
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000375 GrPathUtils::convertCubicToQuads(pathPts, tolScale, false, kDummyDir, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000376 } else {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000377 GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, false, kDummyDir, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000378 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000379 for (int i = 0; i < q.count(); i += 3) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000380 SkPoint* qInDevSpace;
381 // bounds has to be calculated in device space, but q is
382 // in src space when there is perspective.
383 if (persp) {
384 m.mapPoints(devPts, &q[i], 3);
385 bounds.setBounds(devPts, 3);
386 qInDevSpace = devPts;
387 } else {
388 bounds.setBounds(&q[i], 3);
389 qInDevSpace = &q[i];
390 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000391 bounds.outset(SK_Scalar1, SK_Scalar1);
392 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000393 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000394 int subdiv = num_quad_subdivs(qInDevSpace);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000395 SkASSERT(subdiv >= -1);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000396 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000397 SkPoint* pts = lines->push_back_n(4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000398 // lines should always be in device coords
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000399 pts[0] = qInDevSpace[0];
400 pts[1] = qInDevSpace[1];
401 pts[2] = qInDevSpace[1];
402 pts[3] = qInDevSpace[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000403 } else {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000404 SkPoint* pts = quads->push_back_n(3);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000405 // q is already in src space when there is no
406 // perspective and dev coords otherwise.
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000407 pts[0] = q[0 + i];
408 pts[1] = q[1 + i];
409 pts[2] = q[2 + i];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000410 quadSubdivCnts->push_back() = subdiv;
411 totalQuadCount += 1 << subdiv;
412 }
413 }
414 }
415 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000416 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000417 case SkPath::kClose_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000418 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000419 case SkPath::kDone_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000420 return totalQuadCount;
421 }
422 }
423}
424
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000425struct LineVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000426 SkPoint fPos;
egdaniele27065a2014-11-06 08:00:48 -0800427 float fCoverage;
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000428};
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000429
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000430struct BezierVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000431 SkPoint fPos;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000432 union {
433 struct {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000434 SkScalar fK;
435 SkScalar fL;
436 SkScalar fM;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000437 } fConic;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000438 SkVector fQuadCoord;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000439 struct {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000440 SkScalar fBogus[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000441 };
442 };
443};
egdaniel@google.com5383a752013-07-12 20:15:34 +0000444
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000445GR_STATIC_ASSERT(sizeof(BezierVertex) == 3 * sizeof(SkPoint));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000446
447void intersect_lines(const SkPoint& ptA, const SkVector& normA,
448 const SkPoint& ptB, const SkVector& normB,
449 SkPoint* result) {
450
451 SkScalar lineAW = -normA.dot(ptA);
452 SkScalar lineBW = -normB.dot(ptB);
453
454 SkScalar wInv = SkScalarMul(normA.fX, normB.fY) -
egdaniel@google.com5383a752013-07-12 20:15:34 +0000455 SkScalarMul(normA.fY, normB.fX);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000456 wInv = SkScalarInvert(wInv);
457
458 result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY);
459 result->fX = SkScalarMul(result->fX, wInv);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000460
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000461 result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW);
462 result->fY = SkScalarMul(result->fY, wInv);
463}
464
joshualitt5ead6da2014-10-22 16:00:29 -0700465void set_uv_quad(const SkPoint qpts[3], BezierVertex verts[kQuadNumVertices]) {
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000466 // this should be in the src space, not dev coords, when we have perspective
467 GrPathUtils::QuadUVMatrix DevToUV(qpts);
joshualitt5ead6da2014-10-22 16:00:29 -0700468 DevToUV.apply<kQuadNumVertices, sizeof(BezierVertex), sizeof(SkPoint)>(verts);
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000469}
470
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000471void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice,
joshualitt5ead6da2014-10-22 16:00:29 -0700472 const SkMatrix* toSrc, BezierVertex verts[kQuadNumVertices],
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000473 SkRect* devBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000474 SkASSERT(!toDevice == !toSrc);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000475 // original quad is specified by tri a,b,c
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000476 SkPoint a = qpts[0];
477 SkPoint b = qpts[1];
478 SkPoint c = qpts[2];
479
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000480 if (toDevice) {
481 toDevice->mapPoints(&a, 1);
482 toDevice->mapPoints(&b, 1);
483 toDevice->mapPoints(&c, 1);
484 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000485 // make a new poly where we replace a and c by a 1-pixel wide edges orthog
486 // to edges ab and bc:
487 //
488 // before | after
489 // | b0
490 // b |
491 // |
492 // | a0 c0
493 // a c | a1 c1
494 //
495 // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c,
496 // respectively.
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000497 BezierVertex& a0 = verts[0];
498 BezierVertex& a1 = verts[1];
499 BezierVertex& b0 = verts[2];
500 BezierVertex& c0 = verts[3];
501 BezierVertex& c1 = verts[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000502
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000503 SkVector ab = b;
504 ab -= a;
505 SkVector ac = c;
506 ac -= a;
507 SkVector cb = b;
508 cb -= c;
509
510 // We should have already handled degenerates
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000511 SkASSERT(ab.length() > 0 && cb.length() > 0);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000512
513 ab.normalize();
514 SkVector abN;
515 abN.setOrthog(ab, SkVector::kLeft_Side);
516 if (abN.dot(ac) > 0) {
517 abN.negate();
518 }
519
520 cb.normalize();
521 SkVector cbN;
522 cbN.setOrthog(cb, SkVector::kLeft_Side);
523 if (cbN.dot(ac) < 0) {
524 cbN.negate();
525 }
526
527 a0.fPos = a;
528 a0.fPos += abN;
529 a1.fPos = a;
530 a1.fPos -= abN;
531
532 c0.fPos = c;
533 c0.fPos += cbN;
534 c1.fPos = c;
535 c1.fPos -= cbN;
536
537 intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos);
joshualitt5ead6da2014-10-22 16:00:29 -0700538 devBounds->growToInclude(&verts[0].fPos, sizeof(BezierVertex), kQuadNumVertices);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000539
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000540 if (toSrc) {
joshualitt5ead6da2014-10-22 16:00:29 -0700541 toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(BezierVertex), kQuadNumVertices);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000542 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000543}
544
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000545// Equations based off of Loop-Blinn Quadratic GPU Rendering
egdaniel@google.com5383a752013-07-12 20:15:34 +0000546// Input Parametric:
547// 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)
548// Output Implicit:
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000549// f(x, y, w) = f(P) = K^2 - LM
550// K = dot(k, P), L = dot(l, P), M = dot(m, P)
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000551// k, l, m are calculated in function GrPathUtils::getConicKLM
joshualitt5ead6da2014-10-22 16:00:29 -0700552void set_conic_coeffs(const SkPoint p[3], BezierVertex verts[kQuadNumVertices],
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000553 const SkScalar weight) {
554 SkScalar klm[9];
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000555
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000556 GrPathUtils::getConicKLM(p, weight, klm);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000557
joshualitt5ead6da2014-10-22 16:00:29 -0700558 for (int i = 0; i < kQuadNumVertices; ++i) {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000559 const SkPoint pnt = verts[i].fPos;
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000560 verts[i].fConic.fK = pnt.fX * klm[0] + pnt.fY * klm[1] + klm[2];
561 verts[i].fConic.fL = pnt.fX * klm[3] + pnt.fY * klm[4] + klm[5];
562 verts[i].fConic.fM = pnt.fX * klm[6] + pnt.fY * klm[7] + klm[8];
egdaniel@google.com5383a752013-07-12 20:15:34 +0000563 }
564}
565
566void add_conics(const SkPoint p[3],
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000567 const SkScalar weight,
egdaniel@google.com5383a752013-07-12 20:15:34 +0000568 const SkMatrix* toDevice,
569 const SkMatrix* toSrc,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000570 BezierVertex** vert,
egdaniel@google.com5383a752013-07-12 20:15:34 +0000571 SkRect* devBounds) {
572 bloat_quad(p, toDevice, toSrc, *vert, devBounds);
573 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,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000581 BezierVertex** vert,
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000582 SkRect* devBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000583 SkASSERT(subdiv >= 0);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000584 if (subdiv) {
585 SkPoint newP[5];
586 SkChopQuadAtHalf(p, newP);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000587 add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert, devBounds);
588 add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert, devBounds);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000589 } else {
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000590 bloat_quad(p, toDevice, toSrc, *vert, devBounds);
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000591 set_uv_quad(p, *vert);
joshualitt5ead6da2014-10-22 16:00:29 -0700592 *vert += kQuadNumVertices;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000593 }
594}
595
596void add_line(const SkPoint p[2],
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000597 const SkMatrix* toSrc,
egdaniele27065a2014-11-06 08:00:48 -0800598 uint8_t coverage,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000599 LineVertex** vert) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000600 const SkPoint& a = p[0];
601 const SkPoint& b = p[1];
602
robertphillips@google.comada90da2013-09-18 22:14:49 +0000603 SkVector ortho, vec = b;
604 vec -= a;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000605
robertphillips@google.comada90da2013-09-18 22:14:49 +0000606 if (vec.setLength(SK_ScalarHalf)) {
607 // Create a vector orthogonal to 'vec' and of unit length
608 ortho.fX = 2.0f * vec.fY;
609 ortho.fY = -2.0f * vec.fX;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000610
egdaniele27065a2014-11-06 08:00:48 -0800611 float floatCoverage = GrNormalizeByteToFloat(coverage);
612
robertphillips@google.comada90da2013-09-18 22:14:49 +0000613 (*vert)[0].fPos = a;
egdaniele27065a2014-11-06 08:00:48 -0800614 (*vert)[0].fCoverage = floatCoverage;
robertphillips@google.comada90da2013-09-18 22:14:49 +0000615 (*vert)[1].fPos = b;
egdaniele27065a2014-11-06 08:00:48 -0800616 (*vert)[1].fCoverage = floatCoverage;
robertphillips@google.comada90da2013-09-18 22:14:49 +0000617 (*vert)[2].fPos = a - vec + ortho;
618 (*vert)[2].fCoverage = 0;
619 (*vert)[3].fPos = b + vec + ortho;
620 (*vert)[3].fCoverage = 0;
621 (*vert)[4].fPos = a - vec - ortho;
622 (*vert)[4].fCoverage = 0;
623 (*vert)[5].fPos = b + vec - ortho;
624 (*vert)[5].fCoverage = 0;
625
bsalomon49f085d2014-09-05 13:34:00 -0700626 if (toSrc) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000627 toSrc->mapPointsWithStride(&(*vert)->fPos,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000628 sizeof(LineVertex),
joshualitt5ead6da2014-10-22 16:00:29 -0700629 kLineSegNumVertices);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000630 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000631 } else {
632 // just make it degenerate and likely offscreen
joshualitt5ead6da2014-10-22 16:00:29 -0700633 for (int i = 0; i < kLineSegNumVertices; ++i) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000634 (*vert)[i].fPos.set(SK_ScalarMax, SK_ScalarMax);
635 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000636 }
637
joshualitt5ead6da2014-10-22 16:00:29 -0700638 *vert += kLineSegNumVertices;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000639}
640
641}
642
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000643///////////////////////////////////////////////////////////////////////////////
644
joshualitt9853cce2014-11-17 14:22:48 -0800645bool GrAAHairLinePathRenderer::createLineGeom(GrDrawTarget* target,
646 GrDrawState* drawState,
robertphillips@google.comada90da2013-09-18 22:14:49 +0000647 GrDrawTarget::AutoReleaseGeometry* arg,
joshualitt9853cce2014-11-17 14:22:48 -0800648 SkRect* devBounds,
649 const SkPath& path,
650 const PtArray& lines,
651 int lineCnt) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000652 const SkMatrix& viewM = drawState->getViewMatrix();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000653
joshualitt5ead6da2014-10-22 16:00:29 -0700654 int vertCnt = kLineSegNumVertices * lineCnt;
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000655
joshualitt2dd1ae02014-12-03 06:24:10 -0800656 size_t vstride = drawState->getGeometryProcessor()->getVertexStride();
657 SkASSERT(vstride == sizeof(LineVertex));
658 if (!arg->set(target, vertCnt, vstride, 0)) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000659 return false;
660 }
661
662 LineVertex* verts = reinterpret_cast<LineVertex*>(arg->vertices());
663
664 const SkMatrix* toSrc = NULL;
665 SkMatrix ivm;
666
667 if (viewM.hasPerspective()) {
668 if (viewM.invert(&ivm)) {
669 toSrc = &ivm;
670 }
671 }
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000672 devBounds->set(lines.begin(), lines.count());
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000673 for (int i = 0; i < lineCnt; ++i) {
egdaniele27065a2014-11-06 08:00:48 -0800674 add_line(&lines[2*i], toSrc, drawState->getCoverage(), &verts);
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000675 }
skia.committer@gmail.comf91e3d42013-09-20 07:01:33 +0000676 // All the verts computed by add_line are within sqrt(1^2 + 0.5^2) of the end points.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000677 static const SkScalar kSqrtOfOneAndAQuarter = 1.118f;
robertphillips@google.com52c75262013-09-19 16:36:43 +0000678 // Add a little extra to account for vector normalization precision.
679 static const SkScalar kOutset = kSqrtOfOneAndAQuarter + SK_Scalar1 / 20;
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000680 devBounds->outset(kOutset, kOutset);
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000681
682 return true;
683}
684
joshualitt9853cce2014-11-17 14:22:48 -0800685bool GrAAHairLinePathRenderer::createBezierGeom(GrDrawTarget* target,
686 GrDrawState* drawState,
687 GrDrawTarget::AutoReleaseGeometry* arg,
688 SkRect* devBounds,
689 const SkPath& path,
690 const PtArray& quads,
691 int quadCnt,
692 const PtArray& conics,
693 int conicCnt,
694 const IntArray& qSubdivs,
joshualitt2dd1ae02014-12-03 06:24:10 -0800695 const FloatArray& cWeights,
696 size_t vertexStride) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000697 const SkMatrix& viewM = drawState->getViewMatrix();
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000698
joshualitt5ead6da2014-10-22 16:00:29 -0700699 int vertCnt = kQuadNumVertices * quadCnt + kQuadNumVertices * conicCnt;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000700
joshualitt2dd1ae02014-12-03 06:24:10 -0800701 if (!arg->set(target, vertCnt, vertexStride, 0)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000702 return false;
703 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000704
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000705 BezierVertex* verts = reinterpret_cast<BezierVertex*>(arg->vertices());
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000706
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000707 const SkMatrix* toDevice = NULL;
708 const SkMatrix* toSrc = NULL;
709 SkMatrix ivm;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000710
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000711 if (viewM.hasPerspective()) {
712 if (viewM.invert(&ivm)) {
713 toDevice = &viewM;
714 toSrc = &ivm;
715 }
716 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000717
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000718 // Seed the dev bounds with some pts known to be inside. Each quad and conic grows the bounding
719 // box to include its vertices.
720 SkPoint seedPts[2];
721 if (quadCnt) {
722 seedPts[0] = quads[0];
723 seedPts[1] = quads[2];
724 } else if (conicCnt) {
725 seedPts[0] = conics[0];
726 seedPts[1] = conics[2];
727 }
bsalomon49f085d2014-09-05 13:34:00 -0700728 if (toDevice) {
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000729 toDevice->mapPoints(seedPts, 2);
730 }
731 devBounds->set(seedPts[0], seedPts[1]);
732
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000733 int unsubdivQuadCnt = quads.count() / 3;
734 for (int i = 0; i < unsubdivQuadCnt; ++i) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000735 SkASSERT(qSubdivs[i] >= 0);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000736 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts, devBounds);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000737 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000738
egdaniel@google.com5383a752013-07-12 20:15:34 +0000739 // Start Conics
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000740 for (int i = 0; i < conicCnt; ++i) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000741 add_conics(&conics[3*i], cWeights[i], toDevice, toSrc, &verts, devBounds);
742 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000743 return true;
744}
745
joshualitt9853cce2014-11-17 14:22:48 -0800746bool GrAAHairLinePathRenderer::canDrawPath(const GrDrawTarget* target,
747 const GrDrawState* drawState,
748 const SkPath& path,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000749 const SkStrokeRec& stroke,
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000750 bool antiAlias) const {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000751 if (!antiAlias) {
752 return false;
753 }
754
755 if (!IsStrokeHairlineOrEquivalent(stroke,
joshualitt9853cce2014-11-17 14:22:48 -0800756 drawState->getViewMatrix(),
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000757 NULL)) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000758 return false;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000759 }
760
robertphillips@google.come79f3202014-02-11 16:30:21 +0000761 if (SkPath::kLine_SegmentMask == path.getSegmentMasks() ||
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000762 target->caps()->shaderDerivativeSupport()) {
763 return true;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000764 }
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000765 return false;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000766}
767
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000768template <class VertexType>
769bool check_bounds(GrDrawState* drawState, const SkRect& devBounds, void* vertices, int vCount)
770{
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000771 SkRect tolDevBounds = devBounds;
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000772 // The bounds ought to be tight, but in perspective the below code runs the verts
773 // through the view matrix to get back to dev coords, which can introduce imprecision.
774 if (drawState->getViewMatrix().hasPerspective()) {
775 tolDevBounds.outset(SK_Scalar1 / 1000, SK_Scalar1 / 1000);
776 } else {
777 // Non-persp matrices cause this path renderer to draw in device space.
778 SkASSERT(drawState->getViewMatrix().isIdentity());
779 }
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000780 SkRect actualBounds;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000781
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000782 VertexType* verts = reinterpret_cast<VertexType*>(vertices);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000783 bool first = true;
784 for (int i = 0; i < vCount; ++i) {
785 SkPoint pos = verts[i].fPos;
786 // This is a hack to workaround the fact that we move some degenerate segments offscreen.
787 if (SK_ScalarMax == pos.fX) {
788 continue;
789 }
790 drawState->getViewMatrix().mapPoints(&pos, 1);
791 if (first) {
792 actualBounds.set(pos.fX, pos.fY, pos.fX, pos.fY);
793 first = false;
794 } else {
795 actualBounds.growToInclude(pos.fX, pos.fY);
796 }
797 }
798 if (!first) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000799 return tolDevBounds.contains(actualBounds);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000800 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000801
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000802 return true;
803}
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000804
joshualitt9853cce2014-11-17 14:22:48 -0800805bool GrAAHairLinePathRenderer::onDrawPath(GrDrawTarget* target,
806 GrDrawState* drawState,
807 const SkPath& path,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000808 const SkStrokeRec& stroke,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000809 bool antiAlias) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000810 SkScalar hairlineCoverage;
joshualitt9853cce2014-11-17 14:22:48 -0800811 if (IsStrokeHairlineOrEquivalent(stroke, drawState->getViewMatrix(),
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000812 &hairlineCoverage)) {
joshualitt9853cce2014-11-17 14:22:48 -0800813 uint8_t newCoverage = SkScalarRoundToInt(hairlineCoverage * drawState->getCoverage());
814 drawState->setCoverage(newCoverage);
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000815 }
816
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000817 SkIRect devClipBounds;
818 target->getClip()->getConservativeBounds(drawState->getRenderTarget(), &devClipBounds);
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000819
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000820 int lineCnt;
821 int quadCnt;
822 int conicCnt;
823 PREALLOC_PTARRAY(128) lines;
824 PREALLOC_PTARRAY(128) quads;
825 PREALLOC_PTARRAY(128) conics;
826 IntArray qSubdivs;
827 FloatArray cWeights;
robertphillips@google.come79f3202014-02-11 16:30:21 +0000828 quadCnt = generate_lines_and_quads(path, drawState->getViewMatrix(), devClipBounds,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000829 &lines, &quads, &conics, &qSubdivs, &cWeights);
830 lineCnt = lines.count() / 2;
831 conicCnt = conics.count() / 3;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000832
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000833 // do lines first
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000834 if (lineCnt) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000835 GrDrawTarget::AutoReleaseGeometry arg;
836 SkRect devBounds;
837
joshualitt2dd1ae02014-12-03 06:24:10 -0800838 uint32_t gpFlags = GrDefaultGeoProcFactory::kPosition_GPType |
839 GrDefaultGeoProcFactory::kCoverage_GPType;
840 GrDrawState::AutoRestoreEffects are(drawState);
841 drawState->setGeometryProcessor(GrDefaultGeoProcFactory::Create(gpFlags))->unref();
842
joshualitt9853cce2014-11-17 14:22:48 -0800843 if (!this->createLineGeom(target,
844 drawState,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000845 &arg,
joshualitt9853cce2014-11-17 14:22:48 -0800846 &devBounds,
847 path,
848 lines,
849 lineCnt)) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000850 return false;
851 }
852
robertphillips@google.comada90da2013-09-18 22:14:49 +0000853 // createLineGeom transforms the geometry to device space when the matrix does not have
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000854 // perspective.
joshualitt9853cce2014-11-17 14:22:48 -0800855 GrDrawState::AutoViewMatrixRestore avmr;
856 if (!drawState->getViewMatrix().hasPerspective() && !avmr.setIdentity(drawState)) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000857 return false;
858 }
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000859
860 // Check devBounds
robertphillips@google.com52c75262013-09-19 16:36:43 +0000861 SkASSERT(check_bounds<LineVertex>(drawState, devBounds, arg.vertices(),
joshualitt5ead6da2014-10-22 16:00:29 -0700862 kLineSegNumVertices * lineCnt));
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000863
864 {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000865 target->setIndexSourceToBuffer(fLinesIndexBuffer);
866 int lines = 0;
867 while (lines < lineCnt) {
joshualitt5ead6da2014-10-22 16:00:29 -0700868 int n = SkTMin(lineCnt - lines, kLineSegsNumInIdxBuffer);
joshualitt9853cce2014-11-17 14:22:48 -0800869 target->drawIndexed(drawState,
870 kTriangles_GrPrimitiveType,
joshualitt5ead6da2014-10-22 16:00:29 -0700871 kLineSegNumVertices*lines, // startV
872 0, // startI
873 kLineSegNumVertices*n, // vCount
874 kIdxsPerLineSeg*n, // iCount
robertphillips@google.comada90da2013-09-18 22:14:49 +0000875 &devBounds);
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000876 lines += n;
877 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000878 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000879 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000880
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000881 // then quadratics/conics
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000882 if (quadCnt || conicCnt) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000883 GrDrawTarget::AutoReleaseGeometry arg;
884 SkRect devBounds;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000885
joshualitt9853cce2014-11-17 14:22:48 -0800886 if (!this->createBezierGeom(target,
887 drawState,
888 &arg,
889 &devBounds,
890 path,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000891 quads,
892 quadCnt,
893 conics,
894 conicCnt,
895 qSubdivs,
joshualitt2dd1ae02014-12-03 06:24:10 -0800896 cWeights,
897 sizeof(BezierVertex))) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000898 return false;
899 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000900
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000901 // createGeom transforms the geometry to device space when the matrix does not have
902 // perspective.
joshualitt9853cce2014-11-17 14:22:48 -0800903 GrDrawState::AutoViewMatrixRestore avmr;
904 if (!drawState->getViewMatrix().hasPerspective() && !avmr.setIdentity(drawState)) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000905 return false;
906 }
joshualitt9853cce2014-11-17 14:22:48 -0800907
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000908
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000909 // Check devBounds
910 SkASSERT(check_bounds<BezierVertex>(drawState, devBounds, arg.vertices(),
joshualitt5ead6da2014-10-22 16:00:29 -0700911 kQuadNumVertices * quadCnt + kQuadNumVertices * conicCnt));
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000912
commit-bot@chromium.org88462472013-08-23 21:01:52 +0000913 if (quadCnt > 0) {
joshualittb0a8a372014-09-23 09:50:21 -0700914 GrGeometryProcessor* hairQuadProcessor =
915 GrQuadEffect::Create(kHairlineAA_GrProcessorEdgeType, *target->caps());
916 SkASSERT(hairQuadProcessor);
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000917 GrDrawState::AutoRestoreEffects are(drawState);
918 target->setIndexSourceToBuffer(fQuadsIndexBuffer);
joshualitt2dd1ae02014-12-03 06:24:10 -0800919
joshualittb0a8a372014-09-23 09:50:21 -0700920 drawState->setGeometryProcessor(hairQuadProcessor)->unref();
commit-bot@chromium.org88462472013-08-23 21:01:52 +0000921 int quads = 0;
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000922 while (quads < quadCnt) {
joshualitt5ead6da2014-10-22 16:00:29 -0700923 int n = SkTMin(quadCnt - quads, kQuadsNumInIdxBuffer);
joshualitt9853cce2014-11-17 14:22:48 -0800924 target->drawIndexed(drawState,
925 kTriangles_GrPrimitiveType,
joshualitt5ead6da2014-10-22 16:00:29 -0700926 kQuadNumVertices*quads, // startV
927 0, // startI
928 kQuadNumVertices*n, // vCount
929 kIdxsPerQuad*n, // iCount
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000930 &devBounds);
931 quads += n;
932 }
933 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000934
commit-bot@chromium.org88462472013-08-23 21:01:52 +0000935 if (conicCnt > 0) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000936 GrDrawState::AutoRestoreEffects are(drawState);
joshualittb0a8a372014-09-23 09:50:21 -0700937 GrGeometryProcessor* hairConicProcessor = GrConicEffect::Create(
938 kHairlineAA_GrProcessorEdgeType, *target->caps());
939 SkASSERT(hairConicProcessor);
joshualitt2dd1ae02014-12-03 06:24:10 -0800940
joshualittb0a8a372014-09-23 09:50:21 -0700941 drawState->setGeometryProcessor(hairConicProcessor)->unref();
commit-bot@chromium.org88462472013-08-23 21:01:52 +0000942 int conics = 0;
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000943 while (conics < conicCnt) {
joshualitt5ead6da2014-10-22 16:00:29 -0700944 int n = SkTMin(conicCnt - conics, kQuadsNumInIdxBuffer);
joshualitt9853cce2014-11-17 14:22:48 -0800945 target->drawIndexed(drawState,
946 kTriangles_GrPrimitiveType,
joshualitt5ead6da2014-10-22 16:00:29 -0700947 kQuadNumVertices*(quadCnt + conics), // startV
948 0, // startI
949 kQuadNumVertices*n, // vCount
950 kIdxsPerQuad*n, // iCount
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000951 &devBounds);
952 conics += n;
953 }
egdaniel@google.com5383a752013-07-12 20:15:34 +0000954 }
955 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000956
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000957 target->resetIndexSource();
bsalomon@google.com4647f902013-03-26 14:45:27 +0000958
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000959 return true;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000960}