Files
koreader/tilecache.lua
HW d96666f4b1 new cache organization in preparation for more complex tile data
in order to use the tile cache for variable zoomed tiles, we need more
flexibility. thus, now cache tiles are hashed (well, in fact it's a
concatenation) of their metadata.

also, we close pages right after opening them - there was no re-use
before and now we have opening and closing at one place. this should
also make it easier for the garbage collector.
2011-11-20 22:36:31 +01:00

52 lines
1.4 KiB
Lua
Executable File

--[[
a cache for rendered tiles
]]--
cache_max_memsize = 1024*1024*5 -- 5MB tile cache
cache_current_memsize = 0
cache = {}
cache_max_age = 20
function cacheclaim(size)
if(size > cache_max_memsize) then
error("too much memory claimed")
return false
end
repeat
for k, v in pairs(cache) do
if v.age > 0 then
print("aging slot="..k)
v.age = v.age - 1
else
cache_current_memsize = cache_current_memsize - v.size
cache[k] = nil
break -- out of for loop
end
end
until cache_current_memsize + size <= cache_max_memsize
cache_current_memsize = cache_current_memsize + size
print("cleaned cache to fit new tile (size="..size..")")
return true
end
function draworcache(no, zoom, offset_x, offset_y, width, height, gamma)
local hash = cachehash(no, zoom, offset_x, offset_y, width, height, gamma)
if cache[hash] == nil then
cacheclaim(width * height / 2);
cache[hash] = {
age = cache_max_age,
size = width * height / 2,
bb = blitbuffer.new(width, height)
}
print("drawing page="..no.." to slot="..hash)
local page = doc:openPage(no)
local dc = setzoom(page, hash)
page:draw(dc, cache[hash].bb, 0, 0)
page:close()
end
return hash
end
function cachehash(no, zoom, offset_x, offset_y, width, height, gamma)
return no..'_'..zoom..'_'..offset_x..','..offset_y..'-'..width..'x'..height..'_'..gamma;
end
function clearcache()
cache = {}
end