Skip to content

Commit

Permalink
Merge pull request #870 from iabdalkader/camera_fb_align
Browse files Browse the repository at this point in the history
Camera: Fix framebuffer malloc alignment.
  • Loading branch information
pennam authored Apr 19, 2024
2 parents 6a1f8da + eb6f97e commit 4930554
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions libraries/Camera/src/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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() :
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 4930554

Please sign in to comment.