diff options
Diffstat (limited to 'video/out/hwdec')
-rw-r--r-- | video/out/hwdec/dmabuf_interop.h (renamed from video/out/hwdec/hwdec_vaapi.h) | 34 | ||||
-rw-r--r-- | video/out/hwdec/dmabuf_interop_gl.c | 373 | ||||
-rw-r--r-- | video/out/hwdec/dmabuf_interop_pl.c (renamed from video/out/hwdec/hwdec_vaapi_vk.c) | 77 | ||||
-rw-r--r-- | video/out/hwdec/dmabuf_interop_wl.c | 83 | ||||
-rw-r--r-- | video/out/hwdec/hwdec_aimagereader.c | 404 | ||||
-rw-r--r-- | video/out/hwdec/hwdec_cuda.c | 27 | ||||
-rw-r--r-- | video/out/hwdec/hwdec_cuda.h | 3 | ||||
-rw-r--r-- | video/out/hwdec/hwdec_cuda_gl.c | 12 | ||||
-rw-r--r-- | video/out/hwdec/hwdec_cuda_vk.c | 171 | ||||
-rw-r--r-- | video/out/hwdec/hwdec_drmprime.c | 319 | ||||
-rw-r--r-- | video/out/hwdec/hwdec_drmprime_overlay.c | 334 | ||||
-rw-r--r-- | video/out/hwdec/hwdec_ios_gl.m | 222 | ||||
-rw-r--r-- | video/out/hwdec/hwdec_mac_gl.c | 169 | ||||
-rw-r--r-- | video/out/hwdec/hwdec_vaapi.c | 209 | ||||
-rw-r--r-- | video/out/hwdec/hwdec_vaapi_gl.c | 221 | ||||
-rw-r--r-- | video/out/hwdec/hwdec_vt.c | 141 | ||||
-rw-r--r-- | video/out/hwdec/hwdec_vt.h | 63 | ||||
-rw-r--r-- | video/out/hwdec/hwdec_vt_pl.m | 312 | ||||
-rw-r--r-- | video/out/hwdec/hwdec_vulkan.c | 333 |
19 files changed, 3092 insertions, 415 deletions
diff --git a/video/out/hwdec/hwdec_vaapi.h b/video/out/hwdec/dmabuf_interop.h index 471283a0ca..3bf01a0fca 100644 --- a/video/out/hwdec/hwdec_vaapi.h +++ b/video/out/hwdec/dmabuf_interop.h @@ -17,39 +17,41 @@ #pragma once -#include <va/va_drmcommon.h> +#include <libavutil/hwcontext_drm.h> -#include "config.h" -#include "video/vaapi.h" #include "video/out/gpu/hwdec.h" -struct priv_owner { - struct mp_vaapi_ctx *ctx; - VADisplay *display; - int *formats; - bool probing_formats; // temporary during init +struct dmabuf_interop { + bool use_modifiers; + bool composed_layers; bool (*interop_init)(struct ra_hwdec_mapper *mapper, const struct ra_imgfmt_desc *desc); void (*interop_uninit)(const struct ra_hwdec_mapper *mapper); - bool (*interop_map)(struct ra_hwdec_mapper *mapper); + bool (*interop_map)(struct ra_hwdec_mapper *mapper, + struct dmabuf_interop *dmabuf_interop, + bool probing); void (*interop_unmap)(struct ra_hwdec_mapper *mapper); }; -struct priv { +struct dmabuf_interop_priv { int num_planes; struct mp_image layout; - struct ra_tex *tex[4]; + struct ra_tex *tex[AV_DRM_MAX_PLANES]; - VADRMPRIMESurfaceDescriptor desc; + AVDRMFrameDescriptor desc; bool surface_acquired; void *interop_mapper_priv; }; -typedef bool (*vaapi_interop_init)(const struct ra_hwdec *hw); +typedef bool (*dmabuf_interop_init)(const struct ra_hwdec *hw, + struct dmabuf_interop *dmabuf_interop); -bool vaapi_gl_init(const struct ra_hwdec *hw); - -bool vaapi_vk_init(const struct ra_hwdec *hw); +bool dmabuf_interop_gl_init(const struct ra_hwdec *hw, + struct dmabuf_interop *dmabuf_interop); +bool dmabuf_interop_pl_init(const struct ra_hwdec *hw, + struct dmabuf_interop *dmabuf_interop); +bool dmabuf_interop_wl_init(const struct ra_hwdec *hw, + struct dmabuf_interop *dmabuf_interop); diff --git a/video/out/hwdec/dmabuf_interop_gl.c b/video/out/hwdec/dmabuf_interop_gl.c new file mode 100644 index 0000000000..0f6fb89e20 --- /dev/null +++ b/video/out/hwdec/dmabuf_interop_gl.c @@ -0,0 +1,373 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. + */ + +#include "dmabuf_interop.h" + +#include <drm_fourcc.h> +#include <EGL/egl.h> +#include "video/out/opengl/ra_gl.h" + +typedef void* GLeglImageOES; +typedef void *EGLImageKHR; + +// Any EGL_EXT_image_dma_buf_import definitions used in this source file. +#define EGL_LINUX_DMA_BUF_EXT 0x3270 +#define EGL_LINUX_DRM_FOURCC_EXT 0x3271 +#define EGL_DMA_BUF_PLANE0_FD_EXT 0x3272 +#define EGL_DMA_BUF_PLANE0_OFFSET_EXT 0x3273 +#define EGL_DMA_BUF_PLANE0_PITCH_EXT 0x3274 +#define EGL_DMA_BUF_PLANE1_FD_EXT 0x3275 +#define EGL_DMA_BUF_PLANE1_OFFSET_EXT 0x3276 +#define EGL_DMA_BUF_PLANE1_PITCH_EXT 0x3277 +#define EGL_DMA_BUF_PLANE2_FD_EXT 0x3278 +#define EGL_DMA_BUF_PLANE2_OFFSET_EXT 0x3279 +#define EGL_DMA_BUF_PLANE2_PITCH_EXT 0x327A + + +// Any EGL_EXT_image_dma_buf_import definitions used in this source file. +#define EGL_DMA_BUF_PLANE3_FD_EXT 0x3440 +#define EGL_DMA_BUF_PLANE3_OFFSET_EXT 0x3441 +#define EGL_DMA_BUF_PLANE3_PITCH_EXT 0x3442 +#define EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT 0x3443 +#define EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT 0x3444 +#define EGL_DMA_BUF_PLANE1_MODIFIER_LO_EXT 0x3445 +#define EGL_DMA_BUF_PLANE1_MODIFIER_HI_EXT 0x3446 +#define EGL_DMA_BUF_PLANE2_MODIFIER_LO_EXT 0x3447 +#define EGL_DMA_BUF_PLANE2_MODIFIER_HI_EXT 0x3448 +#define EGL_DMA_BUF_PLANE3_MODIFIER_LO_EXT 0x3449 +#define EGL_DMA_BUF_PLANE3_MODIFIER_HI_EXT 0x344A + +struct vaapi_gl_mapper_priv { + GLuint gl_textures[AV_DRM_MAX_PLANES]; + EGLImageKHR images[AV_DRM_MAX_PLANES]; + + const struct ra_format *planes[AV_DRM_MAX_PLANES]; + + EGLImageKHR (EGLAPIENTRY *CreateImageKHR)(EGLDisplay, EGLContext, + EGLenum, EGLClientBuffer, + const EGLint *); + EGLBoolean (EGLAPIENTRY *DestroyImageKHR)(EGLDisplay, EGLImageKHR); + void (EGLAPIENTRY *EGLImageTargetTexture2DOES)(GLenum, GLeglImageOES); + void (EGLAPIENTRY *EGLImageTargetTexStorageEXT)(GLenum, GLeglImageOES, + const GLint *); +}; + +static bool gl_create_textures(struct ra_hwdec_mapper *mapper) +{ + struct dmabuf_interop_priv *p_mapper = mapper->priv; + struct vaapi_gl_mapper_priv *p = p_mapper->interop_mapper_priv; + + GL *gl = ra_gl_get(mapper->ra); + gl->GenTextures(AV_DRM_MAX_PLANES, p->gl_textures); + for (int n = 0; n < p_mapper->num_planes; n++) { + gl->BindTexture(GL_TEXTURE_2D, p->gl_textures[n]); + gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); + gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + gl->TexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + gl->BindTexture(GL_TEXTURE_2D, 0); + + struct ra_tex_params params = { + .dimensions = 2, + .w = mp_image_plane_w(&p_mapper->layout, n), + .h = mp_image_plane_h(&p_mapper->layout, n), + .d = 1, + .format = p->planes[n], + .render_src = true, + .src_linear = true, + }; + + if (params.format->ctype != RA_CTYPE_UNORM) + return false; + + p_mapper->tex[n] = ra_create_wrapped_tex(mapper->ra, ¶ms, + p->gl_textures[n]); + if (!p_mapper->tex[n]) + return false; + } + + return true; +} + +static void gl_delete_textures(const struct ra_hwdec_mapper *mapper) +{ + struct dmabuf_interop_priv *p_mapper = mapper->priv; + struct vaapi_gl_mapper_priv *p = p_mapper->interop_mapper_priv; + + GL *gl = ra_gl_get(mapper->ra); + gl->DeleteTextures(AV_DRM_MAX_PLANES, p->gl_textures); + for (int n = 0; n < AV_DRM_MAX_PLANES; n++) { + p->gl_textures[n] = 0; + ra_tex_free(mapper->ra, &p_mapper->tex[n]); + } +} + +static bool vaapi_gl_mapper_init(struct ra_hwdec_mapper *mapper, + const struct ra_imgfmt_desc *desc) +{ + struct dmabuf_interop_priv *p_mapper = mapper->priv; + struct vaapi_gl_mapper_priv *p = talloc_ptrtype(NULL, p); + p_mapper->interop_mapper_priv = p; + + *p = (struct vaapi_gl_mapper_priv) { + // EGL_KHR_image_base + .CreateImageKHR = (void *)eglGetProcAddress("eglCreateImageKHR"), + .DestroyImageKHR = (void *)eglGetProcAddress("eglDestroyImageKHR"), + }; + if (ra_gl_get(mapper->ra)->es) { + // GL_OES_EGL_image + p->EGLImageTargetTexture2DOES = + (void *)eglGetProcAddress("glEGLImageTargetTexture2DOES"); + } else { + // GL_EXT_EGL_image_storage + p->EGLImageTargetTexStorageEXT = + (void *)eglGetProcAddress("glEGLImageTargetTexStorageEXT"); + } + + if (!p->CreateImageKHR || !p->DestroyImageKHR || + (!p->EGLImageTargetTexture2DOES && !p->EGLImageTargetTexStorageEXT)) { + return false; + } + + static_assert(MP_ARRAY_SIZE(desc->planes) == AV_DRM_MAX_PLANES, ""); + static_assert(MP_ARRAY_SIZE(mapper->tex) == AV_DRM_MAX_PLANES, ""); + + // remember format to allow texture recreation + for (int n = 0; n < desc->num_planes; n++) { + p->planes[n] = desc->planes[n]; + } + if (p->EGLImageTargetTexture2DOES) { + // created only once + if (!gl_create_textures(mapper)) + return false; + } + + return true; +} + +static void vaapi_gl_mapper_uninit(const struct ra_hwdec_mapper *mapper) +{ + struct dmabuf_interop_priv *p_mapper = mapper->priv; + struct vaapi_gl_mapper_priv *p = p_mapper->interop_mapper_priv; + + if (p) { + gl_delete_textures(mapper); + talloc_free(p); + p_mapper->interop_mapper_priv = NULL; + } +} + +#define ADD_ATTRIB(name, value) \ + do { \ + assert(num_attribs + 3 < MP_ARRAY_SIZE(attribs)); \ + attribs[num_attribs++] = (name); \ + attribs[num_attribs++] = (value); \ + attribs[num_attribs] = EGL_NONE; \ + } while(0) + +#define ADD_PLANE_ATTRIBS(nplane) \ + do { \ + const AVDRMPlaneDescriptor *plane = &p_mapper->desc.layers[i].planes[j]; \ + const AVDRMObjectDescriptor *object = \ + &p_mapper->desc.objects[plane->object_index]; \ + ADD_ATTRIB(EGL_DMA_BUF_PLANE ## nplane ## _FD_EXT, object->fd); \ + ADD_ATTRIB(EGL_DMA_BUF_PLANE ## nplane ## _OFFSET_EXT, plane->offset); \ + ADD_ATTRIB(EGL_DMA_BUF_PLANE ## nplane ## _PITCH_EXT, plane->pitch); \ + uint64_t drm_format_modifier = object->format_modifier; \ + if (dmabuf_interop->use_modifiers && \ + drm_format_modifier != DRM_FORMAT_MOD_INVALID) { \ + ADD_ATTRIB(EGL_DMA_BUF_PLANE ## nplane ## _MODIFIER_LO_EXT, \ + drm_format_modifier & 0xfffffffful); \ + ADD_ATTRIB(EGL_DMA_BUF_PLANE ## nplane ## _MODIFIER_HI_EXT, \ + drm_format_modifier >> 32); \ + } \ + } while (0) + +static bool vaapi_gl_map(struct ra_hwdec_mapper *mapper, + struct dmabuf_interop *dmabuf_interop, + bool probing) +{ + struct dmabuf_interop_priv *p_mapper = mapper->priv; + struct vaapi_gl_mapper_priv *p = p_mapper->interop_mapper_priv; + + GL *gl = ra_gl_get(mapper->ra); + + if (p->EGLImageTargetTexStorageEXT) { + if (!gl_create_textures(mapper)) + return false; + } + + for (int i = 0, n = 0; i < p_mapper->desc.nb_layers; i++) { + /* + * As we must map surfaces as one texture per plane, we can only support + * a subset of possible multi-plane layer formats. This is due to having + * to manually establish what DRM format each synthetic layer should + * have. + */ + uint32_t format[AV_DRM_MAX_PLANES] = { + p_mapper->desc.layers[i].format, + }; + + if (p_mapper->desc.layers[i].nb_planes > 1) { + switch (p_mapper->desc.layers[i].format) { + case DRM_FORMAT_NV12: + case DRM_FORMAT_NV16: + format[0] = DRM_FORMAT_R8; + format[1] = DRM_FORMAT_GR88; + break; + case DRM_FORMAT_YUV420: + format[0] = DRM_FORMAT_R8; + format[1] = DRM_FORMAT_R8; + format[2] = DRM_FORMAT_R8; + break; + case DRM_FORMAT_P010: + case DRM_FORMAT_P210: +#ifdef DRM_FORMAT_P030 /* Format added in a newer libdrm version than minimum */ + case DRM_FORMAT_P030: +#endif + format[0] = DRM_FORMAT_R16; + format[1] = DRM_FORMAT_GR1616; + break; + default: + mp_msg(mapper->log, probing ? MSGL_DEBUG : MSGL_ERR, + "Cannot map unknown multi-plane format: 0x%08X\n", + p_mapper->desc.layers[i].format); + return false; + } + } else { + /* + * As OpenGL only has one guaranteed rgba format (rgba8), drivers + * that support importing dmabuf formats with different channel + * orders do implicit swizzling to get to rgba. However, we look at + * the original imgfmt to decide channel order, and we then swizzle + * based on that. So, we can get into a situation where we swizzle + * twice and end up with a mess. + * + * The simplest way to avoid that is to lie to OpenGL and say that + * the surface we are importing is in the natural channel order, so + * that our swizzling does the right thing. + * + * DRM ABGR corresponds to OpenGL RGBA due to different naming + * conventions. + */ + switch (format[0]) { + case DRM_FORMAT_ARGB8888: + case DRM_FORMAT_RGBA8888: + case DRM_FORMAT_BGRA8888: + format[0] = DRM_FORMAT_ABGR8888; + break; + case DRM_FORMAT_XRGB8888: + format[0] = DRM_FORMAT_XBGR8888; + break; + case DRM_FORMAT_RGBX8888: + case DRM_FORMAT_BGRX8888: + // Logically, these two formats should be handled as above, + // but there appear to be additional problems that make the + // format change here insufficient or incorrect, so we're + // doing nothing for now. + break; + } + } + + for (int j = 0; j < p_mapper->desc.layers[i].nb_planes; j++, n++) { + int attribs[48] = {EGL_NONE}; + int num_attribs = 0; + + ADD_ATTRIB(EGL_LINUX_DRM_FOURCC_EXT, format[j]); + ADD_ATTRIB(EGL_WIDTH, p_mapper->tex[n]->params.w); + ADD_ATTRIB(EGL_HEIGHT, p_mapper->tex[n]->params.h); + ADD_PLANE_ATTRIBS(0); + + p->images[n] = p->CreateImageKHR(eglGetCurrentDisplay(), + EGL_NO_CONTEXT, EGL_LINUX_DMA_BUF_EXT, NULL, attribs); + if (!p->images[n]) { + mp_msg(mapper->log, probing ? MSGL_DEBUG : MSGL_ERR, + "Failed to import surface in EGL: %u\n", eglGetError()); + return false; + } + + gl->BindTexture(GL_TEXTURE_2D, p->gl_textures[n]); + if (p->EGLImageTargetTexStorageEXT) { + p->EGLImageTargetTexStorageEXT(GL_TEXTURE_2D, p->images[n], NULL); + } else { + p->EGLImageTargetTexture2DOES(GL_TEXTURE_2D, p->images[n]); + } + + mapper->tex[n] = p_mapper->tex[n]; + } + } + + gl->BindTexture(GL_TEXTURE_2D, 0); + return true; +} + +static void vaapi_gl_unmap(struct ra_hwdec_mapper *mapper) +{ + struct dmabuf_interop_priv *p_mapper = mapper->priv; + struct vaapi_gl_mapper_priv *p = p_mapper->interop_mapper_priv; + + if (!p) + return; + + if (p->EGLImageTargetTexStorageEXT) { + // textures are immutable, can't reuse + gl_delete_textures(mapper); + } + + for (int n = 0; n < AV_DRM_MAX_PLANES; n++) { + if (p->images[n]) + p->DestroyImageKHR(eglGetCurrentDisplay(), p->images[n]); + p->images[n] = 0; + } +} + +bool dmabuf_interop_gl_init(const struct ra_hwdec *hw, + struct dmabuf_interop *dmabuf_interop) +{ + if (!ra_is_gl(hw->ra_ctx->ra)) { + // This is not an OpenGL RA. + return false; + } + + if (!eglGetCurrentContext()) + return false; + + const char *exts = eglQueryString(eglGetCurrentDisplay(), EGL_EXTENSIONS); + if (!exts) + return false; + + GL *gl = ra_gl_get(hw->ra_ctx->ra); + const char *imageext = gl->es ? "GL_OES_EGL_image" : "GL_EXT_EGL_image_storage"; + if (!gl_check_extension(exts, "EGL_EXT_image_dma_buf_import") || + !gl_check_extension(exts, "EGL_KHR_image_base") || + !gl_check_extension(gl->extensions, imageext) || + !(gl->mpgl_caps & MPGL_CAP_TEX_RG)) { + return false; + } + + dmabuf_interop->use_modifiers = + gl_check_extension(exts, "EGL_EXT_image_dma_buf_import_modifiers"); + + MP_VERBOSE(hw, "Using EGL dmabuf interop via %s\n", imageext); + + dmabuf_interop->interop_init = vaapi_gl_mapper_init; + dmabuf_interop->interop_uninit = vaapi_gl_mapper_uninit; + dmabuf_interop->interop_map = vaapi_gl_map; + dmabuf_interop->interop_unmap = vaapi_gl_unmap; + + return true; +} diff --git a/video/out/hwdec/hwdec_vaapi_vk.c b/video/out/hwdec/dmabuf_interop_pl.c index 1cee9e86b9..1f036e3281 100644 --- a/video/out/hwdec/hwdec_vaapi_vk.c +++ b/video/out/hwdec/dmabuf_interop_pl.c @@ -18,39 +18,36 @@ #include <errno.h> #include <unistd.h> -#include "config.h" -#include "hwdec_vaapi.h" +#include "dmabuf_interop.h" #include "video/out/placebo/ra_pl.h" +#include "video/out/placebo/utils.h" -static bool vaapi_vk_map(struct ra_hwdec_mapper *mapper) +static bool vaapi_pl_map(struct ra_hwdec_mapper *mapper, + struct dmabuf_interop *dmabuf_interop, + bool probing) { - struct priv *p = mapper->priv; - const struct pl_gpu *gpu = ra_pl_get(mapper->ra); + struct dmabuf_interop_priv *p = mapper->priv; + pl_gpu gpu = ra_pl_get(mapper->ra); struct ra_imgfmt_desc desc = {0}; if (!ra_get_imgfmt_desc(mapper->ra, mapper->dst_params.imgfmt, &desc)) return false; + // The calling code validates that the total number of exported planes + // equals the number we expected in p->num_planes. + int layer = 0; + int layer_plane = 0; for (int n = 0; n < p->num_planes; n++) { - if (p->desc.layers[n].num_planes > 1) { - // Should never happen because we request separate layers - MP_ERR(mapper, "Multi-plane VA surfaces are not supported\n"); - return false; - } const struct ra_format *format = desc.planes[n]; - int id = p->desc.layers[n].object_index[0]; + int id = p->desc.layers[layer].planes[layer_plane].object_index; int fd = p->desc.objects[id].fd; uint32_t size = p->desc.objects[id].size; - uint32_t offset = p->desc.layers[n].offset[0]; + uint32_t offset = p->desc.layers[layer].planes[layer_plane].offset; + uint32_t pitch = p->desc.layers[layer].planes[layer_plane].pitch; -#if PL_API_VER >= 88 // AMD drivers do not return the size in the surface description, so we - // need to query it manually. The reason we guard this logic behind - // PL_API_VER >= 88 is that the same drivers also require DRM format - // modifier support in order to not produce corrupted textures, so - // having this #ifdef merely exists to protect users from combining - // too-new mpv with too-old libplacebo. + // need to query it manually. if (size == 0) { size = lseek(fd, 0, SEEK_END); if (size == -1) { @@ -65,7 +62,6 @@ static bool vaapi_vk_map(struct ra_hwdec_mapper *mapper) return false; } } -#endif struct pl_tex_params tex_params = { .w = mp_image_plane_w(&p->layout, n), @@ -73,8 +69,6 @@ static bool vaapi_vk_map(struct ra_hwdec_mapper *mapper) .d = 0, .format = format->priv, .sampleable = true, - .sample_mode = format->linear_filter ? PL_TEX_SAMPLE_LINEAR - : PL_TEX_SAMPLE_NEAREST, .import_handle = PL_HANDLE_DMA_BUF, .shared_mem = (struct pl_shared_mem) { .handle = { @@ -82,16 +76,16 @@ static bool vaapi_vk_map(struct ra_hwdec_mapper *mapper) }, .size = size, .offset = offset, -#if PL_API_VER >= 88 - .drm_format_mod = p->desc.objects[id].drm_format_modifier, -#endif + .drm_format_mod = p->desc.objects[id].format_modifier, + .stride_w = pitch, }, }; - const struct pl_tex *pltex = pl_tex_create(gpu, &tex_params); - if (!pltex) { + mppl_log_set_probing(gpu->log, probing); + pl_tex pltex = pl_tex_create(gpu, &tex_params); + mppl_log_set_probing(gpu->log, false); + if (!pltex) return false; - } struct ra_tex *ratex = talloc_ptrtype(NULL, ratex); int ret = mppl_wrap_tex(mapper->ra, pltex, ratex); @@ -104,36 +98,41 @@ static bool vaapi_vk_map(struct ra_hwdec_mapper *mapper) MP_TRACE(mapper, "Object %d with fd %d imported as %p\n", id, fd, ratex); + + layer_plane++; + if (layer_plane == p->desc.layers[layer].nb_planes) { + layer_plane = 0; + layer++; + } } return true; } -static void vaapi_vk_unmap(struct ra_hwdec_mapper *mapper) +static void vaapi_pl_unmap(struct ra_hwdec_mapper *mapper) { - for (int n = 0; n < 4; n++) + for (int n = 0; n < MP_ARRAY_SIZE(mapper->tex); n++) ra_tex_free(mapper->ra, &mapper->tex[n]); } -bool vaapi_vk_init(const struct ra_hwdec *hw) +bool dmabuf_interop_pl_init(const struct ra_hwdec *hw, + struct dmabuf_interop *dmabuf_interop) { - struct priv_owner *p = hw->priv; - - const struct pl_gpu *gpu = ra_pl_get(hw->ra); + pl_gpu gpu = ra_pl_get(hw->ra_ctx->ra); if (!gpu) { - // This is not a Vulkan RA; + // This is not a libplacebo RA; return false; } if (!(gpu->import_caps.tex & PL_HANDLE_DMA_BUF)) { - MP_VERBOSE(hw, "VAAPI Vulkan interop requires support for " - "dma_buf import in Vulkan.\n"); + MP_VERBOSE(hw, "libplacebo dmabuf interop requires support for " + "PL_HANDLE_DMA_BUF import.\n"); return false; } - MP_VERBOSE(hw, "using VAAPI Vulkan interop\n"); + MP_VERBOSE(hw, "using libplacebo dmabuf interop\n"); - p->interop_map = vaapi_vk_map; - p->interop_unmap = vaapi_vk_unmap; + dmabuf_interop->interop_map = vaapi_pl_map; + dmabuf_interop->interop_unmap = vaapi_pl_unmap; return true; } diff --git a/video/out/hwdec/dmabuf_interop_wl.c b/video/out/hwdec/dmabuf_interop_wl.c new file mode 100644 index 0000000000..606a0aa601 --- /dev/null +++ b/video/out/hwdec/dmabuf_interop_wl.c @@ -0,0 +1,83 @@ +/* + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. + */ +#include "video/out/wldmabuf/ra_wldmabuf.h" +#include "dmabuf_interop.h" + +static bool mapper_init(struct ra_hwdec_mapper *mapper, + const struct ra_imgfmt_desc *desc) +{ + return true; +} + +static void mapper_uninit(const struct ra_hwdec_mapper *mapper) +{ +} + +static bool map(struct ra_hwdec_mapper *mapper, + struct dmabuf_interop *dmabuf_interop, + bool probing) +{ + // 1. only validate format when composed layers is enabled (i.e. vaapi) + // 2. for drmprime, just return true for now, as this use case + // has not been tested. + if (!dmabuf_interop->composed_layers) + return true; + + int layer_no = 0; + struct dmabuf_interop_priv *mapper_p = mapper->priv; + uint32_t drm_format = mapper_p->desc.layers[layer_no].format; + + if (mapper_p->desc.nb_layers != 1) { + MP_VERBOSE(mapper, "Mapped surface has separate layers - expected composed layers.\n"); + return false; + } else if (!ra_compatible_format(mapper->ra, drm_format, + mapper_p->desc.objects[0].format_modifier)) { + MP_VERBOSE(mapper, "Mapped surface with format %s; drm format '%s(%016lx)' " + "is not supported by compositor.\n", + mp_imgfmt_to_name(mapper->src->params.hw_subfmt), + mp_tag_str(drm_format), + mapper_p->desc.objects[0].format_modifier); + return false; + } + + MP_VERBOSE(mapper, "Supported Wayland display format %s: '%s(%016lx)'\n", + mp_imgfmt_to_name(mapper->src->params.hw_subfmt), + mp_tag_str(drm_format), mapper_p->desc.objects[0].format_modifier); + + return true; +} + +static void unmap(struct ra_hwdec_mapper *mapper) +{ +} + +bool dmabuf_interop_wl_init(const struct ra_hwdec *hw, + struct dmabuf_interop *dmabuf_interop) +{ + if (!ra_is_wldmabuf(hw->ra_ctx->ra)) + return false; + + if (strstr(hw->driver->name, "vaapi") != NULL) + dmabuf_interop->composed_layers = true; + + dmabuf_interop->interop_init = mapper_init; + dmabuf_interop->interop_uninit = mapper_uninit; + dmabuf_interop->interop_map = map; + dmabuf_interop->interop_unmap = unmap; + + return true; +} diff --git a/video/out/hwdec/hwdec_aimagereader.c b/video/out/hwdec/hwdec_aimagereader.c new file mode 100644 index 0000000000..1aa92eea4a --- /dev/null +++ b/video/out/hwdec/hwdec_aimagereader.c @@ -0,0 +1,404 @@ +/* + * Copyright (c) 2021 sfan5 <sfan5@live.de> + * + * This file is part of mpv. + * + * mpv is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * mpv is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with mpv. If not, see <http://www.gnu.org/licenses/>. + */ + +#include <assert.h> +#include <dlfcn.h> +#include <EGL/egl.h> +#include <media/NdkImageReader.h> +#include <android/native_window_jni.h> +#include <libavcodec/mediacodec.h> +#include <libavutil/hwcontext.h> +#include <libavutil/hwcontext_mediacodec.h> + +#include "misc/jni.h" +#include "osdep/threads.h" +#include "osdep/timer.h" +#include "video/out/gpu/hwdec.h" +#include "video/out/opengl/ra_gl.h" + +typedef void *GLeglImageOES; +typedef void *EGLImageKHR; +#define EGL_NATIVE_BUFFER_ANDROID 0x3140 + +struct priv_owner { + struct mp_hwdec_ctx hwctx; + AImageReader *reader; + jobject surface; + void *lib_handle; + + media_status_t (*AImageReader_newWithUsage)( + int32_t, int32_t, int32_t, uint64_t, int32_t, AImageReader **); + media_status_t (*AImageReader_getWindow)( + AImageReader *, ANativeWindow **); + media_status_t (*AImageReader_setImageListener)( + AImageReader *, AImageReader_ImageListener *); + media_status_t (*AImageReader_acquireLatestImage)(AImageReader *, AImage **); + void (*AImageReader_delete)(AImageReader *); + media_status_t (*AImage_getHardwareBuffer)(const AImage *, AHardwareBuffer **); + void (*AImage_delete)(AImage *); + void (*AHardwareBuffer_describe)(const AHardwareBuffer *, AHardwareBuffer_Desc *); + jobject (*ANativeWindow_toSurface)(JNIEnv *, ANativeWindow *); +}; + +struct priv { + struct mp_log *log; + + GLuint gl_texture; + AImage *image; + EGLImageKHR egl_image; + + mp_mutex lock; + mp_cond cond; + bool image_available; + + EGLImageKHR (EGLAPIENTRY *CreateImageKHR)( + EGLDisplay, EGLContext, EGLenum, EGLClientBuffer, const EGLint *); + EGLBoolean (EGLAPIENTRY *DestroyImageKHR)(EGLDisplay, EGLImageKHR); + EGLClientBuffer (EGLAPIENTRY *GetNativeClientBufferANDROID)( + const struct AHardwareBuffer *); + void (EGLAPIENTRY *EGLImageTargetTexture2DOES)(GLenum, GLeglImageOES); +}; + +static const struct { const char *symbol; int offset; } lib_functions[] = { + { "AImageReader_newWithUsage", offsetof(struct priv_owner, AImageReader_newWithUsage) }, + { "AImageReader_getWindow", offsetof(struct priv_owner, AImageReader_getWindow) }, + { "AImageReader_setImageListener", offsetof(struct priv_owner, AImageReader_setImageListener) }, + { "AImageReader_acquireLatestImage", offsetof(struct priv_owner, AImageReader_acquireLatestImage) }, + { "AImageReader_delete", offsetof(struct priv_owner, AImageReader_delete) }, + { "AImage_getHardwareBuffer", offsetof(struct priv_owner, AImage_getHardwareBuffer) }, + { "AImage_delete", offsetof(struct priv_owner, AImage_delete) }, + { "AHardwareBuffer_describe", offsetof(struct priv_owner, AHardwareBuffer_describe) }, + { "ANativeWindow_toSurface", offsetof(struct priv_owner, ANativeWindow_toSurface) }, + { NULL, 0 }, +}; + + +static AVBufferRef *create_mediacodec_device_ref(jobject surface) +{ + AVBufferRef *device_ref = av_hwdevice_ctx_alloc(AV_HWDEVICE_TYPE_MEDIACODEC); + if (!device_ref) + return NULL; + + AVHWDeviceContext *ctx = (void *)device_ref->data; + AVMediaCodecDeviceContext *hwctx = ctx->hwctx; + hwctx->surface = surface; + + if (av_hwdevice_ctx_init(device_ref) < 0) + av_buffer_unref(&device_ref); + + return device_ref; +} + +static bool load_lib_functions(struct priv_owner *p, struct mp_log *log) +{ + p->lib_handle = dlopen("libmediandk.so", RTLD_NOW | RTLD_GLOBAL); + if (!p->lib_handle) + return false; + for (int i = 0; lib_functions[i].symbol; i++) { + const char *sym = lib_functions[i].symbol; + void *fun = dlsym(p->lib_handle, sym); + if (!fun) + fun = dlsym(RTLD_DEFAULT, sym); + if (!fun) { + mp_warn(log, "Could not resolve symbol %s\n", sym); + return false; + } + + *(void **) ((uint8_t*)p + lib_functions[i].offset) = fun; + } + return true; +} + +static int init(struct ra_hwdec *hw) +{ + struct priv_owner *p = hw->priv; + + if (!ra_is_gl(hw->ra_ctx->ra)) + return -1; + if (!eglGetCurrentContext()) + return -1; + + const char *exts = eglQueryString(eglGetCurrentDisplay(), EGL_EXTENSIONS); + if (!gl_check_extension(exts, "EGL_ANDROID_image_native_buffer")) + return -1; + + JNIEnv *env = MP_JNI_GET_ENV(hw); + if (!env) + return -1; + + if (!load_lib_functions(p, hw->log)) + return -1; + + static const char *es2_exts[] = {"GL_OES_EGL_image_external", 0}; + static const char *es3_exts[] = {"GL_OES_EGL_image_external_essl3", 0}; + GL *gl = ra_gl_get(hw->ra_ctx->ra); + if (gl_check_extension(gl->extensions, es3_exts[0])) + hw->glsl_extensions = es3_exts; + else + hw->glsl_extensions = es2_exts; + + // dummy dimensions, AImageReader only transports hardware buffers + med |