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