GrTessellator: misc. cleanups and minor tweaks.
authorStephen White <senorblanco@chromium.org>
Tue, 21 Feb 2017 20:24:13 +0000 (15:24 -0500)
committerSkia Commit-Bot <skia-commit-bot@chromium.org>
Wed, 22 Feb 2017 15:28:02 +0000 (15:28 +0000)
Invert some math to remove a negation.

Don't keep a persistent count in EdgeList; we'll test for degenerate
boundaries in boundary_to_aa_mesh().

Make connect() use the top/bottom ordering that new_edge has already
done for us.

Don't add the an edge to the same poly twice when it's easily detectable.

Remove some superfluous variables and intialization.

BUG=skia:

Change-Id: I0efd9ec385d6dfec8950b7acfc6dd25572f667b5
Reviewed-on: https://skia-review.googlesource.com/8784
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Stephen White <senorblanco@chromium.org>

src/gpu/GrTessellator.cpp

index dda8f48..388e951 100644 (file)
@@ -378,10 +378,10 @@ struct Edge {
         if (denom == 0.0) {
             return false;
         }
-        double dx = static_cast<double>(fTop->fPoint.fX) - other.fTop->fPoint.fX;
-        double dy = static_cast<double>(fTop->fPoint.fY) - other.fTop->fPoint.fY;
-        double sNumer = -dy * other.fLine.fB - dx * other.fLine.fA;
-        double tNumer = -dy * fLine.fB - dx * fLine.fA;
+        double dx = static_cast<double>(other.fTop->fPoint.fX) - fTop->fPoint.fX;
+        double dy = static_cast<double>(other.fTop->fPoint.fY) - fTop->fPoint.fY;
+        double sNumer = dy * other.fLine.fB + dx * other.fLine.fA;
+        double tNumer = dy * fLine.fB + dx * fLine.fA;
         // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
         // This saves us doing the divide below unless absolutely necessary.
         if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
@@ -409,21 +409,18 @@ struct Edge {
 };
 
 struct EdgeList {
-    EdgeList() : fHead(nullptr), fTail(nullptr), fNext(nullptr), fCount(0) {}
+    EdgeList() : fHead(nullptr), fTail(nullptr), fNext(nullptr) {}
     Edge* fHead;
     Edge* fTail;
     EdgeList* fNext;
-    int fCount;
     void insert(Edge* edge, Edge* prev, Edge* next) {
         list_insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &fHead, &fTail);
-        fCount++;
     }
     void append(Edge* e) {
         insert(e, fTail, nullptr);
     }
     void remove(Edge* edge) {
         list_remove<Edge, &Edge::fLeft, &Edge::fRight>(edge, &fHead, &fTail);
-        fCount--;
     }
     void close() {
         if (fHead && fTail) {
@@ -1050,13 +1047,8 @@ void split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkA
 Edge* connect(Vertex* prev, Vertex* next, Edge::Type type, Comparator& c, SkArenaAlloc& alloc,
               int winding_scale = 1) {
     Edge* edge = new_edge(prev, next, type, c, alloc);
-    if (edge->fWinding > 0) {
-        insert_edge_below(edge, prev, c);
-        insert_edge_above(edge, next, c);
-    } else {
-        insert_edge_below(edge, next, c);
-        insert_edge_above(edge, prev, c);
-    }
+    insert_edge_below(edge, edge->fTop, c);
+    insert_edge_above(edge, edge->fBottom, c);
     edge->fWinding *= winding_scale;
     merge_collinear_edges(edge, nullptr, c);
     return edge;
@@ -1294,8 +1286,8 @@ void simplify(const VertexList& vertices, Comparator& c, SkArenaAlloc& alloc) {
 #if LOGGING_ENABLED
         LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
 #endif
-        Edge* leftEnclosingEdge = nullptr;
-        Edge* rightEnclosingEdge = nullptr;
+        Edge* leftEnclosingEdge;
+        Edge* rightEnclosingEdge;
         bool restartChecks;
         do {
             restartChecks = false;
@@ -1353,11 +1345,11 @@ Poly* tessellate(const VertexList& vertices, SkArenaAlloc& alloc) {
 #if LOGGING_ENABLED
         LOG("\nvertex %g: (%g,%g), alpha %d\n", v->fID, v->fPoint.fX, v->fPoint.fY, v->fAlpha);
 #endif
-        Edge* leftEnclosingEdge = nullptr;
-        Edge* rightEnclosingEdge = nullptr;
+        Edge* leftEnclosingEdge;
+        Edge* rightEnclosingEdge;
         find_enclosing_edges(v, &activeEdges, &leftEnclosingEdge, &rightEnclosingEdge);
-        Poly* leftPoly = nullptr;
-        Poly* rightPoly = nullptr;
+        Poly* leftPoly;
+        Poly* rightPoly;
         if (v->fFirstEdgeAbove) {
             leftPoly = v->fFirstEdgeAbove->fLeftPoly;
             rightPoly = v->fLastEdgeAbove->fRightPoly;
@@ -1385,14 +1377,13 @@ Poly* tessellate(const VertexList& vertices, SkArenaAlloc& alloc) {
                 rightPoly = rightPoly->addEdge(v->fLastEdgeAbove, Poly::kLeft_Side, alloc);
             }
             for (Edge* e = v->fFirstEdgeAbove; e != v->fLastEdgeAbove; e = e->fNextEdgeAbove) {
-                Edge* leftEdge = e;
                 Edge* rightEdge = e->fNextEdgeAbove;
-                SkASSERT(rightEdge->isRightOf(leftEdge->fTop));
-                remove_edge(leftEdge, &activeEdges);
-                if (leftEdge->fRightPoly) {
-                    leftEdge->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc);
+                SkASSERT(rightEdge->isRightOf(e->fTop));
+                remove_edge(e, &activeEdges);
+                if (e->fRightPoly) {
+                    e->fRightPoly->addEdge(e, Poly::kLeft_Side, alloc);
                 }
-                if (rightEdge->fLeftPoly) {
+                if (rightEdge->fLeftPoly && rightEdge->fLeftPoly != e->fRightPoly) {
                     rightEdge->fLeftPoly->addEdge(e, Poly::kRight_Side, alloc);
                 }
             }
@@ -1545,6 +1536,10 @@ void fix_inversions(Vertex* prev, Vertex* next, Edge* prevBisector, Edge* nextBi
 // new antialiased mesh from those vertices.
 
 void boundary_to_aa_mesh(EdgeList* boundary, VertexList* mesh, Comparator& c, SkArenaAlloc& alloc) {
+    // A boundary with fewer than 3 edges is degenerate.
+    if (!boundary->fHead || !boundary->fHead->fRight || !boundary->fHead->fRight->fRight) {
+        return;
+    }
     Edge* prevEdge = boundary->fTail;
     float radius = 0.5f;
     double offset = radius * sqrt(prevEdge->fLine.magSq()) * prevEdge->fWinding;
@@ -1707,9 +1702,7 @@ Poly* contours_to_polys(Vertex** contours, int contourCnt, SkPath::FillType fill
         VertexList aaMesh;
         for (EdgeList* boundary = boundaries; boundary != nullptr; boundary = boundary->fNext) {
             simplify_boundary(boundary, c, alloc);
-            if (boundary->fCount > 2) {
-                boundary_to_aa_mesh(boundary, &aaMesh, c, alloc);
-            }
+            boundary_to_aa_mesh(boundary, &aaMesh, c, alloc);
         }
         sort_and_simplify(&aaMesh, c, alloc);
         return tessellate(aaMesh, alloc);