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

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
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 {
95 90
96 struct Vertex; 91 /**
97 struct Edge; 92 * Vertices are used in three ways: first, the path contours are converted into a circularly-linked
93 * list of vertices for each contour. After edge construction, the same vertices are re-ordered by
94 * the merge sort according to the sweep_lt comparator (usually, increasing in Y ) using the same
95 * fPrev/fNext pointers that were used for the contours, to avoid reallocation. Finally,
96 * MonotonePolys are built containing a circularly-linked list of vertices. Curr ently, those
97 * Vertices are newly-allocated for the MonotonePolys, since an individual verte x from the path mesh
98 * may belong to multiple MonotonePolys, so the original vertices cannot be re-u sed.
99 */
100 struct Vertex {
101 Vertex(const SkPoint& point)
102 : fPoint(point), fPrev(nullptr), fNext(nullptr)
103 , fFirstEdgeAbove(nullptr), fLastEdgeAbove(nullptr)
104 , fFirstEdgeBelow(nullptr), fLastEdgeBelow(nullptr)
105 , fProcessed(false)
106 #if LOGGING_ENABLED
107 , fID (-1.0f)
108 #endif
109 {}
110 SkPoint fPoint; // Vertex position
111 Vertex* fPrev; // Linked list of contours, then Y-sorted vertices .
112 Vertex* fNext; // "
113 Edge* fFirstEdgeAbove; // Linked list of edges above this vertex.
114 Edge* fLastEdgeAbove; // "
115 Edge* fFirstEdgeBelow; // Linked list of edges below this vertex.
116 Edge* fLastEdgeBelow; // "
117 bool fProcessed; // Has this vertex been seen in simplify()?
118 #if LOGGING_ENABLED
119 float fID; // Identifier used for logging.
120 #endif
121 };
122
123 double Edge::dist(const SkPoint& p) const {
124 return fDY * p.fX - fDX * p.fY + fC;
125 }
126
127 bool Edge::isRightOf(Vertex* v) const {
128 return dist(v->fPoint) < 0.0;
129 }
130
131 bool Edge::isLeftOf(Vertex* v) const {
132 return dist(v->fPoint) > 0.0;
133 }
134
135 void Edge::recompute() {
136 fDX = static_cast<double>(fBottom->fPoint.fX) - fTop->fPoint.fX;
137 fDY = static_cast<double>(fBottom->fPoint.fY) - fTop->fPoint.fY;
138 fC = static_cast<double>(fTop->fPoint.fY) * fBottom->fPoint.fX -
139 static_cast<double>(fTop->fPoint.fX) * fBottom->fPoint.fY;
140 }
141
142 bool Edge::intersect(const Edge& other, SkPoint* p) {
143 #if LOGGING_ENABLED
144 LOG("intersecting %g -> %g with %g -> %g\n",
145 fTop->fID, fBottom->fID,
146 other.fTop->fID, other.fBottom->fID);
147 #endif
148 if (fTop == other.fTop || fBottom == other.fBottom) {
149 return false;
150 }
151 double denom = fDX * other.fDY - fDY * other.fDX;
152 if (denom == 0.0) {
153 return false;
154 }
155 double dx = static_cast<double>(fTop->fPoint.fX) - other.fTop->fPoint.fX;
156 double dy = static_cast<double>(fTop->fPoint.fY) - other.fTop->fPoint.fY;
157 double sNumer = dy * other.fDX - dx * other.fDY;
158 double tNumer = dy * fDX - dx * fDY;
159 // If (sNumer / denom) or (tNumer / denom) is not in [0..1], exit early.
160 // This saves us doing the divide below unless absolutely necessary.
161 if (denom > 0.0 ? (sNumer < 0.0 || sNumer > denom || tNumer < 0.0 || tNumer > denom)
162 : (sNumer > 0.0 || sNumer < denom || tNumer > 0.0 || tNumer < denom)) {
163 return false;
164 }
165 double s = sNumer / denom;
166 SkASSERT(s >= 0.0 && s <= 1.0);
167 p->fX = SkDoubleToScalar(fTop->fPoint.fX + s * fDX);
168 p->fY = SkDoubleToScalar(fTop->fPoint.fY + s * fDY);
169 return true;
170 }
171
172 bool Edge::isActive(EdgeList* activeEdges) const {
173 return activeEdges && (fLeft || fRight || activeEdges->fHead == this);
174 }
175
98 struct Poly; 176 struct Poly;
99 177
100 template <class T, T* T::*Prev, T* T::*Next> 178 template <class T, T* T::*Prev, T* T::*Next>
101 void insert(T* t, T* prev, T* next, T** head, T** tail) { 179 void insert(T* t, T* prev, T* next, T** head, T** tail) {
102 t->*Prev = prev; 180 t->*Prev = prev;
103 t->*Next = next; 181 t->*Next = next;
104 if (prev) { 182 if (prev) {
105 prev->*Next = t; 183 prev->*Next = t;
106 } else if (head) { 184 } else if (head) {
107 *head = t; 185 *head = t;
(...skipping 13 matching lines...) Expand all
121 *head = t->*Next; 199 *head = t->*Next;
122 } 200 }
123 if (t->*Next) { 201 if (t->*Next) {
124 t->*Next->*Prev = t->*Prev; 202 t->*Next->*Prev = t->*Prev;
125 } else if (tail) { 203 } else if (tail) {
126 *tail = t->*Prev; 204 *tail = t->*Prev;
127 } 205 }
128 t->*Prev = t->*Next = nullptr; 206 t->*Prev = t->*Next = nullptr;
129 } 207 }
130 208
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 /******************************************************************************* ********/ 209 /******************************************************************************* ********/
166 210
167 typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b); 211 typedef bool (*CompareFunc)(const SkPoint& a, const SkPoint& b);
168 212
169 struct Comparator { 213 struct Comparator {
170 CompareFunc sweep_lt; 214 CompareFunc sweep_lt;
171 CompareFunc sweep_gt; 215 CompareFunc sweep_gt;
172 }; 216 };
173 217
174 bool sweep_lt_horiz(const SkPoint& a, const SkPoint& b) { 218 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) { 230 bool sweep_gt_vert(const SkPoint& a, const SkPoint& b) {
187 return a.fY == b.fY ? a.fX > b.fX : a.fY > b.fY; 231 return a.fY == b.fY ? a.fX > b.fX : a.fY > b.fY;
188 } 232 }
189 233
190 inline SkPoint* emit_vertex(Vertex* v, SkPoint* data) { 234 inline SkPoint* emit_vertex(Vertex* v, SkPoint* data) {
191 *data++ = v->fPoint; 235 *data++ = v->fPoint;
192 return data; 236 return data;
193 } 237 }
194 238
195 SkPoint* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, SkPoint* data) { 239 SkPoint* emit_triangle(Vertex* v0, Vertex* v1, Vertex* v2, SkPoint* data) {
196 #if WIREFRAME 240 #if TESSELLATOR_WIREFRAME
197 data = emit_vertex(v0, data); 241 data = emit_vertex(v0, data);
198 data = emit_vertex(v1, data); 242 data = emit_vertex(v1, data);
199 data = emit_vertex(v1, data); 243 data = emit_vertex(v1, data);
200 data = emit_vertex(v2, data); 244 data = emit_vertex(v2, data);
201 data = emit_vertex(v2, data); 245 data = emit_vertex(v2, data);
202 data = emit_vertex(v0, data); 246 data = emit_vertex(v0, data);
203 #else 247 #else
204 data = emit_vertex(v0, data); 248 data = emit_vertex(v0, data);
205 data = emit_vertex(v1, data); 249 data = emit_vertex(v1, data);
206 data = emit_vertex(v2, data); 250 data = emit_vertex(v2, data);
207 #endif 251 #endif
208 return data; 252 return data;
209 } 253 }
210 254
211 struct EdgeList {
212 EdgeList() : fHead(nullptr), fTail(nullptr) {}
213 Edge* fHead;
214 Edge* fTail;
215 };
216
217 /** 255 /**
218 * An Edge joins a top Vertex to a bottom Vertex. Edge ordering for the list of "edges above" and 256 * 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(). 257 * "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 258 * 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., 259 * 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. 260 * 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 261 * 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 262 * 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. 263 * a lot faster in the "not found" case.
226 * 264 *
227 * The coefficients of the line equation stored in double precision to avoid cat astrphic 265 * 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 266 * 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 267 * 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 268 * 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 269 * output may be incorrect, and adjusting the mesh topology to match (see commen t at the top of
232 * this file). 270 * this file).
233 */ 271 */
234 272
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 /******************************************************************************* ********/ 273 /******************************************************************************* ********/
312 274
313 struct Poly { 275 struct Poly {
314 Poly(int winding) 276 Poly(int winding)
315 : fWinding(winding) 277 : fWinding(winding)
316 , fHead(nullptr) 278 , fHead(nullptr)
317 , fTail(nullptr) 279 , fTail(nullptr)
318 , fActive(nullptr) 280 , fActive(nullptr)
319 , fNext(nullptr) 281 , fNext(nullptr)
320 , fPartner(nullptr) 282 , fPartner(nullptr)
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
354 fTail->fNext = newV; 316 fTail->fNext = newV;
355 fTail = newV; 317 fTail = newV;
356 } else { 318 } else {
357 newV->fNext = fHead; 319 newV->fNext = fHead;
358 fHead->fPrev = newV; 320 fHead->fPrev = newV;
359 fHead = newV; 321 fHead = newV;
360 } 322 }
361 return done; 323 return done;
362 } 324 }
363 325
364 SkPoint* emit(SkPoint* data) { 326 SkPoint* emit(int winding, SkPoint* data) {
365 Vertex* first = fHead; 327 Vertex* first = fHead;
366 Vertex* v = first->fNext; 328 Vertex* v = first->fNext;
367 while (v != fTail) { 329 while (v != fTail) {
368 SkASSERT(v && v->fPrev && v->fNext); 330 SkASSERT(v && v->fPrev && v->fNext);
369 Vertex* prev = v->fPrev; 331 Vertex* prev = v->fPrev;
370 Vertex* curr = v; 332 Vertex* curr = v;
371 Vertex* next = v->fNext; 333 Vertex* next = v->fNext;
372 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint. fX; 334 double ax = static_cast<double>(curr->fPoint.fX) - prev->fPoint. fX;
373 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint. fY; 335 double ay = static_cast<double>(curr->fPoint.fY) - prev->fPoint. fY;
374 double bx = static_cast<double>(next->fPoint.fX) - curr->fPoint. fX; 336 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; 390 fPartner = fPartner->fPartner = nullptr;
429 } 391 }
430 addVertex(v, fActive->fSide == kLeft_Side ? kRight_Side : kLeft_Side, al loc); 392 addVertex(v, fActive->fSide == kLeft_Side ? kRight_Side : kLeft_Side, al loc);
431 } 393 }
432 SkPoint* emit(SkPoint *data) { 394 SkPoint* emit(SkPoint *data) {
433 if (fCount < 3) { 395 if (fCount < 3) {
434 return data; 396 return data;
435 } 397 }
436 LOG("emit() %d, size %d\n", fID, fCount); 398 LOG("emit() %d, size %d\n", fID, fCount);
437 for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) { 399 for (MonotonePoly* m = fHead; m != nullptr; m = m->fNext) {
438 data = m->emit(data); 400 data = m->emit(fWinding, data);
439 } 401 }
440 return data; 402 return data;
441 } 403 }
442 int fWinding; 404 int fWinding;
443 MonotonePoly* fHead; 405 MonotonePoly* fHead;
444 MonotonePoly* fTail; 406 MonotonePoly* fTail;
445 MonotonePoly* fActive; 407 MonotonePoly* fActive;
446 Poly* fNext; 408 Poly* fNext;
447 Poly* fPartner; 409 Poly* fPartner;
448 int fCount; 410 int fCount;
449 #if LOGGING_ENABLED 411 #if LOGGING_ENABLED
450 int fID; 412 int fID;
451 #endif 413 #endif
452 }; 414 };
453 415
454 /******************************************************************************* ********/ 416 /******************************************************************************* ********/
455 417
456 bool coincident(const SkPoint& a, const SkPoint& b) { 418 bool coincident(const SkPoint& a, const SkPoint& b) {
457 return a == b; 419 return a == b;
458 } 420 }
459 421
460 Poly* new_poly(Poly** head, Vertex* v, int winding, SkChunkAlloc& alloc) { 422 Poly* new_poly(Poly** head, Vertex* v, int winding, SkChunkAlloc& alloc) {
461 Poly* poly = ALLOC_NEW(Poly, (winding), alloc); 423 Poly* poly = ALLOC_NEW(Poly, (winding), alloc);
462 poly->addVertex(v, Poly::kNeither_Side, alloc); 424 poly->addVertex(v, Poly::kNeither_Side, alloc);
463 poly->fNext = *head; 425 poly->fNext = *head;
464 *head = poly; 426 *head = poly;
465 return poly; 427 return poly;
466 } 428 }
467 429
468 Vertex* append_point_to_contour(const SkPoint& p, Vertex* prev, Vertex** head, 430 Vertex* append_point_to_contour(const SkPoint& p, Vertex* prev,
469 SkChunkAlloc& alloc) { 431 Vertex** head, SkChunkAlloc& alloc) {
470 Vertex* v = ALLOC_NEW(Vertex, (p), alloc); 432 Vertex* v = ALLOC_NEW(Vertex, (p), alloc);
471 #if LOGGING_ENABLED 433 #if LOGGING_ENABLED
472 static float gID = 0.0f; 434 static float gID = 0.0f;
473 v->fID = gID++; 435 v->fID = gID++;
474 #endif 436 #endif
475 if (prev) { 437 if (prev) {
476 prev->fNext = v; 438 prev->fNext = v;
477 v->fPrev = prev; 439 v->fPrev = prev;
478 } else { 440 } else {
479 *head = v; 441 *head = v;
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after
534 pointsLeft >>= 1; 496 pointsLeft >>= 1;
535 prev = generate_cubic_points(p0, q[0], r[0], s, tolSqd, prev, head, pointsLe ft, alloc); 497 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); 498 prev = generate_cubic_points(s, r[1], q[2], p3, tolSqd, prev, head, pointsLe ft, alloc);
537 return prev; 499 return prev;
538 } 500 }
539 501
540 // Stage 1: convert the input path to a set of linear contours (linked list of V ertices). 502 // Stage 1: convert the input path to a set of linear contours (linked list of V ertices).
541 503
542 void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clip Bounds, 504 void path_to_contours(const SkPath& path, SkScalar tolerance, const SkRect& clip Bounds,
543 Vertex** contours, SkChunkAlloc& alloc, bool *isLinear) { 505 Vertex** contours, SkChunkAlloc& alloc, bool *isLinear) {
544
545 SkScalar toleranceSqd = tolerance * tolerance; 506 SkScalar toleranceSqd = tolerance * tolerance;
546 507
547 SkPoint pts[4]; 508 SkPoint pts[4];
548 bool done = false; 509 bool done = false;
549 *isLinear = true; 510 *isLinear = true;
550 SkPath::Iter iter(path, false); 511 SkPath::Iter iter(path, false);
551 Vertex* prev = nullptr; 512 Vertex* prev = nullptr;
552 Vertex* head = nullptr; 513 Vertex* head = nullptr;
553 if (path.isInverseFillType()) { 514 if (path.isInverseFillType()) {
554 SkPoint quad[4]; 515 SkPoint quad[4];
(...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after
843 } 804 }
844 if (edge->fPrevEdgeBelow && (edge->fBottom == edge->fPrevEdgeBelow->fBottom || 805 if (edge->fPrevEdgeBelow && (edge->fBottom == edge->fPrevEdgeBelow->fBottom ||
845 !edge->fPrevEdgeBelow->isLeftOf(edge->fBottom)) ) { 806 !edge->fPrevEdgeBelow->isLeftOf(edge->fBottom)) ) {
846 merge_edges_below(edge, edge->fPrevEdgeBelow, activeEdges, c); 807 merge_edges_below(edge, edge->fPrevEdgeBelow, activeEdges, c);
847 } else if (edge->fNextEdgeBelow && (edge->fBottom == edge->fNextEdgeBelow->f Bottom || 808 } else if (edge->fNextEdgeBelow && (edge->fBottom == edge->fNextEdgeBelow->f Bottom ||
848 !edge->isLeftOf(edge->fNextEdgeBelow->fB ottom))) { 809 !edge->isLeftOf(edge->fNextEdgeBelow->fB ottom))) {
849 merge_edges_below(edge, edge->fNextEdgeBelow, activeEdges, c); 810 merge_edges_below(edge, edge->fNextEdgeBelow, activeEdges, c);
850 } 811 }
851 } 812 }
852 813
853 void split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c, SkC hunkAlloc& alloc); 814 void split_edge(Edge* edge, Vertex* v, EdgeList* activeEdges, Comparator& c,
815 SkChunkAlloc& alloc);
854 816
855 void cleanup_active_edges(Edge* edge, EdgeList* activeEdges, Comparator& c, SkCh unkAlloc& alloc) { 817 void cleanup_active_edges(Edge* edge, EdgeList* activeEdges, Comparator& c, SkCh unkAlloc& alloc) {
856 Vertex* top = edge->fTop; 818 Vertex* top = edge->fTop;
857 Vertex* bottom = edge->fBottom; 819 Vertex* bottom = edge->fBottom;
858 if (edge->fLeft) { 820 if (edge->fLeft) {
859 Vertex* leftTop = edge->fLeft->fTop; 821 Vertex* leftTop = edge->fLeft->fTop;
860 Vertex* leftBottom = edge->fLeft->fBottom; 822 Vertex* leftBottom = edge->fLeft->fBottom;
861 if (c.sweep_gt(top->fPoint, leftTop->fPoint) && !edge->fLeft->isLeftOf(t op)) { 823 if (c.sweep_gt(top->fPoint, leftTop->fPoint) && !edge->fLeft->isLeftOf(t op)) {
862 split_edge(edge->fLeft, edge->fTop, activeEdges, c, alloc); 824 split_edge(edge->fLeft, edge->fTop, activeEdges, c, alloc);
863 } else if (c.sweep_gt(leftTop->fPoint, top->fPoint) && !edge->isRightOf( leftTop)) { 825 } 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; 876 edge = next;
915 } 877 }
916 for (Edge* edge = src->fFirstEdgeBelow; edge;) { 878 for (Edge* edge = src->fFirstEdgeBelow; edge;) {
917 Edge* next = edge->fNextEdgeBelow; 879 Edge* next = edge->fNextEdgeBelow;
918 set_top(edge, dst, nullptr, c); 880 set_top(edge, dst, nullptr, c);
919 edge = next; 881 edge = next;
920 } 882 }
921 remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(src, head, nullptr); 883 remove<Vertex, &Vertex::fPrev, &Vertex::fNext>(src, head, nullptr);
922 } 884 }
923 885
924 Vertex* check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, C omparator& c, 886 Vertex* check_for_intersection(Edge* edge, Edge* other, EdgeList* activeEdges, C omparator& c,
925 SkChunkAlloc& alloc) { 887 SkChunkAlloc& alloc) {
926 SkPoint p; 888 SkPoint p;
927 if (!edge || !other) { 889 if (!edge || !other) {
928 return nullptr; 890 return nullptr;
929 } 891 }
930 if (edge->intersect(*other, &p)) { 892 if (edge->intersect(*other, &p)) {
931 Vertex* v; 893 Vertex* v;
932 LOG("found intersection, pt is %g, %g\n", p.fX, p.fY); 894 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)) { 895 if (p == edge->fTop->fPoint || c.sweep_lt(p, edge->fTop->fPoint)) {
934 split_edge(other, edge->fTop, activeEdges, c, alloc); 896 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)) { 1106 if (check_for_intersection(edge, leftEnclosingEdge, &activeE dges, c, alloc)) {
1145 restartChecks = true; 1107 restartChecks = true;
1146 break; 1108 break;
1147 } 1109 }
1148 if (check_for_intersection(edge, rightEnclosingEdge, &active Edges, c, alloc)) { 1110 if (check_for_intersection(edge, rightEnclosingEdge, &active Edges, c, alloc)) {
1149 restartChecks = true; 1111 restartChecks = true;
1150 break; 1112 break;
1151 } 1113 }
1152 } 1114 }
1153 } else { 1115 } else {
1154 if (Vertex* pv = check_for_intersection(leftEnclosingEdge, right EnclosingEdge, 1116 if (Vertex* pv = check_for_intersection(leftEnclosingEdge, right EnclosingEdge,
1155 &activeEdges, c, alloc)) { 1117 &activeEdges, c, alloc)) {
1156 if (c.sweep_lt(pv->fPoint, v->fPoint)) { 1118 if (c.sweep_lt(pv->fPoint, v->fPoint)) {
1157 v = pv; 1119 v = pv;
1158 } 1120 }
1159 restartChecks = true; 1121 restartChecks = true;
1160 } 1122 }
1161 1123
1162 } 1124 }
1163 } while (restartChecks); 1125 } while (restartChecks);
1164 for (Edge* e = v->fFirstEdgeAbove; e; e = e->fNextEdgeAbove) { 1126 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, 1248 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); 1249 e->fLeftPoly ? e->fLeftPoly->fID : -1, e->fRightPoly ? e->fRight Poly->fID : -1);
1288 } 1250 }
1289 #endif 1251 #endif
1290 } 1252 }
1291 return polys; 1253 return polys;
1292 } 1254 }
1293 1255
1294 // This is a driver function which calls stages 2-5 in turn. 1256 // This is a driver function which calls stages 2-5 in turn.
1295 1257
1296 Poly* contours_to_polys(Vertex** contours, int contourCnt, Comparator& c, SkChun kAlloc& alloc) { 1258 Poly* contours_to_polys(Vertex** contours, int contourCnt, SkRect pathBounds, Sk ChunkAlloc& alloc) {
1259 Comparator c;
1260 if (pathBounds.width() > pathBounds.height()) {
1261 c.sweep_lt = sweep_lt_horiz;
1262 c.sweep_gt = sweep_gt_horiz;
1263 } else {
1264 c.sweep_lt = sweep_lt_vert;
1265 c.sweep_gt = sweep_gt_vert;
1266 }
1297 #if LOGGING_ENABLED 1267 #if LOGGING_ENABLED
1298 for (int i = 0; i < contourCnt; ++i) { 1268 for (int i = 0; i < contourCnt; ++i) {
1299 Vertex* v = contours[i]; 1269 Vertex* v = contours[i];
1300 SkASSERT(v); 1270 SkASSERT(v);
1301 LOG("path.moveTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY); 1271 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) { 1272 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); 1273 LOG("path.lineTo(%20.20g, %20.20g);\n", v->fPoint.fX, v->fPoint.fY);
1304 } 1274 }
1305 } 1275 }
1306 #endif 1276 #endif
1307 sanitize_contours(contours, contourCnt); 1277 sanitize_contours(contours, contourCnt);
1308 Vertex* vertices = build_edges(contours, contourCnt, c, alloc); 1278 Vertex* vertices = build_edges(contours, contourCnt, c, alloc);
1309 if (!vertices) { 1279 if (!vertices) {
1310 return nullptr; 1280 return nullptr;
1311 } 1281 }
1312 1282
1313 // Sort vertices in Y (secondarily in X). 1283 // Sort vertices in Y (secondarily in X).
1314 merge_sort(&vertices, c); 1284 merge_sort(&vertices, c);
1315 merge_coincident_vertices(&vertices, c, alloc); 1285 merge_coincident_vertices(&vertices, c, alloc);
1316 #if LOGGING_ENABLED 1286 #if LOGGING_ENABLED
1317 for (Vertex* v = vertices; v != nullptr; v = v->fNext) { 1287 for (Vertex* v = vertices; v != nullptr; v = v->fNext) {
1318 static float gID = 0.0f; 1288 static float gID = 0.0f;
1319 v->fID = gID++; 1289 v->fID = gID++;
1320 } 1290 }
1321 #endif 1291 #endif
1322 simplify(vertices, c, alloc); 1292 simplify(vertices, c, alloc);
1323 return tessellate(vertices, alloc); 1293 return tessellate(vertices, alloc);
1324 } 1294 }
1325 1295
1296 Poly* path_to_polys(const SkPath& path, SkScalar tolerance, const SkRect& clipBo unds,
1297 bool* isLinear) {
1298 int contourCnt;
1299 int maxPts = GrPathUtils::worstCasePointCount(path, &contourCnt, tolerance);
1300 if (maxPts <= 0) {
1301 return nullptr;
1302 }
1303 if (maxPts > ((int)SK_MaxU16 + 1)) {
1304 SkDebugf("Path not rendered, too many verts (%d)\n", maxPts);
1305 return nullptr;
1306 }
1307 SkPath::FillType fillType = path.getFillType();
1308 if (SkPath::IsInverseFillType(fillType)) {
1309 contourCnt++;
1310 }
1311 SkAutoTDeleteArray<Vertex*> contours(new Vertex* [contourCnt]);
1312
1313 // For the initial size of the chunk allocator, estimate based on the point count:
1314 // one vertex per point for the initial passes, plus two for the vertices in the
1315 // resulting Polys, since the same point may end up in two Polys. Assume mi nimal
1316 // connectivity of one Edge per Vertex (will grow for intersections).
1317 SkChunkAlloc alloc(maxPts * (3 * sizeof(Vertex) + sizeof(Edge)));
1318 path_to_contours(path, tolerance, clipBounds, contours.get(), alloc, isLinea r);
1319 return contours_to_polys(contours.get(), contourCnt, path.getBounds(), alloc );
1320 }
1321
1326 // Stage 6: Triangulate the monotone polygons into a vertex buffer. 1322 // Stage 6: Triangulate the monotone polygons into a vertex buffer.
1327 1323
1328 SkPoint* polys_to_triangles(Poly* polys, SkPath::FillType fillType, SkPoint* dat a) { 1324 int PathToTriangles(const SkPath& path, SkScalar tolerance, const SkRect& clipBo unds,
1329 SkPoint* d = data; 1325 GrResourceProvider* resourceProvider,
1326 SkAutoTUnref<GrVertexBuffer>& vertexBuffer, bool canMapVB, b ool* isLinear) {
1327 Poly* polys = path_to_polys(path, tolerance, clipBounds, isLinear);
1328 SkPath::FillType fillType = path.getFillType();
1329 int count = 0;
1330 for (Poly* poly = polys; poly; poly = poly->fNext) {
1331 if (apply_fill_type(fillType, poly->fWinding) && poly->fCount >= 3) {
1332 count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3);
1333 }
1334 }
1335 if (0 == count) {
1336 return 0;
1337 }
1338
1339 size_t size = count * sizeof(SkPoint);
1340 if (!vertexBuffer.get() || vertexBuffer->gpuMemorySize() < size) {
1341 vertexBuffer.reset(resourceProvider->createVertexBuffer(
1342 size, GrResourceProvider::kStatic_BufferUsage, 0));
1343 }
1344 if (!vertexBuffer.get()) {
1345 SkDebugf("Could not allocate vertices\n");
1346 return 0;
1347 }
1348 SkPoint* verts;
1349 if (canMapVB) {
1350 verts = static_cast<SkPoint*>(vertexBuffer->map());
1351 } else {
1352 verts = new SkPoint[count];
1353 }
1354 SkPoint* end = verts;
1330 for (Poly* poly = polys; poly; poly = poly->fNext) { 1355 for (Poly* poly = polys; poly; poly = poly->fNext) {
1331 if (apply_fill_type(fillType, poly->fWinding)) { 1356 if (apply_fill_type(fillType, poly->fWinding)) {
1332 d = poly->emit(d); 1357 end = poly->emit(end);
1333 } 1358 }
1334 } 1359 }
1335 return d; 1360 int actualCount = static_cast<int>(end - verts);
1361 LOG("actual count: %d\n", actualCount);
1362 SkASSERT(actualCount <= count);
1363 if (canMapVB) {
1364 vertexBuffer->unmap();
1365 } else {
1366 vertexBuffer->updateData(verts, actualCount * sizeof(SkPoint));
1367 delete[] verts;
1368 }
1369
1370 return actualCount;
1336 } 1371 }
1337 1372
1338 struct TessInfo { 1373 int PathToVertices(const SkPath& path, SkScalar tolerance, const SkRect& clipBou nds,
1339 SkScalar fTolerance; 1374 WindingVertex** verts) {
1340 int fCount; 1375 bool isLinear;
1341 }; 1376 Poly* polys = path_to_polys(path, tolerance, clipBounds, &isLinear);
1377 SkPath::FillType fillType = path.getFillType();
1378 int count = 0;
1379 for (Poly* poly = polys; poly; poly = poly->fNext) {
1380 if (apply_fill_type(fillType, poly->fWinding) && poly->fCount >= 3) {
1381 count += (poly->fCount - 2) * (TESSELLATOR_WIREFRAME ? 6 : 3);
1382 }
1383 }
1384 if (0 == count) {
1385 *verts = nullptr;
1386 return 0;
1387 }
1342 1388
1343 bool cache_match(GrVertexBuffer* vertexBuffer, SkScalar tol, int* actualCount) { 1389 *verts = new WindingVertex[count];
1344 if (!vertexBuffer) { 1390 WindingVertex* vertsEnd = *verts;
1345 return false; 1391 SkPoint* points = new SkPoint[count];
1392 SkPoint* pointsEnd = points;
1393 for (Poly* poly = polys; poly; poly = poly->fNext) {
1394 if (apply_fill_type(fillType, poly->fWinding)) {
1395 SkPoint* start = pointsEnd;
1396 pointsEnd = poly->emit(pointsEnd);
1397 while (start != pointsEnd) {
1398 vertsEnd->fPos = *start;
1399 vertsEnd->fWinding = poly->fWinding;
1400 ++start;
1401 ++vertsEnd;
1402 }
1403 }
1346 } 1404 }
1347 const SkData* data = vertexBuffer->getUniqueKey().getCustomData(); 1405 int actualCount = static_cast<int>(vertsEnd - *verts);
1348 SkASSERT(data); 1406 SkASSERT(actualCount <= count);
1349 const TessInfo* info = static_cast<const TessInfo*>(data->data()); 1407 SkASSERT(pointsEnd - points == actualCount);
1350 if (info->fTolerance == 0 || info->fTolerance < 3.0f * tol) { 1408 delete[] points;
1351 *actualCount = info->fCount; 1409 return actualCount;
1352 return true;
1353 }
1354 return false;
1355 } 1410 }
1356 1411
1357 };
1358
1359 GrTessellatingPathRenderer::GrTessellatingPathRenderer() {
1360 } 1412 }
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

Powered by Google App Engine
This is Rietveld 408576698