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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « src/gpu/GrTessellator.h ('k') | src/gpu/batches/GrTessellatingPathRenderer.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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 GrTessellator {
Stephen White 2016/01/05 16:36:22 Now that they're no longer public, can these thing
95 90
91 struct Poly;
92 struct EdgeList;
96 struct Vertex; 93 struct Vertex;
97 struct Edge; 94
95 struct Edge {
96 Edge(Vertex* top, Vertex* bottom, int winding)
97 : fTop(top)
98 , fBottom(bottom)
99 , fWinding(winding)
100 , fLeft(nullptr)
101 , fRight(nullptr)
102 , fPrevEdgeAbove(nullptr)
103 , fNextEdgeAbove(nullptr)
104 , fPrevEdgeBelow(nullptr)
105 , fNextEdgeBelow(nullptr)
106 , fLeftPoly(nullptr)
107 , fRightPoly(nullptr) {
108 recompute();
109 }
110 Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt).
111 Vertex* fBottom; // The bottom vertex in vertex-sort-order.
112 int fWinding; // 1 == edge goes downward; -1 = edge goes upward.
113 Edge* fLeft; // The linked list of edges in the active edge list.
114 Edge* fRight; // "
115 Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex's "e dges above".
116 Edge* fNextEdgeAbove; // "
117 Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edge s below".
118 Edge* fNextEdgeBelow; // "
119 Poly* fLeftPoly; // The Poly to the left of this edge, if any.
120 Poly* fRightPoly; // The Poly to the right of this edge, if any.
121 double fDX; // The line equation for this edge, in implicit form.
122 double fDY; // fDY * x + fDX * y + fC = 0, for point (x, y) on th e line.
123 double fC;
124 double dist(const SkPoint& p) const;
125 bool isRightOf(Vertex* v) const;
126 bool isLeftOf(Vertex* v) const;
127 void recompute();
128 bool intersect(const Edge& other, SkPoint* p);
129 bool isActive(EdgeList* activeEdges) const;
130 };
131
132 struct EdgeList {
133 EdgeList() : fHead(nullptr), fTail(nullptr) {}
134 Edge* fHead;
135 Edge* fTail;
136 };
137
138 /**
139 * Vertices are used in three ways: first, the path contours are converted into a circularly-linked
140 * list of vertices for each contour. After edge construction, the same vertices are re-ordered by
141 * the merge sort according to the sweep_lt comparator (usually, increasing in Y ) using the same
142 * fPrev/fNext pointers that were used for the contours, to avoid reallocation. Finally,
143 * MonotonePolys are built containing a circularly-linked list of vertices. Curr ently, those
144 * Vertices are newly-allocated for the MonotonePolys, since an individual verte x from the path mesh
145 * may belong to multiple MonotonePolys, so the original vertices cannot be re-u sed.
146 */
147 struct Vertex {
148 Vertex(const SkPoint& point)
149 : fPoint(point), fPrev(nullptr), fNext(nullptr)
150 , fFirstEdgeAbove(nullptr), fLastEdgeAbove(nullptr)
151 , fFirstEdgeBelow(nullptr), fLastEdgeBelow(nullptr)
152 , fProcessed(false)
153 #if LOGGING_ENABLED
154 , fID (-1.0f)
155 #endif
156 {}
157 SkPoint fPoint; // Vertex position
158 Vertex* fPrev; // Linked list of contours, then Y-sorted vertices .
159 Vertex* fNext; // "
160 Edge* fFirstEdgeAbove; // Linked list of edges above this vertex.
161 Edge* fLastEdgeAbove; // "
162 Edge* fFirstEdgeBelow; // Linked list of edges below this vertex.
163 Edge* fLastEdgeBelow; // "
164 bool fProcessed; // Has this vertex been seen in simplify()?
165 #if LOGGING_ENABLED
166 float fID; // Identifier used for logging.
167 #endif
168 };
169
170 double Edge::dist(const SkPoint& p) const {
171 return fDY * p.fX - fDX * p.fY + fC;
172 }
173
174 bool Edge::isRightOf(Vertex* v) const {
175 return dist(v->fPoint) < 0.0;
176 }
177
178 bool Edge::isLeftOf(Vertex* v) const {
179 return dist(v->fPoint) > 0.0;
180 }
181
182 void Edge::recompute() {
183 fDX = static_cast<double>(fBottom->fPoint.fX) - fTop->fPoint.fX;
184 fDY = static_cast<double>(fBottom->fPoint.fY) - fTop->fPoint.fY;
185 fC = static_cast<double>(fTop->fPoint.fY) * fBottom->fPoint.fX -
186 static_cast<double>(fTop->fPoint.fX) * fBottom->fPoint.fY;
187 }
188
189 bool Edge::intersect(const Edge& other, SkPoint* p) {
190 #if LOGGING_ENABLED
191 LOG("intersecting %g -> %g with %g -> %g\n",
192 fTop->fID, fBottom->fID,
193 other.fTop->fID, other.fBottom->fID);
194 #endif
195 if (fTop == other.fTop || fBottom == other.fBottom) {
196 return false;
197 }
198 double denom = fDX * other.fDY - fDY * other.fDX;
199 if (denom == 0.0) {
200 return false;
201 }
202 double dx = static_cast<double>(fTop->fPoint.fX) - other.fTop->fPoint.fX;
203 double dy = static_cast<double>(fTop->fPoint.fY) - other.fTop->fPoint.fY;
204 double sNumer = dy * other.fDX - dx * other.fDY;
205 double tNumer = dy * fDX - dx * fDY;
206 // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
207 // This saves us doing the divide below unless absolutely necessary.
208 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
209 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
210 return false;
211 }
212 double s = sNumer / denom;
213 SkASSERT(s >= 0.0 && s <= 1.0);
214 p->fX = SkDoubleToScalar(fTop->fPoint.fX + s * fDX);
215 p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fDY);
216 return true;
217 }
218
219 bool Edge::isActive(EdgeList* activeEdges) const {
220 return activeEdges && (fLeft || fRight || activeEdges->fHead == this);
221 }
222
98 struct Poly; 223 struct Poly;
99 224
100 template <class T, T* T::*Prev, T* T::*Next> 225 template <class T, T* T::*Prev, T* T::*Next>
101 void insert(T* t, T* prev, T* next, T** head, T** tail) { 226 void insert(T* t, T* prev, T* next, T** head, T** tail) {
102 t->*Prev = prev; 227 t->*Prev = prev;
103 t->*Next = next; 228 t->*Next = next;
104 if (prev) { 229 if (prev) {
105 prev->*Next = t; 230 prev->*Next = t;
106 } else if (head) { 231 } else if (head) {
107 *head = t; 232 *head = t;
(...skipping 13 matching lines...) Expand all
121 *head = t->*Next; 246 *head = t->*Next;
122 } 247 }
123 if (t->*Next) { 248 if (t->*Next) {
124 t->*Next->*Prev = t->*Prev; 249 t->*Next->*Prev = t->*Prev;
125 } else if (tail) { 250 } else if (tail) {
126 *tail = t->*Prev; 251 *tail = t->*Prev;
127 } 252 }
128 t->*Prev = t->*Next = nullptr; 253 t->*Prev = t->*Next = nullptr;
129 } 254 }
130 255
131 /**
132 * Vertices are used in three ways: first, the path contours are converted into a
133 * circularly-linked list of Vertices for each contour. After edge construction, the same Vertices
134 * are re-ordered by the merge sort according to the sweep_lt comparator (usuall y, increasing
135 * in Y) using the same fPrev/fNext pointers that were used for the contours, to avoid
136 * reallocation. Finally, MonotonePolys are built containing a circularly-linked list of
137 * Vertices. (Currently, those Vertices are newly-allocated for the MonotonePoly s, since
138 * an individual Vertex from the path mesh may belong to multiple
139 * MonotonePolys, so the original Vertices cannot be re-used.
140 */
141
142 struct Vertex {
143 Vertex(const SkPoint& point)
144 : fPoint(point), fPrev(nullptr), fNext(nullptr)
145 , fFirstEdgeAbove(nullptr), fLastEdgeAbove(nullptr)
146 , fFirstEdgeBelow(nullptr), fLastEdgeBelow(nullptr)
147 , fProcessed(false)
148 #if LOGGING_ENABLED
149 , fID (-1.0f)
150 #endif
151 {}
152 SkPoint fPoint; // Vertex position
153 Vertex* fPrev; // Linked list of contours, then Y-sorted vertices .
154 Vertex* fNext; // "
155 Edge* fFirstEdgeAbove; // Linked list of edges above this vertex.
156 Edge* fLastEdgeAbove; // "
157 Edge* fFirstEdgeBelow; // Linked list of edges below this vertex.
158 Edge* fLastEdgeBelow; // "
159 bool fProcessed; // Has this vertex been seen in simplify()?
160 #if LOGGING_ENABLED
161 float fID; // Identifier used for logging.
162 #endif
163 };
164
165 /******************************************************************************* ********/ 256 /******************************************************************************* ********/
166 257
167 typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b); 258 typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b);
168 259
169 struct Comparator { 260 struct Comparator {
170 CompareFunc sweep_lt; 261 CompareFunc sweep_lt;
171 CompareFunc sweep_gt; 262 CompareFunc sweep_gt;
172 }; 263 };
173 264
174 bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) { 265 bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) {
(...skipping 11 matching lines...) Expand all
186 bool sweep_gt_vert(const SkPoint& a, const SkPoint& b) { 277 bool sweep_gt_vert(const SkPoint& a, const SkPoint& b) {
187 return a.fY == b.fY ? a.fX > b.fX : a.fY > b.fY; 278 return a.fY == b.fY ? a.fX > b.fX : a.fY > b.fY;
188 } 279 }
189 280
190 inline SkPoint* emit_vertex(Vertex* v, SkPoint* data) { 281 inline SkPoint* emit_vertex(Vertex* v, SkPoint* data) {
191 *data++ = v->fPoint; 282 *data++ = v->fPoint;
192 return data; 283 return data;
193 } 284 }
194 285
195 SkPoint* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, SkPoint* data) { 286 SkPoint* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, SkPoint* data) {
196 #if WIREFRAME 287 #if TESSELLATOR_WIREFRAME
197 data = emit_vertex(v0, data); 288 data = emit_vertex(v0, data);
198 data = emit_vertex(v1, data); 289 data = emit_vertex(v1, data);
199 data = emit_vertex(v1, data); 290 data = emit_vertex(v1, data);
200 data = emit_vertex(v2, data); 291 data = emit_vertex(v2, data);
201 data = emit_vertex(v2, data); 292 data = emit_vertex(v2, data);
202 data = emit_vertex(v0, data); 293 data = emit_vertex(v0, data);
203 #else 294 #else
204 data = emit_vertex(v0, data); 295 data = emit_vertex(v0, data);
205 data = emit_vertex(v1, data); 296 data = emit_vertex(v1, data);
206 data = emit_vertex(v2, data); 297 data = emit_vertex(v2, data);
207 #endif 298 #endif
208 return data; 299 return data;
209 } 300 }
210 301
211 struct EdgeList {
212 EdgeList() : fHead(nullptr), fTail(nullptr) {}
213 Edge* fHead;
214 Edge* fTail;
215 };
216
217 /** 302 /**
218 * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and 303 * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and
219 * "edge below" a vertex as well as for the active edge list is handled by isLef tOf()/isRightOf(). 304 * "edge below" a vertex as well as for the active edge list is handled by isLef tOf()/isRightOf().
220 * Note that an Edge will give occasionally dist() != 0 for its own endpoints (b ecause floating 305 * Note that an Edge will give occasionally dist() != 0 for its own endpoints (b ecause floating
221 * point). For speed, that case is only tested by the callers which require it ( e.g., 306 * point). For speed, that case is only tested by the callers which require it ( e.g.,
222 * cleanup_active_edges()). Edges also handle checking for intersection with oth er edges. 307 * cleanup_active_edges()). Edges also handle checking for intersection with oth er edges.
223 * Currently, this converts the edges to the parametric form, in order to avoid doing a division 308 * Currently, this converts the edges to the parametric form, in order to avoid doing a division
224 * until an intersection has been confirmed. This is slightly slower in the "fou nd" case, but 309 * until an intersection has been confirmed. This is slightly slower in the "fou nd" case, but
225 * a lot faster in the "not found" case. 310 * a lot faster in the "not found" case.
226 * 311 *
227 * The coefficients of the line equation stored in double precision to avoid cat astrphic 312 * The coefficients of the line equation stored in double precision to avoid cat astrphic
228 * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is 313 * cancellation in the isLeftOf() and isRightOf() checks. Using doubles ensures that the result is
229 * correct in float, since it's a polynomial of degree 2. The intersect() functi on, being 314 * correct in float, since it's a polynomial of degree 2. The intersect() functi on, being
230 * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its 315 * degree 5, is still subject to catastrophic cancellation. We deal with that by assuming its
231 * output may be incorrect, and adjusting the mesh topology to match (see commen t at the top of 316 * output may be incorrect, and adjusting the mesh topology to match (see commen t at the top of
232 * this file). 317 * this file).
233 */ 318 */
234 319
235 struct Edge {
236 Edge(Vertex* top, Vertex* bottom, int winding)
237 : fWinding(winding)
238 , fTop(top)
239 , fBottom(bottom)
240 , fLeft(nullptr)
241 , fRight(nullptr)
242 , fPrevEdgeAbove(nullptr)
243 , fNextEdgeAbove(nullptr)
244 , fPrevEdgeBelow(nullptr)
245 , fNextEdgeBelow(nullptr)
246 , fLeftPoly(nullptr)
247 , fRightPoly(nullptr) {
248 recompute();
249 }
250 int fWinding; // 1 == edge goes downward; -1 = edge goes upwar d.
251 Vertex* fTop; // The top vertex in vertex-sort-order (sweep_lt ).
252 Vertex* fBottom; // The bottom vertex in vertex-sort-order.
253 Edge* fLeft; // The linked list of edges in the active edge l ist.
254 Edge* fRight; // "
255 Edge* fPrevEdgeAbove; // The linked list of edges in the bottom Vertex 's "edges above".
256 Edge* fNextEdgeAbove; // "
257 Edge* fPrevEdgeBelow; // The linked list of edges in the top Vertex's "edges below".
258 Edge* fNextEdgeBelow; // "
259 Poly* fLeftPoly; // The Poly to the left of this edge, if any.
260 Poly* fRightPoly; // The Poly to the right of this edge, if any.
261 double fDX; // The line equation for this edge, in implicit form.
262 double fDY; // fDY * x + fDX * y + fC = 0, for point (x, y) on the line.
263 double fC;
264 double dist(const SkPoint& p) const {
265 return fDY * p.fX - fDX * p.fY + fC;
266 }
267 bool isRightOf(Vertex* v) const {
268 return dist(v->fPoint) < 0.0;
269 }
270 bool isLeftOf(Vertex* v) const {
271 return dist(v->fPoint) > 0.0;
272 }
273 void recompute() {
274 fDX = static_cast<double>(fBottom->fPoint.fX) - fTop->fPoint.fX;
275 fDY = static_cast<double>(fBottom->fPoint.fY) - fTop->fPoint.fY;
276 fC = static_cast<double>(fTop->fPoint.fY) * fBottom->fPoint.fX -
277 static_cast<double>(fTop->fPoint.fX) * fBottom->fPoint.fY;
278 }
279 bool intersect(const Edge& other, SkPoint* p) {
280 LOG("intersecting %g -> %g with %g -> %g\n",
281 fTop->fID, fBottom->fID,
282 other.fTop->fID, other.fBottom->fID);
283 if (fTop == other.fTop || fBottom == other.fBottom) {
284 return false;
285 }
286 double denom = fDX * other.fDY - fDY * other.fDX;
287 if (denom == 0.0) {
288 return false;
289 }
290 double dx = static_cast<double>(fTop->fPoint.fX) - other.fTop->fPoint.fX ;
291 double dy = static_cast<double>(fTop->fPoint.fY) - other.fTop->fPoint.fY ;
292 double sNumer = dy * other.fDX - dx * other.fDY;
293 double tNumer = dy * fDX - dx * fDY;
294 // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
295 // This saves us doing the divide below unless absolutely necessary.
296 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNu mer > denom)
297 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNu mer < denom)) {
298 return false;
299 }
300 double s = sNumer / denom;
301 SkASSERT(s >= 0.0 && s <= 1.0);
302 p->fX = SkDoubleToScalar(fTop->fPoint.fX + s * fDX);
303 p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fDY);
304 return true;
305 }
306 bool isActive(EdgeList* activeEdges) const {
307 return activeEdges && (fLeft || fRight || activeEdges->fHead == this);
308 }
309 };
310
311 /******************************************************************************* ********/ 320 /******************************************************************************* ********/
312 321
313 struct Poly { 322 struct Poly {
314 Poly(int winding) 323 Poly(int winding)
315 : fWinding(winding) 324 : fWinding(winding)
316 , fHead(nullptr) 325 , fHead(nullptr)
317 , fTail(nullptr) 326 , fTail(nullptr)
318 , fActive(nullptr) 327 , fActive(nullptr)
319 , fNext(nullptr) 328 , fNext(nullptr)
320 , fPartner(nullptr) 329 , fPartner(nullptr)
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 fTail->fNext = newV; 363 fTail->fNext = newV;
355 fTail = newV; 364 fTail = newV;
356 } else { 365 } else {
357 newV->fNext = fHead; 366 newV->fNext = fHead;
358 fHead->fPrev = newV; 367 fHead->fPrev = newV;
359 fHead = newV; 368 fHead = newV;
360 } 369 }
361 return done; 370 return done;
362 } 371 }
363 372
364 SkPoint* emit(SkPoint* data) { 373 SkPoint* emit(int winding, SkPoint* data) {
365 Vertex* first = fHead; 374 Vertex* first = fHead;
366 Vertex* v = first->fNext; 375 Vertex* v = first->fNext;
367 while (v != fTail) { 376 while (v != fTail) {
368 SkASSERT(v && v->fPrev && v->fNext); 377 SkASSERT(v && v->fPrev && v->fNext);
369 Vertex* prev = v->fPrev; 378 Vertex* prev = v->fPrev;
370 Vertex* curr = v; 379 Vertex* curr = v;
371 Vertex* next = v->fNext; 380 Vertex* next = v->fNext;
372 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint. fX; 381 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint. fX;
373 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint. fY; 382 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint. fY;
374 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint. fX; 383 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint. fX;
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
428 fPartner = fPartner->fPartner = nullptr; 437 fPartner = fPartner->fPartner = nullptr;
429 } 438 }
430 addVertex(v, fActive->fSide == kLeft_Side ? kRight_Side : kLeft_Side, al loc); 439 addVertex(v, fActive->fSide == kLeft_Side ? kRight_Side : kLeft_Side, al loc);
431 } 440 }
432 SkPoint* emit(SkPoint *data) { 441 SkPoint* emit(SkPoint *data) {
433 if (fCount < 3) { 442 if (fCount < 3) {
434 return data; 443 return data;
435 } 444 }
436 LOG("emit() %d, size %d\n", fID, fCount); 445 LOG("emit() %d, size %d\n", fID, fCount);
437 for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) { 446 for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) {
438 data = m->emit(data); 447 data = m->emit(fWinding, data);
439 } 448 }
440 return data; 449 return data;
441 } 450 }
442 int fWinding; 451 int fWinding;
443 MonotonePoly* fHead; 452 MonotonePoly* fHead;
444 MonotonePoly* fTail; 453 MonotonePoly* fTail;
445 MonotonePoly* fActive; 454 MonotonePoly* fActive;
446 Poly* fNext; 455 Poly* fNext;
447 Poly* fPartner; 456 Poly* fPartner;
448 int fCount; 457 int fCount;
449 #if LOGGING_ENABLED 458 #if LOGGING_ENABLED
450 int fID; 459 int fID;
451 #endif 460 #endif
452 }; 461 };
453 462
454 /******************************************************************************* ********/ 463 /******************************************************************************* ********/
455 464
456 bool coincident(const SkPoint& a, const SkPoint& b) { 465 bool coincident(const SkPoint& a, const SkPoint& b) {
457 return a == b; 466 return a == b;
458 } 467 }
459 468
460 Poly* new_poly(Poly** head, Vertex* v, int winding, SkChunkAlloc& alloc) { 469 Poly* new_poly(Poly** head, Vertex* v, int winding, SkChunkAlloc& alloc) {
461 Poly* poly = ALLOC_NEW(Poly, (winding), alloc); 470 Poly* poly = ALLOC_NEW(Poly, (winding), alloc);
462 poly->addVertex(v, Poly::kNeither_Side, alloc); 471 poly->addVertex(v, Poly::kNeither_Side, alloc);
463 poly->fNext = *head; 472 poly->fNext = *head;
464 *head = poly; 473 *head = poly;
465 return poly; 474 return poly;
466 } 475 }
467 476
468 Vertex* append_point_to_contour(const SkPoint& p, Vertex* prev, Vertex** head, 477 Vertex* append_point_to_contour(const SkPoint& p, Vertex* prev,
469 SkChunkAlloc& alloc) { 478 Vertex** head, SkChunkAlloc& alloc) {
470 Vertex* v = ALLOC_NEW(Vertex, (p), alloc); 479 Vertex* v = ALLOC_NEW(Vertex, (p), alloc);
471 #if LOGGING_ENABLED 480 #if LOGGING_ENABLED
472 static float gID = 0.0f; 481 static float gID = 0.0f;
473 v->fID = gID++; 482 v->fID = gID++;
474 #endif 483 #endif
475 if (prev) { 484 if (prev) {
476 prev->fNext = v; 485 prev->fNext = v;
477 v->fPrev = prev; 486 v->fPrev = prev;
478 } else { 487 } else {
479 *head = v; 488 *head = v;
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
534 pointsLeft >>= 1; 543 pointsLeft >>= 1;
535 prev = generate_cubic_points(p0, q[0], r[0], s, tolSqd, prev, head, pointsLe ft, alloc); 544 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); 545 prev = generate_cubic_points(s, r[1], q[2], p3, tolSqd, prev, head, pointsLe ft, alloc);
537 return prev; 546 return prev;
538 } 547 }
539 548
540 // Stage 1: convert the input path to a set of linear contours (linked list of V ertices). 549 // Stage 1: convert the input path to a set of linear contours (linked list of V ertices).
541 550
542 void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clip Bounds, 551 void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clip Bounds,
543 Vertex** contours, SkChunkAlloc& alloc, bool *isLinear) { 552 Vertex** contours, SkChunkAlloc& alloc, bool *isLinear) {
544
545 SkScalar toleranceSqd = tolerance * tolerance; 553 SkScalar toleranceSqd = tolerance * tolerance;
546 554
547 SkPoint pts[4]; 555 SkPoint pts[4];
548 bool done = false; 556 bool done = false;
549 *isLinear = true; 557 *isLinear = true;
550 SkPath::Iter iter(path, false); 558 SkPath::Iter iter(path, false);
551 Vertex* prev = nullptr; 559 Vertex* prev = nullptr;
552 Vertex* head = nullptr; 560 Vertex* head = nullptr;
553 if (path.isInverseFillType()) { 561 if (path.isInverseFillType()) {
554 SkPoint quad[4]; 562 SkPoint quad[4];
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after
843 } 851 }
844 if (edge->fPrevEdgeBelow && (edge->fBottom == edge->fPrevEdgeBelow->fBottom || 852 if (edge->fPrevEdgeBelow && (edge->fBottom == edge->fPrevEdgeBelow->fBottom ||
845 !edge->fPrevEdgeBelow->isLeftOf(edge->fBottom)) ) { 853 !edge->fPrevEdgeBelow->isLeftOf(edge->fBottom)) ) {
846 merge_edges_below(edge, edge->fPrevEdgeBelow, activeEdges, c); 854 merge_edges_below(edge, edge->fPrevEdgeBelow, activeEdges, c);
847 } else if (edge->fNextEdgeBelow && (edge->fBottom == edge->fNextEdgeBelow->f Bottom || 855 } else if (edge->fNextEdgeBelow && (edge->fBottom == edge->fNextEdgeBelow->f Bottom ||
848 !edge->isLeftOf(edge->fNextEdgeBelow->fB ottom))) { 856 !edge->isLeftOf(edge->fNextEdgeBelow->fB ottom))) {
849 merge_edges_below(edge, edge->fNextEdgeBelow, activeEdges, c); 857 merge_edges_below(edge, edge->fNextEdgeBelow, activeEdges, c);
850 } 858 }
851 } 859 }
852 860
853 void split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkC hunkAlloc& alloc); 861 void split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c,
862 SkChunkAlloc& alloc);
854 863
855 void cleanup_active_edges(Edge* edge, EdgeList* activeEdges, Comparator& c, SkCh unkAlloc& alloc) { 864 void cleanup_active_edges(Edge* edge, EdgeList* activeEdges, Comparator& c, SkCh unkAlloc& alloc) {
856 Vertex* top = edge->fTop; 865 Vertex* top = edge->fTop;
857 Vertex* bottom = edge->fBottom; 866 Vertex* bottom = edge->fBottom;
858 if (edge->fLeft) { 867 if (edge->fLeft) {
859 Vertex* leftTop = edge->fLeft->fTop; 868 Vertex* leftTop = edge->fLeft->fTop;
860 Vertex* leftBottom = edge->fLeft->fBottom; 869 Vertex* leftBottom = edge->fLeft->fBottom;
861 if (c.sweep_gt(top->fPoint, leftTop->fPoint) && !edge->fLeft->isLeftOf(t op)) { 870 if (c.sweep_gt(top->fPoint, leftTop->fPoint) && !edge->fLeft->isLeftOf(t op)) {
862 split_edge(edge->fLeft, edge->fTop, activeEdges, c, alloc); 871 split_edge(edge->fLeft, edge->fTop, activeEdges, c, alloc);
863 } else if (c.sweep_gt(leftTop->fPoint, top->fPoint) && !edge->isRightOf( leftTop)) { 872 } else if (c.sweep_gt(leftTop->fPoint, top->fPoint) && !edge->isRightOf( leftTop)) {
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after
914 edge = next; 923 edge = next;
915 } 924 }
916 for (Edge* edge = src->fFirstEdgeBelow; edge;) { 925 for (Edge* edge = src->fFirstEdgeBelow; edge;) {
917 Edge* next = edge->fNextEdgeBelow; 926 Edge* next = edge->fNextEdgeBelow;
918 set_top(edge, dst, nullptr, c); 927 set_top(edge, dst, nullptr, c);
919 edge = next; 928 edge = next;
920 } 929 }
921 remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(src, head, nullptr); 930 remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(src, head, nullptr);
922 } 931 }
923 932
924 Vertex* check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, C omparator& c, 933 Vertex* check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, C omparator& c,
925 SkChunkAlloc& alloc) { 934 SkChunkAlloc& alloc) {
926 SkPoint p; 935 SkPoint p;
927 if (!edge || !other) { 936 if (!edge || !other) {
928 return nullptr; 937 return nullptr;
929 } 938 }
930 if (edge->intersect(*other, &p)) { 939 if (edge->intersect(*other, &p)) {
931 Vertex* v; 940 Vertex* v;
932 LOG("found intersection, pt is %g, %g\n", p.fX, p.fY); 941 LOG("found intersection, pt is %g, %g\n", p.fX, p.fY);
933 if (p == edge->fTop->fPoint || c.sweep_lt(p, edge->fTop->fPoint)) { 942 if (p == edge->fTop->fPoint || c.sweep_lt(p, edge->fTop->fPoint)) {
934 split_edge(other, edge->fTop, activeEdges, c, alloc); 943 split_edge(other, edge->fTop, activeEdges, c, alloc);
(...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after
1144 if (check_for_intersection(edge, leftEnclosingEdge, &activeE dges, c, alloc)) { 1153 if (check_for_intersection(edge, leftEnclosingEdge, &activeE dges, c, alloc)) {
1145 restartChecks = true; 1154 restartChecks = true;
1146 break; 1155 break;
1147 } 1156 }
1148 if (check_for_intersection(edge, rightEnclosingEdge, &active Edges, c, alloc)) { 1157 if (check_for_intersection(edge, rightEnclosingEdge, &active Edges, c, alloc)) {
1149 restartChecks = true; 1158 restartChecks = true;
1150 break; 1159 break;
1151 } 1160 }
1152 } 1161 }
1153 } else { 1162 } else {
1154 if (Vertex* pv = check_for_intersection(leftEnclosingEdge, right EnclosingEdge, 1163 if (Vertex* pv = check_for_intersection(leftEnclosingEdge, right EnclosingEdge,
1155 &activeEdges, c, alloc)) { 1164 &activeEdges, c, alloc)) {
1156 if (c.sweep_lt(pv->fPoint, v->fPoint)) { 1165 if (c.sweep_lt(pv->fPoint, v->fPoint)) {
1157 v = pv; 1166 v = pv;
1158 } 1167 }
1159 restartChecks = true; 1168 restartChecks = true;
1160 } 1169 }
1161 1170
1162 } 1171 }
1163 } while (restartChecks); 1172 } while (restartChecks);
1164 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { 1173 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) {
(...skipping 121 matching lines...) Expand 10 before | Expand all | Expand 10 after
1286 LOG("%g -> %g, lpoly %d, rpoly %d\n", e->fTop->fID, e->fBottom->fID, 1295 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); 1296 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRight Poly->fID : -1);
1288 } 1297 }
1289 #endif 1298 #endif
1290 } 1299 }
1291 return polys; 1300 return polys;
1292 } 1301 }
1293 1302
1294 // This is a driver function which calls stages 2-5 in turn. 1303 // This is a driver function which calls stages 2-5 in turn.
1295 1304
1296 Poly* contours_to_polys(Vertex** contours, int contourCnt, Comparator& c, SkChun kAlloc& alloc) { 1305 Poly* contours_to_polys(Vertex** contours, int contourCnt, SkRect pathBounds, Sk ChunkAlloc& alloc) {
1306 Comparator c;
1307 if (pathBounds.width() > pathBounds.height()) {
1308 c.sweep_lt = sweep_lt_horiz;
1309 c.sweep_gt = sweep_gt_horiz;
1310 } else {
1311 c.sweep_lt = sweep_lt_vert;
1312 c.sweep_gt = sweep_gt_vert;
1313 }
1297 #if LOGGING_ENABLED 1314 #if LOGGING_ENABLED
1298 for (int i = 0; i < contourCnt; ++i) { 1315 for (int i = 0; i < contourCnt; ++i) {
1299 Vertex* v = contours[i]; 1316 Vertex* v = contours[i];
1300 SkASSERT(v); 1317 SkASSERT(v);
1301 LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); 1318 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) { 1319 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); 1320 LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1304 } 1321 }
1305 } 1322 }
1306 #endif 1323 #endif
1307 sanitize_contours(contours, contourCnt); 1324 sanitize_contours(contours, contourCnt);
1308 Vertex* vertices = build_edges(contours, contourCnt, c, alloc); 1325 Vertex* vertices = build_edges(contours, contourCnt, c, alloc);
1309 if (!vertices) { 1326 if (!vertices) {
1310 return nullptr; 1327 return nullptr;
1311 } 1328 }
1312 1329
1313 // Sort vertices in Y (secondarily in X). 1330 // Sort vertices in Y (secondarily in X).
1314 merge_sort(&vertices, c); 1331 merge_sort(&vertices, c);
1315 merge_coincident_vertices(&vertices, c, alloc); 1332 merge_coincident_vertices(&vertices, c, alloc);
1316 #if LOGGING_ENABLED 1333 #if LOGGING_ENABLED
1317 for (Vertex* v = vertices; v != nullptr; v = v->fNext) { 1334 for (Vertex* v = vertices; v != nullptr; v = v->fNext) {
1318 static float gID = 0.0f; 1335 static float gID = 0.0f;
1319 v->fID = gID++; 1336 v->fID = gID++;
1320 } 1337 }
1321 #endif 1338 #endif
1322 simplify(vertices, c, alloc); 1339 simplify(vertices, c, alloc);
1323 return tessellate(vertices, alloc); 1340 return tessellate(vertices, alloc);
1324 } 1341 }
1325 1342
1343 Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBo unds,
1344 bool* isLinear) {
1345 int contourCnt;
1346 int maxPts = GrPathUtils::worstCasePointCount(path, &contourCnt, tolerance);
1347 if (maxPts <= 0) {
1348 return nullptr;
1349 }
1350 if (maxPts > ((int)SK_MaxU16 + 1)) {
1351 SkDebugf("Path not rendered, too many verts (%d)\n", maxPts);
1352 return nullptr;
1353 }
1354 SkPath::FillType fillType = path.getFillType();
1355 if (SkPath::IsInverseFillType(fillType)) {
1356 contourCnt++;
1357 }
1358 SkAutoTDeleteArray<Vertex*> contours(new Vertex* [contourCnt]);
1359
1360 // For the initial size of the chunk allocator, estimate based on the point count:
1361 // one vertex per point for the initial passes, plus two for the vertices in the
1362 // resulting Polys, since the same point may end up in two Polys. Assume mi nimal
1363 // connectivity of one Edge per Vertex (will grow for intersections).
1364 SkChunkAlloc alloc(maxPts * (3 * sizeof(Vertex) + sizeof(Edge)));
1365 path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinea r);
1366 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
1367 }
1368
1326 // Stage 6: Triangulate the monotone polygons into a vertex buffer. 1369 // Stage 6: Triangulate the monotone polygons into a vertex buffer.
1327 1370
1328 SkPoint* polys_to_triangles(Poly* polys, SkPath::FillType fillType, SkPoint* dat a) { 1371 int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBo unds,
1329 SkPoint* d = data; 1372 GrResourceProvider* resourceProvider,
1373 SkAutoTUnref<GrVertexBuffer>& vertexBuffer, bool canMapVB, b ool* isLinear) {
1374 Poly* polys = path_to_polys(path, tolerance, clipBounds, isLinear);
1375 SkPath::FillType fillType = path.getFillType();
1376 int count = 0;
1377 for (Poly* poly = polys; poly; poly = poly->fNext) {
1378 if (apply_fill_type(fillType, poly->fWinding) && poly->fCount >= 3) {
1379 count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3);
1380 }
1381 }
1382 if (0 == count) {
1383 return 0;
1384 }
1385
1386 size_t size = count * sizeof(SkPoint);
1387 if (!vertexBuffer.get() || vertexBuffer->gpuMemorySize() < size) {
1388 vertexBuffer.reset(resourceProvider->createVertexBuffer(
1389 size, GrResourceProvider::kStatic_BufferUsage, 0));
1390 }
1391 if (!vertexBuffer.get()) {
1392 SkDebugf("Could not allocate vertices\n");
1393 return 0;
1394 }
1395 SkPoint* verts;
1396 if (canMapVB) {
1397 verts = static_cast<SkPoint*>(vertexBuffer->map());
1398 } else {
1399 verts = new SkPoint[count];
1400 }
1401 SkPoint* end = verts;
1330 for (Poly* poly = polys; poly; poly = poly->fNext) { 1402 for (Poly* poly = polys; poly; poly = poly->fNext) {
1331 if (apply_fill_type(fillType, poly->fWinding)) { 1403 if (apply_fill_type(fillType, poly->fWinding)) {
1332 d = poly->emit(d); 1404 end = poly->emit(end);
1333 } 1405 }
1334 } 1406 }
1335 return d; 1407 int actualCount = static_cast<int>(end - verts);
1408 LOG("actual count: %d\n", actualCount);
1409 SkASSERT(actualCount <= count);
1410 if (canMapVB) {
1411 vertexBuffer->unmap();
1412 } else {
1413 vertexBuffer->updateData(verts, actualCount * sizeof(SkPoint));
1414 delete[] verts;
1415 }
1416
1417 return actualCount;
1336 } 1418 }
1337 1419
1338 struct TessInfo { 1420 int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBou nds,
1339 SkScalar fTolerance; 1421 WindingVertex** verts) {
1340 int fCount; 1422 bool isLinear;
1341 }; 1423 Poly* polys = path_to_polys(path, tolerance, clipBounds, &isLinear);
1424 SkPath::FillType fillType = path.getFillType();
1425 int count = 0;
1426 for (Poly* poly = polys; poly; poly = poly->fNext) {
1427 if (apply_fill_type(fillType, poly->fWinding) && poly->fCount >= 3) {
1428 count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3);
1429 }
1430 }
1431 if (0 == count) {
1432 *verts = nullptr;
1433 return 0;
1434 }
1342 1435
1343 bool cache_match(GrVertexBuffer* vertexBuffer, SkScalar tol, int* actualCount) { 1436 *verts = new WindingVertex[count];
1344 if (!vertexBuffer) { 1437 WindingVertex* vertsEnd = *verts;
1345 return false; 1438 SkPoint* points = new SkPoint[count];
1439 SkPoint* pointsEnd = points;
1440 for (Poly* poly = polys; poly; poly = poly->fNext) {
1441 if (apply_fill_type(fillType, poly->fWinding)) {
1442 SkPoint* start = pointsEnd;
1443 pointsEnd = poly->emit(pointsEnd);
1444 while (start != pointsEnd) {
1445 vertsEnd->fPos = *start;
1446 vertsEnd->fWinding = poly->fWinding;
1447 ++start;
1448 ++vertsEnd;
1449 }
1450 }
1346 } 1451 }
1347 const SkData* data = vertexBuffer->getUniqueKey().getCustomData(); 1452 int actualCount = static_cast<int>(vertsEnd - *verts);
1348 SkASSERT(data); 1453 SkASSERT(actualCount <= count);
1349 const TessInfo* info = static_cast<const TessInfo*>(data->data()); 1454 SkASSERT(pointsEnd - points == actualCount);
1350 if (info->fTolerance == 0 || info->fTolerance < 3.0f * tol) { 1455 delete[] points;
1351 *actualCount = info->fCount; 1456 return actualCount;
1352 return true;
1353 }
1354 return false;
1355 } 1457 }
1356 1458
1357 };
1358
1359 GrTessellatingPathRenderer::GrTessellatingPathRenderer() {
1360 } 1459 }
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
OLDNEW
« 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