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) { |
bsalomon@google.com | a8a6a32 | 2011-09-23 14:19:58 +0000 | [diff] [blame] | 69 | const GrIndexBuffer* lIdxBuffer = context->getQuadIndexBuffer(); |
| 70 | if (NULL == lIdxBuffer) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 71 | return NULL; |
| 72 | } |
bsalomon@google.com | a8a6a32 | 2011-09-23 14:19:58 +0000 | [diff] [blame] | 73 | GrGpu* gpu = context->getGpu(); |
| 74 | GrIndexBuffer* qIdxBuf = gpu->createIndexBuffer(kQuadIdxSBufize, false); |
| 75 | SkAutoTUnref<GrIndexBuffer> qIdxBuffer(qIdxBuf); |
| 76 | if (NULL == qIdxBuf || |
| 77 | !push_quad_index_data(qIdxBuf)) { |
| 78 | return NULL; |
| 79 | } |
| 80 | return new GrAAHairLinePathRenderer(context, |
| 81 | lIdxBuffer, |
| 82 | qIdxBuf); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | GrAAHairLinePathRenderer::GrAAHairLinePathRenderer( |
| 86 | const GrContext* context, |
| 87 | const GrIndexBuffer* linesIndexBuffer, |
| 88 | const GrIndexBuffer* quadsIndexBuffer) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 89 | fLinesIndexBuffer = linesIndexBuffer; |
| 90 | linesIndexBuffer->ref(); |
| 91 | fQuadsIndexBuffer = quadsIndexBuffer; |
| 92 | quadsIndexBuffer->ref(); |
| 93 | this->resetGeom(); |
| 94 | } |
| 95 | |
| 96 | GrAAHairLinePathRenderer::~GrAAHairLinePathRenderer() { |
| 97 | fLinesIndexBuffer->unref(); |
| 98 | fQuadsIndexBuffer->unref(); |
| 99 | } |
| 100 | |
bsalomon@google.com | 0d2aa84 | 2011-09-26 15:59:20 +0000 | [diff] [blame] | 101 | bool GrAAHairLinePathRenderer::supportsAA(const GrDrawTarget* target, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 102 | const SkPath& path, |
bsalomon@google.com | 0d2aa84 | 2011-09-26 15:59:20 +0000 | [diff] [blame] | 103 | GrPathFill fill) const { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 104 | return kHairLine_PathFill == fill; |
| 105 | } |
| 106 | |
| 107 | bool GrAAHairLinePathRenderer::canDrawPath(const GrDrawTarget* target, |
| 108 | const SkPath& path, |
| 109 | GrPathFill fill) const { |
bsalomon@google.com | a8a6a32 | 2011-09-23 14:19:58 +0000 | [diff] [blame] | 110 | static const uint32_t gReqDerivMask = SkPath::kCubic_SegmentMask | |
| 111 | SkPath::kQuad_SegmentMask; |
| 112 | return (kHairLine_PathFill == fill && |
| 113 | target->isAntialiasState() && |
| 114 | (target->getCaps().fShaderDerivativeSupport || |
| 115 | !(gReqDerivMask & path.getSegmentMasks()))); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | void GrAAHairLinePathRenderer::pathWillClear() { |
| 119 | this->resetGeom(); |
| 120 | } |
| 121 | |
| 122 | void GrAAHairLinePathRenderer::resetGeom() { |
| 123 | fPreviousStages = ~0; |
| 124 | fPreviousRTHeight = ~0; |
| 125 | fPreviousViewMatrix = GrMatrix::InvalidMatrix(); |
| 126 | fLineSegmentCnt = 0; |
| 127 | fQuadCnt = 0; |
| 128 | if ((fQuadCnt || fLineSegmentCnt) && NULL != fTarget) { |
| 129 | fTarget->resetVertexSource(); |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | namespace { |
| 134 | |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 135 | typedef SkTArray<SkPoint, true> PtArray; |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame^] | 136 | #define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true> |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 137 | typedef SkTArray<int, true> IntArray; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 138 | |
| 139 | /** |
| 140 | * We convert cubics to quadratics (for now). |
| 141 | */ |
| 142 | void convert_noninflect_cubic_to_quads(const SkPoint p[4], |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 143 | SkScalar tolScale, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 144 | PtArray* quads, |
| 145 | int sublevel = 0) { |
| 146 | SkVector ab = p[1]; |
| 147 | ab -= p[0]; |
| 148 | SkVector dc = p[2]; |
| 149 | dc -= p[3]; |
| 150 | |
| 151 | static const SkScalar gLengthScale = 3 * SK_Scalar1 / 2; |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 152 | // base tolerance is 2 pixels in dev coords. |
| 153 | const SkScalar distanceSqdTol = SkScalarMul(tolScale, 2 * SK_Scalar1); |
| 154 | static const int kMaxSubdivs = 10; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 155 | |
| 156 | ab.scale(gLengthScale); |
| 157 | dc.scale(gLengthScale); |
| 158 | |
| 159 | SkVector c0 = p[0]; |
| 160 | c0 += ab; |
| 161 | SkVector c1 = p[3]; |
| 162 | c1 += dc; |
| 163 | |
| 164 | SkScalar dSqd = c0.distanceToSqd(c1); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 165 | if (sublevel > kMaxSubdivs || dSqd <= distanceSqdTol) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 166 | SkPoint cAvg = c0; |
| 167 | cAvg += c1; |
| 168 | cAvg.scale(SK_ScalarHalf); |
| 169 | |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 170 | SkPoint* pts = quads->push_back_n(3); |
| 171 | pts[0] = p[0]; |
| 172 | pts[1] = cAvg; |
| 173 | pts[2] = p[3]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 174 | |
| 175 | return; |
| 176 | } else { |
| 177 | SkPoint choppedPts[7]; |
| 178 | SkChopCubicAtHalf(p, choppedPts); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 179 | convert_noninflect_cubic_to_quads(choppedPts + 0, tolScale, |
| 180 | quads, sublevel + 1); |
| 181 | convert_noninflect_cubic_to_quads(choppedPts + 3, tolScale, |
| 182 | quads, sublevel + 1); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 183 | } |
| 184 | } |
| 185 | |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 186 | void convert_cubic_to_quads(const SkPoint p[4], |
| 187 | SkScalar tolScale, |
| 188 | PtArray* quads) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 189 | SkPoint chopped[13]; |
| 190 | int count = SkChopCubicAtInflections(p, chopped); |
| 191 | |
| 192 | for (int i = 0; i < count; ++i) { |
| 193 | SkPoint* cubic = chopped + 3*i; |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 194 | convert_noninflect_cubic_to_quads(cubic, tolScale, quads); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
| 198 | // Takes 178th time of logf on Z600 / VC2010 |
| 199 | int get_float_exp(float x) { |
| 200 | GR_STATIC_ASSERT(sizeof(int) == sizeof(float)); |
| 201 | #if GR_DEBUG |
| 202 | static bool tested; |
| 203 | if (!tested) { |
| 204 | tested = true; |
| 205 | GrAssert(get_float_exp(0.25f) == -2); |
| 206 | GrAssert(get_float_exp(0.3f) == -2); |
| 207 | GrAssert(get_float_exp(0.5f) == -1); |
| 208 | GrAssert(get_float_exp(1.f) == 0); |
| 209 | GrAssert(get_float_exp(2.f) == 1); |
| 210 | GrAssert(get_float_exp(2.5f) == 1); |
| 211 | GrAssert(get_float_exp(8.f) == 3); |
| 212 | GrAssert(get_float_exp(100.f) == 6); |
| 213 | GrAssert(get_float_exp(1000.f) == 9); |
| 214 | GrAssert(get_float_exp(1024.f) == 10); |
| 215 | GrAssert(get_float_exp(3000000.f) == 21); |
| 216 | } |
| 217 | #endif |
bsalomon@google.com | 2ec7280 | 2011-09-21 21:46:03 +0000 | [diff] [blame] | 218 | const int* iptr = (const int*)&x; |
| 219 | return (((*iptr) & 0x7f800000) >> 23) - 127; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // we subdivide the quads to avoid huge overfill |
| 223 | // if it returns -1 then should be drawn as lines |
| 224 | int num_quad_subdivs(const SkPoint p[3]) { |
| 225 | static const SkScalar gDegenerateToLineTol = SK_Scalar1; |
bsalomon@google.com | 46a2a1e | 2011-09-06 22:10:52 +0000 | [diff] [blame] | 226 | static const SkScalar gDegenerateToLineTolSqd = |
| 227 | SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 228 | |
bsalomon@google.com | 46a2a1e | 2011-09-06 22:10:52 +0000 | [diff] [blame] | 229 | if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd || |
| 230 | p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 231 | return -1; |
| 232 | } |
bsalomon@google.com | 46a2a1e | 2011-09-06 22:10:52 +0000 | [diff] [blame] | 233 | |
| 234 | GrScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]); |
| 235 | if (dsqd < gDegenerateToLineTolSqd) { |
| 236 | return -1; |
| 237 | } |
| 238 | |
| 239 | if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 240 | return -1; |
| 241 | } |
| 242 | |
| 243 | static const int kMaxSub = 4; |
| 244 | // tolerance of triangle height in pixels |
| 245 | // tuned on windows Quadro FX 380 / Z600 |
| 246 | // trade off of fill vs cpu time on verts |
| 247 | // maybe different when do this using gpu (geo or tess shaders) |
| 248 | static const SkScalar gSubdivTol = 175 * SK_Scalar1; |
| 249 | |
| 250 | if (dsqd <= gSubdivTol*gSubdivTol) { |
| 251 | return 0; |
| 252 | } else { |
| 253 | // subdividing the quad reduces d by 4. so we want x = log4(d/tol) |
| 254 | // = log4(d*d/tol*tol)/2 |
| 255 | // = log2(d*d/tol*tol) |
| 256 | |
| 257 | #ifdef SK_SCALAR_IS_FLOAT |
| 258 | // +1 since we're ignoring the mantissa contribution. |
| 259 | int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1; |
| 260 | log = GrMin(GrMax(0, log), kMaxSub); |
| 261 | return log; |
| 262 | #else |
bsalomon@google.com | fcb0dbc | 2011-08-30 18:35:18 +0000 | [diff] [blame] | 263 | SkScalar log = SkScalarLog(SkScalarDiv(dsqd,gSubdivTol*gSubdivTol)); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 264 | static const SkScalar conv = SkScalarInvert(SkScalarLog(2)); |
| 265 | log = SkScalarMul(log, conv); |
| 266 | return GrMin(GrMax(0, SkScalarCeilToInt(log)),kMaxSub); |
| 267 | #endif |
| 268 | } |
| 269 | } |
| 270 | |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 271 | /** |
| 272 | * Generates the lines and quads to be rendered. Lines are always recorded in |
| 273 | * device space. We will do a device space bloat to account for the 1pixel |
| 274 | * thickness. |
| 275 | * Quads are recorded in device space unless m contains |
| 276 | * perspective, then in they are in src space. We do this because we will |
| 277 | * subdivide large quads to reduce over-fill. This subdivision has to be |
| 278 | * performed before applying the perspective matrix. |
| 279 | */ |
| 280 | int generate_lines_and_quads(const SkPath& path, |
| 281 | const SkMatrix& m, |
| 282 | const SkVector& translate, |
| 283 | GrIRect clip, |
| 284 | PtArray* lines, |
| 285 | PtArray* quads, |
| 286 | IntArray* quadSubdivCnts) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 287 | SkPath::Iter iter(path, false); |
| 288 | |
| 289 | int totalQuadCount = 0; |
| 290 | GrRect bounds; |
| 291 | GrIRect ibounds; |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 292 | |
| 293 | bool persp = m.hasPerspective(); |
| 294 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 295 | for (;;) { |
| 296 | GrPoint pts[4]; |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 297 | GrPoint devPts[4]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 298 | GrPathCmd cmd = (GrPathCmd)iter.next(pts); |
| 299 | switch (cmd) { |
| 300 | case kMove_PathCmd: |
| 301 | break; |
| 302 | case kLine_PathCmd: |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 303 | SkPoint::Offset(pts, 2, translate); |
| 304 | m.mapPoints(devPts, pts, 2); |
| 305 | bounds.setBounds(devPts, 2); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 306 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 307 | bounds.roundOut(&ibounds); |
| 308 | if (SkIRect::Intersects(clip, ibounds)) { |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 309 | SkPoint* pts = lines->push_back_n(2); |
| 310 | pts[0] = devPts[0]; |
| 311 | pts[1] = devPts[1]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 312 | } |
| 313 | break; |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 314 | case kQuadratic_PathCmd: |
| 315 | SkPoint::Offset(pts, 3, translate); |
| 316 | m.mapPoints(devPts, pts, 3); |
| 317 | bounds.setBounds(devPts, 3); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 318 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 319 | bounds.roundOut(&ibounds); |
| 320 | if (SkIRect::Intersects(clip, ibounds)) { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 321 | int subdiv = num_quad_subdivs(devPts); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 322 | GrAssert(subdiv >= -1); |
| 323 | if (-1 == subdiv) { |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 324 | SkPoint* pts = lines->push_back_n(4); |
| 325 | pts[0] = devPts[0]; |
| 326 | pts[1] = devPts[1]; |
| 327 | pts[2] = devPts[1]; |
| 328 | pts[3] = devPts[2]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 329 | } else { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 330 | // when in perspective keep quads in src space |
| 331 | SkPoint* qPts = persp ? pts : devPts; |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 332 | SkPoint* pts = quads->push_back_n(3); |
| 333 | pts[0] = qPts[0]; |
| 334 | pts[1] = qPts[1]; |
| 335 | pts[2] = qPts[2]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 336 | quadSubdivCnts->push_back() = subdiv; |
| 337 | totalQuadCount += 1 << subdiv; |
| 338 | } |
| 339 | } |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 340 | break; |
| 341 | case kCubic_PathCmd: |
| 342 | SkPoint::Offset(pts, 4, translate); |
| 343 | m.mapPoints(devPts, pts, 4); |
| 344 | bounds.setBounds(devPts, 4); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 345 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 346 | bounds.roundOut(&ibounds); |
| 347 | if (SkIRect::Intersects(clip, ibounds)) { |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame^] | 348 | PREALLOC_PTARRAY(32) q; |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 349 | // in perspective have to do conversion in src space |
| 350 | if (persp) { |
| 351 | SkScalar tolScale = |
| 352 | GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m, |
| 353 | path.getBounds()); |
| 354 | convert_cubic_to_quads(pts, tolScale, &q); |
| 355 | } else { |
| 356 | convert_cubic_to_quads(devPts, SK_Scalar1, &q); |
| 357 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 358 | for (int i = 0; i < q.count(); i += 3) { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 359 | SkPoint* qInDevSpace; |
| 360 | // bounds has to be calculated in device space, but q is |
| 361 | // in src space when there is perspective. |
| 362 | if (persp) { |
| 363 | m.mapPoints(devPts, &q[i], 3); |
| 364 | bounds.setBounds(devPts, 3); |
| 365 | qInDevSpace = devPts; |
| 366 | } else { |
| 367 | bounds.setBounds(&q[i], 3); |
| 368 | qInDevSpace = &q[i]; |
| 369 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 370 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 371 | bounds.roundOut(&ibounds); |
| 372 | if (SkIRect::Intersects(clip, ibounds)) { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 373 | int subdiv = num_quad_subdivs(qInDevSpace); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 374 | GrAssert(subdiv >= -1); |
| 375 | if (-1 == subdiv) { |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 376 | SkPoint* pts = lines->push_back_n(4); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 377 | // lines should always be in device coords |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 378 | pts[0] = qInDevSpace[0]; |
| 379 | pts[1] = qInDevSpace[1]; |
| 380 | pts[2] = qInDevSpace[1]; |
| 381 | pts[3] = qInDevSpace[2]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 382 | } else { |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 383 | SkPoint* pts = quads->push_back_n(3); |
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 | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 386 | pts[0] = q[0 + i]; |
| 387 | pts[1] = q[1 + i]; |
| 388 | pts[2] = q[2 + i]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 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 | |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame^] | 632 | PREALLOC_PTARRAY(128) lines; |
| 633 | PREALLOC_PTARRAY(128) quads; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 634 | IntArray qSubdivs; |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 635 | fQuadCnt = generate_lines_and_quads(*fPath, viewM, fTranslate, clip, |
| 636 | &lines, &quads, &qSubdivs); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 637 | |
| 638 | fLineSegmentCnt = lines.count() / 2; |
| 639 | int vertCnt = kVertsPerLineSeg * fLineSegmentCnt + kVertsPerQuad * fQuadCnt; |
| 640 | |
| 641 | GrAssert(sizeof(Vertex) == GrDrawTarget::VertexSize(layout)); |
| 642 | |
| 643 | Vertex* verts; |
| 644 | if (!fTarget->reserveVertexSpace(layout, vertCnt, (void**)&verts)) { |
| 645 | return false; |
| 646 | } |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 647 | Vertex* base = verts; |
| 648 | |
| 649 | const GrMatrix* toDevice = NULL; |
| 650 | const GrMatrix* toSrc = NULL; |
| 651 | GrMatrix ivm; |
| 652 | |
| 653 | if (viewM.hasPerspective()) { |
| 654 | if (viewM.invert(&ivm)) { |
| 655 | toDevice = &viewM; |
| 656 | toSrc = &ivm; |
| 657 | } |
| 658 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 659 | |
| 660 | for (int i = 0; i < fLineSegmentCnt; ++i) { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 661 | add_line(&lines[2*i], rtHeight, toSrc, &verts); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 662 | } |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 663 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 664 | int unsubdivQuadCnt = quads.count() / 3; |
| 665 | for (int i = 0; i < unsubdivQuadCnt; ++i) { |
| 666 | GrAssert(qSubdivs[i] >= 0); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 667 | add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 668 | } |
| 669 | |
| 670 | fPreviousStages = stages; |
| 671 | fPreviousViewMatrix = fTarget->getViewMatrix(); |
| 672 | fPreviousRTHeight = rtHeight; |
| 673 | fClipRect = clip; |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 674 | fPreviousTranslate = fTranslate; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 675 | return true; |
| 676 | } |
| 677 | |
| 678 | void GrAAHairLinePathRenderer::drawPath(GrDrawTarget::StageBitfield stages) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 679 | |
| 680 | if (!this->createGeom(stages)) { |
| 681 | return; |
| 682 | } |
| 683 | |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 684 | GrDrawTarget::AutoStateRestore asr; |
| 685 | if (!fTarget->getViewMatrix().hasPerspective()) { |
| 686 | asr.set(fTarget); |
| 687 | GrMatrix ivm; |
| 688 | if (fTarget->getViewInverse(&ivm)) { |
| 689 | fTarget->preConcatSamplerMatrices(stages, ivm); |
| 690 | } |
| 691 | fTarget->setViewMatrix(GrMatrix::I()); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 692 | } |
| 693 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 694 | // TODO: See whether rendering lines as degenerate quads improves perf |
| 695 | // when we have a mix |
| 696 | fTarget->setIndexSourceToBuffer(fLinesIndexBuffer); |
| 697 | int lines = 0; |
| 698 | int nBufLines = fLinesIndexBuffer->maxQuads(); |
| 699 | while (lines < fLineSegmentCnt) { |
| 700 | int n = GrMin(fLineSegmentCnt-lines, nBufLines); |
| 701 | fTarget->setVertexEdgeType(GrDrawTarget::kHairLine_EdgeType); |
| 702 | fTarget->drawIndexed(kTriangles_PrimitiveType, |
| 703 | kVertsPerLineSeg*lines, // startV |
| 704 | 0, // startI |
| 705 | kVertsPerLineSeg*n, // vCount |
| 706 | kIdxsPerLineSeg*n); // iCount |
| 707 | lines += n; |
| 708 | } |
| 709 | |
| 710 | fTarget->setIndexSourceToBuffer(fQuadsIndexBuffer); |
| 711 | int quads = 0; |
| 712 | while (quads < fQuadCnt) { |
| 713 | int n = GrMin(fQuadCnt-quads, kNumQuadsInIdxBuffer); |
| 714 | fTarget->setVertexEdgeType(GrDrawTarget::kHairQuad_EdgeType); |
| 715 | fTarget->drawIndexed(kTriangles_PrimitiveType, |
| 716 | 4*fLineSegmentCnt + kVertsPerQuad*quads, // startV |
| 717 | 0, // startI |
| 718 | kVertsPerQuad*n, // vCount |
| 719 | kIdxsPerQuad*n); // iCount |
| 720 | quads += n; |
| 721 | } |
| 722 | |
| 723 | } |
| 724 | |