diff --git a/es-core/src/resources/TextureData.cpp b/es-core/src/resources/TextureData.cpp index e8d9b84f6..391cf8a37 100644 --- a/es-core/src/resources/TextureData.cpp +++ b/es-core/src/resources/TextureData.cpp @@ -33,9 +33,10 @@ TextureData::TextureData(bool tile) , mSourceWidth{0.0f} , mSourceHeight{0.0f} , mScalable{false} + , mHasRGBAData{false} + , mPendingRasterization{false} , mLinearMagnify{false} , mForceRasterization{false} - , mPendingRasterization{false} { } @@ -55,9 +56,9 @@ void TextureData::initFromPath(const std::string& path) bool TextureData::initSVGFromMemory(const std::string& fileData) { - // If already initialized then don't process it again. std::unique_lock lock{mMutex}; + // If already initialized then don't process it again. if (!mDataRGBA.empty()) return true; @@ -111,6 +112,7 @@ bool TextureData::initSVGFromMemory(const std::string& fileData) ImageIO::flipPixelsVert(mDataRGBA.data(), mWidth, mHeight); mPendingRasterization = false; + mHasRGBAData = true; } else { // TODO: Fix this properly instead of using the single byte texture workaround. @@ -125,9 +127,6 @@ bool TextureData::initSVGFromMemory(const std::string& fileData) bool TextureData::initImageFromMemory(const unsigned char* fileData, size_t length) { - size_t width; - size_t height; - // If already initialized then don't process it again. { std::unique_lock lock(mMutex); @@ -135,6 +134,9 @@ bool TextureData::initImageFromMemory(const unsigned char* fileData, size_t leng return true; } + size_t width; + size_t height; + std::vector imageRGBA = ImageIO::loadFromMemoryRGBA32( static_cast(fileData), length, width, height); @@ -166,6 +168,8 @@ bool TextureData::initFromRGBA(const unsigned char* dataRGBA, size_t width, size mWidth = static_cast(width); mHeight = static_cast(height); + mHasRGBAData = true; + return true; } @@ -196,7 +200,8 @@ bool TextureData::isLoaded() { std::unique_lock lock(mMutex); if (!mDataRGBA.empty() || mTextureID != 0) - return true; + if (mHasRGBAData || mPendingRasterization || mTextureID != 0) + return true; return false; } @@ -238,14 +243,13 @@ void TextureData::releaseRAM() if (!mDataRGBA.empty()) { mDataRGBA.clear(); mDataRGBA.swap(swapVector); + mHasRGBAData = false; } } size_t TextureData::width() { - std::unique_lock lock(mMutex); if (mWidth == 0) { - lock.unlock(); load(); } return static_cast(mWidth); @@ -253,9 +257,7 @@ size_t TextureData::width() size_t TextureData::height() { - std::unique_lock lock(mMutex); if (mHeight == 0) { - lock.unlock(); load(); } return static_cast(mHeight); @@ -263,9 +265,7 @@ size_t TextureData::height() float TextureData::sourceWidth() { - std::unique_lock lock(mMutex); if (mSourceWidth == 0) { - lock.unlock(); load(); } return mSourceWidth; @@ -273,9 +273,7 @@ float TextureData::sourceWidth() float TextureData::sourceHeight() { - std::unique_lock lock(mMutex); if (mSourceHeight == 0) { - lock.unlock(); load(); } return mSourceHeight; @@ -295,8 +293,7 @@ void TextureData::setSourceSize(float width, float height) size_t TextureData::getVRAMUsage() { - std::unique_lock lock(mMutex); - if (mTextureID != 0 || !mDataRGBA.empty()) + if (mHasRGBAData || mTextureID != 0) return mWidth * mHeight * 4; else return 0; diff --git a/es-core/src/resources/TextureData.h b/es-core/src/resources/TextureData.h index 062e54d6f..aee2eac4c 100644 --- a/es-core/src/resources/TextureData.h +++ b/es-core/src/resources/TextureData.h @@ -56,7 +56,7 @@ public: float sourceWidth(); float sourceHeight(); void setSourceSize(float width, float height); - glm::vec2 getSize() { return glm::vec2{mWidth, mHeight}; } + glm::vec2 getSize() { return glm::vec2{static_cast(mWidth), static_cast(mHeight)}; } // Whether to use linear filtering when magnifying the texture. void setLinearMagnify(bool setting) { mLinearMagnify = setting; } @@ -72,19 +72,21 @@ public: private: std::mutex mMutex; + bool mTile; std::string mPath; - unsigned int mTextureID; + std::atomic mTextureID; std::vector mDataRGBA; - int mWidth; - int mHeight; - float mSourceWidth; - float mSourceHeight; + std::atomic mWidth; + std::atomic mHeight; + std::atomic mSourceWidth; + std::atomic mSourceHeight; std::atomic mScalable; + std::atomic mHasRGBAData; + std::atomic mPendingRasterization; bool mLinearMagnify; bool mReloadable; bool mForceRasterization; - bool mPendingRasterization; }; #endif // ES_CORE_RESOURCES_TEXTURE_DATA_H diff --git a/es-core/src/resources/TextureResource.h b/es-core/src/resources/TextureResource.h index b36118f2f..b9f5646d8 100644 --- a/es-core/src/resources/TextureResource.h +++ b/es-core/src/resources/TextureResource.h @@ -40,7 +40,7 @@ public: std::vector getRawRGBAData(); // Has the image been loaded but not yet been rasterized as the size was not known? - bool getPendingRasterization() + bool getPendingRasterization() const { return (mTextureData != nullptr ? mTextureData->getPendingRasterization() : false); }