180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/* 380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Copyright 2011 Google Inc. 480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * 580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * Use of this source code is governed by a BSD-style license that can be 680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru * found in the LICENSE file. 780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru */ 880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkMeshUtils.h" 980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkCanvas.h" 1080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkPaint.h" 1180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 1280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruSkMeshIndices::SkMeshIndices() { 1380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru sk_bzero(this, sizeof(*this)); 1480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru} 1580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 1680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste QueruSkMeshIndices::~SkMeshIndices() { 1780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru sk_free(fStorage); 1880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru} 1980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 2080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Querubool SkMeshIndices::init(SkPoint tex[], uint16_t indices[], 2180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru int texW, int texH, int rows, int cols) { 2280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru if (rows < 2 || cols < 2) { 2380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru sk_free(fStorage); 2480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru fStorage = NULL; 2580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru fTex = NULL; 2680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru fIndices = NULL; 2780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru fTexCount = fIndexCount = 0; 2880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru return false; 2980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru } 3080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 3180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru sk_free(fStorage); 3280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru fStorage = NULL; 3380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 3480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru fTexCount = rows * cols; 3580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru rows -= 1; 3680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru cols -= 1; 3780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru fIndexCount = rows * cols * 6; 3880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 3980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru if (tex) { 4080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru fTex = tex; 4180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru fIndices = indices; 4280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru } else { 4380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru fStorage = sk_malloc_throw(fTexCount * sizeof(SkPoint) + 4480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru fIndexCount * sizeof(uint16_t)); 4580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru fTex = (SkPoint*)fStorage; 4680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru fIndices = (uint16_t*)(fTex + fTexCount); 4780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru } 4880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 4980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru // compute the indices 5080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru { 5180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru uint16_t* idx = fIndices; 5280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru int index = 0; 5380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru for (int y = 0; y < cols; y++) { 5480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru for (int x = 0; x < rows; x++) { 5580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *idx++ = index; 5680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *idx++ = index + rows + 1; 5780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *idx++ = index + 1; 5880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 5980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *idx++ = index + 1; 6080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *idx++ = index + rows + 1; 6180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru *idx++ = index + rows + 2; 6280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 6380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru index += 1; 6480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru } 6580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru index += 1; 6680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru } 6780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru } 6880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 6980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru // compute texture coordinates 7080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru { 7180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkPoint* tex = fTex; 7280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru const SkScalar dx = SkIntToScalar(texW) / rows; 7380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru const SkScalar dy = SkIntToScalar(texH) / cols; 7480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru for (int y = 0; y <= cols; y++) { 7580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru for (int x = 0; x <= rows; x++) { 7680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru tex->set(x*dx, y*dy); 7780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru tex += 1; 7880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru } 7980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru } 8080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru } 8180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru return true; 8280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru} 8380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 8480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru/////////////////////////////////////////////////////////////////////////////// 8580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 8680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru#include "SkShader.h" 8780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 8880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queruvoid SkMeshUtils::Draw(SkCanvas* canvas, const SkBitmap& bitmap, 8980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru int rows, int cols, const SkPoint verts[], 9080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru const SkColor colors[], const SkPaint& paint) { 9180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkMeshIndices idx; 9280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru 9380bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru if (idx.init(bitmap.width(), bitmap.height(), rows, cols)) { 9480bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkPaint p(paint); 9580bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru p.setShader(SkShader::CreateBitmapShader(bitmap, 9680bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkShader::kClamp_TileMode, 9780bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru SkShader::kClamp_TileMode))->unref(); 9880bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru canvas->drawVertices(SkCanvas::kTriangles_VertexMode, 9980bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru rows * cols, verts, idx.tex(), colors, NULL, 10080bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru idx.indices(), idx.indexCount(), p); 10180bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru } 10280bacfeb4bda06541e8695bd502229727bccfeaJean-Baptiste Queru} 103