cr and cleanup

This commit is contained in:
Sam Gondelman 2017-12-17 15:56:34 -08:00
parent 3ae52c0e0e
commit 6990dda48d
3 changed files with 13 additions and 9 deletions

View file

@ -772,7 +772,7 @@ void GLBackend::recycle() const {
GLVariableAllocationSupport::manageMemory(); GLVariableAllocationSupport::manageMemory();
GLVariableAllocationSupport::_frameTexturesCreated = 0; GLVariableAllocationSupport::_frameTexturesCreated = 0;
Texture::KtxStorage::clearKtxFiles(); Texture::KtxStorage::releaseOpenKtxFiles();
} }
void GLBackend::setCameraCorrection(const Mat4& correction) { void GLBackend::setCameraCorrection(const Mat4& correction) {

View file

@ -322,9 +322,7 @@ public:
void reset() override { } void reset() override { }
// Don't keep files open forever. We close them at the beginning of each frame (GLBackend::recycle) // Don't keep files open forever. We close them at the beginning of each frame (GLBackend::recycle)
static std::vector<std::pair<std::shared_ptr<storage::FileStorage>, std::shared_ptr<std::mutex>>> _cachedKtxFiles; static void releaseOpenKtxFiles();
static std::mutex _cachedKtxFilesMutex;
static void clearKtxFiles();
protected: protected:
std::shared_ptr<storage::FileStorage> maybeOpenFile() const; std::shared_ptr<storage::FileStorage> maybeOpenFile() const;
@ -332,6 +330,9 @@ public:
mutable std::shared_ptr<std::mutex> _cacheFileMutex { std::make_shared<std::mutex>() }; mutable std::shared_ptr<std::mutex> _cacheFileMutex { std::make_shared<std::mutex>() };
mutable std::shared_ptr<storage::FileStorage> _cacheFile; mutable std::shared_ptr<storage::FileStorage> _cacheFile;
static std::vector<std::pair<std::shared_ptr<storage::FileStorage>, std::shared_ptr<std::mutex>>> _cachedKtxFiles;
static std::mutex _cachedKtxFilesMutex;
std::string _filename; std::string _filename;
cache::FilePointer _cacheEntry; cache::FilePointer _cacheEntry;
std::atomic<uint8_t> _minMipLevelAvailable; std::atomic<uint8_t> _minMipLevelAvailable;

View file

@ -193,19 +193,22 @@ KtxStorage::KtxStorage(const std::string& filename) : _filename(filename) {
std::shared_ptr<storage::FileStorage> KtxStorage::maybeOpenFile() const { std::shared_ptr<storage::FileStorage> KtxStorage::maybeOpenFile() const {
if (!_cacheFile) { if (!_cacheFile) {
_cacheFile = std::make_shared<storage::FileStorage>(_filename.c_str()); _cacheFile = std::make_shared<storage::FileStorage>(_filename.c_str());
std::lock_guard<std::mutex> lock(KtxStorage::_cachedKtxFilesMutex); std::lock_guard<std::mutex> lock(_cachedKtxFilesMutex);
_cachedKtxFiles.emplace_back(_cacheFile, _cacheFileMutex); _cachedKtxFiles.emplace_back(_cacheFile, _cacheFileMutex);
} }
return _cacheFile; return _cacheFile;
} }
void KtxStorage::clearKtxFiles() { void KtxStorage::releaseOpenKtxFiles() {
std::lock_guard<std::mutex> lock(KtxStorage::_cachedKtxFilesMutex); std::vector<std::pair<std::shared_ptr<storage::FileStorage>, std::shared_ptr<std::mutex>>> localKtxFiles;
for (auto& cacheFileAndMutex : KtxStorage::_cachedKtxFiles) { {
std::lock_guard<std::mutex> lock(_cachedKtxFilesMutex);
localKtxFiles.swap(_cachedKtxFiles);
}
for (auto& cacheFileAndMutex : localKtxFiles) {
std::lock_guard<std::mutex> lock(*(cacheFileAndMutex.second)); std::lock_guard<std::mutex> lock(*(cacheFileAndMutex.second));
cacheFileAndMutex.first.reset(); cacheFileAndMutex.first.reset();
} }
_cachedKtxFiles.clear();
} }
PixelsPointer KtxStorage::getMipFace(uint16 level, uint8 face) const { PixelsPointer KtxStorage::getMipFace(uint16 level, uint8 face) const {