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