From 92ec4213c09cfc0d8b4815459c4173deb88ab23c Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Sat, 31 Mar 2012 12:53:40 +0200 Subject: [PATCH 1/9] show djvu cache size #80 Currently it shows cache_max_memsize and cache_item_max_pixels from lua and djvu cache size on menu key --- djvu.c | 9 +++++++++ unireader.lua | 9 ++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/djvu.c b/djvu.c index 6bcac2290..b4296b560 100644 --- a/djvu.c +++ b/djvu.c @@ -447,6 +447,14 @@ static int drawPage(lua_State *L) { return 0; } +static int getCacheSize(lua_State *L) { + DjvuDocument *doc = (DjvuDocument*) luaL_checkudata(L, 1, "djvudocument"); + unsigned long size = ddjvu_cache_get_size(doc->context); + printf("ddjvu_cache_get_size = %d\n", size); + lua_pushnumber(L, size); + return 1; +} + static const struct luaL_Reg djvu_func[] = { {"openDocument", openDocument}, {NULL, NULL} @@ -458,6 +466,7 @@ static const struct luaL_Reg djvudocument_meth[] = { {"getTOC", getTableOfContent}, {"getPageText", getPageText}, {"close", closeDocument}, + {"getCacheSize", getCacheSize}, {"__gc", closeDocument}, {NULL, NULL} }; diff --git a/unireader.lua b/unireader.lua index b2cb982a1..a13a2ab7f 100644 --- a/unireader.lua +++ b/unireader.lua @@ -126,6 +126,13 @@ function UniReader:toggleTextHighLight(word_list) return end +---------------------------------------------------- +-- renderer memory +---------------------------------------------------- + +function UniReader:getCacheSize() + return -1 +end --[ following are default methods ]-- @@ -799,7 +806,7 @@ function UniReader:showMenu() end renderUtf8Text(fb.bb, 10, ypos+6, face, fhash, "Page: "..self.pageno.."/"..self.doc:getPages().. - " "..cur_section, true) + " "..cur_section.." Memory: "..( self.cache_max_memsize / 1024 ).." "..( self.cache_item_max_pixels / 1024 ).." "..( self.doc:getCacheSize() / 1024 ).." k", true) ypos = ypos + 15 blitbuffer.progressBar(fb.bb, 10, ypos, width-20, 15, From 91bd1277d7c3da88a3faacb7928d0d57032ad9aa Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Sat, 31 Mar 2012 13:23:51 +0200 Subject: [PATCH 2/9] control renderer memory usage #80 added setCacheSize and cache_document_size configuration vairable --- djvu.c | 9 +++++++++ unireader.lua | 9 +++++++++ 2 files changed, 18 insertions(+) diff --git a/djvu.c b/djvu.c index b4296b560..5f8d7935b 100644 --- a/djvu.c +++ b/djvu.c @@ -455,6 +455,14 @@ static int getCacheSize(lua_State *L) { return 1; } +static int setCacheSize(lua_State *L) { + DjvuDocument *doc = (DjvuDocument*) luaL_checkudata(L, 1, "djvudocument"); + int size = luaL_checkint(L, 2); + printf("ddjvu_cache_set_size = %d\n", size); + ddjvu_cache_set_size(doc->context, size); + return 0; +} + static const struct luaL_Reg djvu_func[] = { {"openDocument", openDocument}, {NULL, NULL} @@ -467,6 +475,7 @@ static const struct luaL_Reg djvudocument_meth[] = { {"getPageText", getPageText}, {"close", closeDocument}, {"getCacheSize", getCacheSize}, + {"setCacheSize", setCacheSize}, {"__gc", closeDocument}, {NULL, NULL} }; diff --git a/unireader.lua b/unireader.lua index a13a2ab7f..7ad8a6a6f 100644 --- a/unireader.lua +++ b/unireader.lua @@ -72,6 +72,8 @@ UniReader = { -- tile cache state: cache_current_memsize = 0, cache = {}, + -- renderer cache size + cache_document_size = 1024*1024*8, -- FIXME random, needs testing pagehash = nil, @@ -134,6 +136,11 @@ function UniReader:getCacheSize() return -1 end +function UniReader:setCacheSize(size) + return +end + + --[ following are default methods ]-- function UniReader:loadSettings(filename) @@ -158,6 +165,8 @@ function UniReader:loadSettings(filename) self.globalzoom = self.settings:readSetting("globalzoom") or 1.0 self.globalzoommode = self.settings:readSetting("globalzoommode") or -1 + self.doc:setCacheSize( self.cache_document_size ) + return true end return false From 60b7ccb3cb389fe266796f7ca0a766752cf047e2 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Sat, 31 Mar 2012 13:38:33 +0200 Subject: [PATCH 3/9] added cleanCache bound to C key in menu #80 --- djvu.c | 12 ++++++++++-- unireader.lua | 6 ++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/djvu.c b/djvu.c index 5f8d7935b..d60107920 100644 --- a/djvu.c +++ b/djvu.c @@ -450,7 +450,7 @@ static int drawPage(lua_State *L) { static int getCacheSize(lua_State *L) { DjvuDocument *doc = (DjvuDocument*) luaL_checkudata(L, 1, "djvudocument"); unsigned long size = ddjvu_cache_get_size(doc->context); - printf("ddjvu_cache_get_size = %d\n", size); + printf("## ddjvu_cache_get_size = %d\n", size); lua_pushnumber(L, size); return 1; } @@ -458,11 +458,18 @@ static int getCacheSize(lua_State *L) { static int setCacheSize(lua_State *L) { DjvuDocument *doc = (DjvuDocument*) luaL_checkudata(L, 1, "djvudocument"); int size = luaL_checkint(L, 2); - printf("ddjvu_cache_set_size = %d\n", size); + printf("## ddjvu_cache_set_size = %d\n", size); ddjvu_cache_set_size(doc->context, size); return 0; } +static int cleanCache(lua_State *L) { + DjvuDocument *doc = (DjvuDocument*) luaL_checkudata(L, 1, "djvudocument"); + printf("## ddjvu_cache_clear\n"); + ddjvu_cache_clear(doc->context); + return 0; +} + static const struct luaL_Reg djvu_func[] = { {"openDocument", openDocument}, {NULL, NULL} @@ -476,6 +483,7 @@ static const struct luaL_Reg djvudocument_meth[] = { {"close", closeDocument}, {"getCacheSize", getCacheSize}, {"setCacheSize", setCacheSize}, + {"cleanCache", cleanCache}, {"__gc", closeDocument}, {NULL, NULL} }; diff --git a/unireader.lua b/unireader.lua index 7ad8a6a6f..e23389aa4 100644 --- a/unireader.lua +++ b/unireader.lua @@ -140,6 +140,10 @@ function UniReader:setCacheSize(size) return end +function UniReader:cleanCache() + return +end + --[ following are default methods ]-- @@ -827,6 +831,8 @@ function UniReader:showMenu() if ev.type == EV_KEY and ev.value == EVENT_VALUE_KEY_PRESS then if ev.code == KEY_BACK or ev.code == KEY_MENU then return + elseif ev.code == KEY_C then + self.doc:cleanCache() end end end From 4f0b41ebc500abb52dfb3c657f6d0857ef5ce473 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Sat, 31 Mar 2012 13:57:35 +0200 Subject: [PATCH 4/9] show cache_current_memsize --- unireader.lua | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/unireader.lua b/unireader.lua index e23389aa4..d4b163c84 100644 --- a/unireader.lua +++ b/unireader.lua @@ -819,7 +819,11 @@ function UniReader:showMenu() end renderUtf8Text(fb.bb, 10, ypos+6, face, fhash, "Page: "..self.pageno.."/"..self.doc:getPages().. - " "..cur_section.." Memory: "..( self.cache_max_memsize / 1024 ).." "..( self.cache_item_max_pixels / 1024 ).." "..( self.doc:getCacheSize() / 1024 ).." k", true) + " "..cur_section.. + " Memory: ".. + math.ceil( self.cache_current_memsize / 1024 ).."/"..( self.cache_max_memsize / 1024 ).. + " "..( self.cache_item_max_pixels / 1024 ).." "..( self.doc:getCacheSize() / 1024 ).." k", + true) ypos = ypos + 15 blitbuffer.progressBar(fb.bb, 10, ypos, width-20, 15, From 3e83dcc2a4483b3c75d5d3bd3df01d804ee8348d Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Sat, 31 Mar 2012 14:09:50 +0200 Subject: [PATCH 5/9] pass cache_document_size to document open This is how mupdf API is structured --- pdf.c | 2 +- pdfreader.lua | 2 +- unireader.lua | 6 ++---- 3 files changed, 4 insertions(+), 6 deletions(-) diff --git a/pdf.c b/pdf.c index 5583e09a0..df92901d3 100644 --- a/pdf.c +++ b/pdf.c @@ -166,7 +166,7 @@ static int openDocument(lua_State *L) { char *filename = strdup(luaL_checkstring(L, 1)); int cachesize = luaL_optint(L, 2, 64 << 20); // 64 MB limit default char buf[15]; - printf("cachesize: %s\n",readable_fs(cachesize,buf)); + printf("## cachesize: %s\n",readable_fs(cachesize,buf)); PdfDocument *doc = (PdfDocument*) lua_newuserdata(L, sizeof(PdfDocument)); diff --git a/pdfreader.lua b/pdfreader.lua index ec4e1a105..9413e0127 100644 --- a/pdfreader.lua +++ b/pdfreader.lua @@ -8,7 +8,7 @@ function PDFReader:open(filename) -- muPDF manages its own cache, set second parameter -- to the maximum size you want it to grow local ok - ok, self.doc = pcall(pdf.openDocument, filename, 64*1024*1024) + ok, self.doc = pcall(pdf.openDocument, filename, self.cache_document_size) if not ok then return false, self.doc -- will contain error message end diff --git a/unireader.lua b/unireader.lua index d4b163c84..148c2acc5 100644 --- a/unireader.lua +++ b/unireader.lua @@ -107,7 +107,7 @@ end -- open a file and its settings store -- tips: you can use self:loadSettings in open() method. -function UniReader:open(filename, password) +function UniReader:open(filename, cache_size) return false end @@ -149,7 +149,7 @@ end function UniReader:loadSettings(filename) if self.doc ~= nil then - self.settings = DocSettings:open(filename) + self.settings = DocSettings:open(filename,self.cache_document_size) local gamma = self.settings:readSetting("gamma") if gamma then @@ -169,8 +169,6 @@ function UniReader:loadSettings(filename) self.globalzoom = self.settings:readSetting("globalzoom") or 1.0 self.globalzoommode = self.settings:readSetting("globalzoommode") or -1 - self.doc:setCacheSize( self.cache_document_size ) - return true end return false From 2754d996c4da3a4f69f56497fd41fcfb50e18fae Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Sat, 31 Mar 2012 14:32:36 +0200 Subject: [PATCH 6/9] move memory info to top of page This allows section name to be displayed on bottom, and it won't push memory info out of screen --- unireader.lua | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/unireader.lua b/unireader.lua index 148c2acc5..2ce78e177 100644 --- a/unireader.lua +++ b/unireader.lua @@ -817,15 +817,20 @@ function UniReader:showMenu() end renderUtf8Text(fb.bb, 10, ypos+6, face, fhash, "Page: "..self.pageno.."/"..self.doc:getPages().. - " "..cur_section.. - " Memory: ".. - math.ceil( self.cache_current_memsize / 1024 ).."/"..( self.cache_max_memsize / 1024 ).. - " "..( self.cache_item_max_pixels / 1024 ).." "..( self.doc:getCacheSize() / 1024 ).." k", - true) + " "..cur_section, true) ypos = ypos + 15 blitbuffer.progressBar(fb.bb, 10, ypos, width-20, 15, 5, 4, load_percent, 8) + + -- display memory on top of page + fb.bb:paintRect(0, 0, width, 15+6*2, 0) + renderUtf8Text(fb.bb, 10, 15+6, face, fhash, + "Memory: ".. + math.ceil( self.cache_current_memsize / 1024 ).."/"..( self.cache_max_memsize / 1024 ).. + " "..( self.cache_item_max_pixels / 1024 ).." "..( self.cache_document_size / 1024 ).." k", + true) + fb:refresh(1) while 1 do local ev = input.waitForEvent() From 5b1deedd869e18701f6504b421564020d61ae892 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Sat, 31 Mar 2012 14:36:10 +0200 Subject: [PATCH 7/9] remove setCacheSize set cache in openDocument --- djvu.c | 14 ++++---------- djvureader.lua | 2 +- 2 files changed, 5 insertions(+), 11 deletions(-) diff --git a/djvu.c b/djvu.c index d60107920..b8d170bef 100644 --- a/djvu.c +++ b/djvu.c @@ -71,7 +71,7 @@ static int handle(lua_State *L, ddjvu_context_t *ctx, int wait) static int openDocument(lua_State *L) { const char *filename = luaL_checkstring(L, 1); - /*const char *password = luaL_checkstring(L, 2);*/ + int cache_size = luaL_checkint(L, 2); DjvuDocument *doc = (DjvuDocument*) lua_newuserdata(L, sizeof(DjvuDocument)); luaL_getmetatable(L, "djvudocument"); @@ -82,6 +82,9 @@ static int openDocument(lua_State *L) { return luaL_error(L, "cannot create context."); } + printf("## cache_size = %d\n", cache_size); + ddjvu_cache_set_size(doc->context, cache_size); + doc->doc_ref = ddjvu_document_create_by_filename_utf8(doc->context, filename, TRUE); while (! ddjvu_document_decoding_done(doc->doc_ref)) handle(L, doc->context, True); @@ -455,14 +458,6 @@ static int getCacheSize(lua_State *L) { return 1; } -static int setCacheSize(lua_State *L) { - DjvuDocument *doc = (DjvuDocument*) luaL_checkudata(L, 1, "djvudocument"); - int size = luaL_checkint(L, 2); - printf("## ddjvu_cache_set_size = %d\n", size); - ddjvu_cache_set_size(doc->context, size); - return 0; -} - static int cleanCache(lua_State *L) { DjvuDocument *doc = (DjvuDocument*) luaL_checkudata(L, 1, "djvudocument"); printf("## ddjvu_cache_clear\n"); @@ -482,7 +477,6 @@ static const struct luaL_Reg djvudocument_meth[] = { {"getPageText", getPageText}, {"close", closeDocument}, {"getCacheSize", getCacheSize}, - {"setCacheSize", setCacheSize}, {"cleanCache", cleanCache}, {"__gc", closeDocument}, {NULL, NULL} diff --git a/djvureader.lua b/djvureader.lua index cd0ad9885..be72ec4c0 100644 --- a/djvureader.lua +++ b/djvureader.lua @@ -6,7 +6,7 @@ DJVUReader = UniReader:new{} -- DJVU does not support password yet function DJVUReader:open(filename) local ok - ok, self.doc = pcall(djvu.openDocument, filename) + ok, self.doc = pcall(djvu.openDocument, filename, self.cache_document_size) if not ok then return ok, self.doc -- this will be the error message instead end From b8941eee415922bf9226d3e3843cafce39525d71 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Sat, 31 Mar 2012 14:44:09 +0200 Subject: [PATCH 8/9] cleanup cache_size based on coding guidelines --- pdf.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pdf.c b/pdf.c index df92901d3..ffa94be2e 100644 --- a/pdf.c +++ b/pdf.c @@ -164,16 +164,16 @@ fz_alloc_context my_alloc_default = static int openDocument(lua_State *L) { char *filename = strdup(luaL_checkstring(L, 1)); - int cachesize = luaL_optint(L, 2, 64 << 20); // 64 MB limit default + int cache_size = luaL_optint(L, 2, 64 << 20); // 64 MB limit default char buf[15]; - printf("## cachesize: %s\n",readable_fs(cachesize,buf)); + printf("## cache_size: %s\n",readable_fs(cache_size,buf)); PdfDocument *doc = (PdfDocument*) lua_newuserdata(L, sizeof(PdfDocument)); luaL_getmetatable(L, "pdfdocument"); lua_setmetatable(L, -2); - doc->context = fz_new_context(&my_alloc_default, NULL, cachesize); + doc->context = fz_new_context(&my_alloc_default, NULL, cache_size); fz_try(doc->context) { doc->xref = fz_open_document(doc->context, filename); From 5884f69b60d43a28c33d573b5f5848df3e5fec59 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Sat, 31 Mar 2012 15:10:20 +0200 Subject: [PATCH 9/9] setCacheSize cleanup --- unireader.lua | 4 ---- 1 file changed, 4 deletions(-) diff --git a/unireader.lua b/unireader.lua index 2ce78e177..939e6848a 100644 --- a/unireader.lua +++ b/unireader.lua @@ -136,10 +136,6 @@ function UniReader:getCacheSize() return -1 end -function UniReader:setCacheSize(size) - return -end - function UniReader:cleanCache() return end