bsalomon@google.com | f75b84e | 2011-09-29 14:58:28 +0000 | [diff] [blame] | 1 | |
| 2 | /* |
| 3 | * Copyright 2011 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
| 7 | */ |
| 8 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 9 | #include "GrAAHairLinePathRenderer.h" |
| 10 | |
| 11 | #include "GrContext.h" |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 12 | #include "GrDrawState.h" |
bsalomon@google.com | c26d94f | 2013-03-25 18:19:00 +0000 | [diff] [blame] | 13 | #include "GrDrawTargetCaps.h" |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 14 | #include "GrEffect.h" |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 15 | #include "GrGpu.h" |
| 16 | #include "GrIndexBuffer.h" |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 17 | #include "GrPathUtils.h" |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 18 | #include "GrTBackendEffectFactory.h" |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 19 | #include "SkGeometry.h" |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 20 | #include "SkStroke.h" |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 21 | #include "SkTemplates.h" |
| 22 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 23 | #include "gl/GrGLEffect.h" |
| 24 | #include "gl/GrGLSL.h" |
bsalomon@google.com | 4647f90 | 2013-03-26 14:45:27 +0000 | [diff] [blame] | 25 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 26 | namespace { |
| 27 | // quadratics are rendered as 5-sided polys in order to bound the |
| 28 | // 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] | 29 | // bloat_quad. Quadratics and conics share an index buffer |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 30 | static const int kVertsPerQuad = 5; |
| 31 | static const int kIdxsPerQuad = 9; |
| 32 | |
| 33 | static const int kVertsPerLineSeg = 4; |
| 34 | static const int kIdxsPerLineSeg = 6; |
| 35 | |
| 36 | static const int kNumQuadsInIdxBuffer = 256; |
| 37 | static const size_t kQuadIdxSBufize = kIdxsPerQuad * |
| 38 | sizeof(uint16_t) * |
| 39 | kNumQuadsInIdxBuffer; |
| 40 | |
| 41 | bool push_quad_index_data(GrIndexBuffer* qIdxBuffer) { |
| 42 | uint16_t* data = (uint16_t*) qIdxBuffer->lock(); |
| 43 | bool tempData = NULL == data; |
| 44 | if (tempData) { |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 45 | data = SkNEW_ARRAY(uint16_t, kNumQuadsInIdxBuffer * kIdxsPerQuad); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 46 | } |
| 47 | for (int i = 0; i < kNumQuadsInIdxBuffer; ++i) { |
| 48 | |
| 49 | // Each quadratic is rendered as a five sided polygon. This poly bounds |
| 50 | // the quadratic's bounding triangle but has been expanded so that the |
| 51 | // 1-pixel wide area around the curve is inside the poly. |
| 52 | // If a,b,c are the original control points then the poly a0,b0,c0,c1,a1 |
| 53 | // that is rendered would look like this: |
| 54 | // b0 |
| 55 | // b |
| 56 | // |
| 57 | // a0 c0 |
| 58 | // a c |
| 59 | // a1 c1 |
bsalomon@google.com | 0e5104c | 2012-04-10 16:20:41 +0000 | [diff] [blame] | 60 | // Each is drawn as three triangles specified by these 9 indices: |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 61 | int baseIdx = i * kIdxsPerQuad; |
| 62 | uint16_t baseVert = (uint16_t)(i * kVertsPerQuad); |
| 63 | data[0 + baseIdx] = baseVert + 0; // a0 |
| 64 | data[1 + baseIdx] = baseVert + 1; // a1 |
| 65 | data[2 + baseIdx] = baseVert + 2; // b0 |
| 66 | data[3 + baseIdx] = baseVert + 2; // b0 |
| 67 | data[4 + baseIdx] = baseVert + 4; // c1 |
| 68 | data[5 + baseIdx] = baseVert + 3; // c0 |
| 69 | data[6 + baseIdx] = baseVert + 1; // a1 |
| 70 | data[7 + baseIdx] = baseVert + 4; // c1 |
| 71 | data[8 + baseIdx] = baseVert + 2; // b0 |
| 72 | } |
| 73 | if (tempData) { |
| 74 | bool ret = qIdxBuffer->updateData(data, kQuadIdxSBufize); |
| 75 | delete[] data; |
| 76 | return ret; |
| 77 | } else { |
| 78 | qIdxBuffer->unlock(); |
| 79 | return true; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | GrPathRenderer* GrAAHairLinePathRenderer::Create(GrContext* context) { |
bsalomon@google.com | a8a6a32 | 2011-09-23 14:19:58 +0000 | [diff] [blame] | 85 | const GrIndexBuffer* lIdxBuffer = context->getQuadIndexBuffer(); |
| 86 | if (NULL == lIdxBuffer) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 87 | return NULL; |
| 88 | } |
bsalomon@google.com | a8a6a32 | 2011-09-23 14:19:58 +0000 | [diff] [blame] | 89 | GrGpu* gpu = context->getGpu(); |
| 90 | GrIndexBuffer* qIdxBuf = gpu->createIndexBuffer(kQuadIdxSBufize, false); |
| 91 | SkAutoTUnref<GrIndexBuffer> qIdxBuffer(qIdxBuf); |
| 92 | if (NULL == qIdxBuf || |
| 93 | !push_quad_index_data(qIdxBuf)) { |
| 94 | return NULL; |
| 95 | } |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 96 | return SkNEW_ARGS(GrAAHairLinePathRenderer, |
| 97 | (context, lIdxBuffer, qIdxBuf)); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | GrAAHairLinePathRenderer::GrAAHairLinePathRenderer( |
| 101 | const GrContext* context, |
| 102 | const GrIndexBuffer* linesIndexBuffer, |
| 103 | const GrIndexBuffer* quadsIndexBuffer) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 104 | fLinesIndexBuffer = linesIndexBuffer; |
| 105 | linesIndexBuffer->ref(); |
| 106 | fQuadsIndexBuffer = quadsIndexBuffer; |
| 107 | quadsIndexBuffer->ref(); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | GrAAHairLinePathRenderer::~GrAAHairLinePathRenderer() { |
| 111 | fLinesIndexBuffer->unref(); |
| 112 | fQuadsIndexBuffer->unref(); |
| 113 | } |
| 114 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 115 | namespace { |
| 116 | |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 117 | typedef SkTArray<SkPoint, true> PtArray; |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 118 | #define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true> |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 119 | typedef SkTArray<int, true> IntArray; |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 120 | typedef SkTArray<float, true> FloatArray; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 121 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 122 | // Takes 178th time of logf on Z600 / VC2010 |
| 123 | int get_float_exp(float x) { |
| 124 | GR_STATIC_ASSERT(sizeof(int) == sizeof(float)); |
| 125 | #if GR_DEBUG |
| 126 | static bool tested; |
| 127 | if (!tested) { |
| 128 | tested = true; |
| 129 | GrAssert(get_float_exp(0.25f) == -2); |
| 130 | GrAssert(get_float_exp(0.3f) == -2); |
| 131 | GrAssert(get_float_exp(0.5f) == -1); |
| 132 | GrAssert(get_float_exp(1.f) == 0); |
| 133 | GrAssert(get_float_exp(2.f) == 1); |
| 134 | GrAssert(get_float_exp(2.5f) == 1); |
| 135 | GrAssert(get_float_exp(8.f) == 3); |
| 136 | GrAssert(get_float_exp(100.f) == 6); |
| 137 | GrAssert(get_float_exp(1000.f) == 9); |
| 138 | GrAssert(get_float_exp(1024.f) == 10); |
| 139 | GrAssert(get_float_exp(3000000.f) == 21); |
| 140 | } |
| 141 | #endif |
bsalomon@google.com | 2ec7280 | 2011-09-21 21:46:03 +0000 | [diff] [blame] | 142 | const int* iptr = (const int*)&x; |
| 143 | return (((*iptr) & 0x7f800000) >> 23) - 127; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 144 | } |
| 145 | |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 146 | // Uses the max curvature function for quads to estimate |
| 147 | // where to chop the conic. If the max curvature is not |
| 148 | // found along the curve segment it will return 1 and |
| 149 | // dst[0] is the orginal conic. If it returns 2 the dst[0] |
| 150 | // and dst[1] are the two new conics. |
| 151 | int chop_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) { |
| 152 | SkScalar t = SkFindQuadMaxCurvature(src); |
| 153 | if (t == 0) { |
| 154 | if (dst) { |
| 155 | dst[0].set(src, weight); |
| 156 | } |
| 157 | return 1; |
| 158 | } else { |
| 159 | if (dst) { |
| 160 | SkConic conic; |
| 161 | conic.set(src, weight); |
| 162 | conic.chopAt(t, dst); |
| 163 | } |
| 164 | return 2; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // returns 0 if quad/conic is degen or close to it |
| 169 | // in this case approx the path with lines |
| 170 | // otherwise returns 1 |
| 171 | int is_degen_quad_or_conic(const SkPoint p[3]) { |
| 172 | static const SkScalar gDegenerateToLineTol = SK_Scalar1; |
| 173 | static const SkScalar gDegenerateToLineTolSqd = |
| 174 | SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol); |
| 175 | |
| 176 | if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd || |
| 177 | p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) { |
| 178 | return 1; |
| 179 | } |
| 180 | |
| 181 | SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]); |
| 182 | if (dsqd < gDegenerateToLineTolSqd) { |
| 183 | return 1; |
| 184 | } |
| 185 | |
| 186 | if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) { |
| 187 | return 1; |
| 188 | } |
| 189 | return 0; |
| 190 | } |
| 191 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 192 | // we subdivide the quads to avoid huge overfill |
| 193 | // if it returns -1 then should be drawn as lines |
| 194 | int num_quad_subdivs(const SkPoint p[3]) { |
| 195 | static const SkScalar gDegenerateToLineTol = SK_Scalar1; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 196 | static const SkScalar gDegenerateToLineTolSqd = |
bsalomon@google.com | 46a2a1e | 2011-09-06 22:10:52 +0000 | [diff] [blame] | 197 | SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 198 | |
bsalomon@google.com | 46a2a1e | 2011-09-06 22:10:52 +0000 | [diff] [blame] | 199 | if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd || |
| 200 | p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 201 | return -1; |
| 202 | } |
bsalomon@google.com | 46a2a1e | 2011-09-06 22:10:52 +0000 | [diff] [blame] | 203 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 204 | SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]); |
bsalomon@google.com | 46a2a1e | 2011-09-06 22:10:52 +0000 | [diff] [blame] | 205 | if (dsqd < gDegenerateToLineTolSqd) { |
| 206 | return -1; |
| 207 | } |
| 208 | |
| 209 | if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 210 | return -1; |
| 211 | } |
| 212 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 213 | // tolerance of triangle height in pixels |
| 214 | // tuned on windows Quadro FX 380 / Z600 |
| 215 | // trade off of fill vs cpu time on verts |
| 216 | // maybe different when do this using gpu (geo or tess shaders) |
| 217 | static const SkScalar gSubdivTol = 175 * SK_Scalar1; |
| 218 | |
robertphillips@google.com | 7460b37 | 2012-04-25 16:54:51 +0000 | [diff] [blame] | 219 | if (dsqd <= SkScalarMul(gSubdivTol, gSubdivTol)) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 220 | return 0; |
| 221 | } else { |
robertphillips@google.com | 87379e1 | 2013-03-29 12:11:10 +0000 | [diff] [blame] | 222 | static const int kMaxSub = 4; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 223 | // subdividing the quad reduces d by 4. so we want x = log4(d/tol) |
| 224 | // = log4(d*d/tol*tol)/2 |
| 225 | // = log2(d*d/tol*tol) |
| 226 | |
| 227 | #ifdef SK_SCALAR_IS_FLOAT |
| 228 | // +1 since we're ignoring the mantissa contribution. |
| 229 | int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1; |
| 230 | log = GrMin(GrMax(0, log), kMaxSub); |
| 231 | return log; |
| 232 | #else |
robertphillips@google.com | 7460b37 | 2012-04-25 16:54:51 +0000 | [diff] [blame] | 233 | SkScalar log = SkScalarLog( |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 234 | SkScalarDiv(dsqd, |
robertphillips@google.com | 7460b37 | 2012-04-25 16:54:51 +0000 | [diff] [blame] | 235 | SkScalarMul(gSubdivTol, gSubdivTol))); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 236 | static const SkScalar conv = SkScalarInvert(SkScalarLog(2)); |
| 237 | log = SkScalarMul(log, conv); |
| 238 | return GrMin(GrMax(0, SkScalarCeilToInt(log)),kMaxSub); |
| 239 | #endif |
| 240 | } |
| 241 | } |
| 242 | |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 243 | /** |
| 244 | * Generates the lines and quads to be rendered. Lines are always recorded in |
| 245 | * device space. We will do a device space bloat to account for the 1pixel |
| 246 | * thickness. |
| 247 | * Quads are recorded in device space unless m contains |
| 248 | * perspective, then in they are in src space. We do this because we will |
| 249 | * subdivide large quads to reduce over-fill. This subdivision has to be |
| 250 | * performed before applying the perspective matrix. |
| 251 | */ |
| 252 | int generate_lines_and_quads(const SkPath& path, |
| 253 | const SkMatrix& m, |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame^] | 254 | const SkIRect& devClipBounds, |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 255 | PtArray* lines, |
| 256 | PtArray* quads, |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 257 | PtArray* conics, |
| 258 | IntArray* quadSubdivCnts, |
| 259 | FloatArray* conicWeights) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 260 | SkPath::Iter iter(path, false); |
| 261 | |
| 262 | int totalQuadCount = 0; |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame^] | 263 | SkRect bounds; |
| 264 | SkIRect ibounds; |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 265 | |
| 266 | bool persp = m.hasPerspective(); |
| 267 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 268 | for (;;) { |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 269 | GrPoint pathPts[4]; |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 270 | GrPoint devPts[4]; |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 271 | SkPath::Verb verb = iter.next(pathPts); |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 272 | switch (verb) { |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 273 | case SkPath::kConic_Verb: { |
| 274 | SkConic dst[2]; |
| 275 | int conicCnt = chop_conic(pathPts, dst, iter.conicWeight()); |
| 276 | for (int i = 0; i < conicCnt; ++i) { |
| 277 | SkPoint* chopPnts = dst[i].fPts; |
| 278 | m.mapPoints(devPts, chopPnts, 3); |
| 279 | bounds.setBounds(devPts, 3); |
| 280 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 281 | bounds.roundOut(&ibounds); |
| 282 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
| 283 | if (is_degen_quad_or_conic(devPts)) { |
| 284 | SkPoint* pts = lines->push_back_n(4); |
| 285 | pts[0] = devPts[0]; |
| 286 | pts[1] = devPts[1]; |
| 287 | pts[2] = devPts[1]; |
| 288 | pts[3] = devPts[2]; |
| 289 | } else { |
| 290 | // when in perspective keep conics in src space |
| 291 | SkPoint* cPts = persp ? chopPnts : devPts; |
| 292 | SkPoint* pts = conics->push_back_n(3); |
| 293 | pts[0] = cPts[0]; |
| 294 | pts[1] = cPts[1]; |
| 295 | pts[2] = cPts[2]; |
| 296 | conicWeights->push_back() = dst[i].fW; |
| 297 | } |
| 298 | } |
| 299 | } |
reed@google.com | 277c3f8 | 2013-05-31 15:17:50 +0000 | [diff] [blame] | 300 | break; |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 301 | } |
| 302 | case SkPath::kMove_Verb: |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 303 | break; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 304 | case SkPath::kLine_Verb: |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 305 | m.mapPoints(devPts, pathPts, 2); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 306 | bounds.setBounds(devPts, 2); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 307 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 308 | bounds.roundOut(&ibounds); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 309 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 310 | SkPoint* pts = lines->push_back_n(2); |
| 311 | pts[0] = devPts[0]; |
| 312 | pts[1] = devPts[1]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 313 | } |
| 314 | break; |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 315 | case SkPath::kQuad_Verb: { |
| 316 | SkPoint choppedPts[5]; |
| 317 | // Chopping the quad helps when the quad is either degenerate or nearly degenerate. |
| 318 | // When it is degenerate it allows the approximation with lines to work since the |
| 319 | // chop point (if there is one) will be at the parabola's vertex. In the nearly |
| 320 | // degenerate the QuadUVMatrix computed for the points is almost singular which |
| 321 | // can cause rendering artifacts. |
| 322 | int n = SkChopQuadAtMaxCurvature(pathPts, choppedPts); |
| 323 | for (int i = 0; i < n; ++i) { |
| 324 | SkPoint* quadPts = choppedPts + i * 2; |
| 325 | m.mapPoints(devPts, quadPts, 3); |
| 326 | bounds.setBounds(devPts, 3); |
| 327 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 328 | bounds.roundOut(&ibounds); |
| 329 | |
| 330 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
| 331 | int subdiv = num_quad_subdivs(devPts); |
| 332 | GrAssert(subdiv >= -1); |
| 333 | if (-1 == subdiv) { |
| 334 | SkPoint* pts = lines->push_back_n(4); |
| 335 | pts[0] = devPts[0]; |
| 336 | pts[1] = devPts[1]; |
| 337 | pts[2] = devPts[1]; |
| 338 | pts[3] = devPts[2]; |
| 339 | } else { |
| 340 | // when in perspective keep quads in src space |
| 341 | SkPoint* qPts = persp ? quadPts : devPts; |
| 342 | SkPoint* pts = quads->push_back_n(3); |
| 343 | pts[0] = qPts[0]; |
| 344 | pts[1] = qPts[1]; |
| 345 | pts[2] = qPts[2]; |
| 346 | quadSubdivCnts->push_back() = subdiv; |
| 347 | totalQuadCount += 1 << subdiv; |
| 348 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 349 | } |
| 350 | } |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 351 | break; |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 352 | } |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 353 | case SkPath::kCubic_Verb: |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 354 | m.mapPoints(devPts, pathPts, 4); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 355 | bounds.setBounds(devPts, 4); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 356 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 357 | bounds.roundOut(&ibounds); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 358 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 359 | PREALLOC_PTARRAY(32) q; |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 360 | // we don't need a direction if we aren't constraining the subdivision |
| 361 | static const SkPath::Direction kDummyDir = SkPath::kCCW_Direction; |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 362 | // We convert cubics to quadratics (for now). |
| 363 | // In perspective have to do conversion in src space. |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 364 | if (persp) { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 365 | SkScalar tolScale = |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 366 | GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m, |
| 367 | path.getBounds()); |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 368 | GrPathUtils::convertCubicToQuads(pathPts, tolScale, false, kDummyDir, &q); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 369 | } else { |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 370 | GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, false, kDummyDir, &q); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 371 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 372 | for (int i = 0; i < q.count(); i += 3) { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 373 | SkPoint* qInDevSpace; |
| 374 | // bounds has to be calculated in device space, but q is |
| 375 | // in src space when there is perspective. |
| 376 | if (persp) { |
| 377 | m.mapPoints(devPts, &q[i], 3); |
| 378 | bounds.setBounds(devPts, 3); |
| 379 | qInDevSpace = devPts; |
| 380 | } else { |
| 381 | bounds.setBounds(&q[i], 3); |
| 382 | qInDevSpace = &q[i]; |
| 383 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 384 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 385 | bounds.roundOut(&ibounds); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 386 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 387 | int subdiv = num_quad_subdivs(qInDevSpace); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 388 | GrAssert(subdiv >= -1); |
| 389 | if (-1 == subdiv) { |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 390 | SkPoint* pts = lines->push_back_n(4); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 391 | // lines should always be in device coords |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 392 | pts[0] = qInDevSpace[0]; |
| 393 | pts[1] = qInDevSpace[1]; |
| 394 | pts[2] = qInDevSpace[1]; |
| 395 | pts[3] = qInDevSpace[2]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 396 | } else { |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 397 | SkPoint* pts = quads->push_back_n(3); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 398 | // q is already in src space when there is no |
| 399 | // perspective and dev coords otherwise. |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 400 | pts[0] = q[0 + i]; |
| 401 | pts[1] = q[1 + i]; |
| 402 | pts[2] = q[2 + i]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 403 | quadSubdivCnts->push_back() = subdiv; |
| 404 | totalQuadCount += 1 << subdiv; |
| 405 | } |
| 406 | } |
| 407 | } |
| 408 | } |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 409 | break; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 410 | case SkPath::kClose_Verb: |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 411 | break; |
bsalomon@google.com | 94b284d | 2013-05-10 17:14:06 +0000 | [diff] [blame] | 412 | case SkPath::kDone_Verb: |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 413 | return totalQuadCount; |
| 414 | } |
| 415 | } |
| 416 | } |
| 417 | |
| 418 | struct Vertex { |
| 419 | GrPoint fPos; |
| 420 | union { |
| 421 | struct { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 422 | SkScalar fA; |
| 423 | SkScalar fB; |
| 424 | SkScalar fC; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 425 | } fLine; |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 426 | struct { |
| 427 | SkScalar fA; |
| 428 | SkScalar fB; |
| 429 | SkScalar fC; |
| 430 | SkScalar fD; |
| 431 | SkScalar fE; |
| 432 | SkScalar fF; |
| 433 | } fConic; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 434 | GrVec fQuadCoord; |
| 435 | struct { |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 436 | SkScalar fBogus[6]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 437 | }; |
| 438 | }; |
| 439 | }; |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 440 | |
| 441 | GR_STATIC_ASSERT(sizeof(Vertex) == 4 * sizeof(GrPoint)); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 442 | |
| 443 | void intersect_lines(const SkPoint& ptA, const SkVector& normA, |
| 444 | const SkPoint& ptB, const SkVector& normB, |
| 445 | SkPoint* result) { |
| 446 | |
| 447 | SkScalar lineAW = -normA.dot(ptA); |
| 448 | SkScalar lineBW = -normB.dot(ptB); |
| 449 | |
| 450 | SkScalar wInv = SkScalarMul(normA.fX, normB.fY) - |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 451 | SkScalarMul(normA.fY, normB.fX); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 452 | wInv = SkScalarInvert(wInv); |
| 453 | |
| 454 | result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY); |
| 455 | result->fX = SkScalarMul(result->fX, wInv); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 456 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 457 | result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW); |
| 458 | result->fY = SkScalarMul(result->fY, wInv); |
| 459 | } |
| 460 | |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 461 | void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice, |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 462 | const SkMatrix* toSrc, Vertex verts[kVertsPerQuad], |
| 463 | SkRect* devBounds) { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 464 | GrAssert(!toDevice == !toSrc); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 465 | // original quad is specified by tri a,b,c |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 466 | SkPoint a = qpts[0]; |
| 467 | SkPoint b = qpts[1]; |
| 468 | SkPoint c = qpts[2]; |
| 469 | |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 470 | // this should be in the src space, not dev coords, when we have perspective |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 471 | GrPathUtils::QuadUVMatrix DevToUV(qpts); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 472 | |
| 473 | if (toDevice) { |
| 474 | toDevice->mapPoints(&a, 1); |
| 475 | toDevice->mapPoints(&b, 1); |
| 476 | toDevice->mapPoints(&c, 1); |
| 477 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 478 | // make a new poly where we replace a and c by a 1-pixel wide edges orthog |
| 479 | // to edges ab and bc: |
| 480 | // |
| 481 | // before | after |
| 482 | // | b0 |
| 483 | // b | |
| 484 | // | |
| 485 | // | a0 c0 |
| 486 | // a c | a1 c1 |
| 487 | // |
| 488 | // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c, |
| 489 | // respectively. |
| 490 | Vertex& a0 = verts[0]; |
| 491 | Vertex& a1 = verts[1]; |
| 492 | Vertex& b0 = verts[2]; |
| 493 | Vertex& c0 = verts[3]; |
| 494 | Vertex& c1 = verts[4]; |
| 495 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 496 | SkVector ab = b; |
| 497 | ab -= a; |
| 498 | SkVector ac = c; |
| 499 | ac -= a; |
| 500 | SkVector cb = b; |
| 501 | cb -= c; |
| 502 | |
| 503 | // We should have already handled degenerates |
| 504 | GrAssert(ab.length() > 0 && cb.length() > 0); |
| 505 | |
| 506 | ab.normalize(); |
| 507 | SkVector abN; |
| 508 | abN.setOrthog(ab, SkVector::kLeft_Side); |
| 509 | if (abN.dot(ac) > 0) { |
| 510 | abN.negate(); |
| 511 | } |
| 512 | |
| 513 | cb.normalize(); |
| 514 | SkVector cbN; |
| 515 | cbN.setOrthog(cb, SkVector::kLeft_Side); |
| 516 | if (cbN.dot(ac) < 0) { |
| 517 | cbN.negate(); |
| 518 | } |
| 519 | |
| 520 | a0.fPos = a; |
| 521 | a0.fPos += abN; |
| 522 | a1.fPos = a; |
| 523 | a1.fPos -= abN; |
| 524 | |
| 525 | c0.fPos = c; |
| 526 | c0.fPos += cbN; |
| 527 | c1.fPos = c; |
| 528 | c1.fPos -= cbN; |
| 529 | |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 530 | // This point may not be within 1 pixel of a control point. We update the bounding box to |
| 531 | // include it. |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 532 | intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos); |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 533 | devBounds->growToInclude(b0.fPos.fX, b0.fPos.fY); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 534 | |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 535 | if (toSrc) { |
| 536 | toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(Vertex), kVertsPerQuad); |
| 537 | } |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 538 | DevToUV.apply<kVertsPerQuad, sizeof(Vertex), sizeof(GrPoint)>(verts); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 539 | } |
| 540 | |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 541 | |
| 542 | // Input Parametric: |
| 543 | // 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) |
| 544 | // Output Implicit: |
| 545 | // Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0 |
| 546 | // A = 4w^2*(y0-y1)(y1-y2)-(y0-y2)^2 |
| 547 | // B = 4w^2*((x0-x1)(y2-y1)+(x1-x2)(y1-y0)) + 2(x0-x2)(y0-y2) |
| 548 | // C = 4w^2(x0-x1)(x1-x2) - (x0-x2)^2 |
| 549 | // D = 4w^2((x0y1-x1y0)(y1-y2)+(x1y2-x2y1)(y0-y1)) + 2(y2-y0)(x0y2-x2y0) |
| 550 | // E = 4w^2((y0x1-y1x0)(x1-x2)+(y1x2-y2x1)(x0-x1)) + 2(x2-x0)(y0x2-y2x0) |
| 551 | // F = 4w^2(x1y2-x2y1)(x0y1-x1y0) - (x2y0-x0y2)^2 |
| 552 | |
| 553 | void set_conic_coeffs(const SkPoint p[3], Vertex verts[kVertsPerQuad], const float weight) { |
| 554 | const float ww4 = 4 * weight * weight; |
| 555 | const float x0Mx1 = p[0].fX - p[1].fX; |
| 556 | const float x1Mx2 = p[1].fX - p[2].fX; |
| 557 | const float x0Mx2 = p[0].fX - p[2].fX; |
| 558 | const float y0My1 = p[0].fY - p[1].fY; |
| 559 | const float y1My2 = p[1].fY - p[2].fY; |
| 560 | const float y0My2 = p[0].fY - p[2].fY; |
| 561 | const float x0y1Mx1y0 = p[0].fX*p[1].fY - p[1].fX*p[0].fY; |
| 562 | const float x1y2Mx2y1 = p[1].fX*p[2].fY - p[2].fX*p[1].fY; |
| 563 | const float x0y2Mx2y0 = p[0].fX*p[2].fY - p[2].fX*p[0].fY; |
| 564 | const float a = ww4 * y0My1 * y1My2 - y0My2 * y0My2; |
| 565 | const float b = -ww4 * (x0Mx1 * y1My2 + x1Mx2 * y0My1) + 2 * x0Mx2 * y0My2; |
| 566 | const float c = ww4 * x0Mx1 * x1Mx2 - x0Mx2 * x0Mx2; |
| 567 | const float d = ww4 * (x0y1Mx1y0 * y1My2 + x1y2Mx2y1 * y0My1) - 2 * y0My2 * x0y2Mx2y0; |
| 568 | const float e = -ww4 * (x0y1Mx1y0 * x1Mx2 + x1y2Mx2y1 * x0Mx1) + 2 * x0Mx2 * x0y2Mx2y0; |
| 569 | const float f = ww4 * x1y2Mx2y1 * x0y1Mx1y0 - x0y2Mx2y0 * x0y2Mx2y0; |
| 570 | |
| 571 | for (int i = 0; i < kVertsPerQuad; ++i) { |
| 572 | verts[i].fConic.fA = a/f; |
| 573 | verts[i].fConic.fB = b/f; |
| 574 | verts[i].fConic.fC = c/f; |
| 575 | verts[i].fConic.fD = d/f; |
| 576 | verts[i].fConic.fE = e/f; |
| 577 | verts[i].fConic.fF = f/f; |
| 578 | } |
| 579 | } |
| 580 | |
| 581 | void add_conics(const SkPoint p[3], |
| 582 | float weight, |
| 583 | const SkMatrix* toDevice, |
| 584 | const SkMatrix* toSrc, |
| 585 | Vertex** vert, |
| 586 | SkRect* devBounds) { |
| 587 | bloat_quad(p, toDevice, toSrc, *vert, devBounds); |
| 588 | set_conic_coeffs(p, *vert, weight); |
| 589 | *vert += kVertsPerQuad; |
| 590 | } |
| 591 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 592 | void add_quads(const SkPoint p[3], |
| 593 | int subdiv, |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 594 | const SkMatrix* toDevice, |
| 595 | const SkMatrix* toSrc, |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 596 | Vertex** vert, |
| 597 | SkRect* devBounds) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 598 | GrAssert(subdiv >= 0); |
| 599 | if (subdiv) { |
| 600 | SkPoint newP[5]; |
| 601 | SkChopQuadAtHalf(p, newP); |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 602 | add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert, devBounds); |
| 603 | add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert, devBounds); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 604 | } else { |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 605 | bloat_quad(p, toDevice, toSrc, *vert, devBounds); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 606 | *vert += kVertsPerQuad; |
| 607 | } |
| 608 | } |
| 609 | |
| 610 | void add_line(const SkPoint p[2], |
| 611 | int rtHeight, |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 612 | const SkMatrix* toSrc, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 613 | Vertex** vert) { |
| 614 | const SkPoint& a = p[0]; |
| 615 | const SkPoint& b = p[1]; |
| 616 | |
| 617 | SkVector orthVec = b; |
| 618 | orthVec -= a; |
| 619 | |
| 620 | if (orthVec.setLength(SK_Scalar1)) { |
| 621 | orthVec.setOrthog(orthVec); |
| 622 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 623 | SkScalar lineC = -(a.dot(orthVec)); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 624 | for (int i = 0; i < kVertsPerLineSeg; ++i) { |
| 625 | (*vert)[i].fPos = (i < 2) ? a : b; |
| 626 | if (0 == i || 3 == i) { |
| 627 | (*vert)[i].fPos -= orthVec; |
| 628 | } else { |
| 629 | (*vert)[i].fPos += orthVec; |
| 630 | } |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 631 | (*vert)[i].fLine.fA = orthVec.fX; |
| 632 | (*vert)[i].fLine.fB = orthVec.fY; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 633 | (*vert)[i].fLine.fC = lineC; |
| 634 | } |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 635 | if (NULL != toSrc) { |
| 636 | toSrc->mapPointsWithStride(&(*vert)->fPos, |
| 637 | sizeof(Vertex), |
| 638 | kVertsPerLineSeg); |
| 639 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 640 | } else { |
| 641 | // just make it degenerate and likely offscreen |
| 642 | (*vert)[0].fPos.set(SK_ScalarMax, SK_ScalarMax); |
| 643 | (*vert)[1].fPos.set(SK_ScalarMax, SK_ScalarMax); |
| 644 | (*vert)[2].fPos.set(SK_ScalarMax, SK_ScalarMax); |
| 645 | (*vert)[3].fPos.set(SK_ScalarMax, SK_ScalarMax); |
| 646 | } |
| 647 | |
| 648 | *vert += kVertsPerLineSeg; |
| 649 | } |
| 650 | |
| 651 | } |
| 652 | |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 653 | /** |
| 654 | * The output of this effect is a hairline edge for conics. |
| 655 | * Conics specified by implicit equation Ax^2 + Bxy + Cy^2 + Dx + Ey + F = 0. |
| 656 | * A, B, C, D are the first vec4 of vertex attributes and |
| 657 | * E and F are the vec2 attached to 2nd vertex attrribute. |
| 658 | * Coverage is max(0, 1-distance). |
| 659 | */ |
| 660 | class HairConicEdgeEffect : public GrEffect { |
| 661 | public: |
| 662 | static GrEffectRef* Create() { |
| 663 | GR_CREATE_STATIC_EFFECT(gHairConicEdgeEffect, HairConicEdgeEffect, ()); |
| 664 | gHairConicEdgeEffect->ref(); |
| 665 | return gHairConicEdgeEffect; |
| 666 | } |
| 667 | |
| 668 | virtual ~HairConicEdgeEffect() {} |
| 669 | |
| 670 | static const char* Name() { return "HairConicEdge"; } |
| 671 | |
| 672 | virtual void getConstantColorComponents(GrColor* color, |
| 673 | uint32_t* validFlags) const SK_OVERRIDE { |
| 674 | *validFlags = 0; |
| 675 | } |
| 676 | |
| 677 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 678 | return GrTBackendEffectFactory<HairConicEdgeEffect>::getInstance(); |
| 679 | } |
| 680 | |
| 681 | class GLEffect : public GrGLEffect { |
| 682 | public: |
| 683 | GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
| 684 | : INHERITED (factory) {} |
| 685 | |
| 686 | virtual void emitCode(GrGLShaderBuilder* builder, |
| 687 | const GrDrawEffect& drawEffect, |
| 688 | EffectKey key, |
| 689 | const char* outputColor, |
| 690 | const char* inputColor, |
| 691 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 692 | const char *vsCoeffABCDName, *fsCoeffABCDName; |
| 693 | const char *vsCoeffEFName, *fsCoeffEFName; |
| 694 | |
| 695 | SkAssertResult(builder->enableFeature( |
| 696 | GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); |
| 697 | builder->addVarying(kVec4f_GrSLType, "ConicCoeffsABCD", |
| 698 | &vsCoeffABCDName, &fsCoeffABCDName); |
| 699 | const SkString* attr0Name = |
| 700 | builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
| 701 | builder->vsCodeAppendf("\t%s = %s;\n", vsCoeffABCDName, attr0Name->c_str()); |
| 702 | |
| 703 | builder->addVarying(kVec2f_GrSLType, "ConicCoeffsEF", |
| 704 | &vsCoeffEFName, &fsCoeffEFName); |
| 705 | const SkString* attr1Name = |
| 706 | builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[1]); |
| 707 | builder->vsCodeAppendf("\t%s = %s;\n", vsCoeffEFName, attr1Name->c_str()); |
| 708 | |
| 709 | // Based on Gustavson 2006: "Beyond the Pixel: towards infinite resolution textures" |
| 710 | builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n"); |
| 711 | |
| 712 | builder->fsCodeAppendf("\t\tvec3 uv1 = vec3(%s.xy, 1);\n", builder->fragmentPosition()); |
| 713 | builder->fsCodeAppend("\t\tvec3 u2uvv2 = uv1.xxy * uv1.xyy;\n"); |
| 714 | builder->fsCodeAppendf("\t\tvec3 ABC = %s.xyz;\n", fsCoeffABCDName); |
| 715 | builder->fsCodeAppendf("\t\tvec3 DEF = vec3(%s.w, %s.xy);\n", |
| 716 | fsCoeffABCDName, fsCoeffEFName); |
| 717 | |
| 718 | builder->fsCodeAppend("\t\tfloat dfdx = dot(uv1,vec3(2.0*ABC.x,ABC.y,DEF.x));\n"); |
| 719 | builder->fsCodeAppend("\t\tfloat dfdy = dot(uv1,vec3(ABC.y, 2.0*ABC.z,DEF.y));\n"); |
| 720 | builder->fsCodeAppend("\t\tfloat gF = dfdx*dfdx + dfdy*dfdy;\n"); |
| 721 | builder->fsCodeAppend("\t\tedgeAlpha = dot(ABC,u2uvv2) + dot(DEF,uv1);\n"); |
| 722 | builder->fsCodeAppend("\t\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / gF);\n"); |
| 723 | builder->fsCodeAppend("\t\tedgeAlpha = max((1.0 - edgeAlpha), 0.0);\n"); |
| 724 | // Add line below for smooth cubic ramp |
| 725 | // builder->fsCodeAppend("\t\tedgeAlpha = edgeAlpha*edgeAlpha*(3.0-2.0*edgeAlpha);\n"); |
| 726 | |
| 727 | SkString modulate; |
| 728 | GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha"); |
| 729 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); |
| 730 | } |
| 731 | |
| 732 | static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { |
| 733 | return 0x0; |
| 734 | } |
| 735 | |
| 736 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {} |
| 737 | |
| 738 | private: |
| 739 | typedef GrGLEffect INHERITED; |
| 740 | }; |
| 741 | |
| 742 | private: |
| 743 | HairConicEdgeEffect() { |
| 744 | this->addVertexAttrib(kVec4f_GrSLType); |
| 745 | this->addVertexAttrib(kVec2f_GrSLType); |
| 746 | this->setWillReadFragmentPosition(); |
| 747 | } |
| 748 | |
| 749 | virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { |
| 750 | return true; |
| 751 | } |
| 752 | |
| 753 | GR_DECLARE_EFFECT_TEST; |
| 754 | |
| 755 | typedef GrEffect INHERITED; |
| 756 | }; |
| 757 | |
| 758 | GR_DEFINE_EFFECT_TEST(HairConicEdgeEffect); |
| 759 | |
| 760 | GrEffectRef* HairConicEdgeEffect::TestCreate(SkMWCRandom* random, |
| 761 | GrContext*, |
| 762 | const GrDrawTargetCaps& caps, |
| 763 | GrTexture*[]) { |
| 764 | return HairConicEdgeEffect::Create(); |
| 765 | } |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 766 | /////////////////////////////////////////////////////////////////////////////// |
| 767 | |
| 768 | /** |
| 769 | * The output of this effect is a hairline edge for quadratics. |
| 770 | * Quadratic specified by 0=u^2-v canonical coords. u and v are the first |
| 771 | * two components of the vertex attribute. Uses unsigned distance. |
skia.committer@gmail.com | 041e2db | 2013-04-03 07:01:14 +0000 | [diff] [blame] | 772 | * Coverage is min(0, 1-distance). 3rd & 4th component unused. |
| 773 | * Requires shader derivative instruction support. |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 774 | */ |
| 775 | class HairQuadEdgeEffect : public GrEffect { |
| 776 | public: |
| 777 | |
| 778 | static GrEffectRef* Create() { |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 779 | GR_CREATE_STATIC_EFFECT(gHairQuadEdgeEffect, HairQuadEdgeEffect, ()); |
| 780 | gHairQuadEdgeEffect->ref(); |
| 781 | return gHairQuadEdgeEffect; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 782 | } |
| 783 | |
| 784 | virtual ~HairQuadEdgeEffect() {} |
| 785 | |
| 786 | static const char* Name() { return "HairQuadEdge"; } |
| 787 | |
skia.committer@gmail.com | 041e2db | 2013-04-03 07:01:14 +0000 | [diff] [blame] | 788 | virtual void getConstantColorComponents(GrColor* color, |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 789 | uint32_t* validFlags) const SK_OVERRIDE { |
| 790 | *validFlags = 0; |
| 791 | } |
| 792 | |
| 793 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 794 | return GrTBackendEffectFactory<HairQuadEdgeEffect>::getInstance(); |
| 795 | } |
| 796 | |
| 797 | class GLEffect : public GrGLEffect { |
| 798 | public: |
| 799 | GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
| 800 | : INHERITED (factory) {} |
| 801 | |
| 802 | virtual void emitCode(GrGLShaderBuilder* builder, |
| 803 | const GrDrawEffect& drawEffect, |
| 804 | EffectKey key, |
| 805 | const char* outputColor, |
| 806 | const char* inputColor, |
| 807 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 808 | const char *vsName, *fsName; |
| 809 | const SkString* attrName = |
| 810 | builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
| 811 | builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n"); |
| 812 | |
| 813 | SkAssertResult(builder->enableFeature( |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 814 | GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 815 | builder->addVarying(kVec4f_GrSLType, "HairQuadEdge", &vsName, &fsName); |
| 816 | |
| 817 | builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName); |
| 818 | builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName); |
| 819 | builder->fsCodeAppendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n" |
| 820 | "\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n", |
| 821 | fsName, fsName); |
| 822 | builder->fsCodeAppendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, |
| 823 | fsName); |
| 824 | builder->fsCodeAppend("\t\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / dot(gF, gF));\n"); |
| 825 | builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"); |
| 826 | |
| 827 | SkString modulate; |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 828 | GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha"); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 829 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); |
| 830 | |
| 831 | builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str()); |
| 832 | } |
| 833 | |
| 834 | static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { |
| 835 | return 0x0; |
| 836 | } |
| 837 | |
| 838 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {} |
| 839 | |
| 840 | private: |
| 841 | typedef GrGLEffect INHERITED; |
| 842 | }; |
| 843 | |
| 844 | private: |
skia.committer@gmail.com | 041e2db | 2013-04-03 07:01:14 +0000 | [diff] [blame] | 845 | HairQuadEdgeEffect() { |
| 846 | this->addVertexAttrib(kVec4f_GrSLType); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 847 | } |
| 848 | |
| 849 | virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { |
| 850 | return true; |
| 851 | } |
| 852 | |
| 853 | GR_DECLARE_EFFECT_TEST; |
| 854 | |
| 855 | typedef GrEffect INHERITED; |
| 856 | }; |
| 857 | |
| 858 | GR_DEFINE_EFFECT_TEST(HairQuadEdgeEffect); |
| 859 | |
| 860 | GrEffectRef* HairQuadEdgeEffect::TestCreate(SkMWCRandom* random, |
| 861 | GrContext*, |
| 862 | const GrDrawTargetCaps& caps, |
| 863 | GrTexture*[]) { |
| 864 | // Doesn't work without derivative instructions. |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 865 | return caps.shaderDerivativeSupport() ? HairQuadEdgeEffect::Create() : NULL; |
| 866 | } |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 867 | |
| 868 | /////////////////////////////////////////////////////////////////////////////// |
| 869 | |
| 870 | /** |
skia.committer@gmail.com | 041e2db | 2013-04-03 07:01:14 +0000 | [diff] [blame] | 871 | * The output of this effect is a 1-pixel wide line. |
| 872 | * Input is 2D implicit device coord line eq (a*x + b*y +c = 0). 4th component unused. |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 873 | */ |
| 874 | class HairLineEdgeEffect : public GrEffect { |
| 875 | public: |
| 876 | |
| 877 | static GrEffectRef* Create() { |
bsalomon@google.com | d42aca3 | 2013-04-23 15:37:27 +0000 | [diff] [blame] | 878 | GR_CREATE_STATIC_EFFECT(gHairLineEdge, HairLineEdgeEffect, ()); |
| 879 | gHairLineEdge->ref(); |
| 880 | return gHairLineEdge; |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | virtual ~HairLineEdgeEffect() {} |
| 884 | |
| 885 | static const char* Name() { return "HairLineEdge"; } |
| 886 | |
skia.committer@gmail.com | 041e2db | 2013-04-03 07:01:14 +0000 | [diff] [blame] | 887 | virtual void getConstantColorComponents(GrColor* color, |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 888 | uint32_t* validFlags) const SK_OVERRIDE { |
| 889 | *validFlags = 0; |
| 890 | } |
| 891 | |
| 892 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 893 | return GrTBackendEffectFactory<HairLineEdgeEffect>::getInstance(); |
| 894 | } |
| 895 | |
| 896 | class GLEffect : public GrGLEffect { |
| 897 | public: |
| 898 | GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
| 899 | : INHERITED (factory) {} |
| 900 | |
| 901 | virtual void emitCode(GrGLShaderBuilder* builder, |
| 902 | const GrDrawEffect& drawEffect, |
| 903 | EffectKey key, |
| 904 | const char* outputColor, |
| 905 | const char* inputColor, |
| 906 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 907 | const char *vsName, *fsName; |
| 908 | const SkString* attrName = |
| 909 | builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
| 910 | builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n"); |
| 911 | |
| 912 | builder->addVarying(kVec4f_GrSLType, "HairLineEdge", &vsName, &fsName); |
| 913 | |
| 914 | builder->fsCodeAppendf("\t\tedgeAlpha = abs(dot(vec3(%s.xy,1), %s.xyz));\n", |
| 915 | builder->fragmentPosition(), fsName); |
| 916 | builder->fsCodeAppendf("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"); |
| 917 | |
| 918 | SkString modulate; |
bsalomon@google.com | 018f179 | 2013-04-18 19:36:09 +0000 | [diff] [blame] | 919 | GrGLSLModulatef<4>(&modulate, inputColor, "edgeAlpha"); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 920 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); |
| 921 | |
| 922 | builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str()); |
| 923 | } |
| 924 | |
| 925 | static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { |
| 926 | return 0x0; |
| 927 | } |
| 928 | |
| 929 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {} |
| 930 | |
| 931 | private: |
| 932 | typedef GrGLEffect INHERITED; |
| 933 | }; |
| 934 | |
| 935 | private: |
skia.committer@gmail.com | 041e2db | 2013-04-03 07:01:14 +0000 | [diff] [blame] | 936 | HairLineEdgeEffect() { |
| 937 | this->addVertexAttrib(kVec4f_GrSLType); |
commit-bot@chromium.org | 8d47ddc | 2013-05-09 14:55:46 +0000 | [diff] [blame] | 938 | this->setWillReadFragmentPosition(); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 939 | } |
| 940 | |
| 941 | virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { |
| 942 | return true; |
| 943 | } |
| 944 | |
| 945 | GR_DECLARE_EFFECT_TEST; |
| 946 | |
| 947 | typedef GrEffect INHERITED; |
| 948 | }; |
| 949 | |
| 950 | GR_DEFINE_EFFECT_TEST(HairLineEdgeEffect); |
| 951 | |
| 952 | GrEffectRef* HairLineEdgeEffect::TestCreate(SkMWCRandom* random, |
| 953 | GrContext*, |
| 954 | const GrDrawTargetCaps& caps, |
| 955 | GrTexture*[]) { |
| 956 | return HairLineEdgeEffect::Create(); |
| 957 | } |
| 958 | |
| 959 | /////////////////////////////////////////////////////////////////////////////// |
| 960 | |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 961 | namespace { |
| 962 | |
| 963 | // position + edge |
| 964 | extern const GrVertexAttrib gHairlineAttribs[] = { |
| 965 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
| 966 | {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding} |
| 967 | }; |
| 968 | |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 969 | // Conic |
| 970 | // position + ABCD + EF |
| 971 | extern const GrVertexAttrib gConicVertexAttribs[] = { |
| 972 | { kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding }, |
| 973 | { kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding }, |
| 974 | { kVec2f_GrVertexAttribType, 3*sizeof(GrPoint), kEffect_GrVertexAttribBinding } |
| 975 | }; |
robertphillips@google.com | 4290330 | 2013-04-20 12:26:07 +0000 | [diff] [blame] | 976 | }; |
| 977 | |
bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 978 | bool GrAAHairLinePathRenderer::createGeom( |
| 979 | const SkPath& path, |
bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 980 | GrDrawTarget* target, |
bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 981 | int* lineCnt, |
| 982 | int* quadCnt, |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 983 | int* conicCnt, |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 984 | GrDrawTarget::AutoReleaseGeometry* arg, |
| 985 | SkRect* devBounds) { |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 986 | GrDrawState* drawState = target->drawState(); |
| 987 | int rtHeight = drawState->getRenderTarget()->height(); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 988 | |
commit-bot@chromium.org | fd03d4a | 2013-07-17 21:39:42 +0000 | [diff] [blame^] | 989 | SkIRect devClipBounds; |
commit-bot@chromium.org | 912e68e | 2013-05-24 18:51:55 +0000 | [diff] [blame] | 990 | target->getClip()->getConservativeBounds(drawState->getRenderTarget(), &devClipBounds); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 991 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 992 | SkMatrix viewM = drawState->getViewMatrix(); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 993 | |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 994 | // All the vertices that we compute are within 1 of path control points with the exception of |
| 995 | // one of the bounding vertices for each quad. The add_quads() function will update the bounds |
| 996 | // for each quad added. |
| 997 | *devBounds = path.getBounds(); |
| 998 | viewM.mapRect(devBounds); |
| 999 | devBounds->outset(SK_Scalar1, SK_Scalar1); |
| 1000 | |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 1001 | PREALLOC_PTARRAY(128) lines; |
| 1002 | PREALLOC_PTARRAY(128) quads; |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 1003 | PREALLOC_PTARRAY(128) conics; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1004 | IntArray qSubdivs; |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 1005 | FloatArray cWeights; |
bsalomon@google.com | 0f11e1a | 2012-10-08 14:48:36 +0000 | [diff] [blame] | 1006 | *quadCnt = generate_lines_and_quads(path, viewM, devClipBounds, |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 1007 | &lines, &quads, &conics, &qSubdivs, &cWeights); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1008 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 1009 | *lineCnt = lines.count() / 2; |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 1010 | *conicCnt = conics.count() / 3; |
| 1011 | int vertCnt = kVertsPerLineSeg * *lineCnt + kVertsPerQuad * *quadCnt + |
| 1012 | kVertsPerQuad * *conicCnt; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1013 | |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 1014 | target->drawState()->setVertexAttribs<gConicVertexAttribs>(SK_ARRAY_COUNT(gConicVertexAttribs)); |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 1015 | GrAssert(sizeof(Vertex) == target->getDrawState().getVertexSize()); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1016 | |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 1017 | if (!arg->set(target, vertCnt, 0)) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1018 | return false; |
| 1019 | } |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 1020 | |
bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 1021 | Vertex* verts = reinterpret_cast<Vertex*>(arg->vertices()); |
| 1022 | |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 1023 | const SkMatrix* toDevice = NULL; |
| 1024 | const SkMatrix* toSrc = NULL; |
| 1025 | SkMatrix ivm; |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 1026 | |
| 1027 | if (viewM.hasPerspective()) { |
| 1028 | if (viewM.invert(&ivm)) { |
| 1029 | toDevice = &viewM; |
| 1030 | toSrc = &ivm; |
| 1031 | } |
| 1032 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1033 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 1034 | for (int i = 0; i < *lineCnt; ++i) { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 1035 | add_line(&lines[2*i], rtHeight, toSrc, &verts); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1036 | } |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 1037 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1038 | int unsubdivQuadCnt = quads.count() / 3; |
| 1039 | for (int i = 0; i < unsubdivQuadCnt; ++i) { |
| 1040 | GrAssert(qSubdivs[i] >= 0); |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 1041 | add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts, devBounds); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1042 | } |
| 1043 | |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 1044 | // Start Conics |
| 1045 | for (int i = 0; i < *conicCnt; ++i) { |
| 1046 | add_conics(&conics[3*i], cWeights[i], toDevice, toSrc, &verts, devBounds); |
| 1047 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1048 | return true; |
| 1049 | } |
| 1050 | |
robertphillips@google.com | 8a4fc40 | 2012-05-24 12:42:24 +0000 | [diff] [blame] | 1051 | bool GrAAHairLinePathRenderer::canDrawPath(const SkPath& path, |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 1052 | const SkStrokeRec& stroke, |
robertphillips@google.com | 8a4fc40 | 2012-05-24 12:42:24 +0000 | [diff] [blame] | 1053 | const GrDrawTarget* target, |
| 1054 | bool antiAlias) const { |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 1055 | if (!stroke.isHairlineStyle() || !antiAlias) { |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 1056 | return false; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1057 | } |
| 1058 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 1059 | static const uint32_t gReqDerivMask = SkPath::kCubic_SegmentMask | |
| 1060 | SkPath::kQuad_SegmentMask; |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 1061 | if (!target->caps()->shaderDerivativeSupport() && |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 1062 | (gReqDerivMask & path.getSegmentMasks())) { |
| 1063 | return false; |
| 1064 | } |
| 1065 | return true; |
| 1066 | } |
| 1067 | |
| 1068 | bool GrAAHairLinePathRenderer::onDrawPath(const SkPath& path, |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 1069 | const SkStrokeRec&, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 1070 | GrDrawTarget* target, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 1071 | bool antiAlias) { |
| 1072 | |
| 1073 | int lineCnt; |
| 1074 | int quadCnt; |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 1075 | int conicCnt; |
bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 1076 | GrDrawTarget::AutoReleaseGeometry arg; |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 1077 | SkRect devBounds; |
| 1078 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 1079 | if (!this->createGeom(path, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 1080 | target, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 1081 | &lineCnt, |
bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 1082 | &quadCnt, |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 1083 | &conicCnt, |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 1084 | &arg, |
| 1085 | &devBounds)) { |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 1086 | return false; |
| 1087 | } |
| 1088 | |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 1089 | GrDrawTarget::AutoStateRestore asr; |
bsalomon@google.com | 4647f90 | 2013-03-26 14:45:27 +0000 | [diff] [blame] | 1090 | |
bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 1091 | // createGeom transforms the geometry to device space when the matrix does not have |
| 1092 | // perspective. |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 1093 | if (target->getDrawState().getViewMatrix().hasPerspective()) { |
| 1094 | asr.set(target, GrDrawTarget::kPreserve_ASRInit); |
| 1095 | } else if (!asr.setIdentity(target, GrDrawTarget::kPreserve_ASRInit)) { |
| 1096 | return false; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1097 | } |
bsalomon@google.com | 137f134 | 2013-05-29 21:27:53 +0000 | [diff] [blame] | 1098 | GrDrawState* drawState = target->drawState(); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 1099 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1100 | // TODO: See whether rendering lines as degenerate quads improves perf |
| 1101 | // when we have a mix |
bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 1102 | |
bsalomon@google.com | 4647f90 | 2013-03-26 14:45:27 +0000 | [diff] [blame] | 1103 | static const int kEdgeAttrIndex = 1; |
bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 1104 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 1105 | GrEffectRef* hairLineEffect = HairLineEdgeEffect::Create(); |
| 1106 | GrEffectRef* hairQuadEffect = HairQuadEdgeEffect::Create(); |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 1107 | GrEffectRef* hairConicEffect = HairConicEdgeEffect::Create(); |
skia.committer@gmail.com | 37cbc7f | 2013-03-27 07:01:04 +0000 | [diff] [blame] | 1108 | |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 1109 | // Check devBounds |
| 1110 | #if GR_DEBUG |
| 1111 | SkRect tolDevBounds = devBounds; |
| 1112 | tolDevBounds.outset(SK_Scalar1 / 10000, SK_Scalar1 / 10000); |
| 1113 | SkRect actualBounds; |
| 1114 | Vertex* verts = reinterpret_cast<Vertex*>(arg.vertices()); |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 1115 | int vCount = kVertsPerLineSeg * lineCnt + kVertsPerQuad * quadCnt + kVertsPerQuad * conicCnt; |
bsalomon@google.com | 1dd9baa | 2013-05-20 16:49:06 +0000 | [diff] [blame] | 1116 | bool first = true; |
| 1117 | for (int i = 0; i < vCount; ++i) { |
| 1118 | SkPoint pos = verts[i].fPos; |
| 1119 | // This is a hack to workaround the fact that we move some degenerate segments offscreen. |
| 1120 | if (SK_ScalarMax == pos.fX) { |
| 1121 | continue; |
| 1122 | } |
| 1123 | drawState->getViewMatrix().mapPoints(&pos, 1); |
| 1124 | if (first) { |
| 1125 | actualBounds.set(pos.fX, pos.fY, pos.fX, pos.fY); |
| 1126 | first = false; |
| 1127 | } else { |
| 1128 | actualBounds.growToInclude(pos.fX, pos.fY); |
| 1129 | } |
| 1130 | } |
| 1131 | if (!first) { |
| 1132 | GrAssert(tolDevBounds.contains(actualBounds)); |
| 1133 | } |
| 1134 | #endif |
| 1135 | |
bsalomon@google.com | eb6879f | 2013-06-13 19:34:18 +0000 | [diff] [blame] | 1136 | { |
| 1137 | GrDrawState::AutoRestoreEffects are(drawState); |
| 1138 | target->setIndexSourceToBuffer(fLinesIndexBuffer); |
| 1139 | int lines = 0; |
| 1140 | int nBufLines = fLinesIndexBuffer->maxQuads(); |
| 1141 | drawState->addCoverageEffect(hairLineEffect, kEdgeAttrIndex)->unref(); |
| 1142 | while (lines < lineCnt) { |
| 1143 | int n = GrMin(lineCnt - lines, nBufLines); |
| 1144 | target->drawIndexed(kTriangles_GrPrimitiveType, |
| 1145 | kVertsPerLineSeg*lines, // startV |
| 1146 | 0, // startI |
| 1147 | kVertsPerLineSeg*n, // vCount |
| 1148 | kIdxsPerLineSeg*n, |
| 1149 | &devBounds); // iCount |
| 1150 | lines += n; |
| 1151 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
egdaniel@google.com | 5383a75 | 2013-07-12 20:15:34 +0000 | [diff] [blame] | 1154 | { |
| 1155 | GrDrawState::AutoRestoreEffects are(drawState); |
| 1156 | target->setIndexSourceToBuffer(fQuadsIndexBuffer); |
| 1157 | int quads = 0; |
| 1158 | drawState->addCoverageEffect(hairQuadEffect, kEdgeAttrIndex)->unref(); |
| 1159 | while (quads < quadCnt) { |
| 1160 | int n = GrMin(quadCnt - quads, kNumQuadsInIdxBuffer); |
| 1161 | target->drawIndexed(kTriangles_GrPrimitiveType, |
| 1162 | kVertsPerLineSeg * lineCnt + kVertsPerQuad*quads, // startV |
| 1163 | 0, // startI |
| 1164 | kVertsPerQuad*n, // vCount |
| 1165 | kIdxsPerQuad*n, // iCount |
| 1166 | &devBounds); |
| 1167 | quads += n; |
| 1168 | } |
| 1169 | } |
| 1170 | |
| 1171 | { |
| 1172 | GrDrawState::AutoRestoreEffects are(drawState); |
| 1173 | int conics = 0; |
| 1174 | drawState->addCoverageEffect(hairConicEffect, 1, 2)->unref(); |
| 1175 | while (conics < conicCnt) { |
| 1176 | int n = GrMin(conicCnt - conics, kNumQuadsInIdxBuffer); |
| 1177 | target->drawIndexed(kTriangles_GrPrimitiveType, |
| 1178 | kVertsPerLineSeg*lineCnt + |
| 1179 | kVertsPerQuad*(quadCnt + conics), // startV |
| 1180 | 0, // startI |
| 1181 | kVertsPerQuad*n, // vCount |
| 1182 | kIdxsPerQuad*n, // iCount |
| 1183 | &devBounds); |
| 1184 | conics += n; |
| 1185 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1186 | } |
bsalomon@google.com | 0406b9e | 2013-04-02 21:00:15 +0000 | [diff] [blame] | 1187 | target->resetIndexSource(); |
bsalomon@google.com | 4647f90 | 2013-03-26 14:45:27 +0000 | [diff] [blame] | 1188 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 1189 | return true; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 1190 | } |