00001 #ifndef SkGroupShape_DEFINED
00002 #define SkGroupShape_DEFINED
00003
00004 #include "SkMatrix.h"
00005 #include "SkShape.h"
00006 #include "SkTDArray.h"
00007 #include "SkThread.h"
00008
00009 template <typename T> class SkTRefCnt : public T {
00010 public:
00011 SkTRefCnt() : fRefCnt(1) {}
00012 ~SkTRefCnt() { SkASSERT(1 == fRefCnt); }
00013
00014 int32_t getRefCnt() const { return fRefCnt; }
00015
00018 void ref() const {
00019 SkASSERT(fRefCnt > 0);
00020 sk_atomic_inc(&fRefCnt);
00021 }
00022
00028 void unref() const {
00029 SkASSERT(fRefCnt > 0);
00030 if (sk_atomic_dec(&fRefCnt) == 1) {
00031 fRefCnt = 1;
00032 SkDELETE(this);
00033 }
00034 }
00035
00036 static void SafeRef(const SkTRefCnt* obj) {
00037 if (obj) {
00038 obj->ref();
00039 }
00040 }
00041
00042 static void SafeUnref(const SkTRefCnt* obj) {
00043 if (obj) {
00044 obj->unref();
00045 }
00046 }
00047
00048 private:
00049 mutable int32_t fRefCnt;
00050 };
00051
00052 class SkMatrixRef : public SkTRefCnt<SkMatrix> {
00053 public:
00054 SkMatrixRef() { this->reset(); }
00055 explicit SkMatrixRef(const SkMatrix& matrix) {
00056 SkMatrix& m = *this;
00057 m = matrix;
00058 }
00059
00060 SkMatrix& operator=(const SkMatrix& matrix) {
00061 SkMatrix& m = *this;
00062 m = matrix;
00063 return m;
00064 }
00065 };
00066
00067 class SkGroupShape : public SkShape {
00068 public:
00069 SkGroupShape();
00070 virtual ~SkGroupShape();
00071
00074 int countShapes() const;
00075
00079 SkShape* getShape(int index, SkMatrixRef** = NULL) const;
00080
00083 SkMatrixRef* getShapeMatrixRef(int index) const {
00084 SkMatrixRef* mr = NULL;
00085 (void)this->getShape(index, &mr);
00086 return mr;
00087 }
00088
00098 void addShape(int index, SkShape*, SkMatrixRef* = NULL);
00099
00100 void addShape(int index, SkShape* shape, const SkMatrix& matrix) {
00101 SkMatrixRef* mr = SkNEW_ARGS(SkMatrixRef, (matrix));
00102 this->addShape(index, shape, mr);
00103 mr->unref();
00104 }
00105
00108 SkShape* appendShape(SkShape* shape, SkMatrixRef* mr = NULL) {
00109 this->addShape(this->countShapes(), shape, mr);
00110 return shape;
00111 }
00112
00113 SkShape* appendShape(SkShape* shape, const SkMatrix& matrix) {
00114 this->addShape(this->countShapes(), shape, matrix);
00115 return shape;
00116 }
00117
00121 void removeShape(int index);
00122
00125 void removeAllShapes();
00126
00127
00128 virtual Factory getFactory();
00129 virtual void flatten(SkFlattenableWriteBuffer&);
00130
00131
00132 static SkFlattenable* CreateProc(SkFlattenableReadBuffer&);
00133
00134 protected:
00135
00136 virtual void onDraw(SkCanvas*);
00137
00138 SkGroupShape(SkFlattenableReadBuffer&);
00139
00140 private:
00141 struct Rec {
00142 SkShape* fShape;
00143 SkMatrixRef* fMatrixRef;
00144 };
00145 SkTDArray<Rec> fList;
00146
00147 typedef SkShape INHERITED;
00148 };
00149
00150 #endif