2020-09-21 17:17:34 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2020-06-21 12:25:28 +00:00
|
|
|
//
|
2020-09-21 17:17:34 +00:00
|
|
|
// EmulationStation Desktop Edition
|
2020-06-21 12:25:28 +00:00
|
|
|
// TextureDataManager.h
|
|
|
|
//
|
|
|
|
// Loading and unloading of texture data.
|
|
|
|
//
|
|
|
|
|
2017-10-31 17:12:50 +00:00
|
|
|
#ifndef ES_CORE_RESOURCES_TEXTURE_DATA_MANAGER_H
|
|
|
|
#define ES_CORE_RESOURCES_TEXTURE_DATA_MANAGER_H
|
2017-01-22 23:28:06 +00:00
|
|
|
|
2017-11-01 22:21:10 +00:00
|
|
|
#include <condition_variable>
|
|
|
|
#include <list>
|
2017-01-22 23:28:06 +00:00
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <mutex>
|
2017-11-01 22:21:10 +00:00
|
|
|
#include <thread>
|
2017-01-22 23:28:06 +00:00
|
|
|
|
2017-11-01 22:21:10 +00:00
|
|
|
class TextureData;
|
2017-01-22 23:28:06 +00:00
|
|
|
class TextureResource;
|
|
|
|
|
|
|
|
class TextureLoader
|
|
|
|
{
|
|
|
|
public:
|
2020-06-21 12:25:28 +00:00
|
|
|
TextureLoader();
|
|
|
|
~TextureLoader();
|
2017-01-22 23:28:06 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
void load(std::shared_ptr<TextureData> textureData);
|
|
|
|
void remove(std::shared_ptr<TextureData> textureData);
|
2017-01-22 23:28:06 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
size_t getQueueSize();
|
2017-01-22 23:28:06 +00:00
|
|
|
|
|
|
|
private:
|
2020-06-21 12:25:28 +00:00
|
|
|
void processQueue();
|
|
|
|
void threadProc();
|
2017-01-22 23:28:06 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
std::list<std::shared_ptr<TextureData>> mTextureDataQ;
|
|
|
|
std::map<TextureData*,
|
|
|
|
std::list<std::shared_ptr<TextureData>>::const_iterator> mTextureDataLookup;
|
2017-01-22 23:28:06 +00:00
|
|
|
|
2021-05-12 20:49:24 +00:00
|
|
|
std::unique_ptr<std::thread> mThread;
|
2020-06-21 12:25:28 +00:00
|
|
|
std::mutex mMutex;
|
|
|
|
std::condition_variable mEvent;
|
|
|
|
bool mExit;
|
2017-01-22 23:28:06 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
//
|
2020-06-21 12:25:28 +00:00
|
|
|
// This class manages the loading and unloading of textures.
|
2017-01-22 23:28:06 +00:00
|
|
|
//
|
|
|
|
// When textures are added, the texture data is just stored as-is. The texture
|
|
|
|
// data should only have been constructed and not loaded for this to work correctly.
|
|
|
|
// When the get() function is called it indicates that a texture wants to be used so
|
|
|
|
// at this point the texture data is loaded (via a call to load()).
|
|
|
|
//
|
|
|
|
// Once the load is complete (which may not be on the first call to get() if the
|
|
|
|
// data is loaded in a background thread) then the get() function call uploadAndBind()
|
|
|
|
// to upload to VRAM if necessary and bind the texture. This is followed by a call
|
|
|
|
// to releaseRAM() which frees the memory buffer if the texture can be reloaded from
|
2020-06-21 12:25:28 +00:00
|
|
|
// disk if needed again.
|
2017-01-22 23:28:06 +00:00
|
|
|
//
|
|
|
|
class TextureDataManager
|
|
|
|
{
|
|
|
|
public:
|
2020-06-21 12:25:28 +00:00
|
|
|
TextureDataManager();
|
|
|
|
~TextureDataManager();
|
2017-01-22 23:28:06 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
std::shared_ptr<TextureData> add(const TextureResource* key, bool tiled);
|
2017-01-22 23:28:06 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
// The texturedata being removed may be loading in a different thread. However it will
|
2020-08-15 07:28:47 +00:00
|
|
|
// be referenced by a smart pointer so we only need to remove it from our array and it
|
2020-06-21 12:25:28 +00:00
|
|
|
// will be deleted when the other thread has finished with it.
|
|
|
|
void remove(const TextureResource* key);
|
2017-01-22 23:28:06 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
std::shared_ptr<TextureData> get(const TextureResource* key);
|
|
|
|
bool bind(const TextureResource* key);
|
2017-01-22 23:28:06 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
// Get the total size of all textures managed by this object, loaded and unloaded in bytes.
|
|
|
|
size_t getTotalSize();
|
|
|
|
// Get the total size of all committed textures (in VRAM) in bytes.
|
|
|
|
size_t getCommittedSize();
|
|
|
|
// Get the total size of all load-pending textures in the queue - these will
|
|
|
|
// be committed to VRAM as the queue is processed.
|
|
|
|
size_t getQueueSize();
|
|
|
|
// Load a texture, freeing resources as necessary to make space.
|
|
|
|
void load(std::shared_ptr<TextureData> tex, bool block = false);
|
2017-01-22 23:28:06 +00:00
|
|
|
|
|
|
|
private:
|
2020-06-21 12:25:28 +00:00
|
|
|
std::list<std::shared_ptr<TextureData>> mTextures;
|
|
|
|
std::map<const TextureResource*,
|
|
|
|
std::list<std::shared_ptr<TextureData>>::const_iterator> mTextureLookup;
|
|
|
|
std::shared_ptr<TextureData> mBlank;
|
|
|
|
TextureLoader* mLoader;
|
2017-01-22 23:28:06 +00:00
|
|
|
};
|
|
|
|
|
2017-10-31 17:12:50 +00:00
|
|
|
#endif // ES_CORE_RESOURCES_TEXTURE_DATA_MANAGER_H
|