2013-11-03 23:02:21 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
|
|
|
* Parts based on the MPlayer VA-API patch (see vo_vaapi.c).
|
|
|
|
*
|
|
|
|
* mpv is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 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 General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with mpv. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
2015-04-05 20:44:22 +00:00
|
|
|
#include <string.h>
|
2013-11-03 23:02:21 +00:00
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <GL/glx.h>
|
2015-09-27 18:09:10 +00:00
|
|
|
#include <va/va_x11.h>
|
2013-11-03 23:02:21 +00:00
|
|
|
|
2015-08-28 23:10:30 +00:00
|
|
|
#include "video/out/x11_common.h"
|
2015-08-29 02:12:56 +00:00
|
|
|
#include "hwdec.h"
|
2013-11-03 23:02:21 +00:00
|
|
|
#include "video/vaapi.h"
|
|
|
|
|
|
|
|
struct priv {
|
2013-12-21 17:11:01 +00:00
|
|
|
struct mp_log *log;
|
2013-11-03 23:02:21 +00:00
|
|
|
struct mp_vaapi_ctx *ctx;
|
|
|
|
VADisplay *display;
|
2015-04-05 20:44:22 +00:00
|
|
|
Display *xdisplay;
|
2013-11-03 23:02:21 +00:00
|
|
|
GLuint gl_texture;
|
2015-04-05 20:44:22 +00:00
|
|
|
GLXFBConfig fbc;
|
|
|
|
Pixmap pixmap;
|
|
|
|
GLXPixmap glxpixmap;
|
|
|
|
void (*glXBindTexImage)(Display *dpy, GLXDrawable draw, int buffer, int *a);
|
|
|
|
void (*glXReleaseTexImage)(Display *dpy, GLXDrawable draw, int buffer);
|
2013-11-03 23:02:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void destroy_texture(struct gl_hwdec *hw)
|
|
|
|
{
|
|
|
|
struct priv *p = hw->priv;
|
2014-12-06 00:40:23 +00:00
|
|
|
GL *gl = hw->gl;
|
2013-11-03 23:02:21 +00:00
|
|
|
|
2015-04-05 20:44:22 +00:00
|
|
|
if (p->glxpixmap) {
|
|
|
|
p->glXReleaseTexImage(p->xdisplay, p->glxpixmap, GLX_FRONT_EXT);
|
|
|
|
glXDestroyPixmap(p->xdisplay, p->glxpixmap);
|
2013-11-03 23:02:21 +00:00
|
|
|
}
|
2015-04-05 20:44:22 +00:00
|
|
|
p->glxpixmap = 0;
|
|
|
|
|
|
|
|
if (p->pixmap)
|
|
|
|
XFreePixmap(p->xdisplay, p->pixmap);
|
|
|
|
p->pixmap = 0;
|
2013-11-03 23:02:21 +00:00
|
|
|
|
2014-12-06 00:40:23 +00:00
|
|
|
gl->DeleteTextures(1, &p->gl_texture);
|
2013-11-03 23:02:21 +00:00
|
|
|
p->gl_texture = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy(struct gl_hwdec *hw)
|
|
|
|
{
|
|
|
|
struct priv *p = hw->priv;
|
|
|
|
destroy_texture(hw);
|
2016-05-09 17:42:03 +00:00
|
|
|
if (p->ctx)
|
|
|
|
hwdec_devices_remove(hw->devs, &p->ctx->hwctx);
|
2013-11-03 23:02:21 +00:00
|
|
|
va_destroy(p->ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int create(struct gl_hwdec *hw)
|
|
|
|
{
|
2014-12-03 20:07:18 +00:00
|
|
|
Display *x11disp = glXGetCurrentDisplay();
|
|
|
|
if (!x11disp)
|
2013-11-03 23:02:21 +00:00
|
|
|
return -1;
|
2015-11-09 10:58:38 +00:00
|
|
|
if (hw->probing) {
|
|
|
|
MP_VERBOSE(hw, "Not using this by default.\n");
|
|
|
|
return -1;
|
|
|
|
}
|
2015-04-05 20:44:22 +00:00
|
|
|
int x11scr = DefaultScreen(x11disp);
|
2013-11-03 23:02:21 +00:00
|
|
|
struct priv *p = talloc_zero(hw, struct priv);
|
|
|
|
hw->priv = p;
|
2013-12-21 17:11:01 +00:00
|
|
|
p->log = hw->log;
|
2015-04-05 20:44:22 +00:00
|
|
|
p->xdisplay = x11disp;
|
|
|
|
const char *glxext = glXQueryExtensionsString(x11disp, x11scr);
|
|
|
|
if (!glxext || !strstr(glxext, "GLX_EXT_texture_from_pixmap"))
|
|
|
|
return -1;
|
|
|
|
p->glXBindTexImage =
|
|
|
|
(void*)glXGetProcAddressARB((void*)"glXBindTexImageEXT");
|
|
|
|
p->glXReleaseTexImage =
|
|
|
|
(void*)glXGetProcAddressARB((void*)"glXReleaseTexImageEXT");
|
|
|
|
if (!p->glXBindTexImage || !p->glXReleaseTexImage)
|
|
|
|
return -1;
|
|
|
|
p->display = vaGetDisplay(x11disp);
|
2013-11-03 23:02:21 +00:00
|
|
|
if (!p->display)
|
|
|
|
return -1;
|
2015-06-20 20:26:57 +00:00
|
|
|
p->ctx = va_initialize(p->display, p->log, true);
|
2013-11-03 23:02:21 +00:00
|
|
|
if (!p->ctx) {
|
|
|
|
vaTerminate(p->display);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-04-05 20:44:22 +00:00
|
|
|
|
|
|
|
int attribs[] = {
|
|
|
|
GLX_BIND_TO_TEXTURE_RGBA_EXT, True,
|
|
|
|
GLX_DRAWABLE_TYPE, GLX_PIXMAP_BIT,
|
|
|
|
GLX_BIND_TO_TEXTURE_TARGETS_EXT, GLX_TEXTURE_2D_BIT_EXT,
|
|
|
|
GLX_Y_INVERTED_EXT, True,
|
|
|
|
GLX_DOUBLEBUFFER, False,
|
|
|
|
GLX_RED_SIZE, 8,
|
|
|
|
GLX_GREEN_SIZE, 8,
|
|
|
|
GLX_BLUE_SIZE, 8,
|
|
|
|
GLX_ALPHA_SIZE, 0,
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
|
|
|
int fbcount;
|
|
|
|
GLXFBConfig *fbc = glXChooseFBConfig(x11disp, x11scr, attribs, &fbcount);
|
|
|
|
if (fbcount)
|
|
|
|
p->fbc = fbc[0];
|
|
|
|
if (fbc)
|
|
|
|
XFree(fbc);
|
|
|
|
if (!fbcount) {
|
|
|
|
MP_VERBOSE(p, "No texture-from-pixmap support.\n");
|
|
|
|
destroy(hw);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-05-09 17:42:03 +00:00
|
|
|
p->ctx->hwctx.driver_name = hw->driver->name;
|
|
|
|
hwdec_devices_add(hw->devs, &p->ctx->hwctx);
|
2013-11-03 23:02:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-29 18:53:49 +00:00
|
|
|
static int reinit(struct gl_hwdec *hw, struct mp_image_params *params)
|
2013-11-03 23:02:21 +00:00
|
|
|
{
|
|
|
|
struct priv *p = hw->priv;
|
2014-12-03 20:07:18 +00:00
|
|
|
GL *gl = hw->gl;
|
2013-11-03 23:02:21 +00:00
|
|
|
|
|
|
|
destroy_texture(hw);
|
|
|
|
|
|
|
|
gl->GenTextures(1, &p->gl_texture);
|
|
|
|
gl->BindTexture(GL_TEXTURE_2D, p->gl_texture);
|
|
|
|
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);
|
|
|
|
|
2015-04-05 20:44:22 +00:00
|
|
|
p->pixmap = XCreatePixmap(p->xdisplay,
|
|
|
|
RootWindow(p->xdisplay, DefaultScreen(p->xdisplay)),
|
|
|
|
params->w, params->h, 24);
|
|
|
|
if (!p->pixmap) {
|
|
|
|
MP_FATAL(hw, "could not create pixmap\n");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int attribs[] = {
|
|
|
|
GLX_TEXTURE_TARGET_EXT, GLX_TEXTURE_2D_EXT,
|
|
|
|
GLX_TEXTURE_FORMAT_EXT, GLX_TEXTURE_FORMAT_RGB_EXT,
|
|
|
|
GLX_MIPMAP_TEXTURE_EXT, False,
|
|
|
|
None,
|
|
|
|
};
|
|
|
|
p->glxpixmap = glXCreatePixmap(p->xdisplay, p->fbc, p->pixmap, attribs);
|
|
|
|
|
|
|
|
gl->BindTexture(GL_TEXTURE_2D, p->gl_texture);
|
|
|
|
p->glXBindTexImage(p->xdisplay, p->glxpixmap, GLX_FRONT_EXT, NULL);
|
|
|
|
gl->BindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
vo_opengl: refactor how hwdec interop exports textures
Rename gl_hwdec_driver.map_image to map_frame, and let it fill out a
struct gl_hwdec_frame describing the exact texture layout. This gives
more flexibility to what the hwdec interop can export. In particular, it
can export strange component orders/permutations and textures with
padded size. (The latter originating from cropped video.)
The way gl_hwdec_frame works is in the spirit of the rest of the
vo_opengl video processing code, which tends to put as much information
in immediate state (as part of the dataflow), instead of declaring it
globally. To some degree this duplicates the texplane and img_tex
structs, but until we somehow unify those, it's better to give the hwdec
state its own struct. The fact that changing the hwdec struct would
require changes and testing on at least 4 platform/GPU combinations
makes duplicating it almost a requirement to avoid pain later.
Make gl_hwdec_driver.reinit set the new image format and remove the
gl_hwdec.converted_imgfmt field.
Likewise, gl_hwdec.gl_texture_target is replaced with
gl_hwdec_plane.gl_target.
Split out a init_image_desc function from init_format. The latter is not
called in the hwdec case at all anymore. Setting up most of struct
texplane is also completely separate in the hwdec and normal cases.
video.c does not check whether the hwdec "mapped" image format is
supported. This should not really happen anyway, and if it does, the
hwdec interop backend must fail at creation time, so this is not an
issue.
2016-05-10 16:29:10 +00:00
|
|
|
params->imgfmt = IMGFMT_RGB0;
|
2016-07-15 10:01:30 +00:00
|
|
|
params->hw_subfmt = 0;
|
vo_opengl: refactor how hwdec interop exports textures
Rename gl_hwdec_driver.map_image to map_frame, and let it fill out a
struct gl_hwdec_frame describing the exact texture layout. This gives
more flexibility to what the hwdec interop can export. In particular, it
can export strange component orders/permutations and textures with
padded size. (The latter originating from cropped video.)
The way gl_hwdec_frame works is in the spirit of the rest of the
vo_opengl video processing code, which tends to put as much information
in immediate state (as part of the dataflow), instead of declaring it
globally. To some degree this duplicates the texplane and img_tex
structs, but until we somehow unify those, it's better to give the hwdec
state its own struct. The fact that changing the hwdec struct would
require changes and testing on at least 4 platform/GPU combinations
makes duplicating it almost a requirement to avoid pain later.
Make gl_hwdec_driver.reinit set the new image format and remove the
gl_hwdec.converted_imgfmt field.
Likewise, gl_hwdec.gl_texture_target is replaced with
gl_hwdec_plane.gl_target.
Split out a init_image_desc function from init_format. The latter is not
called in the hwdec case at all anymore. Setting up most of struct
texplane is also completely separate in the hwdec and normal cases.
video.c does not check whether the hwdec "mapped" image format is
supported. This should not really happen anyway, and if it does, the
hwdec interop backend must fail at creation time, so this is not an
issue.
2016-05-10 16:29:10 +00:00
|
|
|
|
2015-04-05 20:44:22 +00:00
|
|
|
return 0;
|
2013-11-03 23:02:21 +00:00
|
|
|
}
|
|
|
|
|
vo_opengl: refactor how hwdec interop exports textures
Rename gl_hwdec_driver.map_image to map_frame, and let it fill out a
struct gl_hwdec_frame describing the exact texture layout. This gives
more flexibility to what the hwdec interop can export. In particular, it
can export strange component orders/permutations and textures with
padded size. (The latter originating from cropped video.)
The way gl_hwdec_frame works is in the spirit of the rest of the
vo_opengl video processing code, which tends to put as much information
in immediate state (as part of the dataflow), instead of declaring it
globally. To some degree this duplicates the texplane and img_tex
structs, but until we somehow unify those, it's better to give the hwdec
state its own struct. The fact that changing the hwdec struct would
require changes and testing on at least 4 platform/GPU combinations
makes duplicating it almost a requirement to avoid pain later.
Make gl_hwdec_driver.reinit set the new image format and remove the
gl_hwdec.converted_imgfmt field.
Likewise, gl_hwdec.gl_texture_target is replaced with
gl_hwdec_plane.gl_target.
Split out a init_image_desc function from init_format. The latter is not
called in the hwdec case at all anymore. Setting up most of struct
texplane is also completely separate in the hwdec and normal cases.
video.c does not check whether the hwdec "mapped" image format is
supported. This should not really happen anyway, and if it does, the
hwdec interop backend must fail at creation time, so this is not an
issue.
2016-05-10 16:29:10 +00:00
|
|
|
static int map_frame(struct gl_hwdec *hw, struct mp_image *hw_image,
|
|
|
|
struct gl_hwdec_frame *out_frame)
|
2013-11-03 23:02:21 +00:00
|
|
|
{
|
|
|
|
struct priv *p = hw->priv;
|
|
|
|
VAStatus status;
|
|
|
|
|
2015-04-05 20:44:22 +00:00
|
|
|
if (!p->pixmap)
|
2013-11-03 23:02:21 +00:00
|
|
|
return -1;
|
|
|
|
|
2015-04-05 20:44:22 +00:00
|
|
|
status = vaPutSurface(p->display, va_surface_id(hw_image), p->pixmap,
|
|
|
|
0, 0, hw_image->w, hw_image->h,
|
|
|
|
0, 0, hw_image->w, hw_image->h,
|
|
|
|
NULL, 0,
|
2016-06-29 07:16:13 +00:00
|
|
|
va_get_colorspace_flag(hw_image->params.color.space));
|
2015-04-05 20:44:22 +00:00
|
|
|
CHECK_VA_STATUS(p, "vaPutSurface()");
|
2013-11-03 23:02:21 +00:00
|
|
|
|
vo_opengl: refactor how hwdec interop exports textures
Rename gl_hwdec_driver.map_image to map_frame, and let it fill out a
struct gl_hwdec_frame describing the exact texture layout. This gives
more flexibility to what the hwdec interop can export. In particular, it
can export strange component orders/permutations and textures with
padded size. (The latter originating from cropped video.)
The way gl_hwdec_frame works is in the spirit of the rest of the
vo_opengl video processing code, which tends to put as much information
in immediate state (as part of the dataflow), instead of declaring it
globally. To some degree this duplicates the texplane and img_tex
structs, but until we somehow unify those, it's better to give the hwdec
state its own struct. The fact that changing the hwdec struct would
require changes and testing on at least 4 platform/GPU combinations
makes duplicating it almost a requirement to avoid pain later.
Make gl_hwdec_driver.reinit set the new image format and remove the
gl_hwdec.converted_imgfmt field.
Likewise, gl_hwdec.gl_texture_target is replaced with
gl_hwdec_plane.gl_target.
Split out a init_image_desc function from init_format. The latter is not
called in the hwdec case at all anymore. Setting up most of struct
texplane is also completely separate in the hwdec and normal cases.
video.c does not check whether the hwdec "mapped" image format is
supported. This should not really happen anyway, and if it does, the
hwdec interop backend must fail at creation time, so this is not an
issue.
2016-05-10 16:29:10 +00:00
|
|
|
*out_frame = (struct gl_hwdec_frame){
|
|
|
|
.planes = {
|
|
|
|
{
|
|
|
|
.gl_texture = p->gl_texture,
|
|
|
|
.gl_target = GL_TEXTURE_2D,
|
|
|
|
.tex_w = hw_image->w,
|
|
|
|
.tex_h = hw_image->h,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
};
|
2013-11-03 23:02:21 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct gl_hwdec_driver gl_hwdec_vaglx = {
|
2016-02-01 19:02:52 +00:00
|
|
|
.name = "vaapi-glx",
|
|
|
|
.api = HWDEC_VAAPI,
|
2013-11-29 13:19:51 +00:00
|
|
|
.imgfmt = IMGFMT_VAAPI,
|
2013-11-03 23:02:21 +00:00
|
|
|
.create = create,
|
|
|
|
.reinit = reinit,
|
vo_opengl: refactor how hwdec interop exports textures
Rename gl_hwdec_driver.map_image to map_frame, and let it fill out a
struct gl_hwdec_frame describing the exact texture layout. This gives
more flexibility to what the hwdec interop can export. In particular, it
can export strange component orders/permutations and textures with
padded size. (The latter originating from cropped video.)
The way gl_hwdec_frame works is in the spirit of the rest of the
vo_opengl video processing code, which tends to put as much information
in immediate state (as part of the dataflow), instead of declaring it
globally. To some degree this duplicates the texplane and img_tex
structs, but until we somehow unify those, it's better to give the hwdec
state its own struct. The fact that changing the hwdec struct would
require changes and testing on at least 4 platform/GPU combinations
makes duplicating it almost a requirement to avoid pain later.
Make gl_hwdec_driver.reinit set the new image format and remove the
gl_hwdec.converted_imgfmt field.
Likewise, gl_hwdec.gl_texture_target is replaced with
gl_hwdec_plane.gl_target.
Split out a init_image_desc function from init_format. The latter is not
called in the hwdec case at all anymore. Setting up most of struct
texplane is also completely separate in the hwdec and normal cases.
video.c does not check whether the hwdec "mapped" image format is
supported. This should not really happen anyway, and if it does, the
hwdec interop backend must fail at creation time, so this is not an
issue.
2016-05-10 16:29:10 +00:00
|
|
|
.map_frame = map_frame,
|
2013-11-03 23:02:21 +00:00
|
|
|
.destroy = destroy,
|
|
|
|
};
|