aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/utils/SkBoundaryPatch.cpp
blob: 37f59b4596ce7d9c0e2545cc47e5cc36399e7a9a (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

/*
 * Copyright 2011 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#include "SkBoundaryPatch.h"

SkBoundaryPatch::SkBoundaryPatch() : fBoundary(NULL) {}

SkBoundaryPatch::~SkBoundaryPatch() {
    SkSafeUnref(fBoundary);
}

SkBoundary* SkBoundaryPatch::setBoundary(SkBoundary* b) {
    SkRefCnt_SafeAssign(fBoundary, b);
    return b;
}

static SkPoint SkMakePoint(SkScalar x, SkScalar y) {
    SkPoint pt;
    pt.set(x, y);
    return pt;
}

static SkPoint SkPointInterp(const SkPoint& a, const SkPoint& b, SkScalar t) {
    return SkMakePoint(SkScalarInterp(a.fX, b.fX, t),
                       SkScalarInterp(a.fY, b.fY, t));
}

SkPoint SkBoundaryPatch::eval(SkScalar unitU, SkScalar unitV) {
    SkBoundary* b = fBoundary;
    SkPoint u = SkPointInterp(b->eval(SkBoundary::kLeft, SK_Scalar1 - unitV),
                              b->eval(SkBoundary::kRight, unitV),
                              unitU);
    SkPoint v = SkPointInterp(b->eval(SkBoundary::kTop, unitU),
                              b->eval(SkBoundary::kBottom, SK_Scalar1 - unitU),
                              unitV);
    return SkMakePoint(SkScalarAve(u.fX, v.fX),
                       SkScalarAve(u.fY, v.fY));
}

bool SkBoundaryPatch::evalPatch(SkPoint verts[], int rows, int cols) {
    if (rows < 2 || cols < 2) {
        return false;
    }

    const SkScalar invR = SkScalarInvert(SkIntToScalar(rows - 1));
    const SkScalar invC = SkScalarInvert(SkIntToScalar(cols - 1));
    
    for (int y = 0; y < cols; y++) {
        SkScalar yy = y * invC;
        for (int x = 0; x < rows; x++) {
            *verts++ = this->eval(x * invR, yy);
        }
    }
    return true;
}

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

#include "SkGeometry.h"

SkPoint SkLineBoundary::eval(Edge e, SkScalar t) {
    SkASSERT((unsigned)e < 4);
    return SkPointInterp(fPts[e], fPts[(e + 1) & 3], t);
}

SkPoint SkCubicBoundary::eval(Edge e, SkScalar t) {
    SkASSERT((unsigned)e < 4);

    // ensure our 4th cubic wraps to the start of the first
    fPts[12] = fPts[0];

    SkPoint loc;
    SkEvalCubicAt(&fPts[e * 3], t, &loc, NULL, NULL);
    return loc;
}