Skip to content

Commit

Permalink
Better memory management for large apps with many windows
Browse files Browse the repository at this point in the history
  • Loading branch information
paxo-rch committed Sep 13, 2024
1 parent ac2562b commit 2b1a2e3
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 deletions.
24 changes: 24 additions & 0 deletions lib/gui/src/ElementBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,10 @@ void gui::ElementBase::renderAll(bool onScreen)
m_surface = nullptr;

if (m_surface == nullptr)
{
freeRamFor(m_width * m_height, this->getMaster());
m_surface = std::make_shared<graphics::Surface>(m_width, m_height);
}

// Render the element
render();
Expand Down Expand Up @@ -644,3 +647,24 @@ gui::ElementBase *gui::ElementBase::getElementAt(int index) {
return nullptr;

}

#include "elements/Window.hpp"

void gui::ElementBase::freeRamFor(uint32_t size, ElementBase* window)
{
#ifdef ESP_PLATFORM
size_t free = heap_caps_get_largest_free_block(MALLOC_CAP_8BIT);

if (free < size + 1000000)
{
std::cout << "Not enough RAM, free : " << free << " need : " << size << "\n -> will free other windows" << std::endl;
for (auto i : gui::elements::Window::windows)
{
if(i != window)
{
i->free();
}
}
}
#endif
}
2 changes: 2 additions & 0 deletions lib/gui/src/ElementBase.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ namespace gui
void forceUpdate();

protected:
void freeRamFor(uint32_t size, ElementBase* window);

// variables générales
uint16_t m_width, m_height;

Expand Down
9 changes: 8 additions & 1 deletion lib/gui/src/elements/Window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@ namespace gui::elements {
m_width = graphics::getScreenWidth();
m_height = graphics::getScreenHeight();
m_backgroundColor = COLOR_WHITE;

windows.push_back(this);
}

Window::~Window() = default;
Window::~Window()
{
windows.erase(std::remove(windows.begin(), windows.end(), this), windows.end());
}

void Window::render()
{
m_surface->fillRect(0, 0, m_width, m_height, m_backgroundColor);
}

std::vector<Window*> Window::windows;
} // gui::elements
2 changes: 2 additions & 0 deletions lib/gui/src/elements/Window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ namespace gui::elements
Window();
~Window() override;

static std::vector<Window*> windows;

void render() override;
};
} // gui::elements
Expand Down
3 changes: 2 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,8 @@ void setup()
AppManager::init();

#ifdef ESP_PLATFORM
xTaskCreateUniversal(mainLoop,"newloop", 32*1024, NULL, 1, NULL, ARDUINO_RUNNING_CORE);
// // stack size: >32k = crash due to image decoder in stack
xTaskCreateUniversal(mainLoop,"newloop", 16*1024, NULL, 1, NULL, ARDUINO_RUNNING_CORE);
vTaskDelete(NULL);
#else
mainLoop(NULL);
Expand Down
2 changes: 1 addition & 1 deletion storage/apps/calendrier/config.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"day":{"heureFin":18, "heureDebut":9}, "displayBusinessWeek":false, "defaultView":"week", "displayWeekNum":true}
{"defaultView":"week", "displayBusinessWeek":true, "displayWeekNum":true, "day":{"heureDebut":6, "heureFin":18}}

0 comments on commit 2b1a2e3

Please sign in to comment.