blob: 2b4689e4d60facdca72119d5e6b63f517e09de37 [file] [log] [blame]
bsalomon@google.comf75b84e2011-09-29 14:58:28 +00001/*
2 * Copyright 2011 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
bsalomon@google.comaeb21602011-08-30 18:13:44 +00008#include "GrAAHairLinePathRenderer.h"
9
10#include "GrContext.h"
tomhudson@google.com93813632011-10-27 20:21:16 +000011#include "GrDrawState.h"
bsalomon@google.comc26d94f2013-03-25 18:19:00 +000012#include "GrDrawTargetCaps.h"
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000013#include "GrEffect.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000014#include "GrGpu.h"
15#include "GrIndexBuffer.h"
bsalomon@google.comdbeeac32011-09-12 14:59:34 +000016#include "GrPathUtils.h"
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000017#include "GrTBackendEffectFactory.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000018#include "SkGeometry.h"
sugoi@google.com12b4e272012-12-06 20:13:11 +000019#include "SkStroke.h"
bsalomon@google.comaeb21602011-08-30 18:13:44 +000020#include "SkTemplates.h"
21
commit-bot@chromium.org07e1c3f2013-08-22 20:41:15 +000022#include "effects/GrBezierEffect.h"
bsalomon@google.com4647f902013-03-26 14:45:27 +000023
bsalomon@google.comaeb21602011-08-30 18:13:44 +000024namespace {
25// quadratics are rendered as 5-sided polys in order to bound the
26// AA stroke around the center-curve. See comments in push_quad_index_buffer and
egdaniel@google.com5383a752013-07-12 20:15:34 +000027// bloat_quad. Quadratics and conics share an index buffer
bsalomon@google.comaeb21602011-08-30 18:13:44 +000028static const int kVertsPerQuad = 5;
29static const int kIdxsPerQuad = 9;
30
robertphillips@google.comada90da2013-09-18 22:14:49 +000031// lines are rendered as:
32// *______________*
33// |\ -_______ /|
34// | \ \ / |
35// | *--------* |
36// | / ______/ \ |
37// */_-__________\*
38// For: 6 vertices and 18 indices (for 6 triangles)
jvanverth@google.com681ccf02013-08-16 14:51:51 +000039static const int kVertsPerLineSeg = 6;
robertphillips@google.comada90da2013-09-18 22:14:49 +000040static const int kIdxsPerLineSeg = 18;
bsalomon@google.comaeb21602011-08-30 18:13:44 +000041
42static const int kNumQuadsInIdxBuffer = 256;
43static const size_t kQuadIdxSBufize = kIdxsPerQuad *
44 sizeof(uint16_t) *
45 kNumQuadsInIdxBuffer;
46
jvanverth@google.com681ccf02013-08-16 14:51:51 +000047static const int kNumLineSegsInIdxBuffer = 256;
48static const size_t kLineSegIdxSBufize = kIdxsPerLineSeg *
49 sizeof(uint16_t) *
50 kNumLineSegsInIdxBuffer;
51
52static bool push_quad_index_data(GrIndexBuffer* qIdxBuffer) {
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000053 uint16_t* data = (uint16_t*) qIdxBuffer->map();
bsalomon@google.comaeb21602011-08-30 18:13:44 +000054 bool tempData = NULL == data;
55 if (tempData) {
tomhudson@google.comc377baf2012-07-09 20:17:56 +000056 data = SkNEW_ARRAY(uint16_t, kNumQuadsInIdxBuffer * kIdxsPerQuad);
bsalomon@google.comaeb21602011-08-30 18:13:44 +000057 }
58 for (int i = 0; i < kNumQuadsInIdxBuffer; ++i) {
59
60 // Each quadratic is rendered as a five sided polygon. This poly bounds
61 // the quadratic's bounding triangle but has been expanded so that the
62 // 1-pixel wide area around the curve is inside the poly.
63 // If a,b,c are the original control points then the poly a0,b0,c0,c1,a1
64 // that is rendered would look like this:
65 // b0
66 // b
67 //
68 // a0 c0
69 // a c
70 // a1 c1
bsalomon@google.com0e5104c2012-04-10 16:20:41 +000071 // Each is drawn as three triangles specified by these 9 indices:
bsalomon@google.comaeb21602011-08-30 18:13:44 +000072 int baseIdx = i * kIdxsPerQuad;
73 uint16_t baseVert = (uint16_t)(i * kVertsPerQuad);
74 data[0 + baseIdx] = baseVert + 0; // a0
75 data[1 + baseIdx] = baseVert + 1; // a1
76 data[2 + baseIdx] = baseVert + 2; // b0
77 data[3 + baseIdx] = baseVert + 2; // b0
78 data[4 + baseIdx] = baseVert + 4; // c1
79 data[5 + baseIdx] = baseVert + 3; // c0
80 data[6 + baseIdx] = baseVert + 1; // a1
81 data[7 + baseIdx] = baseVert + 4; // c1
82 data[8 + baseIdx] = baseVert + 2; // b0
83 }
84 if (tempData) {
85 bool ret = qIdxBuffer->updateData(data, kQuadIdxSBufize);
86 delete[] data;
87 return ret;
88 } else {
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000089 qIdxBuffer->unmap();
bsalomon@google.comaeb21602011-08-30 18:13:44 +000090 return true;
91 }
92}
jvanverth@google.com681ccf02013-08-16 14:51:51 +000093
94static bool push_line_index_data(GrIndexBuffer* lIdxBuffer) {
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +000095 uint16_t* data = (uint16_t*) lIdxBuffer->map();
jvanverth@google.com681ccf02013-08-16 14:51:51 +000096 bool tempData = NULL == data;
97 if (tempData) {
98 data = SkNEW_ARRAY(uint16_t, kNumLineSegsInIdxBuffer * kIdxsPerLineSeg);
99 }
100 for (int i = 0; i < kNumLineSegsInIdxBuffer; ++i) {
robertphillips@google.comada90da2013-09-18 22:14:49 +0000101 // Each line segment is rendered as two quads and two triangles.
102 // p0 and p1 have alpha = 1 while all other points have alpha = 0.
103 // The four external points are offset 1 pixel perpendicular to the
104 // line and half a pixel parallel to the line.
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000105 //
106 // p4 p5
robertphillips@google.comada90da2013-09-18 22:14:49 +0000107 // p0 p1
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000108 // p2 p3
109 //
robertphillips@google.comada90da2013-09-18 22:14:49 +0000110 // Each is drawn as six triangles specified by these 18 indices:
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000111 int baseIdx = i * kIdxsPerLineSeg;
112 uint16_t baseVert = (uint16_t)(i * kVertsPerLineSeg);
robertphillips@google.comada90da2013-09-18 22:14:49 +0000113 data[0 + baseIdx] = baseVert + 0;
114 data[1 + baseIdx] = baseVert + 1;
115 data[2 + baseIdx] = baseVert + 3;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000116
robertphillips@google.comada90da2013-09-18 22:14:49 +0000117 data[3 + baseIdx] = baseVert + 0;
118 data[4 + baseIdx] = baseVert + 3;
119 data[5 + baseIdx] = baseVert + 2;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000120
robertphillips@google.comada90da2013-09-18 22:14:49 +0000121 data[6 + baseIdx] = baseVert + 0;
122 data[7 + baseIdx] = baseVert + 4;
123 data[8 + baseIdx] = baseVert + 5;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000124
robertphillips@google.comada90da2013-09-18 22:14:49 +0000125 data[9 + baseIdx] = baseVert + 0;
126 data[10+ baseIdx] = baseVert + 5;
127 data[11+ baseIdx] = baseVert + 1;
128
129 data[12 + baseIdx] = baseVert + 0;
130 data[13 + baseIdx] = baseVert + 2;
131 data[14 + baseIdx] = baseVert + 4;
132
133 data[15 + baseIdx] = baseVert + 1;
134 data[16 + baseIdx] = baseVert + 5;
135 data[17 + baseIdx] = baseVert + 3;
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000136 }
137 if (tempData) {
138 bool ret = lIdxBuffer->updateData(data, kLineSegIdxSBufize);
139 delete[] data;
140 return ret;
141 } else {
commit-bot@chromium.org8341eb72014-05-07 20:51:05 +0000142 lIdxBuffer->unmap();
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000143 return true;
144 }
145}
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000146}
147
148GrPathRenderer* GrAAHairLinePathRenderer::Create(GrContext* context) {
bsalomon@google.coma8a6a322011-09-23 14:19:58 +0000149 GrGpu* gpu = context->getGpu();
150 GrIndexBuffer* qIdxBuf = gpu->createIndexBuffer(kQuadIdxSBufize, false);
151 SkAutoTUnref<GrIndexBuffer> qIdxBuffer(qIdxBuf);
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000152 if (NULL == qIdxBuf || !push_quad_index_data(qIdxBuf)) {
153 return NULL;
154 }
155 GrIndexBuffer* lIdxBuf = gpu->createIndexBuffer(kLineSegIdxSBufize, false);
156 SkAutoTUnref<GrIndexBuffer> lIdxBuffer(lIdxBuf);
157 if (NULL == lIdxBuf || !push_line_index_data(lIdxBuf)) {
bsalomon@google.coma8a6a322011-09-23 14:19:58 +0000158 return NULL;
159 }
tomhudson@google.comc377baf2012-07-09 20:17:56 +0000160 return SkNEW_ARGS(GrAAHairLinePathRenderer,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000161 (context, lIdxBuf, qIdxBuf));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000162}
163
164GrAAHairLinePathRenderer::GrAAHairLinePathRenderer(
165 const GrContext* context,
166 const GrIndexBuffer* linesIndexBuffer,
167 const GrIndexBuffer* quadsIndexBuffer) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000168 fLinesIndexBuffer = linesIndexBuffer;
169 linesIndexBuffer->ref();
170 fQuadsIndexBuffer = quadsIndexBuffer;
171 quadsIndexBuffer->ref();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000172}
173
174GrAAHairLinePathRenderer::~GrAAHairLinePathRenderer() {
175 fLinesIndexBuffer->unref();
176 fQuadsIndexBuffer->unref();
177}
178
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000179namespace {
180
bsalomon@google.com92669012011-09-27 19:10:05 +0000181#define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true>
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000182
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000183// Takes 178th time of logf on Z600 / VC2010
184int get_float_exp(float x) {
185 GR_STATIC_ASSERT(sizeof(int) == sizeof(float));
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000186#ifdef SK_DEBUG
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000187 static bool tested;
188 if (!tested) {
189 tested = true;
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000190 SkASSERT(get_float_exp(0.25f) == -2);
191 SkASSERT(get_float_exp(0.3f) == -2);
192 SkASSERT(get_float_exp(0.5f) == -1);
193 SkASSERT(get_float_exp(1.f) == 0);
194 SkASSERT(get_float_exp(2.f) == 1);
195 SkASSERT(get_float_exp(2.5f) == 1);
196 SkASSERT(get_float_exp(8.f) == 3);
197 SkASSERT(get_float_exp(100.f) == 6);
198 SkASSERT(get_float_exp(1000.f) == 9);
199 SkASSERT(get_float_exp(1024.f) == 10);
200 SkASSERT(get_float_exp(3000000.f) == 21);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000201 }
202#endif
bsalomon@google.com2ec72802011-09-21 21:46:03 +0000203 const int* iptr = (const int*)&x;
204 return (((*iptr) & 0x7f800000) >> 23) - 127;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000205}
206
egdaniel@google.com5383a752013-07-12 20:15:34 +0000207// Uses the max curvature function for quads to estimate
208// where to chop the conic. If the max curvature is not
209// found along the curve segment it will return 1 and
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000210// dst[0] is the original conic. If it returns 2 the dst[0]
egdaniel@google.com5383a752013-07-12 20:15:34 +0000211// and dst[1] are the two new conics.
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000212int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000213 SkScalar t = SkFindQuadMaxCurvature(src);
214 if (t == 0) {
215 if (dst) {
216 dst[0].set(src, weight);
217 }
218 return 1;
219 } else {
220 if (dst) {
221 SkConic conic;
222 conic.set(src, weight);
223 conic.chopAt(t, dst);
224 }
225 return 2;
226 }
227}
228
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000229// Calls split_conic on the entire conic and then once more on each subsection.
230// Most cases will result in either 1 conic (chop point is not within t range)
231// or 3 points (split once and then one subsection is split again).
232int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) {
233 SkConic dstTemp[2];
234 int conicCnt = split_conic(src, dstTemp, weight);
235 if (2 == conicCnt) {
236 int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW);
237 conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW);
238 } else {
239 dst[0] = dstTemp[0];
240 }
241 return conicCnt;
242}
243
egdaniel@google.com5383a752013-07-12 20:15:34 +0000244// returns 0 if quad/conic is degen or close to it
245// in this case approx the path with lines
246// otherwise returns 1
247int is_degen_quad_or_conic(const SkPoint p[3]) {
248 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
249 static const SkScalar gDegenerateToLineTolSqd =
250 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
251
252 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
253 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
254 return 1;
255 }
256
257 SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
258 if (dsqd < gDegenerateToLineTolSqd) {
259 return 1;
260 }
261
262 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
263 return 1;
264 }
265 return 0;
266}
267
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000268// we subdivide the quads to avoid huge overfill
269// if it returns -1 then should be drawn as lines
270int num_quad_subdivs(const SkPoint p[3]) {
271 static const SkScalar gDegenerateToLineTol = SK_Scalar1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000272 static const SkScalar gDegenerateToLineTolSqd =
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000273 SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000274
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000275 if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd ||
276 p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000277 return -1;
278 }
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000279
bsalomon@google.com81712882012-11-01 17:12:34 +0000280 SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]);
bsalomon@google.com46a2a1e2011-09-06 22:10:52 +0000281 if (dsqd < gDegenerateToLineTolSqd) {
282 return -1;
283 }
284
285 if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000286 return -1;
287 }
288
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000289 // tolerance of triangle height in pixels
290 // tuned on windows Quadro FX 380 / Z600
291 // trade off of fill vs cpu time on verts
292 // maybe different when do this using gpu (geo or tess shaders)
293 static const SkScalar gSubdivTol = 175 * SK_Scalar1;
294
robertphillips@google.com7460b372012-04-25 16:54:51 +0000295 if (dsqd <= SkScalarMul(gSubdivTol, gSubdivTol)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000296 return 0;
297 } else {
robertphillips@google.com87379e12013-03-29 12:11:10 +0000298 static const int kMaxSub = 4;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000299 // subdividing the quad reduces d by 4. so we want x = log4(d/tol)
300 // = log4(d*d/tol*tol)/2
301 // = log2(d*d/tol*tol)
302
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000303 // +1 since we're ignoring the mantissa contribution.
304 int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000305 log = SkTMin(SkTMax(0, log), kMaxSub);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000306 return log;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000307 }
308}
309
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000310/**
311 * Generates the lines and quads to be rendered. Lines are always recorded in
312 * device space. We will do a device space bloat to account for the 1pixel
313 * thickness.
314 * Quads are recorded in device space unless m contains
315 * perspective, then in they are in src space. We do this because we will
316 * subdivide large quads to reduce over-fill. This subdivision has to be
317 * performed before applying the perspective matrix.
318 */
319int generate_lines_and_quads(const SkPath& path,
320 const SkMatrix& m,
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000321 const SkIRect& devClipBounds,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000322 GrAAHairLinePathRenderer::PtArray* lines,
323 GrAAHairLinePathRenderer::PtArray* quads,
324 GrAAHairLinePathRenderer::PtArray* conics,
325 GrAAHairLinePathRenderer::IntArray* quadSubdivCnts,
326 GrAAHairLinePathRenderer::FloatArray* conicWeights) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000327 SkPath::Iter iter(path, false);
328
329 int totalQuadCount = 0;
commit-bot@chromium.orgfd03d4a2013-07-17 21:39:42 +0000330 SkRect bounds;
331 SkIRect ibounds;
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000332
333 bool persp = m.hasPerspective();
334
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000335 for (;;) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000336 SkPoint pathPts[4];
337 SkPoint devPts[4];
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000338 SkPath::Verb verb = iter.next(pathPts);
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000339 switch (verb) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000340 case SkPath::kConic_Verb: {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000341 SkConic dst[4];
342 // We chop the conics to create tighter clipping to hide error
343 // that appears near max curvature of very thin conics. Thin
344 // hyperbolas with high weight still show error.
egdaniel@google.com5383a752013-07-12 20:15:34 +0000345 int conicCnt = chop_conic(pathPts, dst, iter.conicWeight());
346 for (int i = 0; i < conicCnt; ++i) {
347 SkPoint* chopPnts = dst[i].fPts;
348 m.mapPoints(devPts, chopPnts, 3);
349 bounds.setBounds(devPts, 3);
350 bounds.outset(SK_Scalar1, SK_Scalar1);
351 bounds.roundOut(&ibounds);
352 if (SkIRect::Intersects(devClipBounds, ibounds)) {
353 if (is_degen_quad_or_conic(devPts)) {
354 SkPoint* pts = lines->push_back_n(4);
355 pts[0] = devPts[0];
356 pts[1] = devPts[1];
357 pts[2] = devPts[1];
358 pts[3] = devPts[2];
359 } else {
360 // when in perspective keep conics in src space
361 SkPoint* cPts = persp ? chopPnts : devPts;
362 SkPoint* pts = conics->push_back_n(3);
363 pts[0] = cPts[0];
364 pts[1] = cPts[1];
365 pts[2] = cPts[2];
366 conicWeights->push_back() = dst[i].fW;
367 }
368 }
369 }
reed@google.com277c3f82013-05-31 15:17:50 +0000370 break;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000371 }
372 case SkPath::kMove_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000373 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000374 case SkPath::kLine_Verb:
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000375 m.mapPoints(devPts, pathPts, 2);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000376 bounds.setBounds(devPts, 2);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000377 bounds.outset(SK_Scalar1, SK_Scalar1);
378 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000379 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000380 SkPoint* pts = lines->push_back_n(2);
381 pts[0] = devPts[0];
382 pts[1] = devPts[1];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000383 }
384 break;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000385 case SkPath::kQuad_Verb: {
386 SkPoint choppedPts[5];
387 // Chopping the quad helps when the quad is either degenerate or nearly degenerate.
388 // When it is degenerate it allows the approximation with lines to work since the
389 // chop point (if there is one) will be at the parabola's vertex. In the nearly
390 // degenerate the QuadUVMatrix computed for the points is almost singular which
391 // can cause rendering artifacts.
392 int n = SkChopQuadAtMaxCurvature(pathPts, choppedPts);
393 for (int i = 0; i < n; ++i) {
394 SkPoint* quadPts = choppedPts + i * 2;
395 m.mapPoints(devPts, quadPts, 3);
396 bounds.setBounds(devPts, 3);
397 bounds.outset(SK_Scalar1, SK_Scalar1);
398 bounds.roundOut(&ibounds);
399
400 if (SkIRect::Intersects(devClipBounds, ibounds)) {
401 int subdiv = num_quad_subdivs(devPts);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000402 SkASSERT(subdiv >= -1);
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000403 if (-1 == subdiv) {
404 SkPoint* pts = lines->push_back_n(4);
405 pts[0] = devPts[0];
406 pts[1] = devPts[1];
407 pts[2] = devPts[1];
408 pts[3] = devPts[2];
409 } else {
410 // when in perspective keep quads in src space
411 SkPoint* qPts = persp ? quadPts : devPts;
412 SkPoint* pts = quads->push_back_n(3);
413 pts[0] = qPts[0];
414 pts[1] = qPts[1];
415 pts[2] = qPts[2];
416 quadSubdivCnts->push_back() = subdiv;
417 totalQuadCount += 1 << subdiv;
418 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000419 }
420 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000421 break;
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000422 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000423 case SkPath::kCubic_Verb:
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000424 m.mapPoints(devPts, pathPts, 4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000425 bounds.setBounds(devPts, 4);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000426 bounds.outset(SK_Scalar1, SK_Scalar1);
427 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000428 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.com92669012011-09-27 19:10:05 +0000429 PREALLOC_PTARRAY(32) q;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000430 // we don't need a direction if we aren't constraining the subdivision
431 static const SkPath::Direction kDummyDir = SkPath::kCCW_Direction;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000432 // We convert cubics to quadratics (for now).
433 // In perspective have to do conversion in src space.
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000434 if (persp) {
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000435 SkScalar tolScale =
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000436 GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m,
437 path.getBounds());
commit-bot@chromium.org912e68e2013-05-24 18:51:55 +0000438 GrPathUtils::convertCubicToQuads(pathPts, tolScale, false, kDummyDir, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000439 } else {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000440 GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, false, kDummyDir, &q);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000441 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000442 for (int i = 0; i < q.count(); i += 3) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000443 SkPoint* qInDevSpace;
444 // bounds has to be calculated in device space, but q is
445 // in src space when there is perspective.
446 if (persp) {
447 m.mapPoints(devPts, &q[i], 3);
448 bounds.setBounds(devPts, 3);
449 qInDevSpace = devPts;
450 } else {
451 bounds.setBounds(&q[i], 3);
452 qInDevSpace = &q[i];
453 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000454 bounds.outset(SK_Scalar1, SK_Scalar1);
455 bounds.roundOut(&ibounds);
robertphillips@google.com7b112892012-07-31 15:18:21 +0000456 if (SkIRect::Intersects(devClipBounds, ibounds)) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000457 int subdiv = num_quad_subdivs(qInDevSpace);
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000458 SkASSERT(subdiv >= -1);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000459 if (-1 == subdiv) {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000460 SkPoint* pts = lines->push_back_n(4);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000461 // lines should always be in device coords
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000462 pts[0] = qInDevSpace[0];
463 pts[1] = qInDevSpace[1];
464 pts[2] = qInDevSpace[1];
465 pts[3] = qInDevSpace[2];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000466 } else {
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000467 SkPoint* pts = quads->push_back_n(3);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000468 // q is already in src space when there is no
469 // perspective and dev coords otherwise.
bsalomon@google.coma996fec2011-09-13 18:49:13 +0000470 pts[0] = q[0 + i];
471 pts[1] = q[1 + i];
472 pts[2] = q[2 + i];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000473 quadSubdivCnts->push_back() = subdiv;
474 totalQuadCount += 1 << subdiv;
475 }
476 }
477 }
478 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000479 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000480 case SkPath::kClose_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000481 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000482 case SkPath::kDone_Verb:
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000483 return totalQuadCount;
484 }
485 }
486}
487
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000488struct LineVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000489 SkPoint fPos;
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000490 GrColor fCoverage;
491};
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000492
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000493struct BezierVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000494 SkPoint fPos;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000495 union {
496 struct {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000497 SkScalar fK;
498 SkScalar fL;
499 SkScalar fM;
egdaniel@google.com5383a752013-07-12 20:15:34 +0000500 } fConic;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000501 SkVector fQuadCoord;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000502 struct {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000503 SkScalar fBogus[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000504 };
505 };
506};
egdaniel@google.com5383a752013-07-12 20:15:34 +0000507
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000508GR_STATIC_ASSERT(sizeof(BezierVertex) == 3 * sizeof(SkPoint));
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000509
510void intersect_lines(const SkPoint& ptA, const SkVector& normA,
511 const SkPoint& ptB, const SkVector& normB,
512 SkPoint* result) {
513
514 SkScalar lineAW = -normA.dot(ptA);
515 SkScalar lineBW = -normB.dot(ptB);
516
517 SkScalar wInv = SkScalarMul(normA.fX, normB.fY) -
egdaniel@google.com5383a752013-07-12 20:15:34 +0000518 SkScalarMul(normA.fY, normB.fX);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000519 wInv = SkScalarInvert(wInv);
520
521 result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY);
522 result->fX = SkScalarMul(result->fX, wInv);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000523
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000524 result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW);
525 result->fY = SkScalarMul(result->fY, wInv);
526}
527
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000528void set_uv_quad(const SkPoint qpts[3], BezierVertex verts[kVertsPerQuad]) {
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000529 // this should be in the src space, not dev coords, when we have perspective
530 GrPathUtils::QuadUVMatrix DevToUV(qpts);
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000531 DevToUV.apply<kVertsPerQuad, sizeof(BezierVertex), sizeof(SkPoint)>(verts);
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000532}
533
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000534void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000535 const SkMatrix* toSrc, BezierVertex verts[kVertsPerQuad],
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000536 SkRect* devBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000537 SkASSERT(!toDevice == !toSrc);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000538 // original quad is specified by tri a,b,c
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000539 SkPoint a = qpts[0];
540 SkPoint b = qpts[1];
541 SkPoint c = qpts[2];
542
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000543 if (toDevice) {
544 toDevice->mapPoints(&a, 1);
545 toDevice->mapPoints(&b, 1);
546 toDevice->mapPoints(&c, 1);
547 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000548 // make a new poly where we replace a and c by a 1-pixel wide edges orthog
549 // to edges ab and bc:
550 //
551 // before | after
552 // | b0
553 // b |
554 // |
555 // | a0 c0
556 // a c | a1 c1
557 //
558 // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c,
559 // respectively.
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000560 BezierVertex& a0 = verts[0];
561 BezierVertex& a1 = verts[1];
562 BezierVertex& b0 = verts[2];
563 BezierVertex& c0 = verts[3];
564 BezierVertex& c1 = verts[4];
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000565
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000566 SkVector ab = b;
567 ab -= a;
568 SkVector ac = c;
569 ac -= a;
570 SkVector cb = b;
571 cb -= c;
572
573 // We should have already handled degenerates
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000574 SkASSERT(ab.length() > 0 && cb.length() > 0);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000575
576 ab.normalize();
577 SkVector abN;
578 abN.setOrthog(ab, SkVector::kLeft_Side);
579 if (abN.dot(ac) > 0) {
580 abN.negate();
581 }
582
583 cb.normalize();
584 SkVector cbN;
585 cbN.setOrthog(cb, SkVector::kLeft_Side);
586 if (cbN.dot(ac) < 0) {
587 cbN.negate();
588 }
589
590 a0.fPos = a;
591 a0.fPos += abN;
592 a1.fPos = a;
593 a1.fPos -= abN;
594
595 c0.fPos = c;
596 c0.fPos += cbN;
597 c1.fPos = c;
598 c1.fPos -= cbN;
599
600 intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos);
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000601 devBounds->growToInclude(&verts[0].fPos, sizeof(BezierVertex), kVertsPerQuad);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000602
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000603 if (toSrc) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000604 toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(BezierVertex), kVertsPerQuad);
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000605 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000606}
607
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000608// Equations based off of Loop-Blinn Quadratic GPU Rendering
egdaniel@google.com5383a752013-07-12 20:15:34 +0000609// Input Parametric:
610// 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)
611// Output Implicit:
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000612// f(x, y, w) = f(P) = K^2 - LM
613// K = dot(k, P), L = dot(l, P), M = dot(m, P)
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000614// k, l, m are calculated in function GrPathUtils::getConicKLM
615void set_conic_coeffs(const SkPoint p[3], BezierVertex verts[kVertsPerQuad],
616 const SkScalar weight) {
617 SkScalar klm[9];
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000618
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000619 GrPathUtils::getConicKLM(p, weight, klm);
egdaniel@google.com5383a752013-07-12 20:15:34 +0000620
621 for (int i = 0; i < kVertsPerQuad; ++i) {
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000622 const SkPoint pnt = verts[i].fPos;
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000623 verts[i].fConic.fK = pnt.fX * klm[0] + pnt.fY * klm[1] + klm[2];
624 verts[i].fConic.fL = pnt.fX * klm[3] + pnt.fY * klm[4] + klm[5];
625 verts[i].fConic.fM = pnt.fX * klm[6] + pnt.fY * klm[7] + klm[8];
egdaniel@google.com5383a752013-07-12 20:15:34 +0000626 }
627}
628
629void add_conics(const SkPoint p[3],
commit-bot@chromium.org13948402013-08-20 17:55:43 +0000630 const SkScalar weight,
egdaniel@google.com5383a752013-07-12 20:15:34 +0000631 const SkMatrix* toDevice,
632 const SkMatrix* toSrc,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000633 BezierVertex** vert,
egdaniel@google.com5383a752013-07-12 20:15:34 +0000634 SkRect* devBounds) {
635 bloat_quad(p, toDevice, toSrc, *vert, devBounds);
636 set_conic_coeffs(p, *vert, weight);
637 *vert += kVertsPerQuad;
638}
639
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000640void add_quads(const SkPoint p[3],
641 int subdiv,
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000642 const SkMatrix* toDevice,
643 const SkMatrix* toSrc,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000644 BezierVertex** vert,
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000645 SkRect* devBounds) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000646 SkASSERT(subdiv >= 0);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000647 if (subdiv) {
648 SkPoint newP[5];
649 SkChopQuadAtHalf(p, newP);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000650 add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert, devBounds);
651 add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert, devBounds);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000652 } else {
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000653 bloat_quad(p, toDevice, toSrc, *vert, devBounds);
egdaniel@google.com34b05ca2013-08-05 20:43:12 +0000654 set_uv_quad(p, *vert);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000655 *vert += kVertsPerQuad;
656 }
657}
658
659void add_line(const SkPoint p[2],
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000660 const SkMatrix* toSrc,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000661 GrColor coverage,
662 LineVertex** vert) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000663 const SkPoint& a = p[0];
664 const SkPoint& b = p[1];
665
robertphillips@google.comada90da2013-09-18 22:14:49 +0000666 SkVector ortho, vec = b;
667 vec -= a;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000668
robertphillips@google.comada90da2013-09-18 22:14:49 +0000669 if (vec.setLength(SK_ScalarHalf)) {
670 // Create a vector orthogonal to 'vec' and of unit length
671 ortho.fX = 2.0f * vec.fY;
672 ortho.fY = -2.0f * vec.fX;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000673
robertphillips@google.comada90da2013-09-18 22:14:49 +0000674 (*vert)[0].fPos = a;
675 (*vert)[0].fCoverage = coverage;
676 (*vert)[1].fPos = b;
677 (*vert)[1].fCoverage = coverage;
678 (*vert)[2].fPos = a - vec + ortho;
679 (*vert)[2].fCoverage = 0;
680 (*vert)[3].fPos = b + vec + ortho;
681 (*vert)[3].fCoverage = 0;
682 (*vert)[4].fPos = a - vec - ortho;
683 (*vert)[4].fCoverage = 0;
684 (*vert)[5].fPos = b + vec - ortho;
685 (*vert)[5].fCoverage = 0;
686
bsalomon49f085d2014-09-05 13:34:00 -0700687 if (toSrc) {
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000688 toSrc->mapPointsWithStride(&(*vert)->fPos,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000689 sizeof(LineVertex),
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000690 kVertsPerLineSeg);
691 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000692 } else {
693 // just make it degenerate and likely offscreen
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000694 for (int i = 0; i < kVertsPerLineSeg; ++i) {
695 (*vert)[i].fPos.set(SK_ScalarMax, SK_ScalarMax);
696 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000697 }
698
699 *vert += kVertsPerLineSeg;
700}
701
702}
703
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000704///////////////////////////////////////////////////////////////////////////////
705
robertphillips@google.com42903302013-04-20 12:26:07 +0000706namespace {
707
708// position + edge
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000709extern const GrVertexAttrib gHairlineBezierAttribs[] = {
robertphillips@google.com42903302013-04-20 12:26:07 +0000710 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000711 {kVec4f_GrVertexAttribType, sizeof(SkPoint), kEffect_GrVertexAttribBinding}
robertphillips@google.com42903302013-04-20 12:26:07 +0000712};
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000713
714// position + coverage
715extern const GrVertexAttrib gHairlineLineAttribs[] = {
716 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000717 {kVec4ub_GrVertexAttribType, sizeof(SkPoint), kCoverage_GrVertexAttribBinding},
robertphillips@google.com42903302013-04-20 12:26:07 +0000718};
719
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000720};
721
robertphillips@google.comada90da2013-09-18 22:14:49 +0000722bool GrAAHairLinePathRenderer::createLineGeom(const SkPath& path,
723 GrDrawTarget* target,
724 const PtArray& lines,
725 int lineCnt,
726 GrDrawTarget::AutoReleaseGeometry* arg,
727 SkRect* devBounds) {
jvanverth@google.com9b855c72013-03-01 18:21:22 +0000728 GrDrawState* drawState = target->drawState();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000729
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000730 const SkMatrix& viewM = drawState->getViewMatrix();
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000731
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000732 int vertCnt = kVertsPerLineSeg * lineCnt;
733
egdaniel7b3d5ee2014-08-28 05:41:14 -0700734 drawState->setVertexAttribs<gHairlineLineAttribs>(SK_ARRAY_COUNT(gHairlineLineAttribs),
735 sizeof(LineVertex));
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000736
737 if (!arg->set(target, vertCnt, 0)) {
738 return false;
739 }
740
741 LineVertex* verts = reinterpret_cast<LineVertex*>(arg->vertices());
742
743 const SkMatrix* toSrc = NULL;
744 SkMatrix ivm;
745
746 if (viewM.hasPerspective()) {
747 if (viewM.invert(&ivm)) {
748 toSrc = &ivm;
749 }
750 }
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000751 devBounds->set(lines.begin(), lines.count());
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000752 for (int i = 0; i < lineCnt; ++i) {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000753 add_line(&lines[2*i], toSrc, drawState->getCoverageColor(), &verts);
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000754 }
skia.committer@gmail.comf91e3d42013-09-20 07:01:33 +0000755 // All the verts computed by add_line are within sqrt(1^2 + 0.5^2) of the end points.
commit-bot@chromium.org4b413c82013-11-25 19:44:07 +0000756 static const SkScalar kSqrtOfOneAndAQuarter = 1.118f;
robertphillips@google.com52c75262013-09-19 16:36:43 +0000757 // Add a little extra to account for vector normalization precision.
758 static const SkScalar kOutset = kSqrtOfOneAndAQuarter + SK_Scalar1 / 20;
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000759 devBounds->outset(kOutset, kOutset);
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000760
761 return true;
762}
763
764bool GrAAHairLinePathRenderer::createBezierGeom(
765 const SkPath& path,
766 GrDrawTarget* target,
767 const PtArray& quads,
768 int quadCnt,
769 const PtArray& conics,
770 int conicCnt,
771 const IntArray& qSubdivs,
772 const FloatArray& cWeights,
773 GrDrawTarget::AutoReleaseGeometry* arg,
774 SkRect* devBounds) {
775 GrDrawState* drawState = target->drawState();
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000776
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000777 const SkMatrix& viewM = drawState->getViewMatrix();
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000778
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000779 int vertCnt = kVertsPerQuad * quadCnt + kVertsPerQuad * conicCnt;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000780
egdaniel7b3d5ee2014-08-28 05:41:14 -0700781 int vAttribCnt = SK_ARRAY_COUNT(gHairlineBezierAttribs);
782 target->drawState()->setVertexAttribs<gHairlineBezierAttribs>(vAttribCnt, sizeof(BezierVertex));
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000783
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000784 if (!arg->set(target, vertCnt, 0)) {
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000785 return false;
786 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000787
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000788 BezierVertex* verts = reinterpret_cast<BezierVertex*>(arg->vertices());
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000789
bsalomon@google.comb9086a02012-11-01 18:02:54 +0000790 const SkMatrix* toDevice = NULL;
791 const SkMatrix* toSrc = NULL;
792 SkMatrix ivm;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000793
bsalomon@google.comdbeeac32011-09-12 14:59:34 +0000794 if (viewM.hasPerspective()) {
795 if (viewM.invert(&ivm)) {
796 toDevice = &viewM;
797 toSrc = &ivm;
798 }
799 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000800
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000801 // Seed the dev bounds with some pts known to be inside. Each quad and conic grows the bounding
802 // box to include its vertices.
803 SkPoint seedPts[2];
804 if (quadCnt) {
805 seedPts[0] = quads[0];
806 seedPts[1] = quads[2];
807 } else if (conicCnt) {
808 seedPts[0] = conics[0];
809 seedPts[1] = conics[2];
810 }
bsalomon49f085d2014-09-05 13:34:00 -0700811 if (toDevice) {
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000812 toDevice->mapPoints(seedPts, 2);
813 }
814 devBounds->set(seedPts[0], seedPts[1]);
815
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000816 int unsubdivQuadCnt = quads.count() / 3;
817 for (int i = 0; i < unsubdivQuadCnt; ++i) {
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000818 SkASSERT(qSubdivs[i] >= 0);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000819 add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts, devBounds);
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000820 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000821
egdaniel@google.com5383a752013-07-12 20:15:34 +0000822 // Start Conics
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000823 for (int i = 0; i < conicCnt; ++i) {
egdaniel@google.com5383a752013-07-12 20:15:34 +0000824 add_conics(&conics[3*i], cWeights[i], toDevice, toSrc, &verts, devBounds);
825 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000826 return true;
827}
828
robertphillips@google.come79f3202014-02-11 16:30:21 +0000829bool GrAAHairLinePathRenderer::canDrawPath(const SkPath& path,
830 const SkStrokeRec& stroke,
robertphillips@google.com8a4fc402012-05-24 12:42:24 +0000831 const GrDrawTarget* target,
832 bool antiAlias) const {
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000833 if (!antiAlias) {
834 return false;
835 }
836
837 if (!IsStrokeHairlineOrEquivalent(stroke,
838 target->getDrawState().getViewMatrix(),
839 NULL)) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000840 return false;
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000841 }
842
robertphillips@google.come79f3202014-02-11 16:30:21 +0000843 if (SkPath::kLine_SegmentMask == path.getSegmentMasks() ||
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000844 target->caps()->shaderDerivativeSupport()) {
845 return true;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000846 }
egdaniel@google.com3f2a2d52013-08-01 17:09:11 +0000847 return false;
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000848}
849
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000850template <class VertexType>
851bool check_bounds(GrDrawState* drawState, const SkRect& devBounds, void* vertices, int vCount)
852{
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000853 SkRect tolDevBounds = devBounds;
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000854 // The bounds ought to be tight, but in perspective the below code runs the verts
855 // through the view matrix to get back to dev coords, which can introduce imprecision.
856 if (drawState->getViewMatrix().hasPerspective()) {
857 tolDevBounds.outset(SK_Scalar1 / 1000, SK_Scalar1 / 1000);
858 } else {
859 // Non-persp matrices cause this path renderer to draw in device space.
860 SkASSERT(drawState->getViewMatrix().isIdentity());
861 }
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000862 SkRect actualBounds;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000863
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000864 VertexType* verts = reinterpret_cast<VertexType*>(vertices);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000865 bool first = true;
866 for (int i = 0; i < vCount; ++i) {
867 SkPoint pos = verts[i].fPos;
868 // This is a hack to workaround the fact that we move some degenerate segments offscreen.
869 if (SK_ScalarMax == pos.fX) {
870 continue;
871 }
872 drawState->getViewMatrix().mapPoints(&pos, 1);
873 if (first) {
874 actualBounds.set(pos.fX, pos.fY, pos.fX, pos.fY);
875 first = false;
876 } else {
877 actualBounds.growToInclude(pos.fX, pos.fY);
878 }
879 }
880 if (!first) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000881 return tolDevBounds.contains(actualBounds);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000882 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000883
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000884 return true;
885}
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000886
robertphillips@google.come79f3202014-02-11 16:30:21 +0000887bool GrAAHairLinePathRenderer::onDrawPath(const SkPath& path,
888 const SkStrokeRec& stroke,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000889 GrDrawTarget* target,
890 bool antiAlias) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000891 GrDrawState* drawState = target->drawState();
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000892
commit-bot@chromium.orge0a868c2013-11-22 07:02:11 +0000893 SkScalar hairlineCoverage;
894 if (IsStrokeHairlineOrEquivalent(stroke,
895 target->getDrawState().getViewMatrix(),
896 &hairlineCoverage)) {
897 uint8_t newCoverage = SkScalarRoundToInt(hairlineCoverage *
898 target->getDrawState().getCoverage());
899 target->drawState()->setCoverage(newCoverage);
900 }
901
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000902 SkIRect devClipBounds;
903 target->getClip()->getConservativeBounds(drawState->getRenderTarget(), &devClipBounds);
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000904
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000905 int lineCnt;
906 int quadCnt;
907 int conicCnt;
908 PREALLOC_PTARRAY(128) lines;
909 PREALLOC_PTARRAY(128) quads;
910 PREALLOC_PTARRAY(128) conics;
911 IntArray qSubdivs;
912 FloatArray cWeights;
robertphillips@google.come79f3202014-02-11 16:30:21 +0000913 quadCnt = generate_lines_and_quads(path, drawState->getViewMatrix(), devClipBounds,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000914 &lines, &quads, &conics, &qSubdivs, &cWeights);
915 lineCnt = lines.count() / 2;
916 conicCnt = conics.count() / 3;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000917
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000918 // do lines first
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000919 if (lineCnt) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000920 GrDrawTarget::AutoReleaseGeometry arg;
921 SkRect devBounds;
922
robertphillips@google.come79f3202014-02-11 16:30:21 +0000923 if (!this->createLineGeom(path,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000924 target,
925 lines,
926 lineCnt,
927 &arg,
928 &devBounds)) {
929 return false;
930 }
931
932 GrDrawTarget::AutoStateRestore asr;
933
robertphillips@google.comada90da2013-09-18 22:14:49 +0000934 // createLineGeom transforms the geometry to device space when the matrix does not have
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000935 // perspective.
936 if (target->getDrawState().getViewMatrix().hasPerspective()) {
937 asr.set(target, GrDrawTarget::kPreserve_ASRInit);
938 } else if (!asr.setIdentity(target, GrDrawTarget::kPreserve_ASRInit)) {
939 return false;
940 }
941 GrDrawState* drawState = target->drawState();
942
943 // Check devBounds
robertphillips@google.com52c75262013-09-19 16:36:43 +0000944 SkASSERT(check_bounds<LineVertex>(drawState, devBounds, arg.vertices(),
945 kVertsPerLineSeg * lineCnt));
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000946
947 {
948 GrDrawState::AutoRestoreEffects are(drawState);
949 target->setIndexSourceToBuffer(fLinesIndexBuffer);
950 int lines = 0;
951 while (lines < lineCnt) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000952 int n = SkTMin(lineCnt - lines, kNumLineSegsInIdxBuffer);
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000953 target->drawIndexed(kTriangles_GrPrimitiveType,
954 kVertsPerLineSeg*lines, // startV
955 0, // startI
956 kVertsPerLineSeg*n, // vCount
robertphillips@google.comada90da2013-09-18 22:14:49 +0000957 kIdxsPerLineSeg*n, // iCount
958 &devBounds);
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000959 lines += n;
960 }
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000961 }
bsalomon@google.comaeb21602011-08-30 18:13:44 +0000962 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000963
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000964 // then quadratics/conics
commit-bot@chromium.orgb8bd6cb2013-09-03 14:56:17 +0000965 if (quadCnt || conicCnt) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000966 GrDrawTarget::AutoReleaseGeometry arg;
967 SkRect devBounds;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000968
robertphillips@google.come79f3202014-02-11 16:30:21 +0000969 if (!this->createBezierGeom(path,
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000970 target,
971 quads,
972 quadCnt,
973 conics,
974 conicCnt,
975 qSubdivs,
976 cWeights,
977 &arg,
978 &devBounds)) {
979 return false;
980 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000981
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000982 GrDrawTarget::AutoStateRestore asr;
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000983
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000984 // createGeom transforms the geometry to device space when the matrix does not have
985 // perspective.
986 if (target->getDrawState().getViewMatrix().hasPerspective()) {
987 asr.set(target, GrDrawTarget::kPreserve_ASRInit);
988 } else if (!asr.setIdentity(target, GrDrawTarget::kPreserve_ASRInit)) {
989 return false;
990 }
991 GrDrawState* drawState = target->drawState();
992
jvanverth@google.com681ccf02013-08-16 14:51:51 +0000993 // Check devBounds
994 SkASSERT(check_bounds<BezierVertex>(drawState, devBounds, arg.vertices(),
995 kVertsPerQuad * quadCnt + kVertsPerQuad * conicCnt));
skia.committer@gmail.com74758112013-08-17 07:01:54 +0000996
commit-bot@chromium.org88462472013-08-23 21:01:52 +0000997 if (quadCnt > 0) {
bsalomon83d081a2014-07-08 09:56:10 -0700998 GrEffect* hairQuadEffect = GrQuadEffect::Create(kHairlineAA_GrEffectEdgeType,
999 *target->caps());
bsalomon49f085d2014-09-05 13:34:00 -07001000 SkASSERT(hairQuadEffect);
jvanverth@google.com681ccf02013-08-16 14:51:51 +00001001 GrDrawState::AutoRestoreEffects are(drawState);
1002 target->setIndexSourceToBuffer(fQuadsIndexBuffer);
joshualitt249af152014-09-15 11:41:13 -07001003 drawState->setGeometryProcessor(hairQuadEffect)->unref();
commit-bot@chromium.org88462472013-08-23 21:01:52 +00001004 int quads = 0;
jvanverth@google.com681ccf02013-08-16 14:51:51 +00001005 while (quads < quadCnt) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001006 int n = SkTMin(quadCnt - quads, kNumQuadsInIdxBuffer);
jvanverth@google.com681ccf02013-08-16 14:51:51 +00001007 target->drawIndexed(kTriangles_GrPrimitiveType,
1008 kVertsPerQuad*quads, // startV
1009 0, // startI
1010 kVertsPerQuad*n, // vCount
1011 kIdxsPerQuad*n, // iCount
1012 &devBounds);
1013 quads += n;
1014 }
1015 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +00001016
commit-bot@chromium.org88462472013-08-23 21:01:52 +00001017 if (conicCnt > 0) {
jvanverth@google.com681ccf02013-08-16 14:51:51 +00001018 GrDrawState::AutoRestoreEffects are(drawState);
bsalomon83d081a2014-07-08 09:56:10 -07001019 GrEffect* hairConicEffect = GrConicEffect::Create(kHairlineAA_GrEffectEdgeType,
1020 *target->caps());
bsalomon49f085d2014-09-05 13:34:00 -07001021 SkASSERT(hairConicEffect);
joshualitt249af152014-09-15 11:41:13 -07001022 drawState->setGeometryProcessor(hairConicEffect)->unref();
commit-bot@chromium.org88462472013-08-23 21:01:52 +00001023 int conics = 0;
jvanverth@google.com681ccf02013-08-16 14:51:51 +00001024 while (conics < conicCnt) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +00001025 int n = SkTMin(conicCnt - conics, kNumQuadsInIdxBuffer);
jvanverth@google.com681ccf02013-08-16 14:51:51 +00001026 target->drawIndexed(kTriangles_GrPrimitiveType,
1027 kVertsPerQuad*(quadCnt + conics), // startV
1028 0, // startI
1029 kVertsPerQuad*n, // vCount
1030 kIdxsPerQuad*n, // iCount
1031 &devBounds);
1032 conics += n;
1033 }
egdaniel@google.com5383a752013-07-12 20:15:34 +00001034 }
1035 }
skia.committer@gmail.com74758112013-08-17 07:01:54 +00001036
bsalomon@google.com0406b9e2013-04-02 21:00:15 +00001037 target->resetIndexSource();
bsalomon@google.com4647f902013-03-26 14:45:27 +00001038
bsalomon@google.comc2099d22012-03-02 21:26:50 +00001039 return true;
bsalomon@google.comaeb21602011-08-30 18:13:44 +00001040}