Merge branch 'master' into blender2.8
[blender.git] / source / blender / depsgraph / intern / nodes / deg_node_id.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): None Yet
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/depsgraph/intern/nodes/deg_node_id.cc
28  *  \ingroup depsgraph
29  */
30
31 #include "intern/nodes/deg_node_id.h"
32
33 #include <stdio.h>
34 #include <cstring>  /* required for STREQ later on. */
35
36 #include "BLI_utildefines.h"
37 #include "BLI_ghash.h"
38
39 extern "C" {
40 #include "DNA_ID.h"
41 #include "DNA_anim_types.h"
42
43 #include "BKE_animsys.h"
44 #include "BKE_library.h"
45 }
46
47 #include "DEG_depsgraph.h"
48
49 #include "intern/eval/deg_eval_copy_on_write.h"
50 #include "intern/nodes/deg_node_time.h"
51 #include "intern/depsgraph_intern.h"
52
53 #include "util/deg_util_foreach.h"
54
55 namespace DEG {
56
57 IDDepsNode::ComponentIDKey::ComponentIDKey(eDepsNode_Type type,
58                                            const char *name)
59         : type(type), name(name)
60 {
61 }
62
63 bool IDDepsNode::ComponentIDKey::operator== (const ComponentIDKey &other) const
64 {
65         return type == other.type &&
66                 STREQ(name, other.name);
67 }
68
69 static unsigned int id_deps_node_hash_key(const void *key_v)
70 {
71         const IDDepsNode::ComponentIDKey *key =
72                 reinterpret_cast<const IDDepsNode::ComponentIDKey *>(key_v);
73         return BLI_ghashutil_combine_hash(BLI_ghashutil_uinthash(key->type),
74                                           BLI_ghashutil_strhash_p(key->name));
75 }
76
77 static bool id_deps_node_hash_key_cmp(const void *a, const void *b)
78 {
79         const IDDepsNode::ComponentIDKey *key_a =
80                 reinterpret_cast<const IDDepsNode::ComponentIDKey *>(a);
81         const IDDepsNode::ComponentIDKey *key_b =
82                 reinterpret_cast<const IDDepsNode::ComponentIDKey *>(b);
83         return !(*key_a == *key_b);
84 }
85
86 static void id_deps_node_hash_key_free(void *key_v)
87 {
88         typedef IDDepsNode::ComponentIDKey ComponentIDKey;
89         ComponentIDKey *key = reinterpret_cast<ComponentIDKey *>(key_v);
90         OBJECT_GUARDED_DELETE(key, ComponentIDKey);
91 }
92
93 static void id_deps_node_hash_value_free(void *value_v)
94 {
95         ComponentDepsNode *comp_node = reinterpret_cast<ComponentDepsNode *>(value_v);
96         OBJECT_GUARDED_DELETE(comp_node, ComponentDepsNode);
97 }
98
99 /* Initialize 'id' node - from pointer data given. */
100 void IDDepsNode::init(const ID *id, const char *UNUSED(subdata))
101 {
102         BLI_assert(id != NULL);
103         /* Store ID-pointer. */
104         id_orig = (ID *)id;
105         eval_flags = 0;
106         linked_state = DEG_ID_LINKED_INDIRECTLY;
107
108         components = BLI_ghash_new(id_deps_node_hash_key,
109                                    id_deps_node_hash_key_cmp,
110                                    "Depsgraph id components hash");
111 }
112
113 void IDDepsNode::init_copy_on_write(ID *id_cow_hint)
114 {
115         /* Early output for non-copy-on-write case: we keep CoW pointer same as
116          * an original one.
117          */
118         if (!DEG_depsgraph_use_copy_on_write()) {
119                 UNUSED_VARS(id_cow_hint);
120                 id_cow = id_orig;
121                 return;
122         }
123         /* Create pointer as early as possible, so we can use it for function
124          * bindings. Rest of data we'll be copying to the new datablock when
125          * it is actually needed.
126          */
127         if (id_cow_hint != NULL) {
128                 // BLI_assert(deg_copy_on_write_is_needed(id_orig));
129                 if (deg_copy_on_write_is_needed(id_orig)) {
130                         id_cow = id_cow_hint;
131                 }
132                 else {
133                         id_cow = id_orig;
134                 }
135         }
136         else if (deg_copy_on_write_is_needed(id_orig)) {
137                 id_cow = (ID *)BKE_libblock_alloc_notest(GS(id_orig->name));
138                 DEG_COW_PRINT("Create shallow copy for %s: id_orig=%p id_cow=%p\n",
139                               id_orig->name, id_orig, id_cow);
140                 deg_tag_copy_on_write_id(id_cow, id_orig);
141         }
142         else {
143                 id_cow = id_orig;
144         }
145 }
146
147 /* Free 'id' node. */
148 IDDepsNode::~IDDepsNode()
149 {
150         destroy();
151 }
152
153 void IDDepsNode::destroy()
154 {
155         if (id_orig == NULL) {
156                 return;
157         }
158
159         BLI_ghash_free(components,
160                        id_deps_node_hash_key_free,
161                        id_deps_node_hash_value_free);
162
163         /* Free memory used by this CoW ID. */
164         if (id_cow != id_orig && id_cow != NULL) {
165                 deg_free_copy_on_write_datablock(id_cow);
166                 MEM_freeN(id_cow);
167                 DEG_COW_PRINT("Destroy CoW for %s: id_orig=%p id_cow=%p\n",
168                               id_orig->name, id_orig, id_cow);
169         }
170
171         /* Tag that the node is freed. */
172         id_orig = NULL;
173 }
174
175 ComponentDepsNode *IDDepsNode::find_component(eDepsNode_Type type,
176                                               const char *name) const
177 {
178         ComponentIDKey key(type, name);
179         return reinterpret_cast<ComponentDepsNode *>(BLI_ghash_lookup(components, &key));
180 }
181
182 ComponentDepsNode *IDDepsNode::add_component(eDepsNode_Type type,
183                                              const char *name)
184 {
185         ComponentDepsNode *comp_node = find_component(type, name);
186         if (!comp_node) {
187                 DepsNodeFactory *factory = deg_type_get_factory(type);
188                 comp_node = (ComponentDepsNode *)factory->create_node(this->id_orig, "", name);
189
190                 /* Register. */
191                 ComponentIDKey *key = OBJECT_GUARDED_NEW(ComponentIDKey, type, name);
192                 BLI_ghash_insert(components, key, comp_node);
193                 comp_node->owner = this;
194         }
195         return comp_node;
196 }
197
198 void IDDepsNode::tag_update(Depsgraph *graph)
199 {
200         GHASH_FOREACH_BEGIN(ComponentDepsNode *, comp_node, components)
201         {
202                 comp_node->tag_update(graph);
203         }
204         GHASH_FOREACH_END();
205 }
206
207 void IDDepsNode::finalize_build(Depsgraph *graph)
208 {
209         /* Finalize build of all components. */
210         GHASH_FOREACH_BEGIN(ComponentDepsNode *, comp_node, components)
211         {
212                 comp_node->finalize_build(graph);
213         }
214         GHASH_FOREACH_END();
215 }
216
217 }  // namespace DEG