[Bf-blender-cvs] [bb308d97625] greasepencil-object: Ensure update-on-write only in active depsgraph

Falk David noreply at git.blender.org
Mon Feb 7 18:35:16 CET 2022


Commit: bb308d97625b2b26e8222d001df3fe5cfecbad96
Author: Falk David
Date:   Fri Feb 4 18:11:14 2022 +0100
Branches: greasepencil-object
https://developer.blender.org/rBbb308d97625b2b26e8222d001df3fe5cfecbad96

Ensure update-on-write only in active depsgraph

Co-authored-by: @yann-lty

===================================================================

M	source/blender/blenkernel/BKE_gpencil.h
M	source/blender/blenkernel/intern/gpencil.c
M	source/blender/depsgraph/CMakeLists.txt
M	source/blender/depsgraph/intern/eval/deg_eval_copy_on_write.cc
M	source/blender/depsgraph/intern/eval/deg_eval_runtime_backup.cc
M	source/blender/depsgraph/intern/eval/deg_eval_runtime_backup.h
A	source/blender/depsgraph/intern/eval/deg_eval_runtime_backup_gpencil.cc
A	source/blender/depsgraph/intern/eval/deg_eval_runtime_backup_gpencil.h

===================================================================

diff --git a/source/blender/blenkernel/BKE_gpencil.h b/source/blender/blenkernel/BKE_gpencil.h
index 4aec1e684af..f70778d2559 100644
--- a/source/blender/blenkernel/BKE_gpencil.h
+++ b/source/blender/blenkernel/BKE_gpencil.h
@@ -741,7 +741,7 @@ int BKE_gpencil_material_find_index_by_name_prefix(struct Object *ob, const char
 
 void BKE_gpencil_blend_read_data(struct BlendDataReader *reader, struct bGPdata *gpd);
 
-bool BKE_gpencil_check_copy_on_write_needed(struct bGPdata *gpd);
+bool BKE_gpencil_update_on_write_check(const struct Depsgraph *depsgraph, struct bGPdata *gpd);
 
 void BKE_gpencil_update_on_write(struct bGPdata *gpd_orig, struct bGPdata *gpd_eval);
 
diff --git a/source/blender/blenkernel/intern/gpencil.c b/source/blender/blenkernel/intern/gpencil.c
index b9ebd19a617..e807aef984d 100644
--- a/source/blender/blenkernel/intern/gpencil.c
+++ b/source/blender/blenkernel/intern/gpencil.c
@@ -2837,13 +2837,20 @@ void BKE_gpencil_frame_selected_hash(bGPdata *gpd, struct GHash *r_list)
   }
 }
 
