Add framebuffer release and font metadata access

Introduce methods to temporarily release the 48KB framebuffer during memory-intensive chapter builds, allowing the memory to be reused. Add font metadata query methods (hasGlyphMeta, ensureAdvance) that check glyph existence and fetch advance metrics without loading full bitmap data, avoiding expensive SD reads during layout measurement.
This commit is contained in:
Justin Mitchell
2026-07-08 04:46:25 -04:00
parent e027f60bf0
commit bfb8aacdae
19 changed files with 447 additions and 119 deletions
+22
View File
@@ -1061,6 +1061,28 @@ void SdCardFont::mergeIntoAdvanceTable(uint8_t styleIdx, const AdvanceEntry* sor
advanceTableSize_[styleIdx] = k;
}
bool SdCardFont::hasGlyphMeta(uint8_t styleIdx, uint32_t codepoint) const {
styleIdx &= (MAX_STYLES - 1);
const PerStyle& s = styles_[styleIdx];
if (!loaded_ || !s.present) return false;
return findGlobalGlyphIndex(s, codepoint) >= 0;
}
uint16_t SdCardFont::ensureAdvance(uint8_t styleIdx, uint32_t codepoint) {
styleIdx &= (MAX_STYLES - 1);
if (!loaded_ || !styles_[styleIdx].present) return 0;
const uint16_t cached = getAdvance(codepoint, styleIdx);
if (cached != 0) return cached;
// 0 is either "not in table" or a genuine zero-width glyph; only pay the
// SD read when the resident interval table says the glyph exists.
if (findGlobalGlyphIndex(styles_[styleIdx], codepoint) < 0) return 0;
uint32_t cp = codepoint;
fetchAdvancesForCodepoints(&cp, 1, static_cast<uint8_t>(1u << styleIdx));
return getAdvance(codepoint, styleIdx);
}
uint8_t SdCardFont::styleIdxFromMissCtx(void* ctx) { return static_cast<OverflowContext*>(ctx)->styleIdx; }
bool SdCardFont::hasAdvanceTable() const {
for (uint8_t i = 0; i < MAX_STYLES; i++) {
if (advanceTable_[i]) return true;
+17
View File
@@ -55,6 +55,23 @@ class SdCardFont {
// Returns the 12.4 fixed-point advance, or 0 if not found.
uint16_t getAdvance(uint32_t codepoint, uint8_t style) const;
// Layout-side metric access — the per-glyph path CpFontAdapter uses while
// the FreeInkBook engine measures a chapter. Neither touches bitmap data:
// going through getGlyph() would stream every occurrence's bitmap through
// the 8-slot overflow ring (one SD read per character — a crawling build).
//
// hasGlyphMeta answers existence from the resident interval table (no I/O).
bool hasGlyphMeta(uint8_t styleIdx, uint32_t codepoint) const;
// ensureAdvance returns the 12.4 fixed-point advanceX, fetching the glyph
// METADATA from SD once on a miss and merging it into the persistent
// advance table (so each unique codepoint costs one small read per
// session). Returns 0 when the style lacks the codepoint.
uint16_t ensureAdvance(uint8_t styleIdx, uint32_t codepoint);
// Recover the style index from an EpdFontData::glyphMissCtx (see
// fromMissCtx below for recovering the SdCardFont itself).
static uint8_t styleIdxFromMissCtx(void* ctx);
// Returns true if advance table is populated for at least one style.
bool hasAdvanceTable() const;
+14
View File
@@ -91,6 +91,20 @@ void GfxRenderer::begin() {
bwBufferChunks.assign((frameBufferSize + BW_BUFFER_CHUNK_SIZE - 1) / BW_BUFFER_CHUNK_SIZE, nullptr);
}
void GfxRenderer::releaseFrameBufferForBuild() {
display.releaseFrameBuffers();
frameBuffer = nullptr;
}
bool GfxRenderer::restoreFrameBufferAfterBuild() {
if (!display.reallocFrameBuffers()) {
LOG_ERR("GFX", "Framebuffer realloc failed after build");
return false;
}
frameBuffer = display.getFrameBuffer();
return frameBuffer != nullptr;
}
bool GfxRenderer::isFontCacheScanning() const { return fontCacheManager_ && fontCacheManager_->isScanning(); }
void GfxRenderer::insertFont(const int fontId, EpdFontFamily font) {
+9
View File
@@ -253,6 +253,15 @@ class GfxRenderer {
// Font helpers
const uint8_t* getGlyphBitmap(const EpdFontData* fontData, const EpdGlyph* glyph) const;
// Lend the 48 KB framebuffer to a memory-hungry phase (chapter builds).
// Between release and a successful restore NOTHING may draw or display —
// the panel keeps showing its last refreshed image. restore returns the
// buffer white, so the caller must redraw the full screen; false means the
// heap could not re-supply the buffer (callers treat that as fatal).
void releaseFrameBufferForBuild();
bool restoreFrameBufferAfterBuild();
bool hasFrameBuffer() const { return frameBuffer != nullptr; }
// Low level functions
uint8_t* getFrameBuffer() const;
size_t getBufferSize() const;
+4
View File
@@ -77,6 +77,10 @@ void HalDisplay::deepSleep() { einkDisplay.deepSleep(); }
uint8_t* HalDisplay::getFrameBuffer() const { return einkDisplay.getFrameBuffer(); }
void HalDisplay::releaseFrameBuffers() { einkDisplay.releaseBuffers(); }
bool HalDisplay::reallocFrameBuffers() { return einkDisplay.reallocBuffers(); }
void HalDisplay::copyGrayscaleBuffers(const uint8_t* lsbBuffer, const uint8_t* msbBuffer) {
einkDisplay.copyGrayscaleBuffers(lsbBuffer, msbBuffer);
}
+6
View File
@@ -47,6 +47,12 @@ class HalDisplay {
// Access to frame buffer
uint8_t* getFrameBuffer() const;
// Lend the framebuffer's ~48 KB to a memory-hungry phase (chapter builds).
// No display calls between release and a successful realloc; the panel
// keeps its last refreshed image. Buffers come back white — redraw fully.
void releaseFrameBuffers();
bool reallocFrameBuffers();
// X3 grayscale preconditioning (OEM "AA-pre-BW(mid)" settle pass), windowed
// to the gray region in physical panel coordinates (no-arg = full frame).
// Call after the BW base frame is displayed and before the grayscale planes