Jeremy Gebben | 1069199 | 2022-01-06 20:34:57 -0700 | [diff] [blame] | 1 | /* 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 | |
| 31 | BASE_NODE::~BASE_NODE() { Destroy(); } |
| 32 | |
| 33 | void BASE_NODE::Destroy() { |
| 34 | Invalidate(); |
| 35 | destroyed_ = true; |
| 36 | } |
| 37 | |
| 38 | bool BASE_NODE::InUse() const { |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 39 | // NOTE: for performance reasons, this method calls up the tree |
| 40 | // with the read lock held. |
| 41 | auto guard = ReadLockTree(); |
Jeremy Gebben | 1069199 | 2022-01-06 20:34:57 -0700 | [diff] [blame] | 42 | bool result = false; |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 43 | for (auto& item : parent_nodes_) { |
| 44 | auto node = item.second.lock(); |
| 45 | if (!node) { |
| 46 | continue; |
| 47 | } |
Jeremy Gebben | 1069199 | 2022-01-06 20:34:57 -0700 | [diff] [blame] | 48 | result |= node->InUse(); |
| 49 | if (result) { |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | return result; |
| 54 | } |
| 55 | |
| 56 | bool BASE_NODE::AddParent(BASE_NODE *parent_node) { |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 57 | auto guard = WriteLockTree(); |
| 58 | auto result = parent_nodes_.emplace(parent_node->Handle(), std::weak_ptr<BASE_NODE>(parent_node->shared_from_this())); |
Jeremy Gebben | 1069199 | 2022-01-06 20:34:57 -0700 | [diff] [blame] | 59 | return result.second; |
| 60 | } |
| 61 | |
| 62 | void BASE_NODE::RemoveParent(BASE_NODE *parent_node) { |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 63 | 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. |
| 70 | BASE_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 | |
| 83 | BASE_NODE::NodeMap BASE_NODE::ObjectBindings() const { |
| 84 | auto guard = ReadLockTree(); |
| 85 | return parent_nodes_; |
Jeremy Gebben | 1069199 | 2022-01-06 20:34:57 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | void BASE_NODE::Invalidate(bool unlink) { |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 89 | 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 Gebben | 1069199 | 2022-01-06 20:34:57 -0700 | [diff] [blame] | 95 | } |
| 96 | |
| 97 | void BASE_NODE::NotifyInvalidate(const NodeList& invalid_nodes, bool unlink) { |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 98 | auto current_parents = GetParentsForInvalidate(unlink); |
| 99 | if (current_parents.size() == 0) { |
Jeremy Gebben | 1069199 | 2022-01-06 20:34:57 -0700 | [diff] [blame] | 100 | return; |
| 101 | } |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 102 | |
Jeremy Gebben | 1069199 | 2022-01-06 20:34:57 -0700 | [diff] [blame] | 103 | NodeList up_nodes = invalid_nodes; |
Jeremy Gebben | 610d3a6 | 2022-01-01 12:53:17 -0700 | [diff] [blame] | 104 | 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 Gebben | 1069199 | 2022-01-06 20:34:57 -0700 | [diff] [blame] | 110 | } |
| 111 | } |