Cogs.Core
TextureManager.h
1#pragma once
2
3#include <map>
4#include <queue>
5#include <thread>
6
7#include "ResourceManager.h"
8
9#include "DataFetcherManager.h"
10
11#include "Texture.h"
12
13#include "ITextureLoader.h"
14
15namespace Cogs
16{
17 namespace Core
18 {
26 class COGSCORE_DLL_API TextureManager : public ResourceManager<Texture, TextureLoadInfo>
27 {
28 public:
30 TextureManager(Context * context) : ResourceManager(context) {}
31
34
36 void initialize() override;
37
38 void clear() override;
39
56 TextureHandle loadTexture(const void * imageData, ResourceDimensions target, int width, int height, int depth, int layers, TextureFormat format, int stride, const ResourceId resourceId, TextureLoadFlags flags);
57
61 void cancelTextureLoad(TextureHandle handle);
62
77 TextureHandle loadTexture(const void * imageData, ResourceDimensions target, int width, int height, int depth, int layers, TextureFormat format, int stride, const TextureHandle& resourceHandle, TextureLoadFlags flags);
78
79 template<typename RESOURCE>
80 TextureHandle loadTexture2D(const void * imageData, int width, int height, TextureFormat format, int stride, RESOURCE resource, TextureLoadFlags flags) {
81 return loadTexture(imageData, ResourceDimensions::Texture2D, width, height, 1, 1, format, stride, resource, flags);
82 }
83
98 TextureHandle loadExternalTexture(intptr_t externalHandle, ResourceDimensions target, int width, int height, int depth, int layers, TextureFormat format, const ResourceId resourceId, TextureLoadFlags flags);
99
110 TextureHandle loadTexture(const StringView & resourceName, const ResourceId resourceId, TextureLoadFlags flags);
111
124 TextureHandle loadTextureFromMemory(const void* dataPtr, const size_t dataSize, const StringView& resourcePath, const ResourceId resourceId, TextureLoadFlags flags);
125
133 TextureHandle loadTexture(TextureLoadInfo * loadInfo);
134
138 TextureHandle getTexture(const StringView & path, bool isQuery = false);
139
142 void handleLoad(TextureLoadInfo * loadInfo) override;
143
144 void handleReload(ResourceHandleBase handle) override;
145
146 void loadFromData(TextureLoadInfo * loadInfo);
147
148
150 ActivationResult handleActivation(TextureHandle handle, Texture * texture) override;
151
153 void handleDeletion(Texture * texture) override;
154
155 TextureHandle white = TextureHandle::NoHandle;
156 TextureHandle whiteCube = TextureHandle::NoHandle;
157
158 protected:
159 void postProcessLoading() override final;
160
161 private:
162 std::thread::id main;
163
164 bool invokeLoader(ITextureLoader* loader, TextureLoadInfo* loadInfo);
165
166 bool invokeLoader(ILoadedTextureLoader* loadedLoader, TextureLoadInfo* loadInfo);
167
168 // Process a fetch data blob. Returns true if it was actually processed.
169 bool processFetchedItem(ILoadedTextureLoader* loadedLoader, TextureLoadInfo* loadInfo, std::unique_ptr<FileContents> data);
170
171 struct FetchedItem {
172 std::unique_ptr<FileContents> data;
173 TextureLoadInfo* loadInfo = nullptr;
174 ILoadedTextureLoader* loadedLoader = nullptr;
175 };
176
177 // Models that has been fetched, but has not yet had the loader run on it.
178 std::queue<FetchedItem> fetchedItems;
179
180 struct {
181 Cogs::Mutex lock;
182 std::map<size_t, DataFetcherManager::FetchId> map;
183 } fetchIds;
184
185 // Takes ownership of loadInfo and passes it to setProcessed eventually.
186 void loadFromPath(TextureLoadInfo* loadInfo);
187
188 };
189 }
190}
A Context instance contains all the services, systems and runtime components needed to use Cogs.
Definition: Context.h:83
The generic resource manager provides a base implementation for specialized resource managers to buil...
Texture manager responsible for loading, processing, activation and lifetime of Texture resources.
TextureManager(Context *context)
Constructs a TextureManager in the given context.
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
TextureLoadFlags
Texture loading flags. May be combined with resource load flags.
Definition: ResourceFlags.h:47
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
STL namespace.
Abstract base class storing data read from a file.
Definition: FileContents.h:20