diff --git a/src/draw/lv_draw_buf.c b/src/draw/lv_draw_buf.c index 0203dc222..79910ce70 100644 --- a/src/draw/lv_draw_buf.c +++ b/src/draw/lv_draw_buf.c @@ -135,6 +135,27 @@ void lv_draw_buf_copy(void * dest_buf, uint32_t dest_w, uint32_t dest_h, const l } } +lv_result_t lv_draw_buf_init(lv_draw_buf_t * draw_buf, uint32_t w, uint32_t h, lv_color_format_t cf, uint32_t stride, + void * data, uint32_t data_size) +{ + LV_ASSERT_NULL(draw_buf); + if(draw_buf == NULL) return LV_RESULT_INVALID; + + lv_memzero(draw_buf, sizeof(lv_draw_buf_t)); + if(stride == 0) stride = lv_draw_buf_width_to_stride(w, cf); + if(stride * h > data_size) { + LV_LOG_WARN("Data size too small, required: %" LV_PRId32 ", provided: %" LV_PRId32, stride * h, + data_size); + return LV_RESULT_INVALID; + } + + lv_image_header_init(&draw_buf->header, w, h, cf, stride, 0); + draw_buf->data = lv_draw_buf_align(data, cf); + draw_buf->unaligned_data = data; + draw_buf->data_size = data_size; + return LV_RESULT_OK; +} + lv_draw_buf_t * lv_draw_buf_create(uint32_t w, uint32_t h, lv_color_format_t cf, uint32_t stride) { lv_draw_buf_t * draw_buf = lv_malloc_zeroed(sizeof(lv_draw_buf_t)); diff --git a/src/draw/lv_draw_buf.h b/src/draw/lv_draw_buf.h index 89f4d0506..4c681eef0 100644 --- a/src/draw/lv_draw_buf.h +++ b/src/draw/lv_draw_buf.h @@ -174,13 +174,27 @@ void lv_draw_buf_copy(void * dest_buf, uint32_t dest_w, uint32_t dest_h, const l * that meets specified requirements. * * @param w the buffer width in pixels - * @param h + * @param h the buffer height in pixels * @param cf the color format for image * @param stride the stride in bytes for image. Use 0 for automatic calculation based on * w, cf, and global stride alignment configuration. */ lv_draw_buf_t * lv_draw_buf_create(uint32_t w, uint32_t h, lv_color_format_t cf, uint32_t stride); +/** + * Initialize a draw buf with the given buffer and parameters. + * @param draw_buf the draw buf to initialize + * @param w the buffer width in pixels + * @param h the buffer height in pixels + * @param cf the color format + * @param stride the stride in bytes. Use 0 for automatic calculation + * @param data the buffer used for drawing. Unaligned `data` will be aligned internally + * @param data_size the size of the buffer in bytes + * @return return LV_RESULT_OK on success, LV_RESULT_INVALID otherwise + */ +lv_result_t lv_draw_buf_init(lv_draw_buf_t * draw_buf, uint32_t w, uint32_t h, lv_color_format_t cf, uint32_t stride, + void * data, uint32_t data_size); + /** * Duplicate a draw buf with same image size, stride and color format. Copy the image data too. * @param draw_buf the draw buf to duplicate diff --git a/src/draw/vg_lite/lv_vg_lite_decoder.c b/src/draw/vg_lite/lv_vg_lite_decoder.c index f25e1ec7d..df5e62675 100644 --- a/src/draw/vg_lite/lv_vg_lite_decoder.c +++ b/src/draw/vg_lite/lv_vg_lite_decoder.c @@ -219,10 +219,8 @@ static lv_result_t decoder_open_variable(lv_image_decoder_t * decoder, lv_image_ lv_draw_buf_t * draw_buf = lv_malloc_zeroed(sizeof(lv_draw_buf_t)); LV_ASSERT_MALLOC(draw_buf); - lv_image_header_init(&draw_buf->header, width, height, cf, stride, LV_VG_LITE_IMAGE_NO_CACHE); + lv_draw_buf_init(draw_buf, width, height, cf, stride, (void *)image_data, LV_VG_LITE_IMAGE_NO_CACHE); dsc->decoded = draw_buf; - draw_buf->data = (void *)image_data; - draw_buf->unaligned_data = (void *)image_data; return LV_RESULT_OK; } diff --git a/src/widgets/canvas/lv_canvas.c b/src/widgets/canvas/lv_canvas.c index fea90b0e4..2360c533a 100644 --- a/src/widgets/canvas/lv_canvas.c +++ b/src/widgets/canvas/lv_canvas.c @@ -70,11 +70,7 @@ void lv_canvas_set_buffer(lv_obj_t * obj, void * buf, int32_t w, int32_t h, lv_c lv_canvas_t * canvas = (lv_canvas_t *)obj; uint32_t stride = lv_draw_buf_width_to_stride(w, cf); - lv_memzero(&canvas->static_buf, sizeof(canvas->static_buf)); - lv_image_header_init(&canvas->static_buf.header, w, h, cf, stride, 0); - canvas->static_buf.data_size = stride * h; - canvas->static_buf.data = lv_draw_buf_align(buf, cf); - canvas->static_buf.unaligned_data = buf; + lv_draw_buf_init(&canvas->static_buf, w, h, cf, stride, buf, stride * h); canvas->draw_buf = &canvas->static_buf; const void * src = lv_image_get_src(obj);