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