Cleanup: remove redundant doxygen \file argument
[blender.git] / source / blender / depsgraph / intern / node / deg_node_id.cc
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2013 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file \ingroup depsgraph
21  */
22
23 #include "intern/node/deg_node_id.h"
24
25 #include <stdio.h>
26 #include <cstring>  /* required for STREQ later on. */
27
28 #include "BLI_utildefines.h"
29 #include "BLI_ghash.h"
30 #include "BLI_string.h"
31
32 extern "C" {
33 #include "DNA_ID.h"
34 #include "DNA_anim_types.h"
35
36 #include "BKE_animsys.h"
37 #include "BKE_library.h"
38 }
39
40 #include "DEG_depsgraph.h"
41
42 #include "intern/eval/deg_eval_copy_on_write.h"
43 #include "intern/node/deg_node_factory.h"
44 #include "intern/node/deg_node_component.h"
45 #include "intern/node/deg_node_time.h"
46
47 namespace DEG {
48
49 const char *linkedStateAsString(eDepsNode_LinkedState_Type linked_state)
50 {
51         switch (linked_state) {
52                 case DEG_ID_LINKED_INDIRECTLY: return "INDIRECTLY";
53                 case DEG_ID_LINKED_VIA_SET: return "VIA_SET";
54                 case DEG_ID_LINKED_DIRECTLY: return "DIRECTLY";
55         }
56         BLI_assert(!"Unhandled linked state, should never happen.");
57         return "UNKNOW";
58 }
59
60 IDNode::ComponentIDKey::ComponentIDKey(NodeType type,
61                                            const char *name)
62         : type(type), name(name)
63 {
64 }
65
66 bool IDNode::ComponentIDKey::operator== (const ComponentIDKey &other) const
67 {
68         return type == other.type &&
69                STREQ(name, other.name);
70 }
71
72 static unsigned int id_deps_node_hash_key(const void *key_v)
73 {
74         const IDNode::ComponentIDKey *key =
75                 reinterpret_cast<const IDNode::ComponentIDKey *>(key_v);
76         const int type_as_int = static_cast<int>(key->type);
77         return BLI_ghashutil_combine_hash(BLI_ghashutil_uinthash(type_as_int),
78                                           BLI_ghashutil_strhash_p(key->name));
79 }
80
81 static bool id_deps_node_hash_key_cmp(const void *a, const void *b)
82 {
83         const IDNode::ComponentIDKey *key_a =
84                 reinterpret_cast<const IDNode::ComponentIDKey *>(a);
85         const IDNode::ComponentIDKey *key_b =
86                 reinterpret_cast<const IDNode::ComponentIDKey *>(b);
87         return !(*key_a == *key_b);
88 }
89
90 static void id_deps_node_hash_key_free(void *key_v)
91 {
92         typedef IDNode::ComponentIDKey ComponentIDKey;
93         ComponentIDKey *key = reinterpret_cast<ComponentIDKey *>(key_v);
94         OBJECT_GUARDED_DELETE(key, ComponentIDKey);
95 }
96
97 static void id_deps_node_hash_value_free(void *value_v)
98 {
99         ComponentNode *comp_node = reinterpret_cast<ComponentNode *>(value_v);
100         OBJECT_GUARDED_DELETE(comp_node, ComponentNode);
101 }
102
103 /* Initialize 'id' node - from pointer data given. */
104 void IDNode::init(const ID *id, const char *UNUSED(subdata))
105 {
106         BLI_assert(id != NULL);
107         /* Store ID-pointer. */
108         id_orig = (ID *)id;
109         eval_flags = 0;
110         previous_eval_flags = 0;
111         customdata_mask = 0;
112         previous_customdata_mask = 0;
113         linked_state = DEG_ID_LINKED_INDIRECTLY;
114         is_directly_visible = true;
115         is_collection_fully_expanded = false;
116
117         visible_components_mask = 0;
118         previously_visible_components_mask = 0;
119
120         components = BLI_ghash_new(id_deps_node_hash_key,
121                                    id_deps_node_hash_key_cmp,
122                                    "Depsgraph id components hash");
123 }
124
125 void IDNode::init_copy_on_write(ID *id_cow_hint)
126 {
127         /* Create pointer as early as possible, so we can use it for function
128          * bindings. Rest of data we'll be copying to the new datablock when
129          * it is actually needed. */
130         if (id_cow_hint != NULL) {
131                 // BLI_assert(deg_copy_on_write_is_needed(id_orig));
132                 if (deg_copy_on_write_is_needed(id_orig)) {
133                         id_cow = id_cow_hint;
134                 }
135                 else {
136                         id_cow = id_orig;
137                 }
138         }
139         else if (deg_copy_on_write_is_needed(id_orig)) {
140                 id_cow = (ID *)BKE_libblock_alloc_notest(GS(id_orig->name));
141                 DEG_COW_PRINT("Create shallow copy for %s: id_orig=%p id_cow=%p\n",
142                               id_orig->name, id_orig, id_cow);
143                 deg_tag_copy_on_write_id(id_cow, id_orig);
144         }
145         else {
146                 id_cow = id_orig;
147         }
148 }
149
150 /* Free 'id' node. */
151 IDNode::~IDNode()
152 {
153         destroy();
154 }
155
156 void IDNode::destroy()
157 {
158         if (id_orig == NULL) {
159                 return;
160         }
161
162         BLI_ghash_free(components,
163                        id_deps_node_hash_key_free,
164                        id_deps_node_hash_value_free);
165
166         /* Free memory used by this CoW ID. */
167         if (id_cow != id_orig && id_cow != NULL) {
168                 deg_free_copy_on_write_datablock(id_cow);
169                 MEM_freeN(id_cow);
170                 id_cow = NULL;
171                 DEG_COW_PRINT("Destroy CoW for %s: id_orig=%p id_cow=%p\n",
172                               id_orig->name, id_orig, id_cow);
173         }
174
175         /* Tag that the node is freed. */
176         id_orig = NULL;
177 }
178
179 string IDNode::identifier() const
180 {
181         char orig_ptr[24], cow_ptr[24];
182         BLI_snprintf(orig_ptr, sizeof(orig_ptr), "%p", id_orig);
183         BLI_snprintf(cow_ptr, sizeof(cow_ptr), "%p", id_cow);
184         return string(nodeTypeAsString(type)) + " : " + name +
185                 " (orig: " + orig_ptr + ", eval: " + cow_ptr +
186                 ", is_directly_visible " + (is_directly_visible ? "true"
187                                                                 : "false") + ")";
188 }
189
190 ComponentNode *IDNode::find_component(NodeType type,
191                                               const char *name) const
192 {
193         ComponentIDKey key(type, name);
194         return reinterpret_cast<ComponentNode *>(BLI_ghash_lookup(components, &key));
195 }
196
197 ComponentNode *IDNode::add_component(NodeType type,
198                                              const char *name)
199 {
200         ComponentNode *comp_node = find_component(type, name);
201         if (!comp_node) {
202                 DepsNodeFactory *factory = type_get_factory(type);
203                 comp_node = (ComponentNode *)factory->create_node(this->id_orig, "", name);
204
205                 /* Register. */
206                 ComponentIDKey *key = OBJECT_GUARDED_NEW(ComponentIDKey, type, name);
207                 BLI_ghash_insert(components, key, comp_node);
208                 comp_node->owner = this;
209         }
210         return comp_node;
211 }
212
213 void IDNode::tag_update(Depsgraph *graph, eUpdateSource source)
214 {
215         GHASH_FOREACH_BEGIN(ComponentNode *, comp_node, components)
216         {
217                 comp_node->tag_update(graph, source);
218         }
219         GHASH_FOREACH_END();
220 }
221
222 void IDNode::finalize_build(Depsgraph *graph)
223 {
224         /* Finalize build of all components. */
225         GHASH_FOREACH_BEGIN(ComponentNode *, comp_node, components)
226         {
227                 comp_node->finalize_build(graph);
228         }
229         GHASH_FOREACH_END();
230         visible_components_mask = get_visible_components_mask();
231 }
232
233 IDComponentsMask IDNode::get_visible_components_mask() const {
234         IDComponentsMask result = 0;
235         GHASH_FOREACH_BEGIN(ComponentNode *, comp_node, components)
236         {
237                 if (comp_node->affects_directly_visible) {
238                         const int component_type_as_int = static_cast<int>(comp_node->type);
239                         BLI_assert(component_type_as_int < 64);
240                         result |= (1ULL << component_type_as_int);
241                 }
242         }
243         GHASH_FOREACH_END();
244         return result;
245 }
246
247 }  // namespace DEG