diff options
author | 2009-10-28 14:25:34 +0000 | |
---|---|---|
committer | 2009-10-28 14:25:34 +0000 | |
commit | 2ee7c640150a863f2859eb0aaf4dfe7ad7836f34 (patch) | |
tree | 14932aa2f166fecb6f36b2fa4b31ad96eac0b288 /src | |
parent | b577b41c8ec26c21ae599e80a2707d42f03eaa60 (diff) |
update on boundary patches
git-svn-id: http://skia.googlecode.com/svn/trunk@409 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src')
-rw-r--r-- | src/utils/SkMeshUtils.cpp | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/src/utils/SkMeshUtils.cpp b/src/utils/SkMeshUtils.cpp new file mode 100644 index 0000000000..0385b99d01 --- /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); + } +} + |