-bool BKE_gpencil_check_copy_on_write_needed(bGPdata *gpd)
+bool BKE_gpencil_update_on_write_check(const Depsgraph *depsgraph, bGPdata *gpd)
 {
-  GPencilUpdateCache *update_cache = gpd->runtime.update_cache;
   if (!U.experimental.use_gpencil_update_cache) {
-    return true;
+    return false;
+  }
+
+  /* For now, we only use the update cache in the active depsgraph. Othwerwise we might access the
+   * cache while another depsgraph frees it. */
+  if (!DEG_is_active(depsgraph)) {
+    return false;
   }
-  return update_cache == NULL || update_cache->flag == GP_UPDATE_NODE_FULL_COPY;
+
+  GPencilUpdateCache *update_cache = gpd->runtime.update_cache;
+  return update_cache != NULL && update_cache->flag != GP_UPDATE_NODE_FULL_COPY;
 }
 
 typedef struct tGPencilUpdateOnWriteTraverseData {
diff --git a/source/blender/depsgraph/CMakeLists.txt b/source/blender/depsgraph/CMakeLists.txt
index 41253117096..91c7fac68de 100644
--- a/source/blender/depsgraph/CMakeLists.txt
+++ b/source/blender/depsgraph/CMakeLists.txt
@@ -70,6 +70,7 @@ set(SRC
   intern/eval/deg_eval_flush.cc
   intern/eval/deg_eval_runtime_backup.cc
   intern/eval/deg_eval_runtime_backup_animation.cc
+  intern/eval/deg_eval_runtime_backup_gpencil.cc
   intern/eval/deg_eval_runtime_backup_modifier.cc
   intern/eval/deg_eval_runtime_backup_movieclip.cc
   intern/eval/deg_eval_runtime_backup_object.cc
@@ -131,6 +132,7 @@ set(SRC
   intern/eval/deg_eval_flush.h
   intern/eval/deg_eval_runtime_backup.h
   intern/eval/deg_eval_runtime_backup_animation.h
+  intern/eval/deg_eval_runtime_backup_gpencil.h
   intern/eval/deg_eval_runtime_backup_modifier.h
   intern/eval/deg_eval_runtime_backup_movieclip.h
   intern/eval/deg_eval_runtime_backup_object.h
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 69e843311ea..d6f6e32e798 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
@@ -890,35 +890,19 @@ ID *deg_update_copy_on_write_datablock(const Depsgraph *depsgraph, const IDNode
       update_edit_mode_pointers(depsgraph, id_orig, id_cow);
       return id_cow;
     }
+    /* In case we don't need to do a copy-on-write, we can use the update cache of the grease
+     * pencil data to do an update-on-write.*/
+    else if (id_type == ID_GD && BKE_gpencil_update_on_write_check((const ::Depsgraph *)depsgraph,
+                                                                   (bGPdata *)id_orig)) {
+      BKE_gpencil_update_on_write((bGPdata *)id_orig, (bGPdata *)id_cow);
+      return id_cow;
+    }
   }
 
   RuntimeBackup backup(depsgraph);
   backup.init_from_id(id_cow);
-
-  const ID_Type id_type = GS(id_orig->name);
-  switch (id_type) {
-    /* For grease pencil, we can avoid a full copy of the data-block and only do an update-on-write. */
-    case ID_GD: {
-      if (check_datablock_expanded(id_cow) &&
-          !BKE_gpencil_check_copy_on_write_needed((bGPdata *)id_orig)) {
-        BKE_gpencil_update_on_write((bGPdata *)id_orig, (bGPdata *)id_cow);
-      }
-      else {
-        /* Free gpd update cache and set it to null so that it's not copied to the eval data. */
-        BKE_gpencil_free_update_cache((bGPdata *)id_orig);
-        deg_free_copy_on_write_datablock(id_cow);
-        deg_expand_copy_on_write_datablock(depsgraph, id_node);
-        BKE_gpencil_data_update_orig_pointers((bGPdata *)id_orig, (bGPdata *)id_cow);
-      }
-      break;
-    }
-    default: {
-      deg_free_copy_on_write_datablock(id_cow);
-      deg_expand_copy_on_write_datablock(depsgraph, id_node);
-      break;
-    }
-  }
-
+  deg_free_copy_on_write_datablock(id_cow);
+  deg_expand_copy_on_write_datablock(depsgraph, id_node);
   backup.restore_to_id(id_cow);
   return id_cow;
 }
diff --git a/source/blender/depsgraph/intern/eval/deg_eval_runtime_backup.cc b/source/blender/depsgraph/intern/eval/deg_eval_runtime_backup.cc
index 8bf64af7d5d..85ac8b509c2 100644
--- a/source/blender/depsgraph/intern/eval/deg_eval_runtime_backup.cc
+++ b/source/blender/depsgraph/intern/eval/deg_eval_runtime_backup.cc
@@ -40,7 +40,8 @@ RuntimeBackup::RuntimeBackup(const Depsgraph *depsgraph)
       object_backup(depsgraph),
       drawdata_ptr(nullptr),
       movieclip_backup(depsgraph),
-      volume_backup(depsgraph)
+      volume_backup(depsgraph),
+      gpencil_backup(depsgraph)
 {
   drawdata_backup.first = drawdata_backup.last = nullptr;
 }
@@ -75,6 +76,8 @@ void RuntimeBackup::init_from_id(ID *id)
     case ID_VO:
       volume_backup.init_from_volume(reinterpret_cast<Volume *>(id));
       break;
+    case ID_GD:
+      gpencil_backup.init_from_gpencil(reinterpret_cast<bGPdata *>(id));
     default:
       break;
   }
