blob: 15a5569b3072c73c05471af3d7a0486042ce89f7 [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"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000018#include "SkGeometry.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000019#include "SkStroke.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000020#include "SkTemplates.h"
21
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000022#include "effects/GrBezierEffect.h"
bsalomon@google.com4647f902013-03-26 14:45:27 +000023
bsalomon@google.comaeb21602011-08-30 18:13:44 +000024// quadratics are rendered as 5-sided polys in order to bound the
25// AA stroke around the center-curve. See comments in push_quad_index_buffer and
egdaniel@google.com5383a752013-07-12 20:15:34 +000026// bloat_quad. Quadratics and conics share an index buffer
bsalomon@google.comaeb21602011-08-30 18:13:44 +000027
robertphillips@google.comada90da2013-09-18 22:14:49 +000028// lines are rendered as:
29// *______________*
30// |\ -_______ /|
31// | \ \ / |
32// | *--------* |
33// | / ______/ \ |
34// */_-__________\*
35// For: 6 vertices and 18 indices (for 6 triangles)
bsalomon@google.comaeb21602011-08-30 18:13:44 +000036
joshualitt5ead6da2014-10-22 16:00:29 -070037// Each quadratic is rendered as a five sided polygon. This poly bounds
38// the quadratic's bounding triangle but has been expanded so that the
39// 1-pixel wide area around the curve is inside the poly.
40// If a,b,c are the original control points then the poly a0,b0,c0,c1,a1
41// that is rendered would look like this:
42// b0
43// b
44//
45// a0 c0
46// a c
47// a1 c1
48// Each is drawn as three triangles specified by these 9 indices:
49static const uint16_t kQuadIdxBufPattern[] = {
50 0, 1, 2,
51 2, 4, 3,
52 1, 4, 2
53};
bsalomon@google.comaeb21602011-08-30 18:13:44 +000054
joshualitt5ead6da2014-10-22 16:00:29 -070055static const int kIdxsPerQuad = SK_ARRAY_COUNT(kQuadIdxBufPattern);
56static const int kQuadNumVertices = 5;
57static const int kQuadsNumInIdxBuffer = 256;
jvanverth@google.com681ccf02013-08-16 14:51:51 +000058
bsalomon@google.comaeb21602011-08-30 18:13:44 +000059
joshualitt5ead6da2014-10-22 16:00:29 -070060// Each line segment is rendered as two quads and two triangles.
61// p0 and p1 have alpha = 1 while all other points have alpha = 0.
62// The four external points are offset 1 pixel perpendicular to the
63// line and half a pixel parallel to the line.
64//
65// p4 p5
66// p0 p1
67// p2 p3
68//
69// Each is drawn as six triangles specified by these 18 indices:
jvanverth@google.com681ccf02013-08-16 14:51:51 +000070
joshualitt5ead6da2014-10-22 16:00:29 -070071static const uint16_t kLineSegIdxBufPattern[] = {
72 0, 1, 3,
73 0, 3, 2,
74 0, 4, 5,
75 0, 5, 1,
76 0, 2, 4,
77 1, 5, 3
78};
skia.committer@gmail.com74758112013-08-17 07:01:54 +000079
joshualitt5ead6da2014-10-22 16:00:29 -070080static const int kIdxsPerLineSeg = SK_ARRAY_COUNT(kLineSegIdxBufPattern);
81static const int kLineSegNumVertices = 6;
82static const int kLineSegsNumInIdxBuffer = 256;
bsalomon@google.comaeb21602011-08-30 18:13:44 +000083
84GrPathRenderer* GrAAHairLinePathRenderer::Create(GrContext* context) {
bsalomon@google.coma8a6a322011-09-23 14:19:58 +000085 GrGpu* gpu = context->getGpu();
joshualitt5ead6da2014-10-22 16:00:29 -070086 GrIndexBuffer* qIdxBuf = gpu->createInstancedIndexBuffer(kQuadIdxBufPattern,
87 kIdxsPerQuad,
88 kQuadsNumInIdxBuffer,
89 kQuadNumVertices);
bsalomon@google.coma8a6a322011-09-23 14:19:58 +000090 SkAutoTUnref<GrIndexBuffer> qIdxBuffer(qIdxBuf);
joshualitt5ead6da2014-10-22 16:00:29 -070091 GrIndexBuffer* lIdxBuf = gpu->createInstancedIndexBuffer(kLineSegIdxBufPattern,
92 kIdxsPerLineSeg,
93 kLineSegsNumInIdxBuffer,
94 kLineSegNumVertices);
jvanverth@google.com681ccf02013-08-16 14:51:51 +000095 SkAutoTUnref<GrIndexBuffer> lIdxBuffer(lIdxBuf);
tomhudson@google.comc377baf2012-07-09 20:17:56 +000096 return SkNEW_ARGS(GrAAHairLinePathRenderer,
jvanverth@google.com681ccf02013-08-16 14:51:51 +000097 (context, lIdxBuf, qIdxBuf));
bsalomon@google.comaeb21602011-08-30 18:13:44 +000098}
99
100GrAAHairLinePathRenderer::GrAAHairLinePathRenderer(
101 const GrContext* context,
102 const GrIndexBuffer* linesIndexBuffer,
103 const GrIndexBuffer* quadsIndexBuffer) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000104 fLinesIndexBuffer = linesIndexBuffer;
105 linesIndexBuffer->ref();
106 fQuadsIndexBuffer = quadsIndexBuffer;
107 quadsIndexBuffer->ref();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000108}
109
110GrAAHairLinePathRenderer::~GrAAHairLinePathRenderer() {
111 fLinesIndexBuffer->unref();
112 fQuadsIndexBuffer->unref();
113}
114
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000115namespace {
116
bsalomon@google.com92669012011-09-27 19:10:05 +0000117#define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true>
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000118
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000119// Takes 178th time of logf on Z600 / VC2010
120int get_float_exp(float x) {
121 GR_STATIC_ASSERT(sizeof(int) == sizeof(float));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000122#ifdef SK_DEBUG
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000123 static bool tested;
124 if (!tested) {
125 tested = true;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000126 SkASSERT(get_float_exp(0.25f) == -2);
127 SkASSERT(get_float_exp(0.3f) == -2);
128 SkASSERT(get_float_exp(0.5f) == -1);
129 SkASSERT(get_float_exp(1.f) == 0);
130 SkASSERT(get_float_exp(2.f) == 1);
131 SkASSERT(get_float_exp(2.5f) == 1);
132 SkASSERT(get_float_exp(8.f) == 3);
133 SkASSERT(get_float_exp(100.f) == 6);
134 SkASSERT(get_float_exp(1000.f) == 9);
135 SkASSERT(get_float_exp(1024.f) == 10);
136 SkASSERT(get_float_exp(3000000.f) == 21);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000137 }
138#endif
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000139 const int* iptr = (const int*)&x;
140 return (((*iptr) & 0x7f800000) >> 23) - 127;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000141}
142
egdaniel@google.com5383a752013-07-12 20:15:34 +0000143// Uses the max curvature function for quads to estimate
144// where to chop the conic. If the max curvature is not
145// found along the curve segment it will return 1 and
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000146// dst[0] is the original conic. If it returns 2 the dst[0]
egdaniel@google.com5383a752013-07-12 20:15:34 +0000147// and dst[1] are the two new conics.
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000148int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000149 SkScalar t = SkFindQuadMaxCurvature(src);
150 if (t == 0) {
151 if (dst) {
152 dst[0].set(src, weight);
153 }
154 return 1;
155 } else {
156 if (dst) {
157 SkConic conic;
158 conic.set(src, weight);
159 conic.chopAt(t, dst);
160 }
161 return 2;
162 }
163}
164
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000165// Calls split_conic on the entire conic and then once more on each subsection.
166// Most cases will result in either 1 conic (chop point is not within t range)
167// or 3 points (split once and then one subsection is split again).
168int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) {
169 SkConic dstTemp[2];
170 int conicCnt = split_conic(src, dstTemp, weight);
171 if (2 == conicCnt) {
172 int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW);
173 conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW);
174 } else {
175 dst[0] = dstTemp[0];
176 }
177 return conicCnt;
178}
179
egdaniel@google.com5383a752013-07-12 20:15:34 +0000180// returns 0 if quad/conic is degen or close to it
181// in this case approx the path with lines
182// otherwise returns 1
183int is_degen_quad_or_conic(const SkPoint p[3]) {
184 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
185 static const SkScalar gDegenerateToLineTolSqd =
186 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
187
188 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
189 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
190 return 1;
191 }
192
193 SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
194 if (dsqd < gDegenerateToLineTolSqd) {
195 return 1;
196 }
197
198 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
199 return 1;
200 }
201 return 0;
202}
203
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000204// we subdivide the quads to avoid huge overfill
205// if it returns -1 then should be drawn as lines
206int num_quad_subdivs(const SkPoint p[3]) {
207 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000208 static const SkScalar gDegenerateToLineTolSqd =
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000209 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000210
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000211 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
212 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000213 return -1;
214 }
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000215
bsalomon@google.com81712882012-11-01 17:12:34 +0000216 SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000217 if (dsqd < gDegenerateToLineTolSqd) {
218 return -1;
219 }
220
221 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000222 return -1;
223 }
224
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000225 // tolerance of triangle height in pixels
226 // tuned on windows Quadro FX 380 / Z600
227 // trade off of fill vs cpu time on verts
228 // maybe different when do this using gpu (geo or tess shaders)
229 static const SkScalar gSubdivTol = 175 * SK_Scalar1;
230
robertphillips@google.com7460b372012-04-25 16:54:51 +0000231 if (dsqd <= SkScalarMul(gSubdivTol, gSubdivTol)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000232 return 0;
233 } else {
robertphillips@google.com87379e12013-03-29 12:11:10 +0000234 static const int kMaxSub = 4;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000235 // subdividing the quad reduces d by 4. so we want x = log4(d/tol)
236 // = log4(d*d/tol*tol)/2
237 // = log2(d*d/tol*tol)
238
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000239 // +1 since we're ignoring the mantissa contribution.
240 int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000241 log = SkTMin(SkTMax(0, log), kMaxSub);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000242 return log;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000243 }
244}
245
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000246/**
247 * Generates the lines and quads to be rendered. Lines are always recorded in
248 * device space. We will do a device space bloat to account for the 1pixel
249 * thickness.
250 * Quads are recorded in device space unless m contains
251 * perspective, then in they are in src space. We do this because we will
252 * subdivide large quads to reduce over-fill. This subdivision has to be
253 * performed before applying the perspective matrix.
254 */
255int generate_lines_and_quads(const SkPath& path,
256 const SkMatrix& m,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000257 const SkIRect& devClipBounds,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000258 GrAAHairLinePathRenderer::PtArray* lines,
259 GrAAHairLinePathRenderer::PtArray* quads,
260 GrAAHairLinePathRenderer::PtArray* conics,
261 GrAAHairLinePathRenderer::IntArray* quadSubdivCnts,
262 GrAAHairLinePathRenderer::FloatArray* conicWeights) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000263 SkPath::Iter iter(path, false);
264
265 int totalQuadCount = 0;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000266 SkRect bounds;
267 SkIRect ibounds;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000268
269 bool persp = m.hasPerspective();
270
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000271 for (;;) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000272 SkPoint pathPts[4];
273 SkPoint devPts[4];
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000274 SkPath::Verb verb = iter.next(pathPts);
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000275 switch (verb) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000276 case SkPath::kConic_Verb: {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000277 SkConic dst[4];
278 // We chop the conics to create tighter clipping to hide error
279 // that appears near max curvature of very thin conics. Thin
280 // hyperbolas with high weight still show error.
egdaniel@google.com5383a752013-07-12 20:15:34 +0000281 int conicCnt = chop_conic(pathPts, dst, iter.conicWeight());
282 for (int i = 0; i < conicCnt; ++i) {
283 SkPoint* chopPnts = dst[i].fPts;
284 m.mapPoints(devPts, chopPnts, 3);
285 bounds.setBounds(devPts, 3);
286 bounds.outset(SK_Scalar1, SK_Scalar1);
287 bounds.roundOut(&ibounds);
288 if (SkIRect::Intersects(devClipBounds, ibounds)) {
289 if (is_degen_quad_or_conic(devPts)) {
290 SkPoint* pts = lines->push_back_n(4);
291 pts[0] = devPts[0];
292 pts[1] = devPts[1];
293 pts[2] = devPts[1];
294 pts[3] = devPts[2];
295 } else {
296 // when in perspective keep conics in src space
297 SkPoint* cPts = persp ? chopPnts : devPts;
298 SkPoint* pts = conics->push_back_n(3);
299 pts[0] = cPts[0];
300 pts[1] = cPts[1];
301 pts[2] = cPts[2];
302 conicWeights->push_back() = dst[i].fW;
303 }
304 }
305 }
reed@google.com277c3f82013-05-31 15:17:50 +0000306 break;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000307 }
308 case SkPath::kMove_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000309 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000310 case SkPath::kLine_Verb:
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000311 m.mapPoints(devPts, pathPts, 2);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000312 bounds.setBounds(devPts, 2);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000313 bounds.outset(SK_Scalar1, SK_Scalar1);
314 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000315 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000316 SkPoint* pts = lines->push_back_n(2);
317 pts[0] = devPts[0];
318 pts[1] = devPts[1];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000319 }
320 break;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000321 case SkPath::kQuad_Verb: {
322 SkPoint choppedPts[5];
323 // Chopping the quad helps when the quad is either degenerate or nearly degenerate.
324 // When it is degenerate it allows the approximation with lines to work since the
325 // chop point (if there is one) will be at the parabola's vertex. In the nearly
326 // degenerate the QuadUVMatrix computed for the points is almost singular which
327 // can cause rendering artifacts.
328 int n = SkChopQuadAtMaxCurvature(pathPts, choppedPts);
329 for (int i = 0; i < n; ++i) {
330 SkPoint* quadPts = choppedPts + i * 2;
331 m.mapPoints(devPts, quadPts, 3);
332 bounds.setBounds(devPts, 3);
333 bounds.outset(SK_Scalar1, SK_Scalar1);
334 bounds.roundOut(&ibounds);
335
336 if (SkIRect::Intersects(devClipBounds, ibounds)) {
337 int subdiv = num_quad_subdivs(devPts);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000338 SkASSERT(subdiv >= -1);
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000339 if (-1 == subdiv) {
340 SkPoint* pts = lines->push_back_n(4);
341 pts[0] = devPts[0];
342 pts[1] = devPts[1];
343 pts[2] = devPts[1];
344 pts[3] = devPts[2];
345 } else {
346 // when in perspective keep quads in src space
347 SkPoint* qPts = persp ? quadPts : devPts;
348 SkPoint* pts = quads->push_back_n(3);
349 pts[0] = qPts[0];
350 pts[1] = qPts[1];
351 pts[2] = qPts[2];
352 quadSubdivCnts->push_back() = subdiv;
353 totalQuadCount += 1 << subdiv;
354 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000355 }
356 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000357 break;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000358 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000359 case SkPath::kCubic_Verb:
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000360 m.mapPoints(devPts, pathPts, 4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000361 bounds.setBounds(devPts, 4);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000362 bounds.outset(SK_Scalar1, SK_Scalar1);
363 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000364 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000365 PREALLOC_PTARRAY(32) q;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000366 // we don't need a direction if we aren't constraining the subdivision
367 static const SkPath::Direction kDummyDir = SkPath::kCCW_Direction;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000368 // We convert cubics to quadratics (for now).
369 // In perspective have to do conversion in src space.
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000370 if (persp) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000371 SkScalar tolScale =
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000372 GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m,
373 path.getBounds());
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000374 GrPathUtils::convertCubicToQuads(pathPts, tolScale, false, kDummyDir, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000375 } else {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000376 GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, false, kDummyDir, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000377 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000378 for (int i = 0; i < q.count(); i += 3) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000379 SkPoint* qInDevSpace;
380 // bounds has to be calculated in device space, but q is
381 // in src space when there is perspective.
382 if (persp) {
383 m.mapPoints(devPts, &q[i], 3);
384 bounds.setBounds(devPts, 3);
385 qInDevSpace = devPts;
386 } else {
387 bounds.setBounds(&q[i], 3);
388 qInDevSpace = &q[i];
389 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000390 bounds.outset(SK_Scalar1, SK_Scalar1);
391 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000392 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000393 int subdiv = num_quad_subdivs(qInDevSpace);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000394 SkASSERT(subdiv >= -1);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000395 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000396 SkPoint* pts = lines->push_back_n(4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000397 // lines should always be in device coords
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000398 pts[0] = qInDevSpace[0];
399 pts[1] = qInDevSpace[1];
400 pts[2] = qInDevSpace[1];
401 pts[3] = qInDevSpace[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000402 } else {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000403 SkPoint* pts = quads->push_back_n(3);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000404 // q is already in src space when there is no
405 // perspective and dev coords otherwise.
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000406 pts[0] = q[0 + i];
407 pts[1] = q[1 + i];
408 pts[2] = q[2 + i];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000409 quadSubdivCnts->push_back() = subdiv;
410 totalQuadCount += 1 << subdiv;
411 }
412 }
413 }
414 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000415 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000416 case SkPath::kClose_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000417 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000418 case SkPath::kDone_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000419 return totalQuadCount;
420 }
421 }
422}
423
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000424struct LineVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000425 SkPoint fPos;
egdaniele27065a2014-11-06 08:00:48 -0800426 float fCoverage;
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000427};
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000428
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000429struct BezierVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000430 SkPoint fPos;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000431 union {
432 struct {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000433 SkScalar fK;
434 SkScalar fL;
435 SkScalar fM;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000436 } fConic;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000437 SkVector fQuadCoord;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000438 struct {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000439 SkScalar fBogus[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000440 };
441 };
442};
egdaniel@google.com5383a752013-07-12 20:15:34 +0000443
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000444GR_STATIC_ASSERT(sizeof(BezierVertex) == 3 * sizeof(SkPoint));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000445
446void intersect_lines(const SkPoint& ptA, const SkVector& normA,
447 const SkPoint& ptB, const SkVector& normB,
448 SkPoint* result) {
449
450 SkScalar lineAW = -normA.dot(ptA);
451 SkScalar lineBW = -normB.dot(ptB);
452
453 SkScalar wInv = SkScalarMul(normA.fX, normB.fY) -
egdaniel@google.com5383a752013-07-12 20:15:34 +0000454 SkScalarMul(normA.fY, normB.fX);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000455 wInv = SkScalarInvert(wInv);
456
457 result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY);
458 result->fX = SkScalarMul(result->fX, wInv);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000459
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000460 result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW);
461 result->fY = SkScalarMul(result->fY, wInv);
462}
463
joshualitt5ead6da2014-10-22 16:00:29 -0700464void set_uv_quad(const SkPoint qpts[3], BezierVertex verts[kQuadNumVertices]) {
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000465 // this should be in the src space, not dev coords, when we have perspective
466 GrPathUtils::QuadUVMatrix DevToUV(qpts);
joshualitt5ead6da2014-10-22 16:00:29 -0700467 DevToUV.apply<kQuadNumVertices, sizeof(BezierVertex), sizeof(SkPoint)>(verts);
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000468}
469
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000470void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice,
joshualitt5ead6da2014-10-22 16:00:29 -0700471 const SkMatrix* toSrc, BezierVertex verts[kQuadNumVertices],
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000472 SkRect* devBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000473 SkASSERT(!toDevice == !toSrc);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000474 // original quad is specified by tri a,b,c
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000475 SkPoint a = qpts[0];
476 SkPoint b = qpts[1];
477 SkPoint c = qpts[2];
478
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000479 if (toDevice) {
480 toDevice->mapPoints(&a, 1);
481 toDevice->mapPoints(&b, 1);
482 toDevice->mapPoints(&c, 1);
483 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000484 // make a new poly where we replace a and c by a 1-pixel wide edges orthog
485 // to edges ab and bc:
486 //
487 // before | after
488 // | b0
489 // b |
490 // |
491 // | a0 c0
492 // a c | a1 c1
493 //
494 // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c,
495 // respectively.
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000496 BezierVertex& a0 = verts[0];
497 BezierVertex& a1 = verts[1];
498 BezierVertex& b0 = verts[2];
499 BezierVertex& c0 = verts[3];
500 BezierVertex& c1 = verts[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000501
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000502 SkVector ab = b;
503 ab -= a;
504 SkVector ac = c;
505 ac -= a;
506 SkVector cb = b;
507 cb -= c;
508
509 // We should have already handled degenerates
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000510 SkASSERT(ab.length() > 0 && cb.length() > 0);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000511
512 ab.normalize();
513 SkVector abN;
514 abN.setOrthog(ab, SkVector::kLeft_Side);
515 if (abN.dot(ac) > 0) {
516 abN.negate();
517 }
518
519 cb.normalize();
520 SkVector cbN;
521 cbN.setOrthog(cb, SkVector::kLeft_Side);
522 if (cbN.dot(ac) < 0) {
523 cbN.negate();
524 }
525
526 a0.fPos = a;
527 a0.fPos += abN;
528 a1.fPos = a;
529 a1.fPos -= abN;
530
531 c0.fPos = c;
532 c0.fPos += cbN;
533 c1.fPos = c;
534 c1.fPos -= cbN;
535
536 intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos);
joshualitt5ead6da2014-10-22 16:00:29 -0700537 devBounds->growToInclude(&verts[0].fPos, sizeof(BezierVertex), kQuadNumVertices);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000538
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000539 if (toSrc) {
joshualitt5ead6da2014-10-22 16:00:29 -0700540 toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(BezierVertex), kQuadNumVertices);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000541 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000542}
543
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000544// Equations based off of Loop-Blinn Quadratic GPU Rendering
egdaniel@google.com5383a752013-07-12 20:15:34 +0000545// Input Parametric:
546// 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)
547// Output Implicit:
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000548// f(x, y, w) = f(P) = K^2 - LM
549// K = dot(k, P), L = dot(l, P), M = dot(m, P)
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000550// k, l, m are calculated in function GrPathUtils::getConicKLM
joshualitt5ead6da2014-10-22 16:00:29 -0700551void set_conic_coeffs(const SkPoint p[3], BezierVertex verts[kQuadNumVertices],
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000552 const SkScalar weight) {
553 SkScalar klm[9];
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000554
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000555 GrPathUtils::getConicKLM(p, weight, klm);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000556
joshualitt5ead6da2014-10-22 16:00:29 -0700557 for (int i = 0; i < kQuadNumVertices; ++i) {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000558 const SkPoint pnt = verts[i].fPos;
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000559 verts[i].fConic.fK = pnt.fX * klm[0] + pnt.fY * klm[1] + klm[2];
560 verts[i].fConic.fL = pnt.fX * klm[3] + pnt.fY * klm[4] + klm[5];
561 verts[i].fConic.fM = pnt.fX * klm[6] + pnt.fY * klm[7] + klm[8];
egdaniel@google.com5383a752013-07-12 20:15:34 +0000562 }
563}
564
565void add_conics(const SkPoint p[3],
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000566 const SkScalar weight,
egdaniel@google.com5383a752013-07-12 20:15:34 +0000567 const SkMatrix* toDevice,
568 const SkMatrix* toSrc,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000569 BezierVertex** vert,
egdaniel@google.com5383a752013-07-12 20:15:34 +0000570 SkRect* devBounds) {
571 bloat_quad(p, toDevice, toSrc, *vert, devBounds);
572 set_conic_coeffs(p, *vert, weight);
joshualitt5ead6da2014-10-22 16:00:29 -0700573 *vert += kQuadNumVertices;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000574}
575
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000576void add_quads(const SkPoint p[3],
577 int subdiv,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000578 const SkMatrix* toDevice,
579 const SkMatrix* toSrc,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000580 BezierVertex** vert,
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000581 SkRect* devBounds) {
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);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000586 add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert, devBounds);
587 add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert, devBounds);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000588 } else {
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000589 bloat_quad(p, toDevice, toSrc, *vert, devBounds);
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::createLineGeom(GrDrawTarget* target,
645 GrDrawState* drawState,
robertphillips@google.comada90da2013-09-18 22:14:49 +0000646 GrDrawTarget::AutoReleaseGeometry* arg,
joshualitt9853cce2014-11-17 14:22:48 -0800647 SkRect* devBounds,
648 const SkPath& path,
649 const PtArray& lines,
650 int lineCnt) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000651 const SkMatrix& viewM = drawState->getViewMatrix();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000652
joshualitt5ead6da2014-10-22 16:00:29 -0700653 int vertCnt = kLineSegNumVertices * lineCnt;
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000654
joshualitt2dd1ae02014-12-03 06:24:10 -0800655 size_t vstride = drawState->getGeometryProcessor()->getVertexStride();
656 SkASSERT(vstride == sizeof(LineVertex));
657 if (!arg->set(target, vertCnt, vstride, 0)) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000658 return false;
659 }
660
661 LineVertex* verts = reinterpret_cast<LineVertex*>(arg->vertices());
662
663 const SkMatrix* toSrc = NULL;
664 SkMatrix ivm;
665
666 if (viewM.hasPerspective()) {
667 if (viewM.invert(&ivm)) {
668 toSrc = &ivm;
669 }
670 }
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000671 devBounds->set(lines.begin(), lines.count());
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000672 for (int i = 0; i < lineCnt; ++i) {
egdaniele27065a2014-11-06 08:00:48 -0800673 add_line(&lines[2*i], toSrc, drawState->getCoverage(), &verts);
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000674 }
skia.committer@gmail.comf91e3d42013-09-20 07:01:33 +0000675 // 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 +0000676 static const SkScalar kSqrtOfOneAndAQuarter = 1.118f;
robertphillips@google.com52c75262013-09-19 16:36:43 +0000677 // Add a little extra to account for vector normalization precision.
678 static const SkScalar kOutset = kSqrtOfOneAndAQuarter + SK_Scalar1 / 20;
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000679 devBounds->outset(kOutset, kOutset);
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000680
681 return true;
682}
683
joshualitt9853cce2014-11-17 14:22:48 -0800684bool GrAAHairLinePathRenderer::createBezierGeom(GrDrawTarget* target,
685 GrDrawState* drawState,
686 GrDrawTarget::AutoReleaseGeometry* arg,
687 SkRect* devBounds,
688 const SkPath& path,
689 const PtArray& quads,
690 int quadCnt,
691 const PtArray& conics,
692 int conicCnt,
693 const IntArray& qSubdivs,
joshualitt2dd1ae02014-12-03 06:24:10 -0800694 const FloatArray& cWeights,
695 size_t vertexStride) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000696 const SkMatrix& viewM = drawState->getViewMatrix();
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000697
joshualitt5ead6da2014-10-22 16:00:29 -0700698 int vertCnt = kQuadNumVertices * quadCnt + kQuadNumVertices * conicCnt;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000699
joshualitt2dd1ae02014-12-03 06:24:10 -0800700 if (!arg->set(target, vertCnt, vertexStride, 0)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000701 return false;
702 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000703
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000704 BezierVertex* verts = reinterpret_cast<BezierVertex*>(arg->vertices());
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000705
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000706 const SkMatrix* toDevice = NULL;
707 const SkMatrix* toSrc = NULL;
708 SkMatrix ivm;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000709
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000710 if (viewM.hasPerspective()) {
711 if (viewM.invert(&ivm)) {
712 toDevice = &viewM;
713 toSrc = &ivm;
714 }
715 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000716
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000717 // Seed the dev bounds with some pts known to be inside. Each quad and conic grows the bounding
718 // box to include its vertices.
719 SkPoint seedPts[2];
720 if (quadCnt) {
721 seedPts[0] = quads[0];
722 seedPts[1] = quads[2];
723 } else if (conicCnt) {
724 seedPts[0] = conics[0];
725 seedPts[1] = conics[2];
726 }
bsalomon49f085d2014-09-05 13:34:00 -0700727 if (toDevice) {
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000728 toDevice->mapPoints(seedPts, 2);
729 }
730 devBounds->set(seedPts[0], seedPts[1]);
731
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000732 int unsubdivQuadCnt = quads.count() / 3;
733 for (int i = 0; i < unsubdivQuadCnt; ++i) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000734 SkASSERT(qSubdivs[i] >= 0);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000735 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts, devBounds);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000736 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000737
egdaniel@google.com5383a752013-07-12 20:15:34 +0000738 // Start Conics
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000739 for (int i = 0; i < conicCnt; ++i) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000740 add_conics(&conics[3*i], cWeights[i], toDevice, toSrc, &verts, devBounds);
741 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000742 return true;
743}
744
joshualitt9853cce2014-11-17 14:22:48 -0800745bool GrAAHairLinePathRenderer::canDrawPath(const GrDrawTarget* target,
746 const GrDrawState* drawState,
747 const SkPath& path,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000748 const SkStrokeRec& stroke,
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000749 bool antiAlias) const {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000750 if (!antiAlias) {
751 return false;
752 }
753
754 if (!IsStrokeHairlineOrEquivalent(stroke,
joshualitt9853cce2014-11-17 14:22:48 -0800755 drawState->getViewMatrix(),
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000756 NULL)) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000757 return false;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000758 }
759
robertphillips@google.come79f3202014-02-11 16:30:21 +0000760 if (SkPath::kLine_SegmentMask == path.getSegmentMasks() ||
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000761 target->caps()->shaderDerivativeSupport()) {
762 return true;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000763 }
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000764 return false;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000765}
766
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000767template <class VertexType>
768bool check_bounds(GrDrawState* drawState, const SkRect& devBounds, void* vertices, int vCount)
769{
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000770 SkRect tolDevBounds = devBounds;
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000771 // The bounds ought to be tight, but in perspective the below code runs the verts
772 // through the view matrix to get back to dev coords, which can introduce imprecision.
773 if (drawState->getViewMatrix().hasPerspective()) {
774 tolDevBounds.outset(SK_Scalar1 / 1000, SK_Scalar1 / 1000);
775 } else {
776 // Non-persp matrices cause this path renderer to draw in device space.
777 SkASSERT(drawState->getViewMatrix().isIdentity());
778 }
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000779 SkRect actualBounds;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000780
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000781 VertexType* verts = reinterpret_cast<VertexType*>(vertices);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000782 bool first = true;
783 for (int i = 0; i < vCount; ++i) {
784 SkPoint pos = verts[i].fPos;
785 // This is a hack to workaround the fact that we move some degenerate segments offscreen.
786 if (SK_ScalarMax == pos.fX) {
787 continue;
788 }
789 drawState->getViewMatrix().mapPoints(&pos, 1);
790 if (first) {
791 actualBounds.set(pos.fX, pos.fY, pos.fX, pos.fY);
792 first = false;
793 } else {
794 actualBounds.growToInclude(pos.fX, pos.fY);
795 }
796 }
797 if (!first) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000798 return tolDevBounds.contains(actualBounds);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000799 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000800
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000801 return true;
802}
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000803
joshualitt9853cce2014-11-17 14:22:48 -0800804bool GrAAHairLinePathRenderer::onDrawPath(GrDrawTarget* target,
805 GrDrawState* drawState,
806 const SkPath& path,
robertphillips@google.come79f3202014-02-11 16:30:21 +0000807 const SkStrokeRec& stroke,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000808 bool antiAlias) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000809 SkScalar hairlineCoverage;
joshualitt9853cce2014-11-17 14:22:48 -0800810 if (IsStrokeHairlineOrEquivalent(stroke, drawState->getViewMatrix(),
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000811 &hairlineCoverage)) {
joshualitt9853cce2014-11-17 14:22:48 -0800812 uint8_t newCoverage = SkScalarRoundToInt(hairlineCoverage * drawState->getCoverage());
813 drawState->setCoverage(newCoverage);
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000814 }
815
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000816 SkIRect devClipBounds;
817 target->getClip()->getConservativeBounds(drawState->getRenderTarget(), &devClipBounds);
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000818
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000819 int lineCnt;
820 int quadCnt;
821 int conicCnt;
822 PREALLOC_PTARRAY(128) lines;
823 PREALLOC_PTARRAY(128) quads;
824 PREALLOC_PTARRAY(128) conics;
825 IntArray qSubdivs;
826 FloatArray cWeights;
robertphillips@google.come79f3202014-02-11 16:30:21 +0000827 quadCnt = generate_lines_and_quads(path, drawState->getViewMatrix(), devClipBounds,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000828 &lines, &quads, &conics, &qSubdivs, &cWeights);
829 lineCnt = lines.count() / 2;
830 conicCnt = conics.count() / 3;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000831
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000832 // do lines first
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000833 if (lineCnt) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000834 GrDrawTarget::AutoReleaseGeometry arg;
835 SkRect devBounds;
836
joshualitt2dd1ae02014-12-03 06:24:10 -0800837 uint32_t gpFlags = GrDefaultGeoProcFactory::kPosition_GPType |
838 GrDefaultGeoProcFactory::kCoverage_GPType;
839 GrDrawState::AutoRestoreEffects are(drawState);
840 drawState->setGeometryProcessor(GrDefaultGeoProcFactory::Create(gpFlags))->unref();
841
joshualitt9853cce2014-11-17 14:22:48 -0800842 if (!this->createLineGeom(target,
843 drawState,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000844 &arg,
joshualitt9853cce2014-11-17 14:22:48 -0800845 &devBounds,
846 path,
847 lines,
848 lineCnt)) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000849 return false;
850 }
851
robertphillips@google.comada90da2013-09-18 22:14:49 +0000852 // createLineGeom transforms the geometry to device space when the matrix does not have
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000853 // perspective.
joshualitt9853cce2014-11-17 14:22:48 -0800854 GrDrawState::AutoViewMatrixRestore avmr;
855 if (!drawState->getViewMatrix().hasPerspective() && !avmr.setIdentity(drawState)) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000856 return false;
857 }
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000858
859 // Check devBounds
robertphillips@google.com52c75262013-09-19 16:36:43 +0000860 SkASSERT(check_bounds<LineVertex>(drawState, devBounds, arg.vertices(),
joshualitt5ead6da2014-10-22 16:00:29 -0700861 kLineSegNumVertices * lineCnt));
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000862
863 {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000864 target->setIndexSourceToBuffer(fLinesIndexBuffer);
865 int lines = 0;
866 while (lines < lineCnt) {
joshualitt5ead6da2014-10-22 16:00:29 -0700867 int n = SkTMin(lineCnt - lines, kLineSegsNumInIdxBuffer);
joshualitt9853cce2014-11-17 14:22:48 -0800868 target->drawIndexed(drawState,
869 kTriangles_GrPrimitiveType,
joshualitt5ead6da2014-10-22 16:00:29 -0700870 kLineSegNumVertices*lines, // startV
871 0, // startI
872 kLineSegNumVertices*n, // vCount
873 kIdxsPerLineSeg*n, // iCount
robertphillips@google.comada90da2013-09-18 22:14:49 +0000874 &devBounds);
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000875 lines += n;
876 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000877 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000878 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000879
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000880 // then quadratics/conics
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000881 if (quadCnt || conicCnt) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000882 GrDrawTarget::AutoReleaseGeometry arg;
883 SkRect devBounds;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000884
joshualitt9853cce2014-11-17 14:22:48 -0800885 if (!this->createBezierGeom(target,
886 drawState,
887 &arg,
888 &devBounds,
889 path,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000890 quads,
891 quadCnt,
892 conics,
893 conicCnt,
894 qSubdivs,
joshualitt2dd1ae02014-12-03 06:24:10 -0800895 cWeights,
896 sizeof(BezierVertex))) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000897 return false;
898 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000899
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000900 // createGeom transforms the geometry to device space when the matrix does not have
901 // perspective.
joshualitt9853cce2014-11-17 14:22:48 -0800902 GrDrawState::AutoViewMatrixRestore avmr;
903 if (!drawState->getViewMatrix().hasPerspective() && !avmr.setIdentity(drawState)) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000904 return false;
905 }
joshualitt9853cce2014-11-17 14:22:48 -0800906
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000907
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000908 // Check devBounds
909 SkASSERT(check_bounds<BezierVertex>(drawState, devBounds, arg.vertices(),
joshualitt5ead6da2014-10-22 16:00:29 -0700910 kQuadNumVertices * quadCnt + kQuadNumVertices * conicCnt));
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000911
commit-bot@chromium.org88462472013-08-23 21:01:52 +0000912 if (quadCnt > 0) {
joshualittb0a8a372014-09-23 09:50:21 -0700913 GrGeometryProcessor* hairQuadProcessor =
914 GrQuadEffect::Create(kHairlineAA_GrProcessorEdgeType, *target->caps());
915 SkASSERT(hairQuadProcessor);
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000916 GrDrawState::AutoRestoreEffects are(drawState);
917 target->setIndexSourceToBuffer(fQuadsIndexBuffer);
joshualitt2dd1ae02014-12-03 06:24:10 -0800918
joshualittb0a8a372014-09-23 09:50:21 -0700919 drawState->setGeometryProcessor(hairQuadProcessor)->unref();
commit-bot@chromium.org88462472013-08-23 21:01:52 +0000920 int quads = 0;
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000921 while (quads < quadCnt) {
joshualitt5ead6da2014-10-22 16:00:29 -0700922 int n = SkTMin(quadCnt - quads, kQuadsNumInIdxBuffer);
joshualitt9853cce2014-11-17 14:22:48 -0800923 target->drawIndexed(drawState,
924 kTriangles_GrPrimitiveType,
joshualitt5ead6da2014-10-22 16:00:29 -0700925 kQuadNumVertices*quads, // startV
926 0, // startI
927 kQuadNumVertices*n, // vCount
928 kIdxsPerQuad*n, // iCount
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000929 &devBounds);
930 quads += n;
931 }
932 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000933
commit-bot@chromium.org88462472013-08-23 21:01:52 +0000934 if (conicCnt > 0) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000935 GrDrawState::AutoRestoreEffects are(drawState);
joshualittb0a8a372014-09-23 09:50:21 -0700936 GrGeometryProcessor* hairConicProcessor = GrConicEffect::Create(
937 kHairlineAA_GrProcessorEdgeType, *target->caps());
938 SkASSERT(hairConicProcessor);
joshualitt2dd1ae02014-12-03 06:24:10 -0800939
joshualittb0a8a372014-09-23 09:50:21 -0700940 drawState->setGeometryProcessor(hairConicProcessor)->unref();
commit-bot@chromium.org88462472013-08-23 21:01:52 +0000941 int conics = 0;
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000942 while (conics < conicCnt) {
joshualitt5ead6da2014-10-22 16:00:29 -0700943 int n = SkTMin(conicCnt - conics, kQuadsNumInIdxBuffer);
joshualitt9853cce2014-11-17 14:22:48 -0800944 target->drawIndexed(drawState,
945 kTriangles_GrPrimitiveType,
joshualitt5ead6da2014-10-22 16:00:29 -0700946 kQuadNumVertices*(quadCnt + conics), // startV
947 0, // startI
948 kQuadNumVertices*n, // vCount
949 kIdxsPerQuad*n, // iCount
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000950 &devBounds);
951 conics += n;
952 }
egdaniel@google.com5383a752013-07-12 20:15:34 +0000953 }
954 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000955
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000956 target->resetIndexSource();
bsalomon@google.com4647f902013-03-26 14:45:27 +0000957
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000958 return true;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000959}