blob: ad6e061ec4d62d7e807f0bb27f69d0b004129fe1 [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.org106655e2013-09-03 21:28:55 +0000233static inline bool contains_inclusive(const SkRect& rect, const SkPoint& p) {
234 return p.fX >= rect.fLeft && p.fX <= rect.fRight && p.fY >= rect.fTop && p.fY <= rect.fBottom;
235}
236static inline void add_quad_segment(const SkPoint pts[3],
237 SegmentArray* segments,
238 SkRect* devBounds) {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000239 if (pts[0].distanceToSqd(pts[1]) < kCloseSqd || pts[1].distanceToSqd(pts[2]) < kCloseSqd) {
240 if (pts[0] != pts[2]) {
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000241 add_line_to_segment(pts[2], segments, devBounds);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000242 }
243 } else {
244 segments->push_back();
245 segments->back().fType = Segment::kQuad;
246 segments->back().fPts[0] = pts[1];
247 segments->back().fPts[1] = pts[2];
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000248 SkASSERT(contains_inclusive(*devBounds, pts[0]));
249 devBounds->growToInclude(pts + 1, 2);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000250 }
251}
252
253static inline void add_cubic_segments(const SkPoint pts[4],
254 SkPath::Direction dir,
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000255 SegmentArray* segments,
256 SkRect* devBounds) {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000257 SkSTArray<15, SkPoint, true> quads;
258 GrPathUtils::convertCubicToQuads(pts, SK_Scalar1, true, dir, &quads);
259 int count = quads.count();
260 for (int q = 0; q < count; q += 3) {
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000261 add_quad_segment(&quads[q], segments, devBounds);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000262 }
263}
264
265static bool get_segments(const SkPath& path,
266 const SkMatrix& m,
267 SegmentArray* segments,
268 SkPoint* fanPt,
269 int* vCount,
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000270 int* iCount,
271 SkRect* devBounds) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000272 SkPath::Iter iter(path, true);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000273 // This renderer over-emphasizes very thin path regions. We use the distance
bsalomon@google.com5cc90d12012-01-17 16:28:34 +0000274 // to the path from the sample to compute coverage. Every pixel intersected
275 // 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 +0000276 // notice that the sample may be close to a very thin area of the path and
bsalomon@google.com5cc90d12012-01-17 16:28:34 +0000277 // thus should be very light. This is particularly egregious for degenerate
278 // line paths. We detect paths that are very close to a line (zero area) and
279 // draw nothing.
bsalomon@google.com9732f622012-01-31 15:19:21 +0000280 DegenerateTestData degenerateData;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000281 SkPath::Direction dir;
282 // get_direction can fail for some degenerate paths.
283 if (!get_direction(path, m, &dir)) {
284 return false;
285 }
bsalomon@google.com9732f622012-01-31 15:19:21 +0000286
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000287 for (;;) {
288 GrPoint pts[4];
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000289 SkPath::Verb verb = iter.next(pts);
290 switch (verb) {
291 case SkPath::kMove_Verb:
bsalomon@google.com1a38d552012-03-15 14:40:46 +0000292 m.mapPoints(pts, 1);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000293 update_degenerate_test(&degenerateData, pts[0]);
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000294 devBounds->set(pts->fX, pts->fY, pts->fX, pts->fY);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000295 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000296 case SkPath::kLine_Verb: {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000297 m.mapPoints(&pts[1], 1);
bsalomon@google.com1a38d552012-03-15 14:40:46 +0000298 update_degenerate_test(&degenerateData, pts[1]);
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000299 add_line_to_segment(pts[1], segments, devBounds);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000300 break;
301 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000302 case SkPath::kQuad_Verb:
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000303 m.mapPoints(pts, 3);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000304 update_degenerate_test(&degenerateData, pts[1]);
305 update_degenerate_test(&degenerateData, pts[2]);
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000306 add_quad_segment(pts, segments, devBounds);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000307 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000308 case SkPath::kCubic_Verb: {
bsalomon@google.com1a38d552012-03-15 14:40:46 +0000309 m.mapPoints(pts, 4);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000310 update_degenerate_test(&degenerateData, pts[1]);
311 update_degenerate_test(&degenerateData, pts[2]);
312 update_degenerate_test(&degenerateData, pts[3]);
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000313 add_cubic_segments(pts, dir, segments, devBounds);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000314 break;
315 };
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000316 case SkPath::kDone_Verb:
bsalomon@google.com9732f622012-01-31 15:19:21 +0000317 if (degenerateData.isDegenerate()) {
318 return false;
319 } else {
bsalomon@google.com278dc692012-02-15 16:52:51 +0000320 compute_vectors(segments, fanPt, dir, vCount, iCount);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000321 return true;
322 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000323 default:
324 break;
325 }
326 }
327}
328
329struct QuadVertex {
330 GrPoint fPos;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000331 GrPoint fUV;
bsalomon@google.com81712882012-11-01 17:12:34 +0000332 SkScalar fD0;
333 SkScalar fD1;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000334};
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000335
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000336struct Draw {
337 Draw() : fVertexCnt(0), fIndexCnt(0) {}
338 int fVertexCnt;
339 int fIndexCnt;
340};
341
342typedef SkTArray<Draw, true> DrawArray;
343
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000344static void create_vertices(const SegmentArray& segments,
345 const SkPoint& fanPt,
346 DrawArray* draws,
347 QuadVertex* verts,
348 uint16_t* idxs) {
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000349 Draw* draw = &draws->push_back();
350 // alias just to make vert/index assignments easier to read.
351 int* v = &draw->fVertexCnt;
352 int* i = &draw->fIndexCnt;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000353
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000354 int count = segments.count();
355 for (int a = 0; a < count; ++a) {
356 const Segment& sega = segments[a];
357 int b = (a + 1) % count;
358 const Segment& segb = segments[b];
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000359
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000360 // Check whether adding the verts for this segment to the current draw would cause index
361 // values to overflow.
362 int vCount = 4;
363 if (Segment::kLine == segb.fType) {
364 vCount += 5;
365 } else {
366 vCount += 6;
367 }
368 if (draw->fVertexCnt + vCount > (1 << 16)) {
369 verts += *v;
370 idxs += *i;
371 draw = &draws->push_back();
372 v = &draw->fVertexCnt;
373 i = &draw->fIndexCnt;
374 }
375
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000376 // FIXME: These tris are inset in the 1 unit arc around the corner
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000377 verts[*v + 0].fPos = sega.endPt();
378 verts[*v + 1].fPos = verts[*v + 0].fPos + sega.endNorm();
379 verts[*v + 2].fPos = verts[*v + 0].fPos + segb.fMid;
380 verts[*v + 3].fPos = verts[*v + 0].fPos + segb.fNorms[0];
381 verts[*v + 0].fUV.set(0,0);
382 verts[*v + 1].fUV.set(0,-SK_Scalar1);
383 verts[*v + 2].fUV.set(0,-SK_Scalar1);
384 verts[*v + 3].fUV.set(0,-SK_Scalar1);
385 verts[*v + 0].fD0 = verts[*v + 0].fD1 = -SK_Scalar1;
386 verts[*v + 1].fD0 = verts[*v + 1].fD1 = -SK_Scalar1;
387 verts[*v + 2].fD0 = verts[*v + 2].fD1 = -SK_Scalar1;
388 verts[*v + 3].fD0 = verts[*v + 3].fD1 = -SK_Scalar1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000389
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000390 idxs[*i + 0] = *v + 0;
391 idxs[*i + 1] = *v + 2;
392 idxs[*i + 2] = *v + 1;
393 idxs[*i + 3] = *v + 0;
394 idxs[*i + 4] = *v + 3;
395 idxs[*i + 5] = *v + 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000396
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000397 *v += 4;
398 *i += 6;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000399
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000400 if (Segment::kLine == segb.fType) {
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000401 verts[*v + 0].fPos = fanPt;
402 verts[*v + 1].fPos = sega.endPt();
403 verts[*v + 2].fPos = segb.fPts[0];
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000404
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000405 verts[*v + 3].fPos = verts[*v + 1].fPos + segb.fNorms[0];
406 verts[*v + 4].fPos = verts[*v + 2].fPos + segb.fNorms[0];
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000407
408 // we draw the line edge as a degenerate quad (u is 0, v is the
409 // signed distance to the edge)
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000410 SkScalar dist = fanPt.distanceToLineBetween(verts[*v + 1].fPos,
411 verts[*v + 2].fPos);
412 verts[*v + 0].fUV.set(0, dist);
413 verts[*v + 1].fUV.set(0, 0);
414 verts[*v + 2].fUV.set(0, 0);
415 verts[*v + 3].fUV.set(0, -SK_Scalar1);
416 verts[*v + 4].fUV.set(0, -SK_Scalar1);
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000417
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000418 verts[*v + 0].fD0 = verts[*v + 0].fD1 = -SK_Scalar1;
419 verts[*v + 1].fD0 = verts[*v + 1].fD1 = -SK_Scalar1;
420 verts[*v + 2].fD0 = verts[*v + 2].fD1 = -SK_Scalar1;
421 verts[*v + 3].fD0 = verts[*v + 3].fD1 = -SK_Scalar1;
422 verts[*v + 4].fD0 = verts[*v + 4].fD1 = -SK_Scalar1;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000423
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000424 idxs[*i + 0] = *v + 0;
425 idxs[*i + 1] = *v + 2;
426 idxs[*i + 2] = *v + 1;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000427
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000428 idxs[*i + 3] = *v + 3;
429 idxs[*i + 4] = *v + 1;
430 idxs[*i + 5] = *v + 2;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000431
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000432 idxs[*i + 6] = *v + 4;
433 idxs[*i + 7] = *v + 3;
434 idxs[*i + 8] = *v + 2;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000435
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000436 *v += 5;
437 *i += 9;
bsalomon@google.com06809612012-01-21 15:03:39 +0000438 } else {
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000439 GrPoint qpts[] = {sega.endPt(), segb.fPts[0], segb.fPts[1]};
bsalomon@google.com495e2102012-01-21 14:48:36 +0000440
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000441 GrVec midVec = segb.fNorms[0] + segb.fNorms[1];
442 midVec.normalize();
bsalomon@google.com06809612012-01-21 15:03:39 +0000443
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000444 verts[*v + 0].fPos = fanPt;
445 verts[*v + 1].fPos = qpts[0];
446 verts[*v + 2].fPos = qpts[2];
447 verts[*v + 3].fPos = qpts[0] + segb.fNorms[0];
448 verts[*v + 4].fPos = qpts[2] + segb.fNorms[1];
449 verts[*v + 5].fPos = qpts[1] + midVec;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000450
bsalomon@google.com81712882012-11-01 17:12:34 +0000451 SkScalar c = segb.fNorms[0].dot(qpts[0]);
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000452 verts[*v + 0].fD0 = -segb.fNorms[0].dot(fanPt) + c;
453 verts[*v + 1].fD0 = 0.f;
454 verts[*v + 2].fD0 = -segb.fNorms[0].dot(qpts[2]) + c;
455 verts[*v + 3].fD0 = -SK_ScalarMax/100;
456 verts[*v + 4].fD0 = -SK_ScalarMax/100;
457 verts[*v + 5].fD0 = -SK_ScalarMax/100;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000458
459 c = segb.fNorms[1].dot(qpts[2]);
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000460 verts[*v + 0].fD1 = -segb.fNorms[1].dot(fanPt) + c;
461 verts[*v + 1].fD1 = -segb.fNorms[1].dot(qpts[0]) + c;
462 verts[*v + 2].fD1 = 0.f;
463 verts[*v + 3].fD1 = -SK_ScalarMax/100;
464 verts[*v + 4].fD1 = -SK_ScalarMax/100;
465 verts[*v + 5].fD1 = -SK_ScalarMax/100;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000466
bsalomon@google.com19713172012-03-15 13:51:08 +0000467 GrPathUtils::QuadUVMatrix toUV(qpts);
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000468 toUV.apply<6, sizeof(QuadVertex), sizeof(GrPoint)>(verts + *v);
bsalomon@google.com06809612012-01-21 15:03:39 +0000469
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000470 idxs[*i + 0] = *v + 3;
471 idxs[*i + 1] = *v + 1;
472 idxs[*i + 2] = *v + 2;
473 idxs[*i + 3] = *v + 4;
474 idxs[*i + 4] = *v + 3;
475 idxs[*i + 5] = *v + 2;
bsalomon@google.com06809612012-01-21 15:03:39 +0000476
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000477 idxs[*i + 6] = *v + 5;
478 idxs[*i + 7] = *v + 3;
479 idxs[*i + 8] = *v + 4;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000480
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000481 idxs[*i + 9] = *v + 0;
482 idxs[*i + 10] = *v + 2;
483 idxs[*i + 11] = *v + 1;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000484
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000485 *v += 6;
486 *i += 12;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000487 }
488 }
489}
490
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000491///////////////////////////////////////////////////////////////////////////////
492
493/*
494 * Quadratic specified by 0=u^2-v canonical coords. u and v are the first
495 * two components of the vertex attribute. Coverage is based on signed
496 * distance with negative being inside, positive outside. The edge is specified in
497 * window space (y-down). If either the third or fourth component of the interpolated
498 * 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 +0000499 * attempt to trim to a portion of the infinite quad.
500 * Requires shader derivative instruction support.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000501 */
502
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000503class QuadEdgeEffect : public GrVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000504public:
505
506 static GrEffectRef* Create() {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000507 GR_CREATE_STATIC_EFFECT(gQuadEdgeEffect, QuadEdgeEffect, ());
508 gQuadEdgeEffect->ref();
509 return gQuadEdgeEffect;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000510 }
511
512 virtual ~QuadEdgeEffect() {}
513
514 static const char* Name() { return "QuadEdge"; }
515
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000516 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000517 uint32_t* validFlags) const SK_OVERRIDE {
518 *validFlags = 0;
519 }
520
521 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
522 return GrTBackendEffectFactory<QuadEdgeEffect>::getInstance();
523 }
524
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000525 class GLEffect : public GrGLVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000526 public:
527 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
528 : INHERITED (factory) {}
529
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000530 virtual void emitCode(GrGLFullShaderBuilder* builder,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000531 const GrDrawEffect& drawEffect,
532 EffectKey key,
533 const char* outputColor,
534 const char* inputColor,
bsalomon@google.com77af6802013-10-02 13:04:56 +0000535 const TransformedCoordsArray&,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000536 const TextureSamplerArray& samplers) SK_OVERRIDE {
537 const char *vsName, *fsName;
538 const SkString* attrName =
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000539 builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000540 builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n");
541
542 SkAssertResult(builder->enableFeature(
543 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000544 builder->addVarying(kVec4f_GrSLType, "QuadEdge", &vsName, &fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000545
546 // keep the derivative instructions outside the conditional
547 builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
548 builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
549 builder->fsCodeAppendf("\t\tif (%s.z > 0.0 && %s.w > 0.0) {\n", fsName, fsName);
550 // today we know z and w are in device space. We could use derivatives
551 builder->fsCodeAppendf("\t\t\tedgeAlpha = min(min(%s.z, %s.w) + 0.5, 1.0);\n", fsName,
552 fsName);
553 builder->fsCodeAppendf ("\t\t} else {\n");
554 builder->fsCodeAppendf("\t\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
555 "\t\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
556 fsName, fsName);
557 builder->fsCodeAppendf("\t\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName,
558 fsName);
559 builder->fsCodeAppendf("\t\t\tedgeAlpha = "
560 "clamp(0.5 - edgeAlpha / length(gF), 0.0, 1.0);\n\t\t}\n");
561
commit-bot@chromium.org824c3462013-10-10 06:30:18 +0000562
563 builder->fsCodeAppendf("\t%s = %s;\n", outputColor,
564 (GrGLSLExpr<4>(inputColor) * GrGLSLExpr<1>("edgeAlpha")).c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000565
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000566 builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000567 }
568
569 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
570 return 0x0;
571 }
572
573 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
574
575 private:
commit-bot@chromium.org261dc562013-10-04 15:42:56 +0000576 typedef GrGLVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000577 };
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000578
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000579private:
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000580 QuadEdgeEffect() {
581 this->addVertexAttrib(kVec4f_GrSLType);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000582 }
583
584 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
585 return true;
586 }
587
588 GR_DECLARE_EFFECT_TEST;
589
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000590 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000591};
592
593GR_DEFINE_EFFECT_TEST(QuadEdgeEffect);
594
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000595GrEffectRef* QuadEdgeEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000596 GrContext*,
597 const GrDrawTargetCaps& caps,
598 GrTexture*[]) {
599 // Doesn't work without derivative instructions.
600 return caps.shaderDerivativeSupport() ? QuadEdgeEffect::Create() : NULL;
601}
602
603///////////////////////////////////////////////////////////////////////////////
604
robertphillips@google.comfa662942012-05-17 12:20:22 +0000605bool GrAAConvexPathRenderer::canDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000606 const SkStrokeRec& stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000607 const GrDrawTarget* target,
608 bool antiAlias) const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000609 return (target->caps()->shaderDerivativeSupport() && antiAlias &&
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000610 stroke.isFillStyle() && !path.isInverseFillType() && path.isConvex());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000611}
612
robertphillips@google.com42903302013-04-20 12:26:07 +0000613namespace {
614
615// position + edge
616extern const GrVertexAttrib gPathAttribs[] = {
617 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
618 {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}
619};
620
621};
622
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000623bool GrAAConvexPathRenderer::onDrawPath(const SkPath& origPath,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000624 const SkStrokeRec&,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000625 GrDrawTarget* target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000626 bool antiAlias) {
627
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000628 const SkPath* path = &origPath;
629 if (path->isEmpty()) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000630 return true;
631 }
bsalomon@google.com4647f902013-03-26 14:45:27 +0000632
bsalomon@google.com137f1342013-05-29 21:27:53 +0000633 SkMatrix viewMatrix = target->getDrawState().getViewMatrix();
634 GrDrawTarget::AutoStateRestore asr;
635 if (!asr.setIdentity(target, GrDrawTarget::kPreserve_ASRInit)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000636 return false;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000637 }
bsalomon@google.com137f1342013-05-29 21:27:53 +0000638 GrDrawState* drawState = target->drawState();
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000639
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000640 // We use the fact that SkPath::transform path does subdivision based on
641 // perspective. Otherwise, we apply the view matrix when copying to the
642 // segment representation.
643 SkPath tmpPath;
bsalomon@google.com137f1342013-05-29 21:27:53 +0000644 if (viewMatrix.hasPerspective()) {
645 origPath.transform(viewMatrix, &tmpPath);
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000646 path = &tmpPath;
bsalomon@google.com137f1342013-05-29 21:27:53 +0000647 viewMatrix = SkMatrix::I();
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000648 }
649
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000650 QuadVertex *verts;
651 uint16_t* idxs;
652
bsalomon@google.com06809612012-01-21 15:03:39 +0000653 int vCount;
654 int iCount;
bsalomon@google.com68a5b262012-03-05 18:24:07 +0000655 enum {
656 kPreallocSegmentCnt = 512 / sizeof(Segment),
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000657 kPreallocDrawCnt = 4,
bsalomon@google.com68a5b262012-03-05 18:24:07 +0000658 };
659 SkSTArray<kPreallocSegmentCnt, Segment, true> segments;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000660 SkPoint fanPt;
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000661
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000662 // We can't simply use the path bounds because we may degenerate cubics to quads which produces
663 // new control points outside the original convex hull.
664 SkRect devBounds;
665 if (!get_segments(*path, viewMatrix, &segments, &fanPt, &vCount, &iCount, &devBounds)) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000666 return false;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000667 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000668
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000669 // Our computed verts should all be within one pixel of the segment control points.
670 devBounds.outset(SK_Scalar1, SK_Scalar1);
671
robertphillips@google.com42903302013-04-20 12:26:07 +0000672 drawState->setVertexAttribs<gPathAttribs>(SK_ARRAY_COUNT(gPathAttribs));
bsalomon@google.com4647f902013-03-26 14:45:27 +0000673
bsalomon@google.com4647f902013-03-26 14:45:27 +0000674 static const int kEdgeAttrIndex = 1;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000675 GrEffectRef* quadEffect = QuadEdgeEffect::Create();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000676 drawState->addCoverageEffect(quadEffect, kEdgeAttrIndex)->unref();
bsalomon@google.com4647f902013-03-26 14:45:27 +0000677
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000678 GrDrawTarget::AutoReleaseGeometry arg(target, vCount, iCount);
bsalomon@google.comb3729422012-03-07 19:13:28 +0000679 if (!arg.succeeded()) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000680 return false;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000681 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000682 SkASSERT(sizeof(QuadVertex) == drawState->getVertexSize());
bsalomon@google.comb3729422012-03-07 19:13:28 +0000683 verts = reinterpret_cast<QuadVertex*>(arg.vertices());
684 idxs = reinterpret_cast<uint16_t*>(arg.indices());
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000685
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000686 SkSTArray<kPreallocDrawCnt, Draw, true> draws;
687 create_vertices(segments, fanPt, &draws, verts, idxs);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000688
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000689 // Check devBounds
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000690#ifdef SK_DEBUG
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000691 SkRect tolDevBounds = devBounds;
692 tolDevBounds.outset(SK_Scalar1 / 10000, SK_Scalar1 / 10000);
693 SkRect actualBounds;
694 actualBounds.set(verts[0].fPos, verts[1].fPos);
695 for (int i = 2; i < vCount; ++i) {
696 actualBounds.growToInclude(verts[i].fPos.fX, verts[i].fPos.fY);
697 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000698 SkASSERT(tolDevBounds.contains(actualBounds));
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000699#endif
700
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000701 int vOffset = 0;
702 for (int i = 0; i < draws.count(); ++i) {
703 const Draw& draw = draws[i];
704 target->drawIndexed(kTriangles_GrPrimitiveType,
705 vOffset, // start vertex
706 0, // start index
707 draw.fVertexCnt,
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000708 draw.fIndexCnt,
709 &devBounds);
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000710 vOffset += draw.fVertexCnt;
711 }
bsalomon@google.coma8347462012-10-08 18:59:39 +0000712
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000713 return true;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000714}