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