Cogs.Core
Classes | Public Member Functions | Public Attributes | Protected Member Functions | Private Member Functions | Private Attributes | List of all members
Cogs::Core::TextureManager Class Reference

Texture manager responsible for loading, processing, activation and lifetime of Texture resources. More...

#include <TextureManager.h>

Inheritance diagram for Cogs::Core::TextureManager:
Cogs::Core::ResourceManager< Texture, TextureLoadInfo > Cogs::Core::ResourceManagerBase Cogs::Core::IResourceManager

Classes

struct  FetchedItem
 

Public Member Functions

 TextureManager (Context *context)
 Constructs a TextureManager in the given context.
 
 ~TextureManager ()
 Destructs the texture manager.
 
void initialize () override
 Initialize the texture manager. Creates the default texture resource.
 
void clear () override
 
TextureHandle loadTexture (const void *imageData, ResourceDimensions target, int width, int height, int depth, int layers, TextureFormat format, int stride, const ResourceId resourceId, TextureLoadFlags flags)
 Load a texture with the given data.
 
void cancelTextureLoad (TextureHandle handle)
 Notify that the texture isn't needed anymore and the texture load can be cancelled if posible.
 
TextureHandle loadTexture (const void *imageData, ResourceDimensions target, int width, int height, int depth, int layers, TextureFormat format, int stride, const TextureHandle &resourceHandle, TextureLoadFlags flags)
 Load an existing texture with the given data.
 
template<typename RESOURCE >
TextureHandle loadTexture2D (const void *imageData, int width, int height, TextureFormat format, int stride, RESOURCE resource, TextureLoadFlags flags)
 
TextureHandle loadExternalTexture (intptr_t externalHandle, ResourceDimensions target, int width, int height, int depth, int layers, TextureFormat format, const ResourceId resourceId, TextureLoadFlags flags)
 Loads a texture resource wrapping the external texture data so it may be used in the Engine like an internal texture resource.
 
TextureHandle loadTexture (const StringView &resourceName, const ResourceId resourceId, TextureLoadFlags flags)
 Loads a texture resource from the named resource given.
 
TextureHandle loadTextureFromMemory (const void *dataPtr, const size_t dataSize, const StringView &resourcePath, const ResourceId resourceId, TextureLoadFlags flags)
 Loads an encoded texture from data in memory.
 
TextureHandle loadTexture (TextureLoadInfo *loadInfo)
 Loads a texture resource using the given loadInfo.
 
TextureHandle getTexture (const StringView &path, bool isQuery=false)
 Gets the texture with the given path.
 
void handleLoad (TextureLoadInfo *loadInfo) override
 
void handleReload (ResourceHandleBase handle) override
 
void loadFromData (TextureLoadInfo *loadInfo)
 
ActivationResult handleActivation (TextureHandle handle, Texture *texture) override
 Overridden to handle texture activation, updating the texture resource in the renderer.
 
void handleDeletion (Texture *texture) override
 Overridden to handle texture deletion, removing the texture resource from the renderer.
 
- Public Member Functions inherited from Cogs::Core::ResourceManager< Texture, TextureLoadInfo >
 ResourceManager (Context *context, Collections::ElementOffset capacity=1024)
 Constructs a resource manager with the given context.
 
 ~ResourceManager () override
 Destructs a resource manager, provided for destruction via base pointer.
 
void clear () override
 Clear the resource manager, cleaning up resources held by member handles.
 
void clearSwapping () override
 Clear the swap queue, ensuring indirect resource references are released.
 
ResourceHandle create ()
 Create a new resource.
 
ResourceHandle release (ResourceId resourceId)
 
ResourceHandle loadResource (TextureLoadInfo *loadInfo)
 
ResourceProxy createLocked ()
 Creates a new resource, but returns a ResourceProxy for thread safe editing instead of a handle to the actual resource.
 
ResourceProxy lock (const ResourceHandle &handle)
 Takes the handle given and returns a resource proxy to edit the contents of the held resource safely on other threads than the main thread.
 
