blob: f60e7cfc303d01871252686d9248fa7f006b9e33 [file] [log] [blame]
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001#include "GrAAHairLinePathRenderer.h"
2
3#include "GrContext.h"
4#include "GrGpu.h"
5#include "GrIndexBuffer.h"
bsalomon@google.comdbeeac32011-09-12 14:59:34 +00006#include "GrPathUtils.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +00007#include "SkGeometry.h"
8#include "SkTemplates.h"
9
10namespace {
11// quadratics are rendered as 5-sided polys in order to bound the
12// AA stroke around the center-curve. See comments in push_quad_index_buffer and
13// bloat_quad.
14static const int kVertsPerQuad = 5;
15static const int kIdxsPerQuad = 9;
16
17static const int kVertsPerLineSeg = 4;
18static const int kIdxsPerLineSeg = 6;
19
20static const int kNumQuadsInIdxBuffer = 256;
21static const size_t kQuadIdxSBufize = kIdxsPerQuad *
22 sizeof(uint16_t) *
23 kNumQuadsInIdxBuffer;
24
25bool push_quad_index_data(GrIndexBuffer* qIdxBuffer) {
26 uint16_t* data = (uint16_t*) qIdxBuffer->lock();
27 bool tempData = NULL == data;
28 if (tempData) {
29 data = new uint16_t[kNumQuadsInIdxBuffer * kIdxsPerQuad];
30 }
31 for (int i = 0; i < kNumQuadsInIdxBuffer; ++i) {
32
33 // Each quadratic is rendered as a five sided polygon. This poly bounds
34 // the quadratic's bounding triangle but has been expanded so that the
35 // 1-pixel wide area around the curve is inside the poly.
36 // If a,b,c are the original control points then the poly a0,b0,c0,c1,a1
37 // that is rendered would look like this:
38 // b0
39 // b
40 //
41 // a0 c0
42 // a c
43 // a1 c1
44 // Each is drawn as three triagnles specified by these 9 indices:
45 int baseIdx = i * kIdxsPerQuad;
46 uint16_t baseVert = (uint16_t)(i * kVertsPerQuad);
47 data[0 + baseIdx] = baseVert + 0; // a0
48 data[1 + baseIdx] = baseVert + 1; // a1
49 data[2 + baseIdx] = baseVert + 2; // b0
50 data[3 + baseIdx] = baseVert + 2; // b0
51 data[4 + baseIdx] = baseVert + 4; // c1
52 data[5 + baseIdx] = baseVert + 3; // c0
53 data[6 + baseIdx] = baseVert + 1; // a1
54 data[7 + baseIdx] = baseVert + 4; // c1
55 data[8 + baseIdx] = baseVert + 2; // b0
56 }
57 if (tempData) {
58 bool ret = qIdxBuffer->updateData(data, kQuadIdxSBufize);
59 delete[] data;
60 return ret;
61 } else {
62 qIdxBuffer->unlock();
63 return true;
64 }
65}
66}
67
68GrPathRenderer* GrAAHairLinePathRenderer::Create(GrContext* context) {
69 if (CanBeUsed(context)) {
70 const GrIndexBuffer* lIdxBuffer = context->getQuadIndexBuffer();
71 if (NULL == lIdxBuffer) {
72 return NULL;
73 }
74 GrGpu* gpu = context->getGpu();
75 GrIndexBuffer* qIdxBuf = gpu->createIndexBuffer(kQuadIdxSBufize, false);
76 SkAutoTUnref<GrIndexBuffer> qIdxBuffer(qIdxBuf); // cons will take a ref
77 if (NULL == qIdxBuf ||
78 !push_quad_index_data(qIdxBuffer.get())) {
79 return NULL;
80 }
81 return new GrAAHairLinePathRenderer(context,
82 lIdxBuffer,
83 qIdxBuf);
84 } else {
85 return NULL;
86 }
87}
88
89bool GrAAHairLinePathRenderer::CanBeUsed(const GrContext* context) {
90 return context->getGpu()->supportsShaderDerivatives();
91
92}
93
94GrAAHairLinePathRenderer::GrAAHairLinePathRenderer(
95 const GrContext* context,
96 const GrIndexBuffer* linesIndexBuffer,
97 const GrIndexBuffer* quadsIndexBuffer) {
98 GrAssert(CanBeUsed(context));
99 fLinesIndexBuffer = linesIndexBuffer;
100 linesIndexBuffer->ref();
101 fQuadsIndexBuffer = quadsIndexBuffer;
102 quadsIndexBuffer->ref();
103 this->resetGeom();
104}
105
106GrAAHairLinePathRenderer::~GrAAHairLinePathRenderer() {
107 fLinesIndexBuffer->unref();
108 fQuadsIndexBuffer->unref();
109}
110
111bool GrAAHairLinePathRenderer::supportsAA(GrDrawTarget* target,
112 const SkPath& path,
113 GrPathFill fill) {
114 return kHairLine_PathFill == fill;
115}
116
117bool GrAAHairLinePathRenderer::canDrawPath(const GrDrawTarget* target,
118 const SkPath& path,
119 GrPathFill fill) const {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000120 return kHairLine_PathFill == fill;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000121}
122
123void GrAAHairLinePathRenderer::pathWillClear() {
124 this->resetGeom();
125}
126
127void GrAAHairLinePathRenderer::resetGeom() {
128 fPreviousStages = ~0;
129 fPreviousRTHeight = ~0;
130 fPreviousViewMatrix = GrMatrix::InvalidMatrix();
131 fLineSegmentCnt = 0;
132 fQuadCnt = 0;
133 if ((fQuadCnt || fLineSegmentCnt) && NULL != fTarget) {
134 fTarget->resetVertexSource();
135 }
136}
137
138namespace {
139
140typedef GrTArray<SkPoint, true> PtArray;
141typedef GrTArray<int, true> IntArray;
142
143/**
144 * We convert cubics to quadratics (for now).
145 */
146void convert_noninflect_cubic_to_quads(const SkPoint p[4],
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000147 SkScalar tolScale,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000148 PtArray* quads,
149 int sublevel = 0) {
150 SkVector ab = p[1];
151 ab -= p[0];
152 SkVector dc = p[2];
153 dc -= p[3];
154
155 static const SkScalar gLengthScale = 3 * SK_Scalar1 / 2;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000156 // base tolerance is 2 pixels in dev coords.
157 const SkScalar distanceSqdTol = SkScalarMul(tolScale, 2 * SK_Scalar1);
158 static const int kMaxSubdivs = 10;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000159
160 ab.scale(gLengthScale);
161 dc.scale(gLengthScale);
162
163 SkVector c0 = p[0];
164 c0 += ab;
165 SkVector c1 = p[3];
166 c1 += dc;
167
168 SkScalar dSqd = c0.distanceToSqd(c1);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000169 if (sublevel > kMaxSubdivs || dSqd <= distanceSqdTol) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000170 SkPoint cAvg = c0;
171 cAvg += c1;
172 cAvg.scale(SK_ScalarHalf);
173
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000174 SkPoint* pts = quads->push_back_n(3);
175 pts[0] = p[0];
176 pts[1] = cAvg;
177 pts[2] = p[3];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000178
179 return;
180 } else {
181 SkPoint choppedPts[7];
182 SkChopCubicAtHalf(p, choppedPts);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000183 convert_noninflect_cubic_to_quads(choppedPts + 0, tolScale,
184 quads, sublevel + 1);
185 convert_noninflect_cubic_to_quads(choppedPts + 3, tolScale,
186 quads, sublevel + 1);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000187 }
188}
189
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000190void convert_cubic_to_quads(const SkPoint p[4],
191 SkScalar tolScale,
192 PtArray* quads) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000193 SkPoint chopped[13];
194 int count = SkChopCubicAtInflections(p, chopped);
195
196 for (int i = 0; i < count; ++i) {
197 SkPoint* cubic = chopped + 3*i;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000198 convert_noninflect_cubic_to_quads(cubic, tolScale, quads);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000199 }
200}
201
202// Takes 178th time of logf on Z600 / VC2010
203int get_float_exp(float x) {
204 GR_STATIC_ASSERT(sizeof(int) == sizeof(float));
205#if GR_DEBUG
206 static bool tested;
207 if (!tested) {
208 tested = true;
209 GrAssert(get_float_exp(0.25f) == -2);
210 GrAssert(get_float_exp(0.3f) == -2);
211 GrAssert(get_float_exp(0.5f) == -1);
212 GrAssert(get_float_exp(1.f) == 0);
213 GrAssert(get_float_exp(2.f) == 1);
214 GrAssert(get_float_exp(2.5f) == 1);
215 GrAssert(get_float_exp(8.f) == 3);
216 GrAssert(get_float_exp(100.f) == 6);
217 GrAssert(get_float_exp(1000.f) == 9);
218 GrAssert(get_float_exp(1024.f) == 10);
219 GrAssert(get_float_exp(3000000.f) == 21);
220 }
221#endif
222 return (((*(int*)&x) & 0x7f800000) >> 23) - 127;
223}
224
225// we subdivide the quads to avoid huge overfill
226// if it returns -1 then should be drawn as lines
227int num_quad_subdivs(const SkPoint p[3]) {
228 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000229 static const SkScalar gDegenerateToLineTolSqd =
230 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000231
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000232 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
233 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000234 return -1;
235 }
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000236
237 GrScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
238 if (dsqd < gDegenerateToLineTolSqd) {
239 return -1;
240 }
241
242 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000243 return -1;
244 }
245
246 static const int kMaxSub = 4;
247 // tolerance of triangle height in pixels
248 // tuned on windows Quadro FX 380 / Z600
249 // trade off of fill vs cpu time on verts
250 // maybe different when do this using gpu (geo or tess shaders)
251 static const SkScalar gSubdivTol = 175 * SK_Scalar1;
252
253 if (dsqd <= gSubdivTol*gSubdivTol) {
254 return 0;
255 } else {
256 // subdividing the quad reduces d by 4. so we want x = log4(d/tol)
257 // = log4(d*d/tol*tol)/2
258 // = log2(d*d/tol*tol)
259
260#ifdef SK_SCALAR_IS_FLOAT
261 // +1 since we're ignoring the mantissa contribution.
262 int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
263 log = GrMin(GrMax(0, log), kMaxSub);
264 return log;
265#else
bsalomon@google.comfcb0dbc2011-08-30 18:35:18 +0000266 SkScalar log = SkScalarLog(SkScalarDiv(dsqd,gSubdivTol*gSubdivTol));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000267 static const SkScalar conv = SkScalarInvert(SkScalarLog(2));
268 log = SkScalarMul(log, conv);
269 return GrMin(GrMax(0, SkScalarCeilToInt(log)),kMaxSub);
270#endif
271 }
272}
273
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000274/**
275 * Generates the lines and quads to be rendered. Lines are always recorded in
276 * device space. We will do a device space bloat to account for the 1pixel
277 * thickness.
278 * Quads are recorded in device space unless m contains
279 * perspective, then in they are in src space. We do this because we will
280 * subdivide large quads to reduce over-fill. This subdivision has to be
281 * performed before applying the perspective matrix.
282 */
283int generate_lines_and_quads(const SkPath& path,
284 const SkMatrix& m,
285 const SkVector& translate,
286 GrIRect clip,
287 PtArray* lines,
288 PtArray* quads,
289 IntArray* quadSubdivCnts) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000290 SkPath::Iter iter(path, false);
291
292 int totalQuadCount = 0;
293 GrRect bounds;
294 GrIRect ibounds;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000295
296 bool persp = m.hasPerspective();
297
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000298 for (;;) {
299 GrPoint pts[4];
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000300 GrPoint devPts[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000301 GrPathCmd cmd = (GrPathCmd)iter.next(pts);
302 switch (cmd) {
303 case kMove_PathCmd:
304 break;
305 case kLine_PathCmd:
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000306 SkPoint::Offset(pts, 2, translate);
307 m.mapPoints(devPts, pts, 2);
308 bounds.setBounds(devPts, 2);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000309 bounds.outset(SK_Scalar1, SK_Scalar1);
310 bounds.roundOut(&ibounds);
311 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000312 SkPoint* pts = lines->push_back_n(2);
313 pts[0] = devPts[0];
314 pts[1] = devPts[1];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000315 }
316 break;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000317 case kQuadratic_PathCmd:
318 SkPoint::Offset(pts, 3, translate);
319 m.mapPoints(devPts, pts, 3);
320 bounds.setBounds(devPts, 3);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000321 bounds.outset(SK_Scalar1, SK_Scalar1);
322 bounds.roundOut(&ibounds);
323 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000324 int subdiv = num_quad_subdivs(devPts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000325 GrAssert(subdiv >= -1);
326 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000327 SkPoint* pts = lines->push_back_n(4);
328 pts[0] = devPts[0];
329 pts[1] = devPts[1];
330 pts[2] = devPts[1];
331 pts[3] = devPts[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000332 } else {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000333 // when in perspective keep quads in src space
334 SkPoint* qPts = persp ? pts : devPts;
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000335 SkPoint* pts = quads->push_back_n(3);
336 pts[0] = qPts[0];
337 pts[1] = qPts[1];
338 pts[2] = qPts[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000339 quadSubdivCnts->push_back() = subdiv;
340 totalQuadCount += 1 << subdiv;
341 }
342 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000343 break;
344 case kCubic_PathCmd:
345 SkPoint::Offset(pts, 4, translate);
346 m.mapPoints(devPts, pts, 4);
347 bounds.setBounds(devPts, 4);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000348 bounds.outset(SK_Scalar1, SK_Scalar1);
349 bounds.roundOut(&ibounds);
350 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000351 SkPoint stackStorage[32];
352 PtArray q((void*)stackStorage, 32);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000353 // in perspective have to do conversion in src space
354 if (persp) {
355 SkScalar tolScale =
356 GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m,
357 path.getBounds());
358 convert_cubic_to_quads(pts, tolScale, &q);
359 } else {
360 convert_cubic_to_quads(devPts, SK_Scalar1, &q);
361 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000362 for (int i = 0; i < q.count(); i += 3) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000363 SkPoint* qInDevSpace;
364 // bounds has to be calculated in device space, but q is
365 // in src space when there is perspective.
366 if (persp) {
367 m.mapPoints(devPts, &q[i], 3);
368 bounds.setBounds(devPts, 3);
369 qInDevSpace = devPts;
370 } else {
371 bounds.setBounds(&q[i], 3);
372 qInDevSpace = &q[i];
373 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000374 bounds.outset(SK_Scalar1, SK_Scalar1);
375 bounds.roundOut(&ibounds);
376 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000377 int subdiv = num_quad_subdivs(qInDevSpace);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000378 GrAssert(subdiv >= -1);
379 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000380 SkPoint* pts = lines->push_back_n(4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000381 // lines should always be in device coords
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000382 pts[0] = qInDevSpace[0];
383 pts[1] = qInDevSpace[1];
384 pts[2] = qInDevSpace[1];
385 pts[3] = qInDevSpace[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000386 } else {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000387 SkPoint* pts = quads->push_back_n(3);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000388 // q is already in src space when there is no
389 // perspective and dev coords otherwise.
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000390 pts[0] = q[0 + i];
391 pts[1] = q[1 + i];
392 pts[2] = q[2 + i];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000393 quadSubdivCnts->push_back() = subdiv;
394 totalQuadCount += 1 << subdiv;
395 }
396 }
397 }
398 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000399 break;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000400 case kClose_PathCmd:
401 break;
402 case kEnd_PathCmd:
403 return totalQuadCount;
404 }
405 }
406}
407
408struct Vertex {
409 GrPoint fPos;
410 union {
411 struct {
412 GrScalar fA;
413 GrScalar fB;
414 GrScalar fC;
415 } fLine;
416 GrVec fQuadCoord;
417 struct {
418 GrScalar fBogus[4];
419 };
420 };
421};
422GR_STATIC_ASSERT(sizeof(Vertex) == 3 * sizeof(GrPoint));
423
424void intersect_lines(const SkPoint& ptA, const SkVector& normA,
425 const SkPoint& ptB, const SkVector& normB,
426 SkPoint* result) {
427
428 SkScalar lineAW = -normA.dot(ptA);
429 SkScalar lineBW = -normB.dot(ptB);
430
431 SkScalar wInv = SkScalarMul(normA.fX, normB.fY) -
432 SkScalarMul(normA.fY, normB.fX);
433 wInv = SkScalarInvert(wInv);
434
435 result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY);
436 result->fX = SkScalarMul(result->fX, wInv);
437
438 result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW);
439 result->fY = SkScalarMul(result->fY, wInv);
440}
441
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000442void bloat_quad(const SkPoint qpts[3], const GrMatrix* toDevice,
443 const GrMatrix* toSrc, Vertex verts[kVertsPerQuad]) {
444 GrAssert(!toDevice == !toSrc);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000445 // original quad is specified by tri a,b,c
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000446 SkPoint a = qpts[0];
447 SkPoint b = qpts[1];
448 SkPoint c = qpts[2];
449
450 // compute a matrix that goes from device coords to U,V quad params
451 // this should be in the src space, not dev coords, when we have perspective
452 SkMatrix DevToUV;
453 DevToUV.setAll(a.fX, b.fX, c.fX,
454 a.fY, b.fY, c.fY,
455 SK_Scalar1, SK_Scalar1, SK_Scalar1);
456 DevToUV.invert(&DevToUV);
457 // can't make this static, no cons :(
458 SkMatrix UVpts;
459 UVpts.setAll(0, SK_ScalarHalf, SK_Scalar1,
460 0, 0, SK_Scalar1,
461 SK_Scalar1, SK_Scalar1, SK_Scalar1);
462 DevToUV.postConcat(UVpts);
463
464 // We really want to avoid perspective matrix muls.
465 // These may wind up really close to zero
466 DevToUV.setPerspX(0);
467 DevToUV.setPerspY(0);
468
469 if (toDevice) {
470 toDevice->mapPoints(&a, 1);
471 toDevice->mapPoints(&b, 1);
472 toDevice->mapPoints(&c, 1);
473 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000474 // make a new poly where we replace a and c by a 1-pixel wide edges orthog
475 // to edges ab and bc:
476 //
477 // before | after
478 // | b0
479 // b |
480 // |
481 // | a0 c0
482 // a c | a1 c1
483 //
484 // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c,
485 // respectively.
486 Vertex& a0 = verts[0];
487 Vertex& a1 = verts[1];
488 Vertex& b0 = verts[2];
489 Vertex& c0 = verts[3];
490 Vertex& c1 = verts[4];
491
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000492 SkVector ab = b;
493 ab -= a;
494 SkVector ac = c;
495 ac -= a;
496 SkVector cb = b;
497 cb -= c;
498
499 // We should have already handled degenerates
500 GrAssert(ab.length() > 0 && cb.length() > 0);
501
502 ab.normalize();
503 SkVector abN;
504 abN.setOrthog(ab, SkVector::kLeft_Side);
505 if (abN.dot(ac) > 0) {
506 abN.negate();
507 }
508
509 cb.normalize();
510 SkVector cbN;
511 cbN.setOrthog(cb, SkVector::kLeft_Side);
512 if (cbN.dot(ac) < 0) {
513 cbN.negate();
514 }
515
516 a0.fPos = a;
517 a0.fPos += abN;
518 a1.fPos = a;
519 a1.fPos -= abN;
520
521 c0.fPos = c;
522 c0.fPos += cbN;
523 c1.fPos = c;
524 c1.fPos -= cbN;
525
526 intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos);
527
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000528 if (toSrc) {
529 toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(Vertex), kVertsPerQuad);
530 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000531 DevToUV.mapPointsWithStride(&verts[0].fQuadCoord,
532 &verts[0].fPos, sizeof(Vertex), kVertsPerQuad);
533}
534
535void add_quads(const SkPoint p[3],
536 int subdiv,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000537 const GrMatrix* toDevice,
538 const GrMatrix* toSrc,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000539 Vertex** vert) {
540 GrAssert(subdiv >= 0);
541 if (subdiv) {
542 SkPoint newP[5];
543 SkChopQuadAtHalf(p, newP);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000544 add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert);
545 add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000546 } else {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000547 bloat_quad(p, toDevice, toSrc, *vert);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000548 *vert += kVertsPerQuad;
549 }
550}
551
552void add_line(const SkPoint p[2],
553 int rtHeight,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000554 const SkMatrix* toSrc,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000555 Vertex** vert) {
556 const SkPoint& a = p[0];
557 const SkPoint& b = p[1];
558
559 SkVector orthVec = b;
560 orthVec -= a;
561
562 if (orthVec.setLength(SK_Scalar1)) {
563 orthVec.setOrthog(orthVec);
564
565 // the values we pass down to the frag shader
566 // have to be in y-points-up space;
567 SkVector normal;
568 normal.fX = orthVec.fX;
569 normal.fY = -orthVec.fY;
570 SkPoint aYDown;
571 aYDown.fX = a.fX;
572 aYDown.fY = rtHeight - a.fY;
573
574 SkScalar lineC = -(aYDown.dot(normal));
575 for (int i = 0; i < kVertsPerLineSeg; ++i) {
576 (*vert)[i].fPos = (i < 2) ? a : b;
577 if (0 == i || 3 == i) {
578 (*vert)[i].fPos -= orthVec;
579 } else {
580 (*vert)[i].fPos += orthVec;
581 }
582 (*vert)[i].fLine.fA = normal.fX;
583 (*vert)[i].fLine.fB = normal.fY;
584 (*vert)[i].fLine.fC = lineC;
585 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000586 if (NULL != toSrc) {
587 toSrc->mapPointsWithStride(&(*vert)->fPos,
588 sizeof(Vertex),
589 kVertsPerLineSeg);
590 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000591 } else {
592 // just make it degenerate and likely offscreen
593 (*vert)[0].fPos.set(SK_ScalarMax, SK_ScalarMax);
594 (*vert)[1].fPos.set(SK_ScalarMax, SK_ScalarMax);
595 (*vert)[2].fPos.set(SK_ScalarMax, SK_ScalarMax);
596 (*vert)[3].fPos.set(SK_ScalarMax, SK_ScalarMax);
597 }
598
599 *vert += kVertsPerLineSeg;
600}
601
602}
603
604bool GrAAHairLinePathRenderer::createGeom(GrDrawTarget::StageBitfield stages) {
605
606 int rtHeight = fTarget->getRenderTarget()->height();
607
608 GrIRect clip;
609 if (fTarget->getClip().hasConservativeBounds()) {
610 GrRect clipRect = fTarget->getClip().getConservativeBounds();
611 clipRect.roundOut(&clip);
612 } else {
613 clip.setLargest();
614 }
615
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000616 // If none of the inputs that affect generation of path geometry have
617 // have changed since last previous path draw then we can reuse the
618 // previous geoemtry.
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000619 if (stages == fPreviousStages &&
620 fPreviousViewMatrix == fTarget->getViewMatrix() &&
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000621 fPreviousTranslate == fTranslate &&
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000622 rtHeight == fPreviousRTHeight &&
623 fClipRect == clip) {
624 return true;
625 }
626
627 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
628 for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
629 if ((1 << s) & stages) {
630 layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s);
631 }
632 }
633
634 GrMatrix viewM = fTarget->getViewMatrix();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000635
636 GrAlignedSTStorage<128, GrPoint> lineStorage;
637 GrAlignedSTStorage<128, GrPoint> quadStorage;
638 PtArray lines(&lineStorage);
639 PtArray quads(&quadStorage);
640 IntArray qSubdivs;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000641 fQuadCnt = generate_lines_and_quads(*fPath, viewM, fTranslate, clip,
642 &lines, &quads, &qSubdivs);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000643
644 fLineSegmentCnt = lines.count() / 2;
645 int vertCnt = kVertsPerLineSeg * fLineSegmentCnt + kVertsPerQuad * fQuadCnt;
646
647 GrAssert(sizeof(Vertex) == GrDrawTarget::VertexSize(layout));
648
649 Vertex* verts;
650 if (!fTarget->reserveVertexSpace(layout, vertCnt, (void**)&verts)) {
651 return false;
652 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000653 Vertex* base = verts;
654
655 const GrMatrix* toDevice = NULL;
656 const GrMatrix* toSrc = NULL;
657 GrMatrix ivm;
658
659 if (viewM.hasPerspective()) {
660 if (viewM.invert(&ivm)) {
661 toDevice = &viewM;
662 toSrc = &ivm;
663 }
664 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000665
666 for (int i = 0; i < fLineSegmentCnt; ++i) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000667 add_line(&lines[2*i], rtHeight, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000668 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000669
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000670 int unsubdivQuadCnt = quads.count() / 3;
671 for (int i = 0; i < unsubdivQuadCnt; ++i) {
672 GrAssert(qSubdivs[i] >= 0);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000673 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000674 }
675
676 fPreviousStages = stages;
677 fPreviousViewMatrix = fTarget->getViewMatrix();
678 fPreviousRTHeight = rtHeight;
679 fClipRect = clip;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000680 fPreviousTranslate = fTranslate;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000681 return true;
682}
683
684void GrAAHairLinePathRenderer::drawPath(GrDrawTarget::StageBitfield stages) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000685
686 if (!this->createGeom(stages)) {
687 return;
688 }
689
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000690 GrDrawTarget::AutoStateRestore asr;
691 if (!fTarget->getViewMatrix().hasPerspective()) {
692 asr.set(fTarget);
693 GrMatrix ivm;
694 if (fTarget->getViewInverse(&ivm)) {
695 fTarget->preConcatSamplerMatrices(stages, ivm);
696 }
697 fTarget->setViewMatrix(GrMatrix::I());
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000698 }
699
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000700 // TODO: See whether rendering lines as degenerate quads improves perf
701 // when we have a mix
702 fTarget->setIndexSourceToBuffer(fLinesIndexBuffer);
703 int lines = 0;
704 int nBufLines = fLinesIndexBuffer->maxQuads();
705 while (lines < fLineSegmentCnt) {
706 int n = GrMin(fLineSegmentCnt-lines, nBufLines);
707 fTarget->setVertexEdgeType(GrDrawTarget::kHairLine_EdgeType);
708 fTarget->drawIndexed(kTriangles_PrimitiveType,
709 kVertsPerLineSeg*lines, // startV
710 0, // startI
711 kVertsPerLineSeg*n, // vCount
712 kIdxsPerLineSeg*n); // iCount
713 lines += n;
714 }
715
716 fTarget->setIndexSourceToBuffer(fQuadsIndexBuffer);
717 int quads = 0;
718 while (quads < fQuadCnt) {
719 int n = GrMin(fQuadCnt-quads, kNumQuadsInIdxBuffer);
720 fTarget->setVertexEdgeType(GrDrawTarget::kHairQuad_EdgeType);
721 fTarget->drawIndexed(kTriangles_PrimitiveType,
722 4*fLineSegmentCnt + kVertsPerQuad*quads, // startV
723 0, // startI
724 kVertsPerQuad*n, // vCount
725 kIdxsPerQuad*n); // iCount
726 quads += n;
727 }
728
729}
730