Chromium Code Reviews| Index: src/gpu/GrTessellator.cpp |
| diff --git a/src/gpu/batches/GrTessellatingPathRenderer.cpp b/src/gpu/GrTessellator.cpp |
| similarity index 63% |
| copy from src/gpu/batches/GrTessellatingPathRenderer.cpp |
| copy to src/gpu/GrTessellator.cpp |
| index 27e287e9c6bceb6b48c7e500ef338fcc7b259775..fdf19676ed927cbf2af47ff0711c7ec0643e78cb 100644 |
| --- a/src/gpu/batches/GrTessellatingPathRenderer.cpp |
| +++ b/src/gpu/GrTessellator.cpp |
| @@ -5,7 +5,7 @@ |
| * found in the LICENSE file. |
| */ |
| -#include "GrTessellatingPathRenderer.h" |
| +#include "GrTessellator.h" |
| #include "GrBatchFlushState.h" |
| #include "GrBatchTest.h" |
| @@ -14,7 +14,6 @@ |
| #include "GrVertices.h" |
| #include "GrResourceCache.h" |
| #include "GrResourceProvider.h" |
| -#include "SkChunkAlloc.h" |
| #include "SkGeometry.h" |
| #include "batches/GrVertexBatch.h" |
| @@ -22,10 +21,6 @@ |
| #include <stdio.h> |
| /* |
| - * This path renderer tessellates the path into triangles, uploads the triangles to a |
| - * vertex buffer, and renders them with a single draw call. It does not currently do |
| - * antialiasing, so it must be used in conjunction with multisampling. |
| - * |
| * There are six stages to the algorithm: |
| * |
| * 1) Linearize the path contours into piecewise linear segments (path_to_contours()). |
| @@ -80,10 +75,8 @@ |
| * increasing in Y; edges to the right are decreasing in Y). That is, the setting rotates 90 |
| * degrees counterclockwise, rather that transposing. |
| */ |
| -#define LOGGING_ENABLED 0 |
|
Stephen White
2016/01/04 22:51:19
If we do manage to keep Vertex and friends in the
|
| -#define WIREFRAME 0 |
| -#if LOGGING_ENABLED |
| +#if TESSELLATOR_LOGGING_ENABLED |
| #define LOG printf |
| #else |
| #define LOG(...) |
| @@ -91,10 +84,59 @@ |
| #define ALLOC_NEW(Type, args, alloc) new (alloc.allocThrow(sizeof(Type))) Type args |
| -namespace { |
| +double Edge::dist(const SkPoint& p) const { |
| + return fDY * p.fX - fDX * p.fY + fC; |
| +} |
| + |
| +bool Edge::isRightOf(TessellatorVertex* v) const { |
| + return dist(v->fPoint) < 0.0; |
| +} |
| + |
| +bool Edge::isLeftOf(TessellatorVertex* v) const { |
| + return dist(v->fPoint) > 0.0; |
| +} |
| + |
| +void Edge::recompute() { |
| + fDX = static_cast<double>(fBottom->fPoint.fX) - fTop->fPoint.fX; |
| + fDY = static_cast<double>(fBottom->fPoint.fY) - fTop->fPoint.fY; |
| + fC = static_cast<double>(fTop->fPoint.fY) * fBottom->fPoint.fX - |
| + static_cast<double>(fTop->fPoint.fX) * fBottom->fPoint.fY; |
| +} |
| + |
| +bool Edge::intersect(const Edge& other, SkPoint* p) { |
| +#if TESSELLATOR_LOGGING_ENABLED |
| + LOG("intersecting %g -> %g with %g -> %g\n", |
| + fTop->fID, fBottom->fID, |
| + other.fTop->fID, other.fBottom->fID); |
| +#endif |
| + if (fTop == other.fTop || fBottom == other.fBottom) { |
| + return false; |
| + } |
| + double denom = fDX * other.fDY - fDY * other.fDX; |
| + 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.fDX - dx * other.fDY; |
| + double tNumer = dy * fDX - dx * fDY; |
| + // 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) |
| + : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) { |
| + return false; |
| + } |
| + double s = sNumer / denom; |
| + SkASSERT(s >= 0.0 && s <= 1.0); |
| + p->fX = SkDoubleToScalar(fTop->fPoint.fX + s * fDX); |
| + p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fDY); |
| + return true; |
| +} |
| + |
| +bool Edge::isActive(EdgeList* activeEdges) const { |
| + return activeEdges && (fLeft || fRight || activeEdges->fHead == this); |
| +} |
| -struct Vertex; |
| -struct Edge; |
| struct Poly; |
| template <class T, T* T::*Prev, T* T::*Next> |
| @@ -128,40 +170,6 @@ void remove(T* t, T** head, T** tail) { |
| t->*Prev = t->*Next = nullptr; |
| } |
| -/** |
| - * Vertices are used in three ways: first, the path contours are converted into a |
| - * circularly-linked list of Vertices for each contour. After edge construction, the same Vertices |
| - * are re-ordered by the merge sort according to the sweep_lt comparator (usually, increasing |
| - * in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid |
| - * reallocation. Finally, MonotonePolys are built containing a circularly-linked list of |
| - * Vertices. (Currently, those Vertices are newly-allocated for the MonotonePolys, since |
| - * an individual Vertex from the path mesh may belong to multiple |
| - * MonotonePolys, so the original Vertices cannot be re-used. |
| - */ |
| - |
| -struct Vertex { |
| - Vertex(const SkPoint& point) |
| - : fPoint(point), fPrev(nullptr), fNext(nullptr) |
| - , fFirstEdgeAbove(nullptr), fLastEdgeAbove(nullptr) |
| - , fFirstEdgeBelow(nullptr), fLastEdgeBelow(nullptr) |
| - , fProcessed(false) |
| -#if LOGGING_ENABLED |
| - , fID (-1.0f) |
| -#endif |
| - {} |
| - SkPoint fPoint; // Vertex position |
| - Vertex* fPrev; // Linked list of contours, then Y-sorted vertices. |
| - Vertex* fNext; // " |
| - Edge* fFirstEdgeAbove; // Linked list of edges above this vertex. |
| - Edge* fLastEdgeAbove; // " |
| - Edge* fFirstEdgeBelow; // Linked list of edges below this vertex. |
| - Edge* fLastEdgeBelow; // " |
| - bool fProcessed; // Has this vertex been seen in simplify()? |
| -#if LOGGING_ENABLED |
| - float fID; // Identifier used for logging. |
| -#endif |
| -}; |
| - |
| /***************************************************************************************/ |
| typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b); |
| @@ -187,13 +195,14 @@ bool sweep_gt_vert(const SkPoint& a, const SkPoint& b) { |
| return a.fY == b.fY ? a.fX > b.fX : a.fY > b.fY; |
| } |
| -inline SkPoint* emit_vertex(Vertex* v, SkPoint* data) { |
| +inline SkPoint* emit_vertex(TessellatorVertex* v, SkPoint* data) { |
| *data++ = v->fPoint; |
| return data; |
| } |
| -SkPoint* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, SkPoint* data) { |
| -#if WIREFRAME |
| +SkPoint* emit_triangle(TessellatorVertex* v0, TessellatorVertex* v1, TessellatorVertex* v2, |
| + SkPoint* data) { |
| +#if TESSELLATOR_WIREFRAME |
| data = emit_vertex(v0, data); |
| data = emit_vertex(v1, data); |
| data = emit_vertex(v1, data); |
| @@ -208,12 +217,6 @@ SkPoint* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, SkPoint* data) { |
| return data; |
| } |
| -struct EdgeList { |
| - EdgeList() : fHead(nullptr), fTail(nullptr) {} |
| - Edge* fHead; |
| - Edge* fTail; |
| -}; |
| - |
| /** |
| * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and |
| * "edge below" a vertex as well as for the active edge list is handled by isLeftOf()/isRightOf(). |
| @@ -232,82 +235,6 @@ struct EdgeList { |
| * this file). |
| */ |
| -struct Edge { |
| - Edge(Vertex* top, Vertex* bottom, int winding) |
| - : fWinding(winding) |
| - , fTop(top) |
| - , fBottom(bottom) |
| - , fLeft(nullptr) |
| - , fRight(nullptr) |
| - , fPrevEdgeAbove(nullptr) |
| - , fNextEdgeAbove(nullptr) |
| - , fPrevEdgeBelow(nullptr) |
| - , fNextEdgeBelow(nullptr) |
| - , fLeftPoly(nullptr) |
| - , fRightPoly(nullptr) { |
| - recompute(); |
| - } |
| - int fWinding; // 1 == edge goes downward; -1 = edge goes upward. |
| - Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt). |
| - Vertex* fBottom; // The bottom vertex in vertex-sort-order. |
| - Edge* fLeft; // The linked list of edges in the active edge list. |
| - Edge* fRight; // " |
| - Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "edges above". |
| - Edge* fNextEdgeAbove; // " |
| - Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below". |
| - Edge* fNextEdgeBelow; // " |
| - Poly* fLeftPoly; // The Poly to the left of this edge, if any. |
| - Poly* fRightPoly; // The Poly to the right of this edge, if any. |
| - double fDX; // The line equation for this edge, in implicit form. |
| - double fDY; // fDY * x + fDX * y + fC = 0, for point (x, y) on the line. |
| - double fC; |
| - double dist(const SkPoint& p) const { |
| - return fDY * p.fX - fDX * p.fY + fC; |
| - } |
| - bool isRightOf(Vertex* v) const { |
| - return dist(v->fPoint) < 0.0; |
| - } |
| - bool isLeftOf(Vertex* v) const { |
| - return dist(v->fPoint) > 0.0; |
| - } |
| - void recompute() { |
| - fDX = static_cast<double>(fBottom->fPoint.fX) - fTop->fPoint.fX; |
| - fDY = static_cast<double>(fBottom->fPoint.fY) - fTop->fPoint.fY; |
| - fC = static_cast<double>(fTop->fPoint.fY) * fBottom->fPoint.fX - |
| - static_cast<double>(fTop->fPoint.fX) * fBottom->fPoint.fY; |
| - } |
| - bool intersect(const Edge& other, SkPoint* p) { |
| - LOG("intersecting %g -> %g with %g -> %g\n", |
| - fTop->fID, fBottom->fID, |
| - other.fTop->fID, other.fBottom->fID); |
| - if (fTop == other.fTop || fBottom == other.fBottom) { |
| - return false; |
| - } |
| - double denom = fDX * other.fDY - fDY * other.fDX; |
| - 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.fDX - dx * other.fDY; |
| - double tNumer = dy * fDX - dx * fDY; |
| - // 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) |
| - : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) { |
| - return false; |
| - } |
| - double s = sNumer / denom; |
| - SkASSERT(s >= 0.0 && s <= 1.0); |
| - p->fX = SkDoubleToScalar(fTop->fPoint.fX + s * fDX); |
| - p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fDY); |
| - return true; |
| - } |
| - bool isActive(EdgeList* activeEdges) const { |
| - return activeEdges && (fLeft || fRight || activeEdges->fHead == this); |
| - } |
| -}; |
| - |
| /***************************************************************************************/ |
| struct Poly { |
| @@ -320,7 +247,7 @@ struct Poly { |
| , fPartner(nullptr) |
| , fCount(0) |
| { |
| -#if LOGGING_ENABLED |
| +#if TESSELLATOR_LOGGING_ENABLED |
| static int gID = 0; |
| fID = gID++; |
| LOG("*** created Poly %d\n", fID); |
| @@ -335,12 +262,12 @@ struct Poly { |
| , fPrev(nullptr) |
| , fNext(nullptr) {} |
| Side fSide; |
| - Vertex* fHead; |
| - Vertex* fTail; |
| + TessellatorVertex* fHead; |
| + TessellatorVertex* fTail; |
| MonotonePoly* fPrev; |
| MonotonePoly* fNext; |
| - bool addVertex(Vertex* v, Side side, SkChunkAlloc& alloc) { |
| - Vertex* newV = ALLOC_NEW(Vertex, (v->fPoint), alloc); |
| + bool addVertex(TessellatorVertex* v, Side side, SkChunkAlloc& alloc) { |
| + TessellatorVertex* newV = ALLOC_NEW(TessellatorVertex, (v->fPoint), alloc); |
| bool done = false; |
| if (fSide == kNeither_Side) { |
| fSide = side; |
| @@ -361,14 +288,14 @@ struct Poly { |
| return done; |
| } |
| - SkPoint* emit(SkPoint* data) { |
| - Vertex* first = fHead; |
| - Vertex* v = first->fNext; |
| + SkPoint* emit(int winding, SkPoint* data) { |
| + TessellatorVertex* first = fHead; |
| + TessellatorVertex* v = first->fNext; |
| while (v != fTail) { |
| SkASSERT(v && v->fPrev && v->fNext); |
| - Vertex* prev = v->fPrev; |
| - Vertex* curr = v; |
| - Vertex* next = v->fNext; |
| + TessellatorVertex* prev = v->fPrev; |
| + TessellatorVertex* curr = v; |
| + TessellatorVertex* next = v->fNext; |
| double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint.fX; |
| double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint.fY; |
| double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint.fX; |
| @@ -389,7 +316,7 @@ struct Poly { |
| return data; |
| } |
| }; |
| - Poly* addVertex(Vertex* v, Side side, SkChunkAlloc& alloc) { |
| + Poly* addVertex(TessellatorVertex* v, Side side, SkChunkAlloc& alloc) { |
| LOG("addVertex() to %d at %g (%g, %g), %s side\n", fID, v->fID, v->fPoint.fX, v->fPoint.fY, |
| side == kLeft_Side ? "left" : side == kRight_Side ? "right" : "neither"); |
| Poly* partner = fPartner; |
| @@ -412,7 +339,7 @@ struct Poly { |
| partner->addVertex(v, side, alloc); |
| poly = partner; |
| } else { |
| - Vertex* prev = fActive->fSide == Poly::kLeft_Side ? |
| + TessellatorVertex* prev = fActive->fSide == Poly::kLeft_Side ? |
| fActive->fHead->fNext : fActive->fTail->fPrev; |
| fActive = ALLOC_NEW(MonotonePoly, , alloc); |
| fActive->addVertex(prev, Poly::kNeither_Side, alloc); |
| @@ -422,7 +349,7 @@ struct Poly { |
| fCount++; |
| return poly; |
| } |
| - void end(Vertex* v, SkChunkAlloc& alloc) { |
| + void end(TessellatorVertex* v, SkChunkAlloc& alloc) { |
| LOG("end() %d at %g, %g\n", fID, v->fPoint.fX, v->fPoint.fY); |
| if (fPartner) { |
| fPartner = fPartner->fPartner = nullptr; |
| @@ -435,7 +362,7 @@ struct Poly { |
| } |
| LOG("emit() %d, size %d\n", fID, fCount); |
| for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) { |
| - data = m->emit(data); |
| + data = m->emit(fWinding, data); |
| } |
| return data; |
| } |
| @@ -446,7 +373,7 @@ struct Poly { |
| Poly* fNext; |
| Poly* fPartner; |
| int fCount; |
| -#if LOGGING_ENABLED |
| +#if TESSELLATOR_LOGGING_ENABLED |
| int fID; |
| #endif |
| }; |
| @@ -457,7 +384,7 @@ bool coincident(const SkPoint& a, const SkPoint& b) { |
| return a == b; |
| } |
| -Poly* new_poly(Poly** head, Vertex* v, int winding, SkChunkAlloc& alloc) { |
| +Poly* new_poly(Poly** head, TessellatorVertex* v, int winding, SkChunkAlloc& alloc) { |
| Poly* poly = ALLOC_NEW(Poly, (winding), alloc); |
| poly->addVertex(v, Poly::kNeither_Side, alloc); |
| poly->fNext = *head; |
| @@ -465,10 +392,10 @@ Poly* new_poly(Poly** head, Vertex* v, int winding, SkChunkAlloc& alloc) { |
| return poly; |
| } |
| -Vertex* append_point_to_contour(const SkPoint& p, Vertex* prev, Vertex** head, |
| - SkChunkAlloc& alloc) { |
| - Vertex* v = ALLOC_NEW(Vertex, (p), alloc); |
| -#if LOGGING_ENABLED |
| +TessellatorVertex* append_point_to_contour(const SkPoint& p, TessellatorVertex* prev, |
| + TessellatorVertex** head, SkChunkAlloc& alloc) { |
| + TessellatorVertex* v = ALLOC_NEW(TessellatorVertex, (p), alloc); |
| +#if TESSELLATOR_LOGGING_ENABLED |
| static float gID = 0.0f; |
| v->fID = gID++; |
| #endif |
| @@ -481,14 +408,14 @@ Vertex* append_point_to_contour(const SkPoint& p, Vertex* prev, Vertex** head, |
| return v; |
| } |
| -Vertex* generate_quadratic_points(const SkPoint& p0, |
| - const SkPoint& p1, |
| - const SkPoint& p2, |
| - SkScalar tolSqd, |
| - Vertex* prev, |
| - Vertex** head, |
| - int pointsLeft, |
| - SkChunkAlloc& alloc) { |
| +TessellatorVertex* generate_quadratic_points(const SkPoint& p0, |
| + const SkPoint& p1, |
| + const SkPoint& p2, |
| + SkScalar tolSqd, |
| + TessellatorVertex* prev, |
| + TessellatorVertex** head, |
| + int pointsLeft, |
| + SkChunkAlloc& alloc) { |
| SkScalar d = p1.distanceToLineSegmentBetweenSqd(p0, p2); |
| if (pointsLeft < 2 || d < tolSqd || !SkScalarIsFinite(d)) { |
| return append_point_to_contour(p2, prev, head, alloc); |
| @@ -506,15 +433,15 @@ Vertex* generate_quadratic_points(const SkPoint& p0, |
| return prev; |
| } |
| -Vertex* generate_cubic_points(const SkPoint& p0, |
| - const SkPoint& p1, |
| - const SkPoint& p2, |
| - const SkPoint& p3, |
| - SkScalar tolSqd, |
| - Vertex* prev, |
| - Vertex** head, |
| - int pointsLeft, |
| - SkChunkAlloc& alloc) { |
| +TessellatorVertex* generate_cubic_points(const SkPoint& p0, |
| + const SkPoint& p1, |
| + const SkPoint& p2, |
| + const SkPoint& p3, |
| + SkScalar tolSqd, |
| + TessellatorVertex* prev, |
| + TessellatorVertex** head, |
| + int pointsLeft, |
| + SkChunkAlloc& alloc) { |
| SkScalar d1 = p1.distanceToLineSegmentBetweenSqd(p0, p3); |
| SkScalar d2 = p2.distanceToLineSegmentBetweenSqd(p0, p3); |
| if (pointsLeft < 2 || (d1 < tolSqd && d2 < tolSqd) || |
| @@ -540,16 +467,15 @@ Vertex* generate_cubic_points(const SkPoint& p0, |
| // Stage 1: convert the input path to a set of linear contours (linked list of Vertices). |
| void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, |
| - Vertex** contours, SkChunkAlloc& alloc, bool *isLinear) { |
| - |
| + TessellatorVertex** contours, SkChunkAlloc& alloc, bool *isLinear) { |
| SkScalar toleranceSqd = tolerance * tolerance; |
| SkPoint pts[4]; |
| bool done = false; |
| *isLinear = true; |
| SkPath::Iter iter(path, false); |
| - Vertex* prev = nullptr; |
| - Vertex* head = nullptr; |
| + TessellatorVertex* prev = nullptr; |
| + TessellatorVertex* head = nullptr; |
| if (path.isInverseFillType()) { |
| SkPoint quad[4]; |
| clipBounds.toQuad(quad); |
| @@ -640,10 +566,11 @@ inline bool apply_fill_type(SkPath::FillType fillType, int winding) { |
| } |
| } |
| -Edge* new_edge(Vertex* prev, Vertex* next, SkChunkAlloc& alloc, Comparator& c) { |
| +Edge* new_edge(TessellatorVertex* prev, TessellatorVertex* next, SkChunkAlloc& alloc, |
| + Comparator& c) { |
| int winding = c.sweep_lt(prev->fPoint, next->fPoint) ? 1 : -1; |
| - Vertex* top = winding < 0 ? next : prev; |
| - Vertex* bottom = winding < 0 ? prev : next; |
| + TessellatorVertex* top = winding < 0 ? next : prev; |
| + TessellatorVertex* bottom = winding < 0 ? prev : next; |
| return ALLOC_NEW(Edge, (top, bottom, winding), alloc); |
| } |
| @@ -660,7 +587,7 @@ void insert_edge(Edge* edge, Edge* prev, EdgeList* edges) { |
| insert<Edge, &Edge::fLeft, &Edge::fRight>(edge, prev, next, &edges->fHead, &edges->fTail); |
| } |
| -void find_enclosing_edges(Vertex* v, EdgeList* edges, Edge** left, Edge** right) { |
| +void find_enclosing_edges(TessellatorVertex* v, EdgeList* edges, Edge** left, Edge** right) { |
| if (v->fFirstEdgeAbove) { |
| *left = v->fFirstEdgeAbove->fLeft; |
| *right = v->fLastEdgeAbove->fRight; |
| @@ -711,7 +638,7 @@ void fix_active_state(Edge* edge, EdgeList* activeEdges, Comparator& c) { |
| } |
| } |
| -void insert_edge_above(Edge* edge, Vertex* v, Comparator& c) { |
| +void insert_edge_above(Edge* edge, TessellatorVertex* v, Comparator& c) { |
| if (edge->fTop->fPoint == edge->fBottom->fPoint || |
| c.sweep_gt(edge->fTop->fPoint, edge->fBottom->fPoint)) { |
| return; |
| @@ -729,7 +656,7 @@ void insert_edge_above(Edge* edge, Vertex* v, Comparator& c) { |
| edge, prev, next, &v->fFirstEdgeAbove, &v->fLastEdgeAbove); |
| } |
| -void insert_edge_below(Edge* edge, Vertex* v, Comparator& c) { |
| +void insert_edge_below(Edge* edge, TessellatorVertex* v, Comparator& c) { |
| if (edge->fTop->fPoint == edge->fBottom->fPoint || |
| c.sweep_gt(edge->fTop->fPoint, edge->fBottom->fPoint)) { |
| return; |
| @@ -775,7 +702,7 @@ void erase_edge_if_zero_winding(Edge* edge, EdgeList* edges) { |
| void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c); |
| -void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) { |
| +void set_top(Edge* edge, TessellatorVertex* v, EdgeList* activeEdges, Comparator& c) { |
| remove_edge_below(edge); |
| edge->fTop = v; |
| edge->recompute(); |
| @@ -784,7 +711,7 @@ void set_top(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) { |
| merge_collinear_edges(edge, activeEdges, c); |
| } |
| -void set_bottom(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c) { |
| +void set_bottom(Edge* edge, TessellatorVertex* v, EdgeList* activeEdges, Comparator& c) { |
| remove_edge_above(edge); |
| edge->fBottom = v; |
| edge->recompute(); |
| @@ -850,14 +777,15 @@ void merge_collinear_edges(Edge* edge, EdgeList* activeEdges, Comparator& c) { |
| } |
| } |
| -void split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkChunkAlloc& alloc); |
| +void split_edge(Edge* edge, TessellatorVertex* v, EdgeList* activeEdges, Comparator& c, |
| + SkChunkAlloc& alloc); |
| void cleanup_active_edges(Edge* edge, EdgeList* activeEdges, Comparator& c, SkChunkAlloc& alloc) { |
| - Vertex* top = edge->fTop; |
| - Vertex* bottom = edge->fBottom; |
| + TessellatorVertex* top = edge->fTop; |
| + TessellatorVertex* bottom = edge->fBottom; |
| if (edge->fLeft) { |
| - Vertex* leftTop = edge->fLeft->fTop; |
| - Vertex* leftBottom = edge->fLeft->fBottom; |
| + TessellatorVertex* leftTop = edge->fLeft->fTop; |
| + TessellatorVertex* leftBottom = edge->fLeft->fBottom; |
| if (c.sweep_gt(top->fPoint, leftTop->fPoint) && !edge->fLeft->isLeftOf(top)) { |
| split_edge(edge->fLeft, edge->fTop, activeEdges, c, alloc); |
| } else if (c.sweep_gt(leftTop->fPoint, top->fPoint) && !edge->isRightOf(leftTop)) { |
| @@ -870,8 +798,8 @@ void cleanup_active_edges(Edge* edge, EdgeList* activeEdges, Comparator& c, SkCh |
| } |
| } |
| if (edge->fRight) { |
| - Vertex* rightTop = edge->fRight->fTop; |
| - Vertex* rightBottom = edge->fRight->fBottom; |
| + TessellatorVertex* rightTop = edge->fRight->fTop; |
| + TessellatorVertex* rightBottom = edge->fRight->fBottom; |
| if (c.sweep_gt(top->fPoint, rightTop->fPoint) && !edge->fRight->isRightOf(top)) { |
| split_edge(edge->fRight, top, activeEdges, c, alloc); |
| } else if (c.sweep_gt(rightTop->fPoint, top->fPoint) && !edge->isLeftOf(rightTop)) { |
| @@ -886,7 +814,8 @@ void cleanup_active_edges(Edge* edge, EdgeList* activeEdges, Comparator& c, SkCh |
| } |
| } |
| -void split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkChunkAlloc& alloc) { |
| +void split_edge(Edge* edge, TessellatorVertex* v, EdgeList* activeEdges, Comparator& c, |
| + SkChunkAlloc& alloc) { |
| LOG("splitting edge (%g -> %g) at vertex %g (%g, %g)\n", |
| edge->fTop->fID, edge->fBottom->fID, |
| v->fID, v->fPoint.fX, v->fPoint.fY); |
| @@ -905,7 +834,8 @@ void split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkC |
| } |
| } |
| -void merge_vertices(Vertex* src, Vertex* dst, Vertex** head, Comparator& c, SkChunkAlloc& alloc) { |
| +void merge_vertices(TessellatorVertex* src, TessellatorVertex* dst, TessellatorVertex** head, |
| + Comparator& c, SkChunkAlloc& alloc) { |
| LOG("found coincident verts at %g, %g; merging %g into %g\n", src->fPoint.fX, src->fPoint.fY, |
| src->fID, dst->fID); |
| for (Edge* edge = src->fFirstEdgeAbove; edge;) { |
| @@ -918,17 +848,18 @@ void merge_vertices(Vertex* src, Vertex* dst, Vertex** head, Comparator& c, SkCh |
| set_top(edge, dst, nullptr, c); |
| edge = next; |
| } |
| - remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(src, head, nullptr); |
| + remove<TessellatorVertex, &TessellatorVertex::fPrev, &TessellatorVertex::fNext>(src, head, |
| + nullptr); |
| } |
| -Vertex* check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c, |
| - SkChunkAlloc& alloc) { |
| +TessellatorVertex* check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, |
| + Comparator& c, SkChunkAlloc& alloc) { |
| SkPoint p; |
| if (!edge || !other) { |
| return nullptr; |
| } |
| if (edge->intersect(*other, &p)) { |
| - Vertex* v; |
| + TessellatorVertex* v; |
| LOG("found intersection, pt is %g, %g\n", p.fX, p.fY); |
| if (p == edge->fTop->fPoint || c.sweep_lt(p, edge->fTop->fPoint)) { |
| split_edge(other, edge->fTop, activeEdges, c, alloc); |
| @@ -943,24 +874,24 @@ Vertex* check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, C |
| split_edge(edge, other->fBottom, activeEdges, c, alloc); |
| v = other->fBottom; |
| } else { |
| - Vertex* nextV = edge->fTop; |
| + TessellatorVertex* nextV = edge->fTop; |
| while (c.sweep_lt(p, nextV->fPoint)) { |
| nextV = nextV->fPrev; |
| } |
| while (c.sweep_lt(nextV->fPoint, p)) { |
| nextV = nextV->fNext; |
| } |
| - Vertex* prevV = nextV->fPrev; |
| + TessellatorVertex* prevV = nextV->fPrev; |
| if (coincident(prevV->fPoint, p)) { |
| v = prevV; |
| } else if (coincident(nextV->fPoint, p)) { |
| v = nextV; |
| } else { |
| - v = ALLOC_NEW(Vertex, (p), alloc); |
| + v = ALLOC_NEW(TessellatorVertex, (p), alloc); |
| LOG("inserting between %g (%g, %g) and %g (%g, %g)\n", |
| prevV->fID, prevV->fPoint.fX, prevV->fPoint.fY, |
| nextV->fID, nextV->fPoint.fX, nextV->fPoint.fY); |
| -#if LOGGING_ENABLED |
| +#if TESSELLATOR_LOGGING_ENABLED |
| v->fID = (nextV->fID + prevV->fID) * 0.5f; |
| #endif |
| v->fPrev = prevV; |
| @@ -976,10 +907,10 @@ Vertex* check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, C |
| return nullptr; |
| } |
| -void sanitize_contours(Vertex** contours, int contourCnt) { |
| +void sanitize_contours(TessellatorVertex** contours, int contourCnt) { |
| for (int i = 0; i < contourCnt; ++i) { |
| SkASSERT(contours[i]); |
| - for (Vertex* v = contours[i];;) { |
| + for (TessellatorVertex* v = contours[i];;) { |
| if (coincident(v->fPrev->fPoint, v->fPoint)) { |
| LOG("vertex %g,%g coincident; removing\n", v->fPoint.fX, v->fPoint.fY); |
| if (v->fPrev == v) { |
| @@ -1000,8 +931,8 @@ void sanitize_contours(Vertex** contours, int contourCnt) { |
| } |
| } |
| -void merge_coincident_vertices(Vertex** vertices, Comparator& c, SkChunkAlloc& alloc) { |
| - for (Vertex* v = (*vertices)->fNext; v != nullptr; v = v->fNext) { |
| +void merge_coincident_vertices(TessellatorVertex** vertices, Comparator& c, SkChunkAlloc& alloc) { |
| + for (TessellatorVertex* v = (*vertices)->fNext; v != nullptr; v = v->fNext) { |
| if (c.sweep_lt(v->fPoint, v->fPrev->fPoint)) { |
| v->fPoint = v->fPrev->fPoint; |
| } |
| @@ -1013,12 +944,13 @@ void merge_coincident_vertices(Vertex** vertices, Comparator& c, SkChunkAlloc& a |
| // Stage 2: convert the contours to a mesh of edges connecting the vertices. |
| -Vertex* build_edges(Vertex** contours, int contourCnt, Comparator& c, SkChunkAlloc& alloc) { |
| - Vertex* vertices = nullptr; |
| - Vertex* prev = nullptr; |
| +TessellatorVertex* build_edges(TessellatorVertex** contours, int contourCnt, Comparator& c, |
| + SkChunkAlloc& alloc) { |
| + TessellatorVertex* vertices = nullptr; |
| + TessellatorVertex* prev = nullptr; |
| for (int i = 0; i < contourCnt; ++i) { |
| - for (Vertex* v = contours[i]; v != nullptr;) { |
| - Vertex* vNext = v->fNext; |
| + for (TessellatorVertex* v = contours[i]; v != nullptr;) { |
| + TessellatorVertex* vNext = v->fNext; |
| Edge* edge = new_edge(v->fPrev, v, alloc, c); |
| if (edge->fWinding > 0) { |
| insert_edge_below(edge, v->fPrev, c); |
| @@ -1047,11 +979,11 @@ Vertex* build_edges(Vertex** contours, int contourCnt, Comparator& c, SkChunkAll |
| // Stage 3: sort the vertices by increasing sweep direction. |
| -Vertex* sorted_merge(Vertex* a, Vertex* b, Comparator& c); |
| +TessellatorVertex* sorted_merge(TessellatorVertex* a, TessellatorVertex* b, Comparator& c); |
| -void front_back_split(Vertex* v, Vertex** pFront, Vertex** pBack) { |
| - Vertex* fast; |
| - Vertex* slow; |
| +void front_back_split(TessellatorVertex* v, TessellatorVertex** pFront, TessellatorVertex** pBack) { |
| + TessellatorVertex* fast; |
| + TessellatorVertex* slow; |
| if (!v || !v->fNext) { |
| *pFront = v; |
| *pBack = nullptr; |
| @@ -1074,13 +1006,13 @@ void front_back_split(Vertex* v, Vertex** pFront, Vertex** pBack) { |
| } |
| } |
| -void merge_sort(Vertex** head, Comparator& c) { |
| +void merge_sort(TessellatorVertex** head, Comparator& c) { |
| if (!*head || !(*head)->fNext) { |
| return; |
| } |
| - Vertex* a; |
| - Vertex* b; |
| + TessellatorVertex* a; |
| + TessellatorVertex* b; |
| front_back_split(*head, &a, &b); |
| merge_sort(&a, c); |
| @@ -1089,25 +1021,31 @@ void merge_sort(Vertex** head, Comparator& c) { |
| *head = sorted_merge(a, b, c); |
| } |
| -inline void append_vertex(Vertex* v, Vertex** head, Vertex** tail) { |
| - insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, *tail, nullptr, head, tail); |
| +inline void append_vertex(TessellatorVertex* v, TessellatorVertex** head, |
| + TessellatorVertex** tail) { |
| + insert<TessellatorVertex, &TessellatorVertex::fPrev, &TessellatorVertex::fNext>(v, *tail, |
| + nullptr, head, |
| + tail); |
| } |
| -inline void append_vertex_list(Vertex* v, Vertex** head, Vertex** tail) { |
| - insert<Vertex, &Vertex::fPrev, &Vertex::fNext>(v, *tail, v->fNext, head, tail); |
| +inline void append_vertex_list(TessellatorVertex* v, TessellatorVertex** head, |
| + TessellatorVertex** tail) { |
| + insert<TessellatorVertex, &TessellatorVertex::fPrev, &TessellatorVertex::fNext>(v, *tail, |
| + v->fNext, head, |
| + tail); |
| } |
| -Vertex* sorted_merge(Vertex* a, Vertex* b, Comparator& c) { |
| - Vertex* head = nullptr; |
| - Vertex* tail = nullptr; |
| +TessellatorVertex* sorted_merge(TessellatorVertex* a, TessellatorVertex* b, Comparator& c) { |
| + TessellatorVertex* head = nullptr; |
| + TessellatorVertex* tail = nullptr; |
| while (a && b) { |
| if (c.sweep_lt(a->fPoint, b->fPoint)) { |
| - Vertex* next = a->fNext; |
| + TessellatorVertex* next = a->fNext; |
| append_vertex(a, &head, &tail); |
| a = next; |
| } else { |
| - Vertex* next = b->fNext; |
| + TessellatorVertex* next = b->fNext; |
| append_vertex(b, &head, &tail); |
| b = next; |
| } |
| @@ -1123,14 +1061,14 @@ Vertex* sorted_merge(Vertex* a, Vertex* b, Comparator& c) { |
| // Stage 4: Simplify the mesh by inserting new vertices at intersecting edges. |
| -void simplify(Vertex* vertices, Comparator& c, SkChunkAlloc& alloc) { |
| +void simplify(TessellatorVertex* vertices, Comparator& c, SkChunkAlloc& alloc) { |
| LOG("simplifying complex polygons\n"); |
| EdgeList activeEdges; |
| - for (Vertex* v = vertices; v != nullptr; v = v->fNext) { |
| + for (TessellatorVertex* v = vertices; v != nullptr; v = v->fNext) { |
| if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) { |
| continue; |
| } |
| -#if LOGGING_ENABLED |
| +#if TESSELLATOR_LOGGING_ENABLED |
| LOG("\nvertex %g: (%g,%g)\n", v->fID, v->fPoint.fX, v->fPoint.fY); |
| #endif |
| Edge* leftEnclosingEdge = nullptr; |
| @@ -1151,8 +1089,9 @@ void simplify(Vertex* vertices, Comparator& c, SkChunkAlloc& alloc) { |
| } |
| } |
| } else { |
| - if (Vertex* pv = check_for_intersection(leftEnclosingEdge, rightEnclosingEdge, |
| - &activeEdges, c, alloc)) { |
| + if (TessellatorVertex* pv = check_for_intersection(leftEnclosingEdge, |
| + rightEnclosingEdge, &activeEdges, |
| + c, alloc)) { |
| if (c.sweep_lt(pv->fPoint, v->fPoint)) { |
| v = pv; |
| } |
| @@ -1175,15 +1114,15 @@ void simplify(Vertex* vertices, Comparator& c, SkChunkAlloc& alloc) { |
| // Stage 5: Tessellate the simplified mesh into monotone polygons. |
| -Poly* tessellate(Vertex* vertices, SkChunkAlloc& alloc) { |
| +Poly* tessellate(TessellatorVertex* vertices, SkChunkAlloc& alloc) { |
| LOG("tessellating simple polygons\n"); |
| EdgeList activeEdges; |
| Poly* polys = nullptr; |
| - for (Vertex* v = vertices; v != nullptr; v = v->fNext) { |
| + for (TessellatorVertex* v = vertices; v != nullptr; v = v->fNext) { |
| if (!v->fFirstEdgeAbove && !v->fFirstEdgeBelow) { |
| continue; |
| } |
| -#if LOGGING_ENABLED |
| +#if TESSELLATOR_LOGGING_ENABLED |
| LOG("\nvertex %g: (%g,%g)\n", v->fID, v->fPoint.fX, v->fPoint.fY); |
| #endif |
| Edge* leftEnclosingEdge = nullptr; |
| @@ -1198,7 +1137,7 @@ Poly* tessellate(Vertex* vertices, SkChunkAlloc& alloc) { |
| leftPoly = leftEnclosingEdge ? leftEnclosingEdge->fRightPoly : nullptr; |
| rightPoly = rightEnclosingEdge ? rightEnclosingEdge->fLeftPoly : nullptr; |
| } |
| -#if LOGGING_ENABLED |
| +#if TESSELLATOR_LOGGING_ENABLED |
| LOG("edges above:\n"); |
| for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { |
| LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, |
| @@ -1280,7 +1219,7 @@ Poly* tessellate(Vertex* vertices, SkChunkAlloc& alloc) { |
| } |
| v->fLastEdgeBelow->fRightPoly = rightPoly; |
| } |
| -#if LOGGING_ENABLED |
| +#if TESSELLATOR_LOGGING_ENABLED |
| LOG("\nactive edges:\n"); |
| for (Edge* e = activeEdges.fHead; e != nullptr; e = e->fRight) { |
| LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, |
| @@ -1293,10 +1232,19 @@ Poly* tessellate(Vertex* vertices, SkChunkAlloc& alloc) { |
| // This is a driver function which calls stages 2-5 in turn. |
| -Poly* contours_to_polys(Vertex** contours, int contourCnt, Comparator& c, SkChunkAlloc& alloc) { |
| -#if LOGGING_ENABLED |
| +Poly* contours_to_polys(TessellatorVertex** contours, int contourCnt, SkRect pathBounds, |
| + SkChunkAlloc& alloc) { |
| + Comparator c; |
| + if (pathBounds.width() > pathBounds.height()) { |
| + c.sweep_lt = sweep_lt_horiz; |
| + c.sweep_gt = sweep_gt_horiz; |
| + } else { |
| + c.sweep_lt = sweep_lt_vert; |
| + c.sweep_gt = sweep_gt_vert; |
| + } |
| +#if TESSELLATOR_LOGGING_ENABLED |
| for (int i = 0; i < contourCnt; ++i) { |
| - Vertex* v = contours[i]; |
| + TessellatorVertex* v = contours[i]; |
| SkASSERT(v); |
| LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); |
| for (v = v->fNext; v != contours[i]; v = v->fNext) { |
| @@ -1305,7 +1253,7 @@ Poly* contours_to_polys(Vertex** contours, int contourCnt, Comparator& c, SkChun |
| } |
| #endif |
| sanitize_contours(contours, contourCnt); |
| - Vertex* vertices = build_edges(contours, contourCnt, c, alloc); |
| + TessellatorVertex* vertices = build_edges(contours, contourCnt, c, alloc); |
| if (!vertices) { |
| return nullptr; |
| } |
| @@ -1313,8 +1261,8 @@ Poly* contours_to_polys(Vertex** contours, int contourCnt, Comparator& c, SkChun |
| // Sort vertices in Y (secondarily in X). |
| merge_sort(&vertices, c); |
| merge_coincident_vertices(&vertices, c, alloc); |
| -#if LOGGING_ENABLED |
| - for (Vertex* v = vertices; v != nullptr; v = v->fNext) { |
| +#if TESSELLATOR_LOGGING_ENABLED |
| + for (TessellatorVertex* v = vertices; v != nullptr; v = v->fNext) { |
| static float gID = 0.0f; |
| v->fID = gID++; |
| } |
| @@ -1323,349 +1271,113 @@ Poly* contours_to_polys(Vertex** contours, int contourCnt, Comparator& c, SkChun |
| return tessellate(vertices, alloc); |
| } |
| -// Stage 6: Triangulate the monotone polygons into a vertex buffer. |
| - |
| -SkPoint* polys_to_triangles(Poly* polys, SkPath::FillType fillType, SkPoint* data) { |
| - SkPoint* d = data; |
| - for (Poly* poly = polys; poly; poly = poly->fNext) { |
| - if (apply_fill_type(fillType, poly->fWinding)) { |
| - d = poly->emit(d); |
| - } |
| +Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, |
| + bool* isLinear) { |
| + int contourCnt; |
| + int maxPts = GrPathUtils::worstCasePointCount(path, &contourCnt, tolerance); |
| + if (maxPts <= 0) { |
| + return nullptr; |
| } |
| - return d; |
| -} |
| - |
| -struct TessInfo { |
| - SkScalar fTolerance; |
| - int fCount; |
| -}; |
| - |
| -bool cache_match(GrVertexBuffer* vertexBuffer, SkScalar tol, int* actualCount) { |
| - if (!vertexBuffer) { |
| - return false; |
| + if (maxPts > ((int)SK_MaxU16 + 1)) { |
| + SkDebugf("Path not rendered, too many verts (%d)\n", maxPts); |
| + return nullptr; |
| } |
| - const SkData* data = vertexBuffer->getUniqueKey().getCustomData(); |
| - SkASSERT(data); |
| - const TessInfo* info = static_cast<const TessInfo*>(data->data()); |
| - if (info->fTolerance == 0 || info->fTolerance < 3.0f * tol) { |
| - *actualCount = info->fCount; |
| - return true; |
| + SkPath::FillType fillType = path.getFillType(); |
| + if (SkPath::IsInverseFillType(fillType)) { |
| + contourCnt++; |
| } |
| - return false; |
| -} |
| - |
| -}; |
| + SkAutoTDeleteArray<TessellatorVertex*> contours(new TessellatorVertex* [contourCnt]); |
| -GrTessellatingPathRenderer::GrTessellatingPathRenderer() { |
| + // For the initial size of the chunk allocator, estimate based on the point count: |
| + // one vertex per point for the initial passes, plus two for the vertices in the |
| + // resulting Polys, since the same point may end up in two Polys. Assume minimal |
| + // connectivity of one Edge per TessellatorVertex (will grow for intersections). |
| + SkChunkAlloc alloc(maxPts * (3 * sizeof(TessellatorVertex) + sizeof(Edge))); |
| + path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear); |
| + return contours_to_polys(contours.get(), contourCnt, path.getBounds(), alloc); |
| } |
| -namespace { |
| - |
| -// When the SkPathRef genID changes, invalidate a corresponding GrResource described by key. |
| -class PathInvalidator : public SkPathRef::GenIDChangeListener { |
| -public: |
| - explicit PathInvalidator(const GrUniqueKey& key) : fMsg(key) {} |
| -private: |
| - GrUniqueKeyInvalidatedMessage fMsg; |
| +// Stage 6: Triangulate the monotone polygons into a vertex buffer. |
| - void onChange() override { |
| - SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); |
| +int polys_to_triangles(Poly* polys, SkPath::FillType fillType, bool isLinear, |
| + GrResourceProvider* resourceProvider, |
| + SkAutoTUnref<GrVertexBuffer>& vertexBuffer, bool canMapVB) { |
| + int count = 0; |
| + for (Poly* poly = polys; poly; poly = poly->fNext) { |
| + if (apply_fill_type(fillType, poly->fWinding) && poly->fCount >= 3) { |
| + count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3); |
| + } |
| } |
| -}; |
| - |
| -} // namespace |
| - |
| -bool GrTessellatingPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) const { |
| - // This path renderer can draw all fill styles, all stroke styles except hairlines, but does |
| - // not do antialiasing. It can do convex and concave paths, but we'll leave the convex ones to |
| - // simpler algorithms. |
| - return !IsStrokeHairlineOrEquivalent(*args.fStroke, *args.fViewMatrix, nullptr) && |
| - !args.fAntiAlias && !args.fPath->isConvex(); |
| -} |
| - |
| -class TessellatingPathBatch : public GrVertexBatch { |
| -public: |
| - DEFINE_BATCH_CLASS_ID |
| - |
| - static GrDrawBatch* Create(const GrColor& color, |
| - const SkPath& path, |
| - const GrStrokeInfo& stroke, |
| - const SkMatrix& viewMatrix, |
| - SkRect clipBounds) { |
| - return new TessellatingPathBatch(color, path, stroke, viewMatrix, clipBounds); |
| + if (0 == count) { |
| + return 0; |
| } |
| - const char* name() const override { return "TessellatingPathBatch"; } |
| - |
| - void computePipelineOptimizations(GrInitInvariantOutput* color, |
| - GrInitInvariantOutput* coverage, |
| - GrBatchToXPOverrides* overrides) const override { |
| - color->setKnownFourComponents(fColor); |
| - coverage->setUnknownSingleComponent(); |
| - overrides->fUsePLSDstRead = false; |
| + size_t size = count * sizeof(SkPoint); |
| + if (!vertexBuffer.get() || vertexBuffer->gpuMemorySize() < size) { |
| + vertexBuffer.reset(resourceProvider->createVertexBuffer( |
| + size, GrResourceProvider::kStatic_BufferUsage, 0)); |
| } |
| - |
| -private: |
| - void initBatchTracker(const GrXPOverridesForBatch& overrides) override { |
| - // Handle any color overrides |
| - if (!overrides.readsColor()) { |
| - fColor = GrColor_ILLEGAL; |
| - } |
| - overrides.getOverrideColorIfSet(&fColor); |
| - fPipelineInfo = overrides; |
| - } |
| - |
| - int tessellate(GrUniqueKey* key, |
| - GrResourceProvider* resourceProvider, |
| - SkAutoTUnref<GrVertexBuffer>& vertexBuffer, |
| - bool canMapVB) const { |
| - SkPath path; |
| - GrStrokeInfo stroke(fStroke); |
| - if (stroke.isDashed()) { |
| - if (!stroke.applyDashToPath(&path, &stroke, fPath)) { |
| - return 0; |
| - } |
| - } else { |
| - path = fPath; |
| - } |
| - if (!stroke.isFillStyle()) { |
| - stroke.setResScale(SkScalarAbs(fViewMatrix.getMaxScale())); |
| - if (!stroke.applyToPath(&path, path)) { |
| - return 0; |
| - } |
| - stroke.setFillStyle(); |
| - } |
| - SkRect pathBounds = path.getBounds(); |
| - Comparator c; |
| - if (pathBounds.width() > pathBounds.height()) { |
| - c.sweep_lt = sweep_lt_horiz; |
| - c.sweep_gt = sweep_gt_horiz; |
| - } else { |
| - c.sweep_lt = sweep_lt_vert; |
| - c.sweep_gt = sweep_gt_vert; |
| - } |
| - SkScalar screenSpaceTol = GrPathUtils::kDefaultTolerance; |
| - SkScalar tol = GrPathUtils::scaleToleranceToSrc(screenSpaceTol, fViewMatrix, pathBounds); |
| - int contourCnt; |
| - int maxPts = GrPathUtils::worstCasePointCount(path, &contourCnt, tol); |
| - if (maxPts <= 0) { |
| - return 0; |
| - } |
| - if (maxPts > ((int)SK_MaxU16 + 1)) { |
| - SkDebugf("Path not rendered, too many verts (%d)\n", maxPts); |
| - return 0; |
| - } |
| - SkPath::FillType fillType = path.getFillType(); |
| - if (SkPath::IsInverseFillType(fillType)) { |
| - contourCnt++; |
| - } |
| - |
| - LOG("got %d pts, %d contours\n", maxPts, contourCnt); |
| - SkAutoTDeleteArray<Vertex*> contours(new Vertex* [contourCnt]); |
| - |
| - // For the initial size of the chunk allocator, estimate based on the point count: |
| - // one vertex per point for the initial passes, plus two for the vertices in the |
| - // resulting Polys, since the same point may end up in two Polys. Assume minimal |
| - // connectivity of one Edge per Vertex (will grow for intersections). |
| - SkChunkAlloc alloc(maxPts * (3 * sizeof(Vertex) + sizeof(Edge))); |
| - bool isLinear; |
| - path_to_contours(path, tol, fClipBounds, contours.get(), alloc, &isLinear); |
| - Poly* polys; |
| - polys = contours_to_polys(contours.get(), contourCnt, c, alloc); |
| - int count = 0; |
| - for (Poly* poly = polys; poly; poly = poly->fNext) { |
| - if (apply_fill_type(fillType, poly->fWinding) && poly->fCount >= 3) { |
| - count += (poly->fCount - 2) * (WIREFRAME ? 6 : 3); |
| - } |
| - } |
| - if (0 == count) { |
| - return 0; |
| - } |
| - |
| - size_t size = count * sizeof(SkPoint); |
| - if (!vertexBuffer.get() || vertexBuffer->gpuMemorySize() < size) { |
| - vertexBuffer.reset(resourceProvider->createVertexBuffer( |
| - size, GrResourceProvider::kStatic_BufferUsage, 0)); |
| - } |
| - if (!vertexBuffer.get()) { |
| - SkDebugf("Could not allocate vertices\n"); |
| - return 0; |
| - } |
| - SkPoint* verts; |
| - if (canMapVB) { |
| - verts = static_cast<SkPoint*>(vertexBuffer->map()); |
| - } else { |
| - verts = new SkPoint[count]; |
| - } |
| - SkPoint* end = polys_to_triangles(polys, fillType, verts); |
| - int actualCount = static_cast<int>(end - verts); |
| - LOG("actual count: %d\n", actualCount); |
| - SkASSERT(actualCount <= count); |
| - if (canMapVB) { |
| - vertexBuffer->unmap(); |
| - } else { |
| - vertexBuffer->updateData(verts, actualCount * sizeof(SkPoint)); |
| - delete[] verts; |
| - } |
| - |
| - |
| - if (!fPath.isVolatile()) { |
| - TessInfo info; |
| - info.fTolerance = isLinear ? 0 : tol; |
| - info.fCount = actualCount; |
| - SkAutoTUnref<SkData> data(SkData::NewWithCopy(&info, sizeof(info))); |
| - key->setCustomData(data.get()); |
| - resourceProvider->assignUniqueKeyToResource(*key, vertexBuffer.get()); |
| - SkPathPriv::AddGenIDChangeListener(fPath, new PathInvalidator(*key)); |
| - } |
| - return actualCount; |
| - } |
| - |
| - void onPrepareDraws(Target* target) const override { |
| - // construct a cache key from the path's genID and the view matrix |
| - static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain(); |
| - GrUniqueKey key; |
| - int clipBoundsSize32 = |
| - fPath.isInverseFillType() ? sizeof(fClipBounds) / sizeof(uint32_t) : 0; |
| - int strokeDataSize32 = fStroke.computeUniqueKeyFragmentData32Cnt(); |
| - GrUniqueKey::Builder builder(&key, kDomain, 2 + clipBoundsSize32 + strokeDataSize32); |
| - builder[0] = fPath.getGenerationID(); |
| - builder[1] = fPath.getFillType(); |
| - // For inverse fills, the tessellation is dependent on clip bounds. |
| - if (fPath.isInverseFillType()) { |
| - memcpy(&builder[2], &fClipBounds, sizeof(fClipBounds)); |
| - } |
| - fStroke.asUniqueKeyFragment(&builder[2 + clipBoundsSize32]); |
| - builder.finish(); |
| - GrResourceProvider* rp = target->resourceProvider(); |
| - SkAutoTUnref<GrVertexBuffer> vertexBuffer(rp->findAndRefTByUniqueKey<GrVertexBuffer>(key)); |
| - int actualCount; |
| - SkScalar screenSpaceTol = GrPathUtils::kDefaultTolerance; |
| - SkScalar tol = GrPathUtils::scaleToleranceToSrc( |
| - screenSpaceTol, fViewMatrix, fPath.getBounds()); |
| - if (!cache_match(vertexBuffer.get(), tol, &actualCount)) { |
| - bool canMapVB = GrCaps::kNone_MapFlags != target->caps().mapBufferFlags(); |
| - actualCount = this->tessellate(&key, rp, vertexBuffer, canMapVB); |
| - } |
| - |
| - if (actualCount == 0) { |
| - return; |
| - } |
| - |
| - SkAutoTUnref<const GrGeometryProcessor> gp; |
| - { |
| - using namespace GrDefaultGeoProcFactory; |
| - |
| - Color color(fColor); |
| - LocalCoords localCoords(fPipelineInfo.readsLocalCoords() ? |
| - LocalCoords::kUsePosition_Type : |
| - LocalCoords::kUnused_Type); |
| - Coverage::Type coverageType; |
| - if (fPipelineInfo.readsCoverage()) { |
| - coverageType = Coverage::kSolid_Type; |
| - } else { |
| - coverageType = Coverage::kNone_Type; |
| - } |
| - Coverage coverage(coverageType); |
| - gp.reset(GrDefaultGeoProcFactory::Create(color, coverage, localCoords, |
| - fViewMatrix)); |
| - } |
| - |
| - target->initDraw(gp, this->pipeline()); |
| - SkASSERT(gp->getVertexStride() == sizeof(SkPoint)); |
| - |
| - GrPrimitiveType primitiveType = WIREFRAME ? kLines_GrPrimitiveType |
| - : kTriangles_GrPrimitiveType; |
| - GrVertices vertices; |
| - vertices.init(primitiveType, vertexBuffer.get(), 0, actualCount); |
| - target->draw(vertices); |
| - } |
| - |
| - bool onCombineIfPossible(GrBatch*, const GrCaps&) override { return false; } |
| - |
| - TessellatingPathBatch(const GrColor& color, |
| - const SkPath& path, |
| - const GrStrokeInfo& stroke, |
| - const SkMatrix& viewMatrix, |
| - const SkRect& clipBounds) |
| - : INHERITED(ClassID()) |
| - , fColor(color) |
| - , fPath(path) |
| - , fStroke(stroke) |
| - , fViewMatrix(viewMatrix) { |
| - const SkRect& pathBounds = path.getBounds(); |
| - fClipBounds = clipBounds; |
| - // Because the clip bounds are used to add a contour for inverse fills, they must also |
| - // include the path bounds. |
| - fClipBounds.join(pathBounds); |
| - if (path.isInverseFillType()) { |
| - fBounds = fClipBounds; |
| - } else { |
| - fBounds = path.getBounds(); |
| - } |
| - if (!stroke.isFillStyle()) { |
| - SkScalar radius = SkScalarHalf(stroke.getWidth()); |
| - if (stroke.getJoin() == SkPaint::kMiter_Join) { |
| - SkScalar scale = stroke.getMiter(); |
| - if (scale > SK_Scalar1) { |
| - radius = SkScalarMul(radius, scale); |
| - } |
| - } |
| - fBounds.outset(radius, radius); |
| - } |
| - viewMatrix.mapRect(&fBounds); |
| + if (!vertexBuffer.get()) { |
| + SkDebugf("Could not allocate vertices\n"); |
| + return 0; |
| } |
| - |
| - GrColor fColor; |
| - SkPath fPath; |
| - GrStrokeInfo fStroke; |
| - SkMatrix fViewMatrix; |
| - SkRect fClipBounds; // in source space |
| - GrXPOverridesForBatch fPipelineInfo; |
| - |
| - typedef GrVertexBatch INHERITED; |
| -}; |
| - |
| -bool GrTessellatingPathRenderer::onDrawPath(const DrawPathArgs& args) { |
| - SkASSERT(!args.fAntiAlias); |
| - const GrRenderTarget* rt = args.fPipelineBuilder->getRenderTarget(); |
| - if (nullptr == rt) { |
| - return false; |
| + SkPoint* verts; |
| + if (canMapVB) { |
| + verts = static_cast<SkPoint*>(vertexBuffer->map()); |
| + } else { |
| + verts = new SkPoint[count]; |
| } |
| - |
| - SkIRect clipBoundsI; |
| - args.fPipelineBuilder->clip().getConservativeBounds(rt->width(), rt->height(), &clipBoundsI); |
| - SkRect clipBounds = SkRect::Make(clipBoundsI); |
| - SkMatrix vmi; |
| - if (!args.fViewMatrix->invert(&vmi)) { |
| - return false; |
| + SkPoint* end = verts; |
| + for (Poly* poly = polys; poly; poly = poly->fNext) { |
| + if (apply_fill_type(fillType, poly->fWinding)) { |
| + end = poly->emit(end); |
| + } |
| + } |
| + int actualCount = static_cast<int>(end - verts); |
| + LOG("actual count: %d\n", actualCount); |
| + SkASSERT(actualCount <= count); |
| + if (canMapVB) { |
| + vertexBuffer->unmap(); |
| + } else { |
| + vertexBuffer->updateData(verts, actualCount * sizeof(SkPoint)); |
| + delete[] verts; |
| } |
| - vmi.mapRect(&clipBounds); |
| - SkAutoTUnref<GrDrawBatch> batch(TessellatingPathBatch::Create(args.fColor, *args.fPath, |
| - *args.fStroke, *args.fViewMatrix, |
| - clipBounds)); |
| - args.fTarget->drawBatch(*args.fPipelineBuilder, batch); |
| - return true; |
| + return actualCount; |
| } |
| -/////////////////////////////////////////////////////////////////////////////////////////////////// |
| - |
| -#ifdef GR_TEST_UTILS |
| +int polys_to_vertices(Poly* polys, SkPath::FillType fillType, bool isLinear, |
| + WindingVertex** verts) { |
| + int count = 0; |
| + for (Poly* poly = polys; poly; poly = poly->fNext) { |
| + if (apply_fill_type(fillType, poly->fWinding) && poly->fCount >= 3) { |
| + count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3); |
| + } |
| + } |
| + if (0 == count) { |
| + *verts = nullptr; |
| + return 0; |
| + } |
| -DRAW_BATCH_TEST_DEFINE(TesselatingPathBatch) { |
| - GrColor color = GrRandomColor(random); |
| - SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); |
| - SkPath path = GrTest::TestPath(random); |
| - SkRect clipBounds = GrTest::TestRect(random); |
| - SkMatrix vmi; |
| - bool result = viewMatrix.invert(&vmi); |
| - if (!result) { |
| - SkFAIL("Cannot invert matrix\n"); |
| + *verts = new WindingVertex[count]; |
| + WindingVertex* vertsEnd = *verts; |
| + SkPoint* points = new SkPoint[count]; |
| + SkPoint* pointsEnd = points; |
| + for (Poly* poly = polys; poly; poly = poly->fNext) { |
| + if (apply_fill_type(fillType, poly->fWinding)) { |
| + SkPoint* start = pointsEnd; |
| + pointsEnd = poly->emit(pointsEnd); |
| + while (start != pointsEnd) { |
| + vertsEnd->fPos = *start; |
| + vertsEnd->fWinding = poly->fWinding; |
| + ++start; |
| + ++vertsEnd; |
| + } |
| + } |
| } |
| - vmi.mapRect(&clipBounds); |
| - GrStrokeInfo strokeInfo = GrTest::TestStrokeInfo(random); |
| - return TessellatingPathBatch::Create(color, path, strokeInfo, viewMatrix, clipBounds); |
| + int actualCount = static_cast<int>(vertsEnd - *verts); |
| + SkASSERT(actualCount <= count); |
| + SkASSERT(pointsEnd - points == actualCount); |
| + delete[] points; |
| + return actualCount; |
| } |
| - |
| -#endif |