52c4ab33dc6cd898e70049672055bc5b3bb104ff
[blender.git] / source / blender / depsgraph / intern / depsgraph_query_iter.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) 2017 Blender Foundation.
19  * All rights reserved.
20  *
21  * Original Author: Dalai Felinto
22  * Contributor(s): Sergey Sharybin
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/depsgraph/intern/depsgraph_query_iter.cc
28  *  \ingroup depsgraph
29  *
30  * Implementation of Querying and Filtering API's
31  */
32
33 /* Silence warnings from copying deprecated fields. */
34 #define DNA_DEPRECATED_ALLOW
35
36 #include "MEM_guardedalloc.h"
37
38 extern "C" {
39 #include "BLI_utildefines.h"
40 #include "BLI_math.h"
41 #include "BKE_anim.h"
42 #include "BKE_idprop.h"
43 #include "BKE_layer.h"
44 #include "BKE_node.h"
45 #include "BKE_object.h"
46 } /* extern "C" */
47
48 #include "DNA_object_types.h"
49 #include "DNA_scene_types.h"
50
51 #include "DEG_depsgraph.h"
52 #include "DEG_depsgraph_query.h"
53
54 #include "intern/depsgraph_intern.h"
55 #include "util/deg_util_foreach.h"
56
57 #include "intern/nodes/deg_node_id.h"
58
59 #ifndef NDEBUG
60 #  include "intern/eval/deg_eval_copy_on_write.h"
61 #endif
62
63 // If defined, all working data will be set to an invalid state, helping
64 // to catch issues when areas accessing data which is considered to be no
65 // longer available.
66 #undef INVALIDATE_WORK_DATA
67
68 #ifndef NDEBUG
69 #  define INVALIDATE_WORK_DATA
70 #endif
71
72 /* ************************ DEG ITERATORS ********************* */
73
74 namespace {
75
76 void deg_invalidate_iterator_work_data(DEGObjectIterData *data)
77 {
78 #ifdef INVALIDATE_WORK_DATA
79         BLI_assert(data != NULL);
80         memset(&data->temp_dupli_object, 0xff, sizeof(data->temp_dupli_object));
81 #else
82         (void) data;
83 #endif
84 }
85
86 void verify_id_properties_freed(DEGObjectIterData *data)
87 {
88         if (data->dupli_object_current == NULL) {
89                 // We didn't enter duplication yet, so we can't have any dangling
90                 // pointers.
91                 return;
92         }
93         const Object *dupli_object = data->dupli_object_current->ob;
94         Object *temp_dupli_object = &data->temp_dupli_object;
95         if (temp_dupli_object->id.properties == NULL) {
96                 // No ID properties in temp datablock -- no leak is possible.
97                 return;
98         }
99         if (temp_dupli_object->id.properties == dupli_object->id.properties) {
100                 // Temp copy of object did not modify ID properties.
101                 return;
102         }
103         // Free memory which is owned by temporary storage which is about to
104         // get overwritten.
105         IDP_FreeProperty(temp_dupli_object->id.properties);
106         MEM_freeN(temp_dupli_object->id.properties);
107         temp_dupli_object->id.properties = NULL;
108 }
109
110 bool deg_objects_dupli_iterator_next(BLI_Iterator *iter)
111 {
112         DEGObjectIterData *data = (DEGObjectIterData *)iter->data;
113         while (data->dupli_object_next != NULL) {
114                 DupliObject *dob = data->dupli_object_next;
115                 Object *obd = dob->ob;
116
117                 data->dupli_object_next = data->dupli_object_next->next;
118
119                 /* Group duplis need to set ob matrices correct, for deform. so no_draw
120                  * is part handled.
121                  */
122                 if ((obd->transflag & OB_RENDER_DUPLI) == 0 && dob->no_draw) {
123                         continue;
124                 }
125
126                 if (obd->type == OB_MBALL) {
127                         continue;
128                 }
129
130                 verify_id_properties_freed(data);
131
132                 data->dupli_object_current = dob;
133
134                 /* Temporary object to evaluate. */
135                 Object *dupli_parent = data->dupli_parent;
136                 Object *temp_dupli_object = &data->temp_dupli_object;
137                 *temp_dupli_object = *dob->ob;
138                 temp_dupli_object->select_color = dupli_parent->select_color;
139                 temp_dupli_object->base_flag = dupli_parent->base_flag | BASE_FROMDUPLI;
140                 temp_dupli_object->base_local_view_bits = dupli_parent->base_local_view_bits;
141
142                 /* Duplicated elements shouldn't care whether their original collection is visible or not. */
143                 temp_dupli_object->base_flag |= BASE_VISIBLE;
144
145                 if (BKE_object_is_visible(temp_dupli_object, OB_VISIBILITY_CHECK_UNKNOWN_RENDER_MODE) == false) {
146                         continue;
147                 }
148
149                 temp_dupli_object->transflag &= ~OB_DUPLI;
150
151                 copy_m4_m4(data->temp_dupli_object.obmat, dob->mat);
152                 iter->current = &data->temp_dupli_object;
153                 BLI_assert(
154                         DEG::deg_validate_copy_on_write_datablock(
155                                 &data->temp_dupli_object.id));
156                 return true;
157         }
158
159         return false;
160 }
161
162 void deg_iterator_objects_step(BLI_Iterator *iter, DEG::IDDepsNode *id_node)
163 {
164         /* Set it early in case we need to exit and we are running from within a loop. */
165         iter->skip = true;
166
167         if (!id_node->is_directly_visible) {
168                 return;
169         }
170
171         DEGObjectIterData *data = (DEGObjectIterData *)iter->data;
172         const ID_Type id_type = GS(id_node->id_orig->name);
173
174         if (id_type != ID_OB) {
175                 return;
176         }
177
178         switch (id_node->linked_state) {
179                 case DEG::DEG_ID_LINKED_DIRECTLY:
180                         if ((data->flag & DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY) == 0) {
181                                 return;
182                         }
183                         break;
184                 case DEG::DEG_ID_LINKED_VIA_SET:
185                         if ((data->flag & DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET) == 0) {
186                                 return;
187                         }
188                         break;
189                 case DEG::DEG_ID_LINKED_INDIRECTLY:
190                         if ((data->flag & DEG_ITER_OBJECT_FLAG_LINKED_INDIRECTLY) == 0) {
191                                 return;
192                         }
193                         break;
194         }
195
196         Object *object = (Object *)id_node->id_cow;
197         BLI_assert(DEG::deg_validate_copy_on_write_datablock(&object->id));
198
199         if ((BKE_object_is_visible(object, OB_VISIBILITY_CHECK_UNKNOWN_RENDER_MODE) == false) &&
200             ((data->flag & DEG_ITER_OBJECT_FLAG_VISIBLE) != 0))
201         {
202                 return;
203         }
204
205         if ((data->flag & DEG_ITER_OBJECT_FLAG_DUPLI) &&
206             (object->transflag & OB_DUPLI))
207         {
208                 data->dupli_parent = object;
209                 data->dupli_list = object_duplilist(data->graph, data->scene, object);
210                 data->dupli_object_next = (DupliObject *)data->dupli_list->first;
211                 if (BKE_object_is_visible(object, (eObjectVisibilityCheck)data->visibility_check) == false) {
212                         return;
213                 }
214         }
215
216         iter->current = object;
217         iter->skip = false;
218 }
219
220 }  // namespace
221
222 void DEG_iterator_objects_begin(BLI_Iterator *iter, DEGObjectIterData *data)
223 {
224         Depsgraph *depsgraph = data->graph;
225         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(depsgraph);
226         const size_t num_id_nodes = deg_graph->id_nodes.size();
227
228         iter->data = data;
229
230         if (num_id_nodes == 0) {
231                 iter->valid = false;
232                 return;
233         }
234
235         data->dupli_parent = NULL;
236         data->dupli_list = NULL;
237         data->dupli_object_next = NULL;
238         data->dupli_object_current = NULL;
239         data->scene = DEG_get_evaluated_scene(depsgraph);
240         data->id_node_index = 0;
241         data->num_id_nodes = num_id_nodes;
242         eEvaluationMode eval_mode = DEG_get_mode(depsgraph);
243         data->visibility_check = (eval_mode == DAG_EVAL_RENDER)
244                                  ? OB_VISIBILITY_CHECK_FOR_RENDER
245                                  : OB_VISIBILITY_CHECK_FOR_VIEWPORT;
246         deg_invalidate_iterator_work_data(data);
247
248         DEG::IDDepsNode *id_node = deg_graph->id_nodes[data->id_node_index];
249         deg_iterator_objects_step(iter, id_node);
250
251         if (iter->skip) {
252                 DEG_iterator_objects_next(iter);
253         }
254 }
255
256 void DEG_iterator_objects_next(BLI_Iterator *iter)
257 {
258         DEGObjectIterData *data = (DEGObjectIterData *)iter->data;
259         Depsgraph *depsgraph = data->graph;
260         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(depsgraph);
261         do {
262                 iter->skip = false;
263                 if (data->dupli_list) {
264                         if (deg_objects_dupli_iterator_next(iter)) {
265                                 return;
266                         }
267                         else {
268                                 verify_id_properties_freed(data);
269                                 free_object_duplilist(data->dupli_list);
270                                 data->dupli_parent = NULL;
271                                 data->dupli_list = NULL;
272                                 data->dupli_object_next = NULL;
273                                 data->dupli_object_current = NULL;
274                                 deg_invalidate_iterator_work_data(data);
275                         }
276                 }
277
278                 ++data->id_node_index;
279                 if (data->id_node_index == data->num_id_nodes) {
280                         iter->valid = false;
281                         return;
282                 }
283
284                 DEG::IDDepsNode *id_node = deg_graph->id_nodes[data->id_node_index];
285                 deg_iterator_objects_step(iter, id_node);
286         } while (iter->skip);
287 }
288
289 void DEG_iterator_objects_end(BLI_Iterator *iter)
290 {
291         DEGObjectIterData *data = (DEGObjectIterData *)iter->data;
292         if (data != NULL) {
293                 /* Force crash in case the iterator data is referenced and accessed down
294                  * the line. (T51718)
295                  */
296                 deg_invalidate_iterator_work_data(data);
297         }
298 }
299
300 /* ************************ DEG ID ITERATOR ********************* */
301
302 static void DEG_iterator_ids_step(BLI_Iterator *iter, DEG::IDDepsNode *id_node, bool only_updated)
303 {
304         ID *id_cow = id_node->id_cow;
305
306         if (!id_node->is_directly_visible) {
307                 iter->skip = true;
308                 return;
309         }
310         else if (only_updated && !(id_cow->recalc & ID_RECALC_ALL)) {
311                 bNodeTree *ntree = ntreeFromID(id_cow);
312
313                 /* Nodetree is considered part of the datablock. */
314                 if (!(ntree && (ntree->id.recalc & ID_RECALC_ALL))) {
315                         iter->skip = true;
316                         return;
317                 }
318         }
319
320         iter->current = id_cow;
321         iter->skip = false;
322 }
323
324 void DEG_iterator_ids_begin(BLI_Iterator *iter, DEGIDIterData *data)
325 {
326         Depsgraph *depsgraph = data->graph;
327         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(depsgraph);
328         const size_t num_id_nodes = deg_graph->id_nodes.size();
329
330         iter->data = data;
331
332         if ((num_id_nodes == 0) ||
333             (data->only_updated && !DEG_id_type_any_updated(depsgraph)))
334         {
335                 iter->valid = false;
336                 return;
337         }
338
339         data->id_node_index = 0;
340         data->num_id_nodes = num_id_nodes;
341
342         DEG::IDDepsNode *id_node = deg_graph->id_nodes[data->id_node_index];
343         DEG_iterator_ids_step(iter, id_node, data->only_updated);
344
345         if (iter->skip) {
346                 DEG_iterator_ids_next(iter);
347         }
348 }
349
350 void DEG_iterator_ids_next(BLI_Iterator *iter)
351 {
352         DEGIDIterData *data = (DEGIDIterData *)iter->data;
353         Depsgraph *depsgraph = data->graph;
354         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(depsgraph);
355
356         do {
357                 iter->skip = false;
358
359                 ++data->id_node_index;
360                 if (data->id_node_index == data->num_id_nodes) {
361                         iter->valid = false;
362                         return;
363                 }
364
365                 DEG::IDDepsNode *id_node = deg_graph->id_nodes[data->id_node_index];
366                 DEG_iterator_ids_step(iter, id_node, data->only_updated);
367         } while (iter->skip);
368 }
369
370 void DEG_iterator_ids_end(BLI_Iterator *UNUSED(iter))
371 {
372 }