00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #ifndef SkMatrix_DEFINED
00018 #define SkMatrix_DEFINED
00019
00020 #include "SkRect.h"
00021
00022 class SkString;
00023
00031 class SkMatrix {
00032 public:
00036 enum TypeMask {
00037 kIdentity_Mask = 0,
00038 kTranslate_Mask = 0x01,
00039 kScale_Mask = 0x02,
00040 kAffine_Mask = 0x04,
00041 kPerspective_Mask = 0x08
00042 };
00043
00049 TypeMask getType() const {
00050 if (fTypeMask & kUnknown_Mask) {
00051 fTypeMask = this->computeTypeMask();
00052 }
00053
00054 return (TypeMask)(fTypeMask & 0xF);
00055 }
00056
00059 bool isIdentity() const {
00060 return this->getType() == 0;
00061 }
00062
00067 bool rectStaysRect() const {
00068 if (fTypeMask & kUnknown_Mask) {
00069 fTypeMask = this->computeTypeMask();
00070 }
00071 return (fTypeMask & kRectStaysRect_Mask) != 0;
00072 }
00073
00074 enum {
00075 kMScaleX,
00076 kMSkewX,
00077 kMTransX,
00078 kMSkewY,
00079 kMScaleY,
00080 kMTransY,
00081 kMPersp0,
00082 kMPersp1,
00083 kMPersp2
00084 };
00085
00086 SkScalar operator[](int index) const {
00087 SkASSERT((unsigned)index < 9);
00088 return fMat[index];
00089 }
00090
00091 SkScalar get(int index) const {
00092 SkASSERT((unsigned)index < 9);
00093 return fMat[index];
00094 }
00095
00096 SkScalar getScaleX() const { return fMat[kMScaleX]; }
00097 SkScalar getScaleY() const { return fMat[kMScaleY]; }
00098 SkScalar getSkewY() const { return fMat[kMSkewY]; }
00099 SkScalar getSkewX() const { return fMat[kMSkewX]; }
00100 SkScalar getTranslateX() const { return fMat[kMTransX]; }
00101 SkScalar getTranslateY() const { return fMat[kMTransY]; }
00102 SkScalar getPerspX() const { return fMat[kMPersp0]; }
00103 SkScalar getPerspY() const { return fMat[kMPersp1]; }
00104
00105 void set(int index, SkScalar value) {
00106 SkASSERT((unsigned)index < 9);
00107 fMat[index] = value;
00108 this->setTypeMask(kUnknown_Mask);
00109 }
00110
00111 void setScaleX(SkScalar v) { this->set(kMScaleX, v); }
00112 void setScaleY(SkScalar v) { this->set(kMScaleY, v); }
00113 void setSkewY(SkScalar v) { this->set(kMSkewY, v); }
00114 void setSkewX(SkScalar v) { this->set(kMSkewX, v); }
00115 void setTranslateX(SkScalar v) { this->set(kMTransX, v); }
00116 void setTranslateY(SkScalar v) { this->set(kMTransY, v); }
00117 void setPerspX(SkScalar v) { this->set(kMPersp0, v); }
00118 void setPerspY(SkScalar v) { this->set(kMPersp1, v); }
00119
00122 void reset();
00123
00126 void setTranslate(SkScalar dx, SkScalar dy);
00131 void setScale(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py);
00134 void setScale(SkScalar sx, SkScalar sy);
00139 void setRotate(SkScalar degrees, SkScalar px, SkScalar py);
00142 void setRotate(SkScalar degrees);
00147 void setSinCos(SkScalar sinValue, SkScalar cosValue,
00148 SkScalar px, SkScalar py);
00151 void setSinCos(SkScalar sinValue, SkScalar cosValue);
00156 void setSkew(SkScalar kx, SkScalar ky, SkScalar px, SkScalar py);
00159 void setSkew(SkScalar kx, SkScalar ky);
00164 bool setConcat(const SkMatrix& a, const SkMatrix& b);
00165
00169 bool preTranslate(SkScalar dx, SkScalar dy);
00173 bool preScale(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py);
00177 bool preScale(SkScalar sx, SkScalar sy);
00181 bool preRotate(SkScalar degrees, SkScalar px, SkScalar py);
00185 bool preRotate(SkScalar degrees);
00189 bool preSkew(SkScalar kx, SkScalar ky, SkScalar px, SkScalar py);
00193 bool preSkew(SkScalar kx, SkScalar ky);
00197 bool preConcat(const SkMatrix& other);
00198
00202 bool postTranslate(SkScalar dx, SkScalar dy);
00206 bool postScale(SkScalar sx, SkScalar sy, SkScalar px, SkScalar py);
00210 bool postScale(SkScalar sx, SkScalar sy);
00214 bool postIDiv(int divx, int divy);
00218 bool postRotate(SkScalar degrees, SkScalar px, SkScalar py);
00222 bool postRotate(SkScalar degrees);
00226 bool postSkew(SkScalar kx, SkScalar ky, SkScalar px, SkScalar py);
00230 bool postSkew(SkScalar kx, SkScalar ky);
00234 bool postConcat(const SkMatrix& other);
00235
00236 enum ScaleToFit {
00241 kFill_ScaleToFit,
00248 kStart_ScaleToFit,
00254 kCenter_ScaleToFit,
00261 kEnd_ScaleToFit
00262 };
00263
00272 bool setRectToRect(const SkRect& src, const SkRect& dst, ScaleToFit stf);
00273
00281 bool setPolyToPoly(const SkPoint src[], const SkPoint dst[], int count);
00282
00287 bool invert(SkMatrix* inverse) const;
00288
00299 void mapPoints(SkPoint dst[], const SkPoint src[], int count) const;
00300
00308 void mapPoints(SkPoint pts[], int count) const {
00309 this->mapPoints(pts, pts, count);
00310 }
00311
00312 void mapXY(SkScalar x, SkScalar y, SkPoint* result) const {
00313 SkASSERT(result);
00314 this->getMapXYProc()(*this, x, y, result);
00315 }
00316
00327 void mapVectors(SkVector dst[], const SkVector src[], int count) const;
00328
00336 void mapVectors(SkVector vecs[], int count) const {
00337 this->mapVectors(vecs, vecs, count);
00338 }
00339
00347 bool mapRect(SkRect* dst, const SkRect& src) const;
00348
00355 bool mapRect(SkRect* rect) const {
00356 return this->mapRect(rect, *rect);
00357 }
00358
00363 SkScalar mapRadius(SkScalar radius) const;
00364
00365 typedef void (*MapXYProc)(const SkMatrix& mat, SkScalar x, SkScalar y,
00366 SkPoint* result);
00367
00368 static MapXYProc GetMapXYProc(TypeMask mask) {
00369 SkASSERT((mask & ~kAllMasks) == 0);
00370 return gMapXYProcs[mask & kAllMasks];
00371 }
00372
00373 MapXYProc getMapXYProc() const {
00374 return GetMapXYProc(this->getType());
00375 }
00376
00377 typedef void (*MapPtsProc)(const SkMatrix& mat, SkPoint dst[],
00378 const SkPoint src[], int count);
00379
00380 static MapPtsProc GetMapPtsProc(TypeMask mask) {
00381 SkASSERT((mask & ~kAllMasks) == 0);
00382 return gMapPtsProcs[mask & kAllMasks];
00383 }
00384
00385 MapPtsProc getMapPtsProc() const {
00386 return GetMapPtsProc(this->getType());
00387 }
00388
00393 bool fixedStepInX(SkScalar y, SkFixed* stepX, SkFixed* stepY) const;
00394
00395 friend bool operator==(const SkMatrix& a, const SkMatrix& b) {
00396 return memcmp(a.fMat, b.fMat, sizeof(a.fMat)) == 0;
00397 }
00398
00399 friend bool operator!=(const SkMatrix& a, const SkMatrix& b) {
00400 return memcmp(a.fMat, b.fMat, sizeof(a.fMat)) != 0;
00401 }
00402
00403 enum {
00404
00405 kMaxFlattenSize = 9 * sizeof(SkScalar) + sizeof(uint32_t)
00406 };
00407
00408 uint32_t flatten(void* buffer) const;
00409
00410 uint32_t unflatten(const void* buffer);
00411
00412 void dump() const;
00413 void toDumpString(SkString*) const;
00414
00415 private:
00416 enum {
00423 kRectStaysRect_Mask = 0x10,
00424
00425 kUnknown_Mask = 0x80,
00426
00427 kAllMasks = kTranslate_Mask |
00428 kScale_Mask |
00429 kAffine_Mask |
00430 kPerspective_Mask |
00431 kRectStaysRect_Mask
00432 };
00433
00434 SkScalar fMat[9];
00435 mutable uint8_t fTypeMask;
00436
00437 uint8_t computeTypeMask() const;
00438
00439 void setTypeMask(int mask) {
00440
00441 SkASSERT(kUnknown_Mask == mask || (mask & kAllMasks) == mask);
00442 fTypeMask = SkToU8(mask);
00443 }
00444
00445 void clearTypeMask(int mask) {
00446
00447 SkASSERT((mask & kAllMasks) == mask);
00448 fTypeMask &= ~mask;
00449 }
00450
00451 static bool Poly2Proc(const SkPoint[], SkMatrix*, const SkPoint& scale);
00452 static bool Poly3Proc(const SkPoint[], SkMatrix*, const SkPoint& scale);
00453 static bool Poly4Proc(const SkPoint[], SkMatrix*, const SkPoint& scale);
00454
00455 static void Identity_xy(const SkMatrix&, SkScalar, SkScalar, SkPoint*);
00456 static void Trans_xy(const SkMatrix&, SkScalar, SkScalar, SkPoint*);
00457 static void Scale_xy(const SkMatrix&, SkScalar, SkScalar, SkPoint*);
00458 static void ScaleTrans_xy(const SkMatrix&, SkScalar, SkScalar, SkPoint*);
00459 static void Rot_xy(const SkMatrix&, SkScalar, SkScalar, SkPoint*);
00460 static void RotTrans_xy(const SkMatrix&, SkScalar, SkScalar, SkPoint*);
00461 static void Persp_xy(const SkMatrix&, SkScalar, SkScalar, SkPoint*);
00462
00463 static const MapXYProc gMapXYProcs[];
00464
00465 static void Identity_pts(const SkMatrix&, SkPoint[], const SkPoint[], int);
00466 static void Trans_pts(const SkMatrix&, SkPoint dst[], const SkPoint[], int);
00467 static void Scale_pts(const SkMatrix&, SkPoint dst[], const SkPoint[], int);
00468 static void ScaleTrans_pts(const SkMatrix&, SkPoint dst[], const SkPoint[],
00469 int count);
00470 static void Rot_pts(const SkMatrix&, SkPoint dst[], const SkPoint[], int);
00471 static void RotTrans_pts(const SkMatrix&, SkPoint dst[], const SkPoint[],
00472 int count);
00473 static void Persp_pts(const SkMatrix&, SkPoint dst[], const SkPoint[], int);
00474
00475 static const MapPtsProc gMapPtsProcs[];
00476
00477 friend class SkPerspIter;
00478 };
00479
00480 #endif
00481