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 | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 13 | #include "GrGpu.h" |
| 14 | #include "GrIndexBuffer.h" |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 15 | #include "GrPathUtils.h" |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 16 | #include "SkGeometry.h" |
| 17 | #include "SkTemplates.h" |
| 18 | |
| 19 | namespace { |
| 20 | // quadratics are rendered as 5-sided polys in order to bound the |
| 21 | // AA stroke around the center-curve. See comments in push_quad_index_buffer and |
| 22 | // bloat_quad. |
| 23 | static const int kVertsPerQuad = 5; |
| 24 | static const int kIdxsPerQuad = 9; |
| 25 | |
| 26 | static const int kVertsPerLineSeg = 4; |
| 27 | static const int kIdxsPerLineSeg = 6; |
| 28 | |
| 29 | static const int kNumQuadsInIdxBuffer = 256; |
| 30 | static const size_t kQuadIdxSBufize = kIdxsPerQuad * |
| 31 | sizeof(uint16_t) * |
| 32 | kNumQuadsInIdxBuffer; |
| 33 | |
| 34 | bool push_quad_index_data(GrIndexBuffer* qIdxBuffer) { |
| 35 | uint16_t* data = (uint16_t*) qIdxBuffer->lock(); |
| 36 | bool tempData = NULL == data; |
| 37 | if (tempData) { |
| 38 | data = new uint16_t[kNumQuadsInIdxBuffer * kIdxsPerQuad]; |
| 39 | } |
| 40 | for (int i = 0; i < kNumQuadsInIdxBuffer; ++i) { |
| 41 | |
| 42 | // Each quadratic is rendered as a five sided polygon. This poly bounds |
| 43 | // the quadratic's bounding triangle but has been expanded so that the |
| 44 | // 1-pixel wide area around the curve is inside the poly. |
| 45 | // If a,b,c are the original control points then the poly a0,b0,c0,c1,a1 |
| 46 | // that is rendered would look like this: |
| 47 | // b0 |
| 48 | // b |
| 49 | // |
| 50 | // a0 c0 |
| 51 | // a c |
| 52 | // a1 c1 |
| 53 | // Each is drawn as three triagnles specified by these 9 indices: |
| 54 | int baseIdx = i * kIdxsPerQuad; |
| 55 | uint16_t baseVert = (uint16_t)(i * kVertsPerQuad); |
| 56 | data[0 + baseIdx] = baseVert + 0; // a0 |
| 57 | data[1 + baseIdx] = baseVert + 1; // a1 |
| 58 | data[2 + baseIdx] = baseVert + 2; // b0 |
| 59 | data[3 + baseIdx] = baseVert + 2; // b0 |
| 60 | data[4 + baseIdx] = baseVert + 4; // c1 |
| 61 | data[5 + baseIdx] = baseVert + 3; // c0 |
| 62 | data[6 + baseIdx] = baseVert + 1; // a1 |
| 63 | data[7 + baseIdx] = baseVert + 4; // c1 |
| 64 | data[8 + baseIdx] = baseVert + 2; // b0 |
| 65 | } |
| 66 | if (tempData) { |
| 67 | bool ret = qIdxBuffer->updateData(data, kQuadIdxSBufize); |
| 68 | delete[] data; |
| 69 | return ret; |
| 70 | } else { |
| 71 | qIdxBuffer->unlock(); |
| 72 | return true; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | GrPathRenderer* GrAAHairLinePathRenderer::Create(GrContext* context) { |
bsalomon@google.com | a8a6a32 | 2011-09-23 14:19:58 +0000 | [diff] [blame] | 78 | const GrIndexBuffer* lIdxBuffer = context->getQuadIndexBuffer(); |
| 79 | if (NULL == lIdxBuffer) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 80 | return NULL; |
| 81 | } |
bsalomon@google.com | a8a6a32 | 2011-09-23 14:19:58 +0000 | [diff] [blame] | 82 | GrGpu* gpu = context->getGpu(); |
| 83 | GrIndexBuffer* qIdxBuf = gpu->createIndexBuffer(kQuadIdxSBufize, false); |
| 84 | SkAutoTUnref<GrIndexBuffer> qIdxBuffer(qIdxBuf); |
| 85 | if (NULL == qIdxBuf || |
| 86 | !push_quad_index_data(qIdxBuf)) { |
| 87 | return NULL; |
| 88 | } |
| 89 | return new GrAAHairLinePathRenderer(context, |
| 90 | lIdxBuffer, |
| 91 | qIdxBuf); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | GrAAHairLinePathRenderer::GrAAHairLinePathRenderer( |
| 95 | const GrContext* context, |
| 96 | const GrIndexBuffer* linesIndexBuffer, |
| 97 | const GrIndexBuffer* quadsIndexBuffer) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 98 | fLinesIndexBuffer = linesIndexBuffer; |
| 99 | linesIndexBuffer->ref(); |
| 100 | fQuadsIndexBuffer = quadsIndexBuffer; |
| 101 | quadsIndexBuffer->ref(); |
| 102 | this->resetGeom(); |
| 103 | } |
| 104 | |
| 105 | GrAAHairLinePathRenderer::~GrAAHairLinePathRenderer() { |
| 106 | fLinesIndexBuffer->unref(); |
| 107 | fQuadsIndexBuffer->unref(); |
| 108 | } |
| 109 | |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 110 | bool GrAAHairLinePathRenderer::canDrawPath(const GrDrawTarget::Caps& targetCaps, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 111 | const SkPath& path, |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 112 | GrPathFill fill, |
| 113 | bool antiAlias) const { |
bsalomon@google.com | a8a6a32 | 2011-09-23 14:19:58 +0000 | [diff] [blame] | 114 | static const uint32_t gReqDerivMask = SkPath::kCubic_SegmentMask | |
| 115 | SkPath::kQuad_SegmentMask; |
| 116 | return (kHairLine_PathFill == fill && |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame] | 117 | antiAlias && |
| 118 | (targetCaps.fShaderDerivativeSupport || |
bsalomon@google.com | a8a6a32 | 2011-09-23 14:19:58 +0000 | [diff] [blame] | 119 | !(gReqDerivMask & path.getSegmentMasks()))); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void GrAAHairLinePathRenderer::pathWillClear() { |
| 123 | this->resetGeom(); |
| 124 | } |
| 125 | |
| 126 | void GrAAHairLinePathRenderer::resetGeom() { |
| 127 | fPreviousStages = ~0; |
| 128 | fPreviousRTHeight = ~0; |
| 129 | fPreviousViewMatrix = GrMatrix::InvalidMatrix(); |
| 130 | fLineSegmentCnt = 0; |
| 131 | fQuadCnt = 0; |
| 132 | if ((fQuadCnt || fLineSegmentCnt) && NULL != fTarget) { |
| 133 | fTarget->resetVertexSource(); |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | namespace { |
| 138 | |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 139 | typedef SkTArray<SkPoint, true> PtArray; |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 140 | #define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true> |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 141 | typedef SkTArray<int, true> IntArray; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 142 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 143 | // Takes 178th time of logf on Z600 / VC2010 |
| 144 | int get_float_exp(float x) { |
| 145 | GR_STATIC_ASSERT(sizeof(int) == sizeof(float)); |
| 146 | #if GR_DEBUG |
| 147 | static bool tested; |
| 148 | if (!tested) { |
| 149 | tested = true; |
| 150 | GrAssert(get_float_exp(0.25f) == -2); |
| 151 | GrAssert(get_float_exp(0.3f) == -2); |
| 152 | GrAssert(get_float_exp(0.5f) == -1); |
| 153 | GrAssert(get_float_exp(1.f) == 0); |
| 154 | GrAssert(get_float_exp(2.f) == 1); |
| 155 | GrAssert(get_float_exp(2.5f) == 1); |
| 156 | GrAssert(get_float_exp(8.f) == 3); |
| 157 | GrAssert(get_float_exp(100.f) == 6); |
| 158 | GrAssert(get_float_exp(1000.f) == 9); |
| 159 | GrAssert(get_float_exp(1024.f) == 10); |
| 160 | GrAssert(get_float_exp(3000000.f) == 21); |
| 161 | } |
| 162 | #endif |
bsalomon@google.com | 2ec7280 | 2011-09-21 21:46:03 +0000 | [diff] [blame] | 163 | const int* iptr = (const int*)&x; |
| 164 | return (((*iptr) & 0x7f800000) >> 23) - 127; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | // we subdivide the quads to avoid huge overfill |
| 168 | // if it returns -1 then should be drawn as lines |
| 169 | int num_quad_subdivs(const SkPoint p[3]) { |
| 170 | static const SkScalar gDegenerateToLineTol = SK_Scalar1; |
bsalomon@google.com | 46a2a1e | 2011-09-06 22:10:52 +0000 | [diff] [blame] | 171 | static const SkScalar gDegenerateToLineTolSqd = |
| 172 | SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 173 | |
bsalomon@google.com | 46a2a1e | 2011-09-06 22:10:52 +0000 | [diff] [blame] | 174 | if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd || |
| 175 | p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 176 | return -1; |
| 177 | } |
bsalomon@google.com | 46a2a1e | 2011-09-06 22:10:52 +0000 | [diff] [blame] | 178 | |
| 179 | GrScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]); |
| 180 | if (dsqd < gDegenerateToLineTolSqd) { |
| 181 | return -1; |
| 182 | } |
| 183 | |
| 184 | if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 185 | return -1; |
| 186 | } |
| 187 | |
| 188 | static const int kMaxSub = 4; |
| 189 | // tolerance of triangle height in pixels |
| 190 | // tuned on windows Quadro FX 380 / Z600 |
| 191 | // trade off of fill vs cpu time on verts |
| 192 | // maybe different when do this using gpu (geo or tess shaders) |
| 193 | static const SkScalar gSubdivTol = 175 * SK_Scalar1; |
| 194 | |
| 195 | if (dsqd <= gSubdivTol*gSubdivTol) { |
| 196 | return 0; |
| 197 | } else { |
| 198 | // subdividing the quad reduces d by 4. so we want x = log4(d/tol) |
| 199 | // = log4(d*d/tol*tol)/2 |
| 200 | // = log2(d*d/tol*tol) |
| 201 | |
| 202 | #ifdef SK_SCALAR_IS_FLOAT |
| 203 | // +1 since we're ignoring the mantissa contribution. |
| 204 | int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1; |
| 205 | log = GrMin(GrMax(0, log), kMaxSub); |
| 206 | return log; |
| 207 | #else |
bsalomon@google.com | fcb0dbc | 2011-08-30 18:35:18 +0000 | [diff] [blame] | 208 | SkScalar log = SkScalarLog(SkScalarDiv(dsqd,gSubdivTol*gSubdivTol)); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 209 | static const SkScalar conv = SkScalarInvert(SkScalarLog(2)); |
| 210 | log = SkScalarMul(log, conv); |
| 211 | return GrMin(GrMax(0, SkScalarCeilToInt(log)),kMaxSub); |
| 212 | #endif |
| 213 | } |
| 214 | } |
| 215 | |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 216 | /** |
| 217 | * Generates the lines and quads to be rendered. Lines are always recorded in |
| 218 | * device space. We will do a device space bloat to account for the 1pixel |
| 219 | * thickness. |
| 220 | * Quads are recorded in device space unless m contains |
| 221 | * perspective, then in they are in src space. We do this because we will |
| 222 | * subdivide large quads to reduce over-fill. This subdivision has to be |
| 223 | * performed before applying the perspective matrix. |
| 224 | */ |
| 225 | int generate_lines_and_quads(const SkPath& path, |
| 226 | const SkMatrix& m, |
| 227 | const SkVector& translate, |
| 228 | GrIRect clip, |
| 229 | PtArray* lines, |
| 230 | PtArray* quads, |
| 231 | IntArray* quadSubdivCnts) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 232 | SkPath::Iter iter(path, false); |
| 233 | |
| 234 | int totalQuadCount = 0; |
| 235 | GrRect bounds; |
| 236 | GrIRect ibounds; |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 237 | |
| 238 | bool persp = m.hasPerspective(); |
| 239 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 240 | for (;;) { |
| 241 | GrPoint pts[4]; |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 242 | GrPoint devPts[4]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 243 | GrPathCmd cmd = (GrPathCmd)iter.next(pts); |
| 244 | switch (cmd) { |
| 245 | case kMove_PathCmd: |
| 246 | break; |
| 247 | case kLine_PathCmd: |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 248 | SkPoint::Offset(pts, 2, translate); |
| 249 | m.mapPoints(devPts, pts, 2); |
| 250 | bounds.setBounds(devPts, 2); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 251 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 252 | bounds.roundOut(&ibounds); |
| 253 | if (SkIRect::Intersects(clip, ibounds)) { |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 254 | SkPoint* pts = lines->push_back_n(2); |
| 255 | pts[0] = devPts[0]; |
| 256 | pts[1] = devPts[1]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 257 | } |
| 258 | break; |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 259 | case kQuadratic_PathCmd: |
| 260 | SkPoint::Offset(pts, 3, translate); |
| 261 | m.mapPoints(devPts, pts, 3); |
| 262 | bounds.setBounds(devPts, 3); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 263 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 264 | bounds.roundOut(&ibounds); |
| 265 | if (SkIRect::Intersects(clip, ibounds)) { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 266 | int subdiv = num_quad_subdivs(devPts); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 267 | GrAssert(subdiv >= -1); |
| 268 | if (-1 == subdiv) { |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 269 | SkPoint* pts = lines->push_back_n(4); |
| 270 | pts[0] = devPts[0]; |
| 271 | pts[1] = devPts[1]; |
| 272 | pts[2] = devPts[1]; |
| 273 | pts[3] = devPts[2]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 274 | } else { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 275 | // when in perspective keep quads in src space |
| 276 | SkPoint* qPts = persp ? pts : devPts; |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 277 | SkPoint* pts = quads->push_back_n(3); |
| 278 | pts[0] = qPts[0]; |
| 279 | pts[1] = qPts[1]; |
| 280 | pts[2] = qPts[2]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 281 | quadSubdivCnts->push_back() = subdiv; |
| 282 | totalQuadCount += 1 << subdiv; |
| 283 | } |
| 284 | } |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 285 | break; |
| 286 | case kCubic_PathCmd: |
| 287 | SkPoint::Offset(pts, 4, translate); |
| 288 | m.mapPoints(devPts, pts, 4); |
| 289 | bounds.setBounds(devPts, 4); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 290 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 291 | bounds.roundOut(&ibounds); |
| 292 | if (SkIRect::Intersects(clip, ibounds)) { |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 293 | PREALLOC_PTARRAY(32) q; |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame^] | 294 | // We convert cubics to quadratics (for now). |
| 295 | // In perspective have to do conversion in src space. |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 296 | if (persp) { |
| 297 | SkScalar tolScale = |
| 298 | GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m, |
| 299 | path.getBounds()); |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame^] | 300 | GrPathUtils::convertCubicToQuads(pts, tolScale, &q); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 301 | } else { |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame^] | 302 | GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, &q); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 303 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 304 | for (int i = 0; i < q.count(); i += 3) { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 305 | SkPoint* qInDevSpace; |
| 306 | // bounds has to be calculated in device space, but q is |
| 307 | // in src space when there is perspective. |
| 308 | if (persp) { |
| 309 | m.mapPoints(devPts, &q[i], 3); |
| 310 | bounds.setBounds(devPts, 3); |
| 311 | qInDevSpace = devPts; |
| 312 | } else { |
| 313 | bounds.setBounds(&q[i], 3); |
| 314 | qInDevSpace = &q[i]; |
| 315 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 316 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 317 | bounds.roundOut(&ibounds); |
| 318 | if (SkIRect::Intersects(clip, ibounds)) { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 319 | int subdiv = num_quad_subdivs(qInDevSpace); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 320 | GrAssert(subdiv >= -1); |
| 321 | if (-1 == subdiv) { |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 322 | SkPoint* pts = lines->push_back_n(4); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 323 | // lines should always be in device coords |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 324 | pts[0] = qInDevSpace[0]; |
| 325 | pts[1] = qInDevSpace[1]; |
| 326 | pts[2] = qInDevSpace[1]; |
| 327 | pts[3] = qInDevSpace[2]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 328 | } else { |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 329 | SkPoint* pts = quads->push_back_n(3); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 330 | // q is already in src space when there is no |
| 331 | // perspective and dev coords otherwise. |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 332 | pts[0] = q[0 + i]; |
| 333 | pts[1] = q[1 + i]; |
| 334 | pts[2] = q[2 + i]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 335 | quadSubdivCnts->push_back() = subdiv; |
| 336 | totalQuadCount += 1 << subdiv; |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | } |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 341 | break; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 342 | case kClose_PathCmd: |
| 343 | break; |
| 344 | case kEnd_PathCmd: |
| 345 | return totalQuadCount; |
| 346 | } |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | struct Vertex { |
| 351 | GrPoint fPos; |
| 352 | union { |
| 353 | struct { |
| 354 | GrScalar fA; |
| 355 | GrScalar fB; |
| 356 | GrScalar fC; |
| 357 | } fLine; |
| 358 | GrVec fQuadCoord; |
| 359 | struct { |
| 360 | GrScalar fBogus[4]; |
| 361 | }; |
| 362 | }; |
| 363 | }; |
| 364 | GR_STATIC_ASSERT(sizeof(Vertex) == 3 * sizeof(GrPoint)); |
| 365 | |
| 366 | void intersect_lines(const SkPoint& ptA, const SkVector& normA, |
| 367 | const SkPoint& ptB, const SkVector& normB, |
| 368 | SkPoint* result) { |
| 369 | |
| 370 | SkScalar lineAW = -normA.dot(ptA); |
| 371 | SkScalar lineBW = -normB.dot(ptB); |
| 372 | |
| 373 | SkScalar wInv = SkScalarMul(normA.fX, normB.fY) - |
| 374 | SkScalarMul(normA.fY, normB.fX); |
| 375 | wInv = SkScalarInvert(wInv); |
| 376 | |
| 377 | result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY); |
| 378 | result->fX = SkScalarMul(result->fX, wInv); |
| 379 | |
| 380 | result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW); |
| 381 | result->fY = SkScalarMul(result->fY, wInv); |
| 382 | } |
| 383 | |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 384 | void bloat_quad(const SkPoint qpts[3], const GrMatrix* toDevice, |
| 385 | const GrMatrix* toSrc, Vertex verts[kVertsPerQuad]) { |
| 386 | GrAssert(!toDevice == !toSrc); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 387 | // original quad is specified by tri a,b,c |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 388 | SkPoint a = qpts[0]; |
| 389 | SkPoint b = qpts[1]; |
| 390 | SkPoint c = qpts[2]; |
| 391 | |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 392 | // this should be in the src space, not dev coords, when we have perspective |
| 393 | SkMatrix DevToUV; |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame^] | 394 | GrPathUtils::quadDesignSpaceToUVCoordsMatrix(qpts, &DevToUV); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 395 | |
| 396 | if (toDevice) { |
| 397 | toDevice->mapPoints(&a, 1); |
| 398 | toDevice->mapPoints(&b, 1); |
| 399 | toDevice->mapPoints(&c, 1); |
| 400 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 401 | // make a new poly where we replace a and c by a 1-pixel wide edges orthog |
| 402 | // to edges ab and bc: |
| 403 | // |
| 404 | // before | after |
| 405 | // | b0 |
| 406 | // b | |
| 407 | // | |
| 408 | // | a0 c0 |
| 409 | // a c | a1 c1 |
| 410 | // |
| 411 | // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c, |
| 412 | // respectively. |
| 413 | Vertex& a0 = verts[0]; |
| 414 | Vertex& a1 = verts[1]; |
| 415 | Vertex& b0 = verts[2]; |
| 416 | Vertex& c0 = verts[3]; |
| 417 | Vertex& c1 = verts[4]; |
| 418 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 419 | SkVector ab = b; |
| 420 | ab -= a; |
| 421 | SkVector ac = c; |
| 422 | ac -= a; |
| 423 | SkVector cb = b; |
| 424 | cb -= c; |
| 425 | |
| 426 | // We should have already handled degenerates |
| 427 | GrAssert(ab.length() > 0 && cb.length() > 0); |
| 428 | |
| 429 | ab.normalize(); |
| 430 | SkVector abN; |
| 431 | abN.setOrthog(ab, SkVector::kLeft_Side); |
| 432 | if (abN.dot(ac) > 0) { |
| 433 | abN.negate(); |
| 434 | } |
| 435 | |
| 436 | cb.normalize(); |
| 437 | SkVector cbN; |
| 438 | cbN.setOrthog(cb, SkVector::kLeft_Side); |
| 439 | if (cbN.dot(ac) < 0) { |
| 440 | cbN.negate(); |
| 441 | } |
| 442 | |
| 443 | a0.fPos = a; |
| 444 | a0.fPos += abN; |
| 445 | a1.fPos = a; |
| 446 | a1.fPos -= abN; |
| 447 | |
| 448 | c0.fPos = c; |
| 449 | c0.fPos += cbN; |
| 450 | c1.fPos = c; |
| 451 | c1.fPos -= cbN; |
| 452 | |
| 453 | intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos); |
| 454 | |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 455 | if (toSrc) { |
| 456 | toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(Vertex), kVertsPerQuad); |
| 457 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 458 | DevToUV.mapPointsWithStride(&verts[0].fQuadCoord, |
| 459 | &verts[0].fPos, sizeof(Vertex), kVertsPerQuad); |
| 460 | } |
| 461 | |
| 462 | void add_quads(const SkPoint p[3], |
| 463 | int subdiv, |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 464 | const GrMatrix* toDevice, |
| 465 | const GrMatrix* toSrc, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 466 | Vertex** vert) { |
| 467 | GrAssert(subdiv >= 0); |
| 468 | if (subdiv) { |
| 469 | SkPoint newP[5]; |
| 470 | SkChopQuadAtHalf(p, newP); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 471 | add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert); |
| 472 | add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 473 | } else { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 474 | bloat_quad(p, toDevice, toSrc, *vert); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 475 | *vert += kVertsPerQuad; |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | void add_line(const SkPoint p[2], |
| 480 | int rtHeight, |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 481 | const SkMatrix* toSrc, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 482 | Vertex** vert) { |
| 483 | const SkPoint& a = p[0]; |
| 484 | const SkPoint& b = p[1]; |
| 485 | |
| 486 | SkVector orthVec = b; |
| 487 | orthVec -= a; |
| 488 | |
| 489 | if (orthVec.setLength(SK_Scalar1)) { |
| 490 | orthVec.setOrthog(orthVec); |
| 491 | |
| 492 | // the values we pass down to the frag shader |
| 493 | // have to be in y-points-up space; |
| 494 | SkVector normal; |
| 495 | normal.fX = orthVec.fX; |
| 496 | normal.fY = -orthVec.fY; |
| 497 | SkPoint aYDown; |
| 498 | aYDown.fX = a.fX; |
| 499 | aYDown.fY = rtHeight - a.fY; |
| 500 | |
| 501 | SkScalar lineC = -(aYDown.dot(normal)); |
| 502 | for (int i = 0; i < kVertsPerLineSeg; ++i) { |
| 503 | (*vert)[i].fPos = (i < 2) ? a : b; |
| 504 | if (0 == i || 3 == i) { |
| 505 | (*vert)[i].fPos -= orthVec; |
| 506 | } else { |
| 507 | (*vert)[i].fPos += orthVec; |
| 508 | } |
| 509 | (*vert)[i].fLine.fA = normal.fX; |
| 510 | (*vert)[i].fLine.fB = normal.fY; |
| 511 | (*vert)[i].fLine.fC = lineC; |
| 512 | } |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 513 | if (NULL != toSrc) { |
| 514 | toSrc->mapPointsWithStride(&(*vert)->fPos, |
| 515 | sizeof(Vertex), |
| 516 | kVertsPerLineSeg); |
| 517 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 518 | } else { |
| 519 | // just make it degenerate and likely offscreen |
| 520 | (*vert)[0].fPos.set(SK_ScalarMax, SK_ScalarMax); |
| 521 | (*vert)[1].fPos.set(SK_ScalarMax, SK_ScalarMax); |
| 522 | (*vert)[2].fPos.set(SK_ScalarMax, SK_ScalarMax); |
| 523 | (*vert)[3].fPos.set(SK_ScalarMax, SK_ScalarMax); |
| 524 | } |
| 525 | |
| 526 | *vert += kVertsPerLineSeg; |
| 527 | } |
| 528 | |
| 529 | } |
| 530 | |
bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 531 | bool GrAAHairLinePathRenderer::createGeom(GrDrawState::StageMask stageMask) { |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 532 | const GrDrawState& drawState = fTarget->getDrawState(); |
| 533 | int rtHeight = drawState.getRenderTarget()->height(); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 534 | |
| 535 | GrIRect clip; |
| 536 | if (fTarget->getClip().hasConservativeBounds()) { |
| 537 | GrRect clipRect = fTarget->getClip().getConservativeBounds(); |
| 538 | clipRect.roundOut(&clip); |
| 539 | } else { |
| 540 | clip.setLargest(); |
| 541 | } |
| 542 | |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 543 | // If none of the inputs that affect generation of path geometry have |
| 544 | // have changed since last previous path draw then we can reuse the |
| 545 | // previous geoemtry. |
bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 546 | if (stageMask == fPreviousStages && |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 547 | fPreviousViewMatrix == drawState.getViewMatrix() && |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 548 | fPreviousTranslate == fTranslate && |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 549 | rtHeight == fPreviousRTHeight && |
| 550 | fClipRect == clip) { |
| 551 | return true; |
| 552 | } |
| 553 | |
| 554 | GrVertexLayout layout = GrDrawTarget::kEdge_VertexLayoutBit; |
tomhudson@google.com | 9381363 | 2011-10-27 20:21:16 +0000 | [diff] [blame] | 555 | for (int s = 0; s < GrDrawState::kNumStages; ++s) { |
bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 556 | if ((1 << s) & stageMask) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 557 | layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s); |
| 558 | } |
| 559 | } |
| 560 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 561 | GrMatrix viewM = drawState.getViewMatrix(); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 562 | |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 563 | PREALLOC_PTARRAY(128) lines; |
| 564 | PREALLOC_PTARRAY(128) quads; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 565 | IntArray qSubdivs; |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 566 | fQuadCnt = generate_lines_and_quads(*fPath, viewM, fTranslate, clip, |
| 567 | &lines, &quads, &qSubdivs); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 568 | |
| 569 | fLineSegmentCnt = lines.count() / 2; |
| 570 | int vertCnt = kVertsPerLineSeg * fLineSegmentCnt + kVertsPerQuad * fQuadCnt; |
| 571 | |
| 572 | GrAssert(sizeof(Vertex) == GrDrawTarget::VertexSize(layout)); |
| 573 | |
| 574 | Vertex* verts; |
| 575 | if (!fTarget->reserveVertexSpace(layout, vertCnt, (void**)&verts)) { |
| 576 | return false; |
| 577 | } |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 578 | Vertex* base = verts; |
| 579 | |
| 580 | const GrMatrix* toDevice = NULL; |
| 581 | const GrMatrix* toSrc = NULL; |
| 582 | GrMatrix ivm; |
| 583 | |
| 584 | if (viewM.hasPerspective()) { |
| 585 | if (viewM.invert(&ivm)) { |
| 586 | toDevice = &viewM; |
| 587 | toSrc = &ivm; |
| 588 | } |
| 589 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 590 | |
| 591 | for (int i = 0; i < fLineSegmentCnt; ++i) { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 592 | add_line(&lines[2*i], rtHeight, toSrc, &verts); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 593 | } |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 594 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 595 | int unsubdivQuadCnt = quads.count() / 3; |
| 596 | for (int i = 0; i < unsubdivQuadCnt; ++i) { |
| 597 | GrAssert(qSubdivs[i] >= 0); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 598 | add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 599 | } |
| 600 | |
bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 601 | fPreviousStages = stageMask; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 602 | fPreviousViewMatrix = drawState.getViewMatrix(); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 603 | fPreviousRTHeight = rtHeight; |
| 604 | fClipRect = clip; |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 605 | fPreviousTranslate = fTranslate; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 606 | return true; |
| 607 | } |
| 608 | |
bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 609 | void GrAAHairLinePathRenderer::drawPath(GrDrawState::StageMask stageMask) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 610 | |
bsalomon@google.com | 39ee0ff | 2011-12-06 15:32:52 +0000 | [diff] [blame] | 611 | if (!this->createGeom(stageMask)) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 612 | return; |
| 613 | } |
| 614 | |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 615 | GrDrawState* drawState = fTarget->drawState(); |
| 616 | |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 617 | GrDrawTarget::AutoStateRestore asr; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 618 | if (!drawState->getViewMatrix().hasPerspective()) { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 619 | asr.set(fTarget); |
| 620 | GrMatrix ivm; |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 621 | if (drawState->getViewInverse(&ivm)) { |
| 622 | drawState->preConcatSamplerMatrices(stageMask, ivm); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 623 | } |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 624 | drawState->setViewMatrix(GrMatrix::I()); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 625 | } |
| 626 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 627 | // TODO: See whether rendering lines as degenerate quads improves perf |
| 628 | // when we have a mix |
| 629 | fTarget->setIndexSourceToBuffer(fLinesIndexBuffer); |
| 630 | int lines = 0; |
| 631 | int nBufLines = fLinesIndexBuffer->maxQuads(); |
| 632 | while (lines < fLineSegmentCnt) { |
| 633 | int n = GrMin(fLineSegmentCnt-lines, nBufLines); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 634 | drawState->setVertexEdgeType(GrDrawState::kHairLine_EdgeType); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 635 | fTarget->drawIndexed(kTriangles_PrimitiveType, |
| 636 | kVertsPerLineSeg*lines, // startV |
| 637 | 0, // startI |
| 638 | kVertsPerLineSeg*n, // vCount |
| 639 | kIdxsPerLineSeg*n); // iCount |
| 640 | lines += n; |
| 641 | } |
| 642 | |
| 643 | fTarget->setIndexSourceToBuffer(fQuadsIndexBuffer); |
| 644 | int quads = 0; |
| 645 | while (quads < fQuadCnt) { |
| 646 | int n = GrMin(fQuadCnt-quads, kNumQuadsInIdxBuffer); |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 647 | drawState->setVertexEdgeType(GrDrawState::kHairQuad_EdgeType); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 648 | fTarget->drawIndexed(kTriangles_PrimitiveType, |
| 649 | 4*fLineSegmentCnt + kVertsPerQuad*quads, // startV |
| 650 | 0, // startI |
| 651 | kVertsPerQuad*n, // vCount |
| 652 | kIdxsPerQuad*n); // iCount |
| 653 | quads += n; |
| 654 | } |
| 655 | |
| 656 | } |
| 657 | |