blob: bed931c0346d6fd0d608c4c64cc1ab998464a92e [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
29// bloat_quad.
30static 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;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000120
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000121// Takes 178th time of logf on Z600 / VC2010
122int get_float_exp(float x) {
123 GR_STATIC_ASSERT(sizeof(int) == sizeof(float));
124#if GR_DEBUG
125 static bool tested;
126 if (!tested) {
127 tested = true;
128 GrAssert(get_float_exp(0.25f) == -2);
129 GrAssert(get_float_exp(0.3f) == -2);
130 GrAssert(get_float_exp(0.5f) == -1);
131 GrAssert(get_float_exp(1.f) == 0);
132 GrAssert(get_float_exp(2.f) == 1);
133 GrAssert(get_float_exp(2.5f) == 1);
134 GrAssert(get_float_exp(8.f) == 3);
135 GrAssert(get_float_exp(100.f) == 6);
136 GrAssert(get_float_exp(1000.f) == 9);
137 GrAssert(get_float_exp(1024.f) == 10);
138 GrAssert(get_float_exp(3000000.f) == 21);
139 }
140#endif
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000141 const int* iptr = (const int*)&x;
142 return (((*iptr) & 0x7f800000) >> 23) - 127;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000143}
144
145// we subdivide the quads to avoid huge overfill
146// if it returns -1 then should be drawn as lines
147int num_quad_subdivs(const SkPoint p[3]) {
148 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000149 static const SkScalar gDegenerateToLineTolSqd =
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000150 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000151
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000152 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
153 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000154 return -1;
155 }
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000156
bsalomon@google.com81712882012-11-01 17:12:34 +0000157 SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000158 if (dsqd < gDegenerateToLineTolSqd) {
159 return -1;
160 }
161
162 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000163 return -1;
164 }
165
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000166 // tolerance of triangle height in pixels
167 // tuned on windows Quadro FX 380 / Z600
168 // trade off of fill vs cpu time on verts
169 // maybe different when do this using gpu (geo or tess shaders)
170 static const SkScalar gSubdivTol = 175 * SK_Scalar1;
171
robertphillips@google.com7460b372012-04-25 16:54:51 +0000172 if (dsqd <= SkScalarMul(gSubdivTol, gSubdivTol)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000173 return 0;
174 } else {
robertphillips@google.com87379e12013-03-29 12:11:10 +0000175 static const int kMaxSub = 4;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000176 // subdividing the quad reduces d by 4. so we want x = log4(d/tol)
177 // = log4(d*d/tol*tol)/2
178 // = log2(d*d/tol*tol)
179
180#ifdef SK_SCALAR_IS_FLOAT
181 // +1 since we're ignoring the mantissa contribution.
182 int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
183 log = GrMin(GrMax(0, log), kMaxSub);
184 return log;
185#else
robertphillips@google.com7460b372012-04-25 16:54:51 +0000186 SkScalar log = SkScalarLog(
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000187 SkScalarDiv(dsqd,
robertphillips@google.com7460b372012-04-25 16:54:51 +0000188 SkScalarMul(gSubdivTol, gSubdivTol)));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000189 static const SkScalar conv = SkScalarInvert(SkScalarLog(2));
190 log = SkScalarMul(log, conv);
191 return GrMin(GrMax(0, SkScalarCeilToInt(log)),kMaxSub);
192#endif
193 }
194}
195
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000196/**
197 * Generates the lines and quads to be rendered. Lines are always recorded in
198 * device space. We will do a device space bloat to account for the 1pixel
199 * thickness.
200 * Quads are recorded in device space unless m contains
201 * perspective, then in they are in src space. We do this because we will
202 * subdivide large quads to reduce over-fill. This subdivision has to be
203 * performed before applying the perspective matrix.
204 */
205int generate_lines_and_quads(const SkPath& path,
206 const SkMatrix& m,
robertphillips@google.com7b112892012-07-31 15:18:21 +0000207 const GrIRect& devClipBounds,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000208 PtArray* lines,
209 PtArray* quads,
210 IntArray* quadSubdivCnts) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000211 SkPath::Iter iter(path, false);
212
213 int totalQuadCount = 0;
214 GrRect bounds;
215 GrIRect ibounds;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000216
217 bool persp = m.hasPerspective();
218
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000219 for (;;) {
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000220 GrPoint pathPts[4];
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000221 GrPoint devPts[4];
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000222 SkPath::Verb verb = iter.next(pathPts);
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000223 switch (verb) {
reed@google.com277c3f82013-05-31 15:17:50 +0000224 case SkPath::kConic_Verb:
225 SkASSERT(0);
226 break;
227 case SkPath::kMove_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000228 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000229 case SkPath::kLine_Verb:
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000230 m.mapPoints(devPts, pathPts, 2);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000231 bounds.setBounds(devPts, 2);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000232 bounds.outset(SK_Scalar1, SK_Scalar1);
233 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000234 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000235 SkPoint* pts = lines->push_back_n(2);
236 pts[0] = devPts[0];
237 pts[1] = devPts[1];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000238 }
239 break;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000240 case SkPath::kQuad_Verb: {
241 SkPoint choppedPts[5];
242 // Chopping the quad helps when the quad is either degenerate or nearly degenerate.
243 // When it is degenerate it allows the approximation with lines to work since the
244 // chop point (if there is one) will be at the parabola's vertex. In the nearly
245 // degenerate the QuadUVMatrix computed for the points is almost singular which
246 // can cause rendering artifacts.
247 int n = SkChopQuadAtMaxCurvature(pathPts, choppedPts);
248 for (int i = 0; i < n; ++i) {
249 SkPoint* quadPts = choppedPts + i * 2;
250 m.mapPoints(devPts, quadPts, 3);
251 bounds.setBounds(devPts, 3);
252 bounds.outset(SK_Scalar1, SK_Scalar1);
253 bounds.roundOut(&ibounds);
254
255 if (SkIRect::Intersects(devClipBounds, ibounds)) {
256 int subdiv = num_quad_subdivs(devPts);
257 GrAssert(subdiv >= -1);
258 if (-1 == subdiv) {
259 SkPoint* pts = lines->push_back_n(4);
260 pts[0] = devPts[0];
261 pts[1] = devPts[1];
262 pts[2] = devPts[1];
263 pts[3] = devPts[2];
264 } else {
265 // when in perspective keep quads in src space
266 SkPoint* qPts = persp ? quadPts : devPts;
267 SkPoint* pts = quads->push_back_n(3);
268 pts[0] = qPts[0];
269 pts[1] = qPts[1];
270 pts[2] = qPts[2];
271 quadSubdivCnts->push_back() = subdiv;
272 totalQuadCount += 1 << subdiv;
273 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000274 }
275 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000276 break;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000277 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000278 case SkPath::kCubic_Verb:
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000279 m.mapPoints(devPts, pathPts, 4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000280 bounds.setBounds(devPts, 4);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000281 bounds.outset(SK_Scalar1, SK_Scalar1);
282 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000283 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000284 PREALLOC_PTARRAY(32) q;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000285 // we don't need a direction if we aren't constraining the subdivision
286 static const SkPath::Direction kDummyDir = SkPath::kCCW_Direction;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000287 // We convert cubics to quadratics (for now).
288 // In perspective have to do conversion in src space.
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000289 if (persp) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000290 SkScalar tolScale =
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000291 GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m,
292 path.getBounds());
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000293 GrPathUtils::convertCubicToQuads(pathPts, tolScale, false, kDummyDir, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000294 } else {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000295 GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, false, kDummyDir, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000296 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000297 for (int i = 0; i < q.count(); i += 3) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000298 SkPoint* qInDevSpace;
299 // bounds has to be calculated in device space, but q is
300 // in src space when there is perspective.
301 if (persp) {
302 m.mapPoints(devPts, &q[i], 3);
303 bounds.setBounds(devPts, 3);
304 qInDevSpace = devPts;
305 } else {
306 bounds.setBounds(&q[i], 3);
307 qInDevSpace = &q[i];
308 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000309 bounds.outset(SK_Scalar1, SK_Scalar1);
310 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000311 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000312 int subdiv = num_quad_subdivs(qInDevSpace);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000313 GrAssert(subdiv >= -1);
314 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000315 SkPoint* pts = lines->push_back_n(4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000316 // lines should always be in device coords
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000317 pts[0] = qInDevSpace[0];
318 pts[1] = qInDevSpace[1];
319 pts[2] = qInDevSpace[1];
320 pts[3] = qInDevSpace[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000321 } else {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000322 SkPoint* pts = quads->push_back_n(3);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000323 // q is already in src space when there is no
324 // perspective and dev coords otherwise.
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000325 pts[0] = q[0 + i];
326 pts[1] = q[1 + i];
327 pts[2] = q[2 + i];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000328 quadSubdivCnts->push_back() = subdiv;
329 totalQuadCount += 1 << subdiv;
330 }
331 }
332 }
333 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000334 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000335 case SkPath::kClose_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000336 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000337 case SkPath::kDone_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000338 return totalQuadCount;
339 }
340 }
341}
342
343struct Vertex {
344 GrPoint fPos;
345 union {
346 struct {
bsalomon@google.com81712882012-11-01 17:12:34 +0000347 SkScalar fA;
348 SkScalar fB;
349 SkScalar fC;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000350 } fLine;
351 GrVec fQuadCoord;
352 struct {
bsalomon@google.com81712882012-11-01 17:12:34 +0000353 SkScalar fBogus[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000354 };
355 };
356};
357GR_STATIC_ASSERT(sizeof(Vertex) == 3 * sizeof(GrPoint));
358
359void intersect_lines(const SkPoint& ptA, const SkVector& normA,
360 const SkPoint& ptB, const SkVector& normB,
361 SkPoint* result) {
362
363 SkScalar lineAW = -normA.dot(ptA);
364 SkScalar lineBW = -normB.dot(ptB);
365
366 SkScalar wInv = SkScalarMul(normA.fX, normB.fY) -
367 SkScalarMul(normA.fY, normB.fX);
368 wInv = SkScalarInvert(wInv);
369
370 result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY);
371 result->fX = SkScalarMul(result->fX, wInv);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000372
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000373 result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW);
374 result->fY = SkScalarMul(result->fY, wInv);
375}
376
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000377void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice,
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000378 const SkMatrix* toSrc, Vertex verts[kVertsPerQuad],
379 SkRect* devBounds) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000380 GrAssert(!toDevice == !toSrc);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000381 // original quad is specified by tri a,b,c
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000382 SkPoint a = qpts[0];
383 SkPoint b = qpts[1];
384 SkPoint c = qpts[2];
385
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000386 // this should be in the src space, not dev coords, when we have perspective
bsalomon@google.com19713172012-03-15 13:51:08 +0000387 GrPathUtils::QuadUVMatrix DevToUV(qpts);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000388
389 if (toDevice) {
390 toDevice->mapPoints(&a, 1);
391 toDevice->mapPoints(&b, 1);
392 toDevice->mapPoints(&c, 1);
393 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000394 // make a new poly where we replace a and c by a 1-pixel wide edges orthog
395 // to edges ab and bc:
396 //
397 // before | after
398 // | b0
399 // b |
400 // |
401 // | a0 c0
402 // a c | a1 c1
403 //
404 // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c,
405 // respectively.
406 Vertex& a0 = verts[0];
407 Vertex& a1 = verts[1];
408 Vertex& b0 = verts[2];
409 Vertex& c0 = verts[3];
410 Vertex& c1 = verts[4];
411
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000412 SkVector ab = b;
413 ab -= a;
414 SkVector ac = c;
415 ac -= a;
416 SkVector cb = b;
417 cb -= c;
418
419 // We should have already handled degenerates
420 GrAssert(ab.length() > 0 && cb.length() > 0);
421
422 ab.normalize();
423 SkVector abN;
424 abN.setOrthog(ab, SkVector::kLeft_Side);
425 if (abN.dot(ac) > 0) {
426 abN.negate();
427 }
428
429 cb.normalize();
430 SkVector cbN;
431 cbN.setOrthog(cb, SkVector::kLeft_Side);
432 if (cbN.dot(ac) < 0) {
433 cbN.negate();
434 }
435
436 a0.fPos = a;
437 a0.fPos += abN;
438 a1.fPos = a;
439 a1.fPos -= abN;
440
441 c0.fPos = c;
442 c0.fPos += cbN;
443 c1.fPos = c;
444 c1.fPos -= cbN;
445
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000446 // This point may not be within 1 pixel of a control point. We update the bounding box to
447 // include it.
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000448 intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000449 devBounds->growToInclude(b0.fPos.fX, b0.fPos.fY);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000450
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000451 if (toSrc) {
452 toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(Vertex), kVertsPerQuad);
453 }
bsalomon@google.com19713172012-03-15 13:51:08 +0000454 DevToUV.apply<kVertsPerQuad, sizeof(Vertex), sizeof(GrPoint)>(verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000455}
456
457void add_quads(const SkPoint p[3],
458 int subdiv,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000459 const SkMatrix* toDevice,
460 const SkMatrix* toSrc,
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000461 Vertex** vert,
462 SkRect* devBounds) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000463 GrAssert(subdiv >= 0);
464 if (subdiv) {
465 SkPoint newP[5];
466 SkChopQuadAtHalf(p, newP);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000467 add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert, devBounds);
468 add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert, devBounds);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000469 } else {
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000470 bloat_quad(p, toDevice, toSrc, *vert, devBounds);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000471 *vert += kVertsPerQuad;
472 }
473}
474
475void add_line(const SkPoint p[2],
476 int rtHeight,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000477 const SkMatrix* toSrc,
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000478 Vertex** vert) {
479 const SkPoint& a = p[0];
480 const SkPoint& b = p[1];
481
482 SkVector orthVec = b;
483 orthVec -= a;
484
485 if (orthVec.setLength(SK_Scalar1)) {
486 orthVec.setOrthog(orthVec);
487
bsalomon@google.com706f6682012-10-23 14:53:55 +0000488 SkScalar lineC = -(a.dot(orthVec));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000489 for (int i = 0; i < kVertsPerLineSeg; ++i) {
490 (*vert)[i].fPos = (i < 2) ? a : b;
491 if (0 == i || 3 == i) {
492 (*vert)[i].fPos -= orthVec;
493 } else {
494 (*vert)[i].fPos += orthVec;
495 }
bsalomon@google.com706f6682012-10-23 14:53:55 +0000496 (*vert)[i].fLine.fA = orthVec.fX;
497 (*vert)[i].fLine.fB = orthVec.fY;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000498 (*vert)[i].fLine.fC = lineC;
499 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000500 if (NULL != toSrc) {
501 toSrc->mapPointsWithStride(&(*vert)->fPos,
502 sizeof(Vertex),
503 kVertsPerLineSeg);
504 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000505 } else {
506 // just make it degenerate and likely offscreen
507 (*vert)[0].fPos.set(SK_ScalarMax, SK_ScalarMax);
508 (*vert)[1].fPos.set(SK_ScalarMax, SK_ScalarMax);
509 (*vert)[2].fPos.set(SK_ScalarMax, SK_ScalarMax);
510 (*vert)[3].fPos.set(SK_ScalarMax, SK_ScalarMax);
511 }
512
513 *vert += kVertsPerLineSeg;
514}
515
516}
517
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000518///////////////////////////////////////////////////////////////////////////////
519
520/**
521 * The output of this effect is a hairline edge for quadratics.
522 * Quadratic specified by 0=u^2-v canonical coords. u and v are the first
523 * two components of the vertex attribute. Uses unsigned distance.
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000524 * Coverage is min(0, 1-distance). 3rd & 4th component unused.
525 * Requires shader derivative instruction support.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000526 */
527class HairQuadEdgeEffect : public GrEffect {
528public:
529
530 static GrEffectRef* Create() {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000531 GR_CREATE_STATIC_EFFECT(gHairQuadEdgeEffect, HairQuadEdgeEffect, ());
532 gHairQuadEdgeEffect->ref();
533 return gHairQuadEdgeEffect;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000534 }
535
536 virtual ~HairQuadEdgeEffect() {}
537
538 static const char* Name() { return "HairQuadEdge"; }
539
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000540 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000541 uint32_t* validFlags) const SK_OVERRIDE {
542 *validFlags = 0;
543 }
544
545 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
546 return GrTBackendEffectFactory<HairQuadEdgeEffect>::getInstance();
547 }
548
549 class GLEffect : public GrGLEffect {
550 public:
551 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
552 : INHERITED (factory) {}
553
554 virtual void emitCode(GrGLShaderBuilder* builder,
555 const GrDrawEffect& drawEffect,
556 EffectKey key,
557 const char* outputColor,
558 const char* inputColor,
559 const TextureSamplerArray& samplers) SK_OVERRIDE {
560 const char *vsName, *fsName;
561 const SkString* attrName =
562 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
563 builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n");
564
565 SkAssertResult(builder->enableFeature(
566 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
567 builder->addVarying(kVec4f_GrSLType, "HairQuadEdge", &vsName, &fsName);
568
569 builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
570 builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
571 builder->fsCodeAppendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
572 "\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
573 fsName, fsName);
574 builder->fsCodeAppendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName,
575 fsName);
576 builder->fsCodeAppend("\t\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / dot(gF, gF));\n");
577 builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
578
579 SkString modulate;
bsalomon@google.com018f1792013-04-18 19:36:09 +0000580 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000581 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
582
583 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str());
584 }
585
586 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
587 return 0x0;
588 }
589
590 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
591
592 private:
593 typedef GrGLEffect INHERITED;
594 };
595
596private:
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000597 HairQuadEdgeEffect() {
598 this->addVertexAttrib(kVec4f_GrSLType);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000599 }
600
601 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
602 return true;
603 }
604
605 GR_DECLARE_EFFECT_TEST;
606
607 typedef GrEffect INHERITED;
608};
609
610GR_DEFINE_EFFECT_TEST(HairQuadEdgeEffect);
611
612GrEffectRef* HairQuadEdgeEffect::TestCreate(SkMWCRandom* random,
613 GrContext*,
614 const GrDrawTargetCaps& caps,
615 GrTexture*[]) {
616 // Doesn't work without derivative instructions.
617 return caps.shaderDerivativeSupport() ? HairQuadEdgeEffect::Create() : NULL;}
618
619///////////////////////////////////////////////////////////////////////////////
620
621/**
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000622 * The output of this effect is a 1-pixel wide line.
623 * 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 +0000624 */
625class HairLineEdgeEffect : public GrEffect {
626public:
627
628 static GrEffectRef* Create() {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000629 GR_CREATE_STATIC_EFFECT(gHairLineEdge, HairLineEdgeEffect, ());
630 gHairLineEdge->ref();
631 return gHairLineEdge;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000632 }
633
634 virtual ~HairLineEdgeEffect() {}
635
636 static const char* Name() { return "HairLineEdge"; }
637
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000638 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000639 uint32_t* validFlags) const SK_OVERRIDE {
640 *validFlags = 0;
641 }
642
643 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
644 return GrTBackendEffectFactory<HairLineEdgeEffect>::getInstance();
645 }
646
647 class GLEffect : public GrGLEffect {
648 public:
649 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
650 : INHERITED (factory) {}
651
652 virtual void emitCode(GrGLShaderBuilder* builder,
653 const GrDrawEffect& drawEffect,
654 EffectKey key,
655 const char* outputColor,
656 const char* inputColor,
657 const TextureSamplerArray& samplers) SK_OVERRIDE {
658 const char *vsName, *fsName;
659 const SkString* attrName =
660 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
661 builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n");
662
663 builder->addVarying(kVec4f_GrSLType, "HairLineEdge", &vsName, &fsName);
664
665 builder->fsCodeAppendf("\t\tedgeAlpha = abs(dot(vec3(%s.xy,1), %s.xyz));\n",
666 builder->fragmentPosition(), fsName);
667 builder->fsCodeAppendf("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n");
668
669 SkString modulate;
bsalomon@google.com018f1792013-04-18 19:36:09 +0000670 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000671 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
672
673 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str());
674 }
675
676 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
677 return 0x0;
678 }
679
680 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
681
682 private:
683 typedef GrGLEffect INHERITED;
684 };
685
686private:
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000687 HairLineEdgeEffect() {
688 this->addVertexAttrib(kVec4f_GrSLType);
commit-bot@chromium.org8d47ddc2013-05-09 14:55:46 +0000689 this->setWillReadFragmentPosition();
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000690 }
691
692 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
693 return true;
694 }
695
696 GR_DECLARE_EFFECT_TEST;
697
698 typedef GrEffect INHERITED;
699};
700
701GR_DEFINE_EFFECT_TEST(HairLineEdgeEffect);
702
703GrEffectRef* HairLineEdgeEffect::TestCreate(SkMWCRandom* random,
704 GrContext*,
705 const GrDrawTargetCaps& caps,
706 GrTexture*[]) {
707 return HairLineEdgeEffect::Create();
708}
709
710///////////////////////////////////////////////////////////////////////////////
711
robertphillips@google.com42903302013-04-20 12:26:07 +0000712namespace {
713
714// position + edge
715extern const GrVertexAttrib gHairlineAttribs[] = {
716 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
717 {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}
718};
719
720};
721
bsalomon@google.comb3729422012-03-07 19:13:28 +0000722bool GrAAHairLinePathRenderer::createGeom(
723 const SkPath& path,
bsalomon@google.comb3729422012-03-07 19:13:28 +0000724 GrDrawTarget* target,
bsalomon@google.comb3729422012-03-07 19:13:28 +0000725 int* lineCnt,
726 int* quadCnt,
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000727 GrDrawTarget::AutoReleaseGeometry* arg,
728 SkRect* devBounds) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000729 GrDrawState* drawState = target->drawState();
730 int rtHeight = drawState->getRenderTarget()->height();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000731
robertphillips@google.com7b112892012-07-31 15:18:21 +0000732 GrIRect devClipBounds;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000733 target->getClip()->getConservativeBounds(drawState->getRenderTarget(), &devClipBounds);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000734
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000735 SkMatrix viewM = drawState->getViewMatrix();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000736
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000737 // All the vertices that we compute are within 1 of path control points with the exception of
738 // one of the bounding vertices for each quad. The add_quads() function will update the bounds
739 // for each quad added.
740 *devBounds = path.getBounds();
741 viewM.mapRect(devBounds);
742 devBounds->outset(SK_Scalar1, SK_Scalar1);
743
bsalomon@google.com92669012011-09-27 19:10:05 +0000744 PREALLOC_PTARRAY(128) lines;
745 PREALLOC_PTARRAY(128) quads;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000746 IntArray qSubdivs;
bsalomon@google.com0f11e1a2012-10-08 14:48:36 +0000747 *quadCnt = generate_lines_and_quads(path, viewM, devClipBounds,
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000748 &lines, &quads, &qSubdivs);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000749
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000750 *lineCnt = lines.count() / 2;
751 int vertCnt = kVertsPerLineSeg * *lineCnt + kVertsPerQuad * *quadCnt;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000752
robertphillips@google.com42903302013-04-20 12:26:07 +0000753 target->drawState()->setVertexAttribs<gHairlineAttribs>(SK_ARRAY_COUNT(gHairlineAttribs));
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000754 GrAssert(sizeof(Vertex) == target->getDrawState().getVertexSize());
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000755
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000756 if (!arg->set(target, vertCnt, 0)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000757 return false;
758 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000759
bsalomon@google.comb3729422012-03-07 19:13:28 +0000760 Vertex* verts = reinterpret_cast<Vertex*>(arg->vertices());
761
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000762 const SkMatrix* toDevice = NULL;
763 const SkMatrix* toSrc = NULL;
764 SkMatrix ivm;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000765
766 if (viewM.hasPerspective()) {
767 if (viewM.invert(&ivm)) {
768 toDevice = &viewM;
769 toSrc = &ivm;
770 }
771 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000772
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000773 for (int i = 0; i < *lineCnt; ++i) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000774 add_line(&lines[2*i], rtHeight, toSrc, &verts);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000775 }
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000776
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000777 int unsubdivQuadCnt = quads.count() / 3;
778 for (int i = 0; i < unsubdivQuadCnt; ++i) {
779 GrAssert(qSubdivs[i] >= 0);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000780 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts, devBounds);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000781 }
782
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000783 return true;
784}
785
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000786bool GrAAHairLinePathRenderer::canDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000787 const SkStrokeRec& stroke,
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000788 const GrDrawTarget* target,
789 bool antiAlias) const {
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000790 if (!stroke.isHairlineStyle() || !antiAlias) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000791 return false;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000792 }
793
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000794 static const uint32_t gReqDerivMask = SkPath::kCubic_SegmentMask |
795 SkPath::kQuad_SegmentMask;
bsalomon@google.combcce8922013-03-25 15:38:39 +0000796 if (!target->caps()->shaderDerivativeSupport() &&
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000797 (gReqDerivMask & path.getSegmentMasks())) {
798 return false;
799 }
800 return true;
801}
802
803bool GrAAHairLinePathRenderer::onDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000804 const SkStrokeRec&,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000805 GrDrawTarget* target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000806 bool antiAlias) {
807
808 int lineCnt;
809 int quadCnt;
bsalomon@google.comb3729422012-03-07 19:13:28 +0000810 GrDrawTarget::AutoReleaseGeometry arg;
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000811 SkRect devBounds;
812
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000813 if (!this->createGeom(path,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000814 target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000815 &lineCnt,
bsalomon@google.comb3729422012-03-07 19:13:28 +0000816 &quadCnt,
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000817 &arg,
818 &devBounds)) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000819 return false;
820 }
821
bsalomon@google.com137f1342013-05-29 21:27:53 +0000822 GrDrawTarget::AutoStateRestore asr;
bsalomon@google.com4647f902013-03-26 14:45:27 +0000823
bsalomon@google.coma8347462012-10-08 18:59:39 +0000824 // createGeom transforms the geometry to device space when the matrix does not have
825 // perspective.
bsalomon@google.com137f1342013-05-29 21:27:53 +0000826 if (target->getDrawState().getViewMatrix().hasPerspective()) {
827 asr.set(target, GrDrawTarget::kPreserve_ASRInit);
828 } else if (!asr.setIdentity(target, GrDrawTarget::kPreserve_ASRInit)) {
829 return false;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000830 }
bsalomon@google.com137f1342013-05-29 21:27:53 +0000831 GrDrawState* drawState = target->drawState();
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000832
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000833 // TODO: See whether rendering lines as degenerate quads improves perf
834 // when we have a mix
bsalomon@google.coma8347462012-10-08 18:59:39 +0000835
bsalomon@google.com4647f902013-03-26 14:45:27 +0000836 enum {
837 // the edge effects share this stage with glyph rendering
838 // (kGlyphMaskStage in GrTextContext) && SW path rendering
839 // (kPathMaskStage in GrSWMaskHelper)
840 kEdgeEffectStage = GrPaint::kTotalStages,
841 };
842 static const int kEdgeAttrIndex = 1;
bsalomon@google.coma8347462012-10-08 18:59:39 +0000843
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000844 GrEffectRef* hairLineEffect = HairLineEdgeEffect::Create();
845 GrEffectRef* hairQuadEffect = HairQuadEdgeEffect::Create();
skia.committer@gmail.com37cbc7f2013-03-27 07:01:04 +0000846
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000847 // Check devBounds
848#if GR_DEBUG
849 SkRect tolDevBounds = devBounds;
850 tolDevBounds.outset(SK_Scalar1 / 10000, SK_Scalar1 / 10000);
851 SkRect actualBounds;
852 Vertex* verts = reinterpret_cast<Vertex*>(arg.vertices());
853 int vCount = kVertsPerLineSeg * lineCnt + kVertsPerQuad * quadCnt;
854 bool first = true;
855 for (int i = 0; i < vCount; ++i) {
856 SkPoint pos = verts[i].fPos;
857 // This is a hack to workaround the fact that we move some degenerate segments offscreen.
858 if (SK_ScalarMax == pos.fX) {
859 continue;
860 }
861 drawState->getViewMatrix().mapPoints(&pos, 1);
862 if (first) {
863 actualBounds.set(pos.fX, pos.fY, pos.fX, pos.fY);
864 first = false;
865 } else {
866 actualBounds.growToInclude(pos.fX, pos.fY);
867 }
868 }
869 if (!first) {
870 GrAssert(tolDevBounds.contains(actualBounds));
871 }
872#endif
873
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000874 target->setIndexSourceToBuffer(fLinesIndexBuffer);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000875 int lines = 0;
876 int nBufLines = fLinesIndexBuffer->maxQuads();
bsalomon@google.com4647f902013-03-26 14:45:27 +0000877 drawState->setEffect(kEdgeEffectStage, hairLineEffect, kEdgeAttrIndex)->unref();
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000878 while (lines < lineCnt) {
879 int n = GrMin(lineCnt - lines, nBufLines);
bsalomon@google.com47059542012-06-06 20:51:20 +0000880 target->drawIndexed(kTriangles_GrPrimitiveType,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000881 kVertsPerLineSeg*lines, // startV
882 0, // startI
883 kVertsPerLineSeg*n, // vCount
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000884 kIdxsPerLineSeg*n,
885 &devBounds); // iCount
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000886 lines += n;
887 }
888
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000889 target->setIndexSourceToBuffer(fQuadsIndexBuffer);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000890 int quads = 0;
bsalomon@google.com4647f902013-03-26 14:45:27 +0000891 drawState->setEffect(kEdgeEffectStage, hairQuadEffect, kEdgeAttrIndex)->unref();
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000892 while (quads < quadCnt) {
893 int n = GrMin(quadCnt - quads, kNumQuadsInIdxBuffer);
bsalomon@google.com47059542012-06-06 20:51:20 +0000894 target->drawIndexed(kTriangles_GrPrimitiveType,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000895 4 * lineCnt + kVertsPerQuad*quads, // startV
896 0, // startI
897 kVertsPerQuad*n, // vCount
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000898 kIdxsPerQuad*n, // iCount
899 &devBounds);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000900 quads += n;
901 }
bsalomon@google.com0406b9e2013-04-02 21:00:15 +0000902 target->resetIndexSource();
bsalomon@google.com4647f902013-03-26 14:45:27 +0000903
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000904 return true;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000905}