blob: 3baa3152ed2a4314535396957ad43b6aeb473973 [file] [log] [blame]
reed@android.comf76bacf2009-05-13 14:00:33 +00001#include "SkGroupShape.h"
2
3SkGroupShape::SkGroupShape() {}
4
5SkGroupShape::~SkGroupShape() {
6 this->removeAllShapes();
7}
8
9int SkGroupShape::countShapes() const {
10 return fList.count();
11}
12
reed@android.comf48f2812009-05-18 04:20:55 +000013SkShape* SkGroupShape::getShape(int index, SkMatrixRef** mr) const {
reed@android.comf76bacf2009-05-13 14:00:33 +000014 if ((unsigned)index < (unsigned)fList.count()) {
reed@android.comf48f2812009-05-18 04:20:55 +000015 const Rec& rec = fList[index];
16 if (mr) {
17 *mr = rec.fMatrixRef;
18 }
19 return rec.fShape;
reed@android.comf76bacf2009-05-13 14:00:33 +000020 }
21 return NULL;
22}
23
reed@android.comf48f2812009-05-18 04:20:55 +000024void SkGroupShape::addShape(int index, SkShape* shape, SkMatrixRef* mr) {
reed@android.comf76bacf2009-05-13 14:00:33 +000025 int count = fList.count();
26 if (NULL == shape || index < 0 || index > count) {
reed@android.comf48f2812009-05-18 04:20:55 +000027 return;
reed@android.comf76bacf2009-05-13 14:00:33 +000028 }
29
30 shape->ref();
reed@android.comf48f2812009-05-18 04:20:55 +000031 SkMatrixRef::SafeRef(mr);
32
33 Rec* rec;
reed@android.comf76bacf2009-05-13 14:00:33 +000034 if (index == count) {
reed@android.comf48f2812009-05-18 04:20:55 +000035 rec = fList.append();
reed@android.comf76bacf2009-05-13 14:00:33 +000036 } else {
reed@android.comf48f2812009-05-18 04:20:55 +000037 rec = fList.insert(index);
reed@android.comf76bacf2009-05-13 14:00:33 +000038 }
reed@android.comf48f2812009-05-18 04:20:55 +000039 rec->fShape = shape;
40 rec->fMatrixRef = mr;
reed@android.comf76bacf2009-05-13 14:00:33 +000041}
42
43void SkGroupShape::removeShape(int index) {
44 if ((unsigned)index < (unsigned)fList.count()) {
reed@android.comf48f2812009-05-18 04:20:55 +000045 Rec& rec = fList[index];
46 rec.fShape->unref();
47 SkMatrixRef::SafeUnref(rec.fMatrixRef);
reed@android.comf76bacf2009-05-13 14:00:33 +000048 fList.remove(index);
49 }
50}
51
52void SkGroupShape::removeAllShapes() {
reed@android.comf48f2812009-05-18 04:20:55 +000053 Rec* rec = fList.begin();
54 Rec* stop = fList.end();
55 while (rec < stop) {
56 rec->fShape->unref();
57 SkMatrixRef::SafeUnref(rec->fMatrixRef);
58 rec++;
59 }
reed@android.comf76bacf2009-05-13 14:00:33 +000060 fList.reset();
61}
62
63///////////////////////////////////////////////////////////////////////////////
64
65void SkGroupShape::onDraw(SkCanvas* canvas) {
reed@android.comf48f2812009-05-18 04:20:55 +000066 const Rec* rec = fList.begin();
67 const Rec* stop = fList.end();
68 while (rec < stop) {
69 SkShape* shape = rec->fShape;
70 if (rec->fMatrixRef) {
71 shape->drawMatrix(canvas, *rec->fMatrixRef);
72 } else {
73 shape->draw(canvas);
74 }
75 rec++;
reed@android.comf76bacf2009-05-13 14:00:33 +000076 }
77}
78
79SkFlattenable::Factory SkGroupShape::getFactory() {
80 return CreateProc;
81}
82
83void SkGroupShape::flatten(SkFlattenableWriteBuffer& buffer) {
84 this->INHERITED::flatten(buffer);
85
86 int count = fList.count();
87 buffer.write32(count);
reed@android.comf48f2812009-05-18 04:20:55 +000088 const Rec* rec = fList.begin();
89 const Rec* stop = fList.end();
90 while (rec < stop) {
91 SkShape* shape = rec->fShape;
92 buffer.writeFunctionPtr((void*)shape->getFactory());
93 shape->flatten(buffer);
94 // todo: flatten the matrixref if present
reed@android.comf76bacf2009-05-13 14:00:33 +000095 }
96}
97
98SkGroupShape::SkGroupShape(SkFlattenableReadBuffer& buffer) : INHERITED(buffer){
99 int count = buffer.readS32();
100 for (int i = 0; i < count; i++) {
101 SkFlattenable::Factory fact =
102 (SkFlattenable::Factory)buffer.readFunctionPtr();
103 this->appendShape((SkShape*)fact(buffer))->unref();
reed@android.comf48f2812009-05-18 04:20:55 +0000104 // todo: unflatten the matrixref if present
reed@android.comf76bacf2009-05-13 14:00:33 +0000105 }
106}
107
108SkFlattenable* SkGroupShape::CreateProc(SkFlattenableReadBuffer& buffer) {
109 return SkNEW_ARGS(SkGroupShape, (buffer));
110}
111