aboutsummaryrefslogtreecommitdiffhomepage
path: root/gpu/src/GrPath.cpp
blob: a740dfc8d340e60756729f516428ba8a29bd7fcb (plain)
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#include "GrPath.h"

GrPath::GrPath() {
    fConvexHint = kNone_ConvexHint;
}

GrPath::GrPath(const GrPath& src) : INHERITED() {
    GrPath::Iter iter(src);
    this->resetFromIter(&iter);
}

GrPath::GrPath(GrPathIter& iter) {
    this->resetFromIter(&iter);
}

GrPath::~GrPath() {
}

bool GrPath::operator ==(const GrPath& path) const {
    if (fCmds.count() != path.fCmds.count() ||
        fPts.count() != path.fPts.count()) {
        return false;
    }

    for (int v = 0; v < fCmds.count(); ++v) {
        if (fCmds[v] != path.fCmds[v]) {
            return false;
        }
    }

    for (int p = 0; p < fPts.count(); ++p) {
        if (fPts[p] != path.fPts[p]) {
            return false;
        }
    }
    return true;
}

void GrPath::ensureMoveTo() {
    if (fCmds.isEmpty() || this->wasLastVerb(kClose_PathCmd)) {
        *fCmds.append() = kMove_PathCmd;
        fPts.append()->set(0, 0);
    }
}

void GrPath::moveTo(GrScalar x, GrScalar y) {
    if (this->wasLastVerb(kMove_PathCmd)) {
        // overwrite prev kMove value
        fPts[fPts.count() - 1].set(x, y);
    } else {
        *fCmds.append() = kMove_PathCmd;
        fPts.append()->set(x, y);
    }
}

void GrPath::lineTo(GrScalar x, GrScalar y) {
    this->ensureMoveTo();
    *fCmds.append() = kLine_PathCmd;
    fPts.append()->set(x, y);
}

void GrPath::quadTo(GrScalar x0, GrScalar y0, GrScalar x1, GrScalar y1) {
    this->ensureMoveTo();
    *fCmds.append() = kQuadratic_PathCmd;
    fPts.append()->set(x0, y0);
    fPts.append()->set(x1, y1);
}

void GrPath::cubicTo(GrScalar x0, GrScalar y0, GrScalar x1, GrScalar y1,
                     GrScalar x2, GrScalar y2) {
    this->ensureMoveTo();
    *fCmds.append() = kCubic_PathCmd;
    fPts.append()->set(x0, y0);
    fPts.append()->set(x1, y1);
    fPts.append()->set(x2, y2);
}

void GrPath::close() {
    if (!fCmds.isEmpty() && !this->wasLastVerb(kClose_PathCmd)) {
        // should we allow kMove followed by kClose?
        *fCmds.append() = kClose_PathCmd;
    }
}

///////////////////////////////////////////////////////////////////////////////

void GrPath::offset(GrScalar tx, GrScalar ty) {
    if (!tx && !ty) {
        return; // nothing to do
    }

    GrPoint* iter = fPts.begin();
    GrPoint* stop = fPts.end();
    while (iter < stop) {
        iter->offset(tx, ty);
        ++iter;
    }
}

///////////////////////////////////////////////////////////////////////////////

static bool check_two_vecs(const GrVec& prevVec,
                           const GrVec& currVec,
                           GrScalar turnDir,
                           int* xDir,
                           int* yDir,
                           int* flipX,
                           int* flipY) {
    if (currVec.fX * *xDir < 0) {
        ++*flipX;
        if (*flipX > 2) {
            return false;
        }
        *xDir = -*xDir;
    }
    if (currVec.fY * *yDir < 0) {
        ++*flipY;
        if (*flipY > 2) {
            return false;
        }
        *yDir = -*yDir;
    }
    GrScalar d = prevVec.cross(currVec);
    return (d * turnDir) >= 0;
}

static void init_from_two_vecs(const GrVec& firstVec,
                               const GrVec& secondVec,
                               GrScalar* turnDir,
                               int* xDir, int* yDir) {
    *turnDir = firstVec.cross(secondVec);
    if (firstVec.fX > 0) {
        *xDir = 1;
    } else if (firstVec.fX < 0) {
        *xDir = -1;
    } else {
        *xDir = 0;
    }
    if (firstVec.fY > 0) {
        *yDir = 1;
    } else if (firstVec.fY < 0) {
        *yDir = -1;
    } else {
        *yDir = 0;
    }
}

