Commit: 0613697fd9a5b32e41ff1b6939d2e6ae62180dba Author: Jeroen Bakker Date: Mon Dec 20 10:24:09 2021 +0100 Branches: temp-T94185-id_remapping-experiment-a https://developer.blender.org/rB0613697fd9a5b32e41ff1b6939d2e6ae62180dba
Modified st->id_remap to new interface. =================================================================== M source/blender/blenkernel/BKE_lib_remap.h M source/blender/blenkernel/BKE_screen.h M source/blender/blenkernel/intern/lib_id_remapper.cc M source/blender/blenkernel/intern/lib_id_remapper_test.cc M source/blender/editors/space_action/space_action.c M source/blender/editors/space_buttons/space_buttons.c M source/blender/editors/space_clip/space_clip.c M source/blender/editors/space_graph/space_graph.c M source/blender/editors/space_image/space_image.c M source/blender/editors/space_nla/space_nla.c M source/blender/editors/space_node/space_node.cc M source/blender/editors/space_sequencer/space_sequencer.c M source/blender/editors/space_spreadsheet/space_spreadsheet.cc M source/blender/editors/space_text/space_text.c M source/blender/editors/space_view3d/space_view3d.c M source/blender/editors/util/ed_util.c =================================================================== diff --git a/source/blender/blenkernel/BKE_lib_remap.h b/source/blender/blenkernel/BKE_lib_remap.h index f360717a804..0703a99f830 100644 --- a/source/blender/blenkernel/BKE_lib_remap.h +++ b/source/blender/blenkernel/BKE_lib_remap.h @@ -38,6 +38,8 @@ extern "C" { #endif +struct ID; + /* BKE_libblock_free, delete are declared in BKE_lib_id.h for convenience. */ /* Also IDRemap->flag. */ @@ -163,8 +165,16 @@ typedef enum IDRemapperApplyResult { ID_REMAP_SOURCE_NOT_MAPPABLE, /** Source has been remapped to a new pointer. */ ID_REMAP_SOURCE_REMAPPED, + /** Source has been set to NULL. */ + ID_REMAP_SOURCE_UNASSIGNED, } IDRemapperApplyResult; +typedef enum IDRemapperApplyOptions { + ID_REMAP_APPLY_DEFAULT = 0, + ID_REMAP_APPLY_UPDATE_REFCOUNT = (1 << 0), + ID_REMAP_APPLY_ENSURE_REAL = (1 << 1), +} IDRemapperApplyOptions; + /** * \brief Create a new ID Remapper. * @@ -182,7 +192,9 @@ void BKE_id_remapper_add(struct IDRemapper *id_remapper, struct ID *old_id, stru * Update the id pointer stored in the given id_ptr_ptr id a remapping rule exists. */ IDRemapperApplyResult BKE_id_remapper_apply(const struct IDRemapper *id_remapper, - struct ID **id_ptr_ptr); + struct ID **id_ptr_ptr, + IDRemapperApplyOptions options); +bool BKE_id_remapper_has_mapping_for(const struct IDRemapper *id_remapper, uint64_t type_filter); #ifdef __cplusplus } diff --git a/source/blender/blenkernel/BKE_screen.h b/source/blender/blenkernel/BKE_screen.h index fd0682ee8f0..4ef7d64a57c 100644 --- a/source/blender/blenkernel/BKE_screen.h +++ b/source/blender/blenkernel/BKE_screen.h @@ -38,6 +38,7 @@ struct BlendLibReader; struct BlendWriter; struct Header; struct ID; +struct IDRemapper; struct LibraryForeachIDData; struct ListBase; struct Menu; @@ -117,10 +118,7 @@ typedef struct SpaceType { bContextDataCallback context; /* Used when we want to replace an ID by another (or NULL). */ - void (*id_remap)(struct ScrArea *area, - struct SpaceLink *sl, - struct ID *old_id, - struct ID *new_id); + void (*id_remap)(struct ScrArea *area, struct SpaceLink *sl, const struct IDRemapper *mappings); int (*space_subtype_get)(struct ScrArea *area); void (*space_subtype_set)(struct ScrArea *area, int value); diff --git a/source/blender/blenkernel/intern/lib_id_remapper.cc b/source/blender/blenkernel/intern/lib_id_remapper.cc index a020f35596c..fc9318a8fdb 100644 --- a/source/blender/blenkernel/intern/lib_id_remapper.cc +++ b/source/blender/blenkernel/intern/lib_id_remapper.cc @@ -1,23 +1,36 @@ +#include "DNA_ID.h" + +#include "BKE_idtype.h" +#include "BKE_lib_id.h" #include "BKE_lib_remap.h" #include "MEM_guardedalloc.h" #include "BLI_map.hh" +using IDTypeFilter = uint64_t; + namespace blender::bke::id::remapper { struct IDRemapper { private: Map<ID *, ID *> mappings; + IDTypeFilter source_types = 0; public: void add(ID *old_id, ID *new_id) { BLI_assert(old_id != nullptr); mappings.add_as(old_id, new_id); + source_types |= BKE_idtype_idcode_to_idfilter(GS(old_id->name)); + } + + bool contains_mappings_for_any(IDTypeFilter filter) const + { + return (source_types & filter) != 0; } - IDRemapperApplyResult apply(ID **id_ptr_ptr) const + IDRemapperApplyResult apply(ID **id_ptr_ptr, IDRemapperApplyOptions options) const { BLI_assert(id_ptr_ptr != nullptr); if (*id_ptr_ptr == nullptr) { @@ -28,7 +41,22 @@ struct IDRemapper { return ID_REMAP_SOURCE_UNAVAILABLE; } + if (options & ID_REMAP_APPLY_UPDATE_REFCOUNT) { + id_us_min(*id_ptr_ptr); + } + *id_ptr_ptr = mappings.lookup(*id_ptr_ptr); + if (*id_ptr_ptr == nullptr) { + return ID_REMAP_SOURCE_UNASSIGNED; + } + + if (options & ID_REMAP_APPLY_UPDATE_REFCOUNT) { + id_us_plus(*id_ptr_ptr); + } + + if (options & ID_REMAP_APPLY_ENSURE_REAL) { + id_us_ensure_real(*id_ptr_ptr); + } return ID_REMAP_SOURCE_REMAPPED; } }; @@ -74,9 +102,17 @@ void BKE_id_remapper_add(IDRemapper *id_remapper, ID *old_id, ID *new_id) remapper->add(old_id, new_id); } -IDRemapperApplyResult BKE_id_remapper_apply(const IDRemapper *id_remapper, ID **id_ptr_ptr) +bool BKE_id_remapper_has_mapping_for(const struct IDRemapper *id_remapper, uint64_t type_filter) +{ + const blender::bke::id::remapper::IDRemapper *remapper = unwrap_const(id_remapper); + return remapper->contains_mappings_for_any(type_filter); +} + +IDRemapperApplyResult BKE_id_remapper_apply(const IDRemapper *id_remapper, + ID **id_ptr_ptr, + const IDRemapperApplyOptions options) { const blender::bke::id::remapper::IDRemapper *remapper = unwrap_const(id_remapper); - return remapper->apply(id_ptr_ptr); + return remapper->apply(id_ptr_ptr, options); } } \ No newline at end of file diff --git a/source/blender/blenkernel/intern/lib_id_remapper_test.cc b/source/blender/blenkernel/intern/lib_id_remapper_test.cc index d802a59d869..ba99fdaca05 100644 --- a/source/blender/blenkernel/intern/lib_id_remapper_test.cc +++ b/source/blender/blenkernel/intern/lib_id_remapper_test.cc @@ -29,7 +29,7 @@ TEST(lib_id_remapper, unavailable) ID *idp = &id1; IDRemapper *remapper = BKE_id_remapper_create(); - IDRemapperApplyResult result = BKE_id_remapper_apply(remapper, &idp); + IDRemapperApplyResult result = BKE_id_remapper_apply(remapper, &idp, ID_REMAP_APPLY_DEFAULT); EXPECT_EQ(result, ID_REMAP_SOURCE_UNAVAILABLE); BKE_id_remapper_free(remapper); @@ -40,7 +40,7 @@ TEST(lib_id_remapper, not_mappable) ID *idp = nullptr; IDRemapper *remapper = BKE_id_remapper_create(); - IDRemapperApplyResult result = BKE_id_remapper_apply(remapper, &idp); + IDRemapperApplyResult result = BKE_id_remapper_apply(remapper, &idp, ID_REMAP_APPLY_DEFAULT); EXPECT_EQ(result, ID_REMAP_SOURCE_NOT_MAPPABLE); BKE_id_remapper_free(remapper); @@ -54,22 +54,22 @@ TEST(lib_id_remapper, mapped) IDRemapper *remapper = BKE_id_remapper_create(); BKE_id_remapper_add(remapper, &id1, &id2); - IDRemapperApplyResult result = BKE_id_remapper_apply(remapper, &idp); + IDRemapperApplyResult result = BKE_id_remapper_apply(remapper, &idp, ID_REMAP_APPLY_DEFAULT); EXPECT_EQ(result, ID_REMAP_SOURCE_REMAPPED); EXPECT_EQ(idp, &id2); BKE_id_remapper_free(remapper); } -TEST(lib_id_remapper, mapped_to_nullptr) +TEST(lib_id_remapper, unassigned) { ID id1; ID *idp = &id1; IDRemapper *remapper = BKE_id_remapper_create(); BKE_id_remapper_add(remapper, &id1, nullptr); - IDRemapperApplyResult result = BKE_id_remapper_apply(remapper, &idp); - EXPECT_EQ(result, ID_REMAP_SOURCE_REMAPPED); + IDRemapperApplyResult result = BKE_id_remapper_apply(remapper, &idp, ID_REMAP_APPLY_DEFAULT); + EXPECT_EQ(result, ID_REMAP_SOURCE_UNASSIGNED); EXPECT_EQ(idp, nullptr); BKE_id_remapper_free(remapper); diff --git a/source/blender/editors/space_action/space_action.c b/source/blender/editors/space_action/space_action.c index 4463856f40a..96bf25c3ba3 100644 --- a/source/blender/editors/space_action/space_action.c +++ b/source/blender/editors/space_action/space_action.c @@ -36,6 +36,7 @@ #include "BLI_utildefines.h" #include "BKE_context.h" +#include "BKE_lib_remap.h" #include "BKE_nla.h" #include "BKE_screen.h" @@ -814,20 +815,15 @@ static void action_refresh(const bContext *C, ScrArea *area) /* XXX re-sizing y-extents of tot should go here? */ } -static void action_id_remap(ScrArea *UNUSED(area), SpaceLink *slink, ID *old_id, ID *new_id) +static void action_id_remap(ScrArea *UNUSED(area), + SpaceLink *slink, + const struct IDRemapper *mappings) { SpaceAction *sact = (SpaceAction *)slink; - if ((ID *)sact->action == old_id) { - sact->action = (bAction *)new_id; - } - - if ((ID *)sact->ads.filter_grp == old_id) { - sact->ads.filter_grp = (Collection *)new_id; - } - if ((ID *)sact->ads.source == old_id) { - sact->ads.source = new_id; - } + BKE_id_remapper_apply(mappings, &sact->action, ID_REMAP_APPLY_DEFAULT); + BKE_id_remapper_apply(mappings, &sact->ads.filter_grp, ID_REMAP_APPLY_DEFAULT); + BKE_id_remapper_apply(mappings, &sact->ads.source, ID_REMAP_APPLY_DEFAULT); } /** diff --git a/source/blender/editors/space_buttons/space_buttons.c b/source/blender/editors/space_buttons/space_buttons.c index 007a9105c76..cf9fb7f1b27 100644 --- a/source/blender/editors/space_buttons/space_buttons.c +++ b/source/blender/editors/space_buttons/space_buttons.c @@ -32,6 +32,7 @@ #include "BKE_context.h" #include "BKE_gpencil_modifier.h" /* Types for registering panels. */ +#include "BKE_lib_remap.h" #include "BKE_modifier.h" #include "BKE_screen.h" #include "BKE_shader_fx.h" @@ -860,54 +861,54 @@ static void buttons_area_listener(const wmSpaceTypeListenerParams *params) } } -static void buttons_id_remap(ScrArea *UNUSED(area), SpaceLink *slink, ID *old_id, ID *new_id) +static void buttons_id_remap(ScrArea *UNUSED(area), + SpaceLink *slink, + const struct IDRemapper *mappings) { SpaceProperties *sbuts = (SpaceProperties *)slink; - if (sbuts->pinid == old_id) { - sbuts->pinid = new_id; - if (new_id == NULL) { - sbuts->flag &= ~SB_PIN_CONTEXT; - } + if (BKE_id_remapper_apply(mappings, &sbuts->pinid, ID_REMAP_APPLY_DEFAULT) == + ID_REMAP_SOURCE_UNASSIGNED) { + sbuts->flag &= ~SB_PIN_CONTEXT; } if (sbuts->path) { ButsContextPath *path = sbuts->path; - int i; - for (i = 0; i < path->len; i++) { - if (path->ptr[i].owner_id == old_id) { - break; - } - } + for (int i = 0; i < path->len; i++) { + switch (BKE_id_remapper_apply(mappings, &path->ptr[i].owner_id, ID_REMAP_APPLY_DEFAULT)) { + case ID_REMAP_SOURCE_UNASSIGNED: { + if (i == 0) { + MEM_SAFE_FREE(sbuts->path); + } + else { + memset(&path->ptr[i], 0, s @@ Diff output truncated at 10240 characters. @@ _______________________________________________ Bf-blender-cvs mailing list Bf-blender-cvs@blender.org List details, subscription details or unsubscribe: https://lists.blender.org/mailman/listinfo/bf-blender-cvs