blob: 33c2ac389b4ff9caf3e8a24593212ec310d0512f [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.org90c240a2013-04-02 17:57:21 +000022#include "gl/GrGLEffect.h"
23#include "gl/GrGLSL.h"
bsalomon@google.com4647f902013-03-26 14:45:27 +000024
bsalomon@google.comaeb21602011-08-30 18:13:44 +000025namespace {
26// quadratics are rendered as 5-sided polys in order to bound the
27// AA stroke around the center-curve. See comments in push_quad_index_buffer and
egdaniel@google.com5383a752013-07-12 20:15:34 +000028// bloat_quad. Quadratics and conics share an index buffer
bsalomon@google.comaeb21602011-08-30 18:13:44 +000029static const int kVertsPerQuad = 5;
30static const int kIdxsPerQuad = 9;
31
commit-bot@chromium.org78b64ae2013-08-08 18:22:07 +000032static const int kVertsPerLineSeg = 6;
33static const int kIdxsPerLineSeg = 12;
bsalomon@google.comaeb21602011-08-30 18:13:44 +000034
35static const int kNumQuadsInIdxBuffer = 256;
36static const size_t kQuadIdxSBufize = kIdxsPerQuad *
37 sizeof(uint16_t) *
38 kNumQuadsInIdxBuffer;
39
commit-bot@chromium.org78b64ae2013-08-08 18:22:07 +000040static const int kNumLineSegsInIdxBuffer = 256;
41static const size_t kLineSegIdxSBufize = kIdxsPerLineSeg *
42 sizeof(uint16_t) *
43 kNumLineSegsInIdxBuffer;
44
45static bool push_quad_index_data(GrIndexBuffer* qIdxBuffer) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +000046 uint16_t* data = (uint16_t*) qIdxBuffer->lock();
47 bool tempData = NULL == data;
48 if (tempData) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000049 data = SkNEW_ARRAY(uint16_t, kNumQuadsInIdxBuffer * kIdxsPerQuad);
bsalomon@google.comaeb21602011-08-30 18:13:44 +000050 }
51 for (int i = 0; i < kNumQuadsInIdxBuffer; ++i) {
52
53 // Each quadratic is rendered as a five sided polygon. This poly bounds
54 // the quadratic's bounding triangle but has been expanded so that the
55 // 1-pixel wide area around the curve is inside the poly.
56 // If a,b,c are the original control points then the poly a0,b0,c0,c1,a1
57 // that is rendered would look like this:
58 // b0
59 // b
60 //
61 // a0 c0
62 // a c
63 // a1 c1
bsalomon@google.com0e5104c2012-04-10 16:20:41 +000064 // Each is drawn as three triangles specified by these 9 indices:
bsalomon@google.comaeb21602011-08-30 18:13:44 +000065 int baseIdx = i * kIdxsPerQuad;
66 uint16_t baseVert = (uint16_t)(i * kVertsPerQuad);
67 data[0 + baseIdx] = baseVert + 0; // a0
68 data[1 + baseIdx] = baseVert + 1; // a1
69 data[2 + baseIdx] = baseVert + 2; // b0
70 data[3 + baseIdx] = baseVert + 2; // b0
71 data[4 + baseIdx] = baseVert + 4; // c1
72 data[5 + baseIdx] = baseVert + 3; // c0
73 data[6 + baseIdx] = baseVert + 1; // a1
74 data[7 + baseIdx] = baseVert + 4; // c1
75 data[8 + baseIdx] = baseVert + 2; // b0
76 }
77 if (tempData) {
78 bool ret = qIdxBuffer->updateData(data, kQuadIdxSBufize);
79 delete[] data;
80 return ret;
81 } else {
82 qIdxBuffer->unlock();
83 return true;
84 }
85}
commit-bot@chromium.org78b64ae2013-08-08 18:22:07 +000086
87static bool push_line_index_data(GrIndexBuffer* lIdxBuffer) {
88 uint16_t* data = (uint16_t*) lIdxBuffer->lock();
89 bool tempData = NULL == data;
90 if (tempData) {
91 data = SkNEW_ARRAY(uint16_t, kNumLineSegsInIdxBuffer * kIdxsPerLineSeg);
92 }
93 for (int i = 0; i < kNumLineSegsInIdxBuffer; ++i) {
94 // Each line segment is rendered as two quads, with alpha = 1 along the
95 // spine of the segment, and alpha = 0 along the outer edges, represented
96 // horizontally (i.e., the line equation is t*(p1-p0) + p0)
97 //
98 // p4 p5
99 // p0 p1
100 // p2 p3
101 //
102 // Each is drawn as four triangles specified by these 12 indices:
103 int baseIdx = i * kIdxsPerLineSeg;
104 uint16_t baseVert = (uint16_t)(i * kVertsPerLineSeg);
105 data[0 + baseIdx] = baseVert + 0; // p0
106 data[1 + baseIdx] = baseVert + 1; // p1
107 data[2 + baseIdx] = baseVert + 2; // p2
108
109 data[3 + baseIdx] = baseVert + 2; // p2
110 data[4 + baseIdx] = baseVert + 1; // p1
111 data[5 + baseIdx] = baseVert + 3; // p3
112
113 data[6 + baseIdx] = baseVert + 0; // p0
114 data[7 + baseIdx] = baseVert + 5; // p5
115 data[8 + baseIdx] = baseVert + 1; // p1
116
117 data[9 + baseIdx] = baseVert + 0; // p0
118 data[10+ baseIdx] = baseVert + 4; // p4
119 data[11+ baseIdx] = baseVert + 5; // p5
120 }
121 if (tempData) {
122 bool ret = lIdxBuffer->updateData(data, kLineSegIdxSBufize);
123 delete[] data;
124 return ret;
125 } else {
126 lIdxBuffer->unlock();
127 return true;
128 }
129}
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000130}
131
132GrPathRenderer* GrAAHairLinePathRenderer::Create(GrContext* context) {
bsalomon@google.coma8a6a322011-09-23 14:19:58 +0000133 GrGpu* gpu = context->getGpu();
134 GrIndexBuffer* qIdxBuf = gpu->createIndexBuffer(kQuadIdxSBufize, false);
135 SkAutoTUnref<GrIndexBuffer> qIdxBuffer(qIdxBuf);
commit-bot@chromium.org78b64ae2013-08-08 18:22:07 +0000136 if (NULL == qIdxBuf || !push_quad_index_data(qIdxBuf)) {
137 return NULL;
138 }
139 GrIndexBuffer* lIdxBuf = gpu->createIndexBuffer(kLineSegIdxSBufize, false);
140 SkAutoTUnref<GrIndexBuffer> lIdxBuffer(lIdxBuf);
141 if (NULL == lIdxBuf || !push_line_index_data(lIdxBuf)) {
bsalomon@google.coma8a6a322011-09-23 14:19:58 +0000142 return NULL;
143 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000144 return SkNEW_ARGS(GrAAHairLinePathRenderer,
commit-bot@chromium.org78b64ae2013-08-08 18:22:07 +0000145 (context, lIdxBuf, qIdxBuf));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000146}
147
148GrAAHairLinePathRenderer::GrAAHairLinePathRenderer(
149 const GrContext* context,
150 const GrIndexBuffer* linesIndexBuffer,
151 const GrIndexBuffer* quadsIndexBuffer) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000152 fLinesIndexBuffer = linesIndexBuffer;
153 linesIndexBuffer->ref();
154 fQuadsIndexBuffer = quadsIndexBuffer;
155 quadsIndexBuffer->ref();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000156}
157
158GrAAHairLinePathRenderer::~GrAAHairLinePathRenderer() {
159 fLinesIndexBuffer->unref();
160 fQuadsIndexBuffer->unref();
161}
162
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000163namespace {
164
bsalomon@google.com49313f62011-09-14 13:54:05 +0000165typedef SkTArray<SkPoint, true> PtArray;
bsalomon@google.com92669012011-09-27 19:10:05 +0000166#define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true>
bsalomon@google.com49313f62011-09-14 13:54:05 +0000167typedef SkTArray<int, true> IntArray;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000168typedef SkTArray<float, true> FloatArray;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000169
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000170// Takes 178th time of logf on Z600 / VC2010
171int get_float_exp(float x) {
172 GR_STATIC_ASSERT(sizeof(int) == sizeof(float));
173#if GR_DEBUG
174 static bool tested;
175 if (!tested) {
176 tested = true;
177 GrAssert(get_float_exp(0.25f) == -2);
178 GrAssert(get_float_exp(0.3f) == -2);
179 GrAssert(get_float_exp(0.5f) == -1);
180 GrAssert(get_float_exp(1.f) == 0);
181 GrAssert(get_float_exp(2.f) == 1);
182 GrAssert(get_float_exp(2.5f) == 1);
183 GrAssert(get_float_exp(8.f) == 3);
184 GrAssert(get_float_exp(100.f) == 6);
185 GrAssert(get_float_exp(1000.f) == 9);
186 GrAssert(get_float_exp(1024.f) == 10);
187 GrAssert(get_float_exp(3000000.f) == 21);
188 }
189#endif
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000190 const int* iptr = (const int*)&x;
191 return (((*iptr) & 0x7f800000) >> 23) - 127;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000192}
193
egdaniel@google.com5383a752013-07-12 20:15:34 +0000194// Uses the max curvature function for quads to estimate
195// where to chop the conic. If the max curvature is not
196// found along the curve segment it will return 1 and
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000197// dst[0] is the original conic. If it returns 2 the dst[0]
egdaniel@google.com5383a752013-07-12 20:15:34 +0000198// and dst[1] are the two new conics.
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000199int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000200 SkScalar t = SkFindQuadMaxCurvature(src);
201 if (t == 0) {
202 if (dst) {
203 dst[0].set(src, weight);
204 }
205 return 1;
206 } else {
207 if (dst) {
208 SkConic conic;
209 conic.set(src, weight);
210 conic.chopAt(t, dst);
211 }
212 return 2;
213 }
214}
215
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000216// Calls split_conic on the entire conic and then once more on each subsection.
217// Most cases will result in either 1 conic (chop point is not within t range)
218// or 3 points (split once and then one subsection is split again).
219int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) {
220 SkConic dstTemp[2];
221 int conicCnt = split_conic(src, dstTemp, weight);
222 if (2 == conicCnt) {
223 int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW);
224 conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW);
225 } else {
226 dst[0] = dstTemp[0];
227 }
228 return conicCnt;
229}
230
egdaniel@google.com5383a752013-07-12 20:15:34 +0000231// returns 0 if quad/conic is degen or close to it
232// in this case approx the path with lines
233// otherwise returns 1
234int is_degen_quad_or_conic(const SkPoint p[3]) {
235 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
236 static const SkScalar gDegenerateToLineTolSqd =
237 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
238
239 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
240 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
241 return 1;
242 }
243
244 SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
245 if (dsqd < gDegenerateToLineTolSqd) {
246 return 1;
247 }
248
249 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
250 return 1;
251 }
252 return 0;
253}
254
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000255// we subdivide the quads to avoid huge overfill
256// if it returns -1 then should be drawn as lines
257int num_quad_subdivs(const SkPoint p[3]) {
258 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000259 static const SkScalar gDegenerateToLineTolSqd =
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000260 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000261
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000262 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
263 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000264 return -1;
265 }
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000266
bsalomon@google.com81712882012-11-01 17:12:34 +0000267 SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000268 if (dsqd < gDegenerateToLineTolSqd) {
269 return -1;
270 }
271
272 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000273 return -1;
274 }
275
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000276 // tolerance of triangle height in pixels
277 // tuned on windows Quadro FX 380 / Z600
278 // trade off of fill vs cpu time on verts
279 // maybe different when do this using gpu (geo or tess shaders)
280 static const SkScalar gSubdivTol = 175 * SK_Scalar1;
281
robertphillips@google.com7460b372012-04-25 16:54:51 +0000282 if (dsqd <= SkScalarMul(gSubdivTol, gSubdivTol)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000283 return 0;
284 } else {
robertphillips@google.com87379e12013-03-29 12:11:10 +0000285 static const int kMaxSub = 4;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000286 // subdividing the quad reduces d by 4. so we want x = log4(d/tol)
287 // = log4(d*d/tol*tol)/2
288 // = log2(d*d/tol*tol)
289
290#ifdef SK_SCALAR_IS_FLOAT
291 // +1 since we're ignoring the mantissa contribution.
292 int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
293 log = GrMin(GrMax(0, log), kMaxSub);
294 return log;
295#else
robertphillips@google.com7460b372012-04-25 16:54:51 +0000296 SkScalar log = SkScalarLog(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000297 SkScalarDiv(dsqd,
robertphillips@google.com7460b372012-04-25 16:54:51 +0000298 SkScalarMul(gSubdivTol, gSubdivTol)));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000299 static const SkScalar conv = SkScalarInvert(SkScalarLog(2));
300 log = SkScalarMul(log, conv);
301 return GrMin(GrMax(0, SkScalarCeilToInt(log)),kMaxSub);
302#endif
303 }
304}
305
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000306/**
307 * Generates the lines and quads to be rendered. Lines are always recorded in
308 * device space. We will do a device space bloat to account for the 1pixel
309 * thickness.
310 * Quads are recorded in device space unless m contains
311 * perspective, then in they are in src space. We do this because we will
312 * subdivide large quads to reduce over-fill. This subdivision has to be
313 * performed before applying the perspective matrix.
314 */
315int generate_lines_and_quads(const SkPath& path,
316 const SkMatrix& m,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000317 const SkIRect& devClipBounds,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000318 PtArray* lines,
319 PtArray* quads,
egdaniel@google.com5383a752013-07-12 20:15:34 +0000320 PtArray* conics,
321 IntArray* quadSubdivCnts,
322 FloatArray* conicWeights) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000323 SkPath::Iter iter(path, false);
324
325 int totalQuadCount = 0;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000326 SkRect bounds;
327 SkIRect ibounds;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000328
329 bool persp = m.hasPerspective();
330
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000331 for (;;) {
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000332 GrPoint pathPts[4];
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000333 GrPoint devPts[4];
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000334 SkPath::Verb verb = iter.next(pathPts);
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000335 switch (verb) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000336 case SkPath::kConic_Verb: {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000337 SkConic dst[4];
338 // We chop the conics to create tighter clipping to hide error
339 // that appears near max curvature of very thin conics. Thin
340 // hyperbolas with high weight still show error.
egdaniel@google.com5383a752013-07-12 20:15:34 +0000341 int conicCnt = chop_conic(pathPts, dst, iter.conicWeight());
342 for (int i = 0; i < conicCnt; ++i) {
343 SkPoint* chopPnts = dst[i].fPts;
344 m.mapPoints(devPts, chopPnts, 3);
345 bounds.setBounds(devPts, 3);
346 bounds.outset(SK_Scalar1, SK_Scalar1);
347 bounds.roundOut(&ibounds);
348 if (SkIRect::Intersects(devClipBounds, ibounds)) {
349 if (is_degen_quad_or_conic(devPts)) {
350 SkPoint* pts = lines->push_back_n(4);
351 pts[0] = devPts[0];
352 pts[1] = devPts[1];
353 pts[2] = devPts[1];
354 pts[3] = devPts[2];
355 } else {
356 // when in perspective keep conics in src space
357 SkPoint* cPts = persp ? chopPnts : devPts;
358 SkPoint* pts = conics->push_back_n(3);
359 pts[0] = cPts[0];
360 pts[1] = cPts[1];
361 pts[2] = cPts[2];
362 conicWeights->push_back() = dst[i].fW;
363 }
364 }
365 }
reed@google.com277c3f82013-05-31 15:17:50 +0000366 break;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000367 }
368 case SkPath::kMove_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000369 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000370 case SkPath::kLine_Verb:
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000371 m.mapPoints(devPts, pathPts, 2);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000372 bounds.setBounds(devPts, 2);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000373 bounds.outset(SK_Scalar1, SK_Scalar1);
374 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000375 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000376 SkPoint* pts = lines->push_back_n(2);
377 pts[0] = devPts[0];
378 pts[1] = devPts[1];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000379 }
380 break;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000381 case SkPath::kQuad_Verb: {
382 SkPoint choppedPts[5];
383 // Chopping the quad helps when the quad is either degenerate or nearly degenerate.
384 // When it is degenerate it allows the approximation with lines to work since the
385 // chop point (if there is one) will be at the parabola's vertex. In the nearly
386 // degenerate the QuadUVMatrix computed for the points is almost singular which
387 // can cause rendering artifacts.
388 int n = SkChopQuadAtMaxCurvature(pathPts, choppedPts);
389 for (int i = 0; i < n; ++i) {
390 SkPoint* quadPts = choppedPts + i * 2;
391 m.mapPoints(devPts, quadPts, 3);
392 bounds.setBounds(devPts, 3);
393 bounds.outset(SK_Scalar1, SK_Scalar1);
394 bounds.roundOut(&ibounds);
395
396 if (SkIRect::Intersects(devClipBounds, ibounds)) {
397 int subdiv = num_quad_subdivs(devPts);
398 GrAssert(subdiv >= -1);
399 if (-1 == subdiv) {
400 SkPoint* pts = lines->push_back_n(4);
401 pts[0] = devPts[0];
402 pts[1] = devPts[1];
403 pts[2] = devPts[1];
404 pts[3] = devPts[2];
405 } else {
406 // when in perspective keep quads in src space
407 SkPoint* qPts = persp ? quadPts : devPts;
408 SkPoint* pts = quads->push_back_n(3);
409 pts[0] = qPts[0];
410 pts[1] = qPts[1];
411 pts[2] = qPts[2];
412 quadSubdivCnts->push_back() = subdiv;
413 totalQuadCount += 1 << subdiv;
414 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000415 }
416 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000417 break;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000418 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000419 case SkPath::kCubic_Verb:
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000420 m.mapPoints(devPts, pathPts, 4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000421 bounds.setBounds(devPts, 4);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000422 bounds.outset(SK_Scalar1, SK_Scalar1);
423 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000424 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000425 PREALLOC_PTARRAY(32) q;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000426 // we don't need a direction if we aren't constraining the subdivision
427 static const SkPath::Direction kDummyDir = SkPath::kCCW_Direction;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000428 // We convert cubics to quadratics (for now).
429 // In perspective have to do conversion in src space.
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000430 if (persp) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000431 SkScalar tolScale =
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000432 GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m,
433 path.getBounds());
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000434 GrPathUtils::convertCubicToQuads(pathPts, tolScale, false, kDummyDir, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000435 } else {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000436 GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, false, kDummyDir, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000437 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000438 for (int i = 0; i < q.count(); i += 3) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000439 SkPoint* qInDevSpace;
440 // bounds has to be calculated in device space, but q is
441 // in src space when there is perspective.
442 if (persp) {
443 m.mapPoints(devPts, &q[i], 3);
444 bounds.setBounds(devPts, 3);
445 qInDevSpace = devPts;
446 } else {
447 bounds.setBounds(&q[i], 3);
448 qInDevSpace = &q[i];
449 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000450 bounds.outset(SK_Scalar1, SK_Scalar1);
451 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000452 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000453 int subdiv = num_quad_subdivs(qInDevSpace);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000454 GrAssert(subdiv >= -1);
455 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000456 SkPoint* pts = lines->push_back_n(4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000457 // lines should always be in device coords
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000458 pts[0] = qInDevSpace[0];
459 pts[1] = qInDevSpace[1];
460 pts[2] = qInDevSpace[1];
461 pts[3] = qInDevSpace[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000462 } else {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000463 SkPoint* pts = quads->push_back_n(3);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000464 // q is already in src space when there is no
465 // perspective and dev coords otherwise.
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000466 pts[0] = q[0 + i];
467 pts[1] = q[1 + i];
468 pts[2] = q[2 + i];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000469 quadSubdivCnts->push_back() = subdiv;
470 totalQuadCount += 1 << subdiv;
471 }
472 }
473 }
474 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000475 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000476 case SkPath::kClose_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000477 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000478 case SkPath::kDone_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000479 return totalQuadCount;
480 }
481 }
482}
483
484struct Vertex {
485 GrPoint fPos;
486 union {
487 struct {
commit-bot@chromium.org78b64ae2013-08-08 18:22:07 +0000488 SkScalar fCoverage;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000489 } fLine;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000490 struct {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000491 SkScalar fK;
492 SkScalar fL;
493 SkScalar fM;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000494 } fConic;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000495 GrVec fQuadCoord;
496 struct {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000497 SkScalar fBogus[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000498 };
499 };
500};
egdaniel@google.com5383a752013-07-12 20:15:34 +0000501
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000502GR_STATIC_ASSERT(sizeof(Vertex) == 3 * sizeof(GrPoint));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000503
504void intersect_lines(const SkPoint& ptA, const SkVector& normA,
505 const SkPoint& ptB, const SkVector& normB,
506 SkPoint* result) {
507
508 SkScalar lineAW = -normA.dot(ptA);
509 SkScalar lineBW = -normB.dot(ptB);
510
511 SkScalar wInv = SkScalarMul(normA.fX, normB.fY) -
egdaniel@google.com5383a752013-07-12 20:15:34 +0000512 SkScalarMul(normA.fY, normB.fX);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000513 wInv = SkScalarInvert(wInv);
514
515 result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY);
516 result->fX = SkScalarMul(result->fX, wInv);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000517
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000518 result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW);
519 result->fY = SkScalarMul(result->fY, wInv);
520}
521
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000522void set_uv_quad(const SkPoint qpts[3], Vertex verts[kVertsPerQuad]) {
523 // this should be in the src space, not dev coords, when we have perspective
524 GrPathUtils::QuadUVMatrix DevToUV(qpts);
525 DevToUV.apply<kVertsPerQuad, sizeof(Vertex), sizeof(GrPoint)>(verts);
526}
527
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000528void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice,
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000529 const SkMatrix* toSrc, Vertex verts[kVertsPerQuad],
530 SkRect* devBounds) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000531 GrAssert(!toDevice == !toSrc);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000532 // original quad is specified by tri a,b,c
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000533 SkPoint a = qpts[0];
534 SkPoint b = qpts[1];
535 SkPoint c = qpts[2];
536
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000537 if (toDevice) {
538 toDevice->mapPoints(&a, 1);
539 toDevice->mapPoints(&b, 1);
540 toDevice->mapPoints(&c, 1);
541 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000542 // make a new poly where we replace a and c by a 1-pixel wide edges orthog
543 // to edges ab and bc:
544 //
545 // before | after
546 // | b0
547 // b |
548 // |
549 // | a0 c0
550 // a c | a1 c1
551 //
552 // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c,
553 // respectively.
554 Vertex& a0 = verts[0];
555 Vertex& a1 = verts[1];
556 Vertex& b0 = verts[2];
557 Vertex& c0 = verts[3];
558 Vertex& c1 = verts[4];
559
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000560 SkVector ab = b;
561 ab -= a;
562 SkVector ac = c;
563 ac -= a;
564 SkVector cb = b;
565 cb -= c;
566
567 // We should have already handled degenerates
568 GrAssert(ab.length() > 0 && cb.length() > 0);
569
570 ab.normalize();
571 SkVector abN;
572 abN.setOrthog(ab, SkVector::kLeft_Side);
573 if (abN.dot(ac) > 0) {
574 abN.negate();
575 }
576
577 cb.normalize();
578 SkVector cbN;
579 cbN.setOrthog(cb, SkVector::kLeft_Side);
580 if (cbN.dot(ac) < 0) {
581 cbN.negate();
582 }
583
584 a0.fPos = a;
585 a0.fPos += abN;
586 a1.fPos = a;
587 a1.fPos -= abN;
588
589 c0.fPos = c;
590 c0.fPos += cbN;
591 c1.fPos = c;
592 c1.fPos -= cbN;
593
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000594 // This point may not be within 1 pixel of a control point. We update the bounding box to
595 // include it.
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000596 intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000597 devBounds->growToInclude(b0.fPos.fX, b0.fPos.fY);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000598
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000599 if (toSrc) {
600 toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(Vertex), kVertsPerQuad);
601 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000602}
603
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000604// Input:
605// Three control points: p[0], p[1], p[2] and weight: w
606// Output:
607// Let:
608// l = (2*w * (y1 - y0), 2*w * (x0 - x1), 2*w * (x1*y0 - x0*y1))
609// m = (2*w * (y2 - y1), 2*w * (x1 - x2), 2*w * (x2*y1 - x1*y2))
610// k = (y2 - y0, x0 - x2, (x2 - x0)*y0 - (y2 - y0)*x0 )
611void calc_conic_klm(const SkPoint p[3], const SkScalar weight,
612 SkScalar k[3], SkScalar l[3], SkScalar m[3]) {
613 const SkScalar w2 = 2 * weight;
614 l[0] = w2 * (p[1].fY - p[0].fY);
615 l[1] = w2 * (p[0].fX - p[1].fX);
616 l[2] = w2 * (p[1].fX * p[0].fY - p[0].fX * p[1].fY);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000617
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000618 m[0] = w2 * (p[2].fY - p[1].fY);
619 m[1] = w2 * (p[1].fX - p[2].fX);
620 m[2] = w2 * (p[2].fX * p[1].fY - p[1].fX * p[2].fY);
621
622 k[0] = p[2].fY - p[0].fY;
623 k[1] = p[0].fX - p[2].fX;
624 k[2] = (p[2].fX - p[0].fX) * p[0].fY - (p[2].fY - p[0].fY) * p[0].fX;
625
626 // scale the max absolute value of coeffs to 10
627 SkScalar scale = 0.0f;
628 for (int i = 0; i < 3; ++i) {
629 scale = SkMaxScalar(scale, SkScalarAbs(k[i]));
630 scale = SkMaxScalar(scale, SkScalarAbs(l[i]));
631 scale = SkMaxScalar(scale, SkScalarAbs(m[i]));
632 }
633 GrAssert(scale > 0);
634 scale /= 10.0f;
635 k[0] /= scale;
636 k[1] /= scale;
637 k[2] /= scale;
638 l[0] /= scale;
639 l[1] /= scale;
640 l[2] /= scale;
641 m[0] /= scale;
642 m[1] /= scale;
643 m[2] /= scale;
644}
645
646// Equations based off of Loop-Blinn Quadratic GPU Rendering
egdaniel@google.com5383a752013-07-12 20:15:34 +0000647// Input Parametric:
648// 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)
649// Output Implicit:
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000650// f(x, y, w) = f(P) = K^2 - LM
651// K = dot(k, P), L = dot(l, P), M = dot(m, P)
652// k, l, m are calculated in function calc_conic_klm
egdaniel@google.com5383a752013-07-12 20:15:34 +0000653void set_conic_coeffs(const SkPoint p[3], Vertex verts[kVertsPerQuad], const float weight) {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000654 SkScalar k[3];
655 SkScalar l[3];
656 SkScalar m[3];
657
658 calc_conic_klm(p, weight, k, l, m);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000659
660 for (int i = 0; i < kVertsPerQuad; ++i) {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000661 const SkPoint pnt = verts[i].fPos;
662 verts[i].fConic.fK = pnt.fX * k[0] + pnt.fY * k[1] + k[2];
663 verts[i].fConic.fL = pnt.fX * l[0] + pnt.fY * l[1] + l[2];
664 verts[i].fConic.fM = pnt.fX * m[0] + pnt.fY * m[1] + m[2];
egdaniel@google.com5383a752013-07-12 20:15:34 +0000665 }
666}
667
668void add_conics(const SkPoint p[3],
669 float weight,
670 const SkMatrix* toDevice,
671 const SkMatrix* toSrc,
672 Vertex** vert,
673 SkRect* devBounds) {
674 bloat_quad(p, toDevice, toSrc, *vert, devBounds);
675 set_conic_coeffs(p, *vert, weight);
676 *vert += kVertsPerQuad;
677}
678
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000679void add_quads(const SkPoint p[3],
680 int subdiv,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000681 const SkMatrix* toDevice,
682 const SkMatrix* toSrc,
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000683 Vertex** vert,
684 SkRect* devBounds) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000685 GrAssert(subdiv >= 0);
686 if (subdiv) {
687 SkPoint newP[5];
688 SkChopQuadAtHalf(p, newP);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000689 add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert, devBounds);
690 add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert, devBounds);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000691 } else {
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000692 bloat_quad(p, toDevice, toSrc, *vert, devBounds);
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000693 set_uv_quad(p, *vert);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000694 *vert += kVertsPerQuad;
695 }
696}
697
698void add_line(const SkPoint p[2],
699 int rtHeight,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000700 const SkMatrix* toSrc,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000701 Vertex** vert) {
702 const SkPoint& a = p[0];
703 const SkPoint& b = p[1];
704
705 SkVector orthVec = b;
706 orthVec -= a;
707
708 if (orthVec.setLength(SK_Scalar1)) {
709 orthVec.setOrthog(orthVec);
710
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000711 for (int i = 0; i < kVertsPerLineSeg; ++i) {
commit-bot@chromium.org78b64ae2013-08-08 18:22:07 +0000712 (*vert)[i].fPos = (i & 0x1) ? b : a;
713 if (i & 0x2) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000714 (*vert)[i].fPos += orthVec;
commit-bot@chromium.org78b64ae2013-08-08 18:22:07 +0000715 (*vert)[i].fLine.fCoverage = 0;
716 } else if (i & 0x4) {
717 (*vert)[i].fPos -= orthVec;
718 (*vert)[i].fLine.fCoverage = 0;
719 } else {
720 (*vert)[i].fLine.fCoverage = SK_Scalar1;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000721 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000722 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000723 if (NULL != toSrc) {
724 toSrc->mapPointsWithStride(&(*vert)->fPos,
725 sizeof(Vertex),
726 kVertsPerLineSeg);
727 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000728 } else {
729 // just make it degenerate and likely offscreen
730 (*vert)[0].fPos.set(SK_ScalarMax, SK_ScalarMax);
731 (*vert)[1].fPos.set(SK_ScalarMax, SK_ScalarMax);
732 (*vert)[2].fPos.set(SK_ScalarMax, SK_ScalarMax);
733 (*vert)[3].fPos.set(SK_ScalarMax, SK_ScalarMax);
commit-bot@chromium.org78b64ae2013-08-08 18:22:07 +0000734 (*vert)[4].fPos.set(SK_ScalarMax, SK_ScalarMax);
735 (*vert)[5].fPos.set(SK_ScalarMax, SK_ScalarMax);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000736 }
737
738 *vert += kVertsPerLineSeg;
739}
740
741}
742
egdaniel@google.com5383a752013-07-12 20:15:34 +0000743/**
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000744 * Shader is based off of Loop-Blinn Quadratic GPU Rendering
egdaniel@google.com5383a752013-07-12 20:15:34 +0000745 * The output of this effect is a hairline edge for conics.
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000746 * Conics specified by implicit equation K^2 - LM.
747 * K, L, and M, are the first three values of the vertex attribute,
748 * the fourth value is not used. Distance is calculated using a
749 * first order approximation from the taylor series.
egdaniel@google.com5383a752013-07-12 20:15:34 +0000750 * Coverage is max(0, 1-distance).
751 */
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000752
753/**
754 * Test were also run using a second order distance approximation.
755 * There were two versions of the second order approx. The first version
756 * is of roughly the form:
757 * f(q) = |f(p)| - ||f'(p)||*||q-p|| - ||f''(p)||*||q-p||^2.
758 * The second is similar:
759 * f(q) = |f(p)| + ||f'(p)||*||q-p|| + ||f''(p)||*||q-p||^2.
760 * The exact version of the equations can be found in the paper
761 * "Distance Approximations for Rasterizing Implicit Curves" by Gabriel Taubin
762 *
763 * In both versions we solve the quadratic for ||q-p||.
764 * Version 1:
765 * gFM is magnitude of first partials and gFM2 is magnitude of 2nd partials (as derived from paper)
766 * builder->fsCodeAppend("\t\tedgeAlpha = (sqrt(gFM*gFM+4.0*func*gF2M) - gFM)/(2.0*gF2M);\n");
767 * Version 2:
768 * builder->fsCodeAppend("\t\tedgeAlpha = (gFM - sqrt(gFM*gFM-4.0*func*gF2M))/(2.0*gF2M);\n");
769 *
770 * Also note that 2nd partials of k,l,m are zero
771 *
772 * When comparing the two second order approximations to the first order approximations,
773 * the following results were found. Version 1 tends to underestimate the distances, thus it
774 * basically increases all the error that we were already seeing in the first order
775 * approx. So this version is not the one to use. Version 2 has the opposite effect
776 * and tends to overestimate the distances. This is much closer to what we are
777 * looking for. It is able to render ellipses (even thin ones) without the need to chop.
778 * However, it can not handle thin hyperbolas well and thus would still rely on
779 * chopping to tighten the clipping. Another side effect of the overestimating is
780 * that the curves become much thinner and "ropey". If all that was ever rendered
781 * were "not too thin" curves and ellipses then 2nd order may have an advantage since
782 * only one geometry would need to be rendered. However no benches were run comparing
783 * chopped first order and non chopped 2nd order.
784 */
egdaniel@google.com5383a752013-07-12 20:15:34 +0000785class HairConicEdgeEffect : public GrEffect {
786public:
787 static GrEffectRef* Create() {
788 GR_CREATE_STATIC_EFFECT(gHairConicEdgeEffect, HairConicEdgeEffect, ());
789 gHairConicEdgeEffect->ref();
790 return gHairConicEdgeEffect;
791 }
792
793 virtual ~HairConicEdgeEffect() {}
794
795 static const char* Name() { return "HairConicEdge"; }
796
797 virtual void getConstantColorComponents(GrColor* color,
798 uint32_t* validFlags) const SK_OVERRIDE {
799 *validFlags = 0;
800 }
801
802 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
803 return GrTBackendEffectFactory<HairConicEdgeEffect>::getInstance();
804 }
805
806 class GLEffect : public GrGLEffect {
807 public:
808 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
809 : INHERITED (factory) {}
810
811 virtual void emitCode(GrGLShaderBuilder* builder,
812 const GrDrawEffect& drawEffect,
813 EffectKey key,
814 const char* outputColor,
815 const char* inputColor,
816 const TextureSamplerArray& samplers) SK_OVERRIDE {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000817 const char *vsName, *fsName;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000818
819 SkAssertResult(builder->enableFeature(
820 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000821 builder->addVarying(kVec4f_GrSLType, "ConicCoeffs",
822 &vsName, &fsName);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000823 const SkString* attr0Name =
824 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000825 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attr0Name->c_str());
egdaniel@google.com5383a752013-07-12 20:15:34 +0000826
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000827 builder->fsCodeAppend("\t\tfloat edgeAlpha;\n");
egdaniel@google.com5383a752013-07-12 20:15:34 +0000828
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000829 builder->fsCodeAppendf("\t\tvec3 dklmdx = dFdx(%s.xyz);\n", fsName);
830 builder->fsCodeAppendf("\t\tvec3 dklmdy = dFdy(%s.xyz);\n", fsName);
831 builder->fsCodeAppendf("\t\tfloat dfdx =\n"
832 "\t\t\t2.0*%s.x*dklmdx.x - %s.y*dklmdx.z - %s.z*dklmdx.y;\n",
833 fsName, fsName, fsName);
834 builder->fsCodeAppendf("\t\tfloat dfdy =\n"
835 "\t\t\t2.0*%s.x*dklmdy.x - %s.y*dklmdy.z - %s.z*dklmdy.y;\n",
836 fsName, fsName, fsName);
837 builder->fsCodeAppend("\t\tvec2 gF = vec2(dfdx, dfdy);\n");
838 builder->fsCodeAppend("\t\tfloat gFM = sqrt(dot(gF, gF));\n");
839 builder->fsCodeAppendf("\t\tfloat func = abs(%s.x*%s.x - %s.y*%s.z);\n", fsName, fsName,
840 fsName, fsName);
841 builder->fsCodeAppend("\t\tedgeAlpha = func / gFM;\n");
842 builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
egdaniel@google.com5383a752013-07-12 20:15:34 +0000843 // Add line below for smooth cubic ramp
844 // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);\n");
845
846 SkString modulate;
847 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
848 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
849 }
850
851 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
852 return 0x0;
853 }
854
855 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
856
857 private:
858 typedef GrGLEffect INHERITED;
859 };
860
861private:
862 HairConicEdgeEffect() {
863 this->addVertexAttrib(kVec4f_GrSLType);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000864 }
865
866 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
867 return true;
868 }
869
870 GR_DECLARE_EFFECT_TEST;
871
872 typedef GrEffect INHERITED;
873};
874
875GR_DEFINE_EFFECT_TEST(HairConicEdgeEffect);
876
877GrEffectRef* HairConicEdgeEffect::TestCreate(SkMWCRandom* random,
878 GrContext*,
879 const GrDrawTargetCaps& caps,
880 GrTexture*[]) {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000881 return caps.shaderDerivativeSupport() ? HairConicEdgeEffect::Create() : NULL;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000882}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000883
884/**
885 * The output of this effect is a hairline edge for quadratics.
886 * Quadratic specified by 0=u^2-v canonical coords. u and v are the first
887 * two components of the vertex attribute. Uses unsigned distance.
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000888 * Coverage is min(0, 1-distance). 3rd & 4th component unused.
889 * Requires shader derivative instruction support.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000890 */
891class HairQuadEdgeEffect : public GrEffect {
892public:
893
894 static GrEffectRef* Create() {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000895 GR_CREATE_STATIC_EFFECT(gHairQuadEdgeEffect, HairQuadEdgeEffect, ());
896 gHairQuadEdgeEffect->ref();
897 return gHairQuadEdgeEffect;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000898 }
899
900 virtual ~HairQuadEdgeEffect() {}
901
902 static const char* Name() { return "HairQuadEdge"; }
903
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000904 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000905 uint32_t* validFlags) const SK_OVERRIDE {
906 *validFlags = 0;
907 }
908
909 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
910 return GrTBackendEffectFactory<HairQuadEdgeEffect>::getInstance();
911 }
912
913 class GLEffect : public GrGLEffect {
914 public:
915 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
916 : INHERITED (factory) {}
917
918 virtual void emitCode(GrGLShaderBuilder* builder,
919 const GrDrawEffect& drawEffect,
920 EffectKey key,
921 const char* outputColor,
922 const char* inputColor,
923 const TextureSamplerArray& samplers) SK_OVERRIDE {
924 const char *vsName, *fsName;
925 const SkString* attrName =
926 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
927 builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n");
928
929 SkAssertResult(builder->enableFeature(
egdaniel@google.com5383a752013-07-12 20:15:34 +0000930 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000931 builder->addVarying(kVec4f_GrSLType, "HairQuadEdge", &vsName, &fsName);
932
933 builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
934 builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
935 builder->fsCodeAppendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
936 "\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
937 fsName, fsName);
938 builder->fsCodeAppendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName,
939 fsName);
940 builder->fsCodeAppend("\t\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / dot(gF, gF));\n");
941 builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
942
943 SkString modulate;
bsalomon@google.com018f1792013-04-18 19:36:09 +0000944 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000945 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
946
947 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str());
948 }
949
950 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
951 return 0x0;
952 }
953
954 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
955
956 private:
957 typedef GrGLEffect INHERITED;
958 };
959
960private:
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000961 HairQuadEdgeEffect() {
962 this->addVertexAttrib(kVec4f_GrSLType);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000963 }
964
965 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
966 return true;
967 }
968
969 GR_DECLARE_EFFECT_TEST;
970
971 typedef GrEffect INHERITED;
972};
973
974GR_DEFINE_EFFECT_TEST(HairQuadEdgeEffect);
975
976GrEffectRef* HairQuadEdgeEffect::TestCreate(SkMWCRandom* random,
977 GrContext*,
978 const GrDrawTargetCaps& caps,
979 GrTexture*[]) {
980 // Doesn't work without derivative instructions.
egdaniel@google.com5383a752013-07-12 20:15:34 +0000981 return caps.shaderDerivativeSupport() ? HairQuadEdgeEffect::Create() : NULL;
982}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000983
984///////////////////////////////////////////////////////////////////////////////
985
986/**
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000987 * The output of this effect is a 1-pixel wide line.
commit-bot@chromium.org78b64ae2013-08-08 18:22:07 +0000988 * Input is coverage relative to the line.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000989 */
990class HairLineEdgeEffect : public GrEffect {
991public:
992
993 static GrEffectRef* Create() {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000994 GR_CREATE_STATIC_EFFECT(gHairLineEdge, HairLineEdgeEffect, ());
995 gHairLineEdge->ref();
996 return gHairLineEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000997 }
998
999 virtual ~HairLineEdgeEffect() {}
1000
1001 static const char* Name() { return "HairLineEdge"; }
1002
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +00001003 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +00001004 uint32_t* validFlags) const SK_OVERRIDE {
1005 *validFlags = 0;
1006 }
1007
1008 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
1009 return GrTBackendEffectFactory<HairLineEdgeEffect>::getInstance();
1010 }
1011
1012 class GLEffect : public GrGLEffect {
1013 public:
1014 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
1015 : INHERITED (factory) {}
1016
1017 virtual void emitCode(GrGLShaderBuilder* builder,
1018 const GrDrawEffect& drawEffect,
1019 EffectKey key,
1020 const char* outputColor,
1021 const char* inputColor,
1022 const TextureSamplerArray& samplers) SK_OVERRIDE {
1023 const char *vsName, *fsName;
1024 const SkString* attrName =
1025 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
1026 builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n");
1027
1028 builder->addVarying(kVec4f_GrSLType, "HairLineEdge", &vsName, &fsName);
1029
commit-bot@chromium.org78b64ae2013-08-08 18:22:07 +00001030 builder->fsCodeAppendf("\t\tedgeAlpha = %s.x;\n", fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +00001031
1032 SkString modulate;
bsalomon@google.com018f1792013-04-18 19:36:09 +00001033 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +00001034 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
1035
1036 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str());
1037 }
1038
1039 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
1040 return 0x0;
1041 }
1042
1043 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
1044
1045 private:
1046 typedef GrGLEffect INHERITED;
1047 };
1048
1049private:
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +00001050 HairLineEdgeEffect() {
1051 this->addVertexAttrib(kVec4f_GrSLType);
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +00001052 this->setWillReadFragmentPosition();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +00001053 }
1054
1055 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
1056 return true;
1057 }
1058
1059 GR_DECLARE_EFFECT_TEST;
1060
1061 typedef GrEffect INHERITED;
1062};
1063
1064GR_DEFINE_EFFECT_TEST(HairLineEdgeEffect);
1065
1066GrEffectRef* HairLineEdgeEffect::TestCreate(SkMWCRandom* random,
1067 GrContext*,
1068 const GrDrawTargetCaps& caps,
1069 GrTexture*[]) {
1070 return HairLineEdgeEffect::Create();
1071}
1072
1073///////////////////////////////////////////////////////////////////////////////
1074
robertphillips@google.com42903302013-04-20 12:26:07 +00001075namespace {
1076
1077// position + edge
1078extern const GrVertexAttrib gHairlineAttribs[] = {
1079 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
1080 {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}
1081};
robertphillips@google.com42903302013-04-20 12:26:07 +00001082};
1083
bsalomon@google.comb3729422012-03-07 19:13:28 +00001084bool GrAAHairLinePathRenderer::createGeom(
1085 const SkPath& path,
bsalomon@google.comb3729422012-03-07 19:13:28 +00001086 GrDrawTarget* target,
bsalomon@google.comb3729422012-03-07 19:13:28 +00001087 int* lineCnt,
1088 int* quadCnt,
egdaniel@google.com5383a752013-07-12 20:15:34 +00001089 int* conicCnt,
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +00001090 GrDrawTarget::AutoReleaseGeometry* arg,
1091 SkRect* devBounds) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001092 GrDrawState* drawState = target->drawState();
1093 int rtHeight = drawState->getRenderTarget()->height();
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001094
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +00001095 SkIRect devClipBounds;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +00001096 target->getClip()->getConservativeBounds(drawState->getRenderTarget(), &devClipBounds);
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001097
jvanverth@google.com9b855c72013-03-01 18:21:22 +00001098 SkMatrix viewM = drawState->getViewMatrix();
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001099
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +00001100 // All the vertices that we compute are within 1 of path control points with the exception of
1101 // one of the bounding vertices for each quad. The add_quads() function will update the bounds
1102 // for each quad added.
1103 *devBounds = path.getBounds();
1104 viewM.mapRect(devBounds);
1105 devBounds->outset(SK_Scalar1, SK_Scalar1);
1106
bsalomon@google.com92669012011-09-27 19:10:05 +00001107 PREALLOC_PTARRAY(128) lines;
1108 PREALLOC_PTARRAY(128) quads;
egdaniel@google.com5383a752013-07-12 20:15:34 +00001109 PREALLOC_PTARRAY(128) conics;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001110 IntArray qSubdivs;
egdaniel@google.com5383a752013-07-12 20:15:34 +00001111 FloatArray cWeights;
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +00001112 *quadCnt = generate_lines_and_quads(path, viewM, devClipBounds,
egdaniel@google.com5383a752013-07-12 20:15:34 +00001113 &lines, &quads, &conics, &qSubdivs, &cWeights);
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001114
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001115 *lineCnt = lines.count() / 2;
egdaniel@google.com5383a752013-07-12 20:15:34 +00001116 *conicCnt = conics.count() / 3;
1117 int vertCnt = kVertsPerLineSeg * *lineCnt + kVertsPerQuad * *quadCnt +
1118 kVertsPerQuad * *conicCnt;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001119
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +00001120 target->drawState()->setVertexAttribs<gHairlineAttribs>(SK_ARRAY_COUNT(gHairlineAttribs));
jvanverth@google.comb75b0a02013-02-05 20:33:30 +00001121 GrAssert(sizeof(Vertex) == target->getDrawState().getVertexSize());
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001122
jvanverth@google.comb75b0a02013-02-05 20:33:30 +00001123 if (!arg->set(target, vertCnt, 0)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001124 return false;
1125 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +00001126
bsalomon@google.comb3729422012-03-07 19:13:28 +00001127 Vertex* verts = reinterpret_cast<Vertex*>(arg->vertices());
1128
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001129 const SkMatrix* toDevice = NULL;
1130 const SkMatrix* toSrc = NULL;
1131 SkMatrix ivm;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +00001132
1133 if (viewM.hasPerspective()) {
1134 if (viewM.invert(&ivm)) {
1135 toDevice = &viewM;
1136 toSrc = &ivm;
1137 }
1138 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001139
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001140 for (int i = 0; i < *lineCnt; ++i) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +00001141 add_line(&lines[2*i], rtHeight, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001142 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +00001143
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001144 int unsubdivQuadCnt = quads.count() / 3;
1145 for (int i = 0; i < unsubdivQuadCnt; ++i) {
1146 GrAssert(qSubdivs[i] >= 0);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +00001147 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts, devBounds);
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001148 }
1149
egdaniel@google.com5383a752013-07-12 20:15:34 +00001150 // Start Conics
1151 for (int i = 0; i < *conicCnt; ++i) {
1152 add_conics(&conics[3*i], cWeights[i], toDevice, toSrc, &verts, devBounds);
1153 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001154 return true;
1155}
1156
robertphillips@google.com8a4fc402012-05-24 12:42:24 +00001157bool GrAAHairLinePathRenderer::canDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001158 const SkStrokeRec& stroke,
robertphillips@google.com8a4fc402012-05-24 12:42:24 +00001159 const GrDrawTarget* target,
1160 bool antiAlias) const {
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001161 if (!stroke.isHairlineStyle() || !antiAlias) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001162 return false;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001163 }
1164
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +00001165 if (SkPath::kLine_SegmentMask == path.getSegmentMasks() ||
1166 target->caps()->shaderDerivativeSupport()) {
1167 return true;
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001168 }
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +00001169 return false;
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001170}
1171
1172bool GrAAHairLinePathRenderer::onDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001173 const SkStrokeRec&,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001174 GrDrawTarget* target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001175 bool antiAlias) {
1176
1177 int lineCnt;
1178 int quadCnt;
egdaniel@google.com5383a752013-07-12 20:15:34 +00001179 int conicCnt;
bsalomon@google.comb3729422012-03-07 19:13:28 +00001180 GrDrawTarget::AutoReleaseGeometry arg;
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +00001181 SkRect devBounds;
1182
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001183 if (!this->createGeom(path,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001184 target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001185 &lineCnt,
bsalomon@google.comb3729422012-03-07 19:13:28 +00001186 &quadCnt,
egdaniel@google.com5383a752013-07-12 20:15:34 +00001187 &conicCnt,
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +00001188 &arg,
1189 &devBounds)) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001190 return false;
1191 }
1192
bsalomon@google.com137f1342013-05-29 21:27:53 +00001193 GrDrawTarget::AutoStateRestore asr;
bsalomon@google.com4647f902013-03-26 14:45:27 +00001194
bsalomon@google.coma8347462012-10-08 18:59:39 +00001195 // createGeom transforms the geometry to device space when the matrix does not have
1196 // perspective.
bsalomon@google.com137f1342013-05-29 21:27:53 +00001197 if (target->getDrawState().getViewMatrix().hasPerspective()) {
1198 asr.set(target, GrDrawTarget::kPreserve_ASRInit);
1199 } else if (!asr.setIdentity(target, GrDrawTarget::kPreserve_ASRInit)) {
1200 return false;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001201 }
bsalomon@google.com137f1342013-05-29 21:27:53 +00001202 GrDrawState* drawState = target->drawState();
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001203
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001204 // TODO: See whether rendering lines as degenerate quads improves perf
1205 // when we have a mix
bsalomon@google.coma8347462012-10-08 18:59:39 +00001206
bsalomon@google.com4647f902013-03-26 14:45:27 +00001207 static const int kEdgeAttrIndex = 1;
bsalomon@google.coma8347462012-10-08 18:59:39 +00001208
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +00001209 GrEffectRef* hairLineEffect = HairLineEdgeEffect::Create();
1210 GrEffectRef* hairQuadEffect = HairQuadEdgeEffect::Create();
egdaniel@google.com5383a752013-07-12 20:15:34 +00001211 GrEffectRef* hairConicEffect = HairConicEdgeEffect::Create();
skia.committer@gmail.com37cbc7f2013-03-27 07:01:04 +00001212
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +00001213 // Check devBounds
1214#if GR_DEBUG
1215 SkRect tolDevBounds = devBounds;
1216 tolDevBounds.outset(SK_Scalar1 / 10000, SK_Scalar1 / 10000);
1217 SkRect actualBounds;
1218 Vertex* verts = reinterpret_cast<Vertex*>(arg.vertices());
egdaniel@google.com5383a752013-07-12 20:15:34 +00001219 int vCount = kVertsPerLineSeg * lineCnt + kVertsPerQuad * quadCnt + kVertsPerQuad * conicCnt;
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +00001220 bool first = true;
1221 for (int i = 0; i < vCount; ++i) {
1222 SkPoint pos = verts[i].fPos;
1223 // This is a hack to workaround the fact that we move some degenerate segments offscreen.
1224 if (SK_ScalarMax == pos.fX) {
1225 continue;
1226 }
1227 drawState->getViewMatrix().mapPoints(&pos, 1);
1228 if (first) {
1229 actualBounds.set(pos.fX, pos.fY, pos.fX, pos.fY);
1230 first = false;
1231 } else {
1232 actualBounds.growToInclude(pos.fX, pos.fY);
1233 }
1234 }
1235 if (!first) {
1236 GrAssert(tolDevBounds.contains(actualBounds));
1237 }
1238#endif
1239
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001240 {
commit-bot@chromium.org78b64ae2013-08-08 18:22:07 +00001241 // the fact we're using an effect to pass per-vertex coverage is kind of dumb,
1242 // but necessary due to the shared vertex buffer
1243 // TODO: refactor so that we render lines with a separate vertex buffer
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001244 GrDrawState::AutoRestoreEffects are(drawState);
1245 target->setIndexSourceToBuffer(fLinesIndexBuffer);
1246 int lines = 0;
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001247 drawState->addCoverageEffect(hairLineEffect, kEdgeAttrIndex)->unref();
1248 while (lines < lineCnt) {
commit-bot@chromium.org78b64ae2013-08-08 18:22:07 +00001249 int n = GrMin(lineCnt - lines, kNumLineSegsInIdxBuffer);
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001250 target->drawIndexed(kTriangles_GrPrimitiveType,
1251 kVertsPerLineSeg*lines, // startV
1252 0, // startI
1253 kVertsPerLineSeg*n, // vCount
1254 kIdxsPerLineSeg*n,
1255 &devBounds); // iCount
1256 lines += n;
1257 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001258 }
1259
egdaniel@google.com5383a752013-07-12 20:15:34 +00001260 {
1261 GrDrawState::AutoRestoreEffects are(drawState);
1262 target->setIndexSourceToBuffer(fQuadsIndexBuffer);
1263 int quads = 0;
1264 drawState->addCoverageEffect(hairQuadEffect, kEdgeAttrIndex)->unref();
1265 while (quads < quadCnt) {
1266 int n = GrMin(quadCnt - quads, kNumQuadsInIdxBuffer);
1267 target->drawIndexed(kTriangles_GrPrimitiveType,
1268 kVertsPerLineSeg * lineCnt + kVertsPerQuad*quads, // startV
1269 0, // startI
1270 kVertsPerQuad*n, // vCount
1271 kIdxsPerQuad*n, // iCount
1272 &devBounds);
1273 quads += n;
1274 }
1275 }
1276
1277 {
1278 GrDrawState::AutoRestoreEffects are(drawState);
1279 int conics = 0;
1280 drawState->addCoverageEffect(hairConicEffect, 1, 2)->unref();
1281 while (conics < conicCnt) {
1282 int n = GrMin(conicCnt - conics, kNumQuadsInIdxBuffer);
1283 target->drawIndexed(kTriangles_GrPrimitiveType,
1284 kVertsPerLineSeg*lineCnt +
1285 kVertsPerQuad*(quadCnt + conics), // startV
1286 0, // startI
1287 kVertsPerQuad*n, // vCount
1288 kIdxsPerQuad*n, // iCount
1289 &devBounds);
1290 conics += n;
1291 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001292 }
bsalomon@google.com0406b9e2013-04-02 21:00:15 +00001293 target->resetIndexSource();
bsalomon@google.com4647f902013-03-26 14:45:27 +00001294
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001295 return true;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001296}