From d1166dcf315f264d514eb48ddceb13deecf01353 Mon Sep 17 00:00:00 2001 From: Sergey Sharybin Date: Fri, 29 Nov 2019 11:43:40 +0100 Subject: Depsgraph: Refactor, split runtime backup into smaller files It started to be a long code of all various cases in a single file, which started to be really confusing. --- .../intern/eval/deg_eval_copy_on_write.cc | 544 +-------------------- 1 file changed, 3 insertions(+), 541 deletions(-) (limited to 'source/blender/depsgraph/intern/eval/deg_eval_copy_on_write.cc') diff --git a/source/blender/depsgraph/intern/eval/deg_eval_copy_on_write.cc b/source/blender/depsgraph/intern/eval/deg_eval_copy_on_write.cc index 8a33453b923..3a2cf35f4d5 100644 --- a/source/blender/depsgraph/intern/eval/deg_eval_copy_on_write.cc +++ b/source/blender/depsgraph/intern/eval/deg_eval_copy_on_write.cc @@ -21,7 +21,7 @@ * \ingroup depsgraph */ -/* Enable special; trickery to treat nested owned IDs (such as nodetree of +/* Enable special trickery to treat nested owned IDs (such as nodetree of * material) to be handled in same way as "real" data-blocks, even tho some * internal BKE routines doesn't treat them like that. * @@ -95,6 +95,7 @@ extern "C" { #include "intern/depsgraph.h" #include "intern/builder/deg_builder.h" #include "intern/builder/deg_builder_nodes.h" +#include "intern/eval/deg_eval_runtime_backup.h" #include "intern/node/deg_node.h" #include "intern/node/deg_node_id.h" @@ -945,545 +946,6 @@ ID *deg_expand_copy_on_write_datablock(const Depsgraph *depsgraph, return deg_expand_copy_on_write_datablock(depsgraph, id_node, node_builder, create_placeholders); } -namespace { - -/* Backup of sequencer strips runtime data. */ - -/* Backup of a single strip. */ -class SequenceBackup { - public: - SequenceBackup() - { - reset(); - } - - inline void reset() - { - scene_sound = NULL; - } - - void init_from_sequence(Sequence *sequence) - { - scene_sound = sequence->scene_sound; - - sequence->scene_sound = NULL; - } - - void restore_to_sequence(Sequence *sequence) - { - sequence->scene_sound = scene_sound; - reset(); - } - - inline bool isEmpty() const - { - return (scene_sound == NULL); - } - - void *scene_sound; -}; - -class SequencerBackup { - public: - SequencerBackup(); - - void init_from_scene(Scene *scene); - void restore_to_scene(Scene *scene); - - typedef map SequencesBackupMap; - SequencesBackupMap sequences_backup; -}; - -SequencerBackup::SequencerBackup() -{ -} - -void SequencerBackup::init_from_scene(Scene *scene) -{ - Sequence *sequence; - SEQ_BEGIN (scene->ed, sequence) { - SequenceBackup sequence_backup; - sequence_backup.init_from_sequence(sequence); - if (!sequence_backup.isEmpty()) { - sequences_backup.insert(make_pair(sequence->orig_sequence, sequence_backup)); - } - } - SEQ_END; -} - -void SequencerBackup::restore_to_scene(Scene *scene) -{ - Sequence *sequence; - SEQ_BEGIN (scene->ed, sequence) { - SequencesBackupMap::iterator it = sequences_backup.find(sequence->orig_sequence); - if (it == sequences_backup.end()) { - continue; - } - SequenceBackup &sequence_backup = it->second; - sequence_backup.restore_to_sequence(sequence); - } - SEQ_END; - /* Cleanup audio while the scene is still known. */ - for (SequencesBackupMap::value_type &it : sequences_backup) { - SequenceBackup &sequence_backup = it.second; - if (sequence_backup.scene_sound != NULL) { - BKE_sound_remove_scene_sound(scene, sequence_backup.scene_sound); - } - } -} - -/* Backup of scene runtime data. */ - -class SceneBackup { - public: - SceneBackup(); - - void reset(); - - void init_from_scene(Scene *scene); - void restore_to_scene(Scene *scene); - - /* Sound/audio related pointers of the scene itself. - * - * NOTE: Scene can not disappear after relations update, because otherwise the entire dependency - * graph will be gone. This means we don't need to compare original scene pointer, or worry about - * freeing those if they cant' be restored: we just copy them over to a new scene. */ - void *sound_scene; - void *playback_handle; - void *sound_scrub_handle; - void *speaker_handles; - float rigidbody_last_time; - - SequencerBackup sequencer_backup; -}; - -SceneBackup::SceneBackup() -{ - reset(); -} - -void SceneBackup::reset() -{ - sound_scene = NULL; - playback_handle = NULL; - sound_scrub_handle = NULL; - speaker_handles = NULL; - rigidbody_last_time = -1; -} - -void SceneBackup::init_from_scene(Scene *scene) -{ - sound_scene = scene->sound_scene; - playback_handle = scene->playback_handle; - sound_scrub_handle = scene->sound_scrub_handle; - speaker_handles = scene->speaker_handles; - - if (scene->rigidbody_world != NULL) { - rigidbody_last_time = scene->rigidbody_world->ltime; - } - - /* Clear pointers stored in the scene, so they are not freed when copied-on-written datablock - * is freed for re-allocation. */ - scene->sound_scene = NULL; - scene->playback_handle = NULL; - scene->sound_scrub_handle = NULL; - scene->speaker_handles = NULL; - - sequencer_backup.init_from_scene(scene); -} - -void SceneBackup::restore_to_scene(Scene *scene) -{ - scene->sound_scene = sound_scene; - scene->playback_handle = playback_handle; - scene->sound_scrub_handle = sound_scrub_handle; - scene->speaker_handles = speaker_handles; - - if (scene->rigidbody_world != NULL) { - scene->rigidbody_world->ltime = rigidbody_last_time; - } - - sequencer_backup.restore_to_scene(scene); - - reset(); -} - -/* Backup of sound datablocks runtime data. */ - -class SoundBackup { - public: - SoundBackup(); - - void reset(); - - void init_from_sound(bSound *sound); - void restore_to_sound(bSound *sound); - - void *cache; - void *waveform; - void *playback_handle; -}; - -SoundBackup::SoundBackup() -{ - reset(); -} - -void SoundBackup::reset() -{ - cache = NULL; - waveform = NULL; - playback_handle = NULL; -} - -void SoundBackup::init_from_sound(bSound *sound) -{ - cache = sound->cache; - waveform = sound->waveform; - playback_handle = sound->playback_handle; - - sound->cache = NULL; - sound->waveform = NULL; - sound->playback_handle = NULL; -} - -void SoundBackup::restore_to_sound(bSound *sound) -{ - sound->cache = cache; - sound->waveform = waveform; - sound->playback_handle = playback_handle; - - reset(); -} - -/* Identifier used to match modifiers to backup/restore their runtime data. - * Identification is happening using original modifier data pointer and the - * modifier type. - * It is not enough to only pointer, since it's possible to have a situation - * when modifier is removed and a new one added, and due to memory allocation - * policy they might have same pointer. - * By adding type into matching we are at least ensuring that modifier will not - * try to interpret runtime data created by another modifier type. */ -class ModifierDataBackupID { - public: - ModifierDataBackupID() : ModifierDataBackupID(NULL, eModifierType_None) - { - } - - ModifierDataBackupID(ModifierData *modifier_data, ModifierType type) - : modifier_data(modifier_data), type(type) - { - } - - bool operator<(const ModifierDataBackupID &other) const - { - if (modifier_data < other.modifier_data) { - return true; - } - if (modifier_data == other.modifier_data) { - return static_cast(type) < static_cast(other.type); - } - return false; - } - - ModifierData *modifier_data; - ModifierType type; -}; - -/* Storage for backed up runtime modifier data. */ -typedef map ModifierRuntimeDataBackup; - -/* Storage for backed up pose channel runtime data. */ -typedef map PoseChannelRuntimeDataBackup; - -class ObjectRuntimeBackup { - public: - ObjectRuntimeBackup() : base_flag(0), base_local_view_bits(0) - { - /* TODO(sergey): Use something like BKE_object_runtime_reset(). */ - memset(&runtime, 0, sizeof(runtime)); - } - - /* Make a backup of object's evaluation runtime data, additionally - * make object to be safe for free without invalidating backed up - * pointers. */ - void init_from_object(Object *object); - void backup_modifier_runtime_data(Object *object); - void backup_pose_channel_runtime_data(Object *object); - - /* Restore all fields to the given object. */ - void restore_to_object(Object *object); - /* NOTE: Will free all runtime data which has not been restored. */ - void restore_modifier_runtime_data(Object *object); - void restore_pose_channel_runtime_data(Object *object); - - Object_Runtime runtime; - short base_flag; - unsigned short base_local_view_bits; - ModifierRuntimeDataBackup modifier_runtime_data; - PoseChannelRuntimeDataBackup pose_channel_runtime_data; -}; - -void ObjectRuntimeBackup::init_from_object(Object *object) -{ - /* Store evaluated mesh and curve_cache, and make sure we don't free it. */ - Mesh *mesh_eval = object->runtime.mesh_eval; - runtime = object->runtime; - BKE_object_runtime_reset(object); - /* Keep bbox (for now at least). */ - object->runtime.bb = runtime.bb; - /* Object update will override actual object->data to an evaluated version. - * Need to make sure we don't have data set to evaluated one before free - * anything. */ - if (mesh_eval != NULL && object->data == mesh_eval) { - object->data = runtime.mesh_orig; - } - /* Make a backup of base flags. */ - base_flag = object->base_flag; - base_local_view_bits = object->base_local_view_bits; - /* Backup tuntime data of all modifiers. */ - backup_modifier_runtime_data(object); - /* Backup runtime data of all pose channels. */ - backup_pose_channel_runtime_data(object); -} - -inline ModifierDataBackupID create_modifier_data_id(const ModifierData *modifier_data) -{ - return ModifierDataBackupID(modifier_data->orig_modifier_data, - static_cast(modifier_data->type)); -} - -void ObjectRuntimeBackup::backup_modifier_runtime_data(Object *object) -{ - LISTBASE_FOREACH (ModifierData *, modifier_data, &object->modifiers) { - if (modifier_data->runtime == NULL) { - continue; - } - BLI_assert(modifier_data->orig_modifier_data != NULL); - ModifierDataBackupID modifier_data_id = create_modifier_data_id(modifier_data); - modifier_runtime_data.insert(make_pair(modifier_data_id, modifier_data->runtime)); - modifier_data->runtime = NULL; - } -} - -void ObjectRuntimeBackup::backup_pose_channel_runtime_data(Object *object) -{ - if (object->pose != NULL) { - LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) { - /* This is NULL in Edit mode. */ - if (pchan->orig_pchan != NULL) { - pose_channel_runtime_data[pchan->orig_pchan] = pchan->runtime; - BKE_pose_channel_runtime_reset(&pchan->runtime); - } - } - } -} - -void ObjectRuntimeBackup::restore_to_object(Object *object) -{ - Mesh *mesh_orig = object->runtime.mesh_orig; - BoundBox *bb = object->runtime.bb; - object->runtime = runtime; - object->runtime.mesh_orig = mesh_orig; - object->runtime.bb = bb; - if (object->type == OB_MESH && object->runtime.mesh_eval != NULL) { - if (object->id.recalc & ID_RECALC_GEOMETRY) { - /* If geometry is tagged for update it means, that part of - * evaluated mesh are not valid anymore. In this case we can not - * have any "persistent" pointers to point to an invalid data. - * - * We restore object's data datablock to an original copy of - * that datablock. */ - object->data = mesh_orig; - - /* After that, immediately free the invalidated caches. */ - BKE_object_free_derived_caches(object); - } - else { - Mesh *mesh_eval = object->runtime.mesh_eval; - /* Do same thing as object update: override actual object data - * pointer with evaluated datablock. */ - object->data = mesh_eval; - /* Evaluated mesh simply copied edit_mesh pointer from - * original mesh during update, need to make sure no dead - * pointers are left behind. */ - mesh_eval->edit_mesh = mesh_orig->edit_mesh; - } - } - object->base_flag = base_flag; - object->base_local_view_bits = base_local_view_bits; - /* Restore modifier's runtime data. - * NOTE: Data of unused modifiers will be freed there. */ - restore_modifier_runtime_data(object); - restore_pose_channel_runtime_data(object); -} - -void ObjectRuntimeBackup::restore_modifier_runtime_data(Object *object) -{ - LISTBASE_FOREACH (ModifierData *, modifier_data, &object->modifiers) { - BLI_assert(modifier_data->orig_modifier_data != NULL); - ModifierDataBackupID modifier_data_id = create_modifier_data_id(modifier_data); - ModifierRuntimeDataBackup::iterator runtime_data_iterator = modifier_runtime_data.find( - modifier_data_id); - if (runtime_data_iterator != modifier_runtime_data.end()) { - modifier_data->runtime = runtime_data_iterator->second; - runtime_data_iterator->second = NULL; - } - } - for (ModifierRuntimeDataBackup::value_type value : modifier_runtime_data) { - const ModifierDataBackupID modifier_data_id = value.first; - void *runtime = value.second; - if (value.second == NULL) { - continue; - } - const ModifierTypeInfo *modifier_type_info = modifierType_getInfo(modifier_data_id.type); - BLI_assert(modifier_type_info != NULL); - modifier_type_info->freeRuntimeData(runtime); - } -} - -void ObjectRuntimeBackup::restore_pose_channel_runtime_data(Object *object) -{ - if (object->pose != NULL) { - LISTBASE_FOREACH (bPoseChannel *, pchan, &object->pose->chanbase) { - /* This is NULL in Edit mode. */ - if (pchan->orig_pchan != NULL) { - PoseChannelRuntimeDataBackup::iterator runtime_data_iterator = - pose_channel_runtime_data.find(pchan->orig_pchan); - if (runtime_data_iterator != pose_channel_runtime_data.end()) { - pchan->runtime = runtime_data_iterator->second; - pose_channel_runtime_data.erase(runtime_data_iterator); - } - } - } - } - for (PoseChannelRuntimeDataBackup::value_type &value : pose_channel_runtime_data) { - BKE_pose_channel_runtime_free(&value.second); - } -} - -/* Backup of movie clip runtime data. */ - -class MovieClipBackup { - public: - MovieClipBackup(); - - void reset(); - - void init_from_movieclip(MovieClip *movieclip); - void restore_to_movieclip(MovieClip *movieclip); - - struct anim *anim; - struct MovieClipCache *cache; -}; - -MovieClipBackup::MovieClipBackup() -{ - reset(); -} - -void MovieClipBackup::reset() -{ - anim = NULL; - cache = NULL; -} - -void MovieClipBackup::init_from_movieclip(MovieClip *movieclip) -{ - anim = movieclip->anim; - cache = movieclip->cache; - /* Clear pointers stored in the movie clip, so they are not freed when copied-on-written - * datablock is freed for re-allocation. */ - movieclip->anim = NULL; - movieclip->cache = NULL; -} - -void MovieClipBackup::restore_to_movieclip(MovieClip *movieclip) -{ - movieclip->anim = anim; - movieclip->cache = cache; - - reset(); -} - -class RuntimeBackup { - public: - RuntimeBackup() : drawdata_ptr(NULL) - { - drawdata_backup.first = drawdata_backup.last = NULL; - } - - /* NOTE: Will reset all runbtime fields which has been backed up to NULL. */ - void init_from_id(ID *id); - - /* Restore fields to the given ID. */ - void restore_to_id(ID *id); - - SceneBackup scene_backup; - SoundBackup sound_backup; - ObjectRuntimeBackup object_backup; - DrawDataList drawdata_backup; - DrawDataList *drawdata_ptr; - MovieClipBackup movieclip_backup; -}; - -void RuntimeBackup::init_from_id(ID *id) -{ - if (!check_datablock_expanded(id)) { - return; - } - const ID_Type id_type = GS(id->name); - switch (id_type) { - case ID_OB: - object_backup.init_from_object(reinterpret_cast(id)); - break; - case ID_SCE: - scene_backup.init_from_scene(reinterpret_cast(id)); - break; - case ID_SO: - sound_backup.init_from_sound(reinterpret_cast(id)); - break; - case ID_MC: - movieclip_backup.init_from_movieclip(reinterpret_cast(id)); - break; - default: - break; - } - /* Note that we never free GPU draw data from here since that's not - * safe for threading and draw data is likely to be re-used. */ - drawdata_ptr = DRW_drawdatalist_from_id(id); - if (drawdata_ptr != NULL) { - drawdata_backup = *drawdata_ptr; - drawdata_ptr->first = drawdata_ptr->last = NULL; - } -} - -void RuntimeBackup::restore_to_id(ID *id) -{ - const ID_Type id_type = GS(id->name); - switch (id_type) { - case ID_OB: - object_backup.restore_to_object(reinterpret_cast(id)); - break; - case ID_SCE: - scene_backup.restore_to_scene(reinterpret_cast(id)); - break; - case ID_SO: - sound_backup.restore_to_sound(reinterpret_cast(id)); - break; - case ID_MC: - movieclip_backup.restore_to_movieclip(reinterpret_cast(id)); - break; - default: - break; - } - if (drawdata_ptr != NULL) { - *drawdata_ptr = drawdata_backup; - } -} - -} // namespace - ID *deg_update_copy_on_write_datablock(const Depsgraph *depsgraph, const IDNode *id_node) { const ID *id_orig = id_node->id_orig; @@ -1492,7 +954,7 @@ ID *deg_update_copy_on_write_datablock(const Depsgraph *depsgraph, const IDNode if (!deg_copy_on_write_is_needed(id_orig)) { return id_cow; } - RuntimeBackup backup; + RuntimeBackup backup(depsgraph); backup.init_from_id(id_cow); deg_free_copy_on_write_datablock(id_cow); deg_expand_copy_on_write_datablock(depsgraph, id_node); -- cgit v1.2.3