1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
|
/*
Copyright 2011 Google Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "GrTesselatedPathRenderer.h"
#include "GrMemory.h"
#include "GrPathUtils.h"
#include <internal_glu.h>
struct PolygonData {
PolygonData(GrTDArray<GrPoint>* vertices, GrTDArray<short>* indices)
: fVertices(vertices)
, fIndices(indices)
{
}
GrTDArray<GrPoint>* fVertices;
GrTDArray<short>* fIndices;
};
static void beginData(GLenum type, void* data)
{
GR_DEBUGASSERT(type == GL_TRIANGLES);
}
static void edgeFlagData(GLboolean flag, void* data)
{
}
static void vertexData(void* vertexData, void* data)
{
short* end = static_cast<PolygonData*>(data)->fIndices->append();
*end = reinterpret_cast<long>(vertexData);
}
static void endData(void* data)
{
}
static void combineData(GLdouble coords[3], void* vertexData[4],
GLfloat weight[4], void **outData, void* data)
{
PolygonData* polygonData = static_cast<PolygonData*>(data);
int index = polygonData->fVertices->count();
*polygonData->fVertices->append() = GrPoint(static_cast<float>(coords[0]),
static_cast<float>(coords[1]));
*outData = reinterpret_cast<void*>(index);
}
typedef void (*TESSCB)();
static unsigned fill_type_to_glu_winding_rule(GrPathFill fill) {
switch (fill) {
case kWinding_PathFill:
return GLU_TESS_WINDING_NONZERO;
case kEvenOdd_PathFill:
return GLU_TESS_WINDING_ODD;
case kInverseWinding_PathFill:
return GLU_TESS_WINDING_POSITIVE;
case kInverseEvenOdd_PathFill:
return GLU_TESS_WINDING_ODD;
case kHairLine_PathFill:
return GLU_TESS_WINDING_NONZERO; // FIXME: handle this
default:
GrAssert(!"Unknown path fill!");
return 0;
}
}
GrTesselatedPathRenderer::GrTesselatedPathRenderer() {
}
void GrTesselatedPathRenderer::drawPath(GrDrawTarget* target,
GrDrawTarget::StageBitfield stages,
GrPathIter* path,
GrPathFill fill,
const GrPoint* translate) {
GrDrawTarget::AutoStateRestore asr(target);
bool colorWritesWereDisabled = target->isColorWriteDisabled();
// face culling doesn't make sense here
GrAssert(GrDrawTarget::kBoth_DrawFace == target->getDrawFace());
GrMatrix viewM = target->getViewMatrix();
// In order to tesselate the path we get a bound on how much the matrix can
// stretch when mapping to screen coordinates.
GrScalar stretch = viewM.getMaxStretch();
bool useStretch = stretch > 0;
GrScalar tol = GrPathUtils::gTolerance;
if (!useStretch) {
// TODO: deal with perspective in some better way.
tol /= 10;
} else {
GrScalar sinv = GR_Scalar1 / stretch;
tol = GrMul(tol, sinv);
}
GrScalar tolSqd = GrMul(tol, tol);
path->rewind();
int subpathCnt;
int maxPts = GrPathUtils::worstCasePointCount(path, &subpathCnt, tol);
GrVertexLayout layout = 0;
for (int s = 0; s < GrDrawTarget::kNumStages; ++s) {
if ((1 << s) & stages) {
layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(s);
}
}
bool inverted = IsFillInverted(fill);
if (inverted) {
maxPts += 4;
subpathCnt++;
}
GrPoint* base = new GrPoint[maxPts];
GrPoint* vert = base;
GrPoint* subpathBase = base;
GrAutoSTMalloc<8, uint16_t> subpathVertCount(subpathCnt);
path->rewind();
GrPoint pts[4];
bool first = true;
int subpath = 0;
for (;;) {
GrPathCmd cmd = path->next(pts);
switch (cmd) {
case kMove_PathCmd:
if (!first) {
subpathVertCount[subpath] = vert-subpathBase;
subpathBase = vert;
++subpath;
}
*vert = pts[0];
vert++;
break;
case kLine_PathCmd:
*vert = pts[1];
vert++;
break;
case kQuadratic_PathCmd: {
GrPathUtils::generateQuadraticPoints(pts[0], pts[1], pts[2],
tolSqd, &vert,
GrPathUtils::quadraticPointCount(pts, tol));
break;
}
case kCubic_PathCmd: {
GrPathUtils::generateCubicPoints(pts[0], pts[1], pts[2], pts[3],
tolSqd, &vert,
GrPathUtils::cubicPointCount(pts, tol));
break;
}
case kClose_PathCmd:
break;
case kEnd_PathCmd:
subpathVertCount[subpath] = vert-subpathBase;
++subpath; // this could be only in debug
goto FINISHED;
}
first = false;
}
FINISHED:
if (translate) {
for (int i = 0; i < vert - base; i++) {
base[i].offset(translate->fX, translate->fY);
}
}
if (inverted) {
GrRect bounds;
GrAssert(NULL != target->getRenderTarget());
bounds.setLTRB(0, 0,
GrIntToScalar(target->getRenderTarget()->width()),
GrIntToScalar(target->getRenderTarget()->height()));
GrMatrix vmi;
if (target->getViewInverse(&vmi)) {
vmi.mapRect(&bounds);
}
*vert++ = GrPoint(bounds.fLeft, bounds.fTop);
*vert++ = GrPoint(bounds.fLeft, bounds.fBottom);
*vert++ = GrPoint(bounds.fRight, bounds.fBottom);
*vert++ = GrPoint(bounds.fRight, bounds.fTop);
subpathVertCount[subpath++] = 4;
}
GrAssert(subpath == subpathCnt);
GrAssert((vert - base) <= maxPts);
size_t count = vert - base;
// FIXME: This copy could be removed if we had (templated?) versions of
// generate_*_point above that wrote directly into doubles.
double* inVertices = new double[count * 3];
for (size_t i = 0; i < count; ++i) {
inVertices[i * 3] = base[i].fX;
inVertices[i * 3 + 1] = base[i].fY;
inVertices[i * 3 + 2] = 1.0;
}
GLUtesselator* tess = internal_gluNewTess();
unsigned windingRule = fill_type_to_glu_winding_rule(fill);
internal_gluTessProperty(tess, GLU_TESS_WINDING_RULE, windingRule);
internal_gluTessCallback(tess, GLU_TESS_BEGIN_DATA, (TESSCB) &beginData);
internal_gluTessCallback(tess, GLU_TESS_VERTEX_DATA, (TESSCB) &vertexData);
internal_gluTessCallback(tess, GLU_TESS_END_DATA, (TESSCB) &endData);
internal_gluTessCallback(tess, GLU_TESS_EDGE_FLAG_DATA, (TESSCB) &edgeFlagData);
internal_gluTessCallback(tess, GLU_TESS_COMBINE_DATA, (TESSCB) &combineData);
GrTDArray<short> indices;
GrTDArray<GrPoint> vertices;
PolygonData data(&vertices, &indices);
internal_gluTessBeginPolygon(tess, &data);
size_t i = 0;
for (int sp = 0; sp < subpathCnt; ++sp) {
internal_gluTessBeginContour(tess);
int start = i;
int end = start + subpathVertCount[sp];
for (; i < end; ++i) {
double* inVertex = &inVertices[i * 3];
*vertices.append() = GrPoint(inVertex[0], inVertex[1]);
internal_gluTessVertex(tess, inVertex, reinterpret_cast<void*>(i));
}
internal_gluTessEndContour(tess);
}
internal_gluTessEndPolygon(tess);
internal_gluDeleteTess(tess);
// FIXME: If we could figure out the maxIndices before running the
// tesselator, we could allocate the geometry upfront, rather than making
// yet another copy.
GrDrawTarget::AutoReleaseGeometry geom(target, layout, vertices.count(), indices.count());
memcpy(geom.vertices(), vertices.begin(), vertices.count() * sizeof(GrPoint));
memcpy(geom.indices(), indices.begin(), indices.count() * sizeof(short));
if (indices.count() > 0) {
target->drawIndexed(kTriangles_PrimitiveType,
0,
0,
vertices.count(),
indices.count());
}
delete[] inVertices;
delete[] base;
}
bool GrTesselatedPathRenderer::canDrawPath(const GrDrawTarget* target,
GrPathIter* path,
GrPathFill fill) const {
return kHairLine_PathFill != fill;
}
void GrTesselatedPathRenderer::drawPathToStencil(GrDrawTarget* target,
GrPathIter* path,
GrPathFill fill,
const GrPoint* translate) {
GrAlwaysAssert(!"multipass stencil should not be needed");
}
|