Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(261)

Unified Diff: src/gpu/GrTessellator.cpp

Issue 1557083002: Broke GrTessellatingPathRenderer's tessellator out into a separate file. (Closed) Base URL: https://skia.googlesource.com/skia.git@master
Patch Set: Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « src/gpu/GrTessellator.h ('k') | src/gpu/batches/GrTessellatingPathRenderer.cpp » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: src/gpu/GrTessellator.cpp
diff --git a/src/gpu/batches/GrTessellatingPathRenderer.cpp b/src/gpu/GrTessellator.cpp
similarity index 79%
copy from src/gpu/batches/GrTessellatingPathRenderer.cpp
copy to src/gpu/GrTessellator.cpp
index 27e287e9c6bceb6b48c7e500ef338fcc7b259775..ea130cf15fec269f741998cd9077d0694db61720 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,18 +14,14 @@
#include "GrVertices.h"
#include "GrResourceCache.h"
#include "GrResourceProvider.h"
-#include "SkChunkAlloc.h"
#include "SkGeometry.h"
+#include "SkChunkAlloc.h"
#include "batches/GrVertexBatch.h"
#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 +76,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
@@ -541,7 +537,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];
@@ -1293,7 +1288,16 @@ 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, const 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 +1327,144 @@ 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,
+ int contourCnt, SkChunkAlloc& alloc, bool* isLinear) {
+ SkPath::FillType fillType = path.getFillType();
+ if (SkPath::IsInverseFillType(fillType)) {
+ contourCnt++;
}
- return d;
-}
+ SkAutoTDeleteArray<Vertex*> contours(new Vertex* [contourCnt]);
-struct TessInfo {
- SkScalar fTolerance;
- int fCount;
-};
+ path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinear);
+ return contours_to_polys(contours.get(), contourCnt, path.getBounds(), alloc);
+}
-bool cache_match(GrVertexBuffer* vertexBuffer, SkScalar tol, int* actualCount) {
- if (!vertexBuffer) {
- return false;
+void get_contour_count_and_size_estimate(const SkPath& path, SkScalar tolerance, int* contourCnt,
+ int* sizeEstimate) {
+ int maxPts = GrPathUtils::worstCasePointCount(path, contourCnt, tolerance);
+ if (maxPts <= 0) {
+ *contourCnt = 0;
+ return;
}
- 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;
+ if (maxPts > ((int)SK_MaxU16 + 1)) {
+ SkDebugf("Path not rendered, too many verts (%d)\n", maxPts);
+ *contourCnt = 0;
+ return;
}
- return false;
-}
-
-};
-
-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).
+ *sizeEstimate = maxPts * (3 * sizeof(Vertex) + sizeof(Edge));
}
-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;
-
- void onChange() override {
- SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg);
+int count_points(Poly* polys, SkPath::FillType fillType) {
+ 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();
+ return count;
}
-class TessellatingPathBatch : public GrVertexBatch {
-public:
- DEFINE_BATCH_CLASS_ID
+} // namespace
- 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);
- }
+namespace GrTessellator {
- const char* name() const override { return "TessellatingPathBatch"; }
+// Stage 6: Triangulate the monotone polygons into a vertex buffer.
- void computePipelineOptimizations(GrInitInvariantOutput* color,
- GrInitInvariantOutput* coverage,
- GrBatchToXPOverrides* overrides) const override {
- color->setKnownFourComponents(fColor);
- coverage->setUnknownSingleComponent();
- overrides->fUsePLSDstRead = false;
+int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
+ GrResourceProvider* resourceProvider,
+ SkAutoTUnref<GrVertexBuffer>& vertexBuffer, bool canMapVB, bool* isLinear) {
+ int contourCnt;
+ int sizeEstimate;
+ get_contour_count_and_size_estimate(path, tolerance, &contourCnt, &sizeEstimate);
+ if (contourCnt <= 0) {
+ *isLinear = true;
+ return 0;
+ }
+ SkChunkAlloc alloc(sizeEstimate);
+ Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, isLinear);
+ SkPath::FillType fillType = path.getFillType();
+ int count = count_points(polys, fillType);
+ 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];
}
-
-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);
+ SkPoint* end = verts;
+ for (Poly* poly = polys; poly; poly = poly->fNext) {
+ if (apply_fill_type(fillType, poly->fWinding)) {
+ end = poly->emit(end);
}
- 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;
}
-
- 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;
+ 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
-
-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");
+int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBounds,
+ GrTessellator::WindingVertex** verts) {
+ int contourCnt;
+ int sizeEstimate;
+ get_contour_count_and_size_estimate(path, tolerance, &contourCnt, &sizeEstimate);
+ if (contourCnt <= 0) {
+ return 0;
+ }
+ SkChunkAlloc alloc(sizeEstimate);
+ bool isLinear;
+ Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, &isLinear);
+ SkPath::FillType fillType = path.getFillType();
+ int count = count_points(polys, fillType);
+ if (0 == count) {
+ *verts = nullptr;
+ return 0;
+ }
+
+ *verts = new GrTessellator::WindingVertex[count];
+ GrTessellator::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
+} // namespace
« no previous file with comments | « src/gpu/GrTessellator.h ('k') | src/gpu/batches/GrTessellatingPathRenderer.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698