mirror of https://github.com/koreader/koreader
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.pull/2/merge
parent
a07ea2f8b8
commit
d96666f4b1
@ -1,64 +1,51 @@
|
|||||||
--[[
|
--[[
|
||||||
a cache for rendered tiles
|
a cache for rendered tiles
|
||||||
]]--
|
]]--
|
||||||
|
cache_max_memsize = 1024*1024*5 -- 5MB tile cache
|
||||||
function init_tilecache()
|
cache_current_memsize = 0
|
||||||
cache = {
|
cache = {}
|
||||||
{ age = 0, no = 0, bb = blitbuffer.new(width, height), dc = pdf.newDC(), page = nil },
|
cache_max_age = 20
|
||||||
{ age = 0, no = 0, bb = blitbuffer.new(width, height), dc = pdf.newDC(), page = nil },
|
function cacheclaim(size)
|
||||||
{ age = 0, no = 0, bb = blitbuffer.new(width, height), dc = pdf.newDC(), page = nil }
|
if(size > cache_max_memsize) then
|
||||||
}
|
error("too much memory claimed")
|
||||||
end
|
return false
|
||||||
function freecache()
|
end
|
||||||
for i = 1, #cache do
|
repeat
|
||||||
if cache[i].page ~= nil then
|
for k, v in pairs(cache) do
|
||||||
print("freeing slot="..i.." oldpage="..cache[i].no)
|
if v.age > 0 then
|
||||||
cache[i].page:close()
|
print("aging slot="..k)
|
||||||
cache[i].page = nil
|
v.age = v.age - 1
|
||||||
|
else
|
||||||
|
cache_current_memsize = cache_current_memsize - v.size
|
||||||
|
cache[k] = nil
|
||||||
|
break -- out of for loop
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
function checkcache(no)
|
function draworcache(no, zoom, offset_x, offset_y, width, height, gamma)
|
||||||
for i = 1, #cache do
|
local hash = cachehash(no, zoom, offset_x, offset_y, width, height, gamma)
|
||||||
if cache[i].no == no and cache[i].page ~= nil then
|
if cache[hash] == nil then
|
||||||
print("cache hit: slot="..i.." page="..no)
|
cacheclaim(width * height / 2);
|
||||||
return i
|
cache[hash] = {
|
||||||
end
|
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
|
end
|
||||||
print("cache miss")
|
return hash
|
||||||
return nil
|
|
||||||
end
|
end
|
||||||
function cacheslot()
|
function cachehash(no, zoom, offset_x, offset_y, width, height, gamma)
|
||||||
freeslot = nil
|
return no..'_'..zoom..'_'..offset_x..','..offset_y..'-'..width..'x'..height..'_'..gamma;
|
||||||
while freeslot == nil do
|
|
||||||
for i = 1, #cache do
|
|
||||||
if cache[i].age > 0 then
|
|
||||||
print("aging slot="..i)
|
|
||||||
cache[i].age = cache[i].age - 1
|
|
||||||
else
|
|
||||||
if cache[i].page ~= nil then
|
|
||||||
print("freeing slot="..i.." oldpage="..cache[i].no)
|
|
||||||
cache[i].page:close()
|
|
||||||
cache[i].page = nil
|
|
||||||
end
|
|
||||||
freeslot = i
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
print("returning free slot="..freeslot)
|
|
||||||
return freeslot
|
|
||||||
end
|
end
|
||||||
|
function clearcache()
|
||||||
function draworcache(no)
|
cache = {}
|
||||||
local slot = checkcache(no)
|
|
||||||
if slot == nil then
|
|
||||||
slot = cacheslot()
|
|
||||||
cache[slot].no = no
|
|
||||||
cache[slot].age = #cache
|
|
||||||
cache[slot].page = doc:openPage(no)
|
|
||||||
setzoom(slot)
|
|
||||||
print("drawing page="..no.." to slot="..slot)
|
|
||||||
cache[slot].page:draw(cache[slot].dc, cache[slot].bb, 0, 0)
|
|
||||||
end
|
|
||||||
return slot
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue