blob: b78be5fcd62db22807475a2307ad80e7203afe18 [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
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000143// Takes 178th time of logf on Z600 / VC2010
144int get_float_exp(float x) {
145 GR_STATIC_ASSERT(sizeof(int) == sizeof(float));
146#if GR_DEBUG
147 static bool tested;
148 if (!tested) {
149 tested = true;
150 GrAssert(get_float_exp(0.25f) == -2);
151 GrAssert(get_float_exp(0.3f) == -2);
152 GrAssert(get_float_exp(0.5f) == -1);
153 GrAssert(get_float_exp(1.f) == 0);
154 GrAssert(get_float_exp(2.f) == 1);
155 GrAssert(get_float_exp(2.5f) == 1);
156 GrAssert(get_float_exp(8.f) == 3);
157 GrAssert(get_float_exp(100.f) == 6);
158 GrAssert(get_float_exp(1000.f) == 9);
159 GrAssert(get_float_exp(1024.f) == 10);
160 GrAssert(get_float_exp(3000000.f) == 21);
161 }
162#endif
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000163 const int* iptr = (const int*)&x;
164 return (((*iptr) & 0x7f800000) >> 23) - 127;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000165}
166
167// we subdivide the quads to avoid huge overfill
168// if it returns -1 then should be drawn as lines
169int num_quad_subdivs(const SkPoint p[3]) {
170 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000171 static const SkScalar gDegenerateToLineTolSqd =
172 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000173
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000174 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
175 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000176 return -1;
177 }
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000178
179 GrScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
180 if (dsqd < gDegenerateToLineTolSqd) {
181 return -1;
182 }
183
184 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000185 return -1;
186 }
187
188 static const int kMaxSub = 4;
189 // tolerance of triangle height in pixels
190 // tuned on windows Quadro FX 380 / Z600
191 // trade off of fill vs cpu time on verts
192 // maybe different when do this using gpu (geo or tess shaders)
193 static const SkScalar gSubdivTol = 175 * SK_Scalar1;
194
195 if (dsqd <= gSubdivTol*gSubdivTol) {
196 return 0;
197 } else {
198 // subdividing the quad reduces d by 4. so we want x = log4(d/tol)
199 // = log4(d*d/tol*tol)/2
200 // = log2(d*d/tol*tol)
201
202#ifdef SK_SCALAR_IS_FLOAT
203 // +1 since we're ignoring the mantissa contribution.
204 int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
205 log = GrMin(GrMax(0, log), kMaxSub);
206 return log;
207#else
bsalomon@google.comfcb0dbc2011-08-30 18:35:18 +0000208 SkScalar log = SkScalarLog(SkScalarDiv(dsqd,gSubdivTol*gSubdivTol));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000209 static const SkScalar conv = SkScalarInvert(SkScalarLog(2));
210 log = SkScalarMul(log, conv);
211 return GrMin(GrMax(0, SkScalarCeilToInt(log)),kMaxSub);
212#endif
213 }
214}
215
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000216/**
217 * Generates the lines and quads to be rendered. Lines are always recorded in
218 * device space. We will do a device space bloat to account for the 1pixel
219 * thickness.
220 * Quads are recorded in device space unless m contains
221 * perspective, then in they are in src space. We do this because we will
222 * subdivide large quads to reduce over-fill. This subdivision has to be
223 * performed before applying the perspective matrix.
224 */
225int generate_lines_and_quads(const SkPath& path,
226 const SkMatrix& m,
227 const SkVector& translate,
228 GrIRect clip,
229 PtArray* lines,
230 PtArray* quads,
231 IntArray* quadSubdivCnts) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000232 SkPath::Iter iter(path, false);
233
234 int totalQuadCount = 0;
235 GrRect bounds;
236 GrIRect ibounds;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000237
238 bool persp = m.hasPerspective();
239
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000240 for (;;) {
241 GrPoint pts[4];
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000242 GrPoint devPts[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000243 GrPathCmd cmd = (GrPathCmd)iter.next(pts);
244 switch (cmd) {
245 case kMove_PathCmd:
246 break;
247 case kLine_PathCmd:
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000248 SkPoint::Offset(pts, 2, translate);
249 m.mapPoints(devPts, pts, 2);
250 bounds.setBounds(devPts, 2);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000251 bounds.outset(SK_Scalar1, SK_Scalar1);
252 bounds.roundOut(&ibounds);
253 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000254 SkPoint* pts = lines->push_back_n(2);
255 pts[0] = devPts[0];
256 pts[1] = devPts[1];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000257 }
258 break;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000259 case kQuadratic_PathCmd:
260 SkPoint::Offset(pts, 3, translate);
261 m.mapPoints(devPts, pts, 3);
262 bounds.setBounds(devPts, 3);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000263 bounds.outset(SK_Scalar1, SK_Scalar1);
264 bounds.roundOut(&ibounds);
265 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000266 int subdiv = num_quad_subdivs(devPts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000267 GrAssert(subdiv >= -1);
268 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000269 SkPoint* pts = lines->push_back_n(4);
270 pts[0] = devPts[0];
271 pts[1] = devPts[1];
272 pts[2] = devPts[1];
273 pts[3] = devPts[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000274 } else {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000275 // when in perspective keep quads in src space
276 SkPoint* qPts = persp ? pts : devPts;
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000277 SkPoint* pts = quads->push_back_n(3);
278 pts[0] = qPts[0];
279 pts[1] = qPts[1];
280 pts[2] = qPts[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000281 quadSubdivCnts->push_back() = subdiv;
282 totalQuadCount += 1 << subdiv;
283 }
284 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000285 break;
286 case kCubic_PathCmd:
287 SkPoint::Offset(pts, 4, translate);
288 m.mapPoints(devPts, pts, 4);
289 bounds.setBounds(devPts, 4);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000290 bounds.outset(SK_Scalar1, SK_Scalar1);
291 bounds.roundOut(&ibounds);
292 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000293 PREALLOC_PTARRAY(32) q;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000294 // We convert cubics to quadratics (for now).
295 // In perspective have to do conversion in src space.
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000296 if (persp) {
297 SkScalar tolScale =
298 GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m,
299 path.getBounds());
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000300 GrPathUtils::convertCubicToQuads(pts, tolScale, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000301 } else {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000302 GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000303 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000304 for (int i = 0; i < q.count(); i += 3) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000305 SkPoint* qInDevSpace;
306 // bounds has to be calculated in device space, but q is
307 // in src space when there is perspective.
308 if (persp) {
309 m.mapPoints(devPts, &q[i], 3);
310 bounds.setBounds(devPts, 3);
311 qInDevSpace = devPts;
312 } else {
313 bounds.setBounds(&q[i], 3);
314 qInDevSpace = &q[i];
315 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000316 bounds.outset(SK_Scalar1, SK_Scalar1);
317 bounds.roundOut(&ibounds);
318 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000319 int subdiv = num_quad_subdivs(qInDevSpace);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000320 GrAssert(subdiv >= -1);
321 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000322 SkPoint* pts = lines->push_back_n(4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000323 // lines should always be in device coords
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000324 pts[0] = qInDevSpace[0];
325 pts[1] = qInDevSpace[1];
326 pts[2] = qInDevSpace[1];
327 pts[3] = qInDevSpace[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000328 } else {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000329 SkPoint* pts = quads->push_back_n(3);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000330 // q is already in src space when there is no
331 // perspective and dev coords otherwise.
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000332 pts[0] = q[0 + i];
333 pts[1] = q[1 + i];
334 pts[2] = q[2 + i];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000335 quadSubdivCnts->push_back() = subdiv;
336 totalQuadCount += 1 << subdiv;
337 }
338 }
339 }
340 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000341 break;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000342 case kClose_PathCmd:
343 break;
344 case kEnd_PathCmd:
345 return totalQuadCount;
346 }
347 }
348}
349
350struct Vertex {
351 GrPoint fPos;
352 union {
353 struct {
354 GrScalar fA;
355 GrScalar fB;
356 GrScalar fC;
357 } fLine;
358 GrVec fQuadCoord;
359 struct {
360 GrScalar fBogus[4];
361 };
362 };
363};
364GR_STATIC_ASSERT(sizeof(Vertex) == 3 * sizeof(GrPoint));
365
366void intersect_lines(const SkPoint& ptA, const SkVector& normA,
367 const SkPoint& ptB, const SkVector& normB,
368 SkPoint* result) {
369
370 SkScalar lineAW = -normA.dot(ptA);
371 SkScalar lineBW = -normB.dot(ptB);
372
373 SkScalar wInv = SkScalarMul(normA.fX, normB.fY) -
374 SkScalarMul(normA.fY, normB.fX);
375 wInv = SkScalarInvert(wInv);
376
377 result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY);
378 result->fX = SkScalarMul(result->fX, wInv);
379
380 result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW);
381 result->fY = SkScalarMul(result->fY, wInv);
382}
383
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000384void bloat_quad(const SkPoint qpts[3], const GrMatrix* toDevice,
385 const GrMatrix* toSrc, Vertex verts[kVertsPerQuad]) {
386 GrAssert(!toDevice == !toSrc);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000387 // original quad is specified by tri a,b,c
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000388 SkPoint a = qpts[0];
389 SkPoint b = qpts[1];
390 SkPoint c = qpts[2];
391
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000392 // this should be in the src space, not dev coords, when we have perspective
393 SkMatrix DevToUV;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000394 GrPathUtils::quadDesignSpaceToUVCoordsMatrix(qpts, &DevToUV);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000395
396 if (toDevice) {
397 toDevice->mapPoints(&a, 1);
398 toDevice->mapPoints(&b, 1);
399 toDevice->mapPoints(&c, 1);
400 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000401 // make a new poly where we replace a and c by a 1-pixel wide edges orthog
402 // to edges ab and bc:
403 //
404 // before | after
405 // | b0
406 // b |
407 // |
408 // | a0 c0
409 // a c | a1 c1
410 //
411 // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c,
412 // respectively.
413 Vertex& a0 = verts[0];
414 Vertex& a1 = verts[1];
415 Vertex& b0 = verts[2];
416 Vertex& c0 = verts[3];
417 Vertex& c1 = verts[4];
418
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000419 SkVector ab = b;
420 ab -= a;
421 SkVector ac = c;
422 ac -= a;
423 SkVector cb = b;
424 cb -= c;
425
426 // We should have already handled degenerates
427 GrAssert(ab.length() > 0 && cb.length() > 0);
428
429 ab.normalize();
430 SkVector abN;
431 abN.setOrthog(ab, SkVector::kLeft_Side);
432 if (abN.dot(ac) > 0) {
433 abN.negate();
434 }
435
436 cb.normalize();
437 SkVector cbN;
438 cbN.setOrthog(cb, SkVector::kLeft_Side);
439 if (cbN.dot(ac) < 0) {
440 cbN.negate();
441 }
442
443 a0.fPos = a;
444 a0.fPos += abN;
445 a1.fPos = a;
446 a1.fPos -= abN;
447
448 c0.fPos = c;
449 c0.fPos += cbN;
450 c1.fPos = c;
451 c1.fPos -= cbN;
452
453 intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos);
454
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000455 if (toSrc) {
456 toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(Vertex), kVertsPerQuad);
457 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000458 DevToUV.mapPointsWithStride(&verts[0].fQuadCoord,
459 &verts[0].fPos, sizeof(Vertex), kVertsPerQuad);
460}
461
462void add_quads(const SkPoint p[3],
463 int subdiv,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000464 const GrMatrix* toDevice,
465 const GrMatrix* toSrc,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000466 Vertex** vert) {
467 GrAssert(subdiv >= 0);
468 if (subdiv) {
469 SkPoint newP[5];
470 SkChopQuadAtHalf(p, newP);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000471 add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert);
472 add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000473 } else {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000474 bloat_quad(p, toDevice, toSrc, *vert);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000475 *vert += kVertsPerQuad;
476 }
477}
478
479void add_line(const SkPoint p[2],
480 int rtHeight,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000481 const SkMatrix* toSrc,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000482 Vertex** vert) {
483 const SkPoint& a = p[0];
484 const SkPoint& b = p[1];
485
486 SkVector orthVec = b;
487 orthVec -= a;
488
489 if (orthVec.setLength(SK_Scalar1)) {
490 orthVec.setOrthog(orthVec);
491
492 // the values we pass down to the frag shader
493 // have to be in y-points-up space;
494 SkVector normal;
495 normal.fX = orthVec.fX;
496 normal.fY = -orthVec.fY;
497 SkPoint aYDown;
498 aYDown.fX = a.fX;
499 aYDown.fY = rtHeight - a.fY;
500
501 SkScalar lineC = -(aYDown.dot(normal));
502 for (int i = 0; i < kVertsPerLineSeg; ++i) {
503 (*vert)[i].fPos = (i < 2) ? a : b;
504 if (0 == i || 3 == i) {
505 (*vert)[i].fPos -= orthVec;
506 } else {
507 (*vert)[i].fPos += orthVec;
508 }
509 (*vert)[i].fLine.fA = normal.fX;
510 (*vert)[i].fLine.fB = normal.fY;
511 (*vert)[i].fLine.fC = lineC;
512 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000513 if (NULL != toSrc) {
514 toSrc->mapPointsWithStride(&(*vert)->fPos,
515 sizeof(Vertex),
516 kVertsPerLineSeg);
517 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000518 } else {
519 // just make it degenerate and likely offscreen
520 (*vert)[0].fPos.set(SK_ScalarMax, SK_ScalarMax);
521 (*vert)[1].fPos.set(SK_ScalarMax, SK_ScalarMax);
522 (*vert)[2].fPos.set(SK_ScalarMax, SK_ScalarMax);
523 (*vert)[3].fPos.set(SK_ScalarMax, SK_ScalarMax);
524 }
525
526 *vert += kVertsPerLineSeg;
527}
528
529}
530
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000531bool GrAAHairLinePathRenderer::createGeom(GrDrawState::StageMask stageMask) {
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000532 const GrDrawState& drawState = fTarget->getDrawState();
533 int rtHeight = drawState.getRenderTarget()->height();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000534
535 GrIRect clip;
536 if (fTarget->getClip().hasConservativeBounds()) {
537 GrRect clipRect = fTarget->getClip().getConservativeBounds();
538 clipRect.roundOut(&clip);
539 } else {
540 clip.setLargest();
541 }
542
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000543 // If none of the inputs that affect generation of path geometry have
544 // have changed since last previous path draw then we can reuse the
545 // previous geoemtry.
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000546 if (stageMask == fPreviousStages &&
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000547 fPreviousViewMatrix == drawState.getViewMatrix() &&
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000548 fPreviousTranslate == fTranslate &&
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000549 rtHeight == fPreviousRTHeight &&
550 fClipRect == clip) {
551 return true;
552 }
553
554 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
tomhudson@google.com93813632011-10-27 20:21:16 +0000555 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000556 if ((1 << s) & stageMask) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000557 layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s);
558 }
559 }
560
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000561 GrMatrix viewM = drawState.getViewMatrix();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000562
bsalomon@google.com92669012011-09-27 19:10:05 +0000563 PREALLOC_PTARRAY(128) lines;
564 PREALLOC_PTARRAY(128) quads;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000565 IntArray qSubdivs;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000566 fQuadCnt = generate_lines_and_quads(*fPath, viewM, fTranslate, clip,
567 &lines, &quads, &qSubdivs);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000568
569 fLineSegmentCnt = lines.count() / 2;
570 int vertCnt = kVertsPerLineSeg * fLineSegmentCnt + kVertsPerQuad * fQuadCnt;
571
572 GrAssert(sizeof(Vertex) == GrDrawTarget::VertexSize(layout));
573
574 Vertex* verts;
575 if (!fTarget->reserveVertexSpace(layout, vertCnt, (void**)&verts)) {
576 return false;
577 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000578 Vertex* base = verts;
579
580 const GrMatrix* toDevice = NULL;
581 const GrMatrix* toSrc = NULL;
582 GrMatrix ivm;
583
584 if (viewM.hasPerspective()) {
585 if (viewM.invert(&ivm)) {
586 toDevice = &viewM;
587 toSrc = &ivm;
588 }
589 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000590
591 for (int i = 0; i < fLineSegmentCnt; ++i) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000592 add_line(&lines[2*i], rtHeight, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000593 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000594
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000595 int unsubdivQuadCnt = quads.count() / 3;
596 for (int i = 0; i < unsubdivQuadCnt; ++i) {
597 GrAssert(qSubdivs[i] >= 0);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000598 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000599 }
600
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000601 fPreviousStages = stageMask;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000602 fPreviousViewMatrix = drawState.getViewMatrix();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000603 fPreviousRTHeight = rtHeight;
604 fClipRect = clip;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000605 fPreviousTranslate = fTranslate;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000606 return true;
607}
608
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000609void GrAAHairLinePathRenderer::drawPath(GrDrawState::StageMask stageMask) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000610
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000611 if (!this->createGeom(stageMask)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000612 return;
613 }
614
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000615 GrDrawState* drawState = fTarget->drawState();
616
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000617 GrDrawTarget::AutoStateRestore asr;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000618 if (!drawState->getViewMatrix().hasPerspective()) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000619 asr.set(fTarget);
620 GrMatrix ivm;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000621 if (drawState->getViewInverse(&ivm)) {
622 drawState->preConcatSamplerMatrices(stageMask, ivm);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000623 }
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000624 drawState->setViewMatrix(GrMatrix::I());
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000625 }
626
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000627 // TODO: See whether rendering lines as degenerate quads improves perf
628 // when we have a mix
629 fTarget->setIndexSourceToBuffer(fLinesIndexBuffer);
630 int lines = 0;
631 int nBufLines = fLinesIndexBuffer->maxQuads();
632 while (lines < fLineSegmentCnt) {
633 int n = GrMin(fLineSegmentCnt-lines, nBufLines);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000634 drawState->setVertexEdgeType(GrDrawState::kHairLine_EdgeType);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000635 fTarget->drawIndexed(kTriangles_PrimitiveType,
636 kVertsPerLineSeg*lines, // startV
637 0, // startI
638 kVertsPerLineSeg*n, // vCount
639 kIdxsPerLineSeg*n); // iCount
640 lines += n;
641 }
642
643 fTarget->setIndexSourceToBuffer(fQuadsIndexBuffer);
644 int quads = 0;
645 while (quads < fQuadCnt) {
646 int n = GrMin(fQuadCnt-quads, kNumQuadsInIdxBuffer);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000647 drawState->setVertexEdgeType(GrDrawState::kHairQuad_EdgeType);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000648 fTarget->drawIndexed(kTriangles_PrimitiveType,
649 4*fLineSegmentCnt + kVertsPerQuad*quads, // startV
650 0, // startI
651 kVertsPerQuad*n, // vCount
652 kIdxsPerQuad*n); // iCount
653 quads += n;
654 }
655
656}
657