gfx/skia/trunk/src/utils/SkMeshUtils.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/gfx/skia/trunk/src/utils/SkMeshUtils.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,102 @@
     1.4 +
     1.5 +/*
     1.6 + * Copyright 2011 Google Inc.
     1.7 + *
     1.8 + * Use of this source code is governed by a BSD-style license that can be
     1.9 + * found in the LICENSE file.
    1.10 + */
    1.11 +#include "SkMeshUtils.h"
    1.12 +#include "SkCanvas.h"
    1.13 +#include "SkPaint.h"
    1.14 +
    1.15 +SkMeshIndices::SkMeshIndices() {
    1.16 +    sk_bzero(this, sizeof(*this));
    1.17 +}
    1.18 +
    1.19 +SkMeshIndices::~SkMeshIndices() {
    1.20 +    sk_free(fStorage);
    1.21 +}
    1.22 +
    1.23 +bool SkMeshIndices::init(SkPoint tex[], uint16_t indices[],
    1.24 +                         int texW, int texH, int rows, int cols) {
    1.25 +    if (rows < 2 || cols < 2) {
    1.26 +        sk_free(fStorage);
    1.27 +        fStorage = NULL;
    1.28 +        fTex = NULL;
    1.29 +        fIndices = NULL;
    1.30 +        fTexCount = fIndexCount = 0;
    1.31 +        return false;
    1.32 +    }
    1.33 +
    1.34 +    sk_free(fStorage);
    1.35 +    fStorage = NULL;
    1.36 +
    1.37 +    fTexCount = rows * cols;
    1.38 +    rows -= 1;
    1.39 +    cols -= 1;
    1.40 +    fIndexCount = rows * cols * 6;
    1.41 +
    1.42 +    if (tex) {
    1.43 +        fTex = tex;
    1.44 +        fIndices = indices;
    1.45 +    } else {
    1.46 +        fStorage = sk_malloc_throw(fTexCount * sizeof(SkPoint) +
    1.47 +                                   fIndexCount * sizeof(uint16_t));
    1.48 +        fTex = (SkPoint*)fStorage;
    1.49 +        fIndices = (uint16_t*)(fTex + fTexCount);
    1.50 +    }
    1.51 +
    1.52 +    // compute the indices
    1.53 +    {
    1.54 +        uint16_t* idx = fIndices;
    1.55 +        int index = 0;
    1.56 +        for (int y = 0; y < cols; y++) {
    1.57 +            for (int x = 0; x < rows; x++) {
    1.58 +                *idx++ = index;
    1.59 +                *idx++ = index + rows + 1;
    1.60 +                *idx++ = index + 1;
    1.61 +
    1.62 +                *idx++ = index + 1;
    1.63 +                *idx++ = index + rows + 1;
    1.64 +                *idx++ = index + rows + 2;
    1.65 +
    1.66 +                index += 1;
    1.67 +            }
    1.68 +            index += 1;
    1.69 +        }
    1.70 +    }
    1.71 +
    1.72 +    // compute texture coordinates
    1.73 +    {
    1.74 +        SkPoint* tex = fTex;
    1.75 +        const SkScalar dx = SkIntToScalar(texW) / rows;
    1.76 +        const SkScalar dy = SkIntToScalar(texH) / cols;
    1.77 +        for (int y = 0; y <= cols; y++) {
    1.78 +            for (int x = 0; x <= rows; x++) {
    1.79 +                tex->set(x*dx, y*dy);
    1.80 +                tex += 1;
    1.81 +            }
    1.82 +        }
    1.83 +    }
    1.84 +    return true;
    1.85 +}
    1.86 +
    1.87 +///////////////////////////////////////////////////////////////////////////////
    1.88 +
    1.89 +#include "SkShader.h"
    1.90 +
    1.91 +void SkMeshUtils::Draw(SkCanvas* canvas, const SkBitmap& bitmap,
    1.92 +                       int rows, int cols, const SkPoint verts[],
    1.93 +                       const SkColor colors[], const SkPaint& paint) {
    1.94 +    SkMeshIndices idx;
    1.95 +
    1.96 +    if (idx.init(bitmap.width(), bitmap.height(), rows, cols)) {
    1.97 +        SkPaint p(paint);
    1.98 +        p.setShader(SkShader::CreateBitmapShader(bitmap,
    1.99 +                                         SkShader::kClamp_TileMode,
   1.100 +                                         SkShader::kClamp_TileMode))->unref();
   1.101 +        canvas->drawVertices(SkCanvas::kTriangles_VertexMode,
   1.102 +                             rows * cols, verts, idx.tex(), colors, NULL,
   1.103 +                             idx.indices(), idx.indexCount(), p);
   1.104 +    }
   1.105 +}

mercurial