blob: f8b8960e625002bf0d513551121386cb73c588c4 [file] [log] [blame]
Jeremy Gebben10691992022-01-06 20:34:57 -07001/* Copyright (c) 2015-2022 The Khronos Group Inc.
2 * Copyright (c) 2015-2022 Valve Corporation
3 * Copyright (c) 2015-2022 LunarG, Inc.
4 * Copyright (C) 2015-2022 Google Inc.
5 * Modifications Copyright (C) 2020 Advanced Micro Devices, Inc. All rights reserved.
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 *
19 * Author: Courtney Goeltzenleuchter <courtneygo@google.com>
20 * Author: Tobin Ehlis <tobine@google.com>
21 * Author: Chris Forbes <chrisf@ijw.co.nz>
22 * Author: Mark Lobodzinski <mark@lunarg.com>
23 * Author: Dave Houlton <daveh@lunarg.com>
24 * Author: John Zulauf <jzulauf@lunarg.com>
25 * Author: Tobias Hector <tobias.hector@amd.com>
26 * Author: Jeremy Gebben <jeremyg@lunarg.com>
27 */
28#include "base_node.h"
29#include "vk_layer_utils.h"
30
31BASE_NODE::~BASE_NODE() { Destroy(); }
32
33void BASE_NODE::Destroy() {
34 Invalidate();
35 destroyed_ = true;
36}
37
38bool BASE_NODE::InUse() const {
Jeremy Gebben610d3a62022-01-01 12:53:17 -070039 // NOTE: for performance reasons, this method calls up the tree
40 // with the read lock held.
41 auto guard = ReadLockTree();
Jeremy Gebben10691992022-01-06 20:34:57 -070042 bool result = false;
Jeremy Gebben610d3a62022-01-01 12:53:17 -070043 for (auto& item : parent_nodes_) {
44 auto node = item.second.lock();
45 if (!node) {
46 continue;
47 }
Jeremy Gebben10691992022-01-06 20:34:57 -070048 result |= node->InUse();
49 if (result) {
50 break;
51 }
52 }
53 return result;
54}
55
56bool BASE_NODE::AddParent(BASE_NODE *parent_node) {
Jeremy Gebben610d3a62022-01-01 12:53:17 -070057 auto guard = WriteLockTree();
58 auto result = parent_nodes_.emplace(parent_node->Handle(), std::weak_ptr<BASE_NODE>(parent_node->shared_from_this()));
Jeremy Gebben10691992022-01-06 20:34:57 -070059 return result.second;
60}
61
62void BASE_NODE::RemoveParent(BASE_NODE *parent_node) {
Jeremy Gebben610d3a62022-01-01 12:53:17 -070063 assert(parent_node);
64 auto guard = WriteLockTree();
65 parent_nodes_.erase(parent_node->Handle());
66}
67
68// copy the current set of parents so that we don't need to hold the lock
69// while calling NotifyInvalidate on them, as that would lead to recursive locking.
70BASE_NODE::NodeMap BASE_NODE::GetParentsForInvalidate(bool unlink) {
71 NodeMap result;
72 if (unlink) {
73 auto guard = WriteLockTree();
74 result = std::move(parent_nodes_);
75 parent_nodes_.clear();
76 } else {
77 auto guard = ReadLockTree();
78 result = parent_nodes_;
79 }
80 return result;
81}
82
83BASE_NODE::NodeMap BASE_NODE::ObjectBindings() const {
84 auto guard = ReadLockTree();
85 return parent_nodes_;
Jeremy Gebben10691992022-01-06 20:34:57 -070086}
87
88void BASE_NODE::Invalidate(bool unlink) {
Jeremy Gebben610d3a62022-01-01 12:53:17 -070089 NodeList empty;
90 // We do not want to call the virtual method here because any special handling
91 // in an overriden NotifyInvalidate() is for when a child node has become invalid.
92 // But calling Invalidate() indicates the current node is invalid.
93 // Calling the default implementation directly here avoids duplicating it inline.
94 BASE_NODE::NotifyInvalidate(empty, unlink);
Jeremy Gebben10691992022-01-06 20:34:57 -070095}
96
97void BASE_NODE::NotifyInvalidate(const NodeList& invalid_nodes, bool unlink) {
Jeremy Gebben610d3a62022-01-01 12:53:17 -070098 auto current_parents = GetParentsForInvalidate(unlink);
99 if (current_parents.size() == 0) {
Jeremy Gebben10691992022-01-06 20:34:57 -0700100 return;
101 }
Jeremy Gebben610d3a62022-01-01 12:53:17 -0700102
Jeremy Gebben10691992022-01-06 20:34:57 -0700103 NodeList up_nodes = invalid_nodes;
Jeremy Gebben610d3a62022-01-01 12:53:17 -0700104 up_nodes.emplace_back(shared_from_this());
105 for (auto& item : current_parents) {
106 auto node = item.second.lock();
107 if (node && !node->Destroyed()) {
108 node->NotifyInvalidate(up_nodes, unlink);
109 }
Jeremy Gebben10691992022-01-06 20:34:57 -0700110 }
111}