blob: e5fcc4051bad0744caacef58b383531bd30cb234 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
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 */
reed@android.com4408cca2009-10-27 02:24:03 +00008#include "SkBoundaryPatch.h"
9
reed@android.com2ee7c642009-10-28 14:25:34 +000010SkBoundaryPatch::SkBoundaryPatch() : fBoundary(NULL) {}
reed@android.com4408cca2009-10-27 02:24:03 +000011
12SkBoundaryPatch::~SkBoundaryPatch() {
reed@android.com2ee7c642009-10-28 14:25:34 +000013 SkSafeUnref(fBoundary);
reed@android.com4408cca2009-10-27 02:24:03 +000014}
15
reed@android.com2ee7c642009-10-28 14:25:34 +000016SkBoundary* SkBoundaryPatch::setBoundary(SkBoundary* b) {
17 SkRefCnt_SafeAssign(fBoundary, b);
18 return b;
reed@android.com4408cca2009-10-27 02:24:03 +000019}
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
reed@android.com2ee7c642009-10-28 14:25:34 +000032SkPoint 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);
reed@android.com4408cca2009-10-27 02:24:03 +000040 return SkMakePoint(SkScalarAve(u.fX, v.fX),
41 SkScalarAve(u.fY, v.fY));
42}
43
reed@android.com2ee7c642009-10-28 14:25:34 +000044bool 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));
rmistry@google.comd6176b02012-08-23 18:14:13 +000051
reed@android.com2ee7c642009-10-28 14:25:34 +000052 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
reed@android.com4408cca2009-10-27 02:24:03 +000061////////////////////////////////////////////////////////////////////////
62
63#include "SkGeometry.h"
64
reed@android.com2ee7c642009-10-28 14:25:34 +000065SkPoint SkLineBoundary::eval(Edge e, SkScalar t) {
66 SkASSERT((unsigned)e < 4);
67 return SkPointInterp(fPts[e], fPts[(e + 1) & 3], t);
reed@android.com4408cca2009-10-27 02:24:03 +000068}
69
reed@android.com2ee7c642009-10-28 14:25:34 +000070SkPoint 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
reed@android.com4408cca2009-10-27 02:24:03 +000076 SkPoint loc;
reed@android.com2ee7c642009-10-28 14:25:34 +000077 SkEvalCubicAt(&fPts[e * 3], t, &loc, NULL, NULL);
reed@android.com4408cca2009-10-27 02:24:03 +000078 return loc;
79}