Blender  V3.3
deg_eval_copy_on_write.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: GPL-2.0-or-later
2  * Copyright 2017 Blender Foundation. All rights reserved. */
3 
8 /* Enable special trickery to treat nested owned IDs (such as nodetree of
9  * material) to be handled in same way as "real" data-blocks, even tho some
10  * internal BKE routines doesn't treat them like that.
11  *
12  * TODO(sergey): Re-evaluate that after new ID handling is in place. */
13 #define NESTED_ID_NASTY_WORKAROUND
14 
15 /* Silence warnings from copying deprecated fields. */
16 #define DNA_DEPRECATED_ALLOW
17 
19 
20 #include <cstring>
21 
22 #include "BLI_listbase.h"
23 #include "BLI_string.h"
24 #include "BLI_threads.h"
25 #include "BLI_utildefines.h"
26 
27 #include "BKE_curve.h"
28 #include "BKE_global.h"
29 #include "BKE_gpencil.h"
31 #include "BKE_idprop.h"
32 #include "BKE_layer.h"
33 #include "BKE_lib_id.h"
34 #include "BKE_scene.h"
35 
36 #include "DEG_depsgraph.h"
37 #include "DEG_depsgraph_query.h"
38 
39 #include "MEM_guardedalloc.h"
40 
41 #include "DNA_ID.h"
42 #include "DNA_anim_types.h"
43 #include "DNA_armature_types.h"
44 #include "DNA_gpencil_types.h"
45 #include "DNA_mesh_types.h"
46 #include "DNA_modifier_types.h"
47 #include "DNA_object_types.h"
48 #include "DNA_particle_types.h"
49 #include "DNA_rigidbody_types.h"
50 #include "DNA_scene_types.h"
51 #include "DNA_sequence_types.h"
52 #include "DNA_simulation_types.h"
53 #include "DNA_sound_types.h"
54 
55 #include "DRW_engine.h"
56 
57 #ifdef NESTED_ID_NASTY_WORKAROUND
58 # include "DNA_curve_types.h"
59 # include "DNA_key_types.h"
60 # include "DNA_lattice_types.h"
61 # include "DNA_light_types.h"
62 # include "DNA_linestyle_types.h"
63 # include "DNA_material_types.h"
64 # include "DNA_meta_types.h"
65 # include "DNA_node_types.h"
66 # include "DNA_texture_types.h"
67 # include "DNA_world_types.h"
68 #endif
69 
70 #include "BKE_action.h"
71 #include "BKE_anim_data.h"
72 #include "BKE_animsys.h"
73 #include "BKE_armature.h"
74 #include "BKE_editmesh.h"
75 #include "BKE_lib_query.h"
76 #include "BKE_modifier.h"
77 #include "BKE_object.h"
78 #include "BKE_pointcache.h"
79 #include "BKE_sound.h"
80 
81 #include "SEQ_relations.h"
82 
85 #include "intern/depsgraph.h"
87 #include "intern/node/deg_node.h"
89 
90 namespace blender::deg {
91 
92 #define DEBUG_PRINT \
93  if (G.debug & G_DEBUG_DEPSGRAPH_EVAL) \
94  printf
95 
96 namespace {
97 
98 #ifdef NESTED_ID_NASTY_WORKAROUND
99 union NestedIDHackTempStorage {
110 };
111 
112 /* Set nested owned ID pointers to nullptr. */
113 void nested_id_hack_discard_pointers(ID *id_cow)
114 {
115  switch (GS(id_cow->name)) {
116 # define SPECIAL_CASE(id_type, dna_type, field) \
117  case id_type: { \
118  ((dna_type *)id_cow)->field = nullptr; \
119  break; \
120  }
121 
123  SPECIAL_CASE(ID_LA, Light, nodetree)
124  SPECIAL_CASE(ID_MA, Material, nodetree)
125  SPECIAL_CASE(ID_TE, Tex, nodetree)
126  SPECIAL_CASE(ID_WO, World, nodetree)
127  SPECIAL_CASE(ID_SIM, Simulation, nodetree)
128 
130  SPECIAL_CASE(ID_LT, Lattice, key)
131  SPECIAL_CASE(ID_ME, Mesh, key)
132 
133  case ID_SCE: {
134  Scene *scene_cow = (Scene *)id_cow;
135  /* Node trees always have their own ID node in the graph, and are
136  * being copied as part of their copy-on-write process. */
137  scene_cow->nodetree = nullptr;
138  /* Tool settings pointer is shared with the original scene. */
139  scene_cow->toolsettings = nullptr;
140  break;
141  }
142 
143  case ID_OB: {
144  /* Clear the ParticleSettings pointer to prevent doubly-freeing it. */
145  Object *ob = (Object *)id_cow;
147  psys->part = nullptr;
148  }
149  break;
150  }
151 # undef SPECIAL_CASE
152 
153  default:
154  break;
155  }
156 }
157 
158 /* Set ID pointer of nested owned IDs (nodetree, key) to nullptr.
159  *
160  * Return pointer to a new ID to be used. */
161 const ID *nested_id_hack_get_discarded_pointers(NestedIDHackTempStorage *storage, const ID *id)
162 {
163  switch (GS(id->name)) {
164 # define SPECIAL_CASE(id_type, dna_type, field, variable) \
165  case id_type: { \
166  storage->variable = dna::shallow_copy(*(dna_type *)id); \
167  storage->variable.field = nullptr; \
168  return &storage->variable.id; \
169  }
170 
172  SPECIAL_CASE(ID_LA, Light, nodetree, lamp)
173  SPECIAL_CASE(ID_MA, Material, nodetree, material)
174  SPECIAL_CASE(ID_TE, Tex, nodetree, tex)
175  SPECIAL_CASE(ID_WO, World, nodetree, world)
177 
180  SPECIAL_CASE(ID_ME, Mesh, key, mesh)
181 
182  case ID_SCE: {
183  storage->scene = *(Scene *)id;
184  storage->scene.toolsettings = nullptr;
185  storage->scene.nodetree = nullptr;
186  return &storage->scene.id;
187  }
188 
189 # undef SPECIAL_CASE
190 
191  default:
192  break;
193  }
194  return id;
195 }
196 
197 /* Set ID pointer of nested owned IDs (nodetree, key) to the original value. */
198 void nested_id_hack_restore_pointers(const ID *old_id, ID *new_id)
199 {
200  if (new_id == nullptr) {
201  return;
202  }
203  switch (GS(old_id->name)) {
204 # define SPECIAL_CASE(id_type, dna_type, field) \
205  case id_type: { \
206  ((dna_type *)(new_id))->field = ((dna_type *)(old_id))->field; \
207  break; \
208  }
209 
211  SPECIAL_CASE(ID_LA, Light, nodetree)
212  SPECIAL_CASE(ID_MA, Material, nodetree)
213  SPECIAL_CASE(ID_SCE, Scene, nodetree)
214  SPECIAL_CASE(ID_TE, Tex, nodetree)
215  SPECIAL_CASE(ID_WO, World, nodetree)
216  SPECIAL_CASE(ID_SIM, Simulation, nodetree)
217 
219  SPECIAL_CASE(ID_LT, Lattice, key)
220  SPECIAL_CASE(ID_ME, Mesh, key)
221 
222 # undef SPECIAL_CASE
223  default:
224  break;
225  }
226 }
227 
228 /* Remap pointer of nested owned IDs (nodetree. key) to the new ID values. */
229 void ntree_hack_remap_pointers(const Depsgraph *depsgraph, ID *id_cow)
230 {
231  switch (GS(id_cow->name)) {
232 # define SPECIAL_CASE(id_type, dna_type, field, field_type) \
233  case id_type: { \
234  dna_type *data = (dna_type *)id_cow; \
235  if (data->field != nullptr) { \
236  ID *ntree_id_cow = depsgraph->get_cow_id(&data->field->id); \
237  if (ntree_id_cow != nullptr) { \
238  DEG_COW_PRINT(" Remapping datablock for %s: id_orig=%p id_cow=%p\n", \
239  data->field->id.name, \
240  data->field, \
241  ntree_id_cow); \
242  data->field = (field_type *)ntree_id_cow; \
243  } \
244  } \
245  break; \
246  }
247 
249  SPECIAL_CASE(ID_LA, Light, nodetree, bNodeTree)
250  SPECIAL_CASE(ID_MA, Material, nodetree, bNodeTree)
251  SPECIAL_CASE(ID_SCE, Scene, nodetree, bNodeTree)
252  SPECIAL_CASE(ID_TE, Tex, nodetree, bNodeTree)
253  SPECIAL_CASE(ID_WO, World, nodetree, bNodeTree)
255 
257  SPECIAL_CASE(ID_LT, Lattice, key, Key)
258  SPECIAL_CASE(ID_ME, Mesh, key, Key)
259 
260 # undef SPECIAL_CASE
261  default:
262  break;
263  }
264 }
265 #endif /* NODETREE_NASTY_WORKAROUND */
266 
267 struct ValidateData {
268  bool is_valid;
269 };
270 
271 /* Similar to generic BKE_id_copy() but does not require main and assumes pointer
272  * is already allocated. */
273 bool id_copy_inplace_no_main(const ID *id, ID *newid)
274 {
275  const ID *id_for_copy = id;
276 
277  if (G.debug & G_DEBUG_DEPSGRAPH_UUID) {
278  const ID_Type id_type = GS(id_for_copy->name);
279  if (id_type == ID_OB) {
280  const Object *object = reinterpret_cast<const Object *>(id_for_copy);
282  }
283  }
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(nullptr,
291  (ID *)id_for_copy,
292  &newid,
294  LIB_ID_COPY_SET_COPIED_ON_WRITE)) != nullptr);
295 
296 #ifdef NESTED_ID_NASTY_WORKAROUND
297  if (result) {
298  nested_id_hack_restore_pointers(id, newid);
299  }
300 #endif
301 
302  return result;
303 }
304 
305 /* Similar to BKE_scene_copy() but does not require main and assumes pointer
306  * is already allocated. */
307 bool scene_copy_inplace_no_main(const Scene *scene, Scene *new_scene)
308 {
309 
310  if (G.debug & G_DEBUG_DEPSGRAPH_UUID) {
312  }
313 
314 #ifdef NESTED_ID_NASTY_WORKAROUND
315  NestedIDHackTempStorage id_hack_storage;
316  const ID *id_for_copy = nested_id_hack_get_discarded_pointers(&id_hack_storage, &scene->id);
317 #else
318  const ID *id_for_copy = &scene->id;
319 #endif
320  bool result = (BKE_id_copy_ex(nullptr,
321  id_for_copy,
322  (ID **)&new_scene,
324  LIB_ID_COPY_SET_COPIED_ON_WRITE)) != nullptr);
325 
326 #ifdef NESTED_ID_NASTY_WORKAROUND
327  if (result) {
328  nested_id_hack_restore_pointers(&scene->id, &new_scene->id);
329  }
330 #endif
331 
332  return result;
333 }
334 
335 /* For the given scene get view layer which corresponds to an original for the
336  * scene's evaluated one. This depends on how the scene is pulled into the
337  * dependency graph. */
338 ViewLayer *get_original_view_layer(const Depsgraph *depsgraph, const IDNode *id_node)
339 {
341  return depsgraph->view_layer;
342  }
344  Scene *scene_orig = reinterpret_cast<Scene *>(id_node->id_orig);
345  return BKE_view_layer_default_render(scene_orig);
346  }
347  /* Is possible to have scene linked indirectly (i.e. via the driver) which
348  * we need to support. Currently there are issues somewhere else, which
349  * makes testing hard. This is a reported problem, so will eventually be
350  * properly fixed.
351  *
352  * TODO(sergey): Support indirectly linked scene. */
353  return nullptr;
354 }
355 
356 /* Remove all view layers but the one which corresponds to an input one. */
357 void scene_remove_unused_view_layers(const Depsgraph *depsgraph,
358  const IDNode *id_node,
359  Scene *scene_cow)
360 {
361  const ViewLayer *view_layer_input;
363  /* If the dependency graph is used for post-processing (such as compositor) we do need to
364  * have access to its view layer names so can not remove any view layers.
365  * On a more positive side we can remove all the bases from all the view layers.
366  *
367  * NOTE: Need to clear pointers which might be pointing to original on freed (due to being
368  * unused) data.
369  *
370  * NOTE: Need to keep view layers for all scenes, even indirect ones. This is because of
371  * render layer node possibly pointing to another scene. */
372  LISTBASE_FOREACH (ViewLayer *, view_layer, &scene_cow->view_layers) {
373  view_layer->basact = nullptr;
374  }
375  return;
376  }
378  /* Indirectly linked scenes means it's not an input scene and not a set scene, and is pulled
379  * via some driver. Such scenes should not have view layers after copy. */
380  view_layer_input = nullptr;
381  }
382  else {
383  view_layer_input = get_original_view_layer(depsgraph, id_node);
384  }
385  ViewLayer *view_layer_eval = nullptr;
386  /* Find evaluated view layer. At the same time we free memory used by
387  * all other of the view layers. */
388  for (ViewLayer *view_layer_cow = reinterpret_cast<ViewLayer *>(scene_cow->view_layers.first),
389  *view_layer_next;
390  view_layer_cow != nullptr;
391  view_layer_cow = view_layer_next) {
392  view_layer_next = view_layer_cow->next;
393  if (view_layer_input != nullptr && STREQ(view_layer_input->name, view_layer_cow->name)) {
394  view_layer_eval = view_layer_cow;
395  }
396  else {
397  BKE_view_layer_free_ex(view_layer_cow, false);
398  }
399  }
400  /* Make evaluated view layer the only one in the evaluated scene (if it exists). */
401  if (view_layer_eval != nullptr) {
402  view_layer_eval->prev = view_layer_eval->next = nullptr;
403  }
404  scene_cow->view_layers.first = view_layer_eval;
405  scene_cow->view_layers.last = view_layer_eval;
406 }
407 
408 void scene_remove_all_bases(Scene *scene_cow)
409 {
410  LISTBASE_FOREACH (ViewLayer *, view_layer, &scene_cow->view_layers) {
411  BLI_freelistN(&view_layer->object_bases);
412  }
413 }
414 
415 /* Makes it so given view layer only has bases corresponding to enabled
416  * objects. */
417 void view_layer_remove_disabled_bases(const Depsgraph *depsgraph, ViewLayer *view_layer)
418 {
419  if (view_layer == nullptr) {
420  return;
421  }
422  ListBase enabled_bases = {nullptr, nullptr};
423  LISTBASE_FOREACH_MUTABLE (Base *, base, &view_layer->object_bases) {
424  /* TODO(sergey): Would be cool to optimize this somehow, or make it so
425  * builder tags bases.
426  *
427  * NOTE: The idea of using id's tag and check whether its copied ot not
428  * is not reliable, since object might be indirectly linked into the
429  * graph.
430  *
431  * NOTE: We are using original base since the object which evaluated base
432  * points to is not yet copied. This is dangerous access from evaluated
433  * domain to original one, but this is how the entire copy-on-write works:
434  * it does need to access original for an initial copy. */
435  const bool is_object_enabled = deg_check_base_in_depsgraph(depsgraph, base);
436  if (is_object_enabled) {
437  BLI_addtail(&enabled_bases, base);
438  }
439  else {
440  if (base == view_layer->basact) {
441  view_layer->basact = nullptr;
442  }
443  MEM_freeN(base);
444  }
445  }
446  view_layer->object_bases = enabled_bases;
447 }
448 
449 void view_layer_update_orig_base_pointers(const ViewLayer *view_layer_orig,
450  ViewLayer *view_layer_eval)
451 {
452  if (view_layer_orig == nullptr || view_layer_eval == nullptr) {
453  /* Happens when scene is only used for parameters or compositor/sequencer. */
454  return;
455  }
456  Base *base_orig = reinterpret_cast<Base *>(view_layer_orig->object_bases.first);
457  LISTBASE_FOREACH (Base *, base_eval, &view_layer_eval->object_bases) {
458  base_eval->base_orig = base_orig;
459  base_orig = base_orig->next;
460  }
461 }
462 
463 void scene_setup_view_layers_before_remap(const Depsgraph *depsgraph,
464  const IDNode *id_node,
465  Scene *scene_cow)
466 {
467  scene_remove_unused_view_layers(depsgraph, id_node, scene_cow);
468  /* If dependency graph is used for post-processing we don't need any bases and can free of them.
469  * Do it before re-mapping to make that process faster. */
471  scene_remove_all_bases(scene_cow);
472  }
473 }
474 
475 void scene_setup_view_layers_after_remap(const Depsgraph *depsgraph,
476  const IDNode *id_node,
477  Scene *scene_cow)
478 {
479  const ViewLayer *view_layer_orig = get_original_view_layer(depsgraph, id_node);
480  ViewLayer *view_layer_eval = reinterpret_cast<ViewLayer *>(scene_cow->view_layers.first);
481  view_layer_update_orig_base_pointers(view_layer_orig, view_layer_eval);
482  view_layer_remove_disabled_bases(depsgraph, view_layer_eval);
483  /* TODO(sergey): Remove objects from collections as well.
484  * Not a HUGE deal for now, nobody is looking into those CURRENTLY.
485  * Still not an excuse to have those. */
486 }
487 
488 /* Check whether given ID is expanded or still a shallow copy. */
489 inline bool check_datablock_expanded(const ID *id_cow)
490 {
491  return (id_cow->name[0] != '\0');
492 }
493 
494 /* Callback for BKE_library_foreach_ID_link which remaps original ID pointer
495  * with the one created by CoW system. */
496 
497 struct RemapCallbackUserData {
498  /* Dependency graph for which remapping is happening. */
500 };
501 
503 {
504  ID **id_p = cb_data->id_pointer;
505  if (*id_p == nullptr) {
506  return IDWALK_RET_NOP;
507  }
508 
509  RemapCallbackUserData *user_data = (RemapCallbackUserData *)cb_data->user_data;
510  const Depsgraph *depsgraph = user_data->depsgraph;
511  ID *id_orig = *id_p;
512  if (deg_copy_on_write_is_needed(id_orig)) {
513  ID *id_cow = depsgraph->get_cow_id(id_orig);
514  BLI_assert(id_cow != nullptr);
516  " Remapping datablock for %s: id_orig=%p id_cow=%p\n", id_orig->name, id_orig, id_cow);
517  *id_p = id_cow;
518  }
519  return IDWALK_RET_NOP;
520 }
521 
522 void update_armature_edit_mode_pointers(const Depsgraph * /*depsgraph*/,
523  const ID *id_orig,
524  ID *id_cow)
525 {
526  const bArmature *armature_orig = (const bArmature *)id_orig;
527  bArmature *armature_cow = (bArmature *)id_cow;
528  armature_cow->edbo = armature_orig->edbo;
529  armature_cow->act_edbone = armature_orig->act_edbone;
530 }
531 
532 void update_curve_edit_mode_pointers(const Depsgraph * /*depsgraph*/,
533  const ID *id_orig,
534  ID *id_cow)
535 {
536  const Curve *curve_orig = (const Curve *)id_orig;
537  Curve *curve_cow = (Curve *)id_cow;
538  curve_cow->editnurb = curve_orig->editnurb;
539  curve_cow->editfont = curve_orig->editfont;
540 }
541 
542 void update_mball_edit_mode_pointers(const Depsgraph * /*depsgraph*/,
543  const ID *id_orig,
544  ID *id_cow)
545 {
546  const MetaBall *mball_orig = (const MetaBall *)id_orig;
547  MetaBall *mball_cow = (MetaBall *)id_cow;
548  mball_cow->editelems = mball_orig->editelems;
549 }
550 
551 void update_lattice_edit_mode_pointers(const Depsgraph * /*depsgraph*/,
552  const ID *id_orig,
553  ID *id_cow)
554 {
555  const Lattice *lt_orig = (const Lattice *)id_orig;
556  Lattice *lt_cow = (Lattice *)id_cow;
557  lt_cow->editlatt = lt_orig->editlatt;
558 }
559 
560 void update_mesh_edit_mode_pointers(const ID *id_orig, ID *id_cow)
561 {
562  const Mesh *mesh_orig = (const Mesh *)id_orig;
563  Mesh *mesh_cow = (Mesh *)id_cow;
564  if (mesh_orig->edit_mesh == nullptr) {
565  return;
566  }
567  mesh_cow->edit_mesh = mesh_orig->edit_mesh;
568 }
569 
570 /* Edit data is stored and owned by original datablocks, copied ones
571  * are simply referencing to them. */
572 void update_edit_mode_pointers(const Depsgraph *depsgraph, const ID *id_orig, ID *id_cow)
573 {
574  const ID_Type type = GS(id_orig->name);
575  switch (type) {
576  case ID_AR:
577  update_armature_edit_mode_pointers(depsgraph, id_orig, id_cow);
578  break;
579  case ID_ME:
580  update_mesh_edit_mode_pointers(id_orig, id_cow);
581  break;
582  case ID_CU_LEGACY:
583  update_curve_edit_mode_pointers(depsgraph, id_orig, id_cow);
584  break;
585  case ID_MB:
586  update_mball_edit_mode_pointers(depsgraph, id_orig, id_cow);
587  break;
588  case ID_LT:
589  update_lattice_edit_mode_pointers(depsgraph, id_orig, id_cow);
590  break;
591  default:
592  break;
593  }
594 }
595 
596 template<typename T>
597 void update_list_orig_pointers(const ListBase *listbase_orig,
598  ListBase *listbase,
599  T *T::*orig_field)
600 {
601  T *element_orig = reinterpret_cast<T *>(listbase_orig->first);
602  T *element_cow = reinterpret_cast<T *>(listbase->first);
603 
604  /* Both lists should have the same number of elements, so the check on
605  * `element_cow` is just to prevent a crash if this is not the case. */
606  while (element_orig != nullptr && element_cow != nullptr) {
607  element_cow->*orig_field = element_orig;
608  element_cow = element_cow->next;
609  element_orig = element_orig->next;
610  }
611 
612  BLI_assert((element_orig == nullptr && element_cow == nullptr) ||
613  !"list of pointers of different sizes, unable to reliably set orig pointer");
614 }
615 
616 void update_particle_system_orig_pointers(const Object *object_orig, Object *object_cow)
617 {
618  update_list_orig_pointers(
619  &object_orig->particlesystem, &object_cow->particlesystem, &ParticleSystem::orig_psys);
620 }
621 
622 void set_particle_system_modifiers_loaded(Object *object_cow)
623 {
624  LISTBASE_FOREACH (ModifierData *, md, &object_cow->modifiers) {
625  if (md->type != eModifierType_ParticleSystem) {
626  continue;
627  }
628  ParticleSystemModifierData *psmd = reinterpret_cast<ParticleSystemModifierData *>(md);
630  }
631 }
632 
633 void reset_particle_system_edit_eval(const Depsgraph *depsgraph, Object *object_cow)
634 {
635  /* Inactive (and render) dependency graphs are living in their own little bubble, should not care
636  * about edit mode at all. */
637  if (!DEG_is_active(reinterpret_cast<const ::Depsgraph *>(depsgraph))) {
638  return;
639  }
640  LISTBASE_FOREACH (ParticleSystem *, psys, &object_cow->particlesystem) {
641  ParticleSystem *orig_psys = psys->orig_psys;
642  if (orig_psys->edit != nullptr) {
643  orig_psys->edit->psys_eval = nullptr;
644  orig_psys->edit->psmd_eval = nullptr;
645  }
646  }
647 }
648 
649 void update_particles_after_copy(const Depsgraph *depsgraph,
650  const Object *object_orig,
651  Object *object_cow)
652 {
653  update_particle_system_orig_pointers(object_orig, object_cow);
654  set_particle_system_modifiers_loaded(object_cow);
655  reset_particle_system_edit_eval(depsgraph, object_cow);
656 }
657 
658 void update_pose_orig_pointers(const bPose *pose_orig, bPose *pose_cow)
659 {
660  update_list_orig_pointers(&pose_orig->chanbase, &pose_cow->chanbase, &bPoseChannel::orig_pchan);
661 }
662 
663 void update_nla_strips_orig_pointers(const ListBase *strips_orig, ListBase *strips_cow)
664 {
665  NlaStrip *strip_orig = reinterpret_cast<NlaStrip *>(strips_orig->first);
666  NlaStrip *strip_cow = reinterpret_cast<NlaStrip *>(strips_cow->first);
667  while (strip_orig != nullptr) {
668  strip_cow->orig_strip = strip_orig;
669  update_nla_strips_orig_pointers(&strip_orig->strips, &strip_cow->strips);
670  strip_cow = strip_cow->next;
671  strip_orig = strip_orig->next;
672  }
673 }
674 
675 void update_nla_tracks_orig_pointers(const ListBase *tracks_orig, ListBase *tracks_cow)
676 {
677  NlaTrack *track_orig = reinterpret_cast<NlaTrack *>(tracks_orig->first);
678  NlaTrack *track_cow = reinterpret_cast<NlaTrack *>(tracks_cow->first);
679  while (track_orig != nullptr) {
680  update_nla_strips_orig_pointers(&track_orig->strips, &track_cow->strips);
681  track_cow = track_cow->next;
682  track_orig = track_orig->next;
683  }
684 }
685 
686 void update_animation_data_after_copy(const ID *id_orig, ID *id_cow)
687 {
688  const AnimData *anim_data_orig = BKE_animdata_from_id(const_cast<ID *>(id_orig));
689  if (anim_data_orig == nullptr) {
690  return;
691  }
692  AnimData *anim_data_cow = BKE_animdata_from_id(id_cow);
693  BLI_assert(anim_data_cow != nullptr);
694  update_nla_tracks_orig_pointers(&anim_data_orig->nla_tracks, &anim_data_cow->nla_tracks);
695 }
696 
697 /* Do some special treatment of data transfer from original ID to its
698  * CoW complementary part.
699  *
700  * Only use for the newly created CoW data-blocks. */
701 void update_id_after_copy(const Depsgraph *depsgraph,
702  const IDNode *id_node,
703  const ID *id_orig,
704  ID *id_cow)
705 {
706  const ID_Type type = GS(id_orig->name);
707  update_animation_data_after_copy(id_orig, id_cow);
708  switch (type) {
709  case ID_OB: {
710  /* Ensure we don't drag someone's else derived mesh to the
711  * new copy of the object. */
712  Object *object_cow = (Object *)id_cow;
713  const Object *object_orig = (const Object *)id_orig;
714  object_cow->mode = object_orig->mode;
715  object_cow->sculpt = object_orig->sculpt;
716  object_cow->runtime.data_orig = (ID *)object_cow->data;
717  if (object_cow->type == OB_ARMATURE) {
718  const bArmature *armature_orig = (bArmature *)object_orig->data;
719  bArmature *armature_cow = (bArmature *)object_cow->data;
720  BKE_pose_remap_bone_pointers(armature_cow, object_cow->pose);
721  if (armature_orig->edbo == nullptr) {
722  update_pose_orig_pointers(object_orig->pose, object_cow->pose);
723  }
724  BKE_pose_pchan_index_rebuild(object_cow->pose);
725  }
726  update_particles_after_copy(depsgraph, object_orig, object_cow);
727  break;
728  }
729  case ID_SCE: {
730  Scene *scene_cow = (Scene *)id_cow;
731  const Scene *scene_orig = (const Scene *)id_orig;
732  scene_cow->toolsettings = scene_orig->toolsettings;
733  scene_cow->eevee.light_cache_data = scene_orig->eevee.light_cache_data;
734  scene_setup_view_layers_after_remap(depsgraph, id_node, reinterpret_cast<Scene *>(id_cow));
735  break;
736  }
737  /* FIXME: This is a temporary fix to update the runtime pointers properly, see T96216. Should
738  * be removed at some point. */
739  case ID_GD: {
740  bGPdata *gpd_cow = (bGPdata *)id_cow;
741  bGPDlayer *gpl = (bGPDlayer *)(gpd_cow->layers.first);
742  if (gpl != nullptr && gpl->runtime.gpl_orig == nullptr) {
743  BKE_gpencil_data_update_orig_pointers((bGPdata *)id_orig, gpd_cow);
744  }
745  break;
746  }
747  default:
748  break;
749  }
750  update_edit_mode_pointers(depsgraph, id_orig, id_cow);
752 }
753 
754 /* This callback is used to validate that all nested ID data-blocks are
755  * properly expanded. */
756 int foreach_libblock_validate_callback(LibraryIDLinkCallbackData *cb_data)
757 {
758  ValidateData *data = (ValidateData *)cb_data->user_data;
759  ID **id_p = cb_data->id_pointer;
760 
761  if (*id_p != nullptr) {
762  if (!check_datablock_expanded(*id_p)) {
763  data->is_valid = false;
764  /* TODO(sergey): Store which is not valid? */
765  }
766  }
767  return IDWALK_RET_NOP;
768 }
769 
770 /* Actual implementation of logic which "expands" all the data which was not
771  * yet copied-on-write.
772  *
773  * NOTE: Expects that CoW datablock is empty. */
774 ID *deg_expand_copy_on_write_datablock(const Depsgraph *depsgraph, const IDNode *id_node)
775 {
776  const ID *id_orig = id_node->id_orig;
777  ID *id_cow = id_node->id_cow;
778  const int id_cow_recalc = id_cow->recalc;
779 
780  /* No need to expand such datablocks, their copied ID is same as original
781  * one already. */
782  if (!deg_copy_on_write_is_needed(id_orig)) {
783  return id_cow;
784  }
785 
787  "Expanding datablock for %s: id_orig=%p id_cow=%p\n", id_orig->name, id_orig, id_cow);
788 
789  /* Sanity checks. */
790  BLI_assert(check_datablock_expanded(id_cow) == false);
791  BLI_assert(id_cow->py_instance == nullptr);
792 
793  /* Copy data from original ID to a copied version. */
794  /* TODO(sergey): Avoid doing full ID copy somehow, make Mesh to reference
795  * original geometry arrays for until those are modified. */
796  /* TODO(sergey): We do some trickery with temp bmain and extra ID pointer
797  * just to be able to use existing API. Ideally we need to replace this with
798  * in-place copy from existing datablock to a prepared memory.
799  *
800  * NOTE: We don't use BKE_main_{new,free} because:
801  * - We don't want heap-allocations here.
802  * - We don't want bmain's content to be freed when main is freed. */
803  bool done = false;
804  /* First we handle special cases which are not covered by BKE_id_copy() yet.
805  * or cases where we want to do something smarter than simple datablock
806  * copy. */
807  const ID_Type id_type = GS(id_orig->name);
808  switch (id_type) {
809  case ID_SCE: {
810  done = scene_copy_inplace_no_main((Scene *)id_orig, (Scene *)id_cow);
811  if (done) {
812  /* NOTE: This is important to do before remap, because this
813  * function will make it so less IDs are to be remapped. */
814  scene_setup_view_layers_before_remap(depsgraph, id_node, (Scene *)id_cow);
815  }
816  break;
817  }
818  case ID_ME: {
819  /* TODO(sergey): Ideally we want to handle meshes in a special
820  * manner here to avoid initial copy of all the geometry arrays. */
821  break;
822  }
823  default:
824  break;
825  }
826  if (!done) {
827  done = id_copy_inplace_no_main(id_orig, id_cow);
828  }
829  if (!done) {
830  BLI_assert_msg(0, "No idea how to perform CoW on datablock");
831  }
832  /* Update pointers to nested ID datablocks. */
834  " Remapping ID links for %s: id_orig=%p id_cow=%p\n", id_orig->name, id_orig, id_cow);
835 
836 #ifdef NESTED_ID_NASTY_WORKAROUND
837  ntree_hack_remap_pointers(depsgraph, id_cow);
838 #endif
839  /* Do it now, so remapping will understand that possibly remapped self ID
840  * is not to be remapped again. */
841  deg_tag_copy_on_write_id(id_cow, id_orig);
842  /* Perform remapping of the nodes. */
843  RemapCallbackUserData user_data = {nullptr};
844  user_data.depsgraph = depsgraph;
846  id_cow,
848  (void *)&user_data,
850  /* Correct or tweak some pointers which are not taken care by foreach
851  * from above. */
852  update_id_after_copy(depsgraph, id_node, id_orig, id_cow);
853  id_cow->recalc = id_cow_recalc;
854  return id_cow;
855 }
856 
857 } // namespace
858 
860 {
861  const ID *id_orig = id_node->id_orig;
862  ID *id_cow = id_node->id_cow;
863  /* Similar to expansion, no need to do anything here. */
864  if (!deg_copy_on_write_is_needed(id_orig)) {
865  return id_cow;
866  }
867 
868  /* When updating object data in edit-mode, don't request COW update since this will duplicate
869  * all object data which is unnecessary when the edit-mode data is used for calculating
870  * modifiers.
871  *
872  * TODO: Investigate modes besides edit-mode. */
873  if (check_datablock_expanded(id_cow) && !id_node->is_cow_explicitly_tagged) {
874  const ID_Type id_type = GS(id_orig->name);
875  /* Pass nullptr as the object is only needed for Curves which do not have edit mode pointers.
876  */
877  if (OB_DATA_SUPPORT_EDITMODE(id_type) && BKE_object_data_is_in_editmode(nullptr, id_orig)) {
878  /* Make sure pointers in the edit mode data are updated in the copy.
879  * This allows depsgraph to pick up changes made in another context after it has been
880  * evaluated. Consider the following scenario:
881  *
882  * - ObjectA in SceneA is using Mesh.
883  * - ObjectB in SceneB is using Mesh (same exact datablock).
884  * - Depsgraph of SceneA is evaluated.
885  * - Depsgraph of SceneB is evaluated.
886  * - User enters edit mode of ObjectA in SceneA. */
887  update_edit_mode_pointers(depsgraph, id_orig, id_cow);
888  return id_cow;
889  }
890  /* In case we don't need to do a copy-on-write, we can use the update cache of the grease
891  * pencil data to do an update-on-write. */
893  (const ::Depsgraph *)depsgraph, (bGPdata *)id_orig)) {
894  BKE_gpencil_update_on_write((bGPdata *)id_orig, (bGPdata *)id_cow);
895  return id_cow;
896  }
897  }
898 
900  backup.init_from_id(id_cow);
902  deg_expand_copy_on_write_datablock(depsgraph, id_node);
903  backup.restore_to_id(id_cow);
904  return id_cow;
905 }
906 
911 {
912  IDNode *id_node = depsgraph->find_id_node(id_orig);
913  BLI_assert(id_node != nullptr);
915 }
916 
917 namespace {
918 
919 void discard_armature_edit_mode_pointers(ID *id_cow)
920 {
921  bArmature *armature_cow = (bArmature *)id_cow;
922  armature_cow->edbo = nullptr;
923 }
924 
925 void discard_curve_edit_mode_pointers(ID *id_cow)
926 {
927  Curve *curve_cow = (Curve *)id_cow;
928  curve_cow->editnurb = nullptr;
929  curve_cow->editfont = nullptr;
930 }
931 
932 void discard_mball_edit_mode_pointers(ID *id_cow)
933 {
934  MetaBall *mball_cow = (MetaBall *)id_cow;
935  mball_cow->editelems = nullptr;
936 }
937 
938 void discard_lattice_edit_mode_pointers(ID *id_cow)
939 {
940  Lattice *lt_cow = (Lattice *)id_cow;
941  lt_cow->editlatt = nullptr;
942 }
943 
944 void discard_mesh_edit_mode_pointers(ID *id_cow)
945 {
946  Mesh *mesh_cow = (Mesh *)id_cow;
947  mesh_cow->edit_mesh = nullptr;
948 }
949 
950 void discard_scene_pointers(ID *id_cow)
951 {
952  Scene *scene_cow = (Scene *)id_cow;
953  scene_cow->toolsettings = nullptr;
954  scene_cow->eevee.light_cache_data = nullptr;
955 }
956 
957 /* nullptr-ify all edit mode pointers which points to data from
958  * original object. */
959 void discard_edit_mode_pointers(ID *id_cow)
960 {
961  const ID_Type type = GS(id_cow->name);
962  switch (type) {
963  case ID_AR:
964  discard_armature_edit_mode_pointers(id_cow);
965  break;
966  case ID_ME:
967  discard_mesh_edit_mode_pointers(id_cow);
968  break;
969  case ID_CU_LEGACY:
970  discard_curve_edit_mode_pointers(id_cow);
971  break;
972  case ID_MB:
973  discard_mball_edit_mode_pointers(id_cow);
974  break;
975  case ID_LT:
976  discard_lattice_edit_mode_pointers(id_cow);
977  break;
978  case ID_SCE:
979  /* Not really edit mode but still needs to run before
980  * BKE_libblock_free_datablock() */
981  discard_scene_pointers(id_cow);
982  break;
983  default:
984  break;
985  }
986 }
987 
988 } // namespace
989 
997 {
998  if (!check_datablock_expanded(id_cow)) {
999  /* Actual content was never copied on top of CoW block, we have
1000  * nothing to free. */
1001  return;
1002  }
1003  const ID_Type type = GS(id_cow->name);
1004 #ifdef NESTED_ID_NASTY_WORKAROUND
1005  nested_id_hack_discard_pointers(id_cow);
1006 #endif
1007  switch (type) {
1008  case ID_OB: {
1009  /* TODO(sergey): This workaround is only to prevent free derived
1010  * caches from modifying object->data. This is currently happening
1011  * due to mesh/curve data-block bound-box tagging dirty. */
1012  Object *ob_cow = (Object *)id_cow;
1013  ob_cow->data = nullptr;
1014  ob_cow->sculpt = nullptr;
1015  break;
1016  }
1017  default:
1018  break;
1019  }
1020  discard_edit_mode_pointers(id_cow);
1021  BKE_libblock_free_data_py(id_cow);
1022  BKE_libblock_free_datablock(id_cow, 0);
1023  BKE_libblock_free_data(id_cow, false);
1024  /* Signal datablock as not being expanded. */
1025  id_cow->name[0] = '\0';
1026 }
1027 
1029 {
1030  const Depsgraph *depsgraph = reinterpret_cast<const Depsgraph *>(graph);
1032  if (id_node->id_orig == &depsgraph->scene->id) {
1033  /* NOTE: This is handled by eval_ctx setup routines, which
1034  * ensures scene and view layer pointers are valid. */
1035  return;
1036  }
1038 }
1039 
1041 {
1042  if (id_cow == nullptr) {
1043  return false;
1044  }
1045  ValidateData data;
1046  data.is_valid = true;
1048  nullptr, id_cow, foreach_libblock_validate_callback, &data, IDWALK_NOP);
1049  return data.is_valid;
1050 }
1051 
1052 void deg_tag_copy_on_write_id(ID *id_cow, const ID *id_orig)
1053 {
1054  BLI_assert(id_cow != id_orig);
1055  BLI_assert((id_orig->tag & LIB_TAG_COPIED_ON_WRITE) == 0);
1056  id_cow->tag |= LIB_TAG_COPIED_ON_WRITE;
1057  /* This ID is no longer localized, is a self-sustaining copy now. */
1058  id_cow->tag &= ~LIB_TAG_LOCALIZED;
1059  id_cow->orig_id = (ID *)id_orig;
1060 }
1061 
1063 {
1064  return check_datablock_expanded(id_cow);
1065 }
1066 
1067 bool deg_copy_on_write_is_needed(const ID *id_orig)
1068 {
1069  const ID_Type id_type = GS(id_orig->name);
1070  return deg_copy_on_write_is_needed(id_type);
1071 }
1072 
1074 {
1075  return ID_TYPE_IS_COW(id_type);
1076 }
1077 
1078 } // namespace blender::deg
Blender kernel action and pose functionality.
struct AnimData * BKE_animdata_from_id(const struct ID *id)
void BKE_animsys_update_driver_array(struct ID *id)
Definition: anim_sys.c:4150
void BKE_pose_remap_bone_pointers(struct bArmature *armature, struct bPose *pose)
Definition: armature.c:2332
void BKE_pose_pchan_index_rebuild(struct bPose *pose)
@ G_DEBUG_DEPSGRAPH_UUID
Definition: BKE_global.h:188
bool BKE_gpencil_can_avoid_full_copy_on_write(const struct Depsgraph *depsgraph, struct bGPdata *gpd)
void BKE_gpencil_data_update_orig_pointers(const struct bGPdata *gpd_orig, const struct bGPdata *gpd_eval)
void BKE_gpencil_update_on_write(struct bGPdata *gpd_orig, struct bGPdata *gpd_eval)
Definition: gpencil.c:3029
void BKE_view_layer_free_ex(struct ViewLayer *view_layer, bool do_id_user)
Definition: layer.c:244
struct ViewLayer * BKE_view_layer_default_render(const struct Scene *scene)
@ LIB_ID_CREATE_NO_ALLOCATE
Definition: BKE_lib_id.h:130
@ LIB_ID_COPY_SET_COPIED_ON_WRITE
Definition: BKE_lib_id.h:143
@ LIB_ID_COPY_LOCALIZE
Definition: BKE_lib_id.h:187
void BKE_libblock_free_data(struct ID *id, bool do_id_user) ATTR_NONNULL()
Definition: lib_id_delete.c:44
struct ID * BKE_id_copy_ex(struct Main *bmain, const struct ID *id, struct ID **r_newid, int flag)
void BKE_libblock_free_data_py(struct ID *id)
void BKE_libblock_free_datablock(struct ID *id, int flag) ATTR_NONNULL()
void BKE_library_foreach_ID_link(struct Main *bmain, struct ID *id, LibraryIDLinkCallback callback, void *user_data, int flag)
Definition: lib_query.c:350
@ IDWALK_NOP
@ IDWALK_IGNORE_EMBEDDED_ID
@ IDWALK_RET_NOP
Definition: BKE_lib_query.h:83
General operations, lookup, etc. for blender objects.
void BKE_object_check_uuids_unique_and_report(const struct Object *object)
bool BKE_object_data_is_in_editmode(const struct Object *ob, const struct ID *id)
#define BLI_assert(a)
Definition: BLI_assert.h:46
#define BLI_assert_msg(a, msg)
Definition: BLI_assert.h:53
#define LISTBASE_FOREACH(type, var, list)
Definition: BLI_listbase.h:336
#define LISTBASE_FOREACH_MUTABLE(type, var, list)
Definition: BLI_listbase.h:354
void void BLI_freelistN(struct ListBase *listbase) ATTR_NONNULL(1)
Definition: listbase.c:466
void BLI_addtail(struct ListBase *listbase, void *vlink) ATTR_NONNULL(1)
Definition: listbase.c:80
#define STREQ(a, b)
struct Depsgraph Depsgraph
Definition: DEG_depsgraph.h:35
bool DEG_is_active(const struct Depsgraph *depsgraph)
Definition: depsgraph.cc:312
void DEG_debug_print_eval(struct Depsgraph *depsgraph, const char *function_name, const char *object_name, const void *object_address)
ID and Library types, which are fundamental for sdna.
#define ID_TYPE_IS_COW(_id_type)
Definition: DNA_ID.h:601
@ LIB_TAG_COPIED_ON_WRITE
Definition: DNA_ID.h:720
@ LIB_TAG_LOCALIZED
Definition: DNA_ID.h:740
ID_Type
Definition: DNA_ID_enums.h:44
@ ID_AR
Definition: DNA_ID_enums.h:66
@ ID_TE
Definition: DNA_ID_enums.h:52
@ ID_LA
Definition: DNA_ID_enums.h:55
@ ID_SCE
Definition: DNA_ID_enums.h:45
@ ID_LS
Definition: DNA_ID_enums.h:75
@ ID_GD
Definition: DNA_ID_enums.h:71
@ ID_WO
Definition: DNA_ID_enums.h:59
@ ID_SIM
Definition: DNA_ID_enums.h:84
@ ID_MA
Definition: DNA_ID_enums.h:51
@ ID_CU_LEGACY
Definition: DNA_ID_enums.h:49
@ ID_ME
Definition: DNA_ID_enums.h:48
@ ID_MB
Definition: DNA_ID_enums.h:50
@ ID_LT
Definition: DNA_ID_enums.h:54
@ ID_OB
Definition: DNA_ID_enums.h:47
@ eParticleSystemFlag_file_loaded
@ eModifierType_ParticleSystem
Object is a sort of wrapper for general info.
@ OB_ARMATURE
#define OB_DATA_SUPPORT_EDITMODE(_type)
Types and defines for representing Rigid Body entities.
_GL_VOID GLfloat value _GL_VOID_RET _GL_VOID const GLuint GLboolean *residences _GL_BOOL_RET _GL_VOID GLsizei GLfloat GLfloat GLfloat GLfloat const GLubyte *bitmap _GL_VOID_RET _GL_VOID GLenum type
Read Guarded memory(de)allocation.
Depsgraph * graph
const IDNode * id_node
Scene scene
Curve curve
FreestyleLineStyle linestyle
bool is_valid
World world
#define SPECIAL_CASE(id_type, dna_type, field)
Material material
Simulation simulation
Light lamp
const Depsgraph * depsgraph
Lattice lattice
#define DEG_COW_PRINT(format,...)
AnimationBackup * backup
void * user_data
#define GS(x)
Definition: iris.c:225
static int foreach_libblock_remap_callback(LibraryIDLinkCallbackData *cb_data)
Definition: lib_remap.c:150
void(* MEM_freeN)(void *vmemh)
Definition: mallocn.c:27
#define T
#define G(x, y, z)
bool deg_check_base_in_depsgraph(const Depsgraph *graph, Base *base)
Definition: deg_builder.cc:48
ID * deg_update_copy_on_write_datablock(const Depsgraph *depsgraph, const IDNode *id_node)
bool deg_copy_on_write_is_needed(const ID *id_orig)
void deg_tag_copy_on_write_id(ID *id_cow, const ID *id_orig)
bool deg_validate_copy_on_write_datablock(ID *id_cow)
@ DEG_ID_LINKED_INDIRECTLY
Definition: deg_node_id.h:25
@ DEG_ID_LINKED_VIA_SET
Definition: deg_node_id.h:27
@ DEG_ID_LINKED_DIRECTLY
Definition: deg_node_id.h:29
bool deg_copy_on_write_is_expanded(const ID *id_cow)
void deg_evaluate_copy_on_write(struct ::Depsgraph *graph, const IDNode *id_node)
void deg_free_copy_on_write_datablock(ID *id_cow)
void SEQ_relations_check_uuids_unique_and_report(const Scene *scene)
ListBase nla_tracks
struct Base * next
struct EditFont * editfont
EditNurb * editnurb
Definition: DNA_ID.h:368
void * py_instance
Definition: DNA_ID.h:435
int tag
Definition: DNA_ID.h:387
int recalc
Definition: DNA_ID.h:390
struct ID * orig_id
Definition: DNA_ID.h:419
char name[66]
Definition: DNA_ID.h:378
struct EditLatt * editlatt
void * last
Definition: DNA_listBase.h:31
void * first
Definition: DNA_listBase.h:31
struct BMEditMesh * edit_mesh
ListBase * editelems
struct NlaStrip * next
ListBase strips
struct NlaStrip * orig_strip
ListBase strips
struct NlaTrack * next
struct ID * data_orig
ListBase particlesystem
struct bPose * pose
ListBase modifiers
Object_Runtime runtime
struct SculptSession * sculpt
void * data
struct ParticleSystemModifierData * psmd_eval
struct ParticleSystem * psys_eval
struct PTCacheEdit * edit
struct ParticleSystem * orig_psys
struct LightCache * light_cache_data
struct bNodeTree * nodetree
string name
Definition: scene.h:198
struct ToolSettings * toolsettings
ListBase view_layers
struct SceneEEVEE eevee
struct ViewLayer * prev
struct ViewLayer * next
struct Base * basact
ListBase object_bases
char name[64]
struct EditBone * act_edbone
ListBase * edbo
struct bGPDlayer * gpl_orig
bGPDlayer_Runtime runtime
ListBase layers
struct bPoseChannel * orig_pchan
ListBase chanbase
ID * get_cow_id(const ID *id_orig) const
Definition: depsgraph.cc:236
IDNode * find_id_node(const ID *id) const
Definition: depsgraph.cc:101
ViewLayer * view_layer
Definition: depsgraph.h:129
eDepsNode_LinkedState_Type linked_state
Definition: deg_node_id.h:92