blob: 7063440ba43c18ff6676b8c0e5aacfc0e1e1ccd6 [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
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000024#include "effects/GrVertexEffect.h"
25
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000026GrAAConvexPathRenderer::GrAAConvexPathRenderer() {
27}
28
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000029struct Segment {
30 enum {
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000031 // These enum values are assumed in member functions below.
32 kLine = 0,
33 kQuad = 1,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000034 } fType;
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000035
bsalomon@google.com9aed1142012-01-30 14:28:39 +000036 // line uses one pt, quad uses 2 pts
37 GrPoint fPts[2];
38 // normal to edge ending at each pt
39 GrVec fNorms[2];
40 // is the corner where the previous segment meets this segment
41 // sharp. If so, fMid is a normalized bisector facing outward.
42 GrVec fMid;
43
44 int countPoints() {
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000045 GR_STATIC_ASSERT(0 == kLine && 1 == kQuad);
46 return fType + 1;
bsalomon@google.com9aed1142012-01-30 14:28:39 +000047 }
48 const SkPoint& endPt() const {
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000049 GR_STATIC_ASSERT(0 == kLine && 1 == kQuad);
50 return fPts[fType];
bsalomon@google.com9aed1142012-01-30 14:28:39 +000051 };
52 const SkPoint& endNorm() const {
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000053 GR_STATIC_ASSERT(0 == kLine && 1 == kQuad);
54 return fNorms[fType];
bsalomon@google.com9aed1142012-01-30 14:28:39 +000055 };
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000056};
57
58typedef SkTArray<Segment, true> SegmentArray;
59
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +000060static void center_of_mass(const SegmentArray& segments, SkPoint* c) {
bsalomon@google.com81712882012-11-01 17:12:34 +000061 SkScalar area = 0;
vandebo@chromium.org6390c722012-03-28 21:03:22 +000062 SkPoint center = {0, 0};
bsalomon@google.com9aed1142012-01-30 14:28:39 +000063 int count = segments.count();
vandebo@chromium.org6390c722012-03-28 21:03:22 +000064 SkPoint p0 = {0, 0};
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000065 if (count > 2) {
66 // We translate the polygon so that the first point is at the origin.
67 // This avoids some precision issues with small area polygons far away
68 // from the origin.
69 p0 = segments[0].endPt();
70 SkPoint pi;
71 SkPoint pj;
bsalomon@google.coma51ab842012-07-10 19:53:34 +000072 // the first and last iteration of the below loop would compute
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000073 // zeros since the starting / ending point is (0,0). So instead we start
74 // at i=1 and make the last iteration i=count-2.
75 pj = segments[1].endPt() - p0;
76 for (int i = 1; i < count - 1; ++i) {
77 pi = pj;
78 const SkPoint pj = segments[i + 1].endPt() - p0;
79
bsalomon@google.com81712882012-11-01 17:12:34 +000080 SkScalar t = SkScalarMul(pi.fX, pj.fY) - SkScalarMul(pj.fX, pi.fY);
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000081 area += t;
82 center.fX += (pi.fX + pj.fX) * t;
83 center.fY += (pi.fY + pj.fY) * t;
84
85 }
bsalomon@google.com9aed1142012-01-30 14:28:39 +000086 }
bsalomon@google.com278dc692012-02-15 16:52:51 +000087 // If the poly has no area then we instead return the average of
88 // its points.
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000089 if (SkScalarNearlyZero(area)) {
bsalomon@google.com278dc692012-02-15 16:52:51 +000090 SkPoint avg;
91 avg.set(0, 0);
92 for (int i = 0; i < count; ++i) {
93 const SkPoint& pt = segments[i].endPt();
94 avg.fX += pt.fX;
95 avg.fY += pt.fY;
96 }
97 SkScalar denom = SK_Scalar1 / count;
98 avg.scale(denom);
99 *c = avg;
100 } else {
101 area *= 3;
bsalomon@google.com81712882012-11-01 17:12:34 +0000102 area = SkScalarDiv(SK_Scalar1, area);
103 center.fX = SkScalarMul(center.fX, area);
104 center.fY = SkScalarMul(center.fY, area);
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +0000105 // undo the translate of p0 to the origin.
106 *c = center + p0;
bsalomon@google.com278dc692012-02-15 16:52:51 +0000107 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000108 SkASSERT(!SkScalarIsNaN(c->fX) && !SkScalarIsNaN(c->fY));
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000109}
110
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000111static void compute_vectors(SegmentArray* segments,
112 SkPoint* fanPt,
113 SkPath::Direction dir,
114 int* vCount,
115 int* iCount) {
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000116 center_of_mass(*segments, fanPt);
117 int count = segments->count();
118
bsalomon@google.com278dc692012-02-15 16:52:51 +0000119 // Make the normals point towards the outside
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000120 GrPoint::Side normSide;
bsalomon@google.com278dc692012-02-15 16:52:51 +0000121 if (dir == SkPath::kCCW_Direction) {
122 normSide = GrPoint::kRight_Side;
123 } else {
124 normSide = GrPoint::kLeft_Side;
125 }
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000126
127 *vCount = 0;
128 *iCount = 0;
129 // compute normals at all points
130 for (int a = 0; a < count; ++a) {
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000131 Segment& sega = (*segments)[a];
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000132 int b = (a + 1) % count;
133 Segment& segb = (*segments)[b];
134
135 const GrPoint* prevPt = &sega.endPt();
136 int n = segb.countPoints();
137 for (int p = 0; p < n; ++p) {
138 segb.fNorms[p] = segb.fPts[p] - *prevPt;
139 segb.fNorms[p].normalize();
140 segb.fNorms[p].setOrthog(segb.fNorms[p], normSide);
141 prevPt = &segb.fPts[p];
142 }
143 if (Segment::kLine == segb.fType) {
144 *vCount += 5;
145 *iCount += 9;
146 } else {
147 *vCount += 6;
148 *iCount += 12;
149 }
150 }
151
152 // compute mid-vectors where segments meet. TODO: Detect shallow corners
153 // and leave out the wedges and close gaps by stitching segments together.
154 for (int a = 0; a < count; ++a) {
155 const Segment& sega = (*segments)[a];
156 int b = (a + 1) % count;
157 Segment& segb = (*segments)[b];
158 segb.fMid = segb.fNorms[0] + sega.endNorm();
159 segb.fMid.normalize();
160 // corner wedges
161 *vCount += 4;
162 *iCount += 6;
163 }
164}
165
bsalomon@google.com9732f622012-01-31 15:19:21 +0000166struct DegenerateTestData {
167 DegenerateTestData() { fStage = kInitial; }
168 bool isDegenerate() const { return kNonDegenerate != fStage; }
169 enum {
170 kInitial,
171 kPoint,
172 kLine,
173 kNonDegenerate
174 } fStage;
175 GrPoint fFirstPoint;
176 GrVec fLineNormal;
bsalomon@google.com81712882012-11-01 17:12:34 +0000177 SkScalar fLineC;
bsalomon@google.com9732f622012-01-31 15:19:21 +0000178};
179
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000180static const SkScalar kClose = (SK_Scalar1 / 16);
181static const SkScalar kCloseSqd = SkScalarMul(kClose, kClose);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000182
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000183static void update_degenerate_test(DegenerateTestData* data, const GrPoint& pt) {
bsalomon@google.com9732f622012-01-31 15:19:21 +0000184 switch (data->fStage) {
185 case DegenerateTestData::kInitial:
186 data->fFirstPoint = pt;
187 data->fStage = DegenerateTestData::kPoint;
188 break;
189 case DegenerateTestData::kPoint:
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000190 if (pt.distanceToSqd(data->fFirstPoint) > kCloseSqd) {
bsalomon@google.com9732f622012-01-31 15:19:21 +0000191 data->fLineNormal = pt - data->fFirstPoint;
192 data->fLineNormal.normalize();
193 data->fLineNormal.setOrthog(data->fLineNormal);
194 data->fLineC = -data->fLineNormal.dot(data->fFirstPoint);
195 data->fStage = DegenerateTestData::kLine;
196 }
197 break;
198 case DegenerateTestData::kLine:
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000199 if (SkScalarAbs(data->fLineNormal.dot(pt) + data->fLineC) > kClose) {
bsalomon@google.com9732f622012-01-31 15:19:21 +0000200 data->fStage = DegenerateTestData::kNonDegenerate;
201 }
202 case DegenerateTestData::kNonDegenerate:
203 break;
204 default:
205 GrCrash("Unexpected degenerate test stage.");
206 }
207}
208
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000209static inline bool get_direction(const SkPath& path, const SkMatrix& m, SkPath::Direction* dir) {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000210 if (!path.cheapComputeDirection(dir)) {
211 return false;
212 }
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000213 // check whether m reverses the orientation
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000214 SkASSERT(!m.hasPerspective());
bsalomon@google.com81712882012-11-01 17:12:34 +0000215 SkScalar det2x2 = SkScalarMul(m.get(SkMatrix::kMScaleX), m.get(SkMatrix::kMScaleY)) -
216 SkScalarMul(m.get(SkMatrix::kMSkewX), m.get(SkMatrix::kMSkewY));
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000217 if (det2x2 < 0) {
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000218 *dir = SkPath::OppositeDirection(*dir);
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000219 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000220 return true;
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000221}
222
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000223static inline void add_line_to_segment(const SkPoint& pt,
224 SegmentArray* segments,
225 SkRect* devBounds) {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000226 segments->push_back();
227 segments->back().fType = Segment::kLine;
228 segments->back().fPts[0] = pt;
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000229 devBounds->growToInclude(pt.fX, pt.fY);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000230}
231
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000232static inline bool contains_inclusive(const SkRect& rect, const SkPoint& p) {
233 return p.fX >= rect.fLeft && p.fX <= rect.fRight && p.fY >= rect.fTop && p.fY <= rect.fBottom;
234}
235static inline void add_quad_segment(const SkPoint pts[3],
236 SegmentArray* segments,
237 SkRect* devBounds) {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000238 if (pts[0].distanceToSqd(pts[1]) < kCloseSqd || pts[1].distanceToSqd(pts[2]) < kCloseSqd) {
239 if (pts[0] != pts[2]) {
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000240 add_line_to_segment(pts[2], segments, devBounds);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000241 }
242 } else {
243 segments->push_back();
244 segments->back().fType = Segment::kQuad;
245 segments->back().fPts[0] = pts[1];
246 segments->back().fPts[1] = pts[2];
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000247 SkASSERT(contains_inclusive(*devBounds, pts[0]));
248 devBounds->growToInclude(pts + 1, 2);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000249 }
250}
251
252static inline void add_cubic_segments(const SkPoint pts[4],
253 SkPath::Direction dir,
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000254 SegmentArray* segments,
255 SkRect* devBounds) {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000256 SkSTArray<15, SkPoint, true> quads;
257 GrPathUtils::convertCubicToQuads(pts, SK_Scalar1, true, dir, &quads);
258 int count = quads.count();
259 for (int q = 0; q < count; q += 3) {
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000260 add_quad_segment(&quads[q], segments, devBounds);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000261 }
262}
263
264static bool get_segments(const SkPath& path,
265 const SkMatrix& m,
266 SegmentArray* segments,
267 SkPoint* fanPt,
268 int* vCount,
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000269 int* iCount,
270 SkRect* devBounds) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000271 SkPath::Iter iter(path, true);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000272 // This renderer over-emphasizes very thin path regions. We use the distance
bsalomon@google.com5cc90d12012-01-17 16:28:34 +0000273 // to the path from the sample to compute coverage. Every pixel intersected
274 // 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 +0000275 // notice that the sample may be close to a very thin area of the path and
bsalomon@google.com5cc90d12012-01-17 16:28:34 +0000276 // thus should be very light. This is particularly egregious for degenerate
277 // line paths. We detect paths that are very close to a line (zero area) and
278 // draw nothing.
bsalomon@google.com9732f622012-01-31 15:19:21 +0000279 DegenerateTestData degenerateData;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000280 SkPath::Direction dir;
281 // get_direction can fail for some degenerate paths.
282 if (!get_direction(path, m, &dir)) {
283 return false;
284 }
bsalomon@google.com9732f622012-01-31 15:19:21 +0000285
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000286 for (;;) {
287 GrPoint pts[4];
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000288 SkPath::Verb verb = iter.next(pts);
289 switch (verb) {
290 case SkPath::kMove_Verb:
bsalomon@google.com1a38d552012-03-15 14:40:46 +0000291 m.mapPoints(pts, 1);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000292 update_degenerate_test(&degenerateData, pts[0]);
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000293 devBounds->set(pts->fX, pts->fY, pts->fX, pts->fY);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000294 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000295 case SkPath::kLine_Verb: {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000296 m.mapPoints(&pts[1], 1);
bsalomon@google.com1a38d552012-03-15 14:40:46 +0000297 update_degenerate_test(&degenerateData, pts[1]);
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000298 add_line_to_segment(pts[1], segments, devBounds);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000299 break;
300 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000301 case SkPath::kQuad_Verb:
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000302 m.mapPoints(pts, 3);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000303 update_degenerate_test(&degenerateData, pts[1]);
304 update_degenerate_test(&degenerateData, pts[2]);
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000305 add_quad_segment(pts, segments, devBounds);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000306 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000307 case SkPath::kCubic_Verb: {
bsalomon@google.com1a38d552012-03-15 14:40:46 +0000308 m.mapPoints(pts, 4);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000309 update_degenerate_test(&degenerateData, pts[1]);
310 update_degenerate_test(&degenerateData, pts[2]);
311 update_degenerate_test(&degenerateData, pts[3]);
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000312 add_cubic_segments(pts, dir, segments, devBounds);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000313 break;
314 };
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000315 case SkPath::kDone_Verb:
bsalomon@google.com9732f622012-01-31 15:19:21 +0000316 if (degenerateData.isDegenerate()) {
317 return false;
318 } else {
bsalomon@google.com278dc692012-02-15 16:52:51 +0000319 compute_vectors(segments, fanPt, dir, vCount, iCount);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000320 return true;
321 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000322 default:
323 break;
324 }
325 }
326}
327
328struct QuadVertex {
329 GrPoint fPos;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000330 GrPoint fUV;
bsalomon@google.com81712882012-11-01 17:12:34 +0000331 SkScalar fD0;
332 SkScalar fD1;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000333};
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000334
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000335struct Draw {
336 Draw() : fVertexCnt(0), fIndexCnt(0) {}
337 int fVertexCnt;
338 int fIndexCnt;
339};
340
341typedef SkTArray<Draw, true> DrawArray;
342
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000343static void create_vertices(const SegmentArray& segments,
344 const SkPoint& fanPt,
345 DrawArray* draws,
346 QuadVertex* verts,
347 uint16_t* idxs) {
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000348 Draw* draw = &draws->push_back();
349 // alias just to make vert/index assignments easier to read.
350 int* v = &draw->fVertexCnt;
351 int* i = &draw->fIndexCnt;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000352
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000353 int count = segments.count();
354 for (int a = 0; a < count; ++a) {
355 const Segment& sega = segments[a];
356 int b = (a + 1) % count;
357 const Segment& segb = segments[b];
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000358
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000359 // Check whether adding the verts for this segment to the current draw would cause index
360 // values to overflow.
361 int vCount = 4;
362 if (Segment::kLine == segb.fType) {
363 vCount += 5;
364 } else {
365 vCount += 6;
366 }
367 if (draw->fVertexCnt + vCount > (1 << 16)) {
368 verts += *v;
369 idxs += *i;
370 draw = &draws->push_back();
371 v = &draw->fVertexCnt;
372 i = &draw->fIndexCnt;
373 }
374
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000375 // FIXME: These tris are inset in the 1 unit arc around the corner
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000376 verts[*v + 0].fPos = sega.endPt();
377 verts[*v + 1].fPos = verts[*v + 0].fPos + sega.endNorm();
378 verts[*v + 2].fPos = verts[*v + 0].fPos + segb.fMid;
379 verts[*v + 3].fPos = verts[*v + 0].fPos + segb.fNorms[0];
380 verts[*v + 0].fUV.set(0,0);
381 verts[*v + 1].fUV.set(0,-SK_Scalar1);
382 verts[*v + 2].fUV.set(0,-SK_Scalar1);
383 verts[*v + 3].fUV.set(0,-SK_Scalar1);
384 verts[*v + 0].fD0 = verts[*v + 0].fD1 = -SK_Scalar1;
385 verts[*v + 1].fD0 = verts[*v + 1].fD1 = -SK_Scalar1;
386 verts[*v + 2].fD0 = verts[*v + 2].fD1 = -SK_Scalar1;
387 verts[*v + 3].fD0 = verts[*v + 3].fD1 = -SK_Scalar1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000388
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000389 idxs[*i + 0] = *v + 0;
390 idxs[*i + 1] = *v + 2;
391 idxs[*i + 2] = *v + 1;
392 idxs[*i + 3] = *v + 0;
393 idxs[*i + 4] = *v + 3;
394 idxs[*i + 5] = *v + 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000395
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000396 *v += 4;
397 *i += 6;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000398
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000399 if (Segment::kLine == segb.fType) {
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000400 verts[*v + 0].fPos = fanPt;
401 verts[*v + 1].fPos = sega.endPt();
402 verts[*v + 2].fPos = segb.fPts[0];
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000403
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000404 verts[*v + 3].fPos = verts[*v + 1].fPos + segb.fNorms[0];
405 verts[*v + 4].fPos = verts[*v + 2].fPos + segb.fNorms[0];
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000406
407 // we draw the line edge as a degenerate quad (u is 0, v is the
408 // signed distance to the edge)
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000409 SkScalar dist = fanPt.distanceToLineBetween(verts[*v + 1].fPos,
410 verts[*v + 2].fPos);
411 verts[*v + 0].fUV.set(0, dist);
412 verts[*v + 1].fUV.set(0, 0);
413 verts[*v + 2].fUV.set(0, 0);
414 verts[*v + 3].fUV.set(0, -SK_Scalar1);
415 verts[*v + 4].fUV.set(0, -SK_Scalar1);
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000416
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000417 verts[*v + 0].fD0 = verts[*v + 0].fD1 = -SK_Scalar1;
418 verts[*v + 1].fD0 = verts[*v + 1].fD1 = -SK_Scalar1;
419 verts[*v + 2].fD0 = verts[*v + 2].fD1 = -SK_Scalar1;
420 verts[*v + 3].fD0 = verts[*v + 3].fD1 = -SK_Scalar1;
421 verts[*v + 4].fD0 = verts[*v + 4].fD1 = -SK_Scalar1;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000422
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000423 idxs[*i + 0] = *v + 0;
424 idxs[*i + 1] = *v + 2;
425 idxs[*i + 2] = *v + 1;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000426
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000427 idxs[*i + 3] = *v + 3;
428 idxs[*i + 4] = *v + 1;
429 idxs[*i + 5] = *v + 2;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000430
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000431 idxs[*i + 6] = *v + 4;
432 idxs[*i + 7] = *v + 3;
433 idxs[*i + 8] = *v + 2;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000434
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000435 *v += 5;
436 *i += 9;
bsalomon@google.com06809612012-01-21 15:03:39 +0000437 } else {
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000438 GrPoint qpts[] = {sega.endPt(), segb.fPts[0], segb.fPts[1]};
bsalomon@google.com495e2102012-01-21 14:48:36 +0000439
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000440 GrVec midVec = segb.fNorms[0] + segb.fNorms[1];
441 midVec.normalize();
bsalomon@google.com06809612012-01-21 15:03:39 +0000442
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000443 verts[*v + 0].fPos = fanPt;
444 verts[*v + 1].fPos = qpts[0];
445 verts[*v + 2].fPos = qpts[2];
446 verts[*v + 3].fPos = qpts[0] + segb.fNorms[0];
447 verts[*v + 4].fPos = qpts[2] + segb.fNorms[1];
448 verts[*v + 5].fPos = qpts[1] + midVec;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000449
bsalomon@google.com81712882012-11-01 17:12:34 +0000450 SkScalar c = segb.fNorms[0].dot(qpts[0]);
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000451 verts[*v + 0].fD0 = -segb.fNorms[0].dot(fanPt) + c;
452 verts[*v + 1].fD0 = 0.f;
453 verts[*v + 2].fD0 = -segb.fNorms[0].dot(qpts[2]) + c;
454 verts[*v + 3].fD0 = -SK_ScalarMax/100;
455 verts[*v + 4].fD0 = -SK_ScalarMax/100;
456 verts[*v + 5].fD0 = -SK_ScalarMax/100;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000457
458 c = segb.fNorms[1].dot(qpts[2]);
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000459 verts[*v + 0].fD1 = -segb.fNorms[1].dot(fanPt) + c;
460 verts[*v + 1].fD1 = -segb.fNorms[1].dot(qpts[0]) + c;
461 verts[*v + 2].fD1 = 0.f;
462 verts[*v + 3].fD1 = -SK_ScalarMax/100;
463 verts[*v + 4].fD1 = -SK_ScalarMax/100;
464 verts[*v + 5].fD1 = -SK_ScalarMax/100;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000465
bsalomon@google.com19713172012-03-15 13:51:08 +0000466 GrPathUtils::QuadUVMatrix toUV(qpts);
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000467 toUV.apply<6, sizeof(QuadVertex), sizeof(GrPoint)>(verts + *v);
bsalomon@google.com06809612012-01-21 15:03:39 +0000468
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000469 idxs[*i + 0] = *v + 3;
470 idxs[*i + 1] = *v + 1;
471 idxs[*i + 2] = *v + 2;
472 idxs[*i + 3] = *v + 4;
473 idxs[*i + 4] = *v + 3;
474 idxs[*i + 5] = *v + 2;
bsalomon@google.com06809612012-01-21 15:03:39 +0000475
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000476 idxs[*i + 6] = *v + 5;
477 idxs[*i + 7] = *v + 3;
478 idxs[*i + 8] = *v + 4;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000479
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000480 idxs[*i + 9] = *v + 0;
481 idxs[*i + 10] = *v + 2;
482 idxs[*i + 11] = *v + 1;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000483
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000484 *v += 6;
485 *i += 12;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000486 }
487 }
488}
489
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000490///////////////////////////////////////////////////////////////////////////////
491
492/*
493 * Quadratic specified by 0=u^2-v canonical coords. u and v are the first
494 * two components of the vertex attribute. Coverage is based on signed
495 * distance with negative being inside, positive outside. The edge is specified in
496 * window space (y-down). If either the third or fourth component of the interpolated
497 * 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 +0000498 * attempt to trim to a portion of the infinite quad.
499 * Requires shader derivative instruction support.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000500 */
501
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000502class QuadEdgeEffect : public GrVertexEffect {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000503public:
504
505 static GrEffectRef* Create() {
bsalomon@google.comd42aca32013-04-23 15:37:27 +0000506 GR_CREATE_STATIC_EFFECT(gQuadEdgeEffect, QuadEdgeEffect, ());
507 gQuadEdgeEffect->ref();
508 return gQuadEdgeEffect;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000509 }
510
511 virtual ~QuadEdgeEffect() {}
512
513 static const char* Name() { return "QuadEdge"; }
514
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000515 virtual void getConstantColorComponents(GrColor* color,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000516 uint32_t* validFlags) const SK_OVERRIDE {
517 *validFlags = 0;
518 }
519
520 virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE {
521 return GrTBackendEffectFactory<QuadEdgeEffect>::getInstance();
522 }
523
524 class GLEffect : public GrGLEffect {
525 public:
526 GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&)
527 : INHERITED (factory) {}
528
529 virtual void emitCode(GrGLShaderBuilder* builder,
530 const GrDrawEffect& drawEffect,
531 EffectKey key,
532 const char* outputColor,
533 const char* inputColor,
534 const TextureSamplerArray& samplers) SK_OVERRIDE {
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000535 GrGLShaderBuilder::VertexBuilder* vertexBuilder = builder->getVertexBuilder();
536 SkASSERT(NULL != vertexBuilder);
537
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000538 const char *vsName, *fsName;
539 const SkString* attrName =
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000540 vertexBuilder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000541 builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n");
542
543 SkAssertResult(builder->enableFeature(
544 GrGLShaderBuilder::kStandardDerivatives_GLSLFeature));
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000545 vertexBuilder->addVarying(kVec4f_GrSLType, "QuadEdge", &vsName, &fsName);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000546
547 // keep the derivative instructions outside the conditional
548 builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName);
549 builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName);
550 builder->fsCodeAppendf("\t\tif (%s.z > 0.0 && %s.w > 0.0) {\n", fsName, fsName);
551 // today we know z and w are in device space. We could use derivatives
552 builder->fsCodeAppendf("\t\t\tedgeAlpha = min(min(%s.z, %s.w) + 0.5, 1.0);\n", fsName,
553 fsName);
554 builder->fsCodeAppendf ("\t\t} else {\n");
555 builder->fsCodeAppendf("\t\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n"
556 "\t\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n",
557 fsName, fsName);
558 builder->fsCodeAppendf("\t\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName,
559 fsName);
560 builder->fsCodeAppendf("\t\t\tedgeAlpha = "
561 "clamp(0.5 - edgeAlpha / length(gF), 0.0, 1.0);\n\t\t}\n");
562
563 SkString modulate;
bsalomon@google.com018f1792013-04-18 19:36:09 +0000564 GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000565 builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str());
566
commit-bot@chromium.org5a02cb42013-08-30 20:17:31 +0000567 vertexBuilder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000568 }
569
570 static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) {
571 return 0x0;
572 }
573
574 virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {}
575
576 private:
577 typedef GrGLEffect INHERITED;
578 };
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000579
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000580private:
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000581 QuadEdgeEffect() {
582 this->addVertexAttrib(kVec4f_GrSLType);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000583 }
584
585 virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE {
586 return true;
587 }
588
589 GR_DECLARE_EFFECT_TEST;
590
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +0000591 typedef GrVertexEffect INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000592};
593
594GR_DEFINE_EFFECT_TEST(QuadEdgeEffect);
595
commit-bot@chromium.orge0e7cfe2013-09-09 20:09:12 +0000596GrEffectRef* QuadEdgeEffect::TestCreate(SkRandom* random,
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000597 GrContext*,
598 const GrDrawTargetCaps& caps,
599 GrTexture*[]) {
600 // Doesn't work without derivative instructions.
601 return caps.shaderDerivativeSupport() ? QuadEdgeEffect::Create() : NULL;
602}
603
604///////////////////////////////////////////////////////////////////////////////
605
robertphillips@google.comfa662942012-05-17 12:20:22 +0000606bool GrAAConvexPathRenderer::canDrawPath(const SkPath& path,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000607 const SkStrokeRec& stroke,
robertphillips@google.comfa662942012-05-17 12:20:22 +0000608 const GrDrawTarget* target,
609 bool antiAlias) const {
bsalomon@google.combcce8922013-03-25 15:38:39 +0000610 return (target->caps()->shaderDerivativeSupport() && antiAlias &&
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000611 stroke.isFillStyle() && !path.isInverseFillType() && path.isConvex());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000612}
613
robertphillips@google.com42903302013-04-20 12:26:07 +0000614namespace {
615
616// position + edge
617extern const GrVertexAttrib gPathAttribs[] = {
618 {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding},
619 {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding}
620};
621
622};
623
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000624bool GrAAConvexPathRenderer::onDrawPath(const SkPath& origPath,
sugoi@google.com5f74cf82012-12-17 21:16:45 +0000625 const SkStrokeRec&,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000626 GrDrawTarget* target,
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000627 bool antiAlias) {
628
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000629 const SkPath* path = &origPath;
630 if (path->isEmpty()) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000631 return true;
632 }
bsalomon@google.com4647f902013-03-26 14:45:27 +0000633
bsalomon@google.com137f1342013-05-29 21:27:53 +0000634 SkMatrix viewMatrix = target->getDrawState().getViewMatrix();
635 GrDrawTarget::AutoStateRestore asr;
636 if (!asr.setIdentity(target, GrDrawTarget::kPreserve_ASRInit)) {
bsalomon@google.come3d32162012-07-20 13:37:06 +0000637 return false;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000638 }
bsalomon@google.com137f1342013-05-29 21:27:53 +0000639 GrDrawState* drawState = target->drawState();
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000640
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000641 // We use the fact that SkPath::transform path does subdivision based on
642 // perspective. Otherwise, we apply the view matrix when copying to the
643 // segment representation.
644 SkPath tmpPath;
bsalomon@google.com137f1342013-05-29 21:27:53 +0000645 if (viewMatrix.hasPerspective()) {
646 origPath.transform(viewMatrix, &tmpPath);
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000647 path = &tmpPath;
bsalomon@google.com137f1342013-05-29 21:27:53 +0000648 viewMatrix = SkMatrix::I();
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000649 }
650
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000651 QuadVertex *verts;
652 uint16_t* idxs;
653
bsalomon@google.com06809612012-01-21 15:03:39 +0000654 int vCount;
655 int iCount;
bsalomon@google.com68a5b262012-03-05 18:24:07 +0000656 enum {
657 kPreallocSegmentCnt = 512 / sizeof(Segment),
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000658 kPreallocDrawCnt = 4,
bsalomon@google.com68a5b262012-03-05 18:24:07 +0000659 };
660 SkSTArray<kPreallocSegmentCnt, Segment, true> segments;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000661 SkPoint fanPt;
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000662
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000663 // We can't simply use the path bounds because we may degenerate cubics to quads which produces
664 // new control points outside the original convex hull.
665 SkRect devBounds;
666 if (!get_segments(*path, viewMatrix, &segments, &fanPt, &vCount, &iCount, &devBounds)) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000667 return false;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000668 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000669
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000670 // Our computed verts should all be within one pixel of the segment control points.
671 devBounds.outset(SK_Scalar1, SK_Scalar1);
672
robertphillips@google.com42903302013-04-20 12:26:07 +0000673 drawState->setVertexAttribs<gPathAttribs>(SK_ARRAY_COUNT(gPathAttribs));
bsalomon@google.com4647f902013-03-26 14:45:27 +0000674
bsalomon@google.com4647f902013-03-26 14:45:27 +0000675 static const int kEdgeAttrIndex = 1;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000676 GrEffectRef* quadEffect = QuadEdgeEffect::Create();
bsalomon@google.comeb6879f2013-06-13 19:34:18 +0000677 drawState->addCoverageEffect(quadEffect, kEdgeAttrIndex)->unref();
bsalomon@google.com4647f902013-03-26 14:45:27 +0000678
jvanverth@google.comb75b0a02013-02-05 20:33:30 +0000679 GrDrawTarget::AutoReleaseGeometry arg(target, vCount, iCount);
bsalomon@google.comb3729422012-03-07 19:13:28 +0000680 if (!arg.succeeded()) {
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000681 return false;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000682 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000683 SkASSERT(sizeof(QuadVertex) == drawState->getVertexSize());
bsalomon@google.comb3729422012-03-07 19:13:28 +0000684 verts = reinterpret_cast<QuadVertex*>(arg.vertices());
685 idxs = reinterpret_cast<uint16_t*>(arg.indices());
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000686
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000687 SkSTArray<kPreallocDrawCnt, Draw, true> draws;
688 create_vertices(segments, fanPt, &draws, verts, idxs);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000689
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000690 // Check devBounds
commit-bot@chromium.org515dcd32013-08-28 14:17:03 +0000691#ifdef SK_DEBUG
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000692 SkRect tolDevBounds = devBounds;
693 tolDevBounds.outset(SK_Scalar1 / 10000, SK_Scalar1 / 10000);
694 SkRect actualBounds;
695 actualBounds.set(verts[0].fPos, verts[1].fPos);
696 for (int i = 2; i < vCount; ++i) {
697 actualBounds.growToInclude(verts[i].fPos.fX, verts[i].fPos.fY);
698 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000699 SkASSERT(tolDevBounds.contains(actualBounds));
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000700#endif
701
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000702 int vOffset = 0;
703 for (int i = 0; i < draws.count(); ++i) {
704 const Draw& draw = draws[i];
705 target->drawIndexed(kTriangles_GrPrimitiveType,
706 vOffset, // start vertex
707 0, // start index
708 draw.fVertexCnt,
bsalomon@google.com1dd9baa2013-05-20 16:49:06 +0000709 draw.fIndexCnt,
710 &devBounds);
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000711 vOffset += draw.fVertexCnt;
712 }
bsalomon@google.coma8347462012-10-08 18:59:39 +0000713
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000714 return true;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000715}