Skip to content

Commit ee827fd

Browse files
committed
drivers: video: Expose buffers request instead of buffers alloc
The video subsystem holds an internal buffer pool and allocate buffers of this pool on its own heap. So, the application should not call alloc_buffers() but request_buffers() instead. This lets the subsystem controls the buffer pool via index. Request is not cumulative. Each new request fully replaces the old one. This is the essential change compared to the old code where buffers are allowed to be allocated at different time and hence, cannot be controlled by index if allocation is done in different code modules. Signed-off-by: Phi Bang Nguyen <[email protected]>
1 parent 04ac445 commit ee827fd

File tree

3 files changed

+41
-50
lines changed

3 files changed

+41
-50
lines changed

drivers/video/video_common.c

Lines changed: 29 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,26 +100,49 @@ void video_buffer_release(struct video_buffer *vbuf)
100100
}
101101
}
102102

103-
int video_enqueue(const struct device *dev, struct video_buffer *buf)
103+
/* To be completed */
104+
int video_request_buffers(uint8_t count, size_t size, enum video_buf_type type)
105+
{
106+
struct video_buffer *buffer;
107+
108+
/* TODO: Input request vs Output request for m2m devices */
109+
if (count > CONFIG_VIDEO_BUFFER_POOL_NUM_MAX) {
110+
return -EINVAL;
111+
}
112+
113+
/* TODO: Buffer request is not cumulative. For each new request, free all the buffers
114+
* that are previously requested
115+
*/
116+
117+
/* Allocate buffers */
118+
for (uint8_t i = 0; i < count; i++) {
119+
buffer =
120+
video_buffer_aligned_alloc(size, CONFIG_VIDEO_BUFFER_POOL_ALIGN, K_FOREVER);
121+
buffer->type = type;
122+
buffer->index = i;
123+
}
124+
125+
return 0;
126+
}
127+
128+
int video_enqueue(const struct device *dev, uint8_t index)
104129
{
105130
const struct video_driver_api *api = (const struct video_driver_api *)dev->api;
106131

107132
__ASSERT_NO_MSG(dev != NULL);
108-
__ASSERT_NO_MSG(buf != NULL);
109-
__ASSERT_NO_MSG(buf->buffer != NULL);
110133

111134
api = (const struct video_driver_api *)dev->api;
112135
if (api->enqueue == NULL) {
113136
return -ENOSYS;
114137
}
115138

116-
if (video_buf[buf->index].state != VIDEO_BUF_STATE_DONE) {
139+
if (video_buf[index].state != VIDEO_BUF_STATE_DONE) {
117140
return -EINVAL;
118141
}
119142

120-
video_buf[buf->index].state = VIDEO_BUF_STATE_QUEUED;
143+
video_buf[index].state = VIDEO_BUF_STATE_QUEUED;
121144

122-
return api->enqueue(dev, &video_buf[buf->index]);
145+
return api->enqueue(dev, &video_buf[index]);
123146
}
124147

125148
int video_format_caps_index(const struct video_format_cap *fmts, const struct video_format *fmt,

include/zephyr/drivers/video.h

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -583,13 +583,13 @@ static inline int video_enum_frmival(const struct device *dev, struct video_frmi
583583
* endpoint incoming queue.
584584
*
585585
* @param dev Pointer to the device structure for the driver instance.
586-
* @param buf Pointer to the video buffer.
586+
* @param index Index of the buffer to be queued
587587
*
588588
* @retval 0 Is successful.
589589
* @retval -EINVAL If parameters are invalid.
590590
* @retval -EIO General input / output error.
591591
*/
592-
int video_enqueue(const struct device *dev, struct video_buffer *buf);
592+
int video_enqueue(const struct device *dev, uint8_t index);
593593

594594
/**
595595
* @brief Dequeue a video buffer.
@@ -895,33 +895,7 @@ static inline int video_get_selection(const struct device *dev, struct video_sel
895895
return api->get_selection(dev, sel);
896896
}
897897

898-
/**
899-
* @brief Allocate aligned video buffer.
900-
*
901-
* @param size Size of the video buffer (in bytes).
902-
* @param align Alignment of the requested memory, must be a power of two.
903-
* @param timeout Timeout duration or K_NO_WAIT
904-
*
905-
* @retval pointer to allocated video buffer
906-
*/
907-
struct video_buffer *video_buffer_aligned_alloc(size_t size, size_t align, k_timeout_t timeout);
908-
909-
/**
910-
* @brief Allocate video buffer.
911-
*
912-
* @param size Size of the video buffer (in bytes).
913-
* @param timeout Timeout duration or K_NO_WAIT
914-
*
915-
* @retval pointer to allocated video buffer
916-
*/
917-
struct video_buffer *video_buffer_alloc(size_t size, k_timeout_t timeout);
918-
919-
/**
920-
* @brief Release a video buffer.
921-
*
922-
* @param buf Pointer to the video buffer to release.
923-
*/
924-
void video_buffer_release(struct video_buffer *buf);
898+
int video_request_buffers(uint8_t count, size_t size, enum video_buf_type type);
925899

926900
/**
927901
* @brief Search for a format that matches in a list of capabilities

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

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -279,21 +279,15 @@ int main(void)
279279
bsize = fmt.pitch * caps.min_line_count;
280280
}
281281

282-
/* Alloc video buffers and enqueue for capture */
282+
err = video_request_buffers(ARRAY_SIZE(buffers), bsize, VIDEO_BUF_TYPE_OUTPUT);
283+
if (err) {
284+
LOG_ERR("Unable to request video buf");
285+
return 0;
286+
}
287+
288+
/* Enqueue buffers for capture */
283289
for (i = 0; i < ARRAY_SIZE(buffers); i++) {
284-
/*
285-
* For some hardwares, such as the PxP used on i.MX RT1170 to do image rotation,
286-
* buffer alignment is needed in order to achieve the best performance
287-
*/
288-
buffers[i] = video_buffer_aligned_alloc(bsize, CONFIG_VIDEO_BUFFER_POOL_ALIGN,
289-
K_FOREVER);
290-
if (buffers[i] == NULL) {
291-
LOG_ERR("Unable to alloc video buffer");
292-
return 0;
293-
}
294-
buffers[i]->type = type;
295-
buffers[i]->index = i;
296-
video_enqueue(video_dev, buffers[i]);
290+
video_enqueue(video_dev, i);
297291
}
298292

299293
/* Start video capture */
@@ -326,7 +320,7 @@ int main(void)
326320
video_display_frame(display_dev, vbuf, fmt);
327321
#endif
328322

329-
err = video_enqueue(video_dev, vbuf);
323+
err = video_enqueue(video_dev, vbuf->index);
330324
if (err) {
331325
LOG_ERR("Unable to requeue video buf");
332326
return 0;

0 commit comments

Comments
 (0)