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 "SkScalar.h" 12 13 /** 14 * A compressed form of a rotation+scale matrix. 15 * 16 * [ fSCos -fSSin fTx ] 17 * [ fSSin fSCos fTy ] 18 * [ 0 0 1 ] 19 */ 20 struct SkRSXform { 21 static SkRSXform Make(SkScalar scos, SkScalar ssin, SkScalar tx, SkScalar ty) { 22 SkRSXform xform = { scos, ssin, tx, ty }; 23 return xform; 24 } 25 26 /* 27 * Initialize a new xform based on the scale, rotation (in radians), final tx,ty location 28 * and anchor-point ax,ay within the src quad. 29 * 30 * Note: the anchor point is not normalized (e.g. 0...1) but is in pixels of the src image. 31 */ 32 static SkRSXform MakeFromRadians(SkScalar scale, SkScalar radians, SkScalar tx, SkScalar ty, 33 SkScalar ax, SkScalar ay) { 34 const SkScalar s = SkScalarSin(radians) * scale; 35 const SkScalar c = SkScalarCos(radians) * scale; 36 return Make(c, s, tx + -c * ax + s * ay, ty + -s * ax - c * ay); 37 } 38 39 SkScalar fSCos; 40 SkScalar fSSin; 41 SkScalar fTx; 42 SkScalar fTy; 43 44 bool rectStaysRect() const { 45 return 0 == fSCos || 0 == fSSin; 46 } 47 48 void setIdentity() { 49 fSCos = 1; 50 fSSin = fTx = fTy = 0; 51 } 52 53 void set(SkScalar scos, SkScalar ssin, SkScalar tx, SkScalar ty) { 54 fSCos = scos; 55 fSSin = ssin; 56 fTx = tx; 57 fTy = ty; 58 } 59 60 void toQuad(SkScalar width, SkScalar height, SkPoint quad[4]) const; 61 void toQuad(const SkSize& size, SkPoint quad[4]) const { 62 this->toQuad(size.width(), size.height(), quad); 63 } 64 }; 65 66 #endif 67 68