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