Depsgraph: remove EvaluationContext, pass Depsgraph instead.
[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 #include "MEM_guardedalloc.h"
34
35 extern "C" {
36 #include "BLI_utildefines.h"
37 #include "BLI_math.h"
38 #include "BKE_anim.h"
39 #include "BKE_idprop.h"
40 #include "BKE_layer.h"
41 #include "BKE_object.h"
42 } /* extern "C" */
43
44 #include "DNA_object_types.h"
45 #include "DNA_scene_types.h"
46
47 #include "DEG_depsgraph.h"
48 #include "DEG_depsgraph_query.h"
49
50 #include "intern/depsgraph_intern.h"
51 #include "util/deg_util_foreach.h"
52
53 #include "intern/nodes/deg_node_id.h"
54
55 #ifndef NDEBUG
56 #  include "intern/eval/deg_eval_copy_on_write.h"
57 #endif
58
59 /* ************************ DEG ITERATORS ********************* */
60
61 static void verify_id_proeprties_freed(DEGObjectIterData *data)
62 {
63         if (data->dupli_object_current == NULL) {
64                 // We didn't enter duplication yet, so we can't have any dangling
65                 // pointers.
66                 return;
67         }
68         const Object *dupli_object = data->dupli_object_current->ob;
69         Object *temp_dupli_object = &data->temp_dupli_object;
70         if (temp_dupli_object->id.properties == NULL) {
71                 // No ID proeprties in temp datablock -- no leak is possible.
72                 return;
73         }
74         if (temp_dupli_object->id.properties == dupli_object->id.properties) {
75                 // Temp copy of object did not modify ID properties.
76                 return;
77         }
78         // Free memory which is owned by temporary storage which is about to
79         // get overwritten.
80         IDP_FreeProperty(temp_dupli_object->id.properties);
81         MEM_freeN(temp_dupli_object->id.properties);
82         temp_dupli_object->id.properties = NULL;
83 }
84
85 static bool deg_objects_dupli_iterator_next(BLI_Iterator *iter)
86 {
87         DEGObjectIterData *data = (DEGObjectIterData *)iter->data;
88         while (data->dupli_object_next != NULL) {
89                 DupliObject *dob = data->dupli_object_next;
90                 Object *obd = dob->ob;
91
92                 data->dupli_object_next = data->dupli_object_next->next;
93
94                 /* Group duplis need to set ob matrices correct, for deform. so no_draw
95                  * is part handled.
96                  */
97                 if ((obd->transflag & OB_RENDER_DUPLI) == 0 && dob->no_draw) {
98                         continue;
99                 }
100
101                 if (obd->type == OB_MBALL) {
102                         continue;
103                 }
104
105                 verify_id_proeprties_freed(data);
106
107                 data->dupli_object_current = dob;
108
109                 /* Temporary object to evaluate. */
110                 Object *dupli_parent = data->dupli_parent;
111                 Object *temp_dupli_object = &data->temp_dupli_object;
112                 *temp_dupli_object = *dob->ob;
113                 temp_dupli_object->select_color = dupli_parent->select_color;
114                 temp_dupli_object->base_flag = dupli_parent->base_flag | BASE_FROMDUPLI;
115
116                 /* Duplicated elements shouldn't care whether their original collection is visible or not. */
117                 temp_dupli_object->base_flag |= BASE_VISIBLED;
118
119                 if (BKE_object_is_visible(temp_dupli_object, (eObjectVisibilityCheck)data->visibility_check) == false) {
120                         continue;
121                 }
122
123                 temp_dupli_object->transflag &= ~OB_DUPLI;
124
125                 if (dob->collection_properties != NULL) {
126                         temp_dupli_object->base_collection_properties = dob->collection_properties;
127                         IDP_MergeGroup(temp_dupli_object->base_collection_properties,
128                                        dupli_parent->base_collection_properties,
129                                        false);
130                 }
131                 else {
132                         temp_dupli_object->base_collection_properties =
133                                 dupli_parent->base_collection_properties;
134                 }
135
136                 copy_m4_m4(data->temp_dupli_object.obmat, dob->mat);
137                 iter->current = &data->temp_dupli_object;
138                 BLI_assert(
139                         DEG::deg_validate_copy_on_write_datablock(
140                                 &data->temp_dupli_object.id));
141                 return true;
142         }
143
144         return false;
145 }
146
147 static void DEG_iterator_objects_step(BLI_Iterator *iter, DEG::IDDepsNode *id_node)
148 {
149         /* Set it early in case we need to exit and we are running from within a loop. */
150         iter->skip = true;
151
152         DEGObjectIterData *data = (DEGObjectIterData *)iter->data;
153         const ID_Type id_type = GS(id_node->id_orig->name);
154
155         if (id_type != ID_OB) {
156                 return;
157         }
158
159         switch (id_node->linked_state) {
160                 case DEG::DEG_ID_LINKED_DIRECTLY:
161                         if ((data->flag & DEG_ITER_OBJECT_FLAG_LINKED_DIRECTLY) == 0) {
162                                 return;
163                         }
164                         break;
165                 case DEG::DEG_ID_LINKED_VIA_SET:
166                         if ((data->flag & DEG_ITER_OBJECT_FLAG_LINKED_VIA_SET) == 0) {
167                                 return;
168                         }
169                         break;
170                 case DEG::DEG_ID_LINKED_INDIRECTLY:
171                         if ((data->flag & DEG_ITER_OBJECT_FLAG_LINKED_INDIRECTLY) == 0) {
172                                 return;
173                         }
174                         break;
175         }
176
177         Object *object = (Object *)id_node->id_cow;
178         BLI_assert(DEG::deg_validate_copy_on_write_datablock(&object->id));
179
180         if ((BKE_object_is_visible(object, OB_VISIBILITY_CHECK_UNKNOWN_RENDER_MODE) == false) &&
181             ((data->flag & DEG_ITER_OBJECT_FLAG_VISIBLE) != 0))
182         {
183                 return;
184         }
185
186         if ((data->flag & DEG_ITER_OBJECT_FLAG_DUPLI) &&
187             (object->transflag & OB_DUPLI))
188         {
189                 data->dupli_parent = object;
190                 data->dupli_list = object_duplilist(data->graph, data->scene, object);
191                 data->dupli_object_next = (DupliObject *)data->dupli_list->first;
192                 if (BKE_object_is_visible(object, (eObjectVisibilityCheck)data->visibility_check) == false) {
193                         return;
194                 }
195         }
196
197         iter->current = object;
198         iter->skip = false;
199 }
200
201 void DEG_iterator_objects_begin(BLI_Iterator *iter, DEGObjectIterData *data)
202 {
203         Depsgraph *depsgraph = data->graph;
204         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(depsgraph);
205         const size_t num_id_nodes = deg_graph->id_nodes.size();
206
207         if (num_id_nodes == 0) {
208                 iter->valid = false;
209                 return;
210         }
211
212         /* TODO: Calling this forces the scene datablock to be expanded,
213          * otherwise we get crashes on load with copy-on-write. There may
214          * be a better solution for this. */
215         DEG_get_evaluated_view_layer(depsgraph);
216
217         iter->data = data;
218         data->dupli_parent = NULL;
219         data->dupli_list = NULL;
220         data->dupli_object_next = NULL;
221         data->dupli_object_current = NULL;
222         data->scene = DEG_get_evaluated_scene(depsgraph);
223         data->id_node_index = 0;
224         data->num_id_nodes = num_id_nodes;
225         data->visibility_check = (data->mode == DEG_ITER_OBJECT_MODE_RENDER)
226                                  ? OB_VISIBILITY_CHECK_FOR_RENDER
227                                  : OB_VISIBILITY_CHECK_FOR_VIEWPORT;
228
229         DEG::IDDepsNode *id_node = deg_graph->id_nodes[data->id_node_index];
230         DEG_iterator_objects_step(iter, id_node);
231
232         if (iter->skip) {
233                 DEG_iterator_objects_next(iter);
234         }
235 }
236
237 void DEG_iterator_objects_next(BLI_Iterator *iter)
238 {
239         DEGObjectIterData *data = (DEGObjectIterData *)iter->data;
240         Depsgraph *depsgraph = data->graph;
241         DEG::Depsgraph *deg_graph = reinterpret_cast<DEG::Depsgraph *>(depsgraph);
242         do {
243                 iter->skip = false;
244                 if (data->dupli_list) {
245                         if (deg_objects_dupli_iterator_next(iter)) {
246                                 return;
247                         }
248                         else {
249                                 verify_id_proeprties_freed(data);
250                                 free_object_duplilist(data->dupli_list);
251                                 data->dupli_parent = NULL;
252                                 data->dupli_list = NULL;
253                                 data->dupli_object_next = NULL;
254                                 data->dupli_object_current = NULL;
255                         }
256                 }
257
258                 ++data->id_node_index;
259                 if (data->id_node_index == data->num_id_nodes) {
260                         iter->valid = false;
261                         return;
262                 }
263
264                 DEG::IDDepsNode *id_node = deg_graph->id_nodes[data->id_node_index];
265                 DEG_iterator_objects_step(iter, id_node);
266         } while (iter->skip);
267 }
268
269 void DEG_iterator_objects_end(BLI_Iterator *iter)
270 {
271 #ifndef NDEBUG
272         DEGObjectIterData *data = (DEGObjectIterData *)iter->data;
273         /* Force crash in case the iterator data is referenced and accessed down
274          * the line. (T51718)
275          */
276         memset(&data->temp_dupli_object, 0xff, sizeof(data->temp_dupli_object));
277 #else
278         (void) iter;
279 #endif
280 }