5daf462970854c7582f41d45bc1eb2e3994e134b
[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                         (void) object_cow;  /* Ignored for release builds. */
440                         BLI_assert(object_cow->derivedFinal == NULL);
441                         BLI_assert(object_cow->derivedDeform == NULL);
442                         break;
443                 }
444                 case ID_ME:
445                 {
446                         /* For meshes we need to update edit_btmesh to make it to point
447                          * to the CoW version of object.
448                          *
449                          * This is kind of confusing, because actual bmesh is not owned by
450                          * the CoW object, so need to be accurate about using link from
451                          * edit_btmesh to object.
452                          */
453                         const Mesh *mesh_orig = (const Mesh *)id_orig;
454                         Mesh *mesh_cow = (Mesh *)id_cow;
455                         if (mesh_orig->edit_btmesh != NULL) {
456                                 mesh_cow->edit_btmesh = (BMEditMesh *)MEM_dupallocN(mesh_orig->edit_btmesh);
457                                 mesh_cow->edit_btmesh->ob =
458                                         (Object *)depsgraph->get_cow_id(&mesh_orig->edit_btmesh->ob->id);
459                                 mesh_cow->edit_btmesh->derivedFinal = NULL;
460                                 mesh_cow->edit_btmesh->derivedCage = NULL;
461                         }
462                         break;
463                 }
464                 case ID_SCE:
465                 {
466                         const Scene *scene_orig = (const Scene *)id_orig;
467                         Scene *scene_cow = (Scene *)id_cow;
468                         if (scene_orig->obedit != NULL) {
469                                 scene_cow->obedit = (Object *)depsgraph->get_cow_id(&scene_orig->obedit->id);
470                         }
471                         else {
472                                 scene_cow->obedit = NULL;
473                         }
474                         break;
475                 }
476                 default:
477                         break;
478         }
479 }
480
481 void update_copy_on_write_layer_collections(
482         ListBase *layer_collections_cow,
483         const ListBase *layer_collections_orig);
484
485 void update_copy_on_write_layer_collection(
486         LayerCollection *layer_collection_cow,
487         const LayerCollection *layer_collection_orig)
488 {
489         // Make a local copy of original layer collection, so we can start
490         // modifying it.
491         LayerCollection local = *layer_collection_orig;
492         // Copy all pointer data from original CoW version of layer collection.
493         local.next = layer_collection_cow->next;
494         local.prev = layer_collection_cow->prev;
495         local.scene_collection = layer_collection_cow->scene_collection;
496         local.object_bases = layer_collection_cow->object_bases;
497         local.overrides = layer_collection_cow->overrides;
498         local.layer_collections = layer_collection_cow->layer_collections;
499         local.properties = layer_collection_cow->properties;
500         local.properties_evaluated = layer_collection_cow->properties_evaluated;
501         // Synchronize pointer-related data.
502         IDP_Reset(local.properties, layer_collection_orig->properties);
503         // Copy synchronized version back.
504         *layer_collection_cow = local;
505         // Recurs into nested layer collections.
506         update_copy_on_write_layer_collections(
507                 &layer_collection_cow->layer_collections,
508                 &layer_collection_orig->layer_collections);
509 }
510
511 void update_copy_on_write_layer_collections(
512         ListBase *layer_collections_cow,
513         const ListBase *layer_collections_orig)
514 {
515         const LayerCollection *layer_collection_orig =
516                 (const LayerCollection *)layer_collections_orig->first;
517         LayerCollection *layer_collection_cow =
518                 (LayerCollection *)layer_collections_cow->first;
519         while (layer_collection_orig != NULL) {
520                 update_copy_on_write_layer_collection(layer_collection_cow,
521                                                       layer_collection_orig);
522                 layer_collection_orig = layer_collection_orig->next;
523                 layer_collection_cow = layer_collection_cow->next;
524         }
525 }
526
527 void update_copy_on_write_view_layer(const Depsgraph *depsgraph,
528                                      ViewLayer *view_layer_cow,
529                                      const ViewLayer *view_layer_orig)
530 {
531         // Update pointers to active base.
532         if (view_layer_orig->basact == NULL) {
533                 view_layer_cow->basact = NULL;
534         }
535         else {
536                 const Object *obact_orig = view_layer_orig->basact->object;
537                 Object *obact_cow = (Object *)depsgraph->get_cow_id(&obact_orig->id);
538                 view_layer_cow->basact = BKE_view_layer_base_find(view_layer_cow, obact_cow);
539         }
540         // Update base flags.
541         //
542         // TODO(sergey): We should probably check visibled/selectabled.
543         // flag here?
544         const Base *base_orig = (Base *)view_layer_orig->object_bases.first;
545         Base *base_cow = (Base *)view_layer_cow->object_bases.first;;
546         while (base_orig != NULL) {
547                 base_cow->flag = base_orig->flag;
548                 base_orig = base_orig->next;
549                 base_cow = base_cow->next;
550         }
551         // Synchronize settings.
552         view_layer_cow->active_collection = view_layer_orig->active_collection;
553         view_layer_cow->flag = view_layer_orig->flag;
554         view_layer_cow->layflag = view_layer_orig->layflag;
555         view_layer_cow->passflag = view_layer_orig->passflag;
556         view_layer_cow->pass_alpha_threshold = view_layer_orig->pass_alpha_threshold;
557         // Synchronize ID properties.
558         IDP_Reset(view_layer_cow->properties, view_layer_orig->properties);
559         IDP_Reset(view_layer_cow->id_properties, view_layer_orig->id_properties);
560         // Synchronize layer collections.
561         update_copy_on_write_layer_collections(
562                 &view_layer_cow->layer_collections,
563                 &view_layer_orig->layer_collections);
564 }
565
566 void update_copy_on_write_view_layers(const Depsgraph *depsgraph,
567                                       Scene *scene_cow,
568                                       const Scene *scene_orig)
569 {
570         const ViewLayer *view_layer_orig = (const ViewLayer *)scene_orig->view_layers.first;
571         ViewLayer *view_layer_cow = (ViewLayer *)scene_cow->view_layers.first;
572         while (view_layer_orig != NULL) {
573                 update_copy_on_write_view_layer(depsgraph,
574                                                 view_layer_cow,
575                                                 view_layer_orig);
576                 view_layer_orig = view_layer_orig->next;
577                 view_layer_cow = view_layer_cow->next;
578         }
579 }
580
581 void update_copy_on_write_scene_collections(
582         ListBase *collections_cow,
583         const ListBase *collections_orig);
584
585 void update_copy_on_write_scene_collection(
586         SceneCollection *collection_cow,
587         const SceneCollection *collection_orig)
588 {
589         collection_cow->active_object_index = collection_orig->active_object_index;
590         update_copy_on_write_scene_collections(
591                 &collection_cow->scene_collections,
592                 &collection_orig->scene_collections);
593 }
594
595 void update_copy_on_write_scene_collections(
596         ListBase *collections_cow,
597         const ListBase *collections_orig)
598 {
599         const SceneCollection *nested_collection_orig =
600                 (const SceneCollection *)collections_orig->first;
601         SceneCollection *nested_collection_cow =
602                 (SceneCollection *)collections_cow->first;
603         while (nested_collection_orig != NULL) {
604                 update_copy_on_write_scene_collection(
605                         nested_collection_cow,
606                         nested_collection_orig);
607                 nested_collection_orig = nested_collection_orig->next;
608                 nested_collection_cow = nested_collection_cow->next;
609         }
610 }
611
612 /* Update copy-on-write version of scene from original scene. */
613 void update_copy_on_write_scene(const Depsgraph *depsgraph,
614                                 Scene *scene_cow,
615                                 const Scene *scene_orig)
616 {
617         // Some non-pointer data sync, current frame for now.
618         // TODO(sergey): Are we missing something here?
619         scene_cow->r.cfra = scene_orig->r.cfra;
620         scene_cow->r.subframe = scene_orig->r.subframe;
621         // Update view layers and collections.
622         update_copy_on_write_view_layers(depsgraph, scene_cow, scene_orig);
623         update_copy_on_write_scene_collection(scene_cow->collection,
624                                               scene_orig->collection);
625         // Update edit object pointer.
626         if (scene_orig->obedit != NULL) {
627                 scene_cow->obedit = (Object *)depsgraph->get_cow_id(&scene_orig->obedit->id);
628         }
629         else {
630                 scene_cow->obedit = NULL;
631         }
632         /* Synchronize active render engine. */
633         BLI_strncpy(scene_cow->view_render.engine_id,
634                     scene_orig->view_render.engine_id,
635                     sizeof(scene_cow->view_render.engine_id));
636         /* TODO(sergey): What else do we need here? */
637 }
638
639 /* Update copy-on-write version of armature object from original scene. */
640 void update_copy_on_write_object(const Depsgraph * /*depsgraph*/,
641                                  Object *object_cow,
642                                  const Object *object_orig)
643 {
644         /* TODO(sergey): This function might be split into a smaller ones,
645          * reused for different updates. And maybe even moved to BKE.
646          */
647         /* Update armature/pose related flags. */
648         bPose *pose_cow = object_cow->pose;
649         const bPose *pose_orig = object_orig->pose;
650         extract_pose_from_pose(pose_cow, pose_orig);
651         /* Update object itself. */
652         BKE_object_transform_copy(object_cow, object_orig);
653 }
654
655 /* Update copy-on-write version of datablock from it's original ID without re-building
656  * the whole datablock from scratch.
657  *
658  * Used for such special cases as scene collections and armatures, which can not use full
659  * re-alloc due to pointers used as function bindings.
660  */
661 void update_copy_on_write_datablock(const Depsgraph *depsgraph,
662                                     const ID *id_orig, ID *id_cow)
663 {
664         bool ok = false;
665         const ID_Type id_type = GS(id_orig->name);
666         switch (id_type) {
667                 case ID_SCE: {
668                         const Scene *scene_orig = (const Scene *)id_orig;
669                         Scene *scene_cow = (Scene *)id_cow;
670                         update_copy_on_write_scene(depsgraph, scene_cow, scene_orig);
671                         ok = true;
672                         break;
673                 }
674                 case ID_OB: {
675                         const Object *object_orig = (const Object *)id_orig;
676                         Object *object_cow = (Object *)id_cow;
677                         if (object_orig->type == OB_ARMATURE) {
678                                 update_copy_on_write_object(depsgraph,
679                                                             object_cow,
680                                                             object_orig);
681                                 ok = true;
682                         }
683                         break;
684                 }
685                 case ID_AR:
686                         /* Nothing to do currently. */
687                         ok = true;
688                         break;
689                 default:
690                         break;
691         }
692         // TODO(sergey): Other ID types here.
693         if (!ok) {
694                 BLI_assert(!"Missing update logic of expanded datablock");
695         }
696 }
697
698 /* This callback is used to validate that all nested ID datablocks are
699  * properly expanded.
700  */
701 int foreach_libblock_validate_callback(void *user_data,
702                                        ID * /*id_self*/,
703                                        ID **id_p,
704                                        int /*cb_flag*/)
705 {
706         ValidateData *data = (ValidateData *)user_data;
707         if (*id_p != NULL) {
708                 if (!check_datablock_expanded(*id_p)) {
709                         data->is_valid = false;
710                         /* TODO(sergey): Store which is is not valid? */
711                 }
712         }
713         return IDWALK_RET_NOP;
714 }
715
716 }  // namespace
717
718 /* Actual implementation of logic which "expands" all the data which was not
719  * yet copied-on-write.
720  *
721  * NOTE: Expects that CoW datablock is empty.
722  */
723 ID *deg_expand_copy_on_write_datablock(const Depsgraph *depsgraph,
724                                        const IDDepsNode *id_node,
725                                        DepsgraphNodeBuilder *node_builder,
726                                        bool create_placeholders)
727 {
728         BLI_assert(!create_placeholders ||
729                    check_datablock_expanded_at_construction(id_node->id_orig));
730         const ID *id_orig = id_node->id_orig;
731         ID *id_cow = id_node->id_cow;
732         /* No need to expand such datablocks, their copied ID is same as original
733          * one already.
734          */
735         if (!deg_copy_on_write_is_needed(id_orig)) {
736                 return id_cow;
737         }
738         DEG_COW_PRINT("Expanding datablock for %s: id_orig=%p id_cow=%p\n",
739                       id_orig->name, id_orig, id_cow);
740         /* Sanity checks. */
741         /* NOTE: Disabled for now, conflicts when re-using evaluated datablock when
742          * rebuilding dependencies.
743          */
744         if (check_datablock_expanded(id_cow) && create_placeholders) {
745                 deg_free_copy_on_write_datablock(id_cow);
746         }
747         // BLI_assert(check_datablock_expanded(id_cow) == false);
748         /* Copy data from original ID to a copied version. */
749         /* TODO(sergey): Avoid doing full ID copy somehow, make Mesh to reference
750          * original geometry arrays for until those are modified.
751          */
752         /* TODO(sergey): We do some trickery with temp bmain and extra ID pointer
753          * just to be able to use existing API. Ideally we need to replace this with
754          * in-place copy from existing datablock to a prepared memory.
755          *
756          * NOTE: We don't use BKE_main_{new,free} because:
757          * - We don't want heap-allocations here.
758          * - We don't want bmain's content to be freed when main is freed.
759          */
760         bool done = false;
761         /* Need to make sure the possibly temporary allocated memory is correct for
762          * until we are fully done with remapping original pointers with copied on
763          * write ones.
764          */
765         ID *newid = NULL;
766         /* First we handle special cases which are not covered by id_copy() yet.
767          * or cases where we want to do something smarter than simple datablock
768          * copy.
769          */
770         const ID_Type id_type = GS(id_orig->name);
771         switch (id_type) {
772                 case ID_SCE:
773                 {
774                         done = scene_copy_inplace_no_main((Scene *)id_orig, (Scene *)id_cow);
775                         break;
776                 }
777                 case ID_ME:
778                 {
779                         /* TODO(sergey): Ideally we want to handle meshes in a special
780                          * manner here to avoid initial copy of all the geometry arrays.
781                          */
782                         break;
783                 }
784                 default:
785                         break;
786         }
787         if (!done) {
788                 done = id_copy_inplace_no_main(id_orig, id_cow);
789         }
790         if (!done) {
791                 BLI_assert(!"No idea how to perform CoW on datablock");
792         }
793         /* Update pointers to nested ID datablocks. */
794         DEG_COW_PRINT("  Remapping ID links for %s: id_orig=%p id_cow=%p\n",
795                       id_orig->name, id_orig, id_cow);
796
797 #ifdef NESTED_ID_NASTY_WORKAROUND
798         ntree_hack_remap_pointers(depsgraph, id_cow);
799 #endif
800         /* Do it now, so remapping will understand that possibly remapped self ID
801          * is not to be remapped again.
802          */
803         deg_tag_copy_on_write_id(id_cow, id_orig);
804         /* Perform remapping of the nodes. */
805         RemapCallbackUserData user_data;
806         user_data.depsgraph = depsgraph;
807         user_data.temp_id = newid;
808         user_data.real_id = id_cow;
809         user_data.node_builder = node_builder;
810         user_data.create_placeholders = create_placeholders;
811         BKE_library_foreach_ID_link(NULL,
812                                     id_cow,
813                                     foreach_libblock_remap_callback,
814                                     (void *)&user_data,
815                                     IDWALK_NOP);
816         /* Correct or tweak some pointers which are not taken care by foreach
817          * from above.
818          */
819         update_special_pointers(depsgraph, id_orig, id_cow);
820         /* Now we can safely discard temporary memory used for copying. */
821         if (newid != NULL) {
822                 MEM_freeN(newid);
823         }
824         return id_cow;
825 }
826
827 /* NOTE: Depsgraph is supposed to have ID node already. */
828 ID *deg_expand_copy_on_write_datablock(const Depsgraph *depsgraph,
829                                        ID *id_orig,
830                                        DepsgraphNodeBuilder *node_builder,
831                                        bool create_placeholders)
832 {
833         DEG::IDDepsNode *id_node = depsgraph->find_id_node(id_orig);
834         BLI_assert(id_node != NULL);
835         return deg_expand_copy_on_write_datablock(depsgraph,
836                                                   id_node,
837                                                   node_builder,
838                                                   create_placeholders);
839 }
840
841 ID *deg_update_copy_on_write_datablock(const Depsgraph *depsgraph,
842                                        const IDDepsNode *id_node)
843 {
844         const ID *id_orig = id_node->id_orig;
845         const ID_Type id_type = GS(id_orig->name);
846         ID *id_cow = id_node->id_cow;
847         /* Similar to expansion, no need to do anything here. */
848         if (!deg_copy_on_write_is_needed(id_orig)) {
849                 return id_cow;
850         }
851         /* Special case for datablocks which are expanded at the dependency graph
852          * construction time. This datablocks must never change pointers of their
853          * nested data since it is used for function bindings.
854          */
855         if (check_datablock_expanded_at_construction(id_orig)) {
856                 BLI_assert(check_datablock_expanded(id_cow) == true);
857                 update_copy_on_write_datablock(depsgraph, id_orig, id_cow);
858                 return id_cow;
859         }
860         /* For the rest if datablock types we use simple logic:
861          * - Free previously expanded data, if any.
862          * - Perform full datablock copy.
863          *
864          * Note that we never free GPU materials from here since that's not
865          * safe for threading and GPU materials are likely to be re-used.
866          */
867         /* TODO(sergey): Either move this to an utility function or redesign
868          * Copy-on-Write components in a way that only needed parts are being
869          * copied over.
870          */
871         ListBase gpumaterial_backup;
872         ListBase *gpumaterial_ptr = NULL;
873         Mesh *mesh_evaluated = NULL;
874         IDProperty *base_collection_properties = NULL;
875         short base_flag = 0;
876         if (check_datablock_expanded(id_cow)) {
877                 switch (id_type) {
878                         case ID_MA:
879                         {
880                                 Material *material = (Material *)id_cow;
881                                 gpumaterial_ptr = &material->gpumaterial;
882                                 break;
883                         }
884                         case ID_WO:
885                         {
886                                 World *world = (World *)id_cow;
887                                 gpumaterial_ptr = &world->gpumaterial;
888                                 break;
889                         }
890                         case ID_OB:
891                         {
892                                 Object *object = (Object *)id_cow;
893                                 /* Store evaluated mesh, make sure we don't free it. */
894                                 mesh_evaluated = object->mesh_evaluated;
895                                 object->mesh_evaluated = NULL;
896                                 /* Currently object update will override actual object->data
897                                  * to an evaluated version. Need to make sure we don't have
898                                  * data set to evaluated one before free anything.
899                                  */
900                                 if (mesh_evaluated != NULL) {
901                                         if (object->data == mesh_evaluated) {
902                                                 object->data = mesh_evaluated->id.orig_id;
903                                         }
904                                 }
905                                 /* Make a backup of base flags. */
906                                 base_collection_properties = object->base_collection_properties;
907                                 base_flag = object->base_flag;
908                                 break;
909                         }
910                         default:
911                                 break;
912                 }
913                 if (gpumaterial_ptr != NULL) {
914                         gpumaterial_backup = *gpumaterial_ptr;
915                         gpumaterial_ptr->first = gpumaterial_ptr->last = NULL;
916                 }
917         }
918         deg_free_copy_on_write_datablock(id_cow);
919         deg_expand_copy_on_write_datablock(depsgraph, id_node);
920         /* Restore GPU materials. */
921         if (gpumaterial_ptr != NULL) {
922                 *gpumaterial_ptr = gpumaterial_backup;
923         }
924         if (id_type == ID_OB) {
925                 Object *object = (Object *)id_cow;
926                 if (mesh_evaluated != NULL) {
927                         object->mesh_evaluated = mesh_evaluated;
928                         /* Do same thing as object update: override actual object data
929                          * pointer with evaluated datablock.
930                          */
931                         if (object->type == OB_MESH) {
932                                 object->data = mesh_evaluated;
933                                 /* Evaluated mesh simply copied edit_btmesh pointer from
934                                  * original mesh during update, need to make sure no dead
935                                  * pointers are left behind.
936                                  */
937                                 mesh_evaluated->edit_btmesh =
938                                         ((Mesh *)mesh_evaluated->id.orig_id)->edit_btmesh;
939                         }
940                 }
941                 if (base_collection_properties != NULL) {
942                         object->base_collection_properties = base_collection_properties;
943                         object->base_flag = base_flag;
944                 }
945         }
946         return id_cow;
947 }
948
949 /* NOTE: Depsgraph is supposed to have ID node already. */
950 ID *deg_update_copy_on_write_datablock(const Depsgraph *depsgraph,
951                                        ID *id_orig)
952 {
953         DEG::IDDepsNode *id_node = depsgraph->find_id_node(id_orig);
954         BLI_assert(id_node != NULL);
955         return deg_update_copy_on_write_datablock(depsgraph, id_node);
956 }
957
958 /* Free content of the CoW datablock
959  * Notes:
960  * - Does not recurs into nested ID datablocks.
961  * - Does not free datablock itself.
962  */
963 void deg_free_copy_on_write_datablock(ID *id_cow)
964 {
965         if (!check_datablock_expanded(id_cow)) {
966                 /* Actual content was never copied on top of CoW block, we have
967                  * nothing to free.
968                  */
969                 return;
970         }
971         const ID_Type type = GS(id_cow->name);
972 #ifdef NESTED_ID_NASTY_WORKAROUND
973         nested_id_hack_discard_pointers(id_cow);
974 #endif
975         switch (type) {
976                 case ID_OB:
977                 {
978                         /* TODO(sergey): This workaround is only to prevent free derived
979                          * caches from modifying object->data. This is currently happening
980                          * due to mesh/curve datablock boundbox tagging dirty.
981                          */
982                         Object *ob_cow = (Object *)id_cow;
983                         ob_cow->data = NULL;
984                         break;
985                 }
986                 case ID_ME:
987                 {
988                         Mesh *mesh_cow = (Mesh *)id_cow;
989                         if (mesh_cow->edit_btmesh != NULL) {
990                                 BKE_editmesh_free_derivedmesh(mesh_cow->edit_btmesh);
991                                 MEM_freeN(mesh_cow->edit_btmesh);
992                                 mesh_cow->edit_btmesh = NULL;
993                         }
994                         break;
995                 }
996                 case ID_SCE:
997                 {
998                         /* Special case for scene: we use explicit function call which
999                          * ensures no access to other datablocks is done.
1000                          */
1001                         Scene *scene = (Scene *)id_cow;
1002                         BKE_scene_free_ex(scene, false);
1003                         BKE_libblock_free_data(id_cow, false);
1004                         id_cow->name[0] = '\0';
1005                         return;
1006                 }
1007                 default:
1008                         break;
1009         }
1010         BKE_libblock_free_datablock(id_cow, 0);
1011         BKE_libblock_free_data(id_cow, false);
1012         /* Signal datablock as not being expanded. */
1013         id_cow->name[0] = '\0';
1014 }
1015
1016 void deg_evaluate_copy_on_write(const EvaluationContext * /*eval_ctx*/,
1017                                 const Depsgraph *depsgraph,
1018                                 const IDDepsNode *id_node)
1019 {
1020         DEBUG_PRINT("%s on %s\n", __func__, id_node->id_orig->name);
1021         deg_update_copy_on_write_datablock(depsgraph, id_node);
1022 }
1023
1024 bool deg_validate_copy_on_write_datablock(ID *id_cow)
1025 {
1026         if (id_cow == NULL) {
1027                 return false;
1028         }
1029         ValidateData data;
1030         data.is_valid = true;
1031         BKE_library_foreach_ID_link(NULL,
1032                                     id_cow,
1033                                     foreach_libblock_validate_callback,
1034                                     &data,
1035                                     IDWALK_NOP);
1036         return data.is_valid;
1037 }
1038
1039 void deg_tag_copy_on_write_id(ID *id_cow, const ID *id_orig)
1040 {
1041         id_cow->tag |= LIB_TAG_COPY_ON_WRITE;
1042         id_cow->orig_id = (ID *)id_orig;
1043 }
1044
1045 bool deg_copy_on_write_is_expanded(const ID *id_cow)
1046 {
1047         return check_datablock_expanded(id_cow);
1048 }
1049
1050 bool deg_copy_on_write_is_needed(const ID *id_orig)
1051 {
1052         const ID_Type id_type = GS(id_orig->name);
1053         return !ELEM(id_type, ID_IM);
1054 }
1055
1056 }  // namespace DEG