blob: af32a14bd5b645fe662f76ade5fca7f9eaec4e9e [file] [log] [blame]
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +00001/*
2 * Copyright 2012 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "GrAAConvexPathRenderer.h"
9
robertphillipse16dfdb2015-05-08 04:46:51 -070010#include "GrAAConvexTessellator.h"
bsalomoneb1cb5c2015-05-22 08:01:09 -070011#include "GrCaps.h"
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000012#include "GrContext.h"
robertphillipse16dfdb2015-05-08 04:46:51 -070013#include "GrDefaultGeoProcFactory.h"
Brian Salomon5ec9def2016-12-20 15:34:05 -050014#include "GrDrawOpTest.h"
joshualitteb2a6762014-12-04 11:35:33 -080015#include "GrGeometryProcessor.h"
Brian Salomon742e31d2016-12-07 17:06:19 -050016#include "GrOpFlushState.h"
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000017#include "GrPathUtils.h"
Brian Salomondad29232016-12-01 16:40:24 -050018#include "GrProcessor.h"
Brian Salomon10978a62017-06-15 16:21:49 -040019#include "GrSimpleMeshDrawOpHelper.h"
egdanielaf18a092015-01-05 10:22:28 -080020#include "SkGeometry.h"
reed026beb52015-06-10 14:23:15 -070021#include "SkPathPriv.h"
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000022#include "SkString.h"
commit-bot@chromium.org933e65d2014-03-20 20:00:24 +000023#include "SkTraceEvent.h"
egdaniel7ea439b2015-12-03 09:20:44 -080024#include "glsl/GrGLSLFragmentShaderBuilder.h"
egdaniele659a582015-11-13 09:55:43 -080025#include "glsl/GrGLSLGeometryProcessor.h"
egdaniel018fb622015-10-28 07:26:40 -070026#include "glsl/GrGLSLProgramDataManager.h"
egdaniel7ea439b2015-12-03 09:20:44 -080027#include "glsl/GrGLSLUniformHandler.h"
egdaniel0eafe792015-11-20 14:01:22 -080028#include "glsl/GrGLSLVarying.h"
egdaniel7ea439b2015-12-03 09:20:44 -080029#include "glsl/GrGLSLVertexShaderBuilder.h"
Brian Salomon89527432016-12-16 09:52:16 -050030#include "ops/GrMeshDrawOp.h"
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000031
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000032GrAAConvexPathRenderer::GrAAConvexPathRenderer() {
33}
34
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000035struct Segment {
36 enum {
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000037 // These enum values are assumed in member functions below.
38 kLine = 0,
39 kQuad = 1,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000040 } fType;
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000041
bsalomon@google.com9aed1142012-01-30 14:28:39 +000042 // line uses one pt, quad uses 2 pts
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000043 SkPoint fPts[2];
bsalomon@google.com9aed1142012-01-30 14:28:39 +000044 // normal to edge ending at each pt
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000045 SkVector fNorms[2];
bsalomon@google.com9aed1142012-01-30 14:28:39 +000046 // is the corner where the previous segment meets this segment
47 // sharp. If so, fMid is a normalized bisector facing outward.
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000048 SkVector fMid;
bsalomon@google.com9aed1142012-01-30 14:28:39 +000049
50 int countPoints() {
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000051 GR_STATIC_ASSERT(0 == kLine && 1 == kQuad);
52 return fType + 1;
bsalomon@google.com9aed1142012-01-30 14:28:39 +000053 }
54 const SkPoint& endPt() const {
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000055 GR_STATIC_ASSERT(0 == kLine && 1 == kQuad);
56 return fPts[fType];
Mike Kleinfc6c37b2016-09-27 09:34:10 -040057 }
bsalomon@google.com9aed1142012-01-30 14:28:39 +000058 const SkPoint& endNorm() const {
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000059 GR_STATIC_ASSERT(0 == kLine && 1 == kQuad);
60 return fNorms[fType];
Mike Kleinfc6c37b2016-09-27 09:34:10 -040061 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000062};
63
64typedef SkTArray<Segment, true> SegmentArray;
65
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +000066static void center_of_mass(const SegmentArray& segments, SkPoint* c) {
bsalomon@google.com81712882012-11-01 17:12:34 +000067 SkScalar area = 0;
vandebo@chromium.org6390c722012-03-28 21:03:22 +000068 SkPoint center = {0, 0};
bsalomon@google.com9aed1142012-01-30 14:28:39 +000069 int count = segments.count();
vandebo@chromium.org6390c722012-03-28 21:03:22 +000070 SkPoint p0 = {0, 0};
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000071 if (count > 2) {
72 // We translate the polygon so that the first point is at the origin.
73 // This avoids some precision issues with small area polygons far away
74 // from the origin.
75 p0 = segments[0].endPt();
76 SkPoint pi;
77 SkPoint pj;
bsalomon@google.coma51ab842012-07-10 19:53:34 +000078 // the first and last iteration of the below loop would compute
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000079 // zeros since the starting / ending point is (0,0). So instead we start
80 // at i=1 and make the last iteration i=count-2.
81 pj = segments[1].endPt() - p0;
82 for (int i = 1; i < count - 1; ++i) {
83 pi = pj;
robertphillips44c31282015-09-03 12:58:48 -070084 pj = segments[i + 1].endPt() - p0;
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000085
robertphillips44c31282015-09-03 12:58:48 -070086 SkScalar t = SkPoint::CrossProduct(pi, pj);
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000087 area += t;
88 center.fX += (pi.fX + pj.fX) * t;
89 center.fY += (pi.fY + pj.fY) * t;
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000090 }
bsalomon@google.com9aed1142012-01-30 14:28:39 +000091 }
robertphillips44c31282015-09-03 12:58:48 -070092
bsalomon@google.com278dc692012-02-15 16:52:51 +000093 // If the poly has no area then we instead return the average of
94 // its points.
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000095 if (SkScalarNearlyZero(area)) {
bsalomon@google.com278dc692012-02-15 16:52:51 +000096 SkPoint avg;
97 avg.set(0, 0);
98 for (int i = 0; i < count; ++i) {
99 const SkPoint& pt = segments[i].endPt();
100 avg.fX += pt.fX;
101 avg.fY += pt.fY;
102 }
103 SkScalar denom = SK_Scalar1 / count;
104 avg.scale(denom);
105 *c = avg;
106 } else {
107 area *= 3;
reed80ea19c2015-05-12 10:37:34 -0700108 area = SkScalarInvert(area);
robertphillips44c31282015-09-03 12:58:48 -0700109 center.scale(area);
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +0000110 // undo the translate of p0 to the origin.
111 *c = center + p0;
bsalomon@google.com278dc692012-02-15 16:52:51 +0000112 }
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000113 SkASSERT(!SkScalarIsNaN(c->fX) && !SkScalarIsNaN(c->fY));
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000114}
115
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000116static void compute_vectors(SegmentArray* segments,
117 SkPoint* fanPt,
reed026beb52015-06-10 14:23:15 -0700118 SkPathPriv::FirstDirection dir,
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000119 int* vCount,
120 int* iCount) {
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000121 center_of_mass(*segments, fanPt);
122 int count = segments->count();
123
bsalomon@google.com278dc692012-02-15 16:52:51 +0000124 // Make the normals point towards the outside
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000125 SkPoint::Side normSide;
reed026beb52015-06-10 14:23:15 -0700126 if (dir == SkPathPriv::kCCW_FirstDirection) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000127 normSide = SkPoint::kRight_Side;
bsalomon@google.com278dc692012-02-15 16:52:51 +0000128 } else {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000129 normSide = SkPoint::kLeft_Side;
bsalomon@google.com278dc692012-02-15 16:52:51 +0000130 }
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000131
132 *vCount = 0;
133 *iCount = 0;
134 // compute normals at all points
135 for (int a = 0; a < count; ++a) {
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000136 Segment& sega = (*segments)[a];
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000137 int b = (a + 1) % count;
138 Segment& segb = (*segments)[b];
139
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000140 const SkPoint* prevPt = &sega.endPt();
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000141 int n = segb.countPoints();
142 for (int p = 0; p < n; ++p) {
143 segb.fNorms[p] = segb.fPts[p] - *prevPt;
144 segb.fNorms[p].normalize();
145 segb.fNorms[p].setOrthog(segb.fNorms[p], normSide);
146 prevPt = &segb.fPts[p];
147 }
148 if (Segment::kLine == segb.fType) {
149 *vCount += 5;
150 *iCount += 9;
151 } else {
152 *vCount += 6;
153 *iCount += 12;
154 }
155 }
156
157 // compute mid-vectors where segments meet. TODO: Detect shallow corners
158 // and leave out the wedges and close gaps by stitching segments together.
159 for (int a = 0; a < count; ++a) {
160 const Segment& sega = (*segments)[a];
161 int b = (a + 1) % count;
162 Segment& segb = (*segments)[b];
163 segb.fMid = segb.fNorms[0] + sega.endNorm();
164 segb.fMid.normalize();
165 // corner wedges
166 *vCount += 4;
167 *iCount += 6;
168 }
169}
170
bsalomon@google.com9732f622012-01-31 15:19:21 +0000171struct DegenerateTestData {
172 DegenerateTestData() { fStage = kInitial; }
173 bool isDegenerate() const { return kNonDegenerate != fStage; }
174 enum {
175 kInitial,
176 kPoint,
177 kLine,
178 kNonDegenerate
179 } fStage;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000180 SkPoint fFirstPoint;
181 SkVector fLineNormal;
bsalomon@google.com81712882012-11-01 17:12:34 +0000182 SkScalar fLineC;
bsalomon@google.com9732f622012-01-31 15:19:21 +0000183};
184
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000185static const SkScalar kClose = (SK_Scalar1 / 16);
Mike Reed8be952a2017-02-13 20:44:33 -0500186static const SkScalar kCloseSqd = kClose * kClose;
bsalomon@google.com9732f622012-01-31 15:19:21 +0000187
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000188static void update_degenerate_test(DegenerateTestData* data, const SkPoint& pt) {
bsalomon@google.com9732f622012-01-31 15:19:21 +0000189 switch (data->fStage) {
190 case DegenerateTestData::kInitial:
191 data->fFirstPoint = pt;
192 data->fStage = DegenerateTestData::kPoint;
193 break;
194 case DegenerateTestData::kPoint:
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000195 if (pt.distanceToSqd(data->fFirstPoint) > kCloseSqd) {
bsalomon@google.com9732f622012-01-31 15:19:21 +0000196 data->fLineNormal = pt - data->fFirstPoint;
197 data->fLineNormal.normalize();
198 data->fLineNormal.setOrthog(data->fLineNormal);
199 data->fLineC = -data->fLineNormal.dot(data->fFirstPoint);
200 data->fStage = DegenerateTestData::kLine;
201 }
202 break;
203 case DegenerateTestData::kLine:
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000204 if (SkScalarAbs(data->fLineNormal.dot(pt) + data->fLineC) > kClose) {
bsalomon@google.com9732f622012-01-31 15:19:21 +0000205 data->fStage = DegenerateTestData::kNonDegenerate;
206 }
207 case DegenerateTestData::kNonDegenerate:
208 break;
209 default:
commit-bot@chromium.org88cb22b2014-04-30 14:17:00 +0000210 SkFAIL("Unexpected degenerate test stage.");
bsalomon@google.com9732f622012-01-31 15:19:21 +0000211 }
212}
213
reed026beb52015-06-10 14:23:15 -0700214static inline bool get_direction(const SkPath& path, const SkMatrix& m,
215 SkPathPriv::FirstDirection* dir) {
216 if (!SkPathPriv::CheapComputeFirstDirection(path, dir)) {
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000217 return false;
218 }
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000219 // check whether m reverses the orientation
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000220 SkASSERT(!m.hasPerspective());
Mike Reed8be952a2017-02-13 20:44:33 -0500221 SkScalar det2x2 = m.get(SkMatrix::kMScaleX) * m.get(SkMatrix::kMScaleY) -
222 m.get(SkMatrix::kMSkewX) * m.get(SkMatrix::kMSkewY);
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000223 if (det2x2 < 0) {
reed026beb52015-06-10 14:23:15 -0700224 *dir = SkPathPriv::OppositeFirstDirection(*dir);
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000225 }
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000226 return true;
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000227}
228
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000229static inline void add_line_to_segment(const SkPoint& pt,
joshualitt27f398f2015-02-05 14:39:01 -0800230 SegmentArray* segments) {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000231 segments->push_back();
232 segments->back().fType = Segment::kLine;
233 segments->back().fPts[0] = pt;
234}
235
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000236static inline void add_quad_segment(const SkPoint pts[3],
joshualitt27f398f2015-02-05 14:39:01 -0800237 SegmentArray* segments) {
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]) {
joshualitt27f398f2015-02-05 14:39:01 -0800240 add_line_to_segment(pts[2], segments);
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];
247 }
248}
249
250static inline void add_cubic_segments(const SkPoint pts[4],
reed026beb52015-06-10 14:23:15 -0700251 SkPathPriv::FirstDirection dir,
joshualitt27f398f2015-02-05 14:39:01 -0800252 SegmentArray* segments) {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000253 SkSTArray<15, SkPoint, true> quads;
bsalomon18fab302016-02-16 08:00:05 -0800254 GrPathUtils::convertCubicToQuadsConstrainToTangents(pts, SK_Scalar1, dir, &quads);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000255 int count = quads.count();
256 for (int q = 0; q < count; q += 3) {
joshualitt27f398f2015-02-05 14:39:01 -0800257 add_quad_segment(&quads[q], segments);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000258 }
259}
260
261static bool get_segments(const SkPath& path,
262 const SkMatrix& m,
263 SegmentArray* segments,
264 SkPoint* fanPt,
265 int* vCount,
joshualitt27f398f2015-02-05 14:39:01 -0800266 int* iCount) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000267 SkPath::Iter iter(path, true);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000268 // This renderer over-emphasizes very thin path regions. We use the distance
bsalomon@google.com5cc90d12012-01-17 16:28:34 +0000269 // to the path from the sample to compute coverage. Every pixel intersected
270 // 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 +0000271 // notice that the sample may be close to a very thin area of the path and
bsalomon@google.com5cc90d12012-01-17 16:28:34 +0000272 // thus should be very light. This is particularly egregious for degenerate
273 // line paths. We detect paths that are very close to a line (zero area) and
274 // draw nothing.
bsalomon@google.com9732f622012-01-31 15:19:21 +0000275 DegenerateTestData degenerateData;
reed026beb52015-06-10 14:23:15 -0700276 SkPathPriv::FirstDirection dir;
bsalomon@google.coma51ab842012-07-10 19:53:34 +0000277 // get_direction can fail for some degenerate paths.
278 if (!get_direction(path, m, &dir)) {
279 return false;
280 }
bsalomon@google.com9732f622012-01-31 15:19:21 +0000281
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000282 for (;;) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000283 SkPoint pts[4];
Brian Salomon97042bf2017-02-28 11:21:28 -0500284 SkPath::Verb verb = iter.next(pts, true, true);
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000285 switch (verb) {
286 case SkPath::kMove_Verb:
bsalomon@google.com1a38d552012-03-15 14:40:46 +0000287 m.mapPoints(pts, 1);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000288 update_degenerate_test(&degenerateData, pts[0]);
289 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000290 case SkPath::kLine_Verb: {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000291 m.mapPoints(&pts[1], 1);
bsalomon@google.com1a38d552012-03-15 14:40:46 +0000292 update_degenerate_test(&degenerateData, pts[1]);
joshualitt27f398f2015-02-05 14:39:01 -0800293 add_line_to_segment(pts[1], segments);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000294 break;
295 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000296 case SkPath::kQuad_Verb:
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000297 m.mapPoints(pts, 3);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000298 update_degenerate_test(&degenerateData, pts[1]);
299 update_degenerate_test(&degenerateData, pts[2]);
joshualitt27f398f2015-02-05 14:39:01 -0800300 add_quad_segment(pts, segments);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000301 break;
egdanielaf18a092015-01-05 10:22:28 -0800302 case SkPath::kConic_Verb: {
303 m.mapPoints(pts, 3);
304 SkScalar weight = iter.conicWeight();
305 SkAutoConicToQuads converter;
306 const SkPoint* quadPts = converter.computeQuads(pts, weight, 0.5f);
307 for (int i = 0; i < converter.countQuads(); ++i) {
308 update_degenerate_test(&degenerateData, quadPts[2*i + 1]);
309 update_degenerate_test(&degenerateData, quadPts[2*i + 2]);
joshualitt27f398f2015-02-05 14:39:01 -0800310 add_quad_segment(quadPts + 2*i, segments);
egdanielaf18a092015-01-05 10:22:28 -0800311 }
312 break;
313 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000314 case SkPath::kCubic_Verb: {
bsalomon@google.com1a38d552012-03-15 14:40:46 +0000315 m.mapPoints(pts, 4);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000316 update_degenerate_test(&degenerateData, pts[1]);
317 update_degenerate_test(&degenerateData, pts[2]);
318 update_degenerate_test(&degenerateData, pts[3]);
joshualitt27f398f2015-02-05 14:39:01 -0800319 add_cubic_segments(pts, dir, segments);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000320 break;
321 };
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000322 case SkPath::kDone_Verb:
bsalomon@google.com9732f622012-01-31 15:19:21 +0000323 if (degenerateData.isDegenerate()) {
324 return false;
325 } else {
bsalomon@google.com278dc692012-02-15 16:52:51 +0000326 compute_vectors(segments, fanPt, dir, vCount, iCount);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000327 return true;
328 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000329 default:
330 break;
331 }
332 }
333}
334
335struct QuadVertex {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000336 SkPoint fPos;
Brian Salomon60fb0b22017-06-15 17:09:36 -0400337 GrColor fColor;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000338 SkPoint fUV;
bsalomon@google.com81712882012-11-01 17:12:34 +0000339 SkScalar fD0;
340 SkScalar fD1;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000341};
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000342
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000343struct Draw {
344 Draw() : fVertexCnt(0), fIndexCnt(0) {}
345 int fVertexCnt;
346 int fIndexCnt;
347};
348
349typedef SkTArray<Draw, true> DrawArray;
350
Brian Salomon60fb0b22017-06-15 17:09:36 -0400351static void create_vertices(const SegmentArray& segments,
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000352 const SkPoint& fanPt,
Brian Salomon60fb0b22017-06-15 17:09:36 -0400353 GrColor color,
354 DrawArray* draws,
355 QuadVertex* verts,
356 uint16_t* idxs) {
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000357 Draw* draw = &draws->push_back();
358 // alias just to make vert/index assignments easier to read.
359 int* v = &draw->fVertexCnt;
360 int* i = &draw->fIndexCnt;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000361
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000362 int count = segments.count();
363 for (int a = 0; a < count; ++a) {
364 const Segment& sega = segments[a];
365 int b = (a + 1) % count;
366 const Segment& segb = segments[b];
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000367
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000368 // Check whether adding the verts for this segment to the current draw would cause index
369 // values to overflow.
370 int vCount = 4;
371 if (Segment::kLine == segb.fType) {
372 vCount += 5;
373 } else {
374 vCount += 6;
375 }
376 if (draw->fVertexCnt + vCount > (1 << 16)) {
377 verts += *v;
378 idxs += *i;
379 draw = &draws->push_back();
380 v = &draw->fVertexCnt;
381 i = &draw->fIndexCnt;
382 }
383
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000384 // FIXME: These tris are inset in the 1 unit arc around the corner
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000385 verts[*v + 0].fPos = sega.endPt();
386 verts[*v + 1].fPos = verts[*v + 0].fPos + sega.endNorm();
387 verts[*v + 2].fPos = verts[*v + 0].fPos + segb.fMid;
388 verts[*v + 3].fPos = verts[*v + 0].fPos + segb.fNorms[0];
Brian Salomon60fb0b22017-06-15 17:09:36 -0400389 verts[*v + 0].fColor = color;
390 verts[*v + 1].fColor = color;
391 verts[*v + 2].fColor = color;
392 verts[*v + 3].fColor = color;
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000393 verts[*v + 0].fUV.set(0,0);
394 verts[*v + 1].fUV.set(0,-SK_Scalar1);
395 verts[*v + 2].fUV.set(0,-SK_Scalar1);
396 verts[*v + 3].fUV.set(0,-SK_Scalar1);
397 verts[*v + 0].fD0 = verts[*v + 0].fD1 = -SK_Scalar1;
398 verts[*v + 1].fD0 = verts[*v + 1].fD1 = -SK_Scalar1;
399 verts[*v + 2].fD0 = verts[*v + 2].fD1 = -SK_Scalar1;
400 verts[*v + 3].fD0 = verts[*v + 3].fD1 = -SK_Scalar1;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000401
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000402 idxs[*i + 0] = *v + 0;
403 idxs[*i + 1] = *v + 2;
404 idxs[*i + 2] = *v + 1;
405 idxs[*i + 3] = *v + 0;
406 idxs[*i + 4] = *v + 3;
407 idxs[*i + 5] = *v + 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000408
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000409 *v += 4;
410 *i += 6;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000411
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000412 if (Segment::kLine == segb.fType) {
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000413 verts[*v + 0].fPos = fanPt;
414 verts[*v + 1].fPos = sega.endPt();
415 verts[*v + 2].fPos = segb.fPts[0];
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000416
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000417 verts[*v + 3].fPos = verts[*v + 1].fPos + segb.fNorms[0];
418 verts[*v + 4].fPos = verts[*v + 2].fPos + segb.fNorms[0];
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000419
Brian Salomon60fb0b22017-06-15 17:09:36 -0400420 verts[*v + 0].fColor = color;
421 verts[*v + 1].fColor = color;
422 verts[*v + 2].fColor = color;
423 verts[*v + 3].fColor = color;
424 verts[*v + 4].fColor = color;
425
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000426 // we draw the line edge as a degenerate quad (u is 0, v is the
427 // signed distance to the edge)
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000428 SkScalar dist = fanPt.distanceToLineBetween(verts[*v + 1].fPos,
429 verts[*v + 2].fPos);
430 verts[*v + 0].fUV.set(0, dist);
431 verts[*v + 1].fUV.set(0, 0);
432 verts[*v + 2].fUV.set(0, 0);
433 verts[*v + 3].fUV.set(0, -SK_Scalar1);
434 verts[*v + 4].fUV.set(0, -SK_Scalar1);
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000435
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000436 verts[*v + 0].fD0 = verts[*v + 0].fD1 = -SK_Scalar1;
437 verts[*v + 1].fD0 = verts[*v + 1].fD1 = -SK_Scalar1;
438 verts[*v + 2].fD0 = verts[*v + 2].fD1 = -SK_Scalar1;
439 verts[*v + 3].fD0 = verts[*v + 3].fD1 = -SK_Scalar1;
440 verts[*v + 4].fD0 = verts[*v + 4].fD1 = -SK_Scalar1;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000441
cdalton35964822015-04-29 10:14:03 -0700442 idxs[*i + 0] = *v + 3;
443 idxs[*i + 1] = *v + 1;
444 idxs[*i + 2] = *v + 2;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000445
cdalton35964822015-04-29 10:14:03 -0700446 idxs[*i + 3] = *v + 4;
447 idxs[*i + 4] = *v + 3;
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000448 idxs[*i + 5] = *v + 2;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000449
cdalton35964822015-04-29 10:14:03 -0700450 *i += 6;
451
452 // Draw the interior fan if it exists.
453 // TODO: Detect and combine colinear segments. This will ensure we catch every case
454 // with no interior, and that the resulting shared edge uses the same endpoints.
455 if (count >= 3) {
456 idxs[*i + 0] = *v + 0;
457 idxs[*i + 1] = *v + 2;
458 idxs[*i + 2] = *v + 1;
459
460 *i += 3;
461 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000462
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000463 *v += 5;
bsalomon@google.com06809612012-01-21 15:03:39 +0000464 } else {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000465 SkPoint qpts[] = {sega.endPt(), segb.fPts[0], segb.fPts[1]};
bsalomon@google.com495e2102012-01-21 14:48:36 +0000466
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000467 SkVector midVec = segb.fNorms[0] + segb.fNorms[1];
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000468 midVec.normalize();
bsalomon@google.com06809612012-01-21 15:03:39 +0000469
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000470 verts[*v + 0].fPos = fanPt;
471 verts[*v + 1].fPos = qpts[0];
472 verts[*v + 2].fPos = qpts[2];
473 verts[*v + 3].fPos = qpts[0] + segb.fNorms[0];
474 verts[*v + 4].fPos = qpts[2] + segb.fNorms[1];
475 verts[*v + 5].fPos = qpts[1] + midVec;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000476
Brian Salomon60fb0b22017-06-15 17:09:36 -0400477 verts[*v + 0].fColor = color;
478 verts[*v + 1].fColor = color;
479 verts[*v + 2].fColor = color;
480 verts[*v + 3].fColor = color;
481 verts[*v + 4].fColor = color;
482 verts[*v + 5].fColor = color;
483
bsalomon@google.com81712882012-11-01 17:12:34 +0000484 SkScalar c = segb.fNorms[0].dot(qpts[0]);
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000485 verts[*v + 0].fD0 = -segb.fNorms[0].dot(fanPt) + c;
486 verts[*v + 1].fD0 = 0.f;
487 verts[*v + 2].fD0 = -segb.fNorms[0].dot(qpts[2]) + c;
488 verts[*v + 3].fD0 = -SK_ScalarMax/100;
489 verts[*v + 4].fD0 = -SK_ScalarMax/100;
490 verts[*v + 5].fD0 = -SK_ScalarMax/100;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000491
492 c = segb.fNorms[1].dot(qpts[2]);
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000493 verts[*v + 0].fD1 = -segb.fNorms[1].dot(fanPt) + c;
494 verts[*v + 1].fD1 = -segb.fNorms[1].dot(qpts[0]) + c;
495 verts[*v + 2].fD1 = 0.f;
496 verts[*v + 3].fD1 = -SK_ScalarMax/100;
497 verts[*v + 4].fD1 = -SK_ScalarMax/100;
498 verts[*v + 5].fD1 = -SK_ScalarMax/100;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000499
bsalomon@google.com19713172012-03-15 13:51:08 +0000500 GrPathUtils::QuadUVMatrix toUV(qpts);
Brian Salomon60fb0b22017-06-15 17:09:36 -0400501 toUV.apply<6, sizeof(QuadVertex), offsetof(QuadVertex, fUV)>(verts + *v);
bsalomon@google.com06809612012-01-21 15:03:39 +0000502
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000503 idxs[*i + 0] = *v + 3;
504 idxs[*i + 1] = *v + 1;
505 idxs[*i + 2] = *v + 2;
506 idxs[*i + 3] = *v + 4;
507 idxs[*i + 4] = *v + 3;
508 idxs[*i + 5] = *v + 2;
bsalomon@google.com06809612012-01-21 15:03:39 +0000509
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000510 idxs[*i + 6] = *v + 5;
511 idxs[*i + 7] = *v + 3;
512 idxs[*i + 8] = *v + 4;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000513
cdalton35964822015-04-29 10:14:03 -0700514 *i += 9;
515
516 // Draw the interior fan if it exists.
517 // TODO: Detect and combine colinear segments. This will ensure we catch every case
518 // with no interior, and that the resulting shared edge uses the same endpoints.
519 if (count >= 3) {
520 idxs[*i + 0] = *v + 0;
521 idxs[*i + 1] = *v + 2;
522 idxs[*i + 2] = *v + 1;
523
524 *i += 3;
525 }
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000526
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000527 *v += 6;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000528 }
529 }
530}
531
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000532///////////////////////////////////////////////////////////////////////////////
533
534/*
535 * Quadratic specified by 0=u^2-v canonical coords. u and v are the first
536 * two components of the vertex attribute. Coverage is based on signed
537 * distance with negative being inside, positive outside. The edge is specified in
538 * window space (y-down). If either the third or fourth component of the interpolated
539 * 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 +0000540 * attempt to trim to a portion of the infinite quad.
541 * Requires shader derivative instruction support.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000542 */
543
joshualitt249af152014-09-15 11:41:13 -0700544class QuadEdgeEffect : public GrGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000545public:
Brian Salomon60fb0b22017-06-15 17:09:36 -0400546 static sk_sp<GrGeometryProcessor> Make(const SkMatrix& localMatrix, bool usesLocalCoords) {
547 return sk_sp<GrGeometryProcessor>(new QuadEdgeEffect(localMatrix, usesLocalCoords));
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000548 }
549
Brian Salomond3b65972017-03-22 12:05:03 -0400550 ~QuadEdgeEffect() override {}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000551
mtklein36352bf2015-03-25 18:17:31 -0700552 const char* name() const override { return "QuadEdge"; }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000553
egdaniel57d3b032015-11-13 11:57:27 -0800554 class GLSLProcessor : public GrGLSLGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000555 public:
Brian Salomon60fb0b22017-06-15 17:09:36 -0400556 GLSLProcessor() {}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000557
mtklein36352bf2015-03-25 18:17:31 -0700558 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
joshualitt2dd1ae02014-12-03 06:24:10 -0800559 const QuadEdgeEffect& qe = args.fGP.cast<QuadEdgeEffect>();
egdaniel4ca2e602015-11-18 08:01:26 -0800560 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800561 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800562 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualitt2dd1ae02014-12-03 06:24:10 -0800563
joshualittabb52a12015-01-13 15:02:10 -0800564 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800565 varyingHandler->emitAttributes(qe);
joshualittabb52a12015-01-13 15:02:10 -0800566
egdaniel8dcdedc2015-11-11 06:27:20 -0800567 GrGLSLVertToFrag v(kVec4f_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800568 varyingHandler->addVarying("QuadEdge", &v);
Brian Salomon60fb0b22017-06-15 17:09:36 -0400569 vertBuilder->codeAppendf("%s = %s;", v.vsOut(), qe.fInQuadEdge->fName);
570
571 // Setup pass through color
572 varyingHandler->addPassThroughAttribute(qe.fInColor, args.fOutputColor);
joshualitt2dd1ae02014-12-03 06:24:10 -0800573
cdalton85285412016-02-18 12:37:07 -0800574 GrGLSLPPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt9b989322014-12-15 14:16:27 -0800575
joshualittabb52a12015-01-13 15:02:10 -0800576 // Setup position
Brian Salomon60fb0b22017-06-15 17:09:36 -0400577 this->setupPosition(vertBuilder, gpArgs, qe.fInPosition->fName);
joshualittabb52a12015-01-13 15:02:10 -0800578
579 // emit transforms
egdaniel7ea439b2015-12-03 09:20:44 -0800580 this->emitTransforms(vertBuilder,
egdaniel0eafe792015-11-20 14:01:22 -0800581 varyingHandler,
egdaniel7ea439b2015-12-03 09:20:44 -0800582 uniformHandler,
egdaniel4ca2e602015-11-18 08:01:26 -0800583 gpArgs->fPositionVar,
Brian Salomon60fb0b22017-06-15 17:09:36 -0400584 qe.fInPosition->fName,
585 qe.fLocalMatrix,
bsalomona624bf32016-09-20 09:12:47 -0700586 args.fFPCoordTransformHandler);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000587
egdaniel4ca2e602015-11-18 08:01:26 -0800588 fragBuilder->codeAppendf("float edgeAlpha;");
joshualitt30ba4362014-08-21 20:18:45 -0700589
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000590 // keep the derivative instructions outside the conditional
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400591 fragBuilder->codeAppendf("float2 duvdx = dFdx(%s.xy);", v.fsIn());
592 fragBuilder->codeAppendf("float2 duvdy = dFdy(%s.xy);", v.fsIn());
egdaniel4ca2e602015-11-18 08:01:26 -0800593 fragBuilder->codeAppendf("if (%s.z > 0.0 && %s.w > 0.0) {", v.fsIn(), v.fsIn());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000594 // today we know z and w are in device space. We could use derivatives
egdaniel4ca2e602015-11-18 08:01:26 -0800595 fragBuilder->codeAppendf("edgeAlpha = min(min(%s.z, %s.w) + 0.5, 1.0);", v.fsIn(),
596 v.fsIn());
597 fragBuilder->codeAppendf ("} else {");
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400598 fragBuilder->codeAppendf("float2 gF = float2(2.0*%s.x*duvdx.x - duvdx.y,"
egdaniel4ca2e602015-11-18 08:01:26 -0800599 " 2.0*%s.x*duvdy.x - duvdy.y);",
600 v.fsIn(), v.fsIn());
601 fragBuilder->codeAppendf("edgeAlpha = (%s.x*%s.x - %s.y);", v.fsIn(), v.fsIn(),
602 v.fsIn());
603 fragBuilder->codeAppendf("edgeAlpha = "
604 "clamp(0.5 - edgeAlpha / length(gF), 0.0, 1.0);}");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000605
Ethan Nicholas5af9ea32017-07-28 15:19:46 -0400606 fragBuilder->codeAppendf("%s = float4(edgeAlpha);", args.fOutputCoverage);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000607 }
608
joshualitt9b989322014-12-15 14:16:27 -0800609 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500610 const GrShaderCaps&,
joshualitt9b989322014-12-15 14:16:27 -0800611 GrProcessorKeyBuilder* b) {
joshualitte3ababe2015-05-15 07:56:07 -0700612 const QuadEdgeEffect& qee = gp.cast<QuadEdgeEffect>();
Brian Salomon60fb0b22017-06-15 17:09:36 -0400613 b->add32(SkToBool(qee.fUsesLocalCoords && qee.fLocalMatrix.hasPerspective()));
joshualitt9b989322014-12-15 14:16:27 -0800614 }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000615
egdaniel018fb622015-10-28 07:26:40 -0700616 void setData(const GrGLSLProgramDataManager& pdman,
bsalomona624bf32016-09-20 09:12:47 -0700617 const GrPrimitiveProcessor& gp,
618 FPCoordTransformIter&& transformIter) override {
joshualittb8c241a2015-05-19 08:23:30 -0700619 const QuadEdgeEffect& qe = gp.cast<QuadEdgeEffect>();
bsalomona624bf32016-09-20 09:12:47 -0700620 this->setTransformDataHelper(qe.fLocalMatrix, pdman, &transformIter);
joshualitte3ababe2015-05-15 07:56:07 -0700621 }
622
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000623 private:
egdaniele659a582015-11-13 09:55:43 -0800624 typedef GrGLSLGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000625 };
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000626
Brian Salomon94efbf52016-11-29 13:43:05 -0500627 void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override {
egdaniel57d3b032015-11-13 11:57:27 -0800628 GLSLProcessor::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800629 }
630
Brian Salomon94efbf52016-11-29 13:43:05 -0500631 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override {
egdaniel57d3b032015-11-13 11:57:27 -0800632 return new GLSLProcessor();
joshualitteb2a6762014-12-04 11:35:33 -0800633 }
634
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000635private:
Brian Salomon60fb0b22017-06-15 17:09:36 -0400636 QuadEdgeEffect(const SkMatrix& localMatrix, bool usesLocalCoords)
637 : fLocalMatrix(localMatrix), fUsesLocalCoords(usesLocalCoords) {
joshualitteb2a6762014-12-04 11:35:33 -0800638 this->initClassID<QuadEdgeEffect>();
bsalomon6cb807b2016-08-17 11:33:39 -0700639 fInPosition = &this->addVertexAttrib("inPosition", kVec2f_GrVertexAttribType);
Brian Salomon60fb0b22017-06-15 17:09:36 -0400640 fInColor = &this->addVertexAttrib("inColor", kVec4ub_GrVertexAttribType);
bsalomon6cb807b2016-08-17 11:33:39 -0700641 fInQuadEdge = &this->addVertexAttrib("inQuadEdge", kVec4f_GrVertexAttribType);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000642 }
643
joshualitt71c92602015-01-14 08:12:47 -0800644 const Attribute* fInPosition;
645 const Attribute* fInQuadEdge;
Brian Salomon60fb0b22017-06-15 17:09:36 -0400646 const Attribute* fInColor;
joshualitte3ababe2015-05-15 07:56:07 -0700647 SkMatrix fLocalMatrix;
joshualittb8c241a2015-05-19 08:23:30 -0700648 bool fUsesLocalCoords;
joshualitt249af152014-09-15 11:41:13 -0700649
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400650 GR_DECLARE_GEOMETRY_PROCESSOR_TEST
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000651
joshualitt2e3b3e32014-12-09 13:31:14 -0800652 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000653};
654
joshualittb0a8a372014-09-23 09:50:21 -0700655GR_DEFINE_GEOMETRY_PROCESSOR_TEST(QuadEdgeEffect);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000656
Hal Canary6f6961e2017-01-31 13:50:44 -0500657#if GR_TEST_UTILS
bungeman06ca8ec2016-06-09 08:01:03 -0700658sk_sp<GrGeometryProcessor> QuadEdgeEffect::TestCreate(GrProcessorTestData* d) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000659 // Doesn't work without derivative instructions.
Robert Phillips296b1cc2017-03-15 10:42:12 -0400660 return d->caps()->shaderCaps()->shaderDerivativeSupport()
Brian Salomon60fb0b22017-06-15 17:09:36 -0400661 ? QuadEdgeEffect::Make(GrTest::TestMatrix(d->fRandom), d->fRandom->nextBool())
Brian Salomon9ae32a22017-01-25 14:58:24 -0500662 : nullptr;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000663}
Hal Canary6f6961e2017-01-31 13:50:44 -0500664#endif
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000665
666///////////////////////////////////////////////////////////////////////////////
667
bsalomon0aff2fa2015-07-31 06:48:27 -0700668bool GrAAConvexPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Eric Karl5c779752017-05-08 12:02:07 -0700669 return (args.fCaps->shaderCaps()->shaderDerivativeSupport() &&
670 (GrAAType::kCoverage == args.fAAType) && args.fShape->style().isSimpleFill() &&
671 !args.fShape->inverseFilled() && args.fShape->knownToBeConvex());
robertphillips@google.comfa662942012-05-17 12:20:22 +0000672}
673
robertphillipse16dfdb2015-05-08 04:46:51 -0700674// extract the result vertices and indices from the GrAAConvexTessellator
Brian Salomon60fb0b22017-06-15 17:09:36 -0400675static void extract_lines_only_verts(const GrAAConvexTessellator& tess,
676 void* vertices,
677 size_t vertexStride,
678 GrColor color,
679 uint16_t* idxs,
680 bool tweakAlphaForCoverage) {
robertphillipse16dfdb2015-05-08 04:46:51 -0700681 intptr_t verts = reinterpret_cast<intptr_t>(vertices);
682
683 for (int i = 0; i < tess.numPts(); ++i) {
684 *((SkPoint*)((intptr_t)verts + i * vertexStride)) = tess.point(i);
685 }
686
687 // Make 'verts' point to the colors
688 verts += sizeof(SkPoint);
689 for (int i = 0; i < tess.numPts(); ++i) {
robertphillipse16dfdb2015-05-08 04:46:51 -0700690 if (tweakAlphaForCoverage) {
fmalitabd5d7e72015-06-26 07:18:24 -0700691 SkASSERT(SkScalarRoundToInt(255.0f * tess.coverage(i)) <= 255);
692 unsigned scale = SkScalarRoundToInt(255.0f * tess.coverage(i));
robertphillipse16dfdb2015-05-08 04:46:51 -0700693 GrColor scaledColor = (0xff == scale) ? color : SkAlphaMulQ(color, scale);
694 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = scaledColor;
695 } else {
696 *reinterpret_cast<GrColor*>(verts + i * vertexStride) = color;
halcanary9d524f22016-03-29 09:03:52 -0700697 *reinterpret_cast<float*>(verts + i * vertexStride + sizeof(GrColor)) =
fmalitabd5d7e72015-06-26 07:18:24 -0700698 tess.coverage(i);
robertphillipse16dfdb2015-05-08 04:46:51 -0700699 }
700 }
701
702 for (int i = 0; i < tess.numIndices(); ++i) {
703 idxs[i] = tess.index(i);
704 }
705}
706
Brian Salomon60fb0b22017-06-15 17:09:36 -0400707static sk_sp<GrGeometryProcessor> make_lines_only_gp(bool tweakAlphaForCoverage,
708 const SkMatrix& viewMatrix,
709 bool usesLocalCoords) {
joshualittdf0c5572015-08-03 11:35:28 -0700710 using namespace GrDefaultGeoProcFactory;
joshualitte494a582015-08-03 09:32:36 -0700711
joshualittdf0c5572015-08-03 11:35:28 -0700712 Coverage::Type coverageType;
Brian Salomon8c852be2017-01-04 10:44:42 -0500713 if (tweakAlphaForCoverage) {
joshualittdf0c5572015-08-03 11:35:28 -0700714 coverageType = Coverage::kSolid_Type;
715 } else {
716 coverageType = Coverage::kAttribute_Type;
717 }
Brian Salomon8c852be2017-01-04 10:44:42 -0500718 LocalCoords::Type localCoordsType =
719 usesLocalCoords ? LocalCoords::kUsePosition_Type : LocalCoords::kUnused_Type;
Brian Salomon3de0aee2017-01-29 09:34:17 -0500720 return MakeForDeviceSpace(Color::kPremulGrColorAttribute_Type, coverageType, localCoordsType,
721 viewMatrix);
robertphillipse16dfdb2015-05-08 04:46:51 -0700722}
723
Brian Salomon10978a62017-06-15 16:21:49 -0400724namespace {
725
726class AAConvexPathOp final : public GrMeshDrawOp {
727private:
728 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
729
joshualitt27f398f2015-02-05 14:39:01 -0800730public:
Brian Salomon25a88092016-12-01 09:36:50 -0500731 DEFINE_OP_CLASS_ID
Brian Salomon10978a62017-06-15 16:21:49 -0400732 static std::unique_ptr<GrDrawOp> Make(GrPaint&& paint, const SkMatrix& viewMatrix,
733 const SkPath& path,
734 const GrUserStencilSettings* stencilSettings) {
735 return Helper::FactoryHelper<AAConvexPathOp>(std::move(paint), viewMatrix, path,
736 stencilSettings);
737 }
738
739 AAConvexPathOp(const Helper::MakeArgs& helperArgs, GrColor color, const SkMatrix& viewMatrix,
740 const SkPath& path, const GrUserStencilSettings* stencilSettings)
Brian Salomon60fb0b22017-06-15 17:09:36 -0400741 : INHERITED(ClassID()), fHelper(helperArgs, GrAAType::kCoverage, stencilSettings) {
742 fPaths.emplace_back(PathData{viewMatrix, path, color});
Brian Salomon10978a62017-06-15 16:21:49 -0400743 this->setTransformedBounds(path.getBounds(), viewMatrix, HasAABloat::kYes, IsZeroArea::kNo);
744 fLinesOnly = SkPath::kLine_SegmentMask == path.getSegmentMasks();
bsalomonf1703092016-06-29 18:41:53 -0700745 }
joshualitt27f398f2015-02-05 14:39:01 -0800746
Brian Salomond0a0a652016-12-15 15:25:22 -0500747 const char* name() const override { return "AAConvexPathOp"; }
joshualitt27f398f2015-02-05 14:39:01 -0800748
Brian Salomon7c3e7182016-12-01 09:35:30 -0500749 SkString dumpInfo() const override {
750 SkString string;
Brian Salomon60fb0b22017-06-15 17:09:36 -0400751 string.appendf("Count: %d\n", fPaths.count());
Brian Salomon10978a62017-06-15 16:21:49 -0400752 string += fHelper.dumpInfo();
753 string += INHERITED::dumpInfo();
Brian Salomon7c3e7182016-12-01 09:35:30 -0500754 return string;
755 }
756
Brian Salomon10978a62017-06-15 16:21:49 -0400757 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
758
Brian Salomonf86d37b2017-06-16 10:04:34 -0400759 RequiresDstTexture finalize(const GrCaps& caps, const GrAppliedClip* clip) override {
Brian Salomon10978a62017-06-15 16:21:49 -0400760 return fHelper.xpRequiresDstTexture(caps, clip, GrProcessorAnalysisCoverage::kSingleChannel,
Brian Salomon60fb0b22017-06-15 17:09:36 -0400761 &fPaths.back().fColor);
Brian Salomon10978a62017-06-15 16:21:49 -0400762 }
763
bsalomone46f9fe2015-08-18 06:05:14 -0700764private:
Brian Salomon91326c32017-08-09 16:02:19 -0400765 void prepareLinesOnlyDraws(Target* target) {
Brian Salomon60fb0b22017-06-15 17:09:36 -0400766 // Setup GrGeometryProcessor
767 sk_sp<GrGeometryProcessor> gp(make_lines_only_gp(fHelper.compatibleWithAlphaAsCoverage(),
768 fPaths.back().fViewMatrix,
769 fHelper.usesLocalCoords()));
joshualittdf0c5572015-08-03 11:35:28 -0700770 if (!gp) {
771 SkDebugf("Could not create GrGeometryProcessor\n");
robertphillipse16dfdb2015-05-08 04:46:51 -0700772 return;
773 }
774
robertphillipse16dfdb2015-05-08 04:46:51 -0700775 size_t vertexStride = gp->getVertexStride();
776
Brian Salomon60fb0b22017-06-15 17:09:36 -0400777 SkASSERT(fHelper.compatibleWithAlphaAsCoverage()
778 ? vertexStride == sizeof(GrDefaultGeoProcFactory::PositionColorAttr)
779 : vertexStride ==
780 sizeof(GrDefaultGeoProcFactory::PositionColorCoverageAttr));
robertphillipse16dfdb2015-05-08 04:46:51 -0700781
782 GrAAConvexTessellator tess;
783
Brian Salomond0a0a652016-12-15 15:25:22 -0500784 int instanceCount = fPaths.count();
Brian Salomon10978a62017-06-15 16:21:49 -0400785 const GrPipeline* pipeline = fHelper.makePipeline(target);
robertphillipse16dfdb2015-05-08 04:46:51 -0700786 for (int i = 0; i < instanceCount; i++) {
787 tess.rewind();
788
Brian Salomond0a0a652016-12-15 15:25:22 -0500789 const PathData& args = fPaths[i];
robertphillipse16dfdb2015-05-08 04:46:51 -0700790
791 if (!tess.tessellate(args.fViewMatrix, args.fPath)) {
792 continue;
793 }
794
cdalton397536c2016-03-25 12:15:03 -0700795 const GrBuffer* vertexBuffer;
robertphillipse16dfdb2015-05-08 04:46:51 -0700796 int firstVertex;
797
bsalomon75398562015-08-17 12:55:38 -0700798 void* verts = target->makeVertexSpace(vertexStride, tess.numPts(), &vertexBuffer,
799 &firstVertex);
robertphillipse16dfdb2015-05-08 04:46:51 -0700800 if (!verts) {
801 SkDebugf("Could not allocate vertices\n");
802 return;
803 }
804
cdalton397536c2016-03-25 12:15:03 -0700805 const GrBuffer* indexBuffer;
robertphillipse16dfdb2015-05-08 04:46:51 -0700806 int firstIndex;
807
bsalomon75398562015-08-17 12:55:38 -0700808 uint16_t* idxs = target->makeIndexSpace(tess.numIndices(), &indexBuffer, &firstIndex);
robertphillipse16dfdb2015-05-08 04:46:51 -0700809 if (!idxs) {
810 SkDebugf("Could not allocate indices\n");
811 return;
812 }
813
Brian Salomon60fb0b22017-06-15 17:09:36 -0400814 extract_lines_only_verts(tess, verts, vertexStride, args.fColor, idxs,
815 fHelper.compatibleWithAlphaAsCoverage());
robertphillipse16dfdb2015-05-08 04:46:51 -0700816
Chris Dalton3809bab2017-06-13 10:55:06 -0600817 GrMesh mesh(GrPrimitiveType::kTriangles);
Chris Dalton114a3c02017-05-26 15:17:19 -0600818 mesh.setIndexed(indexBuffer, tess.numIndices(), firstIndex, 0, tess.numPts() - 1);
819 mesh.setVertexData(vertexBuffer, firstVertex);
Brian Salomon10978a62017-06-15 16:21:49 -0400820 target->draw(gp.get(), pipeline, mesh);
robertphillipse16dfdb2015-05-08 04:46:51 -0700821 }
joshualitt27f398f2015-02-05 14:39:01 -0800822 }
823
Brian Salomon91326c32017-08-09 16:02:19 -0400824 void onPrepareDraws(Target* target) override {
robertphillipse16dfdb2015-05-08 04:46:51 -0700825#ifndef SK_IGNORE_LINEONLY_AA_CONVEX_PATH_OPTS
Brian Salomon10978a62017-06-15 16:21:49 -0400826 if (fLinesOnly) {
bsalomon75398562015-08-17 12:55:38 -0700827 this->prepareLinesOnlyDraws(target);
robertphillipse16dfdb2015-05-08 04:46:51 -0700828 return;
829 }
830#endif
Brian Salomon10978a62017-06-15 16:21:49 -0400831 const GrPipeline* pipeline = fHelper.makePipeline(target);
Brian Salomond0a0a652016-12-15 15:25:22 -0500832 int instanceCount = fPaths.count();
joshualitt27f398f2015-02-05 14:39:01 -0800833
834 SkMatrix invert;
Brian Salomon10978a62017-06-15 16:21:49 -0400835 if (fHelper.usesLocalCoords() && !fPaths.back().fViewMatrix.invert(&invert)) {
joshualitt27f398f2015-02-05 14:39:01 -0800836 SkDebugf("Could not invert viewmatrix\n");
837 return;
838 }
839
840 // Setup GrGeometryProcessor
bungeman06ca8ec2016-06-09 08:01:03 -0700841 sk_sp<GrGeometryProcessor> quadProcessor(
Brian Salomon60fb0b22017-06-15 17:09:36 -0400842 QuadEdgeEffect::Make(invert, fHelper.usesLocalCoords()));
joshualitt27f398f2015-02-05 14:39:01 -0800843
joshualitt27f398f2015-02-05 14:39:01 -0800844 // TODO generate all segments for all paths and use one vertex buffer
845 for (int i = 0; i < instanceCount; i++) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500846 const PathData& args = fPaths[i];
joshualitt27f398f2015-02-05 14:39:01 -0800847
848 // We use the fact that SkPath::transform path does subdivision based on
849 // perspective. Otherwise, we apply the view matrix when copying to the
850 // segment representation.
851 const SkMatrix* viewMatrix = &args.fViewMatrix;
joshualitt144c3c82015-11-30 12:30:13 -0800852
853 // We avoid initializing the path unless we have to
854 const SkPath* pathPtr = &args.fPath;
855 SkTLazy<SkPath> tmpPath;
joshualitt27f398f2015-02-05 14:39:01 -0800856 if (viewMatrix->hasPerspective()) {
joshualitt144c3c82015-11-30 12:30:13 -0800857 SkPath* tmpPathPtr = tmpPath.init(*pathPtr);
858 tmpPathPtr->setIsVolatile(true);
859 tmpPathPtr->transform(*viewMatrix);
joshualitt27f398f2015-02-05 14:39:01 -0800860 viewMatrix = &SkMatrix::I();
joshualitt144c3c82015-11-30 12:30:13 -0800861 pathPtr = tmpPathPtr;
joshualitt27f398f2015-02-05 14:39:01 -0800862 }
863
864 int vertexCount;
865 int indexCount;
866 enum {
867 kPreallocSegmentCnt = 512 / sizeof(Segment),
868 kPreallocDrawCnt = 4,
869 };
870 SkSTArray<kPreallocSegmentCnt, Segment, true> segments;
871 SkPoint fanPt;
872
joshualitt144c3c82015-11-30 12:30:13 -0800873 if (!get_segments(*pathPtr, *viewMatrix, &segments, &fanPt, &vertexCount,
joshualitt27f398f2015-02-05 14:39:01 -0800874 &indexCount)) {
875 continue;
876 }
877
Chris Daltonff926502017-05-03 14:36:54 -0400878 const GrBuffer* vertexBuffer;
Chris Daltonbca46e22017-05-15 11:03:26 -0600879 int firstVertex;
880
joshualitt27f398f2015-02-05 14:39:01 -0800881 size_t vertexStride = quadProcessor->getVertexStride();
bsalomon75398562015-08-17 12:55:38 -0700882 QuadVertex* verts = reinterpret_cast<QuadVertex*>(target->makeVertexSpace(
Chris Daltonbca46e22017-05-15 11:03:26 -0600883 vertexStride, vertexCount, &vertexBuffer, &firstVertex));
bsalomone64eb572015-05-07 11:35:55 -0700884
885 if (!verts) {
joshualitt4b31de82015-03-05 14:33:41 -0800886 SkDebugf("Could not allocate vertices\n");
887 return;
888 }
889
cdalton397536c2016-03-25 12:15:03 -0700890 const GrBuffer* indexBuffer;
Chris Daltonbca46e22017-05-15 11:03:26 -0600891 int firstIndex;
892
893 uint16_t *idxs = target->makeIndexSpace(indexCount, &indexBuffer, &firstIndex);
robertphillipse40d3972015-05-07 09:51:43 -0700894 if (!idxs) {
joshualitt4b31de82015-03-05 14:33:41 -0800895 SkDebugf("Could not allocate indices\n");
896 return;
897 }
898
joshualitt27f398f2015-02-05 14:39:01 -0800899 SkSTArray<kPreallocDrawCnt, Draw, true> draws;
Brian Salomon60fb0b22017-06-15 17:09:36 -0400900 create_vertices(segments, fanPt, args.fColor, &draws, verts, idxs);
joshualitt27f398f2015-02-05 14:39:01 -0800901
Chris Dalton3809bab2017-06-13 10:55:06 -0600902 GrMesh mesh(GrPrimitiveType::kTriangles);
Chris Daltonbca46e22017-05-15 11:03:26 -0600903
robertphillips44c31282015-09-03 12:58:48 -0700904 for (int j = 0; j < draws.count(); ++j) {
905 const Draw& draw = draws[j];
Chris Dalton114a3c02017-05-26 15:17:19 -0600906 mesh.setIndexed(indexBuffer, draw.fIndexCnt, firstIndex, 0, draw.fVertexCnt - 1);
907 mesh.setVertexData(vertexBuffer, firstVertex);
Brian Salomon10978a62017-06-15 16:21:49 -0400908 target->draw(quadProcessor.get(), pipeline, mesh);
Chris Daltonbca46e22017-05-15 11:03:26 -0600909 firstIndex += draw.fIndexCnt;
910 firstVertex += draw.fVertexCnt;
joshualitt27f398f2015-02-05 14:39:01 -0800911 }
912 }
913 }
914
Brian Salomon25a88092016-12-01 09:36:50 -0500915 bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500916 AAConvexPathOp* that = t->cast<AAConvexPathOp>();
Brian Salomon10978a62017-06-15 16:21:49 -0400917 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
joshualitt8cab9a72015-07-16 09:13:50 -0700918 return false;
919 }
Brian Salomon10978a62017-06-15 16:21:49 -0400920 if (fHelper.usesLocalCoords() &&
921 !fPaths[0].fViewMatrix.cheapEqualTo(that->fPaths[0].fViewMatrix)) {
joshualitt27f398f2015-02-05 14:39:01 -0800922 return false;
923 }
924
Brian Salomon10978a62017-06-15 16:21:49 -0400925 if (fLinesOnly != that->fLinesOnly) {
robertphillipse16dfdb2015-05-08 04:46:51 -0700926 return false;
927 }
928
Brian Salomond0a0a652016-12-15 15:25:22 -0500929 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
bsalomon88cf17d2016-07-08 06:40:56 -0700930 this->joinBounds(*that);
joshualitt27f398f2015-02-05 14:39:01 -0800931 return true;
932 }
933
Brian Salomond0a0a652016-12-15 15:25:22 -0500934 struct PathData {
bsalomonf1703092016-06-29 18:41:53 -0700935 SkMatrix fViewMatrix;
936 SkPath fPath;
Brian Salomon60fb0b22017-06-15 17:09:36 -0400937 GrColor fColor;
bsalomonf1703092016-06-29 18:41:53 -0700938 };
939
Brian Salomon10978a62017-06-15 16:21:49 -0400940 Helper fHelper;
Brian Salomond0a0a652016-12-15 15:25:22 -0500941 SkSTArray<1, PathData, true> fPaths;
Brian Salomon10978a62017-06-15 16:21:49 -0400942 bool fLinesOnly;
reed1b55a962015-09-17 20:16:13 -0700943
Brian Salomon10978a62017-06-15 16:21:49 -0400944 typedef GrMeshDrawOp INHERITED;
joshualitt27f398f2015-02-05 14:39:01 -0800945};
946
Brian Salomon10978a62017-06-15 16:21:49 -0400947} // anonymous namespace
948
bsalomon0aff2fa2015-07-31 06:48:27 -0700949bool GrAAConvexPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400950 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700951 "GrAAConvexPathRenderer::onDrawPath");
Brian Salomon7c8460e2017-05-12 11:36:10 -0400952 SkASSERT(GrFSAAType::kUnifiedMSAA != args.fRenderTargetContext->fsaaType());
bsalomon8acedde2016-06-24 10:42:16 -0700953 SkASSERT(!args.fShape->isEmpty());
bsalomon@google.com4647f902013-03-26 14:45:27 +0000954
bsalomonf1703092016-06-29 18:41:53 -0700955 SkPath path;
956 args.fShape->asPath(&path);
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000957
Brian Salomon10978a62017-06-15 16:21:49 -0400958 std::unique_ptr<GrDrawOp> op = AAConvexPathOp::Make(std::move(args.fPaint), *args.fViewMatrix,
959 path, args.fUserStencilSettings);
960 args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op));
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000961 return true;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000962}
joshualitt8e5c1772015-05-11 08:58:52 -0700963
964///////////////////////////////////////////////////////////////////////////////////////////////////
965
Hal Canary6f6961e2017-01-31 13:50:44 -0500966#if GR_TEST_UTILS
joshualitt8e5c1772015-05-11 08:58:52 -0700967
Brian Salomon10978a62017-06-15 16:21:49 -0400968GR_DRAW_OP_TEST_DEFINE(AAConvexPathOp) {
bsalomonf1703092016-06-29 18:41:53 -0700969 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
970 SkPath path = GrTest::TestPathConvex(random);
Brian Salomon10978a62017-06-15 16:21:49 -0400971 const GrUserStencilSettings* stencilSettings = GrGetRandomStencil(random, context);
972 return AAConvexPathOp::Make(std::move(paint), viewMatrix, path, stencilSettings);
joshualitt8e5c1772015-05-11 08:58:52 -0700973}
974
975#endif