OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2015 Google Inc. | 2 * Copyright 2015 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "GrTessellatingPathRenderer.h" | 8 #include "GrTessellator.h" |
9 | 9 |
10 #include "GrBatchFlushState.h" | 10 #include "GrBatchFlushState.h" |
11 #include "GrBatchTest.h" | 11 #include "GrBatchTest.h" |
12 #include "GrDefaultGeoProcFactory.h" | 12 #include "GrDefaultGeoProcFactory.h" |
13 #include "GrPathUtils.h" | 13 #include "GrPathUtils.h" |
14 #include "GrVertices.h" | 14 #include "GrVertices.h" |
15 #include "GrResourceCache.h" | 15 #include "GrResourceCache.h" |
16 #include "GrResourceProvider.h" | 16 #include "GrResourceProvider.h" |
| 17 #include "SkGeometry.h" |
17 #include "SkChunkAlloc.h" | 18 #include "SkChunkAlloc.h" |
18 #include "SkGeometry.h" | |
19 | 19 |
20 #include "batches/GrVertexBatch.h" | 20 #include "batches/GrVertexBatch.h" |
21 | 21 |
22 #include <stdio.h> | 22 #include <stdio.h> |
23 | 23 |
24 /* | 24 /* |
25 * This path renderer tessellates the path into triangles, uploads the triangles
to a | |
26 * vertex buffer, and renders them with a single draw call. It does not currentl
y do | |
27 * antialiasing, so it must be used in conjunction with multisampling. | |
28 * | |
29 * There are six stages to the algorithm: | 25 * There are six stages to the algorithm: |
30 * | 26 * |
31 * 1) Linearize the path contours into piecewise linear segments (path_to_contou
rs()). | 27 * 1) Linearize the path contours into piecewise linear segments (path_to_contou
rs()). |
32 * 2) Build a mesh of edges connecting the vertices (build_edges()). | 28 * 2) Build a mesh of edges connecting the vertices (build_edges()). |
33 * 3) Sort the vertices in Y (and secondarily in X) (merge_sort()). | 29 * 3) Sort the vertices in Y (and secondarily in X) (merge_sort()). |
34 * 4) Simplify the mesh by inserting new vertices at intersecting edges (simplif
y()). | 30 * 4) Simplify the mesh by inserting new vertices at intersecting edges (simplif
y()). |
35 * 5) Tessellate the simplified mesh into monotone polygons (tessellate()). | 31 * 5) Tessellate the simplified mesh into monotone polygons (tessellate()). |
36 * 6) Triangulate the monotone polygons directly into a vertex buffer (polys_to_
triangles()). | 32 * 6) Triangulate the monotone polygons directly into a vertex buffer (polys_to_
triangles()). |
37 * | 33 * |
38 * The vertex sorting in step (3) is a merge sort, since it plays well with the
linked list | 34 * The vertex sorting in step (3) is a merge sort, since it plays well with the
linked list |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 * frequent. There may be other data structures worth investigating, however. | 69 * frequent. There may be other data structures worth investigating, however. |
74 * | 70 * |
75 * Note that the orientation of the line sweep algorithms is determined by the a
spect ratio of the | 71 * Note that the orientation of the line sweep algorithms is determined by the a
spect ratio of the |
76 * path bounds. When the path is taller than it is wide, we sort vertices based
on increasing Y | 72 * path bounds. When the path is taller than it is wide, we sort vertices based
on increasing Y |
77 * coordinate, and secondarily by increasing X coordinate. When the path is wide
r than it is tall, | 73 * coordinate, and secondarily by increasing X coordinate. When the path is wide
r than it is tall, |
78 * we sort by increasing X coordinate, but secondarily by *decreasing* Y coordin
ate. This is so | 74 * we sort by increasing X coordinate, but secondarily by *decreasing* Y coordin
ate. This is so |
79 * that the "left" and "right" orientation in the code remains correct (edges to
the left are | 75 * that the "left" and "right" orientation in the code remains correct (edges to
the left are |
80 * increasing in Y; edges to the right are decreasing in Y). That is, the settin
g rotates 90 | 76 * increasing in Y; edges to the right are decreasing in Y). That is, the settin
g rotates 90 |
81 * degrees counterclockwise, rather that transposing. | 77 * degrees counterclockwise, rather that transposing. |
82 */ | 78 */ |
| 79 |
83 #define LOGGING_ENABLED 0 | 80 #define LOGGING_ENABLED 0 |
84 #define WIREFRAME 0 | |
85 | 81 |
86 #if LOGGING_ENABLED | 82 #if LOGGING_ENABLED |
87 #define LOG printf | 83 #define LOG printf |
88 #else | 84 #else |
89 #define LOG(...) | 85 #define LOG(...) |
90 #endif | 86 #endif |
91 | 87 |
92 #define ALLOC_NEW(Type, args, alloc) new (alloc.allocThrow(sizeof(Type))) Type a
rgs | 88 #define ALLOC_NEW(Type, args, alloc) new (alloc.allocThrow(sizeof(Type))) Type a
rgs |
93 | 89 |
94 namespace { | 90 namespace { |
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
534 pointsLeft >>= 1; | 530 pointsLeft >>= 1; |
535 prev = generate_cubic_points(p0, q[0], r[0], s, tolSqd, prev, head, pointsLe
ft, alloc); | 531 prev = generate_cubic_points(p0, q[0], r[0], s, tolSqd, prev, head, pointsLe
ft, alloc); |
536 prev = generate_cubic_points(s, r[1], q[2], p3, tolSqd, prev, head, pointsLe
ft, alloc); | 532 prev = generate_cubic_points(s, r[1], q[2], p3, tolSqd, prev, head, pointsLe
ft, alloc); |
537 return prev; | 533 return prev; |
538 } | 534 } |
539 | 535 |
540 // Stage 1: convert the input path to a set of linear contours (linked list of V
ertices). | 536 // Stage 1: convert the input path to a set of linear contours (linked list of V
ertices). |
541 | 537 |
542 void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clip
Bounds, | 538 void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clip
Bounds, |
543 Vertex** contours, SkChunkAlloc& alloc, bool *isLinear) { | 539 Vertex** contours, SkChunkAlloc& alloc, bool *isLinear) { |
544 | |
545 SkScalar toleranceSqd = tolerance * tolerance; | 540 SkScalar toleranceSqd = tolerance * tolerance; |
546 | 541 |
547 SkPoint pts[4]; | 542 SkPoint pts[4]; |
548 bool done = false; | 543 bool done = false; |
549 *isLinear = true; | 544 *isLinear = true; |
550 SkPath::Iter iter(path, false); | 545 SkPath::Iter iter(path, false); |
551 Vertex* prev = nullptr; | 546 Vertex* prev = nullptr; |
552 Vertex* head = nullptr; | 547 Vertex* head = nullptr; |
553 if (path.isInverseFillType()) { | 548 if (path.isInverseFillType()) { |
554 SkPoint quad[4]; | 549 SkPoint quad[4]; |
(...skipping 731 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1286 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, | 1281 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, |
1287 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRight
Poly->fID : -1); | 1282 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRight
Poly->fID : -1); |
1288 } | 1283 } |
1289 #endif | 1284 #endif |
1290 } | 1285 } |
1291 return polys; | 1286 return polys; |
1292 } | 1287 } |
1293 | 1288 |
1294 // This is a driver function which calls stages 2-5 in turn. | 1289 // This is a driver function which calls stages 2-5 in turn. |
1295 | 1290 |
1296 Poly* contours_to_polys(Vertex** contours, int contourCnt, Comparator& c, SkChun
kAlloc& alloc) { | 1291 Poly* contours_to_polys(Vertex** contours, int contourCnt, const SkRect& pathBou
nds, |
| 1292 SkChunkAlloc& alloc) { |
| 1293 Comparator c; |
| 1294 if (pathBounds.width() > pathBounds.height()) { |
| 1295 c.sweep_lt = sweep_lt_horiz; |
| 1296 c.sweep_gt = sweep_gt_horiz; |
| 1297 } else { |
| 1298 c.sweep_lt = sweep_lt_vert; |
| 1299 c.sweep_gt = sweep_gt_vert; |
| 1300 } |
1297 #if LOGGING_ENABLED | 1301 #if LOGGING_ENABLED |
1298 for (int i = 0; i < contourCnt; ++i) { | 1302 for (int i = 0; i < contourCnt; ++i) { |
1299 Vertex* v = contours[i]; | 1303 Vertex* v = contours[i]; |
1300 SkASSERT(v); | 1304 SkASSERT(v); |
1301 LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); | 1305 LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); |
1302 for (v = v->fNext; v != contours[i]; v = v->fNext) { | 1306 for (v = v->fNext; v != contours[i]; v = v->fNext) { |
1303 LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); | 1307 LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); |
1304 } | 1308 } |
1305 } | 1309 } |
1306 #endif | 1310 #endif |
1307 sanitize_contours(contours, contourCnt); | 1311 sanitize_contours(contours, contourCnt); |
1308 Vertex* vertices = build_edges(contours, contourCnt, c, alloc); | 1312 Vertex* vertices = build_edges(contours, contourCnt, c, alloc); |
1309 if (!vertices) { | 1313 if (!vertices) { |
1310 return nullptr; | 1314 return nullptr; |
1311 } | 1315 } |
1312 | 1316 |
1313 // Sort vertices in Y (secondarily in X). | 1317 // Sort vertices in Y (secondarily in X). |
1314 merge_sort(&vertices, c); | 1318 merge_sort(&vertices, c); |
1315 merge_coincident_vertices(&vertices, c, alloc); | 1319 merge_coincident_vertices(&vertices, c, alloc); |
1316 #if LOGGING_ENABLED | 1320 #if LOGGING_ENABLED |
1317 for (Vertex* v = vertices; v != nullptr; v = v->fNext) { | 1321 for (Vertex* v = vertices; v != nullptr; v = v->fNext) { |
1318 static float gID = 0.0f; | 1322 static float gID = 0.0f; |
1319 v->fID = gID++; | 1323 v->fID = gID++; |
1320 } | 1324 } |
1321 #endif | 1325 #endif |
1322 simplify(vertices, c, alloc); | 1326 simplify(vertices, c, alloc); |
1323 return tessellate(vertices, alloc); | 1327 return tessellate(vertices, alloc); |
1324 } | 1328 } |
1325 | 1329 |
| 1330 Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBo
unds, |
| 1331 int contourCnt, SkChunkAlloc& alloc, bool* isLinear) { |
| 1332 SkPath::FillType fillType = path.getFillType(); |
| 1333 if (SkPath::IsInverseFillType(fillType)) { |
| 1334 contourCnt++; |
| 1335 } |
| 1336 SkAutoTDeleteArray<Vertex*> contours(new Vertex* [contourCnt]); |
| 1337 |
| 1338 path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinea
r); |
| 1339 return contours_to_polys(contours.get(), contourCnt, path.getBounds(), alloc
); |
| 1340 } |
| 1341 |
| 1342 void get_contour_count_and_size_estimate(const SkPath& path, SkScalar tolerance,
int* contourCnt, |
| 1343 int* sizeEstimate) { |
| 1344 int maxPts = GrPathUtils::worstCasePointCount(path, contourCnt, tolerance); |
| 1345 if (maxPts <= 0) { |
| 1346 *contourCnt = 0; |
| 1347 return; |
| 1348 } |
| 1349 if (maxPts > ((int)SK_MaxU16 + 1)) { |
| 1350 SkDebugf("Path not rendered, too many verts (%d)\n", maxPts); |
| 1351 *contourCnt = 0; |
| 1352 return; |
| 1353 } |
| 1354 // For the initial size of the chunk allocator, estimate based on the point
count: |
| 1355 // one vertex per point for the initial passes, plus two for the vertices in
the |
| 1356 // resulting Polys, since the same point may end up in two Polys. Assume mi
nimal |
| 1357 // connectivity of one Edge per Vertex (will grow for intersections). |
| 1358 *sizeEstimate = maxPts * (3 * sizeof(Vertex) + sizeof(Edge)); |
| 1359 } |
| 1360 |
| 1361 int count_points(Poly* polys, SkPath::FillType fillType) { |
| 1362 int count = 0; |
| 1363 for (Poly* poly = polys; poly; poly = poly->fNext) { |
| 1364 if (apply_fill_type(fillType, poly->fWinding) && poly->fCount >= 3) { |
| 1365 count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3); |
| 1366 } |
| 1367 } |
| 1368 return count; |
| 1369 } |
| 1370 |
| 1371 } // namespace |
| 1372 |
| 1373 namespace GrTessellator { |
| 1374 |
1326 // Stage 6: Triangulate the monotone polygons into a vertex buffer. | 1375 // Stage 6: Triangulate the monotone polygons into a vertex buffer. |
1327 | 1376 |
1328 SkPoint* polys_to_triangles(Poly* polys, SkPath::FillType fillType, SkPoint* dat
a) { | 1377 int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBo
unds, |
1329 SkPoint* d = data; | 1378 GrResourceProvider* resourceProvider, |
| 1379 SkAutoTUnref<GrVertexBuffer>& vertexBuffer, bool canMapVB, b
ool* isLinear) { |
| 1380 int contourCnt; |
| 1381 int sizeEstimate; |
| 1382 get_contour_count_and_size_estimate(path, tolerance, &contourCnt, &sizeEstim
ate); |
| 1383 if (contourCnt <= 0) { |
| 1384 *isLinear = true; |
| 1385 return 0; |
| 1386 } |
| 1387 SkChunkAlloc alloc(sizeEstimate); |
| 1388 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc,
isLinear); |
| 1389 SkPath::FillType fillType = path.getFillType(); |
| 1390 int count = count_points(polys, fillType); |
| 1391 if (0 == count) { |
| 1392 return 0; |
| 1393 } |
| 1394 |
| 1395 size_t size = count * sizeof(SkPoint); |
| 1396 if (!vertexBuffer.get() || vertexBuffer->gpuMemorySize() < size) { |
| 1397 vertexBuffer.reset(resourceProvider->createVertexBuffer( |
| 1398 size, GrResourceProvider::kStatic_BufferUsage, 0)); |
| 1399 } |
| 1400 if (!vertexBuffer.get()) { |
| 1401 SkDebugf("Could not allocate vertices\n"); |
| 1402 return 0; |
| 1403 } |
| 1404 SkPoint* verts; |
| 1405 if (canMapVB) { |
| 1406 verts = static_cast<SkPoint*>(vertexBuffer->map()); |
| 1407 } else { |
| 1408 verts = new SkPoint[count]; |
| 1409 } |
| 1410 SkPoint* end = verts; |
1330 for (Poly* poly = polys; poly; poly = poly->fNext) { | 1411 for (Poly* poly = polys; poly; poly = poly->fNext) { |
1331 if (apply_fill_type(fillType, poly->fWinding)) { | 1412 if (apply_fill_type(fillType, poly->fWinding)) { |
1332 d = poly->emit(d); | 1413 end = poly->emit(end); |
1333 } | 1414 } |
1334 } | 1415 } |
1335 return d; | 1416 int actualCount = static_cast<int>(end - verts); |
| 1417 LOG("actual count: %d\n", actualCount); |
| 1418 SkASSERT(actualCount <= count); |
| 1419 if (canMapVB) { |
| 1420 vertexBuffer->unmap(); |
| 1421 } else { |
| 1422 vertexBuffer->updateData(verts, actualCount * sizeof(SkPoint)); |
| 1423 delete[] verts; |
| 1424 } |
| 1425 |
| 1426 return actualCount; |
1336 } | 1427 } |
1337 | 1428 |
1338 struct TessInfo { | 1429 int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBou
nds, |
1339 SkScalar fTolerance; | 1430 GrTessellator::WindingVertex** verts) { |
1340 int fCount; | 1431 int contourCnt; |
1341 }; | 1432 int sizeEstimate; |
| 1433 get_contour_count_and_size_estimate(path, tolerance, &contourCnt, &sizeEstim
ate); |
| 1434 if (contourCnt <= 0) { |
| 1435 return 0; |
| 1436 } |
| 1437 SkChunkAlloc alloc(sizeEstimate); |
| 1438 bool isLinear; |
| 1439 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc,
&isLinear); |
| 1440 SkPath::FillType fillType = path.getFillType(); |
| 1441 int count = count_points(polys, fillType); |
| 1442 if (0 == count) { |
| 1443 *verts = nullptr; |
| 1444 return 0; |
| 1445 } |
1342 | 1446 |
1343 bool cache_match(GrVertexBuffer* vertexBuffer, SkScalar tol, int* actualCount) { | 1447 *verts = new GrTessellator::WindingVertex[count]; |
1344 if (!vertexBuffer) { | 1448 GrTessellator::WindingVertex* vertsEnd = *verts; |
1345 return false; | 1449 SkPoint* points = new SkPoint[count]; |
| 1450 SkPoint* pointsEnd = points; |
| 1451 for (Poly* poly = polys; poly; poly = poly->fNext) { |
| 1452 if (apply_fill_type(fillType, poly->fWinding)) { |
| 1453 SkPoint* start = pointsEnd; |
| 1454 pointsEnd = poly->emit(pointsEnd); |
| 1455 while (start != pointsEnd) { |
| 1456 vertsEnd->fPos = *start; |
| 1457 vertsEnd->fWinding = poly->fWinding; |
| 1458 ++start; |
| 1459 ++vertsEnd; |
| 1460 } |
| 1461 } |
1346 } | 1462 } |
1347 const SkData* data = vertexBuffer->getUniqueKey().getCustomData(); | 1463 int actualCount = static_cast<int>(vertsEnd - *verts); |
1348 SkASSERT(data); | 1464 SkASSERT(actualCount <= count); |
1349 const TessInfo* info = static_cast<const TessInfo*>(data->data()); | 1465 SkASSERT(pointsEnd - points == actualCount); |
1350 if (info->fTolerance == 0 || info->fTolerance < 3.0f * tol) { | 1466 delete[] points; |
1351 *actualCount = info->fCount; | 1467 return actualCount; |
1352 return true; | |
1353 } | |
1354 return false; | |
1355 } | 1468 } |
1356 | 1469 |
1357 }; | 1470 } // namespace |
1358 | |
1359 GrTessellatingPathRenderer::GrTessellatingPathRenderer() { | |
1360 } | |
1361 | |
1362 namespace { | |
1363 | |
1364 // When the SkPathRef genID changes, invalidate a corresponding GrResource descr
ibed by key. | |
1365 class PathInvalidator : public SkPathRef::GenIDChangeListener { | |
1366 public: | |
1367 explicit PathInvalidator(const GrUniqueKey& key) : fMsg(key) {} | |
1368 private: | |
1369 GrUniqueKeyInvalidatedMessage fMsg; | |
1370 | |
1371 void onChange() override { | |
1372 SkMessageBus<GrUniqueKeyInvalidatedMessage>::Post(fMsg); | |
1373 } | |
1374 }; | |
1375 | |
1376 } // namespace | |
1377 | |
1378 bool GrTessellatingPathRenderer::onCanDrawPath(const CanDrawPathArgs& args) cons
t { | |
1379 // This path renderer can draw all fill styles, all stroke styles except hai
rlines, but does | |
1380 // not do antialiasing. It can do convex and concave paths, but we'll leave
the convex ones to | |
1381 // simpler algorithms. | |
1382 return !IsStrokeHairlineOrEquivalent(*args.fStroke, *args.fViewMatrix, nullp
tr) && | |
1383 !args.fAntiAlias && !args.fPath->isConvex(); | |
1384 } | |
1385 | |
1386 class TessellatingPathBatch : public GrVertexBatch { | |
1387 public: | |
1388 DEFINE_BATCH_CLASS_ID | |
1389 | |
1390 static GrDrawBatch* Create(const GrColor& color, | |
1391 const SkPath& path, | |
1392 const GrStrokeInfo& stroke, | |
1393 const SkMatrix& viewMatrix, | |
1394 SkRect clipBounds) { | |
1395 return new TessellatingPathBatch(color, path, stroke, viewMatrix, clipBo
unds); | |
1396 } | |
1397 | |
1398 const char* name() const override { return "TessellatingPathBatch"; } | |
1399 | |
1400 void computePipelineOptimizations(GrInitInvariantOutput* color, | |
1401 GrInitInvariantOutput* coverage, | |
1402 GrBatchToXPOverrides* overrides) const ove
rride { | |
1403 color->setKnownFourComponents(fColor); | |
1404 coverage->setUnknownSingleComponent(); | |
1405 overrides->fUsePLSDstRead = false; | |
1406 } | |
1407 | |
1408 private: | |
1409 void initBatchTracker(const GrXPOverridesForBatch& overrides) override { | |
1410 // Handle any color overrides | |
1411 if (!overrides.readsColor()) { | |
1412 fColor = GrColor_ILLEGAL; | |
1413 } | |
1414 overrides.getOverrideColorIfSet(&fColor); | |
1415 fPipelineInfo = overrides; | |
1416 } | |
1417 | |
1418 int tessellate(GrUniqueKey* key, | |
1419 GrResourceProvider* resourceProvider, | |
1420 SkAutoTUnref<GrVertexBuffer>& vertexBuffer, | |
1421 bool canMapVB) const { | |
1422 SkPath path; | |
1423 GrStrokeInfo stroke(fStroke); | |
1424 if (stroke.isDashed()) { | |
1425 if (!stroke.applyDashToPath(&path, &stroke, fPath)) { | |
1426 return 0; | |
1427 } | |
1428 } else { | |
1429 path = fPath; | |
1430 } | |
1431 if (!stroke.isFillStyle()) { | |
1432 stroke.setResScale(SkScalarAbs(fViewMatrix.getMaxScale())); | |
1433 if (!stroke.applyToPath(&path, path)) { | |
1434 return 0; | |
1435 } | |
1436 stroke.setFillStyle(); | |
1437 } | |
1438 SkRect pathBounds = path.getBounds(); | |
1439 Comparator c; | |
1440 if (pathBounds.width() > pathBounds.height()) { | |
1441 c.sweep_lt = sweep_lt_horiz; | |
1442 c.sweep_gt = sweep_gt_horiz; | |
1443 } else { | |
1444 c.sweep_lt = sweep_lt_vert; | |
1445 c.sweep_gt = sweep_gt_vert; | |
1446 } | |
1447 SkScalar screenSpaceTol = GrPathUtils::kDefaultTolerance; | |
1448 SkScalar tol = GrPathUtils::scaleToleranceToSrc(screenSpaceTol, fViewMat
rix, pathBounds); | |
1449 int contourCnt; | |
1450 int maxPts = GrPathUtils::worstCasePointCount(path, &contourCnt, tol); | |
1451 if (maxPts <= 0) { | |
1452 return 0; | |
1453 } | |
1454 if (maxPts > ((int)SK_MaxU16 + 1)) { | |
1455 SkDebugf("Path not rendered, too many verts (%d)\n", maxPts); | |
1456 return 0; | |
1457 } | |
1458 SkPath::FillType fillType = path.getFillType(); | |
1459 if (SkPath::IsInverseFillType(fillType)) { | |
1460 contourCnt++; | |
1461 } | |
1462 | |
1463 LOG("got %d pts, %d contours\n", maxPts, contourCnt); | |
1464 SkAutoTDeleteArray<Vertex*> contours(new Vertex* [contourCnt]); | |
1465 | |
1466 // For the initial size of the chunk allocator, estimate based on the po
int count: | |
1467 // one vertex per point for the initial passes, plus two for the vertice
s in the | |
1468 // resulting Polys, since the same point may end up in two Polys. Assum
e minimal | |
1469 // connectivity of one Edge per Vertex (will grow for intersections). | |
1470 SkChunkAlloc alloc(maxPts * (3 * sizeof(Vertex) + sizeof(Edge))); | |
1471 bool isLinear; | |
1472 path_to_contours(path, tol, fClipBounds, contours.get(), alloc, &isLinea
r); | |
1473 Poly* polys; | |
1474 polys = contours_to_polys(contours.get(), contourCnt, c, alloc); | |
1475 int count = 0; | |
1476 for (Poly* poly = polys; poly; poly = poly->fNext) { | |
1477 if (apply_fill_type(fillType, poly->fWinding) && poly->fCount >= 3)
{ | |
1478 count += (poly->fCount - 2) * (WIREFRAME ? 6 : 3); | |
1479 } | |
1480 } | |
1481 if (0 == count) { | |
1482 return 0; | |
1483 } | |
1484 | |
1485 size_t size = count * sizeof(SkPoint); | |
1486 if (!vertexBuffer.get() || vertexBuffer->gpuMemorySize() < size) { | |
1487 vertexBuffer.reset(resourceProvider->createVertexBuffer( | |
1488 size, GrResourceProvider::kStatic_BufferUsage, 0)); | |
1489 } | |
1490 if (!vertexBuffer.get()) { | |
1491 SkDebugf("Could not allocate vertices\n"); | |
1492 return 0; | |
1493 } | |
1494 SkPoint* verts; | |
1495 if (canMapVB) { | |
1496 verts = static_cast<SkPoint*>(vertexBuffer->map()); | |
1497 } else { | |
1498 verts = new SkPoint[count]; | |
1499 } | |
1500 SkPoint* end = polys_to_triangles(polys, fillType, verts); | |
1501 int actualCount = static_cast<int>(end - verts); | |
1502 LOG("actual count: %d\n", actualCount); | |
1503 SkASSERT(actualCount <= count); | |
1504 if (canMapVB) { | |
1505 vertexBuffer->unmap(); | |
1506 } else { | |
1507 vertexBuffer->updateData(verts, actualCount * sizeof(SkPoint)); | |
1508 delete[] verts; | |
1509 } | |
1510 | |
1511 | |
1512 if (!fPath.isVolatile()) { | |
1513 TessInfo info; | |
1514 info.fTolerance = isLinear ? 0 : tol; | |
1515 info.fCount = actualCount; | |
1516 SkAutoTUnref<SkData> data(SkData::NewWithCopy(&info, sizeof(info))); | |
1517 key->setCustomData(data.get()); | |
1518 resourceProvider->assignUniqueKeyToResource(*key, vertexBuffer.get()
); | |
1519 SkPathPriv::AddGenIDChangeListener(fPath, new PathInvalidator(*key))
; | |
1520 } | |
1521 return actualCount; | |
1522 } | |
1523 | |
1524 void onPrepareDraws(Target* target) const override { | |
1525 // construct a cache key from the path's genID and the view matrix | |
1526 static const GrUniqueKey::Domain kDomain = GrUniqueKey::GenerateDomain()
; | |
1527 GrUniqueKey key; | |
1528 int clipBoundsSize32 = | |
1529 fPath.isInverseFillType() ? sizeof(fClipBounds) / sizeof(uint32_t) :
0; | |
1530 int strokeDataSize32 = fStroke.computeUniqueKeyFragmentData32Cnt(); | |
1531 GrUniqueKey::Builder builder(&key, kDomain, 2 + clipBoundsSize32 + strok
eDataSize32); | |
1532 builder[0] = fPath.getGenerationID(); | |
1533 builder[1] = fPath.getFillType(); | |
1534 // For inverse fills, the tessellation is dependent on clip bounds. | |
1535 if (fPath.isInverseFillType()) { | |
1536 memcpy(&builder[2], &fClipBounds, sizeof(fClipBounds)); | |
1537 } | |
1538 fStroke.asUniqueKeyFragment(&builder[2 + clipBoundsSize32]); | |
1539 builder.finish(); | |
1540 GrResourceProvider* rp = target->resourceProvider(); | |
1541 SkAutoTUnref<GrVertexBuffer> vertexBuffer(rp->findAndRefTByUniqueKey<GrV
ertexBuffer>(key)); | |
1542 int actualCount; | |
1543 SkScalar screenSpaceTol = GrPathUtils::kDefaultTolerance; | |
1544 SkScalar tol = GrPathUtils::scaleToleranceToSrc( | |
1545 screenSpaceTol, fViewMatrix, fPath.getBounds()); | |
1546 if (!cache_match(vertexBuffer.get(), tol, &actualCount)) { | |
1547 bool canMapVB = GrCaps::kNone_MapFlags != target->caps().mapBufferFl
ags(); | |
1548 actualCount = this->tessellate(&key, rp, vertexBuffer, canMapVB); | |
1549 } | |
1550 | |
1551 if (actualCount == 0) { | |
1552 return; | |
1553 } | |
1554 | |
1555 SkAutoTUnref<const GrGeometryProcessor> gp; | |
1556 { | |
1557 using namespace GrDefaultGeoProcFactory; | |
1558 | |
1559 Color color(fColor); | |
1560 LocalCoords localCoords(fPipelineInfo.readsLocalCoords() ? | |
1561 LocalCoords::kUsePosition_Type : | |
1562 LocalCoords::kUnused_Type); | |
1563 Coverage::Type coverageType; | |
1564 if (fPipelineInfo.readsCoverage()) { | |
1565 coverageType = Coverage::kSolid_Type; | |
1566 } else { | |
1567 coverageType = Coverage::kNone_Type; | |
1568 } | |
1569 Coverage coverage(coverageType); | |
1570 gp.reset(GrDefaultGeoProcFactory::Create(color, coverage, localCoord
s, | |
1571 fViewMatrix)); | |
1572 } | |
1573 | |
1574 target->initDraw(gp, this->pipeline()); | |
1575 SkASSERT(gp->getVertexStride() == sizeof(SkPoint)); | |
1576 | |
1577 GrPrimitiveType primitiveType = WIREFRAME ? kLines_GrPrimitiveType | |
1578 : kTriangles_GrPrimitiveType; | |
1579 GrVertices vertices; | |
1580 vertices.init(primitiveType, vertexBuffer.get(), 0, actualCount); | |
1581 target->draw(vertices); | |
1582 } | |
1583 | |
1584 bool onCombineIfPossible(GrBatch*, const GrCaps&) override { return false; } | |
1585 | |
1586 TessellatingPathBatch(const GrColor& color, | |
1587 const SkPath& path, | |
1588 const GrStrokeInfo& stroke, | |
1589 const SkMatrix& viewMatrix, | |
1590 const SkRect& clipBounds) | |
1591 : INHERITED(ClassID()) | |
1592 , fColor(color) | |
1593 , fPath(path) | |
1594 , fStroke(stroke) | |
1595 , fViewMatrix(viewMatrix) { | |
1596 const SkRect& pathBounds = path.getBounds(); | |
1597 fClipBounds = clipBounds; | |
1598 // Because the clip bounds are used to add a contour for inverse fills,
they must also | |
1599 // include the path bounds. | |
1600 fClipBounds.join(pathBounds); | |
1601 if (path.isInverseFillType()) { | |
1602 fBounds = fClipBounds; | |
1603 } else { | |
1604 fBounds = path.getBounds(); | |
1605 } | |
1606 if (!stroke.isFillStyle()) { | |
1607 SkScalar radius = SkScalarHalf(stroke.getWidth()); | |
1608 if (stroke.getJoin() == SkPaint::kMiter_Join) { | |
1609 SkScalar scale = stroke.getMiter(); | |
1610 if (scale > SK_Scalar1) { | |
1611 radius = SkScalarMul(radius, scale); | |
1612 } | |
1613 } | |
1614 fBounds.outset(radius, radius); | |
1615 } | |
1616 viewMatrix.mapRect(&fBounds); | |
1617 } | |
1618 | |
1619 GrColor fColor; | |
1620 SkPath fPath; | |
1621 GrStrokeInfo fStroke; | |
1622 SkMatrix fViewMatrix; | |
1623 SkRect fClipBounds; // in source space | |
1624 GrXPOverridesForBatch fPipelineInfo; | |
1625 | |
1626 typedef GrVertexBatch INHERITED; | |
1627 }; | |
1628 | |
1629 bool GrTessellatingPathRenderer::onDrawPath(const DrawPathArgs& args) { | |
1630 SkASSERT(!args.fAntiAlias); | |
1631 const GrRenderTarget* rt = args.fPipelineBuilder->getRenderTarget(); | |
1632 if (nullptr == rt) { | |
1633 return false; | |
1634 } | |
1635 | |
1636 SkIRect clipBoundsI; | |
1637 args.fPipelineBuilder->clip().getConservativeBounds(rt->width(), rt->height(
), &clipBoundsI); | |
1638 SkRect clipBounds = SkRect::Make(clipBoundsI); | |
1639 SkMatrix vmi; | |
1640 if (!args.fViewMatrix->invert(&vmi)) { | |
1641 return false; | |
1642 } | |
1643 vmi.mapRect(&clipBounds); | |
1644 SkAutoTUnref<GrDrawBatch> batch(TessellatingPathBatch::Create(args.fColor, *
args.fPath, | |
1645 *args.fStroke,
*args.fViewMatrix, | |
1646 clipBounds)); | |
1647 args.fTarget->drawBatch(*args.fPipelineBuilder, batch); | |
1648 | |
1649 return true; | |
1650 } | |
1651 | |
1652 ////////////////////////////////////////////////////////////////////////////////
/////////////////// | |
1653 | |
1654 #ifdef GR_TEST_UTILS | |
1655 | |
1656 DRAW_BATCH_TEST_DEFINE(TesselatingPathBatch) { | |
1657 GrColor color = GrRandomColor(random); | |
1658 SkMatrix viewMatrix = GrTest::TestMatrixInvertible(random); | |
1659 SkPath path = GrTest::TestPath(random); | |
1660 SkRect clipBounds = GrTest::TestRect(random); | |
1661 SkMatrix vmi; | |
1662 bool result = viewMatrix.invert(&vmi); | |
1663 if (!result) { | |
1664 SkFAIL("Cannot invert matrix\n"); | |
1665 } | |
1666 vmi.mapRect(&clipBounds); | |
1667 GrStrokeInfo strokeInfo = GrTest::TestStrokeInfo(random); | |
1668 return TessellatingPathBatch::Create(color, path, strokeInfo, viewMatrix, cl
ipBounds); | |
1669 } | |
1670 | |
1671 #endif | |
OLD | NEW |