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 | |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 592 | // This point may not be within 1 pixel of a control point. We update the bounding box to |
| 593 | // include it. |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 594 | intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos); |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 595 | devBounds->growToInclude(b0.fPos.fX, b0.fPos.fY); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 596 | |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 597 | if (toSrc) { |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 598 | toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(BezierVertex), kVertsPerQuad); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 599 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 600 | } |
| 601 | |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 602 | // Equations based off of Loop-Blinn Quadratic GPU Rendering |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 603 | // Input Parametric: |
| 604 | // 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) |
| 605 | // Output Implicit: |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 606 | // f(x, y, w) = f(P) = K^2 - LM |
| 607 | // 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] | 608 | // k, l, m are calculated in function GrPathUtils::getConicKLM |
| 609 | void set_conic_coeffs(const SkPoint p[3], BezierVertex verts[kVertsPerQuad], |
| 610 | const SkScalar weight) { |
| 611 | SkScalar klm[9]; |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 612 | |
commit-bot@chromium.org | 1394840 | 2013-08-20 17:55:43 +0000 | [diff] [blame] | 613 | GrPathUtils::getConicKLM(p, weight, klm); |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 614 | |
| 615 | for (int i = 0; i < kVertsPerQuad; ++i) { |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 616 | const SkPoint pnt = verts[i].fPos; |
commit-bot@chromium.org | 1394840 | 2013-08-20 17:55:43 +0000 | [diff] [blame] | 617 | verts[i].fConic.fK = pnt.fX * klm[0] + pnt.fY * klm[1] + klm[2]; |
| 618 | verts[i].fConic.fL = pnt.fX * klm[3] + pnt.fY * klm[4] + klm[5]; |
| 619 | 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] | 620 | } |
| 621 | } |
| 622 | |
| 623 | void add_conics(const SkPoint p[3], |
commit-bot@chromium.org | 1394840 | 2013-08-20 17:55:43 +0000 | [diff] [blame] | 624 | const SkScalar weight, |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 625 | const SkMatrix* toDevice, |
| 626 | const SkMatrix* toSrc, |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 627 | BezierVertex** vert, |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 628 | SkRect* devBounds) { |
| 629 | bloat_quad(p, toDevice, toSrc, *vert, devBounds); |
| 630 | set_conic_coeffs(p, *vert, weight); |
| 631 | *vert += kVertsPerQuad; |
| 632 | } |
| 633 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 634 | void add_quads(const SkPoint p[3], |
| 635 | int subdiv, |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 636 | const SkMatrix* toDevice, |
| 637 | const SkMatrix* toSrc, |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 638 | BezierVertex** vert, |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 639 | SkRect* devBounds) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 640 | SkASSERT(subdiv >= 0); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 641 | if (subdiv) { |
| 642 | SkPoint newP[5]; |
| 643 | SkChopQuadAtHalf(p, newP); |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 644 | add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert, devBounds); |
| 645 | add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert, devBounds); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 646 | } else { |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 647 | bloat_quad(p, toDevice, toSrc, *vert, devBounds); |
egdaniel@google.com | 34b05ca | 2013-08-05 20:43:12 +0000 | [diff] [blame] | 648 | set_uv_quad(p, *vert); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 649 | *vert += kVertsPerQuad; |
| 650 | } |
| 651 | } |
| 652 | |
| 653 | void add_line(const SkPoint p[2], |
| 654 | int rtHeight, |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 655 | const SkMatrix* toSrc, |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 656 | GrColor coverage, |
| 657 | LineVertex** vert) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 658 | const SkPoint& a = p[0]; |
| 659 | const SkPoint& b = p[1]; |
| 660 | |
| 661 | SkVector orthVec = b; |
| 662 | orthVec -= a; |
| 663 | |
| 664 | if (orthVec.setLength(SK_Scalar1)) { |
| 665 | orthVec.setOrthog(orthVec); |
| 666 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 667 | for (int i = 0; i < kVertsPerLineSeg; ++i) { |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 668 | (*vert)[i].fPos = (i & 0x1) ? b : a; |
| 669 | if (i & 0x2) { |
jvanverth@google.com | 5e2d270 | 2013-08-14 14:29:29 +0000 | [diff] [blame] | 670 | (*vert)[i].fPos += orthVec; |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 671 | (*vert)[i].fCoverage = 0; |
| 672 | } else if (i & 0x4) { |
| 673 | (*vert)[i].fPos -= orthVec; |
| 674 | (*vert)[i].fCoverage = 0; |
| 675 | } else { |
| 676 | (*vert)[i].fCoverage = coverage; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 677 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 678 | } |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 679 | if (NULL != toSrc) { |
| 680 | toSrc->mapPointsWithStride(&(*vert)->fPos, |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 681 | sizeof(LineVertex), |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 682 | kVertsPerLineSeg); |
| 683 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 684 | } else { |
| 685 | // just make it degenerate and likely offscreen |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 686 | for (int i = 0; i < kVertsPerLineSeg; ++i) { |
| 687 | (*vert)[i].fPos.set(SK_ScalarMax, SK_ScalarMax); |
| 688 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 689 | } |
| 690 | |
| 691 | *vert += kVertsPerLineSeg; |
| 692 | } |
| 693 | |
| 694 | } |
| 695 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 696 | /////////////////////////////////////////////////////////////////////////////// |
| 697 | |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 698 | namespace { |
| 699 | |
| 700 | // position + edge |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 701 | extern const GrVertexAttrib gHairlineBezierAttribs[] = { |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 702 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
| 703 | {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding} |
| 704 | }; |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 705 | |
| 706 | // position + coverage |
| 707 | extern const GrVertexAttrib gHairlineLineAttribs[] = { |
| 708 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
| 709 | {kVec4ub_GrVertexAttribType, sizeof(GrPoint), kCoverage_GrVertexAttribBinding}, |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 710 | }; |
| 711 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 712 | }; |
| 713 | |
| 714 | bool GrAAHairLinePathRenderer::createLineGeom( |
bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 715 | const SkPath& path, |
bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 716 | GrDrawTarget* target, |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 717 | const PtArray& lines, |
| 718 | int lineCnt, |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 719 | GrDrawTarget::AutoReleaseGeometry* arg, |
| 720 | SkRect* devBounds) { |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 721 | GrDrawState* drawState = target->drawState(); |
| 722 | int rtHeight = drawState->getRenderTarget()->height(); |
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 | const SkMatrix& viewM = drawState->getViewMatrix(); |
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 | *devBounds = path.getBounds(); |
| 727 | viewM.mapRect(devBounds); |
| 728 | devBounds->outset(SK_Scalar1, SK_Scalar1); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 729 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 730 | int vertCnt = kVertsPerLineSeg * lineCnt; |
| 731 | |
| 732 | target->drawState()->setVertexAttribs<gHairlineLineAttribs>(SK_ARRAY_COUNT(gHairlineLineAttribs)); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 733 | SkASSERT(sizeof(LineVertex) == target->getDrawState().getVertexSize()); |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 734 | |
| 735 | if (!arg->set(target, vertCnt, 0)) { |
| 736 | return false; |
| 737 | } |
| 738 | |
| 739 | LineVertex* verts = reinterpret_cast<LineVertex*>(arg->vertices()); |
| 740 | |
| 741 | const SkMatrix* toSrc = NULL; |
| 742 | SkMatrix ivm; |
| 743 | |
| 744 | if (viewM.hasPerspective()) { |
| 745 | if (viewM.invert(&ivm)) { |
| 746 | toSrc = &ivm; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | for (int i = 0; i < lineCnt; ++i) { |
| 751 | add_line(&lines[2*i], rtHeight, toSrc, drawState->getCoverage(), &verts); |
| 752 | } |
| 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 | |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 772 | // All the vertices that we compute are within 1 of path control points with the exception of |
| 773 | // one of the bounding vertices for each quad. The add_quads() function will update the bounds |
| 774 | // for each quad added. |
| 775 | *devBounds = path.getBounds(); |
| 776 | viewM.mapRect(devBounds); |
| 777 | devBounds->outset(SK_Scalar1, SK_Scalar1); |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 778 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 779 | int vertCnt = kVertsPerQuad * quadCnt + kVertsPerQuad * conicCnt; |
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 | target->drawState()->setVertexAttribs<gHairlineBezierAttribs>(SK_ARRAY_COUNT(gHairlineBezierAttribs)); |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 782 | SkASSERT(sizeof(BezierVertex) == target->getDrawState().getVertexSize()); |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 783 | |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 784 | if (!arg->set(target, vertCnt, 0)) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 785 | return false; |
| 786 | } |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 787 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 788 | BezierVertex* verts = reinterpret_cast<BezierVertex*>(arg->vertices()); |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 789 | |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 790 | const SkMatrix* toDevice = NULL; |
| 791 | const SkMatrix* toSrc = NULL; |
| 792 | SkMatrix ivm; |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 793 | |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 794 | if (viewM.hasPerspective()) { |
| 795 | if (viewM.invert(&ivm)) { |
| 796 | toDevice = &viewM; |
| 797 | toSrc = &ivm; |
| 798 | } |
| 799 | } |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 800 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 801 | int unsubdivQuadCnt = quads.count() / 3; |
| 802 | for (int i = 0; i < unsubdivQuadCnt; ++i) { |
tfarina@chromium.org | f6de475 | 2013-08-17 00:02:59 +0000 | [diff] [blame] | 803 | SkASSERT(qSubdivs[i] >= 0); |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 804 | add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts, devBounds); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 805 | } |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 806 | |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 807 | // Start Conics |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 808 | for (int i = 0; i < conicCnt; ++i) { |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 809 | add_conics(&conics[3*i], cWeights[i], toDevice, toSrc, &verts, devBounds); |
| 810 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 811 | return true; |
| 812 | } |
| 813 | |
robertphillips@google.com | 8a4fc40 | 2012-05-24 12:42:24 +0000 | [diff] [blame] | 814 | bool GrAAHairLinePathRenderer::canDrawPath(const SkPath& path, |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 815 | const SkStrokeRec& stroke, |
robertphillips@google.com | 8a4fc40 | 2012-05-24 12:42:24 +0000 | [diff] [blame] | 816 | const GrDrawTarget* target, |
| 817 | bool antiAlias) const { |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 818 | if (!stroke.isHairlineStyle() || !antiAlias) { |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 819 | return false; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 820 | } |
| 821 | |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 822 | if (SkPath::kLine_SegmentMask == path.getSegmentMasks() || |
| 823 | target->caps()->shaderDerivativeSupport()) { |
| 824 | return true; |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 825 | } |
egdaniel@google.com | 3f2a2d5 | 2013-08-01 17:09:11 +0000 | [diff] [blame] | 826 | return false; |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 827 | } |
| 828 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 829 | template <class VertexType> |
| 830 | bool check_bounds(GrDrawState* drawState, const SkRect& devBounds, void* vertices, int vCount) |
| 831 | { |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 832 | SkRect tolDevBounds = devBounds; |
| 833 | tolDevBounds.outset(SK_Scalar1 / 10000, SK_Scalar1 / 10000); |
| 834 | SkRect actualBounds; |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 835 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 836 | VertexType* verts = reinterpret_cast<VertexType*>(vertices); |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 837 | bool first = true; |
| 838 | for (int i = 0; i < vCount; ++i) { |
| 839 | SkPoint pos = verts[i].fPos; |
| 840 | // This is a hack to workaround the fact that we move some degenerate segments offscreen. |
| 841 | if (SK_ScalarMax == pos.fX) { |
| 842 | continue; |
| 843 | } |
| 844 | drawState->getViewMatrix().mapPoints(&pos, 1); |
| 845 | if (first) { |
| 846 | actualBounds.set(pos.fX, pos.fY, pos.fX, pos.fY); |
| 847 | first = false; |
| 848 | } else { |
| 849 | actualBounds.growToInclude(pos.fX, pos.fY); |
| 850 | } |
| 851 | } |
| 852 | if (!first) { |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 853 | return tolDevBounds.contains(actualBounds); |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 854 | } |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 855 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 856 | return true; |
| 857 | } |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 858 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 859 | bool GrAAHairLinePathRenderer::onDrawPath(const SkPath& path, |
| 860 | const SkStrokeRec&, |
| 861 | GrDrawTarget* target, |
| 862 | bool antiAlias) { |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 863 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 864 | GrDrawState* drawState = target->drawState(); |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 865 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 866 | SkIRect devClipBounds; |
| 867 | target->getClip()->getConservativeBounds(drawState->getRenderTarget(), &devClipBounds); |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 868 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 869 | int lineCnt; |
| 870 | int quadCnt; |
| 871 | int conicCnt; |
| 872 | PREALLOC_PTARRAY(128) lines; |
| 873 | PREALLOC_PTARRAY(128) quads; |
| 874 | PREALLOC_PTARRAY(128) conics; |
| 875 | IntArray qSubdivs; |
| 876 | FloatArray cWeights; |
| 877 | quadCnt = generate_lines_and_quads(path, drawState->getViewMatrix(), devClipBounds, |
| 878 | &lines, &quads, &conics, &qSubdivs, &cWeights); |
| 879 | lineCnt = lines.count() / 2; |
| 880 | conicCnt = conics.count() / 3; |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 881 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 882 | // do lines first |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 883 | { |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 884 | GrDrawTarget::AutoReleaseGeometry arg; |
| 885 | SkRect devBounds; |
| 886 | |
| 887 | if (!this->createLineGeom(path, |
| 888 | target, |
| 889 | lines, |
| 890 | lineCnt, |
| 891 | &arg, |
| 892 | &devBounds)) { |
| 893 | return false; |
| 894 | } |
| 895 | |
| 896 | GrDrawTarget::AutoStateRestore asr; |
| 897 | |
| 898 | // createGeom transforms the geometry to device space when the matrix does not have |
| 899 | // perspective. |
| 900 | if (target->getDrawState().getViewMatrix().hasPerspective()) { |
| 901 | asr.set(target, GrDrawTarget::kPreserve_ASRInit); |
| 902 | } else if (!asr.setIdentity(target, GrDrawTarget::kPreserve_ASRInit)) { |
| 903 | return false; |
| 904 | } |
| 905 | GrDrawState* drawState = target->drawState(); |
| 906 | |
| 907 | // Check devBounds |
| 908 | SkASSERT(check_bounds<LineVertex>(drawState, devBounds, arg.vertices(), |
| 909 | kVertsPerLineSeg * lineCnt)); |
| 910 | |
| 911 | { |
| 912 | GrDrawState::AutoRestoreEffects are(drawState); |
| 913 | target->setIndexSourceToBuffer(fLinesIndexBuffer); |
| 914 | int lines = 0; |
| 915 | while (lines < lineCnt) { |
| 916 | int n = GrMin(lineCnt - lines, kNumLineSegsInIdxBuffer); |
| 917 | target->drawIndexed(kTriangles_GrPrimitiveType, |
| 918 | kVertsPerLineSeg*lines, // startV |
| 919 | 0, // startI |
| 920 | kVertsPerLineSeg*n, // vCount |
| 921 | kIdxsPerLineSeg*n, |
| 922 | &devBounds); // iCount |
| 923 | lines += n; |
| 924 | } |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 925 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 926 | } |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 927 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 928 | // then quadratics/conics |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 929 | { |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 930 | GrDrawTarget::AutoReleaseGeometry arg; |
| 931 | SkRect devBounds; |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 932 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 933 | if (!this->createBezierGeom(path, |
| 934 | target, |
| 935 | quads, |
| 936 | quadCnt, |
| 937 | conics, |
| 938 | conicCnt, |
| 939 | qSubdivs, |
| 940 | cWeights, |
| 941 | &arg, |
| 942 | &devBounds)) { |
| 943 | return false; |
| 944 | } |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 945 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 946 | GrDrawTarget::AutoStateRestore asr; |
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 | // createGeom transforms the geometry to device space when the matrix does not have |
| 949 | // perspective. |
| 950 | if (target->getDrawState().getViewMatrix().hasPerspective()) { |
| 951 | asr.set(target, GrDrawTarget::kPreserve_ASRInit); |
| 952 | } else if (!asr.setIdentity(target, GrDrawTarget::kPreserve_ASRInit)) { |
| 953 | return false; |
| 954 | } |
| 955 | GrDrawState* drawState = target->drawState(); |
| 956 | |
| 957 | static const int kEdgeAttrIndex = 1; |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 958 | |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 959 | // Check devBounds |
| 960 | SkASSERT(check_bounds<BezierVertex>(drawState, devBounds, arg.vertices(), |
| 961 | kVertsPerQuad * quadCnt + kVertsPerQuad * conicCnt)); |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 962 | |
commit-bot@chromium.org | 8846247 | 2013-08-23 21:01:52 +0000 | [diff] [blame] | 963 | if (quadCnt > 0) { |
| 964 | GrEffectRef* hairQuadEffect = GrQuadEffect::Create(kHairAA_GrBezierEdgeType, |
| 965 | *target->caps()); |
| 966 | SkASSERT(NULL != hairQuadEffect); |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 967 | GrDrawState::AutoRestoreEffects are(drawState); |
| 968 | target->setIndexSourceToBuffer(fQuadsIndexBuffer); |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 969 | drawState->addCoverageEffect(hairQuadEffect, kEdgeAttrIndex)->unref(); |
commit-bot@chromium.org | 8846247 | 2013-08-23 21:01:52 +0000 | [diff] [blame] | 970 | int quads = 0; |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 971 | while (quads < quadCnt) { |
| 972 | int n = GrMin(quadCnt - quads, kNumQuadsInIdxBuffer); |
| 973 | target->drawIndexed(kTriangles_GrPrimitiveType, |
| 974 | kVertsPerQuad*quads, // startV |
| 975 | 0, // startI |
| 976 | kVertsPerQuad*n, // vCount |
| 977 | kIdxsPerQuad*n, // iCount |
| 978 | &devBounds); |
| 979 | quads += n; |
| 980 | } |
| 981 | } |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 982 | |
commit-bot@chromium.org | 8846247 | 2013-08-23 21:01:52 +0000 | [diff] [blame] | 983 | if (conicCnt > 0) { |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 984 | GrDrawState::AutoRestoreEffects are(drawState); |
commit-bot@chromium.org | 8846247 | 2013-08-23 21:01:52 +0000 | [diff] [blame] | 985 | GrEffectRef* hairConicEffect = GrConicEffect::Create(kHairAA_GrBezierEdgeType, |
| 986 | *target->caps()); |
| 987 | SkASSERT(NULL != hairConicEffect); |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 988 | drawState->addCoverageEffect(hairConicEffect, 1, 2)->unref(); |
commit-bot@chromium.org | 8846247 | 2013-08-23 21:01:52 +0000 | [diff] [blame] | 989 | int conics = 0; |
jvanverth@google.com | 681ccf0 | 2013-08-16 14:51:51 +0000 | [diff] [blame] | 990 | while (conics < conicCnt) { |
| 991 | int n = GrMin(conicCnt - conics, kNumQuadsInIdxBuffer); |
| 992 | target->drawIndexed(kTriangles_GrPrimitiveType, |
| 993 | kVertsPerQuad*(quadCnt + conics), // startV |
| 994 | 0, // startI |
| 995 | kVertsPerQuad*n, // vCount |
| 996 | kIdxsPerQuad*n, // iCount |
| 997 | &devBounds); |
| 998 | conics += n; |
| 999 | } |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 1000 | } |
| 1001 | } |
skia.committer@gmail.com | 7475811 | 2013-08-17 07:01:54 +0000 | [diff] [blame] | 1002 | |
bsalomon@google.com | 0406b9e | 2013-04-02 21:00:15 +0000 | [diff] [blame] | 1003 | target->resetIndexSource(); |
bsalomon@google.com | 4647f90 | 2013-03-26 14:45:27 +0000 | [diff] [blame] | 1004 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 1005 | return true; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1006 | } |