bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 1 | /* |
| 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 Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 8 | #include "include/core/SkString.h" |
| 9 | #include "include/core/SkTypes.h" |
| 10 | #include "src/core/SkGeometry.h" |
| 11 | #include "src/core/SkPathPriv.h" |
| 12 | #include "src/core/SkPointPriv.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 13 | #include "src/gpu/GrAuditTrail.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 14 | #include "src/gpu/GrCaps.h" |
| 15 | #include "src/gpu/GrDrawOpTest.h" |
| 16 | #include "src/gpu/GrGeometryProcessor.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 17 | #include "src/gpu/GrProcessor.h" |
Robert Phillips | 3eabf4a | 2020-03-10 14:49:20 -0400 | [diff] [blame] | 18 | #include "src/gpu/GrProgramInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 19 | #include "src/gpu/GrRenderTargetContext.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 20 | #include "src/gpu/GrVertexWriter.h" |
Michael Ludwig | 663afe5 | 2019-06-03 16:46:19 -0400 | [diff] [blame] | 21 | #include "src/gpu/geometry/GrPathUtils.h" |
Michael Ludwig | 2686d69 | 2020-04-17 20:21:37 +0000 | [diff] [blame] | 22 | #include "src/gpu/geometry/GrStyledShape.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 23 | #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h" |
| 24 | #include "src/gpu/glsl/GrGLSLGeometryProcessor.h" |
| 25 | #include "src/gpu/glsl/GrGLSLProgramDataManager.h" |
| 26 | #include "src/gpu/glsl/GrGLSLUniformHandler.h" |
| 27 | #include "src/gpu/glsl/GrGLSLVarying.h" |
| 28 | #include "src/gpu/glsl/GrGLSLVertexGeoBuilder.h" |
| 29 | #include "src/gpu/ops/GrAAConvexPathRenderer.h" |
| 30 | #include "src/gpu/ops/GrMeshDrawOp.h" |
Robert Phillips | 55f681f | 2020-02-28 08:58:15 -0500 | [diff] [blame] | 31 | #include "src/gpu/ops/GrSimpleMeshDrawOpHelperWithStencil.h" |
commit-bot@chromium.org | 234d4fb | 2013-09-30 19:55:49 +0000 | [diff] [blame] | 32 | |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 33 | GrAAConvexPathRenderer::GrAAConvexPathRenderer() { |
| 34 | } |
| 35 | |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 36 | struct Segment { |
| 37 | enum { |
bsalomon@google.com | 9b1517e | 2012-03-05 17:58:34 +0000 | [diff] [blame] | 38 | // These enum values are assumed in member functions below. |
| 39 | kLine = 0, |
| 40 | kQuad = 1, |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 41 | } fType; |
bsalomon@google.com | 9b1517e | 2012-03-05 17:58:34 +0000 | [diff] [blame] | 42 | |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 43 | // line uses one pt, quad uses 2 pts |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 44 | SkPoint fPts[2]; |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 45 | // normal to edge ending at each pt |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 46 | SkVector fNorms[2]; |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 47 | // is the corner where the previous segment meets this segment |
| 48 | // sharp. If so, fMid is a normalized bisector facing outward. |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 49 | SkVector fMid; |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 50 | |
| 51 | int countPoints() { |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 52 | static_assert(0 == kLine && 1 == kQuad); |
bsalomon@google.com | 9b1517e | 2012-03-05 17:58:34 +0000 | [diff] [blame] | 53 | return fType + 1; |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 54 | } |
| 55 | const SkPoint& endPt() const { |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 56 | static_assert(0 == kLine && 1 == kQuad); |
bsalomon@google.com | 9b1517e | 2012-03-05 17:58:34 +0000 | [diff] [blame] | 57 | return fPts[fType]; |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 58 | } |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 59 | const SkPoint& endNorm() const { |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 60 | static_assert(0 == kLine && 1 == kQuad); |
bsalomon@google.com | 9b1517e | 2012-03-05 17:58:34 +0000 | [diff] [blame] | 61 | return fNorms[fType]; |
Mike Klein | fc6c37b | 2016-09-27 09:34:10 -0400 | [diff] [blame] | 62 | } |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 63 | }; |
| 64 | |
| 65 | typedef SkTArray<Segment, true> SegmentArray; |
| 66 | |
Greg Daniel | 8b09b96 | 2018-04-03 14:53:45 -0400 | [diff] [blame] | 67 | static bool center_of_mass(const SegmentArray& segments, SkPoint* c) { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 68 | SkScalar area = 0; |
vandebo@chromium.org | 6390c72 | 2012-03-28 21:03:22 +0000 | [diff] [blame] | 69 | SkPoint center = {0, 0}; |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 70 | int count = segments.count(); |
vandebo@chromium.org | 6390c72 | 2012-03-28 21:03:22 +0000 | [diff] [blame] | 71 | SkPoint p0 = {0, 0}; |
bsalomon@google.com | 5b56d9e | 2012-02-23 19:18:37 +0000 | [diff] [blame] | 72 | if (count > 2) { |
| 73 | // We translate the polygon so that the first point is at the origin. |
| 74 | // This avoids some precision issues with small area polygons far away |
| 75 | // from the origin. |
| 76 | p0 = segments[0].endPt(); |
| 77 | SkPoint pi; |
| 78 | SkPoint pj; |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 79 | // the first and last iteration of the below loop would compute |
bsalomon@google.com | 5b56d9e | 2012-02-23 19:18:37 +0000 | [diff] [blame] | 80 | // zeros since the starting / ending point is (0,0). So instead we start |
| 81 | // at i=1 and make the last iteration i=count-2. |
| 82 | pj = segments[1].endPt() - p0; |
| 83 | for (int i = 1; i < count - 1; ++i) { |
| 84 | pi = pj; |
robertphillips | 44c3128 | 2015-09-03 12:58:48 -0700 | [diff] [blame] | 85 | pj = segments[i + 1].endPt() - p0; |
bsalomon@google.com | 5b56d9e | 2012-02-23 19:18:37 +0000 | [diff] [blame] | 86 | |
robertphillips | 44c3128 | 2015-09-03 12:58:48 -0700 | [diff] [blame] | 87 | SkScalar t = SkPoint::CrossProduct(pi, pj); |
bsalomon@google.com | 5b56d9e | 2012-02-23 19:18:37 +0000 | [diff] [blame] | 88 | area += t; |
| 89 | center.fX += (pi.fX + pj.fX) * t; |
| 90 | center.fY += (pi.fY + pj.fY) * t; |
bsalomon@google.com | 5b56d9e | 2012-02-23 19:18:37 +0000 | [diff] [blame] | 91 | } |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 92 | } |
robertphillips | 44c3128 | 2015-09-03 12:58:48 -0700 | [diff] [blame] | 93 | |
bsalomon@google.com | 278dc69 | 2012-02-15 16:52:51 +0000 | [diff] [blame] | 94 | // If the poly has no area then we instead return the average of |
| 95 | // its points. |
bsalomon@google.com | 5b56d9e | 2012-02-23 19:18:37 +0000 | [diff] [blame] | 96 | if (SkScalarNearlyZero(area)) { |
bsalomon@google.com | 278dc69 | 2012-02-15 16:52:51 +0000 | [diff] [blame] | 97 | SkPoint avg; |
| 98 | avg.set(0, 0); |
| 99 | for (int i = 0; i < count; ++i) { |
| 100 | const SkPoint& pt = segments[i].endPt(); |
| 101 | avg.fX += pt.fX; |
| 102 | avg.fY += pt.fY; |
| 103 | } |
| 104 | SkScalar denom = SK_Scalar1 / count; |
| 105 | avg.scale(denom); |
| 106 | *c = avg; |
| 107 | } else { |
| 108 | area *= 3; |
reed | 80ea19c | 2015-05-12 10:37:34 -0700 | [diff] [blame] | 109 | area = SkScalarInvert(area); |
robertphillips | 44c3128 | 2015-09-03 12:58:48 -0700 | [diff] [blame] | 110 | center.scale(area); |
bsalomon@google.com | 5b56d9e | 2012-02-23 19:18:37 +0000 | [diff] [blame] | 111 | // undo the translate of p0 to the origin. |
| 112 | *c = center + p0; |
bsalomon@google.com | 278dc69 | 2012-02-15 16:52:51 +0000 | [diff] [blame] | 113 | } |
Greg Daniel | 62473ad | 2018-04-03 15:44:18 -0400 | [diff] [blame] | 114 | return !SkScalarIsNaN(c->fX) && !SkScalarIsNaN(c->fY) && c->isFinite(); |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Greg Daniel | 8b09b96 | 2018-04-03 14:53:45 -0400 | [diff] [blame] | 117 | static bool compute_vectors(SegmentArray* segments, |
commit-bot@chromium.org | fdfbb9d | 2013-08-15 18:16:27 +0000 | [diff] [blame] | 118 | SkPoint* fanPt, |
reed | 026beb5 | 2015-06-10 14:23:15 -0700 | [diff] [blame] | 119 | SkPathPriv::FirstDirection dir, |
commit-bot@chromium.org | fdfbb9d | 2013-08-15 18:16:27 +0000 | [diff] [blame] | 120 | int* vCount, |
| 121 | int* iCount) { |
Greg Daniel | 8b09b96 | 2018-04-03 14:53:45 -0400 | [diff] [blame] | 122 | if (!center_of_mass(*segments, fanPt)) { |
| 123 | return false; |
| 124 | } |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 125 | int count = segments->count(); |
| 126 | |
bsalomon@google.com | 278dc69 | 2012-02-15 16:52:51 +0000 | [diff] [blame] | 127 | // Make the normals point towards the outside |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 128 | SkPointPriv::Side normSide; |
reed | 026beb5 | 2015-06-10 14:23:15 -0700 | [diff] [blame] | 129 | if (dir == SkPathPriv::kCCW_FirstDirection) { |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 130 | normSide = SkPointPriv::kRight_Side; |
bsalomon@google.com | 278dc69 | 2012-02-15 16:52:51 +0000 | [diff] [blame] | 131 | } else { |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 132 | normSide = SkPointPriv::kLeft_Side; |
bsalomon@google.com | 278dc69 | 2012-02-15 16:52:51 +0000 | [diff] [blame] | 133 | } |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 134 | |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 135 | int64_t vCount64 = 0; |
| 136 | int64_t iCount64 = 0; |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 137 | // compute normals at all points |
| 138 | for (int a = 0; a < count; ++a) { |
bsalomon@google.com | 7d9ffc8 | 2013-05-14 14:20:28 +0000 | [diff] [blame] | 139 | Segment& sega = (*segments)[a]; |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 140 | int b = (a + 1) % count; |
| 141 | Segment& segb = (*segments)[b]; |
| 142 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 143 | const SkPoint* prevPt = &sega.endPt(); |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 144 | int n = segb.countPoints(); |
| 145 | for (int p = 0; p < n; ++p) { |
| 146 | segb.fNorms[p] = segb.fPts[p] - *prevPt; |
| 147 | segb.fNorms[p].normalize(); |
Brian Salomon | 0235c64 | 2018-08-31 12:04:18 -0400 | [diff] [blame] | 148 | segb.fNorms[p] = SkPointPriv::MakeOrthog(segb.fNorms[p], normSide); |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 149 | prevPt = &segb.fPts[p]; |
| 150 | } |
| 151 | if (Segment::kLine == segb.fType) { |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 152 | vCount64 += 5; |
| 153 | iCount64 += 9; |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 154 | } else { |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 155 | vCount64 += 6; |
| 156 | iCount64 += 12; |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | // compute mid-vectors where segments meet. TODO: Detect shallow corners |
| 161 | // and leave out the wedges and close gaps by stitching segments together. |
| 162 | for (int a = 0; a < count; ++a) { |
| 163 | const Segment& sega = (*segments)[a]; |
| 164 | int b = (a + 1) % count; |
| 165 | Segment& segb = (*segments)[b]; |
| 166 | segb.fMid = segb.fNorms[0] + sega.endNorm(); |
| 167 | segb.fMid.normalize(); |
| 168 | // corner wedges |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 169 | vCount64 += 4; |
| 170 | iCount64 += 6; |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 171 | } |
Greg Daniel | d5b4593 | 2018-06-07 13:15:10 -0400 | [diff] [blame] | 172 | if (vCount64 > SK_MaxS32 || iCount64 > SK_MaxS32) { |
| 173 | return false; |
| 174 | } |
| 175 | *vCount = vCount64; |
| 176 | *iCount = iCount64; |
Greg Daniel | 8b09b96 | 2018-04-03 14:53:45 -0400 | [diff] [blame] | 177 | return true; |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 178 | } |
| 179 | |
bsalomon@google.com | 9732f62 | 2012-01-31 15:19:21 +0000 | [diff] [blame] | 180 | struct DegenerateTestData { |
| 181 | DegenerateTestData() { fStage = kInitial; } |
| 182 | bool isDegenerate() const { return kNonDegenerate != fStage; } |
| 183 | enum { |
| 184 | kInitial, |
| 185 | kPoint, |
| 186 | kLine, |
| 187 | kNonDegenerate |
| 188 | } fStage; |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 189 | SkPoint fFirstPoint; |
| 190 | SkVector fLineNormal; |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 191 | SkScalar fLineC; |
bsalomon@google.com | 9732f62 | 2012-01-31 15:19:21 +0000 | [diff] [blame] | 192 | }; |
| 193 | |
commit-bot@chromium.org | fdfbb9d | 2013-08-15 18:16:27 +0000 | [diff] [blame] | 194 | static const SkScalar kClose = (SK_Scalar1 / 16); |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 195 | static const SkScalar kCloseSqd = kClose * kClose; |
bsalomon@google.com | 9732f62 | 2012-01-31 15:19:21 +0000 | [diff] [blame] | 196 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 197 | static void update_degenerate_test(DegenerateTestData* data, const SkPoint& pt) { |
bsalomon@google.com | 9732f62 | 2012-01-31 15:19:21 +0000 | [diff] [blame] | 198 | switch (data->fStage) { |
| 199 | case DegenerateTestData::kInitial: |
| 200 | data->fFirstPoint = pt; |
| 201 | data->fStage = DegenerateTestData::kPoint; |
| 202 | break; |
| 203 | case DegenerateTestData::kPoint: |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 204 | if (SkPointPriv::DistanceToSqd(pt, data->fFirstPoint) > kCloseSqd) { |
bsalomon@google.com | 9732f62 | 2012-01-31 15:19:21 +0000 | [diff] [blame] | 205 | data->fLineNormal = pt - data->fFirstPoint; |
| 206 | data->fLineNormal.normalize(); |
Brian Salomon | 0235c64 | 2018-08-31 12:04:18 -0400 | [diff] [blame] | 207 | data->fLineNormal = SkPointPriv::MakeOrthog(data->fLineNormal); |
bsalomon@google.com | 9732f62 | 2012-01-31 15:19:21 +0000 | [diff] [blame] | 208 | data->fLineC = -data->fLineNormal.dot(data->fFirstPoint); |
| 209 | data->fStage = DegenerateTestData::kLine; |
| 210 | } |
| 211 | break; |
| 212 | case DegenerateTestData::kLine: |
commit-bot@chromium.org | fdfbb9d | 2013-08-15 18:16:27 +0000 | [diff] [blame] | 213 | if (SkScalarAbs(data->fLineNormal.dot(pt) + data->fLineC) > kClose) { |
bsalomon@google.com | 9732f62 | 2012-01-31 15:19:21 +0000 | [diff] [blame] | 214 | data->fStage = DegenerateTestData::kNonDegenerate; |
| 215 | } |
John Stiles | 30212b7 | 2020-06-11 17:55:07 -0400 | [diff] [blame] | 216 | break; |
bsalomon@google.com | 9732f62 | 2012-01-31 15:19:21 +0000 | [diff] [blame] | 217 | case DegenerateTestData::kNonDegenerate: |
| 218 | break; |
| 219 | default: |
Ben Wagner | b4aab9a | 2017-08-16 10:53:04 -0400 | [diff] [blame] | 220 | SK_ABORT("Unexpected degenerate test stage."); |
bsalomon@google.com | 9732f62 | 2012-01-31 15:19:21 +0000 | [diff] [blame] | 221 | } |
| 222 | } |
| 223 | |
Brian Osman | 2caecd2 | 2019-09-25 14:02:26 -0400 | [diff] [blame] | 224 | static inline bool get_direction(const SkPath& path, const SkMatrix& m, |
| 225 | SkPathPriv::FirstDirection* dir) { |
Michael Ludwig | 0a1e9ef | 2019-08-30 10:03:15 -0400 | [diff] [blame] | 226 | // At this point, we've already returned true from canDraw(), which checked that the path's |
| 227 | // direction could be determined, so this should just be fetching the cached direction. |
Brian Osman | 2caecd2 | 2019-09-25 14:02:26 -0400 | [diff] [blame] | 228 | // However, if perspective is involved, we're operating on a transformed path, which may no |
| 229 | // longer have a computable direction. |
| 230 | if (!SkPathPriv::CheapComputeFirstDirection(path, dir)) { |
| 231 | return false; |
| 232 | } |
Michael Ludwig | 0a1e9ef | 2019-08-30 10:03:15 -0400 | [diff] [blame] | 233 | |
bsalomon@google.com | af90f7f | 2012-03-05 20:50:10 +0000 | [diff] [blame] | 234 | // check whether m reverses the orientation |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 235 | SkASSERT(!m.hasPerspective()); |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 236 | SkScalar det2x2 = m.get(SkMatrix::kMScaleX) * m.get(SkMatrix::kMScaleY) - |
| 237 | m.get(SkMatrix::kMSkewX) * m.get(SkMatrix::kMSkewY); |
bsalomon@google.com | af90f7f | 2012-03-05 20:50:10 +0000 | [diff] [blame] | 238 | if (det2x2 < 0) { |
Brian Osman | 2caecd2 | 2019-09-25 14:02:26 -0400 | [diff] [blame] | 239 | *dir = SkPathPriv::OppositeFirstDirection(*dir); |
bsalomon@google.com | af90f7f | 2012-03-05 20:50:10 +0000 | [diff] [blame] | 240 | } |
Michael Ludwig | 0a1e9ef | 2019-08-30 10:03:15 -0400 | [diff] [blame] | 241 | |
Brian Osman | 2caecd2 | 2019-09-25 14:02:26 -0400 | [diff] [blame] | 242 | return true; |
bsalomon@google.com | af90f7f | 2012-03-05 20:50:10 +0000 | [diff] [blame] | 243 | } |
| 244 | |
commit-bot@chromium.org | 106655e | 2013-09-03 21:28:55 +0000 | [diff] [blame] | 245 | static inline void add_line_to_segment(const SkPoint& pt, |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 246 | SegmentArray* segments) { |
commit-bot@chromium.org | fdfbb9d | 2013-08-15 18:16:27 +0000 | [diff] [blame] | 247 | segments->push_back(); |
| 248 | segments->back().fType = Segment::kLine; |
| 249 | segments->back().fPts[0] = pt; |
| 250 | } |
| 251 | |
commit-bot@chromium.org | 106655e | 2013-09-03 21:28:55 +0000 | [diff] [blame] | 252 | static inline void add_quad_segment(const SkPoint pts[3], |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 253 | SegmentArray* segments) { |
Brian Salomon | 5f8a62b | 2019-03-29 13:55:32 -0400 | [diff] [blame] | 254 | if (SkPointPriv::DistanceToLineSegmentBetweenSqd(pts[1], pts[0], pts[2]) < kCloseSqd) { |
commit-bot@chromium.org | fdfbb9d | 2013-08-15 18:16:27 +0000 | [diff] [blame] | 255 | if (pts[0] != pts[2]) { |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 256 | add_line_to_segment(pts[2], segments); |
commit-bot@chromium.org | fdfbb9d | 2013-08-15 18:16:27 +0000 | [diff] [blame] | 257 | } |
| 258 | } else { |
| 259 | segments->push_back(); |
| 260 | segments->back().fType = Segment::kQuad; |
| 261 | segments->back().fPts[0] = pts[1]; |
| 262 | segments->back().fPts[1] = pts[2]; |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | static inline void add_cubic_segments(const SkPoint pts[4], |
reed | 026beb5 | 2015-06-10 14:23:15 -0700 | [diff] [blame] | 267 | SkPathPriv::FirstDirection dir, |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 268 | SegmentArray* segments) { |
commit-bot@chromium.org | fdfbb9d | 2013-08-15 18:16:27 +0000 | [diff] [blame] | 269 | SkSTArray<15, SkPoint, true> quads; |
bsalomon | 18fab30 | 2016-02-16 08:00:05 -0800 | [diff] [blame] | 270 | GrPathUtils::convertCubicToQuadsConstrainToTangents(pts, SK_Scalar1, dir, &quads); |
commit-bot@chromium.org | fdfbb9d | 2013-08-15 18:16:27 +0000 | [diff] [blame] | 271 | int count = quads.count(); |
| 272 | for (int q = 0; q < count; q += 3) { |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 273 | add_quad_segment(&quads[q], segments); |
commit-bot@chromium.org | fdfbb9d | 2013-08-15 18:16:27 +0000 | [diff] [blame] | 274 | } |
| 275 | } |
| 276 | |
| 277 | static bool get_segments(const SkPath& path, |
| 278 | const SkMatrix& m, |
| 279 | SegmentArray* segments, |
| 280 | SkPoint* fanPt, |
| 281 | int* vCount, |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 282 | int* iCount) { |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 283 | SkPath::Iter iter(path, true); |
bsalomon@google.com | 30c174b | 2012-11-13 14:36:42 +0000 | [diff] [blame] | 284 | // This renderer over-emphasizes very thin path regions. We use the distance |
bsalomon@google.com | 5cc90d1 | 2012-01-17 16:28:34 +0000 | [diff] [blame] | 285 | // to the path from the sample to compute coverage. Every pixel intersected |
| 286 | // by the path will be hit and the maximum distance is sqrt(2)/2. We don't |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 287 | // notice that the sample may be close to a very thin area of the path and |
bsalomon@google.com | 5cc90d1 | 2012-01-17 16:28:34 +0000 | [diff] [blame] | 288 | // thus should be very light. This is particularly egregious for degenerate |
| 289 | // line paths. We detect paths that are very close to a line (zero area) and |
| 290 | // draw nothing. |
bsalomon@google.com | 9732f62 | 2012-01-31 15:19:21 +0000 | [diff] [blame] | 291 | DegenerateTestData degenerateData; |
Brian Osman | 2caecd2 | 2019-09-25 14:02:26 -0400 | [diff] [blame] | 292 | SkPathPriv::FirstDirection dir; |
| 293 | if (!get_direction(path, m, &dir)) { |
| 294 | return false; |
| 295 | } |
bsalomon@google.com | 9732f62 | 2012-01-31 15:19:21 +0000 | [diff] [blame] | 296 | |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 297 | for (;;) { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 298 | SkPoint pts[4]; |
Mike Reed | ba7e9a6 | 2019-08-16 13:30:34 -0400 | [diff] [blame] | 299 | SkPath::Verb verb = iter.next(pts); |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 300 | switch (verb) { |
| 301 | case SkPath::kMove_Verb: |
bsalomon@google.com | 1a38d55 | 2012-03-15 14:40:46 +0000 | [diff] [blame] | 302 | m.mapPoints(pts, 1); |
bsalomon@google.com | 9732f62 | 2012-01-31 15:19:21 +0000 | [diff] [blame] | 303 | update_degenerate_test(°enerateData, pts[0]); |
| 304 | break; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 305 | case SkPath::kLine_Verb: { |
Mike Reed | ba7e9a6 | 2019-08-16 13:30:34 -0400 | [diff] [blame] | 306 | if (!SkPathPriv::AllPointsEq(pts, 2)) { |
| 307 | m.mapPoints(&pts[1], 1); |
| 308 | update_degenerate_test(°enerateData, pts[1]); |
| 309 | add_line_to_segment(pts[1], segments); |
| 310 | } |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 311 | break; |
| 312 | } |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 313 | case SkPath::kQuad_Verb: |
Mike Reed | ba7e9a6 | 2019-08-16 13:30:34 -0400 | [diff] [blame] | 314 | if (!SkPathPriv::AllPointsEq(pts, 3)) { |
| 315 | m.mapPoints(pts, 3); |
| 316 | update_degenerate_test(°enerateData, pts[1]); |
| 317 | update_degenerate_test(°enerateData, pts[2]); |
| 318 | add_quad_segment(pts, segments); |
| 319 | } |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 320 | break; |
egdaniel | af18a09 | 2015-01-05 10:22:28 -0800 | [diff] [blame] | 321 | case SkPath::kConic_Verb: { |
Mike Reed | ba7e9a6 | 2019-08-16 13:30:34 -0400 | [diff] [blame] | 322 | if (!SkPathPriv::AllPointsEq(pts, 3)) { |
| 323 | m.mapPoints(pts, 3); |
| 324 | SkScalar weight = iter.conicWeight(); |
| 325 | SkAutoConicToQuads converter; |
| 326 | const SkPoint* quadPts = converter.computeQuads(pts, weight, 0.25f); |
| 327 | for (int i = 0; i < converter.countQuads(); ++i) { |
| 328 | update_degenerate_test(°enerateData, quadPts[2*i + 1]); |
| 329 | update_degenerate_test(°enerateData, quadPts[2*i + 2]); |
| 330 | add_quad_segment(quadPts + 2*i, segments); |
| 331 | } |
egdaniel | af18a09 | 2015-01-05 10:22:28 -0800 | [diff] [blame] | 332 | } |
| 333 | break; |
| 334 | } |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 335 | case SkPath::kCubic_Verb: { |
Mike Reed | ba7e9a6 | 2019-08-16 13:30:34 -0400 | [diff] [blame] | 336 | if (!SkPathPriv::AllPointsEq(pts, 4)) { |
| 337 | m.mapPoints(pts, 4); |
| 338 | update_degenerate_test(°enerateData, pts[1]); |
| 339 | update_degenerate_test(°enerateData, pts[2]); |
| 340 | update_degenerate_test(°enerateData, pts[3]); |
| 341 | add_cubic_segments(pts, dir, segments); |
| 342 | } |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 343 | break; |
Brian Salomon | 2335644 | 2018-11-30 15:33:19 -0500 | [diff] [blame] | 344 | } |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 345 | case SkPath::kDone_Verb: |
bsalomon@google.com | 9732f62 | 2012-01-31 15:19:21 +0000 | [diff] [blame] | 346 | if (degenerateData.isDegenerate()) { |
| 347 | return false; |
| 348 | } else { |
Greg Daniel | 8b09b96 | 2018-04-03 14:53:45 -0400 | [diff] [blame] | 349 | return compute_vectors(segments, fanPt, dir, vCount, iCount); |
bsalomon@google.com | 9732f62 | 2012-01-31 15:19:21 +0000 | [diff] [blame] | 350 | } |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 351 | default: |
| 352 | break; |
| 353 | } |
| 354 | } |
| 355 | } |
| 356 | |
bsalomon@google.com | 7d9ffc8 | 2013-05-14 14:20:28 +0000 | [diff] [blame] | 357 | struct Draw { |
| 358 | Draw() : fVertexCnt(0), fIndexCnt(0) {} |
| 359 | int fVertexCnt; |
| 360 | int fIndexCnt; |
| 361 | }; |
| 362 | |
| 363 | typedef SkTArray<Draw, true> DrawArray; |
| 364 | |
Brian Salomon | 60fb0b2 | 2017-06-15 17:09:36 -0400 | [diff] [blame] | 365 | static void create_vertices(const SegmentArray& segments, |
commit-bot@chromium.org | fdfbb9d | 2013-08-15 18:16:27 +0000 | [diff] [blame] | 366 | const SkPoint& fanPt, |
Brian Osman | 7f4735b | 2019-01-02 13:55:10 +0000 | [diff] [blame] | 367 | const GrVertexColor& color, |
Brian Salomon | 60fb0b2 | 2017-06-15 17:09:36 -0400 | [diff] [blame] | 368 | DrawArray* draws, |
Brian Osman | d2fa2eb | 2018-12-26 16:48:40 -0500 | [diff] [blame] | 369 | GrVertexWriter& verts, |
| 370 | uint16_t* idxs, |
| 371 | size_t vertexStride) { |
bsalomon@google.com | 7d9ffc8 | 2013-05-14 14:20:28 +0000 | [diff] [blame] | 372 | Draw* draw = &draws->push_back(); |
| 373 | // alias just to make vert/index assignments easier to read. |
| 374 | int* v = &draw->fVertexCnt; |
| 375 | int* i = &draw->fIndexCnt; |
Brian Osman | 7f4735b | 2019-01-02 13:55:10 +0000 | [diff] [blame] | 376 | const size_t uvOffset = sizeof(SkPoint) + color.size(); |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 377 | |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 378 | int count = segments.count(); |
| 379 | for (int a = 0; a < count; ++a) { |
| 380 | const Segment& sega = segments[a]; |
| 381 | int b = (a + 1) % count; |
| 382 | const Segment& segb = segments[b]; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 383 | |
bsalomon@google.com | 7d9ffc8 | 2013-05-14 14:20:28 +0000 | [diff] [blame] | 384 | // Check whether adding the verts for this segment to the current draw would cause index |
| 385 | // values to overflow. |
| 386 | int vCount = 4; |
| 387 | if (Segment::kLine == segb.fType) { |
| 388 | vCount += 5; |
| 389 | } else { |
| 390 | vCount += 6; |
| 391 | } |
| 392 | if (draw->fVertexCnt + vCount > (1 << 16)) { |
bsalomon@google.com | 7d9ffc8 | 2013-05-14 14:20:28 +0000 | [diff] [blame] | 393 | idxs += *i; |
| 394 | draw = &draws->push_back(); |
| 395 | v = &draw->fVertexCnt; |
| 396 | i = &draw->fIndexCnt; |
| 397 | } |
| 398 | |
Brian Osman | d2fa2eb | 2018-12-26 16:48:40 -0500 | [diff] [blame] | 399 | const SkScalar negOneDists[2] = { -SK_Scalar1, -SK_Scalar1 }; |
| 400 | |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 401 | // FIXME: These tris are inset in the 1 unit arc around the corner |
Brian Osman | d2fa2eb | 2018-12-26 16:48:40 -0500 | [diff] [blame] | 402 | SkPoint p0 = sega.endPt(); |
| 403 | // Position, Color, UV, D0, D1 |
| 404 | verts.write(p0, color, SkPoint{0, 0}, negOneDists); |
| 405 | verts.write(p0 + sega.endNorm(), color, SkPoint{0, -SK_Scalar1}, negOneDists); |
| 406 | verts.write(p0 + segb.fMid, color, SkPoint{0, -SK_Scalar1}, negOneDists); |
| 407 | verts.write(p0 + segb.fNorms[0], color, SkPoint{0, -SK_Scalar1}, negOneDists); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 408 | |
bsalomon@google.com | 7d9ffc8 | 2013-05-14 14:20:28 +0000 | [diff] [blame] | 409 | idxs[*i + 0] = *v + 0; |
| 410 | idxs[*i + 1] = *v + 2; |
| 411 | idxs[*i + 2] = *v + 1; |
| 412 | idxs[*i + 3] = *v + 0; |
| 413 | idxs[*i + 4] = *v + 3; |
| 414 | idxs[*i + 5] = *v + 2; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 415 | |
bsalomon@google.com | 7d9ffc8 | 2013-05-14 14:20:28 +0000 | [diff] [blame] | 416 | *v += 4; |
| 417 | *i += 6; |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 418 | |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 419 | if (Segment::kLine == segb.fType) { |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 420 | // we draw the line edge as a degenerate quad (u is 0, v is the |
| 421 | // signed distance to the edge) |
Brian Osman | d2fa2eb | 2018-12-26 16:48:40 -0500 | [diff] [blame] | 422 | SkPoint v1Pos = sega.endPt(); |
| 423 | SkPoint v2Pos = segb.fPts[0]; |
| 424 | SkScalar dist = SkPointPriv::DistanceToLineBetween(fanPt, v1Pos, v2Pos); |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 425 | |
Brian Osman | d2fa2eb | 2018-12-26 16:48:40 -0500 | [diff] [blame] | 426 | verts.write(fanPt, color, SkPoint{0, dist}, negOneDists); |
| 427 | verts.write(v1Pos, color, SkPoint{0, 0}, negOneDists); |
| 428 | verts.write(v2Pos, color, SkPoint{0, 0}, negOneDists); |
| 429 | verts.write(v1Pos + segb.fNorms[0], color, SkPoint{0, -SK_Scalar1}, negOneDists); |
| 430 | verts.write(v2Pos + segb.fNorms[0], color, SkPoint{0, -SK_Scalar1}, negOneDists); |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 431 | |
cdalton | 3596482 | 2015-04-29 10:14:03 -0700 | [diff] [blame] | 432 | idxs[*i + 0] = *v + 3; |
| 433 | idxs[*i + 1] = *v + 1; |
| 434 | idxs[*i + 2] = *v + 2; |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 435 | |
cdalton | 3596482 | 2015-04-29 10:14:03 -0700 | [diff] [blame] | 436 | idxs[*i + 3] = *v + 4; |
| 437 | idxs[*i + 4] = *v + 3; |
bsalomon@google.com | 7d9ffc8 | 2013-05-14 14:20:28 +0000 | [diff] [blame] | 438 | idxs[*i + 5] = *v + 2; |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 439 | |
cdalton | 3596482 | 2015-04-29 10:14:03 -0700 | [diff] [blame] | 440 | *i += 6; |
| 441 | |
| 442 | // Draw the interior fan if it exists. |
| 443 | // TODO: Detect and combine colinear segments. This will ensure we catch every case |
| 444 | // with no interior, and that the resulting shared edge uses the same endpoints. |
| 445 | if (count >= 3) { |
| 446 | idxs[*i + 0] = *v + 0; |
| 447 | idxs[*i + 1] = *v + 2; |
| 448 | idxs[*i + 2] = *v + 1; |
| 449 | |
| 450 | *i += 3; |
| 451 | } |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 452 | |
bsalomon@google.com | 7d9ffc8 | 2013-05-14 14:20:28 +0000 | [diff] [blame] | 453 | *v += 5; |
bsalomon@google.com | 0680961 | 2012-01-21 15:03:39 +0000 | [diff] [blame] | 454 | } else { |
Brian Osman | d2fa2eb | 2018-12-26 16:48:40 -0500 | [diff] [blame] | 455 | void* quadVertsBegin = verts.fPtr; |
| 456 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 457 | SkPoint qpts[] = {sega.endPt(), segb.fPts[0], segb.fPts[1]}; |
bsalomon@google.com | 495e210 | 2012-01-21 14:48:36 +0000 | [diff] [blame] | 458 | |
Brian Osman | d2fa2eb | 2018-12-26 16:48:40 -0500 | [diff] [blame] | 459 | SkScalar c0 = segb.fNorms[0].dot(qpts[0]); |
| 460 | SkScalar c1 = segb.fNorms[1].dot(qpts[2]); |
| 461 | GrVertexWriter::Skip<SkPoint> skipUVs; |
| 462 | |
| 463 | verts.write(fanPt, |
| 464 | color, skipUVs, |
| 465 | -segb.fNorms[0].dot(fanPt) + c0, |
| 466 | -segb.fNorms[1].dot(fanPt) + c1); |
| 467 | |
| 468 | verts.write(qpts[0], |
| 469 | color, skipUVs, |
| 470 | 0.0f, |
| 471 | -segb.fNorms[1].dot(qpts[0]) + c1); |
| 472 | |
| 473 | verts.write(qpts[2], |
| 474 | color, skipUVs, |
| 475 | -segb.fNorms[0].dot(qpts[2]) + c0, |
| 476 | 0.0f); |
Greg Daniel | 1204c4b | 2020-04-22 14:10:12 -0400 | [diff] [blame] | 477 | // We need a negative value that is very large that it won't effect results if it is |
| 478 | // interpolated with. However, the value can't be too large of a negative that it |
| 479 | // effects numerical precision on less powerful GPUs. |
| 480 | static const SkScalar kStableLargeNegativeValue = -SK_ScalarMax/1000000; |
Brian Osman | d2fa2eb | 2018-12-26 16:48:40 -0500 | [diff] [blame] | 481 | verts.write(qpts[0] + segb.fNorms[0], |
| 482 | color, skipUVs, |
Greg Daniel | 1204c4b | 2020-04-22 14:10:12 -0400 | [diff] [blame] | 483 | kStableLargeNegativeValue, |
| 484 | kStableLargeNegativeValue); |
Brian Osman | d2fa2eb | 2018-12-26 16:48:40 -0500 | [diff] [blame] | 485 | |
| 486 | verts.write(qpts[2] + segb.fNorms[1], |
| 487 | color, skipUVs, |
Greg Daniel | 1204c4b | 2020-04-22 14:10:12 -0400 | [diff] [blame] | 488 | kStableLargeNegativeValue, |
| 489 | kStableLargeNegativeValue); |
Brian Osman | d2fa2eb | 2018-12-26 16:48:40 -0500 | [diff] [blame] | 490 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 491 | SkVector midVec = segb.fNorms[0] + segb.fNorms[1]; |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 492 | midVec.normalize(); |
bsalomon@google.com | 0680961 | 2012-01-21 15:03:39 +0000 | [diff] [blame] | 493 | |
Brian Osman | d2fa2eb | 2018-12-26 16:48:40 -0500 | [diff] [blame] | 494 | verts.write(qpts[1] + midVec, |
| 495 | color, skipUVs, |
Greg Daniel | 1204c4b | 2020-04-22 14:10:12 -0400 | [diff] [blame] | 496 | kStableLargeNegativeValue, |
| 497 | kStableLargeNegativeValue); |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 498 | |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 499 | GrPathUtils::QuadUVMatrix toUV(qpts); |
Brian Osman | 7f4735b | 2019-01-02 13:55:10 +0000 | [diff] [blame] | 500 | toUV.apply(quadVertsBegin, 6, vertexStride, uvOffset); |
bsalomon@google.com | 0680961 | 2012-01-21 15:03:39 +0000 | [diff] [blame] | 501 | |
bsalomon@google.com | 7d9ffc8 | 2013-05-14 14:20:28 +0000 | [diff] [blame] | 502 | idxs[*i + 0] = *v + 3; |
| 503 | idxs[*i + 1] = *v + 1; |
| 504 | idxs[*i + 2] = *v + 2; |
| 505 | idxs[*i + 3] = *v + 4; |
| 506 | idxs[*i + 4] = *v + 3; |
| 507 | idxs[*i + 5] = *v + 2; |
bsalomon@google.com | 0680961 | 2012-01-21 15:03:39 +0000 | [diff] [blame] | 508 | |
bsalomon@google.com | 7d9ffc8 | 2013-05-14 14:20:28 +0000 | [diff] [blame] | 509 | idxs[*i + 6] = *v + 5; |
| 510 | idxs[*i + 7] = *v + 3; |
| 511 | idxs[*i + 8] = *v + 4; |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 512 | |
cdalton | 3596482 | 2015-04-29 10:14:03 -0700 | [diff] [blame] | 513 | *i += 9; |
| 514 | |
| 515 | // Draw the interior fan if it exists. |
| 516 | // TODO: Detect and combine colinear segments. This will ensure we catch every case |
| 517 | // with no interior, and that the resulting shared edge uses the same endpoints. |
| 518 | if (count >= 3) { |
| 519 | idxs[*i + 0] = *v + 0; |
| 520 | idxs[*i + 1] = *v + 2; |
| 521 | idxs[*i + 2] = *v + 1; |
| 522 | |
| 523 | *i += 3; |
| 524 | } |
bsalomon@google.com | 9aed114 | 2012-01-30 14:28:39 +0000 | [diff] [blame] | 525 | |
bsalomon@google.com | 7d9ffc8 | 2013-05-14 14:20:28 +0000 | [diff] [blame] | 526 | *v += 6; |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 527 | } |
| 528 | } |
| 529 | } |
| 530 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 531 | /////////////////////////////////////////////////////////////////////////////// |
| 532 | |
| 533 | /* |
| 534 | * Quadratic specified by 0=u^2-v canonical coords. u and v are the first |
| 535 | * two components of the vertex attribute. Coverage is based on signed |
| 536 | * distance with negative being inside, positive outside. The edge is specified in |
| 537 | * window space (y-down). If either the third or fourth component of the interpolated |
| 538 | * vertex coord is > 0 then the pixel is considered outside the edge. This is used to |
skia.committer@gmail.com | 041e2db | 2013-04-03 07:01:14 +0000 | [diff] [blame] | 539 | * attempt to trim to a portion of the infinite quad. |
| 540 | * Requires shader derivative instruction support. |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 541 | */ |
| 542 | |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 543 | class QuadEdgeEffect : public GrGeometryProcessor { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 544 | public: |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 545 | static GrGeometryProcessor* Make(SkArenaAlloc* arena, |
| 546 | const SkMatrix& localMatrix, |
| 547 | bool usesLocalCoords, |
| 548 | bool wideColor) { |
| 549 | return arena->make<QuadEdgeEffect>(localMatrix, usesLocalCoords, wideColor); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 550 | } |
| 551 | |
Brian Salomon | d3b6597 | 2017-03-22 12:05:03 -0400 | [diff] [blame] | 552 | ~QuadEdgeEffect() override {} |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 553 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 554 | const char* name() const override { return "QuadEdge"; } |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 555 | |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 556 | class GLSLProcessor : public GrGLSLGeometryProcessor { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 557 | public: |
Brian Salomon | 60fb0b2 | 2017-06-15 17:09:36 -0400 | [diff] [blame] | 558 | GLSLProcessor() {} |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 559 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 560 | void onEmitCode(EmitArgs& args, GrGPArgs* gpArgs) override { |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 561 | const QuadEdgeEffect& qe = args.fGP.cast<QuadEdgeEffect>(); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 562 | GrGLSLVertexBuilder* vertBuilder = args.fVertBuilder; |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 563 | GrGLSLVaryingHandler* varyingHandler = args.fVaryingHandler; |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 564 | GrGLSLUniformHandler* uniformHandler = args.fUniformHandler; |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 565 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 566 | // emit attributes |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 567 | varyingHandler->emitAttributes(qe); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 568 | |
Chris Dalton | 2737288 | 2017-12-08 13:34:21 -0700 | [diff] [blame] | 569 | GrGLSLVarying v(kHalf4_GrSLType); |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 570 | varyingHandler->addVarying("QuadEdge", &v); |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 571 | vertBuilder->codeAppendf("%s = %s;", v.vsOut(), qe.fInQuadEdge.name()); |
Brian Salomon | 60fb0b2 | 2017-06-15 17:09:36 -0400 | [diff] [blame] | 572 | |
| 573 | // Setup pass through color |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 574 | varyingHandler->addPassThroughAttribute(qe.fInColor, args.fOutputColor); |
joshualitt | 2dd1ae0 | 2014-12-03 06:24:10 -0800 | [diff] [blame] | 575 | |
Chris Dalton | 6028361 | 2018-02-14 13:38:14 -0700 | [diff] [blame] | 576 | GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder; |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 577 | |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 578 | // Setup position |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 579 | this->writeOutputPosition(vertBuilder, gpArgs, qe.fInPosition.name()); |
joshualitt | abb52a1 | 2015-01-13 15:02:10 -0800 | [diff] [blame] | 580 | |
| 581 | // emit transforms |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 582 | this->emitTransforms(vertBuilder, |
egdaniel | 0eafe79 | 2015-11-20 14:01:22 -0800 | [diff] [blame] | 583 | varyingHandler, |
egdaniel | 7ea439b | 2015-12-03 09:20:44 -0800 | [diff] [blame] | 584 | uniformHandler, |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 585 | qe.fInPosition.asShaderVar(), |
Brian Salomon | 60fb0b2 | 2017-06-15 17:09:36 -0400 | [diff] [blame] | 586 | qe.fLocalMatrix, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 587 | args.fFPCoordTransformHandler); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 588 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 589 | fragBuilder->codeAppendf("half edgeAlpha;"); |
joshualitt | 30ba436 | 2014-08-21 20:18:45 -0700 | [diff] [blame] | 590 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 591 | // keep the derivative instructions outside the conditional |
Ethan Nicholas | e1f5502 | 2019-02-05 17:17:40 -0500 | [diff] [blame] | 592 | fragBuilder->codeAppendf("half2 duvdx = half2(dFdx(%s.xy));", v.fsIn()); |
| 593 | fragBuilder->codeAppendf("half2 duvdy = half2(dFdy(%s.xy));", v.fsIn()); |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 594 | fragBuilder->codeAppendf("if (%s.z > 0.0 && %s.w > 0.0) {", v.fsIn(), v.fsIn()); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 595 | // today we know z and w are in device space. We could use derivatives |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 596 | fragBuilder->codeAppendf("edgeAlpha = min(min(%s.z, %s.w) + 0.5, 1.0);", v.fsIn(), |
| 597 | v.fsIn()); |
| 598 | fragBuilder->codeAppendf ("} else {"); |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 599 | fragBuilder->codeAppendf("half2 gF = half2(2.0*%s.x*duvdx.x - duvdx.y," |
egdaniel | 4ca2e60 | 2015-11-18 08:01:26 -0800 | [diff] [blame] | 600 | " 2.0*%s.x*duvdy.x - duvdy.y);", |
| 601 | v.fsIn(), v.fsIn()); |
| 602 | fragBuilder->codeAppendf("edgeAlpha = (%s.x*%s.x - %s.y);", v.fsIn(), v.fsIn(), |
| 603 | v.fsIn()); |
| 604 | fragBuilder->codeAppendf("edgeAlpha = " |
Ethan Nicholas | 12fb9cf | 2018-08-03 16:16:57 -0400 | [diff] [blame] | 605 | "saturate(0.5 - edgeAlpha / length(gF));}"); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 606 | |
Ethan Nicholas | f7b8820 | 2017-09-18 14:10:39 -0400 | [diff] [blame] | 607 | fragBuilder->codeAppendf("%s = half4(edgeAlpha);", args.fOutputCoverage); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 608 | } |
| 609 | |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 610 | static inline void GenKey(const GrGeometryProcessor& gp, |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 611 | const GrShaderCaps&, |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 612 | GrProcessorKeyBuilder* b) { |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 613 | const QuadEdgeEffect& qee = gp.cast<QuadEdgeEffect>(); |
Brian Salomon | 60fb0b2 | 2017-06-15 17:09:36 -0400 | [diff] [blame] | 614 | b->add32(SkToBool(qee.fUsesLocalCoords && qee.fLocalMatrix.hasPerspective())); |
joshualitt | 9b98932 | 2014-12-15 14:16:27 -0800 | [diff] [blame] | 615 | } |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 616 | |
egdaniel | 018fb62 | 2015-10-28 07:26:40 -0700 | [diff] [blame] | 617 | void setData(const GrGLSLProgramDataManager& pdman, |
bsalomon | a624bf3 | 2016-09-20 09:12:47 -0700 | [diff] [blame] | 618 | const GrPrimitiveProcessor& gp, |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 619 | const CoordTransformRange& transformRange) override { |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 620 | const QuadEdgeEffect& qe = gp.cast<QuadEdgeEffect>(); |
Brian Salomon | c241b58 | 2019-11-27 08:57:17 -0500 | [diff] [blame] | 621 | this->setTransformDataHelper(qe.fLocalMatrix, pdman, transformRange); |
joshualitt | e3ababe | 2015-05-15 07:56:07 -0700 | [diff] [blame] | 622 | } |
| 623 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 624 | private: |
egdaniel | e659a58 | 2015-11-13 09:55:43 -0800 | [diff] [blame] | 625 | typedef GrGLSLGeometryProcessor INHERITED; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 626 | }; |
skia.committer@gmail.com | 041e2db | 2013-04-03 07:01:14 +0000 | [diff] [blame] | 627 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 628 | void getGLSLProcessorKey(const GrShaderCaps& caps, GrProcessorKeyBuilder* b) const override { |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 629 | GLSLProcessor::GenKey(*this, caps, b); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 630 | } |
| 631 | |
Brian Salomon | 94efbf5 | 2016-11-29 13:43:05 -0500 | [diff] [blame] | 632 | GrGLSLPrimitiveProcessor* createGLSLInstance(const GrShaderCaps&) const override { |
egdaniel | 57d3b03 | 2015-11-13 11:57:27 -0800 | [diff] [blame] | 633 | return new GLSLProcessor(); |
joshualitt | eb2a676 | 2014-12-04 11:35:33 -0800 | [diff] [blame] | 634 | } |
| 635 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 636 | private: |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 637 | friend class ::SkArenaAlloc; // for access to ctor |
| 638 | |
Brian Osman | 7f4735b | 2019-01-02 13:55:10 +0000 | [diff] [blame] | 639 | QuadEdgeEffect(const SkMatrix& localMatrix, bool usesLocalCoords, bool wideColor) |
Ethan Nicholas | abff956 | 2017-10-09 10:54:08 -0400 | [diff] [blame] | 640 | : INHERITED(kQuadEdgeEffect_ClassID) |
| 641 | , fLocalMatrix(localMatrix) |
| 642 | , fUsesLocalCoords(usesLocalCoords) { |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 643 | fInPosition = {"inPosition", kFloat2_GrVertexAttribType, kFloat2_GrSLType}; |
Brian Osman | 7f4735b | 2019-01-02 13:55:10 +0000 | [diff] [blame] | 644 | fInColor = MakeColorAttribute("inColor", wideColor); |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 645 | fInQuadEdge = {"inQuadEdge", kFloat4_GrVertexAttribType, kHalf4_GrSLType}; |
| 646 | this->setVertexAttributes(&fInPosition, 3); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Brian Osman | f04fb3c | 2018-11-12 15:34:00 -0500 | [diff] [blame] | 649 | Attribute fInPosition; |
| 650 | Attribute fInColor; |
| 651 | Attribute fInQuadEdge; |
| 652 | |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 653 | SkMatrix fLocalMatrix; |
| 654 | bool fUsesLocalCoords; |
joshualitt | 249af15 | 2014-09-15 11:41:13 -0700 | [diff] [blame] | 655 | |
Brian Salomon | 0c26a9d | 2017-07-06 10:09:38 -0400 | [diff] [blame] | 656 | GR_DECLARE_GEOMETRY_PROCESSOR_TEST |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 657 | |
joshualitt | 2e3b3e3 | 2014-12-09 13:31:14 -0800 | [diff] [blame] | 658 | typedef GrGeometryProcessor INHERITED; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 659 | }; |
| 660 | |
joshualitt | b0a8a37 | 2014-09-23 09:50:21 -0700 | [diff] [blame] | 661 | GR_DEFINE_GEOMETRY_PROCESSOR_TEST(QuadEdgeEffect); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 662 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 663 | #if GR_TEST_UTILS |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 664 | GrGeometryProcessor* QuadEdgeEffect::TestCreate(GrProcessorTestData* d) { |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 665 | // Doesn't work without derivative instructions. |
Robert Phillips | 296b1cc | 2017-03-15 10:42:12 -0400 | [diff] [blame] | 666 | return d->caps()->shaderCaps()->shaderDerivativeSupport() |
Robert Phillips | 7cd0bfe | 2019-11-20 16:08:10 -0500 | [diff] [blame] | 667 | ? QuadEdgeEffect::Make(d->allocator(), GrTest::TestMatrix(d->fRandom), |
| 668 | d->fRandom->nextBool(), d->fRandom->nextBool()) |
Brian Salomon | 9ae32a2 | 2017-01-25 14:58:24 -0500 | [diff] [blame] | 669 | : nullptr; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 670 | } |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 671 | #endif |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 672 | |
| 673 | /////////////////////////////////////////////////////////////////////////////// |
| 674 | |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 675 | GrPathRenderer::CanDrawPath |
| 676 | GrAAConvexPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
Michael Ludwig | 0a1e9ef | 2019-08-30 10:03:15 -0400 | [diff] [blame] | 677 | // This check requires convexity and known direction, since the direction is used to build |
| 678 | // the geometry segments. Degenerate convex paths will fall through to some other path renderer. |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 679 | if (args.fCaps->shaderCaps()->shaderDerivativeSupport() && |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 680 | (GrAAType::kCoverage == args.fAAType) && args.fShape->style().isSimpleFill() && |
Michael Ludwig | 0a1e9ef | 2019-08-30 10:03:15 -0400 | [diff] [blame] | 681 | !args.fShape->inverseFilled() && args.fShape->knownToBeConvex() && |
| 682 | args.fShape->knownDirection()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 683 | return CanDrawPath::kYes; |
| 684 | } |
| 685 | return CanDrawPath::kNo; |
robertphillips@google.com | fa66294 | 2012-05-17 12:20:22 +0000 | [diff] [blame] | 686 | } |
| 687 | |
Brian Salomon | 10978a6 | 2017-06-15 16:21:49 -0400 | [diff] [blame] | 688 | namespace { |
| 689 | |
| 690 | class AAConvexPathOp final : public GrMeshDrawOp { |
| 691 | private: |
| 692 | using Helper = GrSimpleMeshDrawOpHelperWithStencil; |
| 693 | |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 694 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 695 | DEFINE_OP_CLASS_ID |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 696 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 697 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 698 | GrPaint&& paint, |
| 699 | const SkMatrix& viewMatrix, |
Brian Salomon | 10978a6 | 2017-06-15 16:21:49 -0400 | [diff] [blame] | 700 | const SkPath& path, |
| 701 | const GrUserStencilSettings* stencilSettings) { |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 702 | return Helper::FactoryHelper<AAConvexPathOp>(context, std::move(paint), viewMatrix, path, |
Brian Salomon | 10978a6 | 2017-06-15 16:21:49 -0400 | [diff] [blame] | 703 | stencilSettings); |
| 704 | } |
| 705 | |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 706 | AAConvexPathOp(const Helper::MakeArgs& helperArgs, const SkPMColor4f& color, |
Brian Osman | 936fe7d | 2018-10-30 15:30:35 -0400 | [diff] [blame] | 707 | const SkMatrix& viewMatrix, const SkPath& path, |
| 708 | const GrUserStencilSettings* stencilSettings) |
Brian Salomon | 60fb0b2 | 2017-06-15 17:09:36 -0400 | [diff] [blame] | 709 | : INHERITED(ClassID()), fHelper(helperArgs, GrAAType::kCoverage, stencilSettings) { |
| 710 | fPaths.emplace_back(PathData{viewMatrix, path, color}); |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 711 | this->setTransformedBounds(path.getBounds(), viewMatrix, HasAABloat::kYes, |
| 712 | IsHairline::kNo); |
bsalomon | f170309 | 2016-06-29 18:41:53 -0700 | [diff] [blame] | 713 | } |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 714 | |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 715 | const char* name() const override { return "AAConvexPathOp"; } |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 716 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame] | 717 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | 3eabf4a | 2020-03-10 14:49:20 -0400 | [diff] [blame] | 718 | if (fProgramInfo) { |
Chris Dalton | be45742 | 2020-03-16 18:05:03 -0600 | [diff] [blame] | 719 | fProgramInfo->visitFPProxies(func); |
Robert Phillips | 3eabf4a | 2020-03-10 14:49:20 -0400 | [diff] [blame] | 720 | } else { |
| 721 | fHelper.visitProxies(func); |
| 722 | } |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 723 | } |
| 724 | |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 725 | #ifdef SK_DEBUG |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 726 | SkString dumpInfo() const override { |
| 727 | SkString string; |
Brian Salomon | 60fb0b2 | 2017-06-15 17:09:36 -0400 | [diff] [blame] | 728 | string.appendf("Count: %d\n", fPaths.count()); |
Brian Salomon | 10978a6 | 2017-06-15 16:21:49 -0400 | [diff] [blame] | 729 | string += fHelper.dumpInfo(); |
| 730 | string += INHERITED::dumpInfo(); |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 731 | return string; |
| 732 | } |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 733 | #endif |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 734 | |
Brian Salomon | 10978a6 | 2017-06-15 16:21:49 -0400 | [diff] [blame] | 735 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 736 | |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 737 | GrProcessorSet::Analysis finalize( |
| 738 | const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage, |
| 739 | GrClampType clampType) override { |
Chris Dalton | b8fff0d | 2019-03-05 10:11:58 -0700 | [diff] [blame] | 740 | return fHelper.finalizeProcessors( |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 741 | caps, clip, hasMixedSampledCoverage, clampType, |
| 742 | GrProcessorAnalysisCoverage::kSingleChannel, &fPaths.back().fColor, &fWideColor); |
Brian Salomon | 10978a6 | 2017-06-15 16:21:49 -0400 | [diff] [blame] | 743 | } |
| 744 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 745 | private: |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 746 | GrProgramInfo* programInfo() override { return fProgramInfo; } |
| 747 | |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 748 | void onCreateProgramInfo(const GrCaps* caps, |
| 749 | SkArenaAlloc* arena, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 750 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 751 | GrAppliedClip&& appliedClip, |
| 752 | const GrXferProcessor::DstProxyView& dstProxyView) override { |
Robert Phillips | 3eabf4a | 2020-03-10 14:49:20 -0400 | [diff] [blame] | 753 | SkMatrix invert; |
| 754 | if (fHelper.usesLocalCoords() && !fPaths.back().fViewMatrix.invert(&invert)) { |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 755 | return; |
Robert Phillips | 3eabf4a | 2020-03-10 14:49:20 -0400 | [diff] [blame] | 756 | } |
| 757 | |
| 758 | GrGeometryProcessor* quadProcessor = QuadEdgeEffect::Make(arena, invert, |
| 759 | fHelper.usesLocalCoords(), |
| 760 | fWideColor); |
| 761 | |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame] | 762 | fProgramInfo = fHelper.createProgramInfoWithStencil(caps, arena, writeView, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 763 | std::move(appliedClip), |
| 764 | dstProxyView, quadProcessor, |
| 765 | GrPrimitiveType::kTriangles); |
Robert Phillips | 3eabf4a | 2020-03-10 14:49:20 -0400 | [diff] [blame] | 766 | } |
| 767 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 768 | void onPrepareDraws(Target* target) override { |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 769 | int instanceCount = fPaths.count(); |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 770 | |
Robert Phillips | 3eabf4a | 2020-03-10 14:49:20 -0400 | [diff] [blame] | 771 | if (!fProgramInfo) { |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 772 | this->createProgramInfo(target); |
Robert Phillips | 3eabf4a | 2020-03-10 14:49:20 -0400 | [diff] [blame] | 773 | if (!fProgramInfo) { |
| 774 | return; |
| 775 | } |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 776 | } |
| 777 | |
Robert Phillips | 3eabf4a | 2020-03-10 14:49:20 -0400 | [diff] [blame] | 778 | const size_t kVertexStride = fProgramInfo->primProc().vertexStride(); |
| 779 | |
| 780 | fDraws.reserve(instanceCount); |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 781 | |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 782 | // TODO generate all segments for all paths and use one vertex buffer |
| 783 | for (int i = 0; i < instanceCount; i++) { |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 784 | const PathData& args = fPaths[i]; |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 785 | |
| 786 | // We use the fact that SkPath::transform path does subdivision based on |
| 787 | // perspective. Otherwise, we apply the view matrix when copying to the |
| 788 | // segment representation. |
| 789 | const SkMatrix* viewMatrix = &args.fViewMatrix; |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 790 | |
| 791 | // We avoid initializing the path unless we have to |
| 792 | const SkPath* pathPtr = &args.fPath; |
| 793 | SkTLazy<SkPath> tmpPath; |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 794 | if (viewMatrix->hasPerspective()) { |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 795 | SkPath* tmpPathPtr = tmpPath.init(*pathPtr); |
| 796 | tmpPathPtr->setIsVolatile(true); |
| 797 | tmpPathPtr->transform(*viewMatrix); |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 798 | viewMatrix = &SkMatrix::I(); |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 799 | pathPtr = tmpPathPtr; |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 800 | } |
| 801 | |
| 802 | int vertexCount; |
| 803 | int indexCount; |
| 804 | enum { |
| 805 | kPreallocSegmentCnt = 512 / sizeof(Segment), |
| 806 | kPreallocDrawCnt = 4, |
| 807 | }; |
| 808 | SkSTArray<kPreallocSegmentCnt, Segment, true> segments; |
| 809 | SkPoint fanPt; |
| 810 | |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 811 | if (!get_segments(*pathPtr, *viewMatrix, &segments, &fanPt, &vertexCount, |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 812 | &indexCount)) { |
| 813 | continue; |
| 814 | } |
| 815 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 816 | sk_sp<const GrBuffer> vertexBuffer; |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 817 | int firstVertex; |
| 818 | |
Brian Osman | d2fa2eb | 2018-12-26 16:48:40 -0500 | [diff] [blame] | 819 | GrVertexWriter verts{target->makeVertexSpace(kVertexStride, vertexCount, |
| 820 | &vertexBuffer, &firstVertex)}; |
bsalomon | e64eb57 | 2015-05-07 11:35:55 -0700 | [diff] [blame] | 821 | |
Brian Osman | d2fa2eb | 2018-12-26 16:48:40 -0500 | [diff] [blame] | 822 | if (!verts.fPtr) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 823 | SkDebugf("Could not allocate vertices\n"); |
| 824 | return; |
| 825 | } |
| 826 | |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 827 | sk_sp<const GrBuffer> indexBuffer; |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 828 | int firstIndex; |
| 829 | |
| 830 | uint16_t *idxs = target->makeIndexSpace(indexCount, &indexBuffer, &firstIndex); |
robertphillips | e40d397 | 2015-05-07 09:51:43 -0700 | [diff] [blame] | 831 | if (!idxs) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 832 | SkDebugf("Could not allocate indices\n"); |
| 833 | return; |
| 834 | } |
| 835 | |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 836 | SkSTArray<kPreallocDrawCnt, Draw, true> draws; |
Brian Osman | 7f4735b | 2019-01-02 13:55:10 +0000 | [diff] [blame] | 837 | GrVertexColor color(args.fColor, fWideColor); |
| 838 | create_vertices(segments, fanPt, color, &draws, verts, idxs, kVertexStride); |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 839 | |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 840 | GrSimpleMesh* meshes = target->allocMeshes(draws.count()); |
robertphillips | 44c3128 | 2015-09-03 12:58:48 -0700 | [diff] [blame] | 841 | for (int j = 0; j < draws.count(); ++j) { |
| 842 | const Draw& draw = draws[j]; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 843 | meshes[j].setIndexed(indexBuffer, draw.fIndexCnt, firstIndex, 0, |
Chris Dalton | 37c7bdd | 2020-03-13 09:21:12 -0600 | [diff] [blame] | 844 | draw.fVertexCnt - 1, GrPrimitiveRestart::kNo, vertexBuffer, |
| 845 | firstVertex); |
Chris Dalton | bca46e2 | 2017-05-15 11:03:26 -0600 | [diff] [blame] | 846 | firstIndex += draw.fIndexCnt; |
| 847 | firstVertex += draw.fVertexCnt; |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 848 | } |
Robert Phillips | 3eabf4a | 2020-03-10 14:49:20 -0400 | [diff] [blame] | 849 | |
| 850 | fDraws.push_back({ meshes, draws.count() }); |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 851 | } |
| 852 | } |
| 853 | |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 854 | void onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) override { |
Robert Phillips | 3eabf4a | 2020-03-10 14:49:20 -0400 | [diff] [blame] | 855 | if (!fProgramInfo || fDraws.isEmpty()) { |
| 856 | return; |
| 857 | } |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 858 | |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 859 | flushState->bindPipelineAndScissorClip(*fProgramInfo, chainBounds); |
| 860 | flushState->bindTextures(fProgramInfo->primProc(), nullptr, fProgramInfo->pipeline()); |
Robert Phillips | 3eabf4a | 2020-03-10 14:49:20 -0400 | [diff] [blame] | 861 | for (int i = 0; i < fDraws.count(); ++i) { |
Chris Dalton | 765ed36 | 2020-03-16 17:34:44 -0600 | [diff] [blame] | 862 | for (int j = 0; j < fDraws[i].fMeshCount; ++j) { |
| 863 | flushState->drawMesh(fDraws[i].fMeshes[j]); |
| 864 | } |
Robert Phillips | 3eabf4a | 2020-03-10 14:49:20 -0400 | [diff] [blame] | 865 | } |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 866 | } |
| 867 | |
Michael Ludwig | 28b0c5d | 2019-12-19 14:51:00 -0500 | [diff] [blame] | 868 | CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*, |
| 869 | const GrCaps& caps) override { |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 870 | AAConvexPathOp* that = t->cast<AAConvexPathOp>(); |
Brian Salomon | 10978a6 | 2017-06-15 16:21:49 -0400 | [diff] [blame] | 871 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 872 | return CombineResult::kCannotCombine; |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 873 | } |
Brian Salomon | 10978a6 | 2017-06-15 16:21:49 -0400 | [diff] [blame] | 874 | if (fHelper.usesLocalCoords() && |
Mike Reed | 2c38315 | 2019-12-18 16:47:47 -0500 | [diff] [blame] | 875 | !SkMatrixPriv::CheapEqual(fPaths[0].fViewMatrix, that->fPaths[0].fViewMatrix)) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 876 | return CombineResult::kCannotCombine; |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 877 | } |
| 878 | |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 879 | fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin()); |
Brian Osman | 7f4735b | 2019-01-02 13:55:10 +0000 | [diff] [blame] | 880 | fWideColor |= that->fWideColor; |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 881 | return CombineResult::kMerged; |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 882 | } |
| 883 | |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 884 | struct PathData { |
Robert Phillips | 3eabf4a | 2020-03-10 14:49:20 -0400 | [diff] [blame] | 885 | SkMatrix fViewMatrix; |
| 886 | SkPath fPath; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 887 | SkPMColor4f fColor; |
bsalomon | f170309 | 2016-06-29 18:41:53 -0700 | [diff] [blame] | 888 | }; |
| 889 | |
Brian Salomon | 10978a6 | 2017-06-15 16:21:49 -0400 | [diff] [blame] | 890 | Helper fHelper; |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 891 | SkSTArray<1, PathData, true> fPaths; |
Brian Osman | 7f4735b | 2019-01-02 13:55:10 +0000 | [diff] [blame] | 892 | bool fWideColor; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 893 | |
Robert Phillips | 3eabf4a | 2020-03-10 14:49:20 -0400 | [diff] [blame] | 894 | struct MeshDraw { |
Chris Dalton | eb694b7 | 2020-03-16 09:25:50 -0600 | [diff] [blame] | 895 | GrSimpleMesh* fMeshes; |
| 896 | int fMeshCount; |
Robert Phillips | 3eabf4a | 2020-03-10 14:49:20 -0400 | [diff] [blame] | 897 | }; |
| 898 | |
| 899 | SkTDArray<MeshDraw> fDraws; |
| 900 | GrProgramInfo* fProgramInfo = nullptr; |
| 901 | |
Brian Salomon | 10978a6 | 2017-06-15 16:21:49 -0400 | [diff] [blame] | 902 | typedef GrMeshDrawOp INHERITED; |
joshualitt | 27f398f | 2015-02-05 14:39:01 -0800 | [diff] [blame] | 903 | }; |
| 904 | |
Brian Salomon | 10978a6 | 2017-06-15 16:21:49 -0400 | [diff] [blame] | 905 | } // anonymous namespace |
| 906 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 907 | bool GrAAConvexPathRenderer::onDrawPath(const DrawPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 908 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 909 | "GrAAConvexPathRenderer::onDrawPath"); |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 910 | SkASSERT(args.fRenderTargetContext->numSamples() <= 1); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 911 | SkASSERT(!args.fShape->isEmpty()); |
bsalomon@google.com | 4647f90 | 2013-03-26 14:45:27 +0000 | [diff] [blame] | 912 | |
bsalomon | f170309 | 2016-06-29 18:41:53 -0700 | [diff] [blame] | 913 | SkPath path; |
| 914 | args.fShape->asPath(&path); |
bsalomon@google.com | af90f7f | 2012-03-05 20:50:10 +0000 | [diff] [blame] | 915 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 916 | std::unique_ptr<GrDrawOp> op = AAConvexPathOp::Make(args.fContext, std::move(args.fPaint), |
| 917 | *args.fViewMatrix, |
Brian Salomon | 10978a6 | 2017-06-15 16:21:49 -0400 | [diff] [blame] | 918 | path, args.fUserStencilSettings); |
Michael Ludwig | 7c12e28 | 2020-05-29 09:54:07 -0400 | [diff] [blame] | 919 | args.fRenderTargetContext->addDrawOp(args.fClip, std::move(op)); |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 920 | return true; |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 921 | } |
joshualitt | 8e5c177 | 2015-05-11 08:58:52 -0700 | [diff] [blame] | 922 | |
| 923 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 924 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 925 | #if GR_TEST_UTILS |
joshualitt | 8e5c177 | 2015-05-11 08:58:52 -0700 | [diff] [blame] | 926 | |
Brian Salomon | 10978a6 | 2017-06-15 16:21:49 -0400 | [diff] [blame] | 927 | GR_DRAW_OP_TEST_DEFINE(AAConvexPathOp) { |
bsalomon | f170309 | 2016-06-29 18:41:53 -0700 | [diff] [blame] | 928 | SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
| 929 | SkPath path = GrTest::TestPathConvex(random); |
Brian Salomon | 10978a6 | 2017-06-15 16:21:49 -0400 | [diff] [blame] | 930 | const GrUserStencilSettings* stencilSettings = GrGetRandomStencil(random, context); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 931 | return AAConvexPathOp::Make(context, std::move(paint), viewMatrix, path, stencilSettings); |
joshualitt | 8e5c177 | 2015-05-11 08:58:52 -0700 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | #endif |