Add C++ guards to some blenlib headers
[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         previous_eval_flags = 0;
107         customdata_mask = 0;
108         previous_customdata_mask = 0;
109         linked_state = DEG_ID_LINKED_INDIRECTLY;
110         is_directly_visible = true;
111         is_collection_fully_expanded = false;
112
113         visible_components_mask = 0;
114         previously_visible_components_mask = 0;
115
116         components = BLI_ghash_new(id_deps_node_hash_key,
117                                    id_deps_node_hash_key_cmp,
118                                    "Depsgraph id components hash");
119 }
120
121 void IDDepsNode::init_copy_on_write(ID *id_cow_hint)
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                 id_cow = NULL;
168                 DEG_COW_PRINT("Destroy CoW for %s: id_orig=%p id_cow=%p\n",
169                               id_orig->name, id_orig, id_cow);
170         }
171
172         /* Tag that the node is freed. */
173         id_orig = NULL;
174 }
175
176 string IDDepsNode::identifier() const
177 {
178         char orig_ptr[24], cow_ptr[24];
179         BLI_snprintf(orig_ptr, sizeof(orig_ptr), "%p", id_orig);
180         BLI_snprintf(cow_ptr, sizeof(cow_ptr), "%p", id_cow);
181         return string(nodeTypeAsString(type)) + " : " + name +
182                 " (orig: " + orig_ptr + ", eval: " + cow_ptr +
183                 ", is_directly_visible " + (is_directly_visible ? "true"
184                                                                 : "false") + ")";
185 }
186
187 ComponentDepsNode *IDDepsNode::find_component(eDepsNode_Type type,
188                                               const char *name) const
189 {
190         ComponentIDKey key(type, name);
191         return reinterpret_cast<ComponentDepsNode *>(BLI_ghash_lookup(components, &key));
192 }
193
194 ComponentDepsNode *IDDepsNode::add_component(eDepsNode_Type type,
195                                              const char *name)
196 {
197         ComponentDepsNode *comp_node = find_component(type, name);
198         if (!comp_node) {
199                 DepsNodeFactory *factory = deg_type_get_factory(type);
200                 comp_node = (ComponentDepsNode *)factory->create_node(this->id_orig, "", name);
201
202                 /* Register. */
203                 ComponentIDKey *key = OBJECT_GUARDED_NEW(ComponentIDKey, type, name);
204                 BLI_ghash_insert(components, key, comp_node);
205                 comp_node->owner = this;
206         }
207         return comp_node;
208 }
209
210 void IDDepsNode::tag_update(Depsgraph *graph, eUpdateSource source)
211 {
212         GHASH_FOREACH_BEGIN(ComponentDepsNode *, comp_node, components)
213         {
214                 comp_node->tag_update(graph, source);
215         }
216         GHASH_FOREACH_END();
217 }
218
219 void IDDepsNode::finalize_build(Depsgraph *graph)
220 {
221         /* Finalize build of all components. */
222         GHASH_FOREACH_BEGIN(ComponentDepsNode *, comp_node, components)
223         {
224                 comp_node->finalize_build(graph);
225         }
226         GHASH_FOREACH_END();
227         visible_components_mask = get_visible_components_mask();
228 }
229
230 IDComponentsMask IDDepsNode::get_visible_components_mask() const {
231         IDComponentsMask result = 0;
232         GHASH_FOREACH_BEGIN(ComponentDepsNode *, comp_node, components)
233         {
234                 if (comp_node->affects_directly_visible) {
235                         const int component_type = comp_node->type;
236                         BLI_assert(component_type < 64);
237                         result |= (1ULL << component_type);
238                 }
239         }
240         GHASH_FOREACH_END();
241         return result;
242 }
243
244 }  // namespace DEG