This repository has been archived on 2023-10-09. You can view files and clone it, but cannot push or open issues or pull requests.
Files
blender-archive/source/blender/depsgraph/intern/node/deg_node_id.cc

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

217 lines
6.1 KiB
C++
Raw Normal View History

2017-12-20 16:35:48 +01:00
/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* The Original Code is Copyright (C) 2013 Blender Foundation.
* All rights reserved.
*/
/** \file
* \ingroup depsgraph
2017-12-20 16:35:48 +01:00
*/
#include "intern/node/deg_node_id.h"
2017-12-20 16:35:48 +01:00
#include <cstdio>
2017-12-20 16:35:48 +01:00
#include <cstring> /* required for STREQ later on. */
#include "BLI_string.h"
#include "BLI_utildefines.h"
2017-12-20 16:35:48 +01:00
#include "DNA_ID.h"
#include "DNA_anim_types.h"
#include "BKE_lib_id.h"
2017-12-20 16:35:48 +01:00
#include "DEG_depsgraph.h"
2017-12-20 16:40:49 +01:00
#include "intern/eval/deg_eval_copy_on_write.h"
#include "intern/node/deg_node_component.h"
#include "intern/node/deg_node_factory.h"
#include "intern/node/deg_node_time.h"
2017-12-20 16:35:48 +01:00
namespace blender::deg {
2017-12-20 16:35:48 +01:00
const char *linkedStateAsString(eDepsNode_LinkedState_Type linked_state)
{
switch (linked_state) {
case DEG_ID_LINKED_INDIRECTLY:
return "INDIRECTLY";
case DEG_ID_LINKED_VIA_SET:
return "VIA_SET";
case DEG_ID_LINKED_DIRECTLY:
return "DIRECTLY";
}
BLI_assert(!"Unhandled linked state, should never happen.");
return "UNKNOWN";
}
IDNode::ComponentIDKey::ComponentIDKey(NodeType type, const char *name) : type(type), name(name)
2017-12-20 16:35:48 +01:00
{
}
bool IDNode::ComponentIDKey::operator==(const ComponentIDKey &other) const
2017-12-20 16:35:48 +01:00
{
2017-12-20 16:40:49 +01:00
return type == other.type && STREQ(name, other.name);
2017-12-20 16:35:48 +01:00
}
uint64_t IDNode::ComponentIDKey::hash() const
{
const int type_as_int = static_cast<int>(type);
return BLI_ghashutil_combine_hash(BLI_ghashutil_uinthash(type_as_int),
BLI_ghashutil_strhash_p(name));
}
2017-12-20 16:35:48 +01:00
/* Initialize 'id' node - from pointer data given. */
void IDNode::init(const ID *id, const char *UNUSED(subdata))
2017-12-20 16:35:48 +01:00
{
BLI_assert(id != nullptr);
2017-12-20 16:40:49 +01:00
/* Store ID-pointer. */
id_type = GS(id->name);
2017-12-20 16:40:49 +01:00
id_orig = (ID *)id;
id_orig_session_uuid = id->session_uuid;
2017-12-20 16:40:49 +01:00
eval_flags = 0;
previous_eval_flags = 0;
customdata_masks = DEGCustomDataMeshMasks();
previous_customdata_masks = DEGCustomDataMeshMasks();
2017-12-20 16:40:49 +01:00
linked_state = DEG_ID_LINKED_INDIRECTLY;
is_directly_visible = true;
is_collection_fully_expanded = false;
has_base = false;
is_user_modified = false;
visible_components_mask = 0;
previously_visible_components_mask = 0;
2017-12-20 16:40:49 +01:00
}
2017-12-20 16:35:48 +01:00
void IDNode::init_copy_on_write(ID *id_cow_hint)
2017-12-20 16:40:49 +01:00
{
/* Create pointer as early as possible, so we can use it for function
* bindings. Rest of data we'll be copying to the new datablock when
* it is actually needed. */
if (id_cow_hint != nullptr) {
2017-12-20 16:40:49 +01:00
// BLI_assert(deg_copy_on_write_is_needed(id_orig));
if (deg_copy_on_write_is_needed(id_orig)) {
id_cow = id_cow_hint;
}
else {
id_cow = id_orig;
}
}
else if (deg_copy_on_write_is_needed(id_orig)) {
id_cow = (ID *)BKE_libblock_alloc_notest(GS(id_orig->name));
DEG_COW_PRINT(
"Create shallow copy for %s: id_orig=%p id_cow=%p\n", id_orig->name, id_orig, id_cow);
deg_tag_copy_on_write_id(id_cow, id_orig);
}
else {
id_cow = id_orig;
}
2017-12-20 16:35:48 +01:00
}
/* Free 'id' node. */
IDNode::~IDNode()
2017-12-20 16:35:48 +01:00
{
2017-12-20 16:40:49 +01:00
destroy();
}
void IDNode::destroy()
2017-12-20 16:40:49 +01:00
{
if (id_orig == nullptr) {
2017-12-20 16:40:49 +01:00
return;
}
for (ComponentNode *comp_node : components.values()) {
delete comp_node;
}
2017-12-20 16:40:49 +01:00
/* Free memory used by this CoW ID. */
2020-11-06 12:30:59 +11:00
if (!ELEM(id_cow, id_orig, nullptr)) {
2017-12-20 16:40:49 +01:00
deg_free_copy_on_write_datablock(id_cow);
MEM_freeN(id_cow);
id_cow = nullptr;
2017-12-20 16:40:49 +01:00
DEG_COW_PRINT("Destroy CoW for %s: id_orig=%p id_cow=%p\n", id_orig->name, id_orig, id_cow);
}
/* Tag that the node is freed. */
id_orig = nullptr;
2017-12-20 16:35:48 +01:00
}
string IDNode::identifier() const
{
char orig_ptr[24], cow_ptr[24];
BLI_snprintf(orig_ptr, sizeof(orig_ptr), "%p", id_orig);
BLI_snprintf(cow_ptr, sizeof(cow_ptr), "%p", id_cow);
return string(nodeTypeAsString(type)) + " : " + name + " (orig: " + orig_ptr +
", eval: " + cow_ptr + ", is_directly_visible " +
(is_directly_visible ? "true" : "false") + ")";
}
2019-02-15 16:00:54 +01:00
ComponentNode *IDNode::find_component(NodeType type, const char *name) const
2017-12-20 16:35:48 +01:00
{
ComponentIDKey key(type, name);
return components.lookup_default(key, nullptr);
2017-12-20 16:35:48 +01:00
}
2019-02-15 16:00:54 +01:00
ComponentNode *IDNode::add_component(NodeType type, const char *name)
2017-12-20 16:35:48 +01:00
{
ComponentNode *comp_node = find_component(type, name);
2017-12-20 16:35:48 +01:00
if (!comp_node) {
DepsNodeFactory *factory = type_get_factory(type);
comp_node = (ComponentNode *)factory->create_node(this->id_orig, "", name);
2017-12-20 16:35:48 +01:00
/* Register. */
ComponentIDKey key(type, name);
components.add_new(key, comp_node);
2017-12-20 16:35:48 +01:00
comp_node->owner = this;
}
return comp_node;
}
void IDNode::tag_update(Depsgraph *graph, eUpdateSource source)
2017-12-20 16:35:48 +01:00
{
for (ComponentNode *comp_node : components.values()) {
/* Relations update does explicit animation update when needed. Here we ignore animation
* component to avoid loss of possible unkeyed changes. */
if (comp_node->type == NodeType::ANIMATION && source == DEG_UPDATE_SOURCE_RELATIONS) {
continue;
}
comp_node->tag_update(graph, source);
2017-12-20 16:35:48 +01:00
}
}
void IDNode::finalize_build(Depsgraph *graph)
2017-12-20 16:35:48 +01:00
{
2017-12-20 16:40:49 +01:00
/* Finalize build of all components. */
for (ComponentNode *comp_node : components.values()) {
2017-12-20 16:40:49 +01:00
comp_node->finalize_build(graph);
2017-12-20 16:35:48 +01:00
}
visible_components_mask = get_visible_components_mask();
}
IDComponentsMask IDNode::get_visible_components_mask() const
{
IDComponentsMask result = 0;
for (ComponentNode *comp_node : components.values()) {
if (comp_node->affects_directly_visible) {
const int component_type_as_int = static_cast<int>(comp_node->type);
BLI_assert(component_type_as_int < 64);
result |= (1ULL << component_type_as_int);
}
}
return result;
2017-12-20 16:35:48 +01:00
}
} // namespace blender::deg