From 6dd72cdf32a252f94d337b0593caf91f987f4dd2 Mon Sep 17 00:00:00 2001 From: Wojtek Figat Date: Mon, 11 Dec 2023 14:14:55 +0100 Subject: [PATCH] Add removing thumbnails for deleted assets #1729 --- Source/Editor/Content/PreviewsCache.cpp | 19 ++++++++++++------ .../Content/Thumbnails/ThumbnailsModule.cs | 20 ++----------------- 2 files changed, 15 insertions(+), 24 deletions(-) diff --git a/Source/Editor/Content/PreviewsCache.cpp b/Source/Editor/Content/PreviewsCache.cpp index 2d62e8170..b0f3619a5 100644 --- a/Source/Editor/Content/PreviewsCache.cpp +++ b/Source/Editor/Content/PreviewsCache.cpp @@ -5,6 +5,7 @@ #include "Engine/Graphics/GPUContext.h" #include "Engine/Threading/Threading.h" #include "Engine/Graphics/RenderTools.h" +#include "Engine/Content/Content.h" #include "Engine/Content/Factories/BinaryAssetFactory.h" #include "Engine/ContentImporters/AssetsImportingManager.h" #include "Engine/Content/Upgraders/TextureAssetUpgrader.h" @@ -92,15 +93,12 @@ SpriteHandle PreviewsCache::FindSlot(const Guid& id) { if (WaitForLoaded()) return SpriteHandle::Invalid; - - // Find entry int32 index; if (_assets.Find(id, index)) { const String spriteName = StringUtils::ToString(index); return FindSprite(spriteName); } - return SpriteHandle::Invalid; } @@ -114,6 +112,17 @@ Asset::LoadResult PreviewsCache::load() return LoadResult::Failed; _assets.Set(previewsMetaChunk->Get(), ASSETS_ICONS_PER_ATLAS); + // Verify if cached assets still exist (don't store thumbnails for removed files) + AssetInfo assetInfo; + for (Guid& id : _assets) + { + if (id.IsValid() && Content::GetAsset(id) == nullptr && !Content::GetAssetInfo(id, assetInfo)) + { + // Free slot (no matter the texture contents) + id = Guid::Empty; + } + } + // Setup atlas sprites array Sprite sprite; sprite.Area.Size = static_cast(ASSET_ICON_SIZE) / ASSETS_ICONS_ATLAS_SIZE; @@ -162,7 +171,7 @@ SpriteHandle PreviewsCache::OccupySlot(GPUTexture* source, const Guid& id) if (WaitForLoaded()) return SpriteHandle::Invalid; - // Find free slot and for that asset + // Find this asset slot or use the first empty int32 index = _assets.Find(id); if (index == INVALID_INDEX) index = _assets.Find(Guid::Empty); @@ -201,14 +210,12 @@ bool PreviewsCache::ReleaseSlot(const Guid& id) { bool result = false; ScopeLock lock(Locker); - int32 index = _assets.Find(id); if (index != INVALID_INDEX) { _assets[index] = Guid::Empty; result = true; } - return result; } diff --git a/Source/Editor/Content/Thumbnails/ThumbnailsModule.cs b/Source/Editor/Content/Thumbnails/ThumbnailsModule.cs index 1282e4daa..2b830c933 100644 --- a/Source/Editor/Content/Thumbnails/ThumbnailsModule.cs +++ b/Source/Editor/Content/Thumbnails/ThumbnailsModule.cs @@ -21,15 +21,11 @@ namespace FlaxEditor.Content.Thumbnails /// public const float MinimumRequiredResourcesQuality = 0.8f; - // TODO: free atlas slots for deleted assets - private readonly List _cache = new List(4); private readonly string _cacheFolder; - - private DateTime _lastFlushTime; - private readonly List _requests = new List(128); private readonly PreviewRoot _guiRoot = new PreviewRoot(); + private DateTime _lastFlushTime; private RenderTask _task; private GPUTexture _output; @@ -88,7 +84,6 @@ namespace FlaxEditor.Content.Thumbnails } } - // Add request AddRequest(assetItem, proxy); } } @@ -118,9 +113,7 @@ namespace FlaxEditor.Content.Thumbnails for (int i = 0; i < _cache.Count; i++) { if (_cache[i].ReleaseSlot(assetItem.ID)) - { break; - } } } } @@ -198,13 +191,7 @@ namespace FlaxEditor.Content.Thumbnails /// void IContentItemOwner.OnItemDeleted(ContentItem item) { - if (item is AssetItem assetItem) - { - lock (_requests) - { - RemoveRequest(assetItem); - } - } + DeletePreview(item); } /// @@ -494,10 +481,7 @@ namespace FlaxEditor.Content.Thumbnails { // Wait some frames before start generating previews (late init feature) if (Time.TimeSinceStartup < 1.0f || HasAllAtlasesLoaded() == false) - { - // Back return; - } lock (_requests) {