From 01f045e7f99dff3752cd9342197297406f88cc38 Mon Sep 17 00:00:00 2001 From: chrox Date: Sun, 24 Mar 2013 18:37:33 +0800 Subject: [PATCH] add standalone glyph cache I found it will be especially helpful to cache glyph separately so that glyph caches won't be washed out by a single page cache. --- frontend/cache.lua | 9 ++++++++- frontend/ui/rendertext.lua | 14 +++++++++++--- 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/frontend/cache.lua b/frontend/cache.lua index 9d51b2b42..b19dc144f 100644 --- a/frontend/cache.lua +++ b/frontend/cache.lua @@ -1,3 +1,4 @@ +require "dbg" --[[ Inheritable abstraction for cache items ]]-- @@ -29,8 +30,14 @@ Cache = { cache_order = {} } +function Cache:new(o) + o = o or {} + setmetatable(o, self) + self.__index = self + return o +end + function Cache:insert(key, object) - --@TODO add cache for different types of item 09.01 2013 (houqp) -- guarantee that we have enough memory in cache if(object.size > self.max_memsize) then -- we're not allowed to claim this much at all diff --git a/frontend/ui/rendertext.lua b/frontend/ui/rendertext.lua index aefc1983c..8f9ff9e4c 100644 --- a/frontend/ui/rendertext.lua +++ b/frontend/ui/rendertext.lua @@ -4,11 +4,19 @@ require "cache" TODO: all these functions should probably be methods on Face objects ]]-- +GlyphCache = Cache:new{ + max_memsize = 512*1024, + current_memsize = 0, + cache = {}, + -- this will hold the LRU order of the cache + cache_order = {} +} + function getGlyph(face, charcode, bgcolor, fgcolor) if bgcolor == nil then bgcolor = 0.0 end if fgcolor == nil then fgcolor = 1.0 end local hash = "glyph|"..face.hash.."|"..charcode.."|"..bgcolor.."|"..fgcolor - local glyph = Cache:check(hash) + local glyph = GlyphCache:check(hash) if glyph then -- cache hit return glyph[1] @@ -20,8 +28,8 @@ function getGlyph(face, charcode, bgcolor, fgcolor) end glyph = CacheItem:new{rendered_glyph} glyph.size = glyph[1].bb:getWidth() * glyph[1].bb:getHeight() / 2 + 32 - Cache:insert(hash, glyph) - return glyph[1] + GlyphCache:insert(hash, glyph) + return rendered_glyph end function getSubTextByWidth(text, face, width, kerning)