Depsgraph: Replace LIB_TAG_DOIT with hash lookup
[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                 *type = DEG_NODE_TYPE_PARAMETERS;
218                 *operation_code = DEG_OPCODE_PARAMETERS_EVAL;
219                 *operation_name = "";
220                 *operation_name_tag = -1;
221                 return true;
222         }
223         return false;
224 }
225
226 /* Convenience wrapper to find node given just pointer + property. */
227 DepsNode *Depsgraph::find_node_from_pointer(const PointerRNA *ptr,
228                                             const PropertyRNA *prop) const
229 {
230         ID *id;
231         eDepsNode_Type node_type;
232         const char *component_name, *operation_name;
233         eDepsOperation_Code operation_code;
234         int operation_name_tag;
235
236         if (pointer_to_component_node_criteria(
237                          ptr, prop,
238                          &id, &node_type, &component_name,
239                          &operation_code, &operation_name, &operation_name_tag))
240         {
241                 IDDepsNode *id_node = find_id_node(id);
242                 if (id_node == NULL) {
243                         return NULL;
244                 }
245                 ComponentDepsNode *comp_node =
246                         id_node->find_component(node_type, component_name);
247                 if (comp_node == NULL) {
248                         return NULL;
249                 }
250                 if (operation_code == DEG_OPCODE_OPERATION) {
251                         return comp_node;
252                 }
253                 return comp_node->find_operation(operation_code,
254                                                  operation_name,
255                                                  operation_name_tag);
256         }
257         return NULL;
258 }
259
260 /* Node Management ---------------------------- */
261
262 static void id_node_deleter(void *value)
263 {
264         IDDepsNode *id_node = reinterpret_cast<IDDepsNode *>(value);
265         OBJECT_GUARDED_DELETE(id_node, IDDepsNode);
266 }
267
268 TimeSourceDepsNode *Depsgraph::add_time_source()
269 {
270         if (time_source == NULL) {
271                 DepsNodeFactory *factory = deg_type_get_factory(DEG_NODE_TYPE_TIMESOURCE);
272                 time_source = (TimeSourceDepsNode *)factory->create_node(NULL, "", "Time Source");
273         }
274         return time_source;
275 }
276
277 TimeSourceDepsNode *Depsgraph::find_time_source() const
278 {
279         return time_source;
280 }
281
282 IDDepsNode *Depsgraph::find_id_node(const ID *id) const
283 {
284         return reinterpret_cast<IDDepsNode *>(BLI_ghash_lookup(id_hash, id));
285 }
286
287 IDDepsNode *Depsgraph::add_id_node(ID *id, const char *name)
288 {
289         IDDepsNode *id_node = find_id_node(id);
290         if (!id_node) {
291                 DepsNodeFactory *factory = deg_type_get_factory(DEG_NODE_TYPE_ID_REF);
292                 id_node = (IDDepsNode *)factory->create_node(id, "", name);
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 }