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>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#include <GL/glx.h>
|
|
|
|
#include <va/va_glx.h>
|
|
|
|
|
|
|
|
#include "x11_common.h"
|
2014-12-03 20:54:04 +00:00
|
|
|
#include "gl_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;
|
|
|
|
GLuint gl_texture;
|
|
|
|
void *vaglx_surface;
|
|
|
|
};
|
|
|
|
|
|
|
|
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
|
|
|
VAStatus status;
|
|
|
|
|
|
|
|
if (p->vaglx_surface) {
|
2014-08-21 20:04:25 +00:00
|
|
|
va_lock(p->ctx);
|
2013-11-03 23:02:21 +00:00
|
|
|
status = vaDestroySurfaceGLX(p->display, p->vaglx_surface);
|
2014-08-21 20:04:25 +00:00
|
|
|
va_unlock(p->ctx);
|
2013-12-21 17:11:01 +00:00
|
|
|
CHECK_VA_STATUS(p, "vaDestroySurfaceGLX()");
|
2013-11-03 23:02:21 +00:00
|
|
|
p->vaglx_surface = NULL;
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
va_destroy(p->ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int create(struct gl_hwdec *hw)
|
|
|
|
{
|
2015-01-22 14:32:23 +00:00
|
|
|
if (hw->hwctx)
|
2013-11-03 23:02:21 +00:00
|
|
|
return -1;
|
2014-12-03 20:07:18 +00:00
|
|
|
Display *x11disp = glXGetCurrentDisplay();
|
|
|
|
if (!x11disp)
|
2013-11-03 23:02:21 +00:00
|
|
|
return -1;
|
|
|
|
struct priv *p = talloc_zero(hw, struct priv);
|
|
|
|
hw->priv = p;
|
2013-12-21 17:11:01 +00:00
|
|
|
p->log = hw->log;
|
2014-12-03 20:07:18 +00:00
|
|
|
p->display = vaGetDisplayGLX(x11disp);
|
2013-11-03 23:02:21 +00:00
|
|
|
if (!p->display)
|
|
|
|
return -1;
|
2013-12-21 17:11:01 +00:00
|
|
|
p->ctx = va_initialize(p->display, p->log);
|
2013-11-03 23:02:21 +00:00
|
|
|
if (!p->ctx) {
|
|
|
|
vaTerminate(p->display);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-12-09 16:47:02 +00:00
|
|
|
if (hw->reject_emulated && va_guess_if_emulated(p->ctx)) {
|
|
|
|
destroy(hw);
|
|
|
|
return -1;
|
|
|
|
}
|
2015-01-22 14:32:23 +00:00
|
|
|
hw->hwctx = &p->ctx->hwctx;
|
2013-11-05 21:01:15 +00:00
|
|
|
hw->converted_imgfmt = IMGFMT_RGB0;
|
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
|
|
|
VAStatus status;
|
|
|
|
|
|
|
|
destroy_texture(hw);
|
|
|
|
|
2015-01-29 18:53:49 +00:00
|
|
|
params->imgfmt = hw->driver->imgfmt;
|
|
|
|
|
2013-11-03 23:02:21 +00:00
|
|
|
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);
|
2013-11-05 18:08:44 +00:00
|
|
|
gl->TexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, params->w, params->h, 0,
|
2013-11-03 23:02:21 +00:00
|
|
|
GL_RGBA, GL_UNSIGNED_BYTE, NULL);
|
|
|
|
gl->BindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
2014-08-21 20:04:25 +00:00
|
|
|
va_lock(p->ctx);
|
2013-11-03 23:02:21 +00:00
|
|
|
status = vaCreateSurfaceGLX(p->display, GL_TEXTURE_2D,
|
|
|
|
p->gl_texture, &p->vaglx_surface);
|
2014-08-21 20:04:25 +00:00
|
|
|
va_unlock(p->ctx);
|
2013-12-21 17:11:01 +00:00
|
|
|
return CHECK_VA_STATUS(p, "vaCreateSurfaceGLX()") ? 0 : -1;
|
2013-11-03 23:02:21 +00:00
|
|
|
}
|
|
|
|
|
2013-11-05 18:08:44 +00:00
|
|
|
static int map_image(struct gl_hwdec *hw, struct mp_image *hw_image,
|
|
|
|
GLuint *out_textures)
|
2013-11-03 23:02:21 +00:00
|
|
|
{
|
|
|
|
struct priv *p = hw->priv;
|
|
|
|
VAStatus status;
|
|
|
|
|
|
|
|
if (!p->vaglx_surface)
|
|
|
|
return -1;
|
|
|
|
|
2014-08-21 20:04:25 +00:00
|
|
|
va_lock(p->ctx);
|
2013-11-03 23:02:21 +00:00
|
|
|
status = vaCopySurfaceGLX(p->display, p->vaglx_surface,
|
2014-03-17 17:22:35 +00:00
|
|
|
va_surface_id(hw_image),
|
2014-04-20 19:27:45 +00:00
|
|
|
va_get_colorspace_flag(hw_image->params.colorspace));
|
2014-08-21 20:04:25 +00:00
|
|
|
va_unlock(p->ctx);
|
2013-12-21 17:11:01 +00:00
|
|
|
if (!CHECK_VA_STATUS(p, "vaCopySurfaceGLX()"))
|
2013-11-03 23:02:21 +00:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
out_textures[0] = p->gl_texture;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-05 18:08:44 +00:00
|
|
|
static void unmap_image(struct gl_hwdec *hw)
|
2013-11-03 23:02:21 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct gl_hwdec_driver gl_hwdec_vaglx = {
|
|
|
|
.api_name = "vaapi",
|
2013-11-29 13:19:51 +00:00
|
|
|
.imgfmt = IMGFMT_VAAPI,
|
2013-11-03 23:02:21 +00:00
|
|
|
.create = create,
|
|
|
|
.reinit = reinit,
|
2013-11-05 18:08:44 +00:00
|
|
|
.map_image = map_image,
|
|
|
|
.unmap_image = unmap_image,
|
2013-11-03 23:02:21 +00:00
|
|
|
.destroy = destroy,
|
|
|
|
};
|