From 94a26f967ed7787bbea8d6b2ac8a19ffaacc77fe Mon Sep 17 00:00:00 2001 From: "Xu, Zhengguo" Date: Mon, 15 Apr 2024 15:33:12 +0800 Subject: [PATCH] [Decode] Test shared surface copy between two media instances Signed-off-by: Xu, Zhengguo --- decode/Makefile.am | 6 +- decode/meson.build | 3 + decode/vacopysharedsurface.cpp | 179 +++++++++++++++++++++++++++++++++ 3 files changed, 187 insertions(+), 1 deletion(-) create mode 100644 decode/vacopysharedsurface.cpp diff --git a/decode/Makefile.am b/decode/Makefile.am index 8ea92522..9b8b2c91 100644 --- a/decode/Makefile.am +++ b/decode/Makefile.am @@ -20,7 +20,7 @@ # TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE # SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. -bin_PROGRAMS = mpeg2vldemo loadjpeg +bin_PROGRAMS = mpeg2vldemo loadjpeg vacopysharedsurface AM_CPPFLAGS = \ -Wall \ @@ -40,6 +40,10 @@ TEST_LIBS = \ mpeg2vldemo_LDADD = $(TEST_LIBS) mpeg2vldemo_SOURCES = mpeg2vldemo.cpp +vacopysharedsurface_LDADD = $(TEST_LIBS) +vacopysharedsurface_SOURCES = vacopysharedsurface.cpp + + loadjpeg_LDADD = $(TEST_LIBS) loadjpeg_SOURCES = loadjpeg.c tinyjpeg.c diff --git a/decode/meson.build b/decode/meson.build index d0c1d1b0..1512393f 100644 --- a/decode/meson.build +++ b/decode/meson.build @@ -1,6 +1,9 @@ executable('mpeg2vldemo', [ 'mpeg2vldemo.cpp' ], dependencies: libva_display_dep, install: true) +executable('vacopysharedsurface', [ 'vacopysharedsurface.cpp' ], + dependencies: libva_display_dep, + install: true) executable('loadjpeg', [ 'loadjpeg.c', 'tinyjpeg.c' ], dependencies: libva_display_dep, install: true) diff --git a/decode/vacopysharedsurface.cpp b/decode/vacopysharedsurface.cpp new file mode 100644 index 00000000..8a66303d --- /dev/null +++ b/decode/vacopysharedsurface.cpp @@ -0,0 +1,179 @@ +/* + * Copyright (c) 2024 Intel Corporation. All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the + * "Software"), to deal in the Software without restriction, including + * without limitation the rights to use, copy, modify, merge, publish, + * distribute, sub license, and/or sell copies of the Software, and to + * permit persons to whom the Software is furnished to do so, subject to + * the following conditions: + * + * The above copyright notice and this permission notice (including the + * next paragraph) shall be included in all copies or substantial portions + * of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. + * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR + * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, + * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE + * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "va_display.h" +#include +#include + +#define CHECK_VASTATUS(va_status,func) \ +if (va_status != VA_STATUS_SUCCESS) { \ + fprintf(stderr,"%s:%s (%d) failed,exit\n", __func__, func, __LINE__); \ + exit(1); \ +} + + +#define CLIP_WIDTH 1920 +#define CLIP_HEIGHT 1920 + +VADisplay openDriver(char *device_paths) +{ + int drm_fd = open(device_paths, O_RDWR); + if (drm_fd < 0) + { + printf("####INFO: device %s is invalid !\n", device_paths); + return NULL; + } + + VADisplay va_dpy = vaGetDisplayDRM(drm_fd); + if(va_dpy) + return va_dpy; + + close(drm_fd); + drm_fd = -1; + return 0; +} + +int main(int argc, char **argv) +{ + VASurfaceID surface_id_0, surface_id_1, surface_id_2; + int major_ver, minor_ver; + VADisplay va_dpy_1, va_dpy_2; + VAStatus va_status; + + va_init_display_args(&argc, argv); + va_dpy_1 = openDriver(argv[1]); + va_status = vaInitialize(va_dpy_1, &major_ver, &minor_ver); + assert(va_status == VA_STATUS_SUCCESS); + + va_dpy_2 = openDriver(argv[1]); + va_status = vaInitialize(va_dpy_2, &major_ver, &minor_ver); + assert(va_status == VA_STATUS_SUCCESS); + +////////////////////////////vaCopy in va_dpy_1//////////////////////////////// + va_status = vaCreateSurfaces( + va_dpy_1, + VA_RT_FORMAT_YUV420, CLIP_WIDTH, CLIP_HEIGHT, + &surface_id_0, 1, + NULL, 0 + ); + CHECK_VASTATUS(va_status, "vaCreateSurfaces"); + + va_status = vaCreateSurfaces( + va_dpy_1, + VA_RT_FORMAT_YUV420, CLIP_WIDTH, CLIP_HEIGHT, + &surface_id_1, 1, + NULL, 0 + ); + CHECK_VASTATUS(va_status, "vaCreateSurfaces"); + + //vaCopy + VACopyObject dst; + dst.obj_type = VACopyObjectSurface; + dst.object.surface_id = surface_id_1; + VACopyObject src; + src.obj_type = VACopyObjectSurface; + src.object.surface_id = surface_id_0; + VACopyOption option; + option.bits.va_copy_sync = VA_EXEC_SYNC; + option.bits.va_copy_mode = VA_EXEC_MODE_DEFAULT; + va_status = vaCopy(va_dpy_1, &dst, &src, option); + CHECK_VASTATUS(va_status, "vaCopy"); + + //va_status = vaSyncSurface(va_dpy_1, surface_id_1); + //CHECK_VASTATUS(va_status, "vaSyncSurface"); + + //vaExportSurface to get surface prime_fd + VADRMPRIMESurfaceDescriptor desc; + memset(&desc, 0, sizeof(VADRMPRIMESurfaceDescriptor)); + va_status = vaExportSurfaceHandle(va_dpy_1, surface_id_1, VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2, 0, &desc); +////////////////////////////end vaCopy in va_dpy_1///////////////////////////////////////////////////////////// + +////////////////////////////copy out surface from va_dpy_1 to another surface in va_dpy_2//////////// + ////import surface from va_dpy_1 and create output surface + VASurfaceAttrib surf_attrib[2]; + + surf_attrib[0].type = VASurfaceAttribMemoryType; + surf_attrib[0].value.type = VAGenericValueTypeInteger; + surf_attrib[0].flags = VA_SURFACE_ATTRIB_SETTABLE; + surf_attrib[0].value.value.i = VA_SURFACE_ATTRIB_MEM_TYPE_DRM_PRIME_2; + + surf_attrib[1].type = VASurfaceAttribExternalBufferDescriptor; + surf_attrib[1].value.type = VAGenericValueTypePointer; + surf_attrib[1].flags = VA_SURFACE_ATTRIB_SETTABLE; + surf_attrib[1].value.value.p = &desc; + va_status = vaCreateSurfaces( + va_dpy_2, + VA_RT_FORMAT_YUV420, CLIP_WIDTH, CLIP_HEIGHT, + &surface_id_2, 1, + surf_attrib, 2 + ); + CHECK_VASTATUS(va_status, "vaCreateSurfaces"); + + VASurfaceID surface_id_3; + va_status = vaCreateSurfaces( + va_dpy_2, + VA_RT_FORMAT_YUV420, CLIP_WIDTH, CLIP_HEIGHT, + &surface_id_3, 1, + NULL, 0 + ); + CHECK_VASTATUS(va_status, "vaCreateSurfaces"); + + //vaCopy + dst.obj_type = VACopyObjectSurface; + dst.object.surface_id = surface_id_3; + src.obj_type = VACopyObjectSurface; + src.object.surface_id = surface_id_2; + option.bits.va_copy_sync = VA_EXEC_SYNC; + option.bits.va_copy_mode = VA_EXEC_MODE_DEFAULT; + va_status = vaCopy(va_dpy_2, &dst, &src, option); + CHECK_VASTATUS(va_status, "vaCopy"); + + va_status = vaSyncSurface(va_dpy_2, surface_id_2); + va_status = vaSyncSurface(va_dpy_2, surface_id_3); + CHECK_VASTATUS(va_status, "vaSyncSurface"); +////////////////////////////end copy////////////////////////////////////////////////////////////////// + + vaDestroySurfaces(va_dpy_1, &surface_id_0, 1); + vaDestroySurfaces(va_dpy_1, &surface_id_1, 1); + vaDestroySurfaces(va_dpy_2, &surface_id_2, 1); + vaDestroySurfaces(va_dpy_2, &surface_id_3, 1); + + vaTerminate(va_dpy_1); + vaTerminate(va_dpy_2); + va_close_display(va_dpy_1); + va_close_display(va_dpy_2); + return 0; +}