blob: 224d2a2eca9efd3b211c1e68344c38448f16f459 [file] [log] [blame]
epoger@google.comec3ed6a2011-07-28 14:26:00 +00001
2/*
3 * Copyright 2011 Google Inc.
4 *
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
7 */
reed@android.comf76bacf2009-05-13 14:00:33 +00008#include "SkGroupShape.h"
djsollen@google.comc73dd5c2012-08-07 15:54:32 +00009#include "SkFlattenableBuffers.h"
reed@android.comf76bacf2009-05-13 14:00:33 +000010
11SkGroupShape::SkGroupShape() {}
12
13SkGroupShape::~SkGroupShape() {
14 this->removeAllShapes();
15}
16
17int SkGroupShape::countShapes() const {
18 return fList.count();
19}
20
reed@android.comf48f2812009-05-18 04:20:55 +000021SkShape* SkGroupShape::getShape(int index, SkMatrixRef** mr) const {
reed@android.comf76bacf2009-05-13 14:00:33 +000022 if ((unsigned)index < (unsigned)fList.count()) {
reed@android.comf48f2812009-05-18 04:20:55 +000023 const Rec& rec = fList[index];
24 if (mr) {
25 *mr = rec.fMatrixRef;
26 }
27 return rec.fShape;
reed@android.comf76bacf2009-05-13 14:00:33 +000028 }
29 return NULL;
30}
31
reed@android.comf48f2812009-05-18 04:20:55 +000032void SkGroupShape::addShape(int index, SkShape* shape, SkMatrixRef* mr) {
reed@android.comf76bacf2009-05-13 14:00:33 +000033 int count = fList.count();
34 if (NULL == shape || index < 0 || index > count) {
reed@android.comf48f2812009-05-18 04:20:55 +000035 return;
reed@android.comf76bacf2009-05-13 14:00:33 +000036 }
37
38 shape->ref();
reed@android.comf48f2812009-05-18 04:20:55 +000039 SkMatrixRef::SafeRef(mr);
40
41 Rec* rec;
reed@android.comf76bacf2009-05-13 14:00:33 +000042 if (index == count) {
reed@android.comf48f2812009-05-18 04:20:55 +000043 rec = fList.append();
reed@android.comf76bacf2009-05-13 14:00:33 +000044 } else {
reed@android.comf48f2812009-05-18 04:20:55 +000045 rec = fList.insert(index);
reed@android.comf76bacf2009-05-13 14:00:33 +000046 }
reed@android.comf48f2812009-05-18 04:20:55 +000047 rec->fShape = shape;
48 rec->fMatrixRef = mr;
reed@android.comf76bacf2009-05-13 14:00:33 +000049}
50
51void SkGroupShape::removeShape(int index) {
52 if ((unsigned)index < (unsigned)fList.count()) {
reed@android.comf48f2812009-05-18 04:20:55 +000053 Rec& rec = fList[index];
54 rec.fShape->unref();
55 SkMatrixRef::SafeUnref(rec.fMatrixRef);
reed@android.comf76bacf2009-05-13 14:00:33 +000056 fList.remove(index);
57 }
58}
59
60void SkGroupShape::removeAllShapes() {
reed@android.comf48f2812009-05-18 04:20:55 +000061 Rec* rec = fList.begin();
62 Rec* stop = fList.end();
63 while (rec < stop) {
64 rec->fShape->unref();
65 SkMatrixRef::SafeUnref(rec->fMatrixRef);
66 rec++;
67 }
reed@android.comf76bacf2009-05-13 14:00:33 +000068 fList.reset();
69}
70
71///////////////////////////////////////////////////////////////////////////////
72
73void SkGroupShape::onDraw(SkCanvas* canvas) {
reed@android.comf48f2812009-05-18 04:20:55 +000074 const Rec* rec = fList.begin();
75 const Rec* stop = fList.end();
76 while (rec < stop) {
77 SkShape* shape = rec->fShape;
78 if (rec->fMatrixRef) {
79 shape->drawMatrix(canvas, *rec->fMatrixRef);
80 } else {
81 shape->draw(canvas);
82 }
83 rec++;
reed@android.comf76bacf2009-05-13 14:00:33 +000084 }
85}
86
djsollen@google.com54924242012-03-29 15:18:04 +000087void SkGroupShape::flatten(SkFlattenableWriteBuffer& buffer) const {
reed@android.comf76bacf2009-05-13 14:00:33 +000088 this->INHERITED::flatten(buffer);
89
djsollen@google.comc73dd5c2012-08-07 15:54:32 +000090 buffer.writeInt(fList.count());
reed@android.comf48f2812009-05-18 04:20:55 +000091 const Rec* rec = fList.begin();
92 const Rec* stop = fList.end();
93 while (rec < stop) {
reed@android.com0ad336f2009-06-29 16:02:20 +000094 buffer.writeFlattenable(rec->fShape);
robertphillips@google.comd4144062012-05-31 15:29:44 +000095 buffer.writeBool(NULL != rec->fMatrixRef);
reed@android.com0ad336f2009-06-29 16:02:20 +000096 if (rec->fMatrixRef) {
djsollen@google.comd2700ee2012-05-30 16:54:13 +000097 buffer.writeMatrix(*rec->fMatrixRef);
reed@android.com0ad336f2009-06-29 16:02:20 +000098 }
99 rec += 1;
reed@android.comf76bacf2009-05-13 14:00:33 +0000100 }
101}
102
103SkGroupShape::SkGroupShape(SkFlattenableReadBuffer& buffer) : INHERITED(buffer){
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000104 int count = buffer.readInt();
reed@android.comf76bacf2009-05-13 14:00:33 +0000105 for (int i = 0; i < count; i++) {
djsollen@google.comc73dd5c2012-08-07 15:54:32 +0000106 SkShape* shape = buffer.readFlattenableT<SkShape>();
reed@android.com0ad336f2009-06-29 16:02:20 +0000107 SkMatrixRef* mr = NULL;
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000108 bool hasMatrix = buffer.readBool();
109 if (hasMatrix) {
reed@android.com0ad336f2009-06-29 16:02:20 +0000110 mr = SkNEW(SkMatrixRef);
djsollen@google.comd2700ee2012-05-30 16:54:13 +0000111 buffer.readMatrix(mr);
reed@android.com0ad336f2009-06-29 16:02:20 +0000112 }
113 if (shape) {
114 this->appendShape(shape, mr)->unref();
115 }
116 SkSafeUnref(mr);
reed@android.comf76bacf2009-05-13 14:00:33 +0000117 }
118}
119
caryclark@google.comd26147a2011-12-15 14:16:43 +0000120SK_DEFINE_FLATTENABLE_REGISTRAR(SkGroupShape)
reed@android.com0ad336f2009-06-29 16:02:20 +0000121