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()
|
|
||||||
for i = 1, #cache do
|
|
||||||
if cache[i].page ~= nil then
|
|
||||||
print("freeing slot="..i.." oldpage="..cache[i].no)
|
|
||||||
cache[i].page:close()
|
|
||||||
cache[i].page = nil
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
function checkcache(no)
|
|
||||||
for i = 1, #cache do
|
|
||||||
if cache[i].no == no and cache[i].page ~= nil then
|
|
||||||
print("cache hit: slot="..i.." page="..no)
|
|
||||||
return i
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
print("cache miss")
|
repeat
|
||||||
return nil
|
for k, v in pairs(cache) do
|
||||||
end
|
if v.age > 0 then
|
||||||
function cacheslot()
|
print("aging slot="..k)
|
||||||
freeslot = nil
|
v.age = v.age - 1
|
||||||
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
|
else
|
||||||
if cache[i].page ~= nil then
|
cache_current_memsize = cache_current_memsize - v.size
|
||||||
print("freeing slot="..i.." oldpage="..cache[i].no)
|
cache[k] = nil
|
||||||
cache[i].page:close()
|
break -- out of for loop
|
||||||
cache[i].page = nil
|
|
||||||
end
|
|
||||||
freeslot = i
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
until cache_current_memsize + size <= cache_max_memsize
|
||||||
print("returning free slot="..freeslot)
|
cache_current_memsize = cache_current_memsize + size
|
||||||
return freeslot
|
print("cleaned cache to fit new tile (size="..size..")")
|
||||||
|
return true
|
||||||
end
|
end
|
||||||
|
function draworcache(no, zoom, offset_x, offset_y, width, height, gamma)
|
||||||
function draworcache(no)
|
local hash = cachehash(no, zoom, offset_x, offset_y, width, height, gamma)
|
||||||
local slot = checkcache(no)
|
if cache[hash] == nil then
|
||||||
if slot == nil then
|
cacheclaim(width * height / 2);
|
||||||
slot = cacheslot()
|
cache[hash] = {
|
||||||
cache[slot].no = no
|
age = cache_max_age,
|
||||||
cache[slot].age = #cache
|
size = width * height / 2,
|
||||||
cache[slot].page = doc:openPage(no)
|
bb = blitbuffer.new(width, height)
|
||||||
setzoom(slot)
|
}
|
||||||
print("drawing page="..no.." to slot="..slot)
|
print("drawing page="..no.." to slot="..hash)
|
||||||
cache[slot].page:draw(cache[slot].dc, cache[slot].bb, 0, 0)
|
local page = doc:openPage(no)
|
||||||
|
local dc = setzoom(page, hash)
|
||||||
|
page:draw(dc, cache[hash].bb, 0, 0)
|
||||||
|
page:close()
|
||||||
end
|
end
|
||||||
return slot
|
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
|
end
|
||||||
|
Loading…
Reference in New Issue