michael@0: michael@0: /* michael@0: * Copyright 2011 Google Inc. michael@0: * michael@0: * Use of this source code is governed by a BSD-style license that can be michael@0: * found in the LICENSE file. michael@0: */ michael@0: #include "SkMeshUtils.h" michael@0: #include "SkCanvas.h" michael@0: #include "SkPaint.h" michael@0: michael@0: SkMeshIndices::SkMeshIndices() { michael@0: sk_bzero(this, sizeof(*this)); michael@0: } michael@0: michael@0: SkMeshIndices::~SkMeshIndices() { michael@0: sk_free(fStorage); michael@0: } michael@0: michael@0: bool SkMeshIndices::init(SkPoint tex[], uint16_t indices[], michael@0: int texW, int texH, int rows, int cols) { michael@0: if (rows < 2 || cols < 2) { michael@0: sk_free(fStorage); michael@0: fStorage = NULL; michael@0: fTex = NULL; michael@0: fIndices = NULL; michael@0: fTexCount = fIndexCount = 0; michael@0: return false; michael@0: } michael@0: michael@0: sk_free(fStorage); michael@0: fStorage = NULL; michael@0: michael@0: fTexCount = rows * cols; michael@0: rows -= 1; michael@0: cols -= 1; michael@0: fIndexCount = rows * cols * 6; michael@0: michael@0: if (tex) { michael@0: fTex = tex; michael@0: fIndices = indices; michael@0: } else { michael@0: fStorage = sk_malloc_throw(fTexCount * sizeof(SkPoint) + michael@0: fIndexCount * sizeof(uint16_t)); michael@0: fTex = (SkPoint*)fStorage; michael@0: fIndices = (uint16_t*)(fTex + fTexCount); michael@0: } michael@0: michael@0: // compute the indices michael@0: { michael@0: uint16_t* idx = fIndices; michael@0: int index = 0; michael@0: for (int y = 0; y < cols; y++) { michael@0: for (int x = 0; x < rows; x++) { michael@0: *idx++ = index; michael@0: *idx++ = index + rows + 1; michael@0: *idx++ = index + 1; michael@0: michael@0: *idx++ = index + 1; michael@0: *idx++ = index + rows + 1; michael@0: *idx++ = index + rows + 2; michael@0: michael@0: index += 1; michael@0: } michael@0: index += 1; michael@0: } michael@0: } michael@0: michael@0: // compute texture coordinates michael@0: { michael@0: SkPoint* tex = fTex; michael@0: const SkScalar dx = SkIntToScalar(texW) / rows; michael@0: const SkScalar dy = SkIntToScalar(texH) / cols; michael@0: for (int y = 0; y <= cols; y++) { michael@0: for (int x = 0; x <= rows; x++) { michael@0: tex->set(x*dx, y*dy); michael@0: tex += 1; michael@0: } michael@0: } michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: /////////////////////////////////////////////////////////////////////////////// michael@0: michael@0: #include "SkShader.h" michael@0: michael@0: void SkMeshUtils::Draw(SkCanvas* canvas, const SkBitmap& bitmap, michael@0: int rows, int cols, const SkPoint verts[], michael@0: const SkColor colors[], const SkPaint& paint) { michael@0: SkMeshIndices idx; michael@0: michael@0: if (idx.init(bitmap.width(), bitmap.height(), rows, cols)) { michael@0: SkPaint p(paint); michael@0: p.setShader(SkShader::CreateBitmapShader(bitmap, michael@0: SkShader::kClamp_TileMode, michael@0: SkShader::kClamp_TileMode))->unref(); michael@0: canvas->drawVertices(SkCanvas::kTriangles_VertexMode, michael@0: rows * cols, verts, idx.tex(), colors, NULL, michael@0: idx.indices(), idx.indexCount(), p); michael@0: } michael@0: }