@@ -115,6 +118,8 @@ void RuntimeBackup::restore_to_id(ID *id)
     case ID_VO:
       volume_backup.restore_to_volume(reinterpret_cast<Volume *>(id));
       break;
+    case ID_GD:
+      gpencil_backup.restore_to_gpencil(reinterpret_cast<bGPdata *>(id));
     default:
       break;
   }
diff --git a/source/blender/depsgraph/intern/eval/deg_eval_runtime_backup.h b/source/blender/depsgraph/intern/eval/deg_eval_runtime_backup.h
index 0629dbe62b4..c4f6bd954c0 100644
--- a/source/blender/depsgraph/intern/eval/deg_eval_runtime_backup.h
+++ b/source/blender/depsgraph/intern/eval/deg_eval_runtime_backup.h
@@ -31,6 +31,7 @@
 #include "intern/eval/deg_eval_runtime_backup_scene.h"
 #include "intern/eval/deg_eval_runtime_backup_sound.h"
 #include "intern/eval/deg_eval_runtime_backup_volume.h"
+#include "intern/eval/deg_eval_runtime_backup_gpencil.h"
 
 namespace blender {
 namespace deg {
@@ -71,6 +72,7 @@ class RuntimeBackup {
   DrawDataList *drawdata_ptr;
   MovieClipBackup movieclip_backup;
   VolumeBackup volume_backup;
+  GPencilBackup gpencil_backup;
 };
 
 }  // namespace deg
diff --git a/source/blender/depsgraph/intern/eval/deg_eval_runtime_backup_gpencil.cc b/source/blender/depsgraph/intern/eval/deg_eval_runtime_backup_gpencil.cc
new file mode 100644
index 00000000000..22aa2f48eb9
--- /dev/null
+++ b/source/blender/depsgraph/intern/eval/deg_eval_runtime_backup_gpencil.cc
@@ -0,0 +1,52 @@
+/*
+ * 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) 2019 Blender Foundation.
+ * All rights reserved.
+ */
+
+/** \file
+ * \ingroup depsgraph
+ */
+
+#include "intern/eval/deg_eval_runtime_backup_gpencil.h"
+#include "intern/depsgraph.h"
+
+#include "BKE_gpencil.h"
+#include "BKE_gpencil_update_cache.h"
+
+#include "DNA_gpencil_types.h"
+
+namespace blender::deg {
+
+GPencilBackup::GPencilBackup(const Depsgraph *depsgraph) : depsgraph(depsgraph)
+{
+}
+
+void GPencilBackup::init_from_gpencil(bGPdata *gpd)
+{
+}
+
+void GPencilBackup::restore_to_gpencil(bGPdata *gpd)
+{
+  bGPdata *gpd_orig = reinterpret_cast<bGPdata *>(gpd->id.orig_id);
+  if (depsgraph->is_active) {
+    BKE_gpencil_free_update_cache(gpd_orig);
+    gpd->runtime.update_cache = NULL;
+  }
+  BKE_gpencil_data_update_orig_pointers(gpd_orig, gpd);
+}
+
+}  // namespace blender::deg
diff --git a/source/blender/depsgraph/intern/eval/deg_eval_runtime_backup_gpencil.h b/source/blender/depsgraph/intern/eval/deg_eval_runtime_backup_gpencil.h
new file mode 100644
index 00000000000..d09e0e2258b
--- /dev/null
+++ b/source/blender/depsgraph/intern/eval/deg_eval_runtime_backup_gpencil.h
@@ -0,0 +1,45 @@
+/*
+ * 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) 2019 Blender Foundation.
+ * All rights reserved.
+ */
+
+/** \file
+ * \ingroup depsgraph
+ */
+
+#pragma once
+
+struct bGPdata;
+
+namespace blender {
+namespace deg {
+
+struct Depsgraph;
+
+/* Backup of volume datablocks runtime data. */
+class GPencilBackup {
+ public:
+  GPencilBackup(const Depsgraph *depsg

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list