[Bf-blender-cvs] [39eab45c8e4] asset-browser-grid-view: Let UI do lazy-loading of previews, rather than file-list cache

Julian Eisel noreply at git.blender.org
Wed Feb 16 18:13:18 CET 2022


Commit: 39eab45c8e4481ce6b6a7f31f5085ac85e2277eb
Author: Julian Eisel
Date:   Wed Feb 16 17:59:27 2022 +0100
Branches: asset-browser-grid-view
https://developer.blender.org/rB39eab45c8e4481ce6b6a7f31f5085ac85e2277eb

Let UI do lazy-loading of previews, rather than file-list cache

This is an important step to decouple the asset-views from the file browser
backend. One downside is that the UI preview loading is much slower than the
file browser one, however, I'm pretty sure I know how to address this.

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

M	source/blender/editors/asset/ED_asset_handle.h
M	source/blender/editors/asset/ED_asset_list.h
M	source/blender/editors/asset/intern/asset_handle.cc
M	source/blender/editors/asset/intern/asset_list.cc
M	source/blender/editors/interface/grid_view.cc
M	source/blender/editors/interface/interface_template_asset_view.cc
M	source/blender/editors/space_assets/asset_view.cc
M	source/blender/editors/space_assets/asset_view.hh
M	source/blender/editors/space_file/filelist.c
M	source/blender/editors/space_file/filelist.h
M	source/blender/makesdna/DNA_asset_types.h

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

diff --git a/source/blender/editors/asset/ED_asset_handle.h b/source/blender/editors/asset/ED_asset_handle.h
index 64a95de480f..ff39061f231 100644
--- a/source/blender/editors/asset/ED_asset_handle.h
+++ b/source/blender/editors/asset/ED_asset_handle.h
@@ -27,7 +27,6 @@ const char *ED_asset_handle_get_identifier(const struct AssetHandle *asset);
 struct AssetMetaData *ED_asset_handle_get_metadata(const struct AssetHandle *asset);
 struct ID *ED_asset_handle_get_local_id(const struct AssetHandle *asset);
 ID_Type ED_asset_handle_get_id_type(const struct AssetHandle *asset);
-int ED_asset_handle_get_preview_icon_id(const struct AssetHandle *asset);
 void ED_asset_handle_get_full_library_path(const struct bContext *C,
                                            const struct AssetLibraryReference *asset_library_ref,
                                            const struct AssetHandle *asset,
diff --git a/source/blender/editors/asset/ED_asset_list.h b/source/blender/editors/asset/ED_asset_list.h
index c133ff93782..ddf1b07033e 100644
--- a/source/blender/editors/asset/ED_asset_list.h
+++ b/source/blender/editors/asset/ED_asset_list.h
@@ -51,6 +51,10 @@ void ED_assetlist_storage_id_remap(struct ID *id_old, struct ID *id_new);
  */
 void ED_assetlist_storage_exit(void);
 
+struct PreviewImage *ED_assetlist_asset_preview_request(
+    const struct AssetLibraryReference *library_reference, AssetHandle *asset_handle);
+int ED_assetlist_asset_preview_icon_id_request(const AssetLibraryReference *library_reference,
+                                               AssetHandle *asset_handle);
 struct ImBuf *ED_assetlist_asset_image_get(const AssetHandle *asset_handle);
 const char *ED_assetlist_library_path(const struct AssetLibraryReference *library_reference);
 
diff --git a/source/blender/editors/asset/intern/asset_handle.cc b/source/blender/editors/asset/intern/asset_handle.cc
index 02c0eab0adc..6ff0dfe394b 100644
--- a/source/blender/editors/asset/intern/asset_handle.cc
+++ b/source/blender/editors/asset/intern/asset_handle.cc
@@ -38,11 +38,6 @@ ID_Type ED_asset_handle_get_id_type(const AssetHandle *asset)
   return static_cast<ID_Type>(asset->file_data->blentype);
 }
 
