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 | |
Florin Malita | cd9d074 | 2018-09-10 09:57:15 -0400 | [diff] [blame] | 14 | Group::Group(std::vector<sk_sp<RenderNode>> children) |
| 15 | : fChildren(std::move(children)) { |
| 16 | for (const auto& child : fChildren) { |
| 17 | this->observeInval(child); |
| 18 | } |
| 19 | } |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 20 | |
| 21 | Group::~Group() { |
| 22 | for (const auto& child : fChildren) { |
Florin Malita | 3ba3fa7 | 2018-01-22 10:19:28 -0500 | [diff] [blame] | 23 | this->unobserveInval(child); |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 24 | } |
| 25 | } |
| 26 | |
Florin Malita | 8ec9a60 | 2019-02-01 17:59:25 -0500 | [diff] [blame] | 27 | void Group::clear() { |
| 28 | for (const auto& child : fChildren) { |
| 29 | this->unobserveInval(child); |
| 30 | } |
| 31 | fChildren.clear(); |
| 32 | } |
| 33 | |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 34 | void Group::addChild(sk_sp<RenderNode> node) { |
| 35 | // should we allow duplicates? |
| 36 | for (const auto& child : fChildren) { |
| 37 | if (child == node) { |
| 38 | return; |
| 39 | } |
| 40 | } |
| 41 | |
Florin Malita | 3ba3fa7 | 2018-01-22 10:19:28 -0500 | [diff] [blame] | 42 | this->observeInval(node); |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 43 | fChildren.push_back(std::move(node)); |
Florin Malita | c75e240 | 2018-01-03 16:17:29 -0500 | [diff] [blame] | 44 | |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame] | 45 | this->invalidate(); |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | void Group::removeChild(const sk_sp<RenderNode>& node) { |
Florin Malita | ca858b6 | 2018-09-02 13:44:13 -0400 | [diff] [blame] | 49 | SkDEBUGCODE(const auto origSize = fChildren.size()); |
| 50 | fChildren.erase(std::remove(fChildren.begin(), fChildren.end(), node), fChildren.end()); |
| 51 | SkASSERT(fChildren.size() == origSize - 1); |
Florin Malita | c75e240 | 2018-01-03 16:17:29 -0500 | [diff] [blame] | 52 | |
Florin Malita | bee88d4 | 2018-10-02 13:05:39 -0400 | [diff] [blame] | 53 | this->unobserveInval(node); |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame] | 54 | this->invalidate(); |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 55 | } |
| 56 | |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 57 | void Group::onRender(SkCanvas* canvas, const RenderContext* ctx) const { |
| 58 | // TODO: this heuristic works at the moment, but: |
| 59 | // a) it is fragile because it relies on all leaf render nodes being atomic draws |
| 60 | // 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] | 61 | const auto isolate = fChildren.size() > 1; |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 62 | const auto local_ctx = ScopedRenderContext(canvas, ctx).setIsolation(this->bounds(), isolate); |
| 63 | |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 64 | for (const auto& child : fChildren) { |
Florin Malita | c0132ff | 2018-08-09 07:40:01 -0400 | [diff] [blame] | 65 | child->render(canvas, local_ctx); |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 66 | } |
| 67 | } |
| 68 | |
Florin Malita | eb46bd8 | 2019-02-12 09:33:21 -0500 | [diff] [blame] | 69 | const RenderNode* Group::onNodeAt(const SkPoint& p) const { |
| 70 | for (const auto& child : fChildren) { |
| 71 | if (const auto* node = child->nodeAt(p)) { |
| 72 | return node; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | return nullptr; |
| 77 | } |
| 78 | |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame] | 79 | SkRect Group::onRevalidate(InvalidationController* ic, const SkMatrix& ctm) { |
Florin Malita | c75e240 | 2018-01-03 16:17:29 -0500 | [diff] [blame] | 80 | SkASSERT(this->hasInval()); |
| 81 | |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame] | 82 | SkRect bounds = SkRect::MakeEmpty(); |
Florin Malita | c75e240 | 2018-01-03 16:17:29 -0500 | [diff] [blame] | 83 | |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 84 | for (const auto& child : fChildren) { |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame] | 85 | bounds.join(child->revalidate(ic, ctm)); |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 86 | } |
Florin Malita | c75e240 | 2018-01-03 16:17:29 -0500 | [diff] [blame] | 87 | |
Florin Malita | c14f144 | 2018-01-05 11:32:31 -0500 | [diff] [blame] | 88 | return bounds; |
Florin Malita | 4aa4441 | 2017-12-19 12:21:02 -0500 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | } // namespace sksg |