Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 1 | /* |
| 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 | |
Florin Malita | ca858b6 | 2018-09-02 13:44:13 -0400 | [diff] [blame^] | 10 | #include <algorithm> |
| 11 | |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 12 | namespace sksg { |
| 13 | |
| 14 | Group::Group() {} |
| 15 | |
| 16 | Group::~Group() { |
| 17 | for (const auto& child : fChildren) { |
Florin Malita | 3ba3fa7 | 2018-01-22 10:19:28 -0500 | [diff] [blame] | 18 | this->unobserveInval(child); |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 19 | } |
| 20 | } |
| 21 | |
| 22 | void Group::addChild(sk_sp<RenderNode> node) { |
| 23 | // should we allow duplicates? |
| 24 | for (const auto& child : fChildren) { |
| 25 | if (child == node) { |
| 26 | return; |
| 27 | } |
| 28 | } |
| 29 | |
Florin Malita | 3ba3fa7 | 2018-01-22 10:19:28 -0500 | [diff] [blame] | 30 | this->observeInval(node); |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 31 | fChildren.push_back(std::move(node)); |
Florin Malita | c75e240 | 2018-01-03 16:17:29 -0500 | [diff] [blame] | 32 | |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame] | 33 | this->invalidate(); |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | void Group::removeChild(const sk_sp<RenderNode>& node) { |
Florin Malita | ca858b6 | 2018-09-02 13:44:13 -0400 | [diff] [blame^] | 37 | SkDEBUGCODE(const auto origSize = fChildren.size()); |
| 38 | fChildren.erase(std::remove(fChildren.begin(), fChildren.end(), node), fChildren.end()); |
| 39 | SkASSERT(fChildren.size() == origSize - 1); |
Florin Malita | c75e240 | 2018-01-03 16:17:29 -0500 | [diff] [blame] | 40 | |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame] | 41 | this->invalidate(); |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 42 | } |
| 43 | |
Florin Malita | ca858b6 | 2018-09-02 13:44:13 -0400 | [diff] [blame^] | 44 | void Group::shrink_to_fit() { |
| 45 | fChildren.shrink_to_fit(); |
| 46 | } |
| 47 | |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 48 | void 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 |
Florin Malita | ca858b6 | 2018-09-02 13:44:13 -0400 | [diff] [blame^] | 52 | const auto isolate = fChildren.size() > 1; |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 53 | const auto local_ctx = ScopedRenderContext(canvas, ctx).setIsolation(this->bounds(), isolate); |
| 54 | |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 55 | for (const auto& child : fChildren) { |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 56 | child->render(canvas, local_ctx); |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 57 | } |
| 58 | } |
| 59 | |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame] | 60 | SkRect Group::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) { |
Florin Malita | c75e240 | 2018-01-03 16:17:29 -0500 | [diff] [blame] | 61 | SkASSERT(this->hasInval()); |
| 62 | |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame] | 63 | SkRect bounds = SkRect::MakeEmpty(); |
Florin Malita | c75e240 | 2018-01-03 16:17:29 -0500 | [diff] [blame] | 64 | |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 65 | for (const auto& child : fChildren) { |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame] | 66 | bounds.join(child->revalidate(ic, ctm)); |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 67 | } |
Florin Malita | c75e240 | 2018-01-03 16:17:29 -0500 | [diff] [blame] | 68 | |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame] | 69 | return bounds; |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | } // namespace sksg |