18#include "activeframepool.h"
23ActiveFramePool::ActiveFramePool()
25 Q_ASSERT(mMemoryBudgetInBytes >= (1024 * 1024 * 100));
28ActiveFramePool::~ActiveFramePool()
33void ActiveFramePool::put(
KeyFrame* key)
38 Q_ASSERT(key->pos() > 0);
42 auto it = mCacheFramesMap.find(key);
43 const bool keyExistsInPool = (it != mCacheFramesMap.end());
47 mCacheFramesList.erase(it->second);
49 mCacheFramesList.push_front(key);
50 mCacheFramesMap[key] = mCacheFramesList.begin();
52 key->addEventListener(
this);
56 mTotalUsedMemory += key->memoryUsage();
59 discardLeastUsedFrames();
62void ActiveFramePool::clear()
64 for (
KeyFrame* key : mCacheFramesList)
66 key->removeEventListner(
this);
68 mCacheFramesList.clear();
69 mCacheFramesMap.clear();
72void ActiveFramePool::resize(quint64 memoryBudget)
74 memoryBudget = qMin(memoryBudget, quint64(1024) * 1024 * 1024 * 16);
75 memoryBudget = qMax(memoryBudget, quint64(1024) * 1024 * 100);
76 mMemoryBudgetInBytes = memoryBudget;
77 discardLeastUsedFrames();
80bool ActiveFramePool::isFrameInPool(
KeyFrame* key)
82 auto it = mCacheFramesMap.find(key);
83 return (it != mCacheFramesMap.end());
86void ActiveFramePool::setMinFrameCount(
size_t frameCount)
88 mMinFrameCount = frameCount;
91void ActiveFramePool::onKeyFrameDestroy(
KeyFrame* key)
93 auto it = mCacheFramesMap.find(key);
94 if (it != mCacheFramesMap.end())
96 mCacheFramesList.erase(it->second);
97 mCacheFramesMap.erase(it);
101 recalcuateTotalUsedMemory();
105void ActiveFramePool::discardLeastUsedFrames()
107 while ((mTotalUsedMemory > mMemoryBudgetInBytes) && (mCacheFramesList.size() > mMinFrameCount))
109 list_iterator_t last = mCacheFramesList.end();
113 unloadFrame(lastKeyFrame);
115 mCacheFramesMap.erase(lastKeyFrame);
116 mCacheFramesList.pop_back();
118 lastKeyFrame->removeEventListner(
this);
122void ActiveFramePool::unloadFrame(
KeyFrame* key)
124 mTotalUsedMemory -= key->memoryUsage();
128void ActiveFramePool::recalcuateTotalUsedMemory()
130 mTotalUsedMemory = 0;
131 for (
KeyFrame* key : mCacheFramesList)
133 mTotalUsedMemory += key->memoryUsage();