Depsgraph: Fix missing update with animated curve path
[blender.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_console.h"
39 #include "BLI_hash.h"
40 #include "BLI_ghash.h"
41 #include "BLI_listbase.h"
42
43 extern "C" {
44 #include "DNA_action_types.h"
45 #include "DNA_armature_types.h"
46 #include "DNA_constraint_types.h"
47 #include "DNA_key_types.h"
48 #include "DNA_object_types.h"
49 #include "DNA_sequence_types.h"
50
51 #include "BKE_depsgraph.h"
52
53 #include "RNA_access.h"
54 }
55
56 #include <algorithm>
57 #include <cstring>
58
59 #include "DEG_depsgraph.h"
60
61 #include "intern/nodes/deg_node.h"
62 #include "intern/nodes/deg_node_component.h"
63 #include "intern/nodes/deg_node_id.h"
64 #include "intern/nodes/deg_node_operation.h"
65 #include "intern/nodes/deg_node_time.h"
66
67 #include "intern/depsgraph_intern.h"
68 #include "util/deg_util_foreach.h"
69
70 namespace DEG {
71
72 static DEG_EditorUpdateIDCb deg_editor_update_id_cb = NULL;
73 static DEG_EditorUpdateSceneCb deg_editor_update_scene_cb = NULL;
74 static DEG_EditorUpdateScenePreCb deg_editor_update_scene_pre_cb = NULL;
75
76 /* TODO(sergey): Find a better place for this. */
77 template <typename T>
78 static void remove_from_vector(vector<T> *vector, const T& value)
79 {
80         vector->erase(std::remove(vector->begin(), vector->end(), value),
81                       vector->end());
82 }
83
84 Depsgraph::Depsgraph()
85   : time_source(NULL),
86     need_update(false),
87     layers(0)
88 {
89         BLI_spin_init(&lock);
90         id_hash = BLI_ghash_ptr_new("Depsgraph id hash");
91         entry_tags = BLI_gset_ptr_new("Depsgraph entry_tags");
92 }
93
94 Depsgraph::~Depsgraph()
95 {
96         clear_id_nodes();
97         BLI_ghash_free(id_hash, NULL, NULL);
98         BLI_gset_free(entry_tags, NULL);
99         if (time_source != NULL) {
100                 OBJECT_GUARDED_DELETE(time_source, TimeSourceDepsNode);
101         }
102         BLI_spin_end(&lock);
103 }
104
105 /* Query Conditions from RNA ----------------------- */
106
107 static bool pointer_to_component_node_criteria(
108         const PointerRNA *ptr,
109         const PropertyRNA *prop,
110         ID **id,
111         eDepsNode_Type *type,
112         const char **subdata,
113         eDepsOperation_Code *operation_code,
114         const char **operation_name,
115         int *operation_name_tag)
116 {
117         if (ptr->type == NULL) {
118                 return false;
119         }
120         /* Set default values for returns. */
121         *id = (ID *)ptr->id.data;
122         *subdata = "";
123         *operation_code = DEG_OPCODE_OPERATION;
124         *operation_name = "";
125         *operation_name_tag = -1;
126         /* Handling of commonly known scenarios. */
127         if (ptr->type == &RNA_PoseBone) {
128                 bPoseChannel *pchan = (bPoseChannel *)ptr->data;
129                 if (prop != NULL && RNA_property_is_idprop(prop)) {
130                         *type = DEG_NODE_TYPE_PARAMETERS;
131                         *subdata = "";
132                         *operation_code = DEG_OPCODE_PARAMETERS_EVAL;
133                         *operation_name = pchan->name;
134                 }
135                 else {
136                         /* Bone - generally, we just want the bone component. */
137                         *type = DEG_NODE_TYPE_BONE;
138                         *subdata = pchan->name;
139                 }
140                 return true;
141         }
142         else if (ptr->type == &RNA_Bone) {
143                 Bone *bone = (Bone *)ptr->data;
144                 /* armature-level bone, but it ends up going to bone component anyway */
145                 // NOTE: the ID in thise case will end up being bArmature.
146                 *type = DEG_NODE_TYPE_BONE;
147                 *subdata = bone->name;
148                 return true;
149         }
150         else if (RNA_struct_is_a(ptr->type, &RNA_Constraint)) {
151                 Object *object = (Object *)ptr->id.data;
152                 bConstraint *con = (bConstraint *)ptr->data;
153                 /* Check whether is object or bone constraint. */
154                 /* NOTE: Currently none of the area can address transform of an object
155                  * at a given constraint, but for rigging one might use constraint
156                  * influence to be used to drive some corrective shape keys or so.
157                  */
158                 if (BLI_findindex(&object->constraints, con) != -1) {
159                         *type = DEG_NODE_TYPE_TRANSFORM;
160                         *operation_code = DEG_OPCODE_TRANSFORM_LOCAL;
161                         return true;
162                 }
163                 else if (object->pose != NULL) {
164                         LISTBASE_FOREACH(bPoseChannel *, pchan, &object->pose->chanbase) {
165                                 if (BLI_findindex(&pchan->constraints, con) != -1) {
166                                         *type = DEG_NODE_TYPE_BONE;
167                                         *operation_code = DEG_OPCODE_BONE_LOCAL;
168                                         *subdata = pchan->name;
169                                         return true;
170                                 }
171                         }
172                 }
173         }
174         else if (RNA_struct_is_a(ptr->type, &RNA_Modifier)) {
175                 *type = DEG_NODE_TYPE_GEOMETRY;
176                 return true;
177         }
178         else if (ptr->type == &RNA_Object) {
179                 /* Transforms props? */
180                 if (prop != NULL) {
181                         const char *prop_identifier = RNA_property_identifier((PropertyRNA *)prop);
182                         /* TODO(sergey): How to optimize this? */
183                         if (strstr(prop_identifier, "location") ||
184                             strstr(prop_identifier, "rotation") ||
185                             strstr(prop_identifier, "scale") ||
186                             strstr(prop_identifier, "matrix_"))
187                         {
188                                 *type = DEG_NODE_TYPE_TRANSFORM;
189                                 return true;
190                         }
191                         else if (strstr(prop_identifier, "data")) {
192                                 /* We access object.data, most likely a geometry.
193                                  * Might be a bone tho..
194                                  */
195                                 *type = DEG_NODE_TYPE_GEOMETRY;
196                                 return true;
197                         }
198                 }
199         }
200         else if (ptr->type == &RNA_ShapeKey) {
201                 *id = (ID *)ptr->id.data;
202                 *type = DEG_NODE_TYPE_GEOMETRY;
203                 return true;
204         }
205         else if (ptr->type == &RNA_Key) {
206                 *id = (ID *)ptr->id.data;
207                 *type = DEG_NODE_TYPE_GEOMETRY;
208                 return true;
209         }
210         else if (RNA_struct_is_a(ptr->type, &RNA_Sequence)) {
211                 Sequence *seq = (Sequence *)ptr->data;
212                 /* Sequencer strip */
213                 *type = DEG_NODE_TYPE_SEQUENCER;
214                 *subdata = seq->name; // xxx?
215                 return true;
216         }
217         else if (ptr->type == &RNA_Curve) {
218                 *id = (ID *)ptr->id.data;
219                 *type = DEG_NODE_TYPE_GEOMETRY;
220                 return true;
221         }
222         if (prop != NULL) {
223                 /* All unknown data effectively falls under "parameter evaluation". */
224                 if (RNA_property_is_idprop(prop)) {
225                         *type = DEG_NODE_TYPE_PARAMETERS;
226                         *operation_code = DEG_OPCODE_ID_PROPERTY;
227                         *operation_name = RNA_property_identifier((PropertyRNA *)prop);
228                         *operation_name_tag = -1;
229                 }
230                 else {
231                         *type = DEG_NODE_TYPE_PARAMETERS;
232                         *operation_code = DEG_OPCODE_PARAMETERS_EVAL;
233                         *operation_name = "";
234                         *operation_name_tag = -1;
235                 }
236                 return true;
237         }
238         return false;
239 }
240
241 /* Convenience wrapper to find node given just pointer + property. */
242 DepsNode *Depsgraph::find_node_from_pointer(const PointerRNA *ptr,
243                                             const PropertyRNA *prop) const
244 {
245         ID *id;
246         eDepsNode_Type node_type;
247         const char *component_name, *operation_name;
248         eDepsOperation_Code operation_code;
249         int operation_name_tag;
250
251         if (pointer_to_component_node_criteria(
252                          ptr, prop,
253                          &id, &node_type, &component_name,
254                          &operation_code, &operation_name, &operation_name_tag))
255         {
256                 IDDepsNode *id_node = find_id_node(id);
257                 if (id_node == NULL) {
258                         return NULL;
259                 }
260                 ComponentDepsNode *comp_node =
261                         id_node->find_component(node_type, component_name);
262                 if (comp_node == NULL) {
263                         return NULL;
264                 }
265                 if (operation_code == DEG_OPCODE_OPERATION) {
266                         return comp_node;
267                 }
268                 return comp_node->find_operation(operation_code,
269                                                  operation_name,
270                                                  operation_name_tag);
271         }
272         return NULL;
273 }
274
275 /* Node Management ---------------------------- */
276
277 static void id_node_deleter(void *value)
278 {
279         IDDepsNode *id_node = reinterpret_cast<IDDepsNode *>(value);
280         OBJECT_GUARDED_DELETE(id_node, IDDepsNode);
281 }
282
283 TimeSourceDepsNode *Depsgraph::add_time_source()
284 {
285         if (time_source == NULL) {
286                 DepsNodeFactory *factory = deg_type_get_factory(DEG_NODE_TYPE_TIMESOURCE);
287                 time_source = (TimeSourceDepsNode *)factory->create_node(NULL, "", "Time Source");
288         }
289         return time_source;
290 }
291
292 TimeSourceDepsNode *Depsgraph::find_time_source() const
293 {
294         return time_source;
295 }
296
297 IDDepsNode *Depsgraph::find_id_node(const ID *id) const
298 {
299         return reinterpret_cast<IDDepsNode *>(BLI_ghash_lookup(id_hash, id));
300 }
301
302 IDDepsNode *Depsgraph::add_id_node(ID *id, const char *name)
303 {
304         IDDepsNode *id_node = find_id_node(id);
305         if (!id_node) {
306                 DepsNodeFactory *factory = deg_type_get_factory(DEG_NODE_TYPE_ID_REF);
307                 id_node = (IDDepsNode *)factory->create_node(id, "", name);
308                 /* register */
309                 BLI_ghash_insert(id_hash, id, id_node);
310                 id_nodes.push_back(id_node);
311         }
312         return id_node;
313 }
314
315 void Depsgraph::clear_id_nodes()
316 {
317         BLI_ghash_clear(id_hash, NULL, id_node_deleter);
318         id_nodes.clear();
319 }
320
321 /* Add new relationship between two nodes. */
322 DepsRelation *Depsgraph::add_new_relation(OperationDepsNode *from,
323                                           OperationDepsNode *to,
324                                           const char *description,
325                                           bool check_unique)
326 {
327         DepsRelation *rel = NULL;
328         if (check_unique) {
329                 rel = check_nodes_connected(from, to, description);
330         }
331         if (rel != NULL) {
332                 return rel;
333         }
334         /* Create new relation, and add it to the graph. */
335         rel = OBJECT_GUARDED_NEW(DepsRelation, from, to, description);
336         /* TODO(sergey): Find a better place for this. */
337 #ifdef WITH_OPENSUBDIV
338         ComponentDepsNode *comp_node = from->owner;
339         if (comp_node->type == DEG_NODE_TYPE_GEOMETRY) {
340                 IDDepsNode *id_to = to->owner->owner;
341                 IDDepsNode *id_from = from->owner->owner;
342                 if (id_to != id_from && (id_to->id->recalc & ID_RECALC_ALL)) {
343                         if ((id_from->eval_flags & DAG_EVAL_NEED_CPU) == 0) {
344                                 id_from->tag_update(this);
345                                 id_from->eval_flags |= DAG_EVAL_NEED_CPU;
346                         }
347                 }
348         }
349 #endif
350         return rel;
351 }
352
353 /* Add new relation between two nodes */
354 DepsRelation *Depsgraph::add_new_relation(DepsNode *from, DepsNode *to,
355                                           const char *description,
356                                           bool check_unique)
357 {
358         DepsRelation *rel = NULL;
359         if (check_unique) {
360                 rel = check_nodes_connected(from, to, description);
361         }
362         if (rel != NULL) {
363                 return rel;
364         }
365         /* Create new relation, and add it to the graph. */
366         rel = OBJECT_GUARDED_NEW(DepsRelation, from, to, description);
367         return rel;
368 }
369
370 DepsRelation *Depsgraph::check_nodes_connected(const DepsNode *from,
371                                                const DepsNode *to,
372                                                const char *description)
373 {
374         foreach (DepsRelation *rel, from->outlinks) {
375                 BLI_assert(rel->from == from);
376                 if (rel->to != to) {
377                         continue;
378                 }
379                 if (description != NULL && !STREQ(rel->name, description)) {
380                         continue;
381                 }
382                 return rel;
383         }
384         return NULL;
385 }
386
387 /* ************************ */
388 /* Relationships Management */
389
390 DepsRelation::DepsRelation(DepsNode *from,
391                            DepsNode *to,
392                            const char *description)
393   : from(from),
394     to(to),
395     name(description),
396     flag(0)
397 {
398         /* Hook it up to the nodes which use it.
399          *
400          * NOTE: We register relation in the nodes which this link connects to here
401          * in constructor but we don't unregister it in the destructor.
402          *
403          * Reasoning:
404          *
405          * - Destructor is currently used on global graph destruction, so there's no
406          *   real need in avoiding dangling pointers, all the memory is to be freed
407          *   anyway.
408          *
409          * - Unregistering relation is not a cheap operation, so better to have it
410          *   as an explicit call if we need this.
411          */
412         from->outlinks.push_back(this);
413         to->inlinks.push_back(this);
414 }
415
416 DepsRelation::~DepsRelation()
417 {
418         /* Sanity check. */
419         BLI_assert(from != NULL && to != NULL);
420 }
421
422 void DepsRelation::unlink()
423 {
424         /* Sanity check. */
425         BLI_assert(from != NULL && to != NULL);
426         remove_from_vector(&from->outlinks, this);
427         remove_from_vector(&to->inlinks, this);
428 }
429
430 /* Low level tagging -------------------------------------- */
431
432 /* Tag a specific node as needing updates. */
433 void Depsgraph::add_entry_tag(OperationDepsNode *node)
434 {
435         /* Sanity check. */
436         if (!node)
437                 return;
438
439         /* Add to graph-level set of directly modified nodes to start searching from.
440          * NOTE: this is necessary since we have several thousand nodes to play with...
441          */
442         BLI_gset_insert(entry_tags, node);
443 }
444
445 void Depsgraph::clear_all_nodes()
446 {
447         clear_id_nodes();
448         if (time_source != NULL) {
449                 OBJECT_GUARDED_DELETE(time_source, TimeSourceDepsNode);
450                 time_source = NULL;
451         }
452 }
453
454 void deg_editors_id_update(Main *bmain, ID *id)
455 {
456         if (deg_editor_update_id_cb != NULL) {
457                 deg_editor_update_id_cb(bmain, id);
458         }
459 }
460
461 void deg_editors_scene_update(Main *bmain, Scene *scene, bool updated)
462 {
463         if (deg_editor_update_scene_cb != NULL) {
464                 deg_editor_update_scene_cb(bmain, scene, updated);
465         }
466 }
467
468 bool deg_terminal_do_color(void)
469 {
470         return (G.debug & G_DEBUG_DEPSGRAPH_PRETTY) != 0;
471 }
472
473 string deg_color_for_pointer(const void *pointer)
474 {
475         if (!deg_terminal_do_color()) {
476                 return "";
477         }
478         int r, g, b;
479         BLI_hash_pointer_to_color(pointer, &r, &g, &b);
480         char buffer[64];
481         BLI_snprintf(buffer, sizeof(buffer), TRUECOLOR_ANSI_COLOR_FORMAT, r, g, b);
482         return string(buffer);
483 }
484
485 string deg_color_end(void)
486 {
487         if (!deg_terminal_do_color()) {
488                 return "";
489         }
490         return string(TRUECOLOR_ANSI_COLOR_FINISH);
491 }
492
493 }  // namespace DEG
494
495 /* **************** */
496 /* Public Graph API */
497
498 /* Initialize a new Depsgraph */
499 Depsgraph *DEG_graph_new()
500 {
501         DEG::Depsgraph *deg_depsgraph = OBJECT_GUARDED_NEW(DEG::Depsgraph);
502         return reinterpret_cast<Depsgraph *>(deg_depsgraph);
503 }
504
505 /* Free graph's contents and graph itself */
506 void DEG_graph_free(Depsgraph *graph)
507 {
508         using DEG::Depsgraph;
509         DEG::Depsgraph *deg_depsgraph = reinterpret_cast<DEG::Depsgraph *>(graph);
510         OBJECT_GUARDED_DELETE(deg_depsgraph, Depsgraph);
511 }
512
513 /* Set callbacks which are being called when depsgraph changes. */
514 void DEG_editors_set_update_cb(DEG_EditorUpdateIDCb id_func,
515                                DEG_EditorUpdateSceneCb scene_func,
516                                DEG_EditorUpdateScenePreCb scene_pre_func)
517 {
518         DEG::deg_editor_update_id_cb = id_func;
519         DEG::deg_editor_update_scene_cb = scene_func;
520         DEG::deg_editor_update_scene_pre_cb = scene_pre_func;
521 }
522
523 void DEG_editors_update_pre(Main *bmain, Scene *scene, bool time)
524 {
525         if (DEG::deg_editor_update_scene_pre_cb != NULL) {
526                 DEG::deg_editor_update_scene_pre_cb(bmain, scene, time);
527         }
528 }
529
530 /* Evaluation and debug */
531
532 void DEG_debug_print_eval(const char *function_name,
533                           const char *object_name,
534                           const void *object_address)
535 {
536         if ((G.debug & G_DEBUG_DEPSGRAPH_EVAL) == 0) {
537                 return;
538         }
539         fprintf(stdout,
540                 "%s on %s %s(%p)%s\n",
541                 function_name,
542                 object_name,
543                 DEG::deg_color_for_pointer(object_address).c_str(),
544                 object_address,
545                 DEG::deg_color_end().c_str());
546         fflush(stdout);
547 }
548
549 void DEG_debug_print_eval_subdata(const char *function_name,
550                                   const char *object_name,
551                                   const void *object_address,
552                                   const char *subdata_comment,
553                                   const char *subdata_name,
554                                   const void *subdata_address)
555 {
556         if ((G.debug & G_DEBUG_DEPSGRAPH_EVAL) == 0) {
557                 return;
558         }
559         fprintf(stdout,
560                 "%s on %s %s(%p)%s %s %s %s(%p)%s\n",
561                 function_name,
562                 object_name,
563                 DEG::deg_color_for_pointer(object_address).c_str(),
564                 object_address,
565                 DEG::deg_color_end().c_str(),
566                 subdata_comment,
567                 subdata_name,
568                 DEG::deg_color_for_pointer(subdata_address).c_str(),
569                 subdata_address,
570                 DEG::deg_color_end().c_str());
571         fflush(stdout);
572 }
573
574 void DEG_debug_print_eval_subdata_index(const char *function_name,
575                                         const char *object_name,
576                                         const void *object_address,
577                                         const char *subdata_comment,
578                                         const char *subdata_name,
579                                         const void *subdata_address,
580                                         const int subdata_index)
581 {
582         if ((G.debug & G_DEBUG_DEPSGRAPH_EVAL) == 0) {
583                 return;
584         }
585         fprintf(stdout,
586                 "%s on %s %s(%p)^%s %s %s[%d] %s(%p)%s\n",
587                 function_name,
588                 object_name,
589                 DEG::deg_color_for_pointer(object_address).c_str(),
590                 object_address,
591                 DEG::deg_color_end().c_str(),
592                 subdata_comment,
593                 subdata_name,
594                 subdata_index,
595                 DEG::deg_color_for_pointer(subdata_address).c_str(),
596                 subdata_address,
597                 DEG::deg_color_end().c_str());
598         fflush(stdout);
599 }
600
601 void DEG_debug_print_eval_time(const char *function_name,
602                                const char *object_name,
603                                const void *object_address,
604                                float time)
605 {
606         if ((G.debug & G_DEBUG_DEPSGRAPH_EVAL) == 0) {
607                 return;
608         }
609         fprintf(stdout,
610                 "%s on %s %s(%p)%s at time %f\n",
611                 function_name,
612                 object_name,
613                 DEG::deg_color_for_pointer(object_address).c_str(),
614                 object_address,
615                 DEG::deg_color_end().c_str(),
616                 time);
617         fflush(stdout);
618 }