30146544b76f40270a5e13a17abe8e0c2b9f85f3
[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 <cstring>
55
56 #include "DEG_depsgraph.h"
57
58 #include "intern/nodes/deg_node.h"
59 #include "intern/nodes/deg_node_component.h"
60 #include "intern/nodes/deg_node_operation.h"
61
62 #include "intern/depsgraph_intern.h"
63 #include "util/deg_util_foreach.h"
64
65 namespace DEG {
66
67 static DEG_EditorUpdateIDCb deg_editor_update_id_cb = NULL;
68 static DEG_EditorUpdateSceneCb deg_editor_update_scene_cb = NULL;
69 static DEG_EditorUpdateScenePreCb deg_editor_update_scene_pre_cb = NULL;
70
71 Depsgraph::Depsgraph()
72   : root_node(NULL),
73     need_update(false),
74     layers(0)
75 {
76         BLI_spin_init(&lock);
77         id_hash = BLI_ghash_ptr_new("Depsgraph id hash");
78         entry_tags = BLI_gset_ptr_new("Depsgraph entry_tags");
79 }
80
81 Depsgraph::~Depsgraph()
82 {
83         /* Free root node - it won't have been freed yet... */
84         clear_id_nodes();
85         BLI_ghash_free(id_hash, NULL, NULL);
86         BLI_gset_free(entry_tags, NULL);
87         if (this->root_node != NULL) {
88                 OBJECT_GUARDED_DELETE(this->root_node, RootDepsNode);
89         }
90         BLI_spin_end(&lock);
91 }
92
93 /* Query Conditions from RNA ----------------------- */
94
95 static bool pointer_to_id_node_criteria(const PointerRNA *ptr,
96                                         const PropertyRNA *prop,
97                                         ID **id)
98 {
99         if (!ptr->type)
100                 return false;
101
102         if (!prop) {
103                 if (RNA_struct_is_ID(ptr->type)) {
104                         *id = (ID *)ptr->data;
105                         return true;
106                 }
107         }
108
109         return false;
110 }
111
112 static bool pointer_to_component_node_criteria(const PointerRNA *ptr,
113                                                const PropertyRNA *prop,
114                                                ID **id,
115                                                eDepsNode_Type *type,
116                                                const char **subdata)
117 {
118         if (!ptr->type)
119                 return false;
120
121         /* Set default values for returns. */
122         *id      = (ID *)ptr->id.data;  /* For obvious reasons... */
123         *subdata = "";                 /* Default to no subdata (e.g. bone) name
124                                         * lookup in most cases. */
125
126         /* Handling of commonly known scenarios... */
127         if (ptr->type == &RNA_PoseBone) {
128                 bPoseChannel *pchan = (bPoseChannel *)ptr->data;
129
130                 /* Bone - generally, we just want the bone component... */
131                 *type = DEPSNODE_TYPE_BONE;
132                 *subdata = pchan->name;
133
134                 return true;
135         }
136         else if (ptr->type == &RNA_Bone) {
137                 Bone *bone = (Bone *)ptr->data;
138
139                 /* armature-level bone, but it ends up going to bone component anyway */
140                 // TODO: the ID in thise case will end up being bArmature, not Object as needed!
141                 *type = DEPSNODE_TYPE_BONE;
142                 *subdata = bone->name;
143                 //*id = ...
144
145                 return true;
146         }
147         else if (RNA_struct_is_a(ptr->type, &RNA_Constraint)) {
148                 Object *ob = (Object *)ptr->id.data;
149                 bConstraint *con = (bConstraint *)ptr->data;
150
151                 /* object or bone? */
152                 if (BLI_findindex(&ob->constraints, con) != -1) {
153                         /* object transform */
154                         // XXX: for now, we can't address the specific constraint or the constraint stack...
155                         *type = DEPSNODE_TYPE_TRANSFORM;
156                         return true;
157                 }
158                 else if (ob->pose) {
159                         bPoseChannel *pchan;
160                         for (pchan = (bPoseChannel *)ob->pose->chanbase.first; pchan; pchan = pchan->next) {
161                                 if (BLI_findindex(&pchan->constraints, con) != -1) {
162                                         /* bone transforms */
163                                         *type = DEPSNODE_TYPE_BONE;
164                                         *subdata = pchan->name;
165                                         return true;
166                                 }
167                         }
168                 }
169         }
170         else if (RNA_struct_is_a(ptr->type, &RNA_Modifier)) {
171                 //ModifierData *md = (ModifierData *)ptr->data;
172
173                 /* Modifier */
174                 /* NOTE: subdata is not the same as "operation name",
175                  * so although we have unique ops for modifiers,
176                  * we can't lump them together
177                  */
178                 *type = DEPSNODE_TYPE_BONE;
179                 //*subdata = md->name;
180
181                 return true;
182         }
183         else if (ptr->type == &RNA_Object) {
184                 //Object *ob = (Object *)ptr->data;
185
186                 /* Transforms props? */
187                 if (prop) {
188                         const char *prop_identifier = RNA_property_identifier((PropertyRNA *)prop);
189                         /* TODO(sergey): How to optimize this? */
190                         if (strstr(prop_identifier, "location") ||
191                             strstr(prop_identifier, "rotation") ||
192                             strstr(prop_identifier, "scale") ||
193                             strstr(prop_identifier, "matrix_"))
194                         {
195                                 *type = DEPSNODE_TYPE_TRANSFORM;
196                                 return true;
197                         }
198                         else if (strstr(prop_identifier, "data")) {
199                                 /* We access object.data, most likely a geometry.
200                                  * Might be a bone tho..
201                                  */
202                                 *type = DEPSNODE_TYPE_GEOMETRY;
203                                 return true;
204                         }
205                 }
206         }
207         else if (ptr->type == &RNA_ShapeKey) {
208                 Key *key = (Key *)ptr->id.data;
209
210                 /* ShapeKeys are currently handled as geometry on the geometry that owns it */
211                 *id = key->from; // XXX
212                 *type = DEPSNODE_TYPE_PARAMETERS;
213
214                 return true;
215         }
216         else if (RNA_struct_is_a(ptr->type, &RNA_Sequence)) {
217                 Sequence *seq = (Sequence *)ptr->data;
218                 /* Sequencer strip */
219                 *type = DEPSNODE_TYPE_SEQUENCER;
220                 *subdata = seq->name; // xxx?
221                 return true;
222         }
223
224         if (prop) {
225                 /* All unknown data effectively falls under "parameter evaluation" */
226                 *type = DEPSNODE_TYPE_PARAMETERS;
227                 return true;
228         }
229
230         return false;
231 }
232
233 /* Convenience wrapper to find node given just pointer + property. */
234 DepsNode *Depsgraph::find_node_from_pointer(const PointerRNA *ptr,
235                                             const PropertyRNA *prop) const
236 {
237         ID *id;
238         eDepsNode_Type type;
239         const char *name;
240
241         /* Get querying conditions. */
242         if (pointer_to_id_node_criteria(ptr, prop, &id)) {
243                 return find_id_node(id);
244         }
245         else if (pointer_to_component_node_criteria(ptr, prop, &id, &type, &name)) {
246                 IDDepsNode *id_node = find_id_node(id);
247                 if (id_node != NULL) {
248                         return id_node->find_component(type, name);
249                 }
250         }
251
252         return NULL;
253 }
254
255 /* Node Management ---------------------------- */
256
257 static void id_node_deleter(void *value)
258 {
259         IDDepsNode *id_node = reinterpret_cast<IDDepsNode *>(value);
260         OBJECT_GUARDED_DELETE(id_node, IDDepsNode);
261 }
262
263 RootDepsNode *Depsgraph::add_root_node()
264 {
265         if (!root_node) {
266                 DepsNodeFactory *factory = deg_get_node_factory(DEPSNODE_TYPE_ROOT);
267                 root_node = (RootDepsNode *)factory->create_node(NULL, "", "Root (Scene)");
268         }
269         return root_node;
270 }
271
272 TimeSourceDepsNode *Depsgraph::find_time_source() const
273 {
274         BLI_assert(root_node != NULL);
275         return root_node->time_source;
276 }
277
278 IDDepsNode *Depsgraph::find_id_node(const ID *id) const
279 {
280         return reinterpret_cast<IDDepsNode *>(BLI_ghash_lookup(id_hash, id));
281 }
282
283 IDDepsNode *Depsgraph::add_id_node(ID *id, const char *name)
284 {
285         IDDepsNode *id_node = find_id_node(id);
286         if (!id_node) {
287                 DepsNodeFactory *factory = deg_get_node_factory(DEPSNODE_TYPE_ID_REF);
288                 id_node = (IDDepsNode *)factory->create_node(id, "", name);
289                 id->tag |= LIB_TAG_DOIT;
290                 /* register */
291                 BLI_ghash_insert(id_hash, id, id_node);
292         }
293         return id_node;
294 }
295
296 void Depsgraph::remove_id_node(const ID *id)
297 {
298         IDDepsNode *id_node = find_id_node(id);
299         if (id_node) {
300                 /* unregister */
301                 BLI_ghash_remove(id_hash, id, NULL, NULL);
302                 OBJECT_GUARDED_DELETE(id_node, IDDepsNode);
303         }
304 }
305
306 void Depsgraph::clear_id_nodes()
307 {
308         BLI_ghash_clear(id_hash, NULL, id_node_deleter);
309 }
310
311 /* Add new relationship between two nodes. */
312 DepsRelation *Depsgraph::add_new_relation(OperationDepsNode *from,
313                                           OperationDepsNode *to,
314                                           const char *description)
315 {
316         /* Create new relation, and add it to the graph. */
317         DepsRelation *rel = OBJECT_GUARDED_NEW(DepsRelation, from, to, description);
318         /* TODO(sergey): Find a better place for this. */
319 #ifdef WITH_OPENSUBDIV
320         ComponentDepsNode *comp_node = from->owner;
321         if (comp_node->type == DEPSNODE_TYPE_GEOMETRY) {
322                 IDDepsNode *id_to = to->owner->owner;
323                 IDDepsNode *id_from = from->owner->owner;
324                 if (id_to != id_from && (id_to->id->tag & LIB_TAG_ID_RECALC_ALL)) {
325                         if ((id_from->eval_flags & DAG_EVAL_NEED_CPU) == 0) {
326                                 id_from->tag_update(this);
327                                 id_from->eval_flags |= DAG_EVAL_NEED_CPU;
328                         }
329                 }
330         }
331 #endif
332         return rel;
333 }
334
335 /* Add new relation between two nodes */
336 DepsRelation *Depsgraph::add_new_relation(DepsNode *from, DepsNode *to,
337                                           const char *description)
338 {
339         /* Create new relation, and add it to the graph. */
340         DepsRelation *rel = OBJECT_GUARDED_NEW(DepsRelation, from, to, description);
341         return rel;
342 }
343
344 /* ************************ */
345 /* Relationships Management */
346
347 DepsRelation::DepsRelation(DepsNode *from,
348                            DepsNode *to,
349                            const char *description)
350   : from(from),
351     to(to),
352     name(description),
353     flag(0)
354 {
355 #ifndef NDEBUG
356 /*
357         for (OperationDepsNode::Relations::const_iterator it = from->outlinks.begin();
358              it != from->outlinks.end();
359              ++it)
360         {
361                 DepsRelation *rel = *it;
362                 if (rel->from == from &&
363                     rel->to == to &&
364                     rel->type == type &&
365                     rel->name == description)
366                 {
367                         BLI_assert(!"Duplicated relation, should not happen!");
368                 }
369         }
370 */
371 #endif
372
373         /* Hook it up to the nodes which use it.
374          *
375          * NOTE: We register relation in the nodes which this link connects to here
376          * in constructor but we don't unregister it in the destructor.
377          *
378          * Reasoning:
379          *
380          * - Destructor is currently used on global graph destruction, so there's no
381          *   real need in avoiding dangling pointers, all the memory is to be freed
382          *   anyway.
383          *
384          * - Unregistering relation is not a cheap operation, so better to have it
385          *   as an explicit call if we need this.
386          */
387         from->outlinks.push_back(this);
388         to->inlinks.push_back(this);
389 }
390
391 DepsRelation::~DepsRelation()
392 {
393         /* Sanity check. */
394         BLI_assert(this->from && this->to);
395 }
396
397 /* Low level tagging -------------------------------------- */
398
399 /* Tag a specific node as needing updates. */
400 void Depsgraph::add_entry_tag(OperationDepsNode *node)
401 {
402         /* Sanity check. */
403         if (!node)
404                 return;
405
406         /* Add to graph-level set of directly modified nodes to start searching from.
407          * NOTE: this is necessary since we have several thousand nodes to play with...
408          */
409         BLI_gset_insert(entry_tags, node);
410 }
411
412 void Depsgraph::clear_all_nodes()
413 {
414         clear_id_nodes();
415         BLI_ghash_clear(id_hash, NULL, NULL);
416         if (this->root_node) {
417                 OBJECT_GUARDED_DELETE(this->root_node, RootDepsNode);
418                 root_node = NULL;
419         }
420 }
421
422 void deg_editors_id_update(Main *bmain, ID *id)
423 {
424         if (deg_editor_update_id_cb != NULL) {
425                 deg_editor_update_id_cb(bmain, id);
426         }
427 }
428
429 void deg_editors_scene_update(Main *bmain, Scene *scene, bool updated)
430 {
431         if (deg_editor_update_scene_cb != NULL) {
432                 deg_editor_update_scene_cb(bmain, scene, updated);
433         }
434 }
435
436 }  // namespace DEG
437
438 /* **************** */
439 /* Public Graph API */
440
441 /* Initialize a new Depsgraph */
442 Depsgraph *DEG_graph_new()
443 {
444         DEG::Depsgraph *deg_depsgraph = OBJECT_GUARDED_NEW(DEG::Depsgraph);
445         return reinterpret_cast<Depsgraph *>(deg_depsgraph);
446 }
447
448 /* Free graph's contents and graph itself */
449 void DEG_graph_free(Depsgraph *graph)
450 {
451         using DEG::Depsgraph;
452         DEG::Depsgraph *deg_depsgraph = reinterpret_cast<DEG::Depsgraph *>(graph);
453         OBJECT_GUARDED_DELETE(deg_depsgraph, Depsgraph);
454 }
455
456 /* Set callbacks which are being called when depsgraph changes. */
457 void DEG_editors_set_update_cb(DEG_EditorUpdateIDCb id_func,
458                                DEG_EditorUpdateSceneCb scene_func,
459                                DEG_EditorUpdateScenePreCb scene_pre_func)
460 {
461         DEG::deg_editor_update_id_cb = id_func;
462         DEG::deg_editor_update_scene_cb = scene_func;
463         DEG::deg_editor_update_scene_pre_cb = scene_pre_func;
464 }
465
466 void DEG_editors_update_pre(Main *bmain, Scene *scene, bool time)
467 {
468         if (DEG::deg_editor_update_scene_pre_cb != NULL) {
469                 DEG::deg_editor_update_scene_pre_cb(bmain, scene, time);
470         }
471 }