add boundary patch utils
diff --git a/Android.mk b/Android.mk
index 3d03194..5c0961e 100644
--- a/Android.mk
+++ b/Android.mk
@@ -156,9 +156,11 @@
src/core/SkUnPreMultiply.cpp \
src/core/SkXfermode.cpp \
src/core/SkWriter32.cpp \
+ src/utils/SkBoundaryPatch.cpp \
src/utils/SkCamera.cpp \
src/utils/SkDumpCanvas.cpp \
src/utils/SkInterpolator.cpp \
+ src/utils/SkMeshUtils.cpp \
src/utils/SkNinePatch.cpp \
src/utils/SkProxyCanvas.cpp
diff --git a/include/utils/SkBoundaryPatch.h b/include/utils/SkBoundaryPatch.h
new file mode 100644
index 0000000..835fc3e
--- /dev/null
+++ b/include/utils/SkBoundaryPatch.h
@@ -0,0 +1,55 @@
+#ifndef SkBoundaryPatch_DEFINED
+#define SkBoundaryPatch_DEFINED
+
+#include "SkPoint.h"
+#include "SkRefCnt.h"
+
+class SkBoundary : public SkRefCnt {
+public:
+ // These must be 0, 1, 2, 3 for efficiency in the subclass implementations
+ enum Edge {
+ kTop = 0,
+ kRight = 1,
+ kBottom = 2,
+ kLeft = 3
+ };
+ // Edge index goes clockwise around the boundary, beginning at the "top"
+ virtual SkPoint eval(Edge, SkScalar unitInterval) = 0;
+};
+
+class SkBoundaryPatch {
+public:
+ SkBoundaryPatch();
+ ~SkBoundaryPatch();
+
+ SkBoundary* getBoundary() const { return fBoundary; }
+ SkBoundary* setBoundary(SkBoundary*);
+
+ SkPoint eval(SkScalar unitU, SkScalar unitV);
+ bool evalPatch(SkPoint verts[], int rows, int cols);
+
+private:
+ SkBoundary* fBoundary;
+};
+
+////////////////////////////////////////////////////////////////////////
+
+class SkLineBoundary : public SkBoundary {
+public:
+ SkPoint fPts[4];
+
+ // override
+ virtual SkPoint eval(Edge, SkScalar);
+};
+
+class SkCubicBoundary : public SkBoundary {
+public:
+ // the caller sets the first 12 entries. The 13th is used by the impl.
+ SkPoint fPts[13];
+
+ // override
+ virtual SkPoint eval(Edge, SkScalar);
+};
+
+#endif
+
diff --git a/include/utils/SkMeshUtils.h b/include/utils/SkMeshUtils.h
new file mode 100644
index 0000000..1235485
--- /dev/null
+++ b/include/utils/SkMeshUtils.h
@@ -0,0 +1,43 @@
+#ifndef SkMeshUtils_DEFINED
+#define SkMeshUtils_DEFINED
+
+#include "SkPoint.h"
+#include "SkColor.h"
+
+class SkBitmap;
+class SkCanvas;
+class SkPaint;
+
+class SkMeshIndices {
+public:
+ SkMeshIndices();
+ ~SkMeshIndices();
+
+ bool init(int texW, int texH, int rows, int cols) {
+ return this->init(NULL, NULL, texW, texH, rows, cols);
+ }
+
+ bool init(SkPoint tex[], uint16_t indices[],
+ int texW, int texH, int rows, int cols);
+
+ size_t indexCount() const { return fIndexCount; }
+ const uint16_t* indices() const { return fIndices; }
+
+ size_t texCount() const { return fTexCount; }
+ const SkPoint* tex() const { return fTex; }
+
+private:
+ size_t fIndexCount, fTexCount;
+ SkPoint* fTex;
+ uint16_t* fIndices;
+ void* fStorage; // may be null
+};
+
+class SkMeshUtils {
+public:
+ static void Draw(SkCanvas*, const SkBitmap&, int rows, int cols,
+ const SkPoint verts[], const SkColor colors[],
+ const SkPaint& paint);
+};
+
+#endif
diff --git a/src/utils/SkBoundaryPatch.cpp b/src/utils/SkBoundaryPatch.cpp
new file mode 100644
index 0000000..cdbc877
--- /dev/null
+++ b/src/utils/SkBoundaryPatch.cpp
@@ -0,0 +1,73 @@
+#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;
+}
+
diff --git a/src/utils/SkMeshUtils.cpp b/src/utils/SkMeshUtils.cpp
new file mode 100644
index 0000000..0385b99
--- /dev/null
+++ b/src/utils/SkMeshUtils.cpp
@@ -0,0 +1,96 @@
+#include "SkMeshUtils.h"
+#include "SkCanvas.h"
+#include "SkPaint.h"
+
+SkMeshIndices::SkMeshIndices() {
+ sk_bzero(this, sizeof(*this));
+}
+
+SkMeshIndices::~SkMeshIndices() {
+ sk_free(fStorage);
+}
+
+bool SkMeshIndices::init(SkPoint tex[], uint16_t indices[],
+ int texW, int texH, int rows, int cols) {
+ if (rows < 2 || cols < 2) {
+ sk_free(fStorage);
+ fStorage = NULL;
+ fTex = NULL;
+ fIndices = NULL;
+ fTexCount = fIndexCount = 0;
+ return false;
+ }
+
+ sk_free(fStorage);
+ fStorage = NULL;
+
+ fTexCount = rows * cols;
+ rows -= 1;
+ cols -= 1;
+ fIndexCount = rows * cols * 6;
+
+ if (tex) {
+ fTex = tex;
+ fIndices = indices;
+ } else {
+ fStorage = sk_malloc_throw(fTexCount * sizeof(SkPoint) +
+ fIndexCount * sizeof(uint16_t));
+ fTex = (SkPoint*)fStorage;
+ fIndices = (uint16_t*)(fTex + fTexCount);
+ }
+
+ // compute the indices
+ {
+ uint16_t* idx = fIndices;
+ int index = 0;
+ for (int y = 0; y < cols; y++) {
+ for (int x = 0; x < rows; x++) {
+ *idx++ = index;
+ *idx++ = index + rows + 1;
+ *idx++ = index + 1;
+
+ *idx++ = index + 1;
+ *idx++ = index + rows + 1;
+ *idx++ = index + rows + 2;
+
+ index += 1;
+ }
+ index += 1;
+ }
+ }
+
+ // compute texture coordinates
+ {
+ SkPoint* tex = fTex;
+ const SkScalar dx = SkIntToScalar(texW) / rows;
+ const SkScalar dy = SkIntToScalar(texH) / cols;
+ for (int y = 0; y <= cols; y++) {
+ for (int x = 0; x <= rows; x++) {
+ tex->set(x*dx, y*dy);
+ tex += 1;
+ }
+ }
+ }
+ return true;
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+#include "SkShader.h"
+
+void SkMeshUtils::Draw(SkCanvas* canvas, const SkBitmap& bitmap,
+ int rows, int cols, const SkPoint verts[],
+ const SkColor colors[], const SkPaint& paint) {
+ SkMeshIndices idx;
+
+ if (idx.init(bitmap.width(), bitmap.height(), rows, cols)) {
+ SkPaint p(paint);
+ p.setShader(SkShader::CreateBitmapShader(bitmap,
+ SkShader::kClamp_TileMode,
+ SkShader::kClamp_TileMode))->unref();
+ canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
+ rows * cols, verts, idx.tex(), colors, NULL,
+ idx.indices(), idx.indexCount(), p);
+ }
+}
+