blob: 316eb542652b06ff293aaf6c6cadedeaa3401fab [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,
reed026beb52015-06-10 14:23:15 -0700120 SkPathPriv::FirstDirection 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;
reed026beb52015-06-10 14:23:15 -0700130 if (dir == SkPathPriv::kCCW_FirstDirection) {
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,
226 SkPathPriv::FirstDirection* 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.
231 if (!SkPathPriv::CheapComputeFirstDirection(path, dir)) {
232 return false;
233 }
Michael Ludwig0a1e9ef2019-08-30 10:03:15 -0400234
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000235 // check whether m reverses the orientation
tfarina@chromium.orgf6de4752013-08-17 00:02:59 +0000236 SkASSERT(!m.hasPerspective());
Mike Reed8be952a2017-02-13 20:44:33 -0500237 SkScalar det2x2 = m.get(SkMatrix::kMScaleX) * m.get(SkMatrix::kMScaleY) -
238 m.get(SkMatrix::kMSkewX) * m.get(SkMatrix::kMSkewY);
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000239 if (det2x2 < 0) {
Brian Osman2caecd22019-09-25 14:02:26 -0400240 *dir = SkPathPriv::OppositeFirstDirection(*dir);
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000241 }
Michael Ludwig0a1e9ef2019-08-30 10:03:15 -0400242
Brian Osman2caecd22019-09-25 14:02:26 -0400243 return true;
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000244}
245
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000246static inline void add_line_to_segment(const SkPoint& pt,
joshualitt27f398f2015-02-05 14:39:01 -0800247 SegmentArray* segments) {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000248 segments->push_back();
249 segments->back().fType = Segment::kLine;
250 segments->back().fPts[0] = pt;
251}
252
commit-bot@chromium.org106655e2013-09-03 21:28:55 +0000253static inline void add_quad_segment(const SkPoint pts[3],
joshualitt27f398f2015-02-05 14:39:01 -0800254 SegmentArray* segments) {
Brian Salomon5f8a62b2019-03-29 13:55:32 -0400255 if (SkPointPriv::DistanceToLineSegmentBetweenSqd(pts[1], pts[0], pts[2]) < kCloseSqd) {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000256 if (pts[0] != pts[2]) {
joshualitt27f398f2015-02-05 14:39:01 -0800257 add_line_to_segment(pts[2], segments);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000258 }
259 } else {
260 segments->push_back();
261 segments->back().fType = Segment::kQuad;
262 segments->back().fPts[0] = pts[1];
263 segments->back().fPts[1] = pts[2];
264 }
265}
266
267static inline void add_cubic_segments(const SkPoint pts[4],
reed026beb52015-06-10 14:23:15 -0700268 SkPathPriv::FirstDirection dir,
joshualitt27f398f2015-02-05 14:39:01 -0800269 SegmentArray* segments) {
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000270 SkSTArray<15, SkPoint, true> quads;
bsalomon18fab302016-02-16 08:00:05 -0800271 GrPathUtils::convertCubicToQuadsConstrainToTangents(pts, SK_Scalar1, dir, &quads);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000272 int count = quads.count();
273 for (int q = 0; q < count; q += 3) {
joshualitt27f398f2015-02-05 14:39:01 -0800274 add_quad_segment(&quads[q], segments);
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000275 }
276}
277
278static bool get_segments(const SkPath& path,
279 const SkMatrix& m,
280 SegmentArray* segments,
281 SkPoint* fanPt,
282 int* vCount,
joshualitt27f398f2015-02-05 14:39:01 -0800283 int* iCount) {
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000284 SkPath::Iter iter(path, true);
bsalomon@google.com30c174b2012-11-13 14:36:42 +0000285 // This renderer over-emphasizes very thin path regions. We use the distance
bsalomon@google.com5cc90d12012-01-17 16:28:34 +0000286 // to the path from the sample to compute coverage. Every pixel intersected
287 // 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 +0000288 // notice that the sample may be close to a very thin area of the path and
bsalomon@google.com5cc90d12012-01-17 16:28:34 +0000289 // thus should be very light. This is particularly egregious for degenerate
290 // line paths. We detect paths that are very close to a line (zero area) and
291 // draw nothing.
bsalomon@google.com9732f622012-01-31 15:19:21 +0000292 DegenerateTestData degenerateData;
Brian Osman2caecd22019-09-25 14:02:26 -0400293 SkPathPriv::FirstDirection dir;
294 if (!get_direction(path, m, &dir)) {
295 return false;
296 }
bsalomon@google.com9732f622012-01-31 15:19:21 +0000297
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000298 for (;;) {
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000299 SkPoint pts[4];
Mike Reedba7e9a62019-08-16 13:30:34 -0400300 SkPath::Verb verb = iter.next(pts);
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000301 switch (verb) {
302 case SkPath::kMove_Verb:
bsalomon@google.com1a38d552012-03-15 14:40:46 +0000303 m.mapPoints(pts, 1);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000304 update_degenerate_test(&degenerateData, pts[0]);
305 break;
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000306 case SkPath::kLine_Verb: {
Mike Reedba7e9a62019-08-16 13:30:34 -0400307 if (!SkPathPriv::AllPointsEq(pts, 2)) {
308 m.mapPoints(&pts[1], 1);
309 update_degenerate_test(&degenerateData, pts[1]);
310 add_line_to_segment(pts[1], segments);
311 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000312 break;
313 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000314 case SkPath::kQuad_Verb:
Mike Reedba7e9a62019-08-16 13:30:34 -0400315 if (!SkPathPriv::AllPointsEq(pts, 3)) {
316 m.mapPoints(pts, 3);
317 update_degenerate_test(&degenerateData, pts[1]);
318 update_degenerate_test(&degenerateData, pts[2]);
319 add_quad_segment(pts, segments);
320 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000321 break;
egdanielaf18a092015-01-05 10:22:28 -0800322 case SkPath::kConic_Verb: {
Mike Reedba7e9a62019-08-16 13:30:34 -0400323 if (!SkPathPriv::AllPointsEq(pts, 3)) {
324 m.mapPoints(pts, 3);
325 SkScalar weight = iter.conicWeight();
326 SkAutoConicToQuads converter;
327 const SkPoint* quadPts = converter.computeQuads(pts, weight, 0.25f);
328 for (int i = 0; i < converter.countQuads(); ++i) {
329 update_degenerate_test(&degenerateData, quadPts[2*i + 1]);
330 update_degenerate_test(&degenerateData, quadPts[2*i + 2]);
331 add_quad_segment(quadPts + 2*i, segments);
332 }
egdanielaf18a092015-01-05 10:22:28 -0800333 }
334 break;
335 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000336 case SkPath::kCubic_Verb: {
Mike Reedba7e9a62019-08-16 13:30:34 -0400337 if (!SkPathPriv::AllPointsEq(pts, 4)) {
338 m.mapPoints(pts, 4);
339 update_degenerate_test(&degenerateData, pts[1]);
340 update_degenerate_test(&degenerateData, pts[2]);
341 update_degenerate_test(&degenerateData, pts[3]);
342 add_cubic_segments(pts, dir, segments);
343 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000344 break;
Brian Salomon23356442018-11-30 15:33:19 -0500345 }
bsalomon@google.com94b284d2013-05-10 17:14:06 +0000346 case SkPath::kDone_Verb:
bsalomon@google.com9732f622012-01-31 15:19:21 +0000347 if (degenerateData.isDegenerate()) {
348 return false;
349 } else {
Greg Daniel8b09b962018-04-03 14:53:45 -0400350 return compute_vectors(segments, fanPt, dir, vCount, iCount);
bsalomon@google.com9732f622012-01-31 15:19:21 +0000351 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000352 default:
353 break;
354 }
355 }
356}
357
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000358struct Draw {
359 Draw() : fVertexCnt(0), fIndexCnt(0) {}
360 int fVertexCnt;
361 int fIndexCnt;
362};
363
364typedef SkTArray<Draw, true> DrawArray;
365
Brian Salomon60fb0b22017-06-15 17:09:36 -0400366static void create_vertices(const SegmentArray& segments,
commit-bot@chromium.orgfdfbb9d2013-08-15 18:16:27 +0000367 const SkPoint& fanPt,
Brian Osman7f4735b2019-01-02 13:55:10 +0000368 const GrVertexColor& color,
Brian Salomon60fb0b22017-06-15 17:09:36 -0400369 DrawArray* draws,
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500370 GrVertexWriter& verts,
371 uint16_t* idxs,
372 size_t vertexStride) {
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000373 Draw* draw = &draws->push_back();
374 // alias just to make vert/index assignments easier to read.
375 int* v = &draw->fVertexCnt;
376 int* i = &draw->fIndexCnt;
Brian Osman7f4735b2019-01-02 13:55:10 +0000377 const size_t uvOffset = sizeof(SkPoint) + color.size();
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000378
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000379 int count = segments.count();
380 for (int a = 0; a < count; ++a) {
381 const Segment& sega = segments[a];
382 int b = (a + 1) % count;
383 const Segment& segb = segments[b];
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000384
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000385 // Check whether adding the verts for this segment to the current draw would cause index
386 // values to overflow.
387 int vCount = 4;
388 if (Segment::kLine == segb.fType) {
389 vCount += 5;
390 } else {
391 vCount += 6;
392 }
393 if (draw->fVertexCnt + vCount > (1 << 16)) {
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000394 idxs += *i;
395 draw = &draws->push_back();
396 v = &draw->fVertexCnt;
397 i = &draw->fIndexCnt;
398 }
399
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500400 const SkScalar negOneDists[2] = { -SK_Scalar1, -SK_Scalar1 };
401
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000402 // FIXME: These tris are inset in the 1 unit arc around the corner
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500403 SkPoint p0 = sega.endPt();
404 // Position, Color, UV, D0, D1
405 verts.write(p0, color, SkPoint{0, 0}, negOneDists);
406 verts.write(p0 + sega.endNorm(), color, SkPoint{0, -SK_Scalar1}, negOneDists);
407 verts.write(p0 + segb.fMid, color, SkPoint{0, -SK_Scalar1}, negOneDists);
408 verts.write(p0 + segb.fNorms[0], color, SkPoint{0, -SK_Scalar1}, negOneDists);
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000409
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000410 idxs[*i + 0] = *v + 0;
411 idxs[*i + 1] = *v + 2;
412 idxs[*i + 2] = *v + 1;
413 idxs[*i + 3] = *v + 0;
414 idxs[*i + 4] = *v + 3;
415 idxs[*i + 5] = *v + 2;
rmistry@google.comfbfcd562012-08-23 18:09:54 +0000416
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000417 *v += 4;
418 *i += 6;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000419
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000420 if (Segment::kLine == segb.fType) {
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000421 // we draw the line edge as a degenerate quad (u is 0, v is the
422 // signed distance to the edge)
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500423 SkPoint v1Pos = sega.endPt();
424 SkPoint v2Pos = segb.fPts[0];
425 SkScalar dist = SkPointPriv::DistanceToLineBetween(fanPt, v1Pos, v2Pos);
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000426
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500427 verts.write(fanPt, color, SkPoint{0, dist}, negOneDists);
428 verts.write(v1Pos, color, SkPoint{0, 0}, negOneDists);
429 verts.write(v2Pos, color, SkPoint{0, 0}, negOneDists);
430 verts.write(v1Pos + segb.fNorms[0], color, SkPoint{0, -SK_Scalar1}, negOneDists);
431 verts.write(v2Pos + segb.fNorms[0], color, SkPoint{0, -SK_Scalar1}, negOneDists);
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000432
cdalton35964822015-04-29 10:14:03 -0700433 idxs[*i + 0] = *v + 3;
434 idxs[*i + 1] = *v + 1;
435 idxs[*i + 2] = *v + 2;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000436
cdalton35964822015-04-29 10:14:03 -0700437 idxs[*i + 3] = *v + 4;
438 idxs[*i + 4] = *v + 3;
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000439 idxs[*i + 5] = *v + 2;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000440
cdalton35964822015-04-29 10:14:03 -0700441 *i += 6;
442
443 // Draw the interior fan if it exists.
444 // TODO: Detect and combine colinear segments. This will ensure we catch every case
445 // with no interior, and that the resulting shared edge uses the same endpoints.
446 if (count >= 3) {
447 idxs[*i + 0] = *v + 0;
448 idxs[*i + 1] = *v + 2;
449 idxs[*i + 2] = *v + 1;
450
451 *i += 3;
452 }
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000453
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000454 *v += 5;
bsalomon@google.com06809612012-01-21 15:03:39 +0000455 } else {
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500456 void* quadVertsBegin = verts.fPtr;
457
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000458 SkPoint qpts[] = {sega.endPt(), segb.fPts[0], segb.fPts[1]};
bsalomon@google.com495e2102012-01-21 14:48:36 +0000459
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500460 SkScalar c0 = segb.fNorms[0].dot(qpts[0]);
461 SkScalar c1 = segb.fNorms[1].dot(qpts[2]);
462 GrVertexWriter::Skip<SkPoint> skipUVs;
463
464 verts.write(fanPt,
465 color, skipUVs,
466 -segb.fNorms[0].dot(fanPt) + c0,
467 -segb.fNorms[1].dot(fanPt) + c1);
468
469 verts.write(qpts[0],
470 color, skipUVs,
471 0.0f,
472 -segb.fNorms[1].dot(qpts[0]) + c1);
473
474 verts.write(qpts[2],
475 color, skipUVs,
476 -segb.fNorms[0].dot(qpts[2]) + c0,
477 0.0f);
Greg Daniel1204c4b2020-04-22 14:10:12 -0400478 // We need a negative value that is very large that it won't effect results if it is
479 // interpolated with. However, the value can't be too large of a negative that it
480 // effects numerical precision on less powerful GPUs.
481 static const SkScalar kStableLargeNegativeValue = -SK_ScalarMax/1000000;
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500482 verts.write(qpts[0] + segb.fNorms[0],
483 color, skipUVs,
Greg Daniel1204c4b2020-04-22 14:10:12 -0400484 kStableLargeNegativeValue,
485 kStableLargeNegativeValue);
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500486
487 verts.write(qpts[2] + segb.fNorms[1],
488 color, skipUVs,
Greg Daniel1204c4b2020-04-22 14:10:12 -0400489 kStableLargeNegativeValue,
490 kStableLargeNegativeValue);
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500491
commit-bot@chromium.org972f9cd2014-03-28 17:58:28 +0000492 SkVector midVec = segb.fNorms[0] + segb.fNorms[1];
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000493 midVec.normalize();
bsalomon@google.com06809612012-01-21 15:03:39 +0000494
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500495 verts.write(qpts[1] + midVec,
496 color, skipUVs,
Greg Daniel1204c4b2020-04-22 14:10:12 -0400497 kStableLargeNegativeValue,
498 kStableLargeNegativeValue);
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000499
bsalomon@google.com19713172012-03-15 13:51:08 +0000500 GrPathUtils::QuadUVMatrix toUV(qpts);
Brian Osman7f4735b2019-01-02 13:55:10 +0000501 toUV.apply(quadVertsBegin, 6, vertexStride, uvOffset);
bsalomon@google.com06809612012-01-21 15:03:39 +0000502
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000503 idxs[*i + 0] = *v + 3;
504 idxs[*i + 1] = *v + 1;
505 idxs[*i + 2] = *v + 2;
506 idxs[*i + 3] = *v + 4;
507 idxs[*i + 4] = *v + 3;
508 idxs[*i + 5] = *v + 2;
bsalomon@google.com06809612012-01-21 15:03:39 +0000509
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000510 idxs[*i + 6] = *v + 5;
511 idxs[*i + 7] = *v + 3;
512 idxs[*i + 8] = *v + 4;
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000513
cdalton35964822015-04-29 10:14:03 -0700514 *i += 9;
515
516 // Draw the interior fan if it exists.
517 // TODO: Detect and combine colinear segments. This will ensure we catch every case
518 // with no interior, and that the resulting shared edge uses the same endpoints.
519 if (count >= 3) {
520 idxs[*i + 0] = *v + 0;
521 idxs[*i + 1] = *v + 2;
522 idxs[*i + 2] = *v + 1;
523
524 *i += 3;
525 }
bsalomon@google.com9aed1142012-01-30 14:28:39 +0000526
bsalomon@google.com7d9ffc82013-05-14 14:20:28 +0000527 *v += 6;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000528 }
529 }
530}
531
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000532///////////////////////////////////////////////////////////////////////////////
533
534/*
535 * Quadratic specified by 0=u^2-v canonical coords. u and v are the first
536 * two components of the vertex attribute. Coverage is based on signed
537 * distance with negative being inside, positive outside. The edge is specified in
538 * window space (y-down). If either the third or fourth component of the interpolated
539 * vertex coord is > 0 then the pixel is considered outside the edge. This is used to
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000540 * attempt to trim to a portion of the infinite quad.
541 * Requires shader derivative instruction support.
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000542 */
543
joshualitt249af152014-09-15 11:41:13 -0700544class QuadEdgeEffect : public GrGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000545public:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500546 static GrGeometryProcessor* Make(SkArenaAlloc* arena,
547 const SkMatrix& localMatrix,
548 bool usesLocalCoords,
549 bool wideColor) {
550 return arena->make<QuadEdgeEffect>(localMatrix, usesLocalCoords, wideColor);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000551 }
552
Brian Salomond3b65972017-03-22 12:05:03 -0400553 ~QuadEdgeEffect() override {}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000554
mtklein36352bf2015-03-25 18:17:31 -0700555 const char* name() const override { return "QuadEdge"; }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000556
egdaniel57d3b032015-11-13 11:57:27 -0800557 class GLSLProcessor : public GrGLSLGeometryProcessor {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000558 public:
Brian Salomon60fb0b22017-06-15 17:09:36 -0400559 GLSLProcessor() {}
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000560
mtklein36352bf2015-03-25 18:17:31 -0700561 void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override {
joshualitt2dd1ae02014-12-03 06:24:10 -0800562 const QuadEdgeEffect& qe = args.fGP.cast<QuadEdgeEffect>();
egdaniel4ca2e602015-11-18 08:01:26 -0800563 GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder;
egdaniel0eafe792015-11-20 14:01:22 -0800564 GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler;
egdaniel7ea439b2015-12-03 09:20:44 -0800565 GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
joshualitt2dd1ae02014-12-03 06:24:10 -0800566
joshualittabb52a12015-01-13 15:02:10 -0800567 // emit attributes
egdaniel0eafe792015-11-20 14:01:22 -0800568 varyingHandler->emitAttributes(qe);
joshualittabb52a12015-01-13 15:02:10 -0800569
Chris Dalton27372882017-12-08 13:34:21 -0700570 GrGLSLVarying v(kHalf4_GrSLType);
egdaniel0eafe792015-11-20 14:01:22 -0800571 varyingHandler->addVarying("QuadEdge", &v);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500572 vertBuilder->codeAppendf("%s = %s;", v.vsOut(), qe.fInQuadEdge.name());
Brian Salomon60fb0b22017-06-15 17:09:36 -0400573
574 // Setup pass through color
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500575 varyingHandler->addPassThroughAttribute(qe.fInColor, args.fOutputColor);
joshualitt2dd1ae02014-12-03 06:24:10 -0800576
Chris Dalton60283612018-02-14 13:38:14 -0700577 GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
joshualitt9b989322014-12-15 14:16:27 -0800578
joshualittabb52a12015-01-13 15:02:10 -0800579 // Setup position
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500580 this->writeOutputPosition(vertBuilder, gpArgs, qe.fInPosition.name());
Michael Ludwig553db622020-06-19 10:47:30 -0400581 if (qe.fUsesLocalCoords) {
582 this->writeLocalCoord(vertBuilder, uniformHandler, gpArgs,
583 qe.fInPosition.asShaderVar(), qe.fLocalMatrix,
584 &fLocalMatrixUniform);
585 }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000586
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400587 fragBuilder->codeAppendf("half edgeAlpha;");
joshualitt30ba4362014-08-21 20:18:45 -0700588
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000589 // keep the derivative instructions outside the conditional
Ethan Nicholase1f55022019-02-05 17:17:40 -0500590 fragBuilder->codeAppendf("half2 duvdx = half2(dFdx(%s.xy));", v.fsIn());
591 fragBuilder->codeAppendf("half2 duvdy = half2(dFdy(%s.xy));", v.fsIn());
egdaniel4ca2e602015-11-18 08:01:26 -0800592 fragBuilder->codeAppendf("if (%s.z > 0.0 && %s.w > 0.0) {", v.fsIn(), v.fsIn());
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000593 // today we know z and w are in device space. We could use derivatives
egdaniel4ca2e602015-11-18 08:01:26 -0800594 fragBuilder->codeAppendf("edgeAlpha = min(min(%s.z, %s.w) + 0.5, 1.0);", v.fsIn(),
595 v.fsIn());
596 fragBuilder->codeAppendf ("} else {");
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400597 fragBuilder->codeAppendf("half2 gF = half2(2.0*%s.x*duvdx.x - duvdx.y,"
egdaniel4ca2e602015-11-18 08:01:26 -0800598 " 2.0*%s.x*duvdy.x - duvdy.y);",
599 v.fsIn(), v.fsIn());
600 fragBuilder->codeAppendf("edgeAlpha = (%s.x*%s.x - %s.y);", v.fsIn(), v.fsIn(),
601 v.fsIn());
602 fragBuilder->codeAppendf("edgeAlpha = "
Ethan Nicholas12fb9cf2018-08-03 16:16:57 -0400603 "saturate(0.5 - edgeAlpha / length(gF));}");
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000604
Ethan Nicholasf7b88202017-09-18 14:10:39 -0400605 fragBuilder->codeAppendf("%s = half4(edgeAlpha);", args.fOutputCoverage);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000606 }
607
joshualitt9b989322014-12-15 14:16:27 -0800608 static inline void GenKey(const GrGeometryProcessor& gp,
Brian Salomon94efbf52016-11-29 13:43:05 -0500609 const GrShaderCaps&,
joshualitt9b989322014-12-15 14:16:27 -0800610 GrProcessorKeyBuilder* b) {
joshualitte3ababe2015-05-15 07:56:07 -0700611 const QuadEdgeEffect& qee = gp.cast<QuadEdgeEffect>();
Michael Ludwig553db622020-06-19 10:47:30 -0400612 uint32_t key = (uint32_t) qee.fUsesLocalCoords;
613 key |= ComputeMatrixKey(qee.fLocalMatrix) << 1;
614 b->add32(key);
joshualitt9b989322014-12-15 14:16:27 -0800615 }
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000616
egdaniel018fb622015-10-28 07:26:40 -0700617 void setData(const GrGLSLProgramDataManager& pdman,
bsalomona624bf32016-09-20 09:12:47 -0700618 const GrPrimitiveProcessor& gp,
Brian Salomonc241b582019-11-27 08:57:17 -0500619 const CoordTransformRange& transformRange) override {
joshualittb8c241a2015-05-19 08:23:30 -0700620 const QuadEdgeEffect& qe = gp.cast<QuadEdgeEffect>();
Michael Ludwig553db622020-06-19 10:47:30 -0400621 this->setTransformDataHelper(pdman, transformRange);
622 this->setTransform(pdman, fLocalMatrixUniform, qe.fLocalMatrix, &fLocalMatrix);
joshualitte3ababe2015-05-15 07:56:07 -0700623 }
624
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000625 private:
egdaniele659a582015-11-13 09:55:43 -0800626 typedef GrGLSLGeometryProcessor INHERITED;
Michael Ludwig553db622020-06-19 10:47:30 -0400627
628 SkMatrix fLocalMatrix = SkMatrix::InvalidMatrix();
629 UniformHandle fLocalMatrixUniform;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000630 };
skia.committer@gmail.com041e2db2013-04-03 07:01:14 +0000631
Brian Salomon94efbf52016-11-29 13:43:05 -0500632 void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override {
egdaniel57d3b032015-11-13 11:57:27 -0800633 GLSLProcessor::GenKey(*this, caps, b);
joshualitteb2a6762014-12-04 11:35:33 -0800634 }
635
Brian Salomon94efbf52016-11-29 13:43:05 -0500636 GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override {
egdaniel57d3b032015-11-13 11:57:27 -0800637 return new GLSLProcessor();
joshualitteb2a6762014-12-04 11:35:33 -0800638 }
639
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000640private:
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500641 friend class ::SkArenaAlloc; // for access to ctor
642
Brian Osman7f4735b2019-01-02 13:55:10 +0000643 QuadEdgeEffect(const SkMatrix& localMatrix, bool usesLocalCoords, bool wideColor)
Ethan Nicholasabff9562017-10-09 10:54:08 -0400644 : INHERITED(kQuadEdgeEffect_ClassID)
645 , fLocalMatrix(localMatrix)
646 , fUsesLocalCoords(usesLocalCoords) {
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500647 fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType};
Brian Osman7f4735b2019-01-02 13:55:10 +0000648 fInColor = MakeColorAttribute("inColor", wideColor);
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500649 fInQuadEdge = {"inQuadEdge", kFloat4_GrVertexAttribType, kHalf4_GrSLType};
650 this->setVertexAttributes(&fInPosition, 3);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000651 }
652
Brian Osmanf04fb3c2018-11-12 15:34:00 -0500653 Attribute fInPosition;
654 Attribute fInColor;
655 Attribute fInQuadEdge;
656
Brian Salomon92be2f72018-06-19 14:33:47 -0400657 SkMatrix fLocalMatrix;
658 bool fUsesLocalCoords;
joshualitt249af152014-09-15 11:41:13 -0700659
Brian Salomon0c26a9d2017-07-06 10:09:38 -0400660 GR_DECLARE_GEOMETRY_PROCESSOR_TEST
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000661
joshualitt2e3b3e32014-12-09 13:31:14 -0800662 typedef GrGeometryProcessor INHERITED;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000663};
664
joshualittb0a8a372014-09-23 09:50:21 -0700665GR_DEFINE_GEOMETRY_PROCESSOR_TEST(QuadEdgeEffect);
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000666
Hal Canary6f6961e2017-01-31 13:50:44 -0500667#if GR_TEST_UTILS
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500668GrGeometryProcessor* QuadEdgeEffect::TestCreate(GrProcessorTestData* d) {
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000669 // Doesn't work without derivative instructions.
Robert Phillips296b1cc2017-03-15 10:42:12 -0400670 return d->caps()->shaderCaps()->shaderDerivativeSupport()
Robert Phillips7cd0bfe2019-11-20 16:08:10 -0500671 ? QuadEdgeEffect::Make(d->allocator(), GrTest::TestMatrix(d->fRandom),
672 d->fRandom->nextBool(), d->fRandom->nextBool())
Brian Salomon9ae32a22017-01-25 14:58:24 -0500673 : nullptr;
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000674}
Hal Canary6f6961e2017-01-31 13:50:44 -0500675#endif
commit-bot@chromium.org90c240a2013-04-02 17:57:21 +0000676
677///////////////////////////////////////////////////////////////////////////////
678
Chris Dalton5ed44232017-09-07 13:22:46 -0600679GrPathRenderer::CanDrawPath
680GrAAConvexPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const {
Michael Ludwig0a1e9ef2019-08-30 10:03:15 -0400681 // This check requires convexity and known direction, since the direction is used to build
682 // the geometry segments. Degenerate convex paths will fall through to some other path renderer.
Chris Dalton5ed44232017-09-07 13:22:46 -0600683 if (args.fCaps->shaderCaps()->shaderDerivativeSupport() &&
Chris Dalton6ce447a2019-06-23 18:07:38 -0600684 (GrAAType::kCoverage == args.fAAType) && args.fShape->style().isSimpleFill() &&
Michael Ludwig0a1e9ef2019-08-30 10:03:15 -0400685 !args.fShape->inverseFilled() && args.fShape->knownToBeConvex() &&
686 args.fShape->knownDirection()) {
Chris Dalton5ed44232017-09-07 13:22:46 -0600687 return CanDrawPath::kYes;
688 }
689 return CanDrawPath::kNo;
robertphillips@google.comfa662942012-05-17 12:20:22 +0000690}
691
Brian Salomon10978a62017-06-15 16:21:49 -0400692namespace {
693
694class AAConvexPathOp final : public GrMeshDrawOp {
695private:
696 using Helper = GrSimpleMeshDrawOpHelperWithStencil;
697
joshualitt27f398f2015-02-05 14:39:01 -0800698public:
Brian Salomon25a88092016-12-01 09:36:50 -0500699 DEFINE_OP_CLASS_ID
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400700
Robert Phillipsb97da532019-02-12 15:24:12 -0500701 static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context,
Robert Phillips7c525e62018-06-12 10:11:12 -0400702 GrPaint&& paint,
703 const SkMatrix& viewMatrix,
Brian Salomon10978a62017-06-15 16:21:49 -0400704 const SkPath& path,
705 const GrUserStencilSettings* stencilSettings) {
Robert Phillips7c525e62018-06-12 10:11:12 -0400706 return Helper::FactoryHelper<AAConvexPathOp>(context, std::move(paint), viewMatrix, path,
Brian Salomon10978a62017-06-15 16:21:49 -0400707 stencilSettings);
708 }
709
Brian Osmancf860852018-10-31 14:04:39 -0400710 AAConvexPathOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color,
Brian Osman936fe7d2018-10-30 15:30:35 -0400711 const SkMatrix& viewMatrix, const SkPath& path,
712 const GrUserStencilSettings* stencilSettings)
Brian Salomon60fb0b22017-06-15 17:09:36 -0400713 : INHERITED(ClassID()), fHelper(helperArgs, GrAAType::kCoverage, stencilSettings) {
714 fPaths.emplace_back(PathData{viewMatrix, path, color});
Greg Daniel5faf4742019-10-01 15:14:44 -0400715 this->setTransformedBounds(path.getBounds(), viewMatrix, HasAABloat::kYes,
716 IsHairline::kNo);
bsalomonf1703092016-06-29 18:41:53 -0700717 }
joshualitt27f398f2015-02-05 14:39:01 -0800718
Brian Salomond0a0a652016-12-15 15:25:22 -0500719 const char* name() const override { return "AAConvexPathOp"; }
joshualitt27f398f2015-02-05 14:39:01 -0800720
Chris Dalton1706cbf2019-05-21 19:35:29 -0600721 void visitProxies(const VisitProxyFunc& func) const override {
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400722 if (fProgramInfo) {
Chris Daltonbe457422020-03-16 18:05:03 -0600723 fProgramInfo->visitFPProxies(func);
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400724 } else {
725 fHelper.visitProxies(func);
726 }
Robert Phillipsb493eeb2017-09-13 13:10:52 -0400727 }
728
Brian Osman9a390ac2018-11-12 09:47:48 -0500729#ifdef SK_DEBUG
Brian Salomon7c3e7182016-12-01 09:35:30 -0500730 SkString dumpInfo() const override {
731 SkString string;
Brian Salomon60fb0b22017-06-15 17:09:36 -0400732 string.appendf("Count: %d\n", fPaths.count());
Brian Salomon10978a62017-06-15 16:21:49 -0400733 string += fHelper.dumpInfo();
734 string += INHERITED::dumpInfo();
Brian Salomon7c3e7182016-12-01 09:35:30 -0500735 return string;
736 }
Brian Osman9a390ac2018-11-12 09:47:48 -0500737#endif
Brian Salomon7c3e7182016-12-01 09:35:30 -0500738
Brian Salomon10978a62017-06-15 16:21:49 -0400739 FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); }
740
Chris Dalton6ce447a2019-06-23 18:07:38 -0600741 GrProcessorSet::Analysis finalize(
742 const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage,
743 GrClampType clampType) override {
Chris Daltonb8fff0d2019-03-05 10:11:58 -0700744 return fHelper.finalizeProcessors(
Chris Dalton6ce447a2019-06-23 18:07:38 -0600745 caps, clip, hasMixedSampledCoverage, clampType,
746 GrProcessorAnalysisCoverage::kSingleChannel, &fPaths.back().fColor, &fWideColor);
Brian Salomon10978a62017-06-15 16:21:49 -0400747 }
748
bsalomone46f9fe2015-08-18 06:05:14 -0700749private:
Robert Phillips2669a7b2020-03-12 12:07:19 -0400750 GrProgramInfo* programInfo() override { return fProgramInfo; }
751
Robert Phillips4133dc42020-03-11 15:55:55 -0400752 void onCreateProgramInfo(const GrCaps* caps,
753 SkArenaAlloc* arena,
Brian Salomon8afde5f2020-04-01 16:22:00 -0400754 const GrSurfaceProxyView* writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400755 GrAppliedClip&& appliedClip,
756 const GrXferProcessor::DstProxyView& dstProxyView) override {
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400757 SkMatrix invert;
758 if (fHelper.usesLocalCoords() && !fPaths.back().fViewMatrix.invert(&invert)) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400759 return;
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400760 }
761
762 GrGeometryProcessor* quadProcessor = QuadEdgeEffect::Make(arena, invert,
763 fHelper.usesLocalCoords(),
764 fWideColor);
765
Brian Salomon8afde5f2020-04-01 16:22:00 -0400766 fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView,
Robert Phillips4133dc42020-03-11 15:55:55 -0400767 std::move(appliedClip),
768 dstProxyView, quadProcessor,
769 GrPrimitiveType::kTriangles);
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400770 }
771
Brian Salomon91326c32017-08-09 16:02:19 -0400772 void onPrepareDraws(Target* target) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500773 int instanceCount = fPaths.count();
joshualitt27f398f2015-02-05 14:39:01 -0800774
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400775 if (!fProgramInfo) {
Robert Phillips4133dc42020-03-11 15:55:55 -0400776 this->createProgramInfo(target);
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400777 if (!fProgramInfo) {
778 return;
779 }
joshualitt27f398f2015-02-05 14:39:01 -0800780 }
781
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400782 const size_t kVertexStride = fProgramInfo->primProc().vertexStride();
783
784 fDraws.reserve(instanceCount);
joshualitt27f398f2015-02-05 14:39:01 -0800785
joshualitt27f398f2015-02-05 14:39:01 -0800786 // TODO generate all segments for all paths and use one vertex buffer
787 for (int i = 0; i < instanceCount; i++) {
Brian Salomond0a0a652016-12-15 15:25:22 -0500788 const PathData& args = fPaths[i];
joshualitt27f398f2015-02-05 14:39:01 -0800789
790 // We use the fact that SkPath::transform path does subdivision based on
791 // perspective. Otherwise, we apply the view matrix when copying to the
792 // segment representation.
793 const SkMatrix* viewMatrix = &args.fViewMatrix;
joshualitt144c3c82015-11-30 12:30:13 -0800794
795 // We avoid initializing the path unless we have to
796 const SkPath* pathPtr = &args.fPath;
797 SkTLazy<SkPath> tmpPath;
joshualitt27f398f2015-02-05 14:39:01 -0800798 if (viewMatrix->hasPerspective()) {
joshualitt144c3c82015-11-30 12:30:13 -0800799 SkPath* tmpPathPtr = tmpPath.init(*pathPtr);
800 tmpPathPtr->setIsVolatile(true);
801 tmpPathPtr->transform(*viewMatrix);
joshualitt27f398f2015-02-05 14:39:01 -0800802 viewMatrix = &SkMatrix::I();
joshualitt144c3c82015-11-30 12:30:13 -0800803 pathPtr = tmpPathPtr;
joshualitt27f398f2015-02-05 14:39:01 -0800804 }
805
806 int vertexCount;
807 int indexCount;
808 enum {
809 kPreallocSegmentCnt = 512 / sizeof(Segment),
810 kPreallocDrawCnt = 4,
811 };
812 SkSTArray<kPreallocSegmentCnt, Segment, true> segments;
813 SkPoint fanPt;
814
joshualitt144c3c82015-11-30 12:30:13 -0800815 if (!get_segments(*pathPtr, *viewMatrix, &segments, &fanPt, &vertexCount,
joshualitt27f398f2015-02-05 14:39:01 -0800816 &indexCount)) {
817 continue;
818 }
819
Brian Salomon12d22642019-01-29 14:38:50 -0500820 sk_sp<const GrBuffer> vertexBuffer;
Chris Daltonbca46e22017-05-15 11:03:26 -0600821 int firstVertex;
822
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500823 GrVertexWriter verts{target->makeVertexSpace(kVertexStride, vertexCount,
824 &vertexBuffer, &firstVertex)};
bsalomone64eb572015-05-07 11:35:55 -0700825
Brian Osmand2fa2eb2018-12-26 16:48:40 -0500826 if (!verts.fPtr) {
joshualitt4b31de82015-03-05 14:33:41 -0800827 SkDebugf("Could not allocate vertices\n");
828 return;
829 }
830
Brian Salomon12d22642019-01-29 14:38:50 -0500831 sk_sp<const GrBuffer> indexBuffer;
Chris Daltonbca46e22017-05-15 11:03:26 -0600832 int firstIndex;
833
834 uint16_t *idxs = target->makeIndexSpace(indexCount, &indexBuffer, &firstIndex);
robertphillipse40d3972015-05-07 09:51:43 -0700835 if (!idxs) {
joshualitt4b31de82015-03-05 14:33:41 -0800836 SkDebugf("Could not allocate indices\n");
837 return;
838 }
839
joshualitt27f398f2015-02-05 14:39:01 -0800840 SkSTArray<kPreallocDrawCnt, Draw, true> draws;
Brian Osman7f4735b2019-01-02 13:55:10 +0000841 GrVertexColor color(args.fColor, fWideColor);
842 create_vertices(segments, fanPt, color, &draws, verts, idxs, kVertexStride);
joshualitt27f398f2015-02-05 14:39:01 -0800843
Chris Daltoneb694b72020-03-16 09:25:50 -0600844 GrSimpleMesh* meshes = target->allocMeshes(draws.count());
robertphillips44c31282015-09-03 12:58:48 -0700845 for (int j = 0; j < draws.count(); ++j) {
846 const Draw& draw = draws[j];
Brian Salomon7eae3e02018-08-07 14:02:38 +0000847 meshes[j].setIndexed(indexBuffer, draw.fIndexCnt, firstIndex, 0,
Chris Dalton37c7bdd2020-03-13 09:21:12 -0600848 draw.fVertexCnt - 1, GrPrimitiveRestart::kNo, vertexBuffer,
849 firstVertex);
Chris Daltonbca46e22017-05-15 11:03:26 -0600850 firstIndex += draw.fIndexCnt;
851 firstVertex += draw.fVertexCnt;
joshualitt27f398f2015-02-05 14:39:01 -0800852 }
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400853
854 fDraws.push_back({ meshes, draws.count() });
joshualitt27f398f2015-02-05 14:39:01 -0800855 }
856 }
857
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700858 void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override {
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400859 if (!fProgramInfo || fDraws.isEmpty()) {
860 return;
861 }
Robert Phillips3968fcb2019-12-05 16:40:31 -0500862
Chris Dalton765ed362020-03-16 17:34:44 -0600863 flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds);
864 flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline());
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400865 for (int i = 0; i < fDraws.count(); ++i) {
Chris Dalton765ed362020-03-16 17:34:44 -0600866 for (int j = 0; j < fDraws[i].fMeshCount; ++j) {
867 flushState->drawMesh(fDraws[i].fMeshes[j]);
868 }
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400869 }
Chris Dalton07cdcfc92019-02-26 11:13:22 -0700870 }
871
Michael Ludwig28b0c5d2019-12-19 14:51:00 -0500872 CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*,
873 const GrCaps& caps) override {
Brian Salomond0a0a652016-12-15 15:25:22 -0500874 AAConvexPathOp* that = t->cast<AAConvexPathOp>();
Brian Salomon10978a62017-06-15 16:21:49 -0400875 if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000876 return CombineResult::kCannotCombine;
joshualitt8cab9a72015-07-16 09:13:50 -0700877 }
Brian Salomon10978a62017-06-15 16:21:49 -0400878 if (fHelper.usesLocalCoords() &&
Mike Reed2c383152019-12-18 16:47:47 -0500879 !SkMatrixPriv::CheapEqual(fPaths[0].fViewMatrix, that->fPaths[0].fViewMatrix)) {
Brian Salomon7eae3e02018-08-07 14:02:38 +0000880 return CombineResult::kCannotCombine;
joshualitt27f398f2015-02-05 14:39:01 -0800881 }
882
Brian Salomond0a0a652016-12-15 15:25:22 -0500883 fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin());
Brian Osman7f4735b2019-01-02 13:55:10 +0000884 fWideColor |= that->fWideColor;
Brian Salomon7eae3e02018-08-07 14:02:38 +0000885 return CombineResult::kMerged;
joshualitt27f398f2015-02-05 14:39:01 -0800886 }
887
Brian Salomond0a0a652016-12-15 15:25:22 -0500888 struct PathData {
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400889 SkMatrix fViewMatrix;
890 SkPath fPath;
Brian Osmancf860852018-10-31 14:04:39 -0400891 SkPMColor4f fColor;
bsalomonf1703092016-06-29 18:41:53 -0700892 };
893
Brian Salomon10978a62017-06-15 16:21:49 -0400894 Helper fHelper;
Brian Salomond0a0a652016-12-15 15:25:22 -0500895 SkSTArray<1, PathData, true> fPaths;
Brian Osman7f4735b2019-01-02 13:55:10 +0000896 bool fWideColor;
reed1b55a962015-09-17 20:16:13 -0700897
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400898 struct MeshDraw {
Chris Daltoneb694b72020-03-16 09:25:50 -0600899 GrSimpleMesh* fMeshes;
900 int fMeshCount;
Robert Phillips3eabf4a2020-03-10 14:49:20 -0400901 };
902
903 SkTDArray<MeshDraw> fDraws;
904 GrProgramInfo* fProgramInfo = nullptr;
905
Brian Salomon10978a62017-06-15 16:21:49 -0400906 typedef GrMeshDrawOp INHERITED;
joshualitt27f398f2015-02-05 14:39:01 -0800907};
908
Brian Salomon10978a62017-06-15 16:21:49 -0400909} // anonymous namespace
910
bsalomon0aff2fa2015-07-31 06:48:27 -0700911bool GrAAConvexPathRenderer::onDrawPath(const DrawPathArgs& args) {
Brian Osman11052242016-10-27 14:47:55 -0400912 GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(),
robertphillips976f5f02016-06-03 10:59:20 -0700913 "GrAAConvexPathRenderer::onDrawPath");
Chris Dalton6ce447a2019-06-23 18:07:38 -0600914 SkASSERT(args.fRenderTargetContext->numSamples() <= 1);
bsalomon8acedde2016-06-24 10:42:16 -0700915 SkASSERT(!args.fShape->isEmpty());
bsalomon@google.com4647f902013-03-26 14:45:27 +0000916
bsalomonf1703092016-06-29 18:41:53 -0700917 SkPath path;
918 args.fShape->asPath(&path);
bsalomon@google.comaf90f7f2012-03-05 20:50:10 +0000919
Robert Phillips7c525e62018-06-12 10:11:12 -0400920 std::unique_ptr<GrDrawOp> op = AAConvexPathOp::Make(args.fContext, std::move(args.fPaint),
921 *args.fViewMatrix,
Brian Salomon10978a62017-06-15 16:21:49 -0400922 path, args.fUserStencilSettings);
Michael Ludwig7c12e282020-05-29 09:54:07 -0400923 args.fRenderTargetContext->addDrawOp(args.fClip, std::move(op));
bsalomon@google.comc2099d22012-03-02 21:26:50 +0000924 return true;
bsalomon@google.com69cc6ad2012-01-17 14:25:10 +0000925}
joshualitt8e5c1772015-05-11 08:58:52 -0700926
927///////////////////////////////////////////////////////////////////////////////////////////////////
928
Hal Canary6f6961e2017-01-31 13:50:44 -0500929#if GR_TEST_UTILS
joshualitt8e5c1772015-05-11 08:58:52 -0700930
Brian Salomon10978a62017-06-15 16:21:49 -0400931GR_DRAW_OP_TEST_DEFINE(AAConvexPathOp) {
bsalomonf1703092016-06-29 18:41:53 -0700932 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random);
933 SkPath path = GrTest::TestPathConvex(random);
Brian Salomon10978a62017-06-15 16:21:49 -0400934 const GrUserStencilSettings* stencilSettings = GrGetRandomStencil(random, context);
Robert Phillips7c525e62018-06-12 10:11:12 -0400935 return AAConvexPathOp::Make(context, std::move(paint), viewMatrix, path, stencilSettings);
joshualitt8e5c1772015-05-11 08:58:52 -0700936}
937
938#endif