Skip to content

Commit

Permalink
fix(snapshot): set data_size on returned dsc (lvgl#4972)
Browse files Browse the repository at this point in the history
  • Loading branch information
niklasf authored Dec 11, 2023
1 parent 74d0a81 commit 9ba2992
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 19 deletions.
2 changes: 1 addition & 1 deletion docs/others/snapshot.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void update_snapshot(lv_obj_t * obj, lv_obj_t * img_snapshot)
```
### Use Existing Buffer
If the snapshot needs update now and then, or simply caller provides memory, use API `lv_res_t lv_snapshot_take_to_buf(lv_obj_t * obj, lv_img_cf_t cf, lv_img_dsc_t * dsc, void * buf, uint32_t buff_size);` for this case. It's caller's responsibility to alloc/free the memory.
If the snapshot needs update now and then, or simply caller provides memory, use API `lv_res_t lv_snapshot_take_to_buf(lv_obj_t * obj, lv_img_cf_t cf, lv_img_dsc_t * dsc, void * buf, uint32_t buf_size);` for this case. It's caller's responsibility to alloc/free the memory.
If snapshot is generated successfully, the image descriptor is updated and image data will be stored to provided `buf`.
Expand Down
25 changes: 13 additions & 12 deletions src/extra/others/snapshot/lv_snapshot.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,15 @@ uint32_t lv_snapshot_buf_size_needed(lv_obj_t * obj, lv_img_cf_t cf)

/** Take snapshot for object with its children, save image info to provided buffer.
*
* @param obj The object to generate snapshot.
* @param cf color format for generated image.
* @param dsc image descriptor to store the image result.
* @param buf the buffer to store image data.
* @param buff_size provided buffer size in bytes.
* @param obj The object to generate snapshot.
* @param cf color format for generated image.
* @param dsc image descriptor to store the image result.
* @param buf the buffer to store image data.
* @param buf_size provided buffer size in bytes.
*
* @return LV_RES_OK on success, LV_RES_INV on error.
*/
lv_res_t lv_snapshot_take_to_buf(lv_obj_t * obj, lv_img_cf_t cf, lv_img_dsc_t * dsc, void * buf, uint32_t buff_size)
lv_res_t lv_snapshot_take_to_buf(lv_obj_t * obj, lv_img_cf_t cf, lv_img_dsc_t * dsc, void * buf, uint32_t buf_size)
{
LV_ASSERT_NULL(obj);
LV_ASSERT_NULL(dsc);
Expand All @@ -99,8 +99,8 @@ lv_res_t lv_snapshot_take_to_buf(lv_obj_t * obj, lv_img_cf_t cf, lv_img_dsc_t *
return LV_RES_INV;
}

if(lv_snapshot_buf_size_needed(obj, cf) > buff_size)
return LV_RES_INV;
uint32_t buf_size_needed = lv_snapshot_buf_size_needed(obj, cf);
if(buf_size_needed == 0 || buf_size < buf_size_needed) return LV_RES_INV;

/*Width and height determine snapshot image size.*/
lv_coord_t w = lv_obj_get_width(obj);
Expand All @@ -113,7 +113,7 @@ lv_res_t lv_snapshot_take_to_buf(lv_obj_t * obj, lv_img_cf_t cf, lv_img_dsc_t *
lv_obj_get_coords(obj, &snapshot_area);
lv_area_increase(&snapshot_area, ext_size, ext_size);

lv_memset(buf, 0x00, buff_size);
lv_memset(buf, 0x00, buf_size);
lv_memset_00(dsc, sizeof(lv_img_dsc_t));

lv_disp_t * obj_disp = lv_obj_get_disp(obj);
Expand Down Expand Up @@ -148,6 +148,7 @@ lv_res_t lv_snapshot_take_to_buf(lv_obj_t * obj, lv_img_cf_t cf, lv_img_dsc_t *
lv_mem_free(draw_ctx);

dsc->data = buf;
dsc->data_size = buf_size_needed;
dsc->header.w = w;
dsc->header.h = h;
dsc->header.cf = cf;
Expand All @@ -164,9 +165,9 @@ lv_res_t lv_snapshot_take_to_buf(lv_obj_t * obj, lv_img_cf_t cf, lv_img_dsc_t *
lv_img_dsc_t * lv_snapshot_take(lv_obj_t * obj, lv_img_cf_t cf)
{
LV_ASSERT_NULL(obj);
uint32_t buff_size = lv_snapshot_buf_size_needed(obj, cf);
uint32_t buf_size = lv_snapshot_buf_size_needed(obj, cf);

void * buf = lv_mem_alloc(buff_size);
void * buf = lv_mem_alloc(buf_size);
LV_ASSERT_MALLOC(buf);
if(buf == NULL) {
return NULL;
Expand All @@ -179,7 +180,7 @@ lv_img_dsc_t * lv_snapshot_take(lv_obj_t * obj, lv_img_cf_t cf)
return NULL;
}

if(lv_snapshot_take_to_buf(obj, cf, dsc, buf, buff_size) == LV_RES_INV) {
if(lv_snapshot_take_to_buf(obj, cf, dsc, buf, buf_size) == LV_RES_INV) {
lv_mem_free(buf);
lv_mem_free(dsc);
return NULL;
Expand Down
12 changes: 6 additions & 6 deletions src/extra/others/snapshot/lv_snapshot.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,15 +61,15 @@ uint32_t lv_snapshot_buf_size_needed(lv_obj_t * obj, lv_img_cf_t cf);

/** Take snapshot for object with its children, save image info to provided buffer.
*
* @param obj The object to generate snapshot.
* @param cf color format for generated image.
* @param dsc image descriptor to store the image result.
* @param buff the buffer to store image data.
* @param buff_size provided buffer size in bytes.
* @param obj The object to generate snapshot.
* @param cf color format for generated image.
* @param dsc image descriptor to store the image result.
* @param buf the buffer to store image data.
* @param buf_size provided buffer size in bytes.
*
* @return LV_RES_OK on success, LV_RES_INV on error.
*/
lv_res_t lv_snapshot_take_to_buf(lv_obj_t * obj, lv_img_cf_t cf, lv_img_dsc_t * dsc, void * buf, uint32_t buff_size);
lv_res_t lv_snapshot_take_to_buf(lv_obj_t * obj, lv_img_cf_t cf, lv_img_dsc_t * dsc, void * buf, uint32_t buf_size);


/**********************
Expand Down

0 comments on commit 9ba2992

Please sign in to comment.