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