Reduce memory need

This commit is contained in:
jpirnay
2026-05-08 17:29:59 +02:00
parent 98e7c14c16
commit 13e87ff470
5 changed files with 34 additions and 13 deletions
+10
View File
@@ -182,7 +182,10 @@ void HomeActivity::loadRecentBooks(int maxBooks) {
if (!sidecar.empty()) { if (!sidecar.empty()) {
const bool sidecarAlreadyStored = const bool sidecarAlreadyStored =
book.coverBmpPath == sidecar || book.coverBmpPath.find("sidecar_") != std::string::npos; book.coverBmpPath == sidecar || book.coverBmpPath.find("sidecar_") != std::string::npos;
LOG_DBG("HOME", "Sidecar for %s: stored=%s alreadyStored=%d", book.path.c_str(), book.coverBmpPath.c_str(),
sidecarAlreadyStored ? 1 : 0);
if (!sidecarAlreadyStored) { if (!sidecarAlreadyStored) {
LOG_DBG("HOME", "Updating coverBmpPath to sidecar: %s", sidecar.c_str());
RECENT_BOOKS.updateBook(book.path, book.title, book.author, book.series, sidecar); RECENT_BOOKS.updateBook(book.path, book.title, book.author, book.series, sidecar);
RecentBook updated = book; RecentBook updated = book;
updated.coverBmpPath = sidecar; updated.coverBmpPath = sidecar;
@@ -208,7 +211,9 @@ void HomeActivity::loadRecentCovers(int coverHeight) {
const bool isSidecar = const bool isSidecar =
FsHelpers::hasJpgExtension(book.coverBmpPath) || FsHelpers::hasPngExtension(book.coverBmpPath); FsHelpers::hasJpgExtension(book.coverBmpPath) || FsHelpers::hasPngExtension(book.coverBmpPath);
if (isSidecar) { if (isSidecar) {
LOG_DBG("HOME", "Converting sidecar %s for book %s", book.coverBmpPath.c_str(), book.path.c_str());
if (!Storage.exists(book.coverBmpPath.c_str())) { if (!Storage.exists(book.coverBmpPath.c_str())) {
LOG_ERR("HOME", "Sidecar file missing: %s", book.coverBmpPath.c_str());
RECENT_BOOKS.updateBook(book.path, book.title, book.author, book.series, ""); RECENT_BOOKS.updateBook(book.path, book.title, book.author, book.series, "");
book.coverBmpPath = ""; book.coverBmpPath = "";
} else { } else {
@@ -229,6 +234,7 @@ void HomeActivity::loadRecentCovers(int coverHeight) {
if (convertSidecarToBmp(book.path, book.coverBmpPath, w, coverHeight, name).empty()) success = false; if (convertSidecarToBmp(book.path, book.coverBmpPath, w, coverHeight, name).empty()) success = false;
} }
if (success) { if (success) {
LOG_DBG("HOME", "Sidecar converted, placeholder: %s", placeholder.c_str());
RECENT_BOOKS.updateBook(book.path, book.title, book.author, book.series, placeholder); RECENT_BOOKS.updateBook(book.path, book.title, book.author, book.series, placeholder);
book.coverBmpPath = placeholder; book.coverBmpPath = placeholder;
} else { } else {
@@ -374,6 +380,7 @@ void HomeActivity::onEnter() {
void HomeActivity::onExit() { void HomeActivity::onExit() {
Activity::onExit(); Activity::onExit();
freeCoverBuffer(); freeCoverBuffer();
UITheme::getInstance().getMutableTheme().invalidateFrameCache();
} }
bool HomeActivity::storeCoverBuffer() { bool HomeActivity::storeCoverBuffer() {
@@ -564,6 +571,9 @@ void HomeActivity::render(RenderLock&&) {
if (!firstRenderDone) { if (!firstRenderDone) {
firstRenderDone = true; firstRenderDone = true;
requestUpdate(); requestUpdate();
} else if (!recentsLoaded && !recentsLoading) {
recentsLoading = true;
loadRecentCovers(getHomeCoverRenderHeight(computeHomeScreenLayout(metrics, contentRect.height, menuCount)));
} }
} }
+6 -1
View File
@@ -62,8 +62,13 @@ std::string ReaderActivity::sidecarCoverPath(const std::string& bookPath) {
const std::string base = bookPath.substr(0, dot); const std::string base = bookPath.substr(0, dot);
for (const char* ext : {".jpg", ".jpeg", ".png", ".bmp"}) { for (const char* ext : {".jpg", ".jpeg", ".png", ".bmp"}) {
const std::string candidate = base + ext; const std::string candidate = base + ext;
if (Storage.exists(candidate.c_str())) return candidate; LOG_DBG("SIDECAR", "Checking: %s", candidate.c_str());
if (Storage.exists(candidate.c_str())) {
LOG_DBG("SIDECAR", "Found sidecar cover: %s", candidate.c_str());
return candidate;
} }
}
LOG_DBG("SIDECAR", "No sidecar found for: %s", bookPath.c_str());
return ""; return "";
} }
+4
View File
@@ -193,6 +193,10 @@ class BaseTheme {
// Only one of epub/xtc/txt will be non-null depending on the reader. // Only one of epub/xtc/txt will be non-null depending on the reader.
virtual void onBookWillClose(const std::string& path, Epub* epub, Xtc* xtc, Txt* txt) {} virtual void onBookWillClose(const std::string& path, Epub* epub, Xtc* xtc, Txt* txt) {}
// Called when HomeActivity exits. Themes that hold heap-allocated render caches
// should free them here so the memory is available to child activities.
virtual void invalidateFrameCache() {}
// ---- Shared constants and helpers for battery drawing (used by all themes) ---- // ---- Shared constants and helpers for battery drawing (used by all themes) ----
static constexpr int batteryPercentSpacing = 4; static constexpr int batteryPercentSpacing = 4;
static void drawBatteryOutline(const GfxRenderer& renderer, int x, int y, int battWidth, int rectHeight); static void drawBatteryOutline(const GfxRenderer& renderer, int x, int y, int battWidth, int rectHeight);
@@ -90,9 +90,9 @@ const uint8_t* iconBitmapFor(UIIcon icon) {
// home after settings doesn't re-read covers from SD. // home after settings doesn't re-read covers from SD.
// Freed explicitly via invalidateFrameCache() before entering the reader. // Freed explicitly via invalidateFrameCache() before entering the reader.
// --------------------------------------------------------------------------- // ---------------------------------------------------------------------------
constexpr int kFrameCount = 3; constexpr int kFrameCount = 1;
uint8_t* gCachedFrames[kFrameCount] = {}; uint8_t* gCachedFrames[kFrameCount] = {};
int gCachedFrameBookIdx[kFrameCount] = {-1, -1, -1}; int gCachedFrameBookIdx[kFrameCount] = {-1};
int gCachedFrameCount = 0; int gCachedFrameCount = 0;
std::string gCacheKey; std::string gCacheKey;
@@ -102,14 +102,8 @@ int findFrameSlot(int bookIdx) {
} }
return -1; return -1;
} }
} // namespace
// --------------------------------------------------------------------------- void freeFrameCache() {
// Static helpers
// ---------------------------------------------------------------------------
void LyraCarouselTheme::setPreRenderIndex(int idx) { lastCarouselSelectorIndex = idx; }
void LyraCarouselTheme::invalidateFrameCache() const {
for (int i = 0; i < kFrameCount; ++i) { for (int i = 0; i < kFrameCount; ++i) {
if (gCachedFrames[i]) { if (gCachedFrames[i]) {
free(gCachedFrames[i]); free(gCachedFrames[i]);
@@ -120,6 +114,14 @@ void LyraCarouselTheme::invalidateFrameCache() const {
gCachedFrameCount = 0; gCachedFrameCount = 0;
gCacheKey.clear(); gCacheKey.clear();
} }
} // namespace
// ---------------------------------------------------------------------------
// Static helpers
// ---------------------------------------------------------------------------
void LyraCarouselTheme::setPreRenderIndex(int idx) { lastCarouselSelectorIndex = idx; }
void LyraCarouselTheme::invalidateFrameCache() { freeFrameCache(); }
void LyraCarouselTheme::onBookWillClose(const std::string& /*path*/, Epub* epub, Xtc* xtc, Txt* /*txt*/) { void LyraCarouselTheme::onBookWillClose(const std::string& /*path*/, Epub* epub, Xtc* xtc, Txt* /*txt*/) {
if (epub) { if (epub) {
@@ -212,13 +214,13 @@ bool LyraCarouselTheme::tryFastHomeRender(GfxRenderer& renderer, const std::vect
if (!framesReady) { if (!framesReady) {
// Free old cache and allocate fresh frames // Free old cache and allocate fresh frames
invalidateFrameCache(); freeFrameCache();
const int frameCount = std::min(bookCount, kFrameCount); const int frameCount = std::min(bookCount, kFrameCount);
for (int i = 0; i < frameCount; ++i) { for (int i = 0; i < frameCount; ++i) {
gCachedFrames[i] = static_cast<uint8_t*>(malloc(bufferSize)); gCachedFrames[i] = static_cast<uint8_t*>(malloc(bufferSize));
if (!gCachedFrames[i]) { if (!gCachedFrames[i]) {
LOG_ERR("CAROUSEL", "tryFastHomeRender: malloc failed for frame %d", i); LOG_ERR("CAROUSEL", "tryFastHomeRender: malloc failed for frame %d", i);
invalidateFrameCache(); freeFrameCache();
return false; return false;
} }
} }
@@ -72,7 +72,7 @@ class LyraCarouselTheme : public LyraTheme {
const std::function<UIIcon(int)>& menuIcon, const char* hintBtn1, const char* hintBtn2, const std::function<UIIcon(int)>& menuIcon, const char* hintBtn1, const char* hintBtn2,
const char* hintBtn3, const char* hintBtn4) const override; const char* hintBtn3, const char* hintBtn4) const override;
void onBookWillClose(const std::string& path, Epub* epub, Xtc* xtc, Txt* txt) override; void onBookWillClose(const std::string& path, Epub* epub, Xtc* xtc, Txt* txt) override;
void invalidateFrameCache() const; void invalidateFrameCache() override;
void drawRecentBookCover(GfxRenderer& renderer, Rect rect, const std::vector<RecentBook>& recentBooks, void drawRecentBookCover(GfxRenderer& renderer, Rect rect, const std::vector<RecentBook>& recentBooks,
const int selectorIndex, bool& coverRendered, bool& coverBufferStored, bool& bufferRestored, const int selectorIndex, bool& coverRendered, bool& coverBufferStored, bool& bufferRestored,