// Copyright Epic Games, Inc. All Rights Reserved. #include "ElectraSamplesTextureManager.h" #include "Misc/ScopeLock.h" FElectraSamplesTextureManager::FElectraSamplesTextureManager() { } FElectraSamplesTextureManager::~FElectraSamplesTextureManager() { } void FElectraSamplesTextureManager::CleanupMap() { TArray InvalidKeys; InvalidKeys.Reserve(KnownTextures.Num()); for (const TPair>& Item : KnownTextures) { if (!Item.Value.IsValid()) { InvalidKeys.Add(Item.Key); } } for (void* Key : InvalidKeys) { KnownTextures.Remove(Key); } } TSharedPtr FElectraSamplesTextureManager::CreateTexture(const FIntPoint& Dim, EPixelFormat Fmt) { // note: we create the texture outside the CS to avoid any danger of deadlocks TSharedPtr NewTexture = PlatformCreateTexture(Dim, Fmt); FScopeLock Lock(&AccessCS); CleanupMap(); if (NewTexture.IsValid()) { KnownTextures.Add(PlatformGetTexturePlatform(NewTexture.Get()), NewTexture); } return NewTexture; } void FElectraSamplesTextureManager::PlatformAddSharedTextureRef(TSharedPtr InTextureRef) { FScopeLock Lock(&AccessCS); if (InTextureRef.IsValid()) { // Only add once if it's not already there. KnownTextures.FindOrAdd(PlatformGetTexturePlatform(InTextureRef.Get()), InTextureRef); } } void FElectraSamplesTextureManager::PlatformRemoveSharedTextureRef(TSharedPtr InTextureRef) { FScopeLock Lock(&AccessCS); if (InTextureRef.IsValid()) { KnownTextures.Remove(PlatformGetTexturePlatform(InTextureRef.Get())); } } FTextureRHIRef FElectraSamplesTextureManager::GetRHITextureFromPlatformTexture(void* PlatformTexture) { FScopeLock Lock(&AccessCS); CleanupMap(); TWeakPtr *Texture = KnownTextures.Find(PlatformTexture); if (Texture) { if (TSharedPtr PinnedTexture = Texture->Pin()) { return PlatformGetTextureRHI(PinnedTexture.Get()); } } return FTextureRHIRef(); }