2014-04-30 15:24:44 +00:00
|
|
|
local Blitbuffer = require("ffi/blitbuffer")
|
2013-10-18 20:38:07 +00:00
|
|
|
local CacheItem = require("cacheitem")
|
2021-05-03 03:20:14 +00:00
|
|
|
local Persist = require("persist")
|
2016-12-29 08:10:38 +00:00
|
|
|
local logger = require("logger")
|
2013-10-18 20:38:07 +00:00
|
|
|
|
|
|
|
local TileCacheItem = CacheItem:new{}
|
|
|
|
|
|
|
|
function TileCacheItem:onFree()
|
2021-05-04 21:13:24 +00:00
|
|
|
logger.dbg("TileCacheItem: free blitbuffer", self.bb)
|
|
|
|
self.bb:free()
|
2013-10-18 20:38:07 +00:00
|
|
|
end
|
|
|
|
|
2021-05-03 03:20:14 +00:00
|
|
|
--- @note: Perhaps one day we'll be able to teach bitser or string.buffer about custom structs with pointers to buffers,
|
|
|
|
--- so we won't have to do the BB tostring/fromstring dance anymore...
|
|
|
|
function TileCacheItem:totable()
|
|
|
|
local t = {
|
|
|
|
size = self.size,
|
|
|
|
pageno = self.pageno,
|
|
|
|
excerpt = self.excerpt,
|
|
|
|
persistent = self.persistent,
|
|
|
|
bb = {
|
|
|
|
w = self.bb.w,
|
|
|
|
h = self.bb.h,
|
|
|
|
stride = tonumber(self.bb.stride),
|
|
|
|
fmt = self.bb:getType(),
|
|
|
|
data = Blitbuffer.tostring(self.bb),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return t
|
|
|
|
end
|
|
|
|
|
2014-04-30 15:24:44 +00:00
|
|
|
function TileCacheItem:dump(filename)
|
2021-05-03 03:20:14 +00:00
|
|
|
logger.dbg("Dumping tile cache to", filename, self.excerpt)
|
|
|
|
|
|
|
|
local cache_file = Persist:new{
|
|
|
|
path = filename,
|
|
|
|
codec = "zstd",
|
|
|
|
}
|
|
|
|
|
|
|
|
local ok, size = cache_file:save(self:totable())
|
|
|
|
if ok then
|
|
|
|
return size
|
|
|
|
else
|
|
|
|
logger.warn("Failed to dump tile cache")
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function TileCacheItem:fromtable(t)
|
|
|
|
self.size = t.size
|
|
|
|
self.pageno = t.pageno
|
|
|
|
self.excerpt = t.excerpt
|
|
|
|
self.persistent = t.persistent
|
|
|
|
self.bb = Blitbuffer.fromstring(t.bb.w, t.bb.h, t.bb.fmt, t.bb.data, t.bb.stride)
|
2014-04-30 15:24:44 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
function TileCacheItem:load(filename)
|
2021-05-03 03:20:14 +00:00
|
|
|
local cache_file = Persist:new{
|
|
|
|
path = filename,
|
|
|
|
codec = "zstd",
|
|
|
|
}
|
|
|
|
|
|
|
|
local t = cache_file:load(filename)
|
|
|
|
if t then
|
|
|
|
self:fromtable(t)
|
|
|
|
|
|
|
|
logger.dbg("Loaded tile cache from", filename, self)
|
|
|
|
else
|
|
|
|
logger.warn("Failed to load tile cache from", filename)
|
|
|
|
end
|
2014-04-30 15:24:44 +00:00
|
|
|
end
|
|
|
|
|
2013-10-18 20:38:07 +00:00
|
|
|
return TileCacheItem
|