Skip to content

Commit 4930554

Browse files
authored
Merge pull request #870 from iabdalkader/camera_fb_align
Camera: Fix framebuffer malloc alignment.
2 parents 6a1f8da + eb6f97e commit 4930554

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

libraries/Camera/src/camera.cpp

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727

2828
#define ALIGN_PTR(p,a) ((p & (a-1)) ?(((uintptr_t)p + a) & ~(uintptr_t)(a-1)) : p)
2929

30+
#ifdef __SCB_DCACHE_LINE_SIZE
31+
#define FB_ALIGNMENT __SCB_DCACHE_LINE_SIZE
32+
#else
33+
#define FB_ALIGNMENT 32
34+
#endif
35+
3036
// Include all image sensor drivers here.
3137
#if defined (ARDUINO_PORTENTA_H7_M7)
3238

@@ -337,15 +343,16 @@ FrameBuffer::FrameBuffer(int32_t x, int32_t y, int32_t bpp) :
337343
_fb_size(x*y*bpp),
338344
_isAllocated(true)
339345
{
340-
uint8_t *buffer = (uint8_t *)malloc(x*y*bpp);
341-
_fb = (uint8_t *)ALIGN_PTR((uintptr_t)buffer, 32);
346+
uint8_t *buffer = (uint8_t *) malloc(x * y * bpp + FB_ALIGNMENT);
347+
_fb = (uint8_t *) ALIGN_PTR((uintptr_t) buffer, FB_ALIGNMENT);
342348
}
343349

344350
FrameBuffer::FrameBuffer(int32_t address) :
345351
_fb_size(0),
346-
_isAllocated(true)
352+
_isAllocated(true),
353+
_fb((uint8_t *) address)
347354
{
348-
_fb = (uint8_t *)ALIGN_PTR((uintptr_t)address, 32);
355+
// Assume that `address` is aligned, this will be verified later in grabFrame.
349356
}
350357

351358
FrameBuffer::FrameBuffer() :
@@ -688,17 +695,17 @@ int Camera::grabFrame(FrameBuffer &fb, uint32_t timeout)
688695
}
689696
}
690697
} else {
691-
uint8_t *buffer = (uint8_t *)malloc(framesize+32);
692-
uint8_t *alignedBuff = (uint8_t *)ALIGN_PTR((uintptr_t)buffer, 32);
693-
fb.setBuffer(alignedBuff);
698+
uint8_t *buffer = (uint8_t *) malloc(framesize + FB_ALIGNMENT);
699+
uint8_t *aligned_buffer = (uint8_t *) ALIGN_PTR((uintptr_t) buffer, FB_ALIGNMENT);
700+
fb.setBuffer(aligned_buffer);
694701
}
695702

696703
uint8_t *framebuffer = fb.getBuffer();
697704

698-
// Ensure FB is aligned to 32 bytes cache lines.
699-
if ((uint32_t) framebuffer & 0x1F) {
705+
// Ensure that the framebuffer is aligned.
706+
if ((uint32_t) framebuffer & (FB_ALIGNMENT - 1)) {
700707
if (_debug) {
701-
_debug->println("Framebuffer not aligned to 32 bytes cache lines");
708+
_debug->println("The framebuffer memory is not aligned!");
702709
}
703710
return -1;
704711
}

0 commit comments

Comments
 (0)
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