Skip to content

Commit 1baf93b

Browse files
epc-akekartben
authored andcommitted
drivers: video: Add timeout tovideo_buffer_alloc
This PR fixes a blocking call to video_buffer_alloc in case of memory shortage by addign a timeout parameter to the API. Signed-off-by: Armin Kessler <[email protected]>
1 parent adaa87a commit 1baf93b

File tree

7 files changed

+16
-10
lines changed

7 files changed

+16
-10
lines changed

doc/releases/migration-guide-4.1.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ Device Drivers and Devicetree
7575

7676
* :c:struct:`adc_driver_api`
7777

78+
* The :c:func:`video_buffer_alloc` and :c:func:`video_buffer_aligned_alloc` functions in the
79+
video API now take an additional timeout parameter.
80+
7881
ADC
7982
===
8083

drivers/video/video_common.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ struct mem_block {
3131

3232
static struct mem_block video_block[CONFIG_VIDEO_BUFFER_POOL_NUM_MAX];
3333

34-
struct video_buffer *video_buffer_aligned_alloc(size_t size, size_t align)
34+
struct video_buffer *video_buffer_aligned_alloc(size_t size, size_t align, k_timeout_t timeout)
3535
{
3636
struct video_buffer *vbuf = NULL;
3737
struct mem_block *block;
@@ -51,7 +51,7 @@ struct video_buffer *video_buffer_aligned_alloc(size_t size, size_t align)
5151
}
5252

5353
/* Alloc buffer memory */
54-
block->data = VIDEO_COMMON_HEAP_ALLOC(align, size, K_FOREVER);
54+
block->data = VIDEO_COMMON_HEAP_ALLOC(align, size, timeout);
5555
if (block->data == NULL) {
5656
return NULL;
5757
}
@@ -63,9 +63,9 @@ struct video_buffer *video_buffer_aligned_alloc(size_t size, size_t align)
6363
return vbuf;
6464
}
6565

66-
struct video_buffer *video_buffer_alloc(size_t size)
66+
struct video_buffer *video_buffer_alloc(size_t size, k_timeout_t timeout)
6767
{
68-
return video_buffer_aligned_alloc(size, sizeof(void *));
68+
return video_buffer_aligned_alloc(size, sizeof(void *), timeout);
6969
}
7070

7171
void video_buffer_release(struct video_buffer *vbuf)

include/zephyr/drivers/video.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,19 +732,21 @@ static inline int video_set_signal(const struct device *dev, enum video_endpoint
732732
*
733733
* @param size Size of the video buffer (in bytes).
734734
* @param align Alignment of the requested memory, must be a power of two.
735+
* @param timeout Timeout duration or K_NO_WAIT
735736
*
736737
* @retval pointer to allocated video buffer
737738
*/
738-
struct video_buffer *video_buffer_aligned_alloc(size_t size, size_t align);
739+
struct video_buffer *video_buffer_aligned_alloc(size_t size, size_t align, k_timeout_t timeout);
739740

740741
/**
741742
* @brief Allocate video buffer.
742743
*
743744
* @param size Size of the video buffer (in bytes).
745+
* @param timeout Timeout duration or K_NO_WAIT
744746
*
745747
* @retval pointer to allocated video buffer
746748
*/
747-
struct video_buffer *video_buffer_alloc(size_t size);
749+
struct video_buffer *video_buffer_alloc(size_t size, k_timeout_t timeout);
748750

749751
/**
750752
* @brief Release a video buffer.

samples/drivers/video/capture/src/main.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ int main(void)
211211
* For some hardwares, such as the PxP used on i.MX RT1170 to do image rotation,
212212
* buffer alignment is needed in order to achieve the best performance
213213
*/
214-
buffers[i] = video_buffer_aligned_alloc(bsize, CONFIG_VIDEO_BUFFER_POOL_ALIGN);
214+
buffers[i] = video_buffer_aligned_alloc(bsize, CONFIG_VIDEO_BUFFER_POOL_ALIGN,
215+
K_FOREVER);
215216
if (buffers[i] == NULL) {
216217
LOG_ERR("Unable to alloc video buffer");
217218
return 0;

samples/drivers/video/capture_to_lvgl/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ int main(void)
9797

9898
/* Alloc video buffers and enqueue for capture */
9999
for (i = 0; i < ARRAY_SIZE(buffers); i++) {
100-
buffers[i] = video_buffer_alloc(bsize);
100+
buffers[i] = video_buffer_alloc(bsize, K_FOREVER);
101101
if (buffers[i] == NULL) {
102102
LOG_ERR("Unable to alloc video buffer");
103103
return 0;

samples/drivers/video/tcpserversink/src/main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ int main(void)
105105

106106
/* Alloc Buffers */
107107
for (i = 0; i < ARRAY_SIZE(buffers); i++) {
108-
buffers[i] = video_buffer_alloc(fmt.pitch * fmt.height);
108+
buffers[i] = video_buffer_alloc(fmt.pitch * fmt.height, K_FOREVER);
109109
if (buffers[i] == NULL) {
110110
LOG_ERR("Unable to alloc video buffer");
111111
return 0;

tests/drivers/video/api/src/video_emul.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ ZTEST(video_common, test_video_vbuf)
157157
zexpect_ok(video_set_format(rx_dev, VIDEO_EP_OUT, &fmt));
158158

159159
/* Allocate a buffer, assuming prj.conf gives enough memory for it */
160-
vbuf = video_buffer_alloc(fmt.pitch * fmt.height);
160+
vbuf = video_buffer_alloc(fmt.pitch * fmt.height, K_FOREVER);
161161
zexpect_not_null(vbuf);
162162

163163
/* Start the virtual hardware */

0 commit comments

Comments
 (0)