bsalomon@google.com | f75b84e | 2011-09-29 14:58:28 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2011 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/SkPoint3.h" |
| 9 | #include "include/private/SkTemplates.h" |
| 10 | #include "src/core/SkGeometry.h" |
| 11 | #include "src/core/SkMatrixPriv.h" |
| 12 | #include "src/core/SkPointPriv.h" |
| 13 | #include "src/core/SkRectPriv.h" |
| 14 | #include "src/core/SkStroke.h" |
Greg Daniel | f91aeb2 | 2019-06-18 09:58:02 -0400 | [diff] [blame] | 15 | #include "src/gpu/GrAuditTrail.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 16 | #include "src/gpu/GrBuffer.h" |
| 17 | #include "src/gpu/GrCaps.h" |
| 18 | #include "src/gpu/GrClip.h" |
| 19 | #include "src/gpu/GrDefaultGeoProcFactory.h" |
| 20 | #include "src/gpu/GrDrawOpTest.h" |
| 21 | #include "src/gpu/GrOpFlushState.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 22 | #include "src/gpu/GrProcessor.h" |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 23 | #include "src/gpu/GrProgramInfo.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 24 | #include "src/gpu/GrResourceProvider.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 25 | #include "src/gpu/GrStyle.h" |
| 26 | #include "src/gpu/effects/GrBezierEffect.h" |
Michael Ludwig | 663afe5 | 2019-06-03 16:46:19 -0400 | [diff] [blame] | 27 | #include "src/gpu/geometry/GrPathUtils.h" |
| 28 | #include "src/gpu/geometry/GrShape.h" |
Mike Klein | c0bd9f9 | 2019-04-23 12:05:21 -0500 | [diff] [blame] | 29 | #include "src/gpu/ops/GrAAHairLinePathRenderer.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" |
bsalomon@google.com | 4647f90 | 2013-03-26 14:45:27 +0000 | [diff] [blame] | 32 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 33 | #define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true> |
| 34 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 35 | // quadratics are rendered as 5-sided polys in order to bound the |
| 36 | // AA stroke around the center-curve. See comments in push_quad_index_buffer and |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 37 | // bloat_quad. Quadratics and conics share an index buffer |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 38 | |
robertphillips@google.com | ada90da | 2013-09-18 22:14:49 +0000 | [diff] [blame] | 39 | // lines are rendered as: |
| 40 | // *______________* |
| 41 | // |\ -_______ /| |
| 42 | // | \ \ / | |
| 43 | // | *--------* | |
| 44 | // | / ______/ \ | |
| 45 | // */_-__________\* |
| 46 | // For: 6 vertices and 18 indices (for 6 triangles) |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 47 | |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 48 | // Each quadratic is rendered as a five sided polygon. This poly bounds |
| 49 | // the quadratic's bounding triangle but has been expanded so that the |
| 50 | // 1-pixel wide area around the curve is inside the poly. |
| 51 | // If a,b,c are the original control points then the poly a0,b0,c0,c1,a1 |
| 52 | // that is rendered would look like this: |
| 53 | // b0 |
| 54 | // b |
| 55 | // |
| 56 | // a0 c0 |
| 57 | // a c |
| 58 | // a1 c1 |
egdaniel | 14afb43 | 2014-12-22 10:57:08 -0800 | [diff] [blame] | 59 | // Each is drawn as three triangles ((a0,a1,b0), (b0,c1,c0), (a1,c1,b0)) |
| 60 | // specified by these 9 indices: |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 61 | static const uint16_t kQuadIdxBufPattern[] = { |
| 62 | 0, 1, 2, |
| 63 | 2, 4, 3, |
| 64 | 1, 4, 2 |
| 65 | }; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 66 | |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 67 | static const int kIdxsPerQuad = SK_ARRAY_COUNT(kQuadIdxBufPattern); |
| 68 | static const int kQuadNumVertices = 5; |
| 69 | static const int kQuadsNumInIdxBuffer = 256; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 70 | GR_DECLARE_STATIC_UNIQUE_KEY(gQuadsIndexBufferKey); |
| 71 | |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 72 | static sk_sp<const GrBuffer> get_quads_index_buffer(GrResourceProvider* resourceProvider) { |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 73 | GR_DEFINE_STATIC_UNIQUE_KEY(gQuadsIndexBufferKey); |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 74 | return resourceProvider->findOrCreatePatternedIndexBuffer( |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 75 | kQuadIdxBufPattern, kIdxsPerQuad, kQuadsNumInIdxBuffer, kQuadNumVertices, |
| 76 | gQuadsIndexBufferKey); |
| 77 | } |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 78 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 79 | |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 80 | // Each line segment is rendered as two quads and two triangles. |
| 81 | // p0 and p1 have alpha = 1 while all other points have alpha = 0. |
| 82 | // The four external points are offset 1 pixel perpendicular to the |
| 83 | // line and half a pixel parallel to the line. |
| 84 | // |
| 85 | // p4 p5 |
| 86 | // p0 p1 |
| 87 | // p2 p3 |
| 88 | // |
| 89 | // Each is drawn as six triangles specified by these 18 indices: |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 90 | |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 91 | static const uint16_t kLineSegIdxBufPattern[] = { |
| 92 | 0, 1, 3, |
| 93 | 0, 3, 2, |
| 94 | 0, 4, 5, |
| 95 | 0, 5, 1, |
| 96 | 0, 2, 4, |
| 97 | 1, 5, 3 |
| 98 | }; |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 99 | |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 100 | static const int kIdxsPerLineSeg = SK_ARRAY_COUNT(kLineSegIdxBufPattern); |
| 101 | static const int kLineSegNumVertices = 6; |
| 102 | static const int kLineSegsNumInIdxBuffer = 256; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 103 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 104 | GR_DECLARE_STATIC_UNIQUE_KEY(gLinesIndexBufferKey); |
| 105 | |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 106 | static sk_sp<const GrBuffer> get_lines_index_buffer(GrResourceProvider* resourceProvider) { |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 107 | GR_DEFINE_STATIC_UNIQUE_KEY(gLinesIndexBufferKey); |
Chris Dalton | ff92650 | 2017-05-03 14:36:54 -0400 | [diff] [blame] | 108 | return resourceProvider->findOrCreatePatternedIndexBuffer( |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 109 | kLineSegIdxBufPattern, kIdxsPerLineSeg, kLineSegsNumInIdxBuffer, kLineSegNumVertices, |
| 110 | gLinesIndexBufferKey); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 111 | } |
| 112 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 113 | // Takes 178th time of logf on Z600 / VC2010 |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 114 | static int get_float_exp(float x) { |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 115 | static_assert(sizeof(int) == sizeof(float)); |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 116 | #ifdef SK_DEBUG |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 117 | static bool tested; |
| 118 | if (!tested) { |
| 119 | tested = true; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 120 | SkASSERT(get_float_exp(0.25f) == -2); |
| 121 | SkASSERT(get_float_exp(0.3f) == -2); |
| 122 | SkASSERT(get_float_exp(0.5f) == -1); |
| 123 | SkASSERT(get_float_exp(1.f) == 0); |
| 124 | SkASSERT(get_float_exp(2.f) == 1); |
| 125 | SkASSERT(get_float_exp(2.5f) == 1); |
| 126 | SkASSERT(get_float_exp(8.f) == 3); |
| 127 | SkASSERT(get_float_exp(100.f) == 6); |
| 128 | SkASSERT(get_float_exp(1000.f) == 9); |
| 129 | SkASSERT(get_float_exp(1024.f) == 10); |
| 130 | SkASSERT(get_float_exp(3000000.f) == 21); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 131 | } |
| 132 | #endif |
bsalomon@google.com | 2ec7280 | 2011-09-21 21:46:03 +0000 | [diff] [blame] | 133 | const int* iptr = (const int*)&x; |
| 134 | return (((*iptr) & 0x7f800000) >> 23) - 127; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 135 | } |
| 136 | |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 137 | // Uses the max curvature function for quads to estimate |
| 138 | // where to chop the conic. If the max curvature is not |
| 139 | // found along the curve segment it will return 1 and |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 140 | // dst[0] is the original conic. If it returns 2 the dst[0] |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 141 | // and dst[1] are the two new conics. |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 142 | static int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) { |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 143 | SkScalar t = SkFindQuadMaxCurvature(src); |
Chris Dalton | 1d474dd | 2018-07-24 01:08:31 -0600 | [diff] [blame] | 144 | if (t == 0 || t == 1) { |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 145 | if (dst) { |
| 146 | dst[0].set(src, weight); |
| 147 | } |
| 148 | return 1; |
| 149 | } else { |
| 150 | if (dst) { |
| 151 | SkConic conic; |
| 152 | conic.set(src, weight); |
caryclark | 414c429 | 2016-09-26 11:03:54 -0700 | [diff] [blame] | 153 | if (!conic.chopAt(t, dst)) { |
| 154 | dst[0].set(src, weight); |
| 155 | return 1; |
| 156 | } |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 157 | } |
| 158 | return 2; |
| 159 | } |
| 160 | } |
| 161 | |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 162 | // Calls split_conic on the entire conic and then once more on each subsection. |
| 163 | // Most cases will result in either 1 conic (chop point is not within t range) |
| 164 | // or 3 points (split once and then one subsection is split again). |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 165 | static int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) { |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 166 | SkConic dstTemp[2]; |
| 167 | int conicCnt = split_conic(src, dstTemp, weight); |
| 168 | if (2 == conicCnt) { |
| 169 | int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW); |
| 170 | conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW); |
| 171 | } else { |
| 172 | dst[0] = dstTemp[0]; |
| 173 | } |
| 174 | return conicCnt; |
| 175 | } |
| 176 | |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 177 | // returns 0 if quad/conic is degen or close to it |
| 178 | // in this case approx the path with lines |
| 179 | // otherwise returns 1 |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 180 | static int is_degen_quad_or_conic(const SkPoint p[3], SkScalar* dsqd) { |
jvanverth | cfeb85f | 2016-04-29 07:38:10 -0700 | [diff] [blame] | 181 | static const SkScalar gDegenerateToLineTol = GrPathUtils::kDefaultTolerance; |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 182 | static const SkScalar gDegenerateToLineTolSqd = |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 183 | gDegenerateToLineTol * gDegenerateToLineTol; |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 184 | |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 185 | if (SkPointPriv::DistanceToSqd(p[0], p[1]) < gDegenerateToLineTolSqd || |
| 186 | SkPointPriv::DistanceToSqd(p[1], p[2]) < gDegenerateToLineTolSqd) { |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 187 | return 1; |
| 188 | } |
| 189 | |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 190 | *dsqd = SkPointPriv::DistanceToLineBetweenSqd(p[1], p[0], p[2]); |
joshualitt | 6364807 | 2015-02-19 10:25:21 -0800 | [diff] [blame] | 191 | if (*dsqd < gDegenerateToLineTolSqd) { |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 192 | return 1; |
| 193 | } |
| 194 | |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 195 | if (SkPointPriv::DistanceToLineBetweenSqd(p[2], p[1], p[0]) < gDegenerateToLineTolSqd) { |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 196 | return 1; |
| 197 | } |
| 198 | return 0; |
| 199 | } |
| 200 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 201 | static int is_degen_quad_or_conic(const SkPoint p[3]) { |
joshualitt | 6364807 | 2015-02-19 10:25:21 -0800 | [diff] [blame] | 202 | SkScalar dsqd; |
| 203 | return is_degen_quad_or_conic(p, &dsqd); |
| 204 | } |
| 205 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 206 | // we subdivide the quads to avoid huge overfill |
| 207 | // if it returns -1 then should be drawn as lines |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 208 | static int num_quad_subdivs(const SkPoint p[3]) { |
joshualitt | 6364807 | 2015-02-19 10:25:21 -0800 | [diff] [blame] | 209 | SkScalar dsqd; |
| 210 | if (is_degen_quad_or_conic(p, &dsqd)) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 211 | return -1; |
| 212 | } |
| 213 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 214 | // tolerance of triangle height in pixels |
| 215 | // tuned on windows Quadro FX 380 / Z600 |
| 216 | // trade off of fill vs cpu time on verts |
| 217 | // maybe different when do this using gpu (geo or tess shaders) |
| 218 | static const SkScalar gSubdivTol = 175 * SK_Scalar1; |
| 219 | |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 220 | if (dsqd <= gSubdivTol * gSubdivTol) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 221 | return 0; |
| 222 | } else { |
robertphillips@google.com | 87379e1 | 2013-03-29 12:11:10 +0000 | [diff] [blame] | 223 | static const int kMaxSub = 4; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 224 | // subdividing the quad reduces d by 4. so we want x = log4(d/tol) |
| 225 | // = log4(d*d/tol*tol)/2 |
| 226 | // = log2(d*d/tol*tol) |
| 227 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 228 | // +1 since we're ignoring the mantissa contribution. |
| 229 | int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1; |
Brian Osman | 788b916 | 2020-02-07 10:36:46 -0500 | [diff] [blame] | 230 | log = std::min(std::max(0, log), kMaxSub); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 231 | return log; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 235 | /** |
| 236 | * Generates the lines and quads to be rendered. Lines are always recorded in |
| 237 | * device space. We will do a device space bloat to account for the 1pixel |
| 238 | * thickness. |
| 239 | * Quads are recorded in device space unless m contains |
| 240 | * perspective, then in they are in src space. We do this because we will |
| 241 | * subdivide large quads to reduce over-fill. This subdivision has to be |
| 242 | * performed before applying the perspective matrix. |
| 243 | */ |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 244 | static int gather_lines_and_quads(const SkPath& path, |
| 245 | const SkMatrix& m, |
| 246 | const SkIRect& devClipBounds, |
Brian Osman | cf3dc29 | 2017-06-28 16:45:32 -0400 | [diff] [blame] | 247 | SkScalar capLength, |
Brian Salomon | 969a738 | 2018-05-09 13:28:44 -0400 | [diff] [blame] | 248 | bool convertConicsToQuads, |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 249 | GrAAHairLinePathRenderer::PtArray* lines, |
| 250 | GrAAHairLinePathRenderer::PtArray* quads, |
| 251 | GrAAHairLinePathRenderer::PtArray* conics, |
| 252 | GrAAHairLinePathRenderer::IntArray* quadSubdivCnts, |
| 253 | GrAAHairLinePathRenderer::FloatArray* conicWeights) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 254 | SkPath::Iter iter(path, false); |
| 255 | |
| 256 | int totalQuadCount = 0; |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 257 | SkRect bounds; |
| 258 | SkIRect ibounds; |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 259 | |
| 260 | bool persp = m.hasPerspective(); |
| 261 | |
Brian Osman | cf3dc29 | 2017-06-28 16:45:32 -0400 | [diff] [blame] | 262 | // Whenever a degenerate, zero-length contour is encountered, this code will insert a |
| 263 | // 'capLength' x-aligned line segment. Since this is rendering hairlines it is hoped this will |
| 264 | // suffice for AA square & circle capping. |
| 265 | int verbsInContour = 0; // Does not count moves |
| 266 | bool seenZeroLengthVerb = false; |
| 267 | SkPoint zeroVerbPt; |
| 268 | |
Brian Salomon | 969a738 | 2018-05-09 13:28:44 -0400 | [diff] [blame] | 269 | // Adds a quad that has already been chopped to the list and checks for quads that are close to |
| 270 | // lines. Also does a bounding box check. It takes points that are in src space and device |
| 271 | // space. The src points are only required if the view matrix has perspective. |
| 272 | auto addChoppedQuad = [&](const SkPoint srcPts[3], const SkPoint devPts[4], |
| 273 | bool isContourStart) { |
| 274 | SkRect bounds; |
| 275 | SkIRect ibounds; |
| 276 | bounds.setBounds(devPts, 3); |
| 277 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 278 | bounds.roundOut(&ibounds); |
| 279 | // We only need the src space space pts when not in perspective. |
| 280 | SkASSERT(srcPts || !persp); |
| 281 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
| 282 | int subdiv = num_quad_subdivs(devPts); |
| 283 | SkASSERT(subdiv >= -1); |
| 284 | if (-1 == subdiv) { |
| 285 | SkPoint* pts = lines->push_back_n(4); |
| 286 | pts[0] = devPts[0]; |
| 287 | pts[1] = devPts[1]; |
| 288 | pts[2] = devPts[1]; |
| 289 | pts[3] = devPts[2]; |
| 290 | if (isContourStart && pts[0] == pts[1] && pts[2] == pts[3]) { |
| 291 | seenZeroLengthVerb = true; |
| 292 | zeroVerbPt = pts[0]; |
| 293 | } |
| 294 | } else { |
| 295 | // when in perspective keep quads in src space |
| 296 | const SkPoint* qPts = persp ? srcPts : devPts; |
| 297 | SkPoint* pts = quads->push_back_n(3); |
| 298 | pts[0] = qPts[0]; |
| 299 | pts[1] = qPts[1]; |
| 300 | pts[2] = qPts[2]; |
| 301 | quadSubdivCnts->push_back() = subdiv; |
| 302 | totalQuadCount += 1 << subdiv; |
| 303 | } |
| 304 | } |
| 305 | }; |
| 306 | |
| 307 | // Applies the view matrix to quad src points and calls the above helper. |
| 308 | auto addSrcChoppedQuad = [&](const SkPoint srcSpaceQuadPts[3], bool isContourStart) { |
| 309 | SkPoint devPts[3]; |
| 310 | m.mapPoints(devPts, srcSpaceQuadPts, 3); |
| 311 | addChoppedQuad(srcSpaceQuadPts, devPts, isContourStart); |
| 312 | }; |
| 313 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 314 | for (;;) { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 315 | SkPoint pathPts[4]; |
Mike Reed | ba7e9a6 | 2019-08-16 13:30:34 -0400 | [diff] [blame] | 316 | SkPath::Verb verb = iter.next(pathPts); |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 317 | switch (verb) { |
Brian Salomon | 969a738 | 2018-05-09 13:28:44 -0400 | [diff] [blame] | 318 | case SkPath::kConic_Verb: |
| 319 | if (convertConicsToQuads) { |
| 320 | SkScalar weight = iter.conicWeight(); |
| 321 | SkAutoConicToQuads converter; |
Brian Osman | e3deee1 | 2018-11-20 11:10:15 -0500 | [diff] [blame] | 322 | const SkPoint* quadPts = converter.computeQuads(pathPts, weight, 0.25f); |
Brian Salomon | 969a738 | 2018-05-09 13:28:44 -0400 | [diff] [blame] | 323 | for (int i = 0; i < converter.countQuads(); ++i) { |
| 324 | addSrcChoppedQuad(quadPts + 2 * i, !verbsInContour && 0 == i); |
| 325 | } |
| 326 | } else { |
| 327 | SkConic dst[4]; |
| 328 | // We chop the conics to create tighter clipping to hide error |
| 329 | // that appears near max curvature of very thin conics. Thin |
| 330 | // hyperbolas with high weight still show error. |
| 331 | int conicCnt = chop_conic(pathPts, dst, iter.conicWeight()); |
| 332 | for (int i = 0; i < conicCnt; ++i) { |
| 333 | SkPoint devPts[4]; |
| 334 | SkPoint* chopPnts = dst[i].fPts; |
| 335 | m.mapPoints(devPts, chopPnts, 3); |
| 336 | bounds.setBounds(devPts, 3); |
| 337 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 338 | bounds.roundOut(&ibounds); |
| 339 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
| 340 | if (is_degen_quad_or_conic(devPts)) { |
| 341 | SkPoint* pts = lines->push_back_n(4); |
| 342 | pts[0] = devPts[0]; |
| 343 | pts[1] = devPts[1]; |
| 344 | pts[2] = devPts[1]; |
| 345 | pts[3] = devPts[2]; |
| 346 | if (verbsInContour == 0 && i == 0 && pts[0] == pts[1] && |
| 347 | pts[2] == pts[3]) { |
| 348 | seenZeroLengthVerb = true; |
| 349 | zeroVerbPt = pts[0]; |
| 350 | } |
| 351 | } else { |
| 352 | // when in perspective keep conics in src space |
| 353 | SkPoint* cPts = persp ? chopPnts : devPts; |
| 354 | SkPoint* pts = conics->push_back_n(3); |
| 355 | pts[0] = cPts[0]; |
| 356 | pts[1] = cPts[1]; |
| 357 | pts[2] = cPts[2]; |
| 358 | conicWeights->push_back() = dst[i].fW; |
Brian Osman | cf3dc29 | 2017-06-28 16:45:32 -0400 | [diff] [blame] | 359 | } |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | } |
Brian Osman | cf3dc29 | 2017-06-28 16:45:32 -0400 | [diff] [blame] | 363 | verbsInContour++; |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 364 | break; |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 365 | case SkPath::kMove_Verb: |
Brian Osman | cf3dc29 | 2017-06-28 16:45:32 -0400 | [diff] [blame] | 366 | // New contour (and last one was unclosed). If it was just a zero length drawing |
| 367 | // operation, and we're supposed to draw caps, then add a tiny line. |
| 368 | if (seenZeroLengthVerb && verbsInContour == 1 && capLength > 0) { |
| 369 | SkPoint* pts = lines->push_back_n(2); |
| 370 | pts[0] = SkPoint::Make(zeroVerbPt.fX - capLength, zeroVerbPt.fY); |
| 371 | pts[1] = SkPoint::Make(zeroVerbPt.fX + capLength, zeroVerbPt.fY); |
| 372 | } |
| 373 | verbsInContour = 0; |
| 374 | seenZeroLengthVerb = false; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 375 | break; |
Brian Salomon | 969a738 | 2018-05-09 13:28:44 -0400 | [diff] [blame] | 376 | case SkPath::kLine_Verb: { |
| 377 | SkPoint devPts[2]; |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 378 | m.mapPoints(devPts, pathPts, 2); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 379 | bounds.setBounds(devPts, 2); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 380 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 381 | bounds.roundOut(&ibounds); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 382 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 383 | SkPoint* pts = lines->push_back_n(2); |
| 384 | pts[0] = devPts[0]; |
| 385 | pts[1] = devPts[1]; |
Brian Osman | cf3dc29 | 2017-06-28 16:45:32 -0400 | [diff] [blame] | 386 | if (verbsInContour == 0 && pts[0] == pts[1]) { |
| 387 | seenZeroLengthVerb = true; |
| 388 | zeroVerbPt = pts[0]; |
| 389 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 390 | } |
Brian Osman | cf3dc29 | 2017-06-28 16:45:32 -0400 | [diff] [blame] | 391 | verbsInContour++; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 392 | break; |
Brian Salomon | 969a738 | 2018-05-09 13:28:44 -0400 | [diff] [blame] | 393 | } |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 394 | case SkPath::kQuad_Verb: { |
| 395 | SkPoint choppedPts[5]; |
| 396 | // Chopping the quad helps when the quad is either degenerate or nearly degenerate. |
| 397 | // When it is degenerate it allows the approximation with lines to work since the |
| 398 | // chop point (if there is one) will be at the parabola's vertex. In the nearly |
| 399 | // degenerate the QuadUVMatrix computed for the points is almost singular which |
| 400 | // can cause rendering artifacts. |
| 401 | int n = SkChopQuadAtMaxCurvature(pathPts, choppedPts); |
| 402 | for (int i = 0; i < n; ++i) { |
Brian Salomon | 969a738 | 2018-05-09 13:28:44 -0400 | [diff] [blame] | 403 | addSrcChoppedQuad(choppedPts + i * 2, !verbsInContour && 0 == i); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 404 | } |
Brian Osman | cf3dc29 | 2017-06-28 16:45:32 -0400 | [diff] [blame] | 405 | verbsInContour++; |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 406 | break; |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 407 | } |
Brian Salomon | 969a738 | 2018-05-09 13:28:44 -0400 | [diff] [blame] | 408 | case SkPath::kCubic_Verb: { |
| 409 | SkPoint devPts[4]; |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 410 | m.mapPoints(devPts, pathPts, 4); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 411 | bounds.setBounds(devPts, 4); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 412 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 413 | bounds.roundOut(&ibounds); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 414 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 415 | PREALLOC_PTARRAY(32) q; |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 416 | // We convert cubics to quadratics (for now). |
| 417 | // In perspective have to do conversion in src space. |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 418 | if (persp) { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 419 | SkScalar tolScale = |
bsalomon | 18fab30 | 2016-02-16 08:00:05 -0800 | [diff] [blame] | 420 | GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m, path.getBounds()); |
| 421 | GrPathUtils::convertCubicToQuads(pathPts, tolScale, &q); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 422 | } else { |
bsalomon | 18fab30 | 2016-02-16 08:00:05 -0800 | [diff] [blame] | 423 | GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, &q); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 424 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 425 | for (int i = 0; i < q.count(); i += 3) { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 426 | if (persp) { |
Brian Salomon | 969a738 | 2018-05-09 13:28:44 -0400 | [diff] [blame] | 427 | addSrcChoppedQuad(&q[i], !verbsInContour && 0 == i); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 428 | } else { |
Brian Salomon | 969a738 | 2018-05-09 13:28:44 -0400 | [diff] [blame] | 429 | addChoppedQuad(nullptr, &q[i], !verbsInContour && 0 == i); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | } |
Brian Osman | cf3dc29 | 2017-06-28 16:45:32 -0400 | [diff] [blame] | 433 | verbsInContour++; |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 434 | break; |
Brian Salomon | 969a738 | 2018-05-09 13:28:44 -0400 | [diff] [blame] | 435 | } |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 436 | case SkPath::kClose_Verb: |
Brian Osman | cf3dc29 | 2017-06-28 16:45:32 -0400 | [diff] [blame] | 437 | // Contour is closed, so we don't need to grow the starting line, unless it's |
| 438 | // *just* a zero length subpath. (SVG Spec 11.4, 'stroke'). |
| 439 | if (capLength > 0) { |
| 440 | if (seenZeroLengthVerb && verbsInContour == 1) { |
| 441 | SkPoint* pts = lines->push_back_n(2); |
| 442 | pts[0] = SkPoint::Make(zeroVerbPt.fX - capLength, zeroVerbPt.fY); |
| 443 | pts[1] = SkPoint::Make(zeroVerbPt.fX + capLength, zeroVerbPt.fY); |
| 444 | } else if (verbsInContour == 0) { |
| 445 | // Contour was (moveTo, close). Add a line. |
Brian Salomon | 969a738 | 2018-05-09 13:28:44 -0400 | [diff] [blame] | 446 | SkPoint devPts[2]; |
Brian Osman | cf3dc29 | 2017-06-28 16:45:32 -0400 | [diff] [blame] | 447 | m.mapPoints(devPts, pathPts, 1); |
| 448 | devPts[1] = devPts[0]; |
| 449 | bounds.setBounds(devPts, 2); |
| 450 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 451 | bounds.roundOut(&ibounds); |
| 452 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
| 453 | SkPoint* pts = lines->push_back_n(2); |
| 454 | pts[0] = SkPoint::Make(devPts[0].fX - capLength, devPts[0].fY); |
| 455 | pts[1] = SkPoint::Make(devPts[1].fX + capLength, devPts[1].fY); |
| 456 | } |
| 457 | } |
| 458 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 459 | break; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 460 | case SkPath::kDone_Verb: |
Brian Osman | cf3dc29 | 2017-06-28 16:45:32 -0400 | [diff] [blame] | 461 | if (seenZeroLengthVerb && verbsInContour == 1 && capLength > 0) { |
| 462 | // Path ended with a dangling (moveTo, line|quad|etc). If the final verb is |
| 463 | // degenerate, we need to draw a line. |
| 464 | SkPoint* pts = lines->push_back_n(2); |
| 465 | pts[0] = SkPoint::Make(zeroVerbPt.fX - capLength, zeroVerbPt.fY); |
| 466 | pts[1] = SkPoint::Make(zeroVerbPt.fX + capLength, zeroVerbPt.fY); |
| 467 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 468 | return totalQuadCount; |
| 469 | } |
| 470 | } |
| 471 | } |
| 472 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 473 | struct LineVertex { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 474 | SkPoint fPos; |
egdaniel | e27065a | 2014-11-06 08:00:48 -0800 | [diff] [blame] | 475 | float fCoverage; |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 476 | }; |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 477 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 478 | struct BezierVertex { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 479 | SkPoint fPos; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 480 | union { |
| 481 | struct { |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 482 | SkScalar fKLM[3]; |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 483 | } fConic; |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 484 | SkVector fQuadCoord; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 485 | struct { |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 486 | SkScalar fBogus[4]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 487 | }; |
| 488 | }; |
| 489 | }; |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 490 | |
Brian Salomon | 4dea72a | 2019-12-18 10:43:10 -0500 | [diff] [blame] | 491 | static_assert(sizeof(BezierVertex) == 3 * sizeof(SkPoint)); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 492 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 493 | static void intersect_lines(const SkPoint& ptA, const SkVector& normA, |
| 494 | const SkPoint& ptB, const SkVector& normB, |
| 495 | SkPoint* result) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 496 | |
| 497 | SkScalar lineAW = -normA.dot(ptA); |
| 498 | SkScalar lineBW = -normB.dot(ptB); |
| 499 | |
Mike Reed | 8be952a | 2017-02-13 20:44:33 -0500 | [diff] [blame] | 500 | SkScalar wInv = normA.fX * normB.fY - normA.fY * normB.fX; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 501 | wInv = SkScalarInvert(wInv); |
Jim Van Verth | 1499d9e | 2018-10-09 16:30:52 -0400 | [diff] [blame] | 502 | if (!SkScalarIsFinite(wInv)) { |
| 503 | // lines are parallel, pick the point in between |
| 504 | *result = (ptA + ptB)*SK_ScalarHalf; |
| 505 | *result += normA; |
| 506 | } else { |
| 507 | result->fX = normA.fY * lineBW - lineAW * normB.fY; |
| 508 | result->fX *= wInv; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 509 | |
Jim Van Verth | 1499d9e | 2018-10-09 16:30:52 -0400 | [diff] [blame] | 510 | result->fY = lineAW * normB.fX - normA.fX * lineBW; |
| 511 | result->fY *= wInv; |
| 512 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 513 | } |
| 514 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 515 | static void set_uv_quad(const SkPoint qpts[3], BezierVertex verts[kQuadNumVertices]) { |
egdaniel@google.com | 34b05ca | 2013-08-05 20:43:12 +0000 | [diff] [blame] | 516 | // this should be in the src space, not dev coords, when we have perspective |
| 517 | GrPathUtils::QuadUVMatrix DevToUV(qpts); |
Brian Osman | 568bec7 | 2018-12-26 16:48:25 -0500 | [diff] [blame] | 518 | DevToUV.apply(verts, kQuadNumVertices, sizeof(BezierVertex), sizeof(SkPoint)); |
egdaniel@google.com | 34b05ca | 2013-08-05 20:43:12 +0000 | [diff] [blame] | 519 | } |
| 520 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 521 | static void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice, |
| 522 | const SkMatrix* toSrc, BezierVertex verts[kQuadNumVertices]) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 523 | SkASSERT(!toDevice == !toSrc); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 524 | // original quad is specified by tri a,b,c |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 525 | SkPoint a = qpts[0]; |
| 526 | SkPoint b = qpts[1]; |
| 527 | SkPoint c = qpts[2]; |
| 528 | |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 529 | if (toDevice) { |
| 530 | toDevice->mapPoints(&a, 1); |
| 531 | toDevice->mapPoints(&b, 1); |
| 532 | toDevice->mapPoints(&c, 1); |
| 533 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 534 | // make a new poly where we replace a and c by a 1-pixel wide edges orthog |
| 535 | // to edges ab and bc: |
| 536 | // |
| 537 | // before | after |
| 538 | // | b0 |
| 539 | // b | |
| 540 | // | |
| 541 | // | a0 c0 |
| 542 | // a c | a1 c1 |
| 543 | // |
| 544 | // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c, |
| 545 | // respectively. |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 546 | BezierVertex& a0 = verts[0]; |
| 547 | BezierVertex& a1 = verts[1]; |
| 548 | BezierVertex& b0 = verts[2]; |
| 549 | BezierVertex& c0 = verts[3]; |
| 550 | BezierVertex& c1 = verts[4]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 551 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 552 | SkVector ab = b; |
| 553 | ab -= a; |
| 554 | SkVector ac = c; |
| 555 | ac -= a; |
| 556 | SkVector cb = b; |
| 557 | cb -= c; |
| 558 | |
Jim Van Verth | 1499d9e | 2018-10-09 16:30:52 -0400 | [diff] [blame] | 559 | // After the transform we might have a line, try to do something reasonable |
| 560 | if (toDevice && SkPointPriv::LengthSqd(ab) <= SK_ScalarNearlyZero*SK_ScalarNearlyZero) { |
| 561 | ab = cb; |
| 562 | } |
| 563 | if (toDevice && SkPointPriv::LengthSqd(cb) <= SK_ScalarNearlyZero*SK_ScalarNearlyZero) { |
| 564 | cb = ab; |
| 565 | } |
| 566 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 567 | // We should have already handled degenerates |
Jim Van Verth | 1499d9e | 2018-10-09 16:30:52 -0400 | [diff] [blame] | 568 | SkASSERT(toDevice || (ab.length() > 0 && cb.length() > 0)); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 569 | |
| 570 | ab.normalize(); |
Brian Salomon | 0235c64 | 2018-08-31 12:04:18 -0400 | [diff] [blame] | 571 | SkVector abN = SkPointPriv::MakeOrthog(ab, SkPointPriv::kLeft_Side); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 572 | if (abN.dot(ac) > 0) { |
| 573 | abN.negate(); |
| 574 | } |
| 575 | |
| 576 | cb.normalize(); |
Brian Salomon | 0235c64 | 2018-08-31 12:04:18 -0400 | [diff] [blame] | 577 | SkVector cbN = SkPointPriv::MakeOrthog(cb, SkPointPriv::kLeft_Side); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 578 | if (cbN.dot(ac) < 0) { |
| 579 | cbN.negate(); |
| 580 | } |
| 581 | |
| 582 | a0.fPos = a; |
| 583 | a0.fPos += abN; |
| 584 | a1.fPos = a; |
| 585 | a1.fPos -= abN; |
| 586 | |
Jim Van Verth | 1499d9e | 2018-10-09 16:30:52 -0400 | [diff] [blame] | 587 | if (toDevice && SkPointPriv::LengthSqd(ac) <= SK_ScalarNearlyZero*SK_ScalarNearlyZero) { |
| 588 | c = b; |
| 589 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 590 | c0.fPos = c; |
| 591 | c0.fPos += cbN; |
| 592 | c1.fPos = c; |
| 593 | c1.fPos -= cbN; |
| 594 | |
| 595 | intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos); |
| 596 | |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 597 | if (toSrc) { |
Brian Salomon | fa3783f | 2018-01-05 13:49:07 -0500 | [diff] [blame] | 598 | SkMatrixPriv::MapPointsWithStride(*toSrc, &verts[0].fPos, sizeof(BezierVertex), |
| 599 | kQuadNumVertices); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 600 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 601 | } |
| 602 | |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 603 | // Equations based off of Loop-Blinn Quadratic GPU Rendering |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 604 | // Input Parametric: |
| 605 | // P(t) = (P0*(1-t)^2 + 2*w*P1*t*(1-t) + P2*t^2) / (1-t)^2 + 2*w*t*(1-t) + t^2) |
| 606 | // Output Implicit: |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 607 | // f(x, y, w) = f(P) = K^2 - LM |
| 608 | // K = dot(k, P), L = dot(l, P), M = dot(m, P) |
commit-bot@chromium.org | 1394840 | 2013-08-20 17:55:43 +0000 | [diff] [blame] | 609 | // k, l, m are calculated in function GrPathUtils::getConicKLM |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 610 | static void set_conic_coeffs(const SkPoint p[3], BezierVertex verts[kQuadNumVertices], |
| 611 | const SkScalar weight) { |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 612 | SkMatrix klm; |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 613 | |
csmartdalton | cc26127 | 2017-03-23 13:38:45 -0600 | [diff] [blame] | 614 | GrPathUtils::getConicKLM(p, weight, &klm); |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 615 | |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 616 | for (int i = 0; i < kQuadNumVertices; ++i) { |
Cary Clark | e4442cb | 2017-10-18 11:46:18 -0400 | [diff] [blame] | 617 | const SkPoint3 pt3 = {verts[i].fPos.x(), verts[i].fPos.y(), 1.f}; |
| 618 | klm.mapHomogeneousPoints((SkPoint3* ) verts[i].fConic.fKLM, &pt3, 1); |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 619 | } |
| 620 | } |
| 621 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 622 | static void add_conics(const SkPoint p[3], |
| 623 | const SkScalar weight, |
| 624 | const SkMatrix* toDevice, |
| 625 | const SkMatrix* toSrc, |
| 626 | BezierVertex** vert) { |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 627 | bloat_quad(p, toDevice, toSrc, *vert); |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 628 | set_conic_coeffs(p, *vert, weight); |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 629 | *vert += kQuadNumVertices; |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 630 | } |
| 631 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 632 | static void add_quads(const SkPoint p[3], |
| 633 | int subdiv, |
| 634 | const SkMatrix* toDevice, |
| 635 | const SkMatrix* toSrc, |
| 636 | BezierVertex** vert) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 637 | SkASSERT(subdiv >= 0); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 638 | if (subdiv) { |
| 639 | SkPoint newP[5]; |
| 640 | SkChopQuadAtHalf(p, newP); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 641 | add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert); |
| 642 | add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 643 | } else { |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 644 | bloat_quad(p, toDevice, toSrc, *vert); |
egdaniel@google.com | 34b05ca | 2013-08-05 20:43:12 +0000 | [diff] [blame] | 645 | set_uv_quad(p, *vert); |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 646 | *vert += kQuadNumVertices; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 647 | } |
| 648 | } |
| 649 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 650 | static void add_line(const SkPoint p[2], |
| 651 | const SkMatrix* toSrc, |
| 652 | uint8_t coverage, |
| 653 | LineVertex** vert) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 654 | const SkPoint& a = p[0]; |
| 655 | const SkPoint& b = p[1]; |
| 656 | |
robertphillips@google.com | ada90da | 2013-09-18 22:14:49 +0000 | [diff] [blame] | 657 | SkVector ortho, vec = b; |
| 658 | vec -= a; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 659 | |
Cary Clark | df429f3 | 2017-11-08 11:44:31 -0500 | [diff] [blame] | 660 | SkScalar lengthSqd = SkPointPriv::LengthSqd(vec); |
Greg Daniel | 2e67dea | 2017-10-12 15:14:41 -0400 | [diff] [blame] | 661 | |
robertphillips@google.com | ada90da | 2013-09-18 22:14:49 +0000 | [diff] [blame] | 662 | if (vec.setLength(SK_ScalarHalf)) { |
| 663 | // Create a vector orthogonal to 'vec' and of unit length |
| 664 | ortho.fX = 2.0f * vec.fY; |
| 665 | ortho.fY = -2.0f * vec.fX; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 666 | |
egdaniel | e27065a | 2014-11-06 08:00:48 -0800 | [diff] [blame] | 667 | float floatCoverage = GrNormalizeByteToFloat(coverage); |
| 668 | |
Greg Daniel | 2e67dea | 2017-10-12 15:14:41 -0400 | [diff] [blame] | 669 | if (lengthSqd >= 1.0f) { |
| 670 | // Relative to points a and b: |
| 671 | // The inner vertices are inset half a pixel along the line a,b |
| 672 | (*vert)[0].fPos = a + vec; |
| 673 | (*vert)[0].fCoverage = floatCoverage; |
| 674 | (*vert)[1].fPos = b - vec; |
| 675 | (*vert)[1].fCoverage = floatCoverage; |
| 676 | } else { |
| 677 | // The inner vertices are inset a distance of length(a,b) from the outer edge of |
| 678 | // geometry. For the "a" inset this is the same as insetting from b by half a pixel. |
| 679 | // The coverage is then modulated by the length. This gives us the correct |
| 680 | // coverage for rects shorter than a pixel as they get translated subpixel amounts |
| 681 | // inside of a pixel. |
| 682 | SkScalar length = SkScalarSqrt(lengthSqd); |
| 683 | (*vert)[0].fPos = b - vec; |
| 684 | (*vert)[0].fCoverage = floatCoverage * length; |
| 685 | (*vert)[1].fPos = a + vec; |
| 686 | (*vert)[1].fCoverage = floatCoverage * length; |
| 687 | } |
| 688 | // Relative to points a and b: |
| 689 | // The outer vertices are outset half a pixel along the line a,b and then a whole pixel |
| 690 | // orthogonally. |
robertphillips@google.com | ada90da | 2013-09-18 22:14:49 +0000 | [diff] [blame] | 691 | (*vert)[2].fPos = a - vec + ortho; |
| 692 | (*vert)[2].fCoverage = 0; |
| 693 | (*vert)[3].fPos = b + vec + ortho; |
| 694 | (*vert)[3].fCoverage = 0; |
| 695 | (*vert)[4].fPos = a - vec - ortho; |
| 696 | (*vert)[4].fCoverage = 0; |
| 697 | (*vert)[5].fPos = b + vec - ortho; |
| 698 | (*vert)[5].fCoverage = 0; |
| 699 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 700 | if (toSrc) { |
Brian Salomon | fa3783f | 2018-01-05 13:49:07 -0500 | [diff] [blame] | 701 | SkMatrixPriv::MapPointsWithStride(*toSrc, &(*vert)->fPos, sizeof(LineVertex), |
| 702 | kLineSegNumVertices); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 703 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 704 | } else { |
| 705 | // just make it degenerate and likely offscreen |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 706 | for (int i = 0; i < kLineSegNumVertices; ++i) { |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 707 | (*vert)[i].fPos.set(SK_ScalarMax, SK_ScalarMax); |
| 708 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 709 | } |
| 710 | |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 711 | *vert += kLineSegNumVertices; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 712 | } |
| 713 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 714 | /////////////////////////////////////////////////////////////////////////////// |
| 715 | |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 716 | GrPathRenderer::CanDrawPath |
| 717 | GrAAHairLinePathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 718 | if (GrAAType::kCoverage != args.fAAType) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 719 | return CanDrawPath::kNo; |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 720 | } |
| 721 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 722 | if (!IsStrokeHairlineOrEquivalent(args.fShape->style(), *args.fViewMatrix, nullptr)) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 723 | return CanDrawPath::kNo; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 724 | } |
| 725 | |
| 726 | // We don't currently handle dashing in this class though perhaps we should. |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 727 | if (args.fShape->style().pathEffect()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 728 | return CanDrawPath::kNo; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 729 | } |
| 730 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 731 | if (SkPath::kLine_SegmentMask == args.fShape->segmentMask() || |
Eric Karl | 5c77975 | 2017-05-08 12:02:07 -0700 | [diff] [blame] | 732 | args.fCaps->shaderCaps()->shaderDerivativeSupport()) { |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 733 | return CanDrawPath::kYes; |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 734 | } |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 735 | |
Chris Dalton | 5ed4423 | 2017-09-07 13:22:46 -0600 | [diff] [blame] | 736 | return CanDrawPath::kNo; |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 737 | } |
| 738 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 739 | template <class VertexType> |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 740 | bool check_bounds(const SkMatrix& viewMatrix, const SkRect& devBounds, void* vertices, int vCount) |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 741 | { |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 742 | SkRect tolDevBounds = devBounds; |
commit-bot@chromium.org | b8bd6cb | 2013-09-03 14:56:17 +0000 | [diff] [blame] | 743 | // The bounds ought to be tight, but in perspective the below code runs the verts |
| 744 | // through the view matrix to get back to dev coords, which can introduce imprecision. |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 745 | if (viewMatrix.hasPerspective()) { |
commit-bot@chromium.org | b8bd6cb | 2013-09-03 14:56:17 +0000 | [diff] [blame] | 746 | tolDevBounds.outset(SK_Scalar1 / 1000, SK_Scalar1 / 1000); |
| 747 | } else { |
| 748 | // Non-persp matrices cause this path renderer to draw in device space. |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 749 | SkASSERT(viewMatrix.isIdentity()); |
commit-bot@chromium.org | b8bd6cb | 2013-09-03 14:56:17 +0000 | [diff] [blame] | 750 | } |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 751 | SkRect actualBounds; |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 752 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 753 | VertexType* verts = reinterpret_cast<VertexType*>(vertices); |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 754 | bool first = true; |
| 755 | for (int i = 0; i < vCount; ++i) { |
| 756 | SkPoint pos = verts[i].fPos; |
| 757 | // This is a hack to workaround the fact that we move some degenerate segments offscreen. |
| 758 | if (SK_ScalarMax == pos.fX) { |
| 759 | continue; |
| 760 | } |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 761 | viewMatrix.mapPoints(&pos, 1); |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 762 | if (first) { |
Mike Reed | 92b3335 | 2019-08-24 19:39:13 -0400 | [diff] [blame] | 763 | actualBounds.setLTRB(pos.fX, pos.fY, pos.fX, pos.fY); |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 764 | first = false; |
| 765 | } else { |
Mike Reed | 185ffe9 | 2018-01-08 17:09:54 -0500 | [diff] [blame] | 766 | SkRectPriv::GrowToInclude(&actualBounds, pos); |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | if (!first) { |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 770 | return tolDevBounds.contains(actualBounds); |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 771 | } |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 772 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 773 | return true; |
| 774 | } |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 775 | |
Brian Salomon | a531f25 | 2017-07-07 13:29:28 -0400 | [diff] [blame] | 776 | class AAHairlineOp final : public GrMeshDrawOp { |
| 777 | private: |
| 778 | using Helper = GrSimpleMeshDrawOpHelperWithStencil; |
| 779 | |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 780 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 781 | DEFINE_OP_CLASS_ID |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 782 | |
Robert Phillips | b97da53 | 2019-02-12 15:24:12 -0500 | [diff] [blame] | 783 | static std::unique_ptr<GrDrawOp> Make(GrRecordingContext* context, |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 784 | GrPaint&& paint, |
Brian Salomon | a531f25 | 2017-07-07 13:29:28 -0400 | [diff] [blame] | 785 | const SkMatrix& viewMatrix, |
| 786 | const SkPath& path, |
| 787 | const GrStyle& style, |
| 788 | const SkIRect& devClipBounds, |
| 789 | const GrUserStencilSettings* stencilSettings) { |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 790 | SkScalar hairlineCoverage; |
| 791 | uint8_t newCoverage = 0xff; |
| 792 | if (GrPathRenderer::IsStrokeHairlineOrEquivalent(style, viewMatrix, &hairlineCoverage)) { |
| 793 | newCoverage = SkScalarRoundToInt(hairlineCoverage * 0xff); |
| 794 | } |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 795 | |
Brian Osman | cf3dc29 | 2017-06-28 16:45:32 -0400 | [diff] [blame] | 796 | const SkStrokeRec& stroke = style.strokeRec(); |
| 797 | SkScalar capLength = SkPaint::kButt_Cap != stroke.getCap() ? hairlineCoverage * 0.5f : 0.0f; |
| 798 | |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 799 | return Helper::FactoryHelper<AAHairlineOp>(context, std::move(paint), newCoverage, |
| 800 | viewMatrix, path, |
Brian Salomon | a531f25 | 2017-07-07 13:29:28 -0400 | [diff] [blame] | 801 | devClipBounds, capLength, stencilSettings); |
| 802 | } |
| 803 | |
| 804 | AAHairlineOp(const Helper::MakeArgs& helperArgs, |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 805 | const SkPMColor4f& color, |
Brian Salomon | a531f25 | 2017-07-07 13:29:28 -0400 | [diff] [blame] | 806 | uint8_t coverage, |
| 807 | const SkMatrix& viewMatrix, |
| 808 | const SkPath& path, |
| 809 | SkIRect devClipBounds, |
| 810 | SkScalar capLength, |
| 811 | const GrUserStencilSettings* stencilSettings) |
| 812 | : INHERITED(ClassID()) |
| 813 | , fHelper(helperArgs, GrAAType::kCoverage, stencilSettings) |
| 814 | , fColor(color) |
| 815 | , fCoverage(coverage) { |
| 816 | fPaths.emplace_back(PathData{viewMatrix, path, devClipBounds, capLength}); |
| 817 | |
| 818 | this->setTransformedBounds(path.getBounds(), viewMatrix, HasAABloat::kYes, |
Greg Daniel | 5faf474 | 2019-10-01 15:14:44 -0400 | [diff] [blame] | 819 | IsHairline::kYes); |
bsalomon | f170309 | 2016-06-29 18:41:53 -0700 | [diff] [blame] | 820 | } |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 821 | |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 822 | const char* name() const override { return "AAHairlineOp"; } |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 823 | |
Chris Dalton | 1706cbf | 2019-05-21 19:35:29 -0600 | [diff] [blame] | 824 | void visitProxies(const VisitProxyFunc& func) const override { |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 825 | |
| 826 | bool visited = false; |
| 827 | for (int i = 0; i < 3; ++i) { |
| 828 | if (fProgramInfos[i]) { |
| 829 | fProgramInfos[i]->visitFPProxies(func); |
| 830 | visited = true; |
| 831 | } |
| 832 | } |
| 833 | |
| 834 | if (!visited) { |
| 835 | fHelper.visitProxies(func); |
| 836 | } |
Robert Phillips | b493eeb | 2017-09-13 13:10:52 -0400 | [diff] [blame] | 837 | } |
| 838 | |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 839 | #ifdef SK_DEBUG |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 840 | SkString dumpInfo() const override { |
| 841 | SkString string; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 842 | string.appendf("Color: 0x%08x Coverage: 0x%02x, Count: %d\n", fColor.toBytes_RGBA(), |
Brian Osman | 1be2b7c | 2018-10-29 16:07:15 -0400 | [diff] [blame] | 843 | fCoverage, fPaths.count()); |
Brian Salomon | a531f25 | 2017-07-07 13:29:28 -0400 | [diff] [blame] | 844 | string += INHERITED::dumpInfo(); |
| 845 | string += fHelper.dumpInfo(); |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 846 | return string; |
| 847 | } |
Brian Osman | 9a390ac | 2018-11-12 09:47:48 -0500 | [diff] [blame] | 848 | #endif |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 849 | |
Brian Salomon | a531f25 | 2017-07-07 13:29:28 -0400 | [diff] [blame] | 850 | FixedFunctionFlags fixedFunctionFlags() const override { return fHelper.fixedFunctionFlags(); } |
| 851 | |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 852 | GrProcessorSet::Analysis finalize( |
| 853 | const GrCaps& caps, const GrAppliedClip* clip, bool hasMixedSampledCoverage, |
| 854 | GrClampType clampType) override { |
Brian Osman | 8fa7ab4 | 2019-03-18 10:22:42 -0400 | [diff] [blame] | 855 | // This Op uses uniform (not vertex) color, so doesn't need to track wide color. |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 856 | return fHelper.finalizeProcessors(caps, clip, hasMixedSampledCoverage, clampType, |
Brian Osman | 8fa7ab4 | 2019-03-18 10:22:42 -0400 | [diff] [blame] | 857 | GrProcessorAnalysisCoverage::kSingleChannel, &fColor, |
| 858 | nullptr); |
Brian Salomon | a531f25 | 2017-07-07 13:29:28 -0400 | [diff] [blame] | 859 | } |
| 860 | |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 861 | enum Program : uint8_t { |
| 862 | kNone_Program = 0x0, |
| 863 | kLine_Program = 0x1, |
| 864 | kQuad_Program = 0x2, |
| 865 | kConic_Program = 0x4, |
| 866 | }; |
| 867 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 868 | private: |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 869 | void makeLineProgramInfo(const GrCaps&, SkArenaAlloc*, const GrPipeline*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 870 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 871 | const SkMatrix* geometryProcessorViewM, |
| 872 | const SkMatrix* geometryProcessorLocalM); |
| 873 | void makeQuadProgramInfo(const GrCaps&, SkArenaAlloc*, const GrPipeline*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 874 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 875 | const SkMatrix* geometryProcessorViewM, |
| 876 | const SkMatrix* geometryProcessorLocalM); |
| 877 | void makeConicProgramInfo(const GrCaps&, SkArenaAlloc*, const GrPipeline*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 878 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 879 | const SkMatrix* geometryProcessorViewM, |
| 880 | const SkMatrix* geometryProcessorLocalM); |
Robert Phillips | 1b3c267 | 2019-12-04 14:34:48 -0500 | [diff] [blame] | 881 | |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 882 | GrProgramInfo* programInfo() override { |
| 883 | // This Op has 3 programInfos and implements its own onPrePrepareDraws so this entry point |
| 884 | // should really never be called. |
| 885 | SkASSERT(0); |
| 886 | return nullptr; |
| 887 | } |
| 888 | |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 889 | Program predictPrograms(const GrCaps*) const; |
| 890 | |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 891 | void onCreateProgramInfo(const GrCaps*, |
| 892 | SkArenaAlloc*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 893 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 894 | GrAppliedClip&&, |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 895 | const GrXferProcessor::DstProxyView&) override; |
Robert Phillips | 4133dc4 | 2020-03-11 15:55:55 -0400 | [diff] [blame] | 896 | |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 897 | void onPrePrepareDraws(GrRecordingContext*, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 898 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 899 | GrAppliedClip*, |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 900 | const GrXferProcessor::DstProxyView&) override; |
Robert Phillips | 2669a7b | 2020-03-12 12:07:19 -0400 | [diff] [blame] | 901 | |
Brian Salomon | 91326c3 | 2017-08-09 16:02:19 -0400 | [diff] [blame] | 902 | void onPrepareDraws(Target*) override; |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 903 | void onExecute(GrOpFlushState*, const SkRect& chainBounds) override; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 904 | |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 905 | typedef SkTArray<SkPoint, true> PtArray; |
| 906 | typedef SkTArray<int, true> IntArray; |
| 907 | typedef SkTArray<float, true> FloatArray; |
| 908 | |
Michael Ludwig | 28b0c5d | 2019-12-19 14:51:00 -0500 | [diff] [blame] | 909 | CombineResult onCombineIfPossible(GrOp* t, GrRecordingContext::Arenas*, |
| 910 | const GrCaps& caps) override { |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 911 | AAHairlineOp* that = t->cast<AAHairlineOp>(); |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 912 | |
Brian Salomon | a531f25 | 2017-07-07 13:29:28 -0400 | [diff] [blame] | 913 | if (!fHelper.isCompatible(that->fHelper, caps, this->bounds(), that->bounds())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 914 | return CombineResult::kCannotCombine; |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 915 | } |
| 916 | |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 917 | if (this->viewMatrix().hasPerspective() != that->viewMatrix().hasPerspective()) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 918 | return CombineResult::kCannotCombine; |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | // We go to identity if we don't have perspective |
| 922 | if (this->viewMatrix().hasPerspective() && |
Mike Reed | 2c38315 | 2019-12-18 16:47:47 -0500 | [diff] [blame] | 923 | !SkMatrixPriv::CheapEqual(this->viewMatrix(), that->viewMatrix())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 924 | return CombineResult::kCannotCombine; |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 925 | } |
| 926 | |
Brian Salomon | 53e4c3c | 2016-12-21 11:38:53 -0500 | [diff] [blame] | 927 | // TODO we can actually combine hairlines if they are the same color in a kind of bulk |
| 928 | // method but we haven't implemented this yet |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 929 | // TODO investigate going to vertex color and coverage? |
| 930 | if (this->coverage() != that->coverage()) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 931 | return CombineResult::kCannotCombine; |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 932 | } |
| 933 | |
| 934 | if (this->color() != that->color()) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 935 | return CombineResult::kCannotCombine; |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 936 | } |
| 937 | |
Mike Reed | 2c38315 | 2019-12-18 16:47:47 -0500 | [diff] [blame] | 938 | if (fHelper.usesLocalCoords() && !SkMatrixPriv::CheapEqual(this->viewMatrix(), |
| 939 | that->viewMatrix())) { |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 940 | return CombineResult::kCannotCombine; |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 941 | } |
| 942 | |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 943 | fPaths.push_back_n(that->fPaths.count(), that->fPaths.begin()); |
Brian Salomon | 7eae3e0 | 2018-08-07 14:02:38 +0000 | [diff] [blame] | 944 | return CombineResult::kMerged; |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 945 | } |
| 946 | |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 947 | const SkPMColor4f& color() const { return fColor; } |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 948 | uint8_t coverage() const { return fCoverage; } |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 949 | const SkMatrix& viewMatrix() const { return fPaths[0].fViewMatrix; } |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 950 | |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 951 | struct PathData { |
bsalomon | f170309 | 2016-06-29 18:41:53 -0700 | [diff] [blame] | 952 | SkMatrix fViewMatrix; |
| 953 | SkPath fPath; |
| 954 | SkIRect fDevClipBounds; |
Brian Osman | cf3dc29 | 2017-06-28 16:45:32 -0400 | [diff] [blame] | 955 | SkScalar fCapLength; |
bsalomon | f170309 | 2016-06-29 18:41:53 -0700 | [diff] [blame] | 956 | }; |
| 957 | |
Brian Salomon | a531f25 | 2017-07-07 13:29:28 -0400 | [diff] [blame] | 958 | SkSTArray<1, PathData, true> fPaths; |
| 959 | Helper fHelper; |
Brian Osman | cf86085 | 2018-10-31 14:04:39 -0400 | [diff] [blame] | 960 | SkPMColor4f fColor; |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 961 | uint8_t fCoverage; |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 962 | |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 963 | Program fCharacterization = kNone_Program; // holds a mask of required programs |
| 964 | GrSimpleMesh* fMeshes[3] = { nullptr }; |
| 965 | GrProgramInfo* fProgramInfos[3] = { nullptr }; |
| 966 | |
Brian Salomon | a531f25 | 2017-07-07 13:29:28 -0400 | [diff] [blame] | 967 | typedef GrMeshDrawOp INHERITED; |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 968 | }; |
| 969 | |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 970 | GR_MAKE_BITFIELD_OPS(AAHairlineOp::Program) |
Brian Salomon | a531f25 | 2017-07-07 13:29:28 -0400 | [diff] [blame] | 971 | |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 972 | void AAHairlineOp::makeLineProgramInfo(const GrCaps& caps, SkArenaAlloc* arena, |
| 973 | const GrPipeline* pipeline, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 974 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 975 | const SkMatrix* geometryProcessorViewM, |
| 976 | const SkMatrix* geometryProcessorLocalM) { |
| 977 | if (fProgramInfos[0]) { |
| 978 | return; |
| 979 | } |
Robert Phillips | 1b3c267 | 2019-12-04 14:34:48 -0500 | [diff] [blame] | 980 | |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 981 | GrGeometryProcessor* lineGP; |
| 982 | { |
| 983 | using namespace GrDefaultGeoProcFactory; |
Robert Phillips | 1b3c267 | 2019-12-04 14:34:48 -0500 | [diff] [blame] | 984 | |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 985 | Color color(this->color()); |
| 986 | LocalCoords localCoords(fHelper.usesLocalCoords() ? LocalCoords::kUsePosition_Type |
| 987 | : LocalCoords::kUnused_Type); |
| 988 | localCoords.fMatrix = geometryProcessorLocalM; |
Robert Phillips | 1b3c267 | 2019-12-04 14:34:48 -0500 | [diff] [blame] | 989 | |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 990 | lineGP = GrDefaultGeoProcFactory::Make(arena, |
| 991 | color, |
| 992 | Coverage::kAttribute_Type, |
| 993 | localCoords, |
| 994 | *geometryProcessorViewM); |
| 995 | SkASSERT(sizeof(LineVertex) == lineGP->vertexStride()); |
| 996 | } |
| 997 | |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 998 | fProgramInfos[0] = GrSimpleMeshDrawOpHelper::CreateProgramInfo(arena, pipeline, writeView, lineGP, |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 999 | GrPrimitiveType::kTriangles); |
Robert Phillips | 1b3c267 | 2019-12-04 14:34:48 -0500 | [diff] [blame] | 1000 | } |
| 1001 | |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1002 | void AAHairlineOp::makeQuadProgramInfo(const GrCaps& caps, SkArenaAlloc* arena, |
| 1003 | const GrPipeline* pipeline, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 1004 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1005 | const SkMatrix* geometryProcessorViewM, |
| 1006 | const SkMatrix* geometryProcessorLocalM) { |
| 1007 | if (fProgramInfos[1]) { |
| 1008 | return; |
| 1009 | } |
| 1010 | |
Robert Phillips | 1b3c267 | 2019-12-04 14:34:48 -0500 | [diff] [blame] | 1011 | GrGeometryProcessor* quadGP = GrQuadEffect::Make(arena, |
| 1012 | this->color(), |
| 1013 | *geometryProcessorViewM, |
| 1014 | GrClipEdgeType::kHairlineAA, |
| 1015 | caps, |
| 1016 | *geometryProcessorLocalM, |
| 1017 | fHelper.usesLocalCoords(), |
| 1018 | this->coverage()); |
| 1019 | SkASSERT(sizeof(BezierVertex) == quadGP->vertexStride()); |
| 1020 | |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 1021 | fProgramInfos[1] = GrSimpleMeshDrawOpHelper::CreateProgramInfo(arena, pipeline, writeView, quadGP, |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1022 | GrPrimitiveType::kTriangles); |
Robert Phillips | 1b3c267 | 2019-12-04 14:34:48 -0500 | [diff] [blame] | 1023 | } |
| 1024 | |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1025 | void AAHairlineOp::makeConicProgramInfo(const GrCaps& caps, SkArenaAlloc* arena, |
| 1026 | const GrPipeline* pipeline, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 1027 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1028 | const SkMatrix* geometryProcessorViewM, |
| 1029 | const SkMatrix* geometryProcessorLocalM) { |
| 1030 | if (fProgramInfos[2]) { |
| 1031 | return; |
| 1032 | } |
| 1033 | |
Robert Phillips | 1b3c267 | 2019-12-04 14:34:48 -0500 | [diff] [blame] | 1034 | GrGeometryProcessor* conicGP = GrConicEffect::Make(arena, |
| 1035 | this->color(), |
| 1036 | *geometryProcessorViewM, |
| 1037 | GrClipEdgeType::kHairlineAA, |
| 1038 | caps, |
| 1039 | *geometryProcessorLocalM, |
| 1040 | fHelper.usesLocalCoords(), |
| 1041 | this->coverage()); |
| 1042 | SkASSERT(sizeof(BezierVertex) == conicGP->vertexStride()); |
| 1043 | |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 1044 | fProgramInfos[2] = GrSimpleMeshDrawOpHelper::CreateProgramInfo(arena, pipeline, writeView, conicGP, |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1045 | GrPrimitiveType::kTriangles); |
Robert Phillips | 1b3c267 | 2019-12-04 14:34:48 -0500 | [diff] [blame] | 1046 | } |
| 1047 | |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1048 | AAHairlineOp::Program AAHairlineOp::predictPrograms(const GrCaps* caps) const { |
| 1049 | bool convertConicsToQuads = !caps->shaderCaps()->floatIs32Bits(); |
| 1050 | |
| 1051 | // When predicting the programs we always include the lineProgram bc it is used as a fallback |
| 1052 | // for quads and conics. In non-DDL mode there are cases where it sometimes isn't needed for a |
| 1053 | // given path. |
| 1054 | Program neededPrograms = kLine_Program; |
| 1055 | |
| 1056 | for (int i = 0; i < fPaths.count(); i++) { |
| 1057 | uint32_t mask = fPaths[i].fPath.getSegmentMasks(); |
| 1058 | |
| 1059 | if (mask & (SkPath::kQuad_SegmentMask | SkPath::kCubic_SegmentMask)) { |
| 1060 | neededPrograms |= kQuad_Program; |
| 1061 | } |
| 1062 | if (mask & SkPath::kConic_SegmentMask) { |
| 1063 | if (convertConicsToQuads) { |
| 1064 | neededPrograms |= kQuad_Program; |
| 1065 | } else { |
| 1066 | neededPrograms |= kConic_Program; |
| 1067 | } |
| 1068 | } |
| 1069 | } |
| 1070 | |
| 1071 | return neededPrograms; |
| 1072 | } |
| 1073 | |
| 1074 | void AAHairlineOp::onCreateProgramInfo(const GrCaps* caps, |
| 1075 | SkArenaAlloc* arena, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 1076 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1077 | GrAppliedClip&& appliedClip, |
| 1078 | const GrXferProcessor::DstProxyView& dstProxyView) { |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 1079 | // Setup the viewmatrix and localmatrix for the GrGeometryProcessor. |
| 1080 | SkMatrix invert; |
| 1081 | if (!this->viewMatrix().invert(&invert)) { |
| 1082 | return; |
| 1083 | } |
| 1084 | |
| 1085 | // we will transform to identity space if the viewmatrix does not have perspective |
| 1086 | bool hasPerspective = this->viewMatrix().hasPerspective(); |
| 1087 | const SkMatrix* geometryProcessorViewM = &SkMatrix::I(); |
| 1088 | const SkMatrix* geometryProcessorLocalM = &invert; |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 1089 | if (hasPerspective) { |
| 1090 | geometryProcessorViewM = &this->viewMatrix(); |
| 1091 | geometryProcessorLocalM = &SkMatrix::I(); |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1092 | } |
| 1093 | |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 1094 | auto pipeline = fHelper.createPipelineWithStencil(caps, arena, writeView->swizzle(), |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1095 | std::move(appliedClip), dstProxyView); |
| 1096 | |
| 1097 | if (fCharacterization & kLine_Program) { |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 1098 | this->makeLineProgramInfo(*caps, arena, pipeline, writeView, |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1099 | geometryProcessorViewM, geometryProcessorLocalM); |
| 1100 | } |
| 1101 | if (fCharacterization & kQuad_Program) { |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 1102 | this->makeQuadProgramInfo(*caps, arena, pipeline, writeView, |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1103 | geometryProcessorViewM, geometryProcessorLocalM); |
| 1104 | } |
| 1105 | if (fCharacterization & kConic_Program) { |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 1106 | this->makeConicProgramInfo(*caps, arena, pipeline, writeView, |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1107 | geometryProcessorViewM, geometryProcessorLocalM); |
| 1108 | |
| 1109 | } |
| 1110 | } |
| 1111 | |
| 1112 | void AAHairlineOp::onPrePrepareDraws(GrRecordingContext* context, |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 1113 | const GrSurfaceProxyView* writeView, |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1114 | GrAppliedClip* clip, |
| 1115 | const GrXferProcessor::DstProxyView& dstProxyView) { |
| 1116 | SkArenaAlloc* arena = context->priv().recordTimeAllocator(); |
| 1117 | const GrCaps* caps = context->priv().caps(); |
| 1118 | |
| 1119 | // This is equivalent to a GrOpFlushState::detachAppliedClip |
| 1120 | GrAppliedClip appliedClip = clip ? std::move(*clip) : GrAppliedClip(); |
| 1121 | |
| 1122 | // Conservatively predict which programs will be required |
| 1123 | fCharacterization = this->predictPrograms(caps); |
| 1124 | |
Brian Salomon | 8afde5f | 2020-04-01 16:22:00 -0400 | [diff] [blame^] | 1125 | this->createProgramInfo(caps, arena, writeView, std::move(appliedClip), dstProxyView); |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1126 | |
| 1127 | context->priv().recordProgramInfo(fProgramInfos[0]); |
| 1128 | context->priv().recordProgramInfo(fProgramInfos[1]); |
| 1129 | context->priv().recordProgramInfo(fProgramInfos[2]); |
| 1130 | } |
| 1131 | |
| 1132 | void AAHairlineOp::onPrepareDraws(Target* target) { |
| 1133 | // Setup the viewmatrix and localmatrix for the GrGeometryProcessor. |
| 1134 | SkMatrix invert; |
| 1135 | if (!this->viewMatrix().invert(&invert)) { |
| 1136 | return; |
| 1137 | } |
| 1138 | |
| 1139 | // we will transform to identity space if the viewmatrix does not have perspective |
| 1140 | const SkMatrix* toDevice = nullptr; |
| 1141 | const SkMatrix* toSrc = nullptr; |
| 1142 | if (this->viewMatrix().hasPerspective()) { |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 1143 | toDevice = &this->viewMatrix(); |
| 1144 | toSrc = &invert; |
| 1145 | } |
| 1146 | |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1147 | SkDEBUGCODE(Program predictedPrograms = this->predictPrograms(&target->caps())); |
| 1148 | Program actualPrograms = kNone_Program; |
| 1149 | |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 1150 | // This is hand inlined for maximum performance. |
| 1151 | PREALLOC_PTARRAY(128) lines; |
| 1152 | PREALLOC_PTARRAY(128) quads; |
| 1153 | PREALLOC_PTARRAY(128) conics; |
| 1154 | IntArray qSubdivs; |
| 1155 | FloatArray cWeights; |
joshualitt | 351ba1b | 2015-02-16 08:33:19 -0800 | [diff] [blame] | 1156 | int quadCount = 0; |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 1157 | |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 1158 | int instanceCount = fPaths.count(); |
Brian Salomon | 969a738 | 2018-05-09 13:28:44 -0400 | [diff] [blame] | 1159 | bool convertConicsToQuads = !target->caps().shaderCaps()->floatIs32Bits(); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 1160 | for (int i = 0; i < instanceCount; i++) { |
Brian Salomon | d0a0a65 | 2016-12-15 15:25:22 -0500 | [diff] [blame] | 1161 | const PathData& args = fPaths[i]; |
joshualitt | 351ba1b | 2015-02-16 08:33:19 -0800 | [diff] [blame] | 1162 | quadCount += gather_lines_and_quads(args.fPath, args.fViewMatrix, args.fDevClipBounds, |
Brian Salomon | 969a738 | 2018-05-09 13:28:44 -0400 | [diff] [blame] | 1163 | args.fCapLength, convertConicsToQuads, &lines, &quads, |
| 1164 | &conics, &qSubdivs, &cWeights); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 1165 | } |
| 1166 | |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 1167 | int lineCount = lines.count() / 2; |
| 1168 | int conicCount = conics.count() / 3; |
Brian Salomon | 296de50 | 2018-03-13 12:26:55 -0400 | [diff] [blame] | 1169 | int quadAndConicCount = conicCount + quadCount; |
| 1170 | |
| 1171 | static constexpr int kMaxLines = SK_MaxS32 / kLineSegNumVertices; |
| 1172 | static constexpr int kMaxQuadsAndConics = SK_MaxS32 / kQuadNumVertices; |
| 1173 | if (lineCount > kMaxLines || quadAndConicCount > kMaxQuadsAndConics) { |
| 1174 | return; |
| 1175 | } |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 1176 | |
| 1177 | // do lines first |
| 1178 | if (lineCount) { |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1179 | SkASSERT(predictedPrograms & kLine_Program); |
| 1180 | actualPrograms |= kLine_Program; |
| 1181 | |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 1182 | sk_sp<const GrBuffer> linesIndexBuffer = get_lines_index_buffer(target->resourceProvider()); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 1183 | |
Robert Phillips | e94cdd2 | 2019-11-04 14:15:58 -0500 | [diff] [blame] | 1184 | GrMeshDrawOp::PatternHelper helper(target, GrPrimitiveType::kTriangles, sizeof(LineVertex), |
| 1185 | std::move(linesIndexBuffer), kLineSegNumVertices, |
| 1186 | kIdxsPerLineSeg, lineCount, kLineSegsNumInIdxBuffer); |
| 1187 | |
| 1188 | LineVertex* verts = reinterpret_cast<LineVertex*>(helper.vertices()); |
| 1189 | if (!verts) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 1190 | SkDebugf("Could not allocate vertices\n"); |
| 1191 | return; |
| 1192 | } |
| 1193 | |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 1194 | for (int i = 0; i < lineCount; ++i) { |
| 1195 | add_line(&lines[2*i], toSrc, this->coverage(), &verts); |
| 1196 | } |
| 1197 | |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1198 | fMeshes[0] = helper.mesh(); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 1199 | } |
| 1200 | |
| 1201 | if (quadCount || conicCount) { |
Brian Salomon | 12d2264 | 2019-01-29 14:38:50 -0500 | [diff] [blame] | 1202 | sk_sp<const GrBuffer> vertexBuffer; |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 1203 | int firstVertex; |
| 1204 | |
Brian Salomon | d28a79d | 2017-10-16 13:01:07 -0400 | [diff] [blame] | 1205 | sk_sp<const GrBuffer> quadsIndexBuffer = get_quads_index_buffer(target->resourceProvider()); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 1206 | |
Brian Salomon | 296de50 | 2018-03-13 12:26:55 -0400 | [diff] [blame] | 1207 | int vertexCount = kQuadNumVertices * quadAndConicCount; |
Brian Salomon | 92be2f7 | 2018-06-19 14:33:47 -0400 | [diff] [blame] | 1208 | void* vertices = target->makeVertexSpace(sizeof(BezierVertex), vertexCount, &vertexBuffer, |
| 1209 | &firstVertex); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 1210 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 1211 | if (!vertices || !quadsIndexBuffer) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 1212 | SkDebugf("Could not allocate vertices\n"); |
| 1213 | return; |
| 1214 | } |
| 1215 | |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 1216 | // Setup vertices |
robertphillips | 44c3128 | 2015-09-03 12:58:48 -0700 | [diff] [blame] | 1217 | BezierVertex* bezVerts = reinterpret_cast<BezierVertex*>(vertices); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 1218 | |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 1219 | int unsubdivQuadCnt = quads.count() / 3; |
| 1220 | for (int i = 0; i < unsubdivQuadCnt; ++i) { |
| 1221 | SkASSERT(qSubdivs[i] >= 0); |
robertphillips | 44c3128 | 2015-09-03 12:58:48 -0700 | [diff] [blame] | 1222 | add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &bezVerts); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 1223 | } |
| 1224 | |
| 1225 | // Start Conics |
| 1226 | for (int i = 0; i < conicCount; ++i) { |
robertphillips | 44c3128 | 2015-09-03 12:58:48 -0700 | [diff] [blame] | 1227 | add_conics(&conics[3*i], cWeights[i], toDevice, toSrc, &bezVerts); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 1228 | } |
| 1229 | |
| 1230 | if (quadCount > 0) { |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1231 | SkASSERT(predictedPrograms & kQuad_Program); |
| 1232 | actualPrograms |= kQuad_Program; |
Robert Phillips | 1b3c267 | 2019-12-04 14:34:48 -0500 | [diff] [blame] | 1233 | |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1234 | fMeshes[1] = target->allocMesh(); |
| 1235 | fMeshes[1]->setIndexedPatterned(quadsIndexBuffer, kIdxsPerQuad, quadCount, |
| 1236 | kQuadsNumInIdxBuffer, vertexBuffer, kQuadNumVertices, |
| 1237 | firstVertex); |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 1238 | firstVertex += quadCount * kQuadNumVertices; |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 1239 | } |
| 1240 | |
| 1241 | if (conicCount > 0) { |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1242 | SkASSERT(predictedPrograms & kConic_Program); |
| 1243 | actualPrograms |= kConic_Program; |
Robert Phillips | 1b3c267 | 2019-12-04 14:34:48 -0500 | [diff] [blame] | 1244 | |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1245 | fMeshes[2] = target->allocMesh(); |
| 1246 | fMeshes[2]->setIndexedPatterned(std::move(quadsIndexBuffer), kIdxsPerQuad, conicCount, |
| 1247 | kQuadsNumInIdxBuffer, std::move(vertexBuffer), |
| 1248 | kQuadNumVertices, firstVertex); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 1249 | } |
| 1250 | } |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1251 | |
| 1252 | // In DDL mode this will replace the predicted program requirements with the actual ones. |
| 1253 | // However, we will already have surfaced the predicted programs to the DDL. |
| 1254 | fCharacterization = actualPrograms; |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 1255 | } |
| 1256 | |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 1257 | void AAHairlineOp::onExecute(GrOpFlushState* flushState, const SkRect& chainBounds) { |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1258 | this->createProgramInfo(flushState); |
Robert Phillips | 3968fcb | 2019-12-05 16:40:31 -0500 | [diff] [blame] | 1259 | |
Robert Phillips | 4f93c57 | 2020-03-18 08:13:53 -0400 | [diff] [blame] | 1260 | for (int i = 0; i < 3; ++i) { |
| 1261 | if (fProgramInfos[i] && fMeshes[i]) { |
| 1262 | flushState->bindPipelineAndScissorClip(*fProgramInfos[i], chainBounds); |
| 1263 | flushState->bindTextures(fProgramInfos[i]->primProc(), nullptr, |
| 1264 | fProgramInfos[i]->pipeline()); |
| 1265 | flushState->drawMesh(*fMeshes[i]); |
| 1266 | } |
| 1267 | } |
Chris Dalton | 07cdcfc9 | 2019-02-26 11:13:22 -0700 | [diff] [blame] | 1268 | } |
| 1269 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 1270 | bool GrAAHairLinePathRenderer::onDrawPath(const DrawPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 1271 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 1272 | "GrAAHairlinePathRenderer::onDrawPath"); |
Chris Dalton | 6ce447a | 2019-06-23 18:07:38 -0600 | [diff] [blame] | 1273 | SkASSERT(args.fRenderTargetContext->numSamples() <= 1); |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 1274 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 1275 | SkIRect devClipBounds; |
Robert Phillips | 784b7bf | 2016-12-09 13:35:02 -0500 | [diff] [blame] | 1276 | args.fClip->getConservativeBounds(args.fRenderTargetContext->width(), |
| 1277 | args.fRenderTargetContext->height(), |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 1278 | &devClipBounds); |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 1279 | SkPath path; |
| 1280 | args.fShape->asPath(&path); |
Brian Salomon | a531f25 | 2017-07-07 13:29:28 -0400 | [diff] [blame] | 1281 | std::unique_ptr<GrDrawOp> op = |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 1282 | AAHairlineOp::Make(args.fContext, std::move(args.fPaint), *args.fViewMatrix, path, |
Brian Salomon | a531f25 | 2017-07-07 13:29:28 -0400 | [diff] [blame] | 1283 | args.fShape->style(), devClipBounds, args.fUserStencilSettings); |
| 1284 | args.fRenderTargetContext->addDrawOp(*args.fClip, std::move(op)); |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 1285 | return true; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1286 | } |
joshualitt | 40ded32 | 2015-05-02 07:07:17 -0700 | [diff] [blame] | 1287 | |
| 1288 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 1289 | |
Hal Canary | 6f6961e | 2017-01-31 13:50:44 -0500 | [diff] [blame] | 1290 | #if GR_TEST_UTILS |
joshualitt | 40ded32 | 2015-05-02 07:07:17 -0700 | [diff] [blame] | 1291 | |
Brian Salomon | a531f25 | 2017-07-07 13:29:28 -0400 | [diff] [blame] | 1292 | GR_DRAW_OP_TEST_DEFINE(AAHairlineOp) { |
joshualitt | 40ded32 | 2015-05-02 07:07:17 -0700 | [diff] [blame] | 1293 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
joshualitt | 40ded32 | 2015-05-02 07:07:17 -0700 | [diff] [blame] | 1294 | SkPath path = GrTest::TestPath(random); |
| 1295 | SkIRect devClipBounds; |
| 1296 | devClipBounds.setEmpty(); |
Robert Phillips | 7c525e6 | 2018-06-12 10:11:12 -0400 | [diff] [blame] | 1297 | return AAHairlineOp::Make(context, std::move(paint), viewMatrix, path, |
| 1298 | GrStyle::SimpleHairline(), devClipBounds, |
| 1299 | GrGetRandomStencil(random, context)); |
joshualitt | 40ded32 | 2015-05-02 07:07:17 -0700 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | #endif |