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