blob: 4d05a6bb7dfc096c20a34ed5308b521b8394f348 [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) {
bsalomon@google.com18c9c192011-09-22 21:01:31 +000090 return context->getGpu()->getCaps().fShaderDerivativeSupport;
bsalomon@google.comaeb21602011-08-30 18:13:44 +000091
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
bsalomon@google.com49313f62011-09-14 13:54:05 +0000140typedef SkTArray<SkPoint, true> PtArray;
141typedef SkTArray<int, true> IntArray;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000142
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
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000222 const int* iptr = (const int*)&x;
223 return (((*iptr) & 0x7f800000) >> 23) - 127;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000224}
225
226// we subdivide the quads to avoid huge overfill
227// if it returns -1 then should be drawn as lines
228int num_quad_subdivs(const SkPoint p[3]) {
229 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000230 static const SkScalar gDegenerateToLineTolSqd =
231 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000232
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000233 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
234 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000235 return -1;
236 }
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000237
238 GrScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
239 if (dsqd < gDegenerateToLineTolSqd) {
240 return -1;
241 }
242
243 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000244 return -1;
245 }
246
247 static const int kMaxSub = 4;
248 // tolerance of triangle height in pixels
249 // tuned on windows Quadro FX 380 / Z600
250 // trade off of fill vs cpu time on verts
251 // maybe different when do this using gpu (geo or tess shaders)
252 static const SkScalar gSubdivTol = 175 * SK_Scalar1;
253
254 if (dsqd <= gSubdivTol*gSubdivTol) {
255 return 0;
256 } else {
257 // subdividing the quad reduces d by 4. so we want x = log4(d/tol)
258 // = log4(d*d/tol*tol)/2
259 // = log2(d*d/tol*tol)
260
261#ifdef SK_SCALAR_IS_FLOAT
262 // +1 since we're ignoring the mantissa contribution.
263 int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
264 log = GrMin(GrMax(0, log), kMaxSub);
265 return log;
266#else
bsalomon@google.comfcb0dbc2011-08-30 18:35:18 +0000267 SkScalar log = SkScalarLog(SkScalarDiv(dsqd,gSubdivTol*gSubdivTol));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000268 static const SkScalar conv = SkScalarInvert(SkScalarLog(2));
269 log = SkScalarMul(log, conv);
270 return GrMin(GrMax(0, SkScalarCeilToInt(log)),kMaxSub);
271#endif
272 }
273}
274
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000275/**
276 * Generates the lines and quads to be rendered. Lines are always recorded in
277 * device space. We will do a device space bloat to account for the 1pixel
278 * thickness.
279 * Quads are recorded in device space unless m contains
280 * perspective, then in they are in src space. We do this because we will
281 * subdivide large quads to reduce over-fill. This subdivision has to be
282 * performed before applying the perspective matrix.
283 */
284int generate_lines_and_quads(const SkPath& path,
285 const SkMatrix& m,
286 const SkVector& translate,
287 GrIRect clip,
288 PtArray* lines,
289 PtArray* quads,
290 IntArray* quadSubdivCnts) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000291 SkPath::Iter iter(path, false);
292
293 int totalQuadCount = 0;
294 GrRect bounds;
295 GrIRect ibounds;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000296
297 bool persp = m.hasPerspective();
298
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000299 for (;;) {
300 GrPoint pts[4];
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000301 GrPoint devPts[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000302 GrPathCmd cmd = (GrPathCmd)iter.next(pts);
303 switch (cmd) {
304 case kMove_PathCmd:
305 break;
306 case kLine_PathCmd:
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000307 SkPoint::Offset(pts, 2, translate);
308 m.mapPoints(devPts, pts, 2);
309 bounds.setBounds(devPts, 2);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000310 bounds.outset(SK_Scalar1, SK_Scalar1);
311 bounds.roundOut(&ibounds);
312 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000313 SkPoint* pts = lines->push_back_n(2);
314 pts[0] = devPts[0];
315 pts[1] = devPts[1];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000316 }
317 break;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000318 case kQuadratic_PathCmd:
319 SkPoint::Offset(pts, 3, translate);
320 m.mapPoints(devPts, pts, 3);
321 bounds.setBounds(devPts, 3);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000322 bounds.outset(SK_Scalar1, SK_Scalar1);
323 bounds.roundOut(&ibounds);
324 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000325 int subdiv = num_quad_subdivs(devPts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000326 GrAssert(subdiv >= -1);
327 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000328 SkPoint* pts = lines->push_back_n(4);
329 pts[0] = devPts[0];
330 pts[1] = devPts[1];
331 pts[2] = devPts[1];
332 pts[3] = devPts[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000333 } else {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000334 // when in perspective keep quads in src space
335 SkPoint* qPts = persp ? pts : devPts;
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000336 SkPoint* pts = quads->push_back_n(3);
337 pts[0] = qPts[0];
338 pts[1] = qPts[1];
339 pts[2] = qPts[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000340 quadSubdivCnts->push_back() = subdiv;
341 totalQuadCount += 1 << subdiv;
342 }
343 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000344 break;
345 case kCubic_PathCmd:
346 SkPoint::Offset(pts, 4, translate);
347 m.mapPoints(devPts, pts, 4);
348 bounds.setBounds(devPts, 4);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000349 bounds.outset(SK_Scalar1, SK_Scalar1);
350 bounds.roundOut(&ibounds);
351 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000352 SkPoint stackStorage[32];
353 PtArray q((void*)stackStorage, 32);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000354 // in perspective have to do conversion in src space
355 if (persp) {
356 SkScalar tolScale =
357 GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m,
358 path.getBounds());
359 convert_cubic_to_quads(pts, tolScale, &q);
360 } else {
361 convert_cubic_to_quads(devPts, SK_Scalar1, &q);
362 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000363 for (int i = 0; i < q.count(); i += 3) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000364 SkPoint* qInDevSpace;
365 // bounds has to be calculated in device space, but q is
366 // in src space when there is perspective.
367 if (persp) {
368 m.mapPoints(devPts, &q[i], 3);
369 bounds.setBounds(devPts, 3);
370 qInDevSpace = devPts;
371 } else {
372 bounds.setBounds(&q[i], 3);
373 qInDevSpace = &q[i];
374 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000375 bounds.outset(SK_Scalar1, SK_Scalar1);
376 bounds.roundOut(&ibounds);
377 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000378 int subdiv = num_quad_subdivs(qInDevSpace);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000379 GrAssert(subdiv >= -1);
380 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000381 SkPoint* pts = lines->push_back_n(4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000382 // lines should always be in device coords
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000383 pts[0] = qInDevSpace[0];
384 pts[1] = qInDevSpace[1];
385 pts[2] = qInDevSpace[1];
386 pts[3] = qInDevSpace[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000387 } else {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000388 SkPoint* pts = quads->push_back_n(3);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000389 // q is already in src space when there is no
390 // perspective and dev coords otherwise.
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000391 pts[0] = q[0 + i];
392 pts[1] = q[1 + i];
393 pts[2] = q[2 + i];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000394 quadSubdivCnts->push_back() = subdiv;
395 totalQuadCount += 1 << subdiv;
396 }
397 }
398 }
399 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000400 break;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000401 case kClose_PathCmd:
402 break;
403 case kEnd_PathCmd:
404 return totalQuadCount;
405 }
406 }
407}
408
409struct Vertex {
410 GrPoint fPos;
411 union {
412 struct {
413 GrScalar fA;
414 GrScalar fB;
415 GrScalar fC;
416 } fLine;
417 GrVec fQuadCoord;
418 struct {
419 GrScalar fBogus[4];
420 };
421 };
422};
423GR_STATIC_ASSERT(sizeof(Vertex) == 3 * sizeof(GrPoint));
424
425void intersect_lines(const SkPoint& ptA, const SkVector& normA,
426 const SkPoint& ptB, const SkVector& normB,
427 SkPoint* result) {
428
429 SkScalar lineAW = -normA.dot(ptA);
430 SkScalar lineBW = -normB.dot(ptB);
431
432 SkScalar wInv = SkScalarMul(normA.fX, normB.fY) -
433 SkScalarMul(normA.fY, normB.fX);
434 wInv = SkScalarInvert(wInv);
435
436 result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY);
437 result->fX = SkScalarMul(result->fX, wInv);
438
439 result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW);
440 result->fY = SkScalarMul(result->fY, wInv);
441}
442
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000443void bloat_quad(const SkPoint qpts[3], const GrMatrix* toDevice,
444 const GrMatrix* toSrc, Vertex verts[kVertsPerQuad]) {
445 GrAssert(!toDevice == !toSrc);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000446 // original quad is specified by tri a,b,c
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000447 SkPoint a = qpts[0];
448 SkPoint b = qpts[1];
449 SkPoint c = qpts[2];
450
451 // compute a matrix that goes from device coords to U,V quad params
452 // this should be in the src space, not dev coords, when we have perspective
453 SkMatrix DevToUV;
454 DevToUV.setAll(a.fX, b.fX, c.fX,
455 a.fY, b.fY, c.fY,
456 SK_Scalar1, SK_Scalar1, SK_Scalar1);
457 DevToUV.invert(&DevToUV);
458 // can't make this static, no cons :(
459 SkMatrix UVpts;
460 UVpts.setAll(0, SK_ScalarHalf, SK_Scalar1,
461 0, 0, SK_Scalar1,
462 SK_Scalar1, SK_Scalar1, SK_Scalar1);
463 DevToUV.postConcat(UVpts);
464
465 // We really want to avoid perspective matrix muls.
466 // These may wind up really close to zero
467 DevToUV.setPerspX(0);
468 DevToUV.setPerspY(0);
469
470 if (toDevice) {
471 toDevice->mapPoints(&a, 1);
472 toDevice->mapPoints(&b, 1);
473 toDevice->mapPoints(&c, 1);
474 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000475 // make a new poly where we replace a and c by a 1-pixel wide edges orthog
476 // to edges ab and bc:
477 //
478 // before | after
479 // | b0
480 // b |
481 // |
482 // | a0 c0
483 // a c | a1 c1
484 //
485 // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c,
486 // respectively.
487 Vertex& a0 = verts[0];
488 Vertex& a1 = verts[1];
489 Vertex& b0 = verts[2];
490 Vertex& c0 = verts[3];
491 Vertex& c1 = verts[4];
492
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000493 SkVector ab = b;
494 ab -= a;
495 SkVector ac = c;
496 ac -= a;
497 SkVector cb = b;
498 cb -= c;
499
500 // We should have already handled degenerates
501 GrAssert(ab.length() > 0 && cb.length() > 0);
502
503 ab.normalize();
504 SkVector abN;
505 abN.setOrthog(ab, SkVector::kLeft_Side);
506 if (abN.dot(ac) > 0) {
507 abN.negate();
508 }
509
510 cb.normalize();
511 SkVector cbN;
512 cbN.setOrthog(cb, SkVector::kLeft_Side);
513 if (cbN.dot(ac) < 0) {
514 cbN.negate();
515 }
516
517 a0.fPos = a;
518 a0.fPos += abN;
519 a1.fPos = a;
520 a1.fPos -= abN;
521
522 c0.fPos = c;
523 c0.fPos += cbN;
524 c1.fPos = c;
525 c1.fPos -= cbN;
526
527 intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos);
528
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000529 if (toSrc) {
530 toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(Vertex), kVertsPerQuad);
531 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000532 DevToUV.mapPointsWithStride(&verts[0].fQuadCoord,
533 &verts[0].fPos, sizeof(Vertex), kVertsPerQuad);
534}
535
536void add_quads(const SkPoint p[3],
537 int subdiv,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000538 const GrMatrix* toDevice,
539 const GrMatrix* toSrc,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000540 Vertex** vert) {
541 GrAssert(subdiv >= 0);
542 if (subdiv) {
543 SkPoint newP[5];
544 SkChopQuadAtHalf(p, newP);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000545 add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert);
546 add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000547 } else {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000548 bloat_quad(p, toDevice, toSrc, *vert);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000549 *vert += kVertsPerQuad;
550 }
551}
552
553void add_line(const SkPoint p[2],
554 int rtHeight,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000555 const SkMatrix* toSrc,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000556 Vertex** vert) {
557 const SkPoint& a = p[0];
558 const SkPoint& b = p[1];
559
560 SkVector orthVec = b;
561 orthVec -= a;
562
563 if (orthVec.setLength(SK_Scalar1)) {
564 orthVec.setOrthog(orthVec);
565
566 // the values we pass down to the frag shader
567 // have to be in y-points-up space;
568 SkVector normal;
569 normal.fX = orthVec.fX;
570 normal.fY = -orthVec.fY;
571 SkPoint aYDown;
572 aYDown.fX = a.fX;
573 aYDown.fY = rtHeight - a.fY;
574
575 SkScalar lineC = -(aYDown.dot(normal));
576 for (int i = 0; i < kVertsPerLineSeg; ++i) {
577 (*vert)[i].fPos = (i < 2) ? a : b;
578 if (0 == i || 3 == i) {
579 (*vert)[i].fPos -= orthVec;
580 } else {
581 (*vert)[i].fPos += orthVec;
582 }
583 (*vert)[i].fLine.fA = normal.fX;
584 (*vert)[i].fLine.fB = normal.fY;
585 (*vert)[i].fLine.fC = lineC;
586 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000587 if (NULL != toSrc) {
588 toSrc->mapPointsWithStride(&(*vert)->fPos,
589 sizeof(Vertex),
590 kVertsPerLineSeg);
591 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000592 } else {
593 // just make it degenerate and likely offscreen
594 (*vert)[0].fPos.set(SK_ScalarMax, SK_ScalarMax);
595 (*vert)[1].fPos.set(SK_ScalarMax, SK_ScalarMax);
596 (*vert)[2].fPos.set(SK_ScalarMax, SK_ScalarMax);
597 (*vert)[3].fPos.set(SK_ScalarMax, SK_ScalarMax);
598 }
599
600 *vert += kVertsPerLineSeg;
601}
602
603}
604
605bool GrAAHairLinePathRenderer::createGeom(GrDrawTarget::StageBitfield stages) {
606
607 int rtHeight = fTarget->getRenderTarget()->height();
608
609 GrIRect clip;
610 if (fTarget->getClip().hasConservativeBounds()) {
611 GrRect clipRect = fTarget->getClip().getConservativeBounds();
612 clipRect.roundOut(&clip);
613 } else {
614 clip.setLargest();
615 }
616
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000617 // If none of the inputs that affect generation of path geometry have
618 // have changed since last previous path draw then we can reuse the
619 // previous geoemtry.
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000620 if (stages == fPreviousStages &&
621 fPreviousViewMatrix == fTarget->getViewMatrix() &&
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000622 fPreviousTranslate == fTranslate &&
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000623 rtHeight == fPreviousRTHeight &&
624 fClipRect == clip) {
625 return true;
626 }
627
628 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
629 for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
630 if ((1 << s) & stages) {
631 layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s);
632 }
633 }
634
635 GrMatrix viewM = fTarget->getViewMatrix();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000636
bsalomon@google.com49313f62011-09-14 13:54:05 +0000637 SkAlignedSTStorage<128, GrPoint> lineStorage;
638 SkAlignedSTStorage<128, GrPoint> quadStorage;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000639 PtArray lines(&lineStorage);
640 PtArray quads(&quadStorage);
641 IntArray qSubdivs;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000642 fQuadCnt = generate_lines_and_quads(*fPath, viewM, fTranslate, clip,
643 &lines, &quads, &qSubdivs);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000644
645 fLineSegmentCnt = lines.count() / 2;
646 int vertCnt = kVertsPerLineSeg * fLineSegmentCnt + kVertsPerQuad * fQuadCnt;
647
648 GrAssert(sizeof(Vertex) == GrDrawTarget::VertexSize(layout));
649
650 Vertex* verts;
651 if (!fTarget->reserveVertexSpace(layout, vertCnt, (void**)&verts)) {
652 return false;
653 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000654 Vertex* base = verts;
655
656 const GrMatrix* toDevice = NULL;
657 const GrMatrix* toSrc = NULL;
658 GrMatrix ivm;
659
660 if (viewM.hasPerspective()) {
661 if (viewM.invert(&ivm)) {
662 toDevice = &viewM;
663 toSrc = &ivm;
664 }
665 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000666
667 for (int i = 0; i < fLineSegmentCnt; ++i) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000668 add_line(&lines[2*i], rtHeight, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000669 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000670
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000671 int unsubdivQuadCnt = quads.count() / 3;
672 for (int i = 0; i < unsubdivQuadCnt; ++i) {
673 GrAssert(qSubdivs[i] >= 0);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000674 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000675 }
676
677 fPreviousStages = stages;
678 fPreviousViewMatrix = fTarget->getViewMatrix();
679 fPreviousRTHeight = rtHeight;
680 fClipRect = clip;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000681 fPreviousTranslate = fTranslate;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000682 return true;
683}
684
685void GrAAHairLinePathRenderer::drawPath(GrDrawTarget::StageBitfield stages) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000686
687 if (!this->createGeom(stages)) {
688 return;
689 }
690
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000691 GrDrawTarget::AutoStateRestore asr;
692 if (!fTarget->getViewMatrix().hasPerspective()) {
693 asr.set(fTarget);
694 GrMatrix ivm;
695 if (fTarget->getViewInverse(&ivm)) {
696 fTarget->preConcatSamplerMatrices(stages, ivm);
697 }
698 fTarget->setViewMatrix(GrMatrix::I());
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000699 }
700
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000701 // TODO: See whether rendering lines as degenerate quads improves perf
702 // when we have a mix
703 fTarget->setIndexSourceToBuffer(fLinesIndexBuffer);
704 int lines = 0;
705 int nBufLines = fLinesIndexBuffer->maxQuads();
706 while (lines < fLineSegmentCnt) {
707 int n = GrMin(fLineSegmentCnt-lines, nBufLines);
708 fTarget->setVertexEdgeType(GrDrawTarget::kHairLine_EdgeType);
709 fTarget->drawIndexed(kTriangles_PrimitiveType,
710 kVertsPerLineSeg*lines, // startV
711 0, // startI
712 kVertsPerLineSeg*n, // vCount
713 kIdxsPerLineSeg*n); // iCount
714 lines += n;
715 }
716
717 fTarget->setIndexSourceToBuffer(fQuadsIndexBuffer);
718 int quads = 0;
719 while (quads < fQuadCnt) {
720 int n = GrMin(fQuadCnt-quads, kNumQuadsInIdxBuffer);
721 fTarget->setVertexEdgeType(GrDrawTarget::kHairQuad_EdgeType);
722 fTarget->drawIndexed(kTriangles_PrimitiveType,
723 4*fLineSegmentCnt + kVertsPerQuad*quads, // startV
724 0, // startI
725 kVertsPerQuad*n, // vCount
726 kIdxsPerQuad*n); // iCount
727 quads += n;
728 }
729
730}
731