Merge branch 'master' into blender2.8
[blender.git] / source / blender / depsgraph / intern / nodes / deg_node.cc
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2013 Blender Foundation.
19  * All rights reserved.
20  *
21  * Original Author: Joshua Leung
22  * Contributor(s): None Yet
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/depsgraph/intern/nodes/deg_node.cc
28  *  \ingroup depsgraph
29  */
30
31 #include "intern/nodes/deg_node.h"
32
33 #include <stdio.h>
34 #include <cstring>  /* required for STREQ later on. */
35
36 #include "BLI_utildefines.h"
37 #include "BLI_ghash.h"
38
39 extern "C" {
40 #include "DNA_ID.h"
41 #include "DNA_anim_types.h"
42 #include "DNA_object_types.h"
43
44 #include "BKE_animsys.h"
45 #include "BKE_library.h"
46 }
47
48 #include "DEG_depsgraph.h"
49
50 #include "intern/eval/deg_eval_copy_on_write.h"
51 #include "intern/nodes/deg_node_component.h"
52 #include "intern/nodes/deg_node_operation.h"
53 #include "intern/depsgraph_intern.h"
54 #include "util/deg_util_foreach.h"
55 #include "util/deg_util_function.h"
56
57 namespace DEG {
58
59 /* *************** */
60 /* Node Management */
61
62 /* Add ------------------------------------------------ */
63
64 DepsNode::TypeInfo::TypeInfo(eDepsNode_Type type,
65                              const char *tname,
66                              int id_recalc_tag)
67         : type(type),
68           tname(tname),
69           id_recalc_tag(id_recalc_tag)
70 {
71         if (type == DEG_NODE_TYPE_OPERATION)
72                 this->tclass = DEG_NODE_CLASS_OPERATION;
73         else if (type < DEG_NODE_TYPE_PARAMETERS)
74                 this->tclass = DEG_NODE_CLASS_GENERIC;
75         else
76                 this->tclass = DEG_NODE_CLASS_COMPONENT;
77 }
78
79 DepsNode::DepsNode()
80 {
81         name = "";
82 }
83
84 DepsNode::~DepsNode()
85 {
86         /* Free links. */
87         /* NOTE: We only free incoming links. This is to avoid double-free of links
88          * when we're trying to free same link from both it's sides. We don't have
89          * dangling links so this is not a problem from memory leaks point of view.
90          */
91         foreach (DepsRelation *rel, inlinks) {
92                 OBJECT_GUARDED_DELETE(rel, DepsRelation);
93         }
94 }
95
96
97 /* Generic identifier for Depsgraph Nodes. */
98 string DepsNode::identifier() const
99 {
100         char typebuf[7];
101         sprintf(typebuf, "(%d)", type);
102
103         return string(typebuf) + " : " + name;
104 }
105
106 /* ************* */
107 /* Generic Nodes */
108
109 /* Time Source Node ============================================== */
110
111 void TimeSourceDepsNode::tag_update(Depsgraph *graph)
112 {
113         foreach (DepsRelation *rel, outlinks) {
114                 DepsNode *node = rel->to;
115                 node->tag_update(graph);
116         }
117 }
118
119 /* Time Source Node ======================================= */
120
121 DEG_DEPSNODE_DEFINE(TimeSourceDepsNode, DEG_NODE_TYPE_TIMESOURCE, "Time Source");
122 static DepsNodeFactoryImpl<TimeSourceDepsNode> DNTI_TIMESOURCE;
123
124 /* ID Node ================================================ */
125
126 IDDepsNode::ComponentIDKey::ComponentIDKey(eDepsNode_Type type,
127                                            const char *name)
128         : type(type), name(name)
129 {
130 }
131
132 bool IDDepsNode::ComponentIDKey::operator== (const ComponentIDKey &other) const
133 {
134         return type == other.type &&
135                 STREQ(name, other.name);
136 }
137
138 static unsigned int id_deps_node_hash_key(const void *key_v)
139 {
140         const IDDepsNode::ComponentIDKey *key =
141                 reinterpret_cast<const IDDepsNode::ComponentIDKey *>(key_v);
142         return BLI_ghashutil_combine_hash(BLI_ghashutil_uinthash(key->type),
143                                           BLI_ghashutil_strhash_p(key->name));
144 }
145
146 static bool id_deps_node_hash_key_cmp(const void *a, const void *b)
147 {
148         const IDDepsNode::ComponentIDKey *key_a =
149                 reinterpret_cast<const IDDepsNode::ComponentIDKey *>(a);
150         const IDDepsNode::ComponentIDKey *key_b =
151                 reinterpret_cast<const IDDepsNode::ComponentIDKey *>(b);
152         return !(*key_a == *key_b);
153 }
154
155 static void id_deps_node_hash_key_free(void *key_v)
156 {
157         typedef IDDepsNode::ComponentIDKey ComponentIDKey;
158         ComponentIDKey *key = reinterpret_cast<ComponentIDKey *>(key_v);
159         OBJECT_GUARDED_DELETE(key, ComponentIDKey);
160 }
161
162 static void id_deps_node_hash_value_free(void *value_v)
163 {
164         ComponentDepsNode *comp_node = reinterpret_cast<ComponentDepsNode *>(value_v);
165         OBJECT_GUARDED_DELETE(comp_node, ComponentDepsNode);
166 }
167
168 /* Initialize 'id' node - from pointer data given. */
169 void IDDepsNode::init(const ID *id, const char *UNUSED(subdata))
170 {
171         BLI_assert(id != NULL);
172         /* Store ID-pointer. */
173         id_orig = (ID *)id;
174         eval_flags = 0;
175         linked_state = DEG_ID_LINKED_INDIRECTLY;
176
177         components = BLI_ghash_new(id_deps_node_hash_key,
178                                    id_deps_node_hash_key_cmp,
179                                    "Depsgraph id components hash");
180 }
181
182 void IDDepsNode::init_copy_on_write(ID *id_cow_hint)
183 {
184         /* Early output for non-copy-on-write case: we keep CoW pointer same as
185          * an original one.
186          */
187         if (!DEG_depsgraph_use_copy_on_write()) {
188                 UNUSED_VARS(id_cow_hint);
189                 id_cow = id_orig;
190                 return;
191         }
192         /* Create pointer as early as possible, so we can use it for function
193          * bindings. Rest of data we'll be copying to the new datablock when
194          * it is actually needed.
195          */
196         if (id_cow_hint != NULL) {
197                 // BLI_assert(deg_copy_on_write_is_needed(id_orig));
198                 if (deg_copy_on_write_is_needed(id_orig)) {
199                         id_cow = id_cow_hint;
200                 }
201                 else {
202                         id_cow = id_orig;
203                 }
204         }
205         else if (deg_copy_on_write_is_needed(id_orig)) {
206                 id_cow = (ID *)BKE_libblock_alloc_notest(GS(id_orig->name));
207                 DEG_COW_PRINT("Create shallow copy for %s: id_orig=%p id_cow=%p\n",
208                               id_orig->name, id_orig, id_cow);
209                 deg_tag_copy_on_write_id(id_cow, id_orig);
210         }
211         else {
212                 id_cow = id_orig;
213         }
214 }
215
216 /* Free 'id' node. */
217 IDDepsNode::~IDDepsNode()
218 {
219         destroy();
220 }
221
222 void IDDepsNode::destroy()
223 {
224         if (id_orig == NULL) {
225                 return;
226         }
227
228         BLI_ghash_free(components,
229                        id_deps_node_hash_key_free,
230                        id_deps_node_hash_value_free);
231
232         /* Free memory used by this CoW ID. */
233         if (id_cow != id_orig && id_cow != NULL) {
234                 deg_free_copy_on_write_datablock(id_cow);
235                 MEM_freeN(id_cow);
236                 DEG_COW_PRINT("Destroy CoW for %s: id_orig=%p id_cow=%p\n",
237                               id_orig->name, id_orig, id_cow);
238         }
239
240         /* Tag that the node is freed. */
241         id_orig = NULL;
242 }
243
244 ComponentDepsNode *IDDepsNode::find_component(eDepsNode_Type type,
245                                               const char *name) const
246 {
247         ComponentIDKey key(type, name);
248         return reinterpret_cast<ComponentDepsNode *>(BLI_ghash_lookup(components, &key));
249 }
250
251 ComponentDepsNode *IDDepsNode::add_component(eDepsNode_Type type,
252                                              const char *name)
253 {
254         ComponentDepsNode *comp_node = find_component(type, name);
255         if (!comp_node) {
256                 DepsNodeFactory *factory = deg_type_get_factory(type);
257                 comp_node = (ComponentDepsNode *)factory->create_node(this->id_orig, "", name);
258
259                 /* Register. */
260                 ComponentIDKey *key = OBJECT_GUARDED_NEW(ComponentIDKey, type, name);
261                 BLI_ghash_insert(components, key, comp_node);
262                 comp_node->owner = this;
263         }
264         return comp_node;
265 }
266
267 void IDDepsNode::tag_update(Depsgraph *graph)
268 {
269         GHASH_FOREACH_BEGIN(ComponentDepsNode *, comp_node, components)
270         {
271                 comp_node->tag_update(graph);
272         }
273         GHASH_FOREACH_END();
274 }
275
276 void IDDepsNode::finalize_build(Depsgraph *graph)
277 {
278         /* Finalize build of all components. */
279         GHASH_FOREACH_BEGIN(ComponentDepsNode *, comp_node, components)
280         {
281                 comp_node->finalize_build(graph);
282         }
283         GHASH_FOREACH_END();
284 }
285
286 DEG_DEPSNODE_DEFINE(IDDepsNode, DEG_NODE_TYPE_ID_REF, "ID Node");
287 static DepsNodeFactoryImpl<IDDepsNode> DNTI_ID_REF;
288
289 void deg_register_base_depsnodes()
290 {
291         deg_register_node_typeinfo(&DNTI_TIMESOURCE);
292         deg_register_node_typeinfo(&DNTI_ID_REF);
293 }
294
295 }  // namespace DEG