diff --git a/docs/others/snapshot.md b/docs/others/snapshot.md index fc54ca3fd6ad..65383d00ce62 100644 --- a/docs/others/snapshot.md +++ b/docs/others/snapshot.md @@ -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`. diff --git a/src/extra/others/snapshot/lv_snapshot.c b/src/extra/others/snapshot/lv_snapshot.c index 1b2275115268..c668d0178f7a 100644 --- a/src/extra/others/snapshot/lv_snapshot.c +++ b/src/extra/others/snapshot/lv_snapshot.c @@ -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); @@ -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); @@ -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); @@ -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; @@ -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; @@ -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; diff --git a/src/extra/others/snapshot/lv_snapshot.h b/src/extra/others/snapshot/lv_snapshot.h index 645192631da9..bb9c42889534 100644 --- a/src/extra/others/snapshot/lv_snapshot.h +++ b/src/extra/others/snapshot/lv_snapshot.h @@ -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); /**********************