Blender V5.0
depsgraph_registry.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2019 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
8
9#include "BLI_map.hh"
10#include "BLI_mutex.hh"
11#include "BLI_vector_set.hh"
12
14
15#include "intern/depsgraph.hh"
16
17namespace blender::deg {
18
19/* Global registry for dependency graphs associated with a main database.
20 *
21 * Threads may add or remove depsgraphs for different mains concurrently
22 * (for example for preview rendering), but not the same main. */
23
24/* Use pointer for map value to ensure span returned by get_all_registered_graphs
25 * remains unchanged as other mains are added or removed. */
26using GraphSetPtr = std::unique_ptr<VectorSet<Depsgraph *>>;
31
33{
34 static GraphRegistry graph_registry;
35 return graph_registry;
36}
37
39{
40 GraphRegistry &graph_registry = get_graph_registry();
41 Main *bmain = depsgraph->bmain;
42
43 std::lock_guard lock{graph_registry.mutex};
44 graph_registry.map
45 .lookup_or_add_cb(bmain, []() { return std::make_unique<VectorSet<Depsgraph *>>(); })
46 ->add_new(depsgraph);
47}
48
50{
51 Main *bmain = depsgraph->bmain;
52 GraphRegistry &graph_registry = get_graph_registry();
53
54 std::lock_guard lock{graph_registry.mutex};
55 GraphSetPtr &graphs = graph_registry.map.lookup(bmain);
56 graphs->remove(depsgraph);
57
58 /* If this was the last depsgraph associated with the main, remove the main entry as well. */
59 if (graphs->is_empty()) {
60 graph_registry.map.remove(bmain);
61 }
62}
63
65{
66 GraphRegistry &graph_registry = get_graph_registry();
67 std::lock_guard lock{graph_registry.mutex};
68 GraphSetPtr *graphs = graph_registry.map.lookup_ptr(bmain);
69 if (graphs) {
70 return **graphs;
71 }
72 return {};
73}
74
75} // namespace blender::deg
volatile int lock
BPy_StructRNA * depsgraph
const Value * lookup_ptr(const Key &key) const
Definition BLI_map.hh:508
const Value & lookup(const Key &key) const
Definition BLI_map.hh:545
Value & lookup_or_add_cb(const Key &key, const CreateValueF &create_value)
Definition BLI_map.hh:620
bool remove(const Key &key)
Definition BLI_map.hh:368
Span< Depsgraph * > get_all_registered_graphs(Main *bmain)
std::unique_ptr< VectorSet< Depsgraph * > > GraphSetPtr
static GraphRegistry & get_graph_registry()
void unregister_graph(Depsgraph *depsgraph)
void register_graph(Depsgraph *depsgraph)
std::mutex Mutex
Definition BLI_mutex.hh:47
Map< Main *, GraphSetPtr > map