From eb6f97eeab8dc24adfbfd93ebfb0edf7b7b76d54 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Thu, 18 Apr 2024 09:55:48 +0200 Subject: [PATCH] Camera: Fix framebuffer malloc alignment. - Allocate extra memory for cache-line alignment. - Fixes #838. Signed-off-by: iabdalkader --- libraries/Camera/src/camera.cpp | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/libraries/Camera/src/camera.cpp b/libraries/Camera/src/camera.cpp index 2cc3da2ef..050ea3772 100644 --- a/libraries/Camera/src/camera.cpp +++ b/libraries/Camera/src/camera.cpp @@ -27,6 +27,12 @@ #define ALIGN_PTR(p,a) ((p & (a-1)) ?(((uintptr_t)p + a) & ~(uintptr_t)(a-1)) : p) +#ifdef __SCB_DCACHE_LINE_SIZE +#define FB_ALIGNMENT __SCB_DCACHE_LINE_SIZE +#else +#define FB_ALIGNMENT 32 +#endif + // Include all image sensor drivers here. #if defined (ARDUINO_PORTENTA_H7_M7) @@ -337,15 +343,16 @@ FrameBuffer::FrameBuffer(int32_t x, int32_t y, int32_t bpp) : _fb_size(x*y*bpp), _isAllocated(true) { - uint8_t *buffer = (uint8_t *)malloc(x*y*bpp); - _fb = (uint8_t *)ALIGN_PTR((uintptr_t)buffer, 32); + uint8_t *buffer = (uint8_t *) malloc(x * y * bpp + FB_ALIGNMENT); + _fb = (uint8_t *) ALIGN_PTR((uintptr_t) buffer, FB_ALIGNMENT); } FrameBuffer::FrameBuffer(int32_t address) : _fb_size(0), - _isAllocated(true) + _isAllocated(true), + _fb((uint8_t *) address) { - _fb = (uint8_t *)ALIGN_PTR((uintptr_t)address, 32); + // Assume that `address` is aligned, this will be verified later in grabFrame. } FrameBuffer::FrameBuffer() : @@ -688,17 +695,17 @@ int Camera::grabFrame(FrameBuffer &fb, uint32_t timeout) } } } else { - uint8_t *buffer = (uint8_t *)malloc(framesize+32); - uint8_t *alignedBuff = (uint8_t *)ALIGN_PTR((uintptr_t)buffer, 32); - fb.setBuffer(alignedBuff); + uint8_t *buffer = (uint8_t *) malloc(framesize + FB_ALIGNMENT); + uint8_t *aligned_buffer = (uint8_t *) ALIGN_PTR((uintptr_t) buffer, FB_ALIGNMENT); + fb.setBuffer(aligned_buffer); } uint8_t *framebuffer = fb.getBuffer(); - // Ensure FB is aligned to 32 bytes cache lines. - if ((uint32_t) framebuffer & 0x1F) { + // Ensure that the framebuffer is aligned. + if ((uint32_t) framebuffer & (FB_ALIGNMENT - 1)) { if (_debug) { - _debug->println("Framebuffer not aligned to 32 bytes cache lines"); + _debug->println("The framebuffer memory is not aligned!"); } return -1; } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy