blob: f16c8efa63dfee50b782825785a1325f81fd2bc2 [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.com0d2aa842011-09-26 15:59:20 +0000109bool GrAAHairLinePathRenderer::supportsAA(const GrDrawTarget* target,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000110 const SkPath& path,
bsalomon@google.com0d2aa842011-09-26 15:59:20 +0000111 GrPathFill fill) const {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000112 return kHairLine_PathFill == fill;
113}
114
115bool GrAAHairLinePathRenderer::canDrawPath(const GrDrawTarget* target,
116 const SkPath& path,
117 GrPathFill fill) const {
bsalomon@google.coma8a6a322011-09-23 14:19:58 +0000118 static const uint32_t gReqDerivMask = SkPath::kCubic_SegmentMask |
119 SkPath::kQuad_SegmentMask;
120 return (kHairLine_PathFill == fill &&
121 target->isAntialiasState() &&
122 (target->getCaps().fShaderDerivativeSupport ||
123 !(gReqDerivMask & path.getSegmentMasks())));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000124}
125
126void GrAAHairLinePathRenderer::pathWillClear() {
127 this->resetGeom();
128}
129
130void GrAAHairLinePathRenderer::resetGeom() {
131 fPreviousStages = ~0;
132 fPreviousRTHeight = ~0;
133 fPreviousViewMatrix = GrMatrix::InvalidMatrix();
134 fLineSegmentCnt = 0;
135 fQuadCnt = 0;
136 if ((fQuadCnt || fLineSegmentCnt) && NULL != fTarget) {
137 fTarget->resetVertexSource();
138 }
139}
140
141namespace {
142
bsalomon@google.com49313f62011-09-14 13:54:05 +0000143typedef SkTArray<SkPoint, true> PtArray;
bsalomon@google.com92669012011-09-27 19:10:05 +0000144#define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true>
bsalomon@google.com49313f62011-09-14 13:54:05 +0000145typedef SkTArray<int, true> IntArray;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000146
147/**
148 * We convert cubics to quadratics (for now).
149 */
150void convert_noninflect_cubic_to_quads(const SkPoint p[4],
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000151 SkScalar tolScale,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000152 PtArray* quads,
153 int sublevel = 0) {
154 SkVector ab = p[1];
155 ab -= p[0];
156 SkVector dc = p[2];
157 dc -= p[3];
158
159 static const SkScalar gLengthScale = 3 * SK_Scalar1 / 2;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000160 // base tolerance is 2 pixels in dev coords.
161 const SkScalar distanceSqdTol = SkScalarMul(tolScale, 2 * SK_Scalar1);
162 static const int kMaxSubdivs = 10;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000163
164 ab.scale(gLengthScale);
165 dc.scale(gLengthScale);
166
167 SkVector c0 = p[0];
168 c0 += ab;
169 SkVector c1 = p[3];
170 c1 += dc;
171
172 SkScalar dSqd = c0.distanceToSqd(c1);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000173 if (sublevel > kMaxSubdivs || dSqd <= distanceSqdTol) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000174 SkPoint cAvg = c0;
175 cAvg += c1;
176 cAvg.scale(SK_ScalarHalf);
177
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000178 SkPoint* pts = quads->push_back_n(3);
179 pts[0] = p[0];
180 pts[1] = cAvg;
181 pts[2] = p[3];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000182
183 return;
184 } else {
185 SkPoint choppedPts[7];
186 SkChopCubicAtHalf(p, choppedPts);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000187 convert_noninflect_cubic_to_quads(choppedPts + 0, tolScale,
188 quads, sublevel + 1);
189 convert_noninflect_cubic_to_quads(choppedPts + 3, tolScale,
190 quads, sublevel + 1);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000191 }
192}
193
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000194void convert_cubic_to_quads(const SkPoint p[4],
195 SkScalar tolScale,
196 PtArray* quads) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000197 SkPoint chopped[13];
198 int count = SkChopCubicAtInflections(p, chopped);
199
200 for (int i = 0; i < count; ++i) {
201 SkPoint* cubic = chopped + 3*i;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000202 convert_noninflect_cubic_to_quads(cubic, tolScale, quads);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000203 }
204}
205
206// Takes 178th time of logf on Z600 / VC2010
207int get_float_exp(float x) {
208 GR_STATIC_ASSERT(sizeof(int) == sizeof(float));
209#if GR_DEBUG
210 static bool tested;
211 if (!tested) {
212 tested = true;
213 GrAssert(get_float_exp(0.25f) == -2);
214 GrAssert(get_float_exp(0.3f) == -2);
215 GrAssert(get_float_exp(0.5f) == -1);
216 GrAssert(get_float_exp(1.f) == 0);
217 GrAssert(get_float_exp(2.f) == 1);
218 GrAssert(get_float_exp(2.5f) == 1);
219 GrAssert(get_float_exp(8.f) == 3);
220 GrAssert(get_float_exp(100.f) == 6);
221 GrAssert(get_float_exp(1000.f) == 9);
222 GrAssert(get_float_exp(1024.f) == 10);
223 GrAssert(get_float_exp(3000000.f) == 21);
224 }
225#endif
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000226 const int* iptr = (const int*)&x;
227 return (((*iptr) & 0x7f800000) >> 23) - 127;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000228}
229
230// we subdivide the quads to avoid huge overfill
231// if it returns -1 then should be drawn as lines
232int num_quad_subdivs(const SkPoint p[3]) {
233 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000234 static const SkScalar gDegenerateToLineTolSqd =
235 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000236
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000237 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
238 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000239 return -1;
240 }
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000241
242 GrScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
243 if (dsqd < gDegenerateToLineTolSqd) {
244 return -1;
245 }
246
247 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000248 return -1;
249 }
250
251 static const int kMaxSub = 4;
252 // tolerance of triangle height in pixels
253 // tuned on windows Quadro FX 380 / Z600
254 // trade off of fill vs cpu time on verts
255 // maybe different when do this using gpu (geo or tess shaders)
256 static const SkScalar gSubdivTol = 175 * SK_Scalar1;
257
258 if (dsqd <= gSubdivTol*gSubdivTol) {
259 return 0;
260 } else {
261 // subdividing the quad reduces d by 4. so we want x = log4(d/tol)
262 // = log4(d*d/tol*tol)/2
263 // = log2(d*d/tol*tol)
264
265#ifdef SK_SCALAR_IS_FLOAT
266 // +1 since we're ignoring the mantissa contribution.
267 int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
268 log = GrMin(GrMax(0, log), kMaxSub);
269 return log;
270#else
bsalomon@google.comfcb0dbc2011-08-30 18:35:18 +0000271 SkScalar log = SkScalarLog(SkScalarDiv(dsqd,gSubdivTol*gSubdivTol));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000272 static const SkScalar conv = SkScalarInvert(SkScalarLog(2));
273 log = SkScalarMul(log, conv);
274 return GrMin(GrMax(0, SkScalarCeilToInt(log)),kMaxSub);
275#endif
276 }
277}
278
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000279/**
280 * Generates the lines and quads to be rendered. Lines are always recorded in
281 * device space. We will do a device space bloat to account for the 1pixel
282 * thickness.
283 * Quads are recorded in device space unless m contains
284 * perspective, then in they are in src space. We do this because we will
285 * subdivide large quads to reduce over-fill. This subdivision has to be
286 * performed before applying the perspective matrix.
287 */
288int generate_lines_and_quads(const SkPath& path,
289 const SkMatrix& m,
290 const SkVector& translate,
291 GrIRect clip,
292 PtArray* lines,
293 PtArray* quads,
294 IntArray* quadSubdivCnts) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000295 SkPath::Iter iter(path, false);
296
297 int totalQuadCount = 0;
298 GrRect bounds;
299 GrIRect ibounds;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000300
301 bool persp = m.hasPerspective();
302
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000303 for (;;) {
304 GrPoint pts[4];
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000305 GrPoint devPts[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000306 GrPathCmd cmd = (GrPathCmd)iter.next(pts);
307 switch (cmd) {
308 case kMove_PathCmd:
309 break;
310 case kLine_PathCmd:
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000311 SkPoint::Offset(pts, 2, translate);
312 m.mapPoints(devPts, pts, 2);
313 bounds.setBounds(devPts, 2);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000314 bounds.outset(SK_Scalar1, SK_Scalar1);
315 bounds.roundOut(&ibounds);
316 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000317 SkPoint* pts = lines->push_back_n(2);
318 pts[0] = devPts[0];
319 pts[1] = devPts[1];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000320 }
321 break;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000322 case kQuadratic_PathCmd:
323 SkPoint::Offset(pts, 3, translate);
324 m.mapPoints(devPts, pts, 3);
325 bounds.setBounds(devPts, 3);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000326 bounds.outset(SK_Scalar1, SK_Scalar1);
327 bounds.roundOut(&ibounds);
328 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000329 int subdiv = num_quad_subdivs(devPts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000330 GrAssert(subdiv >= -1);
331 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000332 SkPoint* pts = lines->push_back_n(4);
333 pts[0] = devPts[0];
334 pts[1] = devPts[1];
335 pts[2] = devPts[1];
336 pts[3] = devPts[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000337 } else {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000338 // when in perspective keep quads in src space
339 SkPoint* qPts = persp ? pts : devPts;
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000340 SkPoint* pts = quads->push_back_n(3);
341 pts[0] = qPts[0];
342 pts[1] = qPts[1];
343 pts[2] = qPts[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000344 quadSubdivCnts->push_back() = subdiv;
345 totalQuadCount += 1 << subdiv;
346 }
347 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000348 break;
349 case kCubic_PathCmd:
350 SkPoint::Offset(pts, 4, translate);
351 m.mapPoints(devPts, pts, 4);
352 bounds.setBounds(devPts, 4);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000353 bounds.outset(SK_Scalar1, SK_Scalar1);
354 bounds.roundOut(&ibounds);
355 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000356 PREALLOC_PTARRAY(32) q;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000357 // in perspective have to do conversion in src space
358 if (persp) {
359 SkScalar tolScale =
360 GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m,
361 path.getBounds());
362 convert_cubic_to_quads(pts, tolScale, &q);
363 } else {
364 convert_cubic_to_quads(devPts, SK_Scalar1, &q);
365 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000366 for (int i = 0; i < q.count(); i += 3) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000367 SkPoint* qInDevSpace;
368 // bounds has to be calculated in device space, but q is
369 // in src space when there is perspective.
370 if (persp) {
371 m.mapPoints(devPts, &q[i], 3);
372 bounds.setBounds(devPts, 3);
373 qInDevSpace = devPts;
374 } else {
375 bounds.setBounds(&q[i], 3);
376 qInDevSpace = &q[i];
377 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000378 bounds.outset(SK_Scalar1, SK_Scalar1);
379 bounds.roundOut(&ibounds);
380 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000381 int subdiv = num_quad_subdivs(qInDevSpace);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000382 GrAssert(subdiv >= -1);
383 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000384 SkPoint* pts = lines->push_back_n(4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000385 // lines should always be in device coords
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000386 pts[0] = qInDevSpace[0];
387 pts[1] = qInDevSpace[1];
388 pts[2] = qInDevSpace[1];
389 pts[3] = qInDevSpace[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000390 } else {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000391 SkPoint* pts = quads->push_back_n(3);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000392 // q is already in src space when there is no
393 // perspective and dev coords otherwise.
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000394 pts[0] = q[0 + i];
395 pts[1] = q[1 + i];
396 pts[2] = q[2 + i];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000397 quadSubdivCnts->push_back() = subdiv;
398 totalQuadCount += 1 << subdiv;
399 }
400 }
401 }
402 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000403 break;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000404 case kClose_PathCmd:
405 break;
406 case kEnd_PathCmd:
407 return totalQuadCount;
408 }
409 }
410}
411
412struct Vertex {
413 GrPoint fPos;
414 union {
415 struct {
416 GrScalar fA;
417 GrScalar fB;
418 GrScalar fC;
419 } fLine;
420 GrVec fQuadCoord;
421 struct {
422 GrScalar fBogus[4];
423 };
424 };
425};
426GR_STATIC_ASSERT(sizeof(Vertex) == 3 * sizeof(GrPoint));
427
428void intersect_lines(const SkPoint& ptA, const SkVector& normA,
429 const SkPoint& ptB, const SkVector& normB,
430 SkPoint* result) {
431
432 SkScalar lineAW = -normA.dot(ptA);
433 SkScalar lineBW = -normB.dot(ptB);
434
435 SkScalar wInv = SkScalarMul(normA.fX, normB.fY) -
436 SkScalarMul(normA.fY, normB.fX);
437 wInv = SkScalarInvert(wInv);
438
439 result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY);
440 result->fX = SkScalarMul(result->fX, wInv);
441
442 result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW);
443 result->fY = SkScalarMul(result->fY, wInv);
444}
445
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000446void bloat_quad(const SkPoint qpts[3], const GrMatrix* toDevice,
447 const GrMatrix* toSrc, Vertex verts[kVertsPerQuad]) {
448 GrAssert(!toDevice == !toSrc);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000449 // original quad is specified by tri a,b,c
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000450 SkPoint a = qpts[0];
451 SkPoint b = qpts[1];
452 SkPoint c = qpts[2];
453
454 // compute a matrix that goes from device coords to U,V quad params
455 // this should be in the src space, not dev coords, when we have perspective
456 SkMatrix DevToUV;
457 DevToUV.setAll(a.fX, b.fX, c.fX,
458 a.fY, b.fY, c.fY,
459 SK_Scalar1, SK_Scalar1, SK_Scalar1);
460 DevToUV.invert(&DevToUV);
461 // can't make this static, no cons :(
462 SkMatrix UVpts;
463 UVpts.setAll(0, SK_ScalarHalf, SK_Scalar1,
464 0, 0, SK_Scalar1,
465 SK_Scalar1, SK_Scalar1, SK_Scalar1);
466 DevToUV.postConcat(UVpts);
467
468 // We really want to avoid perspective matrix muls.
469 // These may wind up really close to zero
470 DevToUV.setPerspX(0);
471 DevToUV.setPerspY(0);
472
473 if (toDevice) {
474 toDevice->mapPoints(&a, 1);
475 toDevice->mapPoints(&b, 1);
476 toDevice->mapPoints(&c, 1);
477 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000478 // make a new poly where we replace a and c by a 1-pixel wide edges orthog
479 // to edges ab and bc:
480 //
481 // before | after
482 // | b0
483 // b |
484 // |
485 // | a0 c0
486 // a c | a1 c1
487 //
488 // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c,
489 // respectively.
490 Vertex& a0 = verts[0];
491 Vertex& a1 = verts[1];
492 Vertex& b0 = verts[2];
493 Vertex& c0 = verts[3];
494 Vertex& c1 = verts[4];
495
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000496 SkVector ab = b;
497 ab -= a;
498 SkVector ac = c;
499 ac -= a;
500 SkVector cb = b;
501 cb -= c;
502
503 // We should have already handled degenerates
504 GrAssert(ab.length() > 0 && cb.length() > 0);
505
506 ab.normalize();
507 SkVector abN;
508 abN.setOrthog(ab, SkVector::kLeft_Side);
509 if (abN.dot(ac) > 0) {
510 abN.negate();
511 }
512
513 cb.normalize();
514 SkVector cbN;
515 cbN.setOrthog(cb, SkVector::kLeft_Side);
516 if (cbN.dot(ac) < 0) {
517 cbN.negate();
518 }
519
520 a0.fPos = a;
521 a0.fPos += abN;
522 a1.fPos = a;
523 a1.fPos -= abN;
524
525 c0.fPos = c;
526 c0.fPos += cbN;
527 c1.fPos = c;
528 c1.fPos -= cbN;
529
530 intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos);
531
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000532 if (toSrc) {
533 toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(Vertex), kVertsPerQuad);
534 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000535 DevToUV.mapPointsWithStride(&verts[0].fQuadCoord,
536 &verts[0].fPos, sizeof(Vertex), kVertsPerQuad);
537}
538
539void add_quads(const SkPoint p[3],
540 int subdiv,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000541 const GrMatrix* toDevice,
542 const GrMatrix* toSrc,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000543 Vertex** vert) {
544 GrAssert(subdiv >= 0);
545 if (subdiv) {
546 SkPoint newP[5];
547 SkChopQuadAtHalf(p, newP);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000548 add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert);
549 add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000550 } else {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000551 bloat_quad(p, toDevice, toSrc, *vert);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000552 *vert += kVertsPerQuad;
553 }
554}
555
556void add_line(const SkPoint p[2],
557 int rtHeight,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000558 const SkMatrix* toSrc,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000559 Vertex** vert) {
560 const SkPoint& a = p[0];
561 const SkPoint& b = p[1];
562
563 SkVector orthVec = b;
564 orthVec -= a;
565
566 if (orthVec.setLength(SK_Scalar1)) {
567 orthVec.setOrthog(orthVec);
568
569 // the values we pass down to the frag shader
570 // have to be in y-points-up space;
571 SkVector normal;
572 normal.fX = orthVec.fX;
573 normal.fY = -orthVec.fY;
574 SkPoint aYDown;
575 aYDown.fX = a.fX;
576 aYDown.fY = rtHeight - a.fY;
577
578 SkScalar lineC = -(aYDown.dot(normal));
579 for (int i = 0; i < kVertsPerLineSeg; ++i) {
580 (*vert)[i].fPos = (i < 2) ? a : b;
581 if (0 == i || 3 == i) {
582 (*vert)[i].fPos -= orthVec;
583 } else {
584 (*vert)[i].fPos += orthVec;
585 }
586 (*vert)[i].fLine.fA = normal.fX;
587 (*vert)[i].fLine.fB = normal.fY;
588 (*vert)[i].fLine.fC = lineC;
589 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000590 if (NULL != toSrc) {
591 toSrc->mapPointsWithStride(&(*vert)->fPos,
592 sizeof(Vertex),
593 kVertsPerLineSeg);
594 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000595 } else {
596 // just make it degenerate and likely offscreen
597 (*vert)[0].fPos.set(SK_ScalarMax, SK_ScalarMax);
598 (*vert)[1].fPos.set(SK_ScalarMax, SK_ScalarMax);
599 (*vert)[2].fPos.set(SK_ScalarMax, SK_ScalarMax);
600 (*vert)[3].fPos.set(SK_ScalarMax, SK_ScalarMax);
601 }
602
603 *vert += kVertsPerLineSeg;
604}
605
606}
607
608bool GrAAHairLinePathRenderer::createGeom(GrDrawTarget::StageBitfield stages) {
609
610 int rtHeight = fTarget->getRenderTarget()->height();
611
612 GrIRect clip;
613 if (fTarget->getClip().hasConservativeBounds()) {
614 GrRect clipRect = fTarget->getClip().getConservativeBounds();
615 clipRect.roundOut(&clip);
616 } else {
617 clip.setLargest();
618 }
619
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000620 // If none of the inputs that affect generation of path geometry have
621 // have changed since last previous path draw then we can reuse the
622 // previous geoemtry.
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000623 if (stages == fPreviousStages &&
624 fPreviousViewMatrix == fTarget->getViewMatrix() &&
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000625 fPreviousTranslate == fTranslate &&
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000626 rtHeight == fPreviousRTHeight &&
627 fClipRect == clip) {
628 return true;
629 }
630
631 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
632 for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
633 if ((1 << s) & stages) {
634 layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s);
635 }
636 }
637
638 GrMatrix viewM = fTarget->getViewMatrix();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000639
bsalomon@google.com92669012011-09-27 19:10:05 +0000640 PREALLOC_PTARRAY(128) lines;
641 PREALLOC_PTARRAY(128) quads;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000642 IntArray qSubdivs;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000643 fQuadCnt = generate_lines_and_quads(*fPath, viewM, fTranslate, clip,
644 &lines, &quads, &qSubdivs);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000645
646 fLineSegmentCnt = lines.count() / 2;
647 int vertCnt = kVertsPerLineSeg * fLineSegmentCnt + kVertsPerQuad * fQuadCnt;
648
649 GrAssert(sizeof(Vertex) == GrDrawTarget::VertexSize(layout));
650
651 Vertex* verts;
652 if (!fTarget->reserveVertexSpace(layout, vertCnt, (void**)&verts)) {
653 return false;
654 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000655 Vertex* base = verts;
656
657 const GrMatrix* toDevice = NULL;
658 const GrMatrix* toSrc = NULL;
659 GrMatrix ivm;
660
661 if (viewM.hasPerspective()) {
662 if (viewM.invert(&ivm)) {
663 toDevice = &viewM;
664 toSrc = &ivm;
665 }
666 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000667
668 for (int i = 0; i < fLineSegmentCnt; ++i) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000669 add_line(&lines[2*i], rtHeight, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000670 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000671
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000672 int unsubdivQuadCnt = quads.count() / 3;
673 for (int i = 0; i < unsubdivQuadCnt; ++i) {
674 GrAssert(qSubdivs[i] >= 0);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000675 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000676 }
677
678 fPreviousStages = stages;
679 fPreviousViewMatrix = fTarget->getViewMatrix();
680 fPreviousRTHeight = rtHeight;
681 fClipRect = clip;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000682 fPreviousTranslate = fTranslate;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000683 return true;
684}
685
686void GrAAHairLinePathRenderer::drawPath(GrDrawTarget::StageBitfield stages) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000687
688 if (!this->createGeom(stages)) {
689 return;
690 }
691
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000692 GrDrawTarget::AutoStateRestore asr;
693 if (!fTarget->getViewMatrix().hasPerspective()) {
694 asr.set(fTarget);
695 GrMatrix ivm;
696 if (fTarget->getViewInverse(&ivm)) {
697 fTarget->preConcatSamplerMatrices(stages, ivm);
698 }
699 fTarget->setViewMatrix(GrMatrix::I());
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000700 }
701
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000702 // TODO: See whether rendering lines as degenerate quads improves perf
703 // when we have a mix
704 fTarget->setIndexSourceToBuffer(fLinesIndexBuffer);
705 int lines = 0;
706 int nBufLines = fLinesIndexBuffer->maxQuads();
707 while (lines < fLineSegmentCnt) {
708 int n = GrMin(fLineSegmentCnt-lines, nBufLines);
709 fTarget->setVertexEdgeType(GrDrawTarget::kHairLine_EdgeType);
710 fTarget->drawIndexed(kTriangles_PrimitiveType,
711 kVertsPerLineSeg*lines, // startV
712 0, // startI
713 kVertsPerLineSeg*n, // vCount
714 kIdxsPerLineSeg*n); // iCount
715 lines += n;
716 }
717
718 fTarget->setIndexSourceToBuffer(fQuadsIndexBuffer);
719 int quads = 0;
720 while (quads < fQuadCnt) {
721 int n = GrMin(fQuadCnt-quads, kNumQuadsInIdxBuffer);
722 fTarget->setVertexEdgeType(GrDrawTarget::kHairQuad_EdgeType);
723 fTarget->drawIndexed(kTriangles_PrimitiveType,
724 4*fLineSegmentCnt + kVertsPerQuad*quads, // startV
725 0, // startI
726 kVertsPerQuad*n, // vCount
727 kIdxsPerQuad*n); // iCount
728 quads += n;
729 }
730
731}
732