rename runtimectl to document/canvascontext

pull/4721/head
Qingping Hou 5 years ago committed by Frans de Jonge
parent adb5d5a52c
commit 1605409c60

@ -7,8 +7,8 @@ local lfs = require("libs/libkoreader-lfs")
local logger = require("logger")
local md5 = require("ffi/MD5")
local runtimectl = require("runtimectl")
if runtimectl.should_restrict_JIT then
local CanvasContext = require("document/canvascontext")
if CanvasContext.should_restrict_JIT then
require("jit").off(true, true)
end

@ -27,6 +27,18 @@ local Device = Generic:new{
hasClipboard = yes,
hasColorScreen = yes,
hasOTAUpdates = canUpdateApk,
--[[
Disable jit on some modules on android to make koreader on Android more stable.
The strategy here is that we only use precious mcode memory (jitting)
on deep loops like the several blitting methods in blitbuffer.lua and
the pixel-copying methods in mupdf.lua. So that a small amount of mcode
memory (64KB) allocated when koreader is launched in the android.lua
is enough for the program and it won't need to jit other parts of lua
code and thus won't allocate mcode memory any more which by our
observation will be harder and harder as we run koreader.
]]--
should_restrict_JIT = true,
}
function Device:init()

@ -9,6 +9,7 @@ local Device = {
charging_mode = false,
survive_screen_saver = false,
is_cover_closed = false,
should_restrict_JIT = false,
model = nil,
powerd = nil,
screen = nil,

@ -0,0 +1,61 @@
local Mupdf = require("ffi/mupdf")
local CanvasContext = {
should_restrict_JIT = false,
is_color_rendering_enabled = false,
is_bgr = false,
}
--[[
Initialize CanvasContext with settings from device.
The following key is required for a device object:
* should_restrict_JIT: bool
* hasBGRFrameBuffer: function() -> boolean
* screen: object with following methods:
* getWidth() -> int
* getHeight() -> int
* getDPI() -> int
* getSize() -> Rect
* scaleBySize(int) -> int
]]--
function CanvasContext:init(device)
self.screen = device.screen
self.isAndroid = device.isAndroid
self.isKindle = device.isKindle
self.should_restrict_JIT = device.should_restrict_JIT
-- NOTE: Kobo's fb is BGR, not RGB. Handle the conversion in MuPDF if needed.
if device:hasBGRFrameBuffer() then
self.is_bgr = true
Mupdf.bgr = true
end
end
function CanvasContext:setColorRenderingEnabled(val)
self.is_color_rendering_enabled = val
end
function CanvasContext:getWidth()
return self.screen:getWidth()
end
function CanvasContext:getHeight()
return self.screen:getHeight()
end
function CanvasContext:getDPI()
return self.screen:getDPI()
end
function CanvasContext:getSize()
return self.screen:getSize()
end
function CanvasContext:scaleBySize(px)
return self.screen:scaleBySize(px)
end
return CanvasContext

@ -4,7 +4,7 @@ local Document = require("document/document")
local Font = require("ui/font")
local Geom = require("ui/geometry")
local RenderImage = require("ui/renderimage")
local Runtimectl = require("runtimectl")
local CanvasContext = require("document/canvascontext")
local ffi = require("ffi")
local C = ffi.C
local lfs = require("libs/libkoreader-lfs")
@ -116,7 +116,7 @@ function CreDocument:init()
self._view_mode = DCREREADER_VIEW_MODE == "scroll" and self.SCROLL_VIEW_MODE or self.PAGE_VIEW_MODE
local ok
ok, self._document = pcall(cre.newDocView, Runtimectl:getRenderWidth(), Runtimectl:getRenderHeight(), self._view_mode)
ok, self._document = pcall(cre.newDocView, CanvasContext:getWidth(), CanvasContext:getHeight(), self._view_mode)
if not ok then
error(self._document) -- will contain error message
end
@ -163,8 +163,8 @@ function CreDocument:setupDefaultView()
self._document:setStringProperty("crengine.font.fallback.face",
G_reader_settings:readSetting("fallback_font") or self.fallback_font)
-- adjust font sizes according to dpi set in runtime
self._document:adjustFontSizes(Runtimectl:getRenderDPI())
-- adjust font sizes according to dpi set in canvas context
self._document:adjustFontSizes(CanvasContext:getDPI())
-- set top status bar font size
if G_reader_settings:readSetting("cre_header_status_font_size") then
@ -191,7 +191,7 @@ function CreDocument:render()
-- load document before rendering
self:loadDocument()
-- set visible page count in landscape
if math.max(Runtimectl:getRenderWidth(), Runtimectl:getRenderHeight()) / Runtimectl:getRenderDPI()
if math.max(CanvasContext:getWidth(), CanvasContext:getHeight()) / CanvasContext:getDPI()
< DCREREADER_TWO_PAGE_THRESHOLD then
self:setVisiblePageCount(1)
end

@ -3,7 +3,7 @@ local Cache = require("cache")
local CacheItem = require("cacheitem")
local Configurable = require("configurable")
local DrawContext = require("ffi/drawcontext")
local Runtimectl = require("runtimectl")
local CanvasContext = require("document/canvascontext")
local Geom = require("ui/geometry")
local Math = require("optmath")
local TileCacheItem = require("document/tilecacheitem")
@ -291,7 +291,7 @@ function Document:findText()
end
function Document:updateColorRendering()
if self.is_color_capable and Runtimectl.is_color_rendering_enabled then
if self.is_color_capable and CanvasContext.is_color_rendering_enabled then
self.render_color = true
else
self.render_color = false

@ -9,7 +9,7 @@ local TileCacheItem = require("document/tilecacheitem")
local logger = require("logger")
local serial = require("serialize")
local util = require("ffi/util")
local Runtimectl = require("runtimectl")
local CanvasContext = require("document/canvascontext")
local KoptInterface = {
ocrengine = "ocrengine",
@ -88,7 +88,7 @@ function KoptInterface:createContext(doc, pageno, bbox)
-- 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 = Runtimectl:getRenderSize()
local canvas_size = CanvasContext:getSize()
local lang = doc.configurable.doc_language
if lang == "chi_sim" or lang == "chi_tra" or
lang == "jpn" or lang == "kor" then
@ -99,8 +99,8 @@ function KoptInterface:createContext(doc, pageno, bbox)
kc:setWrap(doc.configurable.text_wrap)
kc:setIndent(doc.configurable.detect_indent)
kc:setColumns(doc.configurable.max_columns)
kc:setDeviceDim(screen_size.w, screen_size.h)
kc:setDeviceDPI(Runtimectl:getRenderDPI())
kc:setDeviceDim(canvas_size.w, canvas_size.h)
kc:setDeviceDPI(CanvasContext:getDPI())
kc:setStraighten(doc.configurable.auto_straighten)
kc:setJustification(doc.configurable.justification)
kc:setWritingDirection(doc.configurable.writing_direction)
@ -124,11 +124,11 @@ function KoptInterface:createContext(doc, pageno, bbox)
end
function KoptInterface:getContextHash(doc, pageno, bbox)
local screen_size = Runtimectl:getRenderSize()
local screen_size_hash = screen_size.w.."|"..screen_size.h
local canvas_size = CanvasContext:getSize()
local canvas_size_hash = canvas_size.w.."|"..canvas_size.h
local bbox_hash = bbox.x0.."|"..bbox.y0.."|"..bbox.x1.."|"..bbox.y1
return doc.file.."|"..doc.mod_time.."|"..pageno.."|"
..doc.configurable:hash("|").."|"..bbox_hash.."|"..screen_size_hash
..doc.configurable:hash("|").."|"..bbox_hash.."|"..canvas_size_hash
end
function KoptInterface:getPageBBox(doc, pageno)
@ -276,8 +276,8 @@ get first page image
--]]
function KoptInterface:getCoverPageImage(doc)
local native_size = Document.getNativePageDimensions(doc, 1)
local screen_size = Runtimectl:getRenderSize()
local zoom = math.min(screen_size.w / native_size.w, screen_size.h / native_size.h)
local canvas_size = CanvasContext:getSize()
local zoom = math.min(canvas_size.w / native_size.w, canvas_size.h / native_size.h)
local tile = Document.renderPage(doc, 1, nil, zoom, 0, 1, 0)
if tile then
return tile.bb:copy()
@ -571,9 +571,8 @@ function KoptInterface:getPageBlock(doc, pageno, x, y)
y1 = page_size.h,
}
local kc = self:createContext(doc, pageno, full_page_bbox)
local screen_size = Runtimectl:getRenderSize()
-- leptonica needs a source image of at least 300dpi
kc:setZoom(screen_size.w / page_size.w * 300 / Runtimectl:getRenderDPI())
kc:setZoom(CanvasContext:getWidth() / page_size.w * 300 / CanvasContext:getDPI())
local page = doc._document:openPage(pageno)
page:getPagePix(kc)
kc:findPageBlocks()
@ -938,8 +937,8 @@ function KoptInterface:getLinkFromPosition(doc, pageno, pos)
pos = self:reflowToNativePosTransform(doc, pageno, pos, {x=0.5, y=0.5})
end
local offset = Runtimectl:scaleByRenderSize(5)
local len = Runtimectl:scaleByRenderSize(10)
local offset = CanvasContext:scaleBySize(5)
local len = CanvasContext:scaleBySize(10)
for i = 1, #page_links do
local link = page_links[i]
-- enlarge tappable link box

