blob: c017c40ae7b0845f8e7a5428cb7049b5f6d40284 [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"
Michael Ludwigfbe28592020-06-26 16:02:15 -040011#include "src/core/SkMatrixPriv.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050012#include "src/core/SkPathPriv.h"
13#include "src/core/SkPointPriv.h"
Greg Danielf91aeb22019-06-18 09:58:02 -040014#include "src/gpu/GrAuditTrail.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050015#include "src/gpu/GrCaps.h"
16#include "src/gpu/GrDrawOpTest.h"
17#include "src/gpu/GrGeometryProcessor.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050018#include "src/gpu/GrProcessor.h"
Robert Phillips3eabf4a2020-03-10 14:49:20 -040019#include "src/gpu/GrProgramInfo.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050020#include "src/gpu/GrRenderTargetContext.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050021#include "src/gpu/GrVertexWriter.h"
Michael Ludwig663afe52019-06-03 16:46:19 -040022#include "src/gpu/geometry/GrPathUtils.h"
Michael Ludwig2686d692020-04-17 20:21:37 +000023#include "src/gpu/geometry/GrStyledShape.h"
Mike Kleinc0bd9f92019-04-23 12:05:21 -050024#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
25#include "src/gpu/glsl/GrGLSLGeometryProcessor.h"
26#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
27#include "src/gpu/glsl/GrGLSLUniformHandler.h"
28#include "src/gpu/glsl/GrGLSLVarying.h"
29#include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h"
30#include "src/gpu/ops/GrAAConvexPathRenderer.h"
31#include "src/gpu/ops/GrMeshDrawOp.h"
Robert Phillips55f681f2020-02-28 08:58:15 -050032#include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h"
commit-bot@chromium.org234d4fb2013-09-30 19:55:49 +000033
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000034GrAAConvexPathRenderer::GrAAConvexPathRenderer() {
35}
36
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000037struct Segment {
38 enum {
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000039 // These enum values are assumed in member functions below.
40 kLine = 0,
41 kQuad = 1,
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000042 } fType;
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000043
bsalomon@google.com9aed1142012-01-30 14:28:39 +000044 // line uses one pt, quad uses 2 pts
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000045 SkPoint fPts[2];
bsalomon@google.com9aed1142012-01-30 14:28:39 +000046 // normal to edge ending at each pt
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000047 SkVector fNorms[2];
bsalomon@google.com9aed1142012-01-30 14:28:39 +000048 // is the corner where the previous segment meets this segment
49 // sharp. If so, fMid is a normalized bisector facing outward.
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +000050 SkVector fMid;
bsalomon@google.com9aed1142012-01-30 14:28:39 +000051
52 int countPoints() {
Brian Salomon4dea72a2019-12-18 10:43:10 -050053 static_assert(0 == kLine && 1 == kQuad);
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000054 return fType + 1;
bsalomon@google.com9aed1142012-01-30 14:28:39 +000055 }
56 const SkPoint& endPt() const {
Brian Salomon4dea72a2019-12-18 10:43:10 -050057 static_assert(0 == kLine && 1 == kQuad);
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000058 return fPts[fType];
Mike Kleinfc6c37b2016-09-27 09:34:10 -040059 }
bsalomon@google.com9aed1142012-01-30 14:28:39 +000060 const SkPoint& endNorm() const {
Brian Salomon4dea72a2019-12-18 10:43:10 -050061 static_assert(0 == kLine && 1 == kQuad);
bsalomon@google.com9b1517e2012-03-05 17:58:34 +000062 return fNorms[fType];
Mike Kleinfc6c37b2016-09-27 09:34:10 -040063 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +000064};
65
66typedef SkTArray<Segment, true> SegmentArray;
67
Greg Daniel8b09b962018-04-03 14:53:45 -040068static bool center_of_mass(const SegmentArray& segments, SkPoint* c) {
bsalomon@google.com81712882012-11-01 17:12:34 +000069 SkScalar area = 0;
vandebo@chromium.org6390c722012-03-28 21:03:22 +000070 SkPoint center = {0, 0};
bsalomon@google.com9aed1142012-01-30 14:28:39 +000071 int count = segments.count();
vandebo@chromium.org6390c722012-03-28 21:03:22 +000072 SkPoint p0 = {0, 0};
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000073 if (count > 2) {
74 // We translate the polygon so that the first point is at the origin.
75 // This avoids some precision issues with small area polygons far away
76 // from the origin.
77 p0 = segments[0].endPt();
78 SkPoint pi;
79 SkPoint pj;
bsalomon@google.coma51ab842012-07-10 19:53:34 +000080 // the first and last iteration of the below loop would compute
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000081 // zeros since the starting / ending point is (0,0). So instead we start
82 // at i=1 and make the last iteration i=count-2.
83 pj = segments[1].endPt() - p0;
84 for (int i = 1; i < count - 1; ++i) {
85 pi = pj;
robertphillips44c31282015-09-03 12:58:48 -070086 pj = segments[i + 1].endPt() - p0;
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000087
robertphillips44c31282015-09-03 12:58:48 -070088 SkScalar t = SkPoint::CrossProduct(pi, pj);
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000089 area += t;
90 center.fX += (pi.fX + pj.fX) * t;
91 center.fY += (pi.fY + pj.fY) * t;
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000092 }
bsalomon@google.com9aed1142012-01-30 14:28:39 +000093 }
robertphillips44c31282015-09-03 12:58:48 -070094
bsalomon@google.com278dc692012-02-15 16:52:51 +000095 // If the poly has no area then we instead return the average of
96 // its points.
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +000097 if (SkScalarNearlyZero(area)) {
bsalomon@google.com278dc692012-02-15 16:52:51 +000098 SkPoint avg;
99 avg.set(0, 0);
100 for (int i = 0; i < count; ++i) {
101 const SkPoint& pt = segments[i].endPt();
102 avg.fX += pt.fX;
103 avg.fY += pt.fY;
104 }
105 SkScalar denom = SK_Scalar1 / count;
106 avg.scale(denom);
107 *c = avg;
108 } else {
109 area *= 3;
reed80ea19c2015-05-12 10:37:34 -0700110 area = SkScalarInvert(area);
robertphillips44c31282015-09-03 12:58:48 -0700111 center.scale(area);
bsalomon@google.com5b56d9e2012-02-23 19:18:37 +0000112 // undo the translate of p0 to the origin.
113 *c = center + p0;
bsalomon@google.com278dc692012-02-15 16:52:51 +0000114 }
Greg Daniel62473ad2018-04-03 15:44:18 -0400115 return !SkScalarIsNaN(c->fX) && !SkScalarIsNaN(c->fY) && c->isFinite();
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000116}
117
Greg Daniel8b09b962018-04-03 14:53:45 -0400118static bool compute_vectors(SegmentArray* segments,
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000119 SkPoint* fanPt,
Mike Reed3872c982020-08-29 17:46:51 -0400120 SkPathFirstDirection dir,
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000121 int* vCount,
122 int* iCount) {
Greg Daniel8b09b962018-04-03 14:53:45 -0400123 if (!center_of_mass(*segments, fanPt)) {
124 return false;
125 }
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000126 int count = segments->count();
127
bsalomon@google.com278dc692012-02-15 16:52:51 +0000128 // Make the normals point towards the outside
Cary Clarkdf429f32017-11-08 11:44:31 -0500129 SkPointPriv::Side normSide;
Mike Reed3872c982020-08-29 17:46:51 -0400130 if (dir == SkPathFirstDirection::kCCW) {
Cary Clarkdf429f32017-11-08 11:44:31 -0500131 normSide = SkPointPriv::kRight_Side;
bsalomon@google.com278dc692012-02-15 16:52:51 +0000132 } else {
Cary Clarkdf429f32017-11-08 11:44:31 -0500133 normSide = SkPointPriv::kLeft_Side;
bsalomon@google.com278dc692012-02-15 16:52:51 +0000134 }
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000135
Greg Danield5b45932018-06-07 13:15:10 -0400136 int64_t vCount64 = 0;
137 int64_t iCount64 = 0;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000138 // compute normals at all points
139 for (int a = 0; a < count; ++a) {
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000140 Segment& sega = (*segments)[a];
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000141 int b = (a + 1) % count;
142 Segment& segb = (*segments)[b];
143
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000144 const SkPoint* prevPt = &sega.endPt();
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000145 int n = segb.countPoints();
146 for (int p = 0; p < n; ++p) {
147 segb.fNorms[p] = segb.fPts[p] - *prevPt;
148 segb.fNorms[p].normalize();
Brian Salomon0235c642018-08-31 12:04:18 -0400149 segb.fNorms[p] = SkPointPriv::MakeOrthog(segb.fNorms[p], normSide);
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000150 prevPt = &segb.fPts[p];
151 }
152 if (Segment::kLine == segb.fType) {
Greg Danield5b45932018-06-07 13:15:10 -0400153 vCount64 += 5;
154 iCount64 += 9;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000155 } else {
Greg Danield5b45932018-06-07 13:15:10 -0400156 vCount64 += 6;
157 iCount64 += 12;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000158 }
159 }
160
161 // compute mid-vectors where segments meet. TODO: Detect shallow corners
162 // and leave out the wedges and close gaps by stitching segments together.
163 for (int a = 0; a < count; ++a) {
164 const Segment& sega = (*segments)[a];
165 int b = (a + 1) % count;
166 Segment& segb = (*segments)[b];
167 segb.fMid = segb.fNorms[0] + sega.endNorm();
168 segb.fMid.normalize();
169 // corner wedges
Greg Danield5b45932018-06-07 13:15:10 -0400170 vCount64 += 4;
171 iCount64 += 6;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000172 }
Greg Danield5b45932018-06-07 13:15:10 -0400173 if (vCount64 > SK_MaxS32 || iCount64 > SK_MaxS32) {
174 return false;
175 }
176 *vCount = vCount64;
177 *iCount = iCount64;
Greg Daniel8b09b962018-04-03 14:53:45 -0400178 return true;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000179}
180
bsalomon@google.com9732f622012-01-31 15:19:21 +0000181struct DegenerateTestData {
182 DegenerateTestData() { fStage = kInitial; }
183 bool isDegenerate() const { return kNonDegenerate != fStage; }
184 enum {
185 kInitial,
186 kPoint,
187 kLine,
188 kNonDegenerate
189 } fStage;
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000190 SkPoint fFirstPoint;
191 SkVector fLineNormal;
bsalomon@google.com81712882012-11-01 17:12:34 +0000192 SkScalar fLineC;
bsalomon@google.com9732f622012-01-31 15:19:21 +0000193};
194
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000195static const SkScalar kClose = (SK_Scalar1 / 16);
Mike Reed8be952a2017-02-13 20:44:33 -0500196static const SkScalar kCloseSqd = kClose * kClose;
bsalomon@google.com9732f622012-01-31 15:19:21 +0000197
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000198static void update_degenerate_test(DegenerateTestData* data, const SkPoint& pt) {
bsalomon@google.com9732f622012-01-31 15:19:21 +0000199 switch (data->fStage) {
200 case DegenerateTestData::kInitial:
201 data->fFirstPoint = pt;
202 data->fStage = DegenerateTestData::kPoint;
203 break;
204 case DegenerateTestData::kPoint:
Cary Clarkdf429f32017-11-08 11:44:31 -0500205 if (SkPointPriv::DistanceToSqd(pt, data->fFirstPoint) > kCloseSqd) {
bsalomon@google.com9732f622012-01-31 15:19:21 +0000206 data->fLineNormal = pt - data->fFirstPoint;
207 data->fLineNormal.normalize();
Brian Salomon0235c642018-08-31 12:04:18 -0400208 data->fLineNormal = SkPointPriv::MakeOrthog(data->fLineNormal);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000209 data->fLineC = -data->fLineNormal.dot(data->fFirstPoint);
210 data->fStage = DegenerateTestData::kLine;
211 }
212 break;
213 case DegenerateTestData::kLine:
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000214 if (SkScalarAbs(data->fLineNormal.dot(pt) + data->fLineC) > kClose) {
bsalomon@google.com9732f622012-01-31 15:19:21 +0000215 data->fStage = DegenerateTestData::kNonDegenerate;
216 }
John Stiles30212b72020-06-11 17:55:07 -0400217 break;
bsalomon@google.com9732f622012-01-31 15:19:21 +0000218 case DegenerateTestData::kNonDegenerate:
219 break;
220 default:
Ben Wagnerb4aab9a2017-08-16 10:53:04 -0400221 SK_ABORT("Unexpected degenerate test stage.");
bsalomon@google.com9732f622012-01-31 15:19:21 +0000222 }
223}
224
Brian Osman2caecd22019-09-25 14:02:26 -0400225static inline bool get_direction(const SkPath& path, const SkMatrix& m,
Mike Reed3872c982020-08-29 17:46:51 -0400226 SkPathFirstDirection* dir) {
Michael Ludwig0a1e9ef2019-08-30 10:03:15 -0400227 // At this point, we've already returned true from canDraw(), which checked that the path's
228 // direction could be determined, so this should just be fetching the cached direction.
Brian Osman2caecd22019-09-25 14:02:26 -0400229 // However, if perspective is involved, we're operating on a transformed path, which may no
230 // longer have a computable direction.
Mike Reed85f51b22020-08-30 10:32:06 -0400231 *dir = SkPathPriv::ComputeFirstDirection(path);
232 if (*dir == SkPathFirstDirection::kUnknown) {
Brian Osman2caecd22019-09-25 14:02:26 -0400233 return false;
234 }
Michael Ludwig0a1e9ef2019-08-30 10:03:15 -0400235
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000236 // check whether m reverses the orientation
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000237 SkASSERT(!m.hasPerspective());
Mike Reed8be952a2017-02-13 20:44:33 -0500238 SkScalar det2x2 = m.get(SkMatrix::kMScaleX) * m.get(SkMatrix::kMScaleY) -
239 m.get(SkMatrix::kMSkewX) * m.get(SkMatrix::kMSkewY);
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000240 if (det2x2 < 0) {
Brian Osman2caecd22019-09-25 14:02:26 -0400241 *dir = SkPathPriv::OppositeFirstDirection(*dir);
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000242 }
Michael Ludwig0a1e9ef2019-08-30 10:03:15 -0400243
Brian Osman2caecd22019-09-25 14:02:26 -0400244 return true;
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000245}
246
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000247static inline void add_line_to_segment(const SkPoint& pt,
joshualitt27f398f2015-02-05 14:39:01 -0800248 SegmentArray* segments) {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000249 segments->push_back();
250 segments->back().fType = Segment::kLine;
251 segments->back().fPts[0] = pt;
252}
253
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000254static inline void add_quad_segment(const SkPoint pts[3],
joshualitt27f398f2015-02-05 14:39:01 -0800255 SegmentArray* segments) {
Brian Salomon5f8a62b2019-03-29 13:55:32 -0400256 if (SkPointPriv::DistanceToLineSegmentBetweenSqd(pts[1], pts[0], pts[2]) < kCloseSqd) {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000257 if (pts[0] != pts[2]) {
joshualitt27f398f2015-02-05 14:39:01 -0800258 add_line_to_segment(pts[2], segments);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000259 }
260 } else {
261 segments->push_back();
262 segments->back().fType = Segment::kQuad;
263 segments->back().fPts[0] = pts[1];
264 segments->back().fPts[1] = pts[2];
265 }
266}
267
268static inline void add_cubic_segments(const SkPoint pts[4],
Mike Reed3872c982020-08-29 17:46:51 -0400269 SkPathFirstDirection dir,
joshualitt27f398f2015-02-05 14:39:01 -0800270 SegmentArray* segments) {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000271 SkSTArray<15, SkPoint, true> quads;
bsalomon18fab302016-02-16 08:00:05 -0800272 GrPathUtils::convertCubicToQuadsConstrainToTangents(pts, SK_Scalar1, dir, &quads);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000273 int count = quads.count();
274 for (int q = 0; q < count; q += 3) {
joshualitt27f398f2015-02-05 14:39:01 -0800275 add_quad_segment(&quads[q], segments);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000276 }
277}
278
279static bool get_segments(const SkPath& path,
280 const SkMatrix& m,
281 SegmentArray* segments,
282 SkPoint* fanPt,
283 int* vCount,
joshualitt27f398f2015-02-05 14:39:01 -0800284 int* iCount) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000285 SkPath::Iter iter(path, true);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000286 // This renderer over-emphasizes very thin path regions. We use the distance
bsalomon@google.com5cc90d12012-01-17 16:28:34 +0000287 // to the path from the sample to compute coverage. Every pixel intersected
288 // 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 +0000289 // notice that the sample may be close to a very thin area of the path and
bsalomon@google.com5cc90d12012-01-17 16:28:34 +0000290 // thus should be very light. This is particularly egregious for degenerate
291 // line paths. We detect paths that are very close to a line (zero area) and
292 // draw nothing.
bsalomon@google.com9732f622012-01-31 15:19:21 +0000293 DegenerateTestData degenerateData;
Mike Reed3872c982020-08-29 17:46:51 -0400294 SkPathFirstDirection dir;
Brian Osman2caecd22019-09-25 14:02:26 -0400295 if (!get_direction(path, m, &dir)) {
296 return false;
297 }
bsalomon@google.com9732f622012-01-31 15:19:21 +0000298
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000299 for (;;) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000300 SkPoint pts[4];
Mike Reedba7e9a62019-08-16 13:30:34 -0400301 SkPath::Verb verb = iter.next(pts);
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000302 switch (verb) {
303 case SkPath::kMove_Verb:
bsalomon@google.com1a38d552012-03-15 14:40:46 +0000304 m.mapPoints(pts, 1);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000305 update_degenerate_test(&degenerateData, pts[0]);
306 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000307 case SkPath::kLine_Verb: {
Mike Reedba7e9a62019-08-16 13:30:34 -0400308 if (!SkPathPriv::AllPointsEq(pts, 2)) {
309 m.mapPoints(&pts[1], 1);
310 update_degenerate_test(&degenerateData, pts[1]);
311 add_line_to_segment(pts[1], segments);
312 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000313 break;
314 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000315 case SkPath::kQuad_Verb:
Mike Reedba7e9a62019-08-16 13:30:34 -0400316 if (!SkPathPriv::AllPointsEq(pts, 3)) {
317 m.mapPoints(pts, 3);
318 update_degenerate_test(&degenerateData, pts[1]);
319 update_degenerate_test(&degenerateData, pts[2]);
320 add_quad_segment(pts, segments);
321 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000322 break;
egdanielaf18a092015-01-05 10:22:28 -0800323 case SkPath::kConic_Verb: {
Mike Reedba7e9a62019-08-16 13:30:34 -0400324 if (!SkPathPriv::AllPointsEq(pts, 3)) {
325 m.mapPoints(pts, 3);
326 SkScalar weight = iter.conicWeight();
327 SkAutoConicToQuads converter;
328 const SkPoint* quadPts = converter.computeQuads(pts, weight, 0.25f);
329 for (int i = 0; i < converter.countQuads(); ++i) {
330 update_degenerate_test(&degenerateData, quadPts[2*i + 1]);
331 update_degenerate_test(&degenerateData, quadPts[2*i + 2]);
332 add_quad_segment(quadPts + 2*i, segments);
333 }
egdanielaf18a092015-01-05 10:22:28 -0800334 }
335 break;
336 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000337 case SkPath::kCubic_Verb: {
Mike Reedba7e9a62019-08-16 13:30:34 -0400338 if (!SkPathPriv::AllPointsEq(pts, 4)) {
339 m.mapPoints(pts, 4);
340 update_degenerate_test(&degenerateData, pts[1]);
341 update_degenerate_test(&degenerateData, pts[2]);
342 update_degenerate_test(&degenerateData, pts[3]);
343 add_cubic_segments(pts, dir, segments);
344 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000345 break;
Brian Salomon23356442018-11-30 15:33:19 -0500346 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000347 case SkPath::kDone_Verb:
bsalomon@google.com9732f622012-01-31 15:19:21 +0000348 if (degenerateData.isDegenerate()) {
349 return false;
350 } else {
Greg Daniel8b09b962018-04-03 14:53:45 -0400351 return compute_vectors(segments, fanPt, dir, vCount, iCount);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000352 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000353 default:
354 break;
355 }
356 }
357}
358
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000359struct Draw {
360 Draw() : fVertexCnt(0), fIndexCnt(0) {}
361 int fVertexCnt;
362 int fIndexCnt;
363};
364
365typedef SkTArray<Draw, true> DrawArray;
366
Brian Salomon60fb0b22017-06-15 17:09:36 -0400367static void create_vertices(const SegmentArray& segments,
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000368 const SkPoint& fanPt,
Brian Osman7f4735b2019-01-02 13:55:10 +0000369 const GrVertexColor& color,
Brian Salomon60fb0b22017-06-15 17:09:36 -0400370 DrawArray* draws,
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500371 GrVertexWriter& verts,
372 uint16_t* idxs,
373 size_t vertexStride) {
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000374 Draw* draw = &draws->push_back();
375 // alias just to make vert/index assignments easier to read.
376 int* v = &draw->fVertexCnt;
377 int* i = &draw->fIndexCnt;
Brian Osman7f4735b2019-01-02 13:55:10 +0000378 const size_t uvOffset = sizeof(SkPoint) + color.size();
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000379
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000380 int count = segments.count();
381 for (int a = 0; a < count; ++a) {
382 const Segment& sega = segments[a];
383 int b = (a + 1) % count;
384 const Segment& segb = segments[b];
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000385
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000386 // Check whether adding the verts for this segment to the current draw would cause index
387 // values to overflow.
388 int vCount = 4;
389 if (Segment::kLine == segb.fType) {
390 vCount += 5;
391 } else {
392 vCount += 6;
393 }
394 if (draw->fVertexCnt + vCount > (1 << 16)) {
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000395 idxs += *i;
396 draw = &draws->push_back();
397 v = &draw->fVertexCnt;
398 i = &draw->fIndexCnt;
399 }
400
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500401 const SkScalar negOneDists[2] = { -SK_Scalar1, -SK_Scalar1 };
402
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000403 // FIXME: These tris are inset in the 1 unit arc around the corner
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500404 SkPoint p0 = sega.endPt();
405 // Position, Color, UV, D0, D1
406 verts.write(p0, color, SkPoint{0, 0}, negOneDists);
407 verts.write(p0 + sega.endNorm(), color, SkPoint{0, -SK_Scalar1}, negOneDists);
408 verts.write(p0 + segb.fMid, color, SkPoint{0, -SK_Scalar1}, negOneDists);
409 verts.write(p0 + segb.fNorms[0], color, SkPoint{0, -SK_Scalar1}, negOneDists);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000410
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000411 idxs[*i + 0] = *v + 0;
412 idxs[*i + 1] = *v + 2;
413 idxs[*i + 2] = *v + 1;
414 idxs[*i + 3] = *v + 0;
415 idxs[*i + 4] = *v + 3;
416 idxs[*i + 5] = *v + 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000417
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000418 *v += 4;
419 *i += 6;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000420
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000421 if (Segment::kLine == segb.fType) {
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000422 // we draw the line edge as a degenerate quad (u is 0, v is the
423 // signed distance to the edge)
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500424 SkPoint v1Pos = sega.endPt();
425 SkPoint v2Pos = segb.fPts[0];
426 SkScalar dist = SkPointPriv::DistanceToLineBetween(fanPt, v1Pos, v2Pos);
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000427
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500428 verts.write(fanPt, color, SkPoint{0, dist}, negOneDists);
429 verts.write(v1Pos, color, SkPoint{0, 0}, negOneDists);
430 verts.write(v2Pos, color, SkPoint{0, 0}, negOneDists);
431 verts.write(v1Pos + segb.fNorms[0], color, SkPoint{0, -SK_Scalar1}, negOneDists);
432 verts.write(v2Pos + segb.fNorms[0], color, SkPoint{0, -SK_Scalar1}, negOneDists);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000433
cdalton35964822015-04-29 10:14:03 -0700434 idxs[*i + 0] = *v + 3;
435 idxs[*i + 1] = *v + 1;
436 idxs[*i + 2] = *v + 2;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000437
cdalton35964822015-04-29 10:14:03 -0700438 idxs[*i + 3] = *v + 4;
439 idxs[*i + 4] = *v + 3;
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000440 idxs[*i + 5] = *v + 2;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000441
cdalton35964822015-04-29 10:14:03 -0700442 *i += 6;
443
444 // Draw the interior fan if it exists.
445 // TODO: Detect and combine colinear segments. This will ensure we catch every case
446 // with no interior, and that the resulting shared edge uses the same endpoints.
447 if (count >= 3) {
448 idxs[*i + 0] = *v + 0;
449 idxs[*i + 1] = *v + 2;
450 idxs[*i + 2] = *v + 1;
451
452 *i += 3;
453 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000454
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000455 *v += 5;
bsalomon@google.com06809612012-01-21 15:03:39 +0000456 } else {
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500457 void* quadVertsBegin = verts.fPtr;
458
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000459 SkPoint qpts[] = {sega.endPt(), segb.fPts[0], segb.fPts[1]};
bsalomon@google.com495e2102012-01-21 14:48:36 +0000460
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500461 SkScalar c0 = segb.fNorms[0].dot(qpts[0]);
462 SkScalar c1 = segb.fNorms[1].dot(qpts[2]);
463 GrVertexWriter::Skip<SkPoint> skipUVs;
464
465 verts.write(fanPt,
466 color, skipUVs,
467 -segb.fNorms[0].dot(fanPt) + c0,
468 -segb.fNorms[1].dot(fanPt) + c1);
469
470 verts.write(qpts[0],
471 color, skipUVs,
472 0.0f,
473 -segb.fNorms[1].dot(qpts[0]) + c1);
474
475 verts.write(qpts[2],
476 color, skipUVs,
477 -segb.fNorms[0].dot(qpts[2]) + c0,
478 0.0f);
Greg Daniel1204c4b2020-04-22 14:10:12 -0400479 // We need a negative value that is very large that it won't effect results if it is
480 // interpolated with. However, the value can't be too large of a negative that it
481 // effects numerical precision on less powerful GPUs.
482 static const SkScalar kStableLargeNegativeValue = -SK_ScalarMax/1000000;
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500483 verts.write(qpts[0] + segb.fNorms[0],
484 color, skipUVs,
Greg Daniel1204c4b2020-04-22 14:10:12 -0400485 kStableLargeNegativeValue,
486 kStableLargeNegativeValue);
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500487
488 verts.write(qpts[2] + segb.fNorms[1],
489 color, skipUVs,
Greg Daniel1204c4b2020-04-22 14:10:12 -0400490 kStableLargeNegativeValue,
491 kStableLargeNegativeValue);
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500492
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000493 SkVector midVec = segb.fNorms[0] + segb.fNorms[1];
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000494 midVec.normalize();
bsalomon@google.com06809612012-01-21 15:03:39 +0000495
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500496 verts.write(qpts[1] + midVec,
497 color, skipUVs,
Greg Daniel1204c4b2020-04-22 14:10:12 -0400498 kStableLargeNegativeValue,
499 kStableLargeNegativeValue);
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000500
bsalomon@google.com19713172012-03-15 13:51:08 +0000501 GrPathUtils::QuadUVMatrix toUV(qpts);
Brian Osman7f4735b2019-01-02 13:55:10 +0000502 toUV.apply(quadVertsBegin, 6, vertexStride, uvOffset);
bsalomon@google.com06809612012-01-21 15:03:39 +0000503
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000504 idxs[*i + 0] = *v + 3;
505 idxs[*i + 1] = *v + 1;
506 idxs[*i + 2] = *v + 2;
507 idxs[*i + 3] = *v + 4;
508 idxs[*i + 4] = *v + 3;
509 idxs[*i + 5] = *v + 2;
bsalomon@google.com06809612012-01-21 15:03:39 +0000510
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000511 idxs[*i + 6] = *v + 5;
512 idxs[*i + 7] = *v + 3;
513 idxs[*i + 8] = *v + 4;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000514
cdalton35964822015-04-29 10:14:03 -0700515 *i += 9;
516
517 // Draw the interior fan if it exists.
518 // TODO: Detect and combine colinear segments. This will ensure we catch every case
519 // with no interior, and that the resulting shared edge uses the same endpoints.
520 if (count >= 3) {
521 idxs[*i + 0] = *v + 0;
522 idxs[*i + 1] = *v + 2;
523 idxs[*i + 2] = *v + 1;
524
525 *i += 3;
526 }
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000527
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000528 *v += 6;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000529 }
530 }
531}
532
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000533///////////////////////////////////////////////////////////////////////////////
534
535/*
536 * Quadratic specified by 0=u^2-v canonical coords. u and v are the first
537 * two components of the vertex attribute. Coverage is based on signed
538 * distance with negative being inside, positive outside. The edge is specified in
539 * window space (y-down). If either the third or fourth component of the interpolated
540 * 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 +0000541 * attempt to trim to a portion of the infinite quad.
542 * Requires shader derivative instruction support.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000543 */
544
joshualitt249af152014-09-15 11:41:13 -0700545class QuadEdgeEffect : public GrGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000546public:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500547 static GrGeometryProcessor* Make(SkArenaAlloc* arena,
548 const SkMatrix& localMatrix,
549 bool usesLocalCoords,
550 bool wideColor) {
551 return arena->make<QuadEdgeEffect>(localMatrix, usesLocalCoords, wideColor);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000552 }
553
Brian Salomond3b65972017-03-22 12:05:03 -0400554 ~QuadEdgeEffect() override {}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000555
mtklein36352bf2015-03-25 18:17:31 -0700556 const char* name() const override { return "QuadEdge"; }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000557
egdaniel57d3b032015-11-13 11:57:27 -0800558 class GLSLProcessor : public GrGLSLGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000559 public:
Brian Salomon60fb0b22017-06-15 17:09:36 -0400560 GLSLProcessor() {}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000561
mtklein36352bf2015-03-25 18:17:31 -0700562 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
joshualitt2dd1ae02014-12-03 06:24:10 -0800563 const QuadEdgeEffect& qe = args.fGP.cast<QuadEdgeEffect>();
egdaniel4ca2e602015-11-18 08:01:26 -0800564 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800565 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800566 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualitt2dd1ae02014-12-03 06:24:10 -0800567
joshualittabb52a12015-01-13 15:02:10 -0800568 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800569 varyingHandler->emitAttributes(qe);
joshualittabb52a12015-01-13 15:02:10 -0800570
Chris Dalton27372882017-12-08 13:34:21 -0700571 GrGLSLVarying v(kHalf4_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800572 varyingHandler->addVarying("QuadEdge", &v);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500573 vertBuilder->codeAppendf("%s = %s;", v.vsOut(), qe.fInQuadEdge.name());
Brian Salomon60fb0b22017-06-15 17:09:36 -0400574
575 // Setup pass through color
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500576 varyingHandler->addPassThroughAttribute(qe.fInColor, args.fOutputColor);
joshualitt2dd1ae02014-12-03 06:24:10 -0800577
Chris Dalton60283612018-02-14 13:38:14 -0700578 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt9b989322014-12-15 14:16:27 -0800579
joshualittabb52a12015-01-13 15:02:10 -0800580 // Setup position
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500581 this->writeOutputPosition(vertBuilder, gpArgs, qe.fInPosition.name());
Michael Ludwig553db622020-06-19 10:47:30 -0400582 if (qe.fUsesLocalCoords) {
583 this->writeLocalCoord(vertBuilder, uniformHandler, gpArgs,
584 qe.fInPosition.asShaderVar(), qe.fLocalMatrix,
585 &fLocalMatrixUniform);
586 }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000587
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400588 fragBuilder->codeAppendf("half 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 Nicholase1f55022019-02-05 17:17:40 -0500591 fragBuilder->codeAppendf("half2 duvdx = half2(dFdx(%s.xy));", v.fsIn());
592 fragBuilder->codeAppendf("half2 duvdy = half2(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 Nicholasf7b88202017-09-18 14:10:39 -0400598 fragBuilder->codeAppendf("half2 gF = half2(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 = "
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400604 "saturate(0.5 - edgeAlpha / length(gF));}");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000605
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400606 fragBuilder->codeAppendf("%s = half4(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>();
Michael Ludwig553db622020-06-19 10:47:30 -0400613 uint32_t key = (uint32_t) qee.fUsesLocalCoords;
614 key |= ComputeMatrixKey(qee.fLocalMatrix) << 1;
615 b->add32(key);
joshualitt9b989322014-12-15 14:16:27 -0800616 }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000617
egdaniel018fb622015-10-28 07:26:40 -0700618 void setData(const GrGLSLProgramDataManager& pdman,
Brian Osman609f1592020-07-01 15:14:39 -0400619 const GrPrimitiveProcessor& gp) override {
joshualittb8c241a2015-05-19 08:23:30 -0700620 const QuadEdgeEffect& qe = gp.cast<QuadEdgeEffect>();
Michael Ludwig553db622020-06-19 10:47:30 -0400621 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:
John Stiles7571f9e2020-09-02 22:42:33 -0400625 using INHERITED = GrGLSLGeometryProcessor;
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
John Stiles7571f9e2020-09-02 22:42:33 -0400661 using INHERITED = GrGeometryProcessor;
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
Herb Derbyc76d4092020-10-07 16:46:15 -0400700 static GrOp::Owner Make(GrRecordingContext* context,
701 GrPaint&& paint,
702 const SkMatrix& viewMatrix,
703 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
Herb Derbyc76d4092020-10-07 16:46:15 -0400709 AAConvexPathOp(GrProcessorSet* processorSet, const SkPMColor4f& color,
Brian Osman936fe7d2018-10-30 15:30:35 -0400710 const SkMatrix& viewMatrix, const SkPath& path,
711 const GrUserStencilSettings* stencilSettings)
Herb Derbyc76d4092020-10-07 16:46:15 -0400712 : INHERITED(ClassID()), fHelper(processorSet, GrAAType::kCoverage, stencilSettings) {
Brian Salomon60fb0b22017-06-15 17:09:36 -0400713 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 Salomon10978a62017-06-15 16:21:49 -0400728 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
729
Chris Dalton6ce447a2019-06-23 18:07:38 -0600730 GrProcessorSet::Analysis finalize(
731 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
732 GrClampType clampType) override {
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700733 return fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600734 caps, clip, hasMixedSampledCoverage, clampType,
735 GrProcessorAnalysisCoverage::kSingleChannel, &fPaths.back().fColor, &fWideColor);
Brian Salomon10978a62017-06-15 16:21:49 -0400736 }
737
bsalomone46f9fe2015-08-18 06:05:14 -0700738private:
Robert Phillips2669a7b2020-03-12 12:07:19 -0400739 GrProgramInfo* programInfo() override { return fProgramInfo; }
740
Robert Phillips4133dc42020-03-11 15:55:55 -0400741 void onCreateProgramInfo(const GrCaps* caps,
742 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400743 const GrSurfaceProxyView* writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400744 GrAppliedClip&& appliedClip,
Greg Danield358cbe2020-09-11 09:33:54 -0400745 const GrXferProcessor::DstProxyView& dstProxyView,
746 GrXferBarrierFlags renderPassXferBarriers) override {
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400747 SkMatrix invert;
748 if (fHelper.usesLocalCoords() && !fPaths.back().fViewMatrix.invert(&invert)) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400749 return;
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400750 }
751
752 GrGeometryProcessor* quadProcessor = QuadEdgeEffect::Make(arena, invert,
753 fHelper.usesLocalCoords(),
754 fWideColor);
755
Brian Salomon8afde5f2020-04-01 16:22:00 -0400756 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400757 std::move(appliedClip),
758 dstProxyView, quadProcessor,
Greg Danield358cbe2020-09-11 09:33:54 -0400759 GrPrimitiveType::kTriangles,
760 renderPassXferBarriers);
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400761 }
762
Brian Salomon91326c32017-08-09 16:02:19 -0400763 void onPrepareDraws(Target* target) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500764 int instanceCount = fPaths.count();
joshualitt27f398f2015-02-05 14:39:01 -0800765
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400766 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400767 this->createProgramInfo(target);
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400768 if (!fProgramInfo) {
769 return;
770 }
joshualitt27f398f2015-02-05 14:39:01 -0800771 }
772
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400773 const size_t kVertexStride = fProgramInfo->primProc().vertexStride();
774
775 fDraws.reserve(instanceCount);
joshualitt27f398f2015-02-05 14:39:01 -0800776
joshualitt27f398f2015-02-05 14:39:01 -0800777 // TODO generate all segments for all paths and use one vertex buffer
778 for (int i = 0; i < instanceCount; i++) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500779 const PathData& args = fPaths[i];
joshualitt27f398f2015-02-05 14:39:01 -0800780
781 // We use the fact that SkPath::transform path does subdivision based on
782 // perspective. Otherwise, we apply the view matrix when copying to the
783 // segment representation.
784 const SkMatrix* viewMatrix = &args.fViewMatrix;
joshualitt144c3c82015-11-30 12:30:13 -0800785
786 // We avoid initializing the path unless we have to
787 const SkPath* pathPtr = &args.fPath;
788 SkTLazy<SkPath> tmpPath;
joshualitt27f398f2015-02-05 14:39:01 -0800789 if (viewMatrix->hasPerspective()) {
joshualitt144c3c82015-11-30 12:30:13 -0800790 SkPath* tmpPathPtr = tmpPath.init(*pathPtr);
791 tmpPathPtr->setIsVolatile(true);
792 tmpPathPtr->transform(*viewMatrix);
joshualitt27f398f2015-02-05 14:39:01 -0800793 viewMatrix = &SkMatrix::I();
joshualitt144c3c82015-11-30 12:30:13 -0800794 pathPtr = tmpPathPtr;
joshualitt27f398f2015-02-05 14:39:01 -0800795 }
796
797 int vertexCount;
798 int indexCount;
799 enum {
800 kPreallocSegmentCnt = 512 / sizeof(Segment),
801 kPreallocDrawCnt = 4,
802 };
803 SkSTArray<kPreallocSegmentCnt, Segment, true> segments;
804 SkPoint fanPt;
805
joshualitt144c3c82015-11-30 12:30:13 -0800806 if (!get_segments(*pathPtr, *viewMatrix, &segments, &fanPt, &vertexCount,
joshualitt27f398f2015-02-05 14:39:01 -0800807 &indexCount)) {
808 continue;
809 }
810
Brian Salomon12d22642019-01-29 14:38:50 -0500811 sk_sp<const GrBuffer> vertexBuffer;
Chris Daltonbca46e22017-05-15 11:03:26 -0600812 int firstVertex;
813
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500814 GrVertexWriter verts{target->makeVertexSpace(kVertexStride, vertexCount,
815 &vertexBuffer, &firstVertex)};
bsalomone64eb572015-05-07 11:35:55 -0700816
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500817 if (!verts.fPtr) {
joshualitt4b31de82015-03-05 14:33:41 -0800818 SkDebugf("Could not allocate vertices\n");
819 return;
820 }
821
Brian Salomon12d22642019-01-29 14:38:50 -0500822 sk_sp<const GrBuffer> indexBuffer;
Chris Daltonbca46e22017-05-15 11:03:26 -0600823 int firstIndex;
824
825 uint16_t *idxs = target->makeIndexSpace(indexCount, &indexBuffer, &firstIndex);
robertphillipse40d3972015-05-07 09:51:43 -0700826 if (!idxs) {
joshualitt4b31de82015-03-05 14:33:41 -0800827 SkDebugf("Could not allocate indices\n");
828 return;
829 }
830
joshualitt27f398f2015-02-05 14:39:01 -0800831 SkSTArray<kPreallocDrawCnt, Draw, true> draws;
Brian Osman7f4735b2019-01-02 13:55:10 +0000832 GrVertexColor color(args.fColor, fWideColor);
833 create_vertices(segments, fanPt, color, &draws, verts, idxs, kVertexStride);
joshualitt27f398f2015-02-05 14:39:01 -0800834
Chris Daltoneb694b72020-03-16 09:25:50 -0600835 GrSimpleMesh* meshes = target->allocMeshes(draws.count());
robertphillips44c31282015-09-03 12:58:48 -0700836 for (int j = 0; j < draws.count(); ++j) {
837 const Draw& draw = draws[j];
Brian Salomon7eae3e02018-08-07 14:02:38 +0000838 meshes[j].setIndexed(indexBuffer, draw.fIndexCnt, firstIndex, 0,
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600839 draw.fVertexCnt - 1, GrPrimitiveRestart::kNo, vertexBuffer,
840 firstVertex);
Chris Daltonbca46e22017-05-15 11:03:26 -0600841 firstIndex += draw.fIndexCnt;
842 firstVertex += draw.fVertexCnt;
joshualitt27f398f2015-02-05 14:39:01 -0800843 }
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400844
845 fDraws.push_back({ meshes, draws.count() });
joshualitt27f398f2015-02-05 14:39:01 -0800846 }
847 }
848
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700849 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400850 if (!fProgramInfo || fDraws.isEmpty()) {
851 return;
852 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500853
Chris Dalton765ed362020-03-16 17:34:44 -0600854 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
855 flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline());
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400856 for (int i = 0; i < fDraws.count(); ++i) {
Chris Dalton765ed362020-03-16 17:34:44 -0600857 for (int j = 0; j < fDraws[i].fMeshCount; ++j) {
858 flushState->drawMesh(fDraws[i].fMeshes[j]);
859 }
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400860 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700861 }
862
Herb Derbye25c3002020-10-27 15:57:27 -0400863 CombineResult onCombineIfPossible(GrOp* t, SkArenaAlloc*, const GrCaps& caps) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500864 AAConvexPathOp* that = t->cast<AAConvexPathOp>();
Brian Salomon10978a62017-06-15 16:21:49 -0400865 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000866 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700867 }
Brian Salomon10978a62017-06-15 16:21:49 -0400868 if (fHelper.usesLocalCoords() &&
Mike Reed2c383152019-12-18 16:47:47 -0500869 !SkMatrixPriv::CheapEqual(fPaths[0].fViewMatrix, that->fPaths[0].fViewMatrix)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000870 return CombineResult::kCannotCombine;
joshualitt27f398f2015-02-05 14:39:01 -0800871 }
872
Brian Salomond0a0a652016-12-15 15:25:22 -0500873 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
Brian Osman7f4735b2019-01-02 13:55:10 +0000874 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000875 return CombineResult::kMerged;
joshualitt27f398f2015-02-05 14:39:01 -0800876 }
877
John Stilesaf366522020-08-13 09:57:34 -0400878#if GR_TEST_UTILS
879 SkString onDumpInfo() const override {
880 return SkStringPrintf("Count: %d\n%s", fPaths.count(), fHelper.dumpInfo().c_str());
881 }
882#endif
883
Brian Salomond0a0a652016-12-15 15:25:22 -0500884 struct PathData {
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400885 SkMatrix fViewMatrix;
886 SkPath fPath;
Brian Osmancf860852018-10-31 14:04:39 -0400887 SkPMColor4f fColor;
bsalomonf1703092016-06-29 18:41:53 -0700888 };
889
Brian Salomon10978a62017-06-15 16:21:49 -0400890 Helper fHelper;
Brian Salomond0a0a652016-12-15 15:25:22 -0500891 SkSTArray<1, PathData, true> fPaths;
Brian Osman7f4735b2019-01-02 13:55:10 +0000892 bool fWideColor;
reed1b55a962015-09-17 20:16:13 -0700893
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400894 struct MeshDraw {
Chris Daltoneb694b72020-03-16 09:25:50 -0600895 GrSimpleMesh* fMeshes;
896 int fMeshCount;
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400897 };
898
899 SkTDArray<MeshDraw> fDraws;
900 GrProgramInfo* fProgramInfo = nullptr;
901
John Stiles7571f9e2020-09-02 22:42:33 -0400902 using INHERITED = GrMeshDrawOp;
joshualitt27f398f2015-02-05 14:39:01 -0800903};
904
Brian Salomon10978a62017-06-15 16:21:49 -0400905} // anonymous namespace
906
bsalomon0aff2fa2015-07-31 06:48:27 -0700907bool GrAAConvexPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400908 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700909 "GrAAConvexPathRenderer::onDrawPath");
Chris Dalton6ce447a2019-06-23 18:07:38 -0600910 SkASSERT(args.fRenderTargetContext->numSamples() <= 1);
bsalomon8acedde2016-06-24 10:42:16 -0700911 SkASSERT(!args.fShape->isEmpty());
bsalomon@google.com4647f902013-03-26 14:45:27 +0000912
bsalomonf1703092016-06-29 18:41:53 -0700913 SkPath path;
914 args.fShape->asPath(&path);
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000915
Herb Derbyc76d4092020-10-07 16:46:15 -0400916 GrOp::Owner op = AAConvexPathOp::Make(args.fContext, std::move(args.fPaint),
917 *args.fViewMatrix,
918 path, args.fUserStencilSettings);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400919 args.fRenderTargetContext->addDrawOp(args.fClip, std::move(op));
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000920 return true;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000921}
joshualitt8e5c1772015-05-11 08:58:52 -0700922
923///////////////////////////////////////////////////////////////////////////////////////////////////
924
Hal Canary6f6961e2017-01-31 13:50:44 -0500925#if GR_TEST_UTILS
joshualitt8e5c1772015-05-11 08:58:52 -0700926
Brian Salomon10978a62017-06-15 16:21:49 -0400927GR_DRAW_OP_TEST_DEFINE(AAConvexPathOp) {
bsalomonf1703092016-06-29 18:41:53 -0700928 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
John Stiles31954bf2020-08-07 17:35:54 -0400929 const SkPath& path = GrTest::TestPathConvex(random);
Brian Salomon10978a62017-06-15 16:21:49 -0400930 const GrUserStencilSettings* stencilSettings = GrGetRandomStencil(random, context);
Robert Phillips7c525e62018-06-12 10:11:12 -0400931 return AAConvexPathOp::Make(context, std::move(paint), viewMatrix, path, stencilSettings);
joshualitt8e5c1772015-05-11 08:58:52 -0700932}
933
934#endif