blob: 750ffefbf9fbfaad55041c67e864ae91b8c48fab [file] [log] [blame]
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +00001
2/*
3 * Copyright 2012 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
9#include "GrAAConvexPathRenderer.h"
10
11#include "GrContext.h"
12#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.com69cc6ad2012-01-17 14:25:10 +000015#include "GrPathUtils.h"
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000016#include "GrTBackendEffectFactory.h"
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000017#include "SkString.h"
sugoi@google.com5f74cf82012-12-17 21:16:45 +000018#include "SkStrokeRec.h"
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000019#include "SkTrace.h"
20
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +000021#include "gl/GrGLEffect.h"
22#include "gl/GrGLSL.h"
bsalomon@google.com4647f902013-03-26 14:45:27 +000023
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000024GrAAConvexPathRenderer::GrAAConvexPathRenderer() {
25}
26
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000027struct Segment {
28 enum {
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000029 // These enum values are assumed in member functions below.
30 kLine = 0,
31 kQuad = 1,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000032 } fType;
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000033
bsalomon@google.com9aed1142012-01-30 14:28:39 +000034 // line uses one pt, quad uses 2 pts
35 GrPoint fPts[2];
36 // normal to edge ending at each pt
37 GrVec fNorms[2];
38 // is the corner where the previous segment meets this segment
39 // sharp. If so, fMid is a normalized bisector facing outward.
40 GrVec fMid;
41
42 int countPoints() {
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000043 GR_STATIC_ASSERT(0 == kLine && 1 == kQuad);
44 return fType + 1;
bsalomon@google.com9aed1142012-01-30 14:28:39 +000045 }
46 const SkPoint& endPt() const {
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000047 GR_STATIC_ASSERT(0 == kLine && 1 == kQuad);
48 return fPts[fType];
bsalomon@google.com9aed1142012-01-30 14:28:39 +000049 };
50 const SkPoint& endNorm() const {
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000051 GR_STATIC_ASSERT(0 == kLine && 1 == kQuad);
52 return fNorms[fType];
bsalomon@google.com9aed1142012-01-30 14:28:39 +000053 };
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000054};
55
56typedef SkTArray<Segment, true> SegmentArray;
57
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +000058static void center_of_mass(const SegmentArray& segments, SkPoint* c) {
bsalomon@google.com81712882012-11-01 17:12:34 +000059 SkScalar area = 0;
vandebo@chromium.org6390c722012-03-28 21:03:22 +000060 SkPoint center = {0, 0};
bsalomon@google.com9aed1142012-01-30 14:28:39 +000061 int count = segments.count();
vandebo@chromium.org6390c722012-03-28 21:03:22 +000062 SkPoint p0 = {0, 0};
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000063 if (count > 2) {
64 // We translate the polygon so that the first point is at the origin.
65 // This avoids some precision issues with small area polygons far away
66 // from the origin.
67 p0 = segments[0].endPt();
68 SkPoint pi;
69 SkPoint pj;
bsalomon@google.coma51ab842012-07-10 19:53:34 +000070 // the first and last iteration of the below loop would compute
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000071 // zeros since the starting / ending point is (0,0). So instead we start
72 // at i=1 and make the last iteration i=count-2.
73 pj = segments[1].endPt() - p0;
74 for (int i = 1; i < count - 1; ++i) {
75 pi = pj;
76 const SkPoint pj = segments[i + 1].endPt() - p0;
77
bsalomon@google.com81712882012-11-01 17:12:34 +000078 SkScalar t = SkScalarMul(pi.fX, pj.fY) - SkScalarMul(pj.fX, pi.fY);
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000079 area += t;
80 center.fX += (pi.fX + pj.fX) * t;
81 center.fY += (pi.fY + pj.fY) * t;
82
83 }
bsalomon@google.com9aed1142012-01-30 14:28:39 +000084 }
bsalomon@google.com278dc692012-02-15 16:52:51 +000085 // If the poly has no area then we instead return the average of
86 // its points.
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000087 if (SkScalarNearlyZero(area)) {
bsalomon@google.com278dc692012-02-15 16:52:51 +000088 SkPoint avg;
89 avg.set(0, 0);
90 for (int i = 0; i < count; ++i) {
91 const SkPoint& pt = segments[i].endPt();
92 avg.fX += pt.fX;
93 avg.fY += pt.fY;
94 }
95 SkScalar denom = SK_Scalar1 / count;
96 avg.scale(denom);
97 *c = avg;
98 } else {
99 area *= 3;
bsalomon@google.com81712882012-11-01 17:12:34 +0000100 area = SkScalarDiv(SK_Scalar1, area);
101 center.fX = SkScalarMul(center.fX, area);
102 center.fY = SkScalarMul(center.fY, area);
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +0000103 // undo the translate of p0 to the origin.
104 *c = center + p0;
bsalomon@google.com278dc692012-02-15 16:52:51 +0000105 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000106 SkASSERT(!SkScalarIsNaN(c->fX) && !SkScalarIsNaN(c->fY));
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000107}
108
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000109static void compute_vectors(SegmentArray* segments,
110 SkPoint* fanPt,
111 SkPath::Direction dir,
112 int* vCount,
113 int* iCount) {
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000114 center_of_mass(*segments, fanPt);
115 int count = segments->count();
116
bsalomon@google.com278dc692012-02-15 16:52:51 +0000117 // Make the normals point towards the outside
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000118 GrPoint::Side normSide;
bsalomon@google.com278dc692012-02-15 16:52:51 +0000119 if (dir == SkPath::kCCW_Direction) {
120 normSide = GrPoint::kRight_Side;
121 } else {
122 normSide = GrPoint::kLeft_Side;
123 }
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000124
125 *vCount = 0;
126 *iCount = 0;
127 // compute normals at all points
128 for (int a = 0; a < count; ++a) {
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000129 Segment& sega = (*segments)[a];
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000130 int b = (a + 1) % count;
131 Segment& segb = (*segments)[b];
132
133 const GrPoint* prevPt = &sega.endPt();
134 int n = segb.countPoints();
135 for (int p = 0; p < n; ++p) {
136 segb.fNorms[p] = segb.fPts[p] - *prevPt;
137 segb.fNorms[p].normalize();
138 segb.fNorms[p].setOrthog(segb.fNorms[p], normSide);
139 prevPt = &segb.fPts[p];
140 }
141 if (Segment::kLine == segb.fType) {
142 *vCount += 5;
143 *iCount += 9;
144 } else {
145 *vCount += 6;
146 *iCount += 12;
147 }
148 }
149
150 // compute mid-vectors where segments meet. TODO: Detect shallow corners
151 // and leave out the wedges and close gaps by stitching segments together.
152 for (int a = 0; a < count; ++a) {
153 const Segment& sega = (*segments)[a];
154 int b = (a + 1) % count;
155 Segment& segb = (*segments)[b];
156 segb.fMid = segb.fNorms[0] + sega.endNorm();
157 segb.fMid.normalize();
158 // corner wedges
159 *vCount += 4;
160 *iCount += 6;
161 }
162}
163
bsalomon@google.com9732f622012-01-31 15:19:21 +0000164struct DegenerateTestData {
165 DegenerateTestData() { fStage = kInitial; }
166 bool isDegenerate() const { return kNonDegenerate != fStage; }
167 enum {
168 kInitial,
169 kPoint,
170 kLine,
171 kNonDegenerate
172 } fStage;
173 GrPoint fFirstPoint;
174 GrVec fLineNormal;
bsalomon@google.com81712882012-11-01 17:12:34 +0000175 SkScalar fLineC;
bsalomon@google.com9732f622012-01-31 15:19:21 +0000176};
177
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000178static const SkScalar kClose = (SK_Scalar1 / 16);
179static const SkScalar kCloseSqd = SkScalarMul(kClose, kClose);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000180
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000181static void update_degenerate_test(DegenerateTestData* data, const GrPoint& pt) {
bsalomon@google.com9732f622012-01-31 15:19:21 +0000182 switch (data->fStage) {
183 case DegenerateTestData::kInitial:
184 data->fFirstPoint = pt;
185 data->fStage = DegenerateTestData::kPoint;
186 break;
187 case DegenerateTestData::kPoint:
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000188 if (pt.distanceToSqd(data->fFirstPoint) > kCloseSqd) {
bsalomon@google.com9732f622012-01-31 15:19:21 +0000189 data->fLineNormal = pt - data->fFirstPoint;
190 data->fLineNormal.normalize();
191 data->fLineNormal.setOrthog(data->fLineNormal);
192 data->fLineC = -data->fLineNormal.dot(data->fFirstPoint);
193 data->fStage = DegenerateTestData::kLine;
194 }
195 break;
196 case DegenerateTestData::kLine:
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000197 if (SkScalarAbs(data->fLineNormal.dot(pt) + data->fLineC) > kClose) {
bsalomon@google.com9732f622012-01-31 15:19:21 +0000198 data->fStage = DegenerateTestData::kNonDegenerate;
199 }
200 case DegenerateTestData::kNonDegenerate:
201 break;
202 default:
203 GrCrash("Unexpected degenerate test stage.");
204 }
205}
206
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000207static inline bool get_direction(const SkPath& path, const SkMatrix& m, SkPath::Direction* dir) {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000208 if (!path.cheapComputeDirection(dir)) {
209 return false;
210 }
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000211 // check whether m reverses the orientation
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000212 SkASSERT(!m.hasPerspective());
bsalomon@google.com81712882012-11-01 17:12:34 +0000213 SkScalar det2x2 = SkScalarMul(m.get(SkMatrix::kMScaleX), m.get(SkMatrix::kMScaleY)) -
214 SkScalarMul(m.get(SkMatrix::kMSkewX), m.get(SkMatrix::kMSkewY));
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000215 if (det2x2 < 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000216 *dir = SkPath::OppositeDirection(*dir);
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000217 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000218 return true;
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000219}
220
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000221static inline void add_line_to_segment(const SkPoint& pt, SegmentArray* segments) {
222 segments->push_back();
223 segments->back().fType = Segment::kLine;
224 segments->back().fPts[0] = pt;
225}
226
227static inline void add_quad_segment(const SkPoint pts[3], SegmentArray* segments) {
228 if (pts[0].distanceToSqd(pts[1]) < kCloseSqd || pts[1].distanceToSqd(pts[2]) < kCloseSqd) {
229 if (pts[0] != pts[2]) {
230 add_line_to_segment(pts[2], segments);
231 }
232 } else {
233 segments->push_back();
234 segments->back().fType = Segment::kQuad;
235 segments->back().fPts[0] = pts[1];
236 segments->back().fPts[1] = pts[2];
237 }
238}
239
240static inline void add_cubic_segments(const SkPoint pts[4],
241 SkPath::Direction dir,
242 SegmentArray* segments) {
243 SkSTArray<15, SkPoint, true> quads;
244 GrPathUtils::convertCubicToQuads(pts, SK_Scalar1, true, dir, &quads);
245 int count = quads.count();
246 for (int q = 0; q < count; q += 3) {
247 add_quad_segment(&quads[q], segments);
248 }
249}
250
251static bool get_segments(const SkPath& path,
252 const SkMatrix& m,
253 SegmentArray* segments,
254 SkPoint* fanPt,
255 int* vCount,
256 int* iCount) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000257 SkPath::Iter iter(path, true);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000258 // This renderer over-emphasizes very thin path regions. We use the distance
bsalomon@google.com5cc90d12012-01-17 16:28:34 +0000259 // to the path from the sample to compute coverage. Every pixel intersected
260 // by the path will be hit and the maximum distance is sqrt(2)/2. We don't
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000261 // notice that the sample may be close to a very thin area of the path and
bsalomon@google.com5cc90d12012-01-17 16:28:34 +0000262 // thus should be very light. This is particularly egregious for degenerate
263 // line paths. We detect paths that are very close to a line (zero area) and
264 // draw nothing.
bsalomon@google.com9732f622012-01-31 15:19:21 +0000265 DegenerateTestData degenerateData;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000266 SkPath::Direction dir;
267 // get_direction can fail for some degenerate paths.
268 if (!get_direction(path, m, &dir)) {
269 return false;
270 }
bsalomon@google.com9732f622012-01-31 15:19:21 +0000271
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000272 for (;;) {
273 GrPoint pts[4];
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000274 SkPath::Verb verb = iter.next(pts);
275 switch (verb) {
276 case SkPath::kMove_Verb:
bsalomon@google.com1a38d552012-03-15 14:40:46 +0000277 m.mapPoints(pts, 1);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000278 update_degenerate_test(&degenerateData, pts[0]);
279 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000280 case SkPath::kLine_Verb: {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000281 m.mapPoints(&pts[1], 1);
bsalomon@google.com1a38d552012-03-15 14:40:46 +0000282 update_degenerate_test(&degenerateData, pts[1]);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000283 add_line_to_segment(pts[1], segments);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000284 break;
285 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000286 case SkPath::kQuad_Verb:
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000287 m.mapPoints(pts, 3);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000288 update_degenerate_test(&degenerateData, pts[1]);
289 update_degenerate_test(&degenerateData, pts[2]);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000290 add_quad_segment(pts, segments);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000291 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000292 case SkPath::kCubic_Verb: {
bsalomon@google.com1a38d552012-03-15 14:40:46 +0000293 m.mapPoints(pts, 4);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000294 update_degenerate_test(&degenerateData, pts[1]);
295 update_degenerate_test(&degenerateData, pts[2]);
296 update_degenerate_test(&degenerateData, pts[3]);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000297 add_cubic_segments(pts, dir, segments);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000298 break;
299 };
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000300 case SkPath::kDone_Verb:
bsalomon@google.com9732f622012-01-31 15:19:21 +0000301 if (degenerateData.isDegenerate()) {
302 return false;
303 } else {
bsalomon@google.com278dc692012-02-15 16:52:51 +0000304 compute_vectors(segments, fanPt, dir, vCount, iCount);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000305 return true;
306 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000307 default:
308 break;
309 }
310 }
311}
312
313struct QuadVertex {
314 GrPoint fPos;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000315 GrPoint fUV;
bsalomon@google.com81712882012-11-01 17:12:34 +0000316 SkScalar fD0;
317 SkScalar fD1;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000318};
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000319
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000320struct Draw {
321 Draw() : fVertexCnt(0), fIndexCnt(0) {}
322 int fVertexCnt;
323 int fIndexCnt;
324};
325
326typedef SkTArray<Draw, true> DrawArray;
327
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000328static void create_vertices(const SegmentArray& segments,
329 const SkPoint& fanPt,
330 DrawArray* draws,
331 QuadVertex* verts,
332 uint16_t* idxs) {
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000333 Draw* draw = &draws->push_back();
334 // alias just to make vert/index assignments easier to read.
335 int* v = &draw->fVertexCnt;
336 int* i = &draw->fIndexCnt;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000337
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000338 int count = segments.count();
339 for (int a = 0; a < count; ++a) {
340 const Segment& sega = segments[a];
341 int b = (a + 1) % count;
342 const Segment& segb = segments[b];
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000343
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000344 // Check whether adding the verts for this segment to the current draw would cause index
345 // values to overflow.
346 int vCount = 4;
347 if (Segment::kLine == segb.fType) {
348 vCount += 5;
349 } else {
350 vCount += 6;
351 }
352 if (draw->fVertexCnt + vCount > (1 << 16)) {
353 verts += *v;
354 idxs += *i;
355 draw = &draws->push_back();
356 v = &draw->fVertexCnt;
357 i = &draw->fIndexCnt;
358 }
359
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000360 // FIXME: These tris are inset in the 1 unit arc around the corner
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000361 verts[*v + 0].fPos = sega.endPt();
362 verts[*v + 1].fPos = verts[*v + 0].fPos + sega.endNorm();
363 verts[*v + 2].fPos = verts[*v + 0].fPos + segb.fMid;
364 verts[*v + 3].fPos = verts[*v + 0].fPos + segb.fNorms[0];
365 verts[*v + 0].fUV.set(0,0);
366 verts[*v + 1].fUV.set(0,-SK_Scalar1);
367 verts[*v + 2].fUV.set(0,-SK_Scalar1);
368 verts[*v + 3].fUV.set(0,-SK_Scalar1);
369 verts[*v + 0].fD0 = verts[*v + 0].fD1 = -SK_Scalar1;
370 verts[*v + 1].fD0 = verts[*v + 1].fD1 = -SK_Scalar1;
371 verts[*v + 2].fD0 = verts[*v + 2].fD1 = -SK_Scalar1;
372 verts[*v + 3].fD0 = verts[*v + 3].fD1 = -SK_Scalar1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000373
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000374 idxs[*i + 0] = *v + 0;
375 idxs[*i + 1] = *v + 2;
376 idxs[*i + 2] = *v + 1;
377 idxs[*i + 3] = *v + 0;
378 idxs[*i + 4] = *v + 3;
379 idxs[*i + 5] = *v + 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000380
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000381 *v += 4;
382 *i += 6;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000383
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000384 if (Segment::kLine == segb.fType) {
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000385 verts[*v + 0].fPos = fanPt;
386 verts[*v + 1].fPos = sega.endPt();
387 verts[*v + 2].fPos = segb.fPts[0];
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000388
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000389 verts[*v + 3].fPos = verts[*v + 1].fPos + segb.fNorms[0];
390 verts[*v + 4].fPos = verts[*v + 2].fPos + segb.fNorms[0];
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000391
392 // we draw the line edge as a degenerate quad (u is 0, v is the
393 // signed distance to the edge)
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000394 SkScalar dist = fanPt.distanceToLineBetween(verts[*v + 1].fPos,
395 verts[*v + 2].fPos);
396 verts[*v + 0].fUV.set(0, dist);
397 verts[*v + 1].fUV.set(0, 0);
398 verts[*v + 2].fUV.set(0, 0);
399 verts[*v + 3].fUV.set(0, -SK_Scalar1);
400 verts[*v + 4].fUV.set(0, -SK_Scalar1);
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000401
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000402 verts[*v + 0].fD0 = verts[*v + 0].fD1 = -SK_Scalar1;
403 verts[*v + 1].fD0 = verts[*v + 1].fD1 = -SK_Scalar1;
404 verts[*v + 2].fD0 = verts[*v + 2].fD1 = -SK_Scalar1;
405 verts[*v + 3].fD0 = verts[*v + 3].fD1 = -SK_Scalar1;
406 verts[*v + 4].fD0 = verts[*v + 4].fD1 = -SK_Scalar1;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000407
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000408 idxs[*i + 0] = *v + 0;
409 idxs[*i + 1] = *v + 2;
410 idxs[*i + 2] = *v + 1;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000411
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000412 idxs[*i + 3] = *v + 3;
413 idxs[*i + 4] = *v + 1;
414 idxs[*i + 5] = *v + 2;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000415
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000416 idxs[*i + 6] = *v + 4;
417 idxs[*i + 7] = *v + 3;
418 idxs[*i + 8] = *v + 2;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000419
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000420 *v += 5;
421 *i += 9;
bsalomon@google.com06809612012-01-21 15:03:39 +0000422 } else {
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000423 GrPoint qpts[] = {sega.endPt(), segb.fPts[0], segb.fPts[1]};
bsalomon@google.com495e2102012-01-21 14:48:36 +0000424
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000425 GrVec midVec = segb.fNorms[0] + segb.fNorms[1];
426 midVec.normalize();
bsalomon@google.com06809612012-01-21 15:03:39 +0000427
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000428 verts[*v + 0].fPos = fanPt;
429 verts[*v + 1].fPos = qpts[0];
430 verts[*v + 2].fPos = qpts[2];
431 verts[*v + 3].fPos = qpts[0] + segb.fNorms[0];
432 verts[*v + 4].fPos = qpts[2] + segb.fNorms[1];
433 verts[*v + 5].fPos = qpts[1] + midVec;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000434
bsalomon@google.com81712882012-11-01 17:12:34 +0000435 SkScalar c = segb.fNorms[0].dot(qpts[0]);
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000436 verts[*v + 0].fD0 = -segb.fNorms[0].dot(fanPt) + c;
437 verts[*v + 1].fD0 = 0.f;
438 verts[*v + 2].fD0 = -segb.fNorms[0].dot(qpts[2]) + c;
439 verts[*v + 3].fD0 = -SK_ScalarMax/100;
440 verts[*v + 4].fD0 = -SK_ScalarMax/100;
441 verts[*v + 5].fD0 = -SK_ScalarMax/100;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000442
443 c = segb.fNorms[1].dot(qpts[2]);
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000444 verts[*v + 0].fD1 = -segb.fNorms[1].dot(fanPt) + c;
445 verts[*v + 1].fD1 = -segb.fNorms[1].dot(qpts[0]) + c;
446 verts[*v + 2].fD1 = 0.f;
447 verts[*v + 3].fD1 = -SK_ScalarMax/100;
448 verts[*v + 4].fD1 = -SK_ScalarMax/100;
449 verts[*v + 5].fD1 = -SK_ScalarMax/100;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000450
bsalomon@google.com19713172012-03-15 13:51:08 +0000451 GrPathUtils::QuadUVMatrix toUV(qpts);
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000452 toUV.apply<6, sizeof(QuadVertex), sizeof(GrPoint)>(verts + *v);
bsalomon@google.com06809612012-01-21 15:03:39 +0000453
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000454 idxs[*i + 0] = *v + 3;
455 idxs[*i + 1] = *v + 1;
456 idxs[*i + 2] = *v + 2;
457 idxs[*i + 3] = *v + 4;
458 idxs[*i + 4] = *v + 3;
459 idxs[*i + 5] = *v + 2;
bsalomon@google.com06809612012-01-21 15:03:39 +0000460
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000461 idxs[*i + 6] = *v + 5;
462 idxs[*i + 7] = *v + 3;
463 idxs[*i + 8] = *v + 4;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000464
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000465 idxs[*i + 9] = *v + 0;
466 idxs[*i + 10] = *v + 2;
467 idxs[*i + 11] = *v + 1;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000468
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000469 *v += 6;
470 *i += 12;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000471 }
472 }
473}
474
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000475///////////////////////////////////////////////////////////////////////////////
476
477/*
478 * Quadratic specified by 0=u^2-v canonical coords. u and v are the first
479 * two components of the vertex attribute. Coverage is based on signed
480 * distance with negative being inside, positive outside. The edge is specified in
481 * window space (y-down). If either the third or fourth component of the interpolated
482 * vertex coord is > 0 then the pixel is considered outside the edge. This is used to
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000483 * attempt to trim to a portion of the infinite quad.
484 * Requires shader derivative instruction support.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000485 */
486
487class QuadEdgeEffect : public GrEffect {
488public:
489
490 static GrEffectRef* Create() {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000491 GR_CREATE_STATIC_EFFECT(gQuadEdgeEffect, QuadEdgeEffect, ());
492 gQuadEdgeEffect->ref();
493 return gQuadEdgeEffect;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000494 }
495
496 virtual ~QuadEdgeEffect() {}
497
498 static const char* Name() { return "QuadEdge"; }
499
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000500 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000501 uint32_t* validFlags) const SK_OVERRIDE {
502 *validFlags = 0;
503 }
504
505 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
506 return GrTBackendEffectFactory<QuadEdgeEffect>::getInstance();
507 }
508
509 class GLEffect : public GrGLEffect {
510 public:
511 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
512 : INHERITED (factory) {}
513
514 virtual void emitCode(GrGLShaderBuilder* builder,
515 const GrDrawEffect& drawEffect,
516 EffectKey key,
517 const char* outputColor,
518 const char* inputColor,
519 const TextureSamplerArray& samplers) SK_OVERRIDE {
520 const char *vsName, *fsName;
521 const SkString* attrName =
522 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
523 builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n");
524
525 SkAssertResult(builder->enableFeature(
526 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
527 builder->addVarying(kVec4f_GrSLType, "QuadEdge", &vsName, &fsName);
528
529 // keep the derivative instructions outside the conditional
530 builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
531 builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
532 builder->fsCodeAppendf("\t\tif (%s.z > 0.0 && %s.w > 0.0) {\n", fsName, fsName);
533 // today we know z and w are in device space. We could use derivatives
534 builder->fsCodeAppendf("\t\t\tedgeAlpha = min(min(%s.z, %s.w) + 0.5, 1.0);\n", fsName,
535 fsName);
536 builder->fsCodeAppendf ("\t\t} else {\n");
537 builder->fsCodeAppendf("\t\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
538 "\t\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
539 fsName, fsName);
540 builder->fsCodeAppendf("\t\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName,
541 fsName);
542 builder->fsCodeAppendf("\t\t\tedgeAlpha = "
543 "clamp(0.5 - edgeAlpha / length(gF), 0.0, 1.0);\n\t\t}\n");
544
545 SkString modulate;
bsalomon@google.com018f1792013-04-18 19:36:09 +0000546 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000547 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
548
549 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str());
550 }
551
552 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
553 return 0x0;
554 }
555
556 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
557
558 private:
559 typedef GrGLEffect INHERITED;
560 };
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000561
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000562private:
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000563 QuadEdgeEffect() {
564 this->addVertexAttrib(kVec4f_GrSLType);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000565 }
566
567 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
568 return true;
569 }
570
571 GR_DECLARE_EFFECT_TEST;
572
573 typedef GrEffect INHERITED;
574};
575
576GR_DEFINE_EFFECT_TEST(QuadEdgeEffect);
577
578GrEffectRef* QuadEdgeEffect::TestCreate(SkMWCRandom* random,
579 GrContext*,
580 const GrDrawTargetCaps& caps,
581 GrTexture*[]) {
582 // Doesn't work without derivative instructions.
583 return caps.shaderDerivativeSupport() ? QuadEdgeEffect::Create() : NULL;
584}
585
586///////////////////////////////////////////////////////////////////////////////
587
robertphillips@google.comfa662942012-05-17 12:20:22 +0000588bool GrAAConvexPathRenderer::canDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000589 const SkStrokeRec& stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000590 const GrDrawTarget* target,
591 bool antiAlias) const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000592 return (target->caps()->shaderDerivativeSupport() && antiAlias &&
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000593 stroke.isFillStyle() && !path.isInverseFillType() && path.isConvex());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000594}
595
robertphillips@google.com42903302013-04-20 12:26:07 +0000596namespace {
597
598// position + edge
599extern const GrVertexAttrib gPathAttribs[] = {
600 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
601 {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}
602};
603
604};
605
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000606bool GrAAConvexPathRenderer::onDrawPath(const SkPath& origPath,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000607 const SkStrokeRec&,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000608 GrDrawTarget* target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000609 bool antiAlias) {
610
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000611 const SkPath* path = &origPath;
612 if (path->isEmpty()) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000613 return true;
614 }
bsalomon@google.com4647f902013-03-26 14:45:27 +0000615
bsalomon@google.com137f1342013-05-29 21:27:53 +0000616 SkMatrix viewMatrix = target->getDrawState().getViewMatrix();
617 GrDrawTarget::AutoStateRestore asr;
618 if (!asr.setIdentity(target, GrDrawTarget::kPreserve_ASRInit)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000619 return false;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000620 }
bsalomon@google.com137f1342013-05-29 21:27:53 +0000621 GrDrawState* drawState = target->drawState();
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000622
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000623 // We use the fact that SkPath::transform path does subdivision based on
624 // perspective. Otherwise, we apply the view matrix when copying to the
625 // segment representation.
626 SkPath tmpPath;
bsalomon@google.com137f1342013-05-29 21:27:53 +0000627 if (viewMatrix.hasPerspective()) {
628 origPath.transform(viewMatrix, &tmpPath);
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000629 path = &tmpPath;
bsalomon@google.com137f1342013-05-29 21:27:53 +0000630 viewMatrix = SkMatrix::I();
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000631 }
632
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000633 QuadVertex *verts;
634 uint16_t* idxs;
635
bsalomon@google.com06809612012-01-21 15:03:39 +0000636 int vCount;
637 int iCount;
bsalomon@google.com68a5b262012-03-05 18:24:07 +0000638 enum {
639 kPreallocSegmentCnt = 512 / sizeof(Segment),
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000640 kPreallocDrawCnt = 4,
bsalomon@google.com68a5b262012-03-05 18:24:07 +0000641 };
642 SkSTArray<kPreallocSegmentCnt, Segment, true> segments;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000643 SkPoint fanPt;
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000644
bsalomon@google.com137f1342013-05-29 21:27:53 +0000645 if (!get_segments(*path, viewMatrix, &segments, &fanPt, &vCount, &iCount)) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000646 return false;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000647 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000648
robertphillips@google.com42903302013-04-20 12:26:07 +0000649 drawState->setVertexAttribs<gPathAttribs>(SK_ARRAY_COUNT(gPathAttribs));
bsalomon@google.com4647f902013-03-26 14:45:27 +0000650
bsalomon@google.com4647f902013-03-26 14:45:27 +0000651 static const int kEdgeAttrIndex = 1;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000652 GrEffectRef* quadEffect = QuadEdgeEffect::Create();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000653 drawState->addCoverageEffect(quadEffect, kEdgeAttrIndex)->unref();
bsalomon@google.com4647f902013-03-26 14:45:27 +0000654
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000655 GrDrawTarget::AutoReleaseGeometry arg(target, vCount, iCount);
bsalomon@google.comb3729422012-03-07 19:13:28 +0000656 if (!arg.succeeded()) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000657 return false;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000658 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000659 SkASSERT(sizeof(QuadVertex) == drawState->getVertexSize());
bsalomon@google.comb3729422012-03-07 19:13:28 +0000660 verts = reinterpret_cast<QuadVertex*>(arg.vertices());
661 idxs = reinterpret_cast<uint16_t*>(arg.indices());
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000662
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000663 SkSTArray<kPreallocDrawCnt, Draw, true> draws;
664 create_vertices(segments, fanPt, &draws, verts, idxs);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000665
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000666 // This is valid because all the computed verts are within 1 pixel of the path control points.
667 SkRect devBounds;
bsalomon@google.com137f1342013-05-29 21:27:53 +0000668 devBounds = path->getBounds();
669 viewMatrix.mapRect(&devBounds);
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000670 devBounds.outset(SK_Scalar1, SK_Scalar1);
671
672 // Check devBounds
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000673#ifdef SK_DEBUG
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000674 SkRect tolDevBounds = devBounds;
675 tolDevBounds.outset(SK_Scalar1 / 10000, SK_Scalar1 / 10000);
676 SkRect actualBounds;
677 actualBounds.set(verts[0].fPos, verts[1].fPos);
678 for (int i = 2; i < vCount; ++i) {
679 actualBounds.growToInclude(verts[i].fPos.fX, verts[i].fPos.fY);
680 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000681 SkASSERT(tolDevBounds.contains(actualBounds));
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000682#endif
683
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000684 int vOffset = 0;
685 for (int i = 0; i < draws.count(); ++i) {
686 const Draw& draw = draws[i];
687 target->drawIndexed(kTriangles_GrPrimitiveType,
688 vOffset, // start vertex
689 0, // start index
690 draw.fVertexCnt,
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000691 draw.fIndexCnt,
692 &devBounds);
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000693 vOffset += draw.fVertexCnt;
694 }
bsalomon@google.coma8347462012-10-08 18:59:39 +0000695
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000696 return true;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000697}