4f3769b6768775453798ccb583d1738d10d624f7
[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 /* ************************ DEG ITERATORS ********************* */
64
65 static void verify_id_proeprties_freed(DEGObjectIterData *data)
66 {
67         if (data->dupli_object_current == NULL) {
68                 // We didn't enter duplication yet, so we can't have any dangling
69                 // pointers.
70                 return;
71         }
72         const Object *dupli_object = data->dupli_object_current->ob;
73         Object *temp_dupli_object = &data->temp_dupli_object;
74         if (temp_dupli_object->id.properties == NULL) {
75                 // No ID proeprties in temp datablock -- no leak is possible.
76                 return;
77         }
78         if (temp_dupli_object->id.properties == dupli_object->id.properties) {
79                 // Temp copy of object did not modify ID properties.
80                 return;
81         }
82         // Free memory which is owned by temporary storage which is about to
83         // get overwritten.
84         IDP_FreeProperty(temp_dupli_object->id.properties);
85         MEM_freeN(temp_dupli_object->id.properties);
86         temp_dupli_object->id.properties = NULL;
87 }
88
89 static bool deg_objects_dupli_iterator_next(BLI_Iterator *iter)
90 {
91         DEGObjectIterData *data = (DEGObjectIterData *)iter->data;
92         while (data->dupli_object_next != NULL) {
93                 DupliObject *dob = data->dupli_object_next;
94                 Object *obd = dob->ob;
95
96                 data->dupli_object_next = data->dupli_object_next->next;
97
98                 /* Group duplis need to set ob matrices correct, for deform. so no_draw
99                  * is part handled.
100                  */
101                 if ((obd->transflag & OB_RENDER_DUPLI) == 0 && dob->no_draw) {
102                         continue;
103                 }
104
105                 if (obd->type == OB_MBALL) {
106                         continue;
107                 }
108
109                 verify_id_proeprties_freed(data);
110
111                 data->dupli_object_current = dob;
112
113                 /* Temporary object to evaluate. */
114                 Object *dupli_parent = data->dupli_parent;
115                 Object *temp_dupli_object = &data->temp_dupli_object;
116                 *temp_dupli_object = *dob->ob;
117                 temp_dupli_object->select_color = dupli_parent->select_color;
118                 temp_dupli_object->base_flag = dupli_parent->base_flag | BASE_FROMDUPLI;
119
120                 /* Duplicated elements shouldn't care whether their original collection is visible or not. */
121                 temp_dupli_object->base_flag |= BASE_VISIBLED;
122
123                 if (BKE_object_is_visible(temp_dupli_object, (eObjectVisibilityCheck)data->visibility_check) == false) {
124                         continue;
125                 }
126
127                 temp_dupli_object->transflag &= ~OB_DUPLI;
128
129                 copy_m4_m4(data->temp_dupli_object.obmat, dob->mat);
130                 iter->current = &data->temp_dupli_object;
131                 BLI_assert(
132                         DEG::deg_validate_copy_on_write_datablock(
133                                 &data->temp_dupli_object.id));
134                 return true;
135         }
136
137         return false;
138 }
139
140 static void DEG_iterator_objects_step(BLI_Iterator *iter, DEG::IDDepsNode *id_node)
141 {
142         /* Set it early in case we need to exit and we are running from within a loop. */
143         iter->skip = true;
144
145         DEGObjectIterData *data = (DEGObjectIterData *)iter->data;
146         const ID_Type id_type = GS(id_node->id_orig->name);
147
148         if (id_type != ID_OB) {
149                 return;
150         }
151
152         switch (id_node->linked_state) {
153                 case DEG::DEG_ID_LINKED_DIRECTLY:
154                         if ((data->flag & DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY) == 0) {
155                                 return;
156                         }
157                         break;
158                 case DEG::DEG_ID_LINKED_VIA_SET:
159                         if ((data->flag & DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET) == 0) {
160                                 return;
161                         }
162                         break;
163                 case DEG::DEG_ID_LINKED_INDIRECTLY:
164                         if ((data->flag & DEG_ITER_OBJECT_FLAG_LINKED_INDIRECTLY) == 0) {
165                                 return;
166                         }
167                         break;
168         }
169
170         Object *object = (Object *)id_node->id_cow;
171         BLI_assert(DEG::deg_validate_copy_on_write_datablock(&object->id));
172
173         if ((BKE_object_is_visible(object, OB_VISIBILITY_CHECK_UNKNOWN_RENDER_MODE) == false) &&
174             ((data->flag & DEG_ITER_OBJECT_FLAG_VISIBLE) != 0))
175         {
176                 return;
177         }
178
179         if ((data->flag & DEG_ITER_OBJECT_FLAG_DUPLI) &&
180             (object->transflag & OB_DUPLI))
181         {
182                 data->dupli_parent = object;
183                 data->dupli_list = object_duplilist(data->graph, data->scene, object);
184                 data->dupli_object_next = (DupliObject *)data->dupli_list->first;
185                 if (BKE_object_is_visible(object, (eObjectVisibilityCheck)data->visibility_check) == false) {
186                         return;
187                 }
188         }
189
190         iter->current = object;
191         iter->skip = false;
192 }
193
194 void DEG_iterator_objects_begin(BLI_Iterator *iter, DEGObjectIterData *data)
195 {
196         Depsgraph *depsgraph = data->graph;
197         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(depsgraph);
198         const size_t num_id_nodes = deg_graph->id_nodes.size();
199
200         iter->data = data;
201
202         if (num_id_nodes == 0) {
203                 iter->valid = false;
204                 return;
205         }
206
207         data->dupli_parent = NULL;
208         data->dupli_list = NULL;
209         data->dupli_object_next = NULL;
210         data->dupli_object_current = NULL;
211         data->scene = DEG_get_evaluated_scene(depsgraph);
212         data->id_node_index = 0;
213         data->num_id_nodes = num_id_nodes;
214         data->visibility_check = (data->mode == DEG_ITER_OBJECT_MODE_RENDER)
215                                  ? OB_VISIBILITY_CHECK_FOR_RENDER
216                                  : OB_VISIBILITY_CHECK_FOR_VIEWPORT;
217
218         DEG::IDDepsNode *id_node = deg_graph->id_nodes[data->id_node_index];
219         DEG_iterator_objects_step(iter, id_node);
220
221         if (iter->skip) {
222                 DEG_iterator_objects_next(iter);
223         }
224 }
225
226 void DEG_iterator_objects_next(BLI_Iterator *iter)
227 {
228         DEGObjectIterData *data = (DEGObjectIterData *)iter->data;
229         Depsgraph *depsgraph = data->graph;
230         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(depsgraph);
231         do {
232                 iter->skip = false;
233                 if (data->dupli_list) {
234                         if (deg_objects_dupli_iterator_next(iter)) {
235                                 return;
236                         }
237                         else {
238                                 verify_id_proeprties_freed(data);
239                                 free_object_duplilist(data->dupli_list);
240                                 data->dupli_parent = NULL;
241                                 data->dupli_list = NULL;
242                                 data->dupli_object_next = NULL;
243                                 data->dupli_object_current = NULL;
244                         }
245                 }
246
247                 ++data->id_node_index;
248                 if (data->id_node_index == data->num_id_nodes) {
249                         iter->valid = false;
250                         return;
251                 }
252
253                 DEG::IDDepsNode *id_node = deg_graph->id_nodes[data->id_node_index];
254                 DEG_iterator_objects_step(iter, id_node);
255         } while (iter->skip);
256 }
257
258 void DEG_iterator_objects_end(BLI_Iterator *iter)
259 {
260 #ifndef NDEBUG
261         DEGObjectIterData *data = (DEGObjectIterData *)iter->data;
262
263         if (data) {
264                 /* Force crash in case the iterator data is referenced and accessed down
265                  * the line. (T51718)
266                  */
267                 memset(&data->temp_dupli_object, 0xff, sizeof(data->temp_dupli_object));
268         }
269 #else
270         (void) iter;
271 #endif
272 }
273
274 /* ************************ DEG ID ITERATOR ********************* */
275
276 static void DEG_iterator_ids_step(BLI_Iterator *iter, DEG::IDDepsNode *id_node, bool only_updated)
277 {
278         ID *id_cow = id_node->id_cow;
279
280         if (only_updated && !(id_cow->recalc & ID_RECALC_ALL)) {
281                 bNodeTree *ntree = ntreeFromID(id_cow);
282
283                 /* Nodetree is considered part of the datablock. */
284                 if (!(ntree && (ntree->id.recalc & ID_RECALC_ALL))) {
285                         iter->skip = true;
286                         return;
287                 }
288         }
289
290         iter->current = id_cow;
291         iter->skip = false;
292 }
293
294 void DEG_iterator_ids_begin(BLI_Iterator *iter, DEGIDIterData *data)
295 {
296         Depsgraph *depsgraph = data->graph;
297         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(depsgraph);
298         const size_t num_id_nodes = deg_graph->id_nodes.size();
299
300         iter->data = data;
301
302         if ((num_id_nodes == 0) ||
303             (data->only_updated && !DEG_id_type_any_updated(depsgraph)))
304         {
305                 iter->valid = false;
306                 return;
307         }
308
309         data->id_node_index = 0;
310         data->num_id_nodes = num_id_nodes;
311
312         DEG::IDDepsNode *id_node = deg_graph->id_nodes[data->id_node_index];
313         DEG_iterator_ids_step(iter, id_node, data->only_updated);
314
315         if (iter->skip) {
316                 DEG_iterator_ids_next(iter);
317         }
318 }
319
320 void DEG_iterator_ids_next(BLI_Iterator *iter)
321 {
322         DEGIDIterData *data = (DEGIDIterData *)iter->data;
323         Depsgraph *depsgraph = data->graph;
324         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(depsgraph);
325
326         do {
327                 iter->skip = false;
328
329                 ++data->id_node_index;
330                 if (data->id_node_index == data->num_id_nodes) {
331                         iter->valid = false;
332                         return;
333                 }
334
335                 DEG::IDDepsNode *id_node = deg_graph->id_nodes[data->id_node_index];
336                 DEG_iterator_ids_step(iter, id_node, data->only_updated);
337         } while (iter->skip);
338 }
339
340 void DEG_iterator_ids_end(BLI_Iterator *UNUSED(iter))
341 {
342 }