From a5c2a944c486fc3ad35e197005fd720eff35c740 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Mon, 20 Jul 2020 20:16:38 +0200 Subject: [PATCH 1/4] Fix fb_get_size. * Return the size of the available memory after the frame. * Fixes #858 --- src/omv/framebuffer.c | 22 +++++++++++++++++++++- src/omv/framebuffer.h | 2 ++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/src/omv/framebuffer.c b/src/omv/framebuffer.c index fca21e161..a6beddd8b 100644 --- a/src/omv/framebuffer.c +++ b/src/omv/framebuffer.c @@ -199,7 +199,27 @@ int32_t framebuffer_get_depth() uint32_t framebuffer_get_size() { - return OMV_RAW_BUF_SIZE; + switch (framebuffer->bpp) { + case -1: { + // Invalid frame. + return 0; + } + case IMAGE_BPP_BINARY: { + return ((framebuffer->w + UINT32_T_MASK) >> UINT32_T_SHIFT) * framebuffer->h; + } + case IMAGE_BPP_GRAYSCALE: { + return (framebuffer->w * framebuffer->h) * sizeof(uint8_t); + } + case IMAGE_BPP_RGB565: { + return (framebuffer->w * framebuffer->h) * sizeof(uint16_t); + } + case IMAGE_BPP_BAYER: { + return framebuffer->w * framebuffer->h; + } + default: { // JPEG + return framebuffer->bpp; + } + } } uint8_t *framebuffer_get_buffer() diff --git a/src/omv/framebuffer.h b/src/omv/framebuffer.h index 59f2cad7e..236cb7c9e 100644 --- a/src/omv/framebuffer.h +++ b/src/omv/framebuffer.h @@ -60,6 +60,8 @@ int32_t framebuffer_get_width(); int32_t framebuffer_get_height(); int32_t framebuffer_get_depth(); +// Return the size of the framebuffer (w * h * bpp) if the framebuffer is initialized, +// otherwise return 0 if the framebuffer is unintialized or invalid (e.g. first frame). uint32_t framebuffer_get_size(); // Return the current buffer address. From e18f83cfe1055b76d2af5c6302343506baba18f2 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Mon, 20 Jul 2020 21:34:40 +0200 Subject: [PATCH 2/4] Rename framebuffer_get_size * This function returns the current frame size, so make its purpose more clear by renaming to framebuffer_get_frame_size. --- src/omv/fb_alloc.c | 2 +- src/omv/framebuffer.c | 2 +- src/omv/framebuffer.h | 4 ++-- src/omv/py/py_helper.c | 2 +- 4 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/omv/fb_alloc.c b/src/omv/fb_alloc.c index 4bd648c52..bcbdd8265 100644 --- a/src/omv/fb_alloc.c +++ b/src/omv/fb_alloc.c @@ -28,7 +28,7 @@ static char *pointer_overlay = &_fballoc_overlay; static char *fb_alloc_min_address() { - return (char *) (framebuffer_get_buffer() + framebuffer_get_size()); + return (char *) (framebuffer_get_buffer() + framebuffer_get_frame_size()); } __weak NORETURN void fb_alloc_fail() diff --git a/src/omv/framebuffer.c b/src/omv/framebuffer.c index a6beddd8b..8d22f141c 100644 --- a/src/omv/framebuffer.c +++ b/src/omv/framebuffer.c @@ -197,7 +197,7 @@ int32_t framebuffer_get_depth() return framebuffer->bpp; } -uint32_t framebuffer_get_size() +uint32_t framebuffer_get_frame_size() { switch (framebuffer->bpp) { case -1: { diff --git a/src/omv/framebuffer.h b/src/omv/framebuffer.h index 236cb7c9e..e294dcec2 100644 --- a/src/omv/framebuffer.h +++ b/src/omv/framebuffer.h @@ -60,9 +60,9 @@ int32_t framebuffer_get_width(); int32_t framebuffer_get_height(); int32_t framebuffer_get_depth(); -// Return the size of the framebuffer (w * h * bpp) if the framebuffer is initialized, +// Return the size of the current frame (w * h * bpp) if the framebuffer is initialized, // otherwise return 0 if the framebuffer is unintialized or invalid (e.g. first frame). -uint32_t framebuffer_get_size(); +uint32_t framebuffer_get_frame_size(); // Return the current buffer address. uint8_t *framebuffer_get_buffer(); diff --git a/src/omv/py/py_helper.c b/src/omv/py/py_helper.c index c9f948659..039ba2820 100644 --- a/src/omv/py/py_helper.c +++ b/src/omv/py/py_helper.c @@ -383,7 +383,7 @@ void py_helper_update_framebuffer(image_t *img) void py_helper_set_to_framebuffer(image_t *img) { - PY_ASSERT_TRUE_MSG((image_size(img) <= framebuffer_get_size()), + PY_ASSERT_TRUE_MSG((image_size(img) <= framebuffer_get_frame_size()), "The image doesn't fit in the frame buffer!"); framebuffer_set(img->w, img->h, img->bpp); img->data = framebuffer_get_buffer(); From 1d93c549835db90054d6fe376a5e3604b25b5516 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Mon, 20 Jul 2020 21:39:27 +0200 Subject: [PATCH 3/4] Add a function to return the frame buffer size. * This returns the frame buffer size without the fb header size. --- src/omv/framebuffer.c | 6 ++++++ src/omv/framebuffer.h | 3 +++ 2 files changed, 9 insertions(+) diff --git a/src/omv/framebuffer.c b/src/omv/framebuffer.c index 8d22f141c..ed2a32c4f 100644 --- a/src/omv/framebuffer.c +++ b/src/omv/framebuffer.c @@ -16,6 +16,7 @@ #define CONSERVATIVE_JPEG_BUF_SIZE (OMV_JPEG_BUF_SIZE-64) extern char _fb_base; +extern char _fballoc; framebuffer_t *framebuffer = (framebuffer_t *) &_fb_base; extern char _jpeg_buf; @@ -222,6 +223,11 @@ uint32_t framebuffer_get_frame_size() } } +uint32_t framebuffer_get_buffer_size() +{ + return &_fballoc - (char *) framebuffer->pixels; +} + uint8_t *framebuffer_get_buffer() { return framebuffer->pixels; diff --git a/src/omv/framebuffer.h b/src/omv/framebuffer.h index e294dcec2..459ea1f34 100644 --- a/src/omv/framebuffer.h +++ b/src/omv/framebuffer.h @@ -64,6 +64,9 @@ int32_t framebuffer_get_depth(); // otherwise return 0 if the framebuffer is unintialized or invalid (e.g. first frame). uint32_t framebuffer_get_frame_size(); +// Return the max frame size that fits the framebuffer (i.e OMV_RAW_BUF_SIZE - sizeof(framebuffer_t)) +uint32_t framebuffer_get_buffer_size(); + // Return the current buffer address. uint8_t *framebuffer_get_buffer(); From b1b7c74b152ab7ca66aaaa8c52f186460d260607 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Mon, 20 Jul 2020 21:41:51 +0200 Subject: [PATCH 4/4] Fix the frame size check when copying images to FB. * The size of the image to be copied was compared to the size of the current frame instead of the size of the whole buffer. --- src/omv/py/py_helper.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/omv/py/py_helper.c b/src/omv/py/py_helper.c index 039ba2820..ab36ae70c 100644 --- a/src/omv/py/py_helper.c +++ b/src/omv/py/py_helper.c @@ -383,7 +383,7 @@ void py_helper_update_framebuffer(image_t *img) void py_helper_set_to_framebuffer(image_t *img) { - PY_ASSERT_TRUE_MSG((image_size(img) <= framebuffer_get_frame_size()), + PY_ASSERT_TRUE_MSG((image_size(img) <= framebuffer_get_buffer_size()), "The image doesn't fit in the frame buffer!"); framebuffer_set(img->w, img->h, img->bpp); img->data = framebuffer_get_buffer();