Chromium Code Reviews| Index: src/gpu/GrTessellator.cpp |
| diff --git a/src/gpu/batches/GrTessellatingPathRenderer.cpp b/src/gpu/GrTessellator.cpp |
| similarity index 73% |
| copy from src/gpu/batches/GrTessellatingPathRenderer.cpp |
| copy to src/gpu/GrTessellator.cpp |
| index 27e287e9c6bceb6b48c7e500ef338fcc7b259775..df8b5eeed2442e95c76569ab9ec6d51c974ee58a 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,8 +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 |
| -#define WIREFRAME 0 |
| #if LOGGING_ENABLED |
| #define LOG printf |
| @@ -91,10 +86,140 @@ |
| #define ALLOC_NEW(Type, args, alloc) new (alloc.allocThrow(sizeof(Type))) Type args |
| -namespace { |
| +namespace GrTessellator { |
|
Stephen White
2016/01/05 16:36:22
Now that they're no longer public, can these thing
|
| +struct Poly; |
| +struct EdgeList; |
| struct Vertex; |
| -struct Edge; |
| + |
| +struct Edge { |
| + Edge(Vertex* top, Vertex* bottom, int winding) |
| + : fTop(top) |
| + , fBottom(bottom) |
| + , fWinding(winding) |
| + , fLeft(nullptr) |
| + , fRight(nullptr) |
| + , fPrevEdgeAbove(nullptr) |
| + , fNextEdgeAbove(nullptr) |
| + , fPrevEdgeBelow(nullptr) |
| + , fNextEdgeBelow(nullptr) |
| + , fLeftPoly(nullptr) |
| + , fRightPoly(nullptr) { |
| + recompute(); |
| + } |
| + Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt). |
| + Vertex* fBottom; // The bottom vertex in vertex-sort-order. |
| + int fWinding; // 1 == edge goes downward; -1 = edge goes upward. |
| + 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; |
| + bool isRightOf(Vertex* v) const; |
| + bool isLeftOf(Vertex* v) const; |
| + void recompute(); |
| + bool intersect(const Edge& other, SkPoint* p); |
| + bool isActive(EdgeList* activeEdges) const; |
| +}; |
| + |
| +struct EdgeList { |
| + EdgeList() : fHead(nullptr), fTail(nullptr) {} |
| + Edge* fHead; |
| + Edge* fTail; |
| +}; |
| + |
| +/** |
| + * 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 |
| +}; |
| + |
| +double Edge::dist(const SkPoint& p) const { |
| + return fDY * p.fX - fDX * p.fY + fC; |
| +} |
| + |
| +bool Edge::isRightOf(Vertex* v) const { |
| + return dist(v->fPoint) < 0.0; |
| +} |
| + |
| +bool Edge::isLeftOf(Vertex* 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 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 Poly; |
| template <class T, T* T::*Prev, T* T::*Next> |
| @@ -128,40 +253,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); |
| @@ -193,7 +284,7 @@ inline SkPoint* emit_vertex(Vertex* v, SkPoint* data) { |
| } |
| SkPoint* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, SkPoint* data) { |
| -#if WIREFRAME |
| +#if TESSELLATOR_WIREFRAME |
| data = emit_vertex(v0, data); |
| data = emit_vertex(v1, data); |
| data = emit_vertex(v1, data); |
| @@ -208,12 +299,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 +317,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 { |
| @@ -361,7 +370,7 @@ struct Poly { |
| return done; |
| } |
| - SkPoint* emit(SkPoint* data) { |
| + SkPoint* emit(int winding, SkPoint* data) { |
| Vertex* first = fHead; |
| Vertex* v = first->fNext; |
| while (v != fTail) { |
| @@ -435,7 +444,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; |
| } |
| @@ -465,8 +474,8 @@ 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* append_point_to_contour(const SkPoint& p, Vertex* prev, |
| + Vertex** head, SkChunkAlloc& alloc) { |
| Vertex* v = ALLOC_NEW(Vertex, (p), alloc); |
| #if LOGGING_ENABLED |
| static float gID = 0.0f; |
| @@ -541,7 +550,6 @@ Vertex* generate_cubic_points(const SkPoint& p0, |
| void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, |
| Vertex** contours, SkChunkAlloc& alloc, bool *isLinear) { |
| - |
| SkScalar toleranceSqd = tolerance * tolerance; |
| SkPoint pts[4]; |
| @@ -850,7 +858,8 @@ 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, Vertex* v, EdgeList* activeEdges, Comparator& c, |
| + SkChunkAlloc& alloc); |
| void cleanup_active_edges(Edge* edge, EdgeList* activeEdges, Comparator& c, SkChunkAlloc& alloc) { |
| Vertex* top = edge->fTop; |
| @@ -921,7 +930,7 @@ void merge_vertices(Vertex* src, Vertex* dst, Vertex** head, Comparator& c, SkCh |
| remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(src, head, nullptr); |
| } |
| -Vertex* check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c, |
| +Vertex* check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, Comparator& c, |
| SkChunkAlloc& alloc) { |
| SkPoint p; |
| if (!edge || !other) { |
| @@ -1151,7 +1160,7 @@ void simplify(Vertex* vertices, Comparator& c, SkChunkAlloc& alloc) { |
| } |
| } |
| } else { |
| - if (Vertex* pv = check_for_intersection(leftEnclosingEdge, rightEnclosingEdge, |
| + if (Vertex* pv = check_for_intersection(leftEnclosingEdge, rightEnclosingEdge, |
| &activeEdges, c, alloc)) { |
| if (c.sweep_lt(pv->fPoint, v->fPoint)) { |
| v = pv; |
| @@ -1293,7 +1302,15 @@ 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) { |
| +Poly* contours_to_polys(Vertex** 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 LOGGING_ENABLED |
| for (int i = 0; i < contourCnt; ++i) { |
| Vertex* v = contours[i]; |
| @@ -1323,349 +1340,120 @@ 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<Vertex*> contours(new Vertex* [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 Vertex (will grow for intersections). |
| + SkChunkAlloc alloc(maxPts * (3 * sizeof(Vertex) + sizeof(Edge))); |
| + path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear); |
| + return contours_to_polys(contours.get(), contourCnt, path.getBounds(), alloc); |
|
Stephen White
2016/01/05 16:36:22
We're now returning a pointer to Polys which were
|
| } |
| -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 PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, |
| + GrResourceProvider* resourceProvider, |
| + SkAutoTUnref<GrVertexBuffer>& vertexBuffer, bool canMapVB, bool* isLinear) { |
| + Poly* polys = path_to_polys(path, tolerance, clipBounds, isLinear); |
| + SkPath::FillType fillType = path.getFillType(); |
| + 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; |
| + 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 = 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; |
| + } |
| - 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)); |
| - } |
| + return actualCount; |
| +} |
| - 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); |
| +int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds, |
| + WindingVertex** verts) { |
| + bool isLinear; |
| + Poly* polys = path_to_polys(path, tolerance, clipBounds, &isLinear); |
| + SkPath::FillType fillType = path.getFillType(); |
| + 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); |
| } |
| - viewMatrix.mapRect(&fBounds); |
| } |
| - |
| - 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; |
| + if (0 == count) { |
| + *verts = nullptr; |
| + return 0; |
| } |
| - 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; |
| + *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); |
| - SkAutoTUnref<GrDrawBatch> batch(TessellatingPathBatch::Create(args.fColor, *args.fPath, |
| - *args.fStroke, *args.fViewMatrix, |
| - clipBounds)); |
| - args.fTarget->drawBatch(*args.fPipelineBuilder, batch); |
| - |
| - return true; |
| + int actualCount = static_cast<int>(vertsEnd - *verts); |
| + SkASSERT(actualCount <= count); |
| + SkASSERT(pointsEnd - points == actualCount); |
| + delete[] points; |
| + return actualCount; |
| } |
| -/////////////////////////////////////////////////////////////////////////////////////////////////// |
| - |
| -#ifdef GR_TEST_UTILS |
| - |
| -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"); |
| - } |
| - vmi.mapRect(&clipBounds); |
| - GrStrokeInfo strokeInfo = GrTest::TestStrokeInfo(random); |
| - return TessellatingPathBatch::Create(color, path, strokeInfo, viewMatrix, clipBounds); |
| } |
| - |
| -#endif |