epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 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. |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 9 | |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 10 | #include "GrTesselatedPathRenderer.h" |
| 11 | |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 12 | #include "GrPathUtils.h" |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 13 | #include "GrPoint.h" |
| 14 | #include "GrTDArray.h" |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 15 | |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 16 | #include "SkTemplates.h" |
| 17 | |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 18 | #include <limits.h> |
senorblanco@chromium.org | 1fa803d | 2011-05-25 14:46:17 +0000 | [diff] [blame] | 19 | #include <sk_glu.h> |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 20 | |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 21 | typedef GrTDArray<GrDrawTarget::Edge> GrEdgeArray; |
| 22 | typedef GrTDArray<GrPoint> GrPointArray; |
| 23 | typedef GrTDArray<uint16_t> GrIndexArray; |
| 24 | typedef void (*TESSCB)(); |
| 25 | |
| 26 | // limit the allowable vertex range to approximately half of the representable |
| 27 | // IEEE exponent in order to avoid overflow when doing multiplies between |
| 28 | // vertex components, |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 29 | const float kMaxVertexValue = 1e18f; |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 30 | |
| 31 | static inline GrDrawTarget::Edge computeEdge(const GrPoint& p, |
| 32 | const GrPoint& q, |
| 33 | float sign) { |
| 34 | GrVec tangent = GrVec::Make(p.fY - q.fY, q.fX - p.fX); |
| 35 | float scale = sign / tangent.length(); |
| 36 | float cross2 = p.fX * q.fY - q.fX * p.fY; |
| 37 | return GrDrawTarget::Edge(tangent.fX * scale, |
| 38 | tangent.fY * scale, |
| 39 | cross2 * scale); |
| 40 | } |
| 41 | |
| 42 | static inline GrPoint sanitizePoint(const GrPoint& pt) { |
| 43 | GrPoint r; |
| 44 | r.fX = SkScalarPin(pt.fX, -kMaxVertexValue, kMaxVertexValue); |
| 45 | r.fY = SkScalarPin(pt.fY, -kMaxVertexValue, kMaxVertexValue); |
| 46 | return r; |
| 47 | } |
| 48 | |
| 49 | class GrTess { |
| 50 | public: |
| 51 | GrTess(int count, unsigned winding_rule) { |
| 52 | fTess = Sk_gluNewTess(); |
| 53 | Sk_gluTessProperty(fTess, GLU_TESS_WINDING_RULE, winding_rule); |
| 54 | Sk_gluTessNormal(fTess, 0.0f, 0.0f, 1.0f); |
| 55 | Sk_gluTessCallback(fTess, GLU_TESS_BEGIN_DATA, (TESSCB) &beginCB); |
| 56 | Sk_gluTessCallback(fTess, GLU_TESS_VERTEX_DATA, (TESSCB) &vertexCB); |
| 57 | Sk_gluTessCallback(fTess, GLU_TESS_END_DATA, (TESSCB) &endCB); |
| 58 | Sk_gluTessCallback(fTess, GLU_TESS_EDGE_FLAG_DATA, (TESSCB) &edgeFlagCB); |
| 59 | Sk_gluTessCallback(fTess, GLU_TESS_COMBINE_DATA, (TESSCB) &combineCB); |
| 60 | fInVertices = new double[count * 3]; |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 61 | } |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 62 | ~GrTess() { |
| 63 | Sk_gluDeleteTess(fTess); |
| 64 | delete[] fInVertices; |
| 65 | } |
| 66 | void addVertex(const GrPoint& pt, int index) { |
| 67 | if (index > USHRT_MAX) return; |
| 68 | double* inVertex = &fInVertices[index * 3]; |
| 69 | inVertex[0] = pt.fX; |
| 70 | inVertex[1] = pt.fY; |
| 71 | inVertex[2] = 0.0; |
| 72 | *fVertices.append() = pt; |
| 73 | Sk_gluTessVertex(fTess, inVertex, reinterpret_cast<void*>(index)); |
| 74 | } |
| 75 | void addVertices(const GrPoint* points, const uint16_t* contours, int numContours) { |
| 76 | Sk_gluTessBeginPolygon(fTess, this); |
| 77 | size_t i = 0; |
| 78 | for (int j = 0; j < numContours; ++j) { |
| 79 | Sk_gluTessBeginContour(fTess); |
| 80 | size_t end = i + contours[j]; |
| 81 | for (; i < end; ++i) { |
| 82 | addVertex(points[i], i); |
| 83 | } |
| 84 | Sk_gluTessEndContour(fTess); |
| 85 | } |
| 86 | Sk_gluTessEndPolygon(fTess); |
| 87 | } |
| 88 | GLUtesselator* tess() { return fTess; } |
| 89 | const GrPointArray& vertices() const { return fVertices; } |
| 90 | protected: |
| 91 | virtual void begin(GLenum type) = 0; |
| 92 | virtual void vertex(int index) = 0; |
| 93 | virtual void edgeFlag(bool flag) = 0; |
| 94 | virtual void end() = 0; |
| 95 | virtual int combine(GLdouble coords[3], int vertexIndices[4], |
| 96 | GLfloat weight[4]) = 0; |
| 97 | static void beginCB(GLenum type, void* data) { |
| 98 | static_cast<GrTess*>(data)->begin(type); |
| 99 | } |
| 100 | static void vertexCB(void* vertexData, void* data) { |
| 101 | static_cast<GrTess*>(data)->vertex(reinterpret_cast<long>(vertexData)); |
| 102 | } |
| 103 | static void edgeFlagCB(GLboolean flag, void* data) { |
| 104 | static_cast<GrTess*>(data)->edgeFlag(flag != 0); |
| 105 | } |
| 106 | static void endCB(void* data) { |
| 107 | static_cast<GrTess*>(data)->end(); |
| 108 | } |
| 109 | static void combineCB(GLdouble coords[3], void* vertexData[4], |
| 110 | GLfloat weight[4], void **outData, void* data) { |
| 111 | int vertexIndex[4]; |
| 112 | vertexIndex[0] = reinterpret_cast<long>(vertexData[0]); |
| 113 | vertexIndex[1] = reinterpret_cast<long>(vertexData[1]); |
| 114 | vertexIndex[2] = reinterpret_cast<long>(vertexData[2]); |
| 115 | vertexIndex[3] = reinterpret_cast<long>(vertexData[3]); |
| 116 | GrTess* tess = static_cast<GrTess*>(data); |
| 117 | int outIndex = tess->combine(coords, vertexIndex, weight); |
| 118 | *reinterpret_cast<long*>(outData) = outIndex; |
| 119 | } |
| 120 | protected: |
| 121 | GLUtesselator* fTess; |
| 122 | GrPointArray fVertices; |
| 123 | double* fInVertices; |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 124 | }; |
| 125 | |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 126 | class GrPolygonTess : public GrTess { |
| 127 | public: |
| 128 | GrPolygonTess(int count, unsigned winding_rule) |
| 129 | : GrTess(count, winding_rule) { |
| 130 | } |
| 131 | ~GrPolygonTess() { |
| 132 | } |
| 133 | const GrIndexArray& indices() const { return fIndices; } |
| 134 | protected: |
| 135 | virtual void begin(GLenum type) { |
| 136 | GR_DEBUGASSERT(type == GL_TRIANGLES); |
| 137 | } |
| 138 | virtual void vertex(int index) { |
| 139 | *fIndices.append() = index; |
| 140 | } |
| 141 | virtual void edgeFlag(bool flag) {} |
| 142 | virtual void end() {} |
| 143 | virtual int combine(GLdouble coords[3], int vertexIndices[4], |
| 144 | GLfloat weight[4]) { |
| 145 | int index = fVertices.count(); |
| 146 | GrPoint p = GrPoint::Make(static_cast<float>(coords[0]), |
| 147 | static_cast<float>(coords[1])); |
| 148 | *fVertices.append() = p; |
| 149 | return index; |
| 150 | } |
| 151 | protected: |
| 152 | GrIndexArray fIndices; |
| 153 | }; |
| 154 | |
| 155 | class GrEdgePolygonTess : public GrPolygonTess { |
| 156 | public: |
| 157 | GrEdgePolygonTess(int count, unsigned winding_rule, const SkMatrix& matrix) |
| 158 | : GrPolygonTess(count, winding_rule), |
| 159 | fMatrix(matrix), |
| 160 | fEdgeFlag(false), |
| 161 | fEdgeVertex(-1), |
| 162 | fTriStartVertex(-1), |
| 163 | fEdges(NULL) { |
| 164 | } |
| 165 | ~GrEdgePolygonTess() { |
| 166 | delete[] fEdges; |
| 167 | } |
| 168 | const GrDrawTarget::Edge* edges() const { return fEdges; } |
| 169 | private: |
| 170 | void addEdge(int index0, int index1) { |
| 171 | GrPoint p = fVertices[index0]; |
| 172 | GrPoint q = fVertices[index1]; |
| 173 | fMatrix.mapPoints(&p, 1); |
| 174 | fMatrix.mapPoints(&q, 1); |
| 175 | p = sanitizePoint(p); |
| 176 | q = sanitizePoint(q); |
| 177 | if (p == q) return; |
| 178 | GrDrawTarget::Edge edge = computeEdge(p, q, 1.0f); |
| 179 | fEdges[index0 * 2 + 1] = edge; |
| 180 | fEdges[index1 * 2] = edge; |
| 181 | } |
| 182 | virtual void begin(GLenum type) { |
| 183 | GR_DEBUGASSERT(type == GL_TRIANGLES); |
| 184 | int count = fVertices.count() * 2; |
| 185 | fEdges = new GrDrawTarget::Edge[count]; |
| 186 | memset(fEdges, 0, count * sizeof(GrDrawTarget::Edge)); |
| 187 | } |
| 188 | virtual void edgeFlag(bool flag) { |
| 189 | fEdgeFlag = flag; |
| 190 | } |
| 191 | virtual void vertex(int index) { |
| 192 | bool triStart = fIndices.count() % 3 == 0; |
| 193 | GrPolygonTess::vertex(index); |
| 194 | if (fEdgeVertex != -1) { |
| 195 | if (triStart) { |
| 196 | addEdge(fEdgeVertex, fTriStartVertex); |
| 197 | } else { |
| 198 | addEdge(fEdgeVertex, index); |
| 199 | } |
| 200 | } |
| 201 | if (triStart) { |
| 202 | fTriStartVertex = index; |
| 203 | } |
| 204 | if (fEdgeFlag) { |
| 205 | fEdgeVertex = index; |
| 206 | } else { |
| 207 | fEdgeVertex = -1; |
| 208 | } |
| 209 | } |
| 210 | virtual void end() { |
| 211 | if (fEdgeVertex != -1) { |
| 212 | addEdge(fEdgeVertex, fTriStartVertex); |
| 213 | } |
| 214 | } |
| 215 | GrMatrix fMatrix; |
| 216 | bool fEdgeFlag; |
| 217 | int fEdgeVertex, fTriStartVertex; |
| 218 | GrDrawTarget::Edge* fEdges; |
| 219 | }; |
| 220 | |
| 221 | class GrBoundaryTess : public GrTess { |
| 222 | public: |
| 223 | GrBoundaryTess(int count, unsigned winding_rule) |
| 224 | : GrTess(count, winding_rule), |
| 225 | fContourStart(0) { |
| 226 | Sk_gluTessProperty(fTess, GLU_TESS_BOUNDARY_ONLY, 1); |
| 227 | } |
| 228 | ~GrBoundaryTess() { |
| 229 | } |
| 230 | GrPointArray& contourPoints() { return fContourPoints; } |
| 231 | const GrIndexArray& contours() const { return fContours; } |
| 232 | private: |
| 233 | virtual void begin(GLenum type) { |
| 234 | fContourStart = fContourPoints.count(); |
| 235 | } |
| 236 | virtual void vertex(int index) { |
| 237 | *fContourPoints.append() = fVertices.at(index); |
| 238 | } |
| 239 | virtual void edgeFlag(bool flag) {} |
| 240 | virtual void end() { |
| 241 | *fContours.append() = fContourPoints.count() - fContourStart; |
| 242 | } |
| 243 | virtual int combine(GLdouble coords[3], int vertexIndices[4], |
| 244 | GLfloat weight[4]) { |
| 245 | int index = fVertices.count(); |
| 246 | *fVertices.append() = GrPoint::Make(static_cast<float>(coords[0]), |
| 247 | static_cast<float>(coords[1])); |
| 248 | return index; |
| 249 | } |
| 250 | GrPointArray fContourPoints; |
| 251 | GrIndexArray fContours; |
| 252 | size_t fContourStart; |
| 253 | }; |
| 254 | |
| 255 | static bool nearlyEqual(float a, float b) { |
| 256 | return fabsf(a - b) < 0.0001f; |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 257 | } |
| 258 | |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 259 | static bool nearlyEqual(const GrPoint& a, const GrPoint& b) { |
| 260 | return nearlyEqual(a.fX, b.fX) && nearlyEqual(a.fY, b.fY); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 261 | } |
| 262 | |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 263 | static bool parallel(const GrDrawTarget::Edge& a, const GrDrawTarget::Edge& b) { |
| 264 | return (nearlyEqual(a.fX, b.fX) && nearlyEqual(a.fY, b.fY)) || |
| 265 | (nearlyEqual(a.fX, -b.fX) && nearlyEqual(a.fY, -b.fY)); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 266 | } |
| 267 | |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 268 | static unsigned fill_type_to_glu_winding_rule(GrPathFill fill) { |
| 269 | switch (fill) { |
| 270 | case kWinding_PathFill: |
| 271 | return GLU_TESS_WINDING_NONZERO; |
| 272 | case kEvenOdd_PathFill: |
| 273 | return GLU_TESS_WINDING_ODD; |
| 274 | case kInverseWinding_PathFill: |
| 275 | return GLU_TESS_WINDING_POSITIVE; |
| 276 | case kInverseEvenOdd_PathFill: |
| 277 | return GLU_TESS_WINDING_ODD; |
| 278 | case kHairLine_PathFill: |
| 279 | return GLU_TESS_WINDING_NONZERO; // FIXME: handle this |
| 280 | default: |
| 281 | GrAssert(!"Unknown path fill!"); |
| 282 | return 0; |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | GrTesselatedPathRenderer::GrTesselatedPathRenderer() { |
| 287 | } |
| 288 | |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 289 | static bool isCCW(const GrPoint* pts, int count) { |
| 290 | GrVec v1, v2; |
| 291 | do { |
| 292 | v1 = pts[1] - pts[0]; |
| 293 | v2 = pts[2] - pts[1]; |
| 294 | pts++; |
| 295 | count--; |
| 296 | } while (nearlyEqual(v1, v2) && count > 3); |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 297 | return v1.cross(v2) < 0; |
| 298 | } |
| 299 | |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 300 | static bool validEdge(const GrDrawTarget::Edge& edge) { |
| 301 | return !(edge.fX == 0.0f && edge.fY == 0.0f && edge.fZ == 0.0f); |
| 302 | } |
| 303 | |
| 304 | static size_t computeEdgesAndIntersect(const GrMatrix& matrix, |
| 305 | const GrMatrix& inverse, |
| 306 | GrPoint* vertices, |
| 307 | size_t numVertices, |
| 308 | GrEdgeArray* edges, |
| 309 | float sign) { |
| 310 | if (numVertices < 3) { |
| 311 | return 0; |
| 312 | } |
senorblanco@chromium.org | ff174b3 | 2011-05-16 16:59:57 +0000 | [diff] [blame] | 313 | matrix.mapPoints(vertices, numVertices); |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 314 | if (sign == 0.0f) { |
| 315 | sign = isCCW(vertices, numVertices) ? -1.0f : 1.0f; |
| 316 | } |
| 317 | GrPoint p = sanitizePoint(vertices[numVertices - 1]); |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 318 | for (size_t i = 0; i < numVertices; ++i) { |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 319 | GrPoint q = sanitizePoint(vertices[i]); |
| 320 | if (p == q) { |
| 321 | continue; |
| 322 | } |
| 323 | GrDrawTarget::Edge edge = computeEdge(p, q, sign); |
| 324 | edge.fZ += 0.5f; // Offset by half a pixel along the tangent. |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 325 | *edges->append() = edge; |
| 326 | p = q; |
| 327 | } |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 328 | int count = edges->count(); |
| 329 | if (count == 0) { |
| 330 | return 0; |
| 331 | } |
| 332 | GrDrawTarget::Edge prev_edge = edges->at(0); |
| 333 | for (int i = 0; i < count; ++i) { |
| 334 | GrDrawTarget::Edge edge = edges->at(i < count - 1 ? i + 1 : 0); |
| 335 | if (parallel(edge, prev_edge)) { |
| 336 | // 3 points are collinear; offset by half the tangent instead |
| 337 | vertices[i].fX -= edge.fX * 0.5f; |
| 338 | vertices[i].fY -= edge.fY * 0.5f; |
| 339 | } else { |
| 340 | vertices[i] = prev_edge.intersect(edge); |
| 341 | } |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 342 | inverse.mapPoints(&vertices[i], 1); |
| 343 | prev_edge = edge; |
| 344 | } |
| 345 | return edges->count(); |
| 346 | } |
| 347 | |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 348 | void GrTesselatedPathRenderer::drawPath(GrDrawTarget::StageBitfield stages) { |
| 349 | GrDrawTarget::AutoStateRestore asr(fTarget); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 350 | // face culling doesn't make sense here |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 351 | GrAssert(GrDrawTarget::kBoth_DrawFace == fTarget->getDrawFace()); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 352 | |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 353 | GrMatrix viewM = fTarget->getViewMatrix(); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 354 | |
bsalomon@google.com | 181e9bd | 2011-09-07 18:42:30 +0000 | [diff] [blame] | 355 | GrScalar tol = GR_Scalar1; |
bsalomon@google.com | 3839632 | 2011-09-09 19:32:04 +0000 | [diff] [blame] | 356 | tol = GrPathUtils::scaleToleranceToSrc(tol, viewM, fPath->getBounds()); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 357 | GrScalar tolSqd = GrMul(tol, tol); |
| 358 | |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 359 | int subpathCnt; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 360 | int maxPts = GrPathUtils::worstCasePointCount(*fPath, &subpathCnt, tol); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 361 | |
| 362 | GrVertexLayout layout = 0; |
| 363 | for (int s = 0; s < GrDrawTarget::kNumStages; ++s) { |
| 364 | if ((1 << s) & stages) { |
| 365 | layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s); |
| 366 | } |
| 367 | } |
| 368 | |
bsalomon@google.com | fa6ac93 | 2011-10-05 19:57:55 +0000 | [diff] [blame] | 369 | bool inverted = GrIsFillInverted(fFill); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 370 | if (inverted) { |
| 371 | maxPts += 4; |
| 372 | subpathCnt++; |
| 373 | } |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 374 | if (maxPts > USHRT_MAX) { |
| 375 | return; |
| 376 | } |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 377 | SkAutoSTMalloc<8, GrPoint> baseMem(maxPts); |
| 378 | GrPoint* base = baseMem; |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 379 | GrPoint* vert = base; |
| 380 | GrPoint* subpathBase = base; |
| 381 | |
bsalomon@google.com | 3582bf9 | 2011-06-30 21:32:31 +0000 | [diff] [blame] | 382 | SkAutoSTMalloc<8, uint16_t> subpathVertCount(subpathCnt); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 383 | |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 384 | GrPoint pts[4]; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 385 | SkPath::Iter iter(*fPath, false); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 386 | |
| 387 | bool first = true; |
| 388 | int subpath = 0; |
| 389 | |
| 390 | for (;;) { |
reed@google.com | 07f3ee1 | 2011-05-16 17:21:57 +0000 | [diff] [blame] | 391 | switch (iter.next(pts)) { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 392 | case kMove_PathCmd: |
| 393 | if (!first) { |
| 394 | subpathVertCount[subpath] = vert-subpathBase; |
| 395 | subpathBase = vert; |
| 396 | ++subpath; |
| 397 | } |
| 398 | *vert = pts[0]; |
| 399 | vert++; |
| 400 | break; |
| 401 | case kLine_PathCmd: |
| 402 | *vert = pts[1]; |
| 403 | vert++; |
| 404 | break; |
| 405 | case kQuadratic_PathCmd: { |
| 406 | GrPathUtils::generateQuadraticPoints(pts[0], pts[1], pts[2], |
| 407 | tolSqd, &vert, |
| 408 | GrPathUtils::quadraticPointCount(pts, tol)); |
| 409 | break; |
| 410 | } |
| 411 | case kCubic_PathCmd: { |
| 412 | GrPathUtils::generateCubicPoints(pts[0], pts[1], pts[2], pts[3], |
| 413 | tolSqd, &vert, |
| 414 | GrPathUtils::cubicPointCount(pts, tol)); |
| 415 | break; |
| 416 | } |
| 417 | case kClose_PathCmd: |
| 418 | break; |
| 419 | case kEnd_PathCmd: |
| 420 | subpathVertCount[subpath] = vert-subpathBase; |
| 421 | ++subpath; // this could be only in debug |
| 422 | goto FINISHED; |
| 423 | } |
| 424 | first = false; |
| 425 | } |
| 426 | FINISHED: |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 427 | if (0 != fTranslate.fX || 0 != fTranslate.fY) { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 428 | for (int i = 0; i < vert - base; i++) { |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 429 | base[i].offset(fTranslate.fX, fTranslate.fY); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 430 | } |
| 431 | } |
| 432 | |
| 433 | if (inverted) { |
| 434 | GrRect bounds; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 435 | GrAssert(NULL != fTarget->getRenderTarget()); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 436 | bounds.setLTRB(0, 0, |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 437 | GrIntToScalar(fTarget->getRenderTarget()->width()), |
| 438 | GrIntToScalar(fTarget->getRenderTarget()->height())); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 439 | GrMatrix vmi; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 440 | if (fTarget->getViewInverse(&vmi)) { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 441 | vmi.mapRect(&bounds); |
| 442 | } |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 443 | *vert++ = GrPoint::Make(bounds.fLeft, bounds.fTop); |
| 444 | *vert++ = GrPoint::Make(bounds.fLeft, bounds.fBottom); |
| 445 | *vert++ = GrPoint::Make(bounds.fRight, bounds.fBottom); |
| 446 | *vert++ = GrPoint::Make(bounds.fRight, bounds.fTop); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 447 | subpathVertCount[subpath++] = 4; |
| 448 | } |
| 449 | |
| 450 | GrAssert(subpath == subpathCnt); |
| 451 | GrAssert((vert - base) <= maxPts); |
| 452 | |
| 453 | size_t count = vert - base; |
| 454 | |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 455 | if (count < 3) { |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 456 | return; |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 457 | } |
| 458 | |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 459 | if (subpathCnt == 1 && !inverted && fPath->isConvex()) { |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame^] | 460 | if (fAntiAlias) { |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 461 | GrEdgeArray edges; |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 462 | GrMatrix inverse, matrix = fTarget->getViewMatrix(); |
| 463 | fTarget->getViewInverse(&inverse); |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 464 | |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 465 | count = computeEdgesAndIntersect(matrix, inverse, base, count, &edges, 0.0f); |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 466 | size_t maxEdges = fTarget->getMaxEdges(); |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 467 | if (count == 0) { |
| 468 | return; |
| 469 | } |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 470 | if (count <= maxEdges) { |
| 471 | // All edges fit; upload all edges and draw all verts as a fan |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 472 | fTarget->setVertexSourceToArray(layout, base, count); |
| 473 | fTarget->setEdgeAAData(&edges[0], count); |
| 474 | fTarget->drawNonIndexed(kTriangleFan_PrimitiveType, 0, count); |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 475 | } else { |
| 476 | // Upload "maxEdges" edges and verts at a time, and draw as |
| 477 | // separate fans |
| 478 | for (size_t i = 0; i < count - 2; i += maxEdges - 2) { |
| 479 | edges[i] = edges[0]; |
| 480 | base[i] = base[0]; |
| 481 | int size = GR_CT_MIN(count - i, maxEdges); |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 482 | fTarget->setVertexSourceToArray(layout, &base[i], size); |
| 483 | fTarget->setEdgeAAData(&edges[i], size); |
| 484 | fTarget->drawNonIndexed(kTriangleFan_PrimitiveType, 0, size); |
senorblanco@chromium.org | ef3913b | 2011-05-19 17:11:07 +0000 | [diff] [blame] | 485 | } |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 486 | } |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 487 | fTarget->setEdgeAAData(NULL, 0); |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 488 | } else { |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 489 | fTarget->setVertexSourceToArray(layout, base, count); |
| 490 | fTarget->drawNonIndexed(kTriangleFan_PrimitiveType, 0, count); |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 491 | } |
senorblanco@chromium.org | cf3edc9 | 2011-03-29 17:42:30 +0000 | [diff] [blame] | 492 | return; |
| 493 | } |
| 494 | |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame^] | 495 | if (fAntiAlias) { |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 496 | // Run the tesselator once to get the boundaries. |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 497 | GrBoundaryTess btess(count, fill_type_to_glu_winding_rule(fFill)); |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 498 | btess.addVertices(base, subpathVertCount, subpathCnt); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 499 | |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 500 | GrMatrix inverse, matrix = fTarget->getViewMatrix(); |
| 501 | if (!fTarget->getViewInverse(&inverse)) { |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 502 | return; |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 503 | } |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 504 | |
| 505 | if (btess.vertices().count() > USHRT_MAX) { |
| 506 | return; |
| 507 | } |
| 508 | |
| 509 | // Inflate the boundary, and run the tesselator again to generate |
| 510 | // interior polys. |
| 511 | const GrPointArray& contourPoints = btess.contourPoints(); |
| 512 | const GrIndexArray& contours = btess.contours(); |
| 513 | GrEdgePolygonTess ptess(contourPoints.count(), GLU_TESS_WINDING_NONZERO, matrix); |
| 514 | |
| 515 | size_t i = 0; |
| 516 | Sk_gluTessBeginPolygon(ptess.tess(), &ptess); |
| 517 | for (int contour = 0; contour < contours.count(); ++contour) { |
| 518 | int count = contours[contour]; |
| 519 | GrEdgeArray edges; |
| 520 | int newCount = computeEdgesAndIntersect(matrix, inverse, &btess.contourPoints()[i], count, &edges, 1.0f); |
| 521 | Sk_gluTessBeginContour(ptess.tess()); |
| 522 | for (int j = 0; j < newCount; j++) { |
| 523 | ptess.addVertex(contourPoints[i + j], ptess.vertices().count()); |
| 524 | } |
| 525 | i += count; |
| 526 | Sk_gluTessEndContour(ptess.tess()); |
| 527 | } |
| 528 | |
| 529 | Sk_gluTessEndPolygon(ptess.tess()); |
| 530 | |
| 531 | if (ptess.vertices().count() > USHRT_MAX) { |
| 532 | return; |
| 533 | } |
| 534 | |
| 535 | // Draw the resulting polys and upload their edge data. |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 536 | fTarget->enableState(GrDrawTarget::kEdgeAAConcave_StateBit); |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 537 | const GrPointArray& vertices = ptess.vertices(); |
| 538 | const GrIndexArray& indices = ptess.indices(); |
| 539 | const GrDrawTarget::Edge* edges = ptess.edges(); |
| 540 | GR_DEBUGASSERT(indices.count() % 3 == 0); |
| 541 | for (int i = 0; i < indices.count(); i += 3) { |
| 542 | GrPoint tri_verts[3]; |
| 543 | int index0 = indices[i]; |
| 544 | int index1 = indices[i + 1]; |
| 545 | int index2 = indices[i + 2]; |
| 546 | tri_verts[0] = vertices[index0]; |
| 547 | tri_verts[1] = vertices[index1]; |
| 548 | tri_verts[2] = vertices[index2]; |
| 549 | GrDrawTarget::Edge tri_edges[6]; |
| 550 | int t = 0; |
| 551 | const GrDrawTarget::Edge& edge0 = edges[index0 * 2]; |
| 552 | const GrDrawTarget::Edge& edge1 = edges[index0 * 2 + 1]; |
| 553 | const GrDrawTarget::Edge& edge2 = edges[index1 * 2]; |
| 554 | const GrDrawTarget::Edge& edge3 = edges[index1 * 2 + 1]; |
| 555 | const GrDrawTarget::Edge& edge4 = edges[index2 * 2]; |
| 556 | const GrDrawTarget::Edge& edge5 = edges[index2 * 2 + 1]; |
| 557 | if (validEdge(edge0) && validEdge(edge1)) { |
| 558 | tri_edges[t++] = edge0; |
| 559 | tri_edges[t++] = edge1; |
| 560 | } |
| 561 | if (validEdge(edge2) && validEdge(edge3)) { |
| 562 | tri_edges[t++] = edge2; |
| 563 | tri_edges[t++] = edge3; |
| 564 | } |
| 565 | if (validEdge(edge4) && validEdge(edge5)) { |
| 566 | tri_edges[t++] = edge4; |
| 567 | tri_edges[t++] = edge5; |
| 568 | } |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 569 | fTarget->setEdgeAAData(&tri_edges[0], t); |
| 570 | fTarget->setVertexSourceToArray(layout, &tri_verts[0], 3); |
| 571 | fTarget->drawNonIndexed(kTriangles_PrimitiveType, 0, 3); |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 572 | } |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 573 | fTarget->setEdgeAAData(NULL, 0); |
| 574 | fTarget->disableState(GrDrawTarget::kEdgeAAConcave_StateBit); |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 575 | return; |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 576 | } |
| 577 | |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 578 | GrPolygonTess ptess(count, fill_type_to_glu_winding_rule(fFill)); |
senorblanco@chromium.org | 129b8e3 | 2011-06-15 17:52:09 +0000 | [diff] [blame] | 579 | ptess.addVertices(base, subpathVertCount, subpathCnt); |
| 580 | const GrPointArray& vertices = ptess.vertices(); |
| 581 | const GrIndexArray& indices = ptess.indices(); |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 582 | if (indices.count() > 0) { |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 583 | fTarget->setVertexSourceToArray(layout, vertices.begin(), vertices.count()); |
| 584 | fTarget->setIndexSourceToArray(indices.begin(), indices.count()); |
| 585 | fTarget->drawIndexed(kTriangles_PrimitiveType, |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 586 | 0, |
| 587 | 0, |
| 588 | vertices.count(), |
| 589 | indices.count()); |
| 590 | } |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 591 | } |
| 592 | |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame^] | 593 | bool GrTesselatedPathRenderer::canDrawPath(const GrDrawTarget::Caps& caps, |
bsalomon@google.com | aeb2160 | 2011-08-30 18:13:44 +0000 | [diff] [blame] | 594 | const SkPath& path, |
bsalomon@google.com | 289533a | 2011-10-27 12:34:25 +0000 | [diff] [blame^] | 595 | GrPathFill fill, |
| 596 | bool antiAlias) const { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 597 | return kHairLine_PathFill != fill; |
| 598 | } |
| 599 | |
bsalomon@google.com | ee43512 | 2011-07-01 14:57:55 +0000 | [diff] [blame] | 600 | void GrTesselatedPathRenderer::drawPathToStencil() { |
senorblanco@chromium.org | 9d18b78 | 2011-03-28 20:47:09 +0000 | [diff] [blame] | 601 | GrAlwaysAssert(!"multipass stencil should not be needed"); |
| 602 | } |
senorblanco@chromium.org | 92e0f22 | 2011-05-12 15:49:15 +0000 | [diff] [blame] | 603 | |