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 "SkChunkAlloc.h" | |
18 #include "SkGeometry.h" | 17 #include "SkGeometry.h" |
19 | 18 |
20 #include "batches/GrVertexBatch.h" | 19 #include "batches/GrVertexBatch.h" |
21 | 20 |
22 #include <stdio.h> | 21 #include <stdio.h> |
23 | 22 |
24 /* | 23 /* |
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: | 24 * There are six stages to the algorithm: |
30 * | 25 * |
31 * 1) Linearize the path contours into piecewise linear segments (path_to_contou rs()). | 26 * 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()). | 27 * 2) Build a mesh of edges connecting the vertices (build_edges()). |
33 * 3) Sort the vertices in Y (and secondarily in X) (merge_sort()). | 28 * 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()). | 29 * 4) Simplify the mesh by inserting new vertices at intersecting edges (simplif y()). |
35 * 5) Tessellate the simplified mesh into monotone polygons (tessellate()). | 30 * 5) Tessellate the simplified mesh into monotone polygons (tessellate()). |
36 * 6) Triangulate the monotone polygons directly into a vertex buffer (polys_to_ triangles()). | 31 * 6) Triangulate the monotone polygons directly into a vertex buffer (polys_to_ triangles()). |
37 * | 32 * |
38 * The vertex sorting in step (3) is a merge sort, since it plays well with the linked list | 33 * 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. | 68 * frequent. There may be other data structures worth investigating, however. |
74 * | 69 * |
75 * Note that the orientation of the line sweep algorithms is determined by the a spect ratio of the | 70 * 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 | 71 * 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, | 72 * 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 | 73 * 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 | 74 * 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 | 75 * increasing in Y; edges to the right are decreasing in Y). That is, the settin g rotates 90 |
81 * degrees counterclockwise, rather that transposing. | 76 * degrees counterclockwise, rather that transposing. |
82 */ | 77 */ |
78 | |
83 #define LOGGING_ENABLED 0 | 79 #define LOGGING_ENABLED 0 |
84 #define WIREFRAME 0 | |
85 | 80 |
86 #if LOGGING_ENABLED | 81 #if LOGGING_ENABLED |
87 #define LOG printf | 82 #define LOG printf |
88 #else | 83 #else |
89 #define LOG(...) | 84 #define LOG(...) |
90 #endif | 85 #endif |
91 | 86 |
92 #define ALLOC_NEW(Type, args, alloc) new (alloc.allocThrow(sizeof(Type))) Type a rgs | 87 #define ALLOC_NEW(Type, args, alloc) new (alloc.allocThrow(sizeof(Type))) Type a rgs |
93 | 88 |
94 namespace { | 89 namespace { |
(...skipping 439 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
534 pointsLeft >>= 1; | 529 pointsLeft >>= 1; |
535 prev = generate_cubic_points(p0, q[0], r[0], s, tolSqd, prev, head, pointsLe ft, alloc); | 530 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); | 531 prev = generate_cubic_points(s, r[1], q[2], p3, tolSqd, prev, head, pointsLe ft, alloc); |
537 return prev; | 532 return prev; |
538 } | 533 } |
539 | 534 |
540 // Stage 1: convert the input path to a set of linear contours (linked list of V ertices). | 535 // Stage 1: convert the input path to a set of linear contours (linked list of V ertices). |
541 | 536 |
542 void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clip Bounds, | 537 void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clip Bounds, |
543 Vertex** contours, SkChunkAlloc& alloc, bool *isLinear) { | 538 Vertex** contours, SkChunkAlloc& alloc, bool *isLinear) { |
544 | |
545 SkScalar toleranceSqd = tolerance * tolerance; | 539 SkScalar toleranceSqd = tolerance * tolerance; |
546 | 540 |
547 SkPoint pts[4]; | 541 SkPoint pts[4]; |
548 bool done = false; | 542 bool done = false; |
549 *isLinear = true; | 543 *isLinear = true; |
550 SkPath::Iter iter(path, false); | 544 SkPath::Iter iter(path, false); |
551 Vertex* prev = nullptr; | 545 Vertex* prev = nullptr; |
552 Vertex* head = nullptr; | 546 Vertex* head = nullptr; |
553 if (path.isInverseFillType()) { | 547 if (path.isInverseFillType()) { |
554 SkPoint quad[4]; | 548 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, | 1280 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); | 1281 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRight Poly->fID : -1); |
1288 } | 1282 } |
1289 #endif | 1283 #endif |
1290 } | 1284 } |
1291 return polys; | 1285 return polys; |
1292 } | 1286 } |
1293 | 1287 |
1294 // This is a driver function which calls stages 2-5 in turn. | 1288 // This is a driver function which calls stages 2-5 in turn. |
1295 | 1289 |
1296 Poly* contours_to_polys(Vertex** contours, int contourCnt, Comparator& c, SkChun kAlloc& alloc) { | 1290 Poly* contours_to_polys(Vertex** contours, int contourCnt, SkRect pathBounds, Sk ChunkAlloc& alloc) { |
Stephen White
2016/01/05 19:12:51
Nit: SkRect could be const-ref.
| |
1291 Comparator c; | |
1292 if (pathBounds.width() > pathBounds.height()) { | |
1293 c.sweep_lt = sweep_lt_horiz; | |
1294 c.sweep_gt = sweep_gt_horiz; | |
1295 } else { | |
1296 c.sweep_lt = sweep_lt_vert; | |
1297 c.sweep_gt = sweep_gt_vert; | |
1298 } | |
1297 #if LOGGING_ENABLED | 1299 #if LOGGING_ENABLED |
1298 for (int i = 0; i < contourCnt; ++i) { | 1300 for (int i = 0; i < contourCnt; ++i) { |
1299 Vertex* v = contours[i]; | 1301 Vertex* v = contours[i]; |
1300 SkASSERT(v); | 1302 SkASSERT(v); |
1301 LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); | 1303 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) { | 1304 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); | 1305 LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); |
1304 } | 1306 } |
1305 } | 1307 } |
1306 #endif | 1308 #endif |
1307 sanitize_contours(contours, contourCnt); | 1309 sanitize_contours(contours, contourCnt); |
1308 Vertex* vertices = build_edges(contours, contourCnt, c, alloc); | 1310 Vertex* vertices = build_edges(contours, contourCnt, c, alloc); |
1309 if (!vertices) { | 1311 if (!vertices) { |
1310 return nullptr; | 1312 return nullptr; |
1311 } | 1313 } |
1312 | 1314 |
1313 // Sort vertices in Y (secondarily in X). | 1315 // Sort vertices in Y (secondarily in X). |
1314 merge_sort(&vertices, c); | 1316 merge_sort(&vertices, c); |
1315 merge_coincident_vertices(&vertices, c, alloc); | 1317 merge_coincident_vertices(&vertices, c, alloc); |
1316 #if LOGGING_ENABLED | 1318 #if LOGGING_ENABLED |
1317 for (Vertex* v = vertices; v != nullptr; v = v->fNext) { | 1319 for (Vertex* v = vertices; v != nullptr; v = v->fNext) { |
1318 static float gID = 0.0f; | 1320 static float gID = 0.0f; |
1319 v->fID = gID++; | 1321 v->fID = gID++; |
1320 } | 1322 } |
1321 #endif | 1323 #endif |
1322 simplify(vertices, c, alloc); | 1324 simplify(vertices, c, alloc); |
1323 return tessellate(vertices, alloc); | 1325 return tessellate(vertices, alloc); |
1324 } | 1326 } |
1325 | 1327 |
1328 Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBo unds, | |
1329 int contourCnt, SkChunkAlloc& alloc, bool* isLinear) { | |
1330 SkPath::FillType fillType = path.getFillType(); | |
1331 if (SkPath::IsInverseFillType(fillType)) { | |
1332 contourCnt++; | |
1333 } | |
1334 SkAutoTDeleteArray<Vertex*> contours(new Vertex* [contourCnt]); | |
1335 | |
1336 path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinea r); | |
1337 return contours_to_polys(contours.get(), contourCnt, path.getBounds(), alloc ); | |
1338 } | |
1339 | |
1340 void get_contour_count_and_size_estimate(const SkPath& path, SkScalar tolerance, int* contourCnt, | |
1341 int* sizeEstimate) { | |
1342 int maxPts = GrPathUtils::worstCasePointCount(path, contourCnt, tolerance); | |
1343 if (maxPts <= 0) { | |
1344 *contourCnt = 0; | |
1345 return; | |
1346 } | |
1347 if (maxPts > ((int)SK_MaxU16 + 1)) { | |
1348 SkDebugf("Path not rendered, too many verts (%d)\n", maxPts); | |
1349 *contourCnt = 0; | |
1350 return; | |
1351 } | |
1352 // For the initial size of the chunk allocator, estimate based on the point count: | |
1353 // one vertex per point for the initial passes, plus two for the vertices in the | |
1354 // resulting Polys, since the same point may end up in two Polys. Assume mi nimal | |
1355 // connectivity of one Edge per Vertex (will grow for intersections). | |
1356 *sizeEstimate = maxPts * (3 * sizeof(Vertex) + sizeof(Edge)); | |
1357 } | |
1358 | |
1359 } | |
1360 | |
1361 namespace GrTessellator { | |
1362 | |
1326 // Stage 6: Triangulate the monotone polygons into a vertex buffer. | 1363 // Stage 6: Triangulate the monotone polygons into a vertex buffer. |
1327 | 1364 |
1328 SkPoint* polys_to_triangles(Poly* polys, SkPath::FillType fillType, SkPoint* dat a) { | 1365 int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBo unds, |
1329 SkPoint* d = data; | 1366 GrResourceProvider* resourceProvider, |
1367 SkAutoTUnref<GrVertexBuffer>& vertexBuffer, bool canMapVB, b ool* isLinear) { | |
1368 int contourCnt; | |
1369 int sizeEstimate; | |
1370 get_contour_count_and_size_estimate(path, tolerance, &contourCnt, &sizeEstim ate); | |
1371 if (contourCnt <= 0) { | |
1372 return 0; | |
1373 } | |
1374 SkChunkAlloc alloc(sizeEstimate); | |
1375 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, isLinear); | |
1376 SkPath::FillType fillType = path.getFillType(); | |
1377 int count = 0; | |
1378 for (Poly* poly = polys; poly; poly = poly->fNext) { | |
1379 if (apply_fill_type(fillType, poly->fWinding) && poly->fCount >= 3) { | |
1380 count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3); | |
1381 } | |
1382 } | |
1383 if (0 == count) { | |
1384 return 0; | |
1385 } | |
1386 | |
1387 size_t size = count * sizeof(SkPoint); | |
1388 if (!vertexBuffer.get() || vertexBuffer->gpuMemorySize() < size) { | |
1389 vertexBuffer.reset(resourceProvider->createVertexBuffer( | |
1390 size, GrResourceProvider::kStatic_BufferUsage, 0)); | |
1391 } | |
1392 if (!vertexBuffer.get()) { | |
1393 SkDebugf("Could not allocate vertices\n"); | |
1394 return 0; | |
1395 } | |
1396 SkPoint* verts; | |
1397 if (canMapVB) { | |
1398 verts = static_cast<SkPoint*>(vertexBuffer->map()); | |
1399 } else { | |
1400 verts = new SkPoint[count]; | |
1401 } | |
1402 SkPoint* end = verts; | |
1330 for (Poly* poly = polys; poly; poly = poly->fNext) { | 1403 for (Poly* poly = polys; poly; poly = poly->fNext) { |
1331 if (apply_fill_type(fillType, poly->fWinding)) { | 1404 if (apply_fill_type(fillType, poly->fWinding)) { |
1332 d = poly->emit(d); | 1405 end = poly->emit(end); |
1333 } | 1406 } |
1334 } | 1407 } |
1335 return d; | 1408 int actualCount = static_cast<int>(end - verts); |
1409 LOG("actual count: %d\n", actualCount); | |
1410 SkASSERT(actualCount <= count); | |
1411 if (canMapVB) { | |
1412 vertexBuffer->unmap(); | |
1413 } else { | |
1414 vertexBuffer->updateData(verts, actualCount * sizeof(SkPoint)); | |
1415 delete[] verts; | |
1416 } | |
1417 | |
1418 return actualCount; | |
1336 } | 1419 } |
1337 | 1420 |
1338 struct TessInfo { | 1421 int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBou nds, |
1339 SkScalar fTolerance; | 1422 GrTessellator::WindingVertex** verts) { |
1340 int fCount; | 1423 int contourCnt; |
1341 }; | 1424 int sizeEstimate; |
1425 get_contour_count_and_size_estimate(path, tolerance, &contourCnt, &sizeEstim ate); | |
1426 if (contourCnt <= 0) { | |
1427 return 0; | |
1428 } | |
1429 SkChunkAlloc alloc(sizeEstimate); | |
1430 bool isLinear; | |
1431 Poly* polys = path_to_polys(path, tolerance, clipBounds, contourCnt, alloc, &isLinear); | |
1432 SkPath::FillType fillType = path.getFillType(); | |
1433 int count = 0; | |
1434 for (Poly* poly = polys; poly; poly = poly->fNext) { | |
1435 if (apply_fill_type(fillType, poly->fWinding) && poly->fCount >= 3) { | |
1436 count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3); | |
1437 } | |
1438 } | |
1439 if (0 == count) { | |
1440 *verts = nullptr; | |
1441 return 0; | |
1442 } | |
1342 | 1443 |
1343 bool cache_match(GrVertexBuffer* vertexBuffer, SkScalar tol, int* actualCount) { | 1444 *verts = new GrTessellator::WindingVertex[count]; |
1344 if (!vertexBuffer) { | 1445 GrTessellator::WindingVertex* vertsEnd = *verts; |
1345 return false; | 1446 SkPoint* points = new SkPoint[count]; |
1447 SkPoint* pointsEnd = points; | |
1448 for (Poly* poly = polys; poly; poly = poly->fNext) { | |
1449 if (apply_fill_type(fillType, poly->fWinding)) { | |
1450 SkPoint* start = pointsEnd; | |
1451 pointsEnd = poly->emit(pointsEnd); | |
1452 while (start != pointsEnd) { | |
1453 vertsEnd->fPos = *start; | |
1454 vertsEnd->fWinding = poly->fWinding; | |
1455 ++start; | |
1456 ++vertsEnd; | |
1457 } | |
1458 } | |
1346 } | 1459 } |
1347 const SkData* data = vertexBuffer->getUniqueKey().getCustomData(); | 1460 int actualCount = static_cast<int>(vertsEnd - *verts); |
1348 SkASSERT(data); | 1461 SkASSERT(actualCount <= count); |
1349 const TessInfo* info = static_cast<const TessInfo*>(data->data()); | 1462 SkASSERT(pointsEnd - points == actualCount); |
1350 if (info->fTolerance == 0 || info->fTolerance < 3.0f * tol) { | 1463 delete[] points; |
1351 *actualCount = info->fCount; | 1464 return actualCount; |
1352 return true; | |
1353 } | |
1354 return false; | |
1355 } | 1465 } |
1356 | 1466 |
1357 }; | |
1358 | |
1359 GrTessellatingPathRenderer::GrTessellatingPathRenderer() { | |
1360 } | 1467 } |
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 |