blob: fd1545d2a98599ae22bc29f128f0f6d6e7c8175e [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
robertphillips@google.coma22e2112012-08-16 14:58:06 +000010SK_DEFINE_INST_COUNT(SkBoundary)
11
reed@android.com2ee7c642009-10-28 14:25:34 +000012SkBoundaryPatch::SkBoundaryPatch() : fBoundary(NULL) {}
reed@android.com4408cca2009-10-27 02:24:03 +000013
14SkBoundaryPatch::~SkBoundaryPatch() {
reed@android.com2ee7c642009-10-28 14:25:34 +000015 SkSafeUnref(fBoundary);
reed@android.com4408cca2009-10-27 02:24:03 +000016}
17
reed@android.com2ee7c642009-10-28 14:25:34 +000018SkBoundary* SkBoundaryPatch::setBoundary(SkBoundary* b) {
19 SkRefCnt_SafeAssign(fBoundary, b);
20 return b;
reed@android.com4408cca2009-10-27 02:24:03 +000021}
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
reed@android.com2ee7c642009-10-28 14:25:34 +000034SkPoint 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);
reed@android.com4408cca2009-10-27 02:24:03 +000042 return SkMakePoint(SkScalarAve(u.fX, v.fX),
43 SkScalarAve(u.fY, v.fY));
44}
45
reed@android.com2ee7c642009-10-28 14:25:34 +000046bool 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));
rmistry@google.comd6176b02012-08-23 18:14:13 +000053
reed@android.com2ee7c642009-10-28 14:25:34 +000054 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
reed@android.com4408cca2009-10-27 02:24:03 +000063////////////////////////////////////////////////////////////////////////
64
65#include "SkGeometry.h"
66
reed@android.com2ee7c642009-10-28 14:25:34 +000067SkPoint SkLineBoundary::eval(Edge e, SkScalar t) {
68 SkASSERT((unsigned)e < 4);
69 return SkPointInterp(fPts[e], fPts[(e + 1) & 3], t);
reed@android.com4408cca2009-10-27 02:24:03 +000070}
71
reed@android.com2ee7c642009-10-28 14:25:34 +000072SkPoint 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
reed@android.com4408cca2009-10-27 02:24:03 +000078 SkPoint loc;
reed@android.com2ee7c642009-10-28 14:25:34 +000079 SkEvalCubicAt(&fPts[e * 3], t, &loc, NULL, NULL);
reed@android.com4408cca2009-10-27 02:24:03 +000080 return loc;
81}