Merge pull request #78 from chrox/kctx_fix

bugfix: fix occasional reader crash when change screen mode from landscape to portrait in reflow mode
pull/80/merge
{Qingping,Dave} Hou 11 years ago
commit 4df8ac4d4d

@ -20,7 +20,7 @@ A global LRU cache
]]--
Cache = {
-- cache configuration:
max_memsize = 1024*1024*5, -- 5MB cache size
max_memsize = 1024*1024*15, -- 15MB cache size
-- cache state:
current_memsize = 0,
-- associative cache

@ -3,14 +3,7 @@ require "ui/geometry"
require "ui/device"
require "ui/reader/readerconfig"
KoptInterface = {
bg_context = {
contex = nil,
pageno = nil,
hash = nil,
cached = false,
},
}
KoptInterface = {}
function KoptInterface:waitForContext(kc)
-- if koptcontext is being processed in background thread
@ -21,20 +14,10 @@ function KoptInterface:waitForContext(kc)
end
end
function KoptInterface:consumeBgContext(doc)
-- clear up background context
self:waitForContext(self.bg_context.context)
if self.bg_context.context and not self.bg_context.cached then
self:makeCache(doc, self.bg_context.pageno, self.bg_context.hash)
self.bg_context.cached = true
end
end
-- get reflow context
function KoptInterface:getKOPTContext(doc, pageno, bbox)
-- since libk2pdfopt only has one bitmap buffer that holds reflowed page
-- we should consume background production before allocating new context.
self:consumeBgContext(doc)
-- Now koptcontext keeps track of its dst bitmap reflowed by libk2pdfopt.
-- So there is no need to check background context when creating new context.
local kc = KOPTContext.new()
local screen_size = Screen:getSize()
kc:setTrim(doc.configurable.trim_page)
@ -57,19 +40,6 @@ function KoptInterface:getKOPTContext(doc, pageno, bbox)
return kc
end
function KoptInterface:setTrimPage(doc, pageno)
if doc.configurable.trim_page == 0 then return end
local page_dimens = doc:getNativePageDimensions(pageno)
--DEBUG("original page dimens", page_dimens)
local orig_bbox = doc:getUsedBBox(pageno)
--DEBUG("original bbox", orig_bbox)
if orig_bbox.x1 - orig_bbox.x0 < page_dimens.w
or orig_bbox.y1 - orig_bbox.y0 < page_dimens.h then
doc.configurable.trim_page = 0
--DEBUG("Set manual crop in koptengine")
end
end
function KoptInterface:getContextHash(doc, pageno, bbox)
local screen_size = Screen:getSize()
local screen_size_hash = screen_size.w.."|"..screen_size.h
@ -109,50 +79,66 @@ function KoptInterface:getAutoBBox(doc, pageno)
end
end
function KoptInterface:getReflowedDim(kc)
self:waitForContext(kc)
return kc:getPageDim()
-- get reflowed page dimension from a cached context. wait for background thread
-- if necessary.
function KoptInterface:getReflowedDim(kctx)
self:waitForContext(kctx)
return kctx:getPageDim()
end
-- calculates page dimensions
function KoptInterface:getPageDimensions(doc, pageno, zoom, rotation)
self:setTrimPage(doc, pageno)
-- get cached koptcontext for centain page. if context doesn't exist in cache make
-- new context and reflow the src page immediatly.
function KoptInterface:getCachedContext(doc, pageno)
local bbox = doc:getPageBBox(pageno)
local context_hash = self:getContextHash(doc, pageno, bbox)
local hash = "kctx|"..context_hash
local cached = Cache:check(hash)
local kctx_hash = "kctx|"..context_hash
local cached = Cache:check(kctx_hash)
if not cached then
-- If kctx is not cached, create one and get reflowed bmp in foreground.
local kc = self:getKOPTContext(doc, pageno, bbox)
local page = doc._document:openPage(pageno)
-- reflow page
--local secs, usecs = util.gettime()
page:reflow(kc, 0)
page:close()
--local nsecs, nusecs = util.gettime()
--local dur = nsecs - secs + (nusecs - usecs) / 1000000
--DEBUG("Reflow duration:", dur)
--self:logReflowDuration(pageno, dur)
page:close()
local fullwidth, fullheight = kc:getPageDim()
DEBUG("page::reflowPage:", "fullwidth:", fullwidth, "fullheight:", fullheight)
local page_size = Geom:new{ w = fullwidth, h = fullheight }
-- cache reflowed page size and kc
Cache:insert(hash, CacheItem:new{ kctx = kc })
return page_size
DEBUG("reflowed page", pageno, "fullwidth:", fullwidth, "fullheight:", fullheight)
Cache:insert(kctx_hash, CacheItem:new{ kctx = kc })
return kc
else
return cached.kctx
end
--DEBUG("Found cached koptcontex on page", pageno, cached)
local fullwidth, fullheight = self:getReflowedDim(cached.kctx)
local page_size = Geom:new{ w = fullwidth, h = fullheight }
return page_size
end
function KoptInterface:makeCache(doc, pageno, context_hash)
-- draw to blitbuffer
local kc_hash = "kctx|"..context_hash
local tile_hash = "renderpg|"..context_hash
local page = doc._document:openPage(pageno)
local cached = Cache:check(kc_hash)
if cached then
local fullwidth, fullheight = self:getReflowedDim(cached.kctx)
-- get reflowed page dimensions
function KoptInterface:getPageDimensions(doc, pageno, zoom, rotation)
local kctx = self:getCachedContext(doc, pageno)
local fullwidth, fullheight = self:getReflowedDim(kctx)
return Geom:new{ w = fullwidth, h = fullheight }
end
-- inherited from common document interface
-- render reflowed page into tile cache.
function KoptInterface:renderPage(doc, pageno, rect, zoom, rotation, render_mode)
doc.render_mode = render_mode
local bbox = doc:getPageBBox(pageno)
local context_hash = self:getContextHash(doc, pageno, bbox)
local renderpg_hash = "renderpg|"..context_hash
local cached = Cache:check(renderpg_hash)
if not cached then
-- do the real reflowing if kctx is not been cached yet
local kctx = self:getCachedContext(doc, pageno)
local fullwidth, fullheight = self:getReflowedDim(kctx)
if not Cache:willAccept(fullwidth * fullheight / 2) then
-- whole page won't fit into cache
error("aborting, since we don't have enough cache for this page")
end
local page = doc._document:openPage(pageno)
-- prepare cache item with contained blitbuffer
local tile = CacheItem:new{
size = fullwidth * fullheight / 2 + 64, -- estimation
@ -160,71 +146,40 @@ function KoptInterface:makeCache(doc, pageno, context_hash)
pageno = pageno,
bb = Blitbuffer.new(fullwidth, fullheight)
}
page:rfdraw(cached.kctx, tile.bb)
page:rfdraw(kctx, tile.bb)
page:close()
--DEBUG("cached hash", hash)
if not Cache:check(tile_hash) then
Cache:insert(tile_hash, tile)
end
-- free dst bitmap in kctx as soon as we draw the bitmap to blitbuffer
kctx:free()
Cache:insert(renderpg_hash, tile)
return tile
end
DEBUG("Error: cannot render page before reflowing.")
end
function KoptInterface:renderPage(doc, pageno, rect, zoom, rotation, render_mode)
self:setTrimPage(doc, pageno)
doc.render_mode = render_mode
local bbox = doc:getPageBBox(pageno)
local context_hash = self:getContextHash(doc, pageno, bbox)
local hash = "renderpg|"..context_hash
local page_size = self:getPageDimensions(doc, pageno, zoom, rotation)
-- this will be the size we actually render
local size = page_size
-- we prefer to render the full page, if it fits into cache
if not Cache:willAccept(size.w * size.h / 2) then
-- whole page won't fit into cache
DEBUG("rendering only part of the page")
-- TODO: figure out how to better segment the page
if not rect then
DEBUG("aborting, since we do not have a specification for that part")
-- required part not given, so abort
return
end
-- only render required part
hash = "renderpg|"..doc.file.."|"..pageno.."|"..doc.configurable:hash("|").."|"..tostring(rect)
size = rect
end
local cached = Cache:check(hash)
if cached then
return cached
else
return self:makeCache(doc, pageno, context_hash)
return cached
end
end
-- inherited from common document interface
-- render reflowed page into cache in background thread. this method returns immediatly
-- leaving the precache flag on in context. subsequent usage of this context should
-- wait for the precache flag off by calling self:waitForContext(kctx)
function KoptInterface:hintPage(doc, pageno, zoom, rotation, gamma, render_mode)
self:setTrimPage(doc, pageno)
local bbox = doc:getPageBBox(pageno)
local context_hash = self:getContextHash(doc, pageno, bbox)
local hash = "kctx|"..context_hash
local cached = Cache:check(hash)
local kctx_hash = "kctx|"..context_hash
local cached = Cache:check(kctx_hash)
if not cached then
local kc = self:getKOPTContext(doc, pageno, bbox)
local page = doc._document:openPage(pageno)
kc:setPreCache()
self.bg_context.context = kc
self.bg_context.pageno = pageno
self.bg_context.hash = context_hash
self.bg_context.cached = false
DEBUG("hinting page", pageno, "in background")
-- will return immediately
-- reflow will return immediately and running in background thread
kc:setPreCache()
page:reflow(kc, 0)
page:close()
Cache:insert(hash, CacheItem:new{ kctx = kc })
Cache:insert(kctx_hash, CacheItem:new{ kctx = kc })
end
end
-- inherited from common document interface
-- draw cached tile pixels into target blitbuffer
function KoptInterface:drawPage(doc, target, x, y, rect, pageno, zoom, rotation, render_mode)
local tile = self:renderPage(doc, pageno, rect, zoom, rotation, render_mode)
--DEBUG("now painting", tile, rect)

@ -102,10 +102,11 @@ KoptOptions = {
item_text = {"Aa","Aa","Aa","Aa","Aa","Aa","Aa","Aa","Aa","Aa"},
item_align_center = 1.0,
spacing = 15,
height = 60,
item_font_size = {22,24,28,32,34,36,38,42,46,50},
values = {0.1, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.6, 2.0, 4.0},
default_value = 1.0,
height = 60,
event = "FontSizeUpdate",
},
{
name = "font_fine_tune",

@ -11,7 +11,9 @@ function ReaderFrontLight:init()
if dev_mod == "KindlePaperWhite" then
require "liblipclua"
self.lipc_handle = lipc.init("com.github.koreader")
self.intensity = self.lipc_handle:get_int_property("com.lab126.powerd", "flIntensity")
if self.lipc_handle then
self.intensity = self.lipc_handle:get_int_property("com.lab126.powerd", "flIntensity")
end
end
self.ges_events = {
Adjust = {
@ -29,7 +31,7 @@ function ReaderFrontLight:init()
end
function ReaderFrontLight:onAdjust(arg, ges)
if self.lipc_handle then
if self.lipc_handle and self.intensity ~=nil then
local rel_proportion = ges.distance / Screen:getWidth()
local delta_int = self.steps[math.ceil(#self.steps*rel_proportion)]
local msg = ""

@ -158,6 +158,7 @@ function ReaderPaging:onToggleFlipping()
end
self.ui:handleEvent(Event:new("SetFlippingMode", self.flipping_mode))
self.ui:handleEvent(Event:new("SetHinting", not self.flipping_mode))
self.ui:handleEvent(Event:new("ReZoom"))
UIManager:setDirty(self.view.dialog, "partial")
end

@ -304,9 +304,7 @@ end
function ReaderView:onToggleScrollMode(page_scroll)
self.page_scroll = page_scroll
self:recalculate()
if self.page_scroll then
self.ui:handleEvent(Event:new("InitScrollPageStates"))
end
self.ui:handleEvent(Event:new("InitScrollPageStates"))
end
function ReaderView:onReadSettings(config)
@ -360,19 +358,20 @@ function ReaderView:onGammaUpdate(gamma)
end
end
function ReaderView:onFontSizeUpdate()
self.ui:handleEvent(Event:new("ReZoom"))
end
function ReaderView:onDefectSizeUpdate()
self.ui:handleEvent(Event:new("ReZoom"))
self.ui:handleEvent(Event:new("InitScrollPageStates"))
end
function ReaderView:onPageCrop()
self.ui:handleEvent(Event:new("ReZoom"))
self.ui:handleEvent(Event:new("InitScrollPageStates"))
end
function ReaderView:onMarginUpdate()
self.ui:handleEvent(Event:new("ReZoom"))
self.ui:handleEvent(Event:new("InitScrollPageStates"))
end
function ReaderView:onSetViewMode(new_mode)

@ -157,6 +157,7 @@ end
function ReaderZooming:onReZoom()
self:setZoom()
self.ui:handleEvent(Event:new("InitScrollPageStates"))
return true
end

Loading…
Cancel
Save