Cleanup: style, whitespace, doxy filepaths
[blender-staging.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 <string.h>
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
43 #include "BKE_animsys.h"
44
45 #include "DEG_depsgraph.h"
46 }
47
48 #include "intern/nodes/deg_node_component.h"
49 #include "intern/nodes/deg_node_operation.h"
50 #include "intern/depsgraph_intern.h"
51 #include "util/deg_util_foreach.h"
52 #include "util/deg_util_hash.h"
53
54 namespace DEG {
55
56 /* *************** */
57 /* Node Management */
58
59 /* Add ------------------------------------------------ */
60
61 DepsNode::TypeInfo::TypeInfo(eDepsNode_Type type, const char *tname)
62 {
63         this->type = type;
64         if (type == DEPSNODE_TYPE_OPERATION)
65                 this->tclass = DEPSNODE_CLASS_OPERATION;
66         else if (type < DEPSNODE_TYPE_PARAMETERS)
67                 this->tclass = DEPSNODE_CLASS_GENERIC;
68         else
69                 this->tclass = DEPSNODE_CLASS_COMPONENT;
70         this->tname = tname;
71 }
72
73 DepsNode::DepsNode()
74 {
75         name[0] = '\0';
76 }
77
78 DepsNode::~DepsNode()
79 {
80         /* Free links. */
81         /* NOTE: We only free incoming links. This is to avoid double-free of links
82          * when we're trying to free same link from both it's sides. We don't have
83          * dangling links so this is not a problem from memory leaks point of view.
84          */
85         foreach (DepsRelation *rel, inlinks) {
86                 OBJECT_GUARDED_DELETE(rel, DepsRelation);
87         }
88 }
89
90
91 /* Generic identifier for Depsgraph Nodes. */
92 string DepsNode::identifier() const
93 {
94         char typebuf[7];
95         sprintf(typebuf, "(%d)", type);
96
97         return string(typebuf) + " : " + name;
98 }
99
100 /* ************* */
101 /* Generic Nodes */
102
103 /* Time Source Node ============================================== */
104
105 void TimeSourceDepsNode::tag_update(Depsgraph *graph)
106 {
107         foreach (DepsRelation *rel, outlinks) {
108                 DepsNode *node = rel->to;
109                 node->tag_update(graph);
110         }
111 }
112
113
114 /* Root Node ============================================== */
115
116 RootDepsNode::RootDepsNode() : scene(NULL), time_source(NULL)
117 {
118 }
119
120 RootDepsNode::~RootDepsNode()
121 {
122         OBJECT_GUARDED_DELETE(time_source, TimeSourceDepsNode);
123 }
124
125 TimeSourceDepsNode *RootDepsNode::add_time_source(const string &name)
126 {
127         if (!time_source) {
128                 DepsNodeFactory *factory = deg_get_node_factory(DEPSNODE_TYPE_TIMESOURCE);
129                 time_source = (TimeSourceDepsNode *)factory->create_node(NULL, "", name);
130                 /*time_source->owner = this;*/ // XXX
131         }
132         return time_source;
133 }
134
135 DEG_DEPSNODE_DEFINE(RootDepsNode, DEPSNODE_TYPE_ROOT, "Root DepsNode");
136 static DepsNodeFactoryImpl<RootDepsNode> DNTI_ROOT;
137
138 /* Time Source Node ======================================= */
139
140 DEG_DEPSNODE_DEFINE(TimeSourceDepsNode, DEPSNODE_TYPE_TIMESOURCE, "Time Source");
141 static DepsNodeFactoryImpl<TimeSourceDepsNode> DNTI_TIMESOURCE;
142
143 /* ID Node ================================================ */
144
145 static unsigned int id_deps_node_hash_key(const void *key_v)
146 {
147         const IDDepsNode::ComponentIDKey *key =
148                 reinterpret_cast<const IDDepsNode::ComponentIDKey *>(key_v);
149         return hash_combine(BLI_ghashutil_uinthash(key->type),
150                             BLI_ghashutil_strhash_p(key->name.c_str()));
151 }
152
153 static bool id_deps_node_hash_key_cmp(const void *a, const void *b)
154 {
155         const IDDepsNode::ComponentIDKey *key_a =
156                 reinterpret_cast<const IDDepsNode::ComponentIDKey *>(a);
157         const IDDepsNode::ComponentIDKey *key_b =
158                 reinterpret_cast<const IDDepsNode::ComponentIDKey *>(b);
159         return !(*key_a == *key_b);
160 }
161
162 static void id_deps_node_hash_key_free(void *key_v)
163 {
164         typedef IDDepsNode::ComponentIDKey ComponentIDKey;
165         ComponentIDKey *key = reinterpret_cast<ComponentIDKey *>(key_v);
166         OBJECT_GUARDED_DELETE(key, ComponentIDKey);
167 }
168
169 static void id_deps_node_hash_value_free(void *value_v)
170 {
171         ComponentDepsNode *comp_node = reinterpret_cast<ComponentDepsNode *>(value_v);
172         OBJECT_GUARDED_DELETE(comp_node, ComponentDepsNode);
173 }
174
175 /* Initialize 'id' node - from pointer data given. */
176 void IDDepsNode::init(const ID *id, const string &UNUSED(subdata))
177 {
178         /* Store ID-pointer. */
179         BLI_assert(id != NULL);
180         this->id = (ID *)id;
181         this->layers = (1 << 20) - 1;
182         this->eval_flags = 0;
183
184         components = BLI_ghash_new(id_deps_node_hash_key,
185                                    id_deps_node_hash_key_cmp,
186                                    "Depsgraph id components hash");
187
188         /* NOTE: components themselves are created if/when needed.
189          * This prevents problems with components getting added
190          * twice if an ID-Ref needs to be created to house it...
191          */
192 }
193
194 /* Free 'id' node. */
195 IDDepsNode::~IDDepsNode()
196 {
197         clear_components();
198         BLI_ghash_free(components, id_deps_node_hash_key_free, NULL);
199 }
200
201 ComponentDepsNode *IDDepsNode::find_component(eDepsNode_Type type,
202                                               const string &name) const
203 {
204         ComponentIDKey key(type, name);
205         return reinterpret_cast<ComponentDepsNode *>(BLI_ghash_lookup(components, &key));
206 }
207
208 ComponentDepsNode *IDDepsNode::add_component(eDepsNode_Type type,
209                                              const string &name)
210 {
211         ComponentDepsNode *comp_node = find_component(type, name);
212         if (!comp_node) {
213                 DepsNodeFactory *factory = deg_get_node_factory(type);
214                 comp_node = (ComponentDepsNode *)factory->create_node(this->id, "", name);
215
216                 /* Register. */
217                 ComponentIDKey *key = OBJECT_GUARDED_NEW(ComponentIDKey, type, name);
218                 BLI_ghash_insert(components, key, comp_node);
219                 comp_node->owner = this;
220         }
221         return comp_node;
222 }
223
224 void IDDepsNode::remove_component(eDepsNode_Type type, const string &name)
225 {
226         ComponentDepsNode *comp_node = find_component(type, name);
227         if (comp_node) {
228                 /* Unregister. */
229                 ComponentIDKey key(type, name);
230                 BLI_ghash_remove(components,
231                                  &key,
232                                  id_deps_node_hash_key_free,
233                                  id_deps_node_hash_value_free);
234         }
235 }
236
237 void IDDepsNode::clear_components()
238 {
239         BLI_ghash_clear(components,
240                         id_deps_node_hash_key_free,
241                         id_deps_node_hash_value_free);
242 }
243
244 void IDDepsNode::tag_update(Depsgraph *graph)
245 {
246         GHASH_FOREACH_BEGIN(ComponentDepsNode *, comp_node, components)
247         {
248                 /* TODO(sergey): What about drievrs? */
249                 bool do_component_tag = comp_node->type != DEPSNODE_TYPE_ANIMATION;
250                 if (comp_node->type == DEPSNODE_TYPE_ANIMATION) {
251                         AnimData *adt = BKE_animdata_from_id(id);
252                         /* Animation data might be null if relations are tagged for update. */
253                         if (adt != NULL && (adt->recalc & ADT_RECALC_ANIM)) {
254                                 do_component_tag = true;
255                         }
256                 }
257                 if (do_component_tag) {
258                         comp_node->tag_update(graph);
259                 }
260         }
261         GHASH_FOREACH_END();
262 }
263
264 void IDDepsNode::finalize_build()
265 {
266         GHASH_FOREACH_BEGIN(ComponentDepsNode *, comp_node, components)
267         {
268                 comp_node->finalize_build();
269         }
270         GHASH_FOREACH_END();
271 }
272
273 DEG_DEPSNODE_DEFINE(IDDepsNode, DEPSNODE_TYPE_ID_REF, "ID Node");
274 static DepsNodeFactoryImpl<IDDepsNode> DNTI_ID_REF;
275
276 /* Subgraph Node ========================================== */
277
278 /* Initialize 'subgraph' node - from pointer data given. */
279 void SubgraphDepsNode::init(const ID *id, const string &UNUSED(subdata))
280 {
281         /* Store ID-ref if provided. */
282         this->root_id = (ID *)id;
283
284         /* NOTE: graph will need to be added manually,
285          * as we don't have any way of passing this down.
286          */
287 }
288
289 /* Free 'subgraph' node */
290 SubgraphDepsNode::~SubgraphDepsNode()
291 {
292         /* Only free if graph not shared, of if this node is the first
293          * reference to it...
294          */
295         // XXX: prune these flags a bit...
296         if ((this->flag & SUBGRAPH_FLAG_FIRSTREF) || !(this->flag & SUBGRAPH_FLAG_SHARED)) {
297                 /* Free the referenced graph. */
298                 DEG_graph_free(reinterpret_cast< ::Depsgraph* >(graph));
299                 graph = NULL;
300         }
301 }
302
303 DEG_DEPSNODE_DEFINE(SubgraphDepsNode, DEPSNODE_TYPE_SUBGRAPH, "Subgraph Node");
304 static DepsNodeFactoryImpl<SubgraphDepsNode> DNTI_SUBGRAPH;
305
306 void deg_register_base_depsnodes()
307 {
308         deg_register_node_typeinfo(&DNTI_ROOT);
309         deg_register_node_typeinfo(&DNTI_TIMESOURCE);
310
311         deg_register_node_typeinfo(&DNTI_ID_REF);
312         deg_register_node_typeinfo(&DNTI_SUBGRAPH);
313 }
314
315 }  // namespace DEG