xref: /OK3568_Linux_fs/external/rockit/mpi/example/common/test_comm_tde.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /*
2  * Copyright 2021 Rockchip Electronics Co. LTD
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #ifdef __cplusplus
17 #if __cplusplus
18 extern "C" {
19 #endif
20 #endif /* End of #ifdef __cplusplus */
21 
22 #include <pthread.h>
23 #include <string.h>
24 #include <cerrno>
25 #include "test_comm_tde.h"
26 #include "test_comm_imgproc.h"
27 #include "test_comm_sys.h"
28 #include "test_comm_utils.h"
29 #include "rk_mpi_tde.h"
30 #include "rk_mpi_cal.h"
31 #include "rk_mpi_mb.h"
32 #include "rk_mpi_mmz.h"
33 #include "rk_mpi_sys.h"
34 #include "rk_common.h"
35 #include "rk_debug.h"
36 
TEST_TDE_Save_DstFrame(TEST_TDE_PROC_CTX_S * pstCtx)37 RK_S32 TEST_TDE_Save_DstFrame(TEST_TDE_PROC_CTX_S *pstCtx) {
38     RK_S32 s32Ret = RK_SUCCESS;
39     MB_PIC_CAL_S stCalResult;
40     PIC_BUF_ATTR_S stPicBufAttr;
41     RK_U32 u32DstWidth = pstCtx->pstDst.u32Width;
42     RK_U32 u32DstHeight = pstCtx->pstDst.u32Height;
43     RK_U32 u32DstPixelFormat = pstCtx->pstDst.enColorFmt;
44     RK_S32 s32DstCompressMode = pstCtx->pstDst.enComprocessMode;;
45     stPicBufAttr.u32Width = u32DstWidth;
46     stPicBufAttr.u32Height = u32DstHeight;
47     stPicBufAttr.enPixelFormat = (PIXEL_FORMAT_E)u32DstPixelFormat;
48     stPicBufAttr.enCompMode = (COMPRESS_MODE_E)s32DstCompressMode;
49 
50     s32Ret = RK_MPI_CAL_VGS_GetPicBufferSize(&stPicBufAttr, &stCalResult);
51     if (pstCtx->dstFileName != RK_NULL) {
52         FILE *fp = fopen(pstCtx->dstFileName, "wb+");
53         if (fp == RK_NULL) {
54             RK_LOGE("fopen %s failed, error: %s", pstCtx->dstFileName, strerror(errno));
55             return RK_FAILURE;
56         }
57         fwrite(RK_MPI_MB_Handle2VirAddr(pstCtx->pstDst.pMbBlk),
58                 1, stCalResult.u32MBSize, fp);
59         fflush(fp);
60         fclose(fp);
61     }
62     return s32Ret;
63 }
64 
TEST_TDE_TransSurfaceToVideoFrame(TEST_TDE_PROC_CTX_S * pstCtx,VIDEO_FRAME_INFO_S * pstFrames)65 RK_S32 TEST_TDE_TransSurfaceToVideoFrame(TEST_TDE_PROC_CTX_S *pstCtx, VIDEO_FRAME_INFO_S *pstFrames) {
66     TDE_SURFACE_S *surface = &pstCtx->pstDst;
67     pstFrames->stVFrame.enCompressMode = surface->enComprocessMode;
68     pstFrames->stVFrame.pMbBlk = surface->pMbBlk;
69     pstFrames->stVFrame.u32Width = surface->u32Width;
70     pstFrames->stVFrame.u32Height = surface->u32Height;
71     pstFrames->stVFrame.u32VirWidth = surface->u32Width;
72     pstFrames->stVFrame.u32VirHeight = surface->u32Height;
73     pstFrames->stVFrame.enPixelFormat = surface->enColorFmt;
74     return RK_SUCCESS;
75 }
76 
TEST_TDE_AddTask(TEST_TDE_PROC_CTX_S * pstCtx,TDE_HANDLE jobHandle)77 RK_S32 TEST_TDE_AddTask(TEST_TDE_PROC_CTX_S *pstCtx, TDE_HANDLE jobHandle) {
78     RK_S32 s32Ret = RK_SUCCESS;
79 
80     switch (pstCtx->opType) {
81         case TDE_OP_QUICK_COPY: {
82           s32Ret = RK_TDE_QuickCopy(jobHandle,
83                 &pstCtx->pstSrc, &pstCtx->pstSrcRect,
84                 &pstCtx->pstDst, &pstCtx->pstDstRect);
85         } break;
86         case TDE_OP_QUICK_RESIZE: {
87           s32Ret = RK_TDE_QuickResize(jobHandle,
88                 &pstCtx->pstSrc, &pstCtx->pstSrcRect,
89                 &pstCtx->pstDst, &pstCtx->pstDstRect);
90         } break;
91         case TDE_OP_QUICK_FILL: {
92           s32Ret = RK_TDE_QuickFill(jobHandle,
93                 &pstCtx->pstDst, &pstCtx->pstDstRect,
94                 pstCtx->fillData);
95         } break;
96         case TDE_OP_ROTATION: {
97           s32Ret = RK_TDE_Rotate(jobHandle,
98                 &pstCtx->pstSrc, &pstCtx->pstSrcRect,
99                 &pstCtx->pstDst, &pstCtx->pstDstRect,
100                 (ROTATION_E)pstCtx->rotateAngle);
101         } break;
102         case TDE_OP_COLOR_KEY:
103         case TDE_OP_MIRROR: {
104           s32Ret = RK_TDE_Bitblit(jobHandle,
105                 &pstCtx->pstDst, &pstCtx->pstDstRect,
106                 &pstCtx->pstSrc, &pstCtx->pstSrcRect,
107                 &pstCtx->pstDst, &pstCtx->pstDstRect,
108                 &pstCtx->stOpt);
109         } break;
110         default: {
111           RK_LOGE("unknown operation type %d", pstCtx->opType);
112         break;
113         }
114     }
115     return s32Ret;
116 }
117 
TEST_TDE_BeginJob()118 TDE_HANDLE TEST_TDE_BeginJob() {
119     TDE_HANDLE jobHandle = -1;
120     jobHandle = RK_TDE_BeginJob();
121     return jobHandle;
122 }
123 
TEST_TDE_EndJob(TDE_HANDLE jobHandle)124 RK_S32 TEST_TDE_EndJob(TDE_HANDLE jobHandle) {
125     RK_S32 s32Ret = RK_SUCCESS;
126     s32Ret = RK_TDE_EndJob(jobHandle, RK_FALSE, RK_TRUE, 10);
127     if (s32Ret != RK_SUCCESS) {
128         RK_TDE_CancelJob(jobHandle);
129         return RK_FAILURE;
130     }
131     RK_TDE_WaitForDone(jobHandle);
132     return s32Ret;
133 }
134 
TEST_TDE_LoadSrcFrame(TEST_TDE_PROC_CTX_S * pstCtx)135 RK_S32 TEST_TDE_LoadSrcFrame(TEST_TDE_PROC_CTX_S *pstCtx) {
136     RK_S32 s32Ret = RK_SUCCESS;
137     PIC_BUF_ATTR_S stPicBufAttr;
138     VIDEO_FRAME_INFO_S stVideoFrame;
139     RK_U8 *pstVideoFrame;
140     RK_U32 u32VirSrcWidth = 0;
141     RK_U32 u32VirSrcHeight = 0;
142     RK_U32 u32SrcWidth = pstCtx->pstSrc.u32Width;
143     RK_U32 u32SrcHeight = pstCtx->pstSrc.u32Height;
144     PIXEL_FORMAT_E u32SrcPixelFormat = pstCtx->pstSrc.enColorFmt;
145     COMPRESS_MODE_E u32SrcCompressMode = pstCtx->pstSrc.enComprocessMode;
146 
147     memset(&stVideoFrame, 0x0, sizeof(VIDEO_FRAME_INFO_S));
148 
149     stPicBufAttr.u32Width = u32SrcWidth;
150     stPicBufAttr.u32Height = u32SrcHeight;
151     stPicBufAttr.enPixelFormat = u32SrcPixelFormat;
152     stPicBufAttr.enCompMode = u32SrcCompressMode;
153     s32Ret = TEST_SYS_CreateVideoFrame(&stPicBufAttr, &stVideoFrame);
154     u32VirSrcWidth = stVideoFrame.stVFrame.u32VirWidth;
155     u32VirSrcHeight = stVideoFrame.stVFrame.u32VirHeight;
156 
157     if (s32Ret != RK_SUCCESS) {
158         goto __FAILED;
159     }
160 
161     pstVideoFrame = (RK_U8 *)RK_MPI_MB_Handle2VirAddr(stVideoFrame.stVFrame.pMbBlk);
162     if (pstCtx->srcFileName != RK_NULL) {
163         s32Ret = TEST_COMM_FileReadOneFrame(pstCtx->srcFileName, &stVideoFrame);
164         if (s32Ret != RK_SUCCESS) {
165             goto __FAILED;
166         }
167     } else {
168         s32Ret = TEST_COMM_FillImage(
169                     pstVideoFrame,
170                     u32SrcWidth, u32SrcHeight,
171                     RK_MPI_CAL_COMM_GetHorStride(u32VirSrcWidth, u32SrcPixelFormat),
172                     u32VirSrcHeight,
173                     u32SrcPixelFormat, 1);
174         if (s32Ret != RK_SUCCESS) {
175             goto __FAILED;
176         }
177     }
178     RK_MPI_SYS_MmzFlushCache(stVideoFrame.stVFrame.pMbBlk, RK_FALSE);
179     pstCtx->pstSrc.pMbBlk = stVideoFrame.stVFrame.pMbBlk;
180 __FAILED:
181     if (s32Ret != RK_SUCCESS) {
182         RK_MPI_MB_ReleaseMB(stVideoFrame.stVFrame.pMbBlk);
183     }
184     return s32Ret;
185 }
186 
TEST_TDE_CreateDstFrame(TEST_TDE_PROC_CTX_S * pstCtx)187 RK_S32 TEST_TDE_CreateDstFrame(TEST_TDE_PROC_CTX_S *pstCtx) {
188     RK_S32 s32Ret = RK_SUCCESS;
189     PIC_BUF_ATTR_S stPicBufAttr;
190     VIDEO_FRAME_INFO_S stVideoFrame;
191 
192     RK_U32 u32DstWidth = pstCtx->pstDst.u32Width;
193     RK_U32 u32DstHeight = pstCtx->pstDst.u32Height;
194     RK_U32 u32DstPixelFormat = pstCtx->pstDst.enColorFmt;
195     RK_S32 s32DstCompressMode = pstCtx->pstDst.enComprocessMode;;
196 
197     stPicBufAttr.u32Width = u32DstWidth;
198     stPicBufAttr.u32Height = u32DstHeight;
199     stPicBufAttr.enPixelFormat = (PIXEL_FORMAT_E)u32DstPixelFormat;
200     stPicBufAttr.enCompMode = (COMPRESS_MODE_E)s32DstCompressMode;
201     s32Ret = TEST_SYS_CreateVideoFrame(&stPicBufAttr, &stVideoFrame);
202     if (s32Ret != RK_SUCCESS) {
203         goto __FAILED;
204     }
205 
206     pstCtx->pstDst.pMbBlk = stVideoFrame.stVFrame.pMbBlk;
207 
208 __FAILED:
209     if (s32Ret != RK_SUCCESS) {
210         RK_MPI_MB_ReleaseMB(stVideoFrame.stVFrame.pMbBlk);
211     }
212     return s32Ret;
213 }
214 
TEST_TDE_ProcessJob(TEST_TDE_PROC_CTX_S * pstCtx,VIDEO_FRAME_INFO_S * pstFrames)215 RK_S32 TEST_TDE_ProcessJob(TEST_TDE_PROC_CTX_S *pstCtx, VIDEO_FRAME_INFO_S *pstFrames) {
216     RK_S32 s32Ret = RK_SUCCESS;
217     TDE_HANDLE jobHandle = TEST_TDE_BeginJob();
218     RK_S32 s32TaskNum = (pstCtx->s32TaskNum == 0) ? 1 : pstCtx->s32TaskNum;
219 
220     TEST_TDE_LoadSrcFrame(pstCtx);
221     TEST_TDE_CreateDstFrame(pstCtx);
222 
223     for (RK_S32 taskIdx = 0; taskIdx < s32TaskNum; taskIdx++) {
224         s32Ret = TEST_TDE_AddTask(pstCtx, jobHandle);
225         if (s32Ret != RK_SUCCESS) {
226             goto __FAILED;
227         }
228     }
229 
230     s32Ret = TEST_TDE_EndJob(jobHandle);
231     if (s32Ret != RK_SUCCESS) {
232         goto __FAILED;
233     }
234     if (pstFrames) {
235         TEST_TDE_TransSurfaceToVideoFrame(pstCtx, pstFrames);
236     }
237 
238 __FAILED:
239     if (s32Ret != RK_SUCCESS) {
240         RK_MPI_MB_ReleaseMB(pstCtx->pstSrc.pMbBlk);
241         RK_MPI_MB_ReleaseMB(pstCtx->pstDst.pMbBlk);
242     }
243     return s32Ret;
244 }
245 
TEST_TDE_Single_ProcessJob(TEST_TDE_PROC_CTX_S * pstCtx,VIDEO_FRAME_INFO_S * pstFrames)246 RK_S32 TEST_TDE_Single_ProcessJob(TEST_TDE_PROC_CTX_S *pstCtx, VIDEO_FRAME_INFO_S *pstFrames) {
247     RK_S32 s32Ret = RK_SUCCESS;
248     TDE_HANDLE jobHandle = TEST_TDE_BeginJob();
249     RK_S32 s32TaskNum = (pstCtx->s32TaskNum == 0) ? 1 : pstCtx->s32TaskNum;
250 
251     for (RK_S32 taskIdx = 0; taskIdx < s32TaskNum; taskIdx++) {
252         s32Ret = TEST_TDE_AddTask(pstCtx, jobHandle);
253         if (s32Ret != RK_SUCCESS) {
254             goto __FAILED;
255         }
256     }
257 
258     s32Ret = TEST_TDE_EndJob(jobHandle);
259     if (s32Ret != RK_SUCCESS) {
260         goto __FAILED;
261     }
262     if (pstFrames) {
263         TEST_TDE_TransSurfaceToVideoFrame(pstCtx, pstFrames);
264     }
265 
266 __FAILED:
267     if (s32Ret != RK_SUCCESS) {
268         RK_MPI_MB_ReleaseMB(pstCtx->pstSrc.pMbBlk);
269         RK_MPI_MB_ReleaseMB(pstCtx->pstDst.pMbBlk);
270     }
271     return s32Ret;
272 }
273 
274 
TEST_TDE_SingleProc(void * pArgs)275 void* TEST_TDE_SingleProc(void *pArgs) {
276     RK_S32 s32Ret = RK_SUCCESS;
277     TEST_TDE_PROC_CTX_S *pstCtx = reinterpret_cast<TEST_TDE_PROC_CTX_S *>(pArgs);
278     VIDEO_FRAME_INFO_S pstFrames;
279 
280     TEST_TDE_LoadSrcFrame(pstCtx);
281     TEST_TDE_CreateDstFrame(pstCtx);
282     s32Ret = TEST_TDE_Single_ProcessJob(pstCtx, &pstFrames);
283     if (s32Ret != RK_SUCCESS) {
284         goto __FAILED;
285     }
286     TEST_TDE_Save_DstFrame(pstCtx);
287 
288 __FAILED:
289     RK_MPI_MB_ReleaseMB(pstCtx->pstSrc.pMbBlk);
290     RK_MPI_MB_ReleaseMB(pstCtx->pstDst.pMbBlk);
291     return RK_NULL;
292 }
293 
TEST_TDE_MultiProc(void * pArgs)294 void* TEST_TDE_MultiProc(void *pArgs) {
295     RK_S32 s32Ret = RK_SUCCESS;
296     TEST_TDE_PROC_CTX_S *pstCtx = reinterpret_cast<TEST_TDE_PROC_CTX_S *>(pArgs);
297     VIDEO_FRAME_INFO_S pstFrames;
298 
299     TEST_TDE_LoadSrcFrame(pstCtx);
300     TEST_TDE_CreateDstFrame(pstCtx);
301     for (RK_S32 i = 0; i < pstCtx->s32ProcessTimes; i++) {
302         s32Ret = TEST_TDE_ProcessJob(pstCtx, &pstFrames);
303         TEST_TDE_Save_DstFrame(pstCtx);
304     }
305     RK_MPI_MB_ReleaseMB(pstCtx->pstSrc.pMbBlk);
306     RK_MPI_MB_ReleaseMB(pstCtx->pstDst.pMbBlk);
307 
308     return RK_NULL;
309 }
310 
TEST_TDE_MultiTest(TEST_TDE_PROC_CTX_S * ctx)311 RK_S32 TEST_TDE_MultiTest(TEST_TDE_PROC_CTX_S *ctx) {
312     RK_S32 s32Ret = RK_SUCCESS;
313     pthread_t tids[TDE_MAX_JOB_NUM];
314     TEST_TDE_PROC_CTX_S tmpCtx[TDE_MAX_JOB_NUM];
315     RK_S64 s64StartTime = 0ll;
316     RK_S64 s64EndTime = 0ll;
317     s64StartTime = TEST_COMM_GetNowUs();
318 
319     for (RK_S32 jobIndex = 0; jobIndex < ctx->s32JobNum; jobIndex++) {
320         memcpy(&(tmpCtx[jobIndex]), ctx, sizeof(TEST_TDE_PROC_CTX_S));
321         pthread_create(&tids[jobIndex], 0, TEST_TDE_MultiProc, reinterpret_cast<void *>(&tmpCtx[jobIndex]));
322     }
323 
324     for (RK_S32 jobIndex = 0; jobIndex < ctx->s32JobNum; jobIndex++) {
325         void *retval;
326         s32Ret = pthread_join(tids[jobIndex], &retval);
327         if (s32Ret != RK_SUCCESS) {
328             RK_LOGE("tde multi test error test id:%d", tids[jobIndex]);
329             s32Ret = RK_FAILURE;
330         }
331     }
332 
333     s64EndTime = TEST_COMM_GetNowUs();
334     RK_S32 s32TaskNum = (ctx->s32TaskNum == 0) ? 1 : ctx->s32TaskNum;
335     RK_S32 runningTimes = ctx->s32JobNum * s32TaskNum * ctx->s32ProcessTimes;
336 
337     RK_DOUBLE s32fps = (RK_DOUBLE)runningTimes * 1000000 / (s64EndTime - s64StartTime);
338     RK_LOGD("run count:%d, run totalTime:%lld, run fps:%f",
339             runningTimes, (s64EndTime - s64StartTime), s32fps);
340 
341     return s32Ret;
342 }
343 
344 #ifdef __cplusplus
345 #if __cplusplus
346 }
347 #endif
348 #endif /* End of #ifdef __cplusplus */
349