aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/effects/SkGroupShape.cpp
blob: b8edc82939179498a003aaf8564e14d821082848 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include "SkGroupShape.h"

SkGroupShape::SkGroupShape() {}

SkGroupShape::~SkGroupShape() {
    this->removeAllShapes();
}

int SkGroupShape::countShapes() const {
    return fList.count();
}

SkShape* SkGroupShape::getShape(int index, SkMatrixRef** mr) const {
    if ((unsigned)index < (unsigned)fList.count()) {
        const Rec& rec = fList[index];
        if (mr) {
            *mr = rec.fMatrixRef;
        }
        return rec.fShape;
    }
    return NULL;
}

void SkGroupShape::addShape(int index, SkShape* shape, SkMatrixRef* mr) {
    int count = fList.count();
    if (NULL == shape || index < 0 || index > count) {
        return;
    }

    shape->ref();
    SkMatrixRef::SafeRef(mr);

    Rec* rec;
    if (index == count) {
        rec = fList.append();
    } else {
        rec = fList.insert(index);
    }
    rec->fShape = shape;
    rec->fMatrixRef = mr;
}

void SkGroupShape::removeShape(int index) {
    if ((unsigned)index < (unsigned)fList.count()) {
        Rec& rec = fList[index];
        rec.fShape->unref();
        SkMatrixRef::SafeUnref(rec.fMatrixRef);
        fList.remove(index);
    }
}

void SkGroupShape::removeAllShapes() {
    Rec* rec = fList.begin();
    Rec* stop = fList.end();
    while (rec < stop) {
        rec->fShape->unref();
        SkMatrixRef::SafeUnref(rec->fMatrixRef);
        rec++;
    }
    fList.reset();
}

///////////////////////////////////////////////////////////////////////////////

void SkGroupShape::onDraw(SkCanvas* canvas) {
    const Rec* rec = fList.begin();
    const Rec* stop = fList.end();
    while (rec < stop) {
        SkShape* shape = rec->fShape;
        if (rec->fMatrixRef) {
            shape->drawMatrix(canvas, *rec->fMatrixRef);
        } else {
            shape->draw(canvas);
        }
        rec++;
    }
}

SkFlattenable::Factory SkGroupShape::getFactory() {
    return CreateProc;
}

void SkGroupShape::flatten(SkFlattenableWriteBuffer& buffer) {
    this->INHERITED::flatten(buffer);

    int count = fList.count();
    buffer.write32(count);
    const Rec* rec = fList.begin();
    const Rec* stop = fList.end();
    while (rec < stop) {
        buffer.writeFlattenable(rec->fShape);
        if (rec->fMatrixRef) {
            char storage[SkMatrix::kMaxFlattenSize];
            uint32_t size = rec->fMatrixRef->flatten(storage);
            buffer.write32(size);
            buffer.writePad(storage, size);
        } else {
            buffer.write32(0);
        }
        rec += 1;
    }
}

SkGroupShape::SkGroupShape(SkFlattenableReadBuffer& buffer) : INHERITED(buffer){
    int count = buffer.readS32();
    for (int i = 0; i < count; i++) {
        SkShape* shape = reinterpret_cast<SkShape*>(buffer.readFlattenable());
        SkMatrixRef* mr = NULL;
        uint32_t size = buffer.readS32();
        if (size) {
            char storage[SkMatrix::kMaxFlattenSize];
            buffer.read(storage, SkAlign4(size));
            mr = SkNEW(SkMatrixRef);
            mr->unflatten(storage);
        }
        if (shape) {
            this->appendShape(shape, mr)->unref();
        }
        SkSafeUnref(mr);
    }
}

SkFlattenable* SkGroupShape::CreateProc(SkFlattenableReadBuffer& buffer) {
    return SkNEW_ARGS(SkGroupShape, (buffer));
}

static SkFlattenable::Registrar gReg("SkGroupShape", SkGroupShape::CreateProc);