From 6eb860e00ef8fa54e391eeb7cd3aa451152b91fa Mon Sep 17 00:00:00 2001 From: "Kevin.xie" Date: Wed, 20 Jul 2022 16:39:33 +0800 Subject: [PATCH 1/3] v4l2test: Use formal pagefilp calling sequence to increase display fps. Signed-off-by: Kevin.xie --- package/starfive/v4l2_test/v4l2_test.c | 79 ++++++++++++++++++++++---- 1 file changed, 68 insertions(+), 11 deletions(-) diff --git a/package/starfive/v4l2_test/v4l2_test.c b/package/starfive/v4l2_test/v4l2_test.c index 33b19d3e..9b776027 100644 --- a/package/starfive/v4l2_test/v4l2_test.c +++ b/package/starfive/v4l2_test/v4l2_test.c @@ -436,19 +436,19 @@ static int frameRead(void) break; case IO_METHOD_MMAP: + { stf_v4l2_dequeue_buffer(pv4l2_param, &buf); LOG(STF_LEVEL_LOG, "buf.index=%d, n_buffers=%d\n", buf.index, gp_cfg_param->v4l2_param.n_buffers); imageProcess((uint8_t *)(pv4l2_param->pBuffers[buf.index].start), dst, buf.timestamp); stf_v4l2_queue_buffer(pv4l2_param, buf.index); if (STF_DISP_DRM == gp_cfg_param->disp_type) { - drmModePageFlip(gp_cfg_param->drm_param.fd, gp_cfg_param->drm_param.dev_head->crtc_id, - gp_cfg_param->drm_param.dev_head->bufs[0].fb_id, - DRM_MODE_PAGE_FLIP_EVENT, gp_cfg_param->drm_param.dev_head); + g_drm_buf_next_idx = buf.index; } LOG(STF_LEVEL_LOG, "buf.index: %d, buf.bytesused=%d\n", buf.index, buf.bytesused); - break; + break; + } case IO_METHOD_USERPTR: stf_v4l2_dequeue_buffer(pv4l2_param, &buf); imageProcess((uint8_t *)(buf.m.userptr), dst, buf.timestamp); @@ -528,6 +528,27 @@ static void page_flip_handler(int fd, unsigned int frame, DRM_MODE_PAGE_FLIP_EVENT, dev); } +static void mmap_page_flip_handler(int fd, unsigned int frame, + unsigned int sec, unsigned int usec, + void *data) +{ + uint8_t *dst = NULL; + dst = gp_cfg_param->drm_param.dev_head->bufs[0].buf; + + if(g_drm_buf_next_idx != -1) { + LOG(STF_LEVEL_TRACE, "Index %d\n", g_drm_buf_curr_idx); + /* TODO: write mmap region here instead of in frameRead*/ + + g_drm_buf_curr_idx = g_drm_buf_next_idx; + g_drm_buf_next_idx = -1; + } + + //LOG(STF_LEVEL_TRACE, "page filp index 0 \n"); + drmModePageFlip(gp_cfg_param->drm_param.fd, gp_cfg_param->drm_param.dev_head->crtc_id, + gp_cfg_param->drm_param.dev_head->bufs[0].fb_id, + DRM_MODE_PAGE_FLIP_EVENT, gp_cfg_param->drm_param.dev_head); +} + static void mainloop() { struct v4l2_buffer buf; @@ -538,14 +559,30 @@ static void mainloop() uint32_t nfds = 0; LOG(STF_LEVEL_TRACE, "Enter\n"); - if (STF_DISP_FB == gp_cfg_param->disp_type || - IO_METHOD_MMAP == gp_cfg_param->io_mthd) { - // fb or (drm + mmap) + if (STF_DISP_FB == gp_cfg_param->disp_type) { + // fb nfds = 1; fds = (struct pollfd*)malloc(sizeof(struct pollfd) * nfds); memset(fds, 0, sizeof(struct pollfd) * nfds); fds[0].fd = gp_cfg_param->v4l2_param.fd; fds[0].events = POLLIN; + + } else if (STF_DISP_DRM == gp_cfg_param->disp_type && + IO_METHOD_MMAP == gp_cfg_param->io_mthd) { + // drm + mmap + nfds = 2; + fds = (struct pollfd*)malloc(sizeof(struct pollfd) * nfds); + memset(fds, 0, sizeof(struct pollfd) * nfds); + fds[0].fd = gp_cfg_param->v4l2_param.fd; + fds[0].events = POLLIN; + fds[1].fd = gp_cfg_param->drm_param.fd; + fds[1].events = POLLIN; + + memset(&ev, 0, sizeof ev); + ev.version = DRM_EVENT_CONTEXT_VERSION; + ev.vblank_handler = NULL; + ev.page_flip_handler = mmap_page_flip_handler; + } else if (STF_DISP_DRM == gp_cfg_param->disp_type && IO_METHOD_DMABUF == gp_cfg_param->io_mthd) { // (drm + dmabuf) @@ -589,13 +626,33 @@ static void mainloop() break; } - if (STF_DISP_FB == gp_cfg_param->disp_type || - IO_METHOD_MMAP == gp_cfg_param->io_mthd) { - // fb or (drm + mmap) + if (STF_DISP_FB == gp_cfg_param->disp_type) { + // fb if (fds[0].revents & POLLIN) { frameRead(); calc_frame_fps(); } + } else if (STF_DISP_DRM == gp_cfg_param->disp_type && + IO_METHOD_MMAP == gp_cfg_param->io_mthd) { + // drm + mmap + if (fds[0].revents & POLLIN) { + frameRead(); + calc_frame_fps(); + } + + if (fds[1].revents & POLLIN) { + static struct timespec ts_roll_in; + static struct timespec after_handle; + clock_gettime(CLOCK_MONOTONIC,&ts_roll_in); + + drmHandleEvent(gp_cfg_param->drm_param.fd, &ev); + + clock_gettime(CLOCK_MONOTONIC,&after_handle); + if(ts_roll_in.tv_sec == after_handle.tv_sec){ + unsigned long interval = (ts_roll_in.tv_nsec - after_handle.tv_nsec) / 1000; + //LOG(STF_LEVEL_LOG, "drmHandleEvent cost %u us\n", interval); + } + } } else if (STF_DISP_DRM == gp_cfg_param->disp_type && IO_METHOD_DMABUF == gp_cfg_param->io_mthd) { // drm + dmabuf @@ -618,7 +675,7 @@ static void mainloop() count = 3; } - usleep(1 * 1000); + //usleep(1 * 1000); } if (fds) { From 520be2db62fe945f7443d16be7640de2cbe8dcb3 Mon Sep 17 00:00:00 2001 From: "Kevin.xie" Date: Fri, 22 Jul 2022 15:35:17 +0800 Subject: [PATCH 2/3] v4l2test: Add pingpong buffer to solve blurred screen problem Signed-off-by: Kevin.xie --- package/starfive/v4l2_test/v4l2_test.c | 35 ++++++-------------------- 1 file changed, 8 insertions(+), 27 deletions(-) diff --git a/package/starfive/v4l2_test/v4l2_test.c b/package/starfive/v4l2_test/v4l2_test.c index 9b776027..53dc98a9 100644 --- a/package/starfive/v4l2_test/v4l2_test.c +++ b/package/starfive/v4l2_test/v4l2_test.c @@ -59,6 +59,7 @@ ConfigParam_t *gp_cfg_param = NULL; static int g_drm_buf_next_idx = -1; static int g_drm_buf_curr_idx = 0; +static int drm_buf_id = 0; static void alloc_default_config(ConfigParam_t **pp_data) { @@ -403,12 +404,15 @@ static int frameRead(void) struct v4l2_buffer buf; V4l2Param_t *pv4l2_param = &gp_cfg_param->v4l2_param; uint8_t *dst = NULL; + int next_index; if (STF_DISP_FB == gp_cfg_param->disp_type) { dst = gp_cfg_param->fb_param.screen_buf; } else if (STF_DISP_DRM == gp_cfg_param->disp_type && IO_METHOD_DMABUF != gp_cfg_param->io_mthd) { - dst = gp_cfg_param->drm_param.dev_head->bufs[0].buf; + //Get ready to compose the backgound buffer + next_index = (drm_buf_id + 1) % 2; + dst = gp_cfg_param->drm_param.dev_head->bufs[next_index].buf; } else { LOG(STF_LEVEL_LOG, "Not display\n"); } @@ -441,12 +445,11 @@ static int frameRead(void) LOG(STF_LEVEL_LOG, "buf.index=%d, n_buffers=%d\n", buf.index, gp_cfg_param->v4l2_param.n_buffers); imageProcess((uint8_t *)(pv4l2_param->pBuffers[buf.index].start), dst, buf.timestamp); - stf_v4l2_queue_buffer(pv4l2_param, buf.index); if (STF_DISP_DRM == gp_cfg_param->disp_type) { - g_drm_buf_next_idx = buf.index; + drm_buf_id = next_index; // Move background buffer to foreground } + stf_v4l2_queue_buffer(pv4l2_param, buf.index); LOG(STF_LEVEL_LOG, "buf.index: %d, buf.bytesused=%d\n", buf.index, buf.bytesused); - break; } case IO_METHOD_USERPTR: @@ -532,20 +535,8 @@ static void mmap_page_flip_handler(int fd, unsigned int frame, unsigned int sec, unsigned int usec, void *data) { - uint8_t *dst = NULL; - dst = gp_cfg_param->drm_param.dev_head->bufs[0].buf; - - if(g_drm_buf_next_idx != -1) { - LOG(STF_LEVEL_TRACE, "Index %d\n", g_drm_buf_curr_idx); - /* TODO: write mmap region here instead of in frameRead*/ - - g_drm_buf_curr_idx = g_drm_buf_next_idx; - g_drm_buf_next_idx = -1; - } - - //LOG(STF_LEVEL_TRACE, "page filp index 0 \n"); drmModePageFlip(gp_cfg_param->drm_param.fd, gp_cfg_param->drm_param.dev_head->crtc_id, - gp_cfg_param->drm_param.dev_head->bufs[0].fb_id, + gp_cfg_param->drm_param.dev_head->bufs[drm_buf_id].fb_id, DRM_MODE_PAGE_FLIP_EVENT, gp_cfg_param->drm_param.dev_head); } @@ -641,17 +632,7 @@ static void mainloop() } if (fds[1].revents & POLLIN) { - static struct timespec ts_roll_in; - static struct timespec after_handle; - clock_gettime(CLOCK_MONOTONIC,&ts_roll_in); - drmHandleEvent(gp_cfg_param->drm_param.fd, &ev); - - clock_gettime(CLOCK_MONOTONIC,&after_handle); - if(ts_roll_in.tv_sec == after_handle.tv_sec){ - unsigned long interval = (ts_roll_in.tv_nsec - after_handle.tv_nsec) / 1000; - //LOG(STF_LEVEL_LOG, "drmHandleEvent cost %u us\n", interval); - } } } else if (STF_DISP_DRM == gp_cfg_param->disp_type && IO_METHOD_DMABUF == gp_cfg_param->io_mthd) { From 29d0679078f18409a9dc9255e673ea4427755ca2 Mon Sep 17 00:00:00 2001 From: "Kevin.xie" Date: Mon, 25 Jul 2022 14:13:56 +0800 Subject: [PATCH 3/3] v4l2test: Do not show the remains frame from last DRM display process Signed-off-by: Kevin.xie --- package/starfive/v4l2_test/stf_drm.c | 11 -------- package/starfive/v4l2_test/v4l2_test.c | 35 +++++++++++++++++++++++++- 2 files changed, 34 insertions(+), 12 deletions(-) diff --git a/package/starfive/v4l2_test/stf_drm.c b/package/starfive/v4l2_test/stf_drm.c index 789114b7..2adec176 100644 --- a/package/starfive/v4l2_test/stf_drm.c +++ b/package/starfive/v4l2_test/stf_drm.c @@ -384,17 +384,6 @@ LOG(STF_LEVEL_INFO, "width=%d,height=%d,pitch=%d,bo_handle=%d,fb_id=%d\n", dev->saved_crtc = drmModeGetCrtc(fd, dev->crtc_id); /* must store crtc data */ - /* First buffer to DRM */ - if (drmModeSetCrtc(fd, dev->crtc_id, dev->bufs[0].fb_id, 0, 0, - &dev->conn_id, 1, &dev->mode)) { - fatal("drmModeSetCrtc() failed"); - } - - /* First flip */ - drmModePageFlip(fd, dev->crtc_id, - dev->bufs[0].fb_id, DRM_MODE_PAGE_FLIP_EVENT, - dev); - LOG(STF_LEVEL_TRACE, "Exit\n"); } diff --git a/package/starfive/v4l2_test/v4l2_test.c b/package/starfive/v4l2_test/v4l2_test.c index 53dc98a9..5981d27c 100644 --- a/package/starfive/v4l2_test/v4l2_test.c +++ b/package/starfive/v4l2_test/v4l2_test.c @@ -419,6 +419,7 @@ static int frameRead(void) switch (pv4l2_param->io_mthd) { case IO_METHOD_READ: + { if (-1 == v4l2_read(pv4l2_param->fd, pv4l2_param->pBuffers[0].start, pv4l2_param->pBuffers[0].length)) { switch (errno) { @@ -438,7 +439,7 @@ static int frameRead(void) timestamp.tv_usec = ts.tv_nsec/1000; imageProcess((uint8_t *)(pv4l2_param->pBuffers[0].start), dst, timestamp); break; - + } case IO_METHOD_MMAP: { stf_v4l2_dequeue_buffer(pv4l2_param, &buf); @@ -447,16 +448,33 @@ static int frameRead(void) imageProcess((uint8_t *)(pv4l2_param->pBuffers[buf.index].start), dst, buf.timestamp); if (STF_DISP_DRM == gp_cfg_param->disp_type) { drm_buf_id = next_index; // Move background buffer to foreground + static int first_frame = 1; + if(first_frame) { + drm_dev_t* dev = gp_cfg_param->drm_param.dev_head; + /* First buffer to DRM */ + if (drmModeSetCrtc(gp_cfg_param->drm_param.fd, + dev->crtc_id, dev->bufs[drm_buf_id].fb_id, + 0, 0, &dev->conn_id, 1, &dev->mode)) { + fatal("drmModeSetCrtc() failed"); + } + /* First flip */ + drmModePageFlip(gp_cfg_param->drm_param.fd, + dev->crtc_id, dev->bufs[drm_buf_id].fb_id, + DRM_MODE_PAGE_FLIP_EVENT, dev); + first_frame = 0; + } } stf_v4l2_queue_buffer(pv4l2_param, buf.index); LOG(STF_LEVEL_LOG, "buf.index: %d, buf.bytesused=%d\n", buf.index, buf.bytesused); break; } case IO_METHOD_USERPTR: + { stf_v4l2_dequeue_buffer(pv4l2_param, &buf); imageProcess((uint8_t *)(buf.m.userptr), dst, buf.timestamp); stf_v4l2_queue_buffer(pv4l2_param, buf.index); break; + } case IO_METHOD_DMABUF: default: break; @@ -640,6 +658,21 @@ static void mainloop() if (fds[0].revents & POLLIN) { int dequeued = stf_v4l2_dequeue_buffer(&gp_cfg_param->v4l2_param, &buf); if (dequeued) { + static int first_frame = 1; + if(first_frame) { + drm_dev_t* dev = gp_cfg_param->drm_param.dev_head; + /* First buffer to DRM */ + if (drmModeSetCrtc(gp_cfg_param->drm_param.fd, + dev->crtc_id, dev->bufs[buf.index].fb_id, + 0, 0, &dev->conn_id, 1, &dev->mode)) { + fatal("drmModeSetCrtc() failed"); + } + /* First flip */ + drmModePageFlip(gp_cfg_param->drm_param.fd, + dev->crtc_id, dev->bufs[buf.index].fb_id, + DRM_MODE_PAGE_FLIP_EVENT, dev); + first_frame = 0; + } g_drm_buf_next_idx = buf.index; frameRead(); // TODO: add support for save file later calc_frame_fps();