| |
| |
| #include "trenderer.h" |
| #include "trasterfx.h" |
| |
| #include <algorithm> |
| |
| #include "tpredictivecachemanager.h" |
| |
| |
| |
| |
| |
| class TPredictiveCacheManagerGenerator final |
| : public TRenderResourceManagerGenerator { |
| public: |
| TPredictiveCacheManagerGenerator() : TRenderResourceManagerGenerator(true) {} |
| |
| TRenderResourceManager *operator()(void) override { |
| return new TPredictiveCacheManager; |
| } |
| }; |
| |
| MANAGER_FILESCOPE_DECLARATION_DEP(TPredictiveCacheManager, |
| TPredictiveCacheManagerGenerator, |
| TFxCacheManager::deps()) |
| |
| |
| |
| TPredictiveCacheManager *TPredictiveCacheManager::instance() { |
| return static_cast<TPredictiveCacheManager *>( |
| |
| TPredictiveCacheManager::gen()->getManager(TRenderer::renderId())); |
| } |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| struct PredictionData { |
| const ResourceDeclaration *m_decl; |
| int m_usageCount; |
| |
| PredictionData(const ResourceDeclaration *declaration) |
| : m_decl(declaration), m_usageCount(1) {} |
| }; |
| |
| |
| |
| |
| |
| |
| |
| class TPredictiveCacheManager::Imp { |
| public: |
| int m_renderStatus; |
| bool m_enabled; |
| |
| std::map<TCacheResourceP, PredictionData> m_resources; |
| QMutex m_mutex; |
| |
| public: |
| Imp() |
| : m_renderStatus(TRenderer::IDLE) |
| , m_enabled(TRenderer::instance().isPrecomputingEnabled()) {} |
| |
| void run(TCacheResourceP &resource, const std::string &alias, const TFxP &fx, |
| double frame, const TRenderSettings &rs, |
| ResourceDeclaration *resData) { |
| switch (m_renderStatus) { |
| case TRenderer::IDLE: |
| case TRenderer::COMPUTING: |
| getResourceComputing(resource, alias, fx, frame, rs, resData); |
| break; |
| case TRenderer::TESTRUN: |
| getResourceTestRun(resource, alias, fx, frame, rs, resData); |
| break; |
| } |
| } |
| |
| private: |
| void getResourceTestRun(TCacheResourceP &resource, const std::string &alias, |
| const TFxP &fx, double frame, |
| const TRenderSettings &rs, |
| ResourceDeclaration *resData); |
| |
| void getResourceComputing(TCacheResourceP &resource, const std::string &alias, |
| const TFxP &fx, double frame, |
| const TRenderSettings &rs, |
| ResourceDeclaration *resData); |
| }; |
| |
| |
| |
| |
| |
| TPredictiveCacheManager::TPredictiveCacheManager() |
| : m_imp(new TPredictiveCacheManager::Imp()) {} |
| |
| |
| |
| TPredictiveCacheManager::~TPredictiveCacheManager() {} |
| |
| |
| |
| void TPredictiveCacheManager::setMaxTileSize(int maxTileSize) {} |
| |
| |
| |
| void TPredictiveCacheManager::setBPP(int bpp) {} |
| |
| |
| |
| void TPredictiveCacheManager::getResource(TCacheResourceP &resource, |
| const std::string &alias, |
| const TFxP &fx, double frame, |
| const TRenderSettings &rs, |
| ResourceDeclaration *resData) { |
| if (!m_imp->m_enabled) return; |
| |
| m_imp->run(resource, alias, fx, frame, rs, resData); |
| } |
| |
| |
| |
| |
| |
| void TPredictiveCacheManager::Imp::getResourceTestRun( |
| TCacheResourceP &resource, const std::string &alias, const TFxP &fx, |
| double frame, const TRenderSettings &rs, ResourceDeclaration *resData) { |
| assert(resData && resData->m_rawData); |
| if (!(resData && resData->m_rawData)) |
| |
| |
| |
| |
| |
| |
| |
| return; |
| |
| if (!resource) resource = TCacheResourceP(alias, true); |
| |
| |
| |
| |
| |
| std::map<TCacheResourceP, PredictionData>::iterator it = |
| m_resources.find(resource); |
| |
| if (it != m_resources.end()) |
| it->second.m_usageCount++; |
| else { |
| |
| m_resources.insert(std::make_pair(resource, PredictionData(resData))).first; |
| } |
| } |
| |
| |
| |
| void TPredictiveCacheManager::Imp::getResourceComputing( |
| TCacheResourceP &resource, const std::string &alias, const TFxP &fx, |
| double frame, const TRenderSettings &rs, ResourceDeclaration *resData) { |
| |
| |
| |
| |
| if (!resData) return; |
| |
| |
| |
| |
| |
| if (!resource) resource = TCacheResourceP(alias); |
| |
| if (!resource) return; |
| |
| |
| QMutexLocker locker(&m_mutex); |
| |
| std::map<TCacheResourceP, PredictionData>::iterator it = |
| m_resources.find(resource); |
| |
| if (it == m_resources.end()) return; |
| |
| if (--it->second.m_usageCount <= 0) m_resources.erase(it); |
| } |
| |
| |
| |
| void TPredictiveCacheManager::onRenderStatusStart(int renderStatus) { |
| m_imp->m_renderStatus = renderStatus; |
| } |
| |
| |
| |
| void TPredictiveCacheManager::onRenderStatusEnd(int renderStatus) { |
| switch (renderStatus) { |
| case TRenderer::TESTRUN: |
| |
| |
| |
| |
| |
| std::map<TCacheResourceP, PredictionData>::iterator it; |
| for (it = m_imp->m_resources.begin(); it != m_imp->m_resources.end();) { |
| const ResourceDeclaration *decl = it->second.m_decl; |
| |
| if (decl->m_tiles.size() == 1 && decl->m_tiles[0].m_refCount == 1) { |
| std::map<TCacheResourceP, PredictionData>::iterator jt = it++; |
| m_imp->m_resources.erase(jt); |
| } else |
| it++; |
| } |
| } |
| } |
| |