| 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 | |
| 10 | #include "GrContext.h" |
| tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 11 | #include "GrDrawState.h" |
| bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 12 | #include "GrDrawTargetCaps.h" |
| commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 13 | #include "GrEffect.h" |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 14 | #include "GrGpu.h" |
| 15 | #include "GrIndexBuffer.h" |
| bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 16 | #include "GrPathUtils.h" |
| commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 17 | #include "GrTBackendEffectFactory.h" |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 18 | #include "SkGeometry.h" |
| sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 19 | #include "SkStroke.h" |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 20 | #include "SkTemplates.h" |
| 21 | |
| commit-bot@chromium.org | 07e1c3f | 2013-08-22 20:41:15 +0000 | [diff] [blame] | 22 | #include "effects/GrBezierEffect.h" |
| bsalomon@google.com | 4647f90 | 2013-03-26 14:45:27 +0000 | [diff] [blame] | 23 | |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 24 | namespace { |
| 25 | // quadratics are rendered as 5-sided polys in order to bound the |
| 26 | // 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] | 27 | // bloat_quad. Quadratics and conics share an index buffer |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 28 | static const int kVertsPerQuad = 5; |
| 29 | static const int kIdxsPerQuad = 9; |
| 30 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 31 | static const int kVertsPerLineSeg = 6; |
| 32 | static const int kIdxsPerLineSeg = 12; |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 33 | |
| 34 | static const int kNumQuadsInIdxBuffer = 256; |
| 35 | static const size_t kQuadIdxSBufize = kIdxsPerQuad * |
| 36 | sizeof(uint16_t) * |
| 37 | kNumQuadsInIdxBuffer; |
| 38 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 39 | static const int kNumLineSegsInIdxBuffer = 256; |
| 40 | static const size_t kLineSegIdxSBufize = kIdxsPerLineSeg * |
| 41 | sizeof(uint16_t) * |
| 42 | kNumLineSegsInIdxBuffer; |
| 43 | |
| 44 | static bool push_quad_index_data(GrIndexBuffer* qIdxBuffer) { |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 45 | uint16_t* data = (uint16_t*) qIdxBuffer->lock(); |
| 46 | bool tempData = NULL == data; |
| 47 | if (tempData) { |
| tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 48 | data = SkNEW_ARRAY(uint16_t, kNumQuadsInIdxBuffer * kIdxsPerQuad); |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 49 | } |
| 50 | for (int i = 0; i < kNumQuadsInIdxBuffer; ++i) { |
| 51 | |
| 52 | // Each quadratic is rendered as a five sided polygon. This poly bounds |
| 53 | // the quadratic's bounding triangle but has been expanded so that the |
| 54 | // 1-pixel wide area around the curve is inside the poly. |
| 55 | // If a,b,c are the original control points then the poly a0,b0,c0,c1,a1 |
| 56 | // that is rendered would look like this: |
| 57 | // b0 |
| 58 | // b |
| 59 | // |
| 60 | // a0 c0 |
| 61 | // a c |
| 62 | // a1 c1 |
| bsalomon@google.com | 0e5104c | 2012-04-10 16:20:41 +0000 | [diff] [blame] | 63 | // Each is drawn as three triangles specified by these 9 indices: |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 64 | int baseIdx = i * kIdxsPerQuad; |
| 65 | uint16_t baseVert = (uint16_t)(i * kVertsPerQuad); |
| 66 | data[0 + baseIdx] = baseVert + 0; // a0 |
| 67 | data[1 + baseIdx] = baseVert + 1; // a1 |
| 68 | data[2 + baseIdx] = baseVert + 2; // b0 |
| 69 | data[3 + baseIdx] = baseVert + 2; // b0 |
| 70 | data[4 + baseIdx] = baseVert + 4; // c1 |
| 71 | data[5 + baseIdx] = baseVert + 3; // c0 |
| 72 | data[6 + baseIdx] = baseVert + 1; // a1 |
| 73 | data[7 + baseIdx] = baseVert + 4; // c1 |
| 74 | data[8 + baseIdx] = baseVert + 2; // b0 |
| 75 | } |
| 76 | if (tempData) { |
| 77 | bool ret = qIdxBuffer->updateData(data, kQuadIdxSBufize); |
| 78 | delete[] data; |
| 79 | return ret; |
| 80 | } else { |
| 81 | qIdxBuffer->unlock(); |
| 82 | return true; |
| 83 | } |
| 84 | } |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 85 | |
| 86 | static bool push_line_index_data(GrIndexBuffer* lIdxBuffer) { |
| 87 | uint16_t* data = (uint16_t*) lIdxBuffer->lock(); |
| 88 | bool tempData = NULL == data; |
| 89 | if (tempData) { |
| 90 | data = SkNEW_ARRAY(uint16_t, kNumLineSegsInIdxBuffer * kIdxsPerLineSeg); |
| 91 | } |
| 92 | for (int i = 0; i < kNumLineSegsInIdxBuffer; ++i) { |
| 93 | // Each line segment is rendered as two quads, with alpha = 1 along the |
| 94 | // spine of the segment, and alpha = 0 along the outer edges, represented |
| 95 | // horizontally (i.e., the line equation is t*(p1-p0) + p0) |
| 96 | // |
| 97 | // p4 p5 |
| 98 | // p0 p1 |
| 99 | // p2 p3 |
| 100 | // |
| 101 | // Each is drawn as four triangles specified by these 12 indices: |
| 102 | int baseIdx = i * kIdxsPerLineSeg; |
| 103 | uint16_t baseVert = (uint16_t)(i * kVertsPerLineSeg); |
| 104 | data[0 + baseIdx] = baseVert + 0; // p0 |
| 105 | data[1 + baseIdx] = baseVert + 1; // p1 |
| 106 | data[2 + baseIdx] = baseVert + 2; // p2 |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 107 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 108 | data[3 + baseIdx] = baseVert + 2; // p2 |
| 109 | data[4 + baseIdx] = baseVert + 1; // p1 |
| 110 | data[5 + baseIdx] = baseVert + 3; // p3 |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 111 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 112 | data[6 + baseIdx] = baseVert + 0; // p0 |
| 113 | data[7 + baseIdx] = baseVert + 5; // p5 |
| 114 | data[8 + baseIdx] = baseVert + 1; // p1 |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 115 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 116 | data[9 + baseIdx] = baseVert + 0; // p0 |
| 117 | data[10+ baseIdx] = baseVert + 4; // p4 |
| 118 | data[11+ baseIdx] = baseVert + 5; // p5 |
| 119 | } |
| 120 | if (tempData) { |
| 121 | bool ret = lIdxBuffer->updateData(data, kLineSegIdxSBufize); |
| 122 | delete[] data; |
| 123 | return ret; |
| 124 | } else { |
| 125 | lIdxBuffer->unlock(); |
| 126 | return true; |
| 127 | } |
| 128 | } |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | GrPathRenderer* GrAAHairLinePathRenderer::Create(GrContext* context) { |
| bsalomon@google.com | a8a6a32 | 2011-09-23 14:19:58 +0000 | [diff] [blame] | 132 | GrGpu* gpu = context->getGpu(); |
| 133 | GrIndexBuffer* qIdxBuf = gpu->createIndexBuffer(kQuadIdxSBufize, false); |
| 134 | SkAutoTUnref<GrIndexBuffer> qIdxBuffer(qIdxBuf); |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 135 | if (NULL == qIdxBuf || !push_quad_index_data(qIdxBuf)) { |
| 136 | return NULL; |
| 137 | } |
| 138 | GrIndexBuffer* lIdxBuf = gpu->createIndexBuffer(kLineSegIdxSBufize, false); |
| 139 | SkAutoTUnref<GrIndexBuffer> lIdxBuffer(lIdxBuf); |
| 140 | if (NULL == lIdxBuf || !push_line_index_data(lIdxBuf)) { |
| bsalomon@google.com | a8a6a32 | 2011-09-23 14:19:58 +0000 | [diff] [blame] | 141 | return NULL; |
| 142 | } |
| tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 143 | return SkNEW_ARGS(GrAAHairLinePathRenderer, |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 144 | (context, lIdxBuf, qIdxBuf)); |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | GrAAHairLinePathRenderer::GrAAHairLinePathRenderer( |
| 148 | const GrContext* context, |
| 149 | const GrIndexBuffer* linesIndexBuffer, |
| 150 | const GrIndexBuffer* quadsIndexBuffer) { |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 151 | fLinesIndexBuffer = linesIndexBuffer; |
| 152 | linesIndexBuffer->ref(); |
| 153 | fQuadsIndexBuffer = quadsIndexBuffer; |
| 154 | quadsIndexBuffer->ref(); |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | GrAAHairLinePathRenderer::~GrAAHairLinePathRenderer() { |
| 158 | fLinesIndexBuffer->unref(); |
| 159 | fQuadsIndexBuffer->unref(); |
| 160 | } |
| 161 | |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 162 | namespace { |
| 163 | |
| bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 164 | #define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true> |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 165 | |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 166 | // Takes 178th time of logf on Z600 / VC2010 |
| 167 | int get_float_exp(float x) { |
| 168 | GR_STATIC_ASSERT(sizeof(int) == sizeof(float)); |
| commit-bot@chromium.org | 515dcd3 | 2013-08-28 14:17:03 +0000 | [diff] [blame] | 169 | #ifdef SK_DEBUG |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 170 | static bool tested; |
| 171 | if (!tested) { |
| 172 | tested = true; |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 173 | SkASSERT(get_float_exp(0.25f) == -2); |
| 174 | SkASSERT(get_float_exp(0.3f) == -2); |
| 175 | SkASSERT(get_float_exp(0.5f) == -1); |
| 176 | SkASSERT(get_float_exp(1.f) == 0); |
| 177 | SkASSERT(get_float_exp(2.f) == 1); |
| 178 | SkASSERT(get_float_exp(2.5f) == 1); |
| 179 | SkASSERT(get_float_exp(8.f) == 3); |
| 180 | SkASSERT(get_float_exp(100.f) == 6); |
| 181 | SkASSERT(get_float_exp(1000.f) == 9); |
| 182 | SkASSERT(get_float_exp(1024.f) == 10); |
| 183 | SkASSERT(get_float_exp(3000000.f) == 21); |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 184 | } |
| 185 | #endif |
| bsalomon@google.com | 2ec7280 | 2011-09-21 21:46:03 +0000 | [diff] [blame] | 186 | const int* iptr = (const int*)&x; |
| 187 | return (((*iptr) & 0x7f800000) >> 23) - 127; |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 188 | } |
| 189 | |
| egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 190 | // Uses the max curvature function for quads to estimate |
| 191 | // where to chop the conic. If the max curvature is not |
| 192 | // found along the curve segment it will return 1 and |
| egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 193 | // 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] | 194 | // and dst[1] are the two new conics. |
| egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 195 | 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] | 196 | SkScalar t = SkFindQuadMaxCurvature(src); |
| 197 | if (t == 0) { |
| 198 | if (dst) { |
| 199 | dst[0].set(src, weight); |
| 200 | } |
| 201 | return 1; |
| 202 | } else { |
| 203 | if (dst) { |
| 204 | SkConic conic; |
| 205 | conic.set(src, weight); |
| 206 | conic.chopAt(t, dst); |
| 207 | } |
| 208 | return 2; |
| 209 | } |
| 210 | } |
| 211 | |
| egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 212 | // Calls split_conic on the entire conic and then once more on each subsection. |
| 213 | // Most cases will result in either 1 conic (chop point is not within t range) |
| 214 | // or 3 points (split once and then one subsection is split again). |
| 215 | int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) { |
| 216 | SkConic dstTemp[2]; |
| 217 | int conicCnt = split_conic(src, dstTemp, weight); |
| 218 | if (2 == conicCnt) { |
| 219 | int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW); |
| 220 | conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW); |
| 221 | } else { |
| 222 | dst[0] = dstTemp[0]; |
| 223 | } |
| 224 | return conicCnt; |
| 225 | } |
| 226 | |
| egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 227 | // returns 0 if quad/conic is degen or close to it |
| 228 | // in this case approx the path with lines |
| 229 | // otherwise returns 1 |
| 230 | int is_degen_quad_or_conic(const SkPoint p[3]) { |
| 231 | static const SkScalar gDegenerateToLineTol = SK_Scalar1; |
| 232 | static const SkScalar gDegenerateToLineTolSqd = |
| 233 | SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol); |
| 234 | |
| 235 | if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd || |
| 236 | p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) { |
| 237 | return 1; |
| 238 | } |
| 239 | |
| 240 | SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]); |
| 241 | if (dsqd < gDegenerateToLineTolSqd) { |
| 242 | return 1; |
| 243 | } |
| 244 | |
| 245 | if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) { |
| 246 | return 1; |
| 247 | } |
| 248 | return 0; |
| 249 | } |
| 250 | |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 251 | // we subdivide the quads to avoid huge overfill |
| 252 | // if it returns -1 then should be drawn as lines |
| 253 | int num_quad_subdivs(const SkPoint p[3]) { |
| 254 | static const SkScalar gDegenerateToLineTol = SK_Scalar1; |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 255 | static const SkScalar gDegenerateToLineTolSqd = |
| bsalomon@google.com | 46a2a1e | 2011-09-06 22:10:52 +0000 | [diff] [blame] | 256 | SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol); |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 257 | |
| bsalomon@google.com | 46a2a1e | 2011-09-06 22:10:52 +0000 | [diff] [blame] | 258 | if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd || |
| 259 | p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) { |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 260 | return -1; |
| 261 | } |
| bsalomon@google.com | 46a2a1e | 2011-09-06 22:10:52 +0000 | [diff] [blame] | 262 | |
| bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 263 | SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]); |
| bsalomon@google.com | 46a2a1e | 2011-09-06 22:10:52 +0000 | [diff] [blame] | 264 | if (dsqd < gDegenerateToLineTolSqd) { |
| 265 | return -1; |
| 266 | } |
| 267 | |
| 268 | if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) { |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 269 | return -1; |
| 270 | } |
| 271 | |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 272 | // tolerance of triangle height in pixels |
| 273 | // tuned on windows Quadro FX 380 / Z600 |
| 274 | // trade off of fill vs cpu time on verts |
| 275 | // maybe different when do this using gpu (geo or tess shaders) |
| 276 | static const SkScalar gSubdivTol = 175 * SK_Scalar1; |
| 277 | |
| robertphillips@google.com | 7460b37 | 2012-04-25 16:54:51 +0000 | [diff] [blame] | 278 | if (dsqd <= SkScalarMul(gSubdivTol, gSubdivTol)) { |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 279 | return 0; |
| 280 | } else { |
| robertphillips@google.com | 87379e1 | 2013-03-29 12:11:10 +0000 | [diff] [blame] | 281 | static const int kMaxSub = 4; |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 282 | // subdividing the quad reduces d by 4. so we want x = log4(d/tol) |
| 283 | // = log4(d*d/tol*tol)/2 |
| 284 | // = log2(d*d/tol*tol) |
| 285 | |
| 286 | #ifdef SK_SCALAR_IS_FLOAT |
| 287 | // +1 since we're ignoring the mantissa contribution. |
| 288 | int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1; |
| 289 | log = GrMin(GrMax(0, log), kMaxSub); |
| 290 | return log; |
| 291 | #else |
| robertphillips@google.com | 7460b37 | 2012-04-25 16:54:51 +0000 | [diff] [blame] | 292 | SkScalar log = SkScalarLog( |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 293 | SkScalarDiv(dsqd, |
| robertphillips@google.com | 7460b37 | 2012-04-25 16:54:51 +0000 | [diff] [blame] | 294 | SkScalarMul(gSubdivTol, gSubdivTol))); |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 295 | static const SkScalar conv = SkScalarInvert(SkScalarLog(2)); |
| 296 | log = SkScalarMul(log, conv); |
| 297 | return GrMin(GrMax(0, SkScalarCeilToInt(log)),kMaxSub); |
| 298 | #endif |
| 299 | } |
| 300 | } |
| 301 | |
| bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 302 | /** |
| 303 | * Generates the lines and quads to be rendered. Lines are always recorded in |
| 304 | * device space. We will do a device space bloat to account for the 1pixel |
| 305 | * thickness. |
| 306 | * Quads are recorded in device space unless m contains |
| 307 | * perspective, then in they are in src space. We do this because we will |
| 308 | * subdivide large quads to reduce over-fill. This subdivision has to be |
| 309 | * performed before applying the perspective matrix. |
| 310 | */ |
| 311 | int generate_lines_and_quads(const SkPath& path, |
| 312 | const SkMatrix& m, |
| commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 313 | const SkIRect& devClipBounds, |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 314 | GrAAHairLinePathRenderer::PtArray* lines, |
| 315 | GrAAHairLinePathRenderer::PtArray* quads, |
| 316 | GrAAHairLinePathRenderer::PtArray* conics, |
| 317 | GrAAHairLinePathRenderer::IntArray* quadSubdivCnts, |
| 318 | GrAAHairLinePathRenderer::FloatArray* conicWeights) { |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 319 | SkPath::Iter iter(path, false); |
| 320 | |
| 321 | int totalQuadCount = 0; |
| commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame] | 322 | SkRect bounds; |
| 323 | SkIRect ibounds; |
| bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 324 | |
| 325 | bool persp = m.hasPerspective(); |
| 326 | |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 327 | for (;;) { |
| commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 328 | GrPoint pathPts[4]; |
| bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 329 | GrPoint devPts[4]; |
| commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 330 | SkPath::Verb verb = iter.next(pathPts); |
| bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 331 | switch (verb) { |
| egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 332 | case SkPath::kConic_Verb: { |
| egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 333 | SkConic dst[4]; |
| 334 | // We chop the conics to create tighter clipping to hide error |
| 335 | // that appears near max curvature of very thin conics. Thin |
| 336 | // hyperbolas with high weight still show error. |
| egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 337 | int conicCnt = chop_conic(pathPts, dst, iter.conicWeight()); |
| 338 | for (int i = 0; i < conicCnt; ++i) { |
| 339 | SkPoint* chopPnts = dst[i].fPts; |
| 340 | m.mapPoints(devPts, chopPnts, 3); |
| 341 | bounds.setBounds(devPts, 3); |
| 342 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 343 | bounds.roundOut(&ibounds); |
| 344 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
| 345 | if (is_degen_quad_or_conic(devPts)) { |
| 346 | SkPoint* pts = lines->push_back_n(4); |
| 347 | pts[0] = devPts[0]; |
| 348 | pts[1] = devPts[1]; |
| 349 | pts[2] = devPts[1]; |
| 350 | pts[3] = devPts[2]; |
| 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; |
| 359 | } |
| 360 | } |
| 361 | } |
| reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 362 | break; |
| egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 363 | } |
| 364 | case SkPath::kMove_Verb: |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 365 | break; |
| bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 366 | case SkPath::kLine_Verb: |
| commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 367 | m.mapPoints(devPts, pathPts, 2); |
| bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 368 | bounds.setBounds(devPts, 2); |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 369 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 370 | bounds.roundOut(&ibounds); |
| robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 371 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
| bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 372 | SkPoint* pts = lines->push_back_n(2); |
| 373 | pts[0] = devPts[0]; |
| 374 | pts[1] = devPts[1]; |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 375 | } |
| 376 | break; |
| commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 377 | case SkPath::kQuad_Verb: { |
| 378 | SkPoint choppedPts[5]; |
| 379 | // Chopping the quad helps when the quad is either degenerate or nearly degenerate. |
| 380 | // When it is degenerate it allows the approximation with lines to work since the |
| 381 | // chop point (if there is one) will be at the parabola's vertex. In the nearly |
| 382 | // degenerate the QuadUVMatrix computed for the points is almost singular which |
| 383 | // can cause rendering artifacts. |
| 384 | int n = SkChopQuadAtMaxCurvature(pathPts, choppedPts); |
| 385 | for (int i = 0; i < n; ++i) { |
| 386 | SkPoint* quadPts = choppedPts + i * 2; |
| 387 | m.mapPoints(devPts, quadPts, 3); |
| 388 | bounds.setBounds(devPts, 3); |
| 389 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 390 | bounds.roundOut(&ibounds); |
| 391 | |
| 392 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
| 393 | int subdiv = num_quad_subdivs(devPts); |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 394 | SkASSERT(subdiv >= -1); |
| commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 395 | if (-1 == subdiv) { |
| 396 | SkPoint* pts = lines->push_back_n(4); |
| 397 | pts[0] = devPts[0]; |
| 398 | pts[1] = devPts[1]; |
| 399 | pts[2] = devPts[1]; |
| 400 | pts[3] = devPts[2]; |
| 401 | } else { |
| 402 | // when in perspective keep quads in src space |
| 403 | SkPoint* qPts = persp ? quadPts : devPts; |
| 404 | SkPoint* pts = quads->push_back_n(3); |
| 405 | pts[0] = qPts[0]; |
| 406 | pts[1] = qPts[1]; |
| 407 | pts[2] = qPts[2]; |
| 408 | quadSubdivCnts->push_back() = subdiv; |
| 409 | totalQuadCount += 1 << subdiv; |
| 410 | } |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 411 | } |
| 412 | } |
| bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 413 | break; |
| commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 414 | } |
| bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 415 | case SkPath::kCubic_Verb: |
| commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 416 | m.mapPoints(devPts, pathPts, 4); |
| bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 417 | bounds.setBounds(devPts, 4); |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 418 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 419 | bounds.roundOut(&ibounds); |
| robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 420 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
| bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 421 | PREALLOC_PTARRAY(32) q; |
| bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 422 | // we don't need a direction if we aren't constraining the subdivision |
| 423 | static const SkPath::Direction kDummyDir = SkPath::kCCW_Direction; |
| bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 424 | // We convert cubics to quadratics (for now). |
| 425 | // In perspective have to do conversion in src space. |
| bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 426 | if (persp) { |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 427 | SkScalar tolScale = |
| bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 428 | GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m, |
| 429 | path.getBounds()); |
| commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 430 | GrPathUtils::convertCubicToQuads(pathPts, tolScale, false, kDummyDir, &q); |
| bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 431 | } else { |
| bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 432 | GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, false, kDummyDir, &q); |
| bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 433 | } |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 434 | for (int i = 0; i < q.count(); i += 3) { |
| bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 435 | SkPoint* qInDevSpace; |
| 436 | // bounds has to be calculated in device space, but q is |
| 437 | // in src space when there is perspective. |
| 438 | if (persp) { |
| 439 | m.mapPoints(devPts, &q[i], 3); |
| 440 | bounds.setBounds(devPts, 3); |
| 441 | qInDevSpace = devPts; |
| 442 | } else { |
| 443 | bounds.setBounds(&q[i], 3); |
| 444 | qInDevSpace = &q[i]; |
| 445 | } |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 446 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 447 | bounds.roundOut(&ibounds); |
| robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 448 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
| bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 449 | int subdiv = num_quad_subdivs(qInDevSpace); |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 450 | SkASSERT(subdiv >= -1); |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 451 | if (-1 == subdiv) { |
| bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 452 | SkPoint* pts = lines->push_back_n(4); |
| bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 453 | // lines should always be in device coords |
| bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 454 | pts[0] = qInDevSpace[0]; |
| 455 | pts[1] = qInDevSpace[1]; |
| 456 | pts[2] = qInDevSpace[1]; |
| 457 | pts[3] = qInDevSpace[2]; |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 458 | } else { |
| bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 459 | SkPoint* pts = quads->push_back_n(3); |
| bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 460 | // q is already in src space when there is no |
| 461 | // perspective and dev coords otherwise. |
| bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 462 | pts[0] = q[0 + i]; |
| 463 | pts[1] = q[1 + i]; |
| 464 | pts[2] = q[2 + i]; |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 465 | quadSubdivCnts->push_back() = subdiv; |
| 466 | totalQuadCount += 1 << subdiv; |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | } |
| bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 471 | break; |
| bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 472 | case SkPath::kClose_Verb: |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 473 | break; |
| bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 474 | case SkPath::kDone_Verb: |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 475 | return totalQuadCount; |
| 476 | } |
| 477 | } |
| 478 | } |
| 479 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 480 | struct LineVertex { |
| 481 | GrPoint fPos; |
| 482 | GrColor fCoverage; |
| 483 | }; |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 484 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 485 | struct BezierVertex { |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 486 | GrPoint fPos; |
| 487 | union { |
| 488 | struct { |
| egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 489 | SkScalar fK; |
| 490 | SkScalar fL; |
| 491 | SkScalar fM; |
| egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 492 | } fConic; |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 493 | GrVec fQuadCoord; |
| 494 | struct { |
| egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 495 | SkScalar fBogus[4]; |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 496 | }; |
| 497 | }; |
| 498 | }; |
| egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 499 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 500 | GR_STATIC_ASSERT(sizeof(BezierVertex) == 3 * sizeof(GrPoint)); |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 501 | |
| 502 | void intersect_lines(const SkPoint& ptA, const SkVector& normA, |
| 503 | const SkPoint& ptB, const SkVector& normB, |
| 504 | SkPoint* result) { |
| 505 | |
| 506 | SkScalar lineAW = -normA.dot(ptA); |
| 507 | SkScalar lineBW = -normB.dot(ptB); |
| 508 | |
| 509 | SkScalar wInv = SkScalarMul(normA.fX, normB.fY) - |
| egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 510 | SkScalarMul(normA.fY, normB.fX); |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 511 | wInv = SkScalarInvert(wInv); |
| 512 | |
| 513 | result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY); |
| 514 | result->fX = SkScalarMul(result->fX, wInv); |
| rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 515 | |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 516 | result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW); |
| 517 | result->fY = SkScalarMul(result->fY, wInv); |
| 518 | } |
| 519 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 520 | void set_uv_quad(const SkPoint qpts[3], BezierVertex verts[kVertsPerQuad]) { |
| egdaniel@google.com | 34b05ca | 2013-08-05 20:43:12 +0000 | [diff] [blame] | 521 | // this should be in the src space, not dev coords, when we have perspective |
| 522 | GrPathUtils::QuadUVMatrix DevToUV(qpts); |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 523 | DevToUV.apply<kVertsPerQuad, sizeof(BezierVertex), sizeof(GrPoint)>(verts); |
| egdaniel@google.com | 34b05ca | 2013-08-05 20:43:12 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 526 | void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice, |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 527 | const SkMatrix* toSrc, BezierVertex verts[kVertsPerQuad], |
| bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 528 | SkRect* devBounds) { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 529 | SkASSERT(!toDevice == !toSrc); |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 530 | // original quad is specified by tri a,b,c |
| bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 531 | SkPoint a = qpts[0]; |
| 532 | SkPoint b = qpts[1]; |
| 533 | SkPoint c = qpts[2]; |
| 534 | |
| bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 535 | if (toDevice) { |
| 536 | toDevice->mapPoints(&a, 1); |
| 537 | toDevice->mapPoints(&b, 1); |
| 538 | toDevice->mapPoints(&c, 1); |
| 539 | } |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 540 | // make a new poly where we replace a and c by a 1-pixel wide edges orthog |
| 541 | // to edges ab and bc: |
| 542 | // |
| 543 | // before | after |
| 544 | // | b0 |
| 545 | // b | |
| 546 | // | |
| 547 | // | a0 c0 |
| 548 | // a c | a1 c1 |
| 549 | // |
| 550 | // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c, |
| 551 | // respectively. |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 552 | BezierVertex& a0 = verts[0]; |
| 553 | BezierVertex& a1 = verts[1]; |
| 554 | BezierVertex& b0 = verts[2]; |
| 555 | BezierVertex& c0 = verts[3]; |
| 556 | BezierVertex& c1 = verts[4]; |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 557 | |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 558 | SkVector ab = b; |
| 559 | ab -= a; |
| 560 | SkVector ac = c; |
| 561 | ac -= a; |
| 562 | SkVector cb = b; |
| 563 | cb -= c; |
| 564 | |
| 565 | // We should have already handled degenerates |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 566 | SkASSERT(ab.length() > 0 && cb.length() > 0); |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 567 | |
| 568 | ab.normalize(); |
| 569 | SkVector abN; |
| 570 | abN.setOrthog(ab, SkVector::kLeft_Side); |
| 571 | if (abN.dot(ac) > 0) { |
| 572 | abN.negate(); |
| 573 | } |
| 574 | |
| 575 | cb.normalize(); |
| 576 | SkVector cbN; |
| 577 | cbN.setOrthog(cb, SkVector::kLeft_Side); |
| 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 | |
| 587 | c0.fPos = c; |
| 588 | c0.fPos += cbN; |
| 589 | c1.fPos = c; |
| 590 | c1.fPos -= cbN; |
| 591 | |
| 592 | intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos); |
| commit-bot@chromium.org | b8bd6cb | 2013-09-03 14:56:17 +0000 | [diff] [blame] | 593 | devBounds->growToInclude(&verts[0].fPos, sizeof(BezierVertex), kVertsPerQuad); |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 594 | |
| bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 595 | if (toSrc) { |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 596 | toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(BezierVertex), kVertsPerQuad); |
| bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 597 | } |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 598 | } |
| 599 | |
| egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 600 | // Equations based off of Loop-Blinn Quadratic GPU Rendering |
| egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 601 | // Input Parametric: |
| 602 | // 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) |
| 603 | // Output Implicit: |
| egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 604 | // f(x, y, w) = f(P) = K^2 - LM |
| 605 | // 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] | 606 | // k, l, m are calculated in function GrPathUtils::getConicKLM |
| 607 | void set_conic_coeffs(const SkPoint p[3], BezierVertex verts[kVertsPerQuad], |
| 608 | const SkScalar weight) { |
| 609 | SkScalar klm[9]; |
| egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 610 | |
| commit-bot@chromium.org | 1394840 | 2013-08-20 17:55:43 +0000 | [diff] [blame] | 611 | GrPathUtils::getConicKLM(p, weight, klm); |
| egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 612 | |
| 613 | for (int i = 0; i < kVertsPerQuad; ++i) { |
| egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 614 | const SkPoint pnt = verts[i].fPos; |
| commit-bot@chromium.org | 1394840 | 2013-08-20 17:55:43 +0000 | [diff] [blame] | 615 | verts[i].fConic.fK = pnt.fX * klm[0] + pnt.fY * klm[1] + klm[2]; |
| 616 | verts[i].fConic.fL = pnt.fX * klm[3] + pnt.fY * klm[4] + klm[5]; |
| 617 | 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] | 618 | } |
| 619 | } |
| 620 | |
| 621 | void add_conics(const SkPoint p[3], |
| commit-bot@chromium.org | 1394840 | 2013-08-20 17:55:43 +0000 | [diff] [blame] | 622 | const SkScalar weight, |
| egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 623 | const SkMatrix* toDevice, |
| 624 | const SkMatrix* toSrc, |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 625 | BezierVertex** vert, |
| egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 626 | SkRect* devBounds) { |
| 627 | bloat_quad(p, toDevice, toSrc, *vert, devBounds); |
| 628 | set_conic_coeffs(p, *vert, weight); |
| 629 | *vert += kVertsPerQuad; |
| 630 | } |
| 631 | |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 632 | void add_quads(const SkPoint p[3], |
| 633 | int subdiv, |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 634 | const SkMatrix* toDevice, |
| 635 | const SkMatrix* toSrc, |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 636 | BezierVertex** vert, |
| bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 637 | SkRect* devBounds) { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 638 | SkASSERT(subdiv >= 0); |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 639 | if (subdiv) { |
| 640 | SkPoint newP[5]; |
| 641 | SkChopQuadAtHalf(p, newP); |
| bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 642 | add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert, devBounds); |
| 643 | add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert, devBounds); |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 644 | } else { |
| bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 645 | bloat_quad(p, toDevice, toSrc, *vert, devBounds); |
| egdaniel@google.com | 34b05ca | 2013-08-05 20:43:12 +0000 | [diff] [blame] | 646 | set_uv_quad(p, *vert); |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 647 | *vert += kVertsPerQuad; |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | void add_line(const SkPoint p[2], |
| 652 | int rtHeight, |
| bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 653 | const SkMatrix* toSrc, |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 654 | GrColor coverage, |
| 655 | LineVertex** vert) { |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 656 | const SkPoint& a = p[0]; |
| 657 | const SkPoint& b = p[1]; |
| 658 | |
| 659 | SkVector orthVec = b; |
| 660 | orthVec -= a; |
| 661 | |
| 662 | if (orthVec.setLength(SK_Scalar1)) { |
| 663 | orthVec.setOrthog(orthVec); |
| 664 | |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 665 | for (int i = 0; i < kVertsPerLineSeg; ++i) { |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 666 | (*vert)[i].fPos = (i & 0x1) ? b : a; |
| 667 | if (i & 0x2) { |
| jvanverth@google.com | 5e2d270 | 2013-08-14 14:29:29 +0000 | [diff] [blame] | 668 | (*vert)[i].fPos += orthVec; |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 669 | (*vert)[i].fCoverage = 0; |
| 670 | } else if (i & 0x4) { |
| 671 | (*vert)[i].fPos -= orthVec; |
| 672 | (*vert)[i].fCoverage = 0; |
| 673 | } else { |
| 674 | (*vert)[i].fCoverage = coverage; |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 675 | } |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 676 | } |
| bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 677 | if (NULL != toSrc) { |
| 678 | toSrc->mapPointsWithStride(&(*vert)->fPos, |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 679 | sizeof(LineVertex), |
| bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 680 | kVertsPerLineSeg); |
| 681 | } |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 682 | } else { |
| 683 | // just make it degenerate and likely offscreen |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 684 | for (int i = 0; i < kVertsPerLineSeg; ++i) { |
| 685 | (*vert)[i].fPos.set(SK_ScalarMax, SK_ScalarMax); |
| 686 | } |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | *vert += kVertsPerLineSeg; |
| 690 | } |
| 691 | |
| 692 | } |
| 693 | |
| commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 694 | /////////////////////////////////////////////////////////////////////////////// |
| 695 | |
| robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 696 | namespace { |
| 697 | |
| 698 | // position + edge |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 699 | extern const GrVertexAttrib gHairlineBezierAttribs[] = { |
| robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 700 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
| 701 | {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding} |
| 702 | }; |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 703 | |
| 704 | // position + coverage |
| 705 | extern const GrVertexAttrib gHairlineLineAttribs[] = { |
| 706 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
| 707 | {kVec4ub_GrVertexAttribType, sizeof(GrPoint), kCoverage_GrVertexAttribBinding}, |
| robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 708 | }; |
| 709 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 710 | }; |
| 711 | |
| 712 | bool GrAAHairLinePathRenderer::createLineGeom( |
| bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 713 | const SkPath& path, |
| bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 714 | GrDrawTarget* target, |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 715 | const PtArray& lines, |
| 716 | int lineCnt, |
| bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 717 | GrDrawTarget::AutoReleaseGeometry* arg, |
| 718 | SkRect* devBounds) { |
| jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 719 | GrDrawState* drawState = target->drawState(); |
| 720 | int rtHeight = drawState->getRenderTarget()->height(); |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 721 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 722 | const SkMatrix& viewM = drawState->getViewMatrix(); |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 723 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 724 | devBounds->outset(SK_Scalar1, SK_Scalar1); |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 725 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 726 | int vertCnt = kVertsPerLineSeg * lineCnt; |
| 727 | |
| 728 | target->drawState()->setVertexAttribs<gHairlineLineAttribs>(SK_ARRAY_COUNT(gHairlineLineAttribs)); |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 729 | SkASSERT(sizeof(LineVertex) == target->getDrawState().getVertexSize()); |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 730 | |
| 731 | if (!arg->set(target, vertCnt, 0)) { |
| 732 | return false; |
| 733 | } |
| 734 | |
| 735 | LineVertex* verts = reinterpret_cast<LineVertex*>(arg->vertices()); |
| 736 | |
| 737 | const SkMatrix* toSrc = NULL; |
| 738 | SkMatrix ivm; |
| 739 | |
| 740 | if (viewM.hasPerspective()) { |
| 741 | if (viewM.invert(&ivm)) { |
| 742 | toSrc = &ivm; |
| 743 | } |
| 744 | } |
| commit-bot@chromium.org | b8bd6cb | 2013-09-03 14:56:17 +0000 | [diff] [blame] | 745 | devBounds->set(lines.begin(), lines.count()); |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 746 | for (int i = 0; i < lineCnt; ++i) { |
| 747 | add_line(&lines[2*i], rtHeight, toSrc, drawState->getCoverage(), &verts); |
| 748 | } |
| commit-bot@chromium.org | b8bd6cb | 2013-09-03 14:56:17 +0000 | [diff] [blame] | 749 | // All the verts computed by add_line are within unit distance of the end points. Add a little |
| 750 | // extra to account for vector normalization precision. |
| 751 | static const SkScalar kOutset = SK_Scalar1 + SK_Scalar1 / 20; |
| 752 | devBounds->outset(kOutset, kOutset); |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 753 | |
| 754 | return true; |
| 755 | } |
| 756 | |
| 757 | bool GrAAHairLinePathRenderer::createBezierGeom( |
| 758 | const SkPath& path, |
| 759 | GrDrawTarget* target, |
| 760 | const PtArray& quads, |
| 761 | int quadCnt, |
| 762 | const PtArray& conics, |
| 763 | int conicCnt, |
| 764 | const IntArray& qSubdivs, |
| 765 | const FloatArray& cWeights, |
| 766 | GrDrawTarget::AutoReleaseGeometry* arg, |
| 767 | SkRect* devBounds) { |
| 768 | GrDrawState* drawState = target->drawState(); |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 769 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 770 | const SkMatrix& viewM = drawState->getViewMatrix(); |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 771 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 772 | int vertCnt = kVertsPerQuad * quadCnt + kVertsPerQuad * conicCnt; |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 773 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 774 | target->drawState()->setVertexAttribs<gHairlineBezierAttribs>(SK_ARRAY_COUNT(gHairlineBezierAttribs)); |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 775 | SkASSERT(sizeof(BezierVertex) == target->getDrawState().getVertexSize()); |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 776 | |
| jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 777 | if (!arg->set(target, vertCnt, 0)) { |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 778 | return false; |
| 779 | } |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 780 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 781 | BezierVertex* verts = reinterpret_cast<BezierVertex*>(arg->vertices()); |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 782 | |
| bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 783 | const SkMatrix* toDevice = NULL; |
| 784 | const SkMatrix* toSrc = NULL; |
| 785 | SkMatrix ivm; |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 786 | |
| bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 787 | if (viewM.hasPerspective()) { |
| 788 | if (viewM.invert(&ivm)) { |
| 789 | toDevice = &viewM; |
| 790 | toSrc = &ivm; |
| 791 | } |
| 792 | } |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 793 | |
| commit-bot@chromium.org | b8bd6cb | 2013-09-03 14:56:17 +0000 | [diff] [blame] | 794 | // Seed the dev bounds with some pts known to be inside. Each quad and conic grows the bounding |
| 795 | // box to include its vertices. |
| 796 | SkPoint seedPts[2]; |
| 797 | if (quadCnt) { |
| 798 | seedPts[0] = quads[0]; |
| 799 | seedPts[1] = quads[2]; |
| 800 | } else if (conicCnt) { |
| 801 | seedPts[0] = conics[0]; |
| 802 | seedPts[1] = conics[2]; |
| 803 | } |
| 804 | if (NULL != toDevice) { |
| 805 | toDevice->mapPoints(seedPts, 2); |
| 806 | } |
| 807 | devBounds->set(seedPts[0], seedPts[1]); |
| 808 | |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 809 | int unsubdivQuadCnt = quads.count() / 3; |
| 810 | for (int i = 0; i < unsubdivQuadCnt; ++i) { |
| tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 811 | SkASSERT(qSubdivs[i] >= 0); |
| bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 812 | add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts, devBounds); |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 813 | } |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 814 | |
| egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 815 | // Start Conics |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 816 | for (int i = 0; i < conicCnt; ++i) { |
| egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 817 | add_conics(&conics[3*i], cWeights[i], toDevice, toSrc, &verts, devBounds); |
| 818 | } |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 819 | return true; |
| 820 | } |
| 821 | |
| robertphillips@google.com | 8a4fc40 | 2012-05-24 12:42:24 +0000 | [diff] [blame] | 822 | bool GrAAHairLinePathRenderer::canDrawPath(const SkPath& path, |
| sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 823 | const SkStrokeRec& stroke, |
| robertphillips@google.com | 8a4fc40 | 2012-05-24 12:42:24 +0000 | [diff] [blame] | 824 | const GrDrawTarget* target, |
| 825 | bool antiAlias) const { |
| sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 826 | if (!stroke.isHairlineStyle() || !antiAlias) { |
| bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 827 | return false; |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 828 | } |
| 829 | |
| egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 830 | if (SkPath::kLine_SegmentMask == path.getSegmentMasks() || |
| 831 | target->caps()->shaderDerivativeSupport()) { |
| 832 | return true; |
| bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 833 | } |
| egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 834 | return false; |
| bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 835 | } |
| 836 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 837 | template <class VertexType> |
| 838 | bool check_bounds(GrDrawState* drawState, const SkRect& devBounds, void* vertices, int vCount) |
| 839 | { |
| bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 840 | SkRect tolDevBounds = devBounds; |
| commit-bot@chromium.org | b8bd6cb | 2013-09-03 14:56:17 +0000 | [diff] [blame] | 841 | // The bounds ought to be tight, but in perspective the below code runs the verts |
| 842 | // through the view matrix to get back to dev coords, which can introduce imprecision. |
| 843 | if (drawState->getViewMatrix().hasPerspective()) { |
| 844 | tolDevBounds.outset(SK_Scalar1 / 1000, SK_Scalar1 / 1000); |
| 845 | } else { |
| 846 | // Non-persp matrices cause this path renderer to draw in device space. |
| 847 | SkASSERT(drawState->getViewMatrix().isIdentity()); |
| 848 | } |
| bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 849 | SkRect actualBounds; |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 850 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 851 | VertexType* verts = reinterpret_cast<VertexType*>(vertices); |
| bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 852 | bool first = true; |
| 853 | for (int i = 0; i < vCount; ++i) { |
| 854 | SkPoint pos = verts[i].fPos; |
| 855 | // This is a hack to workaround the fact that we move some degenerate segments offscreen. |
| 856 | if (SK_ScalarMax == pos.fX) { |
| 857 | continue; |
| 858 | } |
| 859 | drawState->getViewMatrix().mapPoints(&pos, 1); |
| 860 | if (first) { |
| 861 | actualBounds.set(pos.fX, pos.fY, pos.fX, pos.fY); |
| 862 | first = false; |
| 863 | } else { |
| 864 | actualBounds.growToInclude(pos.fX, pos.fY); |
| 865 | } |
| 866 | } |
| 867 | if (!first) { |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 868 | return tolDevBounds.contains(actualBounds); |
| bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 869 | } |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 870 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 871 | return true; |
| 872 | } |
| bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 873 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 874 | bool GrAAHairLinePathRenderer::onDrawPath(const SkPath& path, |
| 875 | const SkStrokeRec&, |
| 876 | GrDrawTarget* target, |
| 877 | bool antiAlias) { |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 878 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 879 | GrDrawState* drawState = target->drawState(); |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 880 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 881 | SkIRect devClipBounds; |
| 882 | target->getClip()->getConservativeBounds(drawState->getRenderTarget(), &devClipBounds); |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 883 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 884 | int lineCnt; |
| 885 | int quadCnt; |
| 886 | int conicCnt; |
| 887 | PREALLOC_PTARRAY(128) lines; |
| 888 | PREALLOC_PTARRAY(128) quads; |
| 889 | PREALLOC_PTARRAY(128) conics; |
| 890 | IntArray qSubdivs; |
| 891 | FloatArray cWeights; |
| 892 | quadCnt = generate_lines_and_quads(path, drawState->getViewMatrix(), devClipBounds, |
| 893 | &lines, &quads, &conics, &qSubdivs, &cWeights); |
| 894 | lineCnt = lines.count() / 2; |
| 895 | conicCnt = conics.count() / 3; |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 896 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 897 | // do lines first |
| commit-bot@chromium.org | b8bd6cb | 2013-09-03 14:56:17 +0000 | [diff] [blame] | 898 | if (lineCnt) { |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 899 | GrDrawTarget::AutoReleaseGeometry arg; |
| 900 | SkRect devBounds; |
| 901 | |
| 902 | if (!this->createLineGeom(path, |
| 903 | target, |
| 904 | lines, |
| 905 | lineCnt, |
| 906 | &arg, |
| 907 | &devBounds)) { |
| 908 | return false; |
| 909 | } |
| 910 | |
| 911 | GrDrawTarget::AutoStateRestore asr; |
| 912 | |
| 913 | // createGeom transforms the geometry to device space when the matrix does not have |
| 914 | // perspective. |
| 915 | if (target->getDrawState().getViewMatrix().hasPerspective()) { |
| 916 | asr.set(target, GrDrawTarget::kPreserve_ASRInit); |
| 917 | } else if (!asr.setIdentity(target, GrDrawTarget::kPreserve_ASRInit)) { |
| 918 | return false; |
| 919 | } |
| 920 | GrDrawState* drawState = target->drawState(); |
| 921 | |
| 922 | // Check devBounds |
| 923 | SkASSERT(check_bounds<LineVertex>(drawState, devBounds, arg.vertices(), |
| 924 | kVertsPerLineSeg * lineCnt)); |
| 925 | |
| 926 | { |
| 927 | GrDrawState::AutoRestoreEffects are(drawState); |
| 928 | target->setIndexSourceToBuffer(fLinesIndexBuffer); |
| 929 | int lines = 0; |
| 930 | while (lines < lineCnt) { |
| 931 | int n = GrMin(lineCnt - lines, kNumLineSegsInIdxBuffer); |
| 932 | target->drawIndexed(kTriangles_GrPrimitiveType, |
| 933 | kVertsPerLineSeg*lines, // startV |
| 934 | 0, // startI |
| 935 | kVertsPerLineSeg*n, // vCount |
| 936 | kIdxsPerLineSeg*n, |
| 937 | &devBounds); // iCount |
| 938 | lines += n; |
| 939 | } |
| bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 940 | } |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 941 | } |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 942 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 943 | // then quadratics/conics |
| commit-bot@chromium.org | b8bd6cb | 2013-09-03 14:56:17 +0000 | [diff] [blame] | 944 | if (quadCnt || conicCnt) { |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 945 | GrDrawTarget::AutoReleaseGeometry arg; |
| 946 | SkRect devBounds; |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 947 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 948 | if (!this->createBezierGeom(path, |
| 949 | target, |
| 950 | quads, |
| 951 | quadCnt, |
| 952 | conics, |
| 953 | conicCnt, |
| 954 | qSubdivs, |
| 955 | cWeights, |
| 956 | &arg, |
| 957 | &devBounds)) { |
| 958 | return false; |
| 959 | } |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 960 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 961 | GrDrawTarget::AutoStateRestore asr; |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 962 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 963 | // createGeom transforms the geometry to device space when the matrix does not have |
| 964 | // perspective. |
| 965 | if (target->getDrawState().getViewMatrix().hasPerspective()) { |
| 966 | asr.set(target, GrDrawTarget::kPreserve_ASRInit); |
| 967 | } else if (!asr.setIdentity(target, GrDrawTarget::kPreserve_ASRInit)) { |
| 968 | return false; |
| 969 | } |
| 970 | GrDrawState* drawState = target->drawState(); |
| 971 | |
| 972 | static const int kEdgeAttrIndex = 1; |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 973 | |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 974 | // Check devBounds |
| 975 | SkASSERT(check_bounds<BezierVertex>(drawState, devBounds, arg.vertices(), |
| 976 | kVertsPerQuad * quadCnt + kVertsPerQuad * conicCnt)); |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 977 | |
| commit-bot@chromium.org | 8846247 | 2013-08-23 21:01:52 +0000 | [diff] [blame] | 978 | if (quadCnt > 0) { |
| 979 | GrEffectRef* hairQuadEffect = GrQuadEffect::Create(kHairAA_GrBezierEdgeType, |
| 980 | *target->caps()); |
| 981 | SkASSERT(NULL != hairQuadEffect); |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 982 | GrDrawState::AutoRestoreEffects are(drawState); |
| 983 | target->setIndexSourceToBuffer(fQuadsIndexBuffer); |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 984 | drawState->addCoverageEffect(hairQuadEffect, kEdgeAttrIndex)->unref(); |
| commit-bot@chromium.org | 8846247 | 2013-08-23 21:01:52 +0000 | [diff] [blame] | 985 | int quads = 0; |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 986 | while (quads < quadCnt) { |
| 987 | int n = GrMin(quadCnt - quads, kNumQuadsInIdxBuffer); |
| 988 | target->drawIndexed(kTriangles_GrPrimitiveType, |
| 989 | kVertsPerQuad*quads, // startV |
| 990 | 0, // startI |
| 991 | kVertsPerQuad*n, // vCount |
| 992 | kIdxsPerQuad*n, // iCount |
| 993 | &devBounds); |
| 994 | quads += n; |
| 995 | } |
| 996 | } |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 997 | |
| commit-bot@chromium.org | 8846247 | 2013-08-23 21:01:52 +0000 | [diff] [blame] | 998 | if (conicCnt > 0) { |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 999 | GrDrawState::AutoRestoreEffects are(drawState); |
| commit-bot@chromium.org | 8846247 | 2013-08-23 21:01:52 +0000 | [diff] [blame] | 1000 | GrEffectRef* hairConicEffect = GrConicEffect::Create(kHairAA_GrBezierEdgeType, |
| 1001 | *target->caps()); |
| 1002 | SkASSERT(NULL != hairConicEffect); |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 1003 | drawState->addCoverageEffect(hairConicEffect, 1, 2)->unref(); |
| commit-bot@chromium.org | 8846247 | 2013-08-23 21:01:52 +0000 | [diff] [blame] | 1004 | int conics = 0; |
| jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 1005 | while (conics < conicCnt) { |
| 1006 | int n = GrMin(conicCnt - conics, kNumQuadsInIdxBuffer); |
| 1007 | target->drawIndexed(kTriangles_GrPrimitiveType, |
| 1008 | kVertsPerQuad*(quadCnt + conics), // startV |
| 1009 | 0, // startI |
| 1010 | kVertsPerQuad*n, // vCount |
| 1011 | kIdxsPerQuad*n, // iCount |
| 1012 | &devBounds); |
| 1013 | conics += n; |
| 1014 | } |
| egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 1015 | } |
| 1016 | } |
| skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 1017 | |
| bsalomon@google.com | 0406b9e | 2013-04-02 21:00:15 +0000 | [diff] [blame] | 1018 | target->resetIndexSource(); |
| bsalomon@google.com | 4647f90 | 2013-03-26 14:45:27 +0000 | [diff] [blame] | 1019 | |
| bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 1020 | return true; |
| bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1021 | } |