Depsgraph: Move time source node to own file
[blender-staging.git] / source / blender / depsgraph / intern / depsgraph.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): Sergey Sharybin
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/depsgraph/intern/depsgraph.cc
28  *  \ingroup depsgraph
29  *
30  * Core routines for how the Depsgraph works.
31  */
32
33 #include "intern/depsgraph.h" /* own include */
34
35 #include "MEM_guardedalloc.h"
36
37 #include "BLI_utildefines.h"
38 #include "BLI_ghash.h"
39 #include "BLI_listbase.h"
40
41 extern "C" {
42 #include "DNA_action_types.h"
43 #include "DNA_armature_types.h"
44 #include "DNA_constraint_types.h"
45 #include "DNA_key_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_sequence_types.h"
48
49 #include "BKE_depsgraph.h"
50
51 #include "RNA_access.h"
52 }
53
54 #include <algorithm>
55 #include <cstring>
56
57 #include "DEG_depsgraph.h"
58
59 #include "intern/nodes/deg_node.h"
60 #include "intern/nodes/deg_node_component.h"
61 #include "intern/nodes/deg_node_operation.h"
62 #include "intern/nodes/deg_node_time.h"
63
64 #include "intern/depsgraph_intern.h"
65 #include "util/deg_util_foreach.h"
66
67 namespace DEG {
68
69 static DEG_EditorUpdateIDCb deg_editor_update_id_cb = NULL;
70 static DEG_EditorUpdateSceneCb deg_editor_update_scene_cb = NULL;
71 static DEG_EditorUpdateScenePreCb deg_editor_update_scene_pre_cb = NULL;
72
73 /* TODO(sergey): Find a better place for this. */
74 template <typename T>
75 static void remove_from_vector(vector<T> *vector, const T& value)
76 {
77         vector->erase(std::remove(vector->begin(), vector->end(), value),
78                       vector->end());
79 }
80
81 Depsgraph::Depsgraph()
82   : time_source(NULL),
83     need_update(false),
84     layers(0)
85 {
86         BLI_spin_init(&lock);
87         id_hash = BLI_ghash_ptr_new("Depsgraph id hash");
88         entry_tags = BLI_gset_ptr_new("Depsgraph entry_tags");
89 }
90
91 Depsgraph::~Depsgraph()
92 {
93         clear_id_nodes();
94         BLI_ghash_free(id_hash, NULL, NULL);
95         BLI_gset_free(entry_tags, NULL);
96         if (time_source != NULL) {
97                 OBJECT_GUARDED_DELETE(time_source, TimeSourceDepsNode);
98         }
99         BLI_spin_end(&lock);
100 }
101
102 /* Query Conditions from RNA ----------------------- */
103
104 static bool pointer_to_component_node_criteria(
105         const PointerRNA *ptr,
106         const PropertyRNA *prop,
107         ID **id,
108         eDepsNode_Type *type,
109         const char **subdata,
110         eDepsOperation_Code *operation_code,
111         const char **operation_name,
112         int *operation_name_tag)
113 {
114         if (ptr->type == NULL) {
115                 return false;
116         }
117         /* Set default values for returns. */
118         *id = (ID *)ptr->id.data;
119         *subdata = "";
120         *operation_code = DEG_OPCODE_OPERATION;
121         *operation_name = "";
122         *operation_name_tag = -1;
123         /* Handling of commonly known scenarios. */
124         if (ptr->type == &RNA_PoseBone) {
125                 bPoseChannel *pchan = (bPoseChannel *)ptr->data;
126                 if (prop != NULL && RNA_property_is_idprop(prop)) {
127                         *type = DEG_NODE_TYPE_PARAMETERS;
128                         *subdata = "";
129                         *operation_code = DEG_OPCODE_PARAMETERS_EVAL;
130                         *operation_name = pchan->name;;
131                 }
132                 else {
133                         /* Bone - generally, we just want the bone component. */
134                         *type = DEG_NODE_TYPE_BONE;
135                         *subdata = pchan->name;
136                 }
137                 return true;
138         }
139         else if (ptr->type == &RNA_Bone) {
140                 Bone *bone = (Bone *)ptr->data;
141                 /* armature-level bone, but it ends up going to bone component anyway */
142                 // NOTE: the ID in thise case will end up being bArmature.
143                 *type = DEG_NODE_TYPE_BONE;
144                 *subdata = bone->name;
145                 return true;
146         }
147         else if (RNA_struct_is_a(ptr->type, &RNA_Constraint)) {
148                 Object *object = (Object *)ptr->id.data;
149                 bConstraint *con = (bConstraint *)ptr->data;
150                 /* Check whether is object or bone constraint. */
151                 if (BLI_findindex(&object->constraints, con) != -1) {
152                         /* Constraint is defining object transform. */
153                         *type = DEG_NODE_TYPE_TRANSFORM;
154                         return true;
155                 }
156                 else if (object->pose != NULL) {
157                         LINKLIST_FOREACH(bPoseChannel *, pchan, &object->pose->chanbase) {
158                                 if (BLI_findindex(&pchan->constraints, con) != -1) {
159                                         /* bone transforms */
160                                         *type = DEG_NODE_TYPE_BONE;
161                                         *subdata = pchan->name;
162                                         return true;
163                                 }
164                         }
165                 }
166         }
167         else if (RNA_struct_is_a(ptr->type, &RNA_Modifier)) {
168                 *type = DEG_NODE_TYPE_GEOMETRY;
169                 return true;
170         }
171         else if (ptr->type == &RNA_Object) {
172                 /* Transforms props? */
173                 if (prop != NULL) {
174                         const char *prop_identifier = RNA_property_identifier((PropertyRNA *)prop);
175                         /* TODO(sergey): How to optimize this? */
176                         if (strstr(prop_identifier, "location") ||
177                             strstr(prop_identifier, "rotation") ||
178                             strstr(prop_identifier, "scale") ||
179                             strstr(prop_identifier, "matrix_"))
180                         {
181                                 *type = DEG_NODE_TYPE_TRANSFORM;
182                                 return true;
183                         }
184                         else if (strstr(prop_identifier, "data")) {
185                                 /* We access object.data, most likely a geometry.
186                                  * Might be a bone tho..
187                                  */
188                                 *type = DEG_NODE_TYPE_GEOMETRY;
189                                 return true;
190                         }
191                 }
192         }
193         else if (ptr->type == &RNA_ShapeKey) {
194                 Key *key = (Key *)ptr->id.data;
195                 /* ShapeKeys are currently handled as geometry on the geometry that
196                  * owns it.
197                  */
198                 *id = key->from;
199                 *type = DEG_NODE_TYPE_GEOMETRY;
200                 return true;
201         }
202         else if (ptr->type == &RNA_Key) {
203                 *id = (ID *)ptr->id.data;
204                 *type = DEG_NODE_TYPE_GEOMETRY;
205                 return true;
206         }
207         else if (RNA_struct_is_a(ptr->type, &RNA_Sequence)) {
208                 Sequence *seq = (Sequence *)ptr->data;
209                 /* Sequencer strip */
210                 *type = DEG_NODE_TYPE_SEQUENCER;
211                 *subdata = seq->name; // xxx?
212                 return true;
213         }
214         if (prop != NULL) {
215                 /* All unknown data effectively falls under "parameter evaluation". */
216                 *type = DEG_NODE_TYPE_PARAMETERS;
217                 *operation_code = DEG_OPCODE_PARAMETERS_EVAL;
218                 *operation_name = "";
219                 *operation_name_tag = -1;
220                 return true;
221         }
222         return false;
223 }
224
225 /* Convenience wrapper to find node given just pointer + property. */
226 DepsNode *Depsgraph::find_node_from_pointer(const PointerRNA *ptr,
227                                             const PropertyRNA *prop) const
228 {
229         ID *id;
230         eDepsNode_Type node_type;
231         const char *component_name, *operation_name;
232         eDepsOperation_Code operation_code;
233         int operation_name_tag;
234
235         if (pointer_to_component_node_criteria(
236                          ptr, prop,
237                          &id, &node_type, &component_name,
238                          &operation_code, &operation_name, &operation_name_tag))
239         {
240                 IDDepsNode *id_node = find_id_node(id);
241                 if (id_node == NULL) {
242                         return NULL;
243                 }
244                 ComponentDepsNode *comp_node =
245                         id_node->find_component(node_type, component_name);
246                 if (comp_node == NULL) {
247                         return NULL;
248                 }
249                 if (operation_code == DEG_OPCODE_OPERATION) {
250                         return comp_node;
251                 }
252                 return comp_node->find_operation(operation_code,
253                                                  operation_name,
254                                                  operation_name_tag);
255         }
256         return NULL;
257 }
258
259 /* Node Management ---------------------------- */
260
261 static void id_node_deleter(void *value)
262 {
263         IDDepsNode *id_node = reinterpret_cast<IDDepsNode *>(value);
264         OBJECT_GUARDED_DELETE(id_node, IDDepsNode);
265 }
266
267 TimeSourceDepsNode *Depsgraph::add_time_source()
268 {
269         if (time_source == NULL) {
270                 DepsNodeFactory *factory = deg_type_get_factory(DEG_NODE_TYPE_TIMESOURCE);
271                 time_source = (TimeSourceDepsNode *)factory->create_node(NULL, "", "Time Source");
272         }
273         return time_source;
274 }
275
276 TimeSourceDepsNode *Depsgraph::find_time_source() const
277 {
278         return time_source;
279 }
280
281 IDDepsNode *Depsgraph::find_id_node(const ID *id) const
282 {
283         return reinterpret_cast<IDDepsNode *>(BLI_ghash_lookup(id_hash, id));
284 }
285
286 IDDepsNode *Depsgraph::add_id_node(ID *id, const char *name)
287 {
288         IDDepsNode *id_node = find_id_node(id);
289         if (!id_node) {
290                 DepsNodeFactory *factory = deg_type_get_factory(DEG_NODE_TYPE_ID_REF);
291                 id_node = (IDDepsNode *)factory->create_node(id, "", name);
292                 id->tag |= LIB_TAG_DOIT;
293                 /* register */
294                 BLI_ghash_insert(id_hash, id, id_node);
295                 id_nodes.push_back(id_node);
296         }
297         return id_node;
298 }
299
300 void Depsgraph::clear_id_nodes()
301 {
302         BLI_ghash_clear(id_hash, NULL, id_node_deleter);
303         id_nodes.clear();
304 }
305
306 /* Add new relationship between two nodes. */
307 DepsRelation *Depsgraph::add_new_relation(OperationDepsNode *from,
308                                           OperationDepsNode *to,
309                                           const char *description,
310                                           bool check_unique)
311 {
312         DepsRelation *rel = NULL;
313         if (check_unique) {
314                 rel = check_nodes_connected(from, to, description);
315         }
316         if (rel != NULL) {
317                 return rel;
318         }
319         /* Create new relation, and add it to the graph. */
320         rel = OBJECT_GUARDED_NEW(DepsRelation, from, to, description);
321         /* TODO(sergey): Find a better place for this. */
322 #ifdef WITH_OPENSUBDIV
323         ComponentDepsNode *comp_node = from->owner;
324         if (comp_node->type == DEG_NODE_TYPE_GEOMETRY) {
325                 IDDepsNode *id_to = to->owner->owner;
326                 IDDepsNode *id_from = from->owner->owner;
327                 if (id_to != id_from && (id_to->id->recalc & ID_RECALC_ALL)) {
328                         if ((id_from->eval_flags & DAG_EVAL_NEED_CPU) == 0) {
329                                 id_from->tag_update(this);
330                                 id_from->eval_flags |= DAG_EVAL_NEED_CPU;
331                         }
332                 }
333         }
334 #endif
335         return rel;
336 }
337
338 /* Add new relation between two nodes */
339 DepsRelation *Depsgraph::add_new_relation(DepsNode *from, DepsNode *to,
340                                           const char *description,
341                                           bool check_unique)
342 {
343         DepsRelation *rel = NULL;
344         if (check_unique) {
345                 rel = check_nodes_connected(from, to, description);
346         }
347         if (rel != NULL) {
348                 return rel;
349         }
350         /* Create new relation, and add it to the graph. */
351         rel = OBJECT_GUARDED_NEW(DepsRelation, from, to, description);
352         return rel;
353 }
354
355 DepsRelation *Depsgraph::check_nodes_connected(const DepsNode *from,
356                                                const DepsNode *to,
357                                                const char *description)
358 {
359         foreach (DepsRelation *rel, from->outlinks) {
360                 BLI_assert(rel->from == from);
361                 if (rel->to != to) {
362                         continue;
363                 }
364                 if (description != NULL && !STREQ(rel->name, description)) {
365                         continue;
366                 }
367                 return rel;
368         }
369         return NULL;
370 }
371
372 /* ************************ */
373 /* Relationships Management */
374
375 DepsRelation::DepsRelation(DepsNode *from,
376                            DepsNode *to,
377                            const char *description)
378   : from(from),
379     to(to),
380     name(description),
381     flag(0)
382 {
383         /* Hook it up to the nodes which use it.
384          *
385          * NOTE: We register relation in the nodes which this link connects to here
386          * in constructor but we don't unregister it in the destructor.
387          *
388          * Reasoning:
389          *
390          * - Destructor is currently used on global graph destruction, so there's no
391          *   real need in avoiding dangling pointers, all the memory is to be freed
392          *   anyway.
393          *
394          * - Unregistering relation is not a cheap operation, so better to have it
395          *   as an explicit call if we need this.
396          */
397         from->outlinks.push_back(this);
398         to->inlinks.push_back(this);
399 }
400
401 DepsRelation::~DepsRelation()
402 {
403         /* Sanity check. */
404         BLI_assert(from != NULL && to != NULL);
405 }
406
407 void DepsRelation::unlink()
408 {
409         /* Sanity check. */
410         BLI_assert(from != NULL && to != NULL);
411         remove_from_vector(&from->outlinks, this);
412         remove_from_vector(&to->inlinks, this);
413 }
414
415 /* Low level tagging -------------------------------------- */
416
417 /* Tag a specific node as needing updates. */
418 void Depsgraph::add_entry_tag(OperationDepsNode *node)
419 {
420         /* Sanity check. */
421         if (!node)
422                 return;
423
424         /* Add to graph-level set of directly modified nodes to start searching from.
425          * NOTE: this is necessary since we have several thousand nodes to play with...
426          */
427         BLI_gset_insert(entry_tags, node);
428 }
429
430 void Depsgraph::clear_all_nodes()
431 {
432         clear_id_nodes();
433         if (time_source != NULL) {
434                 OBJECT_GUARDED_DELETE(time_source, TimeSourceDepsNode);
435                 time_source = NULL;
436         }
437 }
438
439 void deg_editors_id_update(Main *bmain, ID *id)
440 {
441         if (deg_editor_update_id_cb != NULL) {
442                 deg_editor_update_id_cb(bmain, id);
443         }
444 }
445
446 void deg_editors_scene_update(Main *bmain, Scene *scene, bool updated)
447 {
448         if (deg_editor_update_scene_cb != NULL) {
449                 deg_editor_update_scene_cb(bmain, scene, updated);
450         }
451 }
452
453 }  // namespace DEG
454
455 /* **************** */
456 /* Public Graph API */
457
458 /* Initialize a new Depsgraph */
459 Depsgraph *DEG_graph_new()
460 {
461         DEG::Depsgraph *deg_depsgraph = OBJECT_GUARDED_NEW(DEG::Depsgraph);
462         return reinterpret_cast<Depsgraph *>(deg_depsgraph);
463 }
464
465 /* Free graph's contents and graph itself */
466 void DEG_graph_free(Depsgraph *graph)
467 {
468         using DEG::Depsgraph;
469         DEG::Depsgraph *deg_depsgraph = reinterpret_cast<DEG::Depsgraph *>(graph);
470         OBJECT_GUARDED_DELETE(deg_depsgraph, Depsgraph);
471 }
472
473 /* Set callbacks which are being called when depsgraph changes. */
474 void DEG_editors_set_update_cb(DEG_EditorUpdateIDCb id_func,
475                                DEG_EditorUpdateSceneCb scene_func,
476                                DEG_EditorUpdateScenePreCb scene_pre_func)
477 {
478         DEG::deg_editor_update_id_cb = id_func;
479         DEG::deg_editor_update_scene_cb = scene_func;
480         DEG::deg_editor_update_scene_pre_cb = scene_pre_func;
481 }
482
483 void DEG_editors_update_pre(Main *bmain, Scene *scene, bool time)
484 {
485         if (DEG::deg_editor_update_scene_pre_cb != NULL) {
486                 DEG::deg_editor_update_scene_pre_cb(bmain, scene, time);
487         }
488 }