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