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
|
|
|
|
//
|
2022-08-23 20:24:24 +00:00
|
|
|
// Handles textures including loading, unloading and cache management.
|
2020-06-21 12:25:28 +00:00
|
|
|
//
|
|
|
|
|
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-11-01 22:21:10 +00:00
|
|
|
#include "resources/ResourceManager.h"
|
2021-10-25 16:39:58 +00:00
|
|
|
#include "resources/TextureData.h"
|
2017-11-01 22:21:10 +00:00
|
|
|
#include "resources/TextureDataManager.h"
|
2021-08-17 20:11:16 +00:00
|
|
|
#include "utils/MathUtil.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>
|
2021-06-12 19:08:35 +00:00
|
|
|
#include <vector>
|
2017-11-01 22:21:10 +00:00
|
|
|
|
|
|
|
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:
|
2021-07-07 18:31:46 +00:00
|
|
|
static std::shared_ptr<TextureResource> get(const std::string& path,
|
|
|
|
bool tile = false,
|
|
|
|
bool forceLoad = false,
|
|
|
|
bool dynamic = true,
|
2021-08-19 18:16:42 +00:00
|
|
|
bool linearMagnify = false,
|
2022-09-05 21:36:49 +00:00
|
|
|
bool mipmapping = false,
|
2022-08-23 20:24:24 +00:00
|
|
|
size_t width = 0,
|
2022-08-28 18:11:20 +00:00
|
|
|
size_t height = 0,
|
|
|
|
float tileWidth = 0.0f,
|
|
|
|
float tileHeight = 0.0f);
|
2020-06-21 12:25:28 +00:00
|
|
|
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);
|
2022-08-23 20:24:24 +00:00
|
|
|
static void manualUnload(const std::string& path, bool tile);
|
2022-08-14 19:31:02 +00:00
|
|
|
static void manualUnloadAll() { sTextureMap.clear(); }
|
2017-01-22 23:28:06 +00:00
|
|
|
|
2021-06-12 19:08:35 +00:00
|
|
|
// Returns the raw pixel values.
|
|
|
|
std::vector<unsigned char> getRawRGBAData();
|
|
|
|
|
2021-10-25 16:39:58 +00:00
|
|
|
// Has the image been loaded but not yet been rasterized as the size was not known?
|
2022-08-16 19:04:39 +00:00
|
|
|
const bool getPendingRasterization() const
|
2021-10-26 16:22:41 +00:00
|
|
|
{
|
|
|
|
return (mTextureData != nullptr ? mTextureData->getPendingRasterization() : false);
|
|
|
|
}
|
2021-10-25 16:39:58 +00:00
|
|
|
|
2022-08-16 19:04:39 +00:00
|
|
|
const bool getScalable() const
|
|
|
|
{
|
|
|
|
return (mTextureData != nullptr ? mTextureData->getScalable() : false);
|
|
|
|
}
|
|
|
|
|
2022-09-02 18:48:45 +00:00
|
|
|
void setLinearMagnify(bool state) { mTextureData->setLinearMagnify(state); }
|
2022-01-07 17:54:52 +00:00
|
|
|
|
2021-07-02 18:33:50 +00:00
|
|
|
std::string getTextureFilePath();
|
|
|
|
|
2021-10-23 13:45:44 +00:00
|
|
|
void rasterizeAt(float width, float height);
|
2021-08-16 16:25:01 +00:00
|
|
|
glm::vec2 getSourceImageSize() const { return mSourceSize; }
|
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 isTiled() const;
|
2022-08-28 18:11:20 +00:00
|
|
|
void setSize(float width, float height)
|
|
|
|
{
|
|
|
|
mSize.x = static_cast<int>(std::round(width));
|
|
|
|
mSize.y = static_cast<int>(std::round(height));
|
|
|
|
}
|
2013-06-21 16:49:29 +00:00
|
|
|
|
2021-08-17 16:41:45 +00:00
|
|
|
const glm::ivec2 getSize() const { return mSize; }
|
2020-06-21 12:25:28 +00:00
|
|
|
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:
|
2021-08-19 18:16:42 +00:00
|
|
|
TextureResource(const std::string& path,
|
2022-08-28 18:11:20 +00:00
|
|
|
float tileWidth,
|
|
|
|
float tileHeight,
|
2021-08-19 18:16:42 +00:00
|
|
|
bool tile,
|
|
|
|
bool dynamic,
|
|
|
|
bool linearMagnify,
|
2022-09-05 21:36:49 +00:00
|
|
|
bool mipmapping,
|
2022-08-28 18:21:58 +00:00
|
|
|
bool scalable);
|
2022-01-04 20:21:26 +00:00
|
|
|
virtual void unload(ResourceManager& rm);
|
|
|
|
virtual void reload(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.
|
2022-08-14 19:31:02 +00:00
|
|
|
static inline TextureDataManager sTextureDataManager;
|
2017-01-22 23:28:06 +00:00
|
|
|
|
2021-08-17 16:41:45 +00:00
|
|
|
glm::ivec2 mSize;
|
2021-08-16 16:25:01 +00:00
|
|
|
glm::vec2 mSourceSize;
|
2020-06-21 12:25:28 +00:00
|
|
|
bool mForceLoad;
|
2014-03-27 21:47:25 +00:00
|
|
|
|
2022-08-23 20:24:24 +00:00
|
|
|
// File path, tile, linear interpolation, scalable/SVG, width, height.
|
|
|
|
using TextureKeyType = std::tuple<std::string, bool, bool, bool, size_t, size_t>;
|
2020-06-21 12:25:28 +00:00
|
|
|
// Map of textures, used to prevent duplicate textures.
|
2022-08-14 19:31:02 +00:00
|
|
|
static inline std::map<TextureKeyType, std::weak_ptr<TextureResource>> sTextureMap;
|
2020-06-21 12:25:28 +00:00
|
|
|
// Set of all textures, used for memory management.
|
2022-08-14 19:31:02 +00:00
|
|
|
static inline 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
|