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" |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 14 | #include "GrEffect.h" |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 15 | #include "GrGpu.h" |
| 16 | #include "GrIndexBuffer.h" |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 17 | #include "GrPathUtils.h" |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 18 | #include "GrTBackendEffectFactory.h" |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 19 | #include "SkGeometry.h" |
sugoi@google.com | 12b4e27 | 2012-12-06 20:13:11 +0000 | [diff] [blame] | 20 | #include "SkStroke.h" |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 21 | #include "SkTemplates.h" |
| 22 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 23 | #include "gl/GrGLEffect.h" |
| 24 | #include "gl/GrGLSL.h" |
bsalomon@google.com | 4647f90 | 2013-03-26 14:45:27 +0000 | [diff] [blame] | 25 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 26 | namespace { |
| 27 | // quadratics are rendered as 5-sided polys in order to bound the |
| 28 | // AA stroke around the center-curve. See comments in push_quad_index_buffer and |
| 29 | // bloat_quad. |
| 30 | static const int kVertsPerQuad = 5; |
| 31 | static const int kIdxsPerQuad = 9; |
| 32 | |
| 33 | static const int kVertsPerLineSeg = 4; |
| 34 | static const int kIdxsPerLineSeg = 6; |
| 35 | |
| 36 | static const int kNumQuadsInIdxBuffer = 256; |
| 37 | static const size_t kQuadIdxSBufize = kIdxsPerQuad * |
| 38 | sizeof(uint16_t) * |
| 39 | kNumQuadsInIdxBuffer; |
| 40 | |
| 41 | bool push_quad_index_data(GrIndexBuffer* qIdxBuffer) { |
| 42 | uint16_t* data = (uint16_t*) qIdxBuffer->lock(); |
| 43 | bool tempData = NULL == data; |
| 44 | if (tempData) { |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 45 | data = SkNEW_ARRAY(uint16_t, kNumQuadsInIdxBuffer * kIdxsPerQuad); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 46 | } |
| 47 | for (int i = 0; i < kNumQuadsInIdxBuffer; ++i) { |
| 48 | |
| 49 | // Each quadratic is rendered as a five sided polygon. This poly bounds |
| 50 | // the quadratic's bounding triangle but has been expanded so that the |
| 51 | // 1-pixel wide area around the curve is inside the poly. |
| 52 | // If a,b,c are the original control points then the poly a0,b0,c0,c1,a1 |
| 53 | // that is rendered would look like this: |
| 54 | // b0 |
| 55 | // b |
| 56 | // |
| 57 | // a0 c0 |
| 58 | // a c |
| 59 | // a1 c1 |
bsalomon@google.com | 0e5104c | 2012-04-10 16:20:41 +0000 | [diff] [blame] | 60 | // Each is drawn as three triangles specified by these 9 indices: |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 61 | int baseIdx = i * kIdxsPerQuad; |
| 62 | uint16_t baseVert = (uint16_t)(i * kVertsPerQuad); |
| 63 | data[0 + baseIdx] = baseVert + 0; // a0 |
| 64 | data[1 + baseIdx] = baseVert + 1; // a1 |
| 65 | data[2 + baseIdx] = baseVert + 2; // b0 |
| 66 | data[3 + baseIdx] = baseVert + 2; // b0 |
| 67 | data[4 + baseIdx] = baseVert + 4; // c1 |
| 68 | data[5 + baseIdx] = baseVert + 3; // c0 |
| 69 | data[6 + baseIdx] = baseVert + 1; // a1 |
| 70 | data[7 + baseIdx] = baseVert + 4; // c1 |
| 71 | data[8 + baseIdx] = baseVert + 2; // b0 |
| 72 | } |
| 73 | if (tempData) { |
| 74 | bool ret = qIdxBuffer->updateData(data, kQuadIdxSBufize); |
| 75 | delete[] data; |
| 76 | return ret; |
| 77 | } else { |
| 78 | qIdxBuffer->unlock(); |
| 79 | return true; |
| 80 | } |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | GrPathRenderer* GrAAHairLinePathRenderer::Create(GrContext* context) { |
bsalomon@google.com | a8a6a32 | 2011-09-23 14:19:58 +0000 | [diff] [blame] | 85 | const GrIndexBuffer* lIdxBuffer = context->getQuadIndexBuffer(); |
| 86 | if (NULL == lIdxBuffer) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 87 | return NULL; |
| 88 | } |
bsalomon@google.com | a8a6a32 | 2011-09-23 14:19:58 +0000 | [diff] [blame] | 89 | GrGpu* gpu = context->getGpu(); |
| 90 | GrIndexBuffer* qIdxBuf = gpu->createIndexBuffer(kQuadIdxSBufize, false); |
| 91 | SkAutoTUnref<GrIndexBuffer> qIdxBuffer(qIdxBuf); |
| 92 | if (NULL == qIdxBuf || |
| 93 | !push_quad_index_data(qIdxBuf)) { |
| 94 | return NULL; |
| 95 | } |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 96 | return SkNEW_ARGS(GrAAHairLinePathRenderer, |
| 97 | (context, lIdxBuffer, qIdxBuf)); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | GrAAHairLinePathRenderer::GrAAHairLinePathRenderer( |
| 101 | const GrContext* context, |
| 102 | const GrIndexBuffer* linesIndexBuffer, |
| 103 | const GrIndexBuffer* quadsIndexBuffer) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 104 | fLinesIndexBuffer = linesIndexBuffer; |
| 105 | linesIndexBuffer->ref(); |
| 106 | fQuadsIndexBuffer = quadsIndexBuffer; |
| 107 | quadsIndexBuffer->ref(); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | GrAAHairLinePathRenderer::~GrAAHairLinePathRenderer() { |
| 111 | fLinesIndexBuffer->unref(); |
| 112 | fQuadsIndexBuffer->unref(); |
| 113 | } |
| 114 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 115 | namespace { |
| 116 | |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 117 | typedef SkTArray<SkPoint, true> PtArray; |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 118 | #define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true> |
bsalomon@google.com | 49313f6 | 2011-09-14 13:54:05 +0000 | [diff] [blame] | 119 | typedef SkTArray<int, true> IntArray; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 120 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 121 | // Takes 178th time of logf on Z600 / VC2010 |
| 122 | int get_float_exp(float x) { |
| 123 | GR_STATIC_ASSERT(sizeof(int) == sizeof(float)); |
| 124 | #if GR_DEBUG |
| 125 | static bool tested; |
| 126 | if (!tested) { |
| 127 | tested = true; |
| 128 | GrAssert(get_float_exp(0.25f) == -2); |
| 129 | GrAssert(get_float_exp(0.3f) == -2); |
| 130 | GrAssert(get_float_exp(0.5f) == -1); |
| 131 | GrAssert(get_float_exp(1.f) == 0); |
| 132 | GrAssert(get_float_exp(2.f) == 1); |
| 133 | GrAssert(get_float_exp(2.5f) == 1); |
| 134 | GrAssert(get_float_exp(8.f) == 3); |
| 135 | GrAssert(get_float_exp(100.f) == 6); |
| 136 | GrAssert(get_float_exp(1000.f) == 9); |
| 137 | GrAssert(get_float_exp(1024.f) == 10); |
| 138 | GrAssert(get_float_exp(3000000.f) == 21); |
| 139 | } |
| 140 | #endif |
bsalomon@google.com | 2ec7280 | 2011-09-21 21:46:03 +0000 | [diff] [blame] | 141 | const int* iptr = (const int*)&x; |
| 142 | return (((*iptr) & 0x7f800000) >> 23) - 127; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | // we subdivide the quads to avoid huge overfill |
| 146 | // if it returns -1 then should be drawn as lines |
| 147 | int num_quad_subdivs(const SkPoint p[3]) { |
| 148 | static const SkScalar gDegenerateToLineTol = SK_Scalar1; |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 149 | static const SkScalar gDegenerateToLineTolSqd = |
bsalomon@google.com | 46a2a1e | 2011-09-06 22:10:52 +0000 | [diff] [blame] | 150 | SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 151 | |
bsalomon@google.com | 46a2a1e | 2011-09-06 22:10:52 +0000 | [diff] [blame] | 152 | if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd || |
| 153 | p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 154 | return -1; |
| 155 | } |
bsalomon@google.com | 46a2a1e | 2011-09-06 22:10:52 +0000 | [diff] [blame] | 156 | |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 157 | SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]); |
bsalomon@google.com | 46a2a1e | 2011-09-06 22:10:52 +0000 | [diff] [blame] | 158 | if (dsqd < gDegenerateToLineTolSqd) { |
| 159 | return -1; |
| 160 | } |
| 161 | |
| 162 | if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 163 | return -1; |
| 164 | } |
| 165 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 166 | // tolerance of triangle height in pixels |
| 167 | // tuned on windows Quadro FX 380 / Z600 |
| 168 | // trade off of fill vs cpu time on verts |
| 169 | // maybe different when do this using gpu (geo or tess shaders) |
| 170 | static const SkScalar gSubdivTol = 175 * SK_Scalar1; |
| 171 | |
robertphillips@google.com | 7460b37 | 2012-04-25 16:54:51 +0000 | [diff] [blame] | 172 | if (dsqd <= SkScalarMul(gSubdivTol, gSubdivTol)) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 173 | return 0; |
| 174 | } else { |
robertphillips@google.com | 87379e1 | 2013-03-29 12:11:10 +0000 | [diff] [blame] | 175 | static const int kMaxSub = 4; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 176 | // subdividing the quad reduces d by 4. so we want x = log4(d/tol) |
| 177 | // = log4(d*d/tol*tol)/2 |
| 178 | // = log2(d*d/tol*tol) |
| 179 | |
| 180 | #ifdef SK_SCALAR_IS_FLOAT |
| 181 | // +1 since we're ignoring the mantissa contribution. |
| 182 | int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1; |
| 183 | log = GrMin(GrMax(0, log), kMaxSub); |
| 184 | return log; |
| 185 | #else |
robertphillips@google.com | 7460b37 | 2012-04-25 16:54:51 +0000 | [diff] [blame] | 186 | SkScalar log = SkScalarLog( |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 187 | SkScalarDiv(dsqd, |
robertphillips@google.com | 7460b37 | 2012-04-25 16:54:51 +0000 | [diff] [blame] | 188 | SkScalarMul(gSubdivTol, gSubdivTol))); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 189 | static const SkScalar conv = SkScalarInvert(SkScalarLog(2)); |
| 190 | log = SkScalarMul(log, conv); |
| 191 | return GrMin(GrMax(0, SkScalarCeilToInt(log)),kMaxSub); |
| 192 | #endif |
| 193 | } |
| 194 | } |
| 195 | |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 196 | /** |
| 197 | * Generates the lines and quads to be rendered. Lines are always recorded in |
| 198 | * device space. We will do a device space bloat to account for the 1pixel |
| 199 | * thickness. |
| 200 | * Quads are recorded in device space unless m contains |
| 201 | * perspective, then in they are in src space. We do this because we will |
| 202 | * subdivide large quads to reduce over-fill. This subdivision has to be |
| 203 | * performed before applying the perspective matrix. |
| 204 | */ |
| 205 | int generate_lines_and_quads(const SkPath& path, |
| 206 | const SkMatrix& m, |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 207 | const GrIRect& devClipBounds, |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 208 | PtArray* lines, |
| 209 | PtArray* quads, |
| 210 | IntArray* quadSubdivCnts) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 211 | SkPath::Iter iter(path, false); |
| 212 | |
| 213 | int totalQuadCount = 0; |
| 214 | GrRect bounds; |
| 215 | GrIRect ibounds; |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 216 | |
| 217 | bool persp = m.hasPerspective(); |
| 218 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 219 | for (;;) { |
| 220 | GrPoint pts[4]; |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 221 | GrPoint devPts[4]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 222 | GrPathCmd cmd = (GrPathCmd)iter.next(pts); |
| 223 | switch (cmd) { |
| 224 | case kMove_PathCmd: |
| 225 | break; |
| 226 | case kLine_PathCmd: |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 227 | m.mapPoints(devPts, pts, 2); |
| 228 | bounds.setBounds(devPts, 2); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 229 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 230 | bounds.roundOut(&ibounds); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 231 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 232 | SkPoint* pts = lines->push_back_n(2); |
| 233 | pts[0] = devPts[0]; |
| 234 | pts[1] = devPts[1]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 235 | } |
| 236 | break; |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 237 | case kQuadratic_PathCmd: |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 238 | m.mapPoints(devPts, pts, 3); |
| 239 | bounds.setBounds(devPts, 3); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 240 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 241 | bounds.roundOut(&ibounds); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 242 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 243 | int subdiv = num_quad_subdivs(devPts); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 244 | GrAssert(subdiv >= -1); |
| 245 | if (-1 == subdiv) { |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 246 | SkPoint* pts = lines->push_back_n(4); |
| 247 | pts[0] = devPts[0]; |
| 248 | pts[1] = devPts[1]; |
| 249 | pts[2] = devPts[1]; |
| 250 | pts[3] = devPts[2]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 251 | } else { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 252 | // when in perspective keep quads in src space |
| 253 | SkPoint* qPts = persp ? pts : devPts; |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 254 | SkPoint* pts = quads->push_back_n(3); |
| 255 | pts[0] = qPts[0]; |
| 256 | pts[1] = qPts[1]; |
| 257 | pts[2] = qPts[2]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 258 | quadSubdivCnts->push_back() = subdiv; |
| 259 | totalQuadCount += 1 << subdiv; |
| 260 | } |
| 261 | } |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 262 | break; |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 263 | case kCubic_PathCmd: |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 264 | m.mapPoints(devPts, pts, 4); |
| 265 | bounds.setBounds(devPts, 4); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 266 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 267 | bounds.roundOut(&ibounds); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 268 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 269 | PREALLOC_PTARRAY(32) q; |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 270 | // we don't need a direction if we aren't constraining the subdivision |
| 271 | static const SkPath::Direction kDummyDir = SkPath::kCCW_Direction; |
bsalomon@google.com | 69cc6ad | 2012-01-17 14:25:10 +0000 | [diff] [blame] | 272 | // We convert cubics to quadratics (for now). |
| 273 | // In perspective have to do conversion in src space. |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 274 | if (persp) { |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 275 | SkScalar tolScale = |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 276 | GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m, |
| 277 | path.getBounds()); |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 278 | GrPathUtils::convertCubicToQuads(pts, tolScale, false, kDummyDir, &q); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 279 | } else { |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 280 | GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, false, kDummyDir, &q); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 281 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 282 | for (int i = 0; i < q.count(); i += 3) { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 283 | SkPoint* qInDevSpace; |
| 284 | // bounds has to be calculated in device space, but q is |
| 285 | // in src space when there is perspective. |
| 286 | if (persp) { |
| 287 | m.mapPoints(devPts, &q[i], 3); |
| 288 | bounds.setBounds(devPts, 3); |
| 289 | qInDevSpace = devPts; |
| 290 | } else { |
| 291 | bounds.setBounds(&q[i], 3); |
| 292 | qInDevSpace = &q[i]; |
| 293 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 294 | bounds.outset(SK_Scalar1, SK_Scalar1); |
| 295 | bounds.roundOut(&ibounds); |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 296 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 297 | int subdiv = num_quad_subdivs(qInDevSpace); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 298 | GrAssert(subdiv >= -1); |
| 299 | if (-1 == subdiv) { |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 300 | SkPoint* pts = lines->push_back_n(4); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 301 | // lines should always be in device coords |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 302 | pts[0] = qInDevSpace[0]; |
| 303 | pts[1] = qInDevSpace[1]; |
| 304 | pts[2] = qInDevSpace[1]; |
| 305 | pts[3] = qInDevSpace[2]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 306 | } else { |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 307 | SkPoint* pts = quads->push_back_n(3); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 308 | // q is already in src space when there is no |
| 309 | // perspective and dev coords otherwise. |
bsalomon@google.com | a996fec | 2011-09-13 18:49:13 +0000 | [diff] [blame] | 310 | pts[0] = q[0 + i]; |
| 311 | pts[1] = q[1 + i]; |
| 312 | pts[2] = q[2 + i]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 313 | quadSubdivCnts->push_back() = subdiv; |
| 314 | totalQuadCount += 1 << subdiv; |
| 315 | } |
| 316 | } |
| 317 | } |
| 318 | } |
bsalomon@google.com | a51ab84 | 2012-07-10 19:53:34 +0000 | [diff] [blame] | 319 | break; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 320 | case kClose_PathCmd: |
| 321 | break; |
| 322 | case kEnd_PathCmd: |
| 323 | return totalQuadCount; |
| 324 | } |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | struct Vertex { |
| 329 | GrPoint fPos; |
| 330 | union { |
| 331 | struct { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 332 | SkScalar fA; |
| 333 | SkScalar fB; |
| 334 | SkScalar fC; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 335 | } fLine; |
| 336 | GrVec fQuadCoord; |
| 337 | struct { |
bsalomon@google.com | 8171288 | 2012-11-01 17:12:34 +0000 | [diff] [blame] | 338 | SkScalar fBogus[4]; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 339 | }; |
| 340 | }; |
| 341 | }; |
| 342 | GR_STATIC_ASSERT(sizeof(Vertex) == 3 * sizeof(GrPoint)); |
| 343 | |
| 344 | void intersect_lines(const SkPoint& ptA, const SkVector& normA, |
| 345 | const SkPoint& ptB, const SkVector& normB, |
| 346 | SkPoint* result) { |
| 347 | |
| 348 | SkScalar lineAW = -normA.dot(ptA); |
| 349 | SkScalar lineBW = -normB.dot(ptB); |
| 350 | |
| 351 | SkScalar wInv = SkScalarMul(normA.fX, normB.fY) - |
| 352 | SkScalarMul(normA.fY, normB.fX); |
| 353 | wInv = SkScalarInvert(wInv); |
| 354 | |
| 355 | result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY); |
| 356 | result->fX = SkScalarMul(result->fX, wInv); |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 357 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 358 | result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW); |
| 359 | result->fY = SkScalarMul(result->fY, wInv); |
| 360 | } |
| 361 | |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 362 | void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice, |
| 363 | const SkMatrix* toSrc, Vertex verts[kVertsPerQuad]) { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 364 | GrAssert(!toDevice == !toSrc); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 365 | // original quad is specified by tri a,b,c |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 366 | SkPoint a = qpts[0]; |
| 367 | SkPoint b = qpts[1]; |
| 368 | SkPoint c = qpts[2]; |
| 369 | |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 370 | // 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] | 371 | GrPathUtils::QuadUVMatrix DevToUV(qpts); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 372 | |
| 373 | if (toDevice) { |
| 374 | toDevice->mapPoints(&a, 1); |
| 375 | toDevice->mapPoints(&b, 1); |
| 376 | toDevice->mapPoints(&c, 1); |
| 377 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 378 | // make a new poly where we replace a and c by a 1-pixel wide edges orthog |
| 379 | // to edges ab and bc: |
| 380 | // |
| 381 | // before | after |
| 382 | // | b0 |
| 383 | // b | |
| 384 | // | |
| 385 | // | a0 c0 |
| 386 | // a c | a1 c1 |
| 387 | // |
| 388 | // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c, |
| 389 | // respectively. |
| 390 | Vertex& a0 = verts[0]; |
| 391 | Vertex& a1 = verts[1]; |
| 392 | Vertex& b0 = verts[2]; |
| 393 | Vertex& c0 = verts[3]; |
| 394 | Vertex& c1 = verts[4]; |
| 395 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 396 | SkVector ab = b; |
| 397 | ab -= a; |
| 398 | SkVector ac = c; |
| 399 | ac -= a; |
| 400 | SkVector cb = b; |
| 401 | cb -= c; |
| 402 | |
| 403 | // We should have already handled degenerates |
| 404 | GrAssert(ab.length() > 0 && cb.length() > 0); |
| 405 | |
| 406 | ab.normalize(); |
| 407 | SkVector abN; |
| 408 | abN.setOrthog(ab, SkVector::kLeft_Side); |
| 409 | if (abN.dot(ac) > 0) { |
| 410 | abN.negate(); |
| 411 | } |
| 412 | |
| 413 | cb.normalize(); |
| 414 | SkVector cbN; |
| 415 | cbN.setOrthog(cb, SkVector::kLeft_Side); |
| 416 | if (cbN.dot(ac) < 0) { |
| 417 | cbN.negate(); |
| 418 | } |
| 419 | |
| 420 | a0.fPos = a; |
| 421 | a0.fPos += abN; |
| 422 | a1.fPos = a; |
| 423 | a1.fPos -= abN; |
| 424 | |
| 425 | c0.fPos = c; |
| 426 | c0.fPos += cbN; |
| 427 | c1.fPos = c; |
| 428 | c1.fPos -= cbN; |
| 429 | |
| 430 | intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos); |
| 431 | |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 432 | if (toSrc) { |
| 433 | toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(Vertex), kVertsPerQuad); |
| 434 | } |
bsalomon@google.com | 1971317 | 2012-03-15 13:51:08 +0000 | [diff] [blame] | 435 | DevToUV.apply<kVertsPerQuad, sizeof(Vertex), sizeof(GrPoint)>(verts); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | void add_quads(const SkPoint p[3], |
| 439 | int subdiv, |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 440 | const SkMatrix* toDevice, |
| 441 | const SkMatrix* toSrc, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 442 | Vertex** vert) { |
| 443 | GrAssert(subdiv >= 0); |
| 444 | if (subdiv) { |
| 445 | SkPoint newP[5]; |
| 446 | SkChopQuadAtHalf(p, newP); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 447 | add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert); |
| 448 | add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 449 | } else { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 450 | bloat_quad(p, toDevice, toSrc, *vert); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 451 | *vert += kVertsPerQuad; |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | void add_line(const SkPoint p[2], |
| 456 | int rtHeight, |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 457 | const SkMatrix* toSrc, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 458 | Vertex** vert) { |
| 459 | const SkPoint& a = p[0]; |
| 460 | const SkPoint& b = p[1]; |
| 461 | |
| 462 | SkVector orthVec = b; |
| 463 | orthVec -= a; |
| 464 | |
| 465 | if (orthVec.setLength(SK_Scalar1)) { |
| 466 | orthVec.setOrthog(orthVec); |
| 467 | |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 468 | SkScalar lineC = -(a.dot(orthVec)); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 469 | for (int i = 0; i < kVertsPerLineSeg; ++i) { |
| 470 | (*vert)[i].fPos = (i < 2) ? a : b; |
| 471 | if (0 == i || 3 == i) { |
| 472 | (*vert)[i].fPos -= orthVec; |
| 473 | } else { |
| 474 | (*vert)[i].fPos += orthVec; |
| 475 | } |
bsalomon@google.com | 706f668 | 2012-10-23 14:53:55 +0000 | [diff] [blame] | 476 | (*vert)[i].fLine.fA = orthVec.fX; |
| 477 | (*vert)[i].fLine.fB = orthVec.fY; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 478 | (*vert)[i].fLine.fC = lineC; |
| 479 | } |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 480 | if (NULL != toSrc) { |
| 481 | toSrc->mapPointsWithStride(&(*vert)->fPos, |
| 482 | sizeof(Vertex), |
| 483 | kVertsPerLineSeg); |
| 484 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 485 | } else { |
| 486 | // just make it degenerate and likely offscreen |
| 487 | (*vert)[0].fPos.set(SK_ScalarMax, SK_ScalarMax); |
| 488 | (*vert)[1].fPos.set(SK_ScalarMax, SK_ScalarMax); |
| 489 | (*vert)[2].fPos.set(SK_ScalarMax, SK_ScalarMax); |
| 490 | (*vert)[3].fPos.set(SK_ScalarMax, SK_ScalarMax); |
| 491 | } |
| 492 | |
| 493 | *vert += kVertsPerLineSeg; |
| 494 | } |
| 495 | |
| 496 | } |
| 497 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 498 | /////////////////////////////////////////////////////////////////////////////// |
| 499 | |
| 500 | /** |
| 501 | * The output of this effect is a hairline edge for quadratics. |
| 502 | * Quadratic specified by 0=u^2-v canonical coords. u and v are the first |
| 503 | * two components of the vertex attribute. Uses unsigned distance. |
| 504 | * Coverage is min(0, 1-distance). 3rd & 4th component unused. |
| 505 | * Requires shader derivative instruction support. |
| 506 | */ |
| 507 | class HairQuadEdgeEffect : public GrEffect { |
| 508 | public: |
| 509 | |
| 510 | static GrEffectRef* Create() { |
| 511 | // we go through this so we only have one copy of each effect |
commit-bot@chromium.org | 5d01bec | 2013-04-02 20:48:38 +0000 | [diff] [blame] | 512 | static SkAutoTUnref<GrEffectRef> gHairQuadEdgeEffectRef( |
| 513 | CreateEffectRef(AutoEffectUnref(SkNEW(HairQuadEdgeEffect)))); |
| 514 | |
| 515 | gHairQuadEdgeEffectRef.get()->ref(); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 516 | return gHairQuadEdgeEffectRef; |
| 517 | } |
| 518 | |
| 519 | virtual ~HairQuadEdgeEffect() {} |
| 520 | |
| 521 | static const char* Name() { return "HairQuadEdge"; } |
| 522 | |
| 523 | virtual void getConstantColorComponents(GrColor* color, |
| 524 | uint32_t* validFlags) const SK_OVERRIDE { |
| 525 | *validFlags = 0; |
| 526 | } |
| 527 | |
| 528 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 529 | return GrTBackendEffectFactory<HairQuadEdgeEffect>::getInstance(); |
| 530 | } |
| 531 | |
| 532 | class GLEffect : public GrGLEffect { |
| 533 | public: |
| 534 | GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
| 535 | : INHERITED (factory) {} |
| 536 | |
| 537 | virtual void emitCode(GrGLShaderBuilder* builder, |
| 538 | const GrDrawEffect& drawEffect, |
| 539 | EffectKey key, |
| 540 | const char* outputColor, |
| 541 | const char* inputColor, |
| 542 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 543 | const char *vsName, *fsName; |
| 544 | const SkString* attrName = |
| 545 | builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
| 546 | builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n"); |
| 547 | |
| 548 | SkAssertResult(builder->enableFeature( |
| 549 | GrGLShaderBuilder::kStandardDerivatives_GLSLFeature)); |
| 550 | builder->addVarying(kVec4f_GrSLType, "HairQuadEdge", &vsName, &fsName); |
| 551 | |
| 552 | builder->fsCodeAppendf("\t\tvec2 duvdx = dFdx(%s.xy);\n", fsName); |
| 553 | builder->fsCodeAppendf("\t\tvec2 duvdy = dFdy(%s.xy);\n", fsName); |
| 554 | builder->fsCodeAppendf("\t\tvec2 gF = vec2(2.0*%s.x*duvdx.x - duvdx.y,\n" |
| 555 | "\t\t 2.0*%s.x*duvdy.x - duvdy.y);\n", |
| 556 | fsName, fsName); |
| 557 | builder->fsCodeAppendf("\t\tedgeAlpha = (%s.x*%s.x - %s.y);\n", fsName, fsName, |
| 558 | fsName); |
| 559 | builder->fsCodeAppend("\t\tedgeAlpha = sqrt(edgeAlpha*edgeAlpha / dot(gF, gF));\n"); |
| 560 | builder->fsCodeAppend("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"); |
| 561 | |
| 562 | SkString modulate; |
| 563 | GrGLSLModulate4f(&modulate, inputColor, "edgeAlpha"); |
| 564 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); |
| 565 | |
| 566 | builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str()); |
| 567 | } |
| 568 | |
| 569 | static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { |
| 570 | return 0x0; |
| 571 | } |
| 572 | |
| 573 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {} |
| 574 | |
| 575 | private: |
| 576 | typedef GrGLEffect INHERITED; |
| 577 | }; |
| 578 | |
| 579 | private: |
| 580 | HairQuadEdgeEffect() { |
| 581 | this->addVertexAttrib(kVec4f_GrSLType); |
| 582 | } |
| 583 | |
| 584 | virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { |
| 585 | return true; |
| 586 | } |
| 587 | |
| 588 | GR_DECLARE_EFFECT_TEST; |
| 589 | |
| 590 | typedef GrEffect INHERITED; |
| 591 | }; |
| 592 | |
| 593 | GR_DEFINE_EFFECT_TEST(HairQuadEdgeEffect); |
| 594 | |
| 595 | GrEffectRef* HairQuadEdgeEffect::TestCreate(SkMWCRandom* random, |
| 596 | GrContext*, |
| 597 | const GrDrawTargetCaps& caps, |
| 598 | GrTexture*[]) { |
| 599 | // Doesn't work without derivative instructions. |
| 600 | return caps.shaderDerivativeSupport() ? HairQuadEdgeEffect::Create() : NULL;} |
| 601 | |
| 602 | /////////////////////////////////////////////////////////////////////////////// |
| 603 | |
| 604 | /** |
| 605 | * The output of this effect is a 1-pixel wide line. |
| 606 | * Input is 2D implicit device coord line eq (a*x + b*y +c = 0). 4th component unused. |
| 607 | */ |
| 608 | class HairLineEdgeEffect : public GrEffect { |
| 609 | public: |
| 610 | |
| 611 | static GrEffectRef* Create() { |
| 612 | // we go through this so we only have one copy of each effect |
commit-bot@chromium.org | 5d01bec | 2013-04-02 20:48:38 +0000 | [diff] [blame] | 613 | static SkAutoTUnref<GrEffectRef> gHairLineEdgeEffectRef( |
| 614 | CreateEffectRef(AutoEffectUnref(SkNEW(HairLineEdgeEffect)))); |
| 615 | |
| 616 | gHairLineEdgeEffectRef.get()->ref(); |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 617 | return gHairLineEdgeEffectRef; |
| 618 | } |
| 619 | |
| 620 | virtual ~HairLineEdgeEffect() {} |
| 621 | |
| 622 | static const char* Name() { return "HairLineEdge"; } |
| 623 | |
| 624 | virtual void getConstantColorComponents(GrColor* color, |
| 625 | uint32_t* validFlags) const SK_OVERRIDE { |
| 626 | *validFlags = 0; |
| 627 | } |
| 628 | |
| 629 | virtual const GrBackendEffectFactory& getFactory() const SK_OVERRIDE { |
| 630 | return GrTBackendEffectFactory<HairLineEdgeEffect>::getInstance(); |
| 631 | } |
| 632 | |
| 633 | class GLEffect : public GrGLEffect { |
| 634 | public: |
| 635 | GLEffect(const GrBackendEffectFactory& factory, const GrDrawEffect&) |
| 636 | : INHERITED (factory) {} |
| 637 | |
| 638 | virtual void emitCode(GrGLShaderBuilder* builder, |
| 639 | const GrDrawEffect& drawEffect, |
| 640 | EffectKey key, |
| 641 | const char* outputColor, |
| 642 | const char* inputColor, |
| 643 | const TextureSamplerArray& samplers) SK_OVERRIDE { |
| 644 | const char *vsName, *fsName; |
| 645 | const SkString* attrName = |
| 646 | builder->getEffectAttributeName(drawEffect.getVertexAttribIndices()[0]); |
| 647 | builder->fsCodeAppendf("\t\tfloat edgeAlpha;\n"); |
| 648 | |
| 649 | builder->addVarying(kVec4f_GrSLType, "HairLineEdge", &vsName, &fsName); |
| 650 | |
| 651 | builder->fsCodeAppendf("\t\tedgeAlpha = abs(dot(vec3(%s.xy,1), %s.xyz));\n", |
| 652 | builder->fragmentPosition(), fsName); |
| 653 | builder->fsCodeAppendf("\t\tedgeAlpha = max(1.0 - edgeAlpha, 0.0);\n"); |
| 654 | |
| 655 | SkString modulate; |
| 656 | GrGLSLModulate4f(&modulate, inputColor, "edgeAlpha"); |
| 657 | builder->fsCodeAppendf("\t%s = %s;\n", outputColor, modulate.c_str()); |
| 658 | |
| 659 | builder->vsCodeAppendf("\t%s = %s;\n", vsName, attrName->c_str()); |
| 660 | } |
| 661 | |
| 662 | static inline EffectKey GenKey(const GrDrawEffect& drawEffect, const GrGLCaps&) { |
| 663 | return 0x0; |
| 664 | } |
| 665 | |
| 666 | virtual void setData(const GrGLUniformManager&, const GrDrawEffect&) SK_OVERRIDE {} |
| 667 | |
| 668 | private: |
| 669 | typedef GrGLEffect INHERITED; |
| 670 | }; |
| 671 | |
| 672 | private: |
| 673 | HairLineEdgeEffect() { |
| 674 | this->addVertexAttrib(kVec4f_GrSLType); |
| 675 | } |
| 676 | |
| 677 | virtual bool onIsEqual(const GrEffect& other) const SK_OVERRIDE { |
| 678 | return true; |
| 679 | } |
| 680 | |
| 681 | GR_DECLARE_EFFECT_TEST; |
| 682 | |
| 683 | typedef GrEffect INHERITED; |
| 684 | }; |
| 685 | |
| 686 | GR_DEFINE_EFFECT_TEST(HairLineEdgeEffect); |
| 687 | |
| 688 | GrEffectRef* HairLineEdgeEffect::TestCreate(SkMWCRandom* random, |
| 689 | GrContext*, |
| 690 | const GrDrawTargetCaps& caps, |
| 691 | GrTexture*[]) { |
| 692 | return HairLineEdgeEffect::Create(); |
| 693 | } |
| 694 | |
| 695 | /////////////////////////////////////////////////////////////////////////////// |
| 696 | |
bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 697 | bool GrAAHairLinePathRenderer::createGeom( |
| 698 | const SkPath& path, |
bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 699 | GrDrawTarget* target, |
bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 700 | int* lineCnt, |
| 701 | int* quadCnt, |
| 702 | GrDrawTarget::AutoReleaseGeometry* arg) { |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 703 | GrDrawState* drawState = target->drawState(); |
| 704 | int rtHeight = drawState->getRenderTarget()->height(); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 705 | |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 706 | GrIRect devClipBounds; |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 707 | target->getClip()->getConservativeBounds(drawState->getRenderTarget(), |
robertphillips@google.com | 7b11289 | 2012-07-31 15:18:21 +0000 | [diff] [blame] | 708 | &devClipBounds); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 709 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 710 | // position + edge |
| 711 | static const GrVertexAttrib kAttribs[] = { |
jvanverth@google.com | 054ae99 | 2013-04-01 20:06:51 +0000 | [diff] [blame] | 712 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
| 713 | {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding} |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 714 | }; |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 715 | SkMatrix viewM = drawState->getViewMatrix(); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 716 | |
bsalomon@google.com | 9266901 | 2011-09-27 19:10:05 +0000 | [diff] [blame] | 717 | PREALLOC_PTARRAY(128) lines; |
| 718 | PREALLOC_PTARRAY(128) quads; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 719 | IntArray qSubdivs; |
bsalomon@google.com | 0f11e1a | 2012-10-08 14:48:36 +0000 | [diff] [blame] | 720 | *quadCnt = generate_lines_and_quads(path, viewM, devClipBounds, |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 721 | &lines, &quads, &qSubdivs); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 722 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 723 | *lineCnt = lines.count() / 2; |
| 724 | int vertCnt = kVertsPerLineSeg * *lineCnt + kVertsPerQuad * *quadCnt; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 725 | |
jvanverth@google.com | 9b855c7 | 2013-03-01 18:21:22 +0000 | [diff] [blame] | 726 | target->drawState()->setVertexAttribs(kAttribs, SK_ARRAY_COUNT(kAttribs)); |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 727 | GrAssert(sizeof(Vertex) == target->getDrawState().getVertexSize()); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 728 | |
jvanverth@google.com | b75b0a0 | 2013-02-05 20:33:30 +0000 | [diff] [blame] | 729 | if (!arg->set(target, vertCnt, 0)) { |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 730 | return false; |
| 731 | } |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 732 | |
bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 733 | Vertex* verts = reinterpret_cast<Vertex*>(arg->vertices()); |
| 734 | |
bsalomon@google.com | b9086a0 | 2012-11-01 18:02:54 +0000 | [diff] [blame] | 735 | const SkMatrix* toDevice = NULL; |
| 736 | const SkMatrix* toSrc = NULL; |
| 737 | SkMatrix ivm; |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 738 | |
| 739 | if (viewM.hasPerspective()) { |
| 740 | if (viewM.invert(&ivm)) { |
| 741 | toDevice = &viewM; |
| 742 | toSrc = &ivm; |
| 743 | } |
| 744 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 745 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 746 | for (int i = 0; i < *lineCnt; ++i) { |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 747 | add_line(&lines[2*i], rtHeight, toSrc, &verts); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 748 | } |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 749 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 750 | int unsubdivQuadCnt = quads.count() / 3; |
| 751 | for (int i = 0; i < unsubdivQuadCnt; ++i) { |
| 752 | GrAssert(qSubdivs[i] >= 0); |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 753 | add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 754 | } |
| 755 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 756 | return true; |
| 757 | } |
| 758 | |
robertphillips@google.com | 8a4fc40 | 2012-05-24 12:42:24 +0000 | [diff] [blame] | 759 | bool GrAAHairLinePathRenderer::canDrawPath(const SkPath& path, |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 760 | const SkStrokeRec& stroke, |
robertphillips@google.com | 8a4fc40 | 2012-05-24 12:42:24 +0000 | [diff] [blame] | 761 | const GrDrawTarget* target, |
| 762 | bool antiAlias) const { |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 763 | if (!stroke.isHairlineStyle() || !antiAlias) { |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 764 | return false; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 765 | } |
| 766 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 767 | static const uint32_t gReqDerivMask = SkPath::kCubic_SegmentMask | |
| 768 | SkPath::kQuad_SegmentMask; |
bsalomon@google.com | bcce892 | 2013-03-25 15:38:39 +0000 | [diff] [blame] | 769 | if (!target->caps()->shaderDerivativeSupport() && |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 770 | (gReqDerivMask & path.getSegmentMasks())) { |
| 771 | return false; |
| 772 | } |
| 773 | return true; |
| 774 | } |
| 775 | |
| 776 | bool GrAAHairLinePathRenderer::onDrawPath(const SkPath& path, |
sugoi@google.com | 5f74cf8 | 2012-12-17 21:16:45 +0000 | [diff] [blame] | 777 | const SkStrokeRec&, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 778 | GrDrawTarget* target, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 779 | bool antiAlias) { |
| 780 | |
| 781 | int lineCnt; |
| 782 | int quadCnt; |
bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 783 | GrDrawTarget::AutoReleaseGeometry arg; |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 784 | if (!this->createGeom(path, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 785 | target, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 786 | &lineCnt, |
bsalomon@google.com | b372942 | 2012-03-07 19:13:28 +0000 | [diff] [blame] | 787 | &quadCnt, |
| 788 | &arg)) { |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 789 | return false; |
| 790 | } |
| 791 | |
bsalomon@google.com | 4647f90 | 2013-03-26 14:45:27 +0000 | [diff] [blame] | 792 | GrDrawTarget::AutoStateRestore asr(target, GrDrawTarget::kPreserve_ASRInit); |
bsalomon@google.com | 873ea0c | 2012-03-30 15:55:32 +0000 | [diff] [blame] | 793 | GrDrawState* drawState = target->drawState(); |
bsalomon@google.com | 4647f90 | 2013-03-26 14:45:27 +0000 | [diff] [blame] | 794 | |
| 795 | GrDrawState::AutoDeviceCoordDraw adcd; |
bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 796 | // createGeom transforms the geometry to device space when the matrix does not have |
| 797 | // perspective. |
bsalomon@google.com | 8f9cbd6 | 2011-12-09 15:55:34 +0000 | [diff] [blame] | 798 | if (!drawState->getViewMatrix().hasPerspective()) { |
bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 799 | adcd.set(drawState); |
| 800 | if (!adcd.succeeded()) { |
bsalomon@google.com | e3d3216 | 2012-07-20 13:37:06 +0000 | [diff] [blame] | 801 | return false; |
bsalomon@google.com | dbeeac3 | 2011-09-12 14:59:34 +0000 | [diff] [blame] | 802 | } |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 803 | } |
rmistry@google.com | fbfcd56 | 2012-08-23 18:09:54 +0000 | [diff] [blame] | 804 | |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 805 | // TODO: See whether rendering lines as degenerate quads improves perf |
| 806 | // when we have a mix |
bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 807 | |
bsalomon@google.com | 4647f90 | 2013-03-26 14:45:27 +0000 | [diff] [blame] | 808 | enum { |
| 809 | // the edge effects share this stage with glyph rendering |
| 810 | // (kGlyphMaskStage in GrTextContext) && SW path rendering |
| 811 | // (kPathMaskStage in GrSWMaskHelper) |
| 812 | kEdgeEffectStage = GrPaint::kTotalStages, |
| 813 | }; |
| 814 | static const int kEdgeAttrIndex = 1; |
bsalomon@google.com | a834746 | 2012-10-08 18:59:39 +0000 | [diff] [blame] | 815 | |
commit-bot@chromium.org | 90c240a | 2013-04-02 17:57:21 +0000 | [diff] [blame] | 816 | GrEffectRef* hairLineEffect = HairLineEdgeEffect::Create(); |
| 817 | GrEffectRef* hairQuadEffect = HairQuadEdgeEffect::Create(); |
skia.committer@gmail.com | 37cbc7f | 2013-03-27 07:01:04 +0000 | [diff] [blame] | 818 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 819 | target->setIndexSourceToBuffer(fLinesIndexBuffer); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 820 | int lines = 0; |
| 821 | int nBufLines = fLinesIndexBuffer->maxQuads(); |
bsalomon@google.com | 4647f90 | 2013-03-26 14:45:27 +0000 | [diff] [blame] | 822 | drawState->setEffect(kEdgeEffectStage, hairLineEffect, kEdgeAttrIndex)->unref(); |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 823 | while (lines < lineCnt) { |
| 824 | int n = GrMin(lineCnt - lines, nBufLines); |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 825 | target->drawIndexed(kTriangles_GrPrimitiveType, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 826 | kVertsPerLineSeg*lines, // startV |
| 827 | 0, // startI |
| 828 | kVertsPerLineSeg*n, // vCount |
| 829 | kIdxsPerLineSeg*n); // iCount |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 830 | lines += n; |
| 831 | } |
| 832 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 833 | target->setIndexSourceToBuffer(fQuadsIndexBuffer); |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 834 | int quads = 0; |
bsalomon@google.com | 4647f90 | 2013-03-26 14:45:27 +0000 | [diff] [blame] | 835 | drawState->setEffect(kEdgeEffectStage, hairQuadEffect, kEdgeAttrIndex)->unref(); |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 836 | while (quads < quadCnt) { |
| 837 | int n = GrMin(quadCnt - quads, kNumQuadsInIdxBuffer); |
bsalomon@google.com | 4705954 | 2012-06-06 20:51:20 +0000 | [diff] [blame] | 838 | target->drawIndexed(kTriangles_GrPrimitiveType, |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 839 | 4 * lineCnt + kVertsPerQuad*quads, // startV |
| 840 | 0, // startI |
| 841 | kVertsPerQuad*n, // vCount |
| 842 | kIdxsPerQuad*n); // iCount |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 843 | quads += n; |
| 844 | } |
bsalomon@google.com | 0406b9e | 2013-04-02 21:00:15 +0000 | [diff] [blame^] | 845 | target->resetIndexSource(); |
bsalomon@google.com | 4647f90 | 2013-03-26 14:45:27 +0000 | [diff] [blame] | 846 | |
bsalomon@google.com | c2099d2 | 2012-03-02 21:26:50 +0000 | [diff] [blame] | 847 | return true; |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 848 | } |