blob: eafe9ebdc247c0450a6e3765c1b6e439a3063336 [file] [log] [blame]
bsalomon@google.comf75b84e2011-09-29 14:58:28 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
bsalomon@google.comaeb21602011-08-30 18:13:44 +00009#include "GrAAHairLinePathRenderer.h"
10
11#include "GrContext.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000012#include "GrDrawState.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000013#include "GrGpu.h"
14#include "GrIndexBuffer.h"
bsalomon@google.comdbeeac32011-09-12 14:59:34 +000015#include "GrPathUtils.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000016#include "SkGeometry.h"
17#include "SkTemplates.h"
18
19namespace {
20// quadratics are rendered as 5-sided polys in order to bound the
21// AA stroke around the center-curve. See comments in push_quad_index_buffer and
22// bloat_quad.
23static const int kVertsPerQuad = 5;
24static const int kIdxsPerQuad = 9;
25
26static const int kVertsPerLineSeg = 4;
27static const int kIdxsPerLineSeg = 6;
28
29static const int kNumQuadsInIdxBuffer = 256;
30static const size_t kQuadIdxSBufize = kIdxsPerQuad *
31 sizeof(uint16_t) *
32 kNumQuadsInIdxBuffer;
33
34bool push_quad_index_data(GrIndexBuffer* qIdxBuffer) {
35 uint16_t* data = (uint16_t*) qIdxBuffer->lock();
36 bool tempData = NULL == data;
37 if (tempData) {
38 data = new uint16_t[kNumQuadsInIdxBuffer * kIdxsPerQuad];
39 }
40 for (int i = 0; i < kNumQuadsInIdxBuffer; ++i) {
41
42 // Each quadratic is rendered as a five sided polygon. This poly bounds
43 // the quadratic's bounding triangle but has been expanded so that the
44 // 1-pixel wide area around the curve is inside the poly.
45 // If a,b,c are the original control points then the poly a0,b0,c0,c1,a1
46 // that is rendered would look like this:
47 // b0
48 // b
49 //
50 // a0 c0
51 // a c
52 // a1 c1
53 // Each is drawn as three triagnles specified by these 9 indices:
54 int baseIdx = i * kIdxsPerQuad;
55 uint16_t baseVert = (uint16_t)(i * kVertsPerQuad);
56 data[0 + baseIdx] = baseVert + 0; // a0
57 data[1 + baseIdx] = baseVert + 1; // a1
58 data[2 + baseIdx] = baseVert + 2; // b0
59 data[3 + baseIdx] = baseVert + 2; // b0
60 data[4 + baseIdx] = baseVert + 4; // c1
61 data[5 + baseIdx] = baseVert + 3; // c0
62 data[6 + baseIdx] = baseVert + 1; // a1
63 data[7 + baseIdx] = baseVert + 4; // c1
64 data[8 + baseIdx] = baseVert + 2; // b0
65 }
66 if (tempData) {
67 bool ret = qIdxBuffer->updateData(data, kQuadIdxSBufize);
68 delete[] data;
69 return ret;
70 } else {
71 qIdxBuffer->unlock();
72 return true;
73 }
74}
75}
76
77GrPathRenderer* GrAAHairLinePathRenderer::Create(GrContext* context) {
bsalomon@google.coma8a6a322011-09-23 14:19:58 +000078 const GrIndexBuffer* lIdxBuffer = context->getQuadIndexBuffer();
79 if (NULL == lIdxBuffer) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +000080 return NULL;
81 }
bsalomon@google.coma8a6a322011-09-23 14:19:58 +000082 GrGpu* gpu = context->getGpu();
83 GrIndexBuffer* qIdxBuf = gpu->createIndexBuffer(kQuadIdxSBufize, false);
84 SkAutoTUnref<GrIndexBuffer> qIdxBuffer(qIdxBuf);
85 if (NULL == qIdxBuf ||
86 !push_quad_index_data(qIdxBuf)) {
87 return NULL;
88 }
89 return new GrAAHairLinePathRenderer(context,
90 lIdxBuffer,
91 qIdxBuf);
bsalomon@google.comaeb21602011-08-30 18:13:44 +000092}
93
94GrAAHairLinePathRenderer::GrAAHairLinePathRenderer(
95 const GrContext* context,
96 const GrIndexBuffer* linesIndexBuffer,
97 const GrIndexBuffer* quadsIndexBuffer) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +000098 fLinesIndexBuffer = linesIndexBuffer;
99 linesIndexBuffer->ref();
100 fQuadsIndexBuffer = quadsIndexBuffer;
101 quadsIndexBuffer->ref();
102 this->resetGeom();
103}
104
105GrAAHairLinePathRenderer::~GrAAHairLinePathRenderer() {
106 fLinesIndexBuffer->unref();
107 fQuadsIndexBuffer->unref();
108}
109
bsalomon@google.com289533a2011-10-27 12:34:25 +0000110bool GrAAHairLinePathRenderer::canDrawPath(const GrDrawTarget::Caps& targetCaps,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000111 const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +0000112 GrPathFill fill,
113 bool antiAlias) const {
bsalomon@google.coma8a6a322011-09-23 14:19:58 +0000114 static const uint32_t gReqDerivMask = SkPath::kCubic_SegmentMask |
115 SkPath::kQuad_SegmentMask;
116 return (kHairLine_PathFill == fill &&
bsalomon@google.com289533a2011-10-27 12:34:25 +0000117 antiAlias &&
118 (targetCaps.fShaderDerivativeSupport ||
bsalomon@google.coma8a6a322011-09-23 14:19:58 +0000119 !(gReqDerivMask & path.getSegmentMasks())));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000120}
121
122void GrAAHairLinePathRenderer::pathWillClear() {
123 this->resetGeom();
124}
125
126void GrAAHairLinePathRenderer::resetGeom() {
127 fPreviousStages = ~0;
128 fPreviousRTHeight = ~0;
129 fPreviousViewMatrix = GrMatrix::InvalidMatrix();
130 fLineSegmentCnt = 0;
131 fQuadCnt = 0;
132 if ((fQuadCnt || fLineSegmentCnt) && NULL != fTarget) {
133 fTarget->resetVertexSource();
134 }
135}
136
137namespace {
138
bsalomon@google.com49313f62011-09-14 13:54:05 +0000139typedef SkTArray<SkPoint, true> PtArray;
bsalomon@google.com92669012011-09-27 19:10:05 +0000140#define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true>
bsalomon@google.com49313f62011-09-14 13:54:05 +0000141typedef 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.com92669012011-09-27 19:10:05 +0000352 PREALLOC_PTARRAY(32) q;
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;
tomhudson@google.com93813632011-10-27 20:21:16 +0000628 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000629 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
bsalomon@google.com92669012011-09-27 19:10:05 +0000636 PREALLOC_PTARRAY(128) lines;
637 PREALLOC_PTARRAY(128) quads;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000638 IntArray qSubdivs;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000639 fQuadCnt = generate_lines_and_quads(*fPath, viewM, fTranslate, clip,
640 &lines, &quads, &qSubdivs);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000641
642 fLineSegmentCnt = lines.count() / 2;
643 int vertCnt = kVertsPerLineSeg * fLineSegmentCnt + kVertsPerQuad * fQuadCnt;
644
645 GrAssert(sizeof(Vertex) == GrDrawTarget::VertexSize(layout));
646
647 Vertex* verts;
648 if (!fTarget->reserveVertexSpace(layout, vertCnt, (void**)&verts)) {
649 return false;
650 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000651 Vertex* base = verts;
652
653 const GrMatrix* toDevice = NULL;
654 const GrMatrix* toSrc = NULL;
655 GrMatrix ivm;
656
657 if (viewM.hasPerspective()) {
658 if (viewM.invert(&ivm)) {
659 toDevice = &viewM;
660 toSrc = &ivm;
661 }
662 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000663
664 for (int i = 0; i < fLineSegmentCnt; ++i) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000665 add_line(&lines[2*i], rtHeight, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000666 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000667
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000668 int unsubdivQuadCnt = quads.count() / 3;
669 for (int i = 0; i < unsubdivQuadCnt; ++i) {
670 GrAssert(qSubdivs[i] >= 0);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000671 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000672 }
673
674 fPreviousStages = stages;
675 fPreviousViewMatrix = fTarget->getViewMatrix();
676 fPreviousRTHeight = rtHeight;
677 fClipRect = clip;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000678 fPreviousTranslate = fTranslate;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000679 return true;
680}
681
682void GrAAHairLinePathRenderer::drawPath(GrDrawTarget::StageBitfield stages) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000683
684 if (!this->createGeom(stages)) {
685 return;
686 }
687
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000688 GrDrawTarget::AutoStateRestore asr;
689 if (!fTarget->getViewMatrix().hasPerspective()) {
690 asr.set(fTarget);
691 GrMatrix ivm;
692 if (fTarget->getViewInverse(&ivm)) {
693 fTarget->preConcatSamplerMatrices(stages, ivm);
694 }
695 fTarget->setViewMatrix(GrMatrix::I());
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000696 }
697
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000698 // TODO: See whether rendering lines as degenerate quads improves perf
699 // when we have a mix
700 fTarget->setIndexSourceToBuffer(fLinesIndexBuffer);
701 int lines = 0;
702 int nBufLines = fLinesIndexBuffer->maxQuads();
703 while (lines < fLineSegmentCnt) {
704 int n = GrMin(fLineSegmentCnt-lines, nBufLines);
tomhudson@google.com93813632011-10-27 20:21:16 +0000705 fTarget->setVertexEdgeType(GrDrawState::kHairLine_EdgeType);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000706 fTarget->drawIndexed(kTriangles_PrimitiveType,
707 kVertsPerLineSeg*lines, // startV
708 0, // startI
709 kVertsPerLineSeg*n, // vCount
710 kIdxsPerLineSeg*n); // iCount
711 lines += n;
712 }
713
714 fTarget->setIndexSourceToBuffer(fQuadsIndexBuffer);
715 int quads = 0;
716 while (quads < fQuadCnt) {
717 int n = GrMin(fQuadCnt-quads, kNumQuadsInIdxBuffer);
tomhudson@google.com93813632011-10-27 20:21:16 +0000718 fTarget->setVertexEdgeType(GrDrawState::kHairQuad_EdgeType);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000719 fTarget->drawIndexed(kTriangles_PrimitiveType,
720 4*fLineSegmentCnt + kVertsPerQuad*quads, // startV
721 0, // startI
722 kVertsPerQuad*n, // vCount
723 kIdxsPerQuad*n); // iCount
724 quads += n;
725 }
726
727}
728