blob: e50e6b406eb0d276c6ad0cdb89ff0d899326bea7 [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.org106655e2013-09-03 21:28:55 +0000221static inline void add_line_to_segment(const SkPoint& pt,
222 SegmentArray* segments,
223 SkRect* devBounds) {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000224 segments->push_back();
225 segments->back().fType = Segment::kLine;
226 segments->back().fPts[0] = pt;
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000227 devBounds->growToInclude(pt.fX, pt.fY);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000228}
229
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000230static inline bool contains_inclusive(const SkRect& rect, const SkPoint& p) {
231 return p.fX >= rect.fLeft && p.fX <= rect.fRight && p.fY >= rect.fTop && p.fY <= rect.fBottom;
232}
233static inline void add_quad_segment(const SkPoint pts[3],
234 SegmentArray* segments,
235 SkRect* devBounds) {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000236 if (pts[0].distanceToSqd(pts[1]) < kCloseSqd || pts[1].distanceToSqd(pts[2]) < kCloseSqd) {
237 if (pts[0] != pts[2]) {
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000238 add_line_to_segment(pts[2], segments, devBounds);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000239 }
240 } else {
241 segments->push_back();
242 segments->back().fType = Segment::kQuad;
243 segments->back().fPts[0] = pts[1];
244 segments->back().fPts[1] = pts[2];
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000245 SkASSERT(contains_inclusive(*devBounds, pts[0]));
246 devBounds->growToInclude(pts + 1, 2);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000247 }
248}
249
250static inline void add_cubic_segments(const SkPoint pts[4],
251 SkPath::Direction dir,
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000252 SegmentArray* segments,
253 SkRect* devBounds) {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000254 SkSTArray<15, SkPoint, true> quads;
255 GrPathUtils::convertCubicToQuads(pts, SK_Scalar1, true, dir, &quads);
256 int count = quads.count();
257 for (int q = 0; q < count; q += 3) {
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000258 add_quad_segment(&quads[q], segments, devBounds);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000259 }
260}
261
262static bool get_segments(const SkPath& path,
263 const SkMatrix& m,
264 SegmentArray* segments,
265 SkPoint* fanPt,
266 int* vCount,
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000267 int* iCount,
268 SkRect* devBounds) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000269 SkPath::Iter iter(path, true);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000270 // This renderer over-emphasizes very thin path regions. We use the distance
bsalomon@google.com5cc90d12012-01-17 16:28:34 +0000271 // to the path from the sample to compute coverage. Every pixel intersected
272 // 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 +0000273 // notice that the sample may be close to a very thin area of the path and
bsalomon@google.com5cc90d12012-01-17 16:28:34 +0000274 // thus should be very light. This is particularly egregious for degenerate
275 // line paths. We detect paths that are very close to a line (zero area) and
276 // draw nothing.
bsalomon@google.com9732f622012-01-31 15:19:21 +0000277 DegenerateTestData degenerateData;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000278 SkPath::Direction dir;
279 // get_direction can fail for some degenerate paths.
280 if (!get_direction(path, m, &dir)) {
281 return false;
282 }
bsalomon@google.com9732f622012-01-31 15:19:21 +0000283
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000284 for (;;) {
285 GrPoint pts[4];
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000286 SkPath::Verb verb = iter.next(pts);
287 switch (verb) {
288 case SkPath::kMove_Verb:
bsalomon@google.com1a38d552012-03-15 14:40:46 +0000289 m.mapPoints(pts, 1);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000290 update_degenerate_test(&degenerateData, pts[0]);
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000291 devBounds->set(pts->fX, pts->fY, pts->fX, pts->fY);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000292 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000293 case SkPath::kLine_Verb: {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000294 m.mapPoints(&pts[1], 1);
bsalomon@google.com1a38d552012-03-15 14:40:46 +0000295 update_degenerate_test(&degenerateData, pts[1]);
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000296 add_line_to_segment(pts[1], segments, devBounds);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000297 break;
298 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000299 case SkPath::kQuad_Verb:
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000300 m.mapPoints(pts, 3);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000301 update_degenerate_test(&degenerateData, pts[1]);
302 update_degenerate_test(&degenerateData, pts[2]);
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000303 add_quad_segment(pts, segments, devBounds);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000304 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000305 case SkPath::kCubic_Verb: {
bsalomon@google.com1a38d552012-03-15 14:40:46 +0000306 m.mapPoints(pts, 4);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000307 update_degenerate_test(&degenerateData, pts[1]);
308 update_degenerate_test(&degenerateData, pts[2]);
309 update_degenerate_test(&degenerateData, pts[3]);
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000310 add_cubic_segments(pts, dir, segments, devBounds);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000311 break;
312 };
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000313 case SkPath::kDone_Verb:
bsalomon@google.com9732f622012-01-31 15:19:21 +0000314 if (degenerateData.isDegenerate()) {
315 return false;
316 } else {
bsalomon@google.com278dc692012-02-15 16:52:51 +0000317 compute_vectors(segments, fanPt, dir, vCount, iCount);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000318 return true;
319 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000320 default:
321 break;
322 }
323 }
324}
325
326struct QuadVertex {
327 GrPoint fPos;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000328 GrPoint fUV;
bsalomon@google.com81712882012-11-01 17:12:34 +0000329 SkScalar fD0;
330 SkScalar fD1;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000331};
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000332
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000333struct Draw {
334 Draw() : fVertexCnt(0), fIndexCnt(0) {}
335 int fVertexCnt;
336 int fIndexCnt;
337};
338
339typedef SkTArray<Draw, true> DrawArray;
340
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000341static void create_vertices(const SegmentArray& segments,
342 const SkPoint& fanPt,
343 DrawArray* draws,
344 QuadVertex* verts,
345 uint16_t* idxs) {
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000346 Draw* draw = &draws->push_back();
347 // alias just to make vert/index assignments easier to read.
348 int* v = &draw->fVertexCnt;
349 int* i = &draw->fIndexCnt;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000350
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000351 int count = segments.count();
352 for (int a = 0; a < count; ++a) {
353 const Segment& sega = segments[a];
354 int b = (a + 1) % count;
355 const Segment& segb = segments[b];
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000356
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000357 // Check whether adding the verts for this segment to the current draw would cause index
358 // values to overflow.
359 int vCount = 4;
360 if (Segment::kLine == segb.fType) {
361 vCount += 5;
362 } else {
363 vCount += 6;
364 }
365 if (draw->fVertexCnt + vCount > (1 << 16)) {
366 verts += *v;
367 idxs += *i;
368 draw = &draws->push_back();
369 v = &draw->fVertexCnt;
370 i = &draw->fIndexCnt;
371 }
372
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000373 // FIXME: These tris are inset in the 1 unit arc around the corner
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000374 verts[*v + 0].fPos = sega.endPt();
375 verts[*v + 1].fPos = verts[*v + 0].fPos + sega.endNorm();
376 verts[*v + 2].fPos = verts[*v + 0].fPos + segb.fMid;
377 verts[*v + 3].fPos = verts[*v + 0].fPos + segb.fNorms[0];
378 verts[*v + 0].fUV.set(0,0);
379 verts[*v + 1].fUV.set(0,-SK_Scalar1);
380 verts[*v + 2].fUV.set(0,-SK_Scalar1);
381 verts[*v + 3].fUV.set(0,-SK_Scalar1);
382 verts[*v + 0].fD0 = verts[*v + 0].fD1 = -SK_Scalar1;
383 verts[*v + 1].fD0 = verts[*v + 1].fD1 = -SK_Scalar1;
384 verts[*v + 2].fD0 = verts[*v + 2].fD1 = -SK_Scalar1;
385 verts[*v + 3].fD0 = verts[*v + 3].fD1 = -SK_Scalar1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000386
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000387 idxs[*i + 0] = *v + 0;
388 idxs[*i + 1] = *v + 2;
389 idxs[*i + 2] = *v + 1;
390 idxs[*i + 3] = *v + 0;
391 idxs[*i + 4] = *v + 3;
392 idxs[*i + 5] = *v + 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000393
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000394 *v += 4;
395 *i += 6;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000396
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000397 if (Segment::kLine == segb.fType) {
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000398 verts[*v + 0].fPos = fanPt;
399 verts[*v + 1].fPos = sega.endPt();
400 verts[*v + 2].fPos = segb.fPts[0];
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000401
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000402 verts[*v + 3].fPos = verts[*v + 1].fPos + segb.fNorms[0];
403 verts[*v + 4].fPos = verts[*v + 2].fPos + segb.fNorms[0];
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000404
405 // we draw the line edge as a degenerate quad (u is 0, v is the
406 // signed distance to the edge)
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000407 SkScalar dist = fanPt.distanceToLineBetween(verts[*v + 1].fPos,
408 verts[*v + 2].fPos);
409 verts[*v + 0].fUV.set(0, dist);
410 verts[*v + 1].fUV.set(0, 0);
411 verts[*v + 2].fUV.set(0, 0);
412 verts[*v + 3].fUV.set(0, -SK_Scalar1);
413 verts[*v + 4].fUV.set(0, -SK_Scalar1);
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000414
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000415 verts[*v + 0].fD0 = verts[*v + 0].fD1 = -SK_Scalar1;
416 verts[*v + 1].fD0 = verts[*v + 1].fD1 = -SK_Scalar1;
417 verts[*v + 2].fD0 = verts[*v + 2].fD1 = -SK_Scalar1;
418 verts[*v + 3].fD0 = verts[*v + 3].fD1 = -SK_Scalar1;
419 verts[*v + 4].fD0 = verts[*v + 4].fD1 = -SK_Scalar1;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000420
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000421 idxs[*i + 0] = *v + 0;
422 idxs[*i + 1] = *v + 2;
423 idxs[*i + 2] = *v + 1;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000424
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000425 idxs[*i + 3] = *v + 3;
426 idxs[*i + 4] = *v + 1;
427 idxs[*i + 5] = *v + 2;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000428
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000429 idxs[*i + 6] = *v + 4;
430 idxs[*i + 7] = *v + 3;
431 idxs[*i + 8] = *v + 2;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000432
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000433 *v += 5;
434 *i += 9;
bsalomon@google.com06809612012-01-21 15:03:39 +0000435 } else {
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000436 GrPoint qpts[] = {sega.endPt(), segb.fPts[0], segb.fPts[1]};
bsalomon@google.com495e2102012-01-21 14:48:36 +0000437
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000438 GrVec midVec = segb.fNorms[0] + segb.fNorms[1];
439 midVec.normalize();
bsalomon@google.com06809612012-01-21 15:03:39 +0000440
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000441 verts[*v + 0].fPos = fanPt;
442 verts[*v + 1].fPos = qpts[0];
443 verts[*v + 2].fPos = qpts[2];
444 verts[*v + 3].fPos = qpts[0] + segb.fNorms[0];
445 verts[*v + 4].fPos = qpts[2] + segb.fNorms[1];
446 verts[*v + 5].fPos = qpts[1] + midVec;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000447
bsalomon@google.com81712882012-11-01 17:12:34 +0000448 SkScalar c = segb.fNorms[0].dot(qpts[0]);
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000449 verts[*v + 0].fD0 = -segb.fNorms[0].dot(fanPt) + c;
450 verts[*v + 1].fD0 = 0.f;
451 verts[*v + 2].fD0 = -segb.fNorms[0].dot(qpts[2]) + c;
452 verts[*v + 3].fD0 = -SK_ScalarMax/100;
453 verts[*v + 4].fD0 = -SK_ScalarMax/100;
454 verts[*v + 5].fD0 = -SK_ScalarMax/100;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000455
456 c = segb.fNorms[1].dot(qpts[2]);
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000457 verts[*v + 0].fD1 = -segb.fNorms[1].dot(fanPt) + c;
458 verts[*v + 1].fD1 = -segb.fNorms[1].dot(qpts[0]) + c;
459 verts[*v + 2].fD1 = 0.f;
460 verts[*v + 3].fD1 = -SK_ScalarMax/100;
461 verts[*v + 4].fD1 = -SK_ScalarMax/100;
462 verts[*v + 5].fD1 = -SK_ScalarMax/100;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000463
bsalomon@google.com19713172012-03-15 13:51:08 +0000464 GrPathUtils::QuadUVMatrix toUV(qpts);
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000465 toUV.apply<6, sizeof(QuadVertex), sizeof(GrPoint)>(verts + *v);
bsalomon@google.com06809612012-01-21 15:03:39 +0000466
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000467 idxs[*i + 0] = *v + 3;
468 idxs[*i + 1] = *v + 1;
469 idxs[*i + 2] = *v + 2;
470 idxs[*i + 3] = *v + 4;
471 idxs[*i + 4] = *v + 3;
472 idxs[*i + 5] = *v + 2;
bsalomon@google.com06809612012-01-21 15:03:39 +0000473
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000474 idxs[*i + 6] = *v + 5;
475 idxs[*i + 7] = *v + 3;
476 idxs[*i + 8] = *v + 4;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000477
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000478 idxs[*i + 9] = *v + 0;
479 idxs[*i + 10] = *v + 2;
480 idxs[*i + 11] = *v + 1;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000481
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000482 *v += 6;
483 *i += 12;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000484 }
485 }
486}
487
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000488///////////////////////////////////////////////////////////////////////////////
489
490/*
491 * Quadratic specified by 0=u^2-v canonical coords. u and v are the first
492 * two components of the vertex attribute. Coverage is based on signed
493 * distance with negative being inside, positive outside. The edge is specified in
494 * window space (y-down). If either the third or fourth component of the interpolated
495 * 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 +0000496 * attempt to trim to a portion of the infinite quad.
497 * Requires shader derivative instruction support.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000498 */
499
500class QuadEdgeEffect : public GrEffect {
501public:
502
503 static GrEffectRef* Create() {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000504 GR_CREATE_STATIC_EFFECT(gQuadEdgeEffect, QuadEdgeEffect, ());
505 gQuadEdgeEffect->ref();
506 return gQuadEdgeEffect;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000507 }
508
509 virtual ~QuadEdgeEffect() {}
510
511 static const char* Name() { return "QuadEdge"; }
512
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000513 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000514 uint32_t* validFlags) const SK_OVERRIDE {
515 *validFlags = 0;
516 }
517
518 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
519 return GrTBackendEffectFactory<QuadEdgeEffect>::getInstance();
520 }
521
522 class GLEffect : public GrGLEffect {
523 public:
524 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
525 : INHERITED (factory) {}
526
527 virtual void emitCode(GrGLShaderBuilder* builder,
528 const GrDrawEffect& drawEffect,
529 EffectKey key,
530 const char* outputColor,
531 const char* inputColor,
532 const TextureSamplerArray& samplers) SK_OVERRIDE {
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000533 GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertexBuilder();
534 SkASSERT(NULL != vertexBuilder);
535
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000536 const char *vsName, *fsName;
537 const SkString* attrName =
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000538 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000539 builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n");
540
541 SkAssertResult(builder->enableFeature(
542 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000543 vertexBuilder->addVarying(kVec4f_GrSLType, "QuadEdge", &vsName, &fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000544
545 // keep the derivative instructions outside the conditional
546 builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
547 builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
548 builder->fsCodeAppendf("\t\tif (%s.z > 0.0 && %s.w > 0.0) {\n", fsName, fsName);
549 // today we know z and w are in device space. We could use derivatives
550 builder->fsCodeAppendf("\t\t\tedgeAlpha = min(min(%s.z, %s.w) + 0.5, 1.0);\n", fsName,
551 fsName);
552 builder->fsCodeAppendf ("\t\t} else {\n");
553 builder->fsCodeAppendf("\t\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
554 "\t\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
555 fsName, fsName);
556 builder->fsCodeAppendf("\t\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName,
557 fsName);
558 builder->fsCodeAppendf("\t\t\tedgeAlpha = "
559 "clamp(0.5 - edgeAlpha / length(gF), 0.0, 1.0);\n\t\t}\n");
560
561 SkString modulate;
bsalomon@google.com018f1792013-04-18 19:36:09 +0000562 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000563 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
564
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000565 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000566 }
567
568 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
569 return 0x0;
570 }
571
572 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
573
574 private:
575 typedef GrGLEffect INHERITED;
576 };
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000577
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000578private:
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000579 QuadEdgeEffect() {
580 this->addVertexAttrib(kVec4f_GrSLType);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000581 }
582
583 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
584 return true;
585 }
586
587 GR_DECLARE_EFFECT_TEST;
588
589 typedef GrEffect INHERITED;
590};
591
592GR_DEFINE_EFFECT_TEST(QuadEdgeEffect);
593
594GrEffectRef* QuadEdgeEffect::TestCreate(SkMWCRandom* random,
595 GrContext*,
596 const GrDrawTargetCaps& caps,
597 GrTexture*[]) {
598 // Doesn't work without derivative instructions.
599 return caps.shaderDerivativeSupport() ? QuadEdgeEffect::Create() : NULL;
600}
601
602///////////////////////////////////////////////////////////////////////////////
603
robertphillips@google.comfa662942012-05-17 12:20:22 +0000604bool GrAAConvexPathRenderer::canDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000605 const SkStrokeRec& stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000606 const GrDrawTarget* target,
607 bool antiAlias) const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000608 return (target->caps()->shaderDerivativeSupport() && antiAlias &&
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000609 stroke.isFillStyle() && !path.isInverseFillType() && path.isConvex());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000610}
611
robertphillips@google.com42903302013-04-20 12:26:07 +0000612namespace {
613
614// position + edge
615extern const GrVertexAttrib gPathAttribs[] = {
616 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
617 {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}
618};
619
620};
621
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000622bool GrAAConvexPathRenderer::onDrawPath(const SkPath& origPath,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000623 const SkStrokeRec&,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000624 GrDrawTarget* target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000625 bool antiAlias) {
626
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000627 const SkPath* path = &origPath;
628 if (path->isEmpty()) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000629 return true;
630 }
bsalomon@google.com4647f902013-03-26 14:45:27 +0000631
bsalomon@google.com137f1342013-05-29 21:27:53 +0000632 SkMatrix viewMatrix = target->getDrawState().getViewMatrix();
633 GrDrawTarget::AutoStateRestore asr;
634 if (!asr.setIdentity(target, GrDrawTarget::kPreserve_ASRInit)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000635 return false;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000636 }
bsalomon@google.com137f1342013-05-29 21:27:53 +0000637 GrDrawState* drawState = target->drawState();
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000638
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000639 // We use the fact that SkPath::transform path does subdivision based on
640 // perspective. Otherwise, we apply the view matrix when copying to the
641 // segment representation.
642 SkPath tmpPath;
bsalomon@google.com137f1342013-05-29 21:27:53 +0000643 if (viewMatrix.hasPerspective()) {
644 origPath.transform(viewMatrix, &tmpPath);
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000645 path = &tmpPath;
bsalomon@google.com137f1342013-05-29 21:27:53 +0000646 viewMatrix = SkMatrix::I();
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000647 }
648
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000649 QuadVertex *verts;
650 uint16_t* idxs;
651
bsalomon@google.com06809612012-01-21 15:03:39 +0000652 int vCount;
653 int iCount;
bsalomon@google.com68a5b262012-03-05 18:24:07 +0000654 enum {
655 kPreallocSegmentCnt = 512 / sizeof(Segment),
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000656 kPreallocDrawCnt = 4,
bsalomon@google.com68a5b262012-03-05 18:24:07 +0000657 };
658 SkSTArray<kPreallocSegmentCnt, Segment, true> segments;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000659 SkPoint fanPt;
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000660
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000661 // We can't simply use the path bounds because we may degenerate cubics to quads which produces
662 // new control points outside the original convex hull.
663 SkRect devBounds;
664 if (!get_segments(*path, viewMatrix, &segments, &fanPt, &vCount, &iCount, &devBounds)) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000665 return false;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000666 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000667
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000668 // Our computed verts should all be within one pixel of the segment control points.
669 devBounds.outset(SK_Scalar1, SK_Scalar1);
670
robertphillips@google.com42903302013-04-20 12:26:07 +0000671 drawState->setVertexAttribs<gPathAttribs>(SK_ARRAY_COUNT(gPathAttribs));
bsalomon@google.com4647f902013-03-26 14:45:27 +0000672
bsalomon@google.com4647f902013-03-26 14:45:27 +0000673 static const int kEdgeAttrIndex = 1;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000674 GrEffectRef* quadEffect = QuadEdgeEffect::Create();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000675 drawState->addCoverageEffect(quadEffect, kEdgeAttrIndex)->unref();
bsalomon@google.com4647f902013-03-26 14:45:27 +0000676
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000677 GrDrawTarget::AutoReleaseGeometry arg(target, vCount, iCount);
bsalomon@google.comb3729422012-03-07 19:13:28 +0000678 if (!arg.succeeded()) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000679 return false;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000680 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000681 SkASSERT(sizeof(QuadVertex) == drawState->getVertexSize());
bsalomon@google.comb3729422012-03-07 19:13:28 +0000682 verts = reinterpret_cast<QuadVertex*>(arg.vertices());
683 idxs = reinterpret_cast<uint16_t*>(arg.indices());
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000684
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000685 SkSTArray<kPreallocDrawCnt, Draw, true> draws;
686 create_vertices(segments, fanPt, &draws, verts, idxs);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000687
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000688 // Check devBounds
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000689#ifdef SK_DEBUG
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000690 SkRect tolDevBounds = devBounds;
691 tolDevBounds.outset(SK_Scalar1 / 10000, SK_Scalar1 / 10000);
692 SkRect actualBounds;
693 actualBounds.set(verts[0].fPos, verts[1].fPos);
694 for (int i = 2; i < vCount; ++i) {
695 actualBounds.growToInclude(verts[i].fPos.fX, verts[i].fPos.fY);
696 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000697 SkASSERT(tolDevBounds.contains(actualBounds));
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000698#endif
699
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000700 int vOffset = 0;
701 for (int i = 0; i < draws.count(); ++i) {
702 const Draw& draw = draws[i];
703 target->drawIndexed(kTriangles_GrPrimitiveType,
704 vOffset, // start vertex
705 0, // start index
706 draw.fVertexCnt,
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000707 draw.fIndexCnt,
708 &devBounds);
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000709 vOffset += draw.fVertexCnt;
710 }
bsalomon@google.coma8347462012-10-08 18:59:39 +0000711
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000712 return true;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000713}