ExtAssetThumbnail.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // Copyright 2017-2021 marynate. All Rights Reserved.
  2. #pragma once
  3. #include "ExtAssetData.h"
  4. #include "CoreMinimal.h"
  5. #include "AssetThumbnail.h"
  6. #include "Stats/Stats.h"
  7. #include "Misc/Attribute.h"
  8. #include "Rendering/RenderingCommon.h"
  9. #include "Widgets/SWidget.h"
  10. #include "TickableEditorObject.h"
  11. class FExtAssetThumbnailPool;
  12. class AActor;
  13. class FSlateShaderResource;
  14. class FSlateTexture2DRHIRef;
  15. class FSlateTextureRenderTarget2DResource;
  16. struct FPropertyChangedEvent;
  17. /** A struct containing details about how the asset thumbnail should behave */
  18. struct FExtAssetThumbnailConfig
  19. {
  20. FExtAssetThumbnailConfig()
  21. : bAllowFadeIn( false )
  22. , bForceGenericThumbnail( false )
  23. , bAllowHintText( true )
  24. , bAllowAssetSpecificThumbnailOverlay( false )
  25. , ClassThumbnailBrushOverride( NAME_None )
  26. , ThumbnailLabel( EThumbnailLabel::ClassName )
  27. , HighlightedText( FText::GetEmpty() )
  28. , HintColorAndOpacity( FLinearColor( 0.0f, 0.0f, 0.0f, 0.0f ) )
  29. , AssetTypeColorOverride()
  30. , Padding(0)
  31. {
  32. }
  33. bool bAllowFadeIn;
  34. bool bForceGenericThumbnail;
  35. bool bAllowHintText;
  36. bool bAllowAssetSpecificThumbnailOverlay;
  37. FName ClassThumbnailBrushOverride;
  38. EThumbnailLabel::Type ThumbnailLabel;
  39. TAttribute< FText > HighlightedText;
  40. TAttribute< FLinearColor > HintColorAndOpacity;
  41. TOptional< FLinearColor > AssetTypeColorOverride;
  42. FMargin Padding;
  43. TAttribute<int32> GenericThumbnailSize = 64;
  44. };
  45. /**
  46. * Interface for rendering a thumbnail in a slate viewport
  47. */
  48. class FExtAssetThumbnail
  49. : public ISlateViewport
  50. , public TSharedFromThis<FExtAssetThumbnail>
  51. {
  52. public:
  53. /**
  54. * @param InAsset The asset to display a thumbnail for
  55. * @param InWidth The width that the thumbnail should be
  56. * @param InHeight The height that the thumbnail should be
  57. * @param InThumbnailPool The thumbnail pool to request textures from
  58. */
  59. FExtAssetThumbnail( UObject* InAsset, uint32 InWidth, uint32 InHeight, const TSharedPtr<class FExtAssetThumbnailPool>& InThumbnailPool );
  60. FExtAssetThumbnail( const FExtAssetData& InAsset, uint32 InWidth, uint32 InHeight, const TSharedPtr<class FExtAssetThumbnailPool>& InThumbnailPool );
  61. ~FExtAssetThumbnail();
  62. /**
  63. * @return The size of the viewport (thumbnail size)
  64. */
  65. virtual FIntPoint GetSize() const override;
  66. /**
  67. * @return The texture used to display the viewports content
  68. */
  69. virtual FSlateShaderResource* GetViewportRenderTargetTexture() const override;
  70. /**
  71. * Returns true if the viewport should be vsynced.
  72. */
  73. virtual bool RequiresVsync() const override { return false; }
  74. /**
  75. * @return The object we are rendering the thumbnail for
  76. */
  77. UObject* GetAsset() const;
  78. /**
  79. * @return The asset data for the object we are rendering the thumbnail for
  80. */
  81. const FExtAssetData& GetAssetData() const;
  82. /**
  83. * Sets the asset to render the thumnail for
  84. *
  85. * @param InAsset The new asset
  86. */
  87. void SetAsset( const UObject* InAsset );
  88. /**
  89. * Sets the asset to render the thumnail for
  90. *
  91. * @param InAssetData Asset data containin the the new asset to render
  92. */
  93. void SetAsset( const FExtAssetData& InAssetData );
  94. /**
  95. * @return A slate widget representing this thumbnail
  96. */
  97. TSharedRef<SWidget> MakeThumbnailWidget( const FExtAssetThumbnailConfig& InConfig = FExtAssetThumbnailConfig() );
  98. /** Re-renders this thumbnail */
  99. void RefreshThumbnail();
  100. DECLARE_EVENT(FExtAssetThumbnail, FOnAssetDataChanged);
  101. FOnAssetDataChanged& OnAssetDataChanged() { return AssetDataChangedEvent; }
  102. TSharedPtr<SWidget> GetSpecificThumbnailOverlay() const;
  103. const FSlateBrush* GetValidationIconBrush() const;
  104. protected:
  105. EVisibility GetContentTypeOverlayVisibility() const;
  106. private:
  107. /** Thumbnail pool for rendering the thumbnail */
  108. TWeakPtr<FExtAssetThumbnailPool> ThumbnailPool;
  109. /** Triggered when the asset data changes */
  110. FOnAssetDataChanged AssetDataChangedEvent;
  111. /** The asset data for the object we are rendering the thumbnail for */
  112. FExtAssetData AssetData;
  113. /** Width of the thumbnail */
  114. uint32 Width;
  115. /** Height of the thumbnail */
  116. uint32 Height;
  117. };
  118. struct FExtObjectThumbnailPool
  119. {
  120. /** Searches for an object's thumbnail in memory and returns it if found */
  121. const FObjectThumbnail* FindCachedThumbnail(const FString& InFullName);
  122. FObjectThumbnail& AddToPool(const FString& InFullName);
  123. void Reserve(int32 InSize);
  124. void Resize(int32 InSize);
  125. void Free();
  126. uint32 GetAllocatedSize() const;
  127. TMap<FString, FObjectThumbnail> Pool;
  128. /** Max number of thumbnails in the pool */
  129. int32 NumInPool = 1024;
  130. };
  131. /**
  132. * Utility class for keeping track of, rendering, and recycling thumbnails rendered in Slate
  133. */
  134. class FExtAssetThumbnailPool : public FTickableEditorObject
  135. {
  136. public:
  137. /**
  138. * Constructor
  139. *
  140. * @param InNumInPool The number of thumbnails allowed in the pool
  141. * @param InAreRealTimeThumbnailsAllowed Attribute that determines if thumbnails should be rendered in real-time
  142. * @param InMaxFrameTimeAllowance The maximum number of seconds per tick to spend rendering thumbnails
  143. * @param InMaxRealTimeThumbnailsPerFrame The maximum number of real-time thumbnails to render per tick
  144. */
  145. FExtAssetThumbnailPool( uint32 InNumInPool, const TAttribute<bool>& InAreRealTimeThumbnailsAllowed = true, double InMaxFrameTimeAllowance = 0.005, uint32 InMaxRealTimeThumbnailsPerFrame = 3 );
  146. /** Destructor to free all remaining resources */
  147. ~FExtAssetThumbnailPool();
  148. //~ Begin FTickableObject Interface
  149. virtual TStatId GetStatId() const override;
  150. /** Checks if any new thumbnails are queued */
  151. virtual bool IsTickable() const override;
  152. /** Ticks the pool, rendering new thumbnails as needed */
  153. virtual void Tick( float DeltaTime ) override;
  154. //~ End FTickableObject Interface
  155. /**
  156. * Accesses the texture for an object. If a thumbnail was recently rendered this function simply returns the thumbnail. If it was not, it requests a new one be generated
  157. * No assumptions should be made about whether or not it was rendered
  158. *
  159. * @param Asset The asset to get the thumbnail for
  160. * @param Width The width of the thumbnail
  161. * @param Height The height of the thumbnail
  162. * @return The thumbnail for the asset or NULL if one could not be produced
  163. */
  164. FSlateTexture2DRHIRef* AccessTexture( const FExtAssetData& AssetData, uint32 Width, uint32 Height );
  165. /**
  166. * Adds a referencer to keep textures around as long as they are needed
  167. */
  168. void AddReferencer( const FExtAssetThumbnail& AssetThumbnail );
  169. /**
  170. * Removes a referencer to clean up textures that are no longer needed
  171. */
  172. void RemoveReferencer( const FExtAssetThumbnail& AssetThumbnail );
  173. /** Returns true if the thumbnail for the specified asset in the specified size is in the stack of thumbnails to render */
  174. bool IsInRenderStack( const TSharedPtr<FExtAssetThumbnail>& Thumbnail ) const;
  175. /** Returns true if the thumbnail for the specified asset in the specified size has been rendered */
  176. bool IsRendered( const TSharedPtr<FExtAssetThumbnail>& Thumbnail ) const;
  177. /** Brings all items in ThumbnailsToPrioritize to the front of the render stack if they are actually in the stack */
  178. void PrioritizeThumbnails( const TArray< TSharedPtr<FExtAssetThumbnail> >& ThumbnailsToPrioritize, uint32 Width, uint32 Height );
  179. /** Register/Unregister a callback for when thumbnails are rendered */
  180. DECLARE_EVENT_OneParam( FExtAssetThumbnailPool, FThumbnailRendered, const FExtAssetData& );
  181. FThumbnailRendered& OnThumbnailRendered() { return ThumbnailRenderedEvent; }
  182. /** Register/Unregister a callback for when thumbnails fail to render */
  183. DECLARE_EVENT_OneParam( FExtAssetThumbnailPool, FThumbnailRenderFailed, const FExtAssetData& );
  184. FThumbnailRenderFailed& OnThumbnailRenderFailed() { return ThumbnailRenderFailedEvent; }
  185. /** Re-renders the specified thumbnail */
  186. void RefreshThumbnail( const TSharedPtr<FExtAssetThumbnail>& ThumbnailToRefresh );
  187. private:
  188. /**
  189. * Releases all rendering resources held by the pool
  190. */
  191. void ReleaseResources();
  192. /**
  193. * Frees the rendering resources and clears a slot in the pool for an asset thumbnail at the specified width and height
  194. *
  195. * @param ObjectPath The path to the asset whose thumbnail should be free
  196. * @param Width The width of the thumbnail to free
  197. * @param Height The height of the thumbnail to free
  198. */
  199. void FreeThumbnail( const FName& ObjectPath, uint32 Width, uint32 Height );
  200. /** Adds the thumbnails associated with the object found at ObjectPath to the render stack */
  201. void RefreshThumbnailsFor( FName ObjectPath );
  202. /** Handler for when an asset is loaded */
  203. void OnAssetLoaded( UObject* Asset );
  204. /** Handler for when an actor is moved in a level. Used to update world asset thumbnails. */
  205. void OnActorPostEditMove( AActor* Actor );
  206. /** Handler for when an asset is loaded */
  207. void OnObjectPropertyChanged( UObject* Asset, FPropertyChangedEvent& PropertyChangedEvent );
  208. /** Handler to dirty cached thumbnails in packages to make sure they are re-rendered later */
  209. void DirtyThumbnailForObject( UObject* ObjectBeingModified );
  210. private:
  211. /** Information about a thumbnail */
  212. struct FThumbnailInfo
  213. {
  214. /** The object whose thumbnail is rendered */
  215. FExtAssetData AssetData;
  216. /** Rendering resource for slate */
  217. FSlateTexture2DRHIRef* ThumbnailTexture;
  218. /** Render target for slate */
  219. FSlateTextureRenderTarget2DResource* ThumbnailRenderTarget;
  220. /** The time since last access */
  221. float LastAccessTime;
  222. /** The time since last update */
  223. float LastUpdateTime;
  224. /** Width of the thumbnail */
  225. uint32 Width;
  226. /** Height of the thumbnail */
  227. uint32 Height;
  228. ~FThumbnailInfo();
  229. };
  230. struct FThumbnailInfo_RenderThread
  231. {
  232. /** Rendering resource for slate */
  233. FSlateTexture2DRHIRef* ThumbnailTexture;
  234. /** Render target for slate */
  235. FSlateTextureRenderTarget2DResource* ThumbnailRenderTarget;
  236. /** Width of the thumbnail */
  237. uint32 Width;
  238. /** Height of the thumbnail */
  239. uint32 Height;
  240. FThumbnailInfo_RenderThread(const FThumbnailInfo& Info)
  241. : ThumbnailTexture(Info.ThumbnailTexture)
  242. , ThumbnailRenderTarget(Info.ThumbnailRenderTarget)
  243. , Width(Info.Width)
  244. , Height(Info.Height)
  245. {}
  246. };
  247. /** Key for looking up thumbnails in a map */
  248. struct FThumbId
  249. {
  250. FName ObjectPath;
  251. uint32 Width;
  252. uint32 Height;
  253. FThumbId( const FName& InObjectPath, uint32 InWidth, uint32 InHeight )
  254. : ObjectPath( InObjectPath )
  255. , Width( InWidth )
  256. , Height( InHeight )
  257. {}
  258. bool operator==( const FThumbId& Other ) const
  259. {
  260. return ObjectPath == Other.ObjectPath && Width == Other.Width && Height == Other.Height;
  261. }
  262. friend uint32 GetTypeHash( const FThumbId& Id )
  263. {
  264. return GetTypeHash( Id.ObjectPath ) ^ GetTypeHash( Id.Width ) ^ GetTypeHash( Id.Height );
  265. }
  266. };
  267. /** The delegate to execute when a thumbnail is rendered */
  268. FThumbnailRendered ThumbnailRenderedEvent;
  269. /** The delegate to execute when a thumbnail failed to render */
  270. FThumbnailRenderFailed ThumbnailRenderFailedEvent;
  271. /** A mapping of objects to their thumbnails */
  272. TMap< FThumbId, TSharedRef<FThumbnailInfo> > ThumbnailToTextureMap;
  273. /** List of thumbnails to render when possible */
  274. TArray< TSharedRef<FThumbnailInfo> > ThumbnailsToRenderStack;
  275. /** List of thumbnails that can be rendered in real-time */
  276. TArray< TSharedRef<FThumbnailInfo> > RealTimeThumbnails;
  277. /** List of real-time thumbnails that are queued to be rendered */
  278. TArray< TSharedRef<FThumbnailInfo> > RealTimeThumbnailsToRender;
  279. /** List of free thumbnails that can be reused */
  280. TArray< TSharedRef<FThumbnailInfo> > FreeThumbnails;
  281. /** A mapping of objects to the number of referencers */
  282. TMap< FThumbId, int32 > RefCountMap;
  283. /** A list of object paths for recently loaded assets whose thumbnails need to be refreshed. */
  284. TArray<FName> RecentlyLoadedAssets;
  285. /** Attribute that determines if real-time thumbnails are allowed. Called every frame. */
  286. TAttribute<bool> AreRealTimeThumbnailsAllowed;
  287. /** Max number of thumbnails in the pool */
  288. uint32 NumInPool;
  289. /** Shaders are still building */
  290. bool bWereShadersCompilingLastFrame = false;
  291. /** Max number of dynamic thumbnails to update per frame */
  292. uint32 MaxRealTimeThumbnailsPerFrame;
  293. /** Max number of seconds per tick to spend rendering thumbnails */
  294. double MaxFrameTimeAllowance;
  295. };