Depsgraph: Report number of objects to which update as flished
[blender-staging.git] / source / blender / depsgraph / intern / eval / deg_eval_flush.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) 2013 Blender Foundation.
19  * All rights reserved.
20  *
21  * Original Author: Joshua Leung
22  * Contributor(s): None Yet
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27 /** \file blender/depsgraph/intern/eval/deg_eval_flush.cc
28  *  \ingroup depsgraph
29  *
30  * Core routines for how the Depsgraph works.
31  */
32
33 #include "intern/eval/deg_eval_flush.h"
34
35 // TODO(sergey): Use some sort of wrapper.
36 #include <deque>
37
38 extern "C" {
39 #include "DNA_object_types.h"
40
41 #include "BLI_utildefines.h"
42 #include "BLI_task.h"
43 #include "BLI_ghash.h"
44
45 #include "DEG_depsgraph.h"
46 } /* extern "C" */
47
48 #include "intern/nodes/deg_node.h"
49 #include "intern/nodes/deg_node_component.h"
50 #include "intern/nodes/deg_node_operation.h"
51
52 #include "intern/depsgraph_intern.h"
53 #include "util/deg_util_foreach.h"
54
55 namespace DEG {
56
57 namespace {
58
59 // TODO(sergey): De-duplicate with depsgraph_tag,cc
60 void lib_id_recalc_tag(Main *bmain, ID *id)
61 {
62         id->tag |= LIB_TAG_ID_RECALC;
63         DEG_id_type_tag(bmain, GS(id->name));
64 }
65
66 void lib_id_recalc_data_tag(Main *bmain, ID *id)
67 {
68         id->tag |= LIB_TAG_ID_RECALC_DATA;
69         DEG_id_type_tag(bmain, GS(id->name));
70 }
71
72 }  /* namespace */
73
74 typedef std::deque<OperationDepsNode *> FlushQueue;
75
76 static void flush_init_func(void *data_v, int i)
77 {
78         /* ID node's done flag is used to avoid multiple editors update
79          * for the same ID.
80          */
81         Depsgraph *graph = (Depsgraph *)data_v;
82         OperationDepsNode *node = graph->operations[i];
83         ComponentDepsNode *comp_node = node->owner;
84         IDDepsNode *id_node = comp_node->owner;
85         id_node->done = 0;
86         comp_node->done = 0;
87         node->scheduled = false;
88 }
89
90 /* Flush updates from tagged nodes outwards until all affected nodes
91  * are tagged.
92  */
93 void deg_graph_flush_updates(Main *bmain, Depsgraph *graph)
94 {
95         /* Sanity check. */
96         if (graph == NULL) {
97                 return;
98         }
99
100         /* Nothing to update, early out. */
101         if (BLI_gset_size(graph->entry_tags) == 0) {
102                 return;
103         }
104
105         /* TODO(sergey): With a bit of flag magic we can get rid of this
106          * extra loop.
107          */
108         const int num_operations = graph->operations.size();
109         const bool do_threads = num_operations > 256;
110         BLI_task_parallel_range(0,
111                                 num_operations,
112                                 graph,
113                                 flush_init_func,
114                                 do_threads);
115
116         FlushQueue queue;
117         /* Starting from the tagged "entry" nodes, flush outwards... */
118         /* NOTE: Also need to ensure that for each of these, there is a path back to
119          *       root, or else they won't be done.
120          * NOTE: Count how many nodes we need to handle - entry nodes may be
121          *       component nodes which don't count for this purpose!
122          */
123         GSET_FOREACH_BEGIN(OperationDepsNode *, node, graph->entry_tags)
124         {
125                 queue.push_back(node);
126                 node->scheduled = true;
127         }
128         GSET_FOREACH_END();
129
130         int num_flushed_objects = 0;
131         while (!queue.empty()) {
132                 OperationDepsNode *node = queue.front();
133                 queue.pop_front();
134
135                 for (;;) {
136                         node->flag |= DEPSOP_FLAG_NEEDS_UPDATE;
137
138                         ComponentDepsNode *comp_node = node->owner;
139                         IDDepsNode *id_node = comp_node->owner;
140
141                         ID *id = id_node->id;
142                         if(id_node->done == 0) {
143                                 deg_editors_id_update(bmain, id);
144                                 lib_id_recalc_tag(bmain, id);
145                                 /* TODO(sergey): For until we've got proper data nodes in the graph. */
146                                 lib_id_recalc_data_tag(bmain, id);
147                         }
148
149                         if(comp_node->done == 0) {
150                                 Object *object = NULL;
151                                 if (GS(id->name) == ID_OB) {
152                                         object = (Object *)id;
153                                         if(id_node->done == 0) {
154                                                 ++num_flushed_objects;
155                                         }
156                                 }
157                                 foreach (OperationDepsNode *op, comp_node->operations) {
158                                         op->flag |= DEPSOP_FLAG_NEEDS_UPDATE;
159                                 }
160                                 if (object != NULL) {
161                                         /* This code is used to preserve those areas which does
162                                          * direct object update,
163                                          *
164                                          * Plus it ensures visibility changes and relations and
165                                          * layers visibility update has proper flags to work with.
166                                          */
167                                         if (comp_node->type == DEPSNODE_TYPE_ANIMATION) {
168                                                 object->recalc |= OB_RECALC_TIME;
169                                         }
170                                         else if (comp_node->type == DEPSNODE_TYPE_TRANSFORM) {
171                                                 object->recalc |= OB_RECALC_OB;
172                                         }
173                                         else {
174                                                 object->recalc |= OB_RECALC_DATA;
175                                         }
176                                 }
177                         }
178
179                         id_node->done = 1;
180                         comp_node->done = 1;
181
182                         /* Flush to nodes along links... */
183                         if (node->outlinks.size() == 1) {
184                                 OperationDepsNode *to_node = (OperationDepsNode *)node->outlinks[0]->to;
185                                 if (to_node->scheduled == false) {
186                                         to_node->scheduled = true;
187                                         node = to_node;
188                                 }
189                                 else {
190                                         break;
191                                 }
192                         }
193                         else {
194                                 foreach (DepsRelation *rel, node->outlinks) {
195                                         OperationDepsNode *to_node = (OperationDepsNode *)rel->to;
196                                         if (to_node->scheduled == false) {
197                                                 queue.push_front(to_node);
198                                                 to_node->scheduled = true;
199                                         }
200                                 }
201                                 break;
202                         }
203                 }
204         }
205         DEG_DEBUG_PRINTF("Update flushed to %d objects\n", num_flushed_objects);
206 }
207
208 static void graph_clear_func(void *data_v, int i)
209 {
210         Depsgraph *graph = (Depsgraph *)data_v;
211         OperationDepsNode *node = graph->operations[i];
212         /* Clear node's "pending update" settings. */
213         node->flag &= ~(DEPSOP_FLAG_DIRECTLY_MODIFIED | DEPSOP_FLAG_NEEDS_UPDATE);
214 }
215
216 /* Clear tags from all operation nodes. */
217 void deg_graph_clear_tags(Depsgraph *graph)
218 {
219         /* Go over all operation nodes, clearing tags. */
220         const int num_operations = graph->operations.size();
221         const bool do_threads = num_operations > 256;
222         BLI_task_parallel_range(0, num_operations, graph, graph_clear_func, do_threads);
223         /* Clear any entry tags which haven't been flushed. */
224         BLI_gset_clear(graph->entry_tags, NULL);
225 }
226
227 }  // namespace DEG