Sat, 03 Jan 2015 20:18:00 +0100
Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.
michael@0 | 1 | /* |
michael@0 | 2 | * Copyright 2011 Google Inc. |
michael@0 | 3 | * |
michael@0 | 4 | * Use of this source code is governed by a BSD-style license that can be |
michael@0 | 5 | * found in the LICENSE file. |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | #include "GrAAHairLinePathRenderer.h" |
michael@0 | 9 | |
michael@0 | 10 | #include "GrContext.h" |
michael@0 | 11 | #include "GrDrawState.h" |
michael@0 | 12 | #include "GrDrawTargetCaps.h" |
michael@0 | 13 | #include "GrEffect.h" |
michael@0 | 14 | #include "GrGpu.h" |
michael@0 | 15 | #include "GrIndexBuffer.h" |
michael@0 | 16 | #include "GrPathUtils.h" |
michael@0 | 17 | #include "GrTBackendEffectFactory.h" |
michael@0 | 18 | #include "SkGeometry.h" |
michael@0 | 19 | #include "SkStroke.h" |
michael@0 | 20 | #include "SkTemplates.h" |
michael@0 | 21 | |
michael@0 | 22 | #include "effects/GrBezierEffect.h" |
michael@0 | 23 | |
michael@0 | 24 | namespace { |
michael@0 | 25 | // quadratics are rendered as 5-sided polys in order to bound the |
michael@0 | 26 | // AA stroke around the center-curve. See comments in push_quad_index_buffer and |
michael@0 | 27 | // bloat_quad. Quadratics and conics share an index buffer |
michael@0 | 28 | static const int kVertsPerQuad = 5; |
michael@0 | 29 | static const int kIdxsPerQuad = 9; |
michael@0 | 30 | |
michael@0 | 31 | // lines are rendered as: |
michael@0 | 32 | // *______________* |
michael@0 | 33 | // |\ -_______ /| |
michael@0 | 34 | // | \ \ / | |
michael@0 | 35 | // | *--------* | |
michael@0 | 36 | // | / ______/ \ | |
michael@0 | 37 | // */_-__________\* |
michael@0 | 38 | // For: 6 vertices and 18 indices (for 6 triangles) |
michael@0 | 39 | static const int kVertsPerLineSeg = 6; |
michael@0 | 40 | static const int kIdxsPerLineSeg = 18; |
michael@0 | 41 | |
michael@0 | 42 | static const int kNumQuadsInIdxBuffer = 256; |
michael@0 | 43 | static const size_t kQuadIdxSBufize = kIdxsPerQuad * |
michael@0 | 44 | sizeof(uint16_t) * |
michael@0 | 45 | kNumQuadsInIdxBuffer; |
michael@0 | 46 | |
michael@0 | 47 | static const int kNumLineSegsInIdxBuffer = 256; |
michael@0 | 48 | static const size_t kLineSegIdxSBufize = kIdxsPerLineSeg * |
michael@0 | 49 | sizeof(uint16_t) * |
michael@0 | 50 | kNumLineSegsInIdxBuffer; |
michael@0 | 51 | |
michael@0 | 52 | static bool push_quad_index_data(GrIndexBuffer* qIdxBuffer) { |
michael@0 | 53 | uint16_t* data = (uint16_t*) qIdxBuffer->lock(); |
michael@0 | 54 | bool tempData = NULL == data; |
michael@0 | 55 | if (tempData) { |
michael@0 | 56 | data = SkNEW_ARRAY(uint16_t, kNumQuadsInIdxBuffer * kIdxsPerQuad); |
michael@0 | 57 | } |
michael@0 | 58 | for (int i = 0; i < kNumQuadsInIdxBuffer; ++i) { |
michael@0 | 59 | |
michael@0 | 60 | // Each quadratic is rendered as a five sided polygon. This poly bounds |
michael@0 | 61 | // the quadratic's bounding triangle but has been expanded so that the |
michael@0 | 62 | // 1-pixel wide area around the curve is inside the poly. |
michael@0 | 63 | // If a,b,c are the original control points then the poly a0,b0,c0,c1,a1 |
michael@0 | 64 | // that is rendered would look like this: |
michael@0 | 65 | // b0 |
michael@0 | 66 | // b |
michael@0 | 67 | // |
michael@0 | 68 | // a0 c0 |
michael@0 | 69 | // a c |
michael@0 | 70 | // a1 c1 |
michael@0 | 71 | // Each is drawn as three triangles specified by these 9 indices: |
michael@0 | 72 | int baseIdx = i * kIdxsPerQuad; |
michael@0 | 73 | uint16_t baseVert = (uint16_t)(i * kVertsPerQuad); |
michael@0 | 74 | data[0 + baseIdx] = baseVert + 0; // a0 |
michael@0 | 75 | data[1 + baseIdx] = baseVert + 1; // a1 |
michael@0 | 76 | data[2 + baseIdx] = baseVert + 2; // b0 |
michael@0 | 77 | data[3 + baseIdx] = baseVert + 2; // b0 |
michael@0 | 78 | data[4 + baseIdx] = baseVert + 4; // c1 |
michael@0 | 79 | data[5 + baseIdx] = baseVert + 3; // c0 |
michael@0 | 80 | data[6 + baseIdx] = baseVert + 1; // a1 |
michael@0 | 81 | data[7 + baseIdx] = baseVert + 4; // c1 |
michael@0 | 82 | data[8 + baseIdx] = baseVert + 2; // b0 |
michael@0 | 83 | } |
michael@0 | 84 | if (tempData) { |
michael@0 | 85 | bool ret = qIdxBuffer->updateData(data, kQuadIdxSBufize); |
michael@0 | 86 | delete[] data; |
michael@0 | 87 | return ret; |
michael@0 | 88 | } else { |
michael@0 | 89 | qIdxBuffer->unlock(); |
michael@0 | 90 | return true; |
michael@0 | 91 | } |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | static bool push_line_index_data(GrIndexBuffer* lIdxBuffer) { |
michael@0 | 95 | uint16_t* data = (uint16_t*) lIdxBuffer->lock(); |
michael@0 | 96 | bool tempData = NULL == data; |
michael@0 | 97 | if (tempData) { |
michael@0 | 98 | data = SkNEW_ARRAY(uint16_t, kNumLineSegsInIdxBuffer * kIdxsPerLineSeg); |
michael@0 | 99 | } |
michael@0 | 100 | for (int i = 0; i < kNumLineSegsInIdxBuffer; ++i) { |
michael@0 | 101 | // Each line segment is rendered as two quads and two triangles. |
michael@0 | 102 | // p0 and p1 have alpha = 1 while all other points have alpha = 0. |
michael@0 | 103 | // The four external points are offset 1 pixel perpendicular to the |
michael@0 | 104 | // line and half a pixel parallel to the line. |
michael@0 | 105 | // |
michael@0 | 106 | // p4 p5 |
michael@0 | 107 | // p0 p1 |
michael@0 | 108 | // p2 p3 |
michael@0 | 109 | // |
michael@0 | 110 | // Each is drawn as six triangles specified by these 18 indices: |
michael@0 | 111 | int baseIdx = i * kIdxsPerLineSeg; |
michael@0 | 112 | uint16_t baseVert = (uint16_t)(i * kVertsPerLineSeg); |
michael@0 | 113 | data[0 + baseIdx] = baseVert + 0; |
michael@0 | 114 | data[1 + baseIdx] = baseVert + 1; |
michael@0 | 115 | data[2 + baseIdx] = baseVert + 3; |
michael@0 | 116 | |
michael@0 | 117 | data[3 + baseIdx] = baseVert + 0; |
michael@0 | 118 | data[4 + baseIdx] = baseVert + 3; |
michael@0 | 119 | data[5 + baseIdx] = baseVert + 2; |
michael@0 | 120 | |
michael@0 | 121 | data[6 + baseIdx] = baseVert + 0; |
michael@0 | 122 | data[7 + baseIdx] = baseVert + 4; |
michael@0 | 123 | data[8 + baseIdx] = baseVert + 5; |
michael@0 | 124 | |
michael@0 | 125 | data[9 + baseIdx] = baseVert + 0; |
michael@0 | 126 | data[10+ baseIdx] = baseVert + 5; |
michael@0 | 127 | data[11+ baseIdx] = baseVert + 1; |
michael@0 | 128 | |
michael@0 | 129 | data[12 + baseIdx] = baseVert + 0; |
michael@0 | 130 | data[13 + baseIdx] = baseVert + 2; |
michael@0 | 131 | data[14 + baseIdx] = baseVert + 4; |
michael@0 | 132 | |
michael@0 | 133 | data[15 + baseIdx] = baseVert + 1; |
michael@0 | 134 | data[16 + baseIdx] = baseVert + 5; |
michael@0 | 135 | data[17 + baseIdx] = baseVert + 3; |
michael@0 | 136 | } |
michael@0 | 137 | if (tempData) { |
michael@0 | 138 | bool ret = lIdxBuffer->updateData(data, kLineSegIdxSBufize); |
michael@0 | 139 | delete[] data; |
michael@0 | 140 | return ret; |
michael@0 | 141 | } else { |
michael@0 | 142 | lIdxBuffer->unlock(); |
michael@0 | 143 | return true; |
michael@0 | 144 | } |
michael@0 | 145 | } |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | GrPathRenderer* GrAAHairLinePathRenderer::Create(GrContext* context) { |
michael@0 | 149 | GrGpu* gpu = context->getGpu(); |
michael@0 | 150 | GrIndexBuffer* qIdxBuf = gpu->createIndexBuffer(kQuadIdxSBufize, false); |
michael@0 | 151 | SkAutoTUnref<GrIndexBuffer> qIdxBuffer(qIdxBuf); |
michael@0 | 152 | if (NULL == qIdxBuf || !push_quad_index_data(qIdxBuf)) { |
michael@0 | 153 | return NULL; |
michael@0 | 154 | } |
michael@0 | 155 | GrIndexBuffer* lIdxBuf = gpu->createIndexBuffer(kLineSegIdxSBufize, false); |
michael@0 | 156 | SkAutoTUnref<GrIndexBuffer> lIdxBuffer(lIdxBuf); |
michael@0 | 157 | if (NULL == lIdxBuf || !push_line_index_data(lIdxBuf)) { |
michael@0 | 158 | return NULL; |
michael@0 | 159 | } |
michael@0 | 160 | return SkNEW_ARGS(GrAAHairLinePathRenderer, |
michael@0 | 161 | (context, lIdxBuf, qIdxBuf)); |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | GrAAHairLinePathRenderer::GrAAHairLinePathRenderer( |
michael@0 | 165 | const GrContext* context, |
michael@0 | 166 | const GrIndexBuffer* linesIndexBuffer, |
michael@0 | 167 | const GrIndexBuffer* quadsIndexBuffer) { |
michael@0 | 168 | fLinesIndexBuffer = linesIndexBuffer; |
michael@0 | 169 | linesIndexBuffer->ref(); |
michael@0 | 170 | fQuadsIndexBuffer = quadsIndexBuffer; |
michael@0 | 171 | quadsIndexBuffer->ref(); |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | GrAAHairLinePathRenderer::~GrAAHairLinePathRenderer() { |
michael@0 | 175 | fLinesIndexBuffer->unref(); |
michael@0 | 176 | fQuadsIndexBuffer->unref(); |
michael@0 | 177 | } |
michael@0 | 178 | |
michael@0 | 179 | namespace { |
michael@0 | 180 | |
michael@0 | 181 | #define PREALLOC_PTARRAY(N) SkSTArray<(N),SkPoint, true> |
michael@0 | 182 | |
michael@0 | 183 | // Takes 178th time of logf on Z600 / VC2010 |
michael@0 | 184 | int get_float_exp(float x) { |
michael@0 | 185 | GR_STATIC_ASSERT(sizeof(int) == sizeof(float)); |
michael@0 | 186 | #ifdef SK_DEBUG |
michael@0 | 187 | static bool tested; |
michael@0 | 188 | if (!tested) { |
michael@0 | 189 | tested = true; |
michael@0 | 190 | SkASSERT(get_float_exp(0.25f) == -2); |
michael@0 | 191 | SkASSERT(get_float_exp(0.3f) == -2); |
michael@0 | 192 | SkASSERT(get_float_exp(0.5f) == -1); |
michael@0 | 193 | SkASSERT(get_float_exp(1.f) == 0); |
michael@0 | 194 | SkASSERT(get_float_exp(2.f) == 1); |
michael@0 | 195 | SkASSERT(get_float_exp(2.5f) == 1); |
michael@0 | 196 | SkASSERT(get_float_exp(8.f) == 3); |
michael@0 | 197 | SkASSERT(get_float_exp(100.f) == 6); |
michael@0 | 198 | SkASSERT(get_float_exp(1000.f) == 9); |
michael@0 | 199 | SkASSERT(get_float_exp(1024.f) == 10); |
michael@0 | 200 | SkASSERT(get_float_exp(3000000.f) == 21); |
michael@0 | 201 | } |
michael@0 | 202 | #endif |
michael@0 | 203 | const int* iptr = (const int*)&x; |
michael@0 | 204 | return (((*iptr) & 0x7f800000) >> 23) - 127; |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | // Uses the max curvature function for quads to estimate |
michael@0 | 208 | // where to chop the conic. If the max curvature is not |
michael@0 | 209 | // found along the curve segment it will return 1 and |
michael@0 | 210 | // dst[0] is the original conic. If it returns 2 the dst[0] |
michael@0 | 211 | // and dst[1] are the two new conics. |
michael@0 | 212 | int split_conic(const SkPoint src[3], SkConic dst[2], const SkScalar weight) { |
michael@0 | 213 | SkScalar t = SkFindQuadMaxCurvature(src); |
michael@0 | 214 | if (t == 0) { |
michael@0 | 215 | if (dst) { |
michael@0 | 216 | dst[0].set(src, weight); |
michael@0 | 217 | } |
michael@0 | 218 | return 1; |
michael@0 | 219 | } else { |
michael@0 | 220 | if (dst) { |
michael@0 | 221 | SkConic conic; |
michael@0 | 222 | conic.set(src, weight); |
michael@0 | 223 | conic.chopAt(t, dst); |
michael@0 | 224 | } |
michael@0 | 225 | return 2; |
michael@0 | 226 | } |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | // Calls split_conic on the entire conic and then once more on each subsection. |
michael@0 | 230 | // Most cases will result in either 1 conic (chop point is not within t range) |
michael@0 | 231 | // or 3 points (split once and then one subsection is split again). |
michael@0 | 232 | int chop_conic(const SkPoint src[3], SkConic dst[4], const SkScalar weight) { |
michael@0 | 233 | SkConic dstTemp[2]; |
michael@0 | 234 | int conicCnt = split_conic(src, dstTemp, weight); |
michael@0 | 235 | if (2 == conicCnt) { |
michael@0 | 236 | int conicCnt2 = split_conic(dstTemp[0].fPts, dst, dstTemp[0].fW); |
michael@0 | 237 | conicCnt = conicCnt2 + split_conic(dstTemp[1].fPts, &dst[conicCnt2], dstTemp[1].fW); |
michael@0 | 238 | } else { |
michael@0 | 239 | dst[0] = dstTemp[0]; |
michael@0 | 240 | } |
michael@0 | 241 | return conicCnt; |
michael@0 | 242 | } |
michael@0 | 243 | |
michael@0 | 244 | // returns 0 if quad/conic is degen or close to it |
michael@0 | 245 | // in this case approx the path with lines |
michael@0 | 246 | // otherwise returns 1 |
michael@0 | 247 | int is_degen_quad_or_conic(const SkPoint p[3]) { |
michael@0 | 248 | static const SkScalar gDegenerateToLineTol = SK_Scalar1; |
michael@0 | 249 | static const SkScalar gDegenerateToLineTolSqd = |
michael@0 | 250 | SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol); |
michael@0 | 251 | |
michael@0 | 252 | if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd || |
michael@0 | 253 | p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) { |
michael@0 | 254 | return 1; |
michael@0 | 255 | } |
michael@0 | 256 | |
michael@0 | 257 | SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]); |
michael@0 | 258 | if (dsqd < gDegenerateToLineTolSqd) { |
michael@0 | 259 | return 1; |
michael@0 | 260 | } |
michael@0 | 261 | |
michael@0 | 262 | if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) { |
michael@0 | 263 | return 1; |
michael@0 | 264 | } |
michael@0 | 265 | return 0; |
michael@0 | 266 | } |
michael@0 | 267 | |
michael@0 | 268 | // we subdivide the quads to avoid huge overfill |
michael@0 | 269 | // if it returns -1 then should be drawn as lines |
michael@0 | 270 | int num_quad_subdivs(const SkPoint p[3]) { |
michael@0 | 271 | static const SkScalar gDegenerateToLineTol = SK_Scalar1; |
michael@0 | 272 | static const SkScalar gDegenerateToLineTolSqd = |
michael@0 | 273 | SkScalarMul(gDegenerateToLineTol, gDegenerateToLineTol); |
michael@0 | 274 | |
michael@0 | 275 | if (p[0].distanceToSqd(p[1]) < gDegenerateToLineTolSqd || |
michael@0 | 276 | p[1].distanceToSqd(p[2]) < gDegenerateToLineTolSqd) { |
michael@0 | 277 | return -1; |
michael@0 | 278 | } |
michael@0 | 279 | |
michael@0 | 280 | SkScalar dsqd = p[1].distanceToLineBetweenSqd(p[0], p[2]); |
michael@0 | 281 | if (dsqd < gDegenerateToLineTolSqd) { |
michael@0 | 282 | return -1; |
michael@0 | 283 | } |
michael@0 | 284 | |
michael@0 | 285 | if (p[2].distanceToLineBetweenSqd(p[1], p[0]) < gDegenerateToLineTolSqd) { |
michael@0 | 286 | return -1; |
michael@0 | 287 | } |
michael@0 | 288 | |
michael@0 | 289 | // tolerance of triangle height in pixels |
michael@0 | 290 | // tuned on windows Quadro FX 380 / Z600 |
michael@0 | 291 | // trade off of fill vs cpu time on verts |
michael@0 | 292 | // maybe different when do this using gpu (geo or tess shaders) |
michael@0 | 293 | static const SkScalar gSubdivTol = 175 * SK_Scalar1; |
michael@0 | 294 | |
michael@0 | 295 | if (dsqd <= SkScalarMul(gSubdivTol, gSubdivTol)) { |
michael@0 | 296 | return 0; |
michael@0 | 297 | } else { |
michael@0 | 298 | static const int kMaxSub = 4; |
michael@0 | 299 | // subdividing the quad reduces d by 4. so we want x = log4(d/tol) |
michael@0 | 300 | // = log4(d*d/tol*tol)/2 |
michael@0 | 301 | // = log2(d*d/tol*tol) |
michael@0 | 302 | |
michael@0 | 303 | // +1 since we're ignoring the mantissa contribution. |
michael@0 | 304 | int log = get_float_exp(dsqd/(gSubdivTol*gSubdivTol)) + 1; |
michael@0 | 305 | log = GrMin(GrMax(0, log), kMaxSub); |
michael@0 | 306 | return log; |
michael@0 | 307 | } |
michael@0 | 308 | } |
michael@0 | 309 | |
michael@0 | 310 | /** |
michael@0 | 311 | * Generates the lines and quads to be rendered. Lines are always recorded in |
michael@0 | 312 | * device space. We will do a device space bloat to account for the 1pixel |
michael@0 | 313 | * thickness. |
michael@0 | 314 | * Quads are recorded in device space unless m contains |
michael@0 | 315 | * perspective, then in they are in src space. We do this because we will |
michael@0 | 316 | * subdivide large quads to reduce over-fill. This subdivision has to be |
michael@0 | 317 | * performed before applying the perspective matrix. |
michael@0 | 318 | */ |
michael@0 | 319 | int generate_lines_and_quads(const SkPath& path, |
michael@0 | 320 | const SkMatrix& m, |
michael@0 | 321 | const SkIRect& devClipBounds, |
michael@0 | 322 | GrAAHairLinePathRenderer::PtArray* lines, |
michael@0 | 323 | GrAAHairLinePathRenderer::PtArray* quads, |
michael@0 | 324 | GrAAHairLinePathRenderer::PtArray* conics, |
michael@0 | 325 | GrAAHairLinePathRenderer::IntArray* quadSubdivCnts, |
michael@0 | 326 | GrAAHairLinePathRenderer::FloatArray* conicWeights) { |
michael@0 | 327 | SkPath::Iter iter(path, false); |
michael@0 | 328 | |
michael@0 | 329 | int totalQuadCount = 0; |
michael@0 | 330 | SkRect bounds; |
michael@0 | 331 | SkIRect ibounds; |
michael@0 | 332 | |
michael@0 | 333 | bool persp = m.hasPerspective(); |
michael@0 | 334 | |
michael@0 | 335 | for (;;) { |
michael@0 | 336 | GrPoint pathPts[4]; |
michael@0 | 337 | GrPoint devPts[4]; |
michael@0 | 338 | SkPath::Verb verb = iter.next(pathPts); |
michael@0 | 339 | switch (verb) { |
michael@0 | 340 | case SkPath::kConic_Verb: { |
michael@0 | 341 | SkConic dst[4]; |
michael@0 | 342 | // We chop the conics to create tighter clipping to hide error |
michael@0 | 343 | // that appears near max curvature of very thin conics. Thin |
michael@0 | 344 | // hyperbolas with high weight still show error. |
michael@0 | 345 | int conicCnt = chop_conic(pathPts, dst, iter.conicWeight()); |
michael@0 | 346 | for (int i = 0; i < conicCnt; ++i) { |
michael@0 | 347 | SkPoint* chopPnts = dst[i].fPts; |
michael@0 | 348 | m.mapPoints(devPts, chopPnts, 3); |
michael@0 | 349 | bounds.setBounds(devPts, 3); |
michael@0 | 350 | bounds.outset(SK_Scalar1, SK_Scalar1); |
michael@0 | 351 | bounds.roundOut(&ibounds); |
michael@0 | 352 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
michael@0 | 353 | if (is_degen_quad_or_conic(devPts)) { |
michael@0 | 354 | SkPoint* pts = lines->push_back_n(4); |
michael@0 | 355 | pts[0] = devPts[0]; |
michael@0 | 356 | pts[1] = devPts[1]; |
michael@0 | 357 | pts[2] = devPts[1]; |
michael@0 | 358 | pts[3] = devPts[2]; |
michael@0 | 359 | } else { |
michael@0 | 360 | // when in perspective keep conics in src space |
michael@0 | 361 | SkPoint* cPts = persp ? chopPnts : devPts; |
michael@0 | 362 | SkPoint* pts = conics->push_back_n(3); |
michael@0 | 363 | pts[0] = cPts[0]; |
michael@0 | 364 | pts[1] = cPts[1]; |
michael@0 | 365 | pts[2] = cPts[2]; |
michael@0 | 366 | conicWeights->push_back() = dst[i].fW; |
michael@0 | 367 | } |
michael@0 | 368 | } |
michael@0 | 369 | } |
michael@0 | 370 | break; |
michael@0 | 371 | } |
michael@0 | 372 | case SkPath::kMove_Verb: |
michael@0 | 373 | break; |
michael@0 | 374 | case SkPath::kLine_Verb: |
michael@0 | 375 | m.mapPoints(devPts, pathPts, 2); |
michael@0 | 376 | bounds.setBounds(devPts, 2); |
michael@0 | 377 | bounds.outset(SK_Scalar1, SK_Scalar1); |
michael@0 | 378 | bounds.roundOut(&ibounds); |
michael@0 | 379 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
michael@0 | 380 | SkPoint* pts = lines->push_back_n(2); |
michael@0 | 381 | pts[0] = devPts[0]; |
michael@0 | 382 | pts[1] = devPts[1]; |
michael@0 | 383 | } |
michael@0 | 384 | break; |
michael@0 | 385 | case SkPath::kQuad_Verb: { |
michael@0 | 386 | SkPoint choppedPts[5]; |
michael@0 | 387 | // Chopping the quad helps when the quad is either degenerate or nearly degenerate. |
michael@0 | 388 | // When it is degenerate it allows the approximation with lines to work since the |
michael@0 | 389 | // chop point (if there is one) will be at the parabola's vertex. In the nearly |
michael@0 | 390 | // degenerate the QuadUVMatrix computed for the points is almost singular which |
michael@0 | 391 | // can cause rendering artifacts. |
michael@0 | 392 | int n = SkChopQuadAtMaxCurvature(pathPts, choppedPts); |
michael@0 | 393 | for (int i = 0; i < n; ++i) { |
michael@0 | 394 | SkPoint* quadPts = choppedPts + i * 2; |
michael@0 | 395 | m.mapPoints(devPts, quadPts, 3); |
michael@0 | 396 | bounds.setBounds(devPts, 3); |
michael@0 | 397 | bounds.outset(SK_Scalar1, SK_Scalar1); |
michael@0 | 398 | bounds.roundOut(&ibounds); |
michael@0 | 399 | |
michael@0 | 400 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
michael@0 | 401 | int subdiv = num_quad_subdivs(devPts); |
michael@0 | 402 | SkASSERT(subdiv >= -1); |
michael@0 | 403 | if (-1 == subdiv) { |
michael@0 | 404 | SkPoint* pts = lines->push_back_n(4); |
michael@0 | 405 | pts[0] = devPts[0]; |
michael@0 | 406 | pts[1] = devPts[1]; |
michael@0 | 407 | pts[2] = devPts[1]; |
michael@0 | 408 | pts[3] = devPts[2]; |
michael@0 | 409 | } else { |
michael@0 | 410 | // when in perspective keep quads in src space |
michael@0 | 411 | SkPoint* qPts = persp ? quadPts : devPts; |
michael@0 | 412 | SkPoint* pts = quads->push_back_n(3); |
michael@0 | 413 | pts[0] = qPts[0]; |
michael@0 | 414 | pts[1] = qPts[1]; |
michael@0 | 415 | pts[2] = qPts[2]; |
michael@0 | 416 | quadSubdivCnts->push_back() = subdiv; |
michael@0 | 417 | totalQuadCount += 1 << subdiv; |
michael@0 | 418 | } |
michael@0 | 419 | } |
michael@0 | 420 | } |
michael@0 | 421 | break; |
michael@0 | 422 | } |
michael@0 | 423 | case SkPath::kCubic_Verb: |
michael@0 | 424 | m.mapPoints(devPts, pathPts, 4); |
michael@0 | 425 | bounds.setBounds(devPts, 4); |
michael@0 | 426 | bounds.outset(SK_Scalar1, SK_Scalar1); |
michael@0 | 427 | bounds.roundOut(&ibounds); |
michael@0 | 428 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
michael@0 | 429 | PREALLOC_PTARRAY(32) q; |
michael@0 | 430 | // we don't need a direction if we aren't constraining the subdivision |
michael@0 | 431 | static const SkPath::Direction kDummyDir = SkPath::kCCW_Direction; |
michael@0 | 432 | // We convert cubics to quadratics (for now). |
michael@0 | 433 | // In perspective have to do conversion in src space. |
michael@0 | 434 | if (persp) { |
michael@0 | 435 | SkScalar tolScale = |
michael@0 | 436 | GrPathUtils::scaleToleranceToSrc(SK_Scalar1, m, |
michael@0 | 437 | path.getBounds()); |
michael@0 | 438 | GrPathUtils::convertCubicToQuads(pathPts, tolScale, false, kDummyDir, &q); |
michael@0 | 439 | } else { |
michael@0 | 440 | GrPathUtils::convertCubicToQuads(devPts, SK_Scalar1, false, kDummyDir, &q); |
michael@0 | 441 | } |
michael@0 | 442 | for (int i = 0; i < q.count(); i += 3) { |
michael@0 | 443 | SkPoint* qInDevSpace; |
michael@0 | 444 | // bounds has to be calculated in device space, but q is |
michael@0 | 445 | // in src space when there is perspective. |
michael@0 | 446 | if (persp) { |
michael@0 | 447 | m.mapPoints(devPts, &q[i], 3); |
michael@0 | 448 | bounds.setBounds(devPts, 3); |
michael@0 | 449 | qInDevSpace = devPts; |
michael@0 | 450 | } else { |
michael@0 | 451 | bounds.setBounds(&q[i], 3); |
michael@0 | 452 | qInDevSpace = &q[i]; |
michael@0 | 453 | } |
michael@0 | 454 | bounds.outset(SK_Scalar1, SK_Scalar1); |
michael@0 | 455 | bounds.roundOut(&ibounds); |
michael@0 | 456 | if (SkIRect::Intersects(devClipBounds, ibounds)) { |
michael@0 | 457 | int subdiv = num_quad_subdivs(qInDevSpace); |
michael@0 | 458 | SkASSERT(subdiv >= -1); |
michael@0 | 459 | if (-1 == subdiv) { |
michael@0 | 460 | SkPoint* pts = lines->push_back_n(4); |
michael@0 | 461 | // lines should always be in device coords |
michael@0 | 462 | pts[0] = qInDevSpace[0]; |
michael@0 | 463 | pts[1] = qInDevSpace[1]; |
michael@0 | 464 | pts[2] = qInDevSpace[1]; |
michael@0 | 465 | pts[3] = qInDevSpace[2]; |
michael@0 | 466 | } else { |
michael@0 | 467 | SkPoint* pts = quads->push_back_n(3); |
michael@0 | 468 | // q is already in src space when there is no |
michael@0 | 469 | // perspective and dev coords otherwise. |
michael@0 | 470 | pts[0] = q[0 + i]; |
michael@0 | 471 | pts[1] = q[1 + i]; |
michael@0 | 472 | pts[2] = q[2 + i]; |
michael@0 | 473 | quadSubdivCnts->push_back() = subdiv; |
michael@0 | 474 | totalQuadCount += 1 << subdiv; |
michael@0 | 475 | } |
michael@0 | 476 | } |
michael@0 | 477 | } |
michael@0 | 478 | } |
michael@0 | 479 | break; |
michael@0 | 480 | case SkPath::kClose_Verb: |
michael@0 | 481 | break; |
michael@0 | 482 | case SkPath::kDone_Verb: |
michael@0 | 483 | return totalQuadCount; |
michael@0 | 484 | } |
michael@0 | 485 | } |
michael@0 | 486 | } |
michael@0 | 487 | |
michael@0 | 488 | struct LineVertex { |
michael@0 | 489 | GrPoint fPos; |
michael@0 | 490 | GrColor fCoverage; |
michael@0 | 491 | }; |
michael@0 | 492 | |
michael@0 | 493 | struct BezierVertex { |
michael@0 | 494 | GrPoint fPos; |
michael@0 | 495 | union { |
michael@0 | 496 | struct { |
michael@0 | 497 | SkScalar fK; |
michael@0 | 498 | SkScalar fL; |
michael@0 | 499 | SkScalar fM; |
michael@0 | 500 | } fConic; |
michael@0 | 501 | GrVec fQuadCoord; |
michael@0 | 502 | struct { |
michael@0 | 503 | SkScalar fBogus[4]; |
michael@0 | 504 | }; |
michael@0 | 505 | }; |
michael@0 | 506 | }; |
michael@0 | 507 | |
michael@0 | 508 | GR_STATIC_ASSERT(sizeof(BezierVertex) == 3 * sizeof(GrPoint)); |
michael@0 | 509 | |
michael@0 | 510 | void intersect_lines(const SkPoint& ptA, const SkVector& normA, |
michael@0 | 511 | const SkPoint& ptB, const SkVector& normB, |
michael@0 | 512 | SkPoint* result) { |
michael@0 | 513 | |
michael@0 | 514 | SkScalar lineAW = -normA.dot(ptA); |
michael@0 | 515 | SkScalar lineBW = -normB.dot(ptB); |
michael@0 | 516 | |
michael@0 | 517 | SkScalar wInv = SkScalarMul(normA.fX, normB.fY) - |
michael@0 | 518 | SkScalarMul(normA.fY, normB.fX); |
michael@0 | 519 | wInv = SkScalarInvert(wInv); |
michael@0 | 520 | |
michael@0 | 521 | result->fX = SkScalarMul(normA.fY, lineBW) - SkScalarMul(lineAW, normB.fY); |
michael@0 | 522 | result->fX = SkScalarMul(result->fX, wInv); |
michael@0 | 523 | |
michael@0 | 524 | result->fY = SkScalarMul(lineAW, normB.fX) - SkScalarMul(normA.fX, lineBW); |
michael@0 | 525 | result->fY = SkScalarMul(result->fY, wInv); |
michael@0 | 526 | } |
michael@0 | 527 | |
michael@0 | 528 | void set_uv_quad(const SkPoint qpts[3], BezierVertex verts[kVertsPerQuad]) { |
michael@0 | 529 | // this should be in the src space, not dev coords, when we have perspective |
michael@0 | 530 | GrPathUtils::QuadUVMatrix DevToUV(qpts); |
michael@0 | 531 | DevToUV.apply<kVertsPerQuad, sizeof(BezierVertex), sizeof(GrPoint)>(verts); |
michael@0 | 532 | } |
michael@0 | 533 | |
michael@0 | 534 | void bloat_quad(const SkPoint qpts[3], const SkMatrix* toDevice, |
michael@0 | 535 | const SkMatrix* toSrc, BezierVertex verts[kVertsPerQuad], |
michael@0 | 536 | SkRect* devBounds) { |
michael@0 | 537 | SkASSERT(!toDevice == !toSrc); |
michael@0 | 538 | // original quad is specified by tri a,b,c |
michael@0 | 539 | SkPoint a = qpts[0]; |
michael@0 | 540 | SkPoint b = qpts[1]; |
michael@0 | 541 | SkPoint c = qpts[2]; |
michael@0 | 542 | |
michael@0 | 543 | if (toDevice) { |
michael@0 | 544 | toDevice->mapPoints(&a, 1); |
michael@0 | 545 | toDevice->mapPoints(&b, 1); |
michael@0 | 546 | toDevice->mapPoints(&c, 1); |
michael@0 | 547 | } |
michael@0 | 548 | // make a new poly where we replace a and c by a 1-pixel wide edges orthog |
michael@0 | 549 | // to edges ab and bc: |
michael@0 | 550 | // |
michael@0 | 551 | // before | after |
michael@0 | 552 | // | b0 |
michael@0 | 553 | // b | |
michael@0 | 554 | // | |
michael@0 | 555 | // | a0 c0 |
michael@0 | 556 | // a c | a1 c1 |
michael@0 | 557 | // |
michael@0 | 558 | // edges a0->b0 and b0->c0 are parallel to original edges a->b and b->c, |
michael@0 | 559 | // respectively. |
michael@0 | 560 | BezierVertex& a0 = verts[0]; |
michael@0 | 561 | BezierVertex& a1 = verts[1]; |
michael@0 | 562 | BezierVertex& b0 = verts[2]; |
michael@0 | 563 | BezierVertex& c0 = verts[3]; |
michael@0 | 564 | BezierVertex& c1 = verts[4]; |
michael@0 | 565 | |
michael@0 | 566 | SkVector ab = b; |
michael@0 | 567 | ab -= a; |
michael@0 | 568 | SkVector ac = c; |
michael@0 | 569 | ac -= a; |
michael@0 | 570 | SkVector cb = b; |
michael@0 | 571 | cb -= c; |
michael@0 | 572 | |
michael@0 | 573 | // We should have already handled degenerates |
michael@0 | 574 | SkASSERT(ab.length() > 0 && cb.length() > 0); |
michael@0 | 575 | |
michael@0 | 576 | ab.normalize(); |
michael@0 | 577 | SkVector abN; |
michael@0 | 578 | abN.setOrthog(ab, SkVector::kLeft_Side); |
michael@0 | 579 | if (abN.dot(ac) > 0) { |
michael@0 | 580 | abN.negate(); |
michael@0 | 581 | } |
michael@0 | 582 | |
michael@0 | 583 | cb.normalize(); |
michael@0 | 584 | SkVector cbN; |
michael@0 | 585 | cbN.setOrthog(cb, SkVector::kLeft_Side); |
michael@0 | 586 | if (cbN.dot(ac) < 0) { |
michael@0 | 587 | cbN.negate(); |
michael@0 | 588 | } |
michael@0 | 589 | |
michael@0 | 590 | a0.fPos = a; |
michael@0 | 591 | a0.fPos += abN; |
michael@0 | 592 | a1.fPos = a; |
michael@0 | 593 | a1.fPos -= abN; |
michael@0 | 594 | |
michael@0 | 595 | c0.fPos = c; |
michael@0 | 596 | c0.fPos += cbN; |
michael@0 | 597 | c1.fPos = c; |
michael@0 | 598 | c1.fPos -= cbN; |
michael@0 | 599 | |
michael@0 | 600 | intersect_lines(a0.fPos, abN, c0.fPos, cbN, &b0.fPos); |
michael@0 | 601 | devBounds->growToInclude(&verts[0].fPos, sizeof(BezierVertex), kVertsPerQuad); |
michael@0 | 602 | |
michael@0 | 603 | if (toSrc) { |
michael@0 | 604 | toSrc->mapPointsWithStride(&verts[0].fPos, sizeof(BezierVertex), kVertsPerQuad); |
michael@0 | 605 | } |
michael@0 | 606 | } |
michael@0 | 607 | |
michael@0 | 608 | // Equations based off of Loop-Blinn Quadratic GPU Rendering |
michael@0 | 609 | // Input Parametric: |
michael@0 | 610 | // P(t) = (P0*(1-t)^2 + 2*w*P1*t*(1-t) + P2*t^2) / (1-t)^2 + 2*w*t*(1-t) + t^2) |
michael@0 | 611 | // Output Implicit: |
michael@0 | 612 | // f(x, y, w) = f(P) = K^2 - LM |
michael@0 | 613 | // K = dot(k, P), L = dot(l, P), M = dot(m, P) |
michael@0 | 614 | // k, l, m are calculated in function GrPathUtils::getConicKLM |
michael@0 | 615 | void set_conic_coeffs(const SkPoint p[3], BezierVertex verts[kVertsPerQuad], |
michael@0 | 616 | const SkScalar weight) { |
michael@0 | 617 | SkScalar klm[9]; |
michael@0 | 618 | |
michael@0 | 619 | GrPathUtils::getConicKLM(p, weight, klm); |
michael@0 | 620 | |
michael@0 | 621 | for (int i = 0; i < kVertsPerQuad; ++i) { |
michael@0 | 622 | const SkPoint pnt = verts[i].fPos; |
michael@0 | 623 | verts[i].fConic.fK = pnt.fX * klm[0] + pnt.fY * klm[1] + klm[2]; |
michael@0 | 624 | verts[i].fConic.fL = pnt.fX * klm[3] + pnt.fY * klm[4] + klm[5]; |
michael@0 | 625 | verts[i].fConic.fM = pnt.fX * klm[6] + pnt.fY * klm[7] + klm[8]; |
michael@0 | 626 | } |
michael@0 | 627 | } |
michael@0 | 628 | |
michael@0 | 629 | void add_conics(const SkPoint p[3], |
michael@0 | 630 | const SkScalar weight, |
michael@0 | 631 | const SkMatrix* toDevice, |
michael@0 | 632 | const SkMatrix* toSrc, |
michael@0 | 633 | BezierVertex** vert, |
michael@0 | 634 | SkRect* devBounds) { |
michael@0 | 635 | bloat_quad(p, toDevice, toSrc, *vert, devBounds); |
michael@0 | 636 | set_conic_coeffs(p, *vert, weight); |
michael@0 | 637 | *vert += kVertsPerQuad; |
michael@0 | 638 | } |
michael@0 | 639 | |
michael@0 | 640 | void add_quads(const SkPoint p[3], |
michael@0 | 641 | int subdiv, |
michael@0 | 642 | const SkMatrix* toDevice, |
michael@0 | 643 | const SkMatrix* toSrc, |
michael@0 | 644 | BezierVertex** vert, |
michael@0 | 645 | SkRect* devBounds) { |
michael@0 | 646 | SkASSERT(subdiv >= 0); |
michael@0 | 647 | if (subdiv) { |
michael@0 | 648 | SkPoint newP[5]; |
michael@0 | 649 | SkChopQuadAtHalf(p, newP); |
michael@0 | 650 | add_quads(newP + 0, subdiv-1, toDevice, toSrc, vert, devBounds); |
michael@0 | 651 | add_quads(newP + 2, subdiv-1, toDevice, toSrc, vert, devBounds); |
michael@0 | 652 | } else { |
michael@0 | 653 | bloat_quad(p, toDevice, toSrc, *vert, devBounds); |
michael@0 | 654 | set_uv_quad(p, *vert); |
michael@0 | 655 | *vert += kVertsPerQuad; |
michael@0 | 656 | } |
michael@0 | 657 | } |
michael@0 | 658 | |
michael@0 | 659 | void add_line(const SkPoint p[2], |
michael@0 | 660 | const SkMatrix* toSrc, |
michael@0 | 661 | GrColor coverage, |
michael@0 | 662 | LineVertex** vert) { |
michael@0 | 663 | const SkPoint& a = p[0]; |
michael@0 | 664 | const SkPoint& b = p[1]; |
michael@0 | 665 | |
michael@0 | 666 | SkVector ortho, vec = b; |
michael@0 | 667 | vec -= a; |
michael@0 | 668 | |
michael@0 | 669 | if (vec.setLength(SK_ScalarHalf)) { |
michael@0 | 670 | // Create a vector orthogonal to 'vec' and of unit length |
michael@0 | 671 | ortho.fX = 2.0f * vec.fY; |
michael@0 | 672 | ortho.fY = -2.0f * vec.fX; |
michael@0 | 673 | |
michael@0 | 674 | (*vert)[0].fPos = a; |
michael@0 | 675 | (*vert)[0].fCoverage = coverage; |
michael@0 | 676 | (*vert)[1].fPos = b; |
michael@0 | 677 | (*vert)[1].fCoverage = coverage; |
michael@0 | 678 | (*vert)[2].fPos = a - vec + ortho; |
michael@0 | 679 | (*vert)[2].fCoverage = 0; |
michael@0 | 680 | (*vert)[3].fPos = b + vec + ortho; |
michael@0 | 681 | (*vert)[3].fCoverage = 0; |
michael@0 | 682 | (*vert)[4].fPos = a - vec - ortho; |
michael@0 | 683 | (*vert)[4].fCoverage = 0; |
michael@0 | 684 | (*vert)[5].fPos = b + vec - ortho; |
michael@0 | 685 | (*vert)[5].fCoverage = 0; |
michael@0 | 686 | |
michael@0 | 687 | if (NULL != toSrc) { |
michael@0 | 688 | toSrc->mapPointsWithStride(&(*vert)->fPos, |
michael@0 | 689 | sizeof(LineVertex), |
michael@0 | 690 | kVertsPerLineSeg); |
michael@0 | 691 | } |
michael@0 | 692 | } else { |
michael@0 | 693 | // just make it degenerate and likely offscreen |
michael@0 | 694 | for (int i = 0; i < kVertsPerLineSeg; ++i) { |
michael@0 | 695 | (*vert)[i].fPos.set(SK_ScalarMax, SK_ScalarMax); |
michael@0 | 696 | } |
michael@0 | 697 | } |
michael@0 | 698 | |
michael@0 | 699 | *vert += kVertsPerLineSeg; |
michael@0 | 700 | } |
michael@0 | 701 | |
michael@0 | 702 | } |
michael@0 | 703 | |
michael@0 | 704 | /////////////////////////////////////////////////////////////////////////////// |
michael@0 | 705 | |
michael@0 | 706 | namespace { |
michael@0 | 707 | |
michael@0 | 708 | // position + edge |
michael@0 | 709 | extern const GrVertexAttrib gHairlineBezierAttribs[] = { |
michael@0 | 710 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
michael@0 | 711 | {kVec4f_GrVertexAttribType, sizeof(GrPoint), kEffect_GrVertexAttribBinding} |
michael@0 | 712 | }; |
michael@0 | 713 | |
michael@0 | 714 | // position + coverage |
michael@0 | 715 | extern const GrVertexAttrib gHairlineLineAttribs[] = { |
michael@0 | 716 | {kVec2f_GrVertexAttribType, 0, kPosition_GrVertexAttribBinding}, |
michael@0 | 717 | {kVec4ub_GrVertexAttribType, sizeof(GrPoint), kCoverage_GrVertexAttribBinding}, |
michael@0 | 718 | }; |
michael@0 | 719 | |
michael@0 | 720 | }; |
michael@0 | 721 | |
michael@0 | 722 | bool GrAAHairLinePathRenderer::createLineGeom(const SkPath& path, |
michael@0 | 723 | GrDrawTarget* target, |
michael@0 | 724 | const PtArray& lines, |
michael@0 | 725 | int lineCnt, |
michael@0 | 726 | GrDrawTarget::AutoReleaseGeometry* arg, |
michael@0 | 727 | SkRect* devBounds) { |
michael@0 | 728 | GrDrawState* drawState = target->drawState(); |
michael@0 | 729 | |
michael@0 | 730 | const SkMatrix& viewM = drawState->getViewMatrix(); |
michael@0 | 731 | |
michael@0 | 732 | int vertCnt = kVertsPerLineSeg * lineCnt; |
michael@0 | 733 | |
michael@0 | 734 | drawState->setVertexAttribs<gHairlineLineAttribs>(SK_ARRAY_COUNT(gHairlineLineAttribs)); |
michael@0 | 735 | SkASSERT(sizeof(LineVertex) == drawState->getVertexSize()); |
michael@0 | 736 | |
michael@0 | 737 | if (!arg->set(target, vertCnt, 0)) { |
michael@0 | 738 | return false; |
michael@0 | 739 | } |
michael@0 | 740 | |
michael@0 | 741 | LineVertex* verts = reinterpret_cast<LineVertex*>(arg->vertices()); |
michael@0 | 742 | |
michael@0 | 743 | const SkMatrix* toSrc = NULL; |
michael@0 | 744 | SkMatrix ivm; |
michael@0 | 745 | |
michael@0 | 746 | if (viewM.hasPerspective()) { |
michael@0 | 747 | if (viewM.invert(&ivm)) { |
michael@0 | 748 | toSrc = &ivm; |
michael@0 | 749 | } |
michael@0 | 750 | } |
michael@0 | 751 | devBounds->set(lines.begin(), lines.count()); |
michael@0 | 752 | for (int i = 0; i < lineCnt; ++i) { |
michael@0 | 753 | add_line(&lines[2*i], toSrc, drawState->getCoverageColor(), &verts); |
michael@0 | 754 | } |
michael@0 | 755 | // All the verts computed by add_line are within sqrt(1^2 + 0.5^2) of the end points. |
michael@0 | 756 | static const SkScalar kSqrtOfOneAndAQuarter = 1.118f; |
michael@0 | 757 | // Add a little extra to account for vector normalization precision. |
michael@0 | 758 | static const SkScalar kOutset = kSqrtOfOneAndAQuarter + SK_Scalar1 / 20; |
michael@0 | 759 | devBounds->outset(kOutset, kOutset); |
michael@0 | 760 | |
michael@0 | 761 | return true; |
michael@0 | 762 | } |
michael@0 | 763 | |
michael@0 | 764 | bool GrAAHairLinePathRenderer::createBezierGeom( |
michael@0 | 765 | const SkPath& path, |
michael@0 | 766 | GrDrawTarget* target, |
michael@0 | 767 | const PtArray& quads, |
michael@0 | 768 | int quadCnt, |
michael@0 | 769 | const PtArray& conics, |
michael@0 | 770 | int conicCnt, |
michael@0 | 771 | const IntArray& qSubdivs, |
michael@0 | 772 | const FloatArray& cWeights, |
michael@0 | 773 | GrDrawTarget::AutoReleaseGeometry* arg, |
michael@0 | 774 | SkRect* devBounds) { |
michael@0 | 775 | GrDrawState* drawState = target->drawState(); |
michael@0 | 776 | |
michael@0 | 777 | const SkMatrix& viewM = drawState->getViewMatrix(); |
michael@0 | 778 | |
michael@0 | 779 | int vertCnt = kVertsPerQuad * quadCnt + kVertsPerQuad * conicCnt; |
michael@0 | 780 | |
michael@0 | 781 | target->drawState()->setVertexAttribs<gHairlineBezierAttribs>(SK_ARRAY_COUNT(gHairlineBezierAttribs)); |
michael@0 | 782 | SkASSERT(sizeof(BezierVertex) == target->getDrawState().getVertexSize()); |
michael@0 | 783 | |
michael@0 | 784 | if (!arg->set(target, vertCnt, 0)) { |
michael@0 | 785 | return false; |
michael@0 | 786 | } |
michael@0 | 787 | |
michael@0 | 788 | BezierVertex* verts = reinterpret_cast<BezierVertex*>(arg->vertices()); |
michael@0 | 789 | |
michael@0 | 790 | const SkMatrix* toDevice = NULL; |
michael@0 | 791 | const SkMatrix* toSrc = NULL; |
michael@0 | 792 | SkMatrix ivm; |
michael@0 | 793 | |
michael@0 | 794 | if (viewM.hasPerspective()) { |
michael@0 | 795 | if (viewM.invert(&ivm)) { |
michael@0 | 796 | toDevice = &viewM; |
michael@0 | 797 | toSrc = &ivm; |
michael@0 | 798 | } |
michael@0 | 799 | } |
michael@0 | 800 | |
michael@0 | 801 | // Seed the dev bounds with some pts known to be inside. Each quad and conic grows the bounding |
michael@0 | 802 | // box to include its vertices. |
michael@0 | 803 | SkPoint seedPts[2]; |
michael@0 | 804 | if (quadCnt) { |
michael@0 | 805 | seedPts[0] = quads[0]; |
michael@0 | 806 | seedPts[1] = quads[2]; |
michael@0 | 807 | } else if (conicCnt) { |
michael@0 | 808 | seedPts[0] = conics[0]; |
michael@0 | 809 | seedPts[1] = conics[2]; |
michael@0 | 810 | } |
michael@0 | 811 | if (NULL != toDevice) { |
michael@0 | 812 | toDevice->mapPoints(seedPts, 2); |
michael@0 | 813 | } |
michael@0 | 814 | devBounds->set(seedPts[0], seedPts[1]); |
michael@0 | 815 | |
michael@0 | 816 | int unsubdivQuadCnt = quads.count() / 3; |
michael@0 | 817 | for (int i = 0; i < unsubdivQuadCnt; ++i) { |
michael@0 | 818 | SkASSERT(qSubdivs[i] >= 0); |
michael@0 | 819 | add_quads(&quads[3*i], qSubdivs[i], toDevice, toSrc, &verts, devBounds); |
michael@0 | 820 | } |
michael@0 | 821 | |
michael@0 | 822 | // Start Conics |
michael@0 | 823 | for (int i = 0; i < conicCnt; ++i) { |
michael@0 | 824 | add_conics(&conics[3*i], cWeights[i], toDevice, toSrc, &verts, devBounds); |
michael@0 | 825 | } |
michael@0 | 826 | return true; |
michael@0 | 827 | } |
michael@0 | 828 | |
michael@0 | 829 | bool GrAAHairLinePathRenderer::canDrawPath(const SkPath& path, |
michael@0 | 830 | const SkStrokeRec& stroke, |
michael@0 | 831 | const GrDrawTarget* target, |
michael@0 | 832 | bool antiAlias) const { |
michael@0 | 833 | if (!antiAlias) { |
michael@0 | 834 | return false; |
michael@0 | 835 | } |
michael@0 | 836 | |
michael@0 | 837 | if (!IsStrokeHairlineOrEquivalent(stroke, |
michael@0 | 838 | target->getDrawState().getViewMatrix(), |
michael@0 | 839 | NULL)) { |
michael@0 | 840 | return false; |
michael@0 | 841 | } |
michael@0 | 842 | |
michael@0 | 843 | if (SkPath::kLine_SegmentMask == path.getSegmentMasks() || |
michael@0 | 844 | target->caps()->shaderDerivativeSupport()) { |
michael@0 | 845 | return true; |
michael@0 | 846 | } |
michael@0 | 847 | return false; |
michael@0 | 848 | } |
michael@0 | 849 | |
michael@0 | 850 | template <class VertexType> |
michael@0 | 851 | bool check_bounds(GrDrawState* drawState, const SkRect& devBounds, void* vertices, int vCount) |
michael@0 | 852 | { |
michael@0 | 853 | SkRect tolDevBounds = devBounds; |
michael@0 | 854 | // The bounds ought to be tight, but in perspective the below code runs the verts |
michael@0 | 855 | // through the view matrix to get back to dev coords, which can introduce imprecision. |
michael@0 | 856 | if (drawState->getViewMatrix().hasPerspective()) { |
michael@0 | 857 | tolDevBounds.outset(SK_Scalar1 / 1000, SK_Scalar1 / 1000); |
michael@0 | 858 | } else { |
michael@0 | 859 | // Non-persp matrices cause this path renderer to draw in device space. |
michael@0 | 860 | SkASSERT(drawState->getViewMatrix().isIdentity()); |
michael@0 | 861 | } |
michael@0 | 862 | SkRect actualBounds; |
michael@0 | 863 | |
michael@0 | 864 | VertexType* verts = reinterpret_cast<VertexType*>(vertices); |
michael@0 | 865 | bool first = true; |
michael@0 | 866 | for (int i = 0; i < vCount; ++i) { |
michael@0 | 867 | SkPoint pos = verts[i].fPos; |
michael@0 | 868 | // This is a hack to workaround the fact that we move some degenerate segments offscreen. |
michael@0 | 869 | if (SK_ScalarMax == pos.fX) { |
michael@0 | 870 | continue; |
michael@0 | 871 | } |
michael@0 | 872 | drawState->getViewMatrix().mapPoints(&pos, 1); |
michael@0 | 873 | if (first) { |
michael@0 | 874 | actualBounds.set(pos.fX, pos.fY, pos.fX, pos.fY); |
michael@0 | 875 | first = false; |
michael@0 | 876 | } else { |
michael@0 | 877 | actualBounds.growToInclude(pos.fX, pos.fY); |
michael@0 | 878 | } |
michael@0 | 879 | } |
michael@0 | 880 | if (!first) { |
michael@0 | 881 | return tolDevBounds.contains(actualBounds); |
michael@0 | 882 | } |
michael@0 | 883 | |
michael@0 | 884 | return true; |
michael@0 | 885 | } |
michael@0 | 886 | |
michael@0 | 887 | bool GrAAHairLinePathRenderer::onDrawPath(const SkPath& path, |
michael@0 | 888 | const SkStrokeRec& stroke, |
michael@0 | 889 | GrDrawTarget* target, |
michael@0 | 890 | bool antiAlias) { |
michael@0 | 891 | GrDrawState* drawState = target->drawState(); |
michael@0 | 892 | |
michael@0 | 893 | SkScalar hairlineCoverage; |
michael@0 | 894 | if (IsStrokeHairlineOrEquivalent(stroke, |
michael@0 | 895 | target->getDrawState().getViewMatrix(), |
michael@0 | 896 | &hairlineCoverage)) { |
michael@0 | 897 | uint8_t newCoverage = SkScalarRoundToInt(hairlineCoverage * |
michael@0 | 898 | target->getDrawState().getCoverage()); |
michael@0 | 899 | target->drawState()->setCoverage(newCoverage); |
michael@0 | 900 | } |
michael@0 | 901 | |
michael@0 | 902 | SkIRect devClipBounds; |
michael@0 | 903 | target->getClip()->getConservativeBounds(drawState->getRenderTarget(), &devClipBounds); |
michael@0 | 904 | |
michael@0 | 905 | int lineCnt; |
michael@0 | 906 | int quadCnt; |
michael@0 | 907 | int conicCnt; |
michael@0 | 908 | PREALLOC_PTARRAY(128) lines; |
michael@0 | 909 | PREALLOC_PTARRAY(128) quads; |
michael@0 | 910 | PREALLOC_PTARRAY(128) conics; |
michael@0 | 911 | IntArray qSubdivs; |
michael@0 | 912 | FloatArray cWeights; |
michael@0 | 913 | quadCnt = generate_lines_and_quads(path, drawState->getViewMatrix(), devClipBounds, |
michael@0 | 914 | &lines, &quads, &conics, &qSubdivs, &cWeights); |
michael@0 | 915 | lineCnt = lines.count() / 2; |
michael@0 | 916 | conicCnt = conics.count() / 3; |
michael@0 | 917 | |
michael@0 | 918 | // do lines first |
michael@0 | 919 | if (lineCnt) { |
michael@0 | 920 | GrDrawTarget::AutoReleaseGeometry arg; |
michael@0 | 921 | SkRect devBounds; |
michael@0 | 922 | |
michael@0 | 923 | if (!this->createLineGeom(path, |
michael@0 | 924 | target, |
michael@0 | 925 | lines, |
michael@0 | 926 | lineCnt, |
michael@0 | 927 | &arg, |
michael@0 | 928 | &devBounds)) { |
michael@0 | 929 | return false; |
michael@0 | 930 | } |
michael@0 | 931 | |
michael@0 | 932 | GrDrawTarget::AutoStateRestore asr; |
michael@0 | 933 | |
michael@0 | 934 | // createLineGeom transforms the geometry to device space when the matrix does not have |
michael@0 | 935 | // perspective. |
michael@0 | 936 | if (target->getDrawState().getViewMatrix().hasPerspective()) { |
michael@0 | 937 | asr.set(target, GrDrawTarget::kPreserve_ASRInit); |
michael@0 | 938 | } else if (!asr.setIdentity(target, GrDrawTarget::kPreserve_ASRInit)) { |
michael@0 | 939 | return false; |
michael@0 | 940 | } |
michael@0 | 941 | GrDrawState* drawState = target->drawState(); |
michael@0 | 942 | |
michael@0 | 943 | // Check devBounds |
michael@0 | 944 | SkASSERT(check_bounds<LineVertex>(drawState, devBounds, arg.vertices(), |
michael@0 | 945 | kVertsPerLineSeg * lineCnt)); |
michael@0 | 946 | |
michael@0 | 947 | { |
michael@0 | 948 | GrDrawState::AutoRestoreEffects are(drawState); |
michael@0 | 949 | target->setIndexSourceToBuffer(fLinesIndexBuffer); |
michael@0 | 950 | int lines = 0; |
michael@0 | 951 | while (lines < lineCnt) { |
michael@0 | 952 | int n = GrMin(lineCnt - lines, kNumLineSegsInIdxBuffer); |
michael@0 | 953 | target->drawIndexed(kTriangles_GrPrimitiveType, |
michael@0 | 954 | kVertsPerLineSeg*lines, // startV |
michael@0 | 955 | 0, // startI |
michael@0 | 956 | kVertsPerLineSeg*n, // vCount |
michael@0 | 957 | kIdxsPerLineSeg*n, // iCount |
michael@0 | 958 | &devBounds); |
michael@0 | 959 | lines += n; |
michael@0 | 960 | } |
michael@0 | 961 | } |
michael@0 | 962 | } |
michael@0 | 963 | |
michael@0 | 964 | // then quadratics/conics |
michael@0 | 965 | if (quadCnt || conicCnt) { |
michael@0 | 966 | GrDrawTarget::AutoReleaseGeometry arg; |
michael@0 | 967 | SkRect devBounds; |
michael@0 | 968 | |
michael@0 | 969 | if (!this->createBezierGeom(path, |
michael@0 | 970 | target, |
michael@0 | 971 | quads, |
michael@0 | 972 | quadCnt, |
michael@0 | 973 | conics, |
michael@0 | 974 | conicCnt, |
michael@0 | 975 | qSubdivs, |
michael@0 | 976 | cWeights, |
michael@0 | 977 | &arg, |
michael@0 | 978 | &devBounds)) { |
michael@0 | 979 | return false; |
michael@0 | 980 | } |
michael@0 | 981 | |
michael@0 | 982 | GrDrawTarget::AutoStateRestore asr; |
michael@0 | 983 | |
michael@0 | 984 | // createGeom transforms the geometry to device space when the matrix does not have |
michael@0 | 985 | // perspective. |
michael@0 | 986 | if (target->getDrawState().getViewMatrix().hasPerspective()) { |
michael@0 | 987 | asr.set(target, GrDrawTarget::kPreserve_ASRInit); |
michael@0 | 988 | } else if (!asr.setIdentity(target, GrDrawTarget::kPreserve_ASRInit)) { |
michael@0 | 989 | return false; |
michael@0 | 990 | } |
michael@0 | 991 | GrDrawState* drawState = target->drawState(); |
michael@0 | 992 | |
michael@0 | 993 | static const int kEdgeAttrIndex = 1; |
michael@0 | 994 | |
michael@0 | 995 | // Check devBounds |
michael@0 | 996 | SkASSERT(check_bounds<BezierVertex>(drawState, devBounds, arg.vertices(), |
michael@0 | 997 | kVertsPerQuad * quadCnt + kVertsPerQuad * conicCnt)); |
michael@0 | 998 | |
michael@0 | 999 | if (quadCnt > 0) { |
michael@0 | 1000 | GrEffectRef* hairQuadEffect = GrQuadEffect::Create(kHairlineAA_GrEffectEdgeType, |
michael@0 | 1001 | *target->caps()); |
michael@0 | 1002 | SkASSERT(NULL != hairQuadEffect); |
michael@0 | 1003 | GrDrawState::AutoRestoreEffects are(drawState); |
michael@0 | 1004 | target->setIndexSourceToBuffer(fQuadsIndexBuffer); |
michael@0 | 1005 | drawState->addCoverageEffect(hairQuadEffect, kEdgeAttrIndex)->unref(); |
michael@0 | 1006 | int quads = 0; |
michael@0 | 1007 | while (quads < quadCnt) { |
michael@0 | 1008 | int n = GrMin(quadCnt - quads, kNumQuadsInIdxBuffer); |
michael@0 | 1009 | target->drawIndexed(kTriangles_GrPrimitiveType, |
michael@0 | 1010 | kVertsPerQuad*quads, // startV |
michael@0 | 1011 | 0, // startI |
michael@0 | 1012 | kVertsPerQuad*n, // vCount |
michael@0 | 1013 | kIdxsPerQuad*n, // iCount |
michael@0 | 1014 | &devBounds); |
michael@0 | 1015 | quads += n; |
michael@0 | 1016 | } |
michael@0 | 1017 | } |
michael@0 | 1018 | |
michael@0 | 1019 | if (conicCnt > 0) { |
michael@0 | 1020 | GrDrawState::AutoRestoreEffects are(drawState); |
michael@0 | 1021 | GrEffectRef* hairConicEffect = GrConicEffect::Create(kHairlineAA_GrEffectEdgeType, |
michael@0 | 1022 | *target->caps()); |
michael@0 | 1023 | SkASSERT(NULL != hairConicEffect); |
michael@0 | 1024 | drawState->addCoverageEffect(hairConicEffect, 1, 2)->unref(); |
michael@0 | 1025 | int conics = 0; |
michael@0 | 1026 | while (conics < conicCnt) { |
michael@0 | 1027 | int n = GrMin(conicCnt - conics, kNumQuadsInIdxBuffer); |
michael@0 | 1028 | target->drawIndexed(kTriangles_GrPrimitiveType, |
michael@0 | 1029 | kVertsPerQuad*(quadCnt + conics), // startV |
michael@0 | 1030 | 0, // startI |
michael@0 | 1031 | kVertsPerQuad*n, // vCount |
michael@0 | 1032 | kIdxsPerQuad*n, // iCount |
michael@0 | 1033 | &devBounds); |
michael@0 | 1034 | conics += n; |
michael@0 | 1035 | } |
michael@0 | 1036 | } |
michael@0 | 1037 | } |
michael@0 | 1038 | |
michael@0 | 1039 | target->resetIndexSource(); |
michael@0 | 1040 | |
michael@0 | 1041 | return true; |
michael@0 | 1042 | } |