blob: 3857dc954f41ed91f277a3f7916a2f54b6e456bc [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.com2ee7c642009-10-28 14:25:34 +00008#include "SkMeshUtils.h"
9#include "SkCanvas.h"
10#include "SkPaint.h"
11
12SkMeshIndices::SkMeshIndices() {
13 sk_bzero(this, sizeof(*this));
14}
15
16SkMeshIndices::~SkMeshIndices() {
17 sk_free(fStorage);
18}
19
20bool SkMeshIndices::init(SkPoint tex[], uint16_t indices[],
21 int texW, int texH, int rows, int cols) {
22 if (rows < 2 || cols < 2) {
23 sk_free(fStorage);
24 fStorage = NULL;
25 fTex = NULL;
26 fIndices = NULL;
27 fTexCount = fIndexCount = 0;
28 return false;
29 }
30
31 sk_free(fStorage);
32 fStorage = NULL;
33
34 fTexCount = rows * cols;
35 rows -= 1;
36 cols -= 1;
37 fIndexCount = rows * cols * 6;
38
39 if (tex) {
40 fTex = tex;
41 fIndices = indices;
42 } else {
43 fStorage = sk_malloc_throw(fTexCount * sizeof(SkPoint) +
44 fIndexCount * sizeof(uint16_t));
45 fTex = (SkPoint*)fStorage;
46 fIndices = (uint16_t*)(fTex + fTexCount);
47 }
48
49 // compute the indices
50 {
51 uint16_t* idx = fIndices;
52 int index = 0;
53 for (int y = 0; y < cols; y++) {
54 for (int x = 0; x < rows; x++) {
55 *idx++ = index;
56 *idx++ = index + rows + 1;
57 *idx++ = index + 1;
rmistry@google.comd6176b02012-08-23 18:14:13 +000058
reed@android.com2ee7c642009-10-28 14:25:34 +000059 *idx++ = index + 1;
60 *idx++ = index + rows + 1;
61 *idx++ = index + rows + 2;
rmistry@google.comd6176b02012-08-23 18:14:13 +000062
reed@android.com2ee7c642009-10-28 14:25:34 +000063 index += 1;
64 }
65 index += 1;
66 }
67 }
68
69 // compute texture coordinates
70 {
71 SkPoint* tex = fTex;
72 const SkScalar dx = SkIntToScalar(texW) / rows;
73 const SkScalar dy = SkIntToScalar(texH) / cols;
74 for (int y = 0; y <= cols; y++) {
75 for (int x = 0; x <= rows; x++) {
76 tex->set(x*dx, y*dy);
77 tex += 1;
78 }
79 }
80 }
81 return true;
82}
83
84///////////////////////////////////////////////////////////////////////////////
85
86#include "SkShader.h"
87
88void SkMeshUtils::Draw(SkCanvas* canvas, const SkBitmap& bitmap,
89 int rows, int cols, const SkPoint verts[],
90 const SkColor colors[], const SkPaint& paint) {
91 SkMeshIndices idx;
rmistry@google.comd6176b02012-08-23 18:14:13 +000092
reed@android.com2ee7c642009-10-28 14:25:34 +000093 if (idx.init(bitmap.width(), bitmap.height(), rows, cols)) {
94 SkPaint p(paint);
95 p.setShader(SkShader::CreateBitmapShader(bitmap,
96 SkShader::kClamp_TileMode,
97 SkShader::kClamp_TileMode))->unref();
98 canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
99 rows * cols, verts, idx.tex(), colors, NULL,
100 idx.indices(), idx.indexCount(), p);
101 }
102}