blob: 35c4f20ccf993f452ff4f3ec7b2c38e35feba0a6 [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"
12#include "GrGpu.h"
13#include "GrIndexBuffer.h"
bsalomon@google.comdbeeac32011-09-12 14:59:34 +000014#include "GrPathUtils.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000015#include "SkGeometry.h"
16#include "SkTemplates.h"
17
18namespace {
19// quadratics are rendered as 5-sided polys in order to bound the
20// AA stroke around the center-curve. See comments in push_quad_index_buffer and
21// bloat_quad.
22static const int kVertsPerQuad = 5;
23static const int kIdxsPerQuad = 9;
24
25static const int kVertsPerLineSeg = 4;
26static const int kIdxsPerLineSeg = 6;
27
28static const int kNumQuadsInIdxBuffer = 256;
29static const size_t kQuadIdxSBufize = kIdxsPerQuad *
30 sizeof(uint16_t) *
31 kNumQuadsInIdxBuffer;
32
33bool push_quad_index_data(GrIndexBuffer* qIdxBuffer) {
34 uint16_t* data = (uint16_t*) qIdxBuffer->lock();
35 bool tempData = NULL == data;
36 if (tempData) {
37 data = new uint16_t[kNumQuadsInIdxBuffer * kIdxsPerQuad];
38 }
39 for (int i = 0; i < kNumQuadsInIdxBuffer; ++i) {
40
41 // Each quadratic is rendered as a five sided polygon. This poly bounds
42 // the quadratic's bounding triangle but has been expanded so that the
43 // 1-pixel wide area around the curve is inside the poly.
44 // If a,b,c are the original control points then the poly a0,b0,c0,c1,a1
45 // that is rendered would look like this:
46 // b0
47 // b
48 //
49 // a0 c0
50 // a c
51 // a1 c1
52 // Each is drawn as three triagnles specified by these 9 indices:
53 int baseIdx = i * kIdxsPerQuad;
54 uint16_t baseVert = (uint16_t)(i * kVertsPerQuad);
55 data[0 + baseIdx] = baseVert + 0; // a0
56 data[1 + baseIdx] = baseVert + 1; // a1
57 data[2 + baseIdx] = baseVert + 2; // b0
58 data[3 + baseIdx] = baseVert + 2; // b0
59 data[4 + baseIdx] = baseVert + 4; // c1
60 data[5 + baseIdx] = baseVert + 3; // c0
61 data[6 + baseIdx] = baseVert + 1; // a1
62 data[7 + baseIdx] = baseVert + 4; // c1
63 data[8 + baseIdx] = baseVert + 2; // b0
64 }
65 if (tempData) {
66 bool ret = qIdxBuffer->updateData(data, kQuadIdxSBufize);
67 delete[] data;
68 return ret;
69 } else {
70 qIdxBuffer->unlock();
71 return true;
72 }
73}
74}
75
76GrPathRenderer* GrAAHairLinePathRenderer::Create(GrContext* context) {
bsalomon@google.coma8a6a322011-09-23 14:19:58 +000077 const GrIndexBuffer* lIdxBuffer = context->getQuadIndexBuffer();
78 if (NULL == lIdxBuffer) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +000079 return NULL;
80 }
bsalomon@google.coma8a6a322011-09-23 14:19:58 +000081 GrGpu* gpu = context->getGpu();
82 GrIndexBuffer* qIdxBuf = gpu->createIndexBuffer(kQuadIdxSBufize, false);
83 SkAutoTUnref<GrIndexBuffer> qIdxBuffer(qIdxBuf);
84 if (NULL == qIdxBuf ||
85 !push_quad_index_data(qIdxBuf)) {
86 return NULL;
87 }
88 return new GrAAHairLinePathRenderer(context,
89 lIdxBuffer,
90 qIdxBuf);
bsalomon@google.comaeb21602011-08-30 18:13:44 +000091}
92
93GrAAHairLinePathRenderer::GrAAHairLinePathRenderer(
94 const GrContext* context,
95 const GrIndexBuffer* linesIndexBuffer,
96 const GrIndexBuffer* quadsIndexBuffer) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +000097 fLinesIndexBuffer = linesIndexBuffer;
98 linesIndexBuffer->ref();
99 fQuadsIndexBuffer = quadsIndexBuffer;
100 quadsIndexBuffer->ref();
101 this->resetGeom();
102}
103
104GrAAHairLinePathRenderer::~GrAAHairLinePathRenderer() {
105 fLinesIndexBuffer->unref();
106 fQuadsIndexBuffer->unref();
107}
108
bsalomon@google.com289533a2011-10-27 12:34:25 +0000109bool GrAAHairLinePathRenderer::canDrawPath(const GrDrawTarget::Caps& targetCaps,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000110 const SkPath& path,
bsalomon@google.com289533a2011-10-27 12:34:25 +0000111 GrPathFill fill,
112 bool antiAlias) const {
bsalomon@google.coma8a6a322011-09-23 14:19:58 +0000113 static const uint32_t gReqDerivMask = SkPath::kCubic_SegmentMask |
114 SkPath::kQuad_SegmentMask;
115 return (kHairLine_PathFill == fill &&
bsalomon@google.com289533a2011-10-27 12:34:25 +0000116 antiAlias &&
117 (targetCaps.fShaderDerivativeSupport ||
bsalomon@google.coma8a6a322011-09-23 14:19:58 +0000118 !(gReqDerivMask & path.getSegmentMasks())));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000119}
120
121void GrAAHairLinePathRenderer::pathWillClear() {
122 this->resetGeom();
123}
124
125void GrAAHairLinePathRenderer::resetGeom() {
126 fPreviousStages = ~0;
127 fPreviousRTHeight = ~0;
128 fPreviousViewMatrix = GrMatrix::InvalidMatrix();
129 fLineSegmentCnt = 0;
130 fQuadCnt = 0;
131 if ((fQuadCnt || fLineSegmentCnt) && NULL != fTarget) {
132 fTarget->resetVertexSource();
133 }
134}
135
136namespace {
137
bsalomon@google.com49313f62011-09-14 13:54:05 +0000138typedef SkTArray<SkPoint, true> PtArray;
bsalomon@google.com92669012011-09-27 19:10:05 +0000139#define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true>
bsalomon@google.com49313f62011-09-14 13:54:05 +0000140typedef SkTArray<int, true> IntArray;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000141
142/**
143 * We convert cubics to quadratics (for now).
144 */
145void convert_noninflect_cubic_to_quads(const SkPoint p[4],
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000146 SkScalar tolScale,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000147 PtArray* quads,
148 int sublevel = 0) {
149 SkVector ab = p[1];
150 ab -= p[0];
151 SkVector dc = p[2];
152 dc -= p[3];
153
154 static const SkScalar gLengthScale = 3 * SK_Scalar1 / 2;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000155 // base tolerance is 2 pixels in dev coords.
156 const SkScalar distanceSqdTol = SkScalarMul(tolScale, 2 * SK_Scalar1);
157 static const int kMaxSubdivs = 10;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000158
159 ab.scale(gLengthScale);
160 dc.scale(gLengthScale);
161
162 SkVector c0 = p[0];
163 c0 += ab;
164 SkVector c1 = p[3];
165 c1 += dc;
166
167 SkScalar dSqd = c0.distanceToSqd(c1);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000168 if (sublevel > kMaxSubdivs || dSqd <= distanceSqdTol) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000169 SkPoint cAvg = c0;
170 cAvg += c1;
171 cAvg.scale(SK_ScalarHalf);
172
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000173 SkPoint* pts = quads->push_back_n(3);
174 pts[0] = p[0];
175 pts[1] = cAvg;
176 pts[2] = p[3];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000177
178 return;
179 } else {
180 SkPoint choppedPts[7];
181 SkChopCubicAtHalf(p, choppedPts);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000182 convert_noninflect_cubic_to_quads(choppedPts + 0, tolScale,
183 quads, sublevel + 1);
184 convert_noninflect_cubic_to_quads(choppedPts + 3, tolScale,
185 quads, sublevel + 1);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000186 }
187}
188
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000189void convert_cubic_to_quads(const SkPoint p[4],
190 SkScalar tolScale,
191 PtArray* quads) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000192 SkPoint chopped[13];
193 int count = SkChopCubicAtInflections(p, chopped);
194
195 for (int i = 0; i < count; ++i) {
196 SkPoint* cubic = chopped + 3*i;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000197 convert_noninflect_cubic_to_quads(cubic, tolScale, quads);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000198 }
199}
200
201// Takes 178th time of logf on Z600 / VC2010
202int get_float_exp(float x) {
203 GR_STATIC_ASSERT(sizeof(int) == sizeof(float));
204#if GR_DEBUG
205 static bool tested;
206 if (!tested) {
207 tested = true;
208 GrAssert(get_float_exp(0.25f) == -2);
209 GrAssert(get_float_exp(0.3f) == -2);
210 GrAssert(get_float_exp(0.5f) == -1);
211 GrAssert(get_float_exp(1.f) == 0);
212 GrAssert(get_float_exp(2.f) == 1);
213 GrAssert(get_float_exp(2.5f) == 1);
214 GrAssert(get_float_exp(8.f) == 3);
215 GrAssert(get_float_exp(100.f) == 6);
216 GrAssert(get_float_exp(1000.f) == 9);
217 GrAssert(get_float_exp(1024.f) == 10);
218 GrAssert(get_float_exp(3000000.f) == 21);
219 }
220#endif
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000221 const int* iptr = (const int*)&x;
222 return (((*iptr) & 0x7f800000) >> 23) - 127;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000223}
224
225// we subdivide the quads to avoid huge overfill
226// if it returns -1 then should be drawn as lines
227int num_quad_subdivs(const SkPoint p[3]) {
228 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000229 static const SkScalar gDegenerateToLineTolSqd =
230 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000231
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000232 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
233 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000234 return -1;
235 }
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000236
237 GrScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
238 if (dsqd < gDegenerateToLineTolSqd) {
239 return -1;
240 }
241
242 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000243 return -1;
244 }
245
246 static const int kMaxSub = 4;
247 // tolerance of triangle height in pixels
248 // tuned on windows Quadro FX 380 / Z600
249 // trade off of fill vs cpu time on verts
250 // maybe different when do this using gpu (geo or tess shaders)
251 static const SkScalar gSubdivTol = 175 * SK_Scalar1;
252
253 if (dsqd <= gSubdivTol*gSubdivTol) {
254 return 0;
255 } else {
256 // subdividing the quad reduces d by 4. so we want x = log4(d/tol)
257 // = log4(d*d/tol*tol)/2
258 // = log2(d*d/tol*tol)
259
260#ifdef SK_SCALAR_IS_FLOAT
261 // +1 since we're ignoring the mantissa contribution.
262 int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
263 log = GrMin(GrMax(0, log), kMaxSub);
264 return log;
265#else
bsalomon@google.comfcb0dbc2011-08-30 18:35:18 +0000266 SkScalar log = SkScalarLog(SkScalarDiv(dsqd,gSubdivTol*gSubdivTol));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000267 static const SkScalar conv = SkScalarInvert(SkScalarLog(2));
268 log = SkScalarMul(log, conv);
269 return GrMin(GrMax(0, SkScalarCeilToInt(log)),kMaxSub);
270#endif
271 }
272}
273
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000274/**
275 * Generates the lines and quads to be rendered. Lines are always recorded in
276 * device space. We will do a device space bloat to account for the 1pixel
277 * thickness.
278 * Quads are recorded in device space unless m contains
279 * perspective, then in they are in src space. We do this because we will
280 * subdivide large quads to reduce over-fill. This subdivision has to be
281 * performed before applying the perspective matrix.
282 */
283int generate_lines_and_quads(const SkPath& path,
284 const SkMatrix& m,
285 const SkVector& translate,
286 GrIRect clip,
287 PtArray* lines,
288 PtArray* quads,
289 IntArray* quadSubdivCnts) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000290 SkPath::Iter iter(path, false);
291
292 int totalQuadCount = 0;
293 GrRect bounds;
294 GrIRect ibounds;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000295
296 bool persp = m.hasPerspective();
297
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000298 for (;;) {
299 GrPoint pts[4];
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000300 GrPoint devPts[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000301 GrPathCmd cmd = (GrPathCmd)iter.next(pts);
302 switch (cmd) {
303 case kMove_PathCmd:
304 break;
305 case kLine_PathCmd:
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000306 SkPoint::Offset(pts, 2, translate);
307 m.mapPoints(devPts, pts, 2);
308 bounds.setBounds(devPts, 2);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000309 bounds.outset(SK_Scalar1, SK_Scalar1);
310 bounds.roundOut(&ibounds);
311 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000312 SkPoint* pts = lines->push_back_n(2);
313 pts[0] = devPts[0];
314 pts[1] = devPts[1];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000315 }
316 break;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000317 case kQuadratic_PathCmd:
318 SkPoint::Offset(pts, 3, translate);
319 m.mapPoints(devPts, pts, 3);
320 bounds.setBounds(devPts, 3);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000321 bounds.outset(SK_Scalar1, SK_Scalar1);
322 bounds.roundOut(&ibounds);
323 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000324 int subdiv = num_quad_subdivs(devPts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000325 GrAssert(subdiv >= -1);
326 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000327 SkPoint* pts = lines->push_back_n(4);
328 pts[0] = devPts[0];
329 pts[1] = devPts[1];
330 pts[2] = devPts[1];
331 pts[3] = devPts[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000332 } else {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000333 // when in perspective keep quads in src space
334 SkPoint* qPts = persp ? pts : devPts;
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000335 SkPoint* pts = quads->push_back_n(3);
336 pts[0] = qPts[0];
337 pts[1] = qPts[1];
338 pts[2] = qPts[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000339 quadSubdivCnts->push_back() = subdiv;
340 totalQuadCount += 1 << subdiv;
341 }
342 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000343 break;
344 case kCubic_PathCmd:
345 SkPoint::Offset(pts, 4, translate);
346 m.mapPoints(devPts, pts, 4);
347 bounds.setBounds(devPts, 4);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000348 bounds.outset(SK_Scalar1, SK_Scalar1);
349 bounds.roundOut(&ibounds);
350 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000351 PREALLOC_PTARRAY(32) q;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000352 // in perspective have to do conversion in src space
353 if (persp) {
354 SkScalar tolScale =
355 GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m,
356 path.getBounds());
357 convert_cubic_to_quads(pts, tolScale, &q);
358 } else {
359 convert_cubic_to_quads(devPts, SK_Scalar1, &q);
360 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000361 for (int i = 0; i < q.count(); i += 3) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000362 SkPoint* qInDevSpace;
363 // bounds has to be calculated in device space, but q is
364 // in src space when there is perspective.
365 if (persp) {
366 m.mapPoints(devPts, &q[i], 3);
367 bounds.setBounds(devPts, 3);
368 qInDevSpace = devPts;
369 } else {
370 bounds.setBounds(&q[i], 3);
371 qInDevSpace = &q[i];
372 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000373 bounds.outset(SK_Scalar1, SK_Scalar1);
374 bounds.roundOut(&ibounds);
375 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000376 int subdiv = num_quad_subdivs(qInDevSpace);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000377 GrAssert(subdiv >= -1);
378 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000379 SkPoint* pts = lines->push_back_n(4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000380 // lines should always be in device coords
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000381 pts[0] = qInDevSpace[0];
382 pts[1] = qInDevSpace[1];
383 pts[2] = qInDevSpace[1];
384 pts[3] = qInDevSpace[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000385 } else {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000386 SkPoint* pts = quads->push_back_n(3);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000387 // q is already in src space when there is no
388 // perspective and dev coords otherwise.
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000389 pts[0] = q[0 + i];
390 pts[1] = q[1 + i];
391 pts[2] = q[2 + i];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000392 quadSubdivCnts->push_back() = subdiv;
393 totalQuadCount += 1 << subdiv;
394 }
395 }
396 }
397 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000398 break;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000399 case kClose_PathCmd:
400 break;
401 case kEnd_PathCmd:
402 return totalQuadCount;
403 }
404 }
405}
406
407struct Vertex {
408 GrPoint fPos;
409 union {
410 struct {
411 GrScalar fA;
412 GrScalar fB;
413 GrScalar fC;
414 } fLine;
415 GrVec fQuadCoord;
416 struct {
417 GrScalar fBogus[4];
418 };
419 };
420};
421GR_STATIC_ASSERT(sizeof(Vertex) == 3 * sizeof(GrPoint));
422
423void intersect_lines(const SkPoint& ptA, const SkVector& normA,
424 const SkPoint& ptB, const SkVector& normB,
425 SkPoint* result) {
426
427 SkScalar lineAW = -normA.dot(ptA);
428 SkScalar lineBW = -normB.dot(ptB);
429
430 SkScalar wInv = SkScalarMul(normA.fX, normB.fY) -
431 SkScalarMul(normA.fY, normB.fX);
432 wInv = SkScalarInvert(wInv);
433
434 result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY);
435 result->fX = SkScalarMul(result->fX, wInv);
436
437 result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW);
438 result->fY = SkScalarMul(result->fY, wInv);
439}
440
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000441void bloat_quad(const SkPoint qpts[3], const GrMatrix* toDevice,
442 const GrMatrix* toSrc, Vertex verts[kVertsPerQuad]) {
443 GrAssert(!toDevice == !toSrc);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000444 // original quad is specified by tri a,b,c
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000445 SkPoint a = qpts[0];
446 SkPoint b = qpts[1];
447 SkPoint c = qpts[2];
448
449 // compute a matrix that goes from device coords to U,V quad params
450 // this should be in the src space, not dev coords, when we have perspective
451 SkMatrix DevToUV;
452 DevToUV.setAll(a.fX, b.fX, c.fX,
453 a.fY, b.fY, c.fY,
454 SK_Scalar1, SK_Scalar1, SK_Scalar1);
455 DevToUV.invert(&DevToUV);
456 // can't make this static, no cons :(
457 SkMatrix UVpts;
458 UVpts.setAll(0, SK_ScalarHalf, SK_Scalar1,
459 0, 0, SK_Scalar1,
460 SK_Scalar1, SK_Scalar1, SK_Scalar1);
461 DevToUV.postConcat(UVpts);
462
463 // We really want to avoid perspective matrix muls.
464 // These may wind up really close to zero
465 DevToUV.setPerspX(0);
466 DevToUV.setPerspY(0);
467
468 if (toDevice) {
469 toDevice->mapPoints(&a, 1);
470 toDevice->mapPoints(&b, 1);
471 toDevice->mapPoints(&c, 1);
472 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000473 // make a new poly where we replace a and c by a 1-pixel wide edges orthog
474 // to edges ab and bc:
475 //
476 // before | after
477 // | b0
478 // b |
479 // |
480 // | a0 c0
481 // a c | a1 c1
482 //
483 // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c,
484 // respectively.
485 Vertex& a0 = verts[0];
486 Vertex& a1 = verts[1];
487 Vertex& b0 = verts[2];
488 Vertex& c0 = verts[3];
489 Vertex& c1 = verts[4];
490
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000491 SkVector ab = b;
492 ab -= a;
493 SkVector ac = c;
494 ac -= a;
495 SkVector cb = b;
496 cb -= c;
497
498 // We should have already handled degenerates
499 GrAssert(ab.length() > 0 && cb.length() > 0);
500
501 ab.normalize();
502 SkVector abN;
503 abN.setOrthog(ab, SkVector::kLeft_Side);
504 if (abN.dot(ac) > 0) {
505 abN.negate();
506 }
507
508 cb.normalize();
509 SkVector cbN;
510 cbN.setOrthog(cb, SkVector::kLeft_Side);
511 if (cbN.dot(ac) < 0) {
512 cbN.negate();
513 }
514
515 a0.fPos = a;
516 a0.fPos += abN;
517 a1.fPos = a;
518 a1.fPos -= abN;
519
520 c0.fPos = c;
521 c0.fPos += cbN;
522 c1.fPos = c;
523 c1.fPos -= cbN;
524
525 intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos);
526
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000527 if (toSrc) {
528 toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(Vertex), kVertsPerQuad);
529 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000530 DevToUV.mapPointsWithStride(&verts[0].fQuadCoord,
531 &verts[0].fPos, sizeof(Vertex), kVertsPerQuad);
532}
533
534void add_quads(const SkPoint p[3],
535 int subdiv,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000536 const GrMatrix* toDevice,
537 const GrMatrix* toSrc,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000538 Vertex** vert) {
539 GrAssert(subdiv >= 0);
540 if (subdiv) {
541 SkPoint newP[5];
542 SkChopQuadAtHalf(p, newP);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000543 add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert);
544 add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000545 } else {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000546 bloat_quad(p, toDevice, toSrc, *vert);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000547 *vert += kVertsPerQuad;
548 }
549}
550
551void add_line(const SkPoint p[2],
552 int rtHeight,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000553 const SkMatrix* toSrc,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000554 Vertex** vert) {
555 const SkPoint& a = p[0];
556 const SkPoint& b = p[1];
557
558 SkVector orthVec = b;
559 orthVec -= a;
560
561 if (orthVec.setLength(SK_Scalar1)) {
562 orthVec.setOrthog(orthVec);
563
564 // the values we pass down to the frag shader
565 // have to be in y-points-up space;
566 SkVector normal;
567 normal.fX = orthVec.fX;
568 normal.fY = -orthVec.fY;
569 SkPoint aYDown;
570 aYDown.fX = a.fX;
571 aYDown.fY = rtHeight - a.fY;
572
573 SkScalar lineC = -(aYDown.dot(normal));
574 for (int i = 0; i < kVertsPerLineSeg; ++i) {
575 (*vert)[i].fPos = (i < 2) ? a : b;
576 if (0 == i || 3 == i) {
577 (*vert)[i].fPos -= orthVec;
578 } else {
579 (*vert)[i].fPos += orthVec;
580 }
581 (*vert)[i].fLine.fA = normal.fX;
582 (*vert)[i].fLine.fB = normal.fY;
583 (*vert)[i].fLine.fC = lineC;
584 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000585 if (NULL != toSrc) {
586 toSrc->mapPointsWithStride(&(*vert)->fPos,
587 sizeof(Vertex),
588 kVertsPerLineSeg);
589 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000590 } else {
591 // just make it degenerate and likely offscreen
592 (*vert)[0].fPos.set(SK_ScalarMax, SK_ScalarMax);
593 (*vert)[1].fPos.set(SK_ScalarMax, SK_ScalarMax);
594 (*vert)[2].fPos.set(SK_ScalarMax, SK_ScalarMax);
595 (*vert)[3].fPos.set(SK_ScalarMax, SK_ScalarMax);
596 }
597
598 *vert += kVertsPerLineSeg;
599}
600
601}
602
603bool GrAAHairLinePathRenderer::createGeom(GrDrawTarget::StageBitfield stages) {
604
605 int rtHeight = fTarget->getRenderTarget()->height();
606
607 GrIRect clip;
608 if (fTarget->getClip().hasConservativeBounds()) {
609 GrRect clipRect = fTarget->getClip().getConservativeBounds();
610 clipRect.roundOut(&clip);
611 } else {
612 clip.setLargest();
613 }
614
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000615 // If none of the inputs that affect generation of path geometry have
616 // have changed since last previous path draw then we can reuse the
617 // previous geoemtry.
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000618 if (stages == fPreviousStages &&
619 fPreviousViewMatrix == fTarget->getViewMatrix() &&
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000620 fPreviousTranslate == fTranslate &&
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000621 rtHeight == fPreviousRTHeight &&
622 fClipRect == clip) {
623 return true;
624 }
625
626 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
627 for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
628 if ((1 << s) & stages) {
629 layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s);
630 }
631 }
632
633 GrMatrix viewM = fTarget->getViewMatrix();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000634
bsalomon@google.com92669012011-09-27 19:10:05 +0000635 PREALLOC_PTARRAY(128) lines;
636 PREALLOC_PTARRAY(128) quads;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000637 IntArray qSubdivs;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000638 fQuadCnt = generate_lines_and_quads(*fPath, viewM, fTranslate, clip,
639 &lines, &quads, &qSubdivs);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000640
641 fLineSegmentCnt = lines.count() / 2;
642 int vertCnt = kVertsPerLineSeg * fLineSegmentCnt + kVertsPerQuad * fQuadCnt;
643
644 GrAssert(sizeof(Vertex) == GrDrawTarget::VertexSize(layout));
645
646 Vertex* verts;
647 if (!fTarget->reserveVertexSpace(layout, vertCnt, (void**)&verts)) {
648 return false;
649 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000650 Vertex* base = verts;
651
652 const GrMatrix* toDevice = NULL;
653 const GrMatrix* toSrc = NULL;
654 GrMatrix ivm;
655
656 if (viewM.hasPerspective()) {
657 if (viewM.invert(&ivm)) {
658 toDevice = &viewM;
659 toSrc = &ivm;
660 }
661 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000662
663 for (int i = 0; i < fLineSegmentCnt; ++i) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000664 add_line(&lines[2*i], rtHeight, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000665 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000666
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000667 int unsubdivQuadCnt = quads.count() / 3;
668 for (int i = 0; i < unsubdivQuadCnt; ++i) {
669 GrAssert(qSubdivs[i] >= 0);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000670 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000671 }
672
673 fPreviousStages = stages;
674 fPreviousViewMatrix = fTarget->getViewMatrix();
675 fPreviousRTHeight = rtHeight;
676 fClipRect = clip;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000677 fPreviousTranslate = fTranslate;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000678 return true;
679}
680
681void GrAAHairLinePathRenderer::drawPath(GrDrawTarget::StageBitfield stages) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000682
683 if (!this->createGeom(stages)) {
684 return;
685 }
686
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000687 GrDrawTarget::AutoStateRestore asr;
688 if (!fTarget->getViewMatrix().hasPerspective()) {
689 asr.set(fTarget);
690 GrMatrix ivm;
691 if (fTarget->getViewInverse(&ivm)) {
692 fTarget->preConcatSamplerMatrices(stages, ivm);
693 }
694 fTarget->setViewMatrix(GrMatrix::I());
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000695 }
696
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000697 // TODO: See whether rendering lines as degenerate quads improves perf
698 // when we have a mix
699 fTarget->setIndexSourceToBuffer(fLinesIndexBuffer);
700 int lines = 0;
701 int nBufLines = fLinesIndexBuffer->maxQuads();
702 while (lines < fLineSegmentCnt) {
703 int n = GrMin(fLineSegmentCnt-lines, nBufLines);
704 fTarget->setVertexEdgeType(GrDrawTarget::kHairLine_EdgeType);
705 fTarget->drawIndexed(kTriangles_PrimitiveType,
706 kVertsPerLineSeg*lines, // startV
707 0, // startI
708 kVertsPerLineSeg*n, // vCount
709 kIdxsPerLineSeg*n); // iCount
710 lines += n;
711 }
712
713 fTarget->setIndexSourceToBuffer(fQuadsIndexBuffer);
714 int quads = 0;
715 while (quads < fQuadCnt) {
716 int n = GrMin(fQuadCnt-quads, kNumQuadsInIdxBuffer);
717 fTarget->setVertexEdgeType(GrDrawTarget::kHairQuad_EdgeType);
718 fTarget->drawIndexed(kTriangles_PrimitiveType,
719 4*fLineSegmentCnt + kVertsPerQuad*quads, // startV
720 0, // startI
721 kVertsPerQuad*n, // vCount
722 kIdxsPerQuad*n); // iCount
723 quads += n;
724 }
725
726}
727