2013-11-05 21:06:48 +00:00
|
|
|
/*
|
|
|
|
* This file is part of mpv.
|
|
|
|
*
|
2016-01-07 10:22:54 +00:00
|
|
|
* 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.
|
2013-11-05 21:06:48 +00:00
|
|
|
*
|
|
|
|
* 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
|
2016-01-07 10:22:54 +00:00
|
|
|
* GNU Lesser General Public License for more details.
|
2013-11-05 21:06:48 +00:00
|
|
|
*
|
2016-01-07 10:22:54 +00:00
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with mpv. If not, see <http://www.gnu.org/licenses/>.
|
2013-11-05 21:06:48 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
2014-12-03 20:13:59 +00:00
|
|
|
#include <GL/glx.h>
|
|
|
|
|
2015-08-29 02:12:56 +00:00
|
|
|
#include "hwdec.h"
|
|
|
|
#include "utils.h"
|
2013-11-05 21:06:48 +00:00
|
|
|
#include "video/vdpau.h"
|
2014-04-29 13:11:26 +00:00
|
|
|
#include "video/vdpau_mixer.h"
|
2013-11-05 21:06:48 +00:00
|
|
|
|
2013-11-09 22:31:12 +00:00
|
|
|
// This is a GL_NV_vdpau_interop specification bug, and headers (unfortunately)
|
|
|
|
// follow it. I'm not sure about the original nvidia headers.
|
|
|
|
#define BRAINDEATH(x) ((void *)(uintptr_t)(x))
|
|
|
|
|
2015-01-29 18:53:49 +00:00
|
|
|
static int reinit(struct gl_hwdec *hw, struct mp_image_params *params);
|
2013-11-05 21:06:48 +00:00
|
|
|
|
|
|
|
struct priv {
|
2013-12-21 17:05:23 +00:00
|
|
|
struct mp_log *log;
|
2013-11-05 21:06:48 +00:00
|
|
|
struct mp_vdpau_ctx *ctx;
|
|
|
|
uint64_t preemption_counter;
|
|
|
|
struct mp_image_params image_params;
|
|
|
|
GLuint gl_texture;
|
2016-02-08 12:39:33 +00:00
|
|
|
bool vdpgl_initialized;
|
2013-11-05 21:06:48 +00:00
|
|
|
GLvdpauSurfaceNV vdpgl_surface;
|
|
|
|
VdpOutputSurface vdp_surface;
|
2014-04-29 13:11:26 +00:00
|
|
|
struct mp_vdpau_mixer *mixer;
|
2015-03-14 21:36:59 +00:00
|
|
|
bool mapped;
|
2013-11-05 21:06:48 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void mark_vdpau_objects_uninitialized(struct gl_hwdec *hw)
|
|
|
|
{
|
|
|
|
struct priv *p = hw->priv;
|
|
|
|
|
|
|
|
p->vdp_surface = VDP_INVALID_HANDLE;
|
2015-03-14 21:36:59 +00:00
|
|
|
p->mapped = false;
|
2013-11-05 21:06:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy_objects(struct gl_hwdec *hw)
|
|
|
|
{
|
|
|
|
struct priv *p = hw->priv;
|
2014-12-03 20:13:59 +00:00
|
|
|
GL *gl = hw->gl;
|
2014-03-19 18:57:08 +00:00
|
|
|
struct vdp_functions *vdp = &p->ctx->vdp;
|
2013-11-05 21:06:48 +00:00
|
|
|
VdpStatus vdp_st;
|
|
|
|
|
2015-03-14 21:36:59 +00:00
|
|
|
if (p->mapped)
|
|
|
|
gl->VDPAUUnmapSurfacesNV(1, &p->vdpgl_surface);
|
|
|
|
p->mapped = false;
|
|
|
|
|
2013-11-05 21:06:48 +00:00
|
|
|
if (p->vdpgl_surface)
|
|
|
|
gl->VDPAUUnregisterSurfaceNV(p->vdpgl_surface);
|
|
|
|
p->vdpgl_surface = 0;
|
|
|
|
|
|
|
|
glDeleteTextures(1, &p->gl_texture);
|
|
|
|
p->gl_texture = 0;
|
|
|
|
|
|
|
|
if (p->vdp_surface != VDP_INVALID_HANDLE) {
|
|
|
|
vdp_st = vdp->output_surface_destroy(p->vdp_surface);
|
2013-12-21 17:05:23 +00:00
|
|
|
CHECK_VDP_WARNING(p, "Error when calling vdp_output_surface_destroy");
|
2013-11-05 21:06:48 +00:00
|
|
|
}
|
2014-04-29 13:11:26 +00:00
|
|
|
p->vdp_surface = VDP_INVALID_HANDLE;
|
2013-11-05 21:06:48 +00:00
|
|
|
|
|
|
|
glCheckError(gl, hw->log, "Before uninitializing OpenGL interop");
|
|
|
|
|
2016-02-08 12:39:33 +00:00
|
|
|
if (p->vdpgl_initialized)
|
|
|
|
gl->VDPAUFiniNV();
|
2013-11-05 21:06:48 +00:00
|
|
|
|
2016-02-08 12:39:33 +00:00
|
|
|
p->vdpgl_initialized = false;
|
|
|
|
|
|
|
|
glCheckError(gl, hw->log, "After uninitializing OpenGL interop");
|
2013-11-05 21:06:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void destroy(struct gl_hwdec *hw)
|
|
|
|
{
|
|
|
|
struct priv *p = hw->priv;
|
|
|
|
|
|
|
|
destroy_objects(hw);
|
2014-04-29 13:11:26 +00:00
|
|
|
mp_vdpau_mixer_destroy(p->mixer);
|
2016-05-09 17:42:03 +00:00
|
|
|
if (p->ctx)
|
|
|
|
hwdec_devices_remove(hw->devs, &p->ctx->hwctx);
|
2013-11-05 21:06:48 +00:00
|
|
|
mp_vdpau_destroy(p->ctx);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int create(struct gl_hwdec *hw)
|
|
|
|
{
|
2014-12-03 20:13:59 +00:00
|
|
|
GL *gl = hw->gl;
|
|
|
|
Display *x11disp = glXGetCurrentDisplay();
|
|
|
|
if (!x11disp)
|
2013-11-05 21:06:48 +00:00
|
|
|
return -1;
|
|
|
|
if (!(gl->mpgl_caps & MPGL_CAP_VDPAU))
|
|
|
|
return -1;
|
|
|
|
struct priv *p = talloc_zero(hw, struct priv);
|
|
|
|
hw->priv = p;
|
2013-12-21 17:05:23 +00:00
|
|
|
p->log = hw->log;
|
2015-06-20 20:26:57 +00:00
|
|
|
p->ctx = mp_vdpau_create_device_x11(hw->log, x11disp, true);
|
2013-11-05 21:06:48 +00:00
|
|
|
if (!p->ctx)
|
|
|
|
return -1;
|
2014-05-09 19:49:32 +00:00
|
|
|
if (mp_vdpau_handle_preemption(p->ctx, &p->preemption_counter) < 1)
|
|
|
|
return -1;
|
2014-04-29 13:11:26 +00:00
|
|
|
p->vdp_surface = VDP_INVALID_HANDLE;
|
|
|
|
p->mixer = mp_vdpau_mixer_create(p->ctx, hw->log);
|
2015-11-09 10:58:38 +00:00
|
|
|
if (hw->probing && mp_vdpau_guess_if_emulated(p->ctx)) {
|
2014-12-09 16:47:02 +00:00
|
|
|
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-05 21:06:48 +00:00
|
|
|
hw->converted_imgfmt = IMGFMT_RGB0;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-01-29 18:53:49 +00:00
|
|
|
static int reinit(struct gl_hwdec *hw, struct mp_image_params *params)
|
2013-11-05 21:06:48 +00:00
|
|
|
{
|
|
|
|
struct priv *p = hw->priv;
|
2014-12-03 20:13:59 +00:00
|
|
|
GL *gl = hw->gl;
|
2014-03-19 18:57:08 +00:00
|
|
|
struct vdp_functions *vdp = &p->ctx->vdp;
|
2013-11-05 21:06:48 +00:00
|
|
|
VdpStatus vdp_st;
|
|
|
|
|
|
|
|
destroy_objects(hw);
|
|
|
|
|
2015-09-24 21:48:57 +00:00
|
|
|
assert(params->imgfmt == hw->driver->imgfmt);
|
2013-11-05 21:06:48 +00:00
|
|
|
p->image_params = *params;
|
|
|
|
|
2016-01-25 18:40:35 +00:00
|
|
|
if (mp_vdpau_handle_preemption(p->ctx, &p->preemption_counter) < 0)
|
2013-11-05 21:06:48 +00:00
|
|
|
return -1;
|
|
|
|
|
2013-11-09 22:31:12 +00:00
|
|
|
gl->VDPAUInitNV(BRAINDEATH(p->ctx->vdp_device), p->ctx->get_proc_address);
|
2013-11-05 21:06:48 +00:00
|
|
|
|
2016-02-08 12:39:33 +00:00
|
|
|
p->vdpgl_initialized = true;
|
|
|
|
|
2013-11-05 21:06:48 +00:00
|
|
|
vdp_st = vdp->output_surface_create(p->ctx->vdp_device,
|
|
|
|
VDP_RGBA_FORMAT_B8G8R8A8,
|
|
|
|
params->w, params->h, &p->vdp_surface);
|
2013-12-21 17:05:23 +00:00
|
|
|
CHECK_VDP_ERROR(p, "Error when calling vdp_output_surface_create");
|
2013-11-05 21:06:48 +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);
|
|
|
|
gl->BindTexture(GL_TEXTURE_2D, 0);
|
|
|
|
|
2013-11-09 22:31:12 +00:00
|
|
|
p->vdpgl_surface = gl->VDPAURegisterOutputSurfaceNV(BRAINDEATH(p->vdp_surface),
|
2013-11-05 21:06:48 +00:00
|
|
|
GL_TEXTURE_2D,
|
|
|
|
1, &p->gl_texture);
|
|
|
|
if (!p->vdpgl_surface)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
gl->VDPAUSurfaceAccessNV(p->vdpgl_surface, GL_READ_ONLY);
|
|
|
|
|
|
|
|
glCheckError(gl, hw->log, "After initializing vdpau OpenGL interop");
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int map_image(struct gl_hwdec *hw, struct mp_image *hw_image,
|
|
|
|
GLuint *out_textures)
|
|
|
|
{
|
|
|
|
struct priv *p = hw->priv;
|
2014-12-03 20:13:59 +00:00
|
|
|
GL *gl = hw->gl;
|
2013-11-05 21:06:48 +00:00
|
|
|
|
2013-11-29 13:19:44 +00:00
|
|
|
assert(hw_image && hw_image->imgfmt == IMGFMT_VDPAU);
|
2013-11-05 21:06:48 +00:00
|
|
|
|
2014-05-09 19:49:32 +00:00
|
|
|
int pe = mp_vdpau_handle_preemption(p->ctx, &p->preemption_counter);
|
|
|
|
if (pe < 1) {
|
|
|
|
mark_vdpau_objects_uninitialized(hw);
|
|
|
|
if (pe < 0)
|
|
|
|
return -1;
|
|
|
|
if (reinit(hw, &p->image_params) < 0)
|
|
|
|
return -1;
|
|
|
|
}
|
2013-11-05 21:06:48 +00:00
|
|
|
|
|
|
|
if (!p->vdpgl_surface)
|
|
|
|
return -1;
|
|
|
|
|
2015-03-14 21:36:59 +00:00
|
|
|
if (p->mapped)
|
|
|
|
gl->VDPAUUnmapSurfacesNV(1, &p->vdpgl_surface);
|
|
|
|
|
2014-04-29 13:12:38 +00:00
|
|
|
mp_vdpau_mixer_render(p->mixer, NULL, p->vdp_surface, NULL, hw_image, NULL);
|
2013-11-05 21:06:48 +00:00
|
|
|
|
|
|
|
gl->VDPAUMapSurfacesNV(1, &p->vdpgl_surface);
|
2015-03-14 21:36:59 +00:00
|
|
|
p->mapped = true;
|
2013-11-05 21:06:48 +00:00
|
|
|
out_textures[0] = p->gl_texture;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
const struct gl_hwdec_driver gl_hwdec_vdpau = {
|
2016-02-01 19:02:52 +00:00
|
|
|
.name = "vdpau-glx",
|
|
|
|
.api = HWDEC_VDPAU,
|
2013-11-29 13:19:51 +00:00
|
|
|
.imgfmt = IMGFMT_VDPAU,
|
2013-11-05 21:06:48 +00:00
|
|
|
.create = create,
|
|
|
|
.reinit = reinit,
|
|
|
|
.map_image = map_image,
|
|
|
|
.destroy = destroy,
|
|
|
|
};
|