Cleanup: remove redundant doxygen \file argument
[blender.git] / source / blender / depsgraph / intern / depsgraph_query_iter.cc
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2017 Blender Foundation.
17  * All rights reserved.
18  */
19
20 /** \file \ingroup depsgraph
21  *
22  * Implementation of Querying and Filtering API's
23  */
24
25 /* Silence warnings from copying deprecated fields. */
26 #define DNA_DEPRECATED_ALLOW
27
28 #include "MEM_guardedalloc.h"
29
30 extern "C" {
31 #include "BLI_utildefines.h"
32 #include "BLI_math.h"
33 #include "BKE_anim.h"
34 #include "BKE_idprop.h"
35 #include "BKE_layer.h"
36 #include "BKE_node.h"
37 #include "BKE_object.h"
38 } /* extern "C" */
39
40 #include "DNA_object_types.h"
41 #include "DNA_scene_types.h"
42
43 #include "DEG_depsgraph.h"
44 #include "DEG_depsgraph_query.h"
45
46 #include "intern/depsgraph.h"
47 #include "intern/node/deg_node_id.h"
48
49 #ifndef NDEBUG
50 #  include "intern/eval/deg_eval_copy_on_write.h"
51 #endif
52
53 // If defined, all working data will be set to an invalid state, helping
54 // to catch issues when areas accessing data which is considered to be no
55 // longer available.
56 #undef INVALIDATE_WORK_DATA
57
58 #ifndef NDEBUG
59 #  define INVALIDATE_WORK_DATA
60 #endif
61
62 /* ************************ DEG ITERATORS ********************* */
63
64 namespace {
65
66 void deg_invalidate_iterator_work_data(DEGObjectIterData *data)
67 {
68 #ifdef INVALIDATE_WORK_DATA
69         BLI_assert(data != NULL);
70         memset(&data->temp_dupli_object, 0xff, sizeof(data->temp_dupli_object));
71 #else
72         (void) data;
73 #endif
74 }
75
76 void verify_id_properties_freed(DEGObjectIterData *data)
77 {
78         if (data->dupli_object_current == NULL) {
79                 // We didn't enter duplication yet, so we can't have any dangling
80                 // pointers.
81                 return;
82         }
83         const Object *dupli_object = data->dupli_object_current->ob;
84         Object *temp_dupli_object = &data->temp_dupli_object;
85         if (temp_dupli_object->id.properties == NULL) {
86                 // No ID properties in temp datablock -- no leak is possible.
87                 return;
88         }
89         if (temp_dupli_object->id.properties == dupli_object->id.properties) {
90                 // Temp copy of object did not modify ID properties.
91                 return;
92         }
93         // Free memory which is owned by temporary storage which is about to
94         // get overwritten.
95         IDP_FreeProperty(temp_dupli_object->id.properties);
96         MEM_freeN(temp_dupli_object->id.properties);
97         temp_dupli_object->id.properties = NULL;
98 }
99
100 static bool deg_object_hide_original(eEvaluationMode eval_mode, Object *ob, DupliObject *dob)
101 {
102         /* Automatic hiding if this object is being instanced on verts/faces/frames
103          * by its parent. Ideally this should not be needed, but due to the wrong
104          * dependency direction in the data design there is no way to keep the object
105          * visible otherwise. The better solution eventually would be for objects
106          * to specify which object they instance, instead of through parenting. */
107         if (eval_mode == DAG_EVAL_RENDER || dob) {
108                 const int hide_original_types = OB_DUPLIVERTS | OB_DUPLIFACES;
109
110                 if (!dob || !(dob->type & hide_original_types)) {
111                         if (ob->parent && (ob->parent->transflag & hide_original_types)) {
112                                 return true;
113                         }
114                 }
115         }
116
117         return false;
118 }
119
120 bool deg_objects_dupli_iterator_next(BLI_Iterator *iter)
121 {
122         DEGObjectIterData *data = (DEGObjectIterData *)iter->data;
123         while (data->dupli_object_next != NULL) {
124                 DupliObject *dob = data->dupli_object_next;
125                 Object *obd = dob->ob;
126
127                 data->dupli_object_next = data->dupli_object_next->next;
128
129                 if (dob->no_draw) {
130                         continue;
131                 }
132                 if (obd->type == OB_MBALL) {
133                         continue;
134                 }
135                 if (deg_object_hide_original(data->eval_mode, dob->ob, dob)) {
136                         continue;
137                 }
138
139                 verify_id_properties_freed(data);
140
141                 data->dupli_object_current = dob;
142
143                 /* Temporary object to evaluate. */
144                 Object *dupli_parent = data->dupli_parent;
145                 Object *temp_dupli_object = &data->temp_dupli_object;
146                 *temp_dupli_object = *dob->ob;
147                 temp_dupli_object->select_color = dupli_parent->select_color;
148                 temp_dupli_object->base_flag = dupli_parent->base_flag | BASE_FROM_DUPLI;
149                 temp_dupli_object->base_local_view_bits = dupli_parent->base_local_view_bits;
150
151                 /* Duplicated elements shouldn't care whether their original collection is visible or not. */
152                 temp_dupli_object->base_flag |= BASE_VISIBLE;
153
154                 int ob_visibility = BKE_object_visibility(temp_dupli_object, data->eval_mode);
155                 if (ob_visibility == 0) {
156                         continue;
157                 }
158
159                 copy_m4_m4(data->temp_dupli_object.obmat, dob->mat);
160                 iter->current = &data->temp_dupli_object;
161                 BLI_assert(
162                         DEG::deg_validate_copy_on_write_datablock(
163                                 &data->temp_dupli_object.id));
164                 return true;
165         }
166
167         return false;
168 }
169
170 void deg_iterator_objects_step(BLI_Iterator *iter, DEG::IDNode *id_node)
171 {
172         /* Set it early in case we need to exit and we are running from within a loop. */
173         iter->skip = true;
174
175         if (!id_node->is_directly_visible) {
176                 return;
177         }
178
179         DEGObjectIterData *data = (DEGObjectIterData *)iter->data;
180         const ID_Type id_type = GS(id_node->id_orig->name);
181
182         if (id_type != ID_OB) {
183                 return;
184         }
185
186         switch (id_node->linked_state) {
187                 case DEG::DEG_ID_LINKED_DIRECTLY:
188                         if ((data->flag & DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY) == 0) {
189                                 return;
190                         }
191                         break;
192                 case DEG::DEG_ID_LINKED_VIA_SET:
193                         if ((data->flag & DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET) == 0) {
194                                 return;
195                         }
196                         break;
197                 case DEG::DEG_ID_LINKED_INDIRECTLY:
198                         if ((data->flag & DEG_ITER_OBJECT_FLAG_LINKED_INDIRECTLY) == 0) {
199                                 return;
200                         }
201                         break;
202         }
203
204         Object *object = (Object *)id_node->id_cow;
205         BLI_assert(DEG::deg_validate_copy_on_write_datablock(&object->id));
206
207         int ob_visibility = OB_VISIBLE_ALL;
208         if (data->flag & DEG_ITER_OBJECT_FLAG_VISIBLE) {
209                 ob_visibility = BKE_object_visibility(object, data->eval_mode);
210
211                 if (deg_object_hide_original(data->eval_mode, object, NULL)) {
212                         return;
213                 }
214         }
215
216         if (ob_visibility & OB_VISIBLE_INSTANCES) {
217                 if ((data->flag & DEG_ITER_OBJECT_FLAG_DUPLI) &&
218                     (object->transflag & OB_DUPLI))
219                 {
220                         data->dupli_parent = object;
221                         data->dupli_list = object_duplilist(data->graph, data->scene, object);
222                         data->dupli_object_next = (DupliObject *)data->dupli_list->first;
223                 }
224         }
225
226         if (ob_visibility & (OB_VISIBLE_SELF | OB_VISIBLE_PARTICLES)) {
227                 iter->current = object;
228                 iter->skip = false;
229         }
230 }
231
232 }  // namespace
233
234 void DEG_iterator_objects_begin(BLI_Iterator *iter, DEGObjectIterData *data)
235 {
236         Depsgraph *depsgraph = data->graph;
237         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(depsgraph);
238         const size_t num_id_nodes = deg_graph->id_nodes.size();
239
240         iter->data = data;
241
242         if (num_id_nodes == 0) {
243                 iter->valid = false;
244                 return;
245         }
246
247         data->dupli_parent = NULL;
248         data->dupli_list = NULL;
249         data->dupli_object_next = NULL;
250         data->dupli_object_current = NULL;
251         data->scene = DEG_get_evaluated_scene(depsgraph);
252         data->id_node_index = 0;
253         data->num_id_nodes = num_id_nodes;
254         data->eval_mode = DEG_get_mode(depsgraph);
255         deg_invalidate_iterator_work_data(data);
256
257         DEG::IDNode *id_node = deg_graph->id_nodes[data->id_node_index];
258         deg_iterator_objects_step(iter, id_node);
259
260         if (iter->skip) {
261                 DEG_iterator_objects_next(iter);
262         }
263 }
264
265 void DEG_iterator_objects_next(BLI_Iterator *iter)
266 {
267         DEGObjectIterData *data = (DEGObjectIterData *)iter->data;
268         Depsgraph *depsgraph = data->graph;
269         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(depsgraph);
270         do {
271                 iter->skip = false;
272                 if (data->dupli_list) {
273                         if (deg_objects_dupli_iterator_next(iter)) {
274                                 return;
275                         }
276                         else {
277                                 verify_id_properties_freed(data);
278                                 free_object_duplilist(data->dupli_list);
279                                 data->dupli_parent = NULL;
280                                 data->dupli_list = NULL;
281                                 data->dupli_object_next = NULL;
282                                 data->dupli_object_current = NULL;
283                                 deg_invalidate_iterator_work_data(data);
284                         }
285                 }
286
287                 ++data->id_node_index;
288                 if (data->id_node_index == data->num_id_nodes) {
289                         iter->valid = false;
290                         return;
291                 }
292
293                 DEG::IDNode *id_node = deg_graph->id_nodes[data->id_node_index];
294                 deg_iterator_objects_step(iter, id_node);
295         } while (iter->skip);
296 }
297
298 void DEG_iterator_objects_end(BLI_Iterator *iter)
299 {
300         DEGObjectIterData *data = (DEGObjectIterData *)iter->data;
301         if (data != NULL) {
302                 /* Force crash in case the iterator data is referenced and accessed down
303                  * the line. (T51718) */
304                 deg_invalidate_iterator_work_data(data);
305         }
306 }
307
308 /* ************************ DEG ID ITERATOR ********************* */
309
310 static void DEG_iterator_ids_step(BLI_Iterator *iter, DEG::IDNode *id_node, bool only_updated)
311 {
312         ID *id_cow = id_node->id_cow;
313
314         if (!id_node->is_directly_visible) {
315                 iter->skip = true;
316                 return;
317         }
318         else if (only_updated && !(id_cow->recalc & ID_RECALC_ALL)) {
319                 bNodeTree *ntree = ntreeFromID(id_cow);
320
321                 /* Nodetree is considered part of the datablock. */
322                 if (!(ntree && (ntree->id.recalc & ID_RECALC_ALL))) {
323                         iter->skip = true;
324                         return;
325                 }
326         }
327
328         iter->current = id_cow;
329         iter->skip = false;
330 }
331
332 void DEG_iterator_ids_begin(BLI_Iterator *iter, DEGIDIterData *data)
333 {
334         Depsgraph *depsgraph = data->graph;
335         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(depsgraph);
336         const size_t num_id_nodes = deg_graph->id_nodes.size();
337
338         iter->data = data;
339
340         if ((num_id_nodes == 0) ||
341             (data->only_updated && !DEG_id_type_any_updated(depsgraph)))
342         {
343                 iter->valid = false;
344                 return;
345         }
346
347         data->id_node_index = 0;
348         data->num_id_nodes = num_id_nodes;
349
350         DEG::IDNode *id_node = deg_graph->id_nodes[data->id_node_index];
351         DEG_iterator_ids_step(iter, id_node, data->only_updated);
352
353         if (iter->skip) {
354                 DEG_iterator_ids_next(iter);
355         }
356 }
357
358 void DEG_iterator_ids_next(BLI_Iterator *iter)
359 {
360         DEGIDIterData *data = (DEGIDIterData *)iter->data;
361         Depsgraph *depsgraph = data->graph;
362         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(depsgraph);
363
364         do {
365                 iter->skip = false;
366
367                 ++data->id_node_index;
368                 if (data->id_node_index == data->num_id_nodes) {
369                         iter->valid = false;
370                         return;
371                 }
372
373                 DEG::IDNode *id_node = deg_graph->id_nodes[data->id_node_index];
374                 DEG_iterator_ids_step(iter, id_node, data->only_updated);
375         } while (iter->skip);
376 }
377
378 void DEG_iterator_ids_end(BLI_Iterator *UNUSED(iter))
379 {
380 }