Depsgraph: More fixes for shape keys
[blender.git] / source / blender / depsgraph / intern / eval / deg_eval_copy_on_write.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) 20137Blender Foundation.
19  * All rights reserved.
20  *
21  * Original Author: Sergey Sharybin
22  * Contributor(s): None Yet
23  *
24  * ***** END GPL LICENSE BLOCK *****
25  */
26
27
28 /** \file blender/depsgraph/intern/eval/deg_eval_copy_on_write.cc
29  *  \ingroup depsgraph
30  */
31
32 /* Enable special; trickery to treat nested owned IDs (such as nodetree of
33  * material) to be handled in same way as "real" datablocks, even tho some
34  * internal BKE routines doesn't treat them like that.
35  *
36  * TODO(sergey): Re-evaluate that after new ID handling is in place.
37  */
38 #define NESTED_ID_NASTY_WORKAROUND
39
40 #include "intern/eval/deg_eval_copy_on_write.h"
41
42 #include <cstring>
43
44 #include "BLI_utildefines.h"
45 #include "BLI_threads.h"
46 #include "BLI_string.h"
47
48 #include "BKE_global.h"
49 #include "BKE_idprop.h"
50 #include "BKE_layer.h"
51 #include "BKE_library.h"
52 #include "BKE_main.h"
53 #include "BKE_scene.h"
54
55 #include "DEG_depsgraph.h"
56
57 #include "MEM_guardedalloc.h"
58
59 extern "C" {
60 #include "DNA_ID.h"
61 #include "DNA_mesh_types.h"
62 #include "DNA_scene_types.h"
63 #include "DNA_object_types.h"
64
65 #ifdef NESTED_ID_NASTY_WORKAROUND
66 #  include "DNA_curve_types.h"
67 #  include "DNA_key_types.h"
68 #  include "DNA_lamp_types.h"
69 #  include "DNA_lattice_types.h"
70 #  include "DNA_linestyle_types.h"
71 #  include "DNA_material_types.h"
72 #  include "DNA_node_types.h"
73 #  include "DNA_texture_types.h"
74 #  include "DNA_world_types.h"
75 #endif
76
77 #include "BKE_action.h"
78 #include "BKE_editmesh.h"
79 #include "BKE_library_query.h"
80 #include "BKE_object.h"
81 }
82
83 #include "intern/depsgraph.h"
84 #include "intern/builder/deg_builder_nodes.h"
85 #include "intern/nodes/deg_node.h"
86 #include "intern/nodes/deg_node_id.h"
87
88 namespace DEG {
89
90 #define DEBUG_PRINT if (G.debug & G_DEBUG_DEPSGRAPH) printf
91
92 namespace {
93
94 #ifdef NESTED_ID_NASTY_WORKAROUND
95 union NestedIDHackTempStorage {
96         Curve curve;
97         FreestyleLineStyle linestyle;
98         Lamp lamp;
99         Lattice lattice;
100         Material material;
101         Mesh mesh;
102         Scene scene;
103         Tex tex;
104         World world;
105 };
106
107 /* Set nested owned ID pointers to NULL. */
108 void nested_id_hack_discard_pointers(ID *id_cow)
109 {
110         switch (GS(id_cow->name)) {
111 #  define SPECIAL_CASE(id_type, dna_type, field)  \
112                 case id_type:                             \
113                 {                                         \
114                         ((dna_type *)id_cow)->field = NULL;   \
115                         break;                                \
116                 }
117
118                 SPECIAL_CASE(ID_LS, FreestyleLineStyle, nodetree)
119                 SPECIAL_CASE(ID_LA, Lamp, nodetree)
120                 SPECIAL_CASE(ID_MA, Material, nodetree)
121                 SPECIAL_CASE(ID_SCE, Scene, nodetree)
122                 SPECIAL_CASE(ID_TE, Tex, nodetree)
123                 SPECIAL_CASE(ID_WO, World, nodetree)
124
125                 SPECIAL_CASE(ID_CU, Curve, key)
126                 SPECIAL_CASE(ID_LT, Lattice, key)
127                 SPECIAL_CASE(ID_ME, Mesh, key)
128
129 #  undef SPECIAL_CASE
130
131                 default:
132                         break;
133         }
134 }
135
136 /* Set ID pointer of nested owned IDs (nodetree, key) to NULL.
137  *
138  * Return pointer to a new ID to be used.
139  */
140 const ID *nested_id_hack_get_discarded_pointers(NestedIDHackTempStorage *storage,
141                                                 const ID *id)
142 {
143         switch (GS(id->name)) {
144 #  define SPECIAL_CASE(id_type, dna_type, field, variable)  \
145                 case id_type:                                       \
146                 {                                                   \
147                         storage->variable = *(dna_type *)id;            \
148                         storage->variable.field = NULL;                 \
149                         return &storage->variable.id;                   \
150                 }
151
152                 SPECIAL_CASE(ID_LS, FreestyleLineStyle, nodetree, linestyle)
153                 SPECIAL_CASE(ID_LA, Lamp, nodetree, lamp)
154                 SPECIAL_CASE(ID_MA, Material, nodetree, material)
155                 SPECIAL_CASE(ID_SCE, Scene, nodetree, scene)
156                 SPECIAL_CASE(ID_TE, Tex, nodetree, tex)
157                 SPECIAL_CASE(ID_WO, World, nodetree, world)
158
159                 SPECIAL_CASE(ID_CU, Curve, key, curve)
160                 SPECIAL_CASE(ID_LT, Lattice, key, lattice)
161                 SPECIAL_CASE(ID_ME, Mesh, key, mesh)
162
163 #  undef SPECIAL_CASE
164
165                 default:
166                         break;
167         }
168         return id;
169 }
170
171 /* Set ID pointer of nested owned IDs (nodetree, key) to the original value. */
172 void nested_id_hack_restore_pointers(const ID *old_id, ID *new_id)
173 {
174         if (new_id == NULL) {
175                 return;
176         }
177         switch (GS(old_id->name)) {
178 #  define SPECIAL_CASE(id_type, dna_type, field)    \
179                 case id_type:                               \
180                 {                                           \
181                         ((dna_type *)(new_id))->field =         \
182                                 ((dna_type *)(old_id))->field;  \
183                         break;                                  \
184                 }
185
186                 SPECIAL_CASE(ID_LS, FreestyleLineStyle, nodetree)
187                 SPECIAL_CASE(ID_LA, Lamp, nodetree)
188                 SPECIAL_CASE(ID_MA, Material, nodetree)
189                 SPECIAL_CASE(ID_SCE, Scene, nodetree)
190                 SPECIAL_CASE(ID_TE, Tex, nodetree)
191                 SPECIAL_CASE(ID_WO, World, nodetree)
192
193                 SPECIAL_CASE(ID_CU, Curve, key)
194                 SPECIAL_CASE(ID_LT, Lattice, key)
195                 SPECIAL_CASE(ID_ME, Mesh, key)
196
197 #undef SPECIAL_CASE
198                 default:
199                         break;
200         }
201 }
202
203 /* Remap pointer of nested owned IDs (nodetree. key) to the new ID values. */
204 void ntree_hack_remap_pointers(const Depsgraph *depsgraph, ID *id_cow)
205 {
206         switch (GS(id_cow->name)) {
207 #  define SPECIAL_CASE(id_type, dna_type, field, field_type)                   \
208                 case id_type:                                                          \
209                 {                                                                      \
210                         dna_type *data = (dna_type *)id_cow;                               \
211                         if (data->field != NULL) {                                         \
212                                 ID *ntree_id_cow = depsgraph->get_cow_id(&data->field->id);    \
213                                 if (ntree_id_cow != NULL) {                                    \
214                                         DEG_COW_PRINT("    Remapping datablock for %s: id_orig=%p id_cow=%p\n", \
215                                                       data->field->id.name,                        \
216                                                       data->field,                                 \
217                                                       ntree_id_cow);                               \
218                                         data->field = (field_type *)ntree_id_cow;                  \
219                                 }                                                              \
220                         }                                                                  \
221                         break;                                                             \
222                 }
223
224                 SPECIAL_CASE(ID_LS, FreestyleLineStyle, nodetree, bNodeTree)
225                 SPECIAL_CASE(ID_LA, Lamp, nodetree, bNodeTree)
226                 SPECIAL_CASE(ID_MA, Material, nodetree, bNodeTree)
227                 SPECIAL_CASE(ID_SCE, Scene, nodetree, bNodeTree)
228                 SPECIAL_CASE(ID_TE, Tex, nodetree, bNodeTree)
229                 SPECIAL_CASE(ID_WO, World, nodetree, bNodeTree)
230
231                 SPECIAL_CASE(ID_CU, Curve, key, Key)
232                 SPECIAL_CASE(ID_LT, Lattice, key, Key)
233                 SPECIAL_CASE(ID_ME, Mesh, key, Key)
234
235 #undef SPECIAL_CASE
236                 default:
237                         break;
238         }
239 }
240 #endif  /* NODETREE_NASTY_WORKAROUND */
241
242 struct ValidateData {
243         bool is_valid;
244 };
245
246 /* Similar to generic id_copy() but does not require main and assumes pointer
247  * is already allocated,
248  */
249 bool id_copy_inplace_no_main(const ID *id, ID *newid)
250 {
251         const ID *id_for_copy = id;
252
253 #ifdef NESTED_ID_NASTY_WORKAROUND
254         NestedIDHackTempStorage id_hack_storage;
255         id_for_copy = nested_id_hack_get_discarded_pointers(&id_hack_storage, id);
256 #endif
257
258         bool result = BKE_id_copy_ex(NULL,
259                                      (ID *)id_for_copy,
260                                      &newid,
261                                      LIB_ID_CREATE_NO_MAIN |
262                                      LIB_ID_CREATE_NO_USER_REFCOUNT |
263                                      LIB_ID_CREATE_NO_ALLOCATE |
264                                      LIB_ID_CREATE_NO_DEG_TAG,
265                                      false);
266
267 #ifdef NESTED_ID_NASTY_WORKAROUND
268         if (result) {
269                 nested_id_hack_restore_pointers(id, newid);
270         }
271 #endif
272
273         return result;
274 }
275
276 /* Similar to BKE_scene_copy() but does not require main and assumes pointer
277  * is already allocated.
278  */
279 bool scene_copy_inplace_no_main(const Scene *scene, Scene *new_scene)
280 {
281         const ID *id_for_copy = &scene->id;
282
283 #ifdef NESTED_ID_NASTY_WORKAROUND
284         NestedIDHackTempStorage id_hack_storage;
285         id_for_copy = nested_id_hack_get_discarded_pointers(&id_hack_storage,
286                                                             &scene->id);
287 #endif
288
289         bool result = BKE_id_copy_ex(NULL,
290                                      id_for_copy,
291                                      (ID **)&new_scene,
292                                      LIB_ID_COPY_ACTIONS |
293                                      LIB_ID_CREATE_NO_MAIN |
294                                      LIB_ID_CREATE_NO_USER_REFCOUNT |
295                                      LIB_ID_CREATE_NO_ALLOCATE |
296                                      LIB_ID_CREATE_NO_DEG_TAG,
297                                      false);
298
299 #ifdef NESTED_ID_NASTY_WORKAROUND
300         if (result) {
301                 nested_id_hack_restore_pointers(&scene->id, &new_scene->id);
302         }
303 #endif
304
305         return result;
306 }
307
308 /* Check whether given ID is expanded or still a shallow copy. */
309 BLI_INLINE bool check_datablock_expanded(const ID *id_cow)
310 {
311         return (id_cow->name[0] != '\0');
312 }
313
314 /* Check whether datablock was already expanded during depsgraph
315  * construction.
316  */
317 static bool check_datablock_expanded_at_construction(const ID *id_orig)
318 {
319         const ID_Type id_type = GS(id_orig->name);
320         return (id_type == ID_SCE) ||
321                (id_type == ID_OB && ((Object *)id_orig)->type == OB_ARMATURE) ||
322                (id_type == ID_AR);
323 }
324
325 /* Those are datablocks which are not covered by dependency graph and hence
326  * does not need any remapping or anything.
327  *
328  * TODO(sergey): How to make it more robust for the future, so we don't have
329  * to maintain exception lists all over the code?
330  */
331 static bool check_datablocks_copy_on_writable(const ID *id_orig)
332 {
333         const ID_Type id_type = GS(id_orig->name);
334         /* We shouldn't bother if copied ID is same as original one. */
335         if (!deg_copy_on_write_is_needed(id_orig)) {
336                 return false;
337         }
338         return !ELEM(id_type, ID_BR,
339                               ID_LS,
340                               ID_AC,
341                               ID_PAL);
342 }
343
344 /* Callback for BKE_library_foreach_ID_link which remaps original ID pointer
345  * with the one created by CoW system.
346  */
347
348 struct RemapCallbackUserData {
349         /* Dependency graph for which remapping is happening. */
350         const Depsgraph *depsgraph;
351         /* Temporarily allocated memory for copying purposes. This ID will
352          * be discarded after expanding is done, so need to make sure temp_id
353          * is replaced with proper real_id.
354          *
355          * NOTE: This is due to our logic of "inplace" duplication, where we
356          * use generic duplication routines (which gives us new ID) which then
357          * is followed with copying data to a placeholder we prepared before and
358          * discarding pointer returned by duplication routines.
359          */
360         const ID *temp_id;
361         ID *real_id;
362         /* Create placeholder for ID nodes for cases when we need to remap original
363          * ID to it[s CoW version but we don't have required ID node yet.
364          *
365          * This happens when expansion happens a ta construction time.
366          */
367         DepsgraphNodeBuilder *node_builder;
368         bool create_placeholders;
369 };
370
371 int foreach_libblock_remap_callback(void *user_data_v,
372                                     ID *id_self,
373                                     ID **id_p,
374                                     int /*cb_flag*/)
375 {
376         RemapCallbackUserData *user_data = (RemapCallbackUserData *)user_data_v;
377         const Depsgraph *depsgraph = user_data->depsgraph;
378         if (*id_p != NULL) {
379                 ID *id_orig = *id_p;
380                 if (id_orig == user_data->temp_id) {
381                         DEG_COW_PRINT("    Remapping datablock for %s: id_temp=%p id_cow=%p\n",
382                                       id_orig->name, id_orig, user_data->real_id);
383                         *id_p = user_data->real_id;
384                 }
385                 else if (check_datablocks_copy_on_writable(id_orig)) {
386                         ID *id_cow;
387                         if (user_data->create_placeholders) {
388                                 /* Special workaround to stop creating temp datablocks for
389                                  * objects which are coming from scene's collection and which
390                                  * are never linked to any of layers.
391                                  *
392                                  * TODO(sergey): Ideally we need to tell ID looper to ignore
393                                  * those or at least make it more reliable check where the
394                                  * pointer is coming from.
395                                  */
396                                 const ID_Type id_type = GS(id_orig->name);
397                                 const ID_Type id_type_self = GS(id_self->name);
398                                 if (id_type == ID_OB && id_type_self == ID_SCE) {
399                                         IDDepsNode *id_node = depsgraph->find_id_node(id_orig);
400                                         if (id_node == NULL) {
401                                                 id_cow = id_orig;
402                                         }
403                                         else {
404                                                 id_cow = id_node->id_cow;
405                                         }
406                                 }
407                                 else {
408                                         id_cow = user_data->node_builder->ensure_cow_id(id_orig);
409                                 }
410                         }
411                         else {
412                                 id_cow = depsgraph->get_cow_id(id_orig);
413                         }
414                         BLI_assert(id_cow != NULL);
415                         DEG_COW_PRINT("    Remapping datablock for %s: id_orig=%p id_cow=%p\n",
416                                       id_orig->name, id_orig, id_cow);
417                         *id_p = id_cow;
418                 }
419         }
420         return IDWALK_RET_NOP;
421 }
422
423 /* Do some special treatment of data transfer from original ID to it's
424  * CoW complementary part.
425  *
426  * Only use for the newly created CoW datablocks.
427  */
428 void update_special_pointers(const Depsgraph *depsgraph,
429                              const ID *id_orig, ID *id_cow)
430 {
431         const ID_Type type = GS(id_orig->name);
432         switch (type) {
433                 case ID_OB:
434                 {
435                         /* Ensure we don't drag someone's else derived mesh to the
436                          * new copy of the object.
437                          */
438                         Object *object_cow = (Object *)id_cow;
439                         const Object *object_orig = (const Object *)id_orig;
440                         (void) object_cow;  /* Ignored for release builds. */
441                         BLI_assert(object_cow->derivedFinal == NULL);
442                         BLI_assert(object_cow->derivedDeform == NULL);
443                         object_cow->mode = object_orig->mode;
444                         break;
445                 }
446                 case ID_ME:
447                 {
448                         /* For meshes we need to update edit_btmesh to make it to point
449                          * to the CoW version of object.
450                          *
451                          * This is kind of confusing, because actual bmesh is not owned by
452                          * the CoW object, so need to be accurate about using link from
453                          * edit_btmesh to object.
454                          */
455                         const Mesh *mesh_orig = (const Mesh *)id_orig;
456                         Mesh *mesh_cow = (Mesh *)id_cow;
457                         if (mesh_orig->edit_btmesh != NULL) {
458                                 mesh_cow->edit_btmesh = (BMEditMesh *)MEM_dupallocN(mesh_orig->edit_btmesh);
459                                 mesh_cow->edit_btmesh->ob =
460                                         (Object *)depsgraph->get_cow_id(&mesh_orig->edit_btmesh->ob->id);
461                                 mesh_cow->edit_btmesh->derivedFinal = NULL;
462                                 mesh_cow->edit_btmesh->derivedCage = NULL;
463                         }
464                         break;
465                 }
466                 case ID_SCE:
467                 {
468                         const Scene *scene_orig = (const Scene *)id_orig;
469                         Scene *scene_cow = (Scene *)id_cow;
470                         if (scene_orig->obedit != NULL) {
471                                 scene_cow->obedit = (Object *)depsgraph->get_cow_id(&scene_orig->obedit->id);
472                         }
473                         else {
474                                 scene_cow->obedit = NULL;
475                         }
476                         break;
477                 }
478                 default:
479                         break;
480         }
481 }
482
483 void update_copy_on_write_layer_collections(
484         ListBase *layer_collections_cow,
485         const ListBase *layer_collections_orig);
486
487 void update_copy_on_write_layer_collection(
488         LayerCollection *layer_collection_cow,
489         const LayerCollection *layer_collection_orig)
490 {
491         // Make a local copy of original layer collection, so we can start
492         // modifying it.
493         LayerCollection local = *layer_collection_orig;
494         // Copy all pointer data from original CoW version of layer collection.
495         local.next = layer_collection_cow->next;
496         local.prev = layer_collection_cow->prev;
497         local.scene_collection = layer_collection_cow->scene_collection;
498         local.object_bases = layer_collection_cow->object_bases;
499         local.overrides = layer_collection_cow->overrides;
500         local.layer_collections = layer_collection_cow->layer_collections;
501         local.properties = layer_collection_cow->properties;
502         local.properties_evaluated = layer_collection_cow->properties_evaluated;
503         // Synchronize pointer-related data.
504         IDP_Reset(local.properties, layer_collection_orig->properties);
505         // Copy synchronized version back.
506         *layer_collection_cow = local;
507         // Recurs into nested layer collections.
508         update_copy_on_write_layer_collections(
509                 &layer_collection_cow->layer_collections,
510                 &layer_collection_orig->layer_collections);
511 }
512
513 void update_copy_on_write_layer_collections(
514         ListBase *layer_collections_cow,
515         const ListBase *layer_collections_orig)
516 {
517         const LayerCollection *layer_collection_orig =
518                 (const LayerCollection *)layer_collections_orig->first;
519         LayerCollection *layer_collection_cow =
520                 (LayerCollection *)layer_collections_cow->first;
521         while (layer_collection_orig != NULL) {
522                 update_copy_on_write_layer_collection(layer_collection_cow,
523                                                       layer_collection_orig);
524                 layer_collection_orig = layer_collection_orig->next;
525                 layer_collection_cow = layer_collection_cow->next;
526         }
527 }
528
529 void update_copy_on_write_view_layer(const Depsgraph *depsgraph,
530                                      ViewLayer *view_layer_cow,
531                                      const ViewLayer *view_layer_orig)
532 {
533         // Update pointers to active base.
534         if (view_layer_orig->basact == NULL) {
535                 view_layer_cow->basact = NULL;
536         }
537         else {
538                 const Object *obact_orig = view_layer_orig->basact->object;
539                 Object *obact_cow = (Object *)depsgraph->get_cow_id(&obact_orig->id);
540                 view_layer_cow->basact = BKE_view_layer_base_find(view_layer_cow, obact_cow);
541         }
542         // Update base flags.
543         //
544         // TODO(sergey): We should probably check visibled/selectabled.
545         // flag here?
546         const Base *base_orig = (Base *)view_layer_orig->object_bases.first;
547         Base *base_cow = (Base *)view_layer_cow->object_bases.first;;
548         while (base_orig != NULL) {
549                 base_cow->flag = base_orig->flag;
550                 base_orig = base_orig->next;
551                 base_cow = base_cow->next;
552         }
553         // Synchronize settings.
554         view_layer_cow->active_collection = view_layer_orig->active_collection;
555         view_layer_cow->flag = view_layer_orig->flag;
556         view_layer_cow->layflag = view_layer_orig->layflag;
557         view_layer_cow->passflag = view_layer_orig->passflag;
558         view_layer_cow->pass_alpha_threshold = view_layer_orig->pass_alpha_threshold;
559         // Synchronize ID properties.
560         IDP_Reset(view_layer_cow->properties, view_layer_orig->properties);
561         IDP_Reset(view_layer_cow->id_properties, view_layer_orig->id_properties);
562         // Synchronize layer collections.
563         update_copy_on_write_layer_collections(
564                 &view_layer_cow->layer_collections,
565                 &view_layer_orig->layer_collections);
566 }
567
568 void update_copy_on_write_view_layers(const Depsgraph *depsgraph,
569                                       Scene *scene_cow,
570                                       const Scene *scene_orig)
571 {
572         const ViewLayer *view_layer_orig = (const ViewLayer *)scene_orig->view_layers.first;
573         ViewLayer *view_layer_cow = (ViewLayer *)scene_cow->view_layers.first;
574         while (view_layer_orig != NULL) {
575                 update_copy_on_write_view_layer(depsgraph,
576                                                 view_layer_cow,
577                                                 view_layer_orig);
578                 view_layer_orig = view_layer_orig->next;
579                 view_layer_cow = view_layer_cow->next;
580         }
581 }
582
583 void update_copy_on_write_scene_collections(
584         ListBase *collections_cow,
585         const ListBase *collections_orig);
586
587 void update_copy_on_write_scene_collection(
588         SceneCollection *collection_cow,
589         const SceneCollection *collection_orig)
590 {
591         collection_cow->active_object_index = collection_orig->active_object_index;
592         update_copy_on_write_scene_collections(
593                 &collection_cow->scene_collections,
594                 &collection_orig->scene_collections);
595 }
596
597 void update_copy_on_write_scene_collections(
598         ListBase *collections_cow,
599         const ListBase *collections_orig)
600 {
601         const SceneCollection *nested_collection_orig =
602                 (const SceneCollection *)collections_orig->first;
603         SceneCollection *nested_collection_cow =
604                 (SceneCollection *)collections_cow->first;
605         while (nested_collection_orig != NULL) {
606                 update_copy_on_write_scene_collection(
607                         nested_collection_cow,
608                         nested_collection_orig);
609                 nested_collection_orig = nested_collection_orig->next;
610                 nested_collection_cow = nested_collection_cow->next;
611         }
612 }
613
614 /* Update copy-on-write version of scene from original scene. */
615 void update_copy_on_write_scene(const Depsgraph *depsgraph,
616                                 Scene *scene_cow,
617                                 const Scene *scene_orig)
618 {
619         // Some non-pointer data sync, current frame for now.
620         // TODO(sergey): Are we missing something here?
621         scene_cow->r.cfra = scene_orig->r.cfra;
622         scene_cow->r.subframe = scene_orig->r.subframe;
623         // Update view layers and collections.
624         update_copy_on_write_view_layers(depsgraph, scene_cow, scene_orig);
625         update_copy_on_write_scene_collection(scene_cow->collection,
626                                               scene_orig->collection);
627         // Update edit object pointer.
628         if (scene_orig->obedit != NULL) {
629                 scene_cow->obedit = (Object *)depsgraph->get_cow_id(&scene_orig->obedit->id);
630         }
631         else {
632                 scene_cow->obedit = NULL;
633         }
634         /* Synchronize active render engine. */
635         BLI_strncpy(scene_cow->view_render.engine_id,
636                     scene_orig->view_render.engine_id,
637                     sizeof(scene_cow->view_render.engine_id));
638         /* TODO(sergey): What else do we need here? */
639 }
640
641 /* Update copy-on-write version of armature object from original scene. */
642 void update_copy_on_write_object(const Depsgraph * /*depsgraph*/,
643                                  Object *object_cow,
644                                  const Object *object_orig)
645 {
646         /* TODO(sergey): This function might be split into a smaller ones,
647          * reused for different updates. And maybe even moved to BKE.
648          */
649         /* Update armature/pose related flags. */
650         bPose *pose_cow = object_cow->pose;
651         const bPose *pose_orig = object_orig->pose;
652         extract_pose_from_pose(pose_cow, pose_orig);
653         /* Update object itself. */
654         BKE_object_transform_copy(object_cow, object_orig);
655         object_cow->mode = object_orig->mode;
656 }
657
658 /* Update copy-on-write version of datablock from it's original ID without re-building
659  * the whole datablock from scratch.
660  *
661  * Used for such special cases as scene collections and armatures, which can not use full
662  * re-alloc due to pointers used as function bindings.
663  */
664 void update_copy_on_write_datablock(const Depsgraph *depsgraph,
665                                     const ID *id_orig, ID *id_cow)
666 {
667         bool ok = false;
668         const ID_Type id_type = GS(id_orig->name);
669         switch (id_type) {
670                 case ID_SCE: {
671                         const Scene *scene_orig = (const Scene *)id_orig;
672                         Scene *scene_cow = (Scene *)id_cow;
673                         update_copy_on_write_scene(depsgraph, scene_cow, scene_orig);
674                         ok = true;
675                         break;
676                 }
677                 case ID_OB: {
678                         const Object *object_orig = (const Object *)id_orig;
679                         Object *object_cow = (Object *)id_cow;
680                         if (object_orig->type == OB_ARMATURE) {
681                                 update_copy_on_write_object(depsgraph,
682                                                             object_cow,
683                                                             object_orig);
684                                 ok = true;
685                         }
686                         break;
687                 }
688                 case ID_AR:
689                         /* Nothing to do currently. */
690                         ok = true;
691                         break;
692                 default:
693                         break;
694         }
695         // TODO(sergey): Other ID types here.
696         if (!ok) {
697                 BLI_assert(!"Missing update logic of expanded datablock");
698         }
699 }
700
701 /* This callback is used to validate that all nested ID datablocks are
702  * properly expanded.
703  */
704 int foreach_libblock_validate_callback(void *user_data,
705                                        ID * /*id_self*/,
706                                        ID **id_p,
707                                        int /*cb_flag*/)
708 {
709         ValidateData *data = (ValidateData *)user_data;
710         if (*id_p != NULL) {
711                 if (!check_datablock_expanded(*id_p)) {
712                         data->is_valid = false;
713                         /* TODO(sergey): Store which is is not valid? */
714                 }
715         }
716         return IDWALK_RET_NOP;
717 }
718
719 }  // namespace
720
721 /* Actual implementation of logic which "expands" all the data which was not
722  * yet copied-on-write.
723  *
724  * NOTE: Expects that CoW datablock is empty.
725  */
726 ID *deg_expand_copy_on_write_datablock(const Depsgraph *depsgraph,
727                                        const IDDepsNode *id_node,
728                                        DepsgraphNodeBuilder *node_builder,
729                                        bool create_placeholders)
730 {
731         BLI_assert(!create_placeholders ||
732                    check_datablock_expanded_at_construction(id_node->id_orig));
733         const ID *id_orig = id_node->id_orig;
734         ID *id_cow = id_node->id_cow;
735         /* No need to expand such datablocks, their copied ID is same as original
736          * one already.
737          */
738         if (!deg_copy_on_write_is_needed(id_orig)) {
739                 return id_cow;
740         }
741         DEG_COW_PRINT("Expanding datablock for %s: id_orig=%p id_cow=%p\n",
742                       id_orig->name, id_orig, id_cow);
743         /* Sanity checks. */
744         /* NOTE: Disabled for now, conflicts when re-using evaluated datablock when
745          * rebuilding dependencies.
746          */
747         if (check_datablock_expanded(id_cow) && create_placeholders) {
748                 deg_free_copy_on_write_datablock(id_cow);
749         }
750         // BLI_assert(check_datablock_expanded(id_cow) == false);
751         /* Copy data from original ID to a copied version. */
752         /* TODO(sergey): Avoid doing full ID copy somehow, make Mesh to reference
753          * original geometry arrays for until those are modified.
754          */
755         /* TODO(sergey): We do some trickery with temp bmain and extra ID pointer
756          * just to be able to use existing API. Ideally we need to replace this with
757          * in-place copy from existing datablock to a prepared memory.
758          *
759          * NOTE: We don't use BKE_main_{new,free} because:
760          * - We don't want heap-allocations here.
761          * - We don't want bmain's content to be freed when main is freed.
762          */
763         bool done = false;
764         /* Need to make sure the possibly temporary allocated memory is correct for
765          * until we are fully done with remapping original pointers with copied on
766          * write ones.
767          */
768         ID *newid = NULL;
769         /* First we handle special cases which are not covered by id_copy() yet.
770          * or cases where we want to do something smarter than simple datablock
771          * copy.
772          */
773         const ID_Type id_type = GS(id_orig->name);
774         switch (id_type) {
775                 case ID_SCE:
776                 {
777                         done = scene_copy_inplace_no_main((Scene *)id_orig, (Scene *)id_cow);
778                         break;
779                 }
780                 case ID_ME:
781                 {
782                         /* TODO(sergey): Ideally we want to handle meshes in a special
783                          * manner here to avoid initial copy of all the geometry arrays.
784                          */
785                         break;
786                 }
787                 default:
788                         break;
789         }
790         if (!done) {
791                 done = id_copy_inplace_no_main(id_orig, id_cow);
792         }
793         if (!done) {
794                 BLI_assert(!"No idea how to perform CoW on datablock");
795         }
796         /* Update pointers to nested ID datablocks. */
797         DEG_COW_PRINT("  Remapping ID links for %s: id_orig=%p id_cow=%p\n",
798                       id_orig->name, id_orig, id_cow);
799
800 #ifdef NESTED_ID_NASTY_WORKAROUND
801         ntree_hack_remap_pointers(depsgraph, id_cow);
802 #endif
803         /* Do it now, so remapping will understand that possibly remapped self ID
804          * is not to be remapped again.
805          */
806         deg_tag_copy_on_write_id(id_cow, id_orig);
807         /* Perform remapping of the nodes. */
808         RemapCallbackUserData user_data;
809         user_data.depsgraph = depsgraph;
810         user_data.temp_id = newid;
811         user_data.real_id = id_cow;
812         user_data.node_builder = node_builder;
813         user_data.create_placeholders = create_placeholders;
814         BKE_library_foreach_ID_link(NULL,
815                                     id_cow,
816                                     foreach_libblock_remap_callback,
817                                     (void *)&user_data,
818                                     IDWALK_NOP);
819         /* Correct or tweak some pointers which are not taken care by foreach
820          * from above.
821          */
822         update_special_pointers(depsgraph, id_orig, id_cow);
823         /* Now we can safely discard temporary memory used for copying. */
824         if (newid != NULL) {
825                 MEM_freeN(newid);
826         }
827         return id_cow;
828 }
829
830 /* NOTE: Depsgraph is supposed to have ID node already. */
831 ID *deg_expand_copy_on_write_datablock(const Depsgraph *depsgraph,
832                                        ID *id_orig,
833                                        DepsgraphNodeBuilder *node_builder,
834                                        bool create_placeholders)
835 {
836         DEG::IDDepsNode *id_node = depsgraph->find_id_node(id_orig);
837         BLI_assert(id_node != NULL);
838         return deg_expand_copy_on_write_datablock(depsgraph,
839                                                   id_node,
840                                                   node_builder,
841                                                   create_placeholders);
842 }
843
844 ID *deg_update_copy_on_write_datablock(const Depsgraph *depsgraph,
845                                        const IDDepsNode *id_node)
846 {
847         const ID *id_orig = id_node->id_orig;
848         const ID_Type id_type = GS(id_orig->name);
849         ID *id_cow = id_node->id_cow;
850         /* Similar to expansion, no need to do anything here. */
851         if (!deg_copy_on_write_is_needed(id_orig)) {
852                 return id_cow;
853         }
854         /* Special case for datablocks which are expanded at the dependency graph
855          * construction time. This datablocks must never change pointers of their
856          * nested data since it is used for function bindings.
857          */
858         if (check_datablock_expanded_at_construction(id_orig)) {
859                 BLI_assert(check_datablock_expanded(id_cow) == true);
860                 update_copy_on_write_datablock(depsgraph, id_orig, id_cow);
861                 return id_cow;
862         }
863         /* For the rest if datablock types we use simple logic:
864          * - Free previously expanded data, if any.
865          * - Perform full datablock copy.
866          *
867          * Note that we never free GPU materials from here since that's not
868          * safe for threading and GPU materials are likely to be re-used.
869          */
870         /* TODO(sergey): Either move this to an utility function or redesign
871          * Copy-on-Write components in a way that only needed parts are being
872          * copied over.
873          */
874         ListBase gpumaterial_backup;
875         ListBase *gpumaterial_ptr = NULL;
876         Mesh *mesh_evaluated = NULL;
877         IDProperty *base_collection_properties = NULL;
878         short base_flag = 0;
879         if (check_datablock_expanded(id_cow)) {
880                 switch (id_type) {
881                         case ID_MA:
882                         {
883                                 Material *material = (Material *)id_cow;
884                                 gpumaterial_ptr = &material->gpumaterial;
885                                 break;
886                         }
887                         case ID_WO:
888                         {
889                                 World *world = (World *)id_cow;
890                                 gpumaterial_ptr = &world->gpumaterial;
891                                 break;
892                         }
893                         case ID_OB:
894                         {
895                                 Object *object = (Object *)id_cow;
896                                 /* Store evaluated mesh, make sure we don't free it. */
897                                 mesh_evaluated = object->mesh_evaluated;
898                                 object->mesh_evaluated = NULL;
899                                 /* Currently object update will override actual object->data
900                                  * to an evaluated version. Need to make sure we don't have
901                                  * data set to evaluated one before free anything.
902                                  */
903                                 if (mesh_evaluated != NULL) {
904                                         if (object->data == mesh_evaluated) {
905                                                 object->data = mesh_evaluated->id.orig_id;
906                                         }
907                                 }
908                                 /* Make a backup of base flags. */
909                                 base_collection_properties = object->base_collection_properties;
910                                 base_flag = object->base_flag;
911                                 break;
912                         }
913                         default:
914                                 break;
915                 }
916                 if (gpumaterial_ptr != NULL) {
917                         gpumaterial_backup = *gpumaterial_ptr;
918                         gpumaterial_ptr->first = gpumaterial_ptr->last = NULL;
919                 }
920         }
921         deg_free_copy_on_write_datablock(id_cow);
922         deg_expand_copy_on_write_datablock(depsgraph, id_node);
923         /* Restore GPU materials. */
924         if (gpumaterial_ptr != NULL) {
925                 *gpumaterial_ptr = gpumaterial_backup;
926         }
927         if (id_type == ID_OB) {
928                 Object *object = (Object *)id_cow;
929                 if (mesh_evaluated != NULL) {
930                         object->mesh_evaluated = mesh_evaluated;
931                         /* Do same thing as object update: override actual object data
932                          * pointer with evaluated datablock.
933                          */
934                         if (object->type == OB_MESH) {
935                                 object->data = mesh_evaluated;
936                                 /* Evaluated mesh simply copied edit_btmesh pointer from
937                                  * original mesh during update, need to make sure no dead
938                                  * pointers are left behind.
939                                  */
940                                 mesh_evaluated->edit_btmesh =
941                                         ((Mesh *)mesh_evaluated->id.orig_id)->edit_btmesh;
942                         }
943                 }
944                 if (base_collection_properties != NULL) {
945                         object->base_collection_properties = base_collection_properties;
946                         object->base_flag = base_flag;
947                 }
948         }
949         return id_cow;
950 }
951
952 /* NOTE: Depsgraph is supposed to have ID node already. */
953 ID *deg_update_copy_on_write_datablock(const Depsgraph *depsgraph,
954                                        ID *id_orig)
955 {
956         DEG::IDDepsNode *id_node = depsgraph->find_id_node(id_orig);
957         BLI_assert(id_node != NULL);
958         return deg_update_copy_on_write_datablock(depsgraph, id_node);
959 }
960
961 /* Free content of the CoW datablock
962  * Notes:
963  * - Does not recurs into nested ID datablocks.
964  * - Does not free datablock itself.
965  */
966 void deg_free_copy_on_write_datablock(ID *id_cow)
967 {
968         if (!check_datablock_expanded(id_cow)) {
969                 /* Actual content was never copied on top of CoW block, we have
970                  * nothing to free.
971                  */
972                 return;
973         }
974         const ID_Type type = GS(id_cow->name);
975 #ifdef NESTED_ID_NASTY_WORKAROUND
976         nested_id_hack_discard_pointers(id_cow);
977 #endif
978         switch (type) {
979                 case ID_OB:
980                 {
981                         /* TODO(sergey): This workaround is only to prevent free derived
982                          * caches from modifying object->data. This is currently happening
983                          * due to mesh/curve datablock boundbox tagging dirty.
984                          */
985                         Object *ob_cow = (Object *)id_cow;
986                         ob_cow->data = NULL;
987                         break;
988                 }
989                 case ID_ME:
990                 {
991                         Mesh *mesh_cow = (Mesh *)id_cow;
992                         if (mesh_cow->edit_btmesh != NULL) {
993                                 BKE_editmesh_free_derivedmesh(mesh_cow->edit_btmesh);
994                                 MEM_freeN(mesh_cow->edit_btmesh);
995                                 mesh_cow->edit_btmesh = NULL;
996                         }
997                         break;
998                 }
999                 case ID_SCE:
1000                 {
1001                         /* Special case for scene: we use explicit function call which
1002                          * ensures no access to other datablocks is done.
1003                          */
1004                         Scene *scene = (Scene *)id_cow;
1005                         BKE_scene_free_ex(scene, false);
1006                         BKE_libblock_free_data(id_cow, false);
1007                         id_cow->name[0] = '\0';
1008                         return;
1009                 }
1010                 default:
1011                         break;
1012         }
1013         BKE_libblock_free_datablock(id_cow, 0);
1014         BKE_libblock_free_data(id_cow, false);
1015         /* Signal datablock as not being expanded. */
1016         id_cow->name[0] = '\0';
1017 }
1018
1019 void deg_evaluate_copy_on_write(const EvaluationContext * /*eval_ctx*/,
1020                                 const Depsgraph *depsgraph,
1021                                 const IDDepsNode *id_node)
1022 {
1023         DEBUG_PRINT("%s on %s\n", __func__, id_node->id_orig->name);
1024         deg_update_copy_on_write_datablock(depsgraph, id_node);
1025 }
1026
1027 bool deg_validate_copy_on_write_datablock(ID *id_cow)
1028 {
1029         if (id_cow == NULL) {
1030                 return false;
1031         }
1032         ValidateData data;
1033         data.is_valid = true;
1034         BKE_library_foreach_ID_link(NULL,
1035                                     id_cow,
1036                                     foreach_libblock_validate_callback,
1037                                     &data,
1038                                     IDWALK_NOP);
1039         return data.is_valid;
1040 }
1041
1042 void deg_tag_copy_on_write_id(ID *id_cow, const ID *id_orig)
1043 {
1044         id_cow->tag |= LIB_TAG_COPY_ON_WRITE;
1045         id_cow->orig_id = (ID *)id_orig;
1046 }
1047
1048 bool deg_copy_on_write_is_expanded(const ID *id_cow)
1049 {
1050         return check_datablock_expanded(id_cow);
1051 }
1052
1053 bool deg_copy_on_write_is_needed(const ID *id_orig)
1054 {
1055         const ID_Type id_type = GS(id_orig->name);
1056         return !ELEM(id_type, ID_IM);
1057 }
1058
1059 }  // namespace DEG