blob: 2dc3e1add2855cd1e65f98e67270f1036b5a55ad [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
Mike Kleinc0bd9f92019-04-23 12:05:21 -05008#include "include/core/SkString.h"
9#include "include/core/SkTypes.h"
10#include "src/core/SkGeometry.h"
11#include "src/core/SkPathPriv.h"
12#include "src/core/SkPointPriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040013#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050014#include "src/gpu/GrCaps.h"
15#include "src/gpu/GrDrawOpTest.h"
16#include "src/gpu/GrGeometryProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050017#include "src/gpu/GrProcessor.h"
Robert Phillips3eabf4a2020-03-10 14:49:20 -040018#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050019#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrVertexWriter.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040021#include "src/gpu/geometry/GrPathUtils.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000022#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050023#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
24#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
25#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
26#include "src/gpu/glsl/GrGLSLUniformHandler.h"
27#include "src/gpu/glsl/GrGLSLVarying.h"
28#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
29#include "src/gpu/ops/GrAAConvexPathRenderer.h"
30#include "src/gpu/ops/GrMeshDrawOp.h"
Robert Phillips55f681f2020-02-28 08:58:15 -050031#include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h"
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000032
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000033GrAAConvexPathRenderer::GrAAConvexPathRenderer() {
34}
35
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000036struct Segment {
37 enum {
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000038 // These enum values are assumed in member functions below.
39 kLine = 0,
40 kQuad = 1,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000041 } fType;
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000042
bsalomon@google.com9aed1142012-01-30 14:28:39 +000043 // line uses one pt, quad uses 2 pts
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000044 SkPoint fPts[2];
bsalomon@google.com9aed1142012-01-30 14:28:39 +000045 // normal to edge ending at each pt
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000046 SkVector fNorms[2];
bsalomon@google.com9aed1142012-01-30 14:28:39 +000047 // is the corner where the previous segment meets this segment
48 // sharp. If so, fMid is a normalized bisector facing outward.
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000049 SkVector fMid;
bsalomon@google.com9aed1142012-01-30 14:28:39 +000050
51 int countPoints() {
Brian Salomon4dea72a2019-12-18 10:43:10 -050052 static_assert(0 == kLine && 1 == kQuad);
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000053 return fType + 1;
bsalomon@google.com9aed1142012-01-30 14:28:39 +000054 }
55 const SkPoint& endPt() const {
Brian Salomon4dea72a2019-12-18 10:43:10 -050056 static_assert(0 == kLine && 1 == kQuad);
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000057 return fPts[fType];
Mike Kleinfc6c37b2016-09-27 09:34:10 -040058 }
bsalomon@google.com9aed1142012-01-30 14:28:39 +000059 const SkPoint& endNorm() const {
Brian Salomon4dea72a2019-12-18 10:43:10 -050060 static_assert(0 == kLine && 1 == kQuad);
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000061 return fNorms[fType];
Mike Kleinfc6c37b2016-09-27 09:34:10 -040062 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000063};
64
65typedef SkTArray<Segment, true> SegmentArray;
66
Greg Daniel8b09b962018-04-03 14:53:45 -040067static bool center_of_mass(const SegmentArray& segments, SkPoint* c) {
bsalomon@google.com81712882012-11-01 17:12:34 +000068 SkScalar area = 0;
vandebo@chromium.org6390c722012-03-28 21:03:22 +000069 SkPoint center = {0, 0};
bsalomon@google.com9aed1142012-01-30 14:28:39 +000070 int count = segments.count();
vandebo@chromium.org6390c722012-03-28 21:03:22 +000071 SkPoint p0 = {0, 0};
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000072 if (count > 2) {
73 // We translate the polygon so that the first point is at the origin.
74 // This avoids some precision issues with small area polygons far away
75 // from the origin.
76 p0 = segments[0].endPt();
77 SkPoint pi;
78 SkPoint pj;
bsalomon@google.coma51ab842012-07-10 19:53:34 +000079 // the first and last iteration of the below loop would compute
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000080 // zeros since the starting / ending point is (0,0). So instead we start
81 // at i=1 and make the last iteration i=count-2.
82 pj = segments[1].endPt() - p0;
83 for (int i = 1; i < count - 1; ++i) {
84 pi = pj;
robertphillips44c31282015-09-03 12:58:48 -070085 pj = segments[i + 1].endPt() - p0;
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000086
robertphillips44c31282015-09-03 12:58:48 -070087 SkScalar t = SkPoint::CrossProduct(pi, pj);
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000088 area += t;
89 center.fX += (pi.fX + pj.fX) * t;
90 center.fY += (pi.fY + pj.fY) * t;
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000091 }
bsalomon@google.com9aed1142012-01-30 14:28:39 +000092 }
robertphillips44c31282015-09-03 12:58:48 -070093
bsalomon@google.com278dc692012-02-15 16:52:51 +000094 // If the poly has no area then we instead return the average of
95 // its points.
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000096 if (SkScalarNearlyZero(area)) {
bsalomon@google.com278dc692012-02-15 16:52:51 +000097 SkPoint avg;
98 avg.set(0, 0);
99 for (int i = 0; i < count; ++i) {
100 const SkPoint& pt = segments[i].endPt();
101 avg.fX += pt.fX;
102 avg.fY += pt.fY;
103 }
104 SkScalar denom = SK_Scalar1 / count;
105 avg.scale(denom);
106 *c = avg;
107 } else {
108 area *= 3;
reed80ea19c2015-05-12 10:37:34 -0700109 area = SkScalarInvert(area);
robertphillips44c31282015-09-03 12:58:48 -0700110 center.scale(area);
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +0000111 // undo the translate of p0 to the origin.
112 *c = center + p0;
bsalomon@google.com278dc692012-02-15 16:52:51 +0000113 }
Greg Daniel62473ad2018-04-03 15:44:18 -0400114 return !SkScalarIsNaN(c->fX) && !SkScalarIsNaN(c->fY) && c->isFinite();
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000115}
116
Greg Daniel8b09b962018-04-03 14:53:45 -0400117static bool compute_vectors(SegmentArray* segments,
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000118 SkPoint* fanPt,
reed026beb52015-06-10 14:23:15 -0700119 SkPathPriv::FirstDirection dir,
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000120 int* vCount,
121 int* iCount) {
Greg Daniel8b09b962018-04-03 14:53:45 -0400122 if (!center_of_mass(*segments, fanPt)) {
123 return false;
124 }
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000125 int count = segments->count();
126
bsalomon@google.com278dc692012-02-15 16:52:51 +0000127 // Make the normals point towards the outside
Cary Clarkdf429f32017-11-08 11:44:31 -0500128 SkPointPriv::Side normSide;
reed026beb52015-06-10 14:23:15 -0700129 if (dir == SkPathPriv::kCCW_FirstDirection) {
Cary Clarkdf429f32017-11-08 11:44:31 -0500130 normSide = SkPointPriv::kRight_Side;
bsalomon@google.com278dc692012-02-15 16:52:51 +0000131 } else {
Cary Clarkdf429f32017-11-08 11:44:31 -0500132 normSide = SkPointPriv::kLeft_Side;
bsalomon@google.com278dc692012-02-15 16:52:51 +0000133 }
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000134
Greg Danield5b45932018-06-07 13:15:10 -0400135 int64_t vCount64 = 0;
136 int64_t iCount64 = 0;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000137 // compute normals at all points
138 for (int a = 0; a < count; ++a) {
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000139 Segment& sega = (*segments)[a];
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000140 int b = (a + 1) % count;
141 Segment& segb = (*segments)[b];
142
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000143 const SkPoint* prevPt = &sega.endPt();
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000144 int n = segb.countPoints();
145 for (int p = 0; p < n; ++p) {
146 segb.fNorms[p] = segb.fPts[p] - *prevPt;
147 segb.fNorms[p].normalize();
Brian Salomon0235c642018-08-31 12:04:18 -0400148 segb.fNorms[p] = SkPointPriv::MakeOrthog(segb.fNorms[p], normSide);
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000149 prevPt = &segb.fPts[p];
150 }
151 if (Segment::kLine == segb.fType) {
Greg Danield5b45932018-06-07 13:15:10 -0400152 vCount64 += 5;
153 iCount64 += 9;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000154 } else {
Greg Danield5b45932018-06-07 13:15:10 -0400155 vCount64 += 6;
156 iCount64 += 12;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000157 }
158 }
159
160 // compute mid-vectors where segments meet. TODO: Detect shallow corners
161 // and leave out the wedges and close gaps by stitching segments together.
162 for (int a = 0; a < count; ++a) {
163 const Segment& sega = (*segments)[a];
164 int b = (a + 1) % count;
165 Segment& segb = (*segments)[b];
166 segb.fMid = segb.fNorms[0] + sega.endNorm();
167 segb.fMid.normalize();
168 // corner wedges
Greg Danield5b45932018-06-07 13:15:10 -0400169 vCount64 += 4;
170 iCount64 += 6;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000171 }
Greg Danield5b45932018-06-07 13:15:10 -0400172 if (vCount64 > SK_MaxS32 || iCount64 > SK_MaxS32) {
173 return false;
174 }
175 *vCount = vCount64;
176 *iCount = iCount64;
Greg Daniel8b09b962018-04-03 14:53:45 -0400177 return true;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000178}
179
bsalomon@google.com9732f622012-01-31 15:19:21 +0000180struct DegenerateTestData {
181 DegenerateTestData() { fStage = kInitial; }
182 bool isDegenerate() const { return kNonDegenerate != fStage; }
183 enum {
184 kInitial,
185 kPoint,
186 kLine,
187 kNonDegenerate
188 } fStage;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000189 SkPoint fFirstPoint;
190 SkVector fLineNormal;
bsalomon@google.com81712882012-11-01 17:12:34 +0000191 SkScalar fLineC;
bsalomon@google.com9732f622012-01-31 15:19:21 +0000192};
193
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000194static const SkScalar kClose = (SK_Scalar1 / 16);
Mike Reed8be952a2017-02-13 20:44:33 -0500195static const SkScalar kCloseSqd = kClose * kClose;
bsalomon@google.com9732f622012-01-31 15:19:21 +0000196
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000197static void update_degenerate_test(DegenerateTestData* data, const SkPoint& pt) {
bsalomon@google.com9732f622012-01-31 15:19:21 +0000198 switch (data->fStage) {
199 case DegenerateTestData::kInitial:
200 data->fFirstPoint = pt;
201 data->fStage = DegenerateTestData::kPoint;
202 break;
203 case DegenerateTestData::kPoint:
Cary Clarkdf429f32017-11-08 11:44:31 -0500204 if (SkPointPriv::DistanceToSqd(pt, data->fFirstPoint) > kCloseSqd) {
bsalomon@google.com9732f622012-01-31 15:19:21 +0000205 data->fLineNormal = pt - data->fFirstPoint;
206 data->fLineNormal.normalize();
Brian Salomon0235c642018-08-31 12:04:18 -0400207 data->fLineNormal = SkPointPriv::MakeOrthog(data->fLineNormal);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000208 data->fLineC = -data->fLineNormal.dot(data->fFirstPoint);
209 data->fStage = DegenerateTestData::kLine;
210 }
211 break;
212 case DegenerateTestData::kLine:
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000213 if (SkScalarAbs(data->fLineNormal.dot(pt) + data->fLineC) > kClose) {
bsalomon@google.com9732f622012-01-31 15:19:21 +0000214 data->fStage = DegenerateTestData::kNonDegenerate;
215 }
John Stiles30212b72020-06-11 17:55:07 -0400216 break;
bsalomon@google.com9732f622012-01-31 15:19:21 +0000217 case DegenerateTestData::kNonDegenerate:
218 break;
219 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400220 SK_ABORT("Unexpected degenerate test stage.");
bsalomon@google.com9732f622012-01-31 15:19:21 +0000221 }
222}
223
Brian Osman2caecd22019-09-25 14:02:26 -0400224static inline bool get_direction(const SkPath& path, const SkMatrix& m,
225 SkPathPriv::FirstDirection* dir) {
Michael Ludwig0a1e9ef2019-08-30 10:03:15 -0400226 // At this point, we've already returned true from canDraw(), which checked that the path's
227 // direction could be determined, so this should just be fetching the cached direction.
Brian Osman2caecd22019-09-25 14:02:26 -0400228 // However, if perspective is involved, we're operating on a transformed path, which may no
229 // longer have a computable direction.
230 if (!SkPathPriv::CheapComputeFirstDirection(path, dir)) {
231 return false;
232 }
Michael Ludwig0a1e9ef2019-08-30 10:03:15 -0400233
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000234 // check whether m reverses the orientation
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000235 SkASSERT(!m.hasPerspective());
Mike Reed8be952a2017-02-13 20:44:33 -0500236 SkScalar det2x2 = m.get(SkMatrix::kMScaleX) * m.get(SkMatrix::kMScaleY) -
237 m.get(SkMatrix::kMSkewX) * m.get(SkMatrix::kMSkewY);
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000238 if (det2x2 < 0) {
Brian Osman2caecd22019-09-25 14:02:26 -0400239 *dir = SkPathPriv::OppositeFirstDirection(*dir);
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000240 }
Michael Ludwig0a1e9ef2019-08-30 10:03:15 -0400241
Brian Osman2caecd22019-09-25 14:02:26 -0400242 return true;
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000243}
244
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000245static inline void add_line_to_segment(const SkPoint& pt,
joshualitt27f398f2015-02-05 14:39:01 -0800246 SegmentArray* segments) {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000247 segments->push_back();
248 segments->back().fType = Segment::kLine;
249 segments->back().fPts[0] = pt;
250}
251
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000252static inline void add_quad_segment(const SkPoint pts[3],
joshualitt27f398f2015-02-05 14:39:01 -0800253 SegmentArray* segments) {
Brian Salomon5f8a62b2019-03-29 13:55:32 -0400254 if (SkPointPriv::DistanceToLineSegmentBetweenSqd(pts[1], pts[0], pts[2]) < kCloseSqd) {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000255 if (pts[0] != pts[2]) {
joshualitt27f398f2015-02-05 14:39:01 -0800256 add_line_to_segment(pts[2], segments);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000257 }
258 } else {
259 segments->push_back();
260 segments->back().fType = Segment::kQuad;
261 segments->back().fPts[0] = pts[1];
262 segments->back().fPts[1] = pts[2];
263 }
264}
265
266static inline void add_cubic_segments(const SkPoint pts[4],
reed026beb52015-06-10 14:23:15 -0700267 SkPathPriv::FirstDirection dir,
joshualitt27f398f2015-02-05 14:39:01 -0800268 SegmentArray* segments) {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000269 SkSTArray<15, SkPoint, true> quads;
bsalomon18fab302016-02-16 08:00:05 -0800270 GrPathUtils::convertCubicToQuadsConstrainToTangents(pts, SK_Scalar1, dir, &quads);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000271 int count = quads.count();
272 for (int q = 0; q < count; q += 3) {
joshualitt27f398f2015-02-05 14:39:01 -0800273 add_quad_segment(&quads[q], segments);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000274 }
275}
276
277static bool get_segments(const SkPath& path,
278 const SkMatrix& m,
279 SegmentArray* segments,
280 SkPoint* fanPt,
281 int* vCount,
joshualitt27f398f2015-02-05 14:39:01 -0800282 int* iCount) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000283 SkPath::Iter iter(path, true);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000284 // This renderer over-emphasizes very thin path regions. We use the distance
bsalomon@google.com5cc90d12012-01-17 16:28:34 +0000285 // to the path from the sample to compute coverage. Every pixel intersected
286 // 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 +0000287 // notice that the sample may be close to a very thin area of the path and
bsalomon@google.com5cc90d12012-01-17 16:28:34 +0000288 // thus should be very light. This is particularly egregious for degenerate
289 // line paths. We detect paths that are very close to a line (zero area) and
290 // draw nothing.
bsalomon@google.com9732f622012-01-31 15:19:21 +0000291 DegenerateTestData degenerateData;
Brian Osman2caecd22019-09-25 14:02:26 -0400292 SkPathPriv::FirstDirection dir;
293 if (!get_direction(path, m, &dir)) {
294 return false;
295 }
bsalomon@google.com9732f622012-01-31 15:19:21 +0000296
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000297 for (;;) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000298 SkPoint pts[4];
Mike Reedba7e9a62019-08-16 13:30:34 -0400299 SkPath::Verb verb = iter.next(pts);
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000300 switch (verb) {
301 case SkPath::kMove_Verb:
bsalomon@google.com1a38d552012-03-15 14:40:46 +0000302 m.mapPoints(pts, 1);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000303 update_degenerate_test(&degenerateData, pts[0]);
304 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000305 case SkPath::kLine_Verb: {
Mike Reedba7e9a62019-08-16 13:30:34 -0400306 if (!SkPathPriv::AllPointsEq(pts, 2)) {
307 m.mapPoints(&pts[1], 1);
308 update_degenerate_test(&degenerateData, pts[1]);
309 add_line_to_segment(pts[1], segments);
310 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000311 break;
312 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000313 case SkPath::kQuad_Verb:
Mike Reedba7e9a62019-08-16 13:30:34 -0400314 if (!SkPathPriv::AllPointsEq(pts, 3)) {
315 m.mapPoints(pts, 3);
316 update_degenerate_test(&degenerateData, pts[1]);
317 update_degenerate_test(&degenerateData, pts[2]);
318 add_quad_segment(pts, segments);
319 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000320 break;
egdanielaf18a092015-01-05 10:22:28 -0800321 case SkPath::kConic_Verb: {
Mike Reedba7e9a62019-08-16 13:30:34 -0400322 if (!SkPathPriv::AllPointsEq(pts, 3)) {
323 m.mapPoints(pts, 3);
324 SkScalar weight = iter.conicWeight();
325 SkAutoConicToQuads converter;
326 const SkPoint* quadPts = converter.computeQuads(pts, weight, 0.25f);
327 for (int i = 0; i < converter.countQuads(); ++i) {
328 update_degenerate_test(&degenerateData, quadPts[2*i + 1]);
329 update_degenerate_test(&degenerateData, quadPts[2*i + 2]);
330 add_quad_segment(quadPts + 2*i, segments);
331 }
egdanielaf18a092015-01-05 10:22:28 -0800332 }
333 break;
334 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000335 case SkPath::kCubic_Verb: {
Mike Reedba7e9a62019-08-16 13:30:34 -0400336 if (!SkPathPriv::AllPointsEq(pts, 4)) {
337 m.mapPoints(pts, 4);
338 update_degenerate_test(&degenerateData, pts[1]);
339 update_degenerate_test(&degenerateData, pts[2]);
340 update_degenerate_test(&degenerateData, pts[3]);
341 add_cubic_segments(pts, dir, segments);
342 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000343 break;
Brian Salomon23356442018-11-30 15:33:19 -0500344 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000345 case SkPath::kDone_Verb:
bsalomon@google.com9732f622012-01-31 15:19:21 +0000346 if (degenerateData.isDegenerate()) {
347 return false;
348 } else {
Greg Daniel8b09b962018-04-03 14:53:45 -0400349 return compute_vectors(segments, fanPt, dir, vCount, iCount);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000350 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000351 default:
352 break;
353 }
354 }
355}
356
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000357struct Draw {
358 Draw() : fVertexCnt(0), fIndexCnt(0) {}
359 int fVertexCnt;
360 int fIndexCnt;
361};
362
363typedef SkTArray<Draw, true> DrawArray;
364
Brian Salomon60fb0b22017-06-15 17:09:36 -0400365static void create_vertices(const SegmentArray& segments,
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000366 const SkPoint& fanPt,
Brian Osman7f4735b2019-01-02 13:55:10 +0000367 const GrVertexColor& color,
Brian Salomon60fb0b22017-06-15 17:09:36 -0400368 DrawArray* draws,
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500369 GrVertexWriter& verts,
370 uint16_t* idxs,
371 size_t vertexStride) {
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000372 Draw* draw = &draws->push_back();
373 // alias just to make vert/index assignments easier to read.
374 int* v = &draw->fVertexCnt;
375 int* i = &draw->fIndexCnt;
Brian Osman7f4735b2019-01-02 13:55:10 +0000376 const size_t uvOffset = sizeof(SkPoint) + color.size();
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000377
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000378 int count = segments.count();
379 for (int a = 0; a < count; ++a) {
380 const Segment& sega = segments[a];
381 int b = (a + 1) % count;
382 const Segment& segb = segments[b];
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000383
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000384 // Check whether adding the verts for this segment to the current draw would cause index
385 // values to overflow.
386 int vCount = 4;
387 if (Segment::kLine == segb.fType) {
388 vCount += 5;
389 } else {
390 vCount += 6;
391 }
392 if (draw->fVertexCnt + vCount > (1 << 16)) {
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000393 idxs += *i;
394 draw = &draws->push_back();
395 v = &draw->fVertexCnt;
396 i = &draw->fIndexCnt;
397 }
398
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500399 const SkScalar negOneDists[2] = { -SK_Scalar1, -SK_Scalar1 };
400
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000401 // FIXME: These tris are inset in the 1 unit arc around the corner
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500402 SkPoint p0 = sega.endPt();
403 // Position, Color, UV, D0, D1
404 verts.write(p0, color, SkPoint{0, 0}, negOneDists);
405 verts.write(p0 + sega.endNorm(), color, SkPoint{0, -SK_Scalar1}, negOneDists);
406 verts.write(p0 + segb.fMid, color, SkPoint{0, -SK_Scalar1}, negOneDists);
407 verts.write(p0 + segb.fNorms[0], color, SkPoint{0, -SK_Scalar1}, negOneDists);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000408
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000409 idxs[*i + 0] = *v + 0;
410 idxs[*i + 1] = *v + 2;
411 idxs[*i + 2] = *v + 1;
412 idxs[*i + 3] = *v + 0;
413 idxs[*i + 4] = *v + 3;
414 idxs[*i + 5] = *v + 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000415
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000416 *v += 4;
417 *i += 6;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000418
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000419 if (Segment::kLine == segb.fType) {
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000420 // we draw the line edge as a degenerate quad (u is 0, v is the
421 // signed distance to the edge)
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500422 SkPoint v1Pos = sega.endPt();
423 SkPoint v2Pos = segb.fPts[0];
424 SkScalar dist = SkPointPriv::DistanceToLineBetween(fanPt, v1Pos, v2Pos);
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000425
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500426 verts.write(fanPt, color, SkPoint{0, dist}, negOneDists);
427 verts.write(v1Pos, color, SkPoint{0, 0}, negOneDists);
428 verts.write(v2Pos, color, SkPoint{0, 0}, negOneDists);
429 verts.write(v1Pos + segb.fNorms[0], color, SkPoint{0, -SK_Scalar1}, negOneDists);
430 verts.write(v2Pos + segb.fNorms[0], color, SkPoint{0, -SK_Scalar1}, negOneDists);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000431
cdalton35964822015-04-29 10:14:03 -0700432 idxs[*i + 0] = *v + 3;
433 idxs[*i + 1] = *v + 1;
434 idxs[*i + 2] = *v + 2;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000435
cdalton35964822015-04-29 10:14:03 -0700436 idxs[*i + 3] = *v + 4;
437 idxs[*i + 4] = *v + 3;
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000438 idxs[*i + 5] = *v + 2;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000439
cdalton35964822015-04-29 10:14:03 -0700440 *i += 6;
441
442 // Draw the interior fan if it exists.
443 // TODO: Detect and combine colinear segments. This will ensure we catch every case
444 // with no interior, and that the resulting shared edge uses the same endpoints.
445 if (count >= 3) {
446 idxs[*i + 0] = *v + 0;
447 idxs[*i + 1] = *v + 2;
448 idxs[*i + 2] = *v + 1;
449
450 *i += 3;
451 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000452
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000453 *v += 5;
bsalomon@google.com06809612012-01-21 15:03:39 +0000454 } else {
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500455 void* quadVertsBegin = verts.fPtr;
456
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000457 SkPoint qpts[] = {sega.endPt(), segb.fPts[0], segb.fPts[1]};
bsalomon@google.com495e2102012-01-21 14:48:36 +0000458
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500459 SkScalar c0 = segb.fNorms[0].dot(qpts[0]);
460 SkScalar c1 = segb.fNorms[1].dot(qpts[2]);
461 GrVertexWriter::Skip<SkPoint> skipUVs;
462
463 verts.write(fanPt,
464 color, skipUVs,
465 -segb.fNorms[0].dot(fanPt) + c0,
466 -segb.fNorms[1].dot(fanPt) + c1);
467
468 verts.write(qpts[0],
469 color, skipUVs,
470 0.0f,
471 -segb.fNorms[1].dot(qpts[0]) + c1);
472
473 verts.write(qpts[2],
474 color, skipUVs,
475 -segb.fNorms[0].dot(qpts[2]) + c0,
476 0.0f);
Greg Daniel1204c4b2020-04-22 14:10:12 -0400477 // We need a negative value that is very large that it won't effect results if it is
478 // interpolated with. However, the value can't be too large of a negative that it
479 // effects numerical precision on less powerful GPUs.
480 static const SkScalar kStableLargeNegativeValue = -SK_ScalarMax/1000000;
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500481 verts.write(qpts[0] + segb.fNorms[0],
482 color, skipUVs,
Greg Daniel1204c4b2020-04-22 14:10:12 -0400483 kStableLargeNegativeValue,
484 kStableLargeNegativeValue);
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500485
486 verts.write(qpts[2] + segb.fNorms[1],
487 color, skipUVs,
Greg Daniel1204c4b2020-04-22 14:10:12 -0400488 kStableLargeNegativeValue,
489 kStableLargeNegativeValue);
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500490
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000491 SkVector midVec = segb.fNorms[0] + segb.fNorms[1];
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000492 midVec.normalize();
bsalomon@google.com06809612012-01-21 15:03:39 +0000493
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500494 verts.write(qpts[1] + midVec,
495 color, skipUVs,
Greg Daniel1204c4b2020-04-22 14:10:12 -0400496 kStableLargeNegativeValue,
497 kStableLargeNegativeValue);
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000498
bsalomon@google.com19713172012-03-15 13:51:08 +0000499 GrPathUtils::QuadUVMatrix toUV(qpts);
Brian Osman7f4735b2019-01-02 13:55:10 +0000500 toUV.apply(quadVertsBegin, 6, vertexStride, uvOffset);
bsalomon@google.com06809612012-01-21 15:03:39 +0000501
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000502 idxs[*i + 0] = *v + 3;
503 idxs[*i + 1] = *v + 1;
504 idxs[*i + 2] = *v + 2;
505 idxs[*i + 3] = *v + 4;
506 idxs[*i + 4] = *v + 3;
507 idxs[*i + 5] = *v + 2;
bsalomon@google.com06809612012-01-21 15:03:39 +0000508
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000509 idxs[*i + 6] = *v + 5;
510 idxs[*i + 7] = *v + 3;
511 idxs[*i + 8] = *v + 4;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000512
cdalton35964822015-04-29 10:14:03 -0700513 *i += 9;
514
515 // Draw the interior fan if it exists.
516 // TODO: Detect and combine colinear segments. This will ensure we catch every case
517 // with no interior, and that the resulting shared edge uses the same endpoints.
518 if (count >= 3) {
519 idxs[*i + 0] = *v + 0;
520 idxs[*i + 1] = *v + 2;
521 idxs[*i + 2] = *v + 1;
522
523 *i += 3;
524 }
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000525
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000526 *v += 6;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000527 }
528 }
529}
530
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000531///////////////////////////////////////////////////////////////////////////////
532
533/*
534 * Quadratic specified by 0=u^2-v canonical coords. u and v are the first
535 * two components of the vertex attribute. Coverage is based on signed
536 * distance with negative being inside, positive outside. The edge is specified in
537 * window space (y-down). If either the third or fourth component of the interpolated
538 * 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 +0000539 * attempt to trim to a portion of the infinite quad.
540 * Requires shader derivative instruction support.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000541 */
542
joshualitt249af152014-09-15 11:41:13 -0700543class QuadEdgeEffect : public GrGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000544public:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500545 static GrGeometryProcessor* Make(SkArenaAlloc* arena,
546 const SkMatrix& localMatrix,
547 bool usesLocalCoords,
548 bool wideColor) {
549 return arena->make<QuadEdgeEffect>(localMatrix, usesLocalCoords, wideColor);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000550 }
551
Brian Salomond3b65972017-03-22 12:05:03 -0400552 ~QuadEdgeEffect() override {}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000553
mtklein36352bf2015-03-25 18:17:31 -0700554 const char* name() const override { return "QuadEdge"; }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000555
egdaniel57d3b032015-11-13 11:57:27 -0800556 class GLSLProcessor : public GrGLSLGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000557 public:
Brian Salomon60fb0b22017-06-15 17:09:36 -0400558 GLSLProcessor() {}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000559
mtklein36352bf2015-03-25 18:17:31 -0700560 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
joshualitt2dd1ae02014-12-03 06:24:10 -0800561 const QuadEdgeEffect& qe = args.fGP.cast<QuadEdgeEffect>();
egdaniel4ca2e602015-11-18 08:01:26 -0800562 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800563 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800564 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualitt2dd1ae02014-12-03 06:24:10 -0800565
joshualittabb52a12015-01-13 15:02:10 -0800566 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800567 varyingHandler->emitAttributes(qe);
joshualittabb52a12015-01-13 15:02:10 -0800568
Chris Dalton27372882017-12-08 13:34:21 -0700569 GrGLSLVarying v(kHalf4_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800570 varyingHandler->addVarying("QuadEdge", &v);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500571 vertBuilder->codeAppendf("%s = %s;", v.vsOut(), qe.fInQuadEdge.name());
Brian Salomon60fb0b22017-06-15 17:09:36 -0400572
573 // Setup pass through color
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500574 varyingHandler->addPassThroughAttribute(qe.fInColor, args.fOutputColor);
joshualitt2dd1ae02014-12-03 06:24:10 -0800575
Chris Dalton60283612018-02-14 13:38:14 -0700576 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt9b989322014-12-15 14:16:27 -0800577
joshualittabb52a12015-01-13 15:02:10 -0800578 // Setup position
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500579 this->writeOutputPosition(vertBuilder, gpArgs, qe.fInPosition.name());
Michael Ludwig553db622020-06-19 10:47:30 -0400580 if (qe.fUsesLocalCoords) {
581 this->writeLocalCoord(vertBuilder, uniformHandler, gpArgs,
582 qe.fInPosition.asShaderVar(), qe.fLocalMatrix,
583 &fLocalMatrixUniform);
584 }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000585
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400586 fragBuilder->codeAppendf("half edgeAlpha;");
joshualitt30ba4362014-08-21 20:18:45 -0700587
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000588 // keep the derivative instructions outside the conditional
Ethan Nicholase1f55022019-02-05 17:17:40 -0500589 fragBuilder->codeAppendf("half2 duvdx = half2(dFdx(%s.xy));", v.fsIn());
590 fragBuilder->codeAppendf("half2 duvdy = half2(dFdy(%s.xy));", v.fsIn());
egdaniel4ca2e602015-11-18 08:01:26 -0800591 fragBuilder->codeAppendf("if (%s.z > 0.0 && %s.w > 0.0) {", v.fsIn(), v.fsIn());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000592 // today we know z and w are in device space. We could use derivatives
egdaniel4ca2e602015-11-18 08:01:26 -0800593 fragBuilder->codeAppendf("edgeAlpha = min(min(%s.z, %s.w) + 0.5, 1.0);", v.fsIn(),
594 v.fsIn());
595 fragBuilder->codeAppendf ("} else {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400596 fragBuilder->codeAppendf("half2 gF = half2(2.0*%s.x*duvdx.x - duvdx.y,"
egdaniel4ca2e602015-11-18 08:01:26 -0800597 " 2.0*%s.x*duvdy.x - duvdy.y);",
598 v.fsIn(), v.fsIn());
599 fragBuilder->codeAppendf("edgeAlpha = (%s.x*%s.x - %s.y);", v.fsIn(), v.fsIn(),
600 v.fsIn());
601 fragBuilder->codeAppendf("edgeAlpha = "
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400602 "saturate(0.5 - edgeAlpha / length(gF));}");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000603
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400604 fragBuilder->codeAppendf("%s = half4(edgeAlpha);", args.fOutputCoverage);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000605 }
606
joshualitt9b989322014-12-15 14:16:27 -0800607 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500608 const GrShaderCaps&,
joshualitt9b989322014-12-15 14:16:27 -0800609 GrProcessorKeyBuilder* b) {
joshualitte3ababe2015-05-15 07:56:07 -0700610 const QuadEdgeEffect& qee = gp.cast<QuadEdgeEffect>();
Michael Ludwig553db622020-06-19 10:47:30 -0400611 uint32_t key = (uint32_t) qee.fUsesLocalCoords;
612 key |= ComputeMatrixKey(qee.fLocalMatrix) << 1;
613 b->add32(key);
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,
Brian Salomonc241b582019-11-27 08:57:17 -0500618 const CoordTransformRange& transformRange) override {
joshualittb8c241a2015-05-19 08:23:30 -0700619 const QuadEdgeEffect& qe = gp.cast<QuadEdgeEffect>();
Michael Ludwig553db622020-06-19 10:47:30 -0400620 this->setTransformDataHelper(pdman, transformRange);
621 this->setTransform(pdman, fLocalMatrixUniform, qe.fLocalMatrix, &fLocalMatrix);
joshualitte3ababe2015-05-15 07:56:07 -0700622 }
623
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000624 private:
egdaniele659a582015-11-13 09:55:43 -0800625 typedef GrGLSLGeometryProcessor INHERITED;
Michael Ludwig553db622020-06-19 10:47:30 -0400626
627 SkMatrix fLocalMatrix = SkMatrix::InvalidMatrix();
628 UniformHandle fLocalMatrixUniform;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000629 };
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000630
Brian Salomon94efbf52016-11-29 13:43:05 -0500631 void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override {
egdaniel57d3b032015-11-13 11:57:27 -0800632 GLSLProcessor::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800633 }
634
Brian Salomon94efbf52016-11-29 13:43:05 -0500635 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override {
egdaniel57d3b032015-11-13 11:57:27 -0800636 return new GLSLProcessor();
joshualitteb2a6762014-12-04 11:35:33 -0800637 }
638
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000639private:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500640 friend class ::SkArenaAlloc; // for access to ctor
641
Brian Osman7f4735b2019-01-02 13:55:10 +0000642 QuadEdgeEffect(const SkMatrix& localMatrix, bool usesLocalCoords, bool wideColor)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400643 : INHERITED(kQuadEdgeEffect_ClassID)
644 , fLocalMatrix(localMatrix)
645 , fUsesLocalCoords(usesLocalCoords) {
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500646 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Osman7f4735b2019-01-02 13:55:10 +0000647 fInColor = MakeColorAttribute("inColor", wideColor);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500648 fInQuadEdge = {"inQuadEdge", kFloat4_GrVertexAttribType, kHalf4_GrSLType};
649 this->setVertexAttributes(&fInPosition, 3);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000650 }
651
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500652 Attribute fInPosition;
653 Attribute fInColor;
654 Attribute fInQuadEdge;
655
Brian Salomon92be2f72018-06-19 14:33:47 -0400656 SkMatrix fLocalMatrix;
657 bool fUsesLocalCoords;
joshualitt249af152014-09-15 11:41:13 -0700658
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400659 GR_DECLARE_GEOMETRY_PROCESSOR_TEST
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000660
joshualitt2e3b3e32014-12-09 13:31:14 -0800661 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000662};
663
joshualittb0a8a372014-09-23 09:50:21 -0700664GR_DEFINE_GEOMETRY_PROCESSOR_TEST(QuadEdgeEffect);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000665
Hal Canary6f6961e2017-01-31 13:50:44 -0500666#if GR_TEST_UTILS
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500667GrGeometryProcessor* QuadEdgeEffect::TestCreate(GrProcessorTestData* d) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000668 // Doesn't work without derivative instructions.
Robert Phillips296b1cc2017-03-15 10:42:12 -0400669 return d->caps()->shaderCaps()->shaderDerivativeSupport()
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500670 ? QuadEdgeEffect::Make(d->allocator(), GrTest::TestMatrix(d->fRandom),
671 d->fRandom->nextBool(), d->fRandom->nextBool())
Brian Salomon9ae32a22017-01-25 14:58:24 -0500672 : nullptr;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000673}
Hal Canary6f6961e2017-01-31 13:50:44 -0500674#endif
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000675
676///////////////////////////////////////////////////////////////////////////////
677
Chris Dalton5ed44232017-09-07 13:22:46 -0600678GrPathRenderer::CanDrawPath
679GrAAConvexPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Michael Ludwig0a1e9ef2019-08-30 10:03:15 -0400680 // This check requires convexity and known direction, since the direction is used to build
681 // the geometry segments. Degenerate convex paths will fall through to some other path renderer.
Chris Dalton5ed44232017-09-07 13:22:46 -0600682 if (args.fCaps->shaderCaps()->shaderDerivativeSupport() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -0600683 (GrAAType::kCoverage == args.fAAType) && args.fShape->style().isSimpleFill() &&
Michael Ludwig0a1e9ef2019-08-30 10:03:15 -0400684 !args.fShape->inverseFilled() && args.fShape->knownToBeConvex() &&
685 args.fShape->knownDirection()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600686 return CanDrawPath::kYes;
687 }
688 return CanDrawPath::kNo;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000689}
690
Brian Salomon10978a62017-06-15 16:21:49 -0400691namespace {
692
693class AAConvexPathOp final : public GrMeshDrawOp {
694private:
695 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
696
joshualitt27f398f2015-02-05 14:39:01 -0800697public:
Brian Salomon25a88092016-12-01 09:36:50 -0500698 DEFINE_OP_CLASS_ID
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400699
Robert Phillipsb97da532019-02-12 15:24:12 -0500700 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400701 GrPaint&& paint,
702 const SkMatrix& viewMatrix,
Brian Salomon10978a62017-06-15 16:21:49 -0400703 const SkPath& path,
704 const GrUserStencilSettings* stencilSettings) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400705 return Helper::FactoryHelper<AAConvexPathOp>(context, std::move(paint), viewMatrix, path,
Brian Salomon10978a62017-06-15 16:21:49 -0400706 stencilSettings);
707 }
708
Brian Osmancf860852018-10-31 14:04:39 -0400709 AAConvexPathOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
Brian Osman936fe7d2018-10-30 15:30:35 -0400710 const SkMatrix& viewMatrix, const SkPath& path,
711 const GrUserStencilSettings* stencilSettings)
Brian Salomon60fb0b22017-06-15 17:09:36 -0400712 : INHERITED(ClassID()), fHelper(helperArgs, GrAAType::kCoverage, stencilSettings) {
713 fPaths.emplace_back(PathData{viewMatrix, path, color});
Greg Daniel5faf4742019-10-01 15:14:44 -0400714 this->setTransformedBounds(path.getBounds(), viewMatrix, HasAABloat::kYes,
715 IsHairline::kNo);
bsalomonf1703092016-06-29 18:41:53 -0700716 }
joshualitt27f398f2015-02-05 14:39:01 -0800717
Brian Salomond0a0a652016-12-15 15:25:22 -0500718 const char* name() const override { return "AAConvexPathOp"; }
joshualitt27f398f2015-02-05 14:39:01 -0800719
Chris Dalton1706cbf2019-05-21 19:35:29 -0600720 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400721 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600722 fProgramInfo->visitFPProxies(func);
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400723 } else {
724 fHelper.visitProxies(func);
725 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400726 }
727
Brian Osman9a390ac2018-11-12 09:47:48 -0500728#ifdef SK_DEBUG
Brian Salomon7c3e7182016-12-01 09:35:30 -0500729 SkString dumpInfo() const override {
730 SkString string;
Brian Salomon60fb0b22017-06-15 17:09:36 -0400731 string.appendf("Count: %d\n", fPaths.count());
Brian Salomon10978a62017-06-15 16:21:49 -0400732 string += fHelper.dumpInfo();
733 string += INHERITED::dumpInfo();
Brian Salomon7c3e7182016-12-01 09:35:30 -0500734 return string;
735 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500736#endif
Brian Salomon7c3e7182016-12-01 09:35:30 -0500737
Brian Salomon10978a62017-06-15 16:21:49 -0400738 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
739
Chris Dalton6ce447a2019-06-23 18:07:38 -0600740 GrProcessorSet::Analysis finalize(
741 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
742 GrClampType clampType) override {
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700743 return fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600744 caps, clip, hasMixedSampledCoverage, clampType,
745 GrProcessorAnalysisCoverage::kSingleChannel, &fPaths.back().fColor, &fWideColor);
Brian Salomon10978a62017-06-15 16:21:49 -0400746 }
747
bsalomone46f9fe2015-08-18 06:05:14 -0700748private:
Robert Phillips2669a7b2020-03-12 12:07:19 -0400749 GrProgramInfo* programInfo() override { return fProgramInfo; }
750
Robert Phillips4133dc42020-03-11 15:55:55 -0400751 void onCreateProgramInfo(const GrCaps* caps,
752 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400753 const GrSurfaceProxyView* writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400754 GrAppliedClip&& appliedClip,
755 const GrXferProcessor::DstProxyView& dstProxyView) override {
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400756 SkMatrix invert;
757 if (fHelper.usesLocalCoords() && !fPaths.back().fViewMatrix.invert(&invert)) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400758 return;
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400759 }
760
761 GrGeometryProcessor* quadProcessor = QuadEdgeEffect::Make(arena, invert,
762 fHelper.usesLocalCoords(),
763 fWideColor);
764
Brian Salomon8afde5f2020-04-01 16:22:00 -0400765 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400766 std::move(appliedClip),
767 dstProxyView, quadProcessor,
768 GrPrimitiveType::kTriangles);
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400769 }
770
Brian Salomon91326c32017-08-09 16:02:19 -0400771 void onPrepareDraws(Target* target) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500772 int instanceCount = fPaths.count();
joshualitt27f398f2015-02-05 14:39:01 -0800773
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400774 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400775 this->createProgramInfo(target);
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400776 if (!fProgramInfo) {
777 return;
778 }
joshualitt27f398f2015-02-05 14:39:01 -0800779 }
780
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400781 const size_t kVertexStride = fProgramInfo->primProc().vertexStride();
782
783 fDraws.reserve(instanceCount);
joshualitt27f398f2015-02-05 14:39:01 -0800784
joshualitt27f398f2015-02-05 14:39:01 -0800785 // TODO generate all segments for all paths and use one vertex buffer
786 for (int i = 0; i < instanceCount; i++) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500787 const PathData& args = fPaths[i];
joshualitt27f398f2015-02-05 14:39:01 -0800788
789 // We use the fact that SkPath::transform path does subdivision based on
790 // perspective. Otherwise, we apply the view matrix when copying to the
791 // segment representation.
792 const SkMatrix* viewMatrix = &args.fViewMatrix;
joshualitt144c3c82015-11-30 12:30:13 -0800793
794 // We avoid initializing the path unless we have to
795 const SkPath* pathPtr = &args.fPath;
796 SkTLazy<SkPath> tmpPath;
joshualitt27f398f2015-02-05 14:39:01 -0800797 if (viewMatrix->hasPerspective()) {
joshualitt144c3c82015-11-30 12:30:13 -0800798 SkPath* tmpPathPtr = tmpPath.init(*pathPtr);
799 tmpPathPtr->setIsVolatile(true);
800 tmpPathPtr->transform(*viewMatrix);
joshualitt27f398f2015-02-05 14:39:01 -0800801 viewMatrix = &SkMatrix::I();
joshualitt144c3c82015-11-30 12:30:13 -0800802 pathPtr = tmpPathPtr;
joshualitt27f398f2015-02-05 14:39:01 -0800803 }
804
805 int vertexCount;
806 int indexCount;
807 enum {
808 kPreallocSegmentCnt = 512 / sizeof(Segment),
809 kPreallocDrawCnt = 4,
810 };
811 SkSTArray<kPreallocSegmentCnt, Segment, true> segments;
812 SkPoint fanPt;
813
joshualitt144c3c82015-11-30 12:30:13 -0800814 if (!get_segments(*pathPtr, *viewMatrix, &segments, &fanPt, &vertexCount,
joshualitt27f398f2015-02-05 14:39:01 -0800815 &indexCount)) {
816 continue;
817 }
818
Brian Salomon12d22642019-01-29 14:38:50 -0500819 sk_sp<const GrBuffer> vertexBuffer;
Chris Daltonbca46e22017-05-15 11:03:26 -0600820 int firstVertex;
821
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500822 GrVertexWriter verts{target->makeVertexSpace(kVertexStride, vertexCount,
823 &vertexBuffer, &firstVertex)};
bsalomone64eb572015-05-07 11:35:55 -0700824
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500825 if (!verts.fPtr) {
joshualitt4b31de82015-03-05 14:33:41 -0800826 SkDebugf("Could not allocate vertices\n");
827 return;
828 }
829
Brian Salomon12d22642019-01-29 14:38:50 -0500830 sk_sp<const GrBuffer> indexBuffer;
Chris Daltonbca46e22017-05-15 11:03:26 -0600831 int firstIndex;
832
833 uint16_t *idxs = target->makeIndexSpace(indexCount, &indexBuffer, &firstIndex);
robertphillipse40d3972015-05-07 09:51:43 -0700834 if (!idxs) {
joshualitt4b31de82015-03-05 14:33:41 -0800835 SkDebugf("Could not allocate indices\n");
836 return;
837 }
838
joshualitt27f398f2015-02-05 14:39:01 -0800839 SkSTArray<kPreallocDrawCnt, Draw, true> draws;
Brian Osman7f4735b2019-01-02 13:55:10 +0000840 GrVertexColor color(args.fColor, fWideColor);
841 create_vertices(segments, fanPt, color, &draws, verts, idxs, kVertexStride);
joshualitt27f398f2015-02-05 14:39:01 -0800842
Chris Daltoneb694b72020-03-16 09:25:50 -0600843 GrSimpleMesh* meshes = target->allocMeshes(draws.count());
robertphillips44c31282015-09-03 12:58:48 -0700844 for (int j = 0; j < draws.count(); ++j) {
845 const Draw& draw = draws[j];
Brian Salomon7eae3e02018-08-07 14:02:38 +0000846 meshes[j].setIndexed(indexBuffer, draw.fIndexCnt, firstIndex, 0,
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600847 draw.fVertexCnt - 1, GrPrimitiveRestart::kNo, vertexBuffer,
848 firstVertex);
Chris Daltonbca46e22017-05-15 11:03:26 -0600849 firstIndex += draw.fIndexCnt;
850 firstVertex += draw.fVertexCnt;
joshualitt27f398f2015-02-05 14:39:01 -0800851 }
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400852
853 fDraws.push_back({ meshes, draws.count() });
joshualitt27f398f2015-02-05 14:39:01 -0800854 }
855 }
856
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700857 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400858 if (!fProgramInfo || fDraws.isEmpty()) {
859 return;
860 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500861
Chris Dalton765ed362020-03-16 17:34:44 -0600862 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
863 flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline());
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400864 for (int i = 0; i < fDraws.count(); ++i) {
Chris Dalton765ed362020-03-16 17:34:44 -0600865 for (int j = 0; j < fDraws[i].fMeshCount; ++j) {
866 flushState->drawMesh(fDraws[i].fMeshes[j]);
867 }
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400868 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700869 }
870
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500871 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
872 const GrCaps& caps) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500873 AAConvexPathOp* that = t->cast<AAConvexPathOp>();
Brian Salomon10978a62017-06-15 16:21:49 -0400874 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000875 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700876 }
Brian Salomon10978a62017-06-15 16:21:49 -0400877 if (fHelper.usesLocalCoords() &&
Mike Reed2c383152019-12-18 16:47:47 -0500878 !SkMatrixPriv::CheapEqual(fPaths[0].fViewMatrix, that->fPaths[0].fViewMatrix)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000879 return CombineResult::kCannotCombine;
joshualitt27f398f2015-02-05 14:39:01 -0800880 }
881
Brian Salomond0a0a652016-12-15 15:25:22 -0500882 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
Brian Osman7f4735b2019-01-02 13:55:10 +0000883 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000884 return CombineResult::kMerged;
joshualitt27f398f2015-02-05 14:39:01 -0800885 }
886
Brian Salomond0a0a652016-12-15 15:25:22 -0500887 struct PathData {
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400888 SkMatrix fViewMatrix;
889 SkPath fPath;
Brian Osmancf860852018-10-31 14:04:39 -0400890 SkPMColor4f fColor;
bsalomonf1703092016-06-29 18:41:53 -0700891 };
892
Brian Salomon10978a62017-06-15 16:21:49 -0400893 Helper fHelper;
Brian Salomond0a0a652016-12-15 15:25:22 -0500894 SkSTArray<1, PathData, true> fPaths;
Brian Osman7f4735b2019-01-02 13:55:10 +0000895 bool fWideColor;
reed1b55a962015-09-17 20:16:13 -0700896
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400897 struct MeshDraw {
Chris Daltoneb694b72020-03-16 09:25:50 -0600898 GrSimpleMesh* fMeshes;
899 int fMeshCount;
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400900 };
901
902 SkTDArray<MeshDraw> fDraws;
903 GrProgramInfo* fProgramInfo = nullptr;
904
Brian Salomon10978a62017-06-15 16:21:49 -0400905 typedef GrMeshDrawOp INHERITED;
joshualitt27f398f2015-02-05 14:39:01 -0800906};
907
Brian Salomon10978a62017-06-15 16:21:49 -0400908} // anonymous namespace
909
bsalomon0aff2fa2015-07-31 06:48:27 -0700910bool GrAAConvexPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400911 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700912 "GrAAConvexPathRenderer::onDrawPath");
Chris Dalton6ce447a2019-06-23 18:07:38 -0600913 SkASSERT(args.fRenderTargetContext->numSamples() <= 1);
bsalomon8acedde2016-06-24 10:42:16 -0700914 SkASSERT(!args.fShape->isEmpty());
bsalomon@google.com4647f902013-03-26 14:45:27 +0000915
bsalomonf1703092016-06-29 18:41:53 -0700916 SkPath path;
917 args.fShape->asPath(&path);
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000918
Robert Phillips7c525e62018-06-12 10:11:12 -0400919 std::unique_ptr<GrDrawOp> op = AAConvexPathOp::Make(args.fContext, std::move(args.fPaint),
920 *args.fViewMatrix,
Brian Salomon10978a62017-06-15 16:21:49 -0400921 path, args.fUserStencilSettings);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400922 args.fRenderTargetContext->addDrawOp(args.fClip, std::move(op));
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000923 return true;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000924}
joshualitt8e5c1772015-05-11 08:58:52 -0700925
926///////////////////////////////////////////////////////////////////////////////////////////////////
927
Hal Canary6f6961e2017-01-31 13:50:44 -0500928#if GR_TEST_UTILS
joshualitt8e5c1772015-05-11 08:58:52 -0700929
Brian Salomon10978a62017-06-15 16:21:49 -0400930GR_DRAW_OP_TEST_DEFINE(AAConvexPathOp) {
bsalomonf1703092016-06-29 18:41:53 -0700931 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
932 SkPath path = GrTest::TestPathConvex(random);
Brian Salomon10978a62017-06-15 16:21:49 -0400933 const GrUserStencilSettings* stencilSettings = GrGetRandomStencil(random, context);
Robert Phillips7c525e62018-06-12 10:11:12 -0400934 return AAConvexPathOp::Make(context, std::move(paint), viewMatrix, path, stencilSettings);
joshualitt8e5c1772015-05-11 08:58:52 -0700935}
936
937#endif