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