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 <algorithm>
53 #include <cstring>
54
55 #include "DEG_depsgraph.h"
56
57 #include "intern/eval/deg_eval_copy_on_write.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 static bool use_copy_on_write = false;
69
70 bool DEG_depsgraph_use_copy_on_write(void)
71 {
72         return use_copy_on_write;
73 }
74
75 void DEG_depsgraph_enable_copy_on_write(void)
76 {
77         use_copy_on_write = true;
78 }
79
80 namespace DEG {
81
82 static DEG_EditorUpdateIDCb deg_editor_update_id_cb = NULL;
83 static DEG_EditorUpdateSceneCb deg_editor_update_scene_cb = NULL;
84
85 /* TODO(sergey): Find a better place for this. */
86 template <typename T>
87 static void remove_from_vector(vector<T> *vector, const T& value)
88 {
89         vector->erase(std::remove(vector->begin(), vector->end(), value),
90                       vector->end());
91 }
92
93 Depsgraph::Depsgraph()
94   : time_source(NULL),
95     need_update(true),
96     scene(NULL),
97     view_layer(NULL)
98 {
99         BLI_spin_init(&lock);
100         id_hash = BLI_ghash_ptr_new("Depsgraph id hash");
101         entry_tags = BLI_gset_ptr_new("Depsgraph entry_tags");
102 }
103
104 Depsgraph::~Depsgraph()
105 {
106         clear_id_nodes();
107         BLI_ghash_free(id_hash, NULL, NULL);
108         BLI_gset_free(entry_tags, NULL);
109         if (time_source != NULL) {
110                 OBJECT_GUARDED_DELETE(time_source, TimeSourceDepsNode);
111         }
112         BLI_spin_end(&lock);
113 }
114
115 /* Query Conditions from RNA ----------------------- */
116
117 static bool pointer_to_component_node_criteria(
118         const PointerRNA *ptr,
119         const PropertyRNA *prop,
120         ID **id,
121         eDepsNode_Type *type,
122         const char **subdata,
123         eDepsOperation_Code *operation_code,
124         const char **operation_name,
125         int *operation_name_tag)
126 {
127         if (ptr->type == NULL) {
128                 return false;
129         }
130         /* Set default values for returns. */
131         *id = (ID *)ptr->id.data;
132         *subdata = "";
133         *operation_code = DEG_OPCODE_OPERATION;
134         *operation_name = "";
135         *operation_name_tag = -1;
136         /* Handling of commonly known scenarios. */
137         if (ptr->type == &RNA_PoseBone) {
138                 bPoseChannel *pchan = (bPoseChannel *)ptr->data;
139                 if (prop != NULL && RNA_property_is_idprop(prop)) {
140                         *type = DEG_NODE_TYPE_PARAMETERS;
141                         *subdata = "";
142                         *operation_code = DEG_OPCODE_PARAMETERS_EVAL;
143                         *operation_name = pchan->name;;
144                 }
145                 else {
146                         /* Bone - generally, we just want the bone component. */
147                         *type = DEG_NODE_TYPE_BONE;
148                         *subdata = pchan->name;
149                 }
150                 return true;
151         }
152         else if (ptr->type == &RNA_Bone) {
153                 Bone *bone = (Bone *)ptr->data;
154                 /* armature-level bone, but it ends up going to bone component anyway */
155                 // NOTE: the ID in thise case will end up being bArmature.
156                 *type = DEG_NODE_TYPE_BONE;
157                 *subdata = bone->name;
158                 return true;
159         }
160         else if (RNA_struct_is_a(ptr->type, &RNA_Constraint)) {
161                 Object *object = (Object *)ptr->id.data;
162                 bConstraint *con = (bConstraint *)ptr->data;
163                 /* Check whether is object or bone constraint. */
164                 if (BLI_findindex(&object->constraints, con) != -1) {
165                         /* Constraint is defining object transform. */
166                         *type = DEG_NODE_TYPE_TRANSFORM;
167                         return true;
168                 }
169                 else if (object->pose != NULL) {
170                         LINKLIST_FOREACH(bPoseChannel *, pchan, &object->pose->chanbase) {
171                                 if (BLI_findindex(&pchan->constraints, con) != -1) {
172                                         /* bone transforms */
173                                         *type = DEG_NODE_TYPE_BONE;
174                                         *subdata = pchan->name;
175                                         return true;
176                                 }
177                         }
178                 }
179         }
180         else if (RNA_struct_is_a(ptr->type, &RNA_Modifier)) {
181                 *type = DEG_NODE_TYPE_GEOMETRY;
182                 return true;
183         }
184         else if (ptr->type == &RNA_Object) {
185                 /* Transforms props? */
186                 if (prop != NULL) {
187                         const char *prop_identifier = RNA_property_identifier((PropertyRNA *)prop);
188                         /* TODO(sergey): How to optimize this? */
189                         if (strstr(prop_identifier, "location") ||
190                             strstr(prop_identifier, "rotation") ||
191                             strstr(prop_identifier, "scale") ||
192                             strstr(prop_identifier, "matrix_"))
193                         {
194                                 *type = DEG_NODE_TYPE_TRANSFORM;
195                                 return true;
196                         }
197                         else if (strstr(prop_identifier, "data")) {
198                                 /* We access object.data, most likely a geometry.
199                                  * Might be a bone tho..
200                                  */
201                                 *type = DEG_NODE_TYPE_GEOMETRY;
202                                 return true;
203                         }
204                 }
205         }
206         else if (ptr->type == &RNA_ShapeKey) {
207                 *id = (ID *)ptr->id.data;
208                 *type = DEG_NODE_TYPE_GEOMETRY;
209                 return true;
210         }
211         else if (ptr->type == &RNA_Key) {
212                 *id = (ID *)ptr->id.data;
213                 *type = DEG_NODE_TYPE_GEOMETRY;
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 = DEG_NODE_TYPE_SEQUENCER;
220                 *subdata = seq->name; // xxx?
221                 return true;
222         }
223         if (prop != NULL) {
224                 /* All unknown data effectively falls under "parameter evaluation". */
225                 *type = DEG_NODE_TYPE_PARAMETERS;
226                 *operation_code = DEG_OPCODE_PARAMETERS_EVAL;
227                 *operation_name = "";
228                 *operation_name_tag = -1;
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 TimeSourceDepsNode *Depsgraph::add_time_source()
271 {
272         if (time_source == NULL) {
273                 DepsNodeFactory *factory = deg_type_get_factory(DEG_NODE_TYPE_TIMESOURCE);
274                 time_source = (TimeSourceDepsNode *)factory->create_node(NULL, "", "Time Source");
275         }
276         return time_source;
277 }
278
279 TimeSourceDepsNode *Depsgraph::find_time_source() const
280 {
281         return time_source;
282 }
283
284 IDDepsNode *Depsgraph::find_id_node(const ID *id) const
285 {
286         return reinterpret_cast<IDDepsNode *>(BLI_ghash_lookup(id_hash, id));
287 }
288
289 IDDepsNode *Depsgraph::add_id_node(ID *id, bool do_tag, ID *id_cow_hint)
290 {
291         BLI_assert((id->tag & LIB_TAG_COPY_ON_WRITE) == 0);
292         IDDepsNode *id_node = find_id_node(id);
293         if (!id_node) {
294                 DepsNodeFactory *factory = deg_type_get_factory(DEG_NODE_TYPE_ID_REF);
295                 id_node = (IDDepsNode *)factory->create_node(id, "", id->name);
296                 id_node->init_copy_on_write(id_cow_hint);
297                 if (do_tag) {
298                         id->tag |= LIB_TAG_DOIT;
299                 }
300                 /* Register node in ID hash.
301                  *
302                  * NOTE: We address ID nodes by the original ID pointer they are
303                  * referencing to.
304                  */
305                 BLI_ghash_insert(id_hash, id, id_node);
306                 id_nodes.push_back(id_node);
307         }
308         else if (do_tag) {
309                 id->tag |= LIB_TAG_DOIT;
310         }
311         return id_node;
312 }
313
314 void Depsgraph::clear_id_nodes()
315 {
316         /* Free memory used by ID nodes. */
317         if (use_copy_on_write) {
318                 /* Stupid workaround to ensure we free IDs in a proper order. */
319                 foreach (IDDepsNode *id_node, id_nodes) {
320                         if (id_node->id_cow == NULL) {
321                                 /* This means builder "stole" ownership of the copy-on-written
322                                  * datablock for her own dirty needs.
323                                  */
324                                 continue;
325                         }
326                         if (!deg_copy_on_write_is_expanded(id_node->id_cow)) {
327                                 continue;
328                         }
329                         const ID_Type id_type = GS(id_node->id_cow->name);
330                         if (id_type != ID_PA) {
331                                 id_node->destroy();
332                         }
333                 }
334         }
335         foreach (IDDepsNode *id_node, id_nodes) {
336                 OBJECT_GUARDED_DELETE(id_node, IDDepsNode);
337         }
338         /* Clear containers. */
339         BLI_ghash_clear(id_hash, NULL, NULL);
340         id_nodes.clear();
341 }
342
343 /* Add new relationship between two nodes. */
344 DepsRelation *Depsgraph::add_new_relation(OperationDepsNode *from,
345                                           OperationDepsNode *to,
346                                           const char *description,
347                                           bool check_unique)
348 {
349         DepsRelation *rel = NULL;
350         if (check_unique) {
351                 rel = check_nodes_connected(from, to, description);
352         }
353         if (rel != NULL) {
354                 return rel;
355         }
356         /* Create new relation, and add it to the graph. */
357         rel = OBJECT_GUARDED_NEW(DepsRelation, from, to, description);
358         /* TODO(sergey): Find a better place for this. */
359 #ifdef WITH_OPENSUBDIV
360         ComponentDepsNode *comp_node = from->owner;
361         if (comp_node->type == DEG_NODE_TYPE_GEOMETRY) {
362                 IDDepsNode *id_to = to->owner->owner;
363                 IDDepsNode *id_from = from->owner->owner;
364                 if (id_to != id_from && (id_to->id_orig->recalc & ID_RECALC_ALL)) {
365                         if ((id_from->eval_flags & DAG_EVAL_NEED_CPU) == 0) {
366                                 id_from->tag_update(this);
367                                 id_from->eval_flags |= DAG_EVAL_NEED_CPU;
368                         }
369                 }
370         }
371 #endif
372         return rel;
373 }
374
375 /* Add new relation between two nodes */
376 DepsRelation *Depsgraph::add_new_relation(DepsNode *from, DepsNode *to,
377                                           const char *description,
378                                           bool check_unique)
379 {
380         DepsRelation *rel = NULL;
381         if (check_unique) {
382                 rel = check_nodes_connected(from, to, description);
383         }
384         if (rel != NULL) {
385                 return rel;
386         }
387         /* Create new relation, and add it to the graph. */
388         rel = OBJECT_GUARDED_NEW(DepsRelation, from, to, description);
389         return rel;
390 }
391
392 DepsRelation *Depsgraph::check_nodes_connected(const DepsNode *from,
393                                                const DepsNode *to,
394                                                const char *description)
395 {
396         foreach (DepsRelation *rel, from->outlinks) {
397                 BLI_assert(rel->from == from);
398                 if (rel->to != to) {
399                         continue;
400                 }
401                 if (description != NULL && !STREQ(rel->name, description)) {
402                         continue;
403                 }
404                 return rel;
405         }
406         return NULL;
407 }
408
409 /* ************************ */
410 /* Relationships Management */
411
412 DepsRelation::DepsRelation(DepsNode *from,
413                            DepsNode *to,
414                            const char *description)
415   : from(from),
416     to(to),
417     name(description),
418     flag(0)
419 {
420         /* Hook it up to the nodes which use it.
421          *
422          * NOTE: We register relation in the nodes which this link connects to here
423          * in constructor but we don't unregister it in the destructor.
424          *
425          * Reasoning:
426          *
427          * - Destructor is currently used on global graph destruction, so there's no
428          *   real need in avoiding dangling pointers, all the memory is to be freed
429          *   anyway.
430          *
431          * - Unregistering relation is not a cheap operation, so better to have it
432          *   as an explicit call if we need this.
433          */
434         from->outlinks.push_back(this);
435         to->inlinks.push_back(this);
436 }
437
438 DepsRelation::~DepsRelation()
439 {
440         /* Sanity check. */
441         BLI_assert(from != NULL && to != NULL);
442 }
443
444 void DepsRelation::unlink()
445 {
446         /* Sanity check. */
447         BLI_assert(from != NULL && to != NULL);
448         remove_from_vector(&from->outlinks, this);
449         remove_from_vector(&to->inlinks, this);
450 }
451
452 /* Low level tagging -------------------------------------- */
453
454 /* Tag a specific node as needing updates. */
455 void Depsgraph::add_entry_tag(OperationDepsNode *node)
456 {
457         /* Sanity check. */
458         if (node == NULL) {
459                 return;
460         }
461         /* Add to graph-level set of directly modified nodes to start searching from.
462          * NOTE: this is necessary since we have several thousand nodes to play with...
463          */
464         BLI_gset_insert(entry_tags, node);
465 }
466
467 void Depsgraph::clear_all_nodes()
468 {
469         clear_id_nodes();
470         if (time_source != NULL) {
471                 OBJECT_GUARDED_DELETE(time_source, TimeSourceDepsNode);
472                 time_source = NULL;
473         }
474 }
475
476 ID *Depsgraph::get_cow_id(const ID *id_orig) const
477 {
478         IDDepsNode *id_node = find_id_node(id_orig);
479         if (id_node == NULL) {
480                 /* This function is used from places where we expect ID to be either
481                  * already a copy-on-write version or have a corresponding copy-on-write
482                  * version.
483                  *
484                  * We try to enforce that in debug builds, for for release we play a bit
485                  * safer game here.
486                  */
487                 if ((id_orig->tag & LIB_TAG_COPY_ON_WRITE) == 0) {
488                         /* TODO(sergey): This is nice sanity check to have, but it fails
489                          * in following situations:
490                          *
491                          * - Material has link to texture, which is not needed by new
492                          *   shading system and hence can be ignored at construction.
493                          * - Object or mesh has material at a slot which is not used (for
494                          *   example, object has material slot by materials are set to
495                          *   object data).
496                          */
497                         // BLI_assert(!"Request for non-existing copy-on-write ID");
498                 }
499                 return (ID *)id_orig;
500         }
501         return id_node->id_cow;
502 }
503
504 void deg_editors_id_update(const DEGEditorUpdateContext *update_ctx, ID *id)
505 {
506         if (deg_editor_update_id_cb != NULL) {
507                 deg_editor_update_id_cb(update_ctx, id);
508         }
509 }
510
511 void deg_editors_scene_update(const DEGEditorUpdateContext *update_ctx,
512                               bool updated)
513 {
514         if (deg_editor_update_scene_cb != NULL) {
515                 deg_editor_update_scene_cb(update_ctx, updated);
516         }
517 }
518
519 }  // namespace DEG
520
521 /* **************** */
522 /* Public Graph API */
523
524 /* Initialize a new Depsgraph */
525 Depsgraph *DEG_graph_new()
526 {
527         DEG::Depsgraph *deg_depsgraph = OBJECT_GUARDED_NEW(DEG::Depsgraph);
528         return reinterpret_cast<Depsgraph *>(deg_depsgraph);
529 }
530
531 /* Free graph's contents and graph itself */
532 void DEG_graph_free(Depsgraph *graph)
533 {
534         using DEG::Depsgraph;
535         DEG::Depsgraph *deg_depsgraph = reinterpret_cast<DEG::Depsgraph *>(graph);
536         OBJECT_GUARDED_DELETE(deg_depsgraph, Depsgraph);
537 }
538
539 /* Set callbacks which are being called when depsgraph changes. */
540 void DEG_editors_set_update_cb(DEG_EditorUpdateIDCb id_func,
541                                DEG_EditorUpdateSceneCb scene_func)
542 {
543         DEG::deg_editor_update_id_cb = id_func;
544         DEG::deg_editor_update_scene_cb = scene_func;
545 }