blob: b71837a8bf6451b8be7b4e1b876fdbed4b7b4778 [file] [log] [blame]
Florin Malita4aa44412017-12-19 12:21:02 -05001/*
2 * Copyright 2017 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8#include "SkSGGroup.h"
9
10namespace sksg {
11
12Group::Group() {}
13
14Group::~Group() {
15 for (const auto& child : fChildren) {
16 child->removeInvalReceiver(this);
17 }
18}
19
20void Group::addChild(sk_sp<RenderNode> node) {
21 // should we allow duplicates?
22 for (const auto& child : fChildren) {
23 if (child == node) {
24 return;
25 }
26 }
27
28 node->addInvalReceiver(this);
29 fChildren.push_back(std::move(node));
Florin Malitac75e2402018-01-03 16:17:29 -050030
Florin Malitac14f1442018-01-05 11:32:31 -050031 this->invalidate();
Florin Malita4aa44412017-12-19 12:21:02 -050032}
33
34void Group::removeChild(const sk_sp<RenderNode>& node) {
35 int origCount = fChildren.count();
36 for (int i = 0; i < origCount; ++i) {
37 if (fChildren[i] == node) {
38 fChildren.removeShuffle(i);
39 node->removeInvalReceiver(this);
40 break;
41 }
42 }
43 SkASSERT(fChildren.count() == origCount - 1);
Florin Malitac75e2402018-01-03 16:17:29 -050044
Florin Malitac14f1442018-01-05 11:32:31 -050045 this->invalidate();
Florin Malita4aa44412017-12-19 12:21:02 -050046}
47
48void Group::onRender(SkCanvas* canvas) const {
49 for (const auto& child : fChildren) {
50 child->render(canvas);
51 }
52}
53
Florin Malitac14f1442018-01-05 11:32:31 -050054SkRect Group::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
Florin Malitac75e2402018-01-03 16:17:29 -050055 SkASSERT(this->hasInval());
56
Florin Malitac14f1442018-01-05 11:32:31 -050057 SkRect bounds = SkRect::MakeEmpty();
Florin Malitac75e2402018-01-03 16:17:29 -050058
Florin Malita4aa44412017-12-19 12:21:02 -050059 for (const auto& child : fChildren) {
Florin Malitac14f1442018-01-05 11:32:31 -050060 bounds.join(child->revalidate(ic, ctm));
Florin Malita4aa44412017-12-19 12:21:02 -050061 }
Florin Malitac75e2402018-01-03 16:17:29 -050062
Florin Malitac14f1442018-01-05 11:32:31 -050063 return bounds;
Florin Malita4aa44412017-12-19 12:21:02 -050064}
65
66} // namespace sksg