blob: 6c136e222cadd8c7e5bd8f663eae3971465a3640 [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
bsalomon@google.com0d2aa842011-09-26 15:59:20 +0000101bool GrAAHairLinePathRenderer::supportsAA(const GrDrawTarget* target,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000102 const SkPath& path,
bsalomon@google.com0d2aa842011-09-26 15:59:20 +0000103 GrPathFill fill) const {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000104 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;
bsalomon@google.com92669012011-09-27 19:10:05 +0000136#define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true>
bsalomon@google.com49313f62011-09-14 13:54:05 +0000137typedef SkTArray<int, true> IntArray;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000138
139/**
140 * We convert cubics to quadratics (for now).
141 */
142void convert_noninflect_cubic_to_quads(const SkPoint p[4],
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000143 SkScalar tolScale,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000144 PtArray* quads,
145 int sublevel = 0) {
146 SkVector ab = p[1];
147 ab -= p[0];
148 SkVector dc = p[2];
149 dc -= p[3];
150
151 static const SkScalar gLengthScale = 3 * SK_Scalar1 / 2;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000152 // base tolerance is 2 pixels in dev coords.
153 const SkScalar distanceSqdTol = SkScalarMul(tolScale, 2 * SK_Scalar1);
154 static const int kMaxSubdivs = 10;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000155
156 ab.scale(gLengthScale);
157 dc.scale(gLengthScale);
158
159 SkVector c0 = p[0];
160 c0 += ab;
161 SkVector c1 = p[3];
162 c1 += dc;
163
164 SkScalar dSqd = c0.distanceToSqd(c1);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000165 if (sublevel > kMaxSubdivs || dSqd <= distanceSqdTol) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000166 SkPoint cAvg = c0;
167 cAvg += c1;
168 cAvg.scale(SK_ScalarHalf);
169
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000170 SkPoint* pts = quads->push_back_n(3);
171 pts[0] = p[0];
172 pts[1] = cAvg;
173 pts[2] = p[3];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000174
175 return;
176 } else {
177 SkPoint choppedPts[7];
178 SkChopCubicAtHalf(p, choppedPts);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000179 convert_noninflect_cubic_to_quads(choppedPts + 0, tolScale,
180 quads, sublevel + 1);
181 convert_noninflect_cubic_to_quads(choppedPts + 3, tolScale,
182 quads, sublevel + 1);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000183 }
184}
185
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000186void convert_cubic_to_quads(const SkPoint p[4],
187 SkScalar tolScale,
188 PtArray* quads) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000189 SkPoint chopped[13];
190 int count = SkChopCubicAtInflections(p, chopped);
191
192 for (int i = 0; i < count; ++i) {
193 SkPoint* cubic = chopped + 3*i;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000194 convert_noninflect_cubic_to_quads(cubic, tolScale, quads);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000195 }
196}
197
198// Takes 178th time of logf on Z600 / VC2010
199int get_float_exp(float x) {
200 GR_STATIC_ASSERT(sizeof(int) == sizeof(float));
201#if GR_DEBUG
202 static bool tested;
203 if (!tested) {
204 tested = true;
205 GrAssert(get_float_exp(0.25f) == -2);
206 GrAssert(get_float_exp(0.3f) == -2);
207 GrAssert(get_float_exp(0.5f) == -1);
208 GrAssert(get_float_exp(1.f) == 0);
209 GrAssert(get_float_exp(2.f) == 1);
210 GrAssert(get_float_exp(2.5f) == 1);
211 GrAssert(get_float_exp(8.f) == 3);
212 GrAssert(get_float_exp(100.f) == 6);
213 GrAssert(get_float_exp(1000.f) == 9);
214 GrAssert(get_float_exp(1024.f) == 10);
215 GrAssert(get_float_exp(3000000.f) == 21);
216 }
217#endif
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000218 const int* iptr = (const int*)&x;
219 return (((*iptr) & 0x7f800000) >> 23) - 127;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000220}
221
222// we subdivide the quads to avoid huge overfill
223// if it returns -1 then should be drawn as lines
224int num_quad_subdivs(const SkPoint p[3]) {
225 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000226 static const SkScalar gDegenerateToLineTolSqd =
227 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000228
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000229 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
230 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000231 return -1;
232 }
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000233
234 GrScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
235 if (dsqd < gDegenerateToLineTolSqd) {
236 return -1;
237 }
238
239 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000240 return -1;
241 }
242
243 static const int kMaxSub = 4;
244 // tolerance of triangle height in pixels
245 // tuned on windows Quadro FX 380 / Z600
246 // trade off of fill vs cpu time on verts
247 // maybe different when do this using gpu (geo or tess shaders)
248 static const SkScalar gSubdivTol = 175 * SK_Scalar1;
249
250 if (dsqd <= gSubdivTol*gSubdivTol) {
251 return 0;
252 } else {
253 // subdividing the quad reduces d by 4. so we want x = log4(d/tol)
254 // = log4(d*d/tol*tol)/2
255 // = log2(d*d/tol*tol)
256
257#ifdef SK_SCALAR_IS_FLOAT
258 // +1 since we're ignoring the mantissa contribution.
259 int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
260 log = GrMin(GrMax(0, log), kMaxSub);
261 return log;
262#else
bsalomon@google.comfcb0dbc2011-08-30 18:35:18 +0000263 SkScalar log = SkScalarLog(SkScalarDiv(dsqd,gSubdivTol*gSubdivTol));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000264 static const SkScalar conv = SkScalarInvert(SkScalarLog(2));
265 log = SkScalarMul(log, conv);
266 return GrMin(GrMax(0, SkScalarCeilToInt(log)),kMaxSub);
267#endif
268 }
269}
270
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000271/**
272 * Generates the lines and quads to be rendered. Lines are always recorded in
273 * device space. We will do a device space bloat to account for the 1pixel
274 * thickness.
275 * Quads are recorded in device space unless m contains
276 * perspective, then in they are in src space. We do this because we will
277 * subdivide large quads to reduce over-fill. This subdivision has to be
278 * performed before applying the perspective matrix.
279 */
280int generate_lines_and_quads(const SkPath& path,
281 const SkMatrix& m,
282 const SkVector& translate,
283 GrIRect clip,
284 PtArray* lines,
285 PtArray* quads,
286 IntArray* quadSubdivCnts) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000287 SkPath::Iter iter(path, false);
288
289 int totalQuadCount = 0;
290 GrRect bounds;
291 GrIRect ibounds;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000292
293 bool persp = m.hasPerspective();
294
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000295 for (;;) {
296 GrPoint pts[4];
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000297 GrPoint devPts[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000298 GrPathCmd cmd = (GrPathCmd)iter.next(pts);
299 switch (cmd) {
300 case kMove_PathCmd:
301 break;
302 case kLine_PathCmd:
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000303 SkPoint::Offset(pts, 2, translate);
304 m.mapPoints(devPts, pts, 2);
305 bounds.setBounds(devPts, 2);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000306 bounds.outset(SK_Scalar1, SK_Scalar1);
307 bounds.roundOut(&ibounds);
308 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000309 SkPoint* pts = lines->push_back_n(2);
310 pts[0] = devPts[0];
311 pts[1] = devPts[1];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000312 }
313 break;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000314 case kQuadratic_PathCmd:
315 SkPoint::Offset(pts, 3, translate);
316 m.mapPoints(devPts, pts, 3);
317 bounds.setBounds(devPts, 3);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000318 bounds.outset(SK_Scalar1, SK_Scalar1);
319 bounds.roundOut(&ibounds);
320 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000321 int subdiv = num_quad_subdivs(devPts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000322 GrAssert(subdiv >= -1);
323 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000324 SkPoint* pts = lines->push_back_n(4);
325 pts[0] = devPts[0];
326 pts[1] = devPts[1];
327 pts[2] = devPts[1];
328 pts[3] = devPts[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000329 } else {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000330 // when in perspective keep quads in src space
331 SkPoint* qPts = persp ? pts : devPts;
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000332 SkPoint* pts = quads->push_back_n(3);
333 pts[0] = qPts[0];
334 pts[1] = qPts[1];
335 pts[2] = qPts[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000336 quadSubdivCnts->push_back() = subdiv;
337 totalQuadCount += 1 << subdiv;
338 }
339 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000340 break;
341 case kCubic_PathCmd:
342 SkPoint::Offset(pts, 4, translate);
343 m.mapPoints(devPts, pts, 4);
344 bounds.setBounds(devPts, 4);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000345 bounds.outset(SK_Scalar1, SK_Scalar1);
346 bounds.roundOut(&ibounds);
347 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000348 PREALLOC_PTARRAY(32) q;
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.com92669012011-09-27 19:10:05 +0000632 PREALLOC_PTARRAY(128) lines;
633 PREALLOC_PTARRAY(128) quads;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000634 IntArray qSubdivs;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000635 fQuadCnt = generate_lines_and_quads(*fPath, viewM, fTranslate, clip,
636 &lines, &quads, &qSubdivs);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000637
638 fLineSegmentCnt = lines.count() / 2;
639 int vertCnt = kVertsPerLineSeg * fLineSegmentCnt + kVertsPerQuad * fQuadCnt;
640
641 GrAssert(sizeof(Vertex) == GrDrawTarget::VertexSize(layout));
642
643 Vertex* verts;
644 if (!fTarget->reserveVertexSpace(layout, vertCnt, (void**)&verts)) {
645 return false;
646 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000647 Vertex* base = verts;
648
649 const GrMatrix* toDevice = NULL;
650 const GrMatrix* toSrc = NULL;
651 GrMatrix ivm;
652
653 if (viewM.hasPerspective()) {
654 if (viewM.invert(&ivm)) {
655 toDevice = &viewM;
656 toSrc = &ivm;
657 }
658 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000659
660 for (int i = 0; i < fLineSegmentCnt; ++i) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000661 add_line(&lines[2*i], rtHeight, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000662 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000663
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000664 int unsubdivQuadCnt = quads.count() / 3;
665 for (int i = 0; i < unsubdivQuadCnt; ++i) {
666 GrAssert(qSubdivs[i] >= 0);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000667 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000668 }
669
670 fPreviousStages = stages;
671 fPreviousViewMatrix = fTarget->getViewMatrix();
672 fPreviousRTHeight = rtHeight;
673 fClipRect = clip;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000674 fPreviousTranslate = fTranslate;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000675 return true;
676}
677
678void GrAAHairLinePathRenderer::drawPath(GrDrawTarget::StageBitfield stages) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000679
680 if (!this->createGeom(stages)) {
681 return;
682 }
683
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000684 GrDrawTarget::AutoStateRestore asr;
685 if (!fTarget->getViewMatrix().hasPerspective()) {
686 asr.set(fTarget);
687 GrMatrix ivm;
688 if (fTarget->getViewInverse(&ivm)) {
689 fTarget->preConcatSamplerMatrices(stages, ivm);
690 }
691 fTarget->setViewMatrix(GrMatrix::I());
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000692 }
693
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000694 // TODO: See whether rendering lines as degenerate quads improves perf
695 // when we have a mix
696 fTarget->setIndexSourceToBuffer(fLinesIndexBuffer);
697 int lines = 0;
698 int nBufLines = fLinesIndexBuffer->maxQuads();
699 while (lines < fLineSegmentCnt) {
700 int n = GrMin(fLineSegmentCnt-lines, nBufLines);
701 fTarget->setVertexEdgeType(GrDrawTarget::kHairLine_EdgeType);
702 fTarget->drawIndexed(kTriangles_PrimitiveType,
703 kVertsPerLineSeg*lines, // startV
704 0, // startI
705 kVertsPerLineSeg*n, // vCount
706 kIdxsPerLineSeg*n); // iCount
707 lines += n;
708 }
709
710 fTarget->setIndexSourceToBuffer(fQuadsIndexBuffer);
711 int quads = 0;
712 while (quads < fQuadCnt) {
713 int n = GrMin(fQuadCnt-quads, kNumQuadsInIdxBuffer);
714 fTarget->setVertexEdgeType(GrDrawTarget::kHairQuad_EdgeType);
715 fTarget->drawIndexed(kTriangles_PrimitiveType,
716 4*fLineSegmentCnt + kVertsPerQuad*quads, // startV
717 0, // startI
718 kVertsPerQuad*n, // vCount
719 kIdxsPerQuad*n); // iCount
720 quads += n;
721 }
722
723}
724