| 1 |
/* |
| 2 |
* Copyright 2020 Google Inc. |
| 3 |
* |
| 4 |
* Use of this source code is governed by a BSD-style license that can be |
| 5 |
* found in the LICENSE file. |
| 6 |
*/ |
| 7 |
|
| 8 |
#ifndef skgpu_tessellate_MiddleOutPolygonTriangulator_DEFINED |
| 9 |
#define skgpu_tessellate_MiddleOutPolygonTriangulator_DEFINED |
| 10 |
|
| 11 |
#include "include/core/SkPath.h" |
| 12 |
#include "include/core/SkPathTypes.h" |
| 13 |
#include "include/core/SkPoint.h" |
| 14 |
#include "include/private/base/SkAssert.h" |
| 15 |
#include "include/private/base/SkDebug.h" |
| 16 |
#include "include/private/base/SkTemplates.h" |
| 17 |
#include "src/base/SkMathPriv.h" |
| 18 |
#include "src/core/SkPathPriv.h" |
| 19 |
|
| 20 |
#include <algorithm> |
| 21 |
#include <cstring> |
| 22 |
#include <tuple> |
| 23 |
|
| 24 |
namespace skgpu::tess { |
| 25 |
|
| 26 |
// This class generates a middle-out triangulation of a polygon. Conceptually, middle-out emits one |
| 27 |
// large triangle with vertices on both endpoints and a middle point, then recurses on both sides of |
| 28 |
// the new triangle. i.e.: |
| 29 |
// |
| 30 |
// void emit_middle_out_triangulation(int startIdx, int endIdx) { |
| 31 |
// if (startIdx + 1 == endIdx) { |
| 32 |
// return; |
| 33 |
// } |
| 34 |
// int middleIdx = startIdx + SkNextPow2(endIdx - startIdx) / 2; |
| 35 |
// |
| 36 |
// // Recurse on the left half. |
| 37 |
// emit_middle_out_triangulation(startIdx, middleIdx); |
| 38 |
// |
| 39 |
// // Emit a large triangle with vertices on both endpoints and a middle point. |
| 40 |
// emit_triangle(vertices[startIdx], vertices[middleIdx], vertices[endIdx - 1]); |
| 41 |
// |
| 42 |
// // Recurse on the right half. |
| 43 |
// emit_middle_out_triangulation(middleIdx, endIdx); |
| 44 |
// } |
| 45 |
// |
| 46 |
// Middle-out produces drastically less work for the rasterizer as compared a linear triangle strip |
| 47 |
// or fan. |
| 48 |
// |
| 49 |
// This class is designed to not know or store all the vertices in the polygon at once. The caller |
| 50 |
// pushes each vertex in linear order (perhaps while parsing a path), then rather than relying on |
| 51 |
// recursion, we manipulate an O(log N) stack to determine the correct middle-out triangulation. |
| 52 |
class MiddleOutPolygonTriangulator { |
| 53 |
private: |
| 54 |
// Internal representation of how we store vertices on our stack. |
| 55 |
struct StackVertex { |
| 56 |
SkPoint fPoint; |
| 57 |
// How many polygon vertices away is this vertex from the previous vertex on the stack? |
| 58 |
// i.e., the ith stack element's vertex index in the original polygon is: |
| 59 |
// |
| 60 |
// fVertexStack[i].fVertexIdxDelta + fVertexStack[i - 1].fVertexIdxDelta + ... + |
| 61 |
// fVertexStack[1].fVertexIdxDelta. |
| 62 |
// |
| 63 |
// NOTE: fVertexStack[0].fVertexIdxDelta always == 0. |
| 64 |
int fVertexIdxDelta; |
| 65 |
}; |
| 66 |
|
| 67 |
public: |
| 68 |
// RAII. This class is designed to first allow the caller to iterate the triangles that will be |
| 69 |
// popped off our stack, and then (during the destructor) it actually pops the finished vertices |
| 70 |
// and pushes a new one. Example usage: |
| 71 |
// |
| 72 |
// for (auto [p0, p1, p2] : middleOut.pushVertex(pt)) { |
| 73 |
// vertexWriter << p0 << p1 << p2; |
| 74 |
// } |
| 75 |
// |
| 76 |
// The above code iterates over the triangles being popped, and then once iteration is finished, |
| 77 |
// the PoppedTriangleStack is destroyed, triggering the pending stack update. |
| 78 |
class PoppedTriangleStack { |
| 79 |
public: |
| 80 |
PoppedTriangleStack(MiddleOutPolygonTriangulator* middleOut, |
| 81 |
SkPoint lastPoint, |
| 82 |
StackVertex* end, |
| 83 |
StackVertex* newTopVertex, |
| 84 |
StackVertex newTopValue) |
| 85 |
: fMiddleOut(middleOut) |
| 86 |
, fLastPoint(lastPoint) |
| 87 |
, fEnd(end) |
| 88 |
, fNewTopVertex(newTopVertex) |
| 89 |
, fNewTopValue(newTopValue) { |
| 90 |
} |
| 91 |
|
| 92 |
PoppedTriangleStack(PoppedTriangleStack&& that) |
| 93 |
: fMiddleOut(that.fMiddleOut) |
| 94 |
, fLastPoint(that.fLastPoint) |
| 95 |
, fEnd(that.fEnd) |
| 96 |
, fNewTopVertex(that.fNewTopVertex) |
| 97 |
, fNewTopValue(that.fNewTopValue) { |
| 98 |
that.fMiddleOut = nullptr; // Don't do a stack update during our destructor. |
| 99 |
} |
| 100 |
|
| 101 |
~PoppedTriangleStack() { |
| 102 |
if (fMiddleOut) { |
| 103 |
fMiddleOut->fTop = fNewTopVertex; |
| 104 |
*fNewTopVertex = fNewTopValue; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
struct Iter { |
| 109 |
bool operator!=(const Iter& iter) const { return fVertex != iter.fVertex; } |
| 110 |
void operator++() { --fVertex; } |
| 111 |
std::tuple<SkPoint, SkPoint, SkPoint> operator*() { |
| 112 |
return {fVertex[-1].fPoint, fVertex[0].fPoint, fLastPoint}; |
| 113 |
} |
| 114 |
StackVertex* fVertex; |
| 115 |
SkPoint fLastPoint; |
| 116 |
}; |
| 117 |
|
| 118 |
Iter begin() const { return {fMiddleOut ? fMiddleOut->fTop : fEnd, fLastPoint}; } |
| 119 |
Iter end() const { return {fEnd, fLastPoint}; } |
| 120 |
|
| 121 |
private: |
| 122 |
MiddleOutPolygonTriangulator* fMiddleOut; |
| 123 |
SkPoint fLastPoint; |
| 124 |
StackVertex* fEnd; |
| 125 |
StackVertex* fNewTopVertex; |
| 126 |
StackVertex fNewTopValue; |
| 127 |
}; |
| 128 |
|
| 129 |
// maxPushVertexCalls is an upper bound on the number of times the caller will call |
| 130 |
// pushVertex(). The caller must not call it more times than this. (Beware of int overflow.) |
| 131 |
MiddleOutPolygonTriangulator(int maxPushVertexCalls, SkPoint startPoint = {0,0}) { |
| 132 |
SkASSERT(maxPushVertexCalls >= 0); |
| 133 |
// Determine the deepest our stack can ever go. |
| 134 |
int maxStackDepth = SkNextLog2(maxPushVertexCalls) + 1; |
| 135 |
if (maxStackDepth > kStackPreallocCount) { |
| 136 |
fVertexStack.reset(maxStackDepth); |
| 137 |
} |
| 138 |
SkDEBUGCODE(fStackAllocCount = maxStackDepth;) |
| 139 |
// The stack will always contain a starting point. This is an implicit moveTo(0, 0) |
| 140 |
// initially, but will be overridden if moveTo() gets called before adding geometry. |
| 141 |
fVertexStack[0] = {startPoint, 0}; |
| 142 |
fTop = fVertexStack; |
| 143 |
} |
| 144 |
|
| 145 |
// Returns an RAII object that first allows the caller to iterate the triangles we will pop, |
| 146 |
// pops those triangles, and finally pushes 'pt' onto the vertex stack. |
| 147 |
[[nodiscard]] PoppedTriangleStack pushVertex(SkPoint pt) { |
| 148 |
// Our topology wants triangles that have the same vertexIdxDelta on both sides: |
| 149 |
// e.g., a run of 9 points should be triangulated as: |
| 150 |
// |
| 151 |
// [0, 1, 2], [2, 3, 4], [4, 5, 6], [6, 7, 8] // vertexIdxDelta == 1 |
| 152 |
// [0, 2, 4], [4, 6, 8] // vertexIdxDelta == 2 |
| 153 |
// [0, 4, 8] // vertexIdxDelta == 4 |
| 154 |
// |
| 155 |
// Find as many new triangles as we can pop off the stack that have equal-delta sides. (This |
| 156 |
// is a stack-based implementation of the recursive example method from the class comment.) |
| 157 |
StackVertex* endVertex = fTop; |
| 158 |
int vertexIdxDelta = 1; |
| 159 |
while (endVertex->fVertexIdxDelta == vertexIdxDelta) { |
| 160 |
--endVertex; |
| 161 |
vertexIdxDelta *= 2; |
| 162 |
} |
| 163 |
|
| 164 |
// Once the above triangles are popped, push 'pt' to the top of the stack. |
| 165 |
StackVertex* newTopVertex = endVertex + 1; |
| 166 |
StackVertex newTopValue = {pt, vertexIdxDelta}; |
| 167 |
SkASSERT(newTopVertex < fVertexStack + fStackAllocCount); // Is fStackAllocCount enough? |
| 168 |
|
| 169 |
return PoppedTriangleStack(this, pt, endVertex, newTopVertex, newTopValue); |
| 170 |
} |
| 171 |
|
| 172 |
// Returns an RAII object that first allows the caller to iterate the remaining triangles, then |
| 173 |
// resets the vertex stack with newStartPoint. |
| 174 |
[[nodiscard]] PoppedTriangleStack closeAndMove(SkPoint newStartPoint) { |
| 175 |
// Add an implicit line back to the starting point. |
| 176 |
SkPoint startPt = fVertexStack[0].fPoint; |
| 177 |
|
| 178 |
// Triangulate the rest of the polygon. Since we simply have to finish now, we can't be |
| 179 |
// picky anymore about getting a pure middle-out topology. |
| 180 |
StackVertex* endVertex = std::min(fTop, fVertexStack + 1); |
| 181 |
|
| 182 |
// Once every remaining triangle is popped, reset the vertex stack with newStartPoint. |
| 183 |
StackVertex* newTopVertex = fVertexStack; |
| 184 |
StackVertex newTopValue = {newStartPoint, 0}; |
| 185 |
|
| 186 |
return PoppedTriangleStack(this, startPt, endVertex, newTopVertex, newTopValue); |
| 187 |
} |
| 188 |
|
| 189 |
// Returns an RAII object that first allows the caller to iterate the remaining triangles, then |
| 190 |
// resets the vertex stack with the same starting point as it had before. |
| 191 |
[[nodiscard]] PoppedTriangleStack close() { |
| 192 |
return this->closeAndMove(fVertexStack[0].fPoint); |
| 193 |
} |
| 194 |
|
| 195 |
private: |
| 196 |
constexpr static int kStackPreallocCount = 32; |
| 197 |
skia_private::AutoSTMalloc<kStackPreallocCount, StackVertex> fVertexStack; |
| 198 |
SkDEBUGCODE(int fStackAllocCount;) |
| 199 |
StackVertex* fTop; |
| 200 |
}; |
| 201 |
|
| 202 |
// This is a helper class that transforms and pushes a path's inner fan vertices onto a |
| 203 |
// MiddleOutPolygonTriangulator. Example usage: |
| 204 |
// |
| 205 |
// for (PathMiddleOutFanIter it(pathMatrix, path); !it.done();) { |
| 206 |
// for (auto [p0, p1, p2] : it.nextStack()) { |
| 207 |
// vertexWriter << p0 << p1 << p2; |
| 208 |
// } |
| 209 |
// } |
| 210 |
// |
| 211 |
class PathMiddleOutFanIter { |
| 212 |
public: |
| 213 |
PathMiddleOutFanIter(const SkPath& path) : fMiddleOut(path.countVerbs()) { |
| 214 |
SkPathPriv::Iterate it(path); |
| 215 |
fPathIter = it.begin(); |
| 216 |
fPathEnd = it.end(); |
| 217 |
} |
| 218 |
|
| 219 |
bool done() const { return fDone; } |
| 220 |
|
| 221 |
MiddleOutPolygonTriangulator::PoppedTriangleStack nextStack() { |
| 222 |
SkASSERT(!fDone); |
| 223 |
if (fPathIter == fPathEnd) { |
| 224 |
fDone = true; |
| 225 |
return fMiddleOut.close(); |
| 226 |
} |
| 227 |
switch (auto [verb, pts, w] = *fPathIter++; verb) { |
| 228 |
SkPoint pt; |
| 229 |
case SkPathVerb::kMove: |
| 230 |
return fMiddleOut.closeAndMove(pts[0]); |
| 231 |
case SkPathVerb::kLine: |
| 232 |
case SkPathVerb::kQuad: |
| 233 |
case SkPathVerb::kConic: |
| 234 |
case SkPathVerb::kCubic: |
| 235 |
pt = pts[SkPathPriv::PtsInIter((unsigned)verb) - 1]; |
| 236 |
return fMiddleOut.pushVertex(pt); |
| 237 |
case SkPathVerb::kClose: |
| 238 |
return fMiddleOut.close(); |
| 239 |
} |
| 240 |
SkUNREACHABLE; |
| 241 |
} |
| 242 |
|
| 243 |
private: |
| 244 |
MiddleOutPolygonTriangulator fMiddleOut; |
| 245 |
SkPathPriv::RangeIter fPathIter; |
| 246 |
SkPathPriv::RangeIter fPathEnd; |
| 247 |
bool fDone = false; |
| 248 |
}; |
| 249 |
|
| 250 |
} // namespace skgpu::tess |
| 251 |
|
| 252 |
#endif // skgpu_tessellate_MiddleOutPolygonTriangulator_DEFINED |