void unlockProxy (const ResourceHandle &handle, const ResourceHandle &proxy)
 Unlock the given resource proxy.
 
ResourceHandle generateHandle (ResourceBase *resource)
 Generate a handle to the given resource.
 
virtual void handleLoad (TextureLoadInfo *)
 Handler for resource loading.
 
virtual void handleFailedLoad (const TextureLoadInfo *)
 Handler for failed resource loads using the given loadInfo.
 
virtual ActivationResult handleActivation (ResourceHandle, Texture *)
 Handler for activation of resources.
 
virtual void handleDeletion (Texture *)
 Handler for deletion of resources.
 
Textureget (const ResourceHandleBase &handle)
 Resolve the given handle to a resource.
 
void processSwapping () override
 Process queued swap operations, exchanging resource contents of the destination resources by those indicated in the operation data.
 
ResourceHandle getHandle (const ResourceId id) const
 Get a resource handle to the resource with the given id.
 
void registerLoader (IResourceLoader< Texture, TextureLoadInfo > *loader)
 Register a resource loader capable of handling resources of the template type.
 
void destroyLoadInfoInternal (ResourceLoadInfoBase *loadInfo) override
 
TextureLoadInfocreateLoadInfo ()
 
- Public Member Functions inherited from Cogs::Core::ResourceManagerBase
 ResourceManagerBase (Context *context)
 
void initialize () override
 
void clear () override
 
void reportLeaks (std::string_view typeName)
 
std::string getReport ()
 Return a string with current resource usage.
 
bool checkPreloaded (ResourceLoadInfoBase *loadInfo)
 
ResourceHandleBase releaseInternal (ResourceId resourceId)
 Releases a resourceId -> ResourceHandle mapping.
 
ResourceHandleBase loadResourceInternal (ResourceLoadInfoBase *loadInfo)
 Load a resource using the given loadInfo.
 
void processLoading () final
 Process loading resources.
 
void setProcessed (ResourceLoadInfoBase *loadInfo, bool swapping=false)
 Signal the resource being loaded as done loading and ready for activation etc.
 
void activateResources () final
 
virtual void processDeletion () override
 Process resources pending deallocation.
 
void fillDeletionQueue (std::vector< ResourceBase * > &deletion)
 
void destroyLocked (ResourceBase *resource)
 
void safeDestroy (ResourceBase *resource)
 
ResourceId getNextResourceId () override
 Get the next unique resource id.
 
void resourceChanged (ResourceBase *resource) override
 
void resourceDeleted (ResourceBase *resource) override
 
void setResourceId (ResourceBase *resource, ResourceId id) override
 Assign the given id to a previously created resource.
 
void queueResource (ResourceLoadInfoBase *loadInfo)
 Re-queue the given loadInfo to defer loading to the next time the processLoading() phase is performed.
 
virtual void handleLoadInternal (ResourceLoadInfoBase *loadInfo)=0
 
virtual ActivationResult handleActivationInternal (ResourceHandleBase r, ResourceBase *resource)=0
 
virtual void handleDeletionInternal (ResourceBase *resource)=0
 
virtual void handleFailedLoadInternal (ResourceLoadInfoBase *loadInfo)=0
 
virtual void destroyLoadInfoInternal (ResourceLoadInfoBase *loadInfo)=0
 
virtual bool shouldAutoReload () const
 
virtual bool shouldMergeBySource () const
 
virtual void handleReload (ResourceHandleBase)
 
std::vector< ResourceBase * > getAllocatedResources () const
 
ResourceHandleBase getOrCreate (ResourceId id)
 
ResourceHandleBase getByName (std::string_view name) const
 
ResourceHandleBase getBySource (std::string_view source) const
 
ResourceHandleBase createResource ()
 
virtual ResourceBasecreateInternal ()=0
 
virtual void destroyInternal (ResourceBase *resource)=0
 
virtual void destroyInternalLocked (ResourceBase *resource)=0
 
