Skip to content

Commit aa4e169

Browse files
jc-kynesimpopcornmix
authored andcommitted
media: v4l2-mem2mem: allow request job buffer processing after job finish
Allow the capture buffer to be detached from a v4l2 request job such that another job can start before the capture buffer is returned. This allows h/w codecs that can process multiple requests at the same time to operate more efficiently. Signed-off-by: John Cox <jc@kynesim.co.uk>
1 parent 4a0f238 commit aa4e169

3 files changed

Lines changed: 148 additions & 6 deletions

File tree

‎drivers/media/v4l2-core/v4l2-mem2mem.c‎

Lines changed: 98 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -404,15 +404,18 @@ static void v4l2_m2m_cancel_job(struct v4l2_m2m_ctx *m2m_ctx)
404404
{
405405
struct v4l2_m2m_dev *m2m_dev;
406406
unsigned long flags;
407+
bool det_abort_req;
407408

408409
m2m_dev = m2m_ctx->m2m_dev;
409410
spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
410411

412+
det_abort_req = !list_empty(&m2m_ctx->det_list);
411413
m2m_ctx->job_flags |= TRANS_ABORT;
412414
if (m2m_ctx->job_flags & TRANS_RUNNING) {
413415
spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
414416
if (m2m_dev->m2m_ops->job_abort)
415417
m2m_dev->m2m_ops->job_abort(m2m_ctx->priv);
418+
det_abort_req = false;
416419
dprintk("m2m_ctx %p running, will wait to complete\n", m2m_ctx);
417420
wait_event(m2m_ctx->finished,
418421
!(m2m_ctx->job_flags & TRANS_RUNNING));
@@ -426,6 +429,11 @@ static void v4l2_m2m_cancel_job(struct v4l2_m2m_ctx *m2m_ctx)
426429
/* Do nothing, was not on queue/running */
427430
spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
428431
}
432+
433+
/* Wait for detached buffers to come back too */
434+
if (det_abort_req && m2m_dev->m2m_ops->job_abort)
435+
m2m_dev->m2m_ops->job_abort(m2m_ctx->priv);
436+
wait_event(m2m_ctx->det_empty, list_empty(&m2m_ctx->det_list));
429437
}
430438