@ -7,7 +7,7 @@ local util = require("util")
local ffi = require("ffi")
local C = ffi.C
local pdf = nil
local Runtimectl = require("runtimectl")
local CanvasContext = require("document/canvascontext")
local PdfDocument = Document:new{
@ -37,8 +37,10 @@ function PdfDocument:init()
error(self._document) -- will contain error message
end
-- no-op on PDF
self._document:layoutDocument(Runtimectl:getRenderWidth(), Runtimectl:getRenderHeight(),
Runtimectl:scaleByRenderSize(self.epub_font_size))
self._document:layoutDocument(
CanvasContext:getWidth(),
CanvasContext:getHeight(),
CanvasContext:scaleBySize(self.epub_font_size))
self.is_open = true
self.info.has_pages = true
self.info.configurable = true

@ -1,6 +1,6 @@
local Document = require("document/document")
local DrawContext = require("ffi/drawcontext")
local Runtimectl = require("runtimectl")
local CanvasContext = require("document/canvascontext")
local pic = nil
local PicDocument = Document:new{
@ -15,7 +15,7 @@ function PicDocument:init()
self:updateColorRendering()
if not pic then pic = require("ffi/pic") end
-- pic.color needs to be true before opening document to allow toggling color
pic.color = Runtimectl.is_color_rendering_enabled
pic.color = CanvasContext.is_color_rendering_enabled
local ok
ok, self._document = pcall(pic.openDocument, self.file)
if not ok then

@ -1,85 +0,0 @@
local Mupdf = require("ffi/mupdf")
local Runtimectl = {
should_restrict_JIT = false,
is_color_rendering_enabled = false,
is_bgr = false,
}
--[[
Initialize runtimectl with settings from device.
The following key is required for a device object:
* hasBGRFrameBuffer: function() -> boolean
* screen: object with following methods:
* getWidth() -> int
* getHeight() -> int
* getDPI() -> int
* getSize() -> Rect
* scaleBySize(int) -> int
]]--
function Runtimectl:init(device)
self.screen = device.screen
self.isAndroid = device.isAndroid
self.isKindle = device.isKindle
if self.isAndroid() then
self:restrictJIT()
end
-- NOTE: Kobo's fb is BGR, not RGB. Handle the conversion in MuPDF if needed.
if device:hasBGRFrameBuffer() then
self.is_bgr = true
Mupdf.bgr = true
end
end
--[[
Disable jit on some modules on android to make koreader on Android more stable.
The strategy here is that we only use precious mcode memory (jitting)
on deep loops like the several blitting methods in blitbuffer.lua and
the pixel-copying methods in mupdf.lua. So that a small amount of mcode
memory (64KB) allocated when koreader is launched in the android.lua
is enough for the program and it won't need to jit other parts of lua
code and thus won't allocate mcode memory any more which by our
observation will be harder and harder as we run koreader.
]]--
function Runtimectl:restrictJIT()
self.should_restrict_JIT = true
end
function Runtimectl:setColorRenderingEnabled(val)
self.is_color_rendering_enabled = val
end
function Runtimectl:getExternalFontDir()
if self.isAndroid() then
return ANDROID_FONT_DIR
else
return os.getenv("EXT_FONT_DIR")
end
end
function Runtimectl:getRenderWidth()
return self.screen:getWidth()
end
function Runtimectl:getRenderHeight()
return self.screen:getHeight()
end
function Runtimectl:getRenderDPI()
return self.screen:getDPI()
end
function Runtimectl:getRenderSize()
return self.screen:getSize()
end
function Runtimectl:scaleByRenderSize(px)
return self.screen:scaleBySize(px)
end
return Runtimectl

@ -1,7 +1,7 @@
local Event = require("ui/event")
local Screen = require("device").screen
local UIManager = require("ui/uimanager")
local Runtimectl = require("runtimectl")
local CanvasContext = require("document/canvascontext")
local _ = require("gettext")
return {
@ -10,7 +10,7 @@ return {
checked_func = Screen.isColorEnabled,
callback = function()
local new_val = Screen.isColorEnabled()
Runtimectl:setColorRenderingEnabled(new_val)
CanvasContext:setColorRenderingEnabled(new_val)
G_reader_settings:saveSetting("color_rendering", new_val)
UIManager:broadcastEvent(Event:new("ColorRenderingUpdate"))
end

@ -5,7 +5,7 @@ Font module.
local Freetype = require("ffi/freetype")
local lfs = require("libs/libkoreader-lfs")
local logger = require("logger")
local Runtimectl = require("runtimectl")
local CanvasContext = require("document/canvascontext")
local Font = {
fontmap = {
@ -98,7 +98,7 @@ function Font:getFace(font, size)
if not size then size = self.sizemap[font] end
-- original size before scaling by screen DPI
local orig_size = size
size = Runtimectl:scaleByRenderSize(size)
size = CanvasContext:scaleBySize(size)
local hash = font..size
local face_obj = self.faces[hash]
@ -188,7 +188,7 @@ local kindle_fonts_blacklist = {
local function isInFontsBlacklist(f)
-- write test for this
return Runtimectl.isKindle() and kindle_fonts_blacklist[f]
return CanvasContext.isKindle() and kindle_fonts_blacklist[f]
end
function Font:_readList(target, dir)
@ -212,11 +212,19 @@ function Font:_readList(target, dir)
end
end
local function getExternalFontDir()
if CanvasContext.isAndroid() then
return ANDROID_FONT_DIR
else
return os.getenv("EXT_FONT_DIR")
end
end
function Font:getFontList()
local fontlist = {}
self:_readList(fontlist, self.fontdir)
-- multiple paths should be joined with semicolon
for dir in string.gmatch(Runtimectl:getExternalFontDir() or "", "([^;]+)") do
for dir in string.gmatch(getExternalFontDir() or "", "([^;]+)") do
self:_readList(fontlist, dir)
end
table.sort(fontlist)

@ -8,8 +8,8 @@ local CacheItem = require("cacheitem")
local BlitBuffer = require("ffi/blitbuffer")
local logger = require("logger")
local runtimectl = require("runtimectl")
if runtimectl.should_restrict_JIT then
local CanvasContext = require("document/canvascontext")
if CanvasContext.should_restrict_JIT then
require("jit").off(true, true)
end

@ -33,8 +33,8 @@ local WidgetContainer = require("ui/widget/container/widgetcontainer")
local _ = require("gettext")
local Screen = require("device").screen
local runtimectl = require("runtimectl")
if runtimectl.should_restrict_JIT then
local CanvasContext = require("document/canvascontext")
if CanvasContext.should_restrict_JIT then
require("jit").off(true, true)
end

@ -16,8 +16,8 @@ It handles event propagation and painting (with different alignments) for its ch
local Geom = require("ui/geometry")
local Widget = require("ui/widget/widget")
local runtimectl = require("runtimectl")
if runtimectl.should_restrict_JIT then
local CanvasContext = require("document/canvascontext")
if CanvasContext.should_restrict_JIT then
require("jit").off(true, true)
end

@ -34,22 +34,20 @@ if lang_locale then
_.changeLang(lang_locale)
end
-- setup various runtime control
local Runtimectl = require("runtimectl")
local Device = require("device")
Runtimectl:init(Device)
if G_reader_settings:has("color_rendering") then
Runtimectl:setColorRenderingEnabled(G_reader_settings:isTrue("color_rendering"))
else
Runtimectl:setColorRenderingEnabled(Device.screen.isColorScreen())
end
local dpi_override = G_reader_settings:readSetting("screen_dpi")
if dpi_override ~= nil then
Device.screen:setDPI(dpi_override)
end
local CanvasContext = require("document/canvascontext")
CanvasContext:init(Device)
if G_reader_settings:has("color_rendering") then
CanvasContext:setColorRenderingEnabled(G_reader_settings:isTrue("color_rendering"))
else
CanvasContext:setColorRenderingEnabled(Device.screen.isColorScreen())
end
-- option parsing:
local longopts = {
debug = "d",

@ -4,7 +4,7 @@ describe("AutoFrontlight widget tests", function()
setup(function()
require("commonrequire")
package.unloadAll()
require("runtimectl"):init(require("device"))
require("document/canvascontext"):init(require("device"))
MockTime = require("mock_time")
MockTime:install()
@ -23,7 +23,7 @@ describe("AutoFrontlight widget tests", function()
teardown(function()
MockTime:uninstall()
package.unloadAll()
require("runtimectl"):init(require("device"))
require("document/canvascontext"):init(require("device"))
end)
before_each(function()

@ -2,7 +2,7 @@ describe("AutoSuspend widget tests", function()
setup(function()
require("commonrequire")
package.unloadAll()
require("runtimectl"):init(require("device"))
require("document/canvascontext"):init(require("device"))
end)
before_each(function()

@ -4,7 +4,7 @@ describe("BackgroundRunner widget tests", function()
setup(function()
require("commonrequire")
package.unloadAll()
require("runtimectl"):init(require("device"))
require("document/canvascontext"):init(require("device"))
-- Device needs to be loaded before UIManager.
Device = require("device")
Device.input.waitEvent = function() end
@ -19,7 +19,7 @@ describe("BackgroundRunner widget tests", function()
teardown(function()
MockTime:uninstall()
package.unloadAll()
require("runtimectl"):init(require("device"))
require("document/canvascontext"):init(require("device"))
stopBackgroundRunner()
end)

@ -15,7 +15,7 @@ describe("BackgroundTaskPlugin", function()
teardown(function()
MockTime:uninstall()
package.unloadAll()
require("runtimectl"):init(require("device"))
require("document/canvascontext"):init(require("device"))
stopBackgroundRunner()
end)

@ -8,7 +8,7 @@ describe("BatteryState plugin tests #nocov", function()
setup(function()
require("commonrequire")
package.unloadAll()
require("runtimectl"):init(require("device"))
require("document/canvascontext"):init(require("device"))
MockTime = require("mock_time")
MockTime:install()
end)
@ -16,7 +16,7 @@ describe("BatteryState plugin tests #nocov", function()
teardown(function()
MockTime:uninstall()
package.unloadAll()
require("runtimectl"):init(require("device"))
require("document/canvascontext"):init(require("device"))
end)
before_each(function()

@ -18,8 +18,8 @@ einkfb.dummy = true --luacheck: ignore
local Device = require("device")
local Runtimectl = require("runtimectl")
Runtimectl:init(Device)
local CanvasContext = require("document/canvascontext")
CanvasContext:init(Device)
-- init output device
local Screen = Device.screen

@ -20,7 +20,7 @@ describe("device module", function()
}
require("commonrequire")
package.unloadAll()
require("runtimectl"):init(require("device"))
require("document/canvascontext"):init(require("device"))
end)
before_each(function()

@ -3,7 +3,7 @@ describe("FileManager module", function()
setup(function()
require("commonrequire")
package.unloadAll()
require("runtimectl"):init(require("device"))
require("document/canvascontext"):init(require("device"))
FileManager = require("apps/filemanager/filemanager")
Screen = require("device").screen
UIManager = require("ui/uimanager")

@ -6,7 +6,7 @@ describe("Readerfooter module", function()
setup(function()
require("commonrequire")
package.unloadAll()
require("runtimectl"):init(require("device"))
require("document/canvascontext"):init(require("device"))
DocumentRegistry = require("document/documentregistry")
DocSettings = require("docsettings")
ReaderUI = require("apps/reader/readerui")

@ -3,7 +3,7 @@ describe("Readerhighlight module", function()
setup(function()
require("commonrequire")
package.unloadAll()
require("runtimectl"):init(require("device"))
require("document/canvascontext"):init(require("device"))
DocumentRegistry = require("document/documentregistry")
Event = require("ui/event")
Geom = require("ui/geometry")

@ -4,7 +4,7 @@ describe("ReaderLink module", function()
setup(function()
require("commonrequire")
package.unloadAll()
require("runtimectl"):init(require("device"))
require("document/canvascontext"):init(require("device"))
DocumentRegistry = require("document/documentregistry")
Event = require("ui/event")
ReaderUI = require("apps/reader/readerui")

@ -4,7 +4,7 @@ describe("Readerview module", function()
setup(function()
require("commonrequire")
package.unloadAll()
require("runtimectl"):init(require("device"))
require("document/canvascontext"):init(require("device"))
DocumentRegistry = require("document/documentregistry")
Blitbuffer = require("ffi/blitbuffer")
ReaderUI = require("apps/reader/readerui")

Loading…
Cancel
Save