介紹
rv1126使用libdrm作為顯示操作介面,相關說明介紹參考如下:
- RK官方WIKI網站:http://opensource.rock-chips.com/wiki_Graphics;
- rv1126_1109/docs/Linux/Graphics/Rockchip_Developer_Guide_Linux_Graphics_CN.pdf;
《Rockchip_Developer_Guide_Linux_Graphics_CN.pdf》檔案中,DRM介紹如下:
- Connectors,就是螢屏,比如主控芯片通過HDMI、MIPI DSI,分別接2個螢屏顯示,就會有2個對應的Connectors ID;
- CRTCs,表示VOP,一個螢屏一般對應一個crtc;
- Planes,就是圖層,比如視頻層在plane2,UI在plane1,視頻在UI上面;
可以通過modetest程式查看系統當前DRM情況:
# modetest
Encoders:
id crtc type possible crtcs possible clones
56 0 DSI 0x00000001 0x00000000
Connectors:
id encoder status name size (mm) modes encoders
57 0 connected DSI-1 107x172 1 56
modes:
name refresh (Hz) hdisp hss hse htot vdisp vss vse vtot)
800x1280 60 800 818 819 837 1280 1292 1293 1305 66000 flags: phsync, pvsync; type: preferred, driver
props:
......
CRTCs:
id fb pos size
54 0 (0,0) (0x0)
0 0 0 0 0 0 0 0 0 0 flags: ; type:
props:
......
Planes:
id crtc fb CRTC x,y x,y gamma size possible crtcs
53 54 61 0,0 0,0 0 0x00000001
formats: XR24 AR24 XB24 AB24 RG24 BG24 RG16 BG16
props:
9 type:
flags: immutable enum
enums: Overlay=0 Primary=1 Cursor=2
value: 1
51 FEATURE:
flags: immutable bitmask
values: scale=0x1 alpha=0x2 hdr2sdr=0x4 sdr2hdr=0x8 afbdc=0x10
value: 2
55 0 0 0,0 0,0 0 0x00000001
formats: XR24 AR24 XB24 AB24 RG24 BG24 RG16 BG16 NV12 NV16 NV24
props:
9 type:
flags: immutable enum
enums: Overlay=0 Primary=1 Cursor=2
value: 0
51 FEATURE:
flags: immutable bitmask
values: scale=0x1 alpha=0x2 hdr2sdr=0x4 sdr2hdr=0x8 afbdc=0x10
value: 3
Frame buffers:
id size pitch
上面資訊表明當前系統的DRM情況:
- Connectors,有1個,代表可以接1個螢屏輸出,ID是57,該顯示輸出的最大解析度是800x1280,重繪頻率60hz;
- CRTCs,有1個,ID是54;
- Planes,有2個,代表有2個圖層,ID分別是53、55,53是Primary層,55是Overlay層;
可以用指令測驗螢屏輸出顯示:modetest -s 57@54:800x1280@RG24:
- 57,對應Connectors ID;
- 54,對應CRTCs ID;
- 800x1280,輸出最大解析度;
- RG24,代表RGB888,每個像素點24bit,R/G/B各8bit;
使用RK_MPI輸出
單板上是比較老的RK SDK包,新購買的2020-1212版本SDK包支持RK_MPI介面輸出影像到螢屏,將新SDK包編譯后,將libeasymedia.so和librga.so更新到單板即可,
編譯時,lib庫引數:
-lpthread -ldl -lm -lpcre -lz -lglib-2.0 -leasymedia -ldrm -lrockchip_mpp -lavformat -lavcodec -lswresample -lavutil -lliveMedia -lgroupsock -lBasicUsageEnvironment -lUsageEnvironment -lasound -lv4l2 -lv4lconvert -lrga -lRKAP_ANR -lRKAP_Common -lRKAP_AEC -lrknn_api -lrockface -lsqlite3 -lmd_share -lrkaiq -lssl -lcrypto -lrknn_runtime -lod_share -lrockx -lOpenVX -lVSC -lGAL -lArchModelSw -lNNArchPerf
原始碼實作從sensor獲取影像,輸出到螢屏顯示,同時將一路輸入存盤到檔案:
// Copyright 2020 Fuzhou Rockchip Electronics Co., Ltd. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <assert.h>
#include <fcntl.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <unistd.h>
#include <pthread.h>
//#include "common/sample_common.h"
#include "rkmedia_api.h"
static bool quit = false;
// 信號處理程式
static void sigterm_handler(int sig)
{
fprintf(stderr, "signal %d\n", sig);
quit = true;
}
// 影像資料處理執行緒
static void *GetMediaBuffer(void *arg)
{
RGA_CHN rga_chn = *(RGA_CHN *)arg;
char save_path[512];
//
printf("#Start %s thread, rga_chn:%d\n", __func__, rga_chn);
sprintf(save_path, "/userdata/output_vo_%d.nv12", rga_chn);
//
FILE *save_file = fopen(save_path, "w");
if (!save_file)
printf("ERROR: Open %s failed!\n", save_path);
MEDIA_BUFFER mb = NULL;
int save_cnt = 0;
int recv_len;
while (!quit)
{
mb = RK_MPI_SYS_GetMediaBuffer(RK_ID_RGA, rga_chn, 50);
if (!mb)
{
if (!quit)
{
continue;
}
printf("chn-%d:RK_MPI_SYS_GetMediaBuffer get null buffer!\n", rga_chn);
break;
}
recv_len = RK_MPI_MB_GetSize(mb);
printf("Get Frame-chn-%d:ptr:%p, fd:%d, size:%zu, mode:%d, channel:%d, "
"timestamp:%lld\n",
rga_chn,
RK_MPI_MB_GetPtr(mb), RK_MPI_MB_GetFD(mb), recv_len,
RK_MPI_MB_GetModeID(mb), RK_MPI_MB_GetChannelID(mb),
RK_MPI_MB_GetTimestamp(mb));
if (save_file && (save_cnt < 1))
{
int rtn = fwrite(RK_MPI_MB_GetPtr(mb), 1, recv_len, save_file);
fsync(fileno(save_file));
printf("#Save frame-chn-%d:%d to %s, rtn = %d\n", rga_chn, save_cnt, save_path, rtn);
save_cnt++;
}
RK_MPI_MB_ReleaseBuffer(mb);
}
if (save_file)
fclose(save_file);
printf("%s-chn-%d - exit\r\n", __func__, rga_chn);
return NULL;
}
//
int main()
{
int ret = -1;
//
VI_PIPE vi_pipe_0 = 0;
VI_CHN vi_chn_0 = 0;
//
RGA_CHN rga_chn_0 = 0;
//
VO_CHN vo_chn_0 = 0;
// 初始化mpi sys
RK_MPI_SYS_Init();
//
// 資料源,ISP20的輸出:
// rkispp_m_bypass, 不支持設定解析度,不支持縮放, NV12/NV16/YUYV/FBC0/FBC2
// rkispp_scale0, max width: 3264,最大支持 8 倍縮放, NV12/NV16/YUYV
// rkispp_scale1, max width: 1280,最大支持 8 倍縮放, NV12/NV16/YUYV
// rkispp_scale2, max width: 1280,最大支持 8 倍縮放, NV12/NV16/YUYV
//
VI_CHN_ATTR_S vi_chn_attr;
vi_chn_attr.pcVideoNode = "rkispp_scale0";
vi_chn_attr.u32BufCnt = 4;
vi_chn_attr.u32Width = 1920;
vi_chn_attr.u32Height = 1080;
vi_chn_attr.enPixFmt = IMAGE_TYPE_NV12;
vi_chn_attr.enWorkMode = VI_WORK_MODE_NORMAL;
//
ret = RK_MPI_VI_SetChnAttr(vi_pipe_0, vi_chn_0, &vi_chn_attr);
ret |= RK_MPI_VI_EnableChn(vi_pipe_0, vi_chn_0);
if (ret)
{
printf("Create vi[%d] failed! ret=%d\n", vi_chn_0, ret);
return -1;
}
printf("### Create VI DONE\r\n");
//
// 輸出圖層:
// VO_PLANE_PRIMARY, XR24 AR24 XB24 AB24 RG24 BG24 RG16 BG16
// VO_PLANE_OVERLAY, XR24 AR24 XB24 AB24 RG24 BG24 RG16 BG16 NV12 NV16 NV24
//
// 創建VO:VO_PLANE_OVERLAY
// 全屏
VO_CHN_ATTR_S stVoAttr_0 = {0};
stVoAttr_0.pcDevNode = "/dev/dri/card0";
stVoAttr_0.emPlaneType = VO_PLANE_OVERLAY;
stVoAttr_0.enImgType = IMAGE_TYPE_NV12;
stVoAttr_0.u16Zpos = 0;
stVoAttr_0.stDispRect.s32X = 0;
stVoAttr_0.stDispRect.s32Y = 0;
stVoAttr_0.stDispRect.u32Width = 800;
stVoAttr_0.stDispRect.u32Height = 1280;
ret = RK_MPI_VO_CreateChn(vo_chn_0, &stVoAttr_0);
if (ret)
{
goto EXIT_0;
}
printf("### Create VO_PLANE_OVERLAY DONE\r\n");
//
// 輸出的RGA
// 旋轉 270 度
// 縮放 1920 -> 1280
// 縮放 1080 -> 800
//
RGA_ATTR_S stRgaAttr_0;
stRgaAttr_0.bEnBufPool = RK_TRUE;
stRgaAttr_0.u16BufPoolCnt = 4;
stRgaAttr_0.u16Rotaion = 270;
stRgaAttr_0.stImgIn.u32X = 0;
stRgaAttr_0.stImgIn.u32Y = 0;
stRgaAttr_0.stImgIn.imgType = IMAGE_TYPE_NV12;
stRgaAttr_0.stImgIn.u32Width = 1920;
stRgaAttr_0.stImgIn.u32Height = 1080;
stRgaAttr_0.stImgIn.u32HorStride = 1920;
stRgaAttr_0.stImgIn.u32VirStride = 1080;
stRgaAttr_0.stImgOut.u32X = 0;
stRgaAttr_0.stImgOut.u32Y = 0;
stRgaAttr_0.stImgOut.imgType = IMAGE_TYPE_NV12;
stRgaAttr_0.stImgOut.u32Width = 800;
stRgaAttr_0.stImgOut.u32Height = 1280;
stRgaAttr_0.stImgOut.u32HorStride = 800;
stRgaAttr_0.stImgOut.u32VirStride = 1280;
//
ret = RK_MPI_RGA_CreateChn(rga_chn_0, &stRgaAttr_0);
if (ret)
{
printf("Create rga[%d] falied! ret=%d\n", rga_chn_0, ret);
goto EXIT_1;
}
printf("### Create RGA DONE\r\n");
//
// 系結VI輸入
//
// 系結源資料配置
MPP_CHN_S stSrcChn_0;
stSrcChn_0.enModId = RK_ID_VI;
stSrcChn_0.s32DevId = vi_pipe_0;
stSrcChn_0.s32ChnId = vi_chn_0;
//
// 系結目標資料配置
MPP_CHN_S stDestChn_0;
stDestChn_0.enModId = RK_ID_RGA;
stDestChn_0.s32DevId = 0;
stDestChn_0.s32ChnId = rga_chn_0;
//
ret = RK_MPI_SYS_Bind(&stSrcChn_0, &stDestChn_0);
if (ret)
{
printf("Bind vi[0] and rga[%d] failed! ret=%d\n", rga_chn_0, ret);
goto EXIT_2;
}
printf("### Bind VI-RGA DONE\r\n");
//
// 系結VO輸出
//
// 系結源資料配置
MPP_CHN_S stSrcChn_1;
stSrcChn_1.enModId = RK_ID_RGA;
stSrcChn_1.s32DevId = 0;
stSrcChn_1.s32ChnId = rga_chn_0;
//
// 系結目標資料配置
MPP_CHN_S stDestChn_1;
stDestChn_1.enModId = RK_ID_VO;
stDestChn_1.s32DevId = 0;
stDestChn_1.s32ChnId = vo_chn_0;
//
ret = RK_MPI_SYS_Bind(&stSrcChn_1, &stDestChn_1);
if (ret)
{
printf("Bind v0[0] and rga[%d] failed! ret=%d\n", rga_chn_0, ret);
goto EXIT_3;
}
printf("### Bind RGA-VO DONE\r\n");
//
// 順帶把VO顯示的圖,存盤下來
pthread_t read_thread_1;
pthread_create(&read_thread_1, NULL, GetMediaBuffer, &rga_chn_0);
//
// 監聽退出信號
//
printf("%s initial finish\n", __func__);
signal(SIGINT, sigterm_handler);
//
// 等待退出信號
while (!quit)
{
usleep(100);
}
//
// 退出
//
ret = 0;
EXIT_4:
RK_MPI_SYS_UnBind(&stSrcChn_1, &stDestChn_1);
EXIT_3:
RK_MPI_SYS_UnBind(&stSrcChn_0, &stDestChn_0);
EXIT_2:
RK_MPI_RGA_DestroyChn(rga_chn_0);
EXIT_1:
RK_MPI_VO_DestroyChn(vo_chn_0);
EXIT_0:
RK_MPI_VI_DisableChn(vi_pipe_0, vi_chn_0);
return ret;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/237551.html
標籤:其他
