| |
| |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| class THDCacheResourcePool |
| { |
| public: |
| THDCacheResourcePool() {} |
| ~THDCacheResourcePool() {} |
| }; |
| |
| |
| |
| |
| |
| inline bool TCacheResourcePool::isHDActive() |
| { |
| |
| return m_hdPool && m_hdPool->isActive(); |
| |
| return false; |
| |
| } |
| |
| |
| |
| void TCacheResourcePool::reset() |
| { |
| setPath("", "", ""); |
| } |
| |
| |
| |
| |
| |
| |
| |
| void TCacheResourcePool::invalidateAll() |
| { |
| QMutexLocker locker(&m_memMutex); |
| |
| MemResources::iterator it; |
| for (it = m_memResources.begin(); it != m_memResources.end(); ++it) |
| it->second->invalidate(); |
| } |
| |
| |
| |
| inline QString TCacheResourcePool::getPoolRoot(QString cacheRoot, QString projectName, QString sceneName) |
| { |
| return QString(cacheRoot + "/render/" + projectName + "/" + sceneName + "/"); |
| } |
| |
| |
| |
| |
| |
| |
| |
| void TCacheResourcePool::setPath(QString cacheRoot, QString projectName, QString sceneName) |
| { |
| |
| assert(m_memResources.empty()); |
| |
| |
| |
| |
| |
| invalidateAll(); |
| |
| delete m_hdPool; |
| m_hdPool = 0; |
| m_path = TFilePath(); |
| |
| |
| |
| if (!(cacheRoot.isEmpty() || projectName.isEmpty() || sceneName.isEmpty())) { |
| QString hdPoolRoot(getPoolRoot(cacheRoot, projectName, sceneName)); |
| m_hdPool = new THDCacheResourcePool(hdPoolRoot); |
| m_path = m_hdPool->getResourcesFilePath(); |
| } |
| |
| |
| } |
| |
| |
| |
| void TCacheResourcePool::startBacking(TCacheResource *resource) |
| { |
| assert(isHDActive()); |
| if (!isHDActive()) |
| return; |
| |
| |
| |
| resource->m_backEnabled = true; |
| |
| m_hdPool->buildBackingPath(resource); |
| |
| |
| } |
| |
| |
| |
| |
| |
| TCacheResourcePool *TCacheResourcePool::instance() |
| { |
| static TCacheResourcePool theInstance; |
| return &theInstance; |
| } |
| |
| |
| |
| TCacheResourcePool::TCacheResourcePool() |
| : m_memMutex(QMutex::Recursive), m_searchCount(0), m_foundIterator(false), m_searchIterator(m_memResources.end()), m_hdPool(0), m_path() |
| { |
| |
| } |
| |
| |
| |
| TCacheResourcePool::~TCacheResourcePool() |
| { |
| |
| |
| |
| delete m_hdPool; |
| } |
| |
| |
| |
| const TFilePath &TCacheResourcePool::getPath() const |
| { |
| return m_path; |
| } |
| |
| |
| |
| |
| |
| |
| |
| void TCacheResourcePool::beginCachedSearch() |
| { |
| m_memMutex.lock(); |
| m_searchCount++; |
| } |
| |
| |
| |
| |
| |
| void TCacheResourcePool::endCachedSearch() |
| { |
| if (--m_searchCount <= 0) { |
| m_foundIterator = false; |
| m_searchIterator = m_memResources.end(); |
| } |
| m_memMutex.unlock(); |
| } |
| |
| |
| |
| |
| |
| TCacheResource *TCacheResourcePool::getResource(const std::string &name, bool createIfNone) |
| { |
| |
| |
| |
| TCacheResource *result = 0; |
| |
| |
| beginCachedSearch(); |
| |
| |
| if (m_searchIterator == m_memResources.end()) { |
| m_searchIterator = m_memResources.lower_bound(name); |
| if (m_searchIterator != m_memResources.end()) |
| if (!(name < m_searchIterator->first)) |
| m_foundIterator = true; |
| else if (m_searchIterator != m_memResources.begin()) |
| m_searchIterator--; |
| } |
| |
| if (m_foundIterator) { |
| result = m_searchIterator->second; |
| |
| endCachedSearch(); |
| return result; |
| } |
| |
| { |
| QString resourcePath; |
| QString resourceFlags; |
| |
| if (isHDActive()) { |
| |
| |
| |
| |
| |
| |
| ReadQuery query(m_hdPool); |
| |
| bool ret = query.prepare( |
| "SELECT Path, Flags FROM Resources WHERE Name = '" + QString::fromStdString(name) + "';"); |
| |
| |
| assert(ret); |
| |
| if (query.step()) { |
| resourcePath = query.value(0); |
| resourceFlags = query.value(1); |
| } |
| |
| |
| } |
| |
| if (!resourcePath.isEmpty() || createIfNone) { |
| TCacheResource *result = new TCacheResource; |
| result->m_pos = m_searchIterator = |
| m_memResources.insert(m_searchIterator, std::make_pair(name, result)); |
| |
| |
| |
| |
| |
| if (isHDActive()) |
| m_hdPool->loadResourceInfos(result, resourcePath); |
| |
| |
| m_foundIterator = true; |
| endCachedSearch(); |
| |
| return result; |
| } |
| } |
| |
| endCachedSearch(); |
| return 0; |
| } |
| |
| |
| |
| void TCacheResourcePool::releaseResource(TCacheResource *resource) |
| { |
| QMutexLocker locker(&m_memMutex); |
| |
| |
| |
| |
| if (resource->m_refCount > 0) |
| return; |
| |
| |
| QMutexLocker flushLocker(isHDActive() ? &m_hdPool->m_flushMutex : 0); |
| |
| if (isHDActive()) { |
| |
| m_hdPool->flushResources(); |
| |
| |
| m_hdPool->saveResourceInfos(resource); |
| } |
| |
| |
| m_memResources.erase(resource->m_pos); |
| delete resource; |
| } |
| |