blob: 332c6bb5a14055f2728b7d4fe5d77b180ec93368 [file] [log] [blame]
bsalomon@google.comf75b84e2011-09-29 14:58:28 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8
bsalomon@google.comaeb21602011-08-30 18:13:44 +00009#include "GrAAHairLinePathRenderer.h"
10
11#include "GrContext.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000012#include "GrDrawState.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000013#include "GrDrawTargetCaps.h"
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000014#include "GrEffect.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000015#include "GrGpu.h"
16#include "GrIndexBuffer.h"
bsalomon@google.comdbeeac32011-09-12 14:59:34 +000017#include "GrPathUtils.h"
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000018#include "GrTBackendEffectFactory.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000019#include "SkGeometry.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000020#include "SkStroke.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000021#include "SkTemplates.h"
22
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000023#include "gl/GrGLEffect.h"
24#include "gl/GrGLSL.h"
bsalomon@google.com4647f902013-03-26 14:45:27 +000025
bsalomon@google.comaeb21602011-08-30 18:13:44 +000026namespace {
27// quadratics are rendered as 5-sided polys in order to bound the
28// AA stroke around the center-curve. See comments in push_quad_index_buffer and
egdaniel@google.com5383a752013-07-12 20:15:34 +000029// bloat_quad. Quadratics and conics share an index buffer
bsalomon@google.comaeb21602011-08-30 18:13:44 +000030static const int kVertsPerQuad = 5;
31static const int kIdxsPerQuad = 9;
32
33static const int kVertsPerLineSeg = 4;
34static const int kIdxsPerLineSeg = 6;
35
36static const int kNumQuadsInIdxBuffer = 256;
37static const size_t kQuadIdxSBufize = kIdxsPerQuad *
38 sizeof(uint16_t) *
39 kNumQuadsInIdxBuffer;
40
41bool push_quad_index_data(GrIndexBuffer* qIdxBuffer) {
42 uint16_t* data = (uint16_t*) qIdxBuffer->lock();
43 bool tempData = NULL == data;
44 if (tempData) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000045 data = SkNEW_ARRAY(uint16_t, kNumQuadsInIdxBuffer * kIdxsPerQuad);
bsalomon@google.comaeb21602011-08-30 18:13:44 +000046 }
47 for (int i = 0; i < kNumQuadsInIdxBuffer; ++i) {
48
49 // Each quadratic is rendered as a five sided polygon. This poly bounds
50 // the quadratic's bounding triangle but has been expanded so that the
51 // 1-pixel wide area around the curve is inside the poly.
52 // If a,b,c are the original control points then the poly a0,b0,c0,c1,a1
53 // that is rendered would look like this:
54 // b0
55 // b
56 //
57 // a0 c0
58 // a c
59 // a1 c1
bsalomon@google.com0e5104c2012-04-10 16:20:41 +000060 // Each is drawn as three triangles specified by these 9 indices:
bsalomon@google.comaeb21602011-08-30 18:13:44 +000061 int baseIdx = i * kIdxsPerQuad;
62 uint16_t baseVert = (uint16_t)(i * kVertsPerQuad);
63 data[0 + baseIdx] = baseVert + 0; // a0
64 data[1 + baseIdx] = baseVert + 1; // a1
65 data[2 + baseIdx] = baseVert + 2; // b0
66 data[3 + baseIdx] = baseVert + 2; // b0
67 data[4 + baseIdx] = baseVert + 4; // c1
68 data[5 + baseIdx] = baseVert + 3; // c0
69 data[6 + baseIdx] = baseVert + 1; // a1
70 data[7 + baseIdx] = baseVert + 4; // c1
71 data[8 + baseIdx] = baseVert + 2; // b0
72 }
73 if (tempData) {
74 bool ret = qIdxBuffer->updateData(data, kQuadIdxSBufize);
75 delete[] data;
76 return ret;
77 } else {
78 qIdxBuffer->unlock();
79 return true;
80 }
81}
82}
83
84GrPathRenderer* GrAAHairLinePathRenderer::Create(GrContext* context) {
bsalomon@google.coma8a6a322011-09-23 14:19:58 +000085 const GrIndexBuffer* lIdxBuffer = context->getQuadIndexBuffer();
86 if (NULL == lIdxBuffer) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +000087 return NULL;
88 }
bsalomon@google.coma8a6a322011-09-23 14:19:58 +000089 GrGpu* gpu = context->getGpu();
90 GrIndexBuffer* qIdxBuf = gpu->createIndexBuffer(kQuadIdxSBufize, false);
91 SkAutoTUnref<GrIndexBuffer> qIdxBuffer(qIdxBuf);
92 if (NULL == qIdxBuf ||
93 !push_quad_index_data(qIdxBuf)) {
94 return NULL;
95 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +000096 return SkNEW_ARGS(GrAAHairLinePathRenderer,
97 (context, lIdxBuffer, qIdxBuf));
bsalomon@google.comaeb21602011-08-30 18:13:44 +000098}
99
100GrAAHairLinePathRenderer::GrAAHairLinePathRenderer(
101 const GrContext* context,
102 const GrIndexBuffer* linesIndexBuffer,
103 const GrIndexBuffer* quadsIndexBuffer) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000104 fLinesIndexBuffer = linesIndexBuffer;
105 linesIndexBuffer->ref();
106 fQuadsIndexBuffer = quadsIndexBuffer;
107 quadsIndexBuffer->ref();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000108}
109
110GrAAHairLinePathRenderer::~GrAAHairLinePathRenderer() {
111 fLinesIndexBuffer->unref();
112 fQuadsIndexBuffer->unref();
113}
114
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000115namespace {
116
bsalomon@google.com49313f62011-09-14 13:54:05 +0000117typedef SkTArray<SkPoint, true> PtArray;
bsalomon@google.com92669012011-09-27 19:10:05 +0000118#define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true>
bsalomon@google.com49313f62011-09-14 13:54:05 +0000119typedef SkTArray<int, true> IntArray;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000120typedef SkTArray<float, true> FloatArray;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000121
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000122// Takes 178th time of logf on Z600 / VC2010
123int get_float_exp(float x) {
124 GR_STATIC_ASSERT(sizeof(int) == sizeof(float));
125#if GR_DEBUG
126 static bool tested;
127 if (!tested) {
128 tested = true;
129 GrAssert(get_float_exp(0.25f) == -2);
130 GrAssert(get_float_exp(0.3f) == -2);
131 GrAssert(get_float_exp(0.5f) == -1);
132 GrAssert(get_float_exp(1.f) == 0);
133 GrAssert(get_float_exp(2.f) == 1);
134 GrAssert(get_float_exp(2.5f) == 1);
135 GrAssert(get_float_exp(8.f) == 3);
136 GrAssert(get_float_exp(100.f) == 6);
137 GrAssert(get_float_exp(1000.f) == 9);
138 GrAssert(get_float_exp(1024.f) == 10);
139 GrAssert(get_float_exp(3000000.f) == 21);
140 }
141#endif
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000142 const int* iptr = (const int*)&x;
143 return (((*iptr) & 0x7f800000) >> 23) - 127;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000144}
145
egdaniel@google.com5383a752013-07-12 20:15:34 +0000146// Uses the max curvature function for quads to estimate
147// where to chop the conic. If the max curvature is not
148// found along the curve segment it will return 1 and
149// dst[0] is the orginal conic. If it returns 2 the dst[0]
150// and dst[1] are the two new conics.
151int chop_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
152 SkScalar t = SkFindQuadMaxCurvature(src);
153 if (t == 0) {
154 if (dst) {
155 dst[0].set(src, weight);
156 }
157 return 1;
158 } else {
159 if (dst) {
160 SkConic conic;
161 conic.set(src, weight);
162 conic.chopAt(t, dst);
163 }
164 return 2;
165 }
166}
167
168// returns 0 if quad/conic is degen or close to it
169// in this case approx the path with lines
170// otherwise returns 1
171int is_degen_quad_or_conic(const SkPoint p[3]) {
172 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
173 static const SkScalar gDegenerateToLineTolSqd =
174 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
175
176 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
177 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
178 return 1;
179 }
180
181 SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
182 if (dsqd < gDegenerateToLineTolSqd) {
183 return 1;
184 }
185
186 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
187 return 1;
188 }
189 return 0;
190}
191
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000192// we subdivide the quads to avoid huge overfill
193// if it returns -1 then should be drawn as lines
194int num_quad_subdivs(const SkPoint p[3]) {
195 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000196 static const SkScalar gDegenerateToLineTolSqd =
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000197 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000198
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000199 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
200 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000201 return -1;
202 }
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000203
bsalomon@google.com81712882012-11-01 17:12:34 +0000204 SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000205 if (dsqd < gDegenerateToLineTolSqd) {
206 return -1;
207 }
208
209 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000210 return -1;
211 }
212
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000213 // tolerance of triangle height in pixels
214 // tuned on windows Quadro FX 380 / Z600
215 // trade off of fill vs cpu time on verts
216 // maybe different when do this using gpu (geo or tess shaders)
217 static const SkScalar gSubdivTol = 175 * SK_Scalar1;
218
robertphillips@google.com7460b372012-04-25 16:54:51 +0000219 if (dsqd <= SkScalarMul(gSubdivTol, gSubdivTol)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000220 return 0;
221 } else {
robertphillips@google.com87379e12013-03-29 12:11:10 +0000222 static const int kMaxSub = 4;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000223 // subdividing the quad reduces d by 4. so we want x = log4(d/tol)
224 // = log4(d*d/tol*tol)/2
225 // = log2(d*d/tol*tol)
226
227#ifdef SK_SCALAR_IS_FLOAT
228 // +1 since we're ignoring the mantissa contribution.
229 int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
230 log = GrMin(GrMax(0, log), kMaxSub);
231 return log;
232#else
robertphillips@google.com7460b372012-04-25 16:54:51 +0000233 SkScalar log = SkScalarLog(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000234 SkScalarDiv(dsqd,
robertphillips@google.com7460b372012-04-25 16:54:51 +0000235 SkScalarMul(gSubdivTol, gSubdivTol)));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000236 static const SkScalar conv = SkScalarInvert(SkScalarLog(2));
237 log = SkScalarMul(log, conv);
238 return GrMin(GrMax(0, SkScalarCeilToInt(log)),kMaxSub);
239#endif
240 }
241}
242
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000243/**
244 * Generates the lines and quads to be rendered. Lines are always recorded in
245 * device space. We will do a device space bloat to account for the 1pixel
246 * thickness.
247 * Quads are recorded in device space unless m contains
248 * perspective, then in they are in src space. We do this because we will
249 * subdivide large quads to reduce over-fill. This subdivision has to be
250 * performed before applying the perspective matrix.
251 */
252int generate_lines_and_quads(const SkPath& path,
253 const SkMatrix& m,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000254 const GrIRect& devClipBounds,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000255 PtArray* lines,
256 PtArray* quads,
egdaniel@google.com5383a752013-07-12 20:15:34 +0000257 PtArray* conics,
258 IntArray* quadSubdivCnts,
259 FloatArray* conicWeights) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000260 SkPath::Iter iter(path, false);
261
262 int totalQuadCount = 0;
263 GrRect bounds;
264 GrIRect ibounds;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000265
266 bool persp = m.hasPerspective();
267
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000268 for (;;) {
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000269 GrPoint pathPts[4];
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000270 GrPoint devPts[4];
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000271 SkPath::Verb verb = iter.next(pathPts);
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000272 switch (verb) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000273 case SkPath::kConic_Verb: {
274 SkConic dst[2];
275 int conicCnt = chop_conic(pathPts, dst, iter.conicWeight());
276 for (int i = 0; i < conicCnt; ++i) {
277 SkPoint* chopPnts = dst[i].fPts;
278 m.mapPoints(devPts, chopPnts, 3);
279 bounds.setBounds(devPts, 3);
280 bounds.outset(SK_Scalar1, SK_Scalar1);
281 bounds.roundOut(&ibounds);
282 if (SkIRect::Intersects(devClipBounds, ibounds)) {
283 if (is_degen_quad_or_conic(devPts)) {
284 SkPoint* pts = lines->push_back_n(4);
285 pts[0] = devPts[0];
286 pts[1] = devPts[1];
287 pts[2] = devPts[1];
288 pts[3] = devPts[2];
289 } else {
290 // when in perspective keep conics in src space
291 SkPoint* cPts = persp ? chopPnts : devPts;
292 SkPoint* pts = conics->push_back_n(3);
293 pts[0] = cPts[0];
294 pts[1] = cPts[1];
295 pts[2] = cPts[2];
296 conicWeights->push_back() = dst[i].fW;
297 }
298 }
299 }
reed@google.com277c3f82013-05-31 15:17:50 +0000300 break;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000301 }
302 case SkPath::kMove_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000303 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000304 case SkPath::kLine_Verb:
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000305 m.mapPoints(devPts, pathPts, 2);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000306 bounds.setBounds(devPts, 2);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000307 bounds.outset(SK_Scalar1, SK_Scalar1);
308 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000309 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000310 SkPoint* pts = lines->push_back_n(2);
311 pts[0] = devPts[0];
312 pts[1] = devPts[1];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000313 }
314 break;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000315 case SkPath::kQuad_Verb: {
316 SkPoint choppedPts[5];
317 // Chopping the quad helps when the quad is either degenerate or nearly degenerate.
318 // When it is degenerate it allows the approximation with lines to work since the
319 // chop point (if there is one) will be at the parabola's vertex. In the nearly
320 // degenerate the QuadUVMatrix computed for the points is almost singular which
321 // can cause rendering artifacts.
322 int n = SkChopQuadAtMaxCurvature(pathPts, choppedPts);
323 for (int i = 0; i < n; ++i) {
324 SkPoint* quadPts = choppedPts + i * 2;
325 m.mapPoints(devPts, quadPts, 3);
326 bounds.setBounds(devPts, 3);
327 bounds.outset(SK_Scalar1, SK_Scalar1);
328 bounds.roundOut(&ibounds);
329
330 if (SkIRect::Intersects(devClipBounds, ibounds)) {
331 int subdiv = num_quad_subdivs(devPts);
332 GrAssert(subdiv >= -1);
333 if (-1 == subdiv) {
334 SkPoint* pts = lines->push_back_n(4);
335 pts[0] = devPts[0];
336 pts[1] = devPts[1];
337 pts[2] = devPts[1];
338 pts[3] = devPts[2];
339 } else {
340 // when in perspective keep quads in src space
341 SkPoint* qPts = persp ? quadPts : devPts;
342 SkPoint* pts = quads->push_back_n(3);
343 pts[0] = qPts[0];
344 pts[1] = qPts[1];
345 pts[2] = qPts[2];
346 quadSubdivCnts->push_back() = subdiv;
347 totalQuadCount += 1 << subdiv;
348 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000349 }
350 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000351 break;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000352 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000353 case SkPath::kCubic_Verb:
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000354 m.mapPoints(devPts, pathPts, 4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000355 bounds.setBounds(devPts, 4);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000356 bounds.outset(SK_Scalar1, SK_Scalar1);
357 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000358 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000359 PREALLOC_PTARRAY(32) q;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000360 // we don't need a direction if we aren't constraining the subdivision
361 static const SkPath::Direction kDummyDir = SkPath::kCCW_Direction;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000362 // We convert cubics to quadratics (for now).
363 // In perspective have to do conversion in src space.
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000364 if (persp) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000365 SkScalar tolScale =
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000366 GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m,
367 path.getBounds());
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000368 GrPathUtils::convertCubicToQuads(pathPts, tolScale, false, kDummyDir, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000369 } else {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000370 GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, false, kDummyDir, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000371 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000372 for (int i = 0; i < q.count(); i += 3) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000373 SkPoint* qInDevSpace;
374 // bounds has to be calculated in device space, but q is
375 // in src space when there is perspective.
376 if (persp) {
377 m.mapPoints(devPts, &q[i], 3);
378 bounds.setBounds(devPts, 3);
379 qInDevSpace = devPts;
380 } else {
381 bounds.setBounds(&q[i], 3);
382 qInDevSpace = &q[i];
383 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000384 bounds.outset(SK_Scalar1, SK_Scalar1);
385 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000386 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000387 int subdiv = num_quad_subdivs(qInDevSpace);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000388 GrAssert(subdiv >= -1);
389 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000390 SkPoint* pts = lines->push_back_n(4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000391 // lines should always be in device coords
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000392 pts[0] = qInDevSpace[0];
393 pts[1] = qInDevSpace[1];
394 pts[2] = qInDevSpace[1];
395 pts[3] = qInDevSpace[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000396 } else {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000397 SkPoint* pts = quads->push_back_n(3);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000398 // q is already in src space when there is no
399 // perspective and dev coords otherwise.
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000400 pts[0] = q[0 + i];
401 pts[1] = q[1 + i];
402 pts[2] = q[2 + i];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000403 quadSubdivCnts->push_back() = subdiv;
404 totalQuadCount += 1 << subdiv;
405 }
406 }
407 }
408 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000409 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000410 case SkPath::kClose_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000411 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000412 case SkPath::kDone_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000413 return totalQuadCount;
414 }
415 }
416}
417
418struct Vertex {
419 GrPoint fPos;
420 union {
421 struct {
bsalomon@google.com81712882012-11-01 17:12:34 +0000422 SkScalar fA;
423 SkScalar fB;
424 SkScalar fC;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000425 } fLine;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000426 struct {
427 SkScalar fA;
428 SkScalar fB;
429 SkScalar fC;
430 SkScalar fD;
431 SkScalar fE;
432 SkScalar fF;
433 } fConic;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000434 GrVec fQuadCoord;
435 struct {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000436 SkScalar fBogus[6];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000437 };
438 };
439};
egdaniel@google.com5383a752013-07-12 20:15:34 +0000440
441GR_STATIC_ASSERT(sizeof(Vertex) == 4 * sizeof(GrPoint));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000442
443void intersect_lines(const SkPoint& ptA, const SkVector& normA,
444 const SkPoint& ptB, const SkVector& normB,
445 SkPoint* result) {
446
447 SkScalar lineAW = -normA.dot(ptA);
448 SkScalar lineBW = -normB.dot(ptB);
449
450 SkScalar wInv = SkScalarMul(normA.fX, normB.fY) -
egdaniel@google.com5383a752013-07-12 20:15:34 +0000451 SkScalarMul(normA.fY, normB.fX);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000452 wInv = SkScalarInvert(wInv);
453
454 result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY);
455 result->fX = SkScalarMul(result->fX, wInv);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000456
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000457 result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW);
458 result->fY = SkScalarMul(result->fY, wInv);
459}
460
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000461void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice,
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000462 const SkMatrix* toSrc, Vertex verts[kVertsPerQuad],
463 SkRect* devBounds) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000464 GrAssert(!toDevice == !toSrc);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000465 // original quad is specified by tri a,b,c
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000466 SkPoint a = qpts[0];
467 SkPoint b = qpts[1];
468 SkPoint c = qpts[2];
469
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000470 // this should be in the src space, not dev coords, when we have perspective
bsalomon@google.com19713172012-03-15 13:51:08 +0000471 GrPathUtils::QuadUVMatrix DevToUV(qpts);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000472
473 if (toDevice) {
474 toDevice->mapPoints(&a, 1);
475 toDevice->mapPoints(&b, 1);
476 toDevice->mapPoints(&c, 1);
477 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000478 // make a new poly where we replace a and c by a 1-pixel wide edges orthog
479 // to edges ab and bc:
480 //
481 // before | after
482 // | b0
483 // b |
484 // |
485 // | a0 c0
486 // a c | a1 c1
487 //
488 // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c,
489 // respectively.
490 Vertex& a0 = verts[0];
491 Vertex& a1 = verts[1];
492 Vertex& b0 = verts[2];
493 Vertex& c0 = verts[3];
494 Vertex& c1 = verts[4];
495
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000496 SkVector ab = b;
497 ab -= a;
498 SkVector ac = c;
499 ac -= a;
500 SkVector cb = b;
501 cb -= c;
502
503 // We should have already handled degenerates
504 GrAssert(ab.length() > 0 && cb.length() > 0);
505
506 ab.normalize();
507 SkVector abN;
508 abN.setOrthog(ab, SkVector::kLeft_Side);
509 if (abN.dot(ac) > 0) {
510 abN.negate();
511 }
512
513 cb.normalize();
514 SkVector cbN;
515 cbN.setOrthog(cb, SkVector::kLeft_Side);
516 if (cbN.dot(ac) < 0) {
517 cbN.negate();
518 }
519
520 a0.fPos = a;
521 a0.fPos += abN;
522 a1.fPos = a;
523 a1.fPos -= abN;
524
525 c0.fPos = c;
526 c0.fPos += cbN;
527 c1.fPos = c;
528 c1.fPos -= cbN;
529
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000530 // This point may not be within 1 pixel of a control point. We update the bounding box to
531 // include it.
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000532 intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000533 devBounds->growToInclude(b0.fPos.fX, b0.fPos.fY);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000534
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000535 if (toSrc) {
536 toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(Vertex), kVertsPerQuad);
537 }
bsalomon@google.com19713172012-03-15 13:51:08 +0000538 DevToUV.apply<kVertsPerQuad, sizeof(Vertex), sizeof(GrPoint)>(verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000539}
540
egdaniel@google.com5383a752013-07-12 20:15:34 +0000541
542// Input Parametric:
543// 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)
544// Output Implicit:
545// Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0
546// A = 4w^2*(y0-y1)(y1-y2)-(y0-y2)^2
547// B = 4w^2*((x0-x1)(y2-y1)+(x1-x2)(y1-y0)) + 2(x0-x2)(y0-y2)
548// C = 4w^2(x0-x1)(x1-x2) - (x0-x2)^2
549// D = 4w^2((x0y1-x1y0)(y1-y2)+(x1y2-x2y1)(y0-y1)) + 2(y2-y0)(x0y2-x2y0)
550// E = 4w^2((y0x1-y1x0)(x1-x2)+(y1x2-y2x1)(x0-x1)) + 2(x2-x0)(y0x2-y2x0)
551// F = 4w^2(x1y2-x2y1)(x0y1-x1y0) - (x2y0-x0y2)^2
552
553void set_conic_coeffs(const SkPoint p[3], Vertex verts[kVertsPerQuad], const float weight) {
554 const float ww4 = 4 * weight * weight;
555 const float x0Mx1 = p[0].fX - p[1].fX;
556 const float x1Mx2 = p[1].fX - p[2].fX;
557 const float x0Mx2 = p[0].fX - p[2].fX;
558 const float y0My1 = p[0].fY - p[1].fY;
559 const float y1My2 = p[1].fY - p[2].fY;
560 const float y0My2 = p[0].fY - p[2].fY;
561 const float x0y1Mx1y0 = p[0].fX*p[1].fY - p[1].fX*p[0].fY;
562 const float x1y2Mx2y1 = p[1].fX*p[2].fY - p[2].fX*p[1].fY;
563 const float x0y2Mx2y0 = p[0].fX*p[2].fY - p[2].fX*p[0].fY;
564 const float a = ww4 * y0My1 * y1My2 - y0My2 * y0My2;
565 const float b = -ww4 * (x0Mx1 * y1My2 + x1Mx2 * y0My1) + 2 * x0Mx2 * y0My2;
566 const float c = ww4 * x0Mx1 * x1Mx2 - x0Mx2 * x0Mx2;
567 const float d = ww4 * (x0y1Mx1y0 * y1My2 + x1y2Mx2y1 * y0My1) - 2 * y0My2 * x0y2Mx2y0;
568 const float e = -ww4 * (x0y1Mx1y0 * x1Mx2 + x1y2Mx2y1 * x0Mx1) + 2 * x0Mx2 * x0y2Mx2y0;
569 const float f = ww4 * x1y2Mx2y1 * x0y1Mx1y0 - x0y2Mx2y0 * x0y2Mx2y0;
570
571 for (int i = 0; i < kVertsPerQuad; ++i) {
572 verts[i].fConic.fA = a/f;
573 verts[i].fConic.fB = b/f;
574 verts[i].fConic.fC = c/f;
575 verts[i].fConic.fD = d/f;
576 verts[i].fConic.fE = e/f;
577 verts[i].fConic.fF = f/f;
578 }
579}
580
581void add_conics(const SkPoint p[3],
582 float weight,
583 const SkMatrix* toDevice,
584 const SkMatrix* toSrc,
585 Vertex** vert,
586 SkRect* devBounds) {
587 bloat_quad(p, toDevice, toSrc, *vert, devBounds);
588 set_conic_coeffs(p, *vert, weight);
589 *vert += kVertsPerQuad;
590}
591
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000592void add_quads(const SkPoint p[3],
593 int subdiv,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000594 const SkMatrix* toDevice,
595 const SkMatrix* toSrc,
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000596 Vertex** vert,
597 SkRect* devBounds) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000598 GrAssert(subdiv >= 0);
599 if (subdiv) {
600 SkPoint newP[5];
601 SkChopQuadAtHalf(p, newP);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000602 add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert, devBounds);
603 add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert, devBounds);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000604 } else {
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000605 bloat_quad(p, toDevice, toSrc, *vert, devBounds);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000606 *vert += kVertsPerQuad;
607 }
608}
609
610void add_line(const SkPoint p[2],
611 int rtHeight,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000612 const SkMatrix* toSrc,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000613 Vertex** vert) {
614 const SkPoint& a = p[0];
615 const SkPoint& b = p[1];
616
617 SkVector orthVec = b;
618 orthVec -= a;
619
620 if (orthVec.setLength(SK_Scalar1)) {
621 orthVec.setOrthog(orthVec);
622
bsalomon@google.com706f6682012-10-23 14:53:55 +0000623 SkScalar lineC = -(a.dot(orthVec));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000624 for (int i = 0; i < kVertsPerLineSeg; ++i) {
625 (*vert)[i].fPos = (i < 2) ? a : b;
626 if (0 == i || 3 == i) {
627 (*vert)[i].fPos -= orthVec;
628 } else {
629 (*vert)[i].fPos += orthVec;
630 }
bsalomon@google.com706f6682012-10-23 14:53:55 +0000631 (*vert)[i].fLine.fA = orthVec.fX;
632 (*vert)[i].fLine.fB = orthVec.fY;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000633 (*vert)[i].fLine.fC = lineC;
634 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000635 if (NULL != toSrc) {
636 toSrc->mapPointsWithStride(&(*vert)->fPos,
637 sizeof(Vertex),
638 kVertsPerLineSeg);
639 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000640 } else {
641 // just make it degenerate and likely offscreen
642 (*vert)[0].fPos.set(SK_ScalarMax, SK_ScalarMax);
643 (*vert)[1].fPos.set(SK_ScalarMax, SK_ScalarMax);
644 (*vert)[2].fPos.set(SK_ScalarMax, SK_ScalarMax);
645 (*vert)[3].fPos.set(SK_ScalarMax, SK_ScalarMax);
646 }
647
648 *vert += kVertsPerLineSeg;
649}
650
651}
652
egdaniel@google.com5383a752013-07-12 20:15:34 +0000653/**
654 * The output of this effect is a hairline edge for conics.
655 * Conics specified by implicit equation Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0.
656 * A, B, C, D are the first vec4 of vertex attributes and
657 * E and F are the vec2 attached to 2nd vertex attrribute.
658 * Coverage is max(0, 1-distance).
659 */
660class HairConicEdgeEffect : public GrEffect {
661public:
662 static GrEffectRef* Create() {
663 GR_CREATE_STATIC_EFFECT(gHairConicEdgeEffect, HairConicEdgeEffect, ());
664 gHairConicEdgeEffect->ref();
665 return gHairConicEdgeEffect;
666 }
667
668 virtual ~HairConicEdgeEffect() {}
669
670 static const char* Name() { return "HairConicEdge"; }
671
672 virtual void getConstantColorComponents(GrColor* color,
673 uint32_t* validFlags) const SK_OVERRIDE {
674 *validFlags = 0;
675 }
676
677 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
678 return GrTBackendEffectFactory<HairConicEdgeEffect>::getInstance();
679 }
680
681 class GLEffect : public GrGLEffect {
682 public:
683 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
684 : INHERITED (factory) {}
685
686 virtual void emitCode(GrGLShaderBuilder* builder,
687 const GrDrawEffect& drawEffect,
688 EffectKey key,
689 const char* outputColor,
690 const char* inputColor,
691 const TextureSamplerArray& samplers) SK_OVERRIDE {
692 const char *vsCoeffABCDName, *fsCoeffABCDName;
693 const char *vsCoeffEFName, *fsCoeffEFName;
694
695 SkAssertResult(builder->enableFeature(
696 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
697 builder->addVarying(kVec4f_GrSLType, "ConicCoeffsABCD",
698 &vsCoeffABCDName, &fsCoeffABCDName);
699 const SkString* attr0Name =
700 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
701 builder->vsCodeAppendf("\t%s = %s;\n", vsCoeffABCDName, attr0Name->c_str());
702
703 builder->addVarying(kVec2f_GrSLType, "ConicCoeffsEF",
704 &vsCoeffEFName, &fsCoeffEFName);
705 const SkString* attr1Name =
706 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]);
707 builder->vsCodeAppendf("\t%s = %s;\n", vsCoeffEFName, attr1Name->c_str());
708
709 // Based on Gustavson 2006: "Beyond the Pixel: towards infinite resolution textures"
710 builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n");
711
712 builder->fsCodeAppendf("\t\tvec3 uv1 = vec3(%s.xy, 1);\n", builder->fragmentPosition());
713 builder->fsCodeAppend("\t\tvec3 u2uvv2 = uv1.xxy * uv1.xyy;\n");
714 builder->fsCodeAppendf("\t\tvec3 ABC = %s.xyz;\n", fsCoeffABCDName);
715 builder->fsCodeAppendf("\t\tvec3 DEF = vec3(%s.w, %s.xy);\n",
716 fsCoeffABCDName, fsCoeffEFName);
717
718 builder->fsCodeAppend("\t\tfloat dfdx = dot(uv1,vec3(2.0*ABC.x,ABC.y,DEF.x));\n");
719 builder->fsCodeAppend("\t\tfloat dfdy = dot(uv1,vec3(ABC.y, 2.0*ABC.z,DEF.y));\n");
720 builder->fsCodeAppend("\t\tfloat gF = dfdx*dfdx + dfdy*dfdy;\n");
721 builder->fsCodeAppend("\t\tedgeAlpha = dot(ABC,u2uvv2) + dot(DEF,uv1);\n");
722 builder->fsCodeAppend("\t\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / gF);\n");
723 builder->fsCodeAppend("\t\tedgeAlpha = max((1.0 - edgeAlpha), 0.0);\n");
724 // Add line below for smooth cubic ramp
725 // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);\n");
726
727 SkString modulate;
728 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
729 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
730 }
731
732 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
733 return 0x0;
734 }
735
736 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
737
738 private:
739 typedef GrGLEffect INHERITED;
740 };
741
742private:
743 HairConicEdgeEffect() {
744 this->addVertexAttrib(kVec4f_GrSLType);
745 this->addVertexAttrib(kVec2f_GrSLType);
746 this->setWillReadFragmentPosition();
747 }
748
749 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
750 return true;
751 }
752
753 GR_DECLARE_EFFECT_TEST;
754
755 typedef GrEffect INHERITED;
756};
757
758GR_DEFINE_EFFECT_TEST(HairConicEdgeEffect);
759
760GrEffectRef* HairConicEdgeEffect::TestCreate(SkMWCRandom* random,
761 GrContext*,
762 const GrDrawTargetCaps& caps,
763 GrTexture*[]) {
764 return HairConicEdgeEffect::Create();
765}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000766///////////////////////////////////////////////////////////////////////////////
767
768/**
769 * The output of this effect is a hairline edge for quadratics.
770 * Quadratic specified by 0=u^2-v canonical coords. u and v are the first
771 * two components of the vertex attribute. Uses unsigned distance.
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000772 * Coverage is min(0, 1-distance). 3rd & 4th component unused.
773 * Requires shader derivative instruction support.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000774 */
775class HairQuadEdgeEffect : public GrEffect {
776public:
777
778 static GrEffectRef* Create() {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000779 GR_CREATE_STATIC_EFFECT(gHairQuadEdgeEffect, HairQuadEdgeEffect, ());
780 gHairQuadEdgeEffect->ref();
781 return gHairQuadEdgeEffect;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000782 }
783
784 virtual ~HairQuadEdgeEffect() {}
785
786 static const char* Name() { return "HairQuadEdge"; }
787
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000788 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000789 uint32_t* validFlags) const SK_OVERRIDE {
790 *validFlags = 0;
791 }
792
793 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
794 return GrTBackendEffectFactory<HairQuadEdgeEffect>::getInstance();
795 }
796
797 class GLEffect : public GrGLEffect {
798 public:
799 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
800 : INHERITED (factory) {}
801
802 virtual void emitCode(GrGLShaderBuilder* builder,
803 const GrDrawEffect& drawEffect,
804 EffectKey key,
805 const char* outputColor,
806 const char* inputColor,
807 const TextureSamplerArray& samplers) SK_OVERRIDE {
808 const char *vsName, *fsName;
809 const SkString* attrName =
810 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
811 builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n");
812
813 SkAssertResult(builder->enableFeature(
egdaniel@google.com5383a752013-07-12 20:15:34 +0000814 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000815 builder->addVarying(kVec4f_GrSLType, "HairQuadEdge", &vsName, &fsName);
816
817 builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
818 builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
819 builder->fsCodeAppendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
820 "\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
821 fsName, fsName);
822 builder->fsCodeAppendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName,
823 fsName);
824 builder->fsCodeAppend("\t\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / dot(gF, gF));\n");
825 builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
826
827 SkString modulate;
bsalomon@google.com018f1792013-04-18 19:36:09 +0000828 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000829 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
830
831 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str());
832 }
833
834 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
835 return 0x0;
836 }
837
838 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
839
840 private:
841 typedef GrGLEffect INHERITED;
842 };
843
844private:
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000845 HairQuadEdgeEffect() {
846 this->addVertexAttrib(kVec4f_GrSLType);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000847 }
848
849 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
850 return true;
851 }
852
853 GR_DECLARE_EFFECT_TEST;
854
855 typedef GrEffect INHERITED;
856};
857
858GR_DEFINE_EFFECT_TEST(HairQuadEdgeEffect);
859
860GrEffectRef* HairQuadEdgeEffect::TestCreate(SkMWCRandom* random,
861 GrContext*,
862 const GrDrawTargetCaps& caps,
863 GrTexture*[]) {
864 // Doesn't work without derivative instructions.
egdaniel@google.com5383a752013-07-12 20:15:34 +0000865 return caps.shaderDerivativeSupport() ? HairQuadEdgeEffect::Create() : NULL;
866}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000867
868///////////////////////////////////////////////////////////////////////////////
869
870/**
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000871 * The output of this effect is a 1-pixel wide line.
872 * Input is 2D implicit device coord line eq (a*x + b*y +c = 0). 4th component unused.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000873 */
874class HairLineEdgeEffect : public GrEffect {
875public:
876
877 static GrEffectRef* Create() {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000878 GR_CREATE_STATIC_EFFECT(gHairLineEdge, HairLineEdgeEffect, ());
879 gHairLineEdge->ref();
880 return gHairLineEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000881 }
882
883 virtual ~HairLineEdgeEffect() {}
884
885 static const char* Name() { return "HairLineEdge"; }
886
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000887 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000888 uint32_t* validFlags) const SK_OVERRIDE {
889 *validFlags = 0;
890 }
891
892 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
893 return GrTBackendEffectFactory<HairLineEdgeEffect>::getInstance();
894 }
895
896 class GLEffect : public GrGLEffect {
897 public:
898 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
899 : INHERITED (factory) {}
900
901 virtual void emitCode(GrGLShaderBuilder* builder,
902 const GrDrawEffect& drawEffect,
903 EffectKey key,
904 const char* outputColor,
905 const char* inputColor,
906 const TextureSamplerArray& samplers) SK_OVERRIDE {
907 const char *vsName, *fsName;
908 const SkString* attrName =
909 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
910 builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n");
911
912 builder->addVarying(kVec4f_GrSLType, "HairLineEdge", &vsName, &fsName);
913
914 builder->fsCodeAppendf("\t\tedgeAlpha = abs(dot(vec3(%s.xy,1), %s.xyz));\n",
915 builder->fragmentPosition(), fsName);
916 builder->fsCodeAppendf("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
917
918 SkString modulate;
bsalomon@google.com018f1792013-04-18 19:36:09 +0000919 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000920 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
921
922 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str());
923 }
924
925 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
926 return 0x0;
927 }
928
929 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
930
931 private:
932 typedef GrGLEffect INHERITED;
933 };
934
935private:
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000936 HairLineEdgeEffect() {
937 this->addVertexAttrib(kVec4f_GrSLType);
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000938 this->setWillReadFragmentPosition();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000939 }
940
941 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
942 return true;
943 }
944
945 GR_DECLARE_EFFECT_TEST;
946
947 typedef GrEffect INHERITED;
948};
949
950GR_DEFINE_EFFECT_TEST(HairLineEdgeEffect);
951
952GrEffectRef* HairLineEdgeEffect::TestCreate(SkMWCRandom* random,
953 GrContext*,
954 const GrDrawTargetCaps& caps,
955 GrTexture*[]) {
956 return HairLineEdgeEffect::Create();
957}
958
959///////////////////////////////////////////////////////////////////////////////
960
robertphillips@google.com42903302013-04-20 12:26:07 +0000961namespace {
962
963// position + edge
964extern const GrVertexAttrib gHairlineAttribs[] = {
965 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
966 {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}
967};
968
egdaniel@google.com5383a752013-07-12 20:15:34 +0000969// Conic
970// position + ABCD + EF
971extern const GrVertexAttrib gConicVertexAttribs[] = {
972 { kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding },
973 { kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding },
974 { kVec2f_GrVertexAttribType, 3*sizeof(GrPoint), kEffect_GrVertexAttribBinding }
975};
robertphillips@google.com42903302013-04-20 12:26:07 +0000976};
977
bsalomon@google.comb3729422012-03-07 19:13:28 +0000978bool GrAAHairLinePathRenderer::createGeom(
979 const SkPath& path,
bsalomon@google.comb3729422012-03-07 19:13:28 +0000980 GrDrawTarget* target,
bsalomon@google.comb3729422012-03-07 19:13:28 +0000981 int* lineCnt,
982 int* quadCnt,
egdaniel@google.com5383a752013-07-12 20:15:34 +0000983 int* conicCnt,
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000984 GrDrawTarget::AutoReleaseGeometry* arg,
985 SkRect* devBounds) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000986 GrDrawState* drawState = target->drawState();
987 int rtHeight = drawState->getRenderTarget()->height();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000988
robertphillips@google.com7b112892012-07-31 15:18:21 +0000989 GrIRect devClipBounds;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000990 target->getClip()->getConservativeBounds(drawState->getRenderTarget(), &devClipBounds);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000991
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000992 SkMatrix viewM = drawState->getViewMatrix();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000993
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000994 // All the vertices that we compute are within 1 of path control points with the exception of
995 // one of the bounding vertices for each quad. The add_quads() function will update the bounds
996 // for each quad added.
997 *devBounds = path.getBounds();
998 viewM.mapRect(devBounds);
999 devBounds->outset(SK_Scalar1, SK_Scalar1);
1000
bsalomon@google.com92669012011-09-27 19:10:05 +00001001 PREALLOC_PTARRAY(128) lines;
1002 PREALLOC_PTARRAY(128) quads;
egdaniel@google.com5383a752013-07-12 20:15:34 +00001003 PREALLOC_PTARRAY(128) conics;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001004 IntArray qSubdivs;
egdaniel@google.com5383a752013-07-12 20:15:34 +00001005 FloatArray cWeights;
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +00001006 *quadCnt = generate_lines_and_quads(path, viewM, devClipBounds,
egdaniel@google.com5383a752013-07-12 20:15:34 +00001007 &lines, &quads, &conics, &qSubdivs, &cWeights);
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001008
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001009 *lineCnt = lines.count() / 2;
egdaniel@google.com5383a752013-07-12 20:15:34 +00001010 *conicCnt = conics.count() / 3;
1011 int vertCnt = kVertsPerLineSeg * *lineCnt + kVertsPerQuad * *quadCnt +
1012 kVertsPerQuad * *conicCnt;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001013
egdaniel@google.com5383a752013-07-12 20:15:34 +00001014 target->drawState()->setVertexAttribs<gConicVertexAttribs>(SK_ARRAY_COUNT(gConicVertexAttribs));
jvanverth@google.comb75b0a02013-02-05 20:33:30 +00001015 GrAssert(sizeof(Vertex) == target->getDrawState().getVertexSize());
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001016
jvanverth@google.comb75b0a02013-02-05 20:33:30 +00001017 if (!arg->set(target, vertCnt, 0)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001018 return false;
1019 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +00001020
bsalomon@google.comb3729422012-03-07 19:13:28 +00001021 Vertex* verts = reinterpret_cast<Vertex*>(arg->vertices());
1022
bsalomon@google.comb9086a02012-11-01 18:02:54 +00001023 const SkMatrix* toDevice = NULL;
1024 const SkMatrix* toSrc = NULL;
1025 SkMatrix ivm;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +00001026
1027 if (viewM.hasPerspective()) {
1028 if (viewM.invert(&ivm)) {
1029 toDevice = &viewM;
1030 toSrc = &ivm;
1031 }
1032 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001033
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001034 for (int i = 0; i < *lineCnt; ++i) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +00001035 add_line(&lines[2*i], rtHeight, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001036 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +00001037
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001038 int unsubdivQuadCnt = quads.count() / 3;
1039 for (int i = 0; i < unsubdivQuadCnt; ++i) {
1040 GrAssert(qSubdivs[i] >= 0);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +00001041 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts, devBounds);
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001042 }
1043
egdaniel@google.com5383a752013-07-12 20:15:34 +00001044 // Start Conics
1045 for (int i = 0; i < *conicCnt; ++i) {
1046 add_conics(&conics[3*i], cWeights[i], toDevice, toSrc, &verts, devBounds);
1047 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001048 return true;
1049}
1050
robertphillips@google.com8a4fc402012-05-24 12:42:24 +00001051bool GrAAHairLinePathRenderer::canDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001052 const SkStrokeRec& stroke,
robertphillips@google.com8a4fc402012-05-24 12:42:24 +00001053 const GrDrawTarget* target,
1054 bool antiAlias) const {
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001055 if (!stroke.isHairlineStyle() || !antiAlias) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001056 return false;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001057 }
1058
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001059 static const uint32_t gReqDerivMask = SkPath::kCubic_SegmentMask |
1060 SkPath::kQuad_SegmentMask;
bsalomon@google.combcce8922013-03-25 15:38:39 +00001061 if (!target->caps()->shaderDerivativeSupport() &&
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001062 (gReqDerivMask & path.getSegmentMasks())) {
1063 return false;
1064 }
1065 return true;
1066}
1067
1068bool GrAAHairLinePathRenderer::onDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +00001069 const SkStrokeRec&,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001070 GrDrawTarget* target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001071 bool antiAlias) {
1072
1073 int lineCnt;
1074 int quadCnt;
egdaniel@google.com5383a752013-07-12 20:15:34 +00001075 int conicCnt;
bsalomon@google.comb3729422012-03-07 19:13:28 +00001076 GrDrawTarget::AutoReleaseGeometry arg;
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +00001077 SkRect devBounds;
1078
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001079 if (!this->createGeom(path,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001080 target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001081 &lineCnt,
bsalomon@google.comb3729422012-03-07 19:13:28 +00001082 &quadCnt,
egdaniel@google.com5383a752013-07-12 20:15:34 +00001083 &conicCnt,
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +00001084 &arg,
1085 &devBounds)) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001086 return false;
1087 }
1088
bsalomon@google.com137f1342013-05-29 21:27:53 +00001089 GrDrawTarget::AutoStateRestore asr;
bsalomon@google.com4647f902013-03-26 14:45:27 +00001090
bsalomon@google.coma8347462012-10-08 18:59:39 +00001091 // createGeom transforms the geometry to device space when the matrix does not have
1092 // perspective.
bsalomon@google.com137f1342013-05-29 21:27:53 +00001093 if (target->getDrawState().getViewMatrix().hasPerspective()) {
1094 asr.set(target, GrDrawTarget::kPreserve_ASRInit);
1095 } else if (!asr.setIdentity(target, GrDrawTarget::kPreserve_ASRInit)) {
1096 return false;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001097 }
bsalomon@google.com137f1342013-05-29 21:27:53 +00001098 GrDrawState* drawState = target->drawState();
rmistry@google.comfbfcd562012-08-23 18:09:54 +00001099
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001100 // TODO: See whether rendering lines as degenerate quads improves perf
1101 // when we have a mix
bsalomon@google.coma8347462012-10-08 18:59:39 +00001102
bsalomon@google.com4647f902013-03-26 14:45:27 +00001103 enum {
1104 // the edge effects share this stage with glyph rendering
1105 // (kGlyphMaskStage in GrTextContext) && SW path rendering
1106 // (kPathMaskStage in GrSWMaskHelper)
1107 kEdgeEffectStage = GrPaint::kTotalStages,
1108 };
1109 static const int kEdgeAttrIndex = 1;
bsalomon@google.coma8347462012-10-08 18:59:39 +00001110
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +00001111 GrEffectRef* hairLineEffect = HairLineEdgeEffect::Create();
1112 GrEffectRef* hairQuadEffect = HairQuadEdgeEffect::Create();
egdaniel@google.com5383a752013-07-12 20:15:34 +00001113 GrEffectRef* hairConicEffect = HairConicEdgeEffect::Create();
skia.committer@gmail.com37cbc7f2013-03-27 07:01:04 +00001114
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +00001115 // Check devBounds
1116#if GR_DEBUG
1117 SkRect tolDevBounds = devBounds;
1118 tolDevBounds.outset(SK_Scalar1 / 10000, SK_Scalar1 / 10000);
1119 SkRect actualBounds;
1120 Vertex* verts = reinterpret_cast<Vertex*>(arg.vertices());
egdaniel@google.com5383a752013-07-12 20:15:34 +00001121 int vCount = kVertsPerLineSeg * lineCnt + kVertsPerQuad * quadCnt + kVertsPerQuad * conicCnt;
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +00001122 bool first = true;
1123 for (int i = 0; i < vCount; ++i) {
1124 SkPoint pos = verts[i].fPos;
1125 // This is a hack to workaround the fact that we move some degenerate segments offscreen.
1126 if (SK_ScalarMax == pos.fX) {
1127 continue;
1128 }
1129 drawState->getViewMatrix().mapPoints(&pos, 1);
1130 if (first) {
1131 actualBounds.set(pos.fX, pos.fY, pos.fX, pos.fY);
1132 first = false;
1133 } else {
1134 actualBounds.growToInclude(pos.fX, pos.fY);
1135 }
1136 }
1137 if (!first) {
1138 GrAssert(tolDevBounds.contains(actualBounds));
1139 }
1140#endif
1141
bsalomon@google.comeb6879f2013-06-13 19:34:18 +00001142 {
1143 GrDrawState::AutoRestoreEffects are(drawState);
1144 target->setIndexSourceToBuffer(fLinesIndexBuffer);
1145 int lines = 0;
1146 int nBufLines = fLinesIndexBuffer->maxQuads();
1147 drawState->addCoverageEffect(hairLineEffect, kEdgeAttrIndex)->unref();
1148 while (lines < lineCnt) {
1149 int n = GrMin(lineCnt - lines, nBufLines);
1150 target->drawIndexed(kTriangles_GrPrimitiveType,
1151 kVertsPerLineSeg*lines, // startV
1152 0, // startI
1153 kVertsPerLineSeg*n, // vCount
1154 kIdxsPerLineSeg*n,
1155 &devBounds); // iCount
1156 lines += n;
1157 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001158 }
1159
egdaniel@google.com5383a752013-07-12 20:15:34 +00001160 {
1161 GrDrawState::AutoRestoreEffects are(drawState);
1162 target->setIndexSourceToBuffer(fQuadsIndexBuffer);
1163 int quads = 0;
1164 drawState->addCoverageEffect(hairQuadEffect, kEdgeAttrIndex)->unref();
1165 while (quads < quadCnt) {
1166 int n = GrMin(quadCnt - quads, kNumQuadsInIdxBuffer);
1167 target->drawIndexed(kTriangles_GrPrimitiveType,
1168 kVertsPerLineSeg * lineCnt + kVertsPerQuad*quads, // startV
1169 0, // startI
1170 kVertsPerQuad*n, // vCount
1171 kIdxsPerQuad*n, // iCount
1172 &devBounds);
1173 quads += n;
1174 }
1175 }
1176
1177 {
1178 GrDrawState::AutoRestoreEffects are(drawState);
1179 int conics = 0;
1180 drawState->addCoverageEffect(hairConicEffect, 1, 2)->unref();
1181 while (conics < conicCnt) {
1182 int n = GrMin(conicCnt - conics, kNumQuadsInIdxBuffer);
1183 target->drawIndexed(kTriangles_GrPrimitiveType,
1184 kVertsPerLineSeg*lineCnt +
1185 kVertsPerQuad*(quadCnt + conics), // startV
1186 0, // startI
1187 kVertsPerQuad*n, // vCount
1188 kIdxsPerQuad*n, // iCount
1189 &devBounds);
1190 conics += n;
1191 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001192 }
bsalomon@google.com0406b9e2013-04-02 21:00:15 +00001193 target->resetIndexSource();
bsalomon@google.com4647f902013-03-26 14:45:27 +00001194
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001195 return true;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001196}
egdaniel@google.com5383a752013-07-12 20:15:34 +00001197