From c87a06edb80ce16f1136aa216a59d4f21aad89a4 Mon Sep 17 00:00:00 2001 From: cottongin Date: Sat, 24 Jan 2026 04:14:19 -0500 Subject: [PATCH] roll home screen performance tweaks to library screens --- src/activities/home/MyLibraryActivity.cpp | 44 +++++++++++++++++++---- src/activities/home/MyLibraryActivity.h | 12 +++++++ 2 files changed, 49 insertions(+), 7 deletions(-) diff --git a/src/activities/home/MyLibraryActivity.cpp b/src/activities/home/MyLibraryActivity.cpp index b15115d..59d6d69 100644 --- a/src/activities/home/MyLibraryActivity.cpp +++ b/src/activities/home/MyLibraryActivity.cpp @@ -13,6 +13,9 @@ #include "fontIds.h" #include "util/StringUtils.h" +// Static thumbnail existence cache definition +ThumbExistsCache MyLibraryActivity::thumbExistsCache[MyLibraryActivity::MAX_THUMB_CACHE]; + namespace { // Layout constants constexpr int TAB_BAR_Y = 15; @@ -510,15 +513,45 @@ void MyLibraryActivity::renderRecentTab() const { const int y = CONTENT_START_Y + (i % pageItems) * RECENTS_LINE_HEIGHT; const bool isSelected = (i == selectorIndex); - // Try to load and draw micro-thumbnail - const std::string microThumbPath = getMicroThumbPathForBook(book.path); + // Try to load and draw micro-thumbnail (with existence caching) bool hasThumb = false; - if (!microThumbPath.empty() && SdMan.exists(microThumbPath.c_str())) { + + // Check if we have cached existence info for this book + ThumbExistsCache* existsCache = nullptr; + std::string microThumbPath; + bool thumbExists = false; + bool existsCacheHit = false; + + if (i < MAX_THUMB_CACHE) { + existsCache = &thumbExistsCache[i]; + if (existsCache->checked && existsCache->bookPath == book.path) { + // Use cached existence info + existsCacheHit = true; + thumbExists = existsCache->exists; + microThumbPath = existsCache->thumbPath; + } + } + + // If not cached, check existence and cache the result + if (!existsCacheHit) { + microThumbPath = getMicroThumbPathForBook(book.path); + thumbExists = !microThumbPath.empty() && SdMan.exists(microThumbPath.c_str()); + + // Cache the result + if (existsCache != nullptr) { + existsCache->bookPath = book.path; + existsCache->thumbPath = microThumbPath; + existsCache->exists = thumbExists; + existsCache->checked = true; + } + } + + // Load and render thumbnail if it exists + if (thumbExists) { FsFile thumbFile; if (SdMan.openFileForRead("MYL", microThumbPath, thumbFile)) { Bitmap bitmap(thumbFile); if (bitmap.parseHeaders() == BmpReaderError::Ok) { - // Calculate actual drawn size (scaled to fit within max dimensions, preserving aspect ratio) const int bmpW = bitmap.getWidth(); const int bmpH = bitmap.getHeight(); const float scaleX = static_cast(MICRO_THUMB_WIDTH) / static_cast(bmpW); @@ -527,10 +560,7 @@ void MyLibraryActivity::renderRecentTab() const { const int drawnW = static_cast(bmpW * scale); const int drawnH = static_cast(bmpH * scale); - // Center thumbnail vertically within the row using actual drawn height const int thumbY = y + (RECENTS_LINE_HEIGHT - drawnH) / 2; - // When selected, clear only the actual drawn area to white first - // (drawBitmap1Bit only draws pixels, it doesn't clear, so we need the white background) if (isSelected) { renderer.fillRect(thumbX, thumbY, drawnW, drawnH, false); } diff --git a/src/activities/home/MyLibraryActivity.h b/src/activities/home/MyLibraryActivity.h index 0ab93e1..4d32003 100644 --- a/src/activities/home/MyLibraryActivity.h +++ b/src/activities/home/MyLibraryActivity.h @@ -10,6 +10,14 @@ #include "../Activity.h" #include "RecentBooksStore.h" +// Cached thumbnail existence info for Recent tab +struct ThumbExistsCache { + std::string bookPath; // Book path this cache entry belongs to + std::string thumbPath; // Path to micro-thumbnail (if exists) + bool checked = false; // Whether we've checked for this book + bool exists = false; // Whether thumbnail exists +}; + class MyLibraryActivity final : public Activity { public: enum class Tab { Recent, Files }; @@ -35,6 +43,10 @@ class MyLibraryActivity final : public Activity { // Recent tab state std::vector recentBooks; + // Static thumbnail existence cache - persists across activity enter/exit + static constexpr int MAX_THUMB_CACHE = 10; + static ThumbExistsCache thumbExistsCache[MAX_THUMB_CACHE]; + // Files tab state (from FileSelectionActivity) std::string basepath = "/"; std::vector files;