ResourceHandleBase generateHandle (ResourceBase *resource)
 
void clearUpdated ()
 
virtual int getUpdateQuota () const
 
size_t updatedResourceCount ()
 
size_t orphanedResourceCount ()
 
size_t allocatedResourceCount ()
 
virtual void initialize ()=0
 
virtual void clear ()=0
 
virtual void clearSwapping ()=0
 
virtual void processLoading ()=0
 
virtual void processSwapping ()=0
 
virtual void activateResources ()=0
 
virtual void processDeletion ()=0
 
virtual void resourceChanged (ResourceBase *resource)=0
 
virtual void resourceDeleted (ResourceBase *resource)=0
 
virtual ResourceId getNextResourceId ()=0
 
virtual void setResourceId (ResourceBase *resource, ResourceId id)=0
 

Public Attributes

TextureHandle white = TextureHandle::NoHandle
 
TextureHandle whiteCube = TextureHandle::NoHandle
 
Cogs::Mutex lock
 
std::map< size_t, DataFetcherManager::FetchId > map
 

Protected Member Functions

void postProcessLoading () override final
 Hook for resource managers to run code at the tail of processLoading.
 
- Protected Member Functions inherited from Cogs::Core::ResourceManager< Texture, TextureLoadInfo >
TexturecreateInternal () override
 
void destroyInternal (ResourceBase *resource) override
 
void handleLoadInternal (ResourceLoadInfoBase *loadInfo) override
 
ActivationResult handleActivationInternal (ResourceHandleBase r, ResourceBase *resource) override
 
void handleDeletionInternal (ResourceBase *resource) override
 
void handleFailedLoadInternal (ResourceLoadInfoBase *loadInfo) override
 
IResourceLoader< Texture, TextureLoadInfo > * findLoader (const TextureLoadInfo *loadInfo)
 Find a loader capable of loading the resource given by loadInfo.
 
- Protected Member Functions inherited from Cogs::Core::ResourceManagerBase
ResourceHandleBase getResourceHandle (ResourceId id) const
 Get existing resource handle.
 
bool isValidResource (ResourceId id) const
 Check if resource handle points to.
 
virtual void postProcessLoading ()
 Hook for resource managers to run code at the tail of processLoading.
 

Private Member Functions

bool invokeLoader (ITextureLoader *loader, TextureLoadInfo *loadInfo)
 
bool invokeLoader (ILoadedTextureLoader *loadedLoader, TextureLoadInfo *loadInfo)
 
bool processFetchedItem (ILoadedTextureLoader *loadedLoader, TextureLoadInfo *loadInfo, std::unique_ptr< FileContents > data)
 
void loadFromPath (TextureLoadInfo *loadInfo)
 

Private Attributes

std::thread::id main
 
std::queue< FetchedItemfetchedItems
 
struct {
   Cogs::Mutex   lock
 
