1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/gfx/skia/trunk/src/utils/SkBoundaryPatch.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,79 @@ 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 "SkBoundaryPatch.h" 1.12 + 1.13 +SkBoundaryPatch::SkBoundaryPatch() : fBoundary(NULL) {} 1.14 + 1.15 +SkBoundaryPatch::~SkBoundaryPatch() { 1.16 + SkSafeUnref(fBoundary); 1.17 +} 1.18 + 1.19 +SkBoundary* SkBoundaryPatch::setBoundary(SkBoundary* b) { 1.20 + SkRefCnt_SafeAssign(fBoundary, b); 1.21 + return b; 1.22 +} 1.23 + 1.24 +static SkPoint SkMakePoint(SkScalar x, SkScalar y) { 1.25 + SkPoint pt; 1.26 + pt.set(x, y); 1.27 + return pt; 1.28 +} 1.29 + 1.30 +static SkPoint SkPointInterp(const SkPoint& a, const SkPoint& b, SkScalar t) { 1.31 + return SkMakePoint(SkScalarInterp(a.fX, b.fX, t), 1.32 + SkScalarInterp(a.fY, b.fY, t)); 1.33 +} 1.34 + 1.35 +SkPoint SkBoundaryPatch::eval(SkScalar unitU, SkScalar unitV) { 1.36 + SkBoundary* b = fBoundary; 1.37 + SkPoint u = SkPointInterp(b->eval(SkBoundary::kLeft, SK_Scalar1 - unitV), 1.38 + b->eval(SkBoundary::kRight, unitV), 1.39 + unitU); 1.40 + SkPoint v = SkPointInterp(b->eval(SkBoundary::kTop, unitU), 1.41 + b->eval(SkBoundary::kBottom, SK_Scalar1 - unitU), 1.42 + unitV); 1.43 + return SkMakePoint(SkScalarAve(u.fX, v.fX), 1.44 + SkScalarAve(u.fY, v.fY)); 1.45 +} 1.46 + 1.47 +bool SkBoundaryPatch::evalPatch(SkPoint verts[], int rows, int cols) { 1.48 + if (rows < 2 || cols < 2) { 1.49 + return false; 1.50 + } 1.51 + 1.52 + const SkScalar invR = SkScalarInvert(SkIntToScalar(rows - 1)); 1.53 + const SkScalar invC = SkScalarInvert(SkIntToScalar(cols - 1)); 1.54 + 1.55 + for (int y = 0; y < cols; y++) { 1.56 + SkScalar yy = y * invC; 1.57 + for (int x = 0; x < rows; x++) { 1.58 + *verts++ = this->eval(x * invR, yy); 1.59 + } 1.60 + } 1.61 + return true; 1.62 +} 1.63 + 1.64 +//////////////////////////////////////////////////////////////////////// 1.65 + 1.66 +#include "SkGeometry.h" 1.67 + 1.68 +SkPoint SkLineBoundary::eval(Edge e, SkScalar t) { 1.69 + SkASSERT((unsigned)e < 4); 1.70 + return SkPointInterp(fPts[e], fPts[(e + 1) & 3], t); 1.71 +} 1.72 + 1.73 +SkPoint SkCubicBoundary::eval(Edge e, SkScalar t) { 1.74 + SkASSERT((unsigned)e < 4); 1.75 + 1.76 + // ensure our 4th cubic wraps to the start of the first 1.77 + fPts[12] = fPts[0]; 1.78 + 1.79 + SkPoint loc; 1.80 + SkEvalCubicAt(&fPts[e * 3], t, &loc, NULL, NULL); 1.81 + return loc; 1.82 +}