Flutter Engine
The Flutter Engine
SkSGGroup.cpp
Go to the documentation of this file.
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
9
14
15#include <algorithm>
16
17class SkMatrix;
18struct SkPoint;
19
20namespace sksg {
21class InvalidationController;
22
23Group::Group() = default;
24
25Group::Group(std::vector<sk_sp<RenderNode>> children)
26 : fChildren(std::move(children)) {
27 for (const auto& child : fChildren) {
28 this->observeInval(child);
29 }
30}
31
33 for (const auto& child : fChildren) {
34 this->unobserveInval(child);
35 }
36}
37
39 for (const auto& child : fChildren) {
40 this->unobserveInval(child);
41 }
42 fChildren.clear();
43}
44
46 // should we allow duplicates?
47 for (const auto& child : fChildren) {
48 if (child == node) {
49 return;
50 }
51 }
52
53 this->observeInval(node);
54 fChildren.push_back(std::move(node));
55
56 this->invalidate();
57}
58
60 SkDEBUGCODE(const auto origSize = fChildren.size());
61 fChildren.erase(std::remove(fChildren.begin(), fChildren.end(), node), fChildren.end());
62 SkASSERT(fChildren.size() == origSize - 1);
63
64 this->unobserveInval(node);
65 this->invalidate();
66}
67
68void Group::onRender(SkCanvas* canvas, const RenderContext* ctx) const {
69 const auto local_ctx = ScopedRenderContext(canvas, ctx).setIsolation(this->bounds(),
70 canvas->getTotalMatrix(),
71 fRequiresIsolation);
72
73 for (const auto& child : fChildren) {
74 child->render(canvas, local_ctx);
75 }
76}
77
78const RenderNode* Group::onNodeAt(const SkPoint& p) const {
79 for (auto it = fChildren.crbegin(); it != fChildren.crend(); ++it) {
80 if (const auto* node = (*it)->nodeAt(p)) {
81 return node;
82 }
83 }
84
85 return nullptr;
86}
87
89 SkASSERT(this->hasInval());
90
92 fRequiresIsolation = false;
93
94 for (size_t i = 0; i < fChildren.size(); ++i) {
95 const auto child_bounds = fChildren[i]->revalidate(ic, ctm);
96
97 // If any of the child nodes overlap, group effects require layer isolation.
98 if (!fRequiresIsolation && i > 0 && child_bounds.intersects(bounds)) {
99#if 1
100 // Testing conservatively against the union of prev bounds is cheap and good enough.
101 fRequiresIsolation = true;
102#else
103 // Testing exhaustively doesn't seem to increase the layer elision rate in practice.
104 for (size_t j = 0; j < i; ++ j) {
105 if (child_bounds.intersects(fChildren[i]->bounds())) {
106 fRequiresIsolation = true;
107 break;
108 }
109 }
110#endif
111 }
112
113 bounds.join(child_bounds);
114 }
115
116 return bounds;
117}
118
119} // namespace sksg
#define SkASSERT(cond)
Definition: SkAssert.h:116
SkDEBUGCODE(SK_SPI) SkThreadID SkGetThreadID()
SkMatrix getTotalMatrix() const
Definition: SkCanvas.cpp:1629
SkRect onRevalidate(InvalidationController *, const SkMatrix &) override
Definition: SkSGGroup.cpp:88
void removeChild(const sk_sp< RenderNode > &)
Definition: SkSGGroup.cpp:59
void onRender(SkCanvas *, const RenderContext *) const override
Definition: SkSGGroup.cpp:68
~Group() override
Definition: SkSGGroup.cpp:32
void addChild(sk_sp< RenderNode >)
Definition: SkSGGroup.cpp:45
const RenderNode * onNodeAt(const SkPoint &) const override
Definition: SkSGGroup.cpp:78
void clear()
Definition: SkSGGroup.cpp:38
void observeInval(const sk_sp< Node > &)
Definition: SkSGNode.cpp:61
void unobserveInval(const sk_sp< Node > &)
Definition: SkSGNode.cpp:84
const SkRect & bounds() const
Definition: SkSGNode.h:55
bool hasInval() const
Definition: SkSGNode.h:60
void invalidate(bool damage=true)
Definition: SkSGNode.cpp:113
ScopedRenderContext && setIsolation(const SkRect &bounds, const SkMatrix &ctm, bool do_isolate)
def remove(*paths)
Definition: Skottie.h:32
Definition: ref_ptr.h:256
static constexpr SkRect MakeEmpty()
Definition: SkRect.h:595
void join(const SkRect &r)
Definition: SkRect.cpp:126