GLFont doCacheGC() in one pass

This commit is contained in:
vsonnier 2016-06-20 22:06:36 +02:00
parent 92221bccdd
commit 26bf1d9927
1 changed files with 24 additions and 10 deletions

View File

@ -17,6 +17,9 @@ static std::wstring getExePath(void)
#define RES_FOLDER "" #define RES_FOLDER ""
#endif #endif
#define GC_PERIOD 50
#define GC_DRAW_COUNT_LIMIT 10
GLFontStringCache::GLFontStringCache() { GLFontStringCache::GLFontStringCache() {
gc = 0; gc = 0;
} }
@ -512,7 +515,7 @@ void GLFont::drawString(const std::wstring& str, float xpos, float ypos, int pxH
std::lock_guard<std::mutex> lock(cache_busy); std::lock_guard<std::mutex> lock(cache_busy);
if (gcCounter > 50) { if (gcCounter > GC_PERIOD) {
doCacheGC(); doCacheGC();
gcCounter = 0; gcCounter = 0;
@ -759,19 +762,30 @@ GLFontStringCache *GLFont::cacheString(const std::wstring& str, int pxHeight, in
} }
void GLFont::doCacheGC() { void GLFont::doCacheGC() {
std::map<std::wstring, GLFontStringCache * >::iterator cache_iter; std::map<std::wstring, GLFontStringCache * >::iterator cache_iter;
for (cache_iter = stringCache.begin(); cache_iter != stringCache.end(); cache_iter++) { bool flushDone = false;
//do aging and remove in one pass.
cache_iter = stringCache.begin();
while (cache_iter != stringCache.end()) {
//aging
cache_iter->second->gc--; cache_iter->second->gc--;
}
for (cache_iter = stringCache.begin(); cache_iter != stringCache.end(); cache_iter++) { //only flush 1 element per call
if (cache_iter->second->gc < -10) { if (!flushDone && cache_iter->second->gc < -GC_DRAW_COUNT_LIMIT) {
// std::cout << "gc'd " << cache_iter->first << std::endl;
delete cache_iter->second; delete cache_iter->second;
stringCache.erase(cache_iter); cache_iter = stringCache.erase(cache_iter);
return; flushDone = true;
} }
} else {
cache_iter++;
}
} //end while
} }
void GLFont::flushGC() { void GLFont::flushGC() {