1
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
8#include "SkBoundaryPatch.h"
9
10SkBoundaryPatch::SkBoundaryPatch() : fBoundary(NULL) {}
11
12SkBoundaryPatch::~SkBoundaryPatch() {
13    SkSafeUnref(fBoundary);
14}
15
16SkBoundary* SkBoundaryPatch::setBoundary(SkBoundary* b) {
17    SkRefCnt_SafeAssign(fBoundary, b);
18    return b;
19}
20
21static SkPoint SkMakePoint(SkScalar x, SkScalar y) {
22    SkPoint pt;
23    pt.set(x, y);
24    return pt;
25}
26
27static SkPoint SkPointInterp(const SkPoint& a, const SkPoint& b, SkScalar t) {
28    return SkMakePoint(SkScalarInterp(a.fX, b.fX, t),
29                       SkScalarInterp(a.fY, b.fY, t));
30}
31
32SkPoint SkBoundaryPatch::eval(SkScalar unitU, SkScalar unitV) {
33    SkBoundary* b = fBoundary;
34    SkPoint u = SkPointInterp(b->eval(SkBoundary::kLeft, SK_Scalar1 - unitV),
35                              b->eval(SkBoundary::kRight, unitV),
36                              unitU);
37    SkPoint v = SkPointInterp(b->eval(SkBoundary::kTop, unitU),
38                              b->eval(SkBoundary::kBottom, SK_Scalar1 - unitU),
39                              unitV);
40    return SkMakePoint(SkScalarAve(u.fX, v.fX),
41                       SkScalarAve(u.fY, v.fY));
42}
43
44bool SkBoundaryPatch::evalPatch(SkPoint verts[], int rows, int cols) {
45    if (rows < 2 || cols < 2) {
46        return false;
47    }
48
49    const SkScalar invR = SkScalarInvert(SkIntToScalar(rows - 1));
50    const SkScalar invC = SkScalarInvert(SkIntToScalar(cols - 1));
51
52    for (int y = 0; y < cols; y++) {
53        SkScalar yy = y * invC;
54        for (int x = 0; x < rows; x++) {
55            *verts++ = this->eval(x * invR, yy);
56        }
57    }
58    return true;
59}
60
61////////////////////////////////////////////////////////////////////////
62
63#include "SkGeometry.h"
64
65SkPoint SkLineBoundary::eval(Edge e, SkScalar t) {
66    SkASSERT((unsigned)e < 4);
67    return SkPointInterp(fPts[e], fPts[(e + 1) & 3], t);
68}
69
70SkPoint SkCubicBoundary::eval(Edge e, SkScalar t) {
71    SkASSERT((unsigned)e < 4);
72
73    // ensure our 4th cubic wraps to the start of the first
74    fPts[12] = fPts[0];
75
76    SkPoint loc;
77    SkEvalCubicAt(&fPts[e * 3], t, &loc, NULL, NULL);
78    return loc;
79}
80