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