blob: a8af5599910a3689f3af0e574225076ae5e9a712 [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"
commit-bot@chromium.org261dc562013-10-04 15:42:56 +000023#include "gl/GrGLVertexEffect.h"
bsalomon@google.com4647f902013-03-26 14:45:27 +000024
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000025#include "effects/GrVertexEffect.h"
26
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000027GrAAConvexPathRenderer::GrAAConvexPathRenderer() {
28}
29
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000030struct Segment {
31 enum {
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000032 // These enum values are assumed in member functions below.
33 kLine = 0,
34 kQuad = 1,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000035 } fType;
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000036
bsalomon@google.com9aed1142012-01-30 14:28:39 +000037 // line uses one pt, quad uses 2 pts
38 GrPoint fPts[2];
39 // normal to edge ending at each pt
40 GrVec fNorms[2];
41 // is the corner where the previous segment meets this segment
42 // sharp. If so, fMid is a normalized bisector facing outward.
43 GrVec fMid;
44
45 int countPoints() {
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000046 GR_STATIC_ASSERT(0 == kLine && 1 == kQuad);
47 return fType + 1;
bsalomon@google.com9aed1142012-01-30 14:28:39 +000048 }
49 const SkPoint& endPt() const {
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000050 GR_STATIC_ASSERT(0 == kLine && 1 == kQuad);
51 return fPts[fType];
bsalomon@google.com9aed1142012-01-30 14:28:39 +000052 };
53 const SkPoint& endNorm() const {
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000054 GR_STATIC_ASSERT(0 == kLine && 1 == kQuad);
55 return fNorms[fType];
bsalomon@google.com9aed1142012-01-30 14:28:39 +000056 };
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000057};
58
59typedef SkTArray<Segment, true> SegmentArray;
60
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +000061static void center_of_mass(const SegmentArray& segments, SkPoint* c) {
bsalomon@google.com81712882012-11-01 17:12:34 +000062 SkScalar area = 0;
vandebo@chromium.org6390c722012-03-28 21:03:22 +000063 SkPoint center = {0, 0};
bsalomon@google.com9aed1142012-01-30 14:28:39 +000064 int count = segments.count();
vandebo@chromium.org6390c722012-03-28 21:03:22 +000065 SkPoint p0 = {0, 0};
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000066 if (count > 2) {
67 // We translate the polygon so that the first point is at the origin.
68 // This avoids some precision issues with small area polygons far away
69 // from the origin.
70 p0 = segments[0].endPt();
71 SkPoint pi;
72 SkPoint pj;
bsalomon@google.coma51ab842012-07-10 19:53:34 +000073 // the first and last iteration of the below loop would compute
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000074 // zeros since the starting / ending point is (0,0). So instead we start
75 // at i=1 and make the last iteration i=count-2.
76 pj = segments[1].endPt() - p0;
77 for (int i = 1; i < count - 1; ++i) {
78 pi = pj;
79 const SkPoint pj = segments[i + 1].endPt() - p0;
80
bsalomon@google.com81712882012-11-01 17:12:34 +000081 SkScalar t = SkScalarMul(pi.fX, pj.fY) - SkScalarMul(pj.fX, pi.fY);
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000082 area += t;
83 center.fX += (pi.fX + pj.fX) * t;
84 center.fY += (pi.fY + pj.fY) * t;
85
86 }
bsalomon@google.com9aed1142012-01-30 14:28:39 +000087 }
bsalomon@google.com278dc692012-02-15 16:52:51 +000088 // If the poly has no area then we instead return the average of
89 // its points.
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000090 if (SkScalarNearlyZero(area)) {
bsalomon@google.com278dc692012-02-15 16:52:51 +000091 SkPoint avg;
92 avg.set(0, 0);
93 for (int i = 0; i < count; ++i) {
94 const SkPoint& pt = segments[i].endPt();
95 avg.fX += pt.fX;
96 avg.fY += pt.fY;
97 }
98 SkScalar denom = SK_Scalar1 / count;
99 avg.scale(denom);
100 *c = avg;
101 } else {
102 area *= 3;
bsalomon@google.com81712882012-11-01 17:12:34 +0000103 area = SkScalarDiv(SK_Scalar1, area);
104 center.fX = SkScalarMul(center.fX, area);
105 center.fY = SkScalarMul(center.fY, area);
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +0000106 // undo the translate of p0 to the origin.
107 *c = center + p0;
bsalomon@google.com278dc692012-02-15 16:52:51 +0000108 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000109 SkASSERT(!SkScalarIsNaN(c->fX) && !SkScalarIsNaN(c->fY));
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000110}
111
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000112static void compute_vectors(SegmentArray* segments,
113 SkPoint* fanPt,
114 SkPath::Direction dir,
115 int* vCount,
116 int* iCount) {
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000117 center_of_mass(*segments, fanPt);
118 int count = segments->count();
119
bsalomon@google.com278dc692012-02-15 16:52:51 +0000120 // Make the normals point towards the outside
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000121 GrPoint::Side normSide;
bsalomon@google.com278dc692012-02-15 16:52:51 +0000122 if (dir == SkPath::kCCW_Direction) {
123 normSide = GrPoint::kRight_Side;
124 } else {
125 normSide = GrPoint::kLeft_Side;
126 }
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000127
128 *vCount = 0;
129 *iCount = 0;
130 // compute normals at all points
131 for (int a = 0; a < count; ++a) {
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000132 Segment& sega = (*segments)[a];
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000133 int b = (a + 1) % count;
134 Segment& segb = (*segments)[b];
135
136 const GrPoint* prevPt = &sega.endPt();
137 int n = segb.countPoints();
138 for (int p = 0; p < n; ++p) {
139 segb.fNorms[p] = segb.fPts[p] - *prevPt;
140 segb.fNorms[p].normalize();
141 segb.fNorms[p].setOrthog(segb.fNorms[p], normSide);
142 prevPt = &segb.fPts[p];
143 }
144 if (Segment::kLine == segb.fType) {
145 *vCount += 5;
146 *iCount += 9;
147 } else {
148 *vCount += 6;
149 *iCount += 12;
150 }
151 }
152
153 // compute mid-vectors where segments meet. TODO: Detect shallow corners
154 // and leave out the wedges and close gaps by stitching segments together.
155 for (int a = 0; a < count; ++a) {
156 const Segment& sega = (*segments)[a];
157 int b = (a + 1) % count;
158 Segment& segb = (*segments)[b];
159 segb.fMid = segb.fNorms[0] + sega.endNorm();
160 segb.fMid.normalize();
161 // corner wedges
162 *vCount += 4;
163 *iCount += 6;
164 }
165}
166
bsalomon@google.com9732f622012-01-31 15:19:21 +0000167struct DegenerateTestData {
168 DegenerateTestData() { fStage = kInitial; }
169 bool isDegenerate() const { return kNonDegenerate != fStage; }
170 enum {
171 kInitial,
172 kPoint,
173 kLine,
174 kNonDegenerate
175 } fStage;
176 GrPoint fFirstPoint;
177 GrVec fLineNormal;
bsalomon@google.com81712882012-11-01 17:12:34 +0000178 SkScalar fLineC;
bsalomon@google.com9732f622012-01-31 15:19:21 +0000179};
180
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000181static const SkScalar kClose = (SK_Scalar1 / 16);
182static const SkScalar kCloseSqd = SkScalarMul(kClose, kClose);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000183
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000184static void update_degenerate_test(DegenerateTestData* data, const GrPoint& pt) {
bsalomon@google.com9732f622012-01-31 15:19:21 +0000185 switch (data->fStage) {
186 case DegenerateTestData::kInitial:
187 data->fFirstPoint = pt;
188 data->fStage = DegenerateTestData::kPoint;
189 break;
190 case DegenerateTestData::kPoint:
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000191 if (pt.distanceToSqd(data->fFirstPoint) > kCloseSqd) {
bsalomon@google.com9732f622012-01-31 15:19:21 +0000192 data->fLineNormal = pt - data->fFirstPoint;
193 data->fLineNormal.normalize();
194 data->fLineNormal.setOrthog(data->fLineNormal);
195 data->fLineC = -data->fLineNormal.dot(data->fFirstPoint);
196 data->fStage = DegenerateTestData::kLine;
197 }
198 break;
199 case DegenerateTestData::kLine:
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000200 if (SkScalarAbs(data->fLineNormal.dot(pt) + data->fLineC) > kClose) {
bsalomon@google.com9732f622012-01-31 15:19:21 +0000201 data->fStage = DegenerateTestData::kNonDegenerate;
202 }
203 case DegenerateTestData::kNonDegenerate:
204 break;
205 default:
206 GrCrash("Unexpected degenerate test stage.");
207 }
208}
209
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000210static inline bool get_direction(const SkPath& path, const SkMatrix& m, SkPath::Direction* dir) {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000211 if (!path.cheapComputeDirection(dir)) {
212 return false;
213 }
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000214 // check whether m reverses the orientation
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000215 SkASSERT(!m.hasPerspective());
bsalomon@google.com81712882012-11-01 17:12:34 +0000216 SkScalar det2x2 = SkScalarMul(m.get(SkMatrix::kMScaleX), m.get(SkMatrix::kMScaleY)) -
217 SkScalarMul(m.get(SkMatrix::kMSkewX), m.get(SkMatrix::kMSkewY));
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000218 if (det2x2 < 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000219 *dir = SkPath::OppositeDirection(*dir);
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000220 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000221 return true;
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000222}
223
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000224static inline void add_line_to_segment(const SkPoint& pt,
225 SegmentArray* segments,
226 SkRect* devBounds) {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000227 segments->push_back();
228 segments->back().fType = Segment::kLine;
229 segments->back().fPts[0] = pt;
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000230 devBounds->growToInclude(pt.fX, pt.fY);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000231}
232
commit-bot@chromium.org4b7d6732013-10-21 16:41:00 +0000233#ifdef SK_DEBUG
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000234static inline bool contains_inclusive(const SkRect& rect, const SkPoint& p) {
235 return p.fX >= rect.fLeft && p.fX <= rect.fRight && p.fY >= rect.fTop && p.fY <= rect.fBottom;
236}
commit-bot@chromium.org4b7d6732013-10-21 16:41:00 +0000237#endif
238
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000239static inline void add_quad_segment(const SkPoint pts[3],
240 SegmentArray* segments,
241 SkRect* devBounds) {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000242 if (pts[0].distanceToSqd(pts[1]) < kCloseSqd || pts[1].distanceToSqd(pts[2]) < kCloseSqd) {
243 if (pts[0] != pts[2]) {
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000244 add_line_to_segment(pts[2], segments, devBounds);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000245 }
246 } else {
247 segments->push_back();
248 segments->back().fType = Segment::kQuad;
249 segments->back().fPts[0] = pts[1];
250 segments->back().fPts[1] = pts[2];
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000251 SkASSERT(contains_inclusive(*devBounds, pts[0]));
252 devBounds->growToInclude(pts + 1, 2);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000253 }
254}
255
256static inline void add_cubic_segments(const SkPoint pts[4],
257 SkPath::Direction dir,
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000258 SegmentArray* segments,
259 SkRect* devBounds) {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000260 SkSTArray<15, SkPoint, true> quads;
261 GrPathUtils::convertCubicToQuads(pts, SK_Scalar1, true, dir, &quads);
262 int count = quads.count();
263 for (int q = 0; q < count; q += 3) {
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000264 add_quad_segment(&quads[q], segments, devBounds);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000265 }
266}
267
268static bool get_segments(const SkPath& path,
269 const SkMatrix& m,
270 SegmentArray* segments,
271 SkPoint* fanPt,
272 int* vCount,
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000273 int* iCount,
274 SkRect* devBounds) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000275 SkPath::Iter iter(path, true);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000276 // This renderer over-emphasizes very thin path regions. We use the distance
bsalomon@google.com5cc90d12012-01-17 16:28:34 +0000277 // to the path from the sample to compute coverage. Every pixel intersected
278 // 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 +0000279 // notice that the sample may be close to a very thin area of the path and
bsalomon@google.com5cc90d12012-01-17 16:28:34 +0000280 // thus should be very light. This is particularly egregious for degenerate
281 // line paths. We detect paths that are very close to a line (zero area) and
282 // draw nothing.
bsalomon@google.com9732f622012-01-31 15:19:21 +0000283 DegenerateTestData degenerateData;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000284 SkPath::Direction dir;
285 // get_direction can fail for some degenerate paths.
286 if (!get_direction(path, m, &dir)) {
287 return false;
288 }
bsalomon@google.com9732f622012-01-31 15:19:21 +0000289
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000290 for (;;) {
291 GrPoint pts[4];
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000292 SkPath::Verb verb = iter.next(pts);
293 switch (verb) {
294 case SkPath::kMove_Verb:
bsalomon@google.com1a38d552012-03-15 14:40:46 +0000295 m.mapPoints(pts, 1);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000296 update_degenerate_test(&degenerateData, pts[0]);
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000297 devBounds->set(pts->fX, pts->fY, pts->fX, pts->fY);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000298 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000299 case SkPath::kLine_Verb: {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000300 m.mapPoints(&pts[1], 1);
bsalomon@google.com1a38d552012-03-15 14:40:46 +0000301 update_degenerate_test(&degenerateData, pts[1]);
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000302 add_line_to_segment(pts[1], segments, devBounds);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000303 break;
304 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000305 case SkPath::kQuad_Verb:
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000306 m.mapPoints(pts, 3);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000307 update_degenerate_test(&degenerateData, pts[1]);
308 update_degenerate_test(&degenerateData, pts[2]);
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000309 add_quad_segment(pts, segments, devBounds);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000310 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000311 case SkPath::kCubic_Verb: {
bsalomon@google.com1a38d552012-03-15 14:40:46 +0000312 m.mapPoints(pts, 4);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000313 update_degenerate_test(&degenerateData, pts[1]);
314 update_degenerate_test(&degenerateData, pts[2]);
315 update_degenerate_test(&degenerateData, pts[3]);
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000316 add_cubic_segments(pts, dir, segments, devBounds);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000317 break;
318 };
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000319 case SkPath::kDone_Verb:
bsalomon@google.com9732f622012-01-31 15:19:21 +0000320 if (degenerateData.isDegenerate()) {
321 return false;
322 } else {
bsalomon@google.com278dc692012-02-15 16:52:51 +0000323 compute_vectors(segments, fanPt, dir, vCount, iCount);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000324 return true;
325 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000326 default:
327 break;
328 }
329 }
330}
331
332struct QuadVertex {
333 GrPoint fPos;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000334 GrPoint fUV;
bsalomon@google.com81712882012-11-01 17:12:34 +0000335 SkScalar fD0;
336 SkScalar fD1;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000337};
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000338
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000339struct Draw {
340 Draw() : fVertexCnt(0), fIndexCnt(0) {}
341 int fVertexCnt;
342 int fIndexCnt;
343};
344
345typedef SkTArray<Draw, true> DrawArray;
346
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000347static void create_vertices(const SegmentArray& segments,
348 const SkPoint& fanPt,
349 DrawArray* draws,
350 QuadVertex* verts,
351 uint16_t* idxs) {
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000352 Draw* draw = &draws->push_back();
353 // alias just to make vert/index assignments easier to read.
354 int* v = &draw->fVertexCnt;
355 int* i = &draw->fIndexCnt;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000356
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000357 int count = segments.count();
358 for (int a = 0; a < count; ++a) {
359 const Segment& sega = segments[a];
360 int b = (a + 1) % count;
361 const Segment& segb = segments[b];
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000362
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000363 // Check whether adding the verts for this segment to the current draw would cause index
364 // values to overflow.
365 int vCount = 4;
366 if (Segment::kLine == segb.fType) {
367 vCount += 5;
368 } else {
369 vCount += 6;
370 }
371 if (draw->fVertexCnt + vCount > (1 << 16)) {
372 verts += *v;
373 idxs += *i;
374 draw = &draws->push_back();
375 v = &draw->fVertexCnt;
376 i = &draw->fIndexCnt;
377 }
378
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000379 // FIXME: These tris are inset in the 1 unit arc around the corner
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000380 verts[*v + 0].fPos = sega.endPt();
381 verts[*v + 1].fPos = verts[*v + 0].fPos + sega.endNorm();
382 verts[*v + 2].fPos = verts[*v + 0].fPos + segb.fMid;
383 verts[*v + 3].fPos = verts[*v + 0].fPos + segb.fNorms[0];
384 verts[*v + 0].fUV.set(0,0);
385 verts[*v + 1].fUV.set(0,-SK_Scalar1);
386 verts[*v + 2].fUV.set(0,-SK_Scalar1);
387 verts[*v + 3].fUV.set(0,-SK_Scalar1);
388 verts[*v + 0].fD0 = verts[*v + 0].fD1 = -SK_Scalar1;
389 verts[*v + 1].fD0 = verts[*v + 1].fD1 = -SK_Scalar1;
390 verts[*v + 2].fD0 = verts[*v + 2].fD1 = -SK_Scalar1;
391 verts[*v + 3].fD0 = verts[*v + 3].fD1 = -SK_Scalar1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000392
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000393 idxs[*i + 0] = *v + 0;
394 idxs[*i + 1] = *v + 2;
395 idxs[*i + 2] = *v + 1;
396 idxs[*i + 3] = *v + 0;
397 idxs[*i + 4] = *v + 3;
398 idxs[*i + 5] = *v + 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000399
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000400 *v += 4;
401 *i += 6;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000402
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000403 if (Segment::kLine == segb.fType) {
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000404 verts[*v + 0].fPos = fanPt;
405 verts[*v + 1].fPos = sega.endPt();
406 verts[*v + 2].fPos = segb.fPts[0];
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000407
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000408 verts[*v + 3].fPos = verts[*v + 1].fPos + segb.fNorms[0];
409 verts[*v + 4].fPos = verts[*v + 2].fPos + segb.fNorms[0];
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000410
411 // we draw the line edge as a degenerate quad (u is 0, v is the
412 // signed distance to the edge)
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000413 SkScalar dist = fanPt.distanceToLineBetween(verts[*v + 1].fPos,
414 verts[*v + 2].fPos);
415 verts[*v + 0].fUV.set(0, dist);
416 verts[*v + 1].fUV.set(0, 0);
417 verts[*v + 2].fUV.set(0, 0);
418 verts[*v + 3].fUV.set(0, -SK_Scalar1);
419 verts[*v + 4].fUV.set(0, -SK_Scalar1);
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000420
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000421 verts[*v + 0].fD0 = verts[*v + 0].fD1 = -SK_Scalar1;
422 verts[*v + 1].fD0 = verts[*v + 1].fD1 = -SK_Scalar1;
423 verts[*v + 2].fD0 = verts[*v + 2].fD1 = -SK_Scalar1;
424 verts[*v + 3].fD0 = verts[*v + 3].fD1 = -SK_Scalar1;
425 verts[*v + 4].fD0 = verts[*v + 4].fD1 = -SK_Scalar1;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000426
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000427 idxs[*i + 0] = *v + 0;
428 idxs[*i + 1] = *v + 2;
429 idxs[*i + 2] = *v + 1;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000430
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000431 idxs[*i + 3] = *v + 3;
432 idxs[*i + 4] = *v + 1;
433 idxs[*i + 5] = *v + 2;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000434
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000435 idxs[*i + 6] = *v + 4;
436 idxs[*i + 7] = *v + 3;
437 idxs[*i + 8] = *v + 2;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000438
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000439 *v += 5;
440 *i += 9;
bsalomon@google.com06809612012-01-21 15:03:39 +0000441 } else {
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000442 GrPoint qpts[] = {sega.endPt(), segb.fPts[0], segb.fPts[1]};
bsalomon@google.com495e2102012-01-21 14:48:36 +0000443
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000444 GrVec midVec = segb.fNorms[0] + segb.fNorms[1];
445 midVec.normalize();
bsalomon@google.com06809612012-01-21 15:03:39 +0000446
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000447 verts[*v + 0].fPos = fanPt;
448 verts[*v + 1].fPos = qpts[0];
449 verts[*v + 2].fPos = qpts[2];
450 verts[*v + 3].fPos = qpts[0] + segb.fNorms[0];
451 verts[*v + 4].fPos = qpts[2] + segb.fNorms[1];
452 verts[*v + 5].fPos = qpts[1] + midVec;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000453
bsalomon@google.com81712882012-11-01 17:12:34 +0000454 SkScalar c = segb.fNorms[0].dot(qpts[0]);
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000455 verts[*v + 0].fD0 = -segb.fNorms[0].dot(fanPt) + c;
456 verts[*v + 1].fD0 = 0.f;
457 verts[*v + 2].fD0 = -segb.fNorms[0].dot(qpts[2]) + c;
458 verts[*v + 3].fD0 = -SK_ScalarMax/100;
459 verts[*v + 4].fD0 = -SK_ScalarMax/100;
460 verts[*v + 5].fD0 = -SK_ScalarMax/100;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000461
462 c = segb.fNorms[1].dot(qpts[2]);
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000463 verts[*v + 0].fD1 = -segb.fNorms[1].dot(fanPt) + c;
464 verts[*v + 1].fD1 = -segb.fNorms[1].dot(qpts[0]) + c;
465 verts[*v + 2].fD1 = 0.f;
466 verts[*v + 3].fD1 = -SK_ScalarMax/100;
467 verts[*v + 4].fD1 = -SK_ScalarMax/100;
468 verts[*v + 5].fD1 = -SK_ScalarMax/100;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000469
bsalomon@google.com19713172012-03-15 13:51:08 +0000470 GrPathUtils::QuadUVMatrix toUV(qpts);
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000471 toUV.apply<6, sizeof(QuadVertex), sizeof(GrPoint)>(verts + *v);
bsalomon@google.com06809612012-01-21 15:03:39 +0000472
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000473 idxs[*i + 0] = *v + 3;
474 idxs[*i + 1] = *v + 1;
475 idxs[*i + 2] = *v + 2;
476 idxs[*i + 3] = *v + 4;
477 idxs[*i + 4] = *v + 3;
478 idxs[*i + 5] = *v + 2;
bsalomon@google.com06809612012-01-21 15:03:39 +0000479
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000480 idxs[*i + 6] = *v + 5;
481 idxs[*i + 7] = *v + 3;
482 idxs[*i + 8] = *v + 4;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000483
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000484 idxs[*i + 9] = *v + 0;
485 idxs[*i + 10] = *v + 2;
486 idxs[*i + 11] = *v + 1;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000487
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000488 *v += 6;
489 *i += 12;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000490 }
491 }
492}
493
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000494///////////////////////////////////////////////////////////////////////////////
495
496/*
497 * Quadratic specified by 0=u^2-v canonical coords. u and v are the first
498 * two components of the vertex attribute. Coverage is based on signed
499 * distance with negative being inside, positive outside. The edge is specified in
500 * window space (y-down). If either the third or fourth component of the interpolated
501 * 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 +0000502 * attempt to trim to a portion of the infinite quad.
503 * Requires shader derivative instruction support.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000504 */
505
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000506class QuadEdgeEffect : public GrVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000507public:
508
509 static GrEffectRef* Create() {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000510 GR_CREATE_STATIC_EFFECT(gQuadEdgeEffect, QuadEdgeEffect, ());
511 gQuadEdgeEffect->ref();
512 return gQuadEdgeEffect;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000513 }
514
515 virtual ~QuadEdgeEffect() {}
516
517 static const char* Name() { return "QuadEdge"; }
518
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000519 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000520 uint32_t* validFlags) const SK_OVERRIDE {
521 *validFlags = 0;
522 }
523
524 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
525 return GrTBackendEffectFactory<QuadEdgeEffect>::getInstance();
526 }
527
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000528 class GLEffect : public GrGLVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000529 public:
530 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
531 : INHERITED (factory) {}
532
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000533 virtual void emitCode(GrGLFullShaderBuilder* builder,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000534 const GrDrawEffect& drawEffect,
535 EffectKey key,
536 const char* outputColor,
537 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000538 const TransformedCoordsArray&,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000539 const TextureSamplerArray& samplers) SK_OVERRIDE {
540 const char *vsName, *fsName;
541 const SkString* attrName =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000542 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000543 builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n");
544
545 SkAssertResult(builder->enableFeature(
546 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000547 builder->addVarying(kVec4f_GrSLType, "QuadEdge", &vsName, &fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000548
549 // keep the derivative instructions outside the conditional
550 builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
551 builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
552 builder->fsCodeAppendf("\t\tif (%s.z > 0.0 && %s.w > 0.0) {\n", fsName, fsName);
553 // today we know z and w are in device space. We could use derivatives
554 builder->fsCodeAppendf("\t\t\tedgeAlpha = min(min(%s.z, %s.w) + 0.5, 1.0);\n", fsName,
555 fsName);
556 builder->fsCodeAppendf ("\t\t} else {\n");
557 builder->fsCodeAppendf("\t\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
558 "\t\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
559 fsName, fsName);
560 builder->fsCodeAppendf("\t\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName,
561 fsName);
562 builder->fsCodeAppendf("\t\t\tedgeAlpha = "
563 "clamp(0.5 - edgeAlpha / length(gF), 0.0, 1.0);\n\t\t}\n");
564
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000565
566 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
commit-bot@chromium.orga34995e2013-10-23 05:42:03 +0000567 (GrGLSLExpr4(inputColor) * GrGLSLExpr1("edgeAlpha")).c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000568
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000569 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000570 }
571
572 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
573 return 0x0;
574 }
575
576 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
577
578 private:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000579 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000580 };
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000581
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000582private:
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000583 QuadEdgeEffect() {
584 this->addVertexAttrib(kVec4f_GrSLType);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000585 }
586
587 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
588 return true;
589 }
590
591 GR_DECLARE_EFFECT_TEST;
592
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000593 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000594};
595
596GR_DEFINE_EFFECT_TEST(QuadEdgeEffect);
597
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000598GrEffectRef* QuadEdgeEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000599 GrContext*,
600 const GrDrawTargetCaps& caps,
601 GrTexture*[]) {
602 // Doesn't work without derivative instructions.
603 return caps.shaderDerivativeSupport() ? QuadEdgeEffect::Create() : NULL;
604}
605
606///////////////////////////////////////////////////////////////////////////////
607
robertphillips@google.come79f3202014-02-11 16:30:21 +0000608bool GrAAConvexPathRenderer::canDrawPath(const SkPath& path,
609 const SkStrokeRec& stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000610 const GrDrawTarget* target,
611 bool antiAlias) const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000612 return (target->caps()->shaderDerivativeSupport() && antiAlias &&
robertphillips@google.come79f3202014-02-11 16:30:21 +0000613 stroke.isFillStyle() && !path.isInverseFillType() && path.isConvex());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000614}
615
robertphillips@google.com42903302013-04-20 12:26:07 +0000616namespace {
617
618// position + edge
619extern const GrVertexAttrib gPathAttribs[] = {
620 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
621 {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}
622};
623
624};
625
robertphillips@google.come79f3202014-02-11 16:30:21 +0000626bool GrAAConvexPathRenderer::onDrawPath(const SkPath& origPath,
627 const SkStrokeRec&,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000628 GrDrawTarget* target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000629 bool antiAlias) {
630
robertphillips@google.come79f3202014-02-11 16:30:21 +0000631 const SkPath* path = &origPath;
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000632 if (path->isEmpty()) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000633 return true;
634 }
bsalomon@google.com4647f902013-03-26 14:45:27 +0000635
bsalomon@google.com137f1342013-05-29 21:27:53 +0000636 SkMatrix viewMatrix = target->getDrawState().getViewMatrix();
637 GrDrawTarget::AutoStateRestore asr;
638 if (!asr.setIdentity(target, GrDrawTarget::kPreserve_ASRInit)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000639 return false;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000640 }
bsalomon@google.com137f1342013-05-29 21:27:53 +0000641 GrDrawState* drawState = target->drawState();
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000642
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000643 // We use the fact that SkPath::transform path does subdivision based on
644 // perspective. Otherwise, we apply the view matrix when copying to the
645 // segment representation.
646 SkPath tmpPath;
bsalomon@google.com137f1342013-05-29 21:27:53 +0000647 if (viewMatrix.hasPerspective()) {
robertphillips@google.come79f3202014-02-11 16:30:21 +0000648 origPath.transform(viewMatrix, &tmpPath);
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000649 path = &tmpPath;
bsalomon@google.com137f1342013-05-29 21:27:53 +0000650 viewMatrix = SkMatrix::I();
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000651 }
652
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000653 QuadVertex *verts;
654 uint16_t* idxs;
655
bsalomon@google.com06809612012-01-21 15:03:39 +0000656 int vCount;
657 int iCount;
bsalomon@google.com68a5b262012-03-05 18:24:07 +0000658 enum {
659 kPreallocSegmentCnt = 512 / sizeof(Segment),
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000660 kPreallocDrawCnt = 4,
bsalomon@google.com68a5b262012-03-05 18:24:07 +0000661 };
662 SkSTArray<kPreallocSegmentCnt, Segment, true> segments;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000663 SkPoint fanPt;
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000664
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000665 // We can't simply use the path bounds because we may degenerate cubics to quads which produces
666 // new control points outside the original convex hull.
667 SkRect devBounds;
668 if (!get_segments(*path, viewMatrix, &segments, &fanPt, &vCount, &iCount, &devBounds)) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000669 return false;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000670 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000671
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000672 // Our computed verts should all be within one pixel of the segment control points.
673 devBounds.outset(SK_Scalar1, SK_Scalar1);
674
robertphillips@google.com42903302013-04-20 12:26:07 +0000675 drawState->setVertexAttribs<gPathAttribs>(SK_ARRAY_COUNT(gPathAttribs));
bsalomon@google.com4647f902013-03-26 14:45:27 +0000676
bsalomon@google.com4647f902013-03-26 14:45:27 +0000677 static const int kEdgeAttrIndex = 1;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000678 GrEffectRef* quadEffect = QuadEdgeEffect::Create();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000679 drawState->addCoverageEffect(quadEffect, kEdgeAttrIndex)->unref();
bsalomon@google.com4647f902013-03-26 14:45:27 +0000680
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000681 GrDrawTarget::AutoReleaseGeometry arg(target, vCount, iCount);
bsalomon@google.comb3729422012-03-07 19:13:28 +0000682 if (!arg.succeeded()) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000683 return false;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000684 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000685 SkASSERT(sizeof(QuadVertex) == drawState->getVertexSize());
bsalomon@google.comb3729422012-03-07 19:13:28 +0000686 verts = reinterpret_cast<QuadVertex*>(arg.vertices());
687 idxs = reinterpret_cast<uint16_t*>(arg.indices());
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000688
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000689 SkSTArray<kPreallocDrawCnt, Draw, true> draws;
690 create_vertices(segments, fanPt, &draws, verts, idxs);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000691
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000692 // Check devBounds
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000693#ifdef SK_DEBUG
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000694 SkRect tolDevBounds = devBounds;
695 tolDevBounds.outset(SK_Scalar1 / 10000, SK_Scalar1 / 10000);
696 SkRect actualBounds;
697 actualBounds.set(verts[0].fPos, verts[1].fPos);
698 for (int i = 2; i < vCount; ++i) {
699 actualBounds.growToInclude(verts[i].fPos.fX, verts[i].fPos.fY);
700 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000701 SkASSERT(tolDevBounds.contains(actualBounds));
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000702#endif
703
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000704 int vOffset = 0;
705 for (int i = 0; i < draws.count(); ++i) {
706 const Draw& draw = draws[i];
707 target->drawIndexed(kTriangles_GrPrimitiveType,
708 vOffset, // start vertex
709 0, // start index
710 draw.fVertexCnt,
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000711 draw.fIndexCnt,
712 &devBounds);
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000713 vOffset += draw.fVertexCnt;
714 }
bsalomon@google.coma8347462012-10-08 18:59:39 +0000715
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000716 return true;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000717}