Cleanup: BKE_library: remove 'test' param of id_copy.
[blender.git] / source / blender / depsgraph / intern / eval / deg_eval_copy_on_write.cc
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 20137Blender Foundation.
17  * All rights reserved.
18  */
19
20
21 /** \file blender/depsgraph/intern/eval/deg_eval_copy_on_write.cc
22  *  \ingroup depsgraph
23  */
24
25 /* Enable special; trickery to treat nested owned IDs (such as nodetree of
26  * material) to be handled in same way as "real" datablocks, even tho some
27  * internal BKE routines doesn't treat them like that.
28  *
29  * TODO(sergey): Re-evaluate that after new ID handling is in place.
30  */
31 #define NESTED_ID_NASTY_WORKAROUND
32
33 /* Silence warnings from copying deprecated fields. */
34 #define DNA_DEPRECATED_ALLOW
35
36 #include "intern/eval/deg_eval_copy_on_write.h"
37
38 #include <cstring>
39
40 #include "BLI_utildefines.h"
41 #include "BLI_listbase.h"
42 #include "BLI_threads.h"
43 #include "BLI_string.h"
44
45 #include "BKE_curve.h"
46 #include "BKE_global.h"
47 #include "BKE_idprop.h"
48 #include "BKE_layer.h"
49 #include "BKE_library.h"
50 #include "BKE_scene.h"
51
52 #include "DEG_depsgraph.h"
53 #include "DEG_depsgraph_query.h"
54
55 #include "MEM_guardedalloc.h"
56
57 extern "C" {
58 #include "DNA_ID.h"
59 #include "DNA_anim_types.h"
60 #include "DNA_armature_types.h"
61 #include "DNA_mesh_types.h"
62 #include "DNA_scene_types.h"
63 #include "DNA_object_types.h"
64 #include "DNA_particle_types.h"
65
66 #include "DRW_engine.h"
67
68 #ifdef NESTED_ID_NASTY_WORKAROUND
69 #  include "DNA_curve_types.h"
70 #  include "DNA_key_types.h"
71 #  include "DNA_lamp_types.h"
72 #  include "DNA_lattice_types.h"
73 #  include "DNA_linestyle_types.h"
74 #  include "DNA_material_types.h"
75 #  include "DNA_meta_types.h"
76 #  include "DNA_node_types.h"
77 #  include "DNA_texture_types.h"
78 #  include "DNA_world_types.h"
79 #endif
80
81 #include "BKE_action.h"
82 #include "BKE_animsys.h"
83 #include "BKE_armature.h"
84 #include "BKE_editmesh.h"
85 #include "BKE_library_query.h"
86 #include "BKE_object.h"
87 }
88
89 #include "intern/depsgraph.h"
90 #include "intern/builder/deg_builder_nodes.h"
91 #include "intern/node/deg_node.h"
92 #include "intern/node/deg_node_id.h"
93
94 namespace DEG {
95
96 #define DEBUG_PRINT if (G.debug & G_DEBUG_DEPSGRAPH_EVAL) printf
97
98 namespace {
99
100 #ifdef NESTED_ID_NASTY_WORKAROUND
101 union NestedIDHackTempStorage {
102         Curve curve;
103         FreestyleLineStyle linestyle;
104         Lamp lamp;
105         Lattice lattice;
106         Material material;
107         Mesh mesh;
108         Scene scene;
109         Tex tex;
110         World world;
111 };
112
113 /* Set nested owned ID pointers to NULL. */
114 void nested_id_hack_discard_pointers(ID *id_cow)
115 {
116         switch (GS(id_cow->name)) {
117 #  define SPECIAL_CASE(id_type, dna_type, field)  \
118                 case id_type:                             \
119                 {                                         \
120                         ((dna_type *)id_cow)->field = NULL;   \
121                         break;                                \
122                 }
123
124                 SPECIAL_CASE(ID_LS, FreestyleLineStyle, nodetree)
125                 SPECIAL_CASE(ID_LA, Lamp, nodetree)
126                 SPECIAL_CASE(ID_MA, Material, nodetree)
127                 SPECIAL_CASE(ID_TE, Tex, nodetree)
128                 SPECIAL_CASE(ID_WO, World, nodetree)
129
130                 SPECIAL_CASE(ID_CU, Curve, key)
131                 SPECIAL_CASE(ID_LT, Lattice, key)
132                 SPECIAL_CASE(ID_ME, Mesh, key)
133
134                 case ID_SCE:
135                 {
136                         Scene *scene_cow = (Scene *)id_cow;
137                         /* Node trees always have their own ID node in the graph, and are
138                          * being copied as part of their copy-on-write process. */
139                         scene_cow->nodetree = NULL;
140                         /* Tool settings pointer is shared with the original scene. */
141                         scene_cow->toolsettings = NULL;
142                         break;
143                 }
144
145                 case ID_OB:
146                 {
147                         /* Clear the ParticleSettings pointer to prevent doubly-freeing it. */
148                         Object *ob = (Object *)id_cow;
149                         LISTBASE_FOREACH (ParticleSystem *, psys, &ob->particlesystem) {
150                                 psys->part = NULL;
151                         }
152                         break;
153                 }
154 #  undef SPECIAL_CASE
155
156                 default:
157                         break;
158         }
159 }
160
161 /* Set ID pointer of nested owned IDs (nodetree, key) to NULL.
162  *
163  * Return pointer to a new ID to be used.
164  */
165 const ID *nested_id_hack_get_discarded_pointers(NestedIDHackTempStorage *storage,
166                                                 const ID *id)
167 {
168         switch (GS(id->name)) {
169 #  define SPECIAL_CASE(id_type, dna_type, field, variable)  \
170                 case id_type:                                       \
171                 {                                                   \
172                         storage->variable = *(dna_type *)id;            \
173                         storage->variable.field = NULL;                 \
174                         return &storage->variable.id;                   \
175                 }
176
177                 SPECIAL_CASE(ID_LS, FreestyleLineStyle, nodetree, linestyle)
178                 SPECIAL_CASE(ID_LA, Lamp, nodetree, lamp)
179                 SPECIAL_CASE(ID_MA, Material, nodetree, material)
180                 SPECIAL_CASE(ID_TE, Tex, nodetree, tex)
181                 SPECIAL_CASE(ID_WO, World, nodetree, world)
182
183                 SPECIAL_CASE(ID_CU, Curve, key, curve)
184                 SPECIAL_CASE(ID_LT, Lattice, key, lattice)
185                 SPECIAL_CASE(ID_ME, Mesh, key, mesh)
186
187                 case ID_SCE:
188                 {
189                         storage->scene = *(Scene *)id;
190                         storage->scene.toolsettings = NULL;
191                         storage->scene.nodetree = NULL;
192                         return &storage->scene.id;
193                 }
194
195 #  undef SPECIAL_CASE
196
197                 default:
198                         break;
199         }
200         return id;
201 }
202
203 /* Set ID pointer of nested owned IDs (nodetree, key) to the original value. */
204 void nested_id_hack_restore_pointers(const ID *old_id, ID *new_id)
205 {
206         if (new_id == NULL) {
207                 return;
208         }
209         switch (GS(old_id->name)) {
210 #  define SPECIAL_CASE(id_type, dna_type, field)    \
211                 case id_type:                               \
212                 {                                           \
213                         ((dna_type *)(new_id))->field =         \
214                                 ((dna_type *)(old_id))->field;  \
215                         break;                                  \
216                 }
217
218                 SPECIAL_CASE(ID_LS, FreestyleLineStyle, nodetree)
219                 SPECIAL_CASE(ID_LA, Lamp, nodetree)
220                 SPECIAL_CASE(ID_MA, Material, nodetree)
221                 SPECIAL_CASE(ID_SCE, Scene, nodetree)
222                 SPECIAL_CASE(ID_TE, Tex, nodetree)
223                 SPECIAL_CASE(ID_WO, World, nodetree)
224
225                 SPECIAL_CASE(ID_CU, Curve, key)
226                 SPECIAL_CASE(ID_LT, Lattice, key)
227                 SPECIAL_CASE(ID_ME, Mesh, key)
228
229 #undef SPECIAL_CASE
230                 default:
231                         break;
232         }
233 }
234
235 /* Remap pointer of nested owned IDs (nodetree. key) to the new ID values. */
236 void ntree_hack_remap_pointers(const Depsgraph *depsgraph, ID *id_cow)
237 {
238         switch (GS(id_cow->name)) {
239 #  define SPECIAL_CASE(id_type, dna_type, field, field_type)                   \
240                 case id_type:                                                          \
241                 {                                                                      \
242                         dna_type *data = (dna_type *)id_cow;                               \
243                         if (data->field != NULL) {                                         \
244                                 ID *ntree_id_cow = depsgraph->get_cow_id(&data->field->id);    \
245                                 if (ntree_id_cow != NULL) {                                    \
246                                         DEG_COW_PRINT("    Remapping datablock for %s: id_orig=%p id_cow=%p\n", \
247                                                       data->field->id.name,                        \
248                                                       data->field,                                 \
249                                                       ntree_id_cow);                               \
250                                         data->field = (field_type *)ntree_id_cow;                  \
251                                 }                                                              \
252                         }                                                                  \
253                         break;                                                             \
254                 }
255
256                 SPECIAL_CASE(ID_LS, FreestyleLineStyle, nodetree, bNodeTree)
257                 SPECIAL_CASE(ID_LA, Lamp, nodetree, bNodeTree)
258                 SPECIAL_CASE(ID_MA, Material, nodetree, bNodeTree)
259                 SPECIAL_CASE(ID_SCE, Scene, nodetree, bNodeTree)
260                 SPECIAL_CASE(ID_TE, Tex, nodetree, bNodeTree)
261                 SPECIAL_CASE(ID_WO, World, nodetree, bNodeTree)
262
263                 SPECIAL_CASE(ID_CU, Curve, key, Key)
264                 SPECIAL_CASE(ID_LT, Lattice, key, Key)
265                 SPECIAL_CASE(ID_ME, Mesh, key, Key)
266
267 #undef SPECIAL_CASE
268                 default:
269                         break;
270         }
271 }
272 #endif  /* NODETREE_NASTY_WORKAROUND */
273
274 struct ValidateData {
275         bool is_valid;
276 };
277
278 /* Similar to generic id_copy() but does not require main and assumes pointer
279  * is already allocated,
280  */
281 bool id_copy_inplace_no_main(const ID *id, ID *newid)
282 {
283         const ID *id_for_copy = id;
284
285 #ifdef NESTED_ID_NASTY_WORKAROUND
286         NestedIDHackTempStorage id_hack_storage;
287         id_for_copy = nested_id_hack_get_discarded_pointers(&id_hack_storage, id);
288 #endif
289
290         bool result = BKE_id_copy_ex(NULL,
291                                      (ID *)id_for_copy,
292                                      &newid,
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                                       LIB_ID_COPY_CACHES));
298
299 #ifdef NESTED_ID_NASTY_WORKAROUND
300         if (result) {
301                 nested_id_hack_restore_pointers(id, newid);
302         }
303 #endif
304
305         return result;
306 }
307
308 /* Similar to BKE_scene_copy() but does not require main and assumes pointer
309  * is already allocated.
310  */
311 bool scene_copy_inplace_no_main(const Scene *scene, Scene *new_scene)
312 {
313         const ID *id_for_copy = &scene->id;
314
315 #ifdef NESTED_ID_NASTY_WORKAROUND
316         NestedIDHackTempStorage id_hack_storage;
317         id_for_copy = nested_id_hack_get_discarded_pointers(&id_hack_storage,
318                                                             &scene->id);
319 #endif
320
321         bool result = BKE_id_copy_ex(NULL,
322                                      id_for_copy,
323                                      (ID **)&new_scene,
324                                      LIB_ID_CREATE_NO_MAIN |
325                                      LIB_ID_CREATE_NO_USER_REFCOUNT |
326                                      LIB_ID_CREATE_NO_ALLOCATE |
327                                      LIB_ID_CREATE_NO_DEG_TAG);
328
329 #ifdef NESTED_ID_NASTY_WORKAROUND
330         if (result) {
331                 nested_id_hack_restore_pointers(&scene->id, &new_scene->id);
332         }
333 #endif
334
335         return result;
336 }
337
338 /* Remove all view layers but the one which corresponds to an input one. */
339 void scene_remove_unused_view_layers(const Depsgraph *depsgraph,
340                                      Scene *scene_cow)
341 {
342         ViewLayer *view_layer_input = depsgraph->view_layer;
343         ViewLayer *view_layer_eval = NULL;
344         /* Find evaluated view layer. At the same time we free memory used by
345          * all other of the view layers. */
346         for (ViewLayer *view_layer_cow =
347                      reinterpret_cast<ViewLayer *>(scene_cow->view_layers.first),
348                        *view_layer_next;
349              view_layer_cow != NULL;
350              view_layer_cow = view_layer_next)
351         {
352                 view_layer_next = view_layer_cow->next;
353                 if (STREQ(view_layer_input->name, view_layer_cow->name)) {
354                         view_layer_eval = view_layer_cow;
355                 }
356                 else {
357                         BKE_view_layer_free_ex(view_layer_cow, false);
358                 }
359         }
360         BLI_assert(view_layer_eval != NULL);
361         /* Make evaluated view layer the only one in the evaluated scene. */
362         view_layer_eval->prev = view_layer_eval->next = NULL;
363         scene_cow->view_layers.first = view_layer_eval;
364         scene_cow->view_layers.last = view_layer_eval;
365 }
366
367 /* Makes it so given view layer only has bases corresponding to a visible
368  * objects. */
369 void view_layer_remove_invisible_bases(const Depsgraph *depsgraph,
370                                        ViewLayer *view_layer)
371 {
372         const int base_visible_flag = (depsgraph->mode == DAG_EVAL_VIEWPORT) ?
373                 BASE_ENABLED_VIEWPORT : BASE_ENABLED_RENDER;
374         ListBase visible_bases = {NULL, NULL};
375         for (Base *base = reinterpret_cast<Base *>(view_layer->object_bases.first),
376                   *base_next;
377              base != NULL;
378              base = base_next)
379         {
380                 base_next = base->next;
381                 const bool is_object_visible = (base->flag & base_visible_flag);
382                 if (is_object_visible) {
383                         BLI_addtail(&visible_bases, base);
384                 }
385                 else {
386                         if (base == view_layer->basact) {
387                                 view_layer->basact = NULL;
388                         }
389                         MEM_freeN(base);
390                 }
391         }
392         view_layer->object_bases = visible_bases;
393 }
394
395 void scene_cleanup_view_layers(const Depsgraph *depsgraph, Scene *scene_cow)
396 {
397         scene_remove_unused_view_layers(depsgraph, scene_cow);
398         view_layer_remove_invisible_bases(
399                 depsgraph,
400                 reinterpret_cast<ViewLayer *>(scene_cow->view_layers.first));
401         /* TODO(sergey): Remove objects from collections as well.
402          * Not a HUGE deal for now, nobody is looking into those CURRENTLY.
403          * Still not an excuse to have those. */
404 }
405
406 /* Check whether given ID is expanded or still a shallow copy. */
407 BLI_INLINE bool check_datablock_expanded(const ID *id_cow)
408 {
409         return (id_cow->name[0] != '\0');
410 }
411
412 /* Those are datablocks which are not covered by dependency graph and hence
413  * does not need any remapping or anything.
414  *
415  * TODO(sergey): How to make it more robust for the future, so we don't have
416  * to maintain exception lists all over the code?
417  */
418 bool check_datablocks_copy_on_writable(const ID *id_orig)
419 {
420         const ID_Type id_type = GS(id_orig->name);
421         /* We shouldn't bother if copied ID is same as original one. */
422         if (!deg_copy_on_write_is_needed(id_orig)) {
423                 return false;
424         }
425         return !ELEM(id_type, ID_BR,
426                               ID_LS,
427                               ID_PAL);
428 }
429
430 /* Callback for BKE_library_foreach_ID_link which remaps original ID pointer
431  * with the one created by CoW system.
432  */
433
434 struct RemapCallbackUserData {
435         /* Dependency graph for which remapping is happening. */
436         const Depsgraph *depsgraph;
437         /* Create placeholder for ID nodes for cases when we need to remap original
438          * ID to it[s CoW version but we don't have required ID node yet.
439          *
440          * This happens when expansion happens a ta construction time. */
441         DepsgraphNodeBuilder *node_builder;
442         bool create_placeholders;
443 };
444
445 int foreach_libblock_remap_callback(void *user_data_v,
446                                     ID *id_self,
447                                     ID **id_p,
448                                     int /*cb_flag*/)
449 {
450         if (*id_p == NULL) {
451                 return IDWALK_RET_NOP;
452         }
453         RemapCallbackUserData *user_data = (RemapCallbackUserData *)user_data_v;
454         const Depsgraph *depsgraph = user_data->depsgraph;
455         ID *id_orig = *id_p;
456         if (check_datablocks_copy_on_writable(id_orig)) {
457                 ID *id_cow;
458                 if (user_data->create_placeholders) {
459                         /* Special workaround to stop creating temp datablocks for
460                          * objects which are coming from scene's collection and which
461                          * are never linked to any of layers.
462                          *
463                          * TODO(sergey): Ideally we need to tell ID looper to ignore
464                          * those or at least make it more reliable check where the
465                          * pointer is coming from. */
466                         const ID_Type id_type = GS(id_orig->name);
467                         const ID_Type id_type_self = GS(id_self->name);
468                         if (id_type == ID_OB && id_type_self == ID_SCE) {
469                                 IDNode *id_node = depsgraph->find_id_node(id_orig);
470                                 if (id_node == NULL) {
471                                         id_cow = id_orig;
472                                 }
473                                 else {
474                                         id_cow = id_node->id_cow;
475                                 }
476                         }
477                         else {
478                                 id_cow = user_data->node_builder->ensure_cow_id(id_orig);
479                         }
480                 }
481                 else {
482                         id_cow = depsgraph->get_cow_id(id_orig);
483                 }
484                 BLI_assert(id_cow != NULL);
485                 DEG_COW_PRINT("    Remapping datablock for %s: id_orig=%p id_cow=%p\n",
486                               id_orig->name, id_orig, id_cow);
487                 *id_p = id_cow;
488         }
489         return IDWALK_RET_NOP;
490 }
491
492 void update_armature_edit_mode_pointers(const Depsgraph * /*depsgraph*/,
493                                         const ID *id_orig, ID *id_cow)
494 {
495         const bArmature *armature_orig = (const bArmature *)id_orig;
496         bArmature *armature_cow = (bArmature *)id_cow;
497         armature_cow->edbo = armature_orig->edbo;
498 }
499
500 void update_curve_edit_mode_pointers(const Depsgraph * /*depsgraph*/,
501                                      const ID *id_orig, ID *id_cow)
502 {
503         const Curve *curve_orig = (const Curve *)id_orig;
504         Curve *curve_cow = (Curve *)id_cow;
505         curve_cow->editnurb = curve_orig->editnurb;
506         curve_cow->editfont = curve_orig->editfont;
507 }
508
509 void update_mball_edit_mode_pointers(const Depsgraph * /*depsgraph*/,
510                                      const ID *id_orig, ID *id_cow)
511 {
512         const MetaBall *mball_orig = (const MetaBall *)id_orig;
513         MetaBall *mball_cow = (MetaBall *)id_cow;
514         mball_cow->editelems = mball_orig->editelems;
515 }
516
517 void update_lattice_edit_mode_pointers(const Depsgraph * /*depsgraph*/,
518                                      const ID *id_orig, ID *id_cow)
519 {
520         const Lattice *lt_orig = (const Lattice *)id_orig;
521         Lattice *lt_cow = (Lattice *)id_cow;
522         lt_cow->editlatt = lt_orig->editlatt;
523 }
524
525 void update_mesh_edit_mode_pointers(const Depsgraph *depsgraph,
526                                     const ID *id_orig, ID *id_cow)
527 {
528         /* For meshes we need to update edit_btmesh to make it to point
529          * to the CoW version of object.
530          *
531          * This is kind of confusing, because actual bmesh is not owned by
532          * the CoW object, so need to be accurate about using link from
533          * edit_btmesh to object. */
534         const Mesh *mesh_orig = (const Mesh *)id_orig;
535         Mesh *mesh_cow = (Mesh *)id_cow;
536         if (mesh_orig->edit_btmesh == NULL) {
537                 return;
538         }
539         mesh_cow->edit_btmesh = (BMEditMesh *)MEM_dupallocN(mesh_orig->edit_btmesh);
540         mesh_cow->edit_btmesh->ob =
541             (Object *)depsgraph->get_cow_id(&mesh_orig->edit_btmesh->ob->id);
542         mesh_cow->edit_btmesh->mesh_eval_cage = NULL;
543         mesh_cow->edit_btmesh->mesh_eval_final = NULL;
544 }
545
546 /* Edit data is stored and owned by original datablocks, copied ones
547  * are simply referencing to them.
548  */
549 void update_edit_mode_pointers(const Depsgraph *depsgraph,
550                                const ID *id_orig, ID *id_cow)
551 {
552         const ID_Type type = GS(id_orig->name);
553         switch (type) {
554                 case ID_AR:
555                         update_armature_edit_mode_pointers(depsgraph, id_orig, id_cow);
556                         break;
557                 case ID_ME:
558                         update_mesh_edit_mode_pointers(depsgraph, id_orig, id_cow);
559                         break;
560                 case ID_CU:
561                         update_curve_edit_mode_pointers(depsgraph, id_orig, id_cow);
562                         break;
563                 case ID_MB:
564                         update_mball_edit_mode_pointers(depsgraph, id_orig, id_cow);
565                         break;
566                 case ID_LT:
567                         update_lattice_edit_mode_pointers(depsgraph, id_orig, id_cow);
568                         break;
569                 default:
570                         break;
571         }
572 }
573
574 void update_particle_system_orig_pointers(const Object *object_orig,
575                                           Object *object_cow)
576 {
577         ParticleSystem *psys_cow =
578                 (ParticleSystem *) object_cow->particlesystem.first;
579         ParticleSystem *psys_orig =
580                 (ParticleSystem *) object_orig->particlesystem.first;
581         while (psys_orig != NULL) {
582                 psys_cow->orig_psys = psys_orig;
583                 psys_cow = psys_cow->next;
584                 psys_orig = psys_orig->next;
585         }
586 }
587
588 void update_pose_orig_pointers(const bPose *pose_orig, bPose *pose_cow)
589 {
590         bPoseChannel *pchan_cow = (bPoseChannel *) pose_cow->chanbase.first;
591         bPoseChannel *pchan_orig = (bPoseChannel *) pose_orig->chanbase.first;
592         while (pchan_orig != NULL) {
593                 pchan_cow->orig_pchan = pchan_orig;
594                 pchan_cow = pchan_cow->next;
595                 pchan_orig = pchan_orig->next;
596         }
597 }
598
599 /* Do some special treatment of data transfer from original ID to it's
600  * CoW complementary part.
601  *
602  * Only use for the newly created CoW datablocks.
603  */
604 void update_special_pointers(const Depsgraph *depsgraph,
605                              const ID *id_orig, ID *id_cow)
606 {
607         const ID_Type type = GS(id_orig->name);
608         switch (type) {
609                 case ID_OB:
610                 {
611                         /* Ensure we don't drag someone's else derived mesh to the
612                          * new copy of the object. */
613                         Object *object_cow = (Object *)id_cow;
614                         const Object *object_orig = (const Object *)id_orig;
615                         object_cow->mode = object_orig->mode;
616                         object_cow->sculpt = object_orig->sculpt;
617                         if (object_cow->type == OB_MESH) {
618                                 object_cow->runtime.mesh_orig = (Mesh *)object_cow->data;
619                         }
620                         if (object_cow->type == OB_ARMATURE) {
621                                 const bArmature *armature_orig = (bArmature *)object_orig->data;
622                                 bArmature *armature_cow = (bArmature *)object_cow->data;
623                                 BKE_pose_remap_bone_pointers(armature_cow, object_cow->pose);
624                                 if (armature_orig->edbo == NULL) {
625                                         update_pose_orig_pointers(object_orig->pose,
626                                                                   object_cow->pose);
627                                 }
628                         }
629                         update_particle_system_orig_pointers(object_orig, object_cow);
630                         break;
631                 }
632                 case ID_SCE:
633                 {
634                         Scene *scene_cow = (Scene *)id_cow;
635                         const Scene *scene_orig = (const Scene *)id_orig;
636                         scene_cow->toolsettings = scene_orig->toolsettings;
637                         scene_cow->eevee.light_cache = scene_orig->eevee.light_cache;
638                         break;
639                 }
640                 default:
641                         break;
642         }
643         update_edit_mode_pointers(depsgraph, id_orig, id_cow);
644         BKE_animsys_update_driver_array(id_cow);
645 }
646
647 /* This callback is used to validate that all nested ID datablocks are
648  * properly expanded.
649  */
650 int foreach_libblock_validate_callback(void *user_data,
651                                        ID * /*id_self*/,
652                                        ID **id_p,
653                                        int /*cb_flag*/)
654 {
655         ValidateData *data = (ValidateData *)user_data;
656         if (*id_p != NULL) {
657                 if (!check_datablock_expanded(*id_p)) {
658                         data->is_valid = false;
659                         /* TODO(sergey): Store which is is not valid? */
660                 }
661         }
662         return IDWALK_RET_NOP;
663 }
664
665 }  // namespace
666
667 /* Actual implementation of logic which "expands" all the data which was not
668  * yet copied-on-write.
669  *
670  * NOTE: Expects that CoW datablock is empty.
671  */
672 ID *deg_expand_copy_on_write_datablock(const Depsgraph *depsgraph,
673                                        const IDNode *id_node,
674                                        DepsgraphNodeBuilder *node_builder,
675                                        bool create_placeholders)
676 {
677         const ID *id_orig = id_node->id_orig;
678         ID *id_cow = id_node->id_cow;
679         const int id_cow_recalc = id_cow->recalc;
680         /* No need to expand such datablocks, their copied ID is same as original
681          * one already. */
682         if (!deg_copy_on_write_is_needed(id_orig)) {
683                 return id_cow;
684         }
685         DEG_COW_PRINT("Expanding datablock for %s: id_orig=%p id_cow=%p\n",
686                       id_orig->name, id_orig, id_cow);
687         /* Sanity checks. */
688         /* NOTE: Disabled for now, conflicts when re-using evaluated datablock when
689          * rebuilding dependencies. */
690         if (check_datablock_expanded(id_cow) && create_placeholders) {
691                 deg_free_copy_on_write_datablock(id_cow);
692         }
693         // BLI_assert(check_datablock_expanded(id_cow) == false);
694         /* Copy data from original ID to a copied version. */
695         /* TODO(sergey): Avoid doing full ID copy somehow, make Mesh to reference
696          * original geometry arrays for until those are modified. */
697         /* TODO(sergey): We do some trickery with temp bmain and extra ID pointer
698          * just to be able to use existing API. Ideally we need to replace this with
699          * in-place copy from existing datablock to a prepared memory.
700          *
701          * NOTE: We don't use BKE_main_{new,free} because:
702          * - We don't want heap-allocations here.
703          * - We don't want bmain's content to be freed when main is freed. */
704         bool done = false;
705         /* First we handle special cases which are not covered by id_copy() yet.
706          * or cases where we want to do something smarter than simple datablock
707          * copy. */
708         const ID_Type id_type = GS(id_orig->name);
709         switch (id_type) {
710                 case ID_SCE:
711                 {
712                         done = scene_copy_inplace_no_main((Scene *)id_orig, (Scene *)id_cow);
713                         if (done) {
714                                 scene_cleanup_view_layers(depsgraph, (Scene *)id_cow);
715                         }
716                         break;
717                 }
718                 case ID_ME:
719                 {
720                         /* TODO(sergey): Ideally we want to handle meshes in a special
721                          * manner here to avoid initial copy of all the geometry arrays. */
722                         break;
723                 }
724                 default:
725                         break;
726         }
727         if (!done) {
728                 done = id_copy_inplace_no_main(id_orig, id_cow);
729         }
730         if (!done) {
731                 BLI_assert(!"No idea how to perform CoW on datablock");
732         }
733         /* Update pointers to nested ID datablocks. */
734         DEG_COW_PRINT("  Remapping ID links for %s: id_orig=%p id_cow=%p\n",
735                       id_orig->name, id_orig, id_cow);
736
737 #ifdef NESTED_ID_NASTY_WORKAROUND
738         ntree_hack_remap_pointers(depsgraph, id_cow);
739 #endif
740         /* Do it now, so remapping will understand that possibly remapped self ID
741          * is not to be remapped again. */
742         deg_tag_copy_on_write_id(id_cow, id_orig);
743         /* Perform remapping of the nodes. */
744         RemapCallbackUserData user_data = {NULL};
745         user_data.depsgraph = depsgraph;
746         user_data.node_builder = node_builder;
747         user_data.create_placeholders = create_placeholders;
748         BKE_library_foreach_ID_link(NULL,
749                                     id_cow,
750                                     foreach_libblock_remap_callback,
751                                     (void *)&user_data,
752                                     IDWALK_NOP);
753         /* Correct or tweak some pointers which are not taken care by foreach
754          * from above. */
755         update_special_pointers(depsgraph, id_orig, id_cow);
756         id_cow->recalc = id_orig->recalc | id_cow_recalc;
757         return id_cow;
758 }
759
760 /* NOTE: Depsgraph is supposed to have ID node already. */
761 ID *deg_expand_copy_on_write_datablock(const Depsgraph *depsgraph,
762                                        ID *id_orig,
763                                        DepsgraphNodeBuilder *node_builder,
764                                        bool create_placeholders)
765 {
766         DEG::IDNode *id_node = depsgraph->find_id_node(id_orig);
767         BLI_assert(id_node != NULL);
768         return deg_expand_copy_on_write_datablock(depsgraph,
769                                                   id_node,
770                                                   node_builder,
771                                                   create_placeholders);
772 }
773
774 static void deg_update_copy_on_write_animation(const Depsgraph *depsgraph,
775                                                const IDNode *id_node)
776 {
777         DEG_debug_print_eval((::Depsgraph *)depsgraph,
778                              __func__,
779                              id_node->id_orig->name,
780                              id_node->id_cow);
781         BKE_animdata_copy_id(NULL, id_node->id_cow, id_node->id_orig, LIB_ID_CREATE_NO_USER_REFCOUNT);
782         RemapCallbackUserData user_data = {NULL};
783         user_data.depsgraph = depsgraph;
784         BKE_library_foreach_ID_link(NULL,
785                                     id_node->id_cow,
786                                     foreach_libblock_remap_callback,
787                                     (void *)&user_data,
788                                     IDWALK_NOP);
789 }
790
791 typedef struct ObjectRuntimeBackup {
792         Object_Runtime runtime;
793         short base_flag;
794         unsigned short base_local_view_bits;
795 } ObjectRuntimeBackup;
796
797 /* Make a backup of object's evaluation runtime data, additionally
798  * make object to be safe for free without invalidating backed up
799  * pointers.
800  */
801 static void deg_backup_object_runtime(
802         Object *object,
803         ObjectRuntimeBackup *object_runtime_backup)
804 {
805         /* Store evaluated mesh and curve_cache, and make sure we don't free it. */
806         Mesh *mesh_eval = object->runtime.mesh_eval;
807         object_runtime_backup->runtime = object->runtime;
808         BKE_object_runtime_reset(object);
809         /* Object update will override actual object->data to an evaluated version.
810          * Need to make sure we don't have data set to evaluated one before free
811          * anything. */
812         if (mesh_eval != NULL && object->data == mesh_eval) {
813                 object->data = object->runtime.mesh_orig;
814         }
815         /* Make a backup of base flags. */
816         object_runtime_backup->base_flag = object->base_flag;
817         object_runtime_backup->base_local_view_bits = object->base_local_view_bits;
818 }
819
820 static void deg_restore_object_runtime(
821         Object *object,
822         const ObjectRuntimeBackup *object_runtime_backup)
823 {
824         Mesh *mesh_orig = object->runtime.mesh_orig;
825         object->runtime = object_runtime_backup->runtime;
826         object->runtime.mesh_orig = mesh_orig;
827         if (object->type == OB_MESH && object->runtime.mesh_eval != NULL) {
828                 if (object->id.recalc & ID_RECALC_GEOMETRY) {
829                         /* If geometry is tagged for update it means, that part of
830                          * evaluated mesh are not valid anymore. In this case we can not
831                          * have any "persistent" pointers to point to an invalid data.
832                          *
833                          * We restore object's data datablock to an original copy of
834                          * that datablock. */
835                         object->data = mesh_orig;
836
837                         /* After that, immediately free the invalidated caches. */
838                         BKE_object_free_derived_caches(object);
839                 }
840                 else {
841                         Mesh *mesh_eval = object->runtime.mesh_eval;
842                         /* Do same thing as object update: override actual object data
843                          * pointer with evaluated datablock. */
844                         object->data = mesh_eval;
845                         /* Evaluated mesh simply copied edit_btmesh pointer from
846                          * original mesh during update, need to make sure no dead
847                          * pointers are left behind. */
848                         mesh_eval->edit_btmesh = mesh_orig->edit_btmesh;
849                 }
850         }
851         object->base_flag = object_runtime_backup->base_flag;
852         object->base_local_view_bits = object_runtime_backup->base_local_view_bits;
853 }
854
855 ID *deg_update_copy_on_write_datablock(const Depsgraph *depsgraph,
856                                        const IDNode *id_node)
857 {
858         const ID *id_orig = id_node->id_orig;
859         const ID_Type id_type = GS(id_orig->name);
860         ID *id_cow = id_node->id_cow;
861         /* Similar to expansion, no need to do anything here. */
862         if (!deg_copy_on_write_is_needed(id_orig)) {
863                 return id_cow;
864         }
865         /* For the rest if datablock types we use simple logic:
866          * - Free previously expanded data, if any.
867          * - Perform full datablock copy.
868          *
869          * Note that we never free GPU materials from here since that's not
870          * safe for threading and GPU materials are likely to be re-used. */
871         /* TODO(sergey): Either move this to an utility function or redesign
872          * Copy-on-Write components in a way that only needed parts are being
873          * copied over. */
874         /* TODO(sergey): Wrap GPU material backup and object runtime backup to a
875          * generic backup structure. */
876         ListBase gpumaterial_backup;
877         ListBase *gpumaterial_ptr = NULL;
878         DrawDataList drawdata_backup;
879         DrawDataList *drawdata_ptr = NULL;
880         ObjectRuntimeBackup object_runtime_backup = {{NULL}};
881         if (check_datablock_expanded(id_cow)) {
882                 switch (id_type) {
883                         case ID_MA:
884                         {
885                                 Material *material = (Material *)id_cow;
886                                 gpumaterial_ptr = &material->gpumaterial;
887                                 break;
888                         }
889                         case ID_WO:
890                         {
891                                 World *world = (World *)id_cow;
892                                 gpumaterial_ptr = &world->gpumaterial;
893                                 break;
894                         }
895                         case ID_NT:
896                         {
897                                 /* Node trees should try to preserve their socket pointers
898                                  * as much as possible. This is due to UBOs code in GPU,
899                                  * which references sockets from trees.
900                                  *
901                                  * These flags CURRENTLY don't need full datablock update,
902                                  * everything is done by node tree update function which
903                                  * only copies socket values. */
904                                 const int ignore_flag = (ID_RECALC_SHADING |
905                                                          ID_RECALC_ANIMATION |
906                                                          ID_RECALC_COPY_ON_WRITE);
907                                 if ((id_cow->recalc & ~ignore_flag) == 0) {
908                                         deg_update_copy_on_write_animation(depsgraph, id_node);
909                                         return id_cow;
910                                 }
911                                 break;
912                         }
913                         case ID_OB:
914                         {
915                                 Object *ob = (Object *)id_cow;
916                                 deg_backup_object_runtime(ob, &object_runtime_backup);
917                                 break;
918                         }
919                         default:
920                                 break;
921                 }
922                 if (gpumaterial_ptr != NULL) {
923                         gpumaterial_backup = *gpumaterial_ptr;
924                         gpumaterial_ptr->first = gpumaterial_ptr->last = NULL;
925                 }
926                 drawdata_ptr = DRW_drawdatalist_from_id(id_cow);
927                 if (drawdata_ptr != NULL) {
928                         drawdata_backup = *drawdata_ptr;
929                         drawdata_ptr->first = drawdata_ptr->last = NULL;
930                 }
931         }
932         deg_free_copy_on_write_datablock(id_cow);
933         deg_expand_copy_on_write_datablock(depsgraph, id_node);
934         /* Restore GPU materials. */
935         if (gpumaterial_ptr != NULL) {
936                 *gpumaterial_ptr = gpumaterial_backup;
937         }
938         /* Restore DrawData. */
939         if (drawdata_ptr != NULL) {
940                 *drawdata_ptr = drawdata_backup;
941         }
942         if (id_type == ID_OB) {
943                 deg_restore_object_runtime((Object *)id_cow, &object_runtime_backup);
944         }
945         return id_cow;
946 }
947
948 /* NOTE: Depsgraph is supposed to have ID node already. */
949 ID *deg_update_copy_on_write_datablock(const Depsgraph *depsgraph,
950                                        ID *id_orig)
951 {
952         DEG::IDNode *id_node = depsgraph->find_id_node(id_orig);
953         BLI_assert(id_node != NULL);
954         return deg_update_copy_on_write_datablock(depsgraph, id_node);
955 }
956
957 namespace {
958
959 void discard_armature_edit_mode_pointers(ID *id_cow)
960 {
961         bArmature *armature_cow = (bArmature *)id_cow;
962         armature_cow->edbo = NULL;
963 }
964
965 void discard_curve_edit_mode_pointers(ID *id_cow)
966 {
967         Curve *curve_cow = (Curve *)id_cow;
968         curve_cow->editnurb = NULL;
969         curve_cow->editfont = NULL;
970 }
971
972 void discard_mball_edit_mode_pointers(ID *id_cow)
973 {
974         MetaBall *mball_cow = (MetaBall *)id_cow;
975         mball_cow->editelems = NULL;
976 }
977
978 void discard_lattice_edit_mode_pointers(ID *id_cow)
979 {
980         Lattice *lt_cow = (Lattice *)id_cow;
981         lt_cow->editlatt = NULL;
982 }
983
984 void discard_mesh_edit_mode_pointers(ID *id_cow)
985 {
986         Mesh *mesh_cow = (Mesh *)id_cow;
987         if (mesh_cow->edit_btmesh == NULL) {
988                 return;
989         }
990         BKE_editmesh_free_derivedmesh(mesh_cow->edit_btmesh);
991         MEM_freeN(mesh_cow->edit_btmesh);
992         mesh_cow->edit_btmesh = NULL;
993 }
994
995 void discard_scene_pointers(ID *id_cow)
996 {
997         Scene *scene_cow = (Scene *)id_cow;
998         scene_cow->toolsettings = NULL;
999         scene_cow->eevee.light_cache = NULL;
1000 }
1001
1002 /* NULL-ify all edit mode pointers which points to data from
1003  * original object.
1004  */
1005 void discard_edit_mode_pointers(ID *id_cow)
1006 {
1007         const ID_Type type = GS(id_cow->name);
1008         switch (type) {
1009                 case ID_AR:
1010                         discard_armature_edit_mode_pointers(id_cow);
1011                         break;
1012                 case ID_ME:
1013                         discard_mesh_edit_mode_pointers(id_cow);
1014                         break;
1015                 case ID_CU:
1016                         discard_curve_edit_mode_pointers(id_cow);
1017                         break;
1018                 case ID_MB:
1019                         discard_mball_edit_mode_pointers(id_cow);
1020                         break;
1021                 case ID_LT:
1022                         discard_lattice_edit_mode_pointers(id_cow);
1023                         break;
1024                 case ID_SCE:
1025                         /* Not really edit mode but still needs to run before
1026                          * BKE_libblock_free_datablock() */
1027                         discard_scene_pointers(id_cow);
1028                         break;
1029                 default:
1030                         break;
1031         }
1032 }
1033
1034 }  // namespace
1035
1036 /* Free content of the CoW datablock
1037  * Notes:
1038  * - Does not recurs into nested ID datablocks.
1039  * - Does not free datablock itself.
1040  */
1041 void deg_free_copy_on_write_datablock(ID *id_cow)
1042 {
1043         if (!check_datablock_expanded(id_cow)) {
1044                 /* Actual content was never copied on top of CoW block, we have
1045                  * nothing to free. */
1046                 return;
1047         }
1048         const ID_Type type = GS(id_cow->name);
1049 #ifdef NESTED_ID_NASTY_WORKAROUND
1050         nested_id_hack_discard_pointers(id_cow);
1051 #endif
1052         switch (type) {
1053                 case ID_OB:
1054                 {
1055                         /* TODO(sergey): This workaround is only to prevent free derived
1056                          * caches from modifying object->data. This is currently happening
1057                          * due to mesh/curve datablock boundbox tagging dirty. */
1058                         Object *ob_cow = (Object *)id_cow;
1059                         ob_cow->data = NULL;
1060                         ob_cow->sculpt = NULL;
1061                         break;
1062                 }
1063                 default:
1064                         break;
1065         }
1066         discard_edit_mode_pointers(id_cow);
1067         BKE_libblock_free_datablock(id_cow, 0);
1068         BKE_libblock_free_data(id_cow, false);
1069         /* Signal datablock as not being expanded. */
1070         id_cow->name[0] = '\0';
1071 }
1072
1073 void deg_evaluate_copy_on_write(struct ::Depsgraph *graph,
1074                                 const IDNode *id_node)
1075 {
1076         const DEG::Depsgraph *depsgraph = reinterpret_cast<const DEG::Depsgraph *>(graph);
1077         DEG_debug_print_eval(graph, __func__, id_node->id_orig->name, id_node->id_cow);
1078         if (id_node->id_orig == &depsgraph->scene->id) {
1079                 /* NOTE: This is handled by eval_ctx setup routines, which
1080                  * ensures scene and view layer pointers are valid. */
1081                 return;
1082         }
1083         deg_update_copy_on_write_datablock(depsgraph, id_node);
1084 }
1085
1086 bool deg_validate_copy_on_write_datablock(ID *id_cow)
1087 {
1088         if (id_cow == NULL) {
1089                 return false;
1090         }
1091         ValidateData data;
1092         data.is_valid = true;
1093         BKE_library_foreach_ID_link(NULL,
1094                                     id_cow,
1095                                     foreach_libblock_validate_callback,
1096                                     &data,
1097                                     IDWALK_NOP);
1098         return data.is_valid;
1099 }
1100
1101 void deg_tag_copy_on_write_id(ID *id_cow, const ID *id_orig)
1102 {
1103         BLI_assert(id_cow != id_orig);
1104         BLI_assert((id_orig->tag & LIB_TAG_COPIED_ON_WRITE) == 0);
1105         id_cow->tag |= LIB_TAG_COPIED_ON_WRITE;
1106         /* This ID is no longer localized, is a self-sustaining copy now. */
1107         id_cow->tag &= ~LIB_TAG_LOCALIZED;
1108         id_cow->orig_id = (ID *)id_orig;
1109 }
1110
1111 bool deg_copy_on_write_is_expanded(const ID *id_cow)
1112 {
1113         return check_datablock_expanded(id_cow);
1114 }
1115
1116 bool deg_copy_on_write_is_needed(const ID *id_orig)
1117 {
1118         const ID_Type id_type = GS(id_orig->name);
1119         return !ELEM(id_type, ID_IM);
1120 }
1121
1122 }  // namespace DEG