Add Lyra Carousel home theme

This commit is contained in:
jpirnay
2026-05-07 23:42:35 +02:00
parent 0b93445450
commit 4005bf55e5
17 changed files with 875 additions and 103 deletions
+214 -19
View File
@@ -11,6 +11,7 @@
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include "CrossPointSettings.h"
@@ -20,9 +21,39 @@
#include "OpdsServerStore.h"
#include "RecentBooksStore.h"
#include "components/UITheme.h"
#include "components/themes/lyra/LyraCarouselTheme.h"
#include "fontIds.h"
// ---------------------------------------------------------------------------
// Static carousel frame cache — survives HomeActivity re-creation so that
// returning to home (e.g. after settings) doesn't re-read covers from SD.
// Freed explicitly in onSelectBook() before entering the reader.
// ---------------------------------------------------------------------------
namespace {
uint8_t* gCachedFrames[HomeActivity::kCarouselFrameCount] = {};
int gCachedFrameBookIdx[HomeActivity::kCarouselFrameCount] = {-1, -1, -1};
int gCachedFrameCount = 0;
std::string gCacheKey;
int findFrameSlot(int bookIdx) {
for (int i = 0; i < HomeActivity::kCarouselFrameCount; ++i) {
if (gCachedFrameBookIdx[i] == bookIdx && gCachedFrames[i] != nullptr) return i;
}
return -1;
}
void invalidateCarouselCache() {
for (int i = 0; i < HomeActivity::kCarouselFrameCount; ++i) {
if (gCachedFrames[i]) {
free(gCachedFrames[i]);
gCachedFrames[i] = nullptr;
}
gCachedFrameBookIdx[i] = -1;
}
gCachedFrameCount = 0;
gCacheKey.clear();
}
constexpr int CLASSIC_MIN_RECENT_TILE_HEIGHT = 280;
constexpr int LYRA_MIN_RECENT_TILE_HEIGHT = 170;
constexpr int LYRA_3_COVERS_MIN_RECENT_TILE_HEIGHT = 200;
@@ -199,6 +230,7 @@ void HomeActivity::onEnter() {
hasOpdsServers = OPDS_STORE.hasServers();
selectorIndex = 0;
carouselFramesReady = false;
recentsLoading = false;
recentsLoaded = false;
firstRenderDone = false;
@@ -212,6 +244,11 @@ void HomeActivity::onEnter() {
recentsLoaded = true;
}
// Pre-render carousel frames before first display so the fast path is ready.
if (static_cast<CrossPointSettings::UI_THEME>(SETTINGS.uiTheme) == CrossPointSettings::UI_THEME::LYRA_CAROUSEL) {
preRenderCarouselFrames();
}
// Apply focus: book path takes priority, else combined selector index (covers
// "return to the menu entry I was on").
bool focused = false;
@@ -242,8 +279,9 @@ void HomeActivity::onEnter() {
void HomeActivity::onExit() {
Activity::onExit();
// Free the stored cover buffer if any
freeCoverBuffer();
invalidateCarouselCache();
freeCarouselFrames();
}
bool HomeActivity::storeCoverBuffer() {
@@ -288,31 +326,188 @@ void HomeActivity::freeCoverBuffer() {
coverBufferStored = false;
}
void HomeActivity::freeCarouselFrames() {
// Instance pointers are aliases into the static cache — do not free here.
for (int i = 0; i < kCarouselFrameCount; ++i) carouselFrames[i] = nullptr;
carouselFramesReady = false;
}
void HomeActivity::preRenderCarouselFrames() {
const int bookCount = static_cast<int>(recentBooks.size());
if (bookCount == 0) return;
// Build cache key from book paths in order
std::string newKey;
newKey.reserve(128);
for (const auto& b : recentBooks) {
newKey += b.path;
newKey += '\0';
}
// Cache hit: same books in same order — reuse without any SD reads
if (newKey == gCacheKey && gCachedFrameCount > 0) {
for (int i = 0; i < gCachedFrameCount; ++i) carouselFrames[i] = gCachedFrames[i];
carouselFramesReady = true;
coverRendered = false;
coverBufferStored = false;
return;
}
// Cache miss: free old cache and re-render
invalidateCarouselCache();
if (!renderer.getFrameBuffer()) return;
const size_t bufferSize = renderer.getBufferSize();
freeCoverBuffer(); // reclaim 48KB before allocating frames
const int frameCount = std::min(bookCount, kCarouselFrameCount);
for (int i = 0; i < frameCount; ++i) {
gCachedFrames[i] = static_cast<uint8_t*>(malloc(bufferSize));
if (!gCachedFrames[i]) {
LOG_ERR("HOME", "preRenderCarouselFrames: malloc failed for frame %d", i);
invalidateCarouselCache();
return;
}
}
// Render only the currently-selected cover. Adjacent frames are populated
// lazily by updateSlidingWindowCache() after the first paint completes.
const int selectedBookIdx = (selectorIndex < bookCount) ? selectorIndex : lastCarouselBookIndex;
const int initialBookIdx = (selectedBookIdx >= 0 && selectedBookIdx < bookCount) ? selectedBookIdx : 0;
renderCarouselFrame(initialBookIdx, 0);
gCachedFrameCount = frameCount;
gCacheKey = newKey;
carouselFramesReady = true;
coverRendered = false;
coverBufferStored = false;
}
void HomeActivity::renderCarouselFrame(int bookIdx, int slotIdx) {
uint8_t* frameBuffer = renderer.getFrameBuffer();
if (!frameBuffer || !gCachedFrames[slotIdx]) return;
const auto& metrics = UITheme::getInstance().getMetrics();
const int pageWidth = renderer.getScreenWidth();
const int bookCount = static_cast<int>(recentBooks.size());
bool dummy1 = false, dummy2 = false, dummy3 = false;
LyraCarouselTheme::setPreRenderIndex(bookIdx);
renderer.clearScreen();
GUI.drawHeader(renderer, Rect{0, metrics.topPadding, pageWidth, metrics.homeTopPadding}, nullptr);
GUI.drawRecentBookCover(renderer, Rect{0, metrics.homeTopPadding, pageWidth, metrics.homeCoverTileHeight},
recentBooks, bookCount, dummy1, dummy2, dummy3, []() { return true; });
memcpy(gCachedFrames[slotIdx], frameBuffer, renderer.getBufferSize());
gCachedFrameBookIdx[slotIdx] = bookIdx;
carouselFrames[slotIdx] = gCachedFrames[slotIdx];
}
void HomeActivity::updateSlidingWindowCache(int centerIdx, int bookCount) {
if (bookCount <= kCarouselFrameCount || !carouselFramesReady) return;
const int prevIdx = (centerIdx + bookCount - 1) % bookCount;
const int nextIdx = (centerIdx + 1) % bookCount;
const bool hasPrev = findFrameSlot(prevIdx) >= 0;
const bool hasNext = findFrameSlot(nextIdx) >= 0;
if (hasPrev && hasNext) return;
const int missingIdx = !hasPrev ? prevIdx : nextIdx;
int evictSlot = -1;
int maxDist = -1;
for (int i = 0; i < kCarouselFrameCount; ++i) {
if (!gCachedFrames[i]) continue;
const int bookInSlot = gCachedFrameBookIdx[i];
if (bookInSlot == centerIdx) continue;
if (hasPrev && bookInSlot == prevIdx) continue;
if (hasNext && bookInSlot == nextIdx) continue;
const int diff = std::abs(bookInSlot - centerIdx);
const int dist = std::min(diff, bookCount - diff);
if (dist > maxDist) {
maxDist = dist;
evictSlot = i;
}
}
if (evictSlot >= 0) {
LOG_DBG("HOME", "carousel: evict slot %d (book %d) -> book %d", evictSlot, gCachedFrameBookIdx[evictSlot],
missingIdx);
renderCarouselFrame(missingIdx, evictSlot);
}
}
void HomeActivity::loop() {
if (menuEntriesDirty) {
rebuildMenuEntries();
}
const int totalItems = static_cast<int>(recentBooks.size() + menuEntries.size());
if (firstRenderDone && !recentsLoaded && !recentsLoading) {
const auto& metrics = UITheme::getInstance().getMetrics();
const Rect contentRect = UITheme::getContentRect(renderer, true, false);
const HomeScreenLayout layout =
computeHomeScreenLayout(metrics, contentRect.height, static_cast<int>(menuEntries.size()));
loadRecentCovers(getHomeCoverRenderHeight(layout));
return;
const bool isCarousel =
static_cast<CrossPointSettings::UI_THEME>(SETTINGS.uiTheme) == CrossPointSettings::UI_THEME::LYRA_CAROUSEL;
if (isCarousel) {
const int bookCount = static_cast<int>(recentBooks.size());
const int menuItemCount = static_cast<int>(menuEntries.size());
const bool inCarouselRow = (selectorIndex < bookCount);
const int menuIdx = inCarouselRow ? 0 : (selectorIndex - bookCount);
if (mappedInput.wasPressed(MappedInputManager::Button::Right)) {
if (inCarouselRow && bookCount > 0)
selectorIndex = (selectorIndex + 1) % bookCount;
else if (!inCarouselRow)
selectorIndex = bookCount + (menuIdx + 1) % menuItemCount;
requestUpdate();
}
if (mappedInput.wasPressed(MappedInputManager::Button::Left)) {
if (inCarouselRow && bookCount > 0)
selectorIndex = (selectorIndex + bookCount - 1) % bookCount;
else if (!inCarouselRow)
selectorIndex = bookCount + (menuIdx + menuItemCount - 1) % menuItemCount;
requestUpdate();
}
if (mappedInput.wasPressed(MappedInputManager::Button::Down)) {
if (inCarouselRow) {
lastCarouselBookIndex = selectorIndex;
selectorIndex = bookCount;
} else {
selectorIndex = lastCarouselBookIndex;
}
requestUpdate();
}
if (mappedInput.wasPressed(MappedInputManager::Button::Up)) {
if (inCarouselRow) {
lastCarouselBookIndex = selectorIndex;
selectorIndex = bookCount;
} else {
selectorIndex = lastCarouselBookIndex;
}
requestUpdate();
}
} else {
const int totalItems = static_cast<int>(recentBooks.size() + menuEntries.size());
if (firstRenderDone && !recentsLoaded && !recentsLoading) {
const auto& metrics = UITheme::getInstance().getMetrics();
const Rect contentRect = UITheme::getContentRect(renderer, true, false);
const HomeScreenLayout layout =
computeHomeScreenLayout(metrics, contentRect.height, static_cast<int>(menuEntries.size()));
loadRecentCovers(getHomeCoverRenderHeight(layout));
return;
}
buttonNavigator.onNext([this, totalItems] {
selectorIndex = ButtonNavigator::nextIndex(selectorIndex, totalItems);
requestUpdate();
});
buttonNavigator.onPrevious([this, totalItems] {
selectorIndex = ButtonNavigator::previousIndex(selectorIndex, totalItems);
requestUpdate();
});
}
buttonNavigator.onNext([this, totalItems] {
selectorIndex = ButtonNavigator::nextIndex(selectorIndex, totalItems);
requestUpdate();
});
buttonNavigator.onPrevious([this, totalItems] {
selectorIndex = ButtonNavigator::previousIndex(selectorIndex, totalItems);
requestUpdate();
});
if (mappedInput.wasReleased(MappedInputManager::Button::Confirm)) {
const int recentsCount = static_cast<int>(recentBooks.size());
if (selectorIndex < recentsCount) {
+11
View File
@@ -13,6 +13,8 @@ struct Rect;
class HomeActivity final : public Activity {
public:
static constexpr int kCarouselFrameCount = 3;
enum class MenuAction {
FileBrowser,
Recents,
@@ -32,6 +34,7 @@ class HomeActivity final : public Activity {
ButtonNavigator buttonNavigator;
int selectorIndex = 0;
int lastCarouselBookIndex = 0; // remembered position when leaving carousel row
bool recentsLoading = false;
bool recentsLoaded = false;
bool firstRenderDone = false;
@@ -40,6 +43,10 @@ class HomeActivity final : public Activity {
bool coverBufferStored = false; // Track if cover buffer is stored
size_t nextRecentCoverIndex = 0;
uint8_t* coverBuffer = nullptr; // HomeActivity's own buffer for cover image
uint8_t* carouselFrames[kCarouselFrameCount] = {nullptr, nullptr, nullptr};
bool carouselFramesReady = false;
std::vector<RecentBook> recentBooks;
std::vector<MenuEntry> menuEntries;
bool menuEntriesDirty = true;
@@ -54,6 +61,10 @@ class HomeActivity final : public Activity {
bool storeCoverBuffer();
bool restoreCoverBuffer();
void freeCoverBuffer();
void preRenderCarouselFrames();
void freeCarouselFrames();
void renderCarouselFrame(int bookIdx, int slotIdx);
void updateSlidingWindowCache(int centerIdx, int bookCount);
void loadRecentBooks(int maxBooks);
void loadRecentCovers(int coverHeight);