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