-int ED_asset_handle_get_preview_icon_id(const AssetHandle *asset)
-{
-  return asset->file_data->preview_icon_id;
-}
-
 void ED_asset_handle_get_full_library_path(const bContext *C,
                                            const AssetLibraryReference *asset_library_ref,
                                            const AssetHandle *asset,
diff --git a/source/blender/editors/asset/intern/asset_list.cc b/source/blender/editors/asset/intern/asset_list.cc
index 8fe81445568..785c238bfae 100644
--- a/source/blender/editors/asset/intern/asset_list.cc
+++ b/source/blender/editors/asset/intern/asset_list.cc
@@ -20,6 +20,7 @@
 
 #include "DNA_space_types.h"
 
+#include "BKE_icons.h"
 #include "BKE_preferences.h"
 
 #include "ED_fileselect.h"
@@ -99,8 +100,10 @@ class PreviewTimer {
 
 class AssetList : NonCopyable {
   FileListWrapper filelist_;
-  /** Storage for asset handles, items are lazy-created on request. */
-  mutable Map<const FileDirEntry *, AssetHandle> asset_handle_map_;
+  /** Storage for asset handles, items are lazy-created on request.
+   *  Asset handles are stored as a pointer here, to ensure a consistent memory address (address
+   * inside the map changes as the map changes). */
+  mutable Map<const FileDirEntry *, std::unique_ptr<AssetHandle>> asset_handle_map_;
   AssetLibraryReference library_ref_;
   PreviewTimer previews_timer_;
 
@@ -207,7 +210,8 @@ bool AssetList::needsRefetch() const
 
 AssetHandle &AssetList::asset_handle_from_file(const FileDirEntry &file) const
 {
-  return asset_handle_map_.lookup_or_add(&file, AssetHandle{&file});
+  return *asset_handle_map_.lookup_or_add(&file,
+                                          std::make_unique<AssetHandle>(AssetHandle{&file}));
 }
 
 void AssetList::iterate(AssetListIterFn fn) const
@@ -529,6 +533,37 @@ std::string ED_assetlist_asset_filepath_get(const bContext *C,
   return path;
 }
 
+PreviewImage *ED_assetlist_asset_preview_request(const AssetLibraryReference *library_reference,
+                                                 AssetHandle *asset_handle)
+{
+  if (asset_handle->preview) {
+    return asset_handle->preview;
+  }
+
+  if (ID *local_id = ED_asset_handle_get_local_id(asset_handle)) {
+    asset_handle->preview = BKE_previewimg_id_get(local_id);
+  }
+  else {
+    const char *asset_identifier = ED_asset_handle_get_identifier(asset_handle);
+    const int source = filelist_preview_source_get(asset_handle->file_data->typeflag);
+    const std::string asset_path = ED_assetlist_asset_filepath_get(
+        nullptr, *library_reference, *asset_handle);
+
+    asset_handle->preview = BKE_previewimg_cached_thumbnail_read(
+        asset_identifier, asset_path.c_str(), source, false);
+  }
+
+  return asset_handle->preview;
+}
+
+int ED_assetlist_asset_preview_icon_id_request(const AssetLibraryReference *library_reference,
+                                               AssetHandle *asset_handle)
+{
+  PreviewImage *preview = ED_assetlist_asset_preview_request(library_reference, asset_handle);
+  ID *local_id = ED_asset_handle_get_local_id(asset_handle);
+  return BKE_icon_preview_ensure(local_id, preview);
+}
+
 ImBuf *ED_assetlist_asset_image_get(const AssetHandle *asset_handle)
 {
   ImBuf *imbuf = filelist_file_getimage(asset_handle->file_data);
diff --git a/source/blender/editors/interface/grid_view.cc b/source/blender/editors/interface/grid_view.cc
index ced78b93dd6..cbec0b80353 100644
--- a/source/blender/editors/interface/grid_view.cc
+++ b/source/blender/editors/interface/grid_view.cc
@@ -459,21 +459,21 @@ void PreviewGridItem::build_grid_tile(uiLayout &layout) const
 {
   const GridViewStyle &style = get_view().get_style();
   uiBlock *block = uiLayoutGetBlock(&layout);
-  uiBut *but = uiDefIconTextBut(block,
-                                UI_BTYPE_PREVIEW_TILE,
-                                0,
-                                preview_icon_id,
-                                label_.c_str(),
-                                0,
-                                0,
-                                style.tile_width,
-                                style.tile_height,
-                                nullptr,
-                                0,
-                                0,
-                                0,
-                                0,
-                                "");
+
+  uiBut *but = uiDefBut(block,
+                        UI_BTYPE_PREVIEW_TILE,
+                        0,
+                        label_.c_str(),
+                        0,
+                        0,
+                        style.tile_width,
+                        style.tile_height,
+                        nullptr,
+                        0,
+                        0,
+                        0,
+                        0,
+                        "");
   ui_def_but_icon(but,
                   preview_icon_id,
                   /* NOLINTNEXTLINE: bugprone-suspicious-enum-usage */
diff --git a/source/blender/editors/interface/interface_template_asset_view.cc b/source/blender/editors/interface/interface_template_asset_view.cc
index 45c1c9fbc08..b83dfcfa0db 100644
--- a/source/blender/editors/interface/interface_template_asset_view.cc
+++ b/source/blender/editors/interface/interface_template_asset_view.cc
@@ -53,14 +53,15 @@ static void asset_view_item_but_drag_set(uiBut *but,
 
   if (blend_path[0]) {
     ImBuf *imbuf = ED_assetlist_asset_image_get(asset_handle);
-    UI_but_drag_set_asset(but,
-                          asset_handle,
-                          BLI_strdup(blend_path),
-                          ED_asset_handle_get_metadata(asset_handle),
-                          FILE_ASSET_IMPORT_APPEND,
-                          ED_asset_handle_get_preview_icon_id(asset_handle),
-                          imbuf,
-                          1.0f);
+    UI_but_drag_set_asset(
+        but,
+        asset_handle,
+        BLI_strdup(blend_path),
+        ED_asset_handle_get_metadata(asset_handle),
+        FILE_ASSET_IMPORT_APPEND,
+        ED_assetlist_asset_preview_icon_id_request(&list_data->asset_library_ref, asset_handle),
+        imbuf,
+        1.0f);
   }
 }
 
@@ -86,25 +87,27 @@ static void asset_view_draw_item(uiList *ui_list,
   const bool show_names = list_data->show_names;
   const int size_x = UI_preview_tile_size_x();
   const int size_y = show_names ? UI_preview_tile_size_y() : UI_preview_tile_size_y_no_label();
-  uiBut *but = uiDefIconTextBut(block,
-                                UI_BTYPE_PREVIEW_TILE,
-                                0,
-                                ED_asset_handle_get_preview_icon_id(asset_handle),
-                                show_names ? ED_asset_handle_get_name(asset_handle) : "",
-                                0,
-                                0,
-                                size_x,
-                                size_y,
-                                nullptr,
-                                0,
-                                0,
-                                0,
-                                0,
-                                "");
-  ui_def_but_icon(but,
-                  ED_asset_handle_get_preview_icon_id(asset_handle),
-                  /* NOLINTNEXTLINE: bugprone-suspicious-enum-usage */
-                  UI_HAS_ICON | UI_BUT_ICON_PREVIEW);
+  uiBut *but = uiDefIconTextBut(
+      block,
+      UI_BTYPE_PREVIEW_TILE,
+      0,
+      ED_assetlist_asset_preview_icon_id_request(&list_data->asset_library_ref, asset_handle),
+      show_names ? ED_asset_handle_get_name(asset_handle) : "",
+      0,
+      0,
+      size_x,
+      size_y,
+      nullptr,
+      0,
+      0,
+      0,
+      0,
+      "");
+  ui_def_but_icon(
+      but,
+      ED_assetlist_asset_preview_icon_id_request(&list_data->asset_library_ref, asset_handle),
+      /* NOLINTNEXTLINE: bugprone-suspicious-enum-usage */
+      UI_HAS_ICON | UI_BUT_ICON_PREVIEW);
   if (!ui_list->dyn_data->custom_drag_optype) {
     asset_view_item_but_drag_set(but, list_data, ass

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list