blob: f836188c1292e8975e3e51ab9ad5197f0fe26661 [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) {
bsalomon@google.coma8a6a322011-09-23 14:19:58 +000069 const GrIndexBuffer* lIdxBuffer = context->getQuadIndexBuffer();
70 if (NULL == lIdxBuffer) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +000071 return NULL;
72 }
bsalomon@google.coma8a6a322011-09-23 14:19:58 +000073 GrGpu* gpu = context->getGpu();
74 GrIndexBuffer* qIdxBuf = gpu->createIndexBuffer(kQuadIdxSBufize, false);
75 SkAutoTUnref<GrIndexBuffer> qIdxBuffer(qIdxBuf);
76 if (NULL == qIdxBuf ||
77 !push_quad_index_data(qIdxBuf)) {
78 return NULL;
79 }
80 return new GrAAHairLinePathRenderer(context,
81 lIdxBuffer,
82 qIdxBuf);
bsalomon@google.comaeb21602011-08-30 18:13:44 +000083}
84
85GrAAHairLinePathRenderer::GrAAHairLinePathRenderer(
86 const GrContext* context,
87 const GrIndexBuffer* linesIndexBuffer,
88 const GrIndexBuffer* quadsIndexBuffer) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +000089 fLinesIndexBuffer = linesIndexBuffer;
90 linesIndexBuffer->ref();
91 fQuadsIndexBuffer = quadsIndexBuffer;
92 quadsIndexBuffer->ref();
93 this->resetGeom();
94}
95
96GrAAHairLinePathRenderer::~GrAAHairLinePathRenderer() {
97 fLinesIndexBuffer->unref();
98 fQuadsIndexBuffer->unref();
99}
100
101bool GrAAHairLinePathRenderer::supportsAA(GrDrawTarget* target,
102 const SkPath& path,
103 GrPathFill fill) {
104 return kHairLine_PathFill == fill;
105}
106
107bool GrAAHairLinePathRenderer::canDrawPath(const GrDrawTarget* target,
108 const SkPath& path,
109 GrPathFill fill) const {
bsalomon@google.coma8a6a322011-09-23 14:19:58 +0000110 static const uint32_t gReqDerivMask = SkPath::kCubic_SegmentMask |
111 SkPath::kQuad_SegmentMask;
112 return (kHairLine_PathFill == fill &&
113 target->isAntialiasState() &&
114 (target->getCaps().fShaderDerivativeSupport ||
115 !(gReqDerivMask & path.getSegmentMasks())));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000116}
117
118void GrAAHairLinePathRenderer::pathWillClear() {
119 this->resetGeom();
120}
121
122void GrAAHairLinePathRenderer::resetGeom() {
123 fPreviousStages = ~0;
124 fPreviousRTHeight = ~0;
125 fPreviousViewMatrix = GrMatrix::InvalidMatrix();
126 fLineSegmentCnt = 0;
127 fQuadCnt = 0;
128 if ((fQuadCnt || fLineSegmentCnt) && NULL != fTarget) {
129 fTarget->resetVertexSource();
130 }
131}
132
133namespace {
134
bsalomon@google.com49313f62011-09-14 13:54:05 +0000135typedef SkTArray<SkPoint, true> PtArray;
136typedef SkTArray<int, true> IntArray;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000137
138/**
139 * We convert cubics to quadratics (for now).
140 */
141void convert_noninflect_cubic_to_quads(const SkPoint p[4],
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000142 SkScalar tolScale,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000143 PtArray* quads,
144 int sublevel = 0) {
145 SkVector ab = p[1];
146 ab -= p[0];
147 SkVector dc = p[2];
148 dc -= p[3];
149
150 static const SkScalar gLengthScale = 3 * SK_Scalar1 / 2;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000151 // base tolerance is 2 pixels in dev coords.
152 const SkScalar distanceSqdTol = SkScalarMul(tolScale, 2 * SK_Scalar1);
153 static const int kMaxSubdivs = 10;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000154
155 ab.scale(gLengthScale);
156 dc.scale(gLengthScale);
157
158 SkVector c0 = p[0];
159 c0 += ab;
160 SkVector c1 = p[3];
161 c1 += dc;
162
163 SkScalar dSqd = c0.distanceToSqd(c1);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000164 if (sublevel > kMaxSubdivs || dSqd <= distanceSqdTol) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000165 SkPoint cAvg = c0;
166 cAvg += c1;
167 cAvg.scale(SK_ScalarHalf);
168
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000169 SkPoint* pts = quads->push_back_n(3);
170 pts[0] = p[0];
171 pts[1] = cAvg;
172 pts[2] = p[3];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000173
174 return;
175 } else {
176 SkPoint choppedPts[7];
177 SkChopCubicAtHalf(p, choppedPts);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000178 convert_noninflect_cubic_to_quads(choppedPts + 0, tolScale,
179 quads, sublevel + 1);
180 convert_noninflect_cubic_to_quads(choppedPts + 3, tolScale,
181 quads, sublevel + 1);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000182 }
183}
184
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000185void convert_cubic_to_quads(const SkPoint p[4],
186 SkScalar tolScale,
187 PtArray* quads) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000188 SkPoint chopped[13];
189 int count = SkChopCubicAtInflections(p, chopped);
190
191 for (int i = 0; i < count; ++i) {
192 SkPoint* cubic = chopped + 3*i;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000193 convert_noninflect_cubic_to_quads(cubic, tolScale, quads);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000194 }
195}
196
197// Takes 178th time of logf on Z600 / VC2010
198int get_float_exp(float x) {
199 GR_STATIC_ASSERT(sizeof(int) == sizeof(float));
200#if GR_DEBUG
201 static bool tested;
202 if (!tested) {
203 tested = true;
204 GrAssert(get_float_exp(0.25f) == -2);
205 GrAssert(get_float_exp(0.3f) == -2);
206 GrAssert(get_float_exp(0.5f) == -1);
207 GrAssert(get_float_exp(1.f) == 0);
208 GrAssert(get_float_exp(2.f) == 1);
209 GrAssert(get_float_exp(2.5f) == 1);
210 GrAssert(get_float_exp(8.f) == 3);
211 GrAssert(get_float_exp(100.f) == 6);
212 GrAssert(get_float_exp(1000.f) == 9);
213 GrAssert(get_float_exp(1024.f) == 10);
214 GrAssert(get_float_exp(3000000.f) == 21);
215 }
216#endif
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000217 const int* iptr = (const int*)&x;
218 return (((*iptr) & 0x7f800000) >> 23) - 127;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000219}
220
221// we subdivide the quads to avoid huge overfill
222// if it returns -1 then should be drawn as lines
223int num_quad_subdivs(const SkPoint p[3]) {
224 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000225 static const SkScalar gDegenerateToLineTolSqd =
226 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000227
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000228 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
229 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000230 return -1;
231 }
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000232
233 GrScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
234 if (dsqd < gDegenerateToLineTolSqd) {
235 return -1;
236 }
237
238 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000239 return -1;
240 }
241
242 static const int kMaxSub = 4;
243 // tolerance of triangle height in pixels
244 // tuned on windows Quadro FX 380 / Z600
245 // trade off of fill vs cpu time on verts
246 // maybe different when do this using gpu (geo or tess shaders)
247 static const SkScalar gSubdivTol = 175 * SK_Scalar1;
248
249 if (dsqd <= gSubdivTol*gSubdivTol) {
250 return 0;
251 } else {
252 // subdividing the quad reduces d by 4. so we want x = log4(d/tol)
253 // = log4(d*d/tol*tol)/2
254 // = log2(d*d/tol*tol)
255
256#ifdef SK_SCALAR_IS_FLOAT
257 // +1 since we're ignoring the mantissa contribution.
258 int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
259 log = GrMin(GrMax(0, log), kMaxSub);
260 return log;
261#else
bsalomon@google.comfcb0dbc2011-08-30 18:35:18 +0000262 SkScalar log = SkScalarLog(SkScalarDiv(dsqd,gSubdivTol*gSubdivTol));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000263 static const SkScalar conv = SkScalarInvert(SkScalarLog(2));
264 log = SkScalarMul(log, conv);
265 return GrMin(GrMax(0, SkScalarCeilToInt(log)),kMaxSub);
266#endif
267 }
268}
269
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000270/**
271 * Generates the lines and quads to be rendered. Lines are always recorded in
272 * device space. We will do a device space bloat to account for the 1pixel
273 * thickness.
274 * Quads are recorded in device space unless m contains
275 * perspective, then in they are in src space. We do this because we will
276 * subdivide large quads to reduce over-fill. This subdivision has to be
277 * performed before applying the perspective matrix.
278 */
279int generate_lines_and_quads(const SkPath& path,
280 const SkMatrix& m,
281 const SkVector& translate,
282 GrIRect clip,
283 PtArray* lines,
284 PtArray* quads,
285 IntArray* quadSubdivCnts) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000286 SkPath::Iter iter(path, false);
287
288 int totalQuadCount = 0;
289 GrRect bounds;
290 GrIRect ibounds;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000291
292 bool persp = m.hasPerspective();
293
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000294 for (;;) {
295 GrPoint pts[4];
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000296 GrPoint devPts[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000297 GrPathCmd cmd = (GrPathCmd)iter.next(pts);
298 switch (cmd) {
299 case kMove_PathCmd:
300 break;
301 case kLine_PathCmd:
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000302 SkPoint::Offset(pts, 2, translate);
303 m.mapPoints(devPts, pts, 2);
304 bounds.setBounds(devPts, 2);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000305 bounds.outset(SK_Scalar1, SK_Scalar1);
306 bounds.roundOut(&ibounds);
307 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000308 SkPoint* pts = lines->push_back_n(2);
309 pts[0] = devPts[0];
310 pts[1] = devPts[1];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000311 }
312 break;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000313 case kQuadratic_PathCmd:
314 SkPoint::Offset(pts, 3, translate);
315 m.mapPoints(devPts, pts, 3);
316 bounds.setBounds(devPts, 3);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000317 bounds.outset(SK_Scalar1, SK_Scalar1);
318 bounds.roundOut(&ibounds);
319 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000320 int subdiv = num_quad_subdivs(devPts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000321 GrAssert(subdiv >= -1);
322 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000323 SkPoint* pts = lines->push_back_n(4);
324 pts[0] = devPts[0];
325 pts[1] = devPts[1];
326 pts[2] = devPts[1];
327 pts[3] = devPts[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000328 } else {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000329 // when in perspective keep quads in src space
330 SkPoint* qPts = persp ? pts : devPts;
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000331 SkPoint* pts = quads->push_back_n(3);
332 pts[0] = qPts[0];
333 pts[1] = qPts[1];
334 pts[2] = qPts[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000335 quadSubdivCnts->push_back() = subdiv;
336 totalQuadCount += 1 << subdiv;
337 }
338 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000339 break;
340 case kCubic_PathCmd:
341 SkPoint::Offset(pts, 4, translate);
342 m.mapPoints(devPts, pts, 4);
343 bounds.setBounds(devPts, 4);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000344 bounds.outset(SK_Scalar1, SK_Scalar1);
345 bounds.roundOut(&ibounds);
346 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000347 SkPoint stackStorage[32];
348 PtArray q((void*)stackStorage, 32);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000349 // in perspective have to do conversion in src space
350 if (persp) {
351 SkScalar tolScale =
352 GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m,
353 path.getBounds());
354 convert_cubic_to_quads(pts, tolScale, &q);
355 } else {
356 convert_cubic_to_quads(devPts, SK_Scalar1, &q);
357 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000358 for (int i = 0; i < q.count(); i += 3) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000359 SkPoint* qInDevSpace;
360 // bounds has to be calculated in device space, but q is
361 // in src space when there is perspective.
362 if (persp) {
363 m.mapPoints(devPts, &q[i], 3);
364 bounds.setBounds(devPts, 3);
365 qInDevSpace = devPts;
366 } else {
367 bounds.setBounds(&q[i], 3);
368 qInDevSpace = &q[i];
369 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000370 bounds.outset(SK_Scalar1, SK_Scalar1);
371 bounds.roundOut(&ibounds);
372 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000373 int subdiv = num_quad_subdivs(qInDevSpace);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000374 GrAssert(subdiv >= -1);
375 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000376 SkPoint* pts = lines->push_back_n(4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000377 // lines should always be in device coords
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000378 pts[0] = qInDevSpace[0];
379 pts[1] = qInDevSpace[1];
380 pts[2] = qInDevSpace[1];
381 pts[3] = qInDevSpace[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000382 } else {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000383 SkPoint* pts = quads->push_back_n(3);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000384 // q is already in src space when there is no
385 // perspective and dev coords otherwise.
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000386 pts[0] = q[0 + i];
387 pts[1] = q[1 + i];
388 pts[2] = q[2 + i];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000389 quadSubdivCnts->push_back() = subdiv;
390 totalQuadCount += 1 << subdiv;
391 }
392 }
393 }
394 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000395 break;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000396 case kClose_PathCmd:
397 break;
398 case kEnd_PathCmd:
399 return totalQuadCount;
400 }
401 }
402}
403
404struct Vertex {
405 GrPoint fPos;
406 union {
407 struct {
408 GrScalar fA;
409 GrScalar fB;
410 GrScalar fC;
411 } fLine;
412 GrVec fQuadCoord;
413 struct {
414 GrScalar fBogus[4];
415 };
416 };
417};
418GR_STATIC_ASSERT(sizeof(Vertex) == 3 * sizeof(GrPoint));
419
420void intersect_lines(const SkPoint& ptA, const SkVector& normA,
421 const SkPoint& ptB, const SkVector& normB,
422 SkPoint* result) {
423
424 SkScalar lineAW = -normA.dot(ptA);
425 SkScalar lineBW = -normB.dot(ptB);
426
427 SkScalar wInv = SkScalarMul(normA.fX, normB.fY) -
428 SkScalarMul(normA.fY, normB.fX);
429 wInv = SkScalarInvert(wInv);
430
431 result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY);
432 result->fX = SkScalarMul(result->fX, wInv);
433
434 result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW);
435 result->fY = SkScalarMul(result->fY, wInv);
436}
437
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000438void bloat_quad(const SkPoint qpts[3], const GrMatrix* toDevice,
439 const GrMatrix* toSrc, Vertex verts[kVertsPerQuad]) {
440 GrAssert(!toDevice == !toSrc);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000441 // original quad is specified by tri a,b,c
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000442 SkPoint a = qpts[0];
443 SkPoint b = qpts[1];
444 SkPoint c = qpts[2];
445
446 // compute a matrix that goes from device coords to U,V quad params
447 // this should be in the src space, not dev coords, when we have perspective
448 SkMatrix DevToUV;
449 DevToUV.setAll(a.fX, b.fX, c.fX,
450 a.fY, b.fY, c.fY,
451 SK_Scalar1, SK_Scalar1, SK_Scalar1);
452 DevToUV.invert(&DevToUV);
453 // can't make this static, no cons :(
454 SkMatrix UVpts;
455 UVpts.setAll(0, SK_ScalarHalf, SK_Scalar1,
456 0, 0, SK_Scalar1,
457 SK_Scalar1, SK_Scalar1, SK_Scalar1);
458 DevToUV.postConcat(UVpts);
459
460 // We really want to avoid perspective matrix muls.
461 // These may wind up really close to zero
462 DevToUV.setPerspX(0);
463 DevToUV.setPerspY(0);
464
465 if (toDevice) {
466 toDevice->mapPoints(&a, 1);
467 toDevice->mapPoints(&b, 1);
468 toDevice->mapPoints(&c, 1);
469 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000470 // make a new poly where we replace a and c by a 1-pixel wide edges orthog
471 // to edges ab and bc:
472 //
473 // before | after
474 // | b0
475 // b |
476 // |
477 // | a0 c0
478 // a c | a1 c1
479 //
480 // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c,
481 // respectively.
482 Vertex& a0 = verts[0];
483 Vertex& a1 = verts[1];
484 Vertex& b0 = verts[2];
485 Vertex& c0 = verts[3];
486 Vertex& c1 = verts[4];
487
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000488 SkVector ab = b;
489 ab -= a;
490 SkVector ac = c;
491 ac -= a;
492 SkVector cb = b;
493 cb -= c;
494
495 // We should have already handled degenerates
496 GrAssert(ab.length() > 0 && cb.length() > 0);
497
498 ab.normalize();
499 SkVector abN;
500 abN.setOrthog(ab, SkVector::kLeft_Side);
501 if (abN.dot(ac) > 0) {
502 abN.negate();
503 }
504
505 cb.normalize();
506 SkVector cbN;
507 cbN.setOrthog(cb, SkVector::kLeft_Side);
508 if (cbN.dot(ac) < 0) {
509 cbN.negate();
510 }
511
512 a0.fPos = a;
513 a0.fPos += abN;
514 a1.fPos = a;
515 a1.fPos -= abN;
516
517 c0.fPos = c;
518 c0.fPos += cbN;
519 c1.fPos = c;
520 c1.fPos -= cbN;
521
522 intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos);
523
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000524 if (toSrc) {
525 toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(Vertex), kVertsPerQuad);
526 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000527 DevToUV.mapPointsWithStride(&verts[0].fQuadCoord,
528 &verts[0].fPos, sizeof(Vertex), kVertsPerQuad);
529}
530
531void add_quads(const SkPoint p[3],
532 int subdiv,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000533 const GrMatrix* toDevice,
534 const GrMatrix* toSrc,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000535 Vertex** vert) {
536 GrAssert(subdiv >= 0);
537 if (subdiv) {
538 SkPoint newP[5];
539 SkChopQuadAtHalf(p, newP);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000540 add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert);
541 add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000542 } else {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000543 bloat_quad(p, toDevice, toSrc, *vert);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000544 *vert += kVertsPerQuad;
545 }
546}
547
548void add_line(const SkPoint p[2],
549 int rtHeight,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000550 const SkMatrix* toSrc,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000551 Vertex** vert) {
552 const SkPoint& a = p[0];
553 const SkPoint& b = p[1];
554
555 SkVector orthVec = b;
556 orthVec -= a;
557
558 if (orthVec.setLength(SK_Scalar1)) {
559 orthVec.setOrthog(orthVec);
560
561 // the values we pass down to the frag shader
562 // have to be in y-points-up space;
563 SkVector normal;
564 normal.fX = orthVec.fX;
565 normal.fY = -orthVec.fY;
566 SkPoint aYDown;
567 aYDown.fX = a.fX;
568 aYDown.fY = rtHeight - a.fY;
569
570 SkScalar lineC = -(aYDown.dot(normal));
571 for (int i = 0; i < kVertsPerLineSeg; ++i) {
572 (*vert)[i].fPos = (i < 2) ? a : b;
573 if (0 == i || 3 == i) {
574 (*vert)[i].fPos -= orthVec;
575 } else {
576 (*vert)[i].fPos += orthVec;
577 }
578 (*vert)[i].fLine.fA = normal.fX;
579 (*vert)[i].fLine.fB = normal.fY;
580 (*vert)[i].fLine.fC = lineC;
581 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000582 if (NULL != toSrc) {
583 toSrc->mapPointsWithStride(&(*vert)->fPos,
584 sizeof(Vertex),
585 kVertsPerLineSeg);
586 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000587 } else {
588 // just make it degenerate and likely offscreen
589 (*vert)[0].fPos.set(SK_ScalarMax, SK_ScalarMax);
590 (*vert)[1].fPos.set(SK_ScalarMax, SK_ScalarMax);
591 (*vert)[2].fPos.set(SK_ScalarMax, SK_ScalarMax);
592 (*vert)[3].fPos.set(SK_ScalarMax, SK_ScalarMax);
593 }
594
595 *vert += kVertsPerLineSeg;
596}
597
598}
599
600bool GrAAHairLinePathRenderer::createGeom(GrDrawTarget::StageBitfield stages) {
601
602 int rtHeight = fTarget->getRenderTarget()->height();
603
604 GrIRect clip;
605 if (fTarget->getClip().hasConservativeBounds()) {
606 GrRect clipRect = fTarget->getClip().getConservativeBounds();
607 clipRect.roundOut(&clip);
608 } else {
609 clip.setLargest();
610 }
611
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000612 // If none of the inputs that affect generation of path geometry have
613 // have changed since last previous path draw then we can reuse the
614 // previous geoemtry.
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000615 if (stages == fPreviousStages &&
616 fPreviousViewMatrix == fTarget->getViewMatrix() &&
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000617 fPreviousTranslate == fTranslate &&
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000618 rtHeight == fPreviousRTHeight &&
619 fClipRect == clip) {
620 return true;
621 }
622
623 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
624 for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
625 if ((1 << s) & stages) {
626 layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s);
627 }
628 }
629
630 GrMatrix viewM = fTarget->getViewMatrix();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000631
bsalomon@google.com49313f62011-09-14 13:54:05 +0000632 SkAlignedSTStorage<128, GrPoint> lineStorage;
633 SkAlignedSTStorage<128, GrPoint> quadStorage;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000634 PtArray lines(&lineStorage);
635 PtArray quads(&quadStorage);
636 IntArray qSubdivs;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000637 fQuadCnt = generate_lines_and_quads(*fPath, viewM, fTranslate, clip,
638 &lines, &quads, &qSubdivs);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000639
640 fLineSegmentCnt = lines.count() / 2;
641 int vertCnt = kVertsPerLineSeg * fLineSegmentCnt + kVertsPerQuad * fQuadCnt;
642
643 GrAssert(sizeof(Vertex) == GrDrawTarget::VertexSize(layout));
644
645 Vertex* verts;
646 if (!fTarget->reserveVertexSpace(layout, vertCnt, (void**)&verts)) {
647 return false;
648 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000649 Vertex* base = verts;
650
651 const GrMatrix* toDevice = NULL;
652 const GrMatrix* toSrc = NULL;
653 GrMatrix ivm;
654
655 if (viewM.hasPerspective()) {
656 if (viewM.invert(&ivm)) {
657 toDevice = &viewM;
658 toSrc = &ivm;
659 }
660 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000661
662 for (int i = 0; i < fLineSegmentCnt; ++i) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000663 add_line(&lines[2*i], rtHeight, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000664 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000665
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000666 int unsubdivQuadCnt = quads.count() / 3;
667 for (int i = 0; i < unsubdivQuadCnt; ++i) {
668 GrAssert(qSubdivs[i] >= 0);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000669 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000670 }
671
672 fPreviousStages = stages;
673 fPreviousViewMatrix = fTarget->getViewMatrix();
674 fPreviousRTHeight = rtHeight;
675 fClipRect = clip;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000676 fPreviousTranslate = fTranslate;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000677 return true;
678}
679
680void GrAAHairLinePathRenderer::drawPath(GrDrawTarget::StageBitfield stages) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000681
682 if (!this->createGeom(stages)) {
683 return;
684 }
685
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000686 GrDrawTarget::AutoStateRestore asr;
687 if (!fTarget->getViewMatrix().hasPerspective()) {
688 asr.set(fTarget);
689 GrMatrix ivm;
690 if (fTarget->getViewInverse(&ivm)) {
691 fTarget->preConcatSamplerMatrices(stages, ivm);
692 }
693 fTarget->setViewMatrix(GrMatrix::I());
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000694 }
695
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000696 // TODO: See whether rendering lines as degenerate quads improves perf
697 // when we have a mix
698 fTarget->setIndexSourceToBuffer(fLinesIndexBuffer);
699 int lines = 0;
700 int nBufLines = fLinesIndexBuffer->maxQuads();
701 while (lines < fLineSegmentCnt) {
702 int n = GrMin(fLineSegmentCnt-lines, nBufLines);
703 fTarget->setVertexEdgeType(GrDrawTarget::kHairLine_EdgeType);
704 fTarget->drawIndexed(kTriangles_PrimitiveType,
705 kVertsPerLineSeg*lines, // startV
706 0, // startI
707 kVertsPerLineSeg*n, // vCount
708 kIdxsPerLineSeg*n); // iCount
709 lines += n;
710 }
711
712 fTarget->setIndexSourceToBuffer(fQuadsIndexBuffer);
713 int quads = 0;
714 while (quads < fQuadCnt) {
715 int n = GrMin(fQuadCnt-quads, kNumQuadsInIdxBuffer);
716 fTarget->setVertexEdgeType(GrDrawTarget::kHairQuad_EdgeType);
717 fTarget->drawIndexed(kTriangles_PrimitiveType,
718 4*fLineSegmentCnt + kVertsPerQuad*quads, // startV
719 0, // startI
720 kVertsPerQuad*n, // vCount
721 kIdxsPerQuad*n); // iCount
722 quads += n;
723 }
724
725}
726