blob: 701bf3a08d038235abc58a171455c847ed270609 [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
bsalomon@google.com0e5104c2012-04-10 16:20:41 +000053 // Each is drawn as three triangles specified by these 9 indices:
bsalomon@google.comaeb21602011-08-30 18:13:44 +000054 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();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000102}
103
104GrAAHairLinePathRenderer::~GrAAHairLinePathRenderer() {
105 fLinesIndexBuffer->unref();
106 fQuadsIndexBuffer->unref();
107}
108
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000109namespace {
110
bsalomon@google.com49313f62011-09-14 13:54:05 +0000111typedef SkTArray<SkPoint, true> PtArray;
bsalomon@google.com92669012011-09-27 19:10:05 +0000112#define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true>
bsalomon@google.com49313f62011-09-14 13:54:05 +0000113typedef SkTArray<int, true> IntArray;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000114
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000115// Takes 178th time of logf on Z600 / VC2010
116int get_float_exp(float x) {
117 GR_STATIC_ASSERT(sizeof(int) == sizeof(float));
118#if GR_DEBUG
119 static bool tested;
120 if (!tested) {
121 tested = true;
122 GrAssert(get_float_exp(0.25f) == -2);
123 GrAssert(get_float_exp(0.3f) == -2);
124 GrAssert(get_float_exp(0.5f) == -1);
125 GrAssert(get_float_exp(1.f) == 0);
126 GrAssert(get_float_exp(2.f) == 1);
127 GrAssert(get_float_exp(2.5f) == 1);
128 GrAssert(get_float_exp(8.f) == 3);
129 GrAssert(get_float_exp(100.f) == 6);
130 GrAssert(get_float_exp(1000.f) == 9);
131 GrAssert(get_float_exp(1024.f) == 10);
132 GrAssert(get_float_exp(3000000.f) == 21);
133 }
134#endif
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000135 const int* iptr = (const int*)&x;
136 return (((*iptr) & 0x7f800000) >> 23) - 127;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000137}
138
139// we subdivide the quads to avoid huge overfill
140// if it returns -1 then should be drawn as lines
141int num_quad_subdivs(const SkPoint p[3]) {
142 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000143 static const SkScalar gDegenerateToLineTolSqd =
144 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000145
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000146 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
147 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000148 return -1;
149 }
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000150
151 GrScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
152 if (dsqd < gDegenerateToLineTolSqd) {
153 return -1;
154 }
155
156 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000157 return -1;
158 }
159
160 static const int kMaxSub = 4;
161 // tolerance of triangle height in pixels
162 // tuned on windows Quadro FX 380 / Z600
163 // trade off of fill vs cpu time on verts
164 // maybe different when do this using gpu (geo or tess shaders)
165 static const SkScalar gSubdivTol = 175 * SK_Scalar1;
166
robertphillips@google.com7460b372012-04-25 16:54:51 +0000167 if (dsqd <= SkScalarMul(gSubdivTol, gSubdivTol)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000168 return 0;
169 } else {
170 // subdividing the quad reduces d by 4. so we want x = log4(d/tol)
171 // = log4(d*d/tol*tol)/2
172 // = log2(d*d/tol*tol)
173
174#ifdef SK_SCALAR_IS_FLOAT
175 // +1 since we're ignoring the mantissa contribution.
176 int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
177 log = GrMin(GrMax(0, log), kMaxSub);
178 return log;
179#else
robertphillips@google.com7460b372012-04-25 16:54:51 +0000180 SkScalar log = SkScalarLog(
181 SkScalarDiv(dsqd,
182 SkScalarMul(gSubdivTol, gSubdivTol)));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000183 static const SkScalar conv = SkScalarInvert(SkScalarLog(2));
184 log = SkScalarMul(log, conv);
185 return GrMin(GrMax(0, SkScalarCeilToInt(log)),kMaxSub);
186#endif
187 }
188}
189
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000190/**
191 * Generates the lines and quads to be rendered. Lines are always recorded in
192 * device space. We will do a device space bloat to account for the 1pixel
193 * thickness.
194 * Quads are recorded in device space unless m contains
195 * perspective, then in they are in src space. We do this because we will
196 * subdivide large quads to reduce over-fill. This subdivision has to be
197 * performed before applying the perspective matrix.
198 */
199int generate_lines_and_quads(const SkPath& path,
200 const SkMatrix& m,
201 const SkVector& translate,
202 GrIRect clip,
203 PtArray* lines,
204 PtArray* quads,
205 IntArray* quadSubdivCnts) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000206 SkPath::Iter iter(path, false);
207
208 int totalQuadCount = 0;
209 GrRect bounds;
210 GrIRect ibounds;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000211
212 bool persp = m.hasPerspective();
213
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000214 for (;;) {
215 GrPoint pts[4];
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000216 GrPoint devPts[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000217 GrPathCmd cmd = (GrPathCmd)iter.next(pts);
218 switch (cmd) {
219 case kMove_PathCmd:
220 break;
221 case kLine_PathCmd:
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000222 SkPoint::Offset(pts, 2, translate);
223 m.mapPoints(devPts, pts, 2);
224 bounds.setBounds(devPts, 2);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000225 bounds.outset(SK_Scalar1, SK_Scalar1);
226 bounds.roundOut(&ibounds);
227 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000228 SkPoint* pts = lines->push_back_n(2);
229 pts[0] = devPts[0];
230 pts[1] = devPts[1];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000231 }
232 break;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000233 case kQuadratic_PathCmd:
234 SkPoint::Offset(pts, 3, translate);
235 m.mapPoints(devPts, pts, 3);
236 bounds.setBounds(devPts, 3);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000237 bounds.outset(SK_Scalar1, SK_Scalar1);
238 bounds.roundOut(&ibounds);
239 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000240 int subdiv = num_quad_subdivs(devPts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000241 GrAssert(subdiv >= -1);
242 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000243 SkPoint* pts = lines->push_back_n(4);
244 pts[0] = devPts[0];
245 pts[1] = devPts[1];
246 pts[2] = devPts[1];
247 pts[3] = devPts[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000248 } else {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000249 // when in perspective keep quads in src space
250 SkPoint* qPts = persp ? pts : devPts;
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000251 SkPoint* pts = quads->push_back_n(3);
252 pts[0] = qPts[0];
253 pts[1] = qPts[1];
254 pts[2] = qPts[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000255 quadSubdivCnts->push_back() = subdiv;
256 totalQuadCount += 1 << subdiv;
257 }
258 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000259 break;
260 case kCubic_PathCmd:
261 SkPoint::Offset(pts, 4, translate);
262 m.mapPoints(devPts, pts, 4);
263 bounds.setBounds(devPts, 4);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000264 bounds.outset(SK_Scalar1, SK_Scalar1);
265 bounds.roundOut(&ibounds);
266 if (SkIRect::Intersects(clip, ibounds)) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000267 PREALLOC_PTARRAY(32) q;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000268 // We convert cubics to quadratics (for now).
269 // In perspective have to do conversion in src space.
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000270 if (persp) {
271 SkScalar tolScale =
272 GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m,
273 path.getBounds());
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000274 GrPathUtils::convertCubicToQuads(pts, tolScale, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000275 } else {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000276 GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000277 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000278 for (int i = 0; i < q.count(); i += 3) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000279 SkPoint* qInDevSpace;
280 // bounds has to be calculated in device space, but q is
281 // in src space when there is perspective.
282 if (persp) {
283 m.mapPoints(devPts, &q[i], 3);
284 bounds.setBounds(devPts, 3);
285 qInDevSpace = devPts;
286 } else {
287 bounds.setBounds(&q[i], 3);
288 qInDevSpace = &q[i];
289 }
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.comdbeeac32011-09-12 14:59:34 +0000293 int subdiv = num_quad_subdivs(qInDevSpace);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000294 GrAssert(subdiv >= -1);
295 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000296 SkPoint* pts = lines->push_back_n(4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000297 // lines should always be in device coords
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000298 pts[0] = qInDevSpace[0];
299 pts[1] = qInDevSpace[1];
300 pts[2] = qInDevSpace[1];
301 pts[3] = qInDevSpace[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000302 } else {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000303 SkPoint* pts = quads->push_back_n(3);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000304 // q is already in src space when there is no
305 // perspective and dev coords otherwise.
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000306 pts[0] = q[0 + i];
307 pts[1] = q[1 + i];
308 pts[2] = q[2 + i];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000309 quadSubdivCnts->push_back() = subdiv;
310 totalQuadCount += 1 << subdiv;
311 }
312 }
313 }
314 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000315 break;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000316 case kClose_PathCmd:
317 break;
318 case kEnd_PathCmd:
319 return totalQuadCount;
320 }
321 }
322}
323
324struct Vertex {
325 GrPoint fPos;
326 union {
327 struct {
328 GrScalar fA;
329 GrScalar fB;
330 GrScalar fC;
331 } fLine;
332 GrVec fQuadCoord;
333 struct {
334 GrScalar fBogus[4];
335 };
336 };
337};
338GR_STATIC_ASSERT(sizeof(Vertex) == 3 * sizeof(GrPoint));
339
340void intersect_lines(const SkPoint& ptA, const SkVector& normA,
341 const SkPoint& ptB, const SkVector& normB,
342 SkPoint* result) {
343
344 SkScalar lineAW = -normA.dot(ptA);
345 SkScalar lineBW = -normB.dot(ptB);
346
347 SkScalar wInv = SkScalarMul(normA.fX, normB.fY) -
348 SkScalarMul(normA.fY, normB.fX);
349 wInv = SkScalarInvert(wInv);
350
351 result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY);
352 result->fX = SkScalarMul(result->fX, wInv);
353
354 result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW);
355 result->fY = SkScalarMul(result->fY, wInv);
356}
357
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000358void bloat_quad(const SkPoint qpts[3], const GrMatrix* toDevice,
359 const GrMatrix* toSrc, Vertex verts[kVertsPerQuad]) {
360 GrAssert(!toDevice == !toSrc);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000361 // original quad is specified by tri a,b,c
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000362 SkPoint a = qpts[0];
363 SkPoint b = qpts[1];
364 SkPoint c = qpts[2];
365
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000366 // this should be in the src space, not dev coords, when we have perspective
bsalomon@google.com19713172012-03-15 13:51:08 +0000367 GrPathUtils::QuadUVMatrix DevToUV(qpts);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000368
369 if (toDevice) {
370 toDevice->mapPoints(&a, 1);
371 toDevice->mapPoints(&b, 1);
372 toDevice->mapPoints(&c, 1);
373 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000374 // make a new poly where we replace a and c by a 1-pixel wide edges orthog
375 // to edges ab and bc:
376 //
377 // before | after
378 // | b0
379 // b |
380 // |
381 // | a0 c0
382 // a c | a1 c1
383 //
384 // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c,
385 // respectively.
386 Vertex& a0 = verts[0];
387 Vertex& a1 = verts[1];
388 Vertex& b0 = verts[2];
389 Vertex& c0 = verts[3];
390 Vertex& c1 = verts[4];
391
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000392 SkVector ab = b;
393 ab -= a;
394 SkVector ac = c;
395 ac -= a;
396 SkVector cb = b;
397 cb -= c;
398
399 // We should have already handled degenerates
400 GrAssert(ab.length() > 0 && cb.length() > 0);
401
402 ab.normalize();
403 SkVector abN;
404 abN.setOrthog(ab, SkVector::kLeft_Side);
405 if (abN.dot(ac) > 0) {
406 abN.negate();
407 }
408
409 cb.normalize();
410 SkVector cbN;
411 cbN.setOrthog(cb, SkVector::kLeft_Side);
412 if (cbN.dot(ac) < 0) {
413 cbN.negate();
414 }
415
416 a0.fPos = a;
417 a0.fPos += abN;
418 a1.fPos = a;
419 a1.fPos -= abN;
420
421 c0.fPos = c;
422 c0.fPos += cbN;
423 c1.fPos = c;
424 c1.fPos -= cbN;
425
426 intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos);
427
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000428 if (toSrc) {
429 toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(Vertex), kVertsPerQuad);
430 }
bsalomon@google.com19713172012-03-15 13:51:08 +0000431 DevToUV.apply<kVertsPerQuad, sizeof(Vertex), sizeof(GrPoint)>(verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000432}
433
434void add_quads(const SkPoint p[3],
435 int subdiv,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000436 const GrMatrix* toDevice,
437 const GrMatrix* toSrc,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000438 Vertex** vert) {
439 GrAssert(subdiv >= 0);
440 if (subdiv) {
441 SkPoint newP[5];
442 SkChopQuadAtHalf(p, newP);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000443 add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert);
444 add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000445 } else {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000446 bloat_quad(p, toDevice, toSrc, *vert);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000447 *vert += kVertsPerQuad;
448 }
449}
450
451void add_line(const SkPoint p[2],
452 int rtHeight,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000453 const SkMatrix* toSrc,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000454 Vertex** vert) {
455 const SkPoint& a = p[0];
456 const SkPoint& b = p[1];
457
458 SkVector orthVec = b;
459 orthVec -= a;
460
461 if (orthVec.setLength(SK_Scalar1)) {
462 orthVec.setOrthog(orthVec);
463
464 // the values we pass down to the frag shader
465 // have to be in y-points-up space;
466 SkVector normal;
467 normal.fX = orthVec.fX;
468 normal.fY = -orthVec.fY;
469 SkPoint aYDown;
470 aYDown.fX = a.fX;
471 aYDown.fY = rtHeight - a.fY;
472
473 SkScalar lineC = -(aYDown.dot(normal));
474 for (int i = 0; i < kVertsPerLineSeg; ++i) {
475 (*vert)[i].fPos = (i < 2) ? a : b;
476 if (0 == i || 3 == i) {
477 (*vert)[i].fPos -= orthVec;
478 } else {
479 (*vert)[i].fPos += orthVec;
480 }
481 (*vert)[i].fLine.fA = normal.fX;
482 (*vert)[i].fLine.fB = normal.fY;
483 (*vert)[i].fLine.fC = lineC;
484 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000485 if (NULL != toSrc) {
486 toSrc->mapPointsWithStride(&(*vert)->fPos,
487 sizeof(Vertex),
488 kVertsPerLineSeg);
489 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000490 } else {
491 // just make it degenerate and likely offscreen
492 (*vert)[0].fPos.set(SK_ScalarMax, SK_ScalarMax);
493 (*vert)[1].fPos.set(SK_ScalarMax, SK_ScalarMax);
494 (*vert)[2].fPos.set(SK_ScalarMax, SK_ScalarMax);
495 (*vert)[3].fPos.set(SK_ScalarMax, SK_ScalarMax);
496 }
497
498 *vert += kVertsPerLineSeg;
499}
500
501}
502
bsalomon@google.comb3729422012-03-07 19:13:28 +0000503bool GrAAHairLinePathRenderer::createGeom(
504 const SkPath& path,
505 const GrVec* translate,
506 GrDrawTarget* target,
507 GrDrawState::StageMask stageMask,
508 int* lineCnt,
509 int* quadCnt,
510 GrDrawTarget::AutoReleaseGeometry* arg) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000511 const GrDrawState& drawState = target->getDrawState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000512 int rtHeight = drawState.getRenderTarget()->height();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000513
514 GrIRect clip;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000515 if (target->getClip().hasConservativeBounds()) {
516 GrRect clipRect = target->getClip().getConservativeBounds();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000517 clipRect.roundOut(&clip);
518 } else {
519 clip.setLargest();
520 }
521
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000522
523 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
tomhudson@google.com93813632011-10-27 20:21:16 +0000524 for (int s = 0; s < GrDrawState::kNumStages; ++s) {
bsalomon@google.com39ee0ff2011-12-06 15:32:52 +0000525 if ((1 << s) & stageMask) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000526 layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s);
527 }
528 }
529
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000530 GrMatrix viewM = drawState.getViewMatrix();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000531
bsalomon@google.com92669012011-09-27 19:10:05 +0000532 PREALLOC_PTARRAY(128) lines;
533 PREALLOC_PTARRAY(128) quads;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000534 IntArray qSubdivs;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000535 static const GrVec gZeroVec = {0, 0};
536 if (NULL == translate) {
537 translate = &gZeroVec;
538 }
539 *quadCnt = generate_lines_and_quads(path, viewM, *translate, clip,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000540 &lines, &quads, &qSubdivs);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000541
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000542 *lineCnt = lines.count() / 2;
543 int vertCnt = kVertsPerLineSeg * *lineCnt + kVertsPerQuad * *quadCnt;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000544
545 GrAssert(sizeof(Vertex) == GrDrawTarget::VertexSize(layout));
546
bsalomon@google.comb3729422012-03-07 19:13:28 +0000547 if (!arg->set(target, layout, vertCnt, 0)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000548 return false;
549 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000550
bsalomon@google.comb3729422012-03-07 19:13:28 +0000551 Vertex* verts = reinterpret_cast<Vertex*>(arg->vertices());
552
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000553 const GrMatrix* toDevice = NULL;
554 const GrMatrix* toSrc = NULL;
555 GrMatrix ivm;
556
557 if (viewM.hasPerspective()) {
558 if (viewM.invert(&ivm)) {
559 toDevice = &viewM;
560 toSrc = &ivm;
561 }
562 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000563
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000564 for (int i = 0; i < *lineCnt; ++i) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000565 add_line(&lines[2*i], rtHeight, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000566 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000567
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000568 int unsubdivQuadCnt = quads.count() / 3;
569 for (int i = 0; i < unsubdivQuadCnt; ++i) {
570 GrAssert(qSubdivs[i] >= 0);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000571 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000572 }
573
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000574 return true;
575}
576
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000577bool GrAAHairLinePathRenderer::canDrawPath(const SkPath& path,
578 GrPathFill fill,
579 const GrDrawTarget* target,
580 bool antiAlias) const {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000581 if (fill != kHairLine_PathFill || !antiAlias) {
582 return false;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000583 }
584
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000585 static const uint32_t gReqDerivMask = SkPath::kCubic_SegmentMask |
586 SkPath::kQuad_SegmentMask;
587 if (!target->getCaps().fShaderDerivativeSupport &&
588 (gReqDerivMask & path.getSegmentMasks())) {
589 return false;
590 }
591 return true;
592}
593
594bool GrAAHairLinePathRenderer::onDrawPath(const SkPath& path,
595 GrPathFill fill,
596 const GrVec* translate,
597 GrDrawTarget* target,
598 GrDrawState::StageMask stageMask,
599 bool antiAlias) {
600
601 int lineCnt;
602 int quadCnt;
bsalomon@google.comb3729422012-03-07 19:13:28 +0000603 GrDrawTarget::AutoReleaseGeometry arg;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000604 if (!this->createGeom(path,
605 translate,
606 target,
607 stageMask,
608 &lineCnt,
bsalomon@google.comb3729422012-03-07 19:13:28 +0000609 &quadCnt,
610 &arg)) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000611 return false;
612 }
613
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000614 GrDrawTarget::AutoStateRestore asr;
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000615 GrDrawState* drawState = target->drawState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000616 if (!drawState->getViewMatrix().hasPerspective()) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000617 // we are going to whack the view matrix to identity to remove
618 // perspective.
619 asr.set(target,
620 GrDrawTarget::kPreserve_ASRInit);
621 drawState = target->drawState();
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000622 GrMatrix ivm;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000623 if (drawState->getViewInverse(&ivm)) {
624 drawState->preConcatSamplerMatrices(stageMask, ivm);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000625 }
bsalomon@google.comb3e40c02012-03-20 15:36:32 +0000626 drawState->viewMatrix()->reset();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000627 }
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000628
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000629
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000630 // TODO: See whether rendering lines as degenerate quads improves perf
631 // when we have a mix
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000632 target->setIndexSourceToBuffer(fLinesIndexBuffer);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000633 int lines = 0;
634 int nBufLines = fLinesIndexBuffer->maxQuads();
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000635 while (lines < lineCnt) {
636 int n = GrMin(lineCnt - lines, nBufLines);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000637 drawState->setVertexEdgeType(GrDrawState::kHairLine_EdgeType);
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000638 target->drawIndexed(kTriangles_PrimitiveType,
639 kVertsPerLineSeg*lines, // startV
640 0, // startI
641 kVertsPerLineSeg*n, // vCount
642 kIdxsPerLineSeg*n); // iCount
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000643 lines += n;
644 }
645
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000646 target->setIndexSourceToBuffer(fQuadsIndexBuffer);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000647 int quads = 0;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000648 while (quads < quadCnt) {
649 int n = GrMin(quadCnt - quads, kNumQuadsInIdxBuffer);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000650 drawState->setVertexEdgeType(GrDrawState::kHairQuad_EdgeType);
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000651 target->drawIndexed(kTriangles_PrimitiveType,
652 4 * lineCnt + kVertsPerQuad*quads, // startV
653 0, // startI
654 kVertsPerQuad*n, // vCount
655 kIdxsPerQuad*n); // iCount
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000656 quads += n;
657 }
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000658 return true;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000659}
660