1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-21 06:53:01 +08:00
lvgl/src/lv_hal/lv_hal_disp.c

359 lines
9.7 KiB
C
Raw Normal View History

/**
* @file hal_disp.c
*
* @description HAL layer for display driver
*
*/
/*********************
* INCLUDES
*********************/
#include <stdint.h>
#include <stddef.h>
2019-02-10 11:06:47 +01:00
#include "lv_hal.h"
2019-09-24 16:30:38 +02:00
#include "../lv_core/lv_debug.h"
2017-11-23 20:42:14 +01:00
#include "../lv_misc/lv_mem.h"
2017-11-30 11:35:33 +01:00
#include "../lv_core/lv_obj.h"
#include "../lv_core/lv_refr.h"
#include "../lv_misc/lv_gc.h"
#if defined(LV_GC_INCLUDE)
2019-04-04 07:15:40 +02:00
#include LV_GC_INCLUDE
#endif /* LV_ENABLE_GC */
/*********************
* DEFINES
*********************/
/**********************
* TYPEDEFS
**********************/
/**********************
* STATIC PROTOTYPES
**********************/
/**********************
* STATIC VARIABLES
**********************/
2019-02-20 10:16:33 +01:00
static lv_disp_t * disp_def;
/**********************
* MACROS
**********************/
/**********************
* GLOBAL FUNCTIONS
**********************/
/**
* Initialize a display driver with default values.
* It is used to surly have known values in the fields ant not memory junk.
* After it you can set the fields.
* @param driver pointer to driver variable to initialize
*/
2018-06-19 09:49:58 +02:00
void lv_disp_drv_init(lv_disp_drv_t * driver)
{
2019-02-20 10:16:33 +01:00
memset(driver, 0, sizeof(lv_disp_drv_t));
2019-06-06 06:05:40 +02:00
driver->flush_cb = NULL;
driver->hor_res = LV_HOR_RES_MAX;
driver->ver_res = LV_VER_RES_MAX;
driver->buffer = NULL;
driver->rotated = 0;
driver->color_chroma_key = LV_COLOR_TRANSP;
#if LV_ANTIALIAS
driver->antialiasing = true;
#endif
2019-06-25 15:14:47 +02:00
#if LV_COLOR_SCREEN_TRANSP
driver->screen_transp = 1;
#endif
#if LV_USE_GPU
driver->gpu_blend_cb = NULL;
driver->gpu_fill_cb = NULL;
#endif
#if LV_USE_USER_DATA
driver->user_data = NULL;
#endif
2019-02-20 23:58:13 +01:00
driver->set_px_cb = NULL;
}
2019-02-20 10:16:33 +01:00
/**
* Initialize a display buffer
* @param disp_buf pointer `lv_disp_buf_t` variable to initialize
* @param buf1 A buffer to be used by LittlevGL to draw the image.
* Always has to specified and can't be NULL.
* Can be an array allocated by the user. E.g. `static lv_color_t disp_buf1[1024 * 10]`
* Or a memory address e.g. in external SRAM
* @param buf2 Optionally specify a second buffer to make image rendering and image flushing
* (sending to the display) parallel.
* In the `disp_drv->flush` you should use DMA or similar hardware to send
* the image to the display in the background.
2019-04-04 07:15:40 +02:00
* It lets LittlevGL to render next frame into the other buffer while previous is being
* sent. Set to `NULL` if unused.
2019-04-11 05:16:31 +02:00
* @param size_in_px_cnt size of the `buf1` and `buf2` in pixel count.
2019-02-20 10:16:33 +01:00
*/
2019-04-11 05:16:31 +02:00
void lv_disp_buf_init(lv_disp_buf_t * disp_buf, void * buf1, void * buf2, uint32_t size_in_px_cnt)
2019-02-20 10:16:33 +01:00
{
memset(disp_buf, 0, sizeof(lv_disp_buf_t));
2019-04-04 07:15:40 +02:00
disp_buf->buf1 = buf1;
disp_buf->buf2 = buf2;
2019-02-20 10:16:33 +01:00
disp_buf->buf_act = disp_buf->buf1;
2019-04-11 05:16:31 +02:00
disp_buf->size = size_in_px_cnt;
2019-02-20 10:16:33 +01:00
}
/**
2017-10-20 22:41:10 +02:00
* Register an initialized display driver.
* Automatically set the first display as active.
* @param driver pointer to an initialized 'lv_disp_drv_t' variable (can be local variable)
* @return pointer to the new display or NULL on error
*/
2018-06-19 09:49:58 +02:00
lv_disp_t * lv_disp_drv_register(lv_disp_drv_t * driver)
{
lv_disp_t * disp = lv_ll_ins_head(&LV_GC_ROOT(_lv_disp_ll));
if(!disp) {
2019-09-24 23:14:17 +02:00
LV_ASSERT_MEM(disp);
2019-02-10 11:06:47 +01:00
return NULL;
}
memcpy(&disp->driver, driver, sizeof(lv_disp_drv_t));
2019-03-15 22:19:21 +01:00
memset(&disp->inv_area_joined, 0, sizeof(disp->inv_area_joined));
memset(&disp->inv_areas, 0, sizeof(disp->inv_areas));
lv_ll_init(&disp->scr_ll, sizeof(lv_obj_t));
2019-02-10 11:06:47 +01:00
2019-02-20 10:16:33 +01:00
if(disp_def == NULL) disp_def = disp;
2019-04-04 07:15:40 +02:00
lv_disp_t * disp_def_tmp = disp_def;
2019-06-06 06:05:40 +02:00
disp_def = disp; /*Temporarily change the default screen to create the default screens on the
new display*/
2019-02-20 23:58:13 +01:00
disp->inv_p = 0;
2019-04-04 07:15:40 +02:00
disp->act_scr = lv_obj_create(NULL, NULL); /*Create a default screen on the display*/
disp->top_layer = lv_obj_create(NULL, NULL); /*Create top layer on the display*/
2019-09-30 16:56:47 +08:00
disp->sys_layer = lv_obj_create(NULL, NULL); /*Create sys layer on the display*/
lv_obj_set_style(disp->top_layer, &lv_style_transp);
lv_obj_set_style(disp->sys_layer, &lv_style_transp);
2019-02-20 10:16:33 +01:00
lv_obj_invalidate(disp->act_scr);
2019-04-04 07:15:40 +02:00
disp_def = disp_def_tmp; /*Revert the default display*/
2019-02-20 23:58:13 +01:00
/*Create a refresh task*/
2019-04-30 15:03:52 +02:00
disp->refr_task = lv_task_create(lv_disp_refr_task, LV_DISP_DEF_REFR_PERIOD, LV_TASK_PRIO_MID, disp);
2019-09-24 23:14:17 +02:00
LV_ASSERT_MEM(disp->refr_task);
if(disp->refr_task == NULL) return NULL;
2019-04-04 07:15:40 +02:00
lv_task_ready(disp->refr_task); /*Be sure the screen will be refreshed immediately on start up*/
2019-02-20 10:16:33 +01:00
return disp;
}
/**
* Update the driver in run time.
* @param disp pointer to a display. (return value of `lv_disp_drv_register`)
* @param new_drv pointer to the new driver
*/
void lv_disp_drv_update(lv_disp_t * disp, lv_disp_drv_t * new_drv)
{
memcpy(&disp->driver, new_drv, sizeof(lv_disp_drv_t));
lv_obj_t * scr;
2019-04-04 07:15:40 +02:00
LV_LL_READ(disp->scr_ll, scr)
{
lv_obj_set_size(scr, lv_disp_get_hor_res(disp), lv_disp_get_ver_res(disp));
}
}
2019-03-08 06:57:16 +01:00
/**
* Remove a display
* @param disp pointer to display
*/
void lv_disp_remove(lv_disp_t * disp)
{
bool was_default = false;
if(disp == lv_disp_get_default()) was_default = true;
/*Detach the input devices */
lv_indev_t * indev;
indev = lv_indev_get_next(NULL);
2019-03-08 06:57:16 +01:00
while(indev) {
if(indev->driver.disp == disp) {
indev->driver.disp = NULL;
}
indev = lv_indev_get_next(indev);
2019-03-08 06:57:16 +01:00
}
lv_ll_rem(&LV_GC_ROOT(_lv_disp_ll), disp);
lv_mem_free(disp);
if(was_default) lv_disp_set_default(lv_ll_get_head(&LV_GC_ROOT(_lv_disp_ll)));
}
2019-02-13 01:40:22 +01:00
/**
2019-02-20 23:58:13 +01:00
* Set a default screen. The new screens will be created on it by default.
* @param disp pointer to a display
2019-02-13 01:40:22 +01:00
*/
2019-02-20 10:16:33 +01:00
void lv_disp_set_default(lv_disp_t * disp)
{
2019-02-20 10:16:33 +01:00
disp_def = disp;
}
2017-10-20 22:41:10 +02:00
2019-02-20 23:58:13 +01:00
/**
* Get the default display
* @return pointer to the default display
*/
2019-02-20 10:16:33 +01:00
lv_disp_t * lv_disp_get_default(void)
{
return disp_def;
}
2018-07-25 17:57:08 +02:00
2019-02-20 23:58:13 +01:00
/**
* Get the horizontal resolution of a display
* @param disp pointer to a display (NULL to use the default display)
* @return the horizontal resolution of the display
*/
2019-02-10 11:06:47 +01:00
lv_coord_t lv_disp_get_hor_res(lv_disp_t * disp)
{
2019-02-20 10:16:33 +01:00
if(disp == NULL) disp = lv_disp_get_default();
2019-02-10 11:06:47 +01:00
2019-04-04 07:15:40 +02:00
if(disp == NULL)
return LV_HOR_RES_MAX;
2019-04-04 07:15:40 +02:00
else
return disp->driver.rotated == 0 ? disp->driver.hor_res : disp->driver.ver_res;
2019-02-10 11:06:47 +01:00
}
2019-02-20 23:58:13 +01:00
/**
* Get the vertical resolution of a display
* @param disp pointer to a display (NULL to use the default display)
* @return the vertical resolution of the display
*/
2019-02-10 11:06:47 +01:00
lv_coord_t lv_disp_get_ver_res(lv_disp_t * disp)
{
2019-02-20 10:16:33 +01:00
if(disp == NULL) disp = lv_disp_get_default();
2019-02-10 11:06:47 +01:00
2019-04-04 07:15:40 +02:00
if(disp == NULL)
return LV_VER_RES_MAX;
2019-04-04 07:15:40 +02:00
else
return disp->driver.rotated == 0 ? disp->driver.ver_res : disp->driver.hor_res;
}
/**
* Get if anti-aliasing is enabled for a display or not
* @param disp pointer to a display (NULL to use the default display)
* @return true: anti-aliasing is enabled; false: disabled
*/
bool lv_disp_get_antialiasing(lv_disp_t * disp)
{
#if LV_ANTIALIAS == 0
return false;
#else
if(disp == NULL) disp = lv_disp_get_default();
if(disp == NULL) return false;
return disp->driver.antialiasing ? true : false;
#endif
}
2017-10-20 22:41:10 +02:00
/**
* Call in the display driver's `flush_cb` function when the flushing is finished
* @param disp_drv pointer to display driver in `flush_cb` where this function is called
2017-10-20 22:41:10 +02:00
*/
LV_ATTRIBUTE_FLUSH_READY void lv_disp_flush_ready(lv_disp_drv_t * disp_drv)
{
2019-02-20 10:16:33 +01:00
/*If the screen is transparent initialize it when the flushing is ready*/
#if LV_COLOR_SCREEN_TRANSP
2019-06-25 15:14:47 +02:00
if(disp_drv->screen_transp) {
memset(disp_drv->buffer->buf_act, 0x00, disp_drv->buffer->size * sizeof(lv_color32_t));
}
#endif
disp_drv->buffer->flushing = 0;
2019-02-20 10:16:33 +01:00
}
2019-02-20 23:58:13 +01:00
/**
* Get the next display.
* @param disp pointer to the current display. NULL to initialize.
* @return the next display or NULL if no more. Give the first display when the parameter is NULL
*/
lv_disp_t * lv_disp_get_next(lv_disp_t * disp)
{
2019-04-04 07:15:40 +02:00
if(disp == NULL)
return lv_ll_get_head(&LV_GC_ROOT(_lv_disp_ll));
else
return lv_ll_get_next(&LV_GC_ROOT(_lv_disp_ll), disp);
2019-02-20 23:58:13 +01:00
}
/**
* Get the internal buffer of a display
* @param disp pointer to a display
* @return pointer to the internal buffers
*/
lv_disp_buf_t * lv_disp_get_buf(lv_disp_t * disp)
{
return disp->driver.buffer;
}
/**
* Get the number of areas in the buffer
* @return number of invalid areas
*/
uint16_t lv_disp_get_inv_buf_size(lv_disp_t * disp)
{
return disp->inv_p;
}
/**
* Pop (delete) the last 'num' invalidated areas from the buffer
* @param num number of areas to delete
*/
void lv_disp_pop_from_inv_buf(lv_disp_t * disp, uint16_t num)
{
2019-04-04 07:15:40 +02:00
if(disp->inv_p < num)
disp->inv_p = 0;
else
disp->inv_p -= num;
2019-02-20 23:58:13 +01:00
}
/**
* Check the driver configuration if it's double buffered (both `buf1` and `buf2` are set)
* @param disp pointer to to display to check
* @return true: double buffered; false: not double buffered
*/
bool lv_disp_is_double_buf(lv_disp_t * disp)
{
2019-04-04 07:15:40 +02:00
if(disp->driver.buffer->buf1 && disp->driver.buffer->buf2)
return true;
else
return false;
2019-02-20 23:58:13 +01:00
}
/**
2019-04-04 07:15:40 +02:00
* Check the driver configuration if it's TRUE double buffered (both `buf1` and `buf2` are set and
* `size` is screen sized)
2019-02-20 23:58:13 +01:00
* @param disp pointer to to display to check
* @return true: double buffered; false: not double buffered
*/
bool lv_disp_is_true_double_buf(lv_disp_t * disp)
{
uint32_t scr_size = disp->driver.hor_res * disp->driver.ver_res;
2019-04-04 07:15:40 +02:00
if(lv_disp_is_double_buf(disp) && disp->driver.buffer->size == scr_size) {
2019-02-20 23:58:13 +01:00
return true;
2019-04-04 07:15:40 +02:00
} else {
2019-02-20 23:58:13 +01:00
return false;
}
}
/**********************
* STATIC FUNCTIONS
**********************/