diff --git a/es-core/src/resources/TextureData.cpp b/es-core/src/resources/TextureData.cpp index 8182106f5..e5e8d0a4a 100644 --- a/es-core/src/resources/TextureData.cpp +++ b/es-core/src/resources/TextureData.cpp @@ -179,7 +179,9 @@ bool TextureData::load() const ResourceData& data = rm->getFileData(mPath); // Is it an SVG? if (Utils::String::toLower(mPath.substr(mPath.size() - 4, std::string::npos)) == ".svg") { + std::unique_lock lock(mMutex); mScalable = true; + lock.unlock(); std::string dataString; dataString.assign(std::string(reinterpret_cast(data.ptr.get()), data.length)); retval = initSVGFromMemory(dataString); @@ -243,35 +245,51 @@ void TextureData::releaseRAM() size_t TextureData::width() { - if (mWidth == 0) + std::unique_lock lock(mMutex); + if (mWidth == 0) { + lock.unlock(); load(); + } return static_cast(mWidth); } size_t TextureData::height() { - if (mHeight == 0) + std::unique_lock lock(mMutex); + if (mHeight == 0) { + lock.unlock(); load(); + } return static_cast(mHeight); } float TextureData::sourceWidth() { - if (mSourceWidth == 0) + std::unique_lock lock(mMutex); + if (mSourceWidth == 0) { + lock.unlock(); load(); + } return mSourceWidth; } float TextureData::sourceHeight() { - if (mSourceHeight == 0) + std::unique_lock lock(mMutex); + if (mSourceHeight == 0) { + lock.unlock(); load(); + } return mSourceHeight; } void TextureData::setSourceSize(float width, float height) { - if (mScalable) { + std::unique_lock lock(mMutex); + bool scalable = mScalable; + lock.unlock(); + + if (scalable) { if ((mSourceWidth != width) || (mSourceHeight != height)) { mSourceWidth = width; mSourceHeight = height; @@ -283,6 +301,7 @@ void TextureData::setSourceSize(float width, float height) size_t TextureData::getVRAMUsage() { + std::unique_lock lock(mMutex); if (mTextureID != 0 || !mDataRGBA.empty()) return mWidth * mHeight * 4; else diff --git a/es-core/src/resources/TextureDataManager.cpp b/es-core/src/resources/TextureDataManager.cpp index b4a64de2a..f43a84b3f 100644 --- a/es-core/src/resources/TextureDataManager.cpp +++ b/es-core/src/resources/TextureDataManager.cpp @@ -16,7 +16,7 @@ TextureDataManager::TextureDataManager() { unsigned char data[5 * 5 * 4]; - mBlank = std::shared_ptr(new TextureData(false)); + mBlank = std::make_shared(false); for (int i = 0; i < (5 * 5); i++) { data[i * 4] = (i % 2) * 255; data[i * 4 + 1] = (i % 2) * 255; @@ -36,7 +36,7 @@ TextureDataManager::~TextureDataManager() std::shared_ptr TextureDataManager::add(const TextureResource* key, bool tiled) { remove(key); - std::shared_ptr data(new TextureData(tiled)); + std::shared_ptr data = std::make_shared(tiled); mTextures.push_front(data); mTextureLookup[key] = mTextures.cbegin(); return data; @@ -158,11 +158,13 @@ TextureLoader::TextureLoader() TextureLoader::~TextureLoader() { // Just abort any waiting texture. + std::unique_lock lock(mMutex); mTextureDataQ.clear(); mTextureDataLookup.clear(); // Exit the thread. mExit = true; + lock.unlock(); mEvent.notify_one(); mThread->join(); mThread.reset(); @@ -170,7 +172,9 @@ TextureLoader::~TextureLoader() void TextureLoader::threadProc() { - while (!mExit) { + bool exit = false; + + while (!exit) { std::shared_ptr textureData; { // Wait for an event to say there is something in the queue. @@ -195,6 +199,7 @@ void TextureLoader::threadProc() mTextureDataLookup.erase(mTextureDataLookup.find(textureData.get())); } } + exit = mExit; } }