blob: e4ee8a850da0f7acfc2b5e87bcaf4fb1c6408022 [file] [log] [blame]
bsalomon@google.comf75b84e2011-09-29 14:58:28 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
bsalomon@google.comaeb21602011-08-30 18:13:44 +00008#include "GrAAHairLinePathRenderer.h"
9
10#include "GrContext.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000011#include "GrDrawState.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000012#include "GrDrawTargetCaps.h"
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000013#include "GrEffect.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"
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000017#include "GrTBackendEffectFactory.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000018#include "SkGeometry.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000019#include "SkStroke.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000020#include "SkTemplates.h"
21
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000022#include "effects/GrBezierEffect.h"
bsalomon@google.com4647f902013-03-26 14:45:27 +000023
bsalomon@google.comaeb21602011-08-30 18:13:44 +000024namespace {
25// quadratics are rendered as 5-sided polys in order to bound the
26// AA stroke around the center-curve. See comments in push_quad_index_buffer and
egdaniel@google.com5383a752013-07-12 20:15:34 +000027// bloat_quad. Quadratics and conics share an index buffer
bsalomon@google.comaeb21602011-08-30 18:13:44 +000028static const int kVertsPerQuad = 5;
29static const int kIdxsPerQuad = 9;
30
jvanverth@google.com681ccf02013-08-16 14:51:51 +000031static const int kVertsPerLineSeg = 6;
32static const int kIdxsPerLineSeg = 12;
bsalomon@google.comaeb21602011-08-30 18:13:44 +000033
34static const int kNumQuadsInIdxBuffer = 256;
35static const size_t kQuadIdxSBufize = kIdxsPerQuad *
36 sizeof(uint16_t) *
37 kNumQuadsInIdxBuffer;
38
jvanverth@google.com681ccf02013-08-16 14:51:51 +000039static const int kNumLineSegsInIdxBuffer = 256;
40static const size_t kLineSegIdxSBufize = kIdxsPerLineSeg *
41 sizeof(uint16_t) *
42 kNumLineSegsInIdxBuffer;
43
44static bool push_quad_index_data(GrIndexBuffer* qIdxBuffer) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +000045 uint16_t* data = (uint16_t*) qIdxBuffer->lock();
46 bool tempData = NULL == data;
47 if (tempData) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000048 data = SkNEW_ARRAY(uint16_t, kNumQuadsInIdxBuffer * kIdxsPerQuad);
bsalomon@google.comaeb21602011-08-30 18:13:44 +000049 }
50 for (int i = 0; i < kNumQuadsInIdxBuffer; ++i) {
51
52 // Each quadratic is rendered as a five sided polygon. This poly bounds
53 // the quadratic's bounding triangle but has been expanded so that the
54 // 1-pixel wide area around the curve is inside the poly.
55 // If a,b,c are the original control points then the poly a0,b0,c0,c1,a1
56 // that is rendered would look like this:
57 // b0
58 // b
59 //
60 // a0 c0
61 // a c
62 // a1 c1
bsalomon@google.com0e5104c2012-04-10 16:20:41 +000063 // Each is drawn as three triangles specified by these 9 indices:
bsalomon@google.comaeb21602011-08-30 18:13:44 +000064 int baseIdx = i * kIdxsPerQuad;
65 uint16_t baseVert = (uint16_t)(i * kVertsPerQuad);
66 data[0 + baseIdx] = baseVert + 0; // a0
67 data[1 + baseIdx] = baseVert + 1; // a1
68 data[2 + baseIdx] = baseVert + 2; // b0
69 data[3 + baseIdx] = baseVert + 2; // b0
70 data[4 + baseIdx] = baseVert + 4; // c1
71 data[5 + baseIdx] = baseVert + 3; // c0
72 data[6 + baseIdx] = baseVert + 1; // a1
73 data[7 + baseIdx] = baseVert + 4; // c1
74 data[8 + baseIdx] = baseVert + 2; // b0
75 }
76 if (tempData) {
77 bool ret = qIdxBuffer->updateData(data, kQuadIdxSBufize);
78 delete[] data;
79 return ret;
80 } else {
81 qIdxBuffer->unlock();
82 return true;
83 }
84}
jvanverth@google.com681ccf02013-08-16 14:51:51 +000085
86static bool push_line_index_data(GrIndexBuffer* lIdxBuffer) {
87 uint16_t* data = (uint16_t*) lIdxBuffer->lock();
88 bool tempData = NULL == data;
89 if (tempData) {
90 data = SkNEW_ARRAY(uint16_t, kNumLineSegsInIdxBuffer * kIdxsPerLineSeg);
91 }
92 for (int i = 0; i < kNumLineSegsInIdxBuffer; ++i) {
93 // Each line segment is rendered as two quads, with alpha = 1 along the
94 // spine of the segment, and alpha = 0 along the outer edges, represented
95 // horizontally (i.e., the line equation is t*(p1-p0) + p0)
96 //
97 // p4 p5
98 // p0 p1
99 // p2 p3
100 //
101 // Each is drawn as four triangles specified by these 12 indices:
102 int baseIdx = i * kIdxsPerLineSeg;
103 uint16_t baseVert = (uint16_t)(i * kVertsPerLineSeg);
104 data[0 + baseIdx] = baseVert + 0; // p0
105 data[1 + baseIdx] = baseVert + 1; // p1
106 data[2 + baseIdx] = baseVert + 2; // p2
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000107
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000108 data[3 + baseIdx] = baseVert + 2; // p2
109 data[4 + baseIdx] = baseVert + 1; // p1
110 data[5 + baseIdx] = baseVert + 3; // p3
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000111
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000112 data[6 + baseIdx] = baseVert + 0; // p0
113 data[7 + baseIdx] = baseVert + 5; // p5
114 data[8 + baseIdx] = baseVert + 1; // p1
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000115
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000116 data[9 + baseIdx] = baseVert + 0; // p0
117 data[10+ baseIdx] = baseVert + 4; // p4
118 data[11+ baseIdx] = baseVert + 5; // p5
119 }
120 if (tempData) {
121 bool ret = lIdxBuffer->updateData(data, kLineSegIdxSBufize);
122 delete[] data;
123 return ret;
124 } else {
125 lIdxBuffer->unlock();
126 return true;
127 }
128}
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000129}
130
131GrPathRenderer* GrAAHairLinePathRenderer::Create(GrContext* context) {
bsalomon@google.coma8a6a322011-09-23 14:19:58 +0000132 GrGpu* gpu = context->getGpu();
133 GrIndexBuffer* qIdxBuf = gpu->createIndexBuffer(kQuadIdxSBufize, false);
134 SkAutoTUnref<GrIndexBuffer> qIdxBuffer(qIdxBuf);
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000135 if (NULL == qIdxBuf || !push_quad_index_data(qIdxBuf)) {
136 return NULL;
137 }
138 GrIndexBuffer* lIdxBuf = gpu->createIndexBuffer(kLineSegIdxSBufize, false);
139 SkAutoTUnref<GrIndexBuffer> lIdxBuffer(lIdxBuf);
140 if (NULL == lIdxBuf || !push_line_index_data(lIdxBuf)) {
bsalomon@google.coma8a6a322011-09-23 14:19:58 +0000141 return NULL;
142 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000143 return SkNEW_ARGS(GrAAHairLinePathRenderer,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000144 (context, lIdxBuf, qIdxBuf));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000145}
146
147GrAAHairLinePathRenderer::GrAAHairLinePathRenderer(
148 const GrContext* context,
149 const GrIndexBuffer* linesIndexBuffer,
150 const GrIndexBuffer* quadsIndexBuffer) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000151 fLinesIndexBuffer = linesIndexBuffer;
152 linesIndexBuffer->ref();
153 fQuadsIndexBuffer = quadsIndexBuffer;
154 quadsIndexBuffer->ref();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000155}
156
157GrAAHairLinePathRenderer::~GrAAHairLinePathRenderer() {
158 fLinesIndexBuffer->unref();
159 fQuadsIndexBuffer->unref();
160}
161
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000162namespace {
163
bsalomon@google.com92669012011-09-27 19:10:05 +0000164#define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true>
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000165
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000166// Takes 178th time of logf on Z600 / VC2010
167int get_float_exp(float x) {
168 GR_STATIC_ASSERT(sizeof(int) == sizeof(float));
169#if GR_DEBUG
170 static bool tested;
171 if (!tested) {
172 tested = true;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000173 SkASSERT(get_float_exp(0.25f) == -2);
174 SkASSERT(get_float_exp(0.3f) == -2);
175 SkASSERT(get_float_exp(0.5f) == -1);
176 SkASSERT(get_float_exp(1.f) == 0);
177 SkASSERT(get_float_exp(2.f) == 1);
178 SkASSERT(get_float_exp(2.5f) == 1);
179 SkASSERT(get_float_exp(8.f) == 3);
180 SkASSERT(get_float_exp(100.f) == 6);
181 SkASSERT(get_float_exp(1000.f) == 9);
182 SkASSERT(get_float_exp(1024.f) == 10);
183 SkASSERT(get_float_exp(3000000.f) == 21);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000184 }
185#endif
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000186 const int* iptr = (const int*)&x;
187 return (((*iptr) & 0x7f800000) >> 23) - 127;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000188}
189
egdaniel@google.com5383a752013-07-12 20:15:34 +0000190// Uses the max curvature function for quads to estimate
191// where to chop the conic. If the max curvature is not
192// found along the curve segment it will return 1 and
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000193// dst[0] is the original conic. If it returns 2 the dst[0]
egdaniel@google.com5383a752013-07-12 20:15:34 +0000194// and dst[1] are the two new conics.
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000195int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000196 SkScalar t = SkFindQuadMaxCurvature(src);
197 if (t == 0) {
198 if (dst) {
199 dst[0].set(src, weight);
200 }
201 return 1;
202 } else {
203 if (dst) {
204 SkConic conic;
205 conic.set(src, weight);
206 conic.chopAt(t, dst);
207 }
208 return 2;
209 }
210}
211
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000212// Calls split_conic on the entire conic and then once more on each subsection.
213// Most cases will result in either 1 conic (chop point is not within t range)
214// or 3 points (split once and then one subsection is split again).
215int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) {
216 SkConic dstTemp[2];
217 int conicCnt = split_conic(src, dstTemp, weight);
218 if (2 == conicCnt) {
219 int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW);
220 conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW);
221 } else {
222 dst[0] = dstTemp[0];
223 }
224 return conicCnt;
225}
226
egdaniel@google.com5383a752013-07-12 20:15:34 +0000227// returns 0 if quad/conic is degen or close to it
228// in this case approx the path with lines
229// otherwise returns 1
230int is_degen_quad_or_conic(const SkPoint p[3]) {
231 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
232 static const SkScalar gDegenerateToLineTolSqd =
233 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
234
235 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
236 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
237 return 1;
238 }
239
240 SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
241 if (dsqd < gDegenerateToLineTolSqd) {
242 return 1;
243 }
244
245 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
246 return 1;
247 }
248 return 0;
249}
250
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000251// we subdivide the quads to avoid huge overfill
252// if it returns -1 then should be drawn as lines
253int num_quad_subdivs(const SkPoint p[3]) {
254 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000255 static const SkScalar gDegenerateToLineTolSqd =
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000256 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000257
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000258 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
259 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000260 return -1;
261 }
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000262
bsalomon@google.com81712882012-11-01 17:12:34 +0000263 SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000264 if (dsqd < gDegenerateToLineTolSqd) {
265 return -1;
266 }
267
268 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000269 return -1;
270 }
271
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000272 // tolerance of triangle height in pixels
273 // tuned on windows Quadro FX 380 / Z600
274 // trade off of fill vs cpu time on verts
275 // maybe different when do this using gpu (geo or tess shaders)
276 static const SkScalar gSubdivTol = 175 * SK_Scalar1;
277
robertphillips@google.com7460b372012-04-25 16:54:51 +0000278 if (dsqd <= SkScalarMul(gSubdivTol, gSubdivTol)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000279 return 0;
280 } else {
robertphillips@google.com87379e12013-03-29 12:11:10 +0000281 static const int kMaxSub = 4;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000282 // subdividing the quad reduces d by 4. so we want x = log4(d/tol)
283 // = log4(d*d/tol*tol)/2
284 // = log2(d*d/tol*tol)
285
286#ifdef SK_SCALAR_IS_FLOAT
287 // +1 since we're ignoring the mantissa contribution.
288 int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
289 log = GrMin(GrMax(0, log), kMaxSub);
290 return log;
291#else
robertphillips@google.com7460b372012-04-25 16:54:51 +0000292 SkScalar log = SkScalarLog(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000293 SkScalarDiv(dsqd,
robertphillips@google.com7460b372012-04-25 16:54:51 +0000294 SkScalarMul(gSubdivTol, gSubdivTol)));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000295 static const SkScalar conv = SkScalarInvert(SkScalarLog(2));
296 log = SkScalarMul(log, conv);
297 return GrMin(GrMax(0, SkScalarCeilToInt(log)),kMaxSub);
298#endif
299 }
300}
301
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000302/**
303 * Generates the lines and quads to be rendered. Lines are always recorded in
304 * device space. We will do a device space bloat to account for the 1pixel
305 * thickness.
306 * Quads are recorded in device space unless m contains
307 * perspective, then in they are in src space. We do this because we will
308 * subdivide large quads to reduce over-fill. This subdivision has to be
309 * performed before applying the perspective matrix.
310 */
311int generate_lines_and_quads(const SkPath& path,
312 const SkMatrix& m,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000313 const SkIRect& devClipBounds,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000314 GrAAHairLinePathRenderer::PtArray* lines,
315 GrAAHairLinePathRenderer::PtArray* quads,
316 GrAAHairLinePathRenderer::PtArray* conics,
317 GrAAHairLinePathRenderer::IntArray* quadSubdivCnts,
318 GrAAHairLinePathRenderer::FloatArray* conicWeights) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000319 SkPath::Iter iter(path, false);
320
321 int totalQuadCount = 0;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000322 SkRect bounds;
323 SkIRect ibounds;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000324
325 bool persp = m.hasPerspective();
326
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000327 for (;;) {
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000328 GrPoint pathPts[4];
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000329 GrPoint devPts[4];
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000330 SkPath::Verb verb = iter.next(pathPts);
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000331 switch (verb) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000332 case SkPath::kConic_Verb: {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000333 SkConic dst[4];
334 // We chop the conics to create tighter clipping to hide error
335 // that appears near max curvature of very thin conics. Thin
336 // hyperbolas with high weight still show error.
egdaniel@google.com5383a752013-07-12 20:15:34 +0000337 int conicCnt = chop_conic(pathPts, dst, iter.conicWeight());
338 for (int i = 0; i < conicCnt; ++i) {
339 SkPoint* chopPnts = dst[i].fPts;
340 m.mapPoints(devPts, chopPnts, 3);
341 bounds.setBounds(devPts, 3);
342 bounds.outset(SK_Scalar1, SK_Scalar1);
343 bounds.roundOut(&ibounds);
344 if (SkIRect::Intersects(devClipBounds, ibounds)) {
345 if (is_degen_quad_or_conic(devPts)) {
346 SkPoint* pts = lines->push_back_n(4);
347 pts[0] = devPts[0];
348 pts[1] = devPts[1];
349 pts[2] = devPts[1];
350 pts[3] = devPts[2];
351 } else {
352 // when in perspective keep conics in src space
353 SkPoint* cPts = persp ? chopPnts : devPts;
354 SkPoint* pts = conics->push_back_n(3);
355 pts[0] = cPts[0];
356 pts[1] = cPts[1];
357 pts[2] = cPts[2];
358 conicWeights->push_back() = dst[i].fW;
359 }
360 }
361 }
reed@google.com277c3f82013-05-31 15:17:50 +0000362 break;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000363 }
364 case SkPath::kMove_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000365 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000366 case SkPath::kLine_Verb:
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000367 m.mapPoints(devPts, pathPts, 2);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000368 bounds.setBounds(devPts, 2);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000369 bounds.outset(SK_Scalar1, SK_Scalar1);
370 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000371 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000372 SkPoint* pts = lines->push_back_n(2);
373 pts[0] = devPts[0];
374 pts[1] = devPts[1];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000375 }
376 break;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000377 case SkPath::kQuad_Verb: {
378 SkPoint choppedPts[5];
379 // Chopping the quad helps when the quad is either degenerate or nearly degenerate.
380 // When it is degenerate it allows the approximation with lines to work since the
381 // chop point (if there is one) will be at the parabola's vertex. In the nearly
382 // degenerate the QuadUVMatrix computed for the points is almost singular which
383 // can cause rendering artifacts.
384 int n = SkChopQuadAtMaxCurvature(pathPts, choppedPts);
385 for (int i = 0; i < n; ++i) {
386 SkPoint* quadPts = choppedPts + i * 2;
387 m.mapPoints(devPts, quadPts, 3);
388 bounds.setBounds(devPts, 3);
389 bounds.outset(SK_Scalar1, SK_Scalar1);
390 bounds.roundOut(&ibounds);
391
392 if (SkIRect::Intersects(devClipBounds, ibounds)) {
393 int subdiv = num_quad_subdivs(devPts);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000394 SkASSERT(subdiv >= -1);
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000395 if (-1 == subdiv) {
396 SkPoint* pts = lines->push_back_n(4);
397 pts[0] = devPts[0];
398 pts[1] = devPts[1];
399 pts[2] = devPts[1];
400 pts[3] = devPts[2];
401 } else {
402 // when in perspective keep quads in src space
403 SkPoint* qPts = persp ? quadPts : devPts;
404 SkPoint* pts = quads->push_back_n(3);
405 pts[0] = qPts[0];
406 pts[1] = qPts[1];
407 pts[2] = qPts[2];
408 quadSubdivCnts->push_back() = subdiv;
409 totalQuadCount += 1 << subdiv;
410 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000411 }
412 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000413 break;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000414 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000415 case SkPath::kCubic_Verb:
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000416 m.mapPoints(devPts, pathPts, 4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000417 bounds.setBounds(devPts, 4);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000418 bounds.outset(SK_Scalar1, SK_Scalar1);
419 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000420 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000421 PREALLOC_PTARRAY(32) q;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000422 // we don't need a direction if we aren't constraining the subdivision
423 static const SkPath::Direction kDummyDir = SkPath::kCCW_Direction;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000424 // We convert cubics to quadratics (for now).
425 // In perspective have to do conversion in src space.
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000426 if (persp) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000427 SkScalar tolScale =
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000428 GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m,
429 path.getBounds());
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000430 GrPathUtils::convertCubicToQuads(pathPts, tolScale, false, kDummyDir, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000431 } else {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000432 GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, false, kDummyDir, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000433 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000434 for (int i = 0; i < q.count(); i += 3) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000435 SkPoint* qInDevSpace;
436 // bounds has to be calculated in device space, but q is
437 // in src space when there is perspective.
438 if (persp) {
439 m.mapPoints(devPts, &q[i], 3);
440 bounds.setBounds(devPts, 3);
441 qInDevSpace = devPts;
442 } else {
443 bounds.setBounds(&q[i], 3);
444 qInDevSpace = &q[i];
445 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000446 bounds.outset(SK_Scalar1, SK_Scalar1);
447 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000448 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000449 int subdiv = num_quad_subdivs(qInDevSpace);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000450 SkASSERT(subdiv >= -1);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000451 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000452 SkPoint* pts = lines->push_back_n(4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000453 // lines should always be in device coords
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000454 pts[0] = qInDevSpace[0];
455 pts[1] = qInDevSpace[1];
456 pts[2] = qInDevSpace[1];
457 pts[3] = qInDevSpace[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000458 } else {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000459 SkPoint* pts = quads->push_back_n(3);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000460 // q is already in src space when there is no
461 // perspective and dev coords otherwise.
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000462 pts[0] = q[0 + i];
463 pts[1] = q[1 + i];
464 pts[2] = q[2 + i];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000465 quadSubdivCnts->push_back() = subdiv;
466 totalQuadCount += 1 << subdiv;
467 }
468 }
469 }
470 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000471 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000472 case SkPath::kClose_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000473 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000474 case SkPath::kDone_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000475 return totalQuadCount;
476 }
477 }
478}
479
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000480struct LineVertex {
481 GrPoint fPos;
482 GrColor fCoverage;
483};
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000484
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000485struct BezierVertex {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000486 GrPoint fPos;
487 union {
488 struct {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000489 SkScalar fK;
490 SkScalar fL;
491 SkScalar fM;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000492 } fConic;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000493 GrVec fQuadCoord;
494 struct {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000495 SkScalar fBogus[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000496 };
497 };
498};
egdaniel@google.com5383a752013-07-12 20:15:34 +0000499
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000500GR_STATIC_ASSERT(sizeof(BezierVertex) == 3 * sizeof(GrPoint));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000501
502void intersect_lines(const SkPoint& ptA, const SkVector& normA,
503 const SkPoint& ptB, const SkVector& normB,
504 SkPoint* result) {
505
506 SkScalar lineAW = -normA.dot(ptA);
507 SkScalar lineBW = -normB.dot(ptB);
508
509 SkScalar wInv = SkScalarMul(normA.fX, normB.fY) -
egdaniel@google.com5383a752013-07-12 20:15:34 +0000510 SkScalarMul(normA.fY, normB.fX);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000511 wInv = SkScalarInvert(wInv);
512
513 result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY);
514 result->fX = SkScalarMul(result->fX, wInv);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000515
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000516 result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW);
517 result->fY = SkScalarMul(result->fY, wInv);
518}
519
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000520void set_uv_quad(const SkPoint qpts[3], BezierVertex verts[kVertsPerQuad]) {
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000521 // this should be in the src space, not dev coords, when we have perspective
522 GrPathUtils::QuadUVMatrix DevToUV(qpts);
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000523 DevToUV.apply<kVertsPerQuad, sizeof(BezierVertex), sizeof(GrPoint)>(verts);
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000524}
525
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000526void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000527 const SkMatrix* toSrc, BezierVertex verts[kVertsPerQuad],
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000528 SkRect* devBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000529 SkASSERT(!toDevice == !toSrc);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000530 // original quad is specified by tri a,b,c
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000531 SkPoint a = qpts[0];
532 SkPoint b = qpts[1];
533 SkPoint c = qpts[2];
534
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000535 if (toDevice) {
536 toDevice->mapPoints(&a, 1);
537 toDevice->mapPoints(&b, 1);
538 toDevice->mapPoints(&c, 1);
539 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000540 // make a new poly where we replace a and c by a 1-pixel wide edges orthog
541 // to edges ab and bc:
542 //
543 // before | after
544 // | b0
545 // b |
546 // |
547 // | a0 c0
548 // a c | a1 c1
549 //
550 // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c,
551 // respectively.
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000552 BezierVertex& a0 = verts[0];
553 BezierVertex& a1 = verts[1];
554 BezierVertex& b0 = verts[2];
555 BezierVertex& c0 = verts[3];
556 BezierVertex& c1 = verts[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000557
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000558 SkVector ab = b;
559 ab -= a;
560 SkVector ac = c;
561 ac -= a;
562 SkVector cb = b;
563 cb -= c;
564
565 // We should have already handled degenerates
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000566 SkASSERT(ab.length() > 0 && cb.length() > 0);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000567
568 ab.normalize();
569 SkVector abN;
570 abN.setOrthog(ab, SkVector::kLeft_Side);
571 if (abN.dot(ac) > 0) {
572 abN.negate();
573 }
574
575 cb.normalize();
576 SkVector cbN;
577 cbN.setOrthog(cb, SkVector::kLeft_Side);
578 if (cbN.dot(ac) < 0) {
579 cbN.negate();
580 }
581
582 a0.fPos = a;
583 a0.fPos += abN;
584 a1.fPos = a;
585 a1.fPos -= abN;
586
587 c0.fPos = c;
588 c0.fPos += cbN;
589 c1.fPos = c;
590 c1.fPos -= cbN;
591
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000592 // This point may not be within 1 pixel of a control point. We update the bounding box to
593 // include it.
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000594 intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000595 devBounds->growToInclude(b0.fPos.fX, b0.fPos.fY);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000596
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000597 if (toSrc) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000598 toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(BezierVertex), kVertsPerQuad);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000599 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000600}
601
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000602// Equations based off of Loop-Blinn Quadratic GPU Rendering
egdaniel@google.com5383a752013-07-12 20:15:34 +0000603// Input Parametric:
604// P(t) = (P0*(1-t)^2 + 2*w*P1*t*(1-t) + P2*t^2) / (1-t)^2 + 2*w*t*(1-t) + t^2)
605// Output Implicit:
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000606// f(x, y, w) = f(P) = K^2 - LM
607// K = dot(k, P), L = dot(l, P), M = dot(m, P)
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000608// k, l, m are calculated in function GrPathUtils::getConicKLM
609void set_conic_coeffs(const SkPoint p[3], BezierVertex verts[kVertsPerQuad],
610 const SkScalar weight) {
611 SkScalar klm[9];
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000612
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000613 GrPathUtils::getConicKLM(p, weight, klm);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000614
615 for (int i = 0; i < kVertsPerQuad; ++i) {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000616 const SkPoint pnt = verts[i].fPos;
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000617 verts[i].fConic.fK = pnt.fX * klm[0] + pnt.fY * klm[1] + klm[2];
618 verts[i].fConic.fL = pnt.fX * klm[3] + pnt.fY * klm[4] + klm[5];
619 verts[i].fConic.fM = pnt.fX * klm[6] + pnt.fY * klm[7] + klm[8];
egdaniel@google.com5383a752013-07-12 20:15:34 +0000620 }
621}
622
623void add_conics(const SkPoint p[3],
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000624 const SkScalar weight,
egdaniel@google.com5383a752013-07-12 20:15:34 +0000625 const SkMatrix* toDevice,
626 const SkMatrix* toSrc,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000627 BezierVertex** vert,
egdaniel@google.com5383a752013-07-12 20:15:34 +0000628 SkRect* devBounds) {
629 bloat_quad(p, toDevice, toSrc, *vert, devBounds);
630 set_conic_coeffs(p, *vert, weight);
631 *vert += kVertsPerQuad;
632}
633
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000634void add_quads(const SkPoint p[3],
635 int subdiv,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000636 const SkMatrix* toDevice,
637 const SkMatrix* toSrc,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000638 BezierVertex** vert,
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000639 SkRect* devBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000640 SkASSERT(subdiv >= 0);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000641 if (subdiv) {
642 SkPoint newP[5];
643 SkChopQuadAtHalf(p, newP);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000644 add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert, devBounds);
645 add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert, devBounds);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000646 } else {
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000647 bloat_quad(p, toDevice, toSrc, *vert, devBounds);
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000648 set_uv_quad(p, *vert);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000649 *vert += kVertsPerQuad;
650 }
651}
652
653void add_line(const SkPoint p[2],
654 int rtHeight,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000655 const SkMatrix* toSrc,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000656 GrColor coverage,
657 LineVertex** vert) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000658 const SkPoint& a = p[0];
659 const SkPoint& b = p[1];
660
661 SkVector orthVec = b;
662 orthVec -= a;
663
664 if (orthVec.setLength(SK_Scalar1)) {
665 orthVec.setOrthog(orthVec);
666
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000667 for (int i = 0; i < kVertsPerLineSeg; ++i) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000668 (*vert)[i].fPos = (i & 0x1) ? b : a;
669 if (i & 0x2) {
jvanverth@google.com5e2d2702013-08-14 14:29:29 +0000670 (*vert)[i].fPos += orthVec;
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000671 (*vert)[i].fCoverage = 0;
672 } else if (i & 0x4) {
673 (*vert)[i].fPos -= orthVec;
674 (*vert)[i].fCoverage = 0;
675 } else {
676 (*vert)[i].fCoverage = coverage;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000677 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000678 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000679 if (NULL != toSrc) {
680 toSrc->mapPointsWithStride(&(*vert)->fPos,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000681 sizeof(LineVertex),
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000682 kVertsPerLineSeg);
683 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000684 } else {
685 // just make it degenerate and likely offscreen
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000686 for (int i = 0; i < kVertsPerLineSeg; ++i) {
687 (*vert)[i].fPos.set(SK_ScalarMax, SK_ScalarMax);
688 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000689 }
690
691 *vert += kVertsPerLineSeg;
692}
693
694}
695
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000696///////////////////////////////////////////////////////////////////////////////
697
robertphillips@google.com42903302013-04-20 12:26:07 +0000698namespace {
699
700// position + edge
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000701extern const GrVertexAttrib gHairlineBezierAttribs[] = {
robertphillips@google.com42903302013-04-20 12:26:07 +0000702 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
703 {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}
704};
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000705
706// position + coverage
707extern const GrVertexAttrib gHairlineLineAttribs[] = {
708 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
709 {kVec4ub_GrVertexAttribType, sizeof(GrPoint), kCoverage_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +0000710};
711
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000712};
713
714bool GrAAHairLinePathRenderer::createLineGeom(
bsalomon@google.comb3729422012-03-07 19:13:28 +0000715 const SkPath& path,
bsalomon@google.comb3729422012-03-07 19:13:28 +0000716 GrDrawTarget* target,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000717 const PtArray& lines,
718 int lineCnt,
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000719 GrDrawTarget::AutoReleaseGeometry* arg,
720 SkRect* devBounds) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000721 GrDrawState* drawState = target->drawState();
722 int rtHeight = drawState->getRenderTarget()->height();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000723
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000724 const SkMatrix& viewM = drawState->getViewMatrix();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000725
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000726 *devBounds = path.getBounds();
727 viewM.mapRect(devBounds);
728 devBounds->outset(SK_Scalar1, SK_Scalar1);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000729
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000730 int vertCnt = kVertsPerLineSeg * lineCnt;
731
732 target->drawState()->setVertexAttribs<gHairlineLineAttribs>(SK_ARRAY_COUNT(gHairlineLineAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000733 SkASSERT(sizeof(LineVertex) == target->getDrawState().getVertexSize());
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000734
735 if (!arg->set(target, vertCnt, 0)) {
736 return false;
737 }
738
739 LineVertex* verts = reinterpret_cast<LineVertex*>(arg->vertices());
740
741 const SkMatrix* toSrc = NULL;
742 SkMatrix ivm;
743
744 if (viewM.hasPerspective()) {
745 if (viewM.invert(&ivm)) {
746 toSrc = &ivm;
747 }
748 }
749
750 for (int i = 0; i < lineCnt; ++i) {
751 add_line(&lines[2*i], rtHeight, toSrc, drawState->getCoverage(), &verts);
752 }
753
754 return true;
755}
756
757bool GrAAHairLinePathRenderer::createBezierGeom(
758 const SkPath& path,
759 GrDrawTarget* target,
760 const PtArray& quads,
761 int quadCnt,
762 const PtArray& conics,
763 int conicCnt,
764 const IntArray& qSubdivs,
765 const FloatArray& cWeights,
766 GrDrawTarget::AutoReleaseGeometry* arg,
767 SkRect* devBounds) {
768 GrDrawState* drawState = target->drawState();
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000769
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000770 const SkMatrix& viewM = drawState->getViewMatrix();
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000771
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000772 // All the vertices that we compute are within 1 of path control points with the exception of
773 // one of the bounding vertices for each quad. The add_quads() function will update the bounds
774 // for each quad added.
775 *devBounds = path.getBounds();
776 viewM.mapRect(devBounds);
777 devBounds->outset(SK_Scalar1, SK_Scalar1);
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000778
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000779 int vertCnt = kVertsPerQuad * quadCnt + kVertsPerQuad * conicCnt;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000780
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000781 target->drawState()->setVertexAttribs<gHairlineBezierAttribs>(SK_ARRAY_COUNT(gHairlineBezierAttribs));
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000782 SkASSERT(sizeof(BezierVertex) == target->getDrawState().getVertexSize());
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000783
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000784 if (!arg->set(target, vertCnt, 0)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000785 return false;
786 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000787
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000788 BezierVertex* verts = reinterpret_cast<BezierVertex*>(arg->vertices());
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000789
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000790 const SkMatrix* toDevice = NULL;
791 const SkMatrix* toSrc = NULL;
792 SkMatrix ivm;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000793
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000794 if (viewM.hasPerspective()) {
795 if (viewM.invert(&ivm)) {
796 toDevice = &viewM;
797 toSrc = &ivm;
798 }
799 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000800
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000801 int unsubdivQuadCnt = quads.count() / 3;
802 for (int i = 0; i < unsubdivQuadCnt; ++i) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000803 SkASSERT(qSubdivs[i] >= 0);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000804 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts, devBounds);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000805 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000806
egdaniel@google.com5383a752013-07-12 20:15:34 +0000807 // Start Conics
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000808 for (int i = 0; i < conicCnt; ++i) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000809 add_conics(&conics[3*i], cWeights[i], toDevice, toSrc, &verts, devBounds);
810 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000811 return true;
812}
813
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000814bool GrAAHairLinePathRenderer::canDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000815 const SkStrokeRec& stroke,
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000816 const GrDrawTarget* target,
817 bool antiAlias) const {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000818 if (!stroke.isHairlineStyle() || !antiAlias) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000819 return false;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000820 }
821
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000822 if (SkPath::kLine_SegmentMask == path.getSegmentMasks() ||
823 target->caps()->shaderDerivativeSupport()) {
824 return true;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000825 }
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000826 return false;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000827}
828
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000829template <class VertexType>
830bool check_bounds(GrDrawState* drawState, const SkRect& devBounds, void* vertices, int vCount)
831{
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000832 SkRect tolDevBounds = devBounds;
833 tolDevBounds.outset(SK_Scalar1 / 10000, SK_Scalar1 / 10000);
834 SkRect actualBounds;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000835
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000836 VertexType* verts = reinterpret_cast<VertexType*>(vertices);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000837 bool first = true;
838 for (int i = 0; i < vCount; ++i) {
839 SkPoint pos = verts[i].fPos;
840 // This is a hack to workaround the fact that we move some degenerate segments offscreen.
841 if (SK_ScalarMax == pos.fX) {
842 continue;
843 }
844 drawState->getViewMatrix().mapPoints(&pos, 1);
845 if (first) {
846 actualBounds.set(pos.fX, pos.fY, pos.fX, pos.fY);
847 first = false;
848 } else {
849 actualBounds.growToInclude(pos.fX, pos.fY);
850 }
851 }
852 if (!first) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000853 return tolDevBounds.contains(actualBounds);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000854 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000855
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000856 return true;
857}
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000858
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000859bool GrAAHairLinePathRenderer::onDrawPath(const SkPath& path,
860 const SkStrokeRec&,
861 GrDrawTarget* target,
862 bool antiAlias) {
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000863
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000864 GrDrawState* drawState = target->drawState();
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000865
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000866 SkIRect devClipBounds;
867 target->getClip()->getConservativeBounds(drawState->getRenderTarget(), &devClipBounds);
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000868
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000869 int lineCnt;
870 int quadCnt;
871 int conicCnt;
872 PREALLOC_PTARRAY(128) lines;
873 PREALLOC_PTARRAY(128) quads;
874 PREALLOC_PTARRAY(128) conics;
875 IntArray qSubdivs;
876 FloatArray cWeights;
877 quadCnt = generate_lines_and_quads(path, drawState->getViewMatrix(), devClipBounds,
878 &lines, &quads, &conics, &qSubdivs, &cWeights);
879 lineCnt = lines.count() / 2;
880 conicCnt = conics.count() / 3;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000881
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000882 // do lines first
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000883 {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000884 GrDrawTarget::AutoReleaseGeometry arg;
885 SkRect devBounds;
886
887 if (!this->createLineGeom(path,
888 target,
889 lines,
890 lineCnt,
891 &arg,
892 &devBounds)) {
893 return false;
894 }
895
896 GrDrawTarget::AutoStateRestore asr;
897
898 // createGeom transforms the geometry to device space when the matrix does not have
899 // perspective.
900 if (target->getDrawState().getViewMatrix().hasPerspective()) {
901 asr.set(target, GrDrawTarget::kPreserve_ASRInit);
902 } else if (!asr.setIdentity(target, GrDrawTarget::kPreserve_ASRInit)) {
903 return false;
904 }
905 GrDrawState* drawState = target->drawState();
906
907 // Check devBounds
908 SkASSERT(check_bounds<LineVertex>(drawState, devBounds, arg.vertices(),
909 kVertsPerLineSeg * lineCnt));
910
911 {
912 GrDrawState::AutoRestoreEffects are(drawState);
913 target->setIndexSourceToBuffer(fLinesIndexBuffer);
914 int lines = 0;
915 while (lines < lineCnt) {
916 int n = GrMin(lineCnt - lines, kNumLineSegsInIdxBuffer);
917 target->drawIndexed(kTriangles_GrPrimitiveType,
918 kVertsPerLineSeg*lines, // startV
919 0, // startI
920 kVertsPerLineSeg*n, // vCount
921 kIdxsPerLineSeg*n,
922 &devBounds); // iCount
923 lines += n;
924 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000925 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000926 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000927
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000928 // then quadratics/conics
egdaniel@google.com5383a752013-07-12 20:15:34 +0000929 {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000930 GrDrawTarget::AutoReleaseGeometry arg;
931 SkRect devBounds;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000932
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000933 if (!this->createBezierGeom(path,
934 target,
935 quads,
936 quadCnt,
937 conics,
938 conicCnt,
939 qSubdivs,
940 cWeights,
941 &arg,
942 &devBounds)) {
943 return false;
944 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000945
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000946 GrDrawTarget::AutoStateRestore asr;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000947
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000948 // createGeom transforms the geometry to device space when the matrix does not have
949 // perspective.
950 if (target->getDrawState().getViewMatrix().hasPerspective()) {
951 asr.set(target, GrDrawTarget::kPreserve_ASRInit);
952 } else if (!asr.setIdentity(target, GrDrawTarget::kPreserve_ASRInit)) {
953 return false;
954 }
955 GrDrawState* drawState = target->drawState();
956
957 static const int kEdgeAttrIndex = 1;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000958
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000959 // Check devBounds
960 SkASSERT(check_bounds<BezierVertex>(drawState, devBounds, arg.vertices(),
961 kVertsPerQuad * quadCnt + kVertsPerQuad * conicCnt));
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000962
commit-bot@chromium.org88462472013-08-23 21:01:52 +0000963 if (quadCnt > 0) {
964 GrEffectRef* hairQuadEffect = GrQuadEffect::Create(kHairAA_GrBezierEdgeType,
965 *target->caps());
966 SkASSERT(NULL != hairQuadEffect);
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000967 GrDrawState::AutoRestoreEffects are(drawState);
968 target->setIndexSourceToBuffer(fQuadsIndexBuffer);
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000969 drawState->addCoverageEffect(hairQuadEffect, kEdgeAttrIndex)->unref();
commit-bot@chromium.org88462472013-08-23 21:01:52 +0000970 int quads = 0;
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000971 while (quads < quadCnt) {
972 int n = GrMin(quadCnt - quads, kNumQuadsInIdxBuffer);
973 target->drawIndexed(kTriangles_GrPrimitiveType,
974 kVertsPerQuad*quads, // startV
975 0, // startI
976 kVertsPerQuad*n, // vCount
977 kIdxsPerQuad*n, // iCount
978 &devBounds);
979 quads += n;
980 }
981 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000982
commit-bot@chromium.org88462472013-08-23 21:01:52 +0000983 if (conicCnt > 0) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000984 GrDrawState::AutoRestoreEffects are(drawState);
commit-bot@chromium.org88462472013-08-23 21:01:52 +0000985 GrEffectRef* hairConicEffect = GrConicEffect::Create(kHairAA_GrBezierEdgeType,
986 *target->caps());
987 SkASSERT(NULL != hairConicEffect);
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000988 drawState->addCoverageEffect(hairConicEffect, 1, 2)->unref();
commit-bot@chromium.org88462472013-08-23 21:01:52 +0000989 int conics = 0;
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000990 while (conics < conicCnt) {
991 int n = GrMin(conicCnt - conics, kNumQuadsInIdxBuffer);
992 target->drawIndexed(kTriangles_GrPrimitiveType,
993 kVertsPerQuad*(quadCnt + conics), // startV
994 0, // startI
995 kVertsPerQuad*n, // vCount
996 kIdxsPerQuad*n, // iCount
997 &devBounds);
998 conics += n;
999 }
egdaniel@google.com5383a752013-07-12 20:15:34 +00001000 }
1001 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +00001002
bsalomon@google.com0406b9e2013-04-02 21:00:15 +00001003 target->resetIndexSource();
bsalomon@google.com4647f902013-03-26 14:45:27 +00001004
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001005 return true;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001006}