blob: 9911292ea1a49c2147b1562da79d13116ff9a67b [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) {
Florin Malita3ba3fa72018-01-22 10:19:28 -050016 this->unobserveInval(child);
Florin Malita4aa44412017-12-19 12:21:02 -050017 }
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
Florin Malita3ba3fa72018-01-22 10:19:28 -050028 this->observeInval(node);
Florin Malita4aa44412017-12-19 12:21:02 -050029 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);
Florin Malita3ba3fa72018-01-22 10:19:28 -050039 this->unobserveInval(node);
Florin Malita4aa44412017-12-19 12:21:02 -050040 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
Florin Malitac0132ff2018-08-09 07:40:01 -040048void Group::onRender(SkCanvas* canvas, const RenderContext* ctx) const {
49 // TODO: this heuristic works at the moment, but:
50 // a) it is fragile because it relies on all leaf render nodes being atomic draws
51 // b) could be improved by e.g. detecting all leaf render draws are non-overlapping
52 const auto isolate = fChildren.count() > 1;
53 const auto local_ctx = ScopedRenderContext(canvas, ctx).setIsolation(this->bounds(), isolate);
54
Florin Malita4aa44412017-12-19 12:21:02 -050055 for (const auto& child : fChildren) {
Florin Malitac0132ff2018-08-09 07:40:01 -040056 child->render(canvas, local_ctx);
Florin Malita4aa44412017-12-19 12:21:02 -050057 }
58}
59
Florin Malitac14f1442018-01-05 11:32:31 -050060SkRect Group::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) {
Florin Malitac75e2402018-01-03 16:17:29 -050061 SkASSERT(this->hasInval());
62
Florin Malitac14f1442018-01-05 11:32:31 -050063 SkRect bounds = SkRect::MakeEmpty();
Florin Malitac75e2402018-01-03 16:17:29 -050064
Florin Malita4aa44412017-12-19 12:21:02 -050065 for (const auto& child : fChildren) {
Florin Malitac14f1442018-01-05 11:32:31 -050066 bounds.join(child->revalidate(ic, ctm));
Florin Malita4aa44412017-12-19 12:21:02 -050067 }
Florin Malitac75e2402018-01-03 16:17:29 -050068
Florin Malitac14f1442018-01-05 11:32:31 -050069 return bounds;
Florin Malita4aa44412017-12-19 12:21:02 -050070}
71
72} // namespace sksg