blob: 80304f822efd1f11e4bbe48571e58b45b9e17e82 [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.comc26d94f2013-03-25 18:19:00 +000013#include "GrDrawTargetCaps.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000014#include "GrGpu.h"
15#include "GrIndexBuffer.h"
bsalomon@google.comdbeeac32011-09-12 14:59:34 +000016#include "GrPathUtils.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000017#include "SkGeometry.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000018#include "SkStroke.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000019#include "SkTemplates.h"
20
21namespace {
22// quadratics are rendered as 5-sided polys in order to bound the
23// AA stroke around the center-curve. See comments in push_quad_index_buffer and
24// bloat_quad.
25static const int kVertsPerQuad = 5;
26static const int kIdxsPerQuad = 9;
27
28static const int kVertsPerLineSeg = 4;
29static const int kIdxsPerLineSeg = 6;
30
31static const int kNumQuadsInIdxBuffer = 256;
32static const size_t kQuadIdxSBufize = kIdxsPerQuad *
33 sizeof(uint16_t) *
34 kNumQuadsInIdxBuffer;
35
36bool push_quad_index_data(GrIndexBuffer* qIdxBuffer) {
37 uint16_t* data = (uint16_t*) qIdxBuffer->lock();
38 bool tempData = NULL == data;
39 if (tempData) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000040 data = SkNEW_ARRAY(uint16_t, kNumQuadsInIdxBuffer * kIdxsPerQuad);
bsalomon@google.comaeb21602011-08-30 18:13:44 +000041 }
42 for (int i = 0; i < kNumQuadsInIdxBuffer; ++i) {
43
44 // Each quadratic is rendered as a five sided polygon. This poly bounds
45 // the quadratic's bounding triangle but has been expanded so that the
46 // 1-pixel wide area around the curve is inside the poly.
47 // If a,b,c are the original control points then the poly a0,b0,c0,c1,a1
48 // that is rendered would look like this:
49 // b0
50 // b
51 //
52 // a0 c0
53 // a c
54 // a1 c1
bsalomon@google.com0e5104c2012-04-10 16:20:41 +000055 // Each is drawn as three triangles specified by these 9 indices:
bsalomon@google.comaeb21602011-08-30 18:13:44 +000056 int baseIdx = i * kIdxsPerQuad;
57 uint16_t baseVert = (uint16_t)(i * kVertsPerQuad);
58 data[0 + baseIdx] = baseVert + 0; // a0
59 data[1 + baseIdx] = baseVert + 1; // a1
60 data[2 + baseIdx] = baseVert + 2; // b0
61 data[3 + baseIdx] = baseVert + 2; // b0
62 data[4 + baseIdx] = baseVert + 4; // c1
63 data[5 + baseIdx] = baseVert + 3; // c0
64 data[6 + baseIdx] = baseVert + 1; // a1
65 data[7 + baseIdx] = baseVert + 4; // c1
66 data[8 + baseIdx] = baseVert + 2; // b0
67 }
68 if (tempData) {
69 bool ret = qIdxBuffer->updateData(data, kQuadIdxSBufize);
70 delete[] data;
71 return ret;
72 } else {
73 qIdxBuffer->unlock();
74 return true;
75 }
76}
77}
78
79GrPathRenderer* GrAAHairLinePathRenderer::Create(GrContext* context) {
bsalomon@google.coma8a6a322011-09-23 14:19:58 +000080 const GrIndexBuffer* lIdxBuffer = context->getQuadIndexBuffer();
81 if (NULL == lIdxBuffer) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +000082 return NULL;
83 }
bsalomon@google.coma8a6a322011-09-23 14:19:58 +000084 GrGpu* gpu = context->getGpu();
85 GrIndexBuffer* qIdxBuf = gpu->createIndexBuffer(kQuadIdxSBufize, false);
86 SkAutoTUnref<GrIndexBuffer> qIdxBuffer(qIdxBuf);
87 if (NULL == qIdxBuf ||
88 !push_quad_index_data(qIdxBuf)) {
89 return NULL;
90 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +000091 return SkNEW_ARGS(GrAAHairLinePathRenderer,
92 (context, lIdxBuffer, qIdxBuf));
bsalomon@google.comaeb21602011-08-30 18:13:44 +000093}
94
95GrAAHairLinePathRenderer::GrAAHairLinePathRenderer(
96 const GrContext* context,
97 const GrIndexBuffer* linesIndexBuffer,
98 const GrIndexBuffer* quadsIndexBuffer) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +000099 fLinesIndexBuffer = linesIndexBuffer;
100 linesIndexBuffer->ref();
101 fQuadsIndexBuffer = quadsIndexBuffer;
102 quadsIndexBuffer->ref();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000103}
104
105GrAAHairLinePathRenderer::~GrAAHairLinePathRenderer() {
106 fLinesIndexBuffer->unref();
107 fQuadsIndexBuffer->unref();
108}
109
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000110namespace {
111
bsalomon@google.com49313f62011-09-14 13:54:05 +0000112typedef SkTArray<SkPoint, true> PtArray;
bsalomon@google.com92669012011-09-27 19:10:05 +0000113#define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true>
bsalomon@google.com49313f62011-09-14 13:54:05 +0000114typedef SkTArray<int, true> IntArray;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000115
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000116// Takes 178th time of logf on Z600 / VC2010
117int get_float_exp(float x) {
118 GR_STATIC_ASSERT(sizeof(int) == sizeof(float));
119#if GR_DEBUG
120 static bool tested;
121 if (!tested) {
122 tested = true;
123 GrAssert(get_float_exp(0.25f) == -2);
124 GrAssert(get_float_exp(0.3f) == -2);
125 GrAssert(get_float_exp(0.5f) == -1);
126 GrAssert(get_float_exp(1.f) == 0);
127 GrAssert(get_float_exp(2.f) == 1);
128 GrAssert(get_float_exp(2.5f) == 1);
129 GrAssert(get_float_exp(8.f) == 3);
130 GrAssert(get_float_exp(100.f) == 6);
131 GrAssert(get_float_exp(1000.f) == 9);
132 GrAssert(get_float_exp(1024.f) == 10);
133 GrAssert(get_float_exp(3000000.f) == 21);
134 }
135#endif
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000136 const int* iptr = (const int*)&x;
137 return (((*iptr) & 0x7f800000) >> 23) - 127;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000138}
139
140// we subdivide the quads to avoid huge overfill
141// if it returns -1 then should be drawn as lines
142int num_quad_subdivs(const SkPoint p[3]) {
143 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000144 static const SkScalar gDegenerateToLineTolSqd =
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000145 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000146
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000147 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
148 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000149 return -1;
150 }
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000151
bsalomon@google.com81712882012-11-01 17:12:34 +0000152 SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000153 if (dsqd < gDegenerateToLineTolSqd) {
154 return -1;
155 }
156
157 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000158 return -1;
159 }
160
161 static const int kMaxSub = 4;
162 // tolerance of triangle height in pixels
163 // tuned on windows Quadro FX 380 / Z600
164 // trade off of fill vs cpu time on verts
165 // maybe different when do this using gpu (geo or tess shaders)
166 static const SkScalar gSubdivTol = 175 * SK_Scalar1;
167
robertphillips@google.com7460b372012-04-25 16:54:51 +0000168 if (dsqd <= SkScalarMul(gSubdivTol, gSubdivTol)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000169 return 0;
170 } else {
171 // subdividing the quad reduces d by 4. so we want x = log4(d/tol)
172 // = log4(d*d/tol*tol)/2
173 // = log2(d*d/tol*tol)
174
175#ifdef SK_SCALAR_IS_FLOAT
176 // +1 since we're ignoring the mantissa contribution.
177 int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
178 log = GrMin(GrMax(0, log), kMaxSub);
179 return log;
180#else
robertphillips@google.com7460b372012-04-25 16:54:51 +0000181 SkScalar log = SkScalarLog(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000182 SkScalarDiv(dsqd,
robertphillips@google.com7460b372012-04-25 16:54:51 +0000183 SkScalarMul(gSubdivTol, gSubdivTol)));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000184 static const SkScalar conv = SkScalarInvert(SkScalarLog(2));
185 log = SkScalarMul(log, conv);
186 return GrMin(GrMax(0, SkScalarCeilToInt(log)),kMaxSub);
187#endif
188 }
189}
190
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000191/**
192 * Generates the lines and quads to be rendered. Lines are always recorded in
193 * device space. We will do a device space bloat to account for the 1pixel
194 * thickness.
195 * Quads are recorded in device space unless m contains
196 * perspective, then in they are in src space. We do this because we will
197 * subdivide large quads to reduce over-fill. This subdivision has to be
198 * performed before applying the perspective matrix.
199 */
200int generate_lines_and_quads(const SkPath& path,
201 const SkMatrix& m,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000202 const GrIRect& devClipBounds,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000203 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 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:
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000233 m.mapPoints(devPts, pts, 3);
234 bounds.setBounds(devPts, 3);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000235 bounds.outset(SK_Scalar1, SK_Scalar1);
236 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000237 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000238 int subdiv = num_quad_subdivs(devPts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000239 GrAssert(subdiv >= -1);
240 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000241 SkPoint* pts = lines->push_back_n(4);
242 pts[0] = devPts[0];
243 pts[1] = devPts[1];
244 pts[2] = devPts[1];
245 pts[3] = devPts[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000246 } else {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000247 // when in perspective keep quads in src space
248 SkPoint* qPts = persp ? pts : devPts;
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000249 SkPoint* pts = quads->push_back_n(3);
250 pts[0] = qPts[0];
251 pts[1] = qPts[1];
252 pts[2] = qPts[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000253 quadSubdivCnts->push_back() = subdiv;
254 totalQuadCount += 1 << subdiv;
255 }
256 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000257 break;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000258 case kCubic_PathCmd:
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000259 m.mapPoints(devPts, pts, 4);
260 bounds.setBounds(devPts, 4);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000261 bounds.outset(SK_Scalar1, SK_Scalar1);
262 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000263 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000264 PREALLOC_PTARRAY(32) q;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000265 // we don't need a direction if we aren't constraining the subdivision
266 static const SkPath::Direction kDummyDir = SkPath::kCCW_Direction;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000267 // We convert cubics to quadratics (for now).
268 // In perspective have to do conversion in src space.
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000269 if (persp) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000270 SkScalar tolScale =
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000271 GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m,
272 path.getBounds());
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000273 GrPathUtils::convertCubicToQuads(pts, tolScale, false, kDummyDir, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000274 } else {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000275 GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, false, kDummyDir, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000276 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000277 for (int i = 0; i < q.count(); i += 3) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000278 SkPoint* qInDevSpace;
279 // bounds has to be calculated in device space, but q is
280 // in src space when there is perspective.
281 if (persp) {
282 m.mapPoints(devPts, &q[i], 3);
283 bounds.setBounds(devPts, 3);
284 qInDevSpace = devPts;
285 } else {
286 bounds.setBounds(&q[i], 3);
287 qInDevSpace = &q[i];
288 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000289 bounds.outset(SK_Scalar1, SK_Scalar1);
290 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000291 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000292 int subdiv = num_quad_subdivs(qInDevSpace);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000293 GrAssert(subdiv >= -1);
294 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000295 SkPoint* pts = lines->push_back_n(4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000296 // lines should always be in device coords
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000297 pts[0] = qInDevSpace[0];
298 pts[1] = qInDevSpace[1];
299 pts[2] = qInDevSpace[1];
300 pts[3] = qInDevSpace[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000301 } else {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000302 SkPoint* pts = quads->push_back_n(3);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000303 // q is already in src space when there is no
304 // perspective and dev coords otherwise.
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000305 pts[0] = q[0 + i];
306 pts[1] = q[1 + i];
307 pts[2] = q[2 + i];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000308 quadSubdivCnts->push_back() = subdiv;
309 totalQuadCount += 1 << subdiv;
310 }
311 }
312 }
313 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000314 break;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000315 case kClose_PathCmd:
316 break;
317 case kEnd_PathCmd:
318 return totalQuadCount;
319 }
320 }
321}
322
323struct Vertex {
324 GrPoint fPos;
325 union {
326 struct {
bsalomon@google.com81712882012-11-01 17:12:34 +0000327 SkScalar fA;
328 SkScalar fB;
329 SkScalar fC;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000330 } fLine;
331 GrVec fQuadCoord;
332 struct {
bsalomon@google.com81712882012-11-01 17:12:34 +0000333 SkScalar fBogus[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000334 };
335 };
336};
337GR_STATIC_ASSERT(sizeof(Vertex) == 3 * sizeof(GrPoint));
338
339void intersect_lines(const SkPoint& ptA, const SkVector& normA,
340 const SkPoint& ptB, const SkVector& normB,
341 SkPoint* result) {
342
343 SkScalar lineAW = -normA.dot(ptA);
344 SkScalar lineBW = -normB.dot(ptB);
345
346 SkScalar wInv = SkScalarMul(normA.fX, normB.fY) -
347 SkScalarMul(normA.fY, normB.fX);
348 wInv = SkScalarInvert(wInv);
349
350 result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY);
351 result->fX = SkScalarMul(result->fX, wInv);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000352
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000353 result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW);
354 result->fY = SkScalarMul(result->fY, wInv);
355}
356
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000357void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice,
358 const SkMatrix* toSrc, Vertex verts[kVertsPerQuad]) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000359 GrAssert(!toDevice == !toSrc);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000360 // original quad is specified by tri a,b,c
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000361 SkPoint a = qpts[0];
362 SkPoint b = qpts[1];
363 SkPoint c = qpts[2];
364
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000365 // this should be in the src space, not dev coords, when we have perspective
bsalomon@google.com19713172012-03-15 13:51:08 +0000366 GrPathUtils::QuadUVMatrix DevToUV(qpts);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000367
368 if (toDevice) {
369 toDevice->mapPoints(&a, 1);
370 toDevice->mapPoints(&b, 1);
371 toDevice->mapPoints(&c, 1);
372 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000373 // make a new poly where we replace a and c by a 1-pixel wide edges orthog
374 // to edges ab and bc:
375 //
376 // before | after
377 // | b0
378 // b |
379 // |
380 // | a0 c0
381 // a c | a1 c1
382 //
383 // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c,
384 // respectively.
385 Vertex& a0 = verts[0];
386 Vertex& a1 = verts[1];
387 Vertex& b0 = verts[2];
388 Vertex& c0 = verts[3];
389 Vertex& c1 = verts[4];
390
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000391 SkVector ab = b;
392 ab -= a;
393 SkVector ac = c;
394 ac -= a;
395 SkVector cb = b;
396 cb -= c;
397
398 // We should have already handled degenerates
399 GrAssert(ab.length() > 0 && cb.length() > 0);
400
401 ab.normalize();
402 SkVector abN;
403 abN.setOrthog(ab, SkVector::kLeft_Side);
404 if (abN.dot(ac) > 0) {
405 abN.negate();
406 }
407
408 cb.normalize();
409 SkVector cbN;
410 cbN.setOrthog(cb, SkVector::kLeft_Side);
411 if (cbN.dot(ac) < 0) {
412 cbN.negate();
413 }
414
415 a0.fPos = a;
416 a0.fPos += abN;
417 a1.fPos = a;
418 a1.fPos -= abN;
419
420 c0.fPos = c;
421 c0.fPos += cbN;
422 c1.fPos = c;
423 c1.fPos -= cbN;
424
425 intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos);
426
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000427 if (toSrc) {
428 toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(Vertex), kVertsPerQuad);
429 }
bsalomon@google.com19713172012-03-15 13:51:08 +0000430 DevToUV.apply<kVertsPerQuad, sizeof(Vertex), sizeof(GrPoint)>(verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000431}
432
433void add_quads(const SkPoint p[3],
434 int subdiv,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000435 const SkMatrix* toDevice,
436 const SkMatrix* toSrc,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000437 Vertex** vert) {
438 GrAssert(subdiv >= 0);
439 if (subdiv) {
440 SkPoint newP[5];
441 SkChopQuadAtHalf(p, newP);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000442 add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert);
443 add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000444 } else {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000445 bloat_quad(p, toDevice, toSrc, *vert);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000446 *vert += kVertsPerQuad;
447 }
448}
449
450void add_line(const SkPoint p[2],
451 int rtHeight,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000452 const SkMatrix* toSrc,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000453 Vertex** vert) {
454 const SkPoint& a = p[0];
455 const SkPoint& b = p[1];
456
457 SkVector orthVec = b;
458 orthVec -= a;
459
460 if (orthVec.setLength(SK_Scalar1)) {
461 orthVec.setOrthog(orthVec);
462
bsalomon@google.com706f6682012-10-23 14:53:55 +0000463 SkScalar lineC = -(a.dot(orthVec));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000464 for (int i = 0; i < kVertsPerLineSeg; ++i) {
465 (*vert)[i].fPos = (i < 2) ? a : b;
466 if (0 == i || 3 == i) {
467 (*vert)[i].fPos -= orthVec;
468 } else {
469 (*vert)[i].fPos += orthVec;
470 }
bsalomon@google.com706f6682012-10-23 14:53:55 +0000471 (*vert)[i].fLine.fA = orthVec.fX;
472 (*vert)[i].fLine.fB = orthVec.fY;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000473 (*vert)[i].fLine.fC = lineC;
474 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000475 if (NULL != toSrc) {
476 toSrc->mapPointsWithStride(&(*vert)->fPos,
477 sizeof(Vertex),
478 kVertsPerLineSeg);
479 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000480 } else {
481 // just make it degenerate and likely offscreen
482 (*vert)[0].fPos.set(SK_ScalarMax, SK_ScalarMax);
483 (*vert)[1].fPos.set(SK_ScalarMax, SK_ScalarMax);
484 (*vert)[2].fPos.set(SK_ScalarMax, SK_ScalarMax);
485 (*vert)[3].fPos.set(SK_ScalarMax, SK_ScalarMax);
486 }
487
488 *vert += kVertsPerLineSeg;
489}
490
491}
492
bsalomon@google.comb3729422012-03-07 19:13:28 +0000493bool GrAAHairLinePathRenderer::createGeom(
494 const SkPath& path,
bsalomon@google.comb3729422012-03-07 19:13:28 +0000495 GrDrawTarget* target,
bsalomon@google.comb3729422012-03-07 19:13:28 +0000496 int* lineCnt,
497 int* quadCnt,
498 GrDrawTarget::AutoReleaseGeometry* arg) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000499 GrDrawState* drawState = target->drawState();
500 int rtHeight = drawState->getRenderTarget()->height();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000501
robertphillips@google.com7b112892012-07-31 15:18:21 +0000502 GrIRect devClipBounds;
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000503 target->getClip()->getConservativeBounds(drawState->getRenderTarget(),
robertphillips@google.com7b112892012-07-31 15:18:21 +0000504 &devClipBounds);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000505
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000506 // position + edge
507 static const GrVertexAttrib kAttribs[] = {
jvanverth@google.com3b0d6312013-03-01 20:30:01 +0000508 {kVec2f_GrVertexAttribType, 0},
509 {kVec4f_GrVertexAttribType, sizeof(GrPoint)}
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000510 };
511 static const GrAttribBindings kBindings = GrDrawState::kEdge_AttribBindingsBit;
512 SkMatrix viewM = drawState->getViewMatrix();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000513
bsalomon@google.com92669012011-09-27 19:10:05 +0000514 PREALLOC_PTARRAY(128) lines;
515 PREALLOC_PTARRAY(128) quads;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000516 IntArray qSubdivs;
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000517 *quadCnt = generate_lines_and_quads(path, viewM, devClipBounds,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000518 &lines, &quads, &qSubdivs);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000519
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000520 *lineCnt = lines.count() / 2;
521 int vertCnt = kVertsPerLineSeg * *lineCnt + kVertsPerQuad * *quadCnt;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000522
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000523 target->drawState()->setVertexAttribs(kAttribs, SK_ARRAY_COUNT(kAttribs));
524 target->drawState()->setAttribIndex(GrDrawState::kPosition_AttribIndex, 0);
525 target->drawState()->setAttribIndex(GrDrawState::kEdge_AttribIndex, 1);
526 target->drawState()->setAttribBindings(kBindings);
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000527 GrAssert(sizeof(Vertex) == target->getDrawState().getVertexSize());
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000528
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000529 if (!arg->set(target, vertCnt, 0)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000530 return false;
531 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000532
bsalomon@google.comb3729422012-03-07 19:13:28 +0000533 Vertex* verts = reinterpret_cast<Vertex*>(arg->vertices());
534
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000535 const SkMatrix* toDevice = NULL;
536 const SkMatrix* toSrc = NULL;
537 SkMatrix ivm;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000538
539 if (viewM.hasPerspective()) {
540 if (viewM.invert(&ivm)) {
541 toDevice = &viewM;
542 toSrc = &ivm;
543 }
544 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000545
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000546 for (int i = 0; i < *lineCnt; ++i) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000547 add_line(&lines[2*i], rtHeight, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000548 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000549
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000550 int unsubdivQuadCnt = quads.count() / 3;
551 for (int i = 0; i < unsubdivQuadCnt; ++i) {
552 GrAssert(qSubdivs[i] >= 0);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000553 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000554 }
555
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000556 return true;
557}
558
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000559bool GrAAHairLinePathRenderer::canDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000560 const SkStrokeRec& stroke,
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000561 const GrDrawTarget* target,
562 bool antiAlias) const {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000563 if (!stroke.isHairlineStyle() || !antiAlias) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000564 return false;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000565 }
566
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000567 static const uint32_t gReqDerivMask = SkPath::kCubic_SegmentMask |
568 SkPath::kQuad_SegmentMask;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000569 if (!target->caps()->shaderDerivativeSupport() &&
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000570 (gReqDerivMask & path.getSegmentMasks())) {
571 return false;
572 }
573 return true;
574}
575
576bool GrAAHairLinePathRenderer::onDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000577 const SkStrokeRec&,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000578 GrDrawTarget* target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000579 bool antiAlias) {
580
581 int lineCnt;
582 int quadCnt;
bsalomon@google.comb3729422012-03-07 19:13:28 +0000583 GrDrawTarget::AutoReleaseGeometry arg;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000584 if (!this->createGeom(path,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000585 target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000586 &lineCnt,
bsalomon@google.comb3729422012-03-07 19:13:28 +0000587 &quadCnt,
588 &arg)) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000589 return false;
590 }
591
bsalomon@google.coma8347462012-10-08 18:59:39 +0000592 GrDrawState::AutoDeviceCoordDraw adcd;
bsalomon@google.com873ea0c2012-03-30 15:55:32 +0000593 GrDrawState* drawState = target->drawState();
bsalomon@google.coma8347462012-10-08 18:59:39 +0000594 // createGeom transforms the geometry to device space when the matrix does not have
595 // perspective.
bsalomon@google.com8f9cbd62011-12-09 15:55:34 +0000596 if (!drawState->getViewMatrix().hasPerspective()) {
bsalomon@google.coma8347462012-10-08 18:59:39 +0000597 adcd.set(drawState);
598 if (!adcd.succeeded()) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000599 return false;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000600 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000601 }
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000602
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000603 // TODO: See whether rendering lines as degenerate quads improves perf
604 // when we have a mix
bsalomon@google.coma8347462012-10-08 18:59:39 +0000605
606 GrDrawState::VertexEdgeType oldEdgeType = drawState->getVertexEdgeType();
607
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000608 target->setIndexSourceToBuffer(fLinesIndexBuffer);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000609 int lines = 0;
610 int nBufLines = fLinesIndexBuffer->maxQuads();
bsalomon@google.coma8347462012-10-08 18:59:39 +0000611 drawState->setVertexEdgeType(GrDrawState::kHairLine_EdgeType);
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000612 while (lines < lineCnt) {
613 int n = GrMin(lineCnt - lines, nBufLines);
bsalomon@google.com47059542012-06-06 20:51:20 +0000614 target->drawIndexed(kTriangles_GrPrimitiveType,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000615 kVertsPerLineSeg*lines, // startV
616 0, // startI
617 kVertsPerLineSeg*n, // vCount
618 kIdxsPerLineSeg*n); // iCount
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000619 lines += n;
620 }
621
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000622 target->setIndexSourceToBuffer(fQuadsIndexBuffer);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000623 int quads = 0;
bsalomon@google.coma8347462012-10-08 18:59:39 +0000624 drawState->setVertexEdgeType(GrDrawState::kHairQuad_EdgeType);
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000625 while (quads < quadCnt) {
626 int n = GrMin(quadCnt - quads, kNumQuadsInIdxBuffer);
bsalomon@google.com47059542012-06-06 20:51:20 +0000627 target->drawIndexed(kTriangles_GrPrimitiveType,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000628 4 * lineCnt + kVertsPerQuad*quads, // startV
629 0, // startI
630 kVertsPerQuad*n, // vCount
631 kIdxsPerQuad*n); // iCount
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000632 quads += n;
633 }
bsalomon@google.coma8347462012-10-08 18:59:39 +0000634 drawState->setVertexEdgeType(oldEdgeType);
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000635 return true;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000636}