1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-28 07:03:00 +08:00

feat(draw_buf) make lv_draw_buf_malloc a static function (#5318)

Signed-off-by: Xu Xingliang <xuxingliang@xiaomi.com>
This commit is contained in:
Neo Xu 2024-01-15 19:39:28 +08:00 committed by GitHub
parent d619d6b7f5
commit c8bd402192
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 35 deletions

View File

@ -26,6 +26,8 @@
static void * buf_malloc(size_t size, lv_color_format_t color_format); static void * buf_malloc(size_t size, lv_color_format_t color_format);
static void buf_free(void * buf); static void buf_free(void * buf);
static void * buf_align(void * buf, lv_color_format_t color_format); static void * buf_align(void * buf, lv_color_format_t color_format);
static void * draw_buf_malloc(size_t size_bytes, lv_color_format_t color_format);
static void draw_buf_free(void * buf);
static uint32_t width_to_stride(uint32_t w, lv_color_format_t color_format); static uint32_t width_to_stride(uint32_t w, lv_color_format_t color_format);
static uint32_t _calculate_draw_buf_size(uint32_t w, uint32_t h, lv_color_format_t cf, uint32_t stride); static uint32_t _calculate_draw_buf_size(uint32_t w, uint32_t h, lv_color_format_t cf, uint32_t stride);
@ -62,17 +64,6 @@ uint32_t lv_draw_buf_width_to_stride(uint32_t w, lv_color_format_t color_format)
else return 0; else return 0;
} }
void * lv_draw_buf_malloc(size_t size_bytes, lv_color_format_t color_format)
{
if(handlers.buf_malloc_cb) return handlers.buf_malloc_cb(size_bytes, color_format);
else return NULL;
}
void lv_draw_buf_free(void * buf)
{
if(handlers.buf_free_cb) handlers.buf_free_cb(buf);
}
void * lv_draw_buf_align(void * data, lv_color_format_t color_format) void * lv_draw_buf_align(void * data, lv_color_format_t color_format)
{ {
if(handlers.align_pointer_cb) return handlers.align_pointer_cb(data, color_format); if(handlers.align_pointer_cb) return handlers.align_pointer_cb(data, color_format);
@ -189,7 +180,7 @@ lv_draw_buf_t * lv_draw_buf_create(uint32_t w, uint32_t h, lv_color_format_t cf,
uint32_t size = _calculate_draw_buf_size(w, h, cf, stride); uint32_t size = _calculate_draw_buf_size(w, h, cf, stride);
void * buf = lv_draw_buf_malloc(size, cf); void * buf = draw_buf_malloc(size, cf);
LV_ASSERT_MALLOC(buf); LV_ASSERT_MALLOC(buf);
if(buf == NULL) { if(buf == NULL) {
LV_LOG_WARN("No memory: %"LV_PRIu32"x%"LV_PRIu32", cf: %d, stride: %"LV_PRIu32", %"LV_PRIu32"Byte, ", LV_LOG_WARN("No memory: %"LV_PRIu32"x%"LV_PRIu32", cf: %d, stride: %"LV_PRIu32", %"LV_PRIu32"Byte, ",
@ -257,7 +248,7 @@ void lv_draw_buf_destroy(lv_draw_buf_t * buf)
if(buf == NULL) return; if(buf == NULL) return;
if(buf->header.flags & LV_IMAGE_FLAGS_ALLOCATED) { if(buf->header.flags & LV_IMAGE_FLAGS_ALLOCATED) {
lv_draw_buf_free(buf->unaligned_data); draw_buf_free(buf->unaligned_data);
lv_free(buf); lv_free(buf);
} }
else { else {
@ -417,6 +408,17 @@ static uint32_t width_to_stride(uint32_t w, lv_color_format_t color_format)
return (width_byte + LV_DRAW_BUF_STRIDE_ALIGN - 1) & ~(LV_DRAW_BUF_STRIDE_ALIGN - 1); return (width_byte + LV_DRAW_BUF_STRIDE_ALIGN - 1) & ~(LV_DRAW_BUF_STRIDE_ALIGN - 1);
} }
static void * draw_buf_malloc(size_t size_bytes, lv_color_format_t color_format)
{
if(handlers.buf_malloc_cb) return handlers.buf_malloc_cb(size_bytes, color_format);
else return NULL;
}
static void draw_buf_free(void * buf)
{
if(handlers.buf_free_cb) handlers.buf_free_cb(buf);
}
/** /**
* For given width, height, color format, and stride, calculate the size needed for a new draw buffer. * For given width, height, color format, and stride, calculate the size needed for a new draw buffer.
*/ */

View File

@ -102,22 +102,6 @@ void _lv_draw_buf_init_handlers(void);
*/ */
lv_draw_buf_handlers_t * lv_draw_buf_get_handlers(void); lv_draw_buf_handlers_t * lv_draw_buf_get_handlers(void);
/**
* Allocate a buffer with the given size. It might allocate slightly larger buffer to fulfill the alignment requirements.
* @param size the size to allocate in bytes
* @param color_format the color format of the buffer to allocate
* @return the allocated buffer.
* @note The returned value can be saved in draw_buf->buf
* @note lv_draw_buf_align can be sued the align the returned pointer
*/
void * lv_draw_buf_malloc(size_t size_bytes, lv_color_format_t color_format);
/**
* Free a buffer allocated by lv_draw_buf_malloc
* @param buf pointer to a buffer
*/
void lv_draw_buf_free(void * buf);
/** /**
* Align the address of a buffer. The buffer needs to be large enough for the real data after alignment * Align the address of a buffer. The buffer needs to be large enough for the real data after alignment
* @param buf the data to align * @param buf the data to align

View File

@ -640,8 +640,9 @@ static lv_result_t decode_indexed(lv_image_decoder_t * decoder, lv_image_decoder
exit_with_buf: exit_with_buf:
if(dsc->src_type == LV_IMAGE_SRC_FILE && !is_compressed) { if(dsc->src_type == LV_IMAGE_SRC_FILE && !is_compressed) {
lv_free((void *)palette); lv_free((void *)palette);
lv_draw_buf_free((void *)indexed_data);
} }
if(draw_buf_indexed) lv_draw_buf_destroy(draw_buf_indexed);
return LV_RESULT_INVALID; return LV_RESULT_INVALID;
#else #else
LV_UNUSED(stride); LV_UNUSED(stride);
@ -759,7 +760,7 @@ static lv_result_t decode_rgb(lv_image_decoder_t * decoder, lv_image_decoder_dsc
res = fs_read_file_at(f, sizeof(lv_image_header_t), img_data, len, &rn); res = fs_read_file_at(f, sizeof(lv_image_header_t), img_data, len, &rn);
if(res != LV_FS_RES_OK || rn != len) { if(res != LV_FS_RES_OK || rn != len) {
LV_LOG_WARN("Read rgb file failed: %d", res); LV_LOG_WARN("Read rgb file failed: %d", res);
lv_draw_buf_free(img_data); lv_draw_buf_destroy(decoded);
return LV_RESULT_INVALID; return LV_RESULT_INVALID;
} }

View File

@ -268,7 +268,7 @@ static lv_draw_buf_t * decode_jpeg_file(const char * filename)
JSAMPARRAY buffer; /* Output row buffer */ JSAMPARRAY buffer; /* Output row buffer */
int row_stride; /* physical row width in output buffer */ int row_stride; /* physical row width in output buffer */
uint8_t * output_buffer = NULL; lv_draw_buf_t * decoded = NULL;
/* In this example we want to open the input file before doing anything else, /* In this example we want to open the input file before doing anything else,
* so that the setjmp() error recovery below can assume the file is open. * so that the setjmp() error recovery below can assume the file is open.
@ -293,8 +293,8 @@ static lv_draw_buf_t * decode_jpeg_file(const char * filename)
LV_LOG_WARN("decoding error"); LV_LOG_WARN("decoding error");
if(output_buffer) { if(decoded) {
lv_draw_buf_free(output_buffer); lv_draw_buf_destroy(decoded);
} }
/* If we get here, the JPEG code has signaled an error. /* If we get here, the JPEG code has signaled an error.
@ -344,7 +344,6 @@ static lv_draw_buf_t * decode_jpeg_file(const char * filename)
* In this example, we need to make an output work buffer of the right size. * In this example, we need to make an output work buffer of the right size.
*/ */
/* JSAMPLEs per row in output buffer */ /* JSAMPLEs per row in output buffer */
lv_draw_buf_t * decoded;
row_stride = cinfo.output_width * cinfo.output_components; row_stride = cinfo.output_width * cinfo.output_components;
/* Make a one-row-high sample array that will go away when done with image */ /* Make a one-row-high sample array that will go away when done with image */
buffer = (*cinfo.mem->alloc_sarray) buffer = (*cinfo.mem->alloc_sarray)