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 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 8 | #include "GrAAHairLinePathRenderer.h" |
| 9 | |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 10 | #include "GrBatchFlushState.h" |
joshualitt | 40ded32 | 2015-05-02 07:07:17 -0700 | [diff] [blame] | 11 | #include "GrBatchTest.h" |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 12 | #include "GrBuffer.h" |
bsalomon | eb1cb5c | 2015-05-22 08:01:09 -0700 | [diff] [blame] | 13 | #include "GrCaps.h" |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 14 | #include "GrContext.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 15 | #include "GrDefaultGeoProcFactory.h" |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 16 | #include "GrPathUtils.h" |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 17 | #include "GrPipelineBuilder.h" |
joshualitt | 5478d42 | 2014-11-14 16:00:38 -0800 | [diff] [blame] | 18 | #include "GrProcessor.h" |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 19 | #include "GrResourceProvider.h" |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 20 | #include "SkGeometry.h" |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 21 | #include "SkStroke.h" |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 22 | #include "SkTemplates.h" |
| 23 | |
bsalomon | 16b9913 | 2015-08-13 14:55:50 -0700 | [diff] [blame] | 24 | #include "batches/GrVertexBatch.h" |
joshualitt | 7441782 | 2015-08-07 11:42:16 -0700 | [diff] [blame] | 25 | |
commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 26 | #include "effects/GrBezierEffect.h" |
bsalomon@google.com | 4647f90 | 2013-03-26 14:45:27 +0000 | [diff] [blame] | 27 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 28 | #define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true> |
| 29 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 30 | // quadratics are rendered as 5-sided polys in order to bound the |
| 31 | // 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] | 32 | // bloat_quad. Quadratics and conics share an index buffer |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 33 | |
robertphillips@google.com | ada90da | 2013-09-18 22:14:49 +0000 | [diff] [blame] | 34 | // lines are rendered as: |
| 35 | // *______________* |
| 36 | // |\ -_______ /| |
| 37 | // | \ \ / | |
| 38 | // | *--------* | |
| 39 | // | / ______/ \ | |
| 40 | // */_-__________\* |
| 41 | // For: 6 vertices and 18 indices (for 6 triangles) |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 42 | |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 43 | // Each quadratic is rendered as a five sided polygon. This poly bounds |
| 44 | // the quadratic's bounding triangle but has been expanded so that the |
| 45 | // 1-pixel wide area around the curve is inside the poly. |
| 46 | // If a,b,c are the original control points then the poly a0,b0,c0,c1,a1 |
| 47 | // that is rendered would look like this: |
| 48 | // b0 |
| 49 | // b |
| 50 | // |
| 51 | // a0 c0 |
| 52 | // a c |
| 53 | // a1 c1 |
egdaniel | 14afb43 | 2014-12-22 10:57:08 -0800 | [diff] [blame] | 54 | // Each is drawn as three triangles ((a0,a1,b0), (b0,c1,c0), (a1,c1,b0)) |
| 55 | // specified by these 9 indices: |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 56 | static const uint16_t kQuadIdxBufPattern[] = { |
| 57 | 0, 1, 2, |
| 58 | 2, 4, 3, |
| 59 | 1, 4, 2 |
| 60 | }; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 61 | |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 62 | static const int kIdxsPerQuad = SK_ARRAY_COUNT(kQuadIdxBufPattern); |
| 63 | static const int kQuadNumVertices = 5; |
| 64 | static const int kQuadsNumInIdxBuffer = 256; |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 65 | GR_DECLARE_STATIC_UNIQUE_KEY(gQuadsIndexBufferKey); |
| 66 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 67 | static const GrBuffer* ref_quads_index_buffer(GrResourceProvider* resourceProvider) { |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 68 | GR_DEFINE_STATIC_UNIQUE_KEY(gQuadsIndexBufferKey); |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 69 | return resourceProvider->findOrCreateInstancedIndexBuffer( |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 70 | kQuadIdxBufPattern, kIdxsPerQuad, kQuadsNumInIdxBuffer, kQuadNumVertices, |
| 71 | gQuadsIndexBufferKey); |
| 72 | } |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 73 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 74 | |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 75 | // Each line segment is rendered as two quads and two triangles. |
| 76 | // p0 and p1 have alpha = 1 while all other points have alpha = 0. |
| 77 | // The four external points are offset 1 pixel perpendicular to the |
| 78 | // line and half a pixel parallel to the line. |
| 79 | // |
| 80 | // p4 p5 |
| 81 | // p0 p1 |
| 82 | // p2 p3 |
| 83 | // |
| 84 | // Each is drawn as six triangles specified by these 18 indices: |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 85 | |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 86 | static const uint16_t kLineSegIdxBufPattern[] = { |
| 87 | 0, 1, 3, |
| 88 | 0, 3, 2, |
| 89 | 0, 4, 5, |
| 90 | 0, 5, 1, |
| 91 | 0, 2, 4, |
| 92 | 1, 5, 3 |
| 93 | }; |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 94 | |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 95 | static const int kIdxsPerLineSeg = SK_ARRAY_COUNT(kLineSegIdxBufPattern); |
| 96 | static const int kLineSegNumVertices = 6; |
| 97 | static const int kLineSegsNumInIdxBuffer = 256; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 98 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 99 | GR_DECLARE_STATIC_UNIQUE_KEY(gLinesIndexBufferKey); |
| 100 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 101 | static const GrBuffer* ref_lines_index_buffer(GrResourceProvider* resourceProvider) { |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 102 | GR_DEFINE_STATIC_UNIQUE_KEY(gLinesIndexBufferKey); |
bsalomon | eae6200 | 2015-07-31 13:59:30 -0700 | [diff] [blame] | 103 | return resourceProvider->findOrCreateInstancedIndexBuffer( |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 104 | kLineSegIdxBufPattern, kIdxsPerLineSeg, kLineSegsNumInIdxBuffer, kLineSegNumVertices, |
| 105 | gLinesIndexBufferKey); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 106 | } |
| 107 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 108 | // Takes 178th time of logf on Z600 / VC2010 |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 109 | static int get_float_exp(float x) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 110 | GR_STATIC_ASSERT(sizeof(int) == sizeof(float)); |
commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 111 | #ifdef SK_DEBUG |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 112 | static bool tested; |
| 113 | if (!tested) { |
| 114 | tested = true; |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 115 | SkASSERT(get_float_exp(0.25f) == -2); |
| 116 | SkASSERT(get_float_exp(0.3f) == -2); |
| 117 | SkASSERT(get_float_exp(0.5f) == -1); |
| 118 | SkASSERT(get_float_exp(1.f) == 0); |
| 119 | SkASSERT(get_float_exp(2.f) == 1); |
| 120 | SkASSERT(get_float_exp(2.5f) == 1); |
| 121 | SkASSERT(get_float_exp(8.f) == 3); |
| 122 | SkASSERT(get_float_exp(100.f) == 6); |
| 123 | SkASSERT(get_float_exp(1000.f) == 9); |
| 124 | SkASSERT(get_float_exp(1024.f) == 10); |
| 125 | SkASSERT(get_float_exp(3000000.f) == 21); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 126 | } |
| 127 | #endif |
bsalomon@google.com | 2ec7280 | 2011-09-21 21:46:03 +0000 | [diff] [blame] | 128 | const int* iptr = (const int*)&x; |
| 129 | return (((*iptr) & 0x7f800000) >> 23) - 127; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 130 | } |
| 131 | |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 132 | // Uses the max curvature function for quads to estimate |
| 133 | // where to chop the conic. If the max curvature is not |
| 134 | // found along the curve segment it will return 1 and |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 135 | // 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] | 136 | // and dst[1] are the two new conics. |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 137 | 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] | 138 | SkScalar t = SkFindQuadMaxCurvature(src); |
| 139 | if (t == 0) { |
| 140 | if (dst) { |
| 141 | dst[0].set(src, weight); |
| 142 | } |
| 143 | return 1; |
| 144 | } else { |
| 145 | if (dst) { |
| 146 | SkConic conic; |
| 147 | conic.set(src, weight); |
caryclark | 414c429 | 2016-09-26 11:03:54 -0700 | [diff] [blame] | 148 | if (!conic.chopAt(t, dst)) { |
| 149 | dst[0].set(src, weight); |
| 150 | return 1; |
| 151 | } |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 152 | } |
| 153 | return 2; |
| 154 | } |
| 155 | } |
| 156 | |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 157 | // Calls split_conic on the entire conic and then once more on each subsection. |
| 158 | // Most cases will result in either 1 conic (chop point is not within t range) |
| 159 | // or 3 points (split once and then one subsection is split again). |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 160 | 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] | 161 | SkConic dstTemp[2]; |
| 162 | int conicCnt = split_conic(src, dstTemp, weight); |
| 163 | if (2 == conicCnt) { |
| 164 | int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW); |
| 165 | conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW); |
| 166 | } else { |
| 167 | dst[0] = dstTemp[0]; |
| 168 | } |
| 169 | return conicCnt; |
| 170 | } |
| 171 | |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 172 | // returns 0 if quad/conic is degen or close to it |
| 173 | // in this case approx the path with lines |
| 174 | // otherwise returns 1 |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 175 | static int is_degen_quad_or_conic(const SkPoint p[3], SkScalar* dsqd) { |
jvanverth | cfeb85f | 2016-04-29 07:38:10 -0700 | [diff] [blame] | 176 | static const SkScalar gDegenerateToLineTol = GrPathUtils::kDefaultTolerance; |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 177 | static const SkScalar gDegenerateToLineTolSqd = |
| 178 | SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol); |
| 179 | |
| 180 | if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd || |
| 181 | p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) { |
| 182 | return 1; |
| 183 | } |
| 184 | |
joshualitt | 6364807 | 2015-02-19 10:25:21 -0800 | [diff] [blame] | 185 | *dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]); |
| 186 | if (*dsqd < gDegenerateToLineTolSqd) { |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 187 | return 1; |
| 188 | } |
| 189 | |
| 190 | if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) { |
| 191 | return 1; |
| 192 | } |
| 193 | return 0; |
| 194 | } |
| 195 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 196 | static int is_degen_quad_or_conic(const SkPoint p[3]) { |
joshualitt | 6364807 | 2015-02-19 10:25:21 -0800 | [diff] [blame] | 197 | SkScalar dsqd; |
| 198 | return is_degen_quad_or_conic(p, &dsqd); |
| 199 | } |
| 200 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 201 | // we subdivide the quads to avoid huge overfill |
| 202 | // if it returns -1 then should be drawn as lines |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 203 | static int num_quad_subdivs(const SkPoint p[3]) { |
joshualitt | 6364807 | 2015-02-19 10:25:21 -0800 | [diff] [blame] | 204 | SkScalar dsqd; |
| 205 | if (is_degen_quad_or_conic(p, &dsqd)) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 206 | return -1; |
| 207 | } |
| 208 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 209 | // tolerance of triangle height in pixels |
| 210 | // tuned on windows Quadro FX 380 / Z600 |
| 211 | // trade off of fill vs cpu time on verts |
| 212 | // maybe different when do this using gpu (geo or tess shaders) |
| 213 | static const SkScalar gSubdivTol = 175 * SK_Scalar1; |
| 214 | |
robertphillips@google.com | 7460b37 | 2012-04-25 16:54:51 +0000 | [diff] [blame] | 215 | if (dsqd <= SkScalarMul(gSubdivTol, gSubdivTol)) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 216 | return 0; |
| 217 | } else { |
robertphillips@google.com | 87379e1 | 2013-03-29 12:11:10 +0000 | [diff] [blame] | 218 | static const int kMaxSub = 4; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 219 | // subdividing the quad reduces d by 4. so we want x = log4(d/tol) |
| 220 | // = log4(d*d/tol*tol)/2 |
| 221 | // = log2(d*d/tol*tol) |
| 222 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 223 | // +1 since we're ignoring the mantissa contribution. |
| 224 | int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1; |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 225 | log = SkTMin(SkTMax(0, log), kMaxSub); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 226 | return log; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 227 | } |
| 228 | } |
| 229 | |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 230 | /** |
| 231 | * Generates the lines and quads to be rendered. Lines are always recorded in |
| 232 | * device space. We will do a device space bloat to account for the 1pixel |
| 233 | * thickness. |
| 234 | * Quads are recorded in device space unless m contains |
| 235 | * perspective, then in they are in src space. We do this because we will |
| 236 | * subdivide large quads to reduce over-fill. This subdivision has to be |
| 237 | * performed before applying the perspective matrix. |
| 238 | */ |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 239 | static int gather_lines_and_quads(const SkPath& path, |
| 240 | const SkMatrix& m, |
| 241 | const SkIRect& devClipBounds, |
| 242 | GrAAHairLinePathRenderer::PtArray* lines, |
| 243 | GrAAHairLinePathRenderer::PtArray* quads, |
| 244 | GrAAHairLinePathRenderer::PtArray* conics, |
| 245 | GrAAHairLinePathRenderer::IntArray* quadSubdivCnts, |
| 246 | GrAAHairLinePathRenderer::FloatArray* conicWeights) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 247 | SkPath::Iter iter(path, false); |
| 248 | |
| 249 | int totalQuadCount = 0; |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 250 | SkRect bounds; |
| 251 | SkIRect ibounds; |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 252 | |
| 253 | bool persp = m.hasPerspective(); |
| 254 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 255 | for (;;) { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 256 | SkPoint pathPts[4]; |
| 257 | SkPoint devPts[4]; |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 258 | SkPath::Verb verb = iter.next(pathPts); |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 259 | switch (verb) { |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 260 | case SkPath::kConic_Verb: { |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 261 | SkConic dst[4]; |
| 262 | // We chop the conics to create tighter clipping to hide error |
| 263 | // that appears near max curvature of very thin conics. Thin |
| 264 | // hyperbolas with high weight still show error. |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 265 | int conicCnt = chop_conic(pathPts, dst, iter.conicWeight()); |
| 266 | for (int i = 0; i < conicCnt; ++i) { |
| 267 | SkPoint* chopPnts = dst[i].fPts; |
| 268 | m.mapPoints(devPts, chopPnts, 3); |
| 269 | bounds.setBounds(devPts, 3); |
| 270 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 271 | bounds.roundOut(&ibounds); |
| 272 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
| 273 | if (is_degen_quad_or_conic(devPts)) { |
| 274 | SkPoint* pts = lines->push_back_n(4); |
| 275 | pts[0] = devPts[0]; |
| 276 | pts[1] = devPts[1]; |
| 277 | pts[2] = devPts[1]; |
| 278 | pts[3] = devPts[2]; |
| 279 | } else { |
| 280 | // when in perspective keep conics in src space |
| 281 | SkPoint* cPts = persp ? chopPnts : devPts; |
| 282 | SkPoint* pts = conics->push_back_n(3); |
| 283 | pts[0] = cPts[0]; |
| 284 | pts[1] = cPts[1]; |
| 285 | pts[2] = cPts[2]; |
| 286 | conicWeights->push_back() = dst[i].fW; |
| 287 | } |
| 288 | } |
| 289 | } |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 290 | break; |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 291 | } |
| 292 | case SkPath::kMove_Verb: |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 293 | break; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 294 | case SkPath::kLine_Verb: |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 295 | m.mapPoints(devPts, pathPts, 2); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 296 | bounds.setBounds(devPts, 2); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 297 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 298 | bounds.roundOut(&ibounds); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 299 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 300 | SkPoint* pts = lines->push_back_n(2); |
| 301 | pts[0] = devPts[0]; |
| 302 | pts[1] = devPts[1]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 303 | } |
| 304 | break; |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 305 | case SkPath::kQuad_Verb: { |
| 306 | SkPoint choppedPts[5]; |
| 307 | // Chopping the quad helps when the quad is either degenerate or nearly degenerate. |
| 308 | // When it is degenerate it allows the approximation with lines to work since the |
| 309 | // chop point (if there is one) will be at the parabola's vertex. In the nearly |
| 310 | // degenerate the QuadUVMatrix computed for the points is almost singular which |
| 311 | // can cause rendering artifacts. |
| 312 | int n = SkChopQuadAtMaxCurvature(pathPts, choppedPts); |
| 313 | for (int i = 0; i < n; ++i) { |
| 314 | SkPoint* quadPts = choppedPts + i * 2; |
| 315 | m.mapPoints(devPts, quadPts, 3); |
| 316 | bounds.setBounds(devPts, 3); |
| 317 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 318 | bounds.roundOut(&ibounds); |
| 319 | |
| 320 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
| 321 | int subdiv = num_quad_subdivs(devPts); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 322 | SkASSERT(subdiv >= -1); |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 323 | if (-1 == subdiv) { |
| 324 | SkPoint* pts = lines->push_back_n(4); |
| 325 | pts[0] = devPts[0]; |
| 326 | pts[1] = devPts[1]; |
| 327 | pts[2] = devPts[1]; |
| 328 | pts[3] = devPts[2]; |
| 329 | } else { |
| 330 | // when in perspective keep quads in src space |
| 331 | SkPoint* qPts = persp ? quadPts : devPts; |
| 332 | SkPoint* pts = quads->push_back_n(3); |
| 333 | pts[0] = qPts[0]; |
| 334 | pts[1] = qPts[1]; |
| 335 | pts[2] = qPts[2]; |
| 336 | quadSubdivCnts->push_back() = subdiv; |
| 337 | totalQuadCount += 1 << subdiv; |
| 338 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 339 | } |
| 340 | } |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 341 | break; |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 342 | } |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 343 | case SkPath::kCubic_Verb: |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 344 | m.mapPoints(devPts, pathPts, 4); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 345 | bounds.setBounds(devPts, 4); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 346 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 347 | bounds.roundOut(&ibounds); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 348 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 349 | PREALLOC_PTARRAY(32) q; |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 350 | // We convert cubics to quadratics (for now). |
| 351 | // In perspective have to do conversion in src space. |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 352 | if (persp) { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 353 | SkScalar tolScale = |
bsalomon | 18fab30 | 2016-02-16 08:00:05 -0800 | [diff] [blame] | 354 | GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m, path.getBounds()); |
| 355 | GrPathUtils::convertCubicToQuads(pathPts, tolScale, &q); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 356 | } else { |
bsalomon | 18fab30 | 2016-02-16 08:00:05 -0800 | [diff] [blame] | 357 | GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, &q); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 358 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 359 | for (int i = 0; i < q.count(); i += 3) { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 360 | SkPoint* qInDevSpace; |
| 361 | // bounds has to be calculated in device space, but q is |
| 362 | // in src space when there is perspective. |
| 363 | if (persp) { |
| 364 | m.mapPoints(devPts, &q[i], 3); |
| 365 | bounds.setBounds(devPts, 3); |
| 366 | qInDevSpace = devPts; |
| 367 | } else { |
| 368 | bounds.setBounds(&q[i], 3); |
| 369 | qInDevSpace = &q[i]; |
| 370 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 371 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 372 | bounds.roundOut(&ibounds); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 373 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 374 | int subdiv = num_quad_subdivs(qInDevSpace); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 375 | SkASSERT(subdiv >= -1); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 376 | if (-1 == subdiv) { |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 377 | SkPoint* pts = lines->push_back_n(4); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 378 | // lines should always be in device coords |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 379 | pts[0] = qInDevSpace[0]; |
| 380 | pts[1] = qInDevSpace[1]; |
| 381 | pts[2] = qInDevSpace[1]; |
| 382 | pts[3] = qInDevSpace[2]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 383 | } else { |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 384 | SkPoint* pts = quads->push_back_n(3); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 385 | // q is already in src space when there is no |
| 386 | // perspective and dev coords otherwise. |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 387 | pts[0] = q[0 + i]; |
| 388 | pts[1] = q[1 + i]; |
| 389 | pts[2] = q[2 + i]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 390 | quadSubdivCnts->push_back() = subdiv; |
| 391 | totalQuadCount += 1 << subdiv; |
| 392 | } |
| 393 | } |
| 394 | } |
| 395 | } |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 396 | break; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 397 | case SkPath::kClose_Verb: |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 398 | break; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 399 | case SkPath::kDone_Verb: |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 400 | return totalQuadCount; |
| 401 | } |
| 402 | } |
| 403 | } |
| 404 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 405 | struct LineVertex { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 406 | SkPoint fPos; |
egdaniel | e27065a | 2014-11-06 08:00:48 -0800 | [diff] [blame] | 407 | float fCoverage; |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 408 | }; |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 409 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 410 | struct BezierVertex { |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 411 | SkPoint fPos; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 412 | union { |
| 413 | struct { |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 414 | SkScalar fK; |
| 415 | SkScalar fL; |
| 416 | SkScalar fM; |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 417 | } fConic; |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 418 | SkVector fQuadCoord; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 419 | struct { |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 420 | SkScalar fBogus[4]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 421 | }; |
| 422 | }; |
| 423 | }; |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 424 | |
commit-bot@chromium.org | 972f9cd | 2014-03-28 17:58:28 +0000 | [diff] [blame] | 425 | GR_STATIC_ASSERT(sizeof(BezierVertex) == 3 * sizeof(SkPoint)); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 426 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 427 | static void intersect_lines(const SkPoint& ptA, const SkVector& normA, |
| 428 | const SkPoint& ptB, const SkVector& normB, |
| 429 | SkPoint* result) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 430 | |
| 431 | SkScalar lineAW = -normA.dot(ptA); |
| 432 | SkScalar lineBW = -normB.dot(ptB); |
| 433 | |
| 434 | SkScalar wInv = SkScalarMul(normA.fX, normB.fY) - |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 435 | SkScalarMul(normA.fY, normB.fX); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 436 | wInv = SkScalarInvert(wInv); |
| 437 | |
| 438 | result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY); |
| 439 | result->fX = SkScalarMul(result->fX, wInv); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 440 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 441 | result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW); |
| 442 | result->fY = SkScalarMul(result->fY, wInv); |
| 443 | } |
| 444 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 445 | 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] | 446 | // this should be in the src space, not dev coords, when we have perspective |
| 447 | GrPathUtils::QuadUVMatrix DevToUV(qpts); |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 448 | DevToUV.apply<kQuadNumVertices, sizeof(BezierVertex), sizeof(SkPoint)>(verts); |
egdaniel@google.com | 34b05ca | 2013-08-05 20:43:12 +0000 | [diff] [blame] | 449 | } |
| 450 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 451 | static void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice, |
| 452 | const SkMatrix* toSrc, BezierVertex verts[kQuadNumVertices]) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 453 | SkASSERT(!toDevice == !toSrc); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 454 | // original quad is specified by tri a,b,c |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 455 | SkPoint a = qpts[0]; |
| 456 | SkPoint b = qpts[1]; |
| 457 | SkPoint c = qpts[2]; |
| 458 | |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 459 | if (toDevice) { |
| 460 | toDevice->mapPoints(&a, 1); |
| 461 | toDevice->mapPoints(&b, 1); |
| 462 | toDevice->mapPoints(&c, 1); |
| 463 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 464 | // make a new poly where we replace a and c by a 1-pixel wide edges orthog |
| 465 | // to edges ab and bc: |
| 466 | // |
| 467 | // before | after |
| 468 | // | b0 |
| 469 | // b | |
| 470 | // | |
| 471 | // | a0 c0 |
| 472 | // a c | a1 c1 |
| 473 | // |
| 474 | // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c, |
| 475 | // respectively. |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 476 | BezierVertex& a0 = verts[0]; |
| 477 | BezierVertex& a1 = verts[1]; |
| 478 | BezierVertex& b0 = verts[2]; |
| 479 | BezierVertex& c0 = verts[3]; |
| 480 | BezierVertex& c1 = verts[4]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 481 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 482 | SkVector ab = b; |
| 483 | ab -= a; |
| 484 | SkVector ac = c; |
| 485 | ac -= a; |
| 486 | SkVector cb = b; |
| 487 | cb -= c; |
| 488 | |
| 489 | // We should have already handled degenerates |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 490 | SkASSERT(ab.length() > 0 && cb.length() > 0); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 491 | |
| 492 | ab.normalize(); |
| 493 | SkVector abN; |
| 494 | abN.setOrthog(ab, SkVector::kLeft_Side); |
| 495 | if (abN.dot(ac) > 0) { |
| 496 | abN.negate(); |
| 497 | } |
| 498 | |
| 499 | cb.normalize(); |
| 500 | SkVector cbN; |
| 501 | cbN.setOrthog(cb, SkVector::kLeft_Side); |
| 502 | if (cbN.dot(ac) < 0) { |
| 503 | cbN.negate(); |
| 504 | } |
| 505 | |
| 506 | a0.fPos = a; |
| 507 | a0.fPos += abN; |
| 508 | a1.fPos = a; |
| 509 | a1.fPos -= abN; |
| 510 | |
| 511 | c0.fPos = c; |
| 512 | c0.fPos += cbN; |
| 513 | c1.fPos = c; |
| 514 | c1.fPos -= cbN; |
| 515 | |
| 516 | intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos); |
| 517 | |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 518 | if (toSrc) { |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 519 | toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(BezierVertex), kQuadNumVertices); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 520 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 521 | } |
| 522 | |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 523 | // Equations based off of Loop-Blinn Quadratic GPU Rendering |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 524 | // Input Parametric: |
| 525 | // 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) |
| 526 | // Output Implicit: |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 527 | // f(x, y, w) = f(P) = K^2 - LM |
| 528 | // 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] | 529 | // k, l, m are calculated in function GrPathUtils::getConicKLM |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 530 | static void set_conic_coeffs(const SkPoint p[3], BezierVertex verts[kQuadNumVertices], |
| 531 | const SkScalar weight) { |
commit-bot@chromium.org | 1394840 | 2013-08-20 17:55:43 +0000 | [diff] [blame] | 532 | SkScalar klm[9]; |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 533 | |
commit-bot@chromium.org | 1394840 | 2013-08-20 17:55:43 +0000 | [diff] [blame] | 534 | GrPathUtils::getConicKLM(p, weight, klm); |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 535 | |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 536 | for (int i = 0; i < kQuadNumVertices; ++i) { |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 537 | const SkPoint pnt = verts[i].fPos; |
commit-bot@chromium.org | 1394840 | 2013-08-20 17:55:43 +0000 | [diff] [blame] | 538 | verts[i].fConic.fK = pnt.fX * klm[0] + pnt.fY * klm[1] + klm[2]; |
| 539 | verts[i].fConic.fL = pnt.fX * klm[3] + pnt.fY * klm[4] + klm[5]; |
| 540 | verts[i].fConic.fM = pnt.fX * klm[6] + pnt.fY * klm[7] + klm[8]; |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 541 | } |
| 542 | } |
| 543 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 544 | static void add_conics(const SkPoint p[3], |
| 545 | const SkScalar weight, |
| 546 | const SkMatrix* toDevice, |
| 547 | const SkMatrix* toSrc, |
| 548 | BezierVertex** vert) { |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 549 | bloat_quad(p, toDevice, toSrc, *vert); |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 550 | set_conic_coeffs(p, *vert, weight); |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 551 | *vert += kQuadNumVertices; |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 552 | } |
| 553 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 554 | static void add_quads(const SkPoint p[3], |
| 555 | int subdiv, |
| 556 | const SkMatrix* toDevice, |
| 557 | const SkMatrix* toSrc, |
| 558 | BezierVertex** vert) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 559 | SkASSERT(subdiv >= 0); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 560 | if (subdiv) { |
| 561 | SkPoint newP[5]; |
| 562 | SkChopQuadAtHalf(p, newP); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 563 | add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert); |
| 564 | add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 565 | } else { |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 566 | bloat_quad(p, toDevice, toSrc, *vert); |
egdaniel@google.com | 34b05ca | 2013-08-05 20:43:12 +0000 | [diff] [blame] | 567 | set_uv_quad(p, *vert); |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 568 | *vert += kQuadNumVertices; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 569 | } |
| 570 | } |
| 571 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 572 | static void add_line(const SkPoint p[2], |
| 573 | const SkMatrix* toSrc, |
| 574 | uint8_t coverage, |
| 575 | LineVertex** vert) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 576 | const SkPoint& a = p[0]; |
| 577 | const SkPoint& b = p[1]; |
| 578 | |
robertphillips@google.com | ada90da | 2013-09-18 22:14:49 +0000 | [diff] [blame] | 579 | SkVector ortho, vec = b; |
| 580 | vec -= a; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 581 | |
robertphillips@google.com | ada90da | 2013-09-18 22:14:49 +0000 | [diff] [blame] | 582 | if (vec.setLength(SK_ScalarHalf)) { |
| 583 | // Create a vector orthogonal to 'vec' and of unit length |
| 584 | ortho.fX = 2.0f * vec.fY; |
| 585 | ortho.fY = -2.0f * vec.fX; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 586 | |
egdaniel | e27065a | 2014-11-06 08:00:48 -0800 | [diff] [blame] | 587 | float floatCoverage = GrNormalizeByteToFloat(coverage); |
| 588 | |
robertphillips@google.com | ada90da | 2013-09-18 22:14:49 +0000 | [diff] [blame] | 589 | (*vert)[0].fPos = a; |
egdaniel | e27065a | 2014-11-06 08:00:48 -0800 | [diff] [blame] | 590 | (*vert)[0].fCoverage = floatCoverage; |
robertphillips@google.com | ada90da | 2013-09-18 22:14:49 +0000 | [diff] [blame] | 591 | (*vert)[1].fPos = b; |
egdaniel | e27065a | 2014-11-06 08:00:48 -0800 | [diff] [blame] | 592 | (*vert)[1].fCoverage = floatCoverage; |
robertphillips@google.com | ada90da | 2013-09-18 22:14:49 +0000 | [diff] [blame] | 593 | (*vert)[2].fPos = a - vec + ortho; |
| 594 | (*vert)[2].fCoverage = 0; |
| 595 | (*vert)[3].fPos = b + vec + ortho; |
| 596 | (*vert)[3].fCoverage = 0; |
| 597 | (*vert)[4].fPos = a - vec - ortho; |
| 598 | (*vert)[4].fCoverage = 0; |
| 599 | (*vert)[5].fPos = b + vec - ortho; |
| 600 | (*vert)[5].fCoverage = 0; |
| 601 | |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 602 | if (toSrc) { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 603 | toSrc->mapPointsWithStride(&(*vert)->fPos, |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 604 | sizeof(LineVertex), |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 605 | kLineSegNumVertices); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 606 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 607 | } else { |
| 608 | // just make it degenerate and likely offscreen |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 609 | for (int i = 0; i < kLineSegNumVertices; ++i) { |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 610 | (*vert)[i].fPos.set(SK_ScalarMax, SK_ScalarMax); |
| 611 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 612 | } |
| 613 | |
joshualitt | 5ead6da | 2014-10-22 16:00:29 -0700 | [diff] [blame] | 614 | *vert += kLineSegNumVertices; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 615 | } |
| 616 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 617 | /////////////////////////////////////////////////////////////////////////////// |
| 618 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 619 | bool GrAAHairLinePathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
| 620 | if (!args.fAntiAlias) { |
commit-bot@chromium.org | e0a868c | 2013-11-22 07:02:11 +0000 | [diff] [blame] | 621 | return false; |
| 622 | } |
| 623 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 624 | if (!IsStrokeHairlineOrEquivalent(args.fShape->style(), *args.fViewMatrix, nullptr)) { |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 625 | return false; |
| 626 | } |
| 627 | |
| 628 | // We don't currently handle dashing in this class though perhaps we should. |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 629 | if (args.fShape->style().pathEffect()) { |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 630 | return false; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 631 | } |
| 632 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 633 | if (SkPath::kLine_SegmentMask == args.fShape->segmentMask() || |
robertphillips | e7d4b2f | 2015-08-13 07:57:10 -0700 | [diff] [blame] | 634 | args.fShaderCaps->shaderDerivativeSupport()) { |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 635 | return true; |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 636 | } |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 637 | |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 638 | return false; |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 639 | } |
| 640 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 641 | template <class VertexType> |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 642 | 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] | 643 | { |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 644 | SkRect tolDevBounds = devBounds; |
commit-bot@chromium.org | b8bd6cb | 2013-09-03 14:56:17 +0000 | [diff] [blame] | 645 | // The bounds ought to be tight, but in perspective the below code runs the verts |
| 646 | // 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] | 647 | if (viewMatrix.hasPerspective()) { |
commit-bot@chromium.org | b8bd6cb | 2013-09-03 14:56:17 +0000 | [diff] [blame] | 648 | tolDevBounds.outset(SK_Scalar1 / 1000, SK_Scalar1 / 1000); |
| 649 | } else { |
| 650 | // Non-persp matrices cause this path renderer to draw in device space. |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 651 | SkASSERT(viewMatrix.isIdentity()); |
commit-bot@chromium.org | b8bd6cb | 2013-09-03 14:56:17 +0000 | [diff] [blame] | 652 | } |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 653 | SkRect actualBounds; |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 654 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 655 | VertexType* verts = reinterpret_cast<VertexType*>(vertices); |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 656 | bool first = true; |
| 657 | for (int i = 0; i < vCount; ++i) { |
| 658 | SkPoint pos = verts[i].fPos; |
| 659 | // This is a hack to workaround the fact that we move some degenerate segments offscreen. |
| 660 | if (SK_ScalarMax == pos.fX) { |
| 661 | continue; |
| 662 | } |
joshualitt | 8059eb9 | 2014-12-29 15:10:07 -0800 | [diff] [blame] | 663 | viewMatrix.mapPoints(&pos, 1); |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 664 | if (first) { |
| 665 | actualBounds.set(pos.fX, pos.fY, pos.fX, pos.fY); |
| 666 | first = false; |
| 667 | } else { |
| 668 | actualBounds.growToInclude(pos.fX, pos.fY); |
| 669 | } |
| 670 | } |
| 671 | if (!first) { |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 672 | return tolDevBounds.contains(actualBounds); |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 673 | } |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 674 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 675 | return true; |
| 676 | } |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 677 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 678 | class AAHairlineBatch : public GrVertexBatch { |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 679 | public: |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 680 | DEFINE_OP_CLASS_ID |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 681 | |
bsalomon | f170309 | 2016-06-29 18:41:53 -0700 | [diff] [blame] | 682 | AAHairlineBatch(GrColor color, |
| 683 | uint8_t coverage, |
| 684 | const SkMatrix& viewMatrix, |
| 685 | const SkPath& path, |
| 686 | SkIRect devClipBounds) : INHERITED(ClassID()) { |
| 687 | fGeoData.emplace_back(Geometry{color, coverage, viewMatrix, path, devClipBounds}); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 688 | |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 689 | this->setTransformedBounds(path.getBounds(), viewMatrix, HasAABloat::kYes, |
| 690 | IsZeroArea::kYes); |
bsalomon | f170309 | 2016-06-29 18:41:53 -0700 | [diff] [blame] | 691 | } |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 692 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 693 | const char* name() const override { return "AAHairlineBatch"; } |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 694 | |
Brian Salomon | 7c3e718 | 2016-12-01 09:35:30 -0500 | [diff] [blame] | 695 | SkString dumpInfo() const override { |
| 696 | SkString string; |
| 697 | for (const auto& geo : fGeoData) { |
| 698 | string.appendf("Color: 0x%08x Coverage: 0x%02x\n", geo.fColor, geo.fCoverage); |
| 699 | } |
| 700 | string.append(DumpPipelineInfo(*this->pipeline())); |
| 701 | string.append(INHERITED::dumpInfo()); |
| 702 | return string; |
| 703 | } |
| 704 | |
halcanary | 9d524f2 | 2016-03-29 09:03:52 -0700 | [diff] [blame] | 705 | void computePipelineOptimizations(GrInitInvariantOutput* color, |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 706 | GrInitInvariantOutput* coverage, |
| 707 | GrBatchToXPOverrides* overrides) const override { |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 708 | // When this is called on a batch, there is only one geometry bundle |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 709 | color->setKnownFourComponents(fGeoData[0].fColor); |
| 710 | coverage->setUnknownSingleComponent(); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 711 | } |
| 712 | |
bsalomon | e46f9fe | 2015-08-18 06:05:14 -0700 | [diff] [blame] | 713 | private: |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 714 | void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 715 | // Handle any color overrides |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 716 | if (!overrides.readsColor()) { |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 717 | fGeoData[0].fColor = GrColor_ILLEGAL; |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 718 | } |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 719 | overrides.getOverrideColorIfSet(&fGeoData[0].fColor); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 720 | |
| 721 | // setup batch properties |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 722 | fBatch.fColorIgnored = !overrides.readsColor(); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 723 | fBatch.fColor = fGeoData[0].fColor; |
ethannicholas | ff21032 | 2015-11-24 12:10:10 -0800 | [diff] [blame] | 724 | fBatch.fUsesLocalCoords = overrides.readsLocalCoords(); |
| 725 | fBatch.fCoverageIgnored = !overrides.readsCoverage(); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 726 | fBatch.fCoverage = fGeoData[0].fCoverage; |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 727 | } |
| 728 | |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 729 | void onPrepareDraws(Target*) const override; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 730 | |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 731 | typedef SkTArray<SkPoint, true> PtArray; |
| 732 | typedef SkTArray<int, true> IntArray; |
| 733 | typedef SkTArray<float, true> FloatArray; |
| 734 | |
Brian Salomon | 25a8809 | 2016-12-01 09:36:50 -0500 | [diff] [blame] | 735 | bool onCombineIfPossible(GrOp* t, const GrCaps& caps) override { |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 736 | AAHairlineBatch* that = t->cast<AAHairlineBatch>(); |
| 737 | |
| 738 | if (!GrPipeline::CanCombine(*this->pipeline(), this->bounds(), *that->pipeline(), |
| 739 | that->bounds(), caps)) { |
joshualitt | 8cab9a7 | 2015-07-16 09:13:50 -0700 | [diff] [blame] | 740 | return false; |
| 741 | } |
| 742 | |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 743 | if (this->viewMatrix().hasPerspective() != that->viewMatrix().hasPerspective()) { |
| 744 | return false; |
| 745 | } |
| 746 | |
| 747 | // We go to identity if we don't have perspective |
| 748 | if (this->viewMatrix().hasPerspective() && |
| 749 | !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 750 | return false; |
| 751 | } |
| 752 | |
| 753 | // TODO we can actually batch hairlines if they are the same color in a kind of bulk method |
| 754 | // but we haven't implemented this yet |
| 755 | // TODO investigate going to vertex color and coverage? |
| 756 | if (this->coverage() != that->coverage()) { |
| 757 | return false; |
| 758 | } |
| 759 | |
| 760 | if (this->color() != that->color()) { |
| 761 | return false; |
| 762 | } |
| 763 | |
| 764 | SkASSERT(this->usesLocalCoords() == that->usesLocalCoords()); |
| 765 | if (this->usesLocalCoords() && !this->viewMatrix().cheapEqualTo(that->viewMatrix())) { |
| 766 | return false; |
| 767 | } |
| 768 | |
bsalomon | f170309 | 2016-06-29 18:41:53 -0700 | [diff] [blame] | 769 | fGeoData.push_back_n(that->fGeoData.count(), that->fGeoData.begin()); |
bsalomon | 88cf17d | 2016-07-08 06:40:56 -0700 | [diff] [blame] | 770 | this->joinBounds(*that); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 771 | return true; |
| 772 | } |
| 773 | |
| 774 | GrColor color() const { return fBatch.fColor; } |
| 775 | uint8_t coverage() const { return fBatch.fCoverage; } |
| 776 | bool usesLocalCoords() const { return fBatch.fUsesLocalCoords; } |
| 777 | const SkMatrix& viewMatrix() const { return fGeoData[0].fViewMatrix; } |
joshualitt | b8c241a | 2015-05-19 08:23:30 -0700 | [diff] [blame] | 778 | bool coverageIgnored() const { return fBatch.fCoverageIgnored; } |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 779 | |
bsalomon | f170309 | 2016-06-29 18:41:53 -0700 | [diff] [blame] | 780 | |
| 781 | struct Geometry { |
| 782 | GrColor fColor; |
| 783 | uint8_t fCoverage; |
| 784 | SkMatrix fViewMatrix; |
| 785 | SkPath fPath; |
| 786 | SkIRect fDevClipBounds; |
| 787 | }; |
| 788 | |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 789 | struct BatchTracker { |
| 790 | GrColor fColor; |
| 791 | uint8_t fCoverage; |
| 792 | SkRect fDevBounds; |
| 793 | bool fUsesLocalCoords; |
| 794 | bool fColorIgnored; |
| 795 | bool fCoverageIgnored; |
| 796 | }; |
| 797 | |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 798 | BatchTracker fBatch; |
| 799 | SkSTArray<1, Geometry, true> fGeoData; |
reed | 1b55a96 | 2015-09-17 20:16:13 -0700 | [diff] [blame] | 800 | |
| 801 | typedef GrVertexBatch INHERITED; |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 802 | }; |
| 803 | |
joshualitt | 144c3c8 | 2015-11-30 12:30:13 -0800 | [diff] [blame] | 804 | void AAHairlineBatch::onPrepareDraws(Target* target) const { |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 805 | // Setup the viewmatrix and localmatrix for the GrGeometryProcessor. |
| 806 | SkMatrix invert; |
| 807 | if (!this->viewMatrix().invert(&invert)) { |
| 808 | return; |
| 809 | } |
| 810 | |
| 811 | // we will transform to identity space if the viewmatrix does not have perspective |
| 812 | bool hasPerspective = this->viewMatrix().hasPerspective(); |
| 813 | const SkMatrix* geometryProcessorViewM = &SkMatrix::I(); |
| 814 | const SkMatrix* geometryProcessorLocalM = &invert; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 815 | const SkMatrix* toDevice = nullptr; |
| 816 | const SkMatrix* toSrc = nullptr; |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 817 | if (hasPerspective) { |
| 818 | geometryProcessorViewM = &this->viewMatrix(); |
| 819 | geometryProcessorLocalM = &SkMatrix::I(); |
| 820 | toDevice = &this->viewMatrix(); |
| 821 | toSrc = &invert; |
| 822 | } |
| 823 | |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 824 | // This is hand inlined for maximum performance. |
| 825 | PREALLOC_PTARRAY(128) lines; |
| 826 | PREALLOC_PTARRAY(128) quads; |
| 827 | PREALLOC_PTARRAY(128) conics; |
| 828 | IntArray qSubdivs; |
| 829 | FloatArray cWeights; |
joshualitt | 351ba1b | 2015-02-16 08:33:19 -0800 | [diff] [blame] | 830 | int quadCount = 0; |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 831 | |
| 832 | int instanceCount = fGeoData.count(); |
| 833 | for (int i = 0; i < instanceCount; i++) { |
| 834 | const Geometry& args = fGeoData[i]; |
joshualitt | 351ba1b | 2015-02-16 08:33:19 -0800 | [diff] [blame] | 835 | quadCount += gather_lines_and_quads(args.fPath, args.fViewMatrix, args.fDevClipBounds, |
| 836 | &lines, &quads, &conics, &qSubdivs, &cWeights); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 837 | } |
| 838 | |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 839 | int lineCount = lines.count() / 2; |
| 840 | int conicCount = conics.count() / 3; |
| 841 | |
| 842 | // do lines first |
| 843 | if (lineCount) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 844 | sk_sp<GrGeometryProcessor> lineGP; |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 845 | { |
| 846 | using namespace GrDefaultGeoProcFactory; |
| 847 | |
| 848 | Color color(this->color()); |
| 849 | Coverage coverage(Coverage::kAttribute_Type); |
| 850 | LocalCoords localCoords(this->usesLocalCoords() ? LocalCoords::kUsePosition_Type : |
| 851 | LocalCoords::kUnused_Type); |
| 852 | localCoords.fMatrix = geometryProcessorLocalM; |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 853 | lineGP = GrDefaultGeoProcFactory::Make(color, coverage, localCoords, |
| 854 | *geometryProcessorViewM); |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 857 | sk_sp<const GrBuffer> linesIndexBuffer( |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 858 | ref_lines_index_buffer(target->resourceProvider())); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 859 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 860 | const GrBuffer* vertexBuffer; |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 861 | int firstVertex; |
| 862 | |
| 863 | size_t vertexStride = lineGP->getVertexStride(); |
| 864 | int vertexCount = kLineSegNumVertices * lineCount; |
bsalomon | e64eb57 | 2015-05-07 11:35:55 -0700 | [diff] [blame] | 865 | LineVertex* verts = reinterpret_cast<LineVertex*>( |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 866 | target->makeVertexSpace(vertexStride, vertexCount, &vertexBuffer, &firstVertex)); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 867 | |
bsalomon | e64eb57 | 2015-05-07 11:35:55 -0700 | [diff] [blame] | 868 | if (!verts|| !linesIndexBuffer) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 869 | SkDebugf("Could not allocate vertices\n"); |
| 870 | return; |
| 871 | } |
| 872 | |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 873 | SkASSERT(lineGP->getVertexStride() == sizeof(LineVertex)); |
| 874 | |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 875 | for (int i = 0; i < lineCount; ++i) { |
| 876 | add_line(&lines[2*i], toSrc, this->coverage(), &verts); |
| 877 | } |
| 878 | |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 879 | GrMesh mesh; |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 880 | mesh.initInstanced(kTriangles_GrPrimitiveType, vertexBuffer, linesIndexBuffer.get(), |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 881 | firstVertex, kLineSegNumVertices, kIdxsPerLineSeg, lineCount, |
| 882 | kLineSegsNumInIdxBuffer); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 883 | target->draw(lineGP.get(), mesh); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 884 | } |
| 885 | |
| 886 | if (quadCount || conicCount) { |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 887 | sk_sp<GrGeometryProcessor> quadGP( |
| 888 | GrQuadEffect::Make(this->color(), |
| 889 | *geometryProcessorViewM, |
| 890 | kHairlineAA_GrProcessorEdgeType, |
| 891 | target->caps(), |
| 892 | *geometryProcessorLocalM, |
| 893 | this->usesLocalCoords(), |
| 894 | this->coverage())); |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 895 | |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 896 | sk_sp<GrGeometryProcessor> conicGP( |
| 897 | GrConicEffect::Make(this->color(), |
| 898 | *geometryProcessorViewM, |
| 899 | kHairlineAA_GrProcessorEdgeType, |
| 900 | target->caps(), |
| 901 | *geometryProcessorLocalM, |
| 902 | this->usesLocalCoords(), |
| 903 | this->coverage())); |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 904 | |
cdalton | 397536c | 2016-03-25 12:15:03 -0700 | [diff] [blame] | 905 | const GrBuffer* vertexBuffer; |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 906 | int firstVertex; |
| 907 | |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 908 | sk_sp<const GrBuffer> quadsIndexBuffer( |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 909 | ref_quads_index_buffer(target->resourceProvider())); |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 910 | |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 911 | size_t vertexStride = sizeof(BezierVertex); |
| 912 | int vertexCount = kQuadNumVertices * quadCount + kQuadNumVertices * conicCount; |
bsalomon | 7539856 | 2015-08-17 12:55:38 -0700 | [diff] [blame] | 913 | void *vertices = target->makeVertexSpace(vertexStride, vertexCount, |
| 914 | &vertexBuffer, &firstVertex); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 915 | |
bsalomon | ed0bcad | 2015-05-04 10:36:42 -0700 | [diff] [blame] | 916 | if (!vertices || !quadsIndexBuffer) { |
joshualitt | 4b31de8 | 2015-03-05 14:33:41 -0800 | [diff] [blame] | 917 | SkDebugf("Could not allocate vertices\n"); |
| 918 | return; |
| 919 | } |
| 920 | |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 921 | // Setup vertices |
robertphillips | 44c3128 | 2015-09-03 12:58:48 -0700 | [diff] [blame] | 922 | BezierVertex* bezVerts = reinterpret_cast<BezierVertex*>(vertices); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 923 | |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 924 | int unsubdivQuadCnt = quads.count() / 3; |
| 925 | for (int i = 0; i < unsubdivQuadCnt; ++i) { |
| 926 | SkASSERT(qSubdivs[i] >= 0); |
robertphillips | 44c3128 | 2015-09-03 12:58:48 -0700 | [diff] [blame] | 927 | add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &bezVerts); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | // Start Conics |
| 931 | for (int i = 0; i < conicCount; ++i) { |
robertphillips | 44c3128 | 2015-09-03 12:58:48 -0700 | [diff] [blame] | 932 | add_conics(&conics[3*i], cWeights[i], toDevice, toSrc, &bezVerts); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 933 | } |
| 934 | |
| 935 | if (quadCount > 0) { |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 936 | GrMesh mesh; |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 937 | mesh.initInstanced(kTriangles_GrPrimitiveType, vertexBuffer, quadsIndexBuffer.get(), |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 938 | firstVertex, kQuadNumVertices, kIdxsPerQuad, quadCount, |
| 939 | kQuadsNumInIdxBuffer); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 940 | target->draw(quadGP.get(), mesh); |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 941 | firstVertex += quadCount * kQuadNumVertices; |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 942 | } |
| 943 | |
| 944 | if (conicCount > 0) { |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 945 | GrMesh mesh; |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 946 | mesh.initInstanced(kTriangles_GrPrimitiveType, vertexBuffer, quadsIndexBuffer.get(), |
bsalomon | 342bfc2 | 2016-04-01 06:06:20 -0700 | [diff] [blame] | 947 | firstVertex, kQuadNumVertices, kIdxsPerQuad, conicCount, |
| 948 | kQuadsNumInIdxBuffer); |
bungeman | 06ca8ec | 2016-06-09 08:01:03 -0700 | [diff] [blame] | 949 | target->draw(conicGP.get(), mesh); |
joshualitt | 7bc18b7 | 2015-02-03 16:41:41 -0800 | [diff] [blame] | 950 | } |
| 951 | } |
| 952 | } |
| 953 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 954 | static GrDrawBatch* create_hairline_batch(GrColor color, |
| 955 | const SkMatrix& viewMatrix, |
| 956 | const SkPath& path, |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 957 | const GrStyle& style, |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 958 | const SkIRect& devClipBounds) { |
joshualitt | 40ded32 | 2015-05-02 07:07:17 -0700 | [diff] [blame] | 959 | SkScalar hairlineCoverage; |
| 960 | uint8_t newCoverage = 0xff; |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 961 | if (GrPathRenderer::IsStrokeHairlineOrEquivalent(style, viewMatrix, &hairlineCoverage)) { |
joshualitt | 40ded32 | 2015-05-02 07:07:17 -0700 | [diff] [blame] | 962 | newCoverage = SkScalarRoundToInt(hairlineCoverage * 0xff); |
| 963 | } |
| 964 | |
bsalomon | f170309 | 2016-06-29 18:41:53 -0700 | [diff] [blame] | 965 | return new AAHairlineBatch(color, newCoverage, viewMatrix, path, devClipBounds); |
joshualitt | 40ded32 | 2015-05-02 07:07:17 -0700 | [diff] [blame] | 966 | } |
| 967 | |
bsalomon | 0aff2fa | 2015-07-31 06:48:27 -0700 | [diff] [blame] | 968 | bool GrAAHairLinePathRenderer::onDrawPath(const DrawPathArgs& args) { |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 969 | GR_AUDIT_TRAIL_AUTO_FRAME(args.fRenderTargetContext->auditTrail(), |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 970 | "GrAAHairlinePathRenderer::onDrawPath"); |
Brian Osman | 1105224 | 2016-10-27 14:47:55 -0400 | [diff] [blame] | 971 | SkASSERT(!args.fRenderTargetContext->isUnifiedMultisampled()); |
csmartdalton | ecbc12b | 2016-06-08 10:08:43 -0700 | [diff] [blame] | 972 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 973 | SkIRect devClipBounds; |
Robert Phillips | 93f1633 | 2016-11-23 19:37:13 -0500 | [diff] [blame] | 974 | args.fClip->getConservativeBounds(args.fRenderTargetContext->worstCaseWidth(), |
| 975 | args.fRenderTargetContext->worstCaseHeight(), |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 976 | &devClipBounds); |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 977 | |
bsalomon | 8acedde | 2016-06-24 10:42:16 -0700 | [diff] [blame] | 978 | SkPath path; |
| 979 | args.fShape->asPath(&path); |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 980 | sk_sp<GrDrawBatch> batch(create_hairline_batch(args.fPaint->getColor(), |
| 981 | *args.fViewMatrix, path, |
| 982 | args.fShape->style(), devClipBounds)); |
robertphillips | 976f5f0 | 2016-06-03 10:59:20 -0700 | [diff] [blame] | 983 | |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 984 | GrPipelineBuilder pipelineBuilder(*args.fPaint); |
| 985 | pipelineBuilder.setUserStencil(args.fUserStencilSettings); |
Hal Canary | 144caf5 | 2016-11-07 17:57:18 -0500 | [diff] [blame] | 986 | args.fRenderTargetContext->drawBatch(pipelineBuilder, *args.fClip, batch.get()); |
bsalomon | bb24383 | 2016-07-22 07:10:19 -0700 | [diff] [blame] | 987 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 988 | return true; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 989 | } |
joshualitt | 40ded32 | 2015-05-02 07:07:17 -0700 | [diff] [blame] | 990 | |
| 991 | /////////////////////////////////////////////////////////////////////////////////////////////////// |
| 992 | |
| 993 | #ifdef GR_TEST_UTILS |
| 994 | |
bsalomon | abd30f5 | 2015-08-13 13:34:48 -0700 | [diff] [blame] | 995 | DRAW_BATCH_TEST_DEFINE(AAHairlineBatch) { |
joshualitt | 40ded32 | 2015-05-02 07:07:17 -0700 | [diff] [blame] | 996 | GrColor color = GrRandomColor(random); |
| 997 | SkMatrix viewMatrix = GrTest::TestMatrix(random); |
joshualitt | 40ded32 | 2015-05-02 07:07:17 -0700 | [diff] [blame] | 998 | SkPath path = GrTest::TestPath(random); |
| 999 | SkIRect devClipBounds; |
| 1000 | devClipBounds.setEmpty(); |
bsalomon | 6663acf | 2016-05-10 09:14:17 -0700 | [diff] [blame] | 1001 | return create_hairline_batch(color, viewMatrix, path, GrStyle::SimpleHairline(), devClipBounds); |
joshualitt | 40ded32 | 2015-05-02 07:07:17 -0700 | [diff] [blame] | 1002 | } |
| 1003 | |
| 1004 | #endif |