2 * Copyright 2011 Google Inc.
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
7 #include "SkMeshUtils.h"
11 SkMeshIndices::SkMeshIndices() {
12 sk_bzero(this, sizeof(*this));
15 SkMeshIndices::~SkMeshIndices() {
19 bool SkMeshIndices::init(SkPoint tex[], uint16_t indices[],
20 int texW, int texH, int rows, int cols) {
21 if (rows < 2 || cols < 2) {
26 fTexCount = fIndexCount = 0;
33 fTexCount = rows * cols;
36 fIndexCount = rows * cols * 6;
42 fStorage = sk_malloc_throw(fTexCount * sizeof(SkPoint) +
43 fIndexCount * sizeof(uint16_t));
44 fTex = (SkPoint*)fStorage;
45 fIndices = (uint16_t*)(fTex + fTexCount);
48 // compute the indices
50 uint16_t* idx = fIndices;
52 for (int y = 0; y < cols; y++) {
53 for (int x = 0; x < rows; x++) {
55 *idx++ = index + rows + 1;
59 *idx++ = index + rows + 1;
60 *idx++ = index + rows + 2;
68 // compute texture coordinates
71 const SkScalar dx = SkIntToScalar(texW) / rows;
72 const SkScalar dy = SkIntToScalar(texH) / cols;
73 for (int y = 0; y <= cols; y++) {
74 for (int x = 0; x <= rows; x++) {
83 ///////////////////////////////////////////////////////////////////////////////
87 void SkMeshUtils::Draw(SkCanvas* canvas, const SkBitmap& bitmap,
88 int rows, int cols, const SkPoint verts[],
89 const SkColor colors[], const SkPaint& paint) {
92 if (idx.init(bitmap.width(), bitmap.height(), rows, cols)) {
94 p.setShader(SkShader::MakeBitmapShader(bitmap,
95 SkShader::kClamp_TileMode,
96 SkShader::kClamp_TileMode));
97 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
98 rows * cols, verts, idx.tex(), colors, nullptr,
99 idx.indices(), idx.indexCount(), p);