   std::map< size_t, DataFetcherManager::FetchId >   map
 
fetchIds
 

Additional Inherited Members

- Public Types inherited from Cogs::Core::ResourceManager< Texture, TextureLoadInfo >
using ResourceHandle = ResourceHandle_t< Texture >
 Type of handle used to hold resources.
 
using ResourceProxy = ResourceProxy< Texture, ResourceManager >
 Type of resource proxy objects, specialized on the type of resource.
 
- Protected Attributes inherited from Cogs::Core::ResourceManager< Texture, TextureLoadInfo >
Mutex swapMutex
 
Collections::Pool< Textureresources
 
std::vector< IResourceLoader< Texture, TextureLoadInfo > * > loaders
 
std::deque< SwapOperation > swapQueue
 
ResourceHandle defaultResource
 
- Protected Attributes inherited from Cogs::Core::ResourceManagerBase
std::unique_ptr< ResourceManagerBaseStoragestorage
 
Contextcontext = nullptr
 
ResourceTypes resourceType = ResourceTypes::Unknown
 

Detailed Description

Texture manager responsible for loading, processing, activation and lifetime of Texture resources.

Texture resources can either be created and modified manually by using the create() method, or automatically using one of the load*() methods. Using the load methods will create a texture resource and populate it using the load information given.

Definition at line 26 of file TextureManager.h.

Constructor & Destructor Documentation

◆ TextureManager()

Cogs::Core::TextureManager::TextureManager ( Context context)
inline

Constructs a TextureManager in the given context.

Definition at line 30 of file TextureManager.h.

◆ ~TextureManager()

Cogs::Core::TextureManager::~TextureManager ( )

Destructs the texture manager.

Definition at line 122 of file TextureManager.cpp.

Member Function Documentation

◆ cancelTextureLoad()

void Cogs::Core::TextureManager::cancelTextureLoad ( TextureHandle  handle)

Notify that the texture isn't needed anymore and the texture load can be cancelled if posible.

Definition at line 213 of file TextureManager.cpp.

◆ clear()

void Cogs::Core::TextureManager::clear ( )
overridevirtual

Reimplemented from Cogs::Core::ResourceManagerBase.

Definition at line 206 of file TextureManager.cpp.

◆ getTexture()

Cogs::Core::TextureHandle Cogs::Core::TextureManager::getTexture ( const StringView path,
bool  isQuery = false 
)

◆ handleActivation()

Cogs::Core::ActivationResult Cogs::Core::TextureManager::handleActivation ( TextureHandle  handle,
Texture texture 
)
overridevirtual

Overridden to handle texture activation, updating the texture resource in the renderer.

Reimplemented from Cogs::Core::ResourceManager< Texture, TextureLoadInfo >.

Definition at line 548 of file TextureManager.cpp.

◆ handleDeletion()

void Cogs::Core::TextureManager::handleDeletion ( Texture texture)
overridevirtual

Overridden to handle texture deletion, removing the texture resource from the renderer.

Reimplemented from Cogs::Core::ResourceManager< Texture, TextureLoadInfo >.

Definition at line 553 of file TextureManager.cpp.

◆ handleLoad()

void Cogs::Core::TextureManager::handleLoad ( TextureLoadInfo loadInfo)
overridevirtual

Overridden to handle loading of textures. Loading might be handed off to loaders implementing the ITextureLoader interface.

Reimplemented from Cogs::Core::ResourceManager< Texture, TextureLoadInfo >.

Definition at line 474 of file TextureManager.cpp.

References Cogs::Core::ResourceLoadInfoBase::resourcePath, and Cogs::Core::TaskManager::ResourceQueue.

◆ handleReload()

void Cogs::Core::TextureManager::handleReload ( ResourceHandleBase  handle)
overridevirtual

Reimplemented from Cogs::Core::ResourceManagerBase.

Definition at line 503 of file TextureManager.cpp.

◆ initialize()

void Cogs::Core::TextureManager::initialize ( )
overridevirtual

◆ invokeLoader() [1/2]

bool Cogs::Core::TextureManager::invokeLoader ( ILoadedTextureLoader loadedLoader,
TextureLoadInfo loadInfo 
)
private

Definition at line 616 of file TextureManager.cpp.

◆ invokeLoader() [2/2]

bool Cogs::Core::TextureManager::invokeLoader ( ITextureLoader loader,
TextureLoadInfo loadInfo 
)
private

Definition at line 604 of file TextureManager.cpp.

◆ loadExternalTexture()

Cogs::Core::TextureHandle Cogs::Core::TextureManager::loadExternalTexture ( intptr_t  externalHandle,
ResourceDimensions  target,
int  width,
int  height,
int  depth,
int  layers,
TextureFormat  format,
const ResourceId  resourceId,
TextureLoadFlags  flags 
)

Loads a texture resource wrapping the external texture data so it may be used in the Engine like an internal texture resource.

Parameters
imageDataHandle to an external texture resource (platform dependent).
widthWidth of the image in pixels.
heightHeight of the image in pixels.
formatFormat of the image data.
resourceIdResource identifier to assign to the Texture. If a texture resource with the same id already exists, the existing Texture contents are replaced with the new data.
flagsLoading flags describing how to load the texture resource.
Returns
Handle to the destination Texture resource wrapping the external texture object.

Definition at line 516 of file TextureManager.cpp.

References Cogs::FormatInfo::elements, Cogs::TextureFlags::GenerateMipMaps, Cogs::Core::NoDelete, Cogs::TextureFlags::NoDelete, Cogs::Core::NoMipMaps, and Cogs::Core::None.

◆ loadFromData()

void Cogs::Core::TextureManager::loadFromData ( TextureLoadInfo loadInfo)

Definition at line 723 of file TextureManager.cpp.

◆ loadFromPath()

void Cogs::Core::TextureManager::loadFromPath ( TextureLoadInfo loadInfo)
private

Definition at line 628 of file TextureManager.cpp.

◆ loadTexture() [1/4]

Cogs::Core::TextureHandle Cogs::Core::TextureManager::loadTexture ( const StringView resourceName,
const ResourceId  resourceId,
TextureLoadFlags  flags 
)

Loads a texture resource from the named resource given.

The \resourceName might point to a resource on disk or in the ResourceStore.

Parameters
resourceIdResource identifier to assign to the Texture. If a texture resource with the same id already exists, the existing Texture contents are replaced with the new data.
flagsLoading flags describing how to load the texture resource.
Returns
Handle to the destination Texture resource receiving the given data.

Definition at line 288 of file TextureManager.cpp.

References Cogs::Core::DoNotStoreSource, Cogs::Core::Flip, Cogs::Core::LinearColorSpace, Cogs::Core::ResourceLoadInfoBase::loadFlags, Cogs::Core::ResourceLoadInfoBase::resourceId, Cogs::Core::ResourceLoadInfoBase::resourcePath, and Cogs::StringView::to_string().

◆ loadTexture() [2/4]

Cogs::Core::TextureHandle Cogs::Core::TextureManager::loadTexture ( const void *  imageData,
ResourceDimensions  target,
int  width,
int  height,
int  depth,
int  layers,
TextureFormat  format,
int  stride,
const ResourceId  resourceId,
TextureLoadFlags  flags 
)

Load a texture with the given data.

A Texture resource is created and the given data queued to be loaded at an suitable time.

Parameters
imageDataPointer to image data buffer of size at least width (or stride) * height * bpp of the format.
widthWidth of the image in pixels.
heightHeight of the image in pixels.
formatFormat of the image data.
strideRow width stride in bytes. If set to zero, defaults to the size of one row of pixels.
resourceIdResource identifier to assign to the Texture. If a texture resource with the same id already exists, the existing Texture contents are replaced with the new data.
flagsLoading flags describing how to load the texture resource.
Returns
Handle to the destination Texture resource receiving the given data.

Definition at line 233 of file TextureManager.cpp.

References Cogs::Core::ResourceHandle_t< Texture >::NoHandle, Cogs::Core::ResourceLoadInfoBase::resourceData, and Cogs::Core::ResourceLoadInfoBase::resourceId.

◆ loadTexture() [3/4]

Cogs::Core::TextureHandle Cogs::Core::TextureManager::loadTexture ( const void *  imageData,
ResourceDimensions  target,
int  width,
int  height,
int  depth,
int  layers,
TextureFormat  format,
int  stride,
const TextureHandle resourceHandle,
TextureLoadFlags  flags 
)

Load an existing texture with the given data.

Parameters
imageDataPointer to image data buffer of size at least width (or stride) * height * bpp of the format.
widthWidth of the image in pixels.
heightHeight of the image in pixels.
formatFormat of the image data.
strideRow width stride in bytes. If set to zero, defaults to the size of one row of pixels.
resourceHandleResource handle whose Texture contents are to be replaced with the new data.
flagsLoading flags describing how to load the texture resource.
Returns
Handle to the destination Texture resource receiving the given data.

Definition at line 261 of file TextureManager.cpp.

References Cogs::Core::ResourceLoadInfoBase::handle, Cogs::Core::ResourceHandle_t< Texture >::NoHandle, and Cogs::Core::ResourceLoadInfoBase::resourceData.

◆ loadTexture() [4/4]

Cogs::Core::TextureHandle Cogs::Core::TextureManager::loadTexture ( TextureLoadInfo loadInfo)

Loads a texture resource using the given loadInfo.

Parameters
loadInfoContains information on how to load the texture. Takes ownership of object.
Returns
Handle to the destination Texture resource.

Definition at line 330 of file TextureManager.cpp.

◆ loadTexture2D()

template<typename RESOURCE >
TextureHandle Cogs::Core::TextureManager::loadTexture2D ( const void *  imageData,
int  width,
int  height,
TextureFormat  format,
int  stride,
RESOURCE  resource,
TextureLoadFlags  flags 
)
inline

Definition at line 80 of file TextureManager.h.

◆ loadTextureFromMemory()

TextureHandle Cogs::Core::TextureManager::loadTextureFromMemory ( const void *  dataPtr,
const size_t  dataSize,
const StringView resourcePath,
const ResourceId  resourceId,
TextureLoadFlags  flags 
)

Loads an encoded texture from data in memory.

Parameters
dataPtrPointer to the memory block with encoded data, the data is copied.
dataSizeSizeo fo the memory block with encoded data, the data is copied.
resourcePathSuffix of file path is used to determine loader, can be a dummy file path.
resourceIdResource identifier to assign to the Texture. If a texture resource with the same id already exists, the existing Texture contents are replaced with the new data.
flagsLoading flags describing how to load the texture resource.
Returns
Handle to the destination Texture resource receiving the given data.

Definition at line 307 of file TextureManager.cpp.

References Cogs::Core::ColorSpaceFromLoadInfo, Cogs::Core::DoNotStoreSource, Cogs::Core::Flip, Cogs::Core::ForceUnique, Cogs::Core::LinearColorSpace, Cogs::Core::ResourceLoadInfoBase::loadFlags, Cogs::Core::NoMipMaps, Cogs::Core::ResourceLoadInfoBase::resourceData, Cogs::Core::ResourceLoadInfoBase::resourceId, Cogs::Core::ResourceLoadInfoBase::resourcePath, and Cogs::StringView::to_string().

◆ postProcessLoading()

void Cogs::Core::TextureManager::postProcessLoading ( )
finaloverrideprotectedvirtual

Hook for resource managers to run code at the tail of processLoading.

Reimplemented from Cogs::Core::ResourceManagerBase.

Definition at line 432 of file TextureManager.cpp.

References Cogs::Core::ResourceLoadInfoBase::resourceData.

◆ processFetchedItem()

bool Cogs::Core::TextureManager::processFetchedItem ( ILoadedTextureLoader loadedLoader,
TextureLoadInfo loadInfo,
std::unique_ptr< FileContents data 
)
private

Definition at line 558 of file TextureManager.cpp.

Member Data Documentation

◆ fetchedItems

std::queue<FetchedItem> Cogs::Core::TextureManager::fetchedItems
private

Definition at line 178 of file TextureManager.h.

◆ lock

Cogs::Mutex Cogs::Core::TextureManager::lock

Definition at line 181 of file TextureManager.h.

◆ main

std::thread::id Cogs::Core::TextureManager::main
private

Definition at line 162 of file TextureManager.h.

◆ map

std::map<size_t, DataFetcherManager::FetchId> Cogs::Core::TextureManager::map

Definition at line 182 of file TextureManager.h.

◆ white

TextureHandle Cogs::Core::TextureManager::white = TextureHandle::NoHandle

Definition at line 155 of file TextureManager.h.

◆ whiteCube

TextureHandle Cogs::Core::TextureManager::whiteCube = TextureHandle::NoHandle

Definition at line 156 of file TextureManager.h.


The documentation for this class was generated from the following files: