| 1 |
/* |
| 2 |
* Copyright (C) 2007-2018 Team Kodi |
| 3 |
* This file is part of Kodi - https://kodi.tv |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSES/README.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "VdpauGL.h" |
| 10 |
|
| 11 |
#include "cores/VideoPlayer/DVDCodecs/Video/VDPAU.h" |
| 12 |
#include "utils/log.h" |
| 13 |
|
| 14 |
#include <GL/glx.h> |
| 15 |
|
| 16 |
using namespace VDPAU; |
| 17 |
|
| 18 |
//----------------------------------------------------------------------------- |
| 19 |
// interop state |
| 20 |
//----------------------------------------------------------------------------- |
| 21 |
|
| 22 |
bool CInteropState::Init(void *device, void *procFunc, int64_t ident) |
| 23 |
{ |
| 24 |
m_device = device; |
| 25 |
m_procFunc = procFunc; |
| 26 |
m_ident = ident; |
| 27 |
|
| 28 |
m_interop.glVDPAUInitNV = (PFNGLVDPAUINITNVPROC)glXGetProcAddress((const GLubyte *) "glVDPAUInitNV"); |
| 29 |
m_interop.glVDPAUFiniNV = (PFNGLVDPAUFININVPROC)glXGetProcAddress((const GLubyte *) "glVDPAUFiniNV"); |
| 30 |
m_interop.glVDPAURegisterOutputSurfaceNV = (PFNGLVDPAUREGISTEROUTPUTSURFACENVPROC)glXGetProcAddress((const GLubyte *) "glVDPAURegisterOutputSurfaceNV"); |
| 31 |
m_interop.glVDPAURegisterVideoSurfaceNV = (PFNGLVDPAUREGISTERVIDEOSURFACENVPROC)glXGetProcAddress((const GLubyte *) "glVDPAURegisterVideoSurfaceNV"); |
| 32 |
m_interop.glVDPAUIsSurfaceNV = (PFNGLVDPAUISSURFACENVPROC)glXGetProcAddress((const GLubyte *) "glVDPAUIsSurfaceNV"); |
| 33 |
m_interop.glVDPAUUnregisterSurfaceNV = (PFNGLVDPAUUNREGISTERSURFACENVPROC)glXGetProcAddress((const GLubyte *) "glVDPAUUnregisterSurfaceNV"); |
| 34 |
m_interop.glVDPAUSurfaceAccessNV = (PFNGLVDPAUSURFACEACCESSNVPROC)glXGetProcAddress((const GLubyte *) "glVDPAUSurfaceAccessNV"); |
| 35 |
m_interop.glVDPAUMapSurfacesNV = (PFNGLVDPAUMAPSURFACESNVPROC)glXGetProcAddress((const GLubyte *) "glVDPAUMapSurfacesNV"); |
| 36 |
m_interop.glVDPAUUnmapSurfacesNV = (PFNGLVDPAUUNMAPSURFACESNVPROC)glXGetProcAddress((const GLubyte *) "glVDPAUUnmapSurfacesNV"); |
| 37 |
m_interop.glVDPAUGetSurfaceivNV = (PFNGLVDPAUGETSURFACEIVNVPROC)glXGetProcAddress((const GLubyte *) "glVDPAUGetSurfaceivNV"); |
| 38 |
|
| 39 |
while (glGetError() != GL_NO_ERROR); |
| 40 |
m_interop.glVDPAUInitNV(m_device, m_procFunc); |
| 41 |
if (glGetError() != GL_NO_ERROR) |
| 42 |
{ |
| 43 |
CLog::Log(LOGERROR, "CInteropState::Init - GLInitInterop glVDPAUInitNV failed"); |
| 44 |
return false; |
| 45 |
} |
| 46 |
CLog::Log(LOGINFO, "CInteropState::Init: vdpau gl interop initialized"); |
| 47 |
|
| 48 |
m_interop.textureTarget = GL_TEXTURE_2D; |
| 49 |
|
| 50 |
return true; |
| 51 |
} |
| 52 |
|
| 53 |
void CInteropState::Finish() |
| 54 |
{ |
| 55 |
m_interop.glVDPAUFiniNV(); |
| 56 |
m_device = nullptr; |
| 57 |
m_procFunc = nullptr; |
| 58 |
} |
| 59 |
|
| 60 |
InteropInfo &CInteropState::GetInterop() |
| 61 |
{ |
| 62 |
return m_interop; |
| 63 |
} |
| 64 |
|
| 65 |
bool CInteropState::NeedInit(void *device, void *procFunc, int64_t ident) |
| 66 |
{ |
| 67 |
if (m_device != device) |
| 68 |
return true; |
| 69 |
if (m_procFunc != procFunc) |
| 70 |
return true; |
| 71 |
if (m_ident != ident) |
| 72 |
return true; |
| 73 |
|
| 74 |
return false; |
| 75 |
} |
| 76 |
|
| 77 |
//----------------------------------------------------------------------------- |
| 78 |
// textures |
| 79 |
//----------------------------------------------------------------------------- |
| 80 |
|
| 81 |
void CVdpauTexture::Init(InteropInfo &interop) |
| 82 |
{ |
| 83 |
m_interop = interop; |
| 84 |
} |
| 85 |
|
| 86 |
bool CVdpauTexture::Map(CVdpauRenderPicture *pic) |
| 87 |
{ |
| 88 |
|
| 89 |
if (m_vdpauPic) |
| 90 |
return true; |
| 91 |
|
| 92 |
m_vdpauPic = pic; |
| 93 |
m_vdpauPic->Acquire(); |
| 94 |
|
| 95 |
m_texWidth = pic->width; |
| 96 |
m_texHeight = pic->height; |
| 97 |
|
| 98 |
bool success = false; |
| 99 |
if (m_vdpauPic->procPic.isYuv) |
| 100 |
success = MapNV12(); |
| 101 |
else |
| 102 |
success = MapRGB(); |
| 103 |
|
| 104 |
if (!success) |
| 105 |
{ |
| 106 |
m_vdpauPic->Release(); |
| 107 |
m_vdpauPic = nullptr; |
| 108 |
} |
| 109 |
|
| 110 |
return success; |
| 111 |
} |
| 112 |
|
| 113 |
void CVdpauTexture::Unmap() |
| 114 |
{ |
| 115 |
if (!m_vdpauPic) |
| 116 |
return; |
| 117 |
|
| 118 |
if (m_vdpauPic->procPic.isYuv) |
| 119 |
UnmapNV12(); |
| 120 |
else |
| 121 |
UnmapRGB(); |
| 122 |
|
| 123 |
m_vdpauPic->Release(); |
| 124 |
m_vdpauPic = nullptr; |
| 125 |
} |
| 126 |
|
| 127 |
bool CVdpauTexture::MapNV12() |
| 128 |
{ |
| 129 |
GLuint textures[4]; |
| 130 |
while (glGetError() != GL_NO_ERROR) ; |
| 131 |
glGenTextures(4, textures); |
| 132 |
if (glGetError() != GL_NO_ERROR) |
| 133 |
{ |
| 134 |
CLog::Log(LOGERROR, "CVdpauTexture::MapNV12 error creating texture"); |
| 135 |
return false; |
| 136 |
} |
| 137 |
|
| 138 |
const void *videoSurface = reinterpret_cast<void*>(m_vdpauPic->procPic.videoSurface); |
| 139 |
m_glSurface.glVdpauSurface = m_interop.glVDPAURegisterVideoSurfaceNV(videoSurface, |
| 140 |
m_interop.textureTarget, 4, textures); |
| 141 |
if (glGetError() != GL_NO_ERROR) |
| 142 |
{ |
| 143 |
CLog::Log(LOGERROR, "CVdpauTexture::MapNV12 error register video surface"); |
| 144 |
glDeleteTextures(4, textures); |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
m_interop.glVDPAUSurfaceAccessNV(m_glSurface.glVdpauSurface, GL_READ_ONLY); |
| 149 |
if (glGetError() != GL_NO_ERROR) |
| 150 |
{ |
| 151 |
CLog::Log(LOGERROR, "CVdpauTexture::MapNV12 error setting access"); |
| 152 |
glDeleteTextures(4, textures); |
| 153 |
return false; |
| 154 |
} |
| 155 |
|
| 156 |
m_interop.glVDPAUMapSurfacesNV(1, &m_glSurface.glVdpauSurface); |
| 157 |
if (glGetError() != GL_NO_ERROR) |
| 158 |
{ |
| 159 |
CLog::Log(LOGERROR, "CVdpauTexture::MapNV12 error mapping surface"); |
| 160 |
glDeleteTextures(4, textures); |
| 161 |
return false; |
| 162 |
} |
| 163 |
|
| 164 |
m_interop.glVDPAUUnregisterSurfaceNV(m_glSurface.glVdpauSurface); |
| 165 |
|
| 166 |
m_textureTopY = textures[0]; |
| 167 |
m_textureTopUV = textures[2]; |
| 168 |
m_textureBotY = textures[1]; |
| 169 |
m_textureBotUV = textures[3]; |
| 170 |
|
| 171 |
return true; |
| 172 |
} |
| 173 |
|
| 174 |
void CVdpauTexture::UnmapNV12() |
| 175 |
{ |
| 176 |
m_interop.glVDPAUUnmapSurfacesNV(1, &m_glSurface.glVdpauSurface); |
| 177 |
glDeleteTextures(1, &m_textureTopY); |
| 178 |
glDeleteTextures(1, &m_textureTopUV); |
| 179 |
glDeleteTextures(1, &m_textureBotY); |
| 180 |
glDeleteTextures(1, &m_textureBotUV); |
| 181 |
} |
| 182 |
|
| 183 |
bool CVdpauTexture::MapRGB() |
| 184 |
{ |
| 185 |
glGenTextures(1, &m_texture); |
| 186 |
const void *outSurface = reinterpret_cast<void*>(m_vdpauPic->procPic.outputSurface); |
| 187 |
m_glSurface.glVdpauSurface = m_interop.glVDPAURegisterOutputSurfaceNV(outSurface, |
| 188 |
m_interop.textureTarget, 1, &m_texture); |
| 189 |
GLenum err = glGetError(); |
| 190 |
if (err != GL_NO_ERROR) |
| 191 |
{ |
| 192 |
CLog::Log(LOGERROR, "CVdpauTexture::MapRGB error register output surface: {}", err); |
| 193 |
return false; |
| 194 |
} |
| 195 |
|
| 196 |
m_interop.glVDPAUSurfaceAccessNV(m_glSurface.glVdpauSurface, GL_READ_ONLY); |
| 197 |
if (glGetError() != GL_NO_ERROR) |
| 198 |
{ |
| 199 |
CLog::Log(LOGERROR, "CVdpauTexture::MapRGB error setting access"); |
| 200 |
glDeleteTextures(1, &m_texture); |
| 201 |
return false; |
| 202 |
} |
| 203 |
|
| 204 |
while (glGetError() != GL_NO_ERROR) ; |
| 205 |
m_interop.glVDPAUMapSurfacesNV(1, &m_glSurface.glVdpauSurface); |
| 206 |
if (glGetError() != GL_NO_ERROR) |
| 207 |
{ |
| 208 |
CLog::Log(LOGERROR, "VDPAU::COutput error mapping surface"); |
| 209 |
glDeleteTextures(1, &m_texture); |
| 210 |
return false; |
| 211 |
} |
| 212 |
|
| 213 |
return true; |
| 214 |
} |
| 215 |
|
| 216 |
void CVdpauTexture::UnmapRGB() |
| 217 |
{ |
| 218 |
m_interop.glVDPAUUnmapSurfacesNV(1, &m_glSurface.glVdpauSurface); |
| 219 |
m_interop.glVDPAUUnregisterSurfaceNV(m_glSurface.glVdpauSurface); |
| 220 |
glDeleteTextures(1, &m_texture); |
| 221 |
} |
| 222 |
|