431439
/*
@@ -463,6 +471,7 @@ static bool _v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev,
463471

464472
list_del(&m2m_dev->curr_ctx->queue);
465473
m2m_dev->curr_ctx->job_flags &= ~(TRANS_QUEUED | TRANS_RUNNING);
474+
m2m_ctx->cap_detached = false;
466475
wake_up(&m2m_dev->curr_ctx->finished);
467476
m2m_dev->curr_ctx = NULL;
468477
return true;
@@ -490,6 +499,80 @@ void v4l2_m2m_job_finish(struct v4l2_m2m_dev *m2m_dev,
490499
}
491500
EXPORT_SYMBOL(v4l2_m2m_job_finish);
492501

502+
struct vb2_v4l2_buffer *_v4l2_m2m_cap_buf_detach(struct v4l2_m2m_ctx *m2m_ctx)
503+
{
504+
struct vb2_v4l2_buffer *buf;
505+
506+
buf = v4l2_m2m_dst_buf_remove(m2m_ctx);
507+
list_add_tail(&container_of(buf, struct v4l2_m2m_buffer, vb)->list,
508+
&m2m_ctx->det_list);
509+
m2m_ctx->cap_detached = true;
510+
buf->is_held = true;
511+
buf->det_state = VB2_BUF_STATE_ACTIVE;
512+
513+
return buf;
514+
}
515+
516+
struct vb2_v4l2_buffer *v4l2_m2m_cap_buf_detach(struct v4l2_m2m_dev *m2m_dev,
517+
struct v4l2_m2m_ctx *m2m_ctx)
518+
{
519+
unsigned long flags;
520+
struct vb2_v4l2_buffer *src_buf, *dst_buf;
521+
522+
spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
523+
524+
dst_buf = NULL;
525+
src_buf = v4l2_m2m_next_src_buf(m2m_ctx);
526+
527+
if (!(src_buf->flags & V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF) &&
528+
!m2m_ctx->cap_detached)
529+
dst_buf = _v4l2_m2m_cap_buf_detach(m2m_ctx);
530+
531+
spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
532+
return dst_buf;
533+
}
534+
EXPORT_SYMBOL(v4l2_m2m_cap_buf_detach);
535+
536+
static void _v4l2_m2m_cap_buf_return(struct v4l2_m2m_ctx *m2m_ctx,
537+
struct vb2_v4l2_buffer *buf,
538+
enum vb2_buffer_state state)
539+
{
540+
buf->det_state = state;
541+
542+
/*
543+
* Always signal done in the order we got stuff
544+
* Stop if we find a buf that is still in use
545+
*/
546+
while (!list_empty(&m2m_ctx->det_list)) {
547+
buf = &list_first_entry(&m2m_ctx->det_list,
548+
struct v4l2_m2m_buffer, list)->vb;
549+
state = buf->det_state;
550+
if (state != VB2_BUF_STATE_DONE &&
551+
state != VB2_BUF_STATE_ERROR)
552+
return;
553+
list_del(&container_of(buf, struct v4l2_m2m_buffer, vb)->list);
554+
buf->det_state = VB2_BUF_STATE_DEQUEUED;
555+
v4l2_m2m_buf_done(buf, state);
556+
}
557+
wake_up(&m2m_ctx->det_empty);
558+
}
559+
560+
void v4l2_m2m_cap_buf_return(struct v4l2_m2m_dev *m2m_dev,
561+
struct v4l2_m2m_ctx *m2m_ctx,
562+
struct vb2_v4l2_buffer *buf,
563+
enum vb2_buffer_state state)
564+
{
565+
unsigned long flags;
566+
567+
if (!buf)
568+
return;
569+
570+
spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
571+
_v4l2_m2m_cap_buf_return(m2m_ctx, buf, state);
572+
spin_unlock_irqrestore(&m2m_dev->job_spinlock, flags);
573+
}
574+
EXPORT_SYMBOL(v4l2_m2m_cap_buf_return);
575+
493576
void v4l2_m2m_buf_done_and_job_finish(struct v4l2_m2m_dev *m2m_dev,
494577
struct v4l2_m2m_ctx *m2m_ctx,
495578
enum vb2_buffer_state state)
@@ -500,14 +583,21 @@ void v4l2_m2m_buf_done_and_job_finish(struct v4l2_m2m_dev *m2m_dev,
500583

501584
spin_lock_irqsave(&m2m_dev->job_spinlock, flags);
502585
src_buf = v4l2_m2m_src_buf_remove(m2m_ctx);
503-
dst_buf = v4l2_m2m_next_dst_buf(m2m_ctx);
504586

505-
if (WARN_ON(!src_buf || !dst_buf))
587+
if (WARN_ON(!src_buf))
506588
goto unlock;
507-
dst_buf->is_held = src_buf->flags & V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF;
508-
if (!dst_buf->is_held) {
509-
v4l2_m2m_dst_buf_remove(m2m_ctx);
510-
v4l2_m2m_buf_done(dst_buf, state);
589+
if (!m2m_ctx->cap_detached) {
590+
dst_buf = v4l2_m2m_next_dst_buf(m2m_ctx);
591+
if (WARN_ON(!dst_buf))
592+
goto unlock;
593+
594+
dst_buf->is_held = src_buf->flags
595+
& V4L2_BUF_FLAG_M2M_HOLD_CAPTURE_BUF;
596+
597+
if (!dst_buf->is_held) {
598+
dst_buf = _v4l2_m2m_cap_buf_detach(m2m_ctx);
599+
_v4l2_m2m_cap_buf_return(m2m_ctx, dst_buf, state);
600+
}
511601
}
512602
/*
513603
* If the request API is being used, returning the OUTPUT
@@ -1145,12 +1235,14 @@ struct v4l2_m2m_ctx *v4l2_m2m_ctx_init(struct v4l2_m2m_dev *m2m_dev,
11451235
m2m_ctx->priv = drv_priv;
11461236
m2m_ctx->m2m_dev = m2m_dev;
11471237
init_waitqueue_head(&m2m_ctx->finished);
1238+
init_waitqueue_head(&m2m_ctx->det_empty);
11481239

11491240
out_q_ctx = &m2m_ctx->out_q_ctx;
11501241
cap_q_ctx = &m2m_ctx->cap_q_ctx;
11511242

11521243
INIT_LIST_HEAD(&out_q_ctx->rdy_queue);
11531244
INIT_LIST_HEAD(&cap_q_ctx->rdy_queue);
1245+
INIT_LIST_HEAD(&m2m_ctx->det_list);
11541246
spin_lock_init(&out_q_ctx->rdy_spinlock);
11551247
spin_lock_init(&cap_q_ctx->rdy_spinlock);
11561248

‎include/media/v4l2-mem2mem.h‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,9 @@ struct v4l2_m2m_queue_ctx {
9292
* %TRANS_QUEUED, %TRANS_RUNNING and %TRANS_ABORT.
9393
* @finished: Wait queue used to signalize when a job queue finished.
9494
* @priv: Instance private data
95+
* @cap_detached: Current job's capture buffer has been detached
96+
* @det_list: List of detached (post-job but still in flight) capture buffers
97+
* @det_empty: Wait queue signalled when det_list goes empty
9598
*
9699
* The memory to memory context is specific to a file handle, NOT to e.g.
97100
* a device.
@@ -120,6 +123,11 @@ struct v4l2_m2m_ctx {
120123
wait_queue_head_t finished;
121124

122125
void *priv;
126+
127+
/* Detached buffer handling */
128+
bool cap_detached;
129+
struct list_head det_list;
130+
wait_queue_head_t det_empty;
123131
};
124132

125133
/**
@@ -304,6 +312,45 @@ v4l2_m2m_is_last_draining_src_buf(struct v4l2_m2m_ctx *m2m_ctx,
304312
void v4l2_m2m_last_buffer_done(struct v4l2_m2m_ctx *m2m_ctx,
305313
struct vb2_v4l2_buffer *vbuf);
306314

315+
/**
316+
* v4l2_m2m_cap_buf_detach() - detach the capture buffer from the job and
317+
* return it.
318+
*
319+
* @m2m_dev: opaque pointer to the internal data to handle M2M context
320+
* @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
321+
*
322+
* This function is designed to be used in conjunction with
323+
* v4l2_m2m_buf_done_and_job_finish(). It allows the next job to start
324+
* execution before the capture buffer is returned to the user which can be
325+
* important if the underlying processing has multiple phases that are more
326+
* efficiently executed in parallel.
327+
*
328+
* If used then it must be called before v4l2_m2m_buf_done_and_job_finish()
329+
* as otherwise the buffer will have already gone.
330+
*
331+
* It is the callers reponsibilty to ensure that all detached buffers are
332+
* returned.
333+
*/
334+
struct vb2_v4l2_buffer *v4l2_m2m_cap_buf_detach(struct v4l2_m2m_dev *m2m_dev,
335+
struct v4l2_m2m_ctx *m2m_ctx);
336+
337+
/**
338+
* v4l2_m2m_cap_buf_return() - return a capture buffer, previously detached
339+
* with v4l2_m2m_cap_buf_detach() to the user.
340+
*
341+
* @m2m_dev: opaque pointer to the internal data to handle M2M context
342+
* @m2m_ctx: m2m context assigned to the instance given by struct &v4l2_m2m_ctx
343+
* @buf: the buffer to return
344+
* @state: vb2 buffer state passed to v4l2_m2m_buf_done().
345+
*
346+
* Buffers returned by this function will be returned to the user in the order
347+
* of the original jobs rather than the order in which this function is called.
348+
*/
349+
void v4l2_m2m_cap_buf_return(struct v4l2_m2m_dev *m2m_dev,
350+
struct v4l2_m2m_ctx *m2m_ctx,
351+
struct vb2_v4l2_buffer *buf,
352+
enum vb2_buffer_state state);
353+
307354
/**
308355
* v4l2_m2m_reqbufs() - multi-queue-aware REQBUFS multiplexer
309356
*

‎include/media/videobuf2-v4l2.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
* @request_fd: the request_fd associated with this buffer
3636
* @is_held: if true, then this capture buffer was held
3737
* @planes: plane information (userptr/fd, length, bytesused, data_offset).
38+
* @det_state: if a detached request capture buffer then this contains its
39+
* current state
3840
*
3941
* Should contain enough information to be able to cover all the fields
4042
* of &struct v4l2_buffer at ``videodev2.h``.
@@ -49,6 +51,7 @@ struct vb2_v4l2_buffer {
4951
__s32 request_fd;
5052
bool is_held;
5153
struct vb2_plane planes[VB2_MAX_PLANES];
54+
enum vb2_buffer_state det_state;
5255
};
5356

5457
/* VB2 V4L2 flags as set in vb2_queue.subsystem_flags */

0 commit comments

Comments
 (0)