Skip to content

Commit

Permalink
Enable Micropython gc (Garbage Collection) on lvgl. This is controlle…
Browse files Browse the repository at this point in the history
…d by LV_ENABLE_GC macro defined in lv_conf.h. When enabled, lv_conf.h should also define LV_MEM_CUSTOM_REALLOC, LV_MEM_CUSTOM_GET_SIZE, LV_GC_INCLUDE and LV_GC_ROOT
  • Loading branch information
amirgon authored and kisvegabor committed Jan 28, 2019
1 parent d44a618 commit f5bd68f
Show file tree
Hide file tree
Showing 11 changed files with 260 additions and 88 deletions.
4 changes: 3 additions & 1 deletion .gitignore
@@ -1 +1,3 @@
**/*.o
**/*.o
**/*.swp
**/*.swo
49 changes: 27 additions & 22 deletions lv_core/lv_obj.c
Expand Up @@ -19,6 +19,11 @@
#include "../lv_misc/lv_ufs.h"
#include <stdint.h>
#include <string.h>
#include "../lv_misc/lv_gc.h"

#if defined(LV_GC_INCLUDE)
# include LV_GC_INCLUDE
#endif /* LV_ENABLE_GC */

/*********************
* DEFINES
Expand All @@ -43,11 +48,6 @@ static lv_res_t lv_obj_signal(lv_obj_t * obj, lv_signal_t sign, void * param);
/**********************
* STATIC VARIABLES
**********************/
static lv_obj_t * def_scr = NULL;
static lv_obj_t * act_scr = NULL;
static lv_obj_t * top_layer = NULL;
static lv_obj_t * sys_layer = NULL;
static lv_ll_t scr_ll; /*Linked list of screens*/

