Depsgraph: Move time source node to own file
[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 <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
43 #include "BKE_animsys.h"
44 }
45
46 #include "DEG_depsgraph.h"
47
48 #include "intern/nodes/deg_node_component.h"
49 #include "intern/nodes/deg_node_operation.h"
50 #include "intern/nodes/deg_node_time.h"
51 #include "intern/depsgraph_intern.h"
52
53 #include "util/deg_util_foreach.h"
54
55 namespace DEG {
56
57 /* *************** */
58 /* Node Management */
59
60 /* Add ------------------------------------------------ */
61
62 DepsNode::TypeInfo::TypeInfo(eDepsNode_Type type,
63                              const char *tname,
64                              int id_recalc_tag)
65         : type(type),
66           tname(tname),
67           id_recalc_tag(id_recalc_tag)
68 {
69         if (type == DEG_NODE_TYPE_OPERATION)
70                 this->tclass = DEG_NODE_CLASS_OPERATION;
71         else if (type < DEG_NODE_TYPE_PARAMETERS)
72                 this->tclass = DEG_NODE_CLASS_GENERIC;
73         else
74                 this->tclass = DEG_NODE_CLASS_COMPONENT;
75 }
76
77 DepsNode::DepsNode()
78 {
79         name = "";
80 }
81
82 DepsNode::~DepsNode()
83 {
84         /* Free links. */
85         /* NOTE: We only free incoming links. This is to avoid double-free of links
86          * when we're trying to free same link from both it's sides. We don't have
87          * dangling links so this is not a problem from memory leaks point of view.
88          */
89         foreach (DepsRelation *rel, inlinks) {
90                 OBJECT_GUARDED_DELETE(rel, DepsRelation);
91         }
92 }
93
94
95 /* Generic identifier for Depsgraph Nodes. */
96 string DepsNode::identifier() const
97 {
98         char typebuf[7];
99         sprintf(typebuf, "(%d)", type);
100
101         return string(typebuf) + " : " + name;
102 }
103
104 /* ************* */
105 /* Generic Nodes */
106
107 /* Time Source Node ======================================= */
108
109 DEG_DEPSNODE_DEFINE(TimeSourceDepsNode, DEG_NODE_TYPE_TIMESOURCE, "Time Source");
110 static DepsNodeFactoryImpl<TimeSourceDepsNode> DNTI_TIMESOURCE;
111
112 /* ID Node ================================================ */
113
114 IDDepsNode::ComponentIDKey::ComponentIDKey(eDepsNode_Type type,
115                                            const char *name)
116         : type(type), name(name)
117 {
118 }
119
120 bool IDDepsNode::ComponentIDKey::operator== (const ComponentIDKey &other) const
121 {
122     return type == other.type &&
123            STREQ(name, other.name);
124 }
125
126 static unsigned int id_deps_node_hash_key(const void *key_v)
127 {
128         const IDDepsNode::ComponentIDKey *key =
129                 reinterpret_cast<const IDDepsNode::ComponentIDKey *>(key_v);
130         return BLI_ghashutil_combine_hash(BLI_ghashutil_uinthash(key->type),
131                                           BLI_ghashutil_strhash_p(key->name));
132 }
133
134 static bool id_deps_node_hash_key_cmp(const void *a, const void *b)
135 {
136         const IDDepsNode::ComponentIDKey *key_a =
137                 reinterpret_cast<const IDDepsNode::ComponentIDKey *>(a);
138         const IDDepsNode::ComponentIDKey *key_b =
139                 reinterpret_cast<const IDDepsNode::ComponentIDKey *>(b);
140         return !(*key_a == *key_b);
141 }
142
143 static void id_deps_node_hash_key_free(void *key_v)
144 {
145         typedef IDDepsNode::ComponentIDKey ComponentIDKey;
146         ComponentIDKey *key = reinterpret_cast<ComponentIDKey *>(key_v);
147         OBJECT_GUARDED_DELETE(key, ComponentIDKey);
148 }
149
150 static void id_deps_node_hash_value_free(void *value_v)
151 {
152         ComponentDepsNode *comp_node = reinterpret_cast<ComponentDepsNode *>(value_v);
153         OBJECT_GUARDED_DELETE(comp_node, ComponentDepsNode);
154 }
155
156 /* Initialize 'id' node - from pointer data given. */
157 void IDDepsNode::init(const ID *id, const char *UNUSED(subdata))
158 {
159         /* Store ID-pointer. */
160         BLI_assert(id != NULL);
161         this->id = (ID *)id;
162         this->layers = (1 << 20) - 1;
163         this->eval_flags = 0;
164
165         /* For object we initialize layers to layer from base. */
166         if (GS(id->name) == ID_OB) {
167                 this->layers = 0;
168         }
169
170         components = BLI_ghash_new(id_deps_node_hash_key,
171                                    id_deps_node_hash_key_cmp,
172                                    "Depsgraph id components hash");
173
174         /* NOTE: components themselves are created if/when needed.
175          * This prevents problems with components getting added
176          * twice if an ID-Ref needs to be created to house it...
177          */
178 }
179
180 /* Free 'id' node. */
181 IDDepsNode::~IDDepsNode()
182 {
183         BLI_ghash_free(components,
184                        id_deps_node_hash_key_free,
185                        id_deps_node_hash_value_free);
186 }
187
188 ComponentDepsNode *IDDepsNode::find_component(eDepsNode_Type type,
189                                               const char *name) const
190 {
191         ComponentIDKey key(type, name);
192         return reinterpret_cast<ComponentDepsNode *>(BLI_ghash_lookup(components, &key));
193 }
194
195 ComponentDepsNode *IDDepsNode::add_component(eDepsNode_Type type,
196                                              const char *name)
197 {
198         ComponentDepsNode *comp_node = find_component(type, name);
199         if (!comp_node) {
200                 DepsNodeFactory *factory = deg_type_get_factory(type);
201                 comp_node = (ComponentDepsNode *)factory->create_node(this->id, "", name);
202
203                 /* Register. */
204                 ComponentIDKey *key = OBJECT_GUARDED_NEW(ComponentIDKey, type, name);
205                 BLI_ghash_insert(components, key, comp_node);
206                 comp_node->owner = this;
207         }
208         return comp_node;
209 }
210
211 void IDDepsNode::tag_update(Depsgraph *graph)
212 {
213         GHASH_FOREACH_BEGIN(ComponentDepsNode *, comp_node, components)
214         {
215                 /* TODO(sergey): What about drievrs? */
216                 bool do_component_tag = comp_node->type != DEG_NODE_TYPE_ANIMATION;
217                 if (comp_node->type == DEG_NODE_TYPE_ANIMATION) {
218                         AnimData *adt = BKE_animdata_from_id(id);
219                         /* Animation data might be null if relations are tagged for update. */
220                         if (adt != NULL && (adt->recalc & ADT_RECALC_ANIM)) {
221                                 do_component_tag = true;
222                         }
223                 }
224                 if (do_component_tag) {
225                         comp_node->tag_update(graph);
226                 }
227         }
228         GHASH_FOREACH_END();
229 }
230
231 void IDDepsNode::finalize_build()
232 {
233         GHASH_FOREACH_BEGIN(ComponentDepsNode *, comp_node, components)
234         {
235                 comp_node->finalize_build();
236         }
237         GHASH_FOREACH_END();
238 }
239
240 DEG_DEPSNODE_DEFINE(IDDepsNode, DEG_NODE_TYPE_ID_REF, "ID Node");
241 static DepsNodeFactoryImpl<IDDepsNode> DNTI_ID_REF;
242
243 void deg_register_base_depsnodes()
244 {
245         deg_register_node_typeinfo(&DNTI_TIMESOURCE);
246         deg_register_node_typeinfo(&DNTI_ID_REF);
247 }
248
249 }  // namespace DEG