add helper to create RSXform w/ anchorPt
authorreed <reed@google.com>
Thu, 30 Jul 2015 12:46:05 +0000 (05:46 -0700)
committerCommit bot <commit-bot@chromium.org>
Thu, 30 Jul 2015 12:46:05 +0000 (05:46 -0700)
BUG=skia:

Review URL: https://codereview.chromium.org/1269563006

include/core/SkRSXform.h
samplecode/SampleAtlas.cpp

index 5dffdc2..7af6e67 100644 (file)
  *  [     0          0      1 ]
  */
 struct SkRSXform {
-    SkScalar    fSCos;
-    SkScalar    fSSin;
-    SkScalar    fTx;
-    SkScalar    fTy;
+    static SkRSXform Make(SkScalar scos, SkScalar ssin, SkScalar tx, SkScalar ty) {
+        SkRSXform xform = { scos, ssin, tx, ty };
+        return xform;
+    }
+
+    /*
+     *  Initialize a new xform based on the scale, rotation (in radians), final tx,ty location
+     *  and anchor-point ax,ay within the src quad.
+     *
+     *  Note: the anchor point is not normalized (e.g. 0...1) but is in pixels of the src image.
+     */
+    static SkRSXform MakeFromRadians(SkScalar scale, SkScalar radians, SkScalar tx, SkScalar ty,
+                                     SkScalar ax, SkScalar ay) {
+        const SkScalar s = SkScalarSin(radians) * scale;
+        const SkScalar c = SkScalarCos(radians) * scale;
+        return Make(c, s, tx + -c * ax + s * ay, ty + -s * ax - c * ay);
+    }
+
+    SkScalar fSCos;
+    SkScalar fSSin;
+    SkScalar fTx;
+    SkScalar fTy;
 
     bool rectStaysRect() const {
         return 0 == fSCos || 0 == fSSin;
index ed208c3..079d679 100644 (file)
@@ -96,18 +96,8 @@ class DrawAtlasDrawable : public SkDrawable {
         }
         
         SkRSXform asRSXform() const {
-            SkMatrix m;
-            m.setTranslate(-8, -8);
-            m.postScale(fScale, fScale);
-            m.postRotate(SkRadiansToDegrees(fRadian));
-            m.postTranslate(fCenter.fX, fCenter.fY);
-
-            SkRSXform x;
-            x.fSCos = m.getScaleX();
-            x.fSSin = m.getSkewY();
-            x.fTx = m.getTranslateX();
-            x.fTy = m.getTranslateY();
-            return x;
+            return SkRSXform::MakeFromRadians(fScale, fRadian, fCenter.x(), fCenter.y(),
+                                              SkScalarHalf(kCellSize), SkScalarHalf(kCellSize));
         }
     };
 
@@ -138,7 +128,7 @@ public:
                 fRec[i].fVelocity.fX = rand.nextSScalar1() * kMaxSpeed;
                 fRec[i].fVelocity.fY = rand.nextSScalar1() * kMaxSpeed;
                 fRec[i].fScale = 1;
-                fRec[i].fDScale = rand.nextSScalar1() / 4;
+                fRec[i].fDScale = rand.nextSScalar1() / 16;
                 fRec[i].fRadian = 0;
                 fRec[i].fDRadian = rand.nextSScalar1() / 8;
                 fRec[i].fAlpha = rand.nextUScalar1();