1 /* 2 * Copyright 2015 Google Inc. 3 * 4 * Use of this source code is governed by a BSD-style license that can be 5 * found in the LICENSE file. 6 */ 7 8 #ifndef SkRSXform_DEFINED 9 #define SkRSXform_DEFINED 10 11 #include "include/core/SkPoint.h" 12 #include "include/core/SkScalar.h" 13 #include "include/core/SkSize.h" 14 #include "include/private/base/SkAPI.h" 15 16 /** 17 * A compressed form of a rotation+scale matrix. 18 * 19 * [ fSCos -fSSin fTx ] 20 * [ fSSin fSCos fTy ] 21 * [ 0 0 1 ] 22 */ 23 struct SK_API SkRSXform { MakeSkRSXform24 static SkRSXform Make(SkScalar scos, SkScalar ssin, SkScalar tx, SkScalar ty) { 25 SkRSXform xform = { scos, ssin, tx, ty }; 26 return xform; 27 } 28 29 /* 30 * Initialize a new xform based on the scale, rotation (in radians), final tx,ty location 31 * and anchor-point ax,ay within the src quad. 32 * 33 * Note: the anchor point is not normalized (e.g. 0...1) but is in pixels of the src image. 34 */ MakeFromRadiansSkRSXform35 static SkRSXform MakeFromRadians(SkScalar scale, SkScalar radians, SkScalar tx, SkScalar ty, 36 SkScalar ax, SkScalar ay) { 37 const SkScalar s = SkScalarSin(radians) * scale; 38 const SkScalar c = SkScalarCos(radians) * scale; 39 return Make(c, s, tx + -c * ax + s * ay, ty + -s * ax - c * ay); 40 } 41 42 SkScalar fSCos; 43 SkScalar fSSin; 44 SkScalar fTx; 45 SkScalar fTy; 46 rectStaysRectSkRSXform47 bool rectStaysRect() const { 48 return 0 == fSCos || 0 == fSSin; 49 } 50 setIdentitySkRSXform51 void setIdentity() { 52 fSCos = 1; 53 fSSin = fTx = fTy = 0; 54 } 55 setSkRSXform56 void set(SkScalar scos, SkScalar ssin, SkScalar tx, SkScalar ty) { 57 fSCos = scos; 58 fSSin = ssin; 59 fTx = tx; 60 fTy = ty; 61 } 62 63 void toQuad(SkScalar width, SkScalar height, SkPoint quad[4]) const; toQuadSkRSXform64 void toQuad(const SkSize& size, SkPoint quad[4]) const { 65 this->toQuad(size.width(), size.height(), quad); 66 } 67 void toTriStrip(SkScalar width, SkScalar height, SkPoint strip[4]) const; 68 }; 69 70 #endif 71 72