aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/utils/SkMeshUtils.cpp
blob: 7970ba1af402b27084fc69146e2c64a5db6f7628 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

/*
 * Copyright 2011 Google Inc.
 *
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 */
#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);
    }
}