void GrPath::resetFromIter(GrPathIter* iter) {
    fPts.reset();
    fCmds.reset();

    fConvexHint = iter->convexHint();

    // first point of the subpath
    GrPoint firstPt(0,0);
    // first edge of the subpath
    GrVec firstVec(0,0);
    // vec of most recently processed edge, that wasn't degenerate
    GrVec previousVec(0,0);
    // most recently processed point
    GrPoint previousPt(0,0);

    // sign indicates whether we're bending left or right
    GrScalar turnDir = 0;
    // number of times the direction has flipped in x or y

    // we track which direction we are moving in x/y and the
    // number of times it changes.
    int xDir = 0;
    int yDir = 0;
    int flipX = 0;
    int flipY = 0;

    // counts number of sub path pts that didn't add a degenerate edge.
    int subPathPts = 0;
    bool subPathClosed = false;

    int numSubPaths = 0;
    iter->rewind();
    GrPathCmd cmd;
    GrPoint pts[4];
    do {
        cmd = iter->next(pts);
        // If the convexity test is ever updated to handle multiple subpaths
        // the loop has to be adjusted to handle moving to a new subpath without
        // closing the previous one. Currently the implicit closing vectors for a
        // filled path would never be examined.
        switch (cmd) {
            case kMove_PathCmd:
                this->moveTo(pts[0].fX, pts[0].fY);
                subPathPts = 0;
                subPathClosed = false;
                break;
            case kLine_PathCmd:
                this->lineTo(pts[1].fX, pts[1].fY);
                break;
            case kQuadratic_PathCmd:
                this->quadTo(pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY);
                break;
            case kCubic_PathCmd:
                this->cubicTo(pts[1].fX, pts[1].fY, pts[2].fX, pts[2].fY,
                              pts[3].fX, pts[3].fY);
                break;
            case kClose_PathCmd:
                this->close();
                subPathClosed = true;
                break;
            case kEnd_PathCmd:
                break;
        }
        int n = NumPathCmdPoints(cmd);
        if (0 == subPathPts && n > 0) {
            previousPt = pts[0];
            firstPt = previousPt;
            flipX = 0;
            flipY = 0;
            turnDir = 0;
            subPathPts = 1;
            ++numSubPaths;
        }
        // either we skip the first pt because it is redundant with
        // last point of the previous subpath cmd or we just ate it
        // in the above if.
        int consumed = 1;
        if (numSubPaths < 2 && kNone_ConvexHint == fConvexHint) {
            while (consumed < n) {
                GrAssert(pts[consumed-1] == previousPt);
                GrVec vec;
                vec.setBetween(previousPt, pts[consumed]);
                if (vec.fX || vec.fY) {
                    if (subPathPts >= 2) {
                        if (0 == turnDir) {
                            firstVec = previousVec;
                            init_from_two_vecs(firstVec, vec,
                                               &turnDir, &xDir, &yDir);
                            // here we aren't checking whether the x/y dirs
                            // change between the first and second edge. It
                            // gets covered when the path is closed.
                        } else {
                            if (!check_two_vecs(previousVec, vec, turnDir,
                                                &xDir, &yDir,
                                                &flipX, &flipY)) {
                                fConvexHint = kConcave_ConvexHint;
                                break;
                            }
                        }
                    }
                    previousVec = vec;
                    previousPt = pts[consumed];
                    ++subPathPts;
                }
                ++consumed;
            }
            if (subPathPts > 2 && (kClose_PathCmd == cmd ||
                        (!subPathClosed && kEnd_PathCmd == cmd ))) {
                // if an additional vector is needed to close the loop check
                // that it validates against the previous vector.
                GrVec vec;
                vec.setBetween(previousPt, firstPt);
                if (vec.fX || vec.fY) {
                    if (!check_two_vecs(previousVec, vec, turnDir,
                                        &xDir, &yDir, &flipX, &flipY)) {
                        fConvexHint = kConcave_ConvexHint;
                        break;
                    }
                    previousVec = vec;
                }
                // check that closing vector validates against the first vector.
                if (!check_two_vecs(previousVec, firstVec, turnDir,
                                    &xDir, &yDir, &flipX, &flipY)) {
                    fConvexHint = kConcave_ConvexHint;
                    break;
                }
            }
        }
    } while (cmd != kEnd_PathCmd);
    if (kNone_ConvexHint == fConvexHint && numSubPaths < 2) {
        fConvexHint = kConvex_ConvexHint;
    } else {
        bool recurse = false;
        if (recurse) {
            this->resetFromIter(iter);
        }
    }
}

void GrPath::ConvexUnitTest() {
    GrPath testPath;
    GrPath::Iter testIter;

    GrPath pt;
    pt.moveTo(0, 0);
    pt.close();

    testIter.reset(pt);
    testPath.resetFromIter(&testIter);
    GrAssert(kConvex_ConvexHint == testPath.getConvexHint());

    GrPath line;
    line.moveTo(GrIntToScalar(12), GrIntToScalar(20));
    line.lineTo(GrIntToScalar(-12), GrIntToScalar(-20));
    line.close();

    testIter.reset(line);
    testPath.resetFromIter(&testIter);
    GrAssert(kConvex_ConvexHint == testPath.getConvexHint());

    GrPath triLeft;
    triLeft.moveTo(0, 0);
    triLeft.lineTo(1, 0);
    triLeft.lineTo(1, 1);
    triLeft.close();

    testIter.reset(triLeft);
    testPath.resetFromIter(&testIter);
    GrAssert(kConvex_ConvexHint == testPath.getConvexHint());

    GrPath triRight;
    triRight.moveTo(0, 0);
    triRight.lineTo(-1, 0);
    triRight.lineTo(1, 1);
    triRight.close();

    testIter.reset(triRight);
    testPath.resetFromIter(&testIter);
    GrAssert(kConvex_ConvexHint == testPath.getConvexHint());

    GrPath square;
    square.moveTo(0, 0);
    square.lineTo(1, 0);
    square.lineTo(1, 1);
    square.lineTo(0, 1);
    square.close();

    testIter.reset(square);
    testPath.resetFromIter(&testIter);
    GrAssert(kConvex_ConvexHint == testPath.getConvexHint());

    GrPath redundantSquare;
    square.moveTo(0, 0);
    square.lineTo(0, 0);
    square.lineTo(0, 0);
    square.lineTo(1, 0);
    square.lineTo(1, 0);
    square.lineTo(1, 0);
    square.lineTo(1, 1);
    square.lineTo(1, 1);
    square.lineTo(1, 1);
    square.lineTo(0, 1);
    square.lineTo(0, 1);
    square.lineTo(0, 1);
    square.close();

    testIter.reset(redundantSquare);
    testPath.resetFromIter(&testIter);
    GrAssert(kConvex_ConvexHint == testPath.getConvexHint());

    GrPath bowTie;
    bowTie.moveTo(0, 0);
    bowTie.lineTo(0, 0);
    bowTie.lineTo(0, 0);
    bowTie.lineTo(1, 1);
    bowTie.lineTo(1, 1);
    bowTie.lineTo(1, 1);
    bowTie.lineTo(1, 0);
    bowTie.lineTo(1, 0);
    bowTie.lineTo(1, 0);
    bowTie.lineTo(0, 1);
    bowTie.lineTo(0, 1);
    bowTie.lineTo(0, 1);
    bowTie.close();

    testIter.reset(bowTie);
    testPath.resetFromIter(&testIter);
    GrAssert(kConcave_ConvexHint == testPath.getConvexHint());

    GrPath spiral;
    spiral.moveTo(0, 0);
    spiral.lineTo(1, 0);
    spiral.lineTo(1, 1);
    spiral.lineTo(0, 1);
    spiral.lineTo(0,.5);
    spiral.lineTo(.5,.5);
    spiral.lineTo(.5,.75);
    spiral.close();

    testIter.reset(spiral);
    testPath.resetFromIter(&testIter);
    GrAssert(kConcave_ConvexHint == testPath.getConvexHint());

    GrPath dent;
    dent.moveTo(0, 0);
    dent.lineTo(1, 1);
    dent.lineTo(0, 1);
    dent.lineTo(-.5,2);
    dent.lineTo(-2, 1);
    dent.close();

    testIter.reset(dent);
    testPath.resetFromIter(&testIter);
    GrAssert(kConcave_ConvexHint == testPath.getConvexHint());
}
///////////////////////////////////////////////////////////////////////////////

GrPath::Iter::Iter() : fPath(NULL) {
}

GrPath::Iter::Iter(const GrPath& path) : fPath(&path) {
    this->rewind();
}

GrPathCmd GrPath::Iter::next(GrPoint points[]) {
    if (fCmdIndex == fPath->fCmds.count()) {
        GrAssert(fPtIndex == fPath->fPts.count());
        return kEnd_PathCmd;
    } else {
        GrAssert(fCmdIndex < fPath->fCmds.count());
    }

    GrPathCmd cmd = fPath->fCmds[fCmdIndex++];
    const GrPoint* srcPts = fPath->fPts.begin() + fPtIndex;

    switch (cmd) {
        case kMove_PathCmd:
            if (points) {
                points[0] = srcPts[0];
            }
            fLastPt = srcPts[0];
            GrAssert(fPtIndex <= fPath->fPts.count() + 1);
            fPtIndex += 1;
            break;
        case kLine_PathCmd:
            if (points) {
                points[0] = fLastPt;
                points[1] = srcPts[0];
            }
            fLastPt = srcPts[0];
            GrAssert(fPtIndex <= fPath->fPts.count() + 1);
            fPtIndex += 1;
            break;
        case kQuadratic_PathCmd:
            if (points) {
                points[0] = fLastPt;
                points[1] = srcPts[0];
                points[2] = srcPts[1];
            }
            fLastPt = srcPts[1];
            GrAssert(fPtIndex <= fPath->fPts.count() + 2);
            fPtIndex += 2;
            break;
        case kCubic_PathCmd:
            if (points) {
                points[0] = fLastPt;
                points[1] = srcPts[0];
                points[2] = srcPts[1];
                points[3] = srcPts[2];
            }
            fLastPt = srcPts[2];
            GrAssert(fPtIndex <= fPath->fPts.count() + 3);
            fPtIndex += 3;
            break;
        case kClose_PathCmd:
            break;
        default:
            GrAssert(!"unknown grpath cmd");
            break;
    }
    return cmd;
}

GrConvexHint GrPath::Iter::convexHint() const {
    return fPath->getConvexHint();
}

GrPathCmd GrPath::Iter::next() {
    return this->next(NULL);
}

void GrPath::Iter::rewind() {
    this->reset(*fPath);
}

void GrPath::Iter::reset(const GrPath& path) {
    fPath = &path;
    fCmdIndex = fPtIndex = 0;
}