blob: 94b607ce1265ae23bc724106e7366117edfbb90f [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) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000038 data = SkNEW_ARRAY(uint16_t, kNumQuadsInIdxBuffer * kIdxsPerQuad);
bsalomon@google.comaeb21602011-08-30 18:13:44 +000039 }
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 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +000089 return SkNEW_ARGS(GrAAHairLinePathRenderer,
90 (context, lIdxBuffer, 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();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000101}
102
103GrAAHairLinePathRenderer::~GrAAHairLinePathRenderer() {
104 fLinesIndexBuffer->unref();
105 fQuadsIndexBuffer->unref();
106}
107
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000108namespace {
109
bsalomon@google.com49313f62011-09-14 13:54:05 +0000110typedef SkTArray<SkPoint, true> PtArray;
bsalomon@google.com92669012011-09-27 19:10:05 +0000111#define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true>
bsalomon@google.com49313f62011-09-14 13:54:05 +0000112typedef SkTArray<int, true> IntArray;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000113
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000114// Takes 178th time of logf on Z600 / VC2010
115int get_float_exp(float x) {
116 GR_STATIC_ASSERT(sizeof(int) == sizeof(float));
117#if GR_DEBUG
118 static bool tested;
119 if (!tested) {
120 tested = true;
121 GrAssert(get_float_exp(0.25f) == -2);
122 GrAssert(get_float_exp(0.3f) == -2);
123 GrAssert(get_float_exp(0.5f) == -1);
124 GrAssert(get_float_exp(1.f) == 0);
125 GrAssert(get_float_exp(2.f) == 1);
126 GrAssert(get_float_exp(2.5f) == 1);
127 GrAssert(get_float_exp(8.f) == 3);
128 GrAssert(get_float_exp(100.f) == 6);
129 GrAssert(get_float_exp(1000.f) == 9);
130 GrAssert(get_float_exp(1024.f) == 10);
131 GrAssert(get_float_exp(3000000.f) == 21);
132 }
133#endif
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000134 const int* iptr = (const int*)&x;
135 return (((*iptr) & 0x7f800000) >> 23) - 127;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000136}
137
138// we subdivide the quads to avoid huge overfill
139// if it returns -1 then should be drawn as lines
140int num_quad_subdivs(const SkPoint p[3]) {
141 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000142 static const SkScalar gDegenerateToLineTolSqd =
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000143 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000144
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000145 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
146 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000147 return -1;
148 }
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000149
150 GrScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
151 if (dsqd < gDegenerateToLineTolSqd) {
152 return -1;
153 }
154
155 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000156 return -1;
157 }
158
159 static const int kMaxSub = 4;
160 // tolerance of triangle height in pixels
161 // tuned on windows Quadro FX 380 / Z600
162 // trade off of fill vs cpu time on verts
163 // maybe different when do this using gpu (geo or tess shaders)
164 static const SkScalar gSubdivTol = 175 * SK_Scalar1;
165
robertphillips@google.com7460b372012-04-25 16:54:51 +0000166 if (dsqd <= SkScalarMul(gSubdivTol, gSubdivTol)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000167 return 0;
168 } else {
169 // subdividing the quad reduces d by 4. so we want x = log4(d/tol)
170 // = log4(d*d/tol*tol)/2
171 // = log2(d*d/tol*tol)
172
173#ifdef SK_SCALAR_IS_FLOAT
174 // +1 since we're ignoring the mantissa contribution.
175 int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
176 log = GrMin(GrMax(0, log), kMaxSub);
177 return log;
178#else
robertphillips@google.com7460b372012-04-25 16:54:51 +0000179 SkScalar log = SkScalarLog(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000180 SkScalarDiv(dsqd,
robertphillips@google.com7460b372012-04-25 16:54:51 +0000181 SkScalarMul(gSubdivTol, gSubdivTol)));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000182 static const SkScalar conv = SkScalarInvert(SkScalarLog(2));
183 log = SkScalarMul(log, conv);
184 return GrMin(GrMax(0, SkScalarCeilToInt(log)),kMaxSub);
185#endif
186 }
187}
188
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000189/**
190 * Generates the lines and quads to be rendered. Lines are always recorded in
191 * device space. We will do a device space bloat to account for the 1pixel
192 * thickness.
193 * Quads are recorded in device space unless m contains
194 * perspective, then in they are in src space. We do this because we will
195 * subdivide large quads to reduce over-fill. This subdivision has to be
196 * performed before applying the perspective matrix.
197 */
198int generate_lines_and_quads(const SkPath& path,
199 const SkMatrix& m,
200 const SkVector& translate,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000201 const GrIRect& devClipBounds,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000202 PtArray* lines,
203 PtArray* quads,
204 IntArray* quadSubdivCnts) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000205 SkPath::Iter iter(path, false);
206
207 int totalQuadCount = 0;
208 GrRect bounds;
209 GrIRect ibounds;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000210
211 bool persp = m.hasPerspective();
212
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000213 for (;;) {
214 GrPoint pts[4];
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000215 GrPoint devPts[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000216 GrPathCmd cmd = (GrPathCmd)iter.next(pts);
217 switch (cmd) {
218 case kMove_PathCmd:
219 break;
220 case kLine_PathCmd:
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000221 SkPoint::Offset(pts, 2, translate);
222 m.mapPoints(devPts, pts, 2);
223 bounds.setBounds(devPts, 2);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000224 bounds.outset(SK_Scalar1, SK_Scalar1);
225 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000226 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000227 SkPoint* pts = lines->push_back_n(2);
228 pts[0] = devPts[0];
229 pts[1] = devPts[1];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000230 }
231 break;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000232 case kQuadratic_PathCmd:
233 SkPoint::Offset(pts, 3, translate);
234 m.mapPoints(devPts, pts, 3);
235 bounds.setBounds(devPts, 3);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000236 bounds.outset(SK_Scalar1, SK_Scalar1);
237 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000238 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000239 int subdiv = num_quad_subdivs(devPts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000240 GrAssert(subdiv >= -1);
241 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000242 SkPoint* pts = lines->push_back_n(4);
243 pts[0] = devPts[0];
244 pts[1] = devPts[1];
245 pts[2] = devPts[1];
246 pts[3] = devPts[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000247 } else {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000248 // when in perspective keep quads in src space
249 SkPoint* qPts = persp ? pts : devPts;
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000250 SkPoint* pts = quads->push_back_n(3);
251 pts[0] = qPts[0];
252 pts[1] = qPts[1];
253 pts[2] = qPts[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000254 quadSubdivCnts->push_back() = subdiv;
255 totalQuadCount += 1 << subdiv;
256 }
257 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000258 break;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000259 case kCubic_PathCmd:
260 SkPoint::Offset(pts, 4, translate);
261 m.mapPoints(devPts, pts, 4);
262 bounds.setBounds(devPts, 4);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000263 bounds.outset(SK_Scalar1, SK_Scalar1);
264 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000265 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000266 PREALLOC_PTARRAY(32) q;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000267 // we don't need a direction if we aren't constraining the subdivision
268 static const SkPath::Direction kDummyDir = SkPath::kCCW_Direction;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000269 // We convert cubics to quadratics (for now).
270 // In perspective have to do conversion in src space.
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000271 if (persp) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000272 SkScalar tolScale =
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000273 GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m,
274 path.getBounds());
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000275 GrPathUtils::convertCubicToQuads(pts, tolScale, false, kDummyDir, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000276 } else {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000277 GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, false, kDummyDir, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000278 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000279 for (int i = 0; i < q.count(); i += 3) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000280 SkPoint* qInDevSpace;
281 // bounds has to be calculated in device space, but q is
282 // in src space when there is perspective.
283 if (persp) {
284 m.mapPoints(devPts, &q[i], 3);
285 bounds.setBounds(devPts, 3);
286 qInDevSpace = devPts;
287 } else {
288 bounds.setBounds(&q[i], 3);
289 qInDevSpace = &q[i];
290 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000291 bounds.outset(SK_Scalar1, SK_Scalar1);
292 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000293 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000294 int subdiv = num_quad_subdivs(qInDevSpace);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000295 GrAssert(subdiv >= -1);
296 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000297 SkPoint* pts = lines->push_back_n(4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000298 // lines should always be in device coords
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000299 pts[0] = qInDevSpace[0];
300 pts[1] = qInDevSpace[1];
301 pts[2] = qInDevSpace[1];
302 pts[3] = qInDevSpace[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000303 } else {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000304 SkPoint* pts = quads->push_back_n(3);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000305 // q is already in src space when there is no
306 // perspective and dev coords otherwise.
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000307 pts[0] = q[0 + i];
308 pts[1] = q[1 + i];
309 pts[2] = q[2 + i];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000310 quadSubdivCnts->push_back() = subdiv;
311 totalQuadCount += 1 << subdiv;
312 }
313 }
314 }
315 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000316 break;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000317 case kClose_PathCmd:
318 break;
319 case kEnd_PathCmd:
320 return totalQuadCount;
321 }
322 }
323}
324
325struct Vertex {
326 GrPoint fPos;
327 union {
328 struct {
329 GrScalar fA;
330 GrScalar fB;
331 GrScalar fC;
332 } fLine;
333 GrVec fQuadCoord;
334 struct {
335 GrScalar fBogus[4];
336 };
337 };
338};
339GR_STATIC_ASSERT(sizeof(Vertex) == 3 * sizeof(GrPoint));
340
341void intersect_lines(const SkPoint& ptA, const SkVector& normA,
342 const SkPoint& ptB, const SkVector& normB,
343 SkPoint* result) {
344
345 SkScalar lineAW = -normA.dot(ptA);
346 SkScalar lineBW = -normB.dot(ptB);
347
348 SkScalar wInv = SkScalarMul(normA.fX, normB.fY) -
349 SkScalarMul(normA.fY, normB.fX);
350 wInv = SkScalarInvert(wInv);
351
352 result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY);
353 result->fX = SkScalarMul(result->fX, wInv);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000354
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000355 result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW);
356 result->fY = SkScalarMul(result->fY, wInv);
357}
358
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000359void bloat_quad(const SkPoint qpts[3], const GrMatrix* toDevice,
360 const GrMatrix* toSrc, Vertex verts[kVertsPerQuad]) {
361 GrAssert(!toDevice == !toSrc);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000362 // original quad is specified by tri a,b,c
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000363 SkPoint a = qpts[0];
364 SkPoint b = qpts[1];
365 SkPoint c = qpts[2];
366
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000367 // this should be in the src space, not dev coords, when we have perspective
bsalomon@google.com19713172012-03-15 13:51:08 +0000368 GrPathUtils::QuadUVMatrix DevToUV(qpts);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000369
370 if (toDevice) {
371 toDevice->mapPoints(&a, 1);
372 toDevice->mapPoints(&b, 1);
373 toDevice->mapPoints(&c, 1);
374 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000375 // make a new poly where we replace a and c by a 1-pixel wide edges orthog
376 // to edges ab and bc:
377 //
378 // before | after
379 // | b0
380 // b |
381 // |
382 // | a0 c0
383 // a c | a1 c1
384 //
385 // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c,
386 // respectively.
387 Vertex& a0 = verts[0];
388 Vertex& a1 = verts[1];
389 Vertex& b0 = verts[2];
390 Vertex& c0 = verts[3];
391 Vertex& c1 = verts[4];
392
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000393 SkVector ab = b;
394 ab -= a;
395 SkVector ac = c;
396 ac -= a;
397 SkVector cb = b;
398 cb -= c;
399
400 // We should have already handled degenerates
401 GrAssert(ab.length() > 0 && cb.length() > 0);
402
403 ab.normalize();
404 SkVector abN;
405 abN.setOrthog(ab, SkVector::kLeft_Side);
406 if (abN.dot(ac) > 0) {
407 abN.negate();
408 }
409
410 cb.normalize();
411 SkVector cbN;
412 cbN.setOrthog(cb, SkVector::kLeft_Side);
413 if (cbN.dot(ac) < 0) {
414 cbN.negate();
415 }
416
417 a0.fPos = a;
418 a0.fPos += abN;
419 a1.fPos = a;
420 a1.fPos -= abN;
421
422 c0.fPos = c;
423 c0.fPos += cbN;
424 c1.fPos = c;
425 c1.fPos -= cbN;
426
427 intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos);
428
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000429 if (toSrc) {
430 toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(Vertex), kVertsPerQuad);
431 }
bsalomon@google.com19713172012-03-15 13:51:08 +0000432 DevToUV.apply<kVertsPerQuad, sizeof(Vertex), sizeof(GrPoint)>(verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000433}
434
435void add_quads(const SkPoint p[3],
436 int subdiv,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000437 const GrMatrix* toDevice,
438 const GrMatrix* toSrc,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000439 Vertex** vert) {
440 GrAssert(subdiv >= 0);
441 if (subdiv) {
442 SkPoint newP[5];
443 SkChopQuadAtHalf(p, newP);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000444 add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert);
445 add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000446 } else {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000447 bloat_quad(p, toDevice, toSrc, *vert);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000448 *vert += kVertsPerQuad;
449 }
450}
451
452void add_line(const SkPoint p[2],
453 int rtHeight,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000454 const SkMatrix* toSrc,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000455 Vertex** vert) {
456 const SkPoint& a = p[0];
457 const SkPoint& b = p[1];
458
459 SkVector orthVec = b;
460 orthVec -= a;
461
462 if (orthVec.setLength(SK_Scalar1)) {
463 orthVec.setOrthog(orthVec);
464
465 // the values we pass down to the frag shader
466 // have to be in y-points-up space;
467 SkVector normal;
468 normal.fX = orthVec.fX;
469 normal.fY = -orthVec.fY;
470 SkPoint aYDown;
471 aYDown.fX = a.fX;
472 aYDown.fY = rtHeight - a.fY;
473
474 SkScalar lineC = -(aYDown.dot(normal));
475 for (int i = 0; i < kVertsPerLineSeg; ++i) {
476 (*vert)[i].fPos = (i < 2) ? a : b;
477 if (0 == i || 3 == i) {
478 (*vert)[i].fPos -= orthVec;
479 } else {
480 (*vert)[i].fPos += orthVec;
481 }
482 (*vert)[i].fLine.fA = normal.fX;
483 (*vert)[i].fLine.fB = normal.fY;
484 (*vert)[i].fLine.fC = lineC;
485 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000486 if (NULL != toSrc) {
487 toSrc->mapPointsWithStride(&(*vert)->fPos,
488 sizeof(Vertex),
489 kVertsPerLineSeg);
490 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000491 } else {
492 // just make it degenerate and likely offscreen
493 (*vert)[0].fPos.set(SK_ScalarMax, SK_ScalarMax);
494 (*vert)[1].fPos.set(SK_ScalarMax, SK_ScalarMax);
495 (*vert)[2].fPos.set(SK_ScalarMax, SK_ScalarMax);
496 (*vert)[3].fPos.set(SK_ScalarMax, SK_ScalarMax);
497 }
498
499 *vert += kVertsPerLineSeg;
500}
501
502}
503
bsalomon@google.comb3729422012-03-07 19:13:28 +0000504bool GrAAHairLinePathRenderer::createGeom(
505 const SkPath& path,
506 const GrVec* translate,
507 GrDrawTarget* target,
bsalomon@google.comb3729422012-03-07 19:13:28 +0000508 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
robertphillips@google.com7b112892012-07-31 15:18:21 +0000514 GrIRect devClipBounds;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000515 target->getClip()->getConservativeBounds(drawState.getRenderTarget(),
robertphillips@google.com7b112892012-07-31 15:18:21 +0000516 &devClipBounds);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000517
518 GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit;
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000519 GrMatrix viewM = drawState.getViewMatrix();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000520
bsalomon@google.com92669012011-09-27 19:10:05 +0000521 PREALLOC_PTARRAY(128) lines;
522 PREALLOC_PTARRAY(128) quads;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000523 IntArray qSubdivs;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000524 static const GrVec gZeroVec = {0, 0};
525 if (NULL == translate) {
526 translate = &gZeroVec;
527 }
robertphillips@google.com7b112892012-07-31 15:18:21 +0000528 *quadCnt = generate_lines_and_quads(path, viewM, *translate, devClipBounds,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000529 &lines, &quads, &qSubdivs);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000530
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000531 *lineCnt = lines.count() / 2;
532 int vertCnt = kVertsPerLineSeg * *lineCnt + kVertsPerQuad * *quadCnt;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000533
534 GrAssert(sizeof(Vertex) == GrDrawTarget::VertexSize(layout));
535
bsalomon@google.comb3729422012-03-07 19:13:28 +0000536 if (!arg->set(target, layout, vertCnt, 0)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000537 return false;
538 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000539
bsalomon@google.comb3729422012-03-07 19:13:28 +0000540 Vertex* verts = reinterpret_cast<Vertex*>(arg->vertices());
541
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000542 const GrMatrix* toDevice = NULL;
543 const GrMatrix* toSrc = NULL;
544 GrMatrix ivm;
545
546 if (viewM.hasPerspective()) {
547 if (viewM.invert(&ivm)) {
548 toDevice = &viewM;
549 toSrc = &ivm;
550 }
551 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000552
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000553 for (int i = 0; i < *lineCnt; ++i) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000554 add_line(&lines[2*i], rtHeight, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000555 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000556
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000557 int unsubdivQuadCnt = quads.count() / 3;
558 for (int i = 0; i < unsubdivQuadCnt; ++i) {
559 GrAssert(qSubdivs[i] >= 0);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000560 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000561 }
562
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000563 return true;
564}
565
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000566bool GrAAHairLinePathRenderer::canDrawPath(const SkPath& path,
567 GrPathFill fill,
568 const GrDrawTarget* target,
569 bool antiAlias) const {
bsalomon@google.com47059542012-06-06 20:51:20 +0000570 if (fill != kHairLine_GrPathFill || !antiAlias) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000571 return false;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000572 }
573
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000574 static const uint32_t gReqDerivMask = SkPath::kCubic_SegmentMask |
575 SkPath::kQuad_SegmentMask;
576 if (!target->getCaps().fShaderDerivativeSupport &&
577 (gReqDerivMask & path.getSegmentMasks())) {
578 return false;
579 }
580 return true;
581}
582
583bool GrAAHairLinePathRenderer::onDrawPath(const SkPath& path,
584 GrPathFill fill,
585 const GrVec* translate,
586 GrDrawTarget* target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000587 bool antiAlias) {
588
589 int lineCnt;
590 int quadCnt;
bsalomon@google.comb3729422012-03-07 19:13:28 +0000591 GrDrawTarget::AutoReleaseGeometry arg;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000592 if (!this->createGeom(path,
593 translate,
594 target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000595 &lineCnt,
bsalomon@google.comb3729422012-03-07 19:13:28 +0000596 &quadCnt,
597 &arg)) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000598 return false;
599 }
600
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000601 GrDrawTarget::AutoStateRestore asr;
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000602 GrDrawState* drawState = target->drawState();
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000603 if (!drawState->getViewMatrix().hasPerspective()) {
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000604 // we are going to whack the view matrix to identity to remove
605 // perspective.
606 asr.set(target,
607 GrDrawTarget::kPreserve_ASRInit);
608 drawState = target->drawState();
bsalomon@google.come3d32162012-07-20 13:37:06 +0000609 if (!drawState->preConcatSamplerMatricesWithInverse(drawState->getViewMatrix())) {
610 return false;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000611 }
bsalomon@google.comb3e40c02012-03-20 15:36:32 +0000612 drawState->viewMatrix()->reset();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000613 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000614
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000615
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000616 // TODO: See whether rendering lines as degenerate quads improves perf
617 // when we have a mix
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000618 target->setIndexSourceToBuffer(fLinesIndexBuffer);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000619 int lines = 0;
620 int nBufLines = fLinesIndexBuffer->maxQuads();
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000621 while (lines < lineCnt) {
622 int n = GrMin(lineCnt - lines, nBufLines);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000623 drawState->setVertexEdgeType(GrDrawState::kHairLine_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +0000624 target->drawIndexed(kTriangles_GrPrimitiveType,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000625 kVertsPerLineSeg*lines, // startV
626 0, // startI
627 kVertsPerLineSeg*n, // vCount
628 kIdxsPerLineSeg*n); // iCount
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000629 lines += n;
630 }
631
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000632 target->setIndexSourceToBuffer(fQuadsIndexBuffer);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000633 int quads = 0;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000634 while (quads < quadCnt) {
635 int n = GrMin(quadCnt - quads, kNumQuadsInIdxBuffer);
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000636 drawState->setVertexEdgeType(GrDrawState::kHairQuad_EdgeType);
bsalomon@google.com47059542012-06-06 20:51:20 +0000637 target->drawIndexed(kTriangles_GrPrimitiveType,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000638 4 * lineCnt + kVertsPerQuad*quads, // startV
639 0, // startI
640 kVertsPerQuad*n, // vCount
641 kIdxsPerQuad*n); // iCount
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000642 quads += n;
643 }
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000644 return true;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000645}
646