/**********************
* MACROS
Expand All @@ -62,6 +62,11 @@ static lv_ll_t scr_ll; /*Linked list of screens*/
*/
void lv_init(void)
{
LV_GC_ROOT(def_scr) = NULL;
LV_GC_ROOT(act_scr) = NULL;
LV_GC_ROOT(top_layer) = NULL;
LV_GC_ROOT(sys_layer) = NULL;

LV_LOG_TRACE("lv_init started");

/*Initialize the lv_misc modules*/
Expand All @@ -85,19 +90,19 @@ void lv_init(void)
lv_refr_init();

/*Create the default screen*/
lv_ll_init(&scr_ll, sizeof(lv_obj_t));
def_scr = lv_obj_create(NULL, NULL);
lv_ll_init(&LV_GC_ROOT(scr_ll), sizeof(lv_obj_t));
LV_GC_ROOT(def_scr) = lv_obj_create(NULL, NULL);

act_scr = def_scr;
LV_GC_ROOT(act_scr) = LV_GC_ROOT(def_scr);

top_layer = lv_obj_create(NULL, NULL);
lv_obj_set_style(top_layer, &lv_style_transp_fit);
LV_GC_ROOT(top_layer) = lv_obj_create(NULL, NULL);
lv_obj_set_style(LV_GC_ROOT(top_layer), &lv_style_transp_fit);

sys_layer = lv_obj_create(NULL, NULL);
lv_obj_set_style(sys_layer, &lv_style_transp_fit);
LV_GC_ROOT(sys_layer) = lv_obj_create(NULL, NULL);
lv_obj_set_style(LV_GC_ROOT(sys_layer), &lv_style_transp_fit);

/*Refresh the screen*/
lv_obj_invalidate(act_scr);
lv_obj_invalidate(LV_GC_ROOT(act_scr));

#if LV_INDEV_READ_PERIOD != 0
/*Init the input device handling*/
Expand Down Expand Up @@ -127,7 +132,7 @@ lv_obj_t * lv_obj_create(lv_obj_t * parent, const lv_obj_t * copy)
if(parent == NULL) {
LV_LOG_TRACE("Screen create started");

new_obj = lv_ll_ins_head(&scr_ll);
new_obj = lv_ll_ins_head(&LV_GC_ROOT(scr_ll));
lv_mem_assert(new_obj);
if(new_obj == NULL) return NULL;

Expand Down Expand Up @@ -350,7 +355,7 @@ lv_res_t lv_obj_del(lv_obj_t * obj)
/*Remove the object from parent's children list*/
lv_obj_t * par = lv_obj_get_parent(obj);
if(par == NULL) { /*It is a screen*/
lv_ll_rem(&scr_ll, obj);
lv_ll_rem(&LV_GC_ROOT(scr_ll), obj);
} else {
lv_ll_rem(&(par->child_ll), obj);
}
Expand Down Expand Up @@ -406,7 +411,7 @@ void lv_obj_invalidate(const lv_obj_t * obj)
{
if(lv_obj_get_hidden(obj)) return;

/*Invalidate the object only if it belongs to the 'act_scr'*/
/*Invalidate the object only if it belongs to the 'LV_GC_ROOT(act_scr)'*/
lv_obj_t * obj_scr = lv_obj_get_screen(obj);
if(obj_scr == lv_scr_act() ||
obj_scr == lv_layer_top() ||
Expand Down Expand Up @@ -451,9 +456,9 @@ void lv_obj_invalidate(const lv_obj_t * obj)
*/
void lv_scr_load(lv_obj_t * scr)
{
act_scr = scr;
LV_GC_ROOT(act_scr) = scr;

lv_obj_invalidate(act_scr);
lv_obj_invalidate(LV_GC_ROOT(act_scr));
}

/*--------------------
Expand Down Expand Up @@ -1000,7 +1005,7 @@ void lv_obj_refresh_style(lv_obj_t * obj)
void lv_obj_report_style_mod(lv_style_t * style)
{
lv_obj_t * i;
LL_READ(scr_ll, i) {
LL_READ(LV_GC_ROOT(scr_ll), i) {
if(i->style_p == style || style == NULL) {
lv_obj_refresh_style(i);
}
Expand Down Expand Up @@ -1297,7 +1302,7 @@ void lv_obj_animate(lv_obj_t * obj, lv_anim_builtin_t type, uint16_t time, uint1
*/
lv_obj_t * lv_scr_act(void)
{
return act_scr;
return LV_GC_ROOT(act_scr);
}

/**
Expand All @@ -1306,7 +1311,7 @@ lv_obj_t * lv_scr_act(void)
*/
lv_obj_t * lv_layer_top(void)
{
return top_layer;
return LV_GC_ROOT(top_layer);
}

/**
Expand All @@ -1316,7 +1321,7 @@ lv_obj_t * lv_layer_top(void)
*/
lv_obj_t * lv_layer_sys(void)
{
return sys_layer;
return LV_GC_ROOT(sys_layer);
}

/**
Expand Down
20 changes: 13 additions & 7 deletions lv_hal/lv_hal_disp.c
Expand Up @@ -14,6 +14,12 @@
#include "../lv_hal/lv_hal_disp.h"
#include "../lv_misc/lv_mem.h"
#include "../lv_core/lv_obj.h"
#include "../lv_misc/lv_gc.h"

#if defined(LV_GC_INCLUDE)
# include LV_GC_INCLUDE
#endif /* LV_ENABLE_GC */


/*********************
* DEFINES
Expand All @@ -30,7 +36,6 @@
/**********************
* STATIC VARIABLES
**********************/
static lv_disp_t * disp_list = NULL;
static lv_disp_t * active;

/**********************
Expand All @@ -49,6 +54,7 @@ static lv_disp_t * active;
*/
void lv_disp_drv_init(lv_disp_drv_t * driver)
{
LV_GC_ROOT(disp_list) = NULL;
driver->disp_fill = NULL;
driver->disp_map = NULL;
driver->disp_flush = NULL;
Expand Down Expand Up @@ -81,12 +87,12 @@ lv_disp_t * lv_disp_drv_register(lv_disp_drv_t * driver)
node->next = NULL;

/* Set first display as active by default */
if(disp_list == NULL) {
disp_list = node;
if(LV_GC_ROOT(disp_list) == NULL) {
LV_GC_ROOT(disp_list) = node;
active = node;
lv_obj_invalidate(lv_scr_act());
} else {
disp_list->next = node;
((lv_disp_t*)LV_GC_ROOT(disp_list))->next = node;
}

return node;
Expand Down Expand Up @@ -120,10 +126,10 @@ lv_disp_t * lv_disp_get_active(void)
lv_disp_t * lv_disp_next(lv_disp_t * disp)
{
if(disp == NULL) {
return disp_list;
return LV_GC_ROOT(disp_list);
} else {
if(disp_list->next == NULL) return NULL;
else return disp_list->next;
if(((lv_disp_t*)LV_GC_ROOT(disp_list))->next == NULL) return NULL;
else return ((lv_disp_t*)LV_GC_ROOT(disp_list))->next;
}
}

Expand Down
16 changes: 11 additions & 5 deletions lv_hal/lv_hal_indev.c
Expand Up @@ -10,6 +10,12 @@
*********************/
#include "../lv_hal/lv_hal_indev.h"
#include "../lv_misc/lv_mem.h"
#include "../lv_misc/lv_gc.h"

#if defined(LV_GC_INCLUDE)
# include LV_GC_INCLUDE
#endif /* LV_ENABLE_GC */


/*********************
* DEFINES
Expand All @@ -22,7 +28,6 @@
/**********************
* STATIC PROTOTYPES
**********************/
static lv_indev_t * indev_list = NULL;

/**********************
* STATIC VARIABLES
Expand All @@ -44,6 +49,7 @@ static lv_indev_t * indev_list = NULL;
*/
void lv_indev_drv_init(lv_indev_drv_t * driver)
{
LV_GC_ROOT(indev_list) = NULL;
driver->read = NULL;
driver->type = LV_INDEV_TYPE_NONE;
driver->user_data = NULL;
Expand All @@ -70,10 +76,10 @@ lv_indev_t * lv_indev_drv_register(lv_indev_drv_t * driver)
node->group = NULL;
node->btn_points = NULL;

if(indev_list == NULL) {
indev_list = node;
if(LV_GC_ROOT(indev_list) == NULL) {
LV_GC_ROOT(indev_list) = node;
} else {
lv_indev_t * last = indev_list;
lv_indev_t * last = LV_GC_ROOT(indev_list);
while(last->next)
last = last->next;

Expand All @@ -92,7 +98,7 @@ lv_indev_t * lv_indev_next(lv_indev_t * indev)
{

if(indev == NULL) {
return indev_list;
return LV_GC_ROOT(indev_list);
} else {
if(indev->next == NULL) return NULL;
else return indev->next;
Expand Down
29 changes: 17 additions & 12 deletions lv_misc/lv_anim.c
Expand Up @@ -14,6 +14,12 @@
#include "../lv_hal/lv_hal_tick.h"
#include "lv_task.h"
#include "lv_math.h"
#include "lv_gc.h"

#if defined(LV_GC_INCLUDE)
# include LV_GC_INCLUDE
#endif /* LV_ENABLE_GC */


/*********************
* DEFINES
Expand All @@ -34,7 +40,6 @@ static bool anim_ready_handler(lv_anim_t * a);
/**********************
* STATIC VARIABLES
**********************/
static lv_ll_t anim_ll;
static uint32_t last_task_run;
static bool anim_list_changed;

Expand All @@ -51,7 +56,7 @@ static bool anim_list_changed;
*/
void lv_anim_init(void)
{
lv_ll_init(&anim_ll, sizeof(lv_anim_t));
lv_ll_init(&LV_GC_ROOT(anim_ll), sizeof(lv_anim_t));
last_task_run = lv_tick_get();
lv_task_create(anim_task, LV_REFR_PERIOD, LV_TASK_PRIO_MID, NULL);
}
Expand All @@ -67,7 +72,7 @@ void lv_anim_create(lv_anim_t * anim_p)
if(anim_p->fp != NULL) lv_anim_del(anim_p->var, anim_p->fp); /*fp == NULL would delete all animations of var*/

/*Add the new animation to the animation linked list*/
lv_anim_t * new_anim = lv_ll_ins_head(&anim_ll);
lv_anim_t * new_anim = lv_ll_ins_head(&LV_GC_ROOT(anim_ll));
lv_mem_assert(new_anim);
if(new_anim == NULL) return;

Expand Down Expand Up @@ -97,13 +102,13 @@ bool lv_anim_del(void * var, lv_anim_fp_t fp)
lv_anim_t * a;
lv_anim_t * a_next;
bool del = false;
a = lv_ll_get_head(&anim_ll);
a = lv_ll_get_head(&LV_GC_ROOT(anim_ll));
while(a != NULL) {
/*'a' might be deleted, so get the next object while 'a' is valid*/
a_next = lv_ll_get_next(&anim_ll, a);
a_next = lv_ll_get_next(&LV_GC_ROOT(anim_ll), a);

if(a->var == var && (a->fp == fp || fp == NULL)) {
lv_ll_rem(&anim_ll, a);
lv_ll_rem(&LV_GC_ROOT(anim_ll), a);
lv_mem_free(a);
anim_list_changed = true; /*Read by `anim_task`. It need to know if a delete occurred in the linked list*/
del = true;
Expand Down Expand Up @@ -352,12 +357,12 @@ static void anim_task(void * param)
(void)param;

lv_anim_t * a;
LL_READ(anim_ll, a) {
LL_READ(LV_GC_ROOT(anim_ll), a) {
a->has_run = 0;
}

uint32_t elaps = lv_tick_elaps(last_task_run);
a = lv_ll_get_head(&anim_ll);
a = lv_ll_get_head(&LV_GC_ROOT(anim_ll));

while(a != NULL) {
/*It can be set by `lv_anim_del()` typically in `end_cb`. If set then an animation delete happened in `anim_ready_handler`
Expand Down Expand Up @@ -385,8 +390,8 @@ static void anim_task(void * param)

/* If the linked list changed due to anim. delete then it's not safe to continue
* the reading of the list from here -> start from the head*/
if(anim_list_changed) a = lv_ll_get_head(&anim_ll);
else a = lv_ll_get_next(&anim_ll, a);
if(anim_list_changed) a = lv_ll_get_head(&LV_GC_ROOT(anim_ll));
else a = lv_ll_get_next(&LV_GC_ROOT(anim_ll), a);
}

last_task_run = lv_tick_get();
Expand All @@ -396,7 +401,7 @@ static void anim_task(void * param)
* Called when an animation is ready to do the necessary thinks
* e.g. repeat, play back, delete etc.
* @param a pointer to an animation descriptor
* @return true: animation delete occurred nnd the `anim_ll` has changed
* @return true: animation delete occurred nnd the `LV_GC_ROOT(anim_ll)` has changed
* */
static bool anim_ready_handler(lv_anim_t * a)
{
Expand All @@ -408,7 +413,7 @@ static bool anim_ready_handler(lv_anim_t * a)
(a->repeat == 0 && a->playback == 1 && a->playback_now == 1)) {
void (*cb)(void *) = a->end_cb;
void * p = a->var;
lv_ll_rem(&anim_ll, a);
lv_ll_rem(&LV_GC_ROOT(anim_ll), a);
lv_mem_free(a);
anim_list_changed = true;

Expand Down

0 comments on commit f5bd68f

Please sign in to comment.