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
|
|
|
// TextureResource.h
|
|
|
|
//
|
|
|
|
// Handles OpenGL textures.
|
|
|
|
//
|
|
|
|
|
2017-10-31 17:12:50 +00:00
|
|
|
#ifndef ES_CORE_RESOURCES_TEXTURE_RESOURCE_H
|
|
|
|
#define ES_CORE_RESOURCES_TEXTURE_RESOURCE_H
|
2013-06-21 16:49:29 +00:00
|
|
|
|
2017-10-28 20:24:35 +00:00
|
|
|
#include "math/Vector2f.h"
|
2020-11-14 16:18:00 +00:00
|
|
|
#include "math/Vector2i.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
#include "resources/ResourceManager.h"
|
|
|
|
#include "resources/TextureDataManager.h"
|
2020-09-21 17:17:34 +00:00
|
|
|
|
2020-12-28 22:49:34 +00:00
|
|
|
#include <cmath>
|
2017-11-01 22:21:10 +00:00
|
|
|
#include <set>
|
|
|
|
#include <string>
|
|
|
|
|
|
|
|
class TextureData;
|
2013-06-21 16:49:29 +00:00
|
|
|
|
2014-01-24 22:21:10 +00:00
|
|
|
// An OpenGL texture.
|
|
|
|
// Automatically recreates the texture with renderer deinit/reinit.
|
2013-07-09 05:44:24 +00:00
|
|
|
class TextureResource : public IReloadable
|
2013-06-21 16:49:29 +00:00
|
|
|
{
|
|
|
|
public:
|
2020-06-21 12:25:28 +00:00
|
|
|
static std::shared_ptr<TextureResource> get(const std::string& path, bool tile = false,
|
|
|
|
bool forceLoad = false, bool dynamic = true);
|
|
|
|
void initFromPixels(const unsigned char* dataRGBA, size_t width, size_t height);
|
2020-09-04 17:05:48 +00:00
|
|
|
virtual void initFromMemory(const char* data, size_t length);
|
2020-11-14 16:18:00 +00:00
|
|
|
static void manualUnload(std::string path, bool tile);
|
2017-01-22 23:28:06 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
// For scalable source images in textures we want to set the resolution to rasterize at.
|
|
|
|
void rasterizeAt(size_t width, size_t height);
|
|
|
|
Vector2f getSourceImageSize() const;
|
2013-06-21 16:49:29 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
virtual ~TextureResource();
|
2013-06-21 16:49:29 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
bool isInitialized() const;
|
|
|
|
bool isTiled() const;
|
2013-06-21 16:49:29 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
const Vector2i getSize() const;
|
|
|
|
bool bind();
|
2013-07-09 05:44:24 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
// Returns an approximation of total VRAM used by textures (in bytes).
|
|
|
|
static size_t getTotalMemUsage();
|
|
|
|
// Returns the number of bytes that would be used if all textures were in memory.
|
|
|
|
static size_t getTotalTextureSize();
|
2014-03-27 21:47:25 +00:00
|
|
|
|
2014-03-20 01:13:59 +00:00
|
|
|
protected:
|
2020-06-21 12:25:28 +00:00
|
|
|
TextureResource(const std::string& path, bool tile, bool dynamic);
|
|
|
|
virtual void unload(std::shared_ptr<ResourceManager>& rm);
|
|
|
|
virtual void reload(std::shared_ptr<ResourceManager>& rm);
|
2013-07-09 05:44:24 +00:00
|
|
|
|
2014-03-20 01:13:59 +00:00
|
|
|
private:
|
2020-06-21 12:25:28 +00:00
|
|
|
// mTextureData is used for textures that are not loaded from a file - these ones
|
|
|
|
// are permanently allocated and cannot be loaded and unloaded based on resources.
|
|
|
|
std::shared_ptr<TextureData> mTextureData;
|
2017-01-22 23:28:06 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
// The texture data manager manages loading and unloading of filesystem based textures.
|
|
|
|
static TextureDataManager sTextureDataManager;
|
2017-01-22 23:28:06 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
Vector2i mSize;
|
|
|
|
Vector2f mSourceSize;
|
|
|
|
bool mForceLoad;
|
2014-03-27 21:47:25 +00:00
|
|
|
|
2020-06-21 12:25:28 +00:00
|
|
|
typedef std::pair<std::string, bool> TextureKeyType;
|
|
|
|
// Map of textures, used to prevent duplicate textures.
|
2020-11-14 16:18:00 +00:00
|
|
|
static std::map<TextureKeyType, std::weak_ptr<TextureResource>> sTextureMap;
|
2020-06-21 12:25:28 +00:00
|
|
|
// Set of all textures, used for memory management.
|
|
|
|
static std::set<TextureResource*> sAllTextures;
|
2013-06-21 16:49:29 +00:00
|
|
|
};
|
2017-10-31 17:12:50 +00:00
|
|
|
|
|
|
|
#endif // ES_CORE_RESOURCES_TEXTURE_RESOURCE_H
|