| 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 "RendererVAAPIGL.h" |
| 10 |
|
| 11 |
#include "../RenderFactory.h" |
| 12 |
#include "cores/VideoPlayer/DVDCodecs/Video/VAAPI.h" |
| 13 |
#include "utils/GLUtils.h" |
| 14 |
#include "utils/log.h" |
| 15 |
|
| 16 |
#include <memory> |
| 17 |
|
| 18 |
using namespace VAAPI; |
| 19 |
|
| 20 |
IVaapiWinSystem* CRendererVAAPIGL::m_pWinSystem = nullptr; |
| 21 |
|
| 22 |
CBaseRenderer* CRendererVAAPIGL::Create(CVideoBuffer* buffer) |
| 23 |
{ |
| 24 |
CVaapiRenderPicture *vb = dynamic_cast<CVaapiRenderPicture*>(buffer); |
| 25 |
if (vb) |
| 26 |
return new CRendererVAAPIGL(); |
| 27 |
|
| 28 |
return nullptr; |
| 29 |
} |
| 30 |
|
| 31 |
void CRendererVAAPIGL::Register(IVaapiWinSystem* winSystem, |
| 32 |
VADisplay vaDpy, |
| 33 |
EGLDisplay eglDisplay, |
| 34 |
bool& general, |
| 35 |
bool& deepColor) |
| 36 |
{ |
| 37 |
general = deepColor = false; |
| 38 |
|
| 39 |
int major_version, minor_version; |
| 40 |
if (vaInitialize(vaDpy, &major_version, &minor_version) != VA_STATUS_SUCCESS) |
| 41 |
{ |
| 42 |
vaTerminate(vaDpy); |
| 43 |
return; |
| 44 |
} |
| 45 |
|
| 46 |
CVaapi2Texture::TestInterop(vaDpy, eglDisplay, general, deepColor); |
| 47 |
CLog::Log(LOGDEBUG, "Vaapi2 EGL interop test results: general {}, deepColor {}", |
| 48 |
general ? "yes" : "no", deepColor ? "yes" : "no"); |
| 49 |
if (!general) |
| 50 |
{ |
| 51 |
CVaapi1Texture::TestInterop(vaDpy, eglDisplay, general, deepColor); |
| 52 |
CLog::Log(LOGDEBUG, "Vaapi1 EGL interop test results: general {}, deepColor {}", |
| 53 |
general ? "yes" : "no", deepColor ? "yes" : "no"); |
| 54 |
} |
| 55 |
|
| 56 |
vaTerminate(vaDpy); |
| 57 |
|
| 58 |
if (general) |
| 59 |
{ |
| 60 |
VIDEOPLAYER::CRendererFactory::RegisterRenderer("vaapi", CRendererVAAPIGL::Create); |
| 61 |
m_pWinSystem = winSystem; |
| 62 |
} |
| 63 |
} |
| 64 |
|
| 65 |
CRendererVAAPIGL::CRendererVAAPIGL() = default; |
| 66 |
|
| 67 |
CRendererVAAPIGL::~CRendererVAAPIGL() |
| 68 |
{ |
| 69 |
for (int i = 0; i < NUM_BUFFERS; ++i) |
| 70 |
{ |
| 71 |
DeleteTexture(i); |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
bool CRendererVAAPIGL::Configure(const VideoPicture& picture, float fps, unsigned int orientation) |
| 76 |
{ |
| 77 |
CVaapiRenderPicture *pic = dynamic_cast<CVaapiRenderPicture*>(picture.videoBuffer); |
| 78 |
if (pic->procPic.videoSurface == VA_INVALID_ID) |
| 79 |
{ |
| 80 |
m_isVAAPIBuffer = false; |
| 81 |
} |
| 82 |
else |
| 83 |
{ |
| 84 |
m_isVAAPIBuffer = true; |
| 85 |
|
| 86 |
InteropInfo interop; |
| 87 |
interop.textureTarget = GL_TEXTURE_2D; |
| 88 |
interop.eglCreateImageKHR = (PFNEGLCREATEIMAGEKHRPROC)eglGetProcAddress("eglCreateImageKHR"); |
| 89 |
interop.eglDestroyImageKHR = (PFNEGLDESTROYIMAGEKHRPROC)eglGetProcAddress("eglDestroyImageKHR"); |
| 90 |
interop.glEGLImageTargetTexture2DOES = (PFNGLEGLIMAGETARGETTEXTURE2DOESPROC)eglGetProcAddress("glEGLImageTargetTexture2DOES"); |
| 91 |
interop.eglDisplay = CRendererVAAPIGL::m_pWinSystem->GetEGLDisplay(); |
| 92 |
|
| 93 |
bool useVaapi2 = VAAPI::CVaapi2Texture::TestInteropGeneral( |
| 94 |
pic->vadsp, CRendererVAAPIGL::m_pWinSystem->GetEGLDisplay()); |
| 95 |
|
| 96 |
for (auto &tex : m_vaapiTextures) |
| 97 |
{ |
| 98 |
if (useVaapi2) |
| 99 |
{ |
| 100 |
tex = std::make_unique<VAAPI::CVaapi2Texture>(); |
| 101 |
} |
| 102 |
else |
| 103 |
{ |
| 104 |
tex = std::make_unique<VAAPI::CVaapi1Texture>(); |
| 105 |
} |
| 106 |
tex->Init(interop); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
for (auto &fence : m_fences) |
| 111 |
{ |
| 112 |
fence = {}; |
| 113 |
} |
| 114 |
|
| 115 |
return CLinuxRendererGL::Configure(picture, fps, orientation); |
| 116 |
} |
| 117 |
|
| 118 |
bool CRendererVAAPIGL::Flush(bool saveBuffers) |
| 119 |
{ |
| 120 |
for (auto &vaapiTexture : m_vaapiTextures) |
| 121 |
{ |
| 122 |
if (m_isVAAPIBuffer) |
| 123 |
{ |
| 124 |
vaapiTexture->Unmap(); |
| 125 |
} |
| 126 |
} |
| 127 |
return CLinuxRendererGL::Flush(saveBuffers); |
| 128 |
} |
| 129 |
|
| 130 |
bool CRendererVAAPIGL::ConfigChanged(const VideoPicture& picture) |
| 131 |
{ |
| 132 |
CVaapiRenderPicture *pic = dynamic_cast<CVaapiRenderPicture*>(picture.videoBuffer); |
| 133 |
if ((pic->procPic.videoSurface != VA_INVALID_ID && !m_isVAAPIBuffer) || |
| 134 |
(pic->procPic.videoSurface == VA_INVALID_ID && m_isVAAPIBuffer)) |
| 135 |
return true; |
| 136 |
|
| 137 |
return false; |
| 138 |
} |
| 139 |
|
| 140 |
bool CRendererVAAPIGL::Supports(ERENDERFEATURE feature) const |
| 141 |
{ |
| 142 |
return CLinuxRendererGL::Supports(feature); |
| 143 |
} |
| 144 |
|
| 145 |
bool CRendererVAAPIGL::Supports(ESCALINGMETHOD method) const |
| 146 |
{ |
| 147 |
return CLinuxRendererGL::Supports(method); |
| 148 |
} |
| 149 |
|
| 150 |
EShaderFormat CRendererVAAPIGL::GetShaderFormat() |
| 151 |
{ |
| 152 |
return SHADER_NV12; |
| 153 |
} |
| 154 |
|
| 155 |
bool CRendererVAAPIGL::LoadShadersHook() |
| 156 |
{ |
| 157 |
return false; |
| 158 |
} |
| 159 |
|
| 160 |
bool CRendererVAAPIGL::RenderHook(int idx) |
| 161 |
{ |
| 162 |
return false; |
| 163 |
} |
| 164 |
|
| 165 |
bool CRendererVAAPIGL::CreateTexture(int index) |
| 166 |
{ |
| 167 |
if (!m_isVAAPIBuffer) |
| 168 |
{ |
| 169 |
return CreateNV12Texture(index); |
| 170 |
} |
| 171 |
|
| 172 |
CPictureBuffer &buf = m_buffers[index]; |
| 173 |
YuvImage &im = buf.image; |
| 174 |
CYuvPlane (&planes)[YuvImage::MAX_PLANES] = buf.fields[0]; |
| 175 |
|
| 176 |
DeleteTexture(index); |
| 177 |
|
| 178 |
memset(&im, 0, sizeof(im)); |
| 179 |
memset(&planes, 0, sizeof(CYuvPlane[YuvImage::MAX_PLANES])); |
| 180 |
im.height = m_sourceHeight; |
| 181 |
im.width = m_sourceWidth; |
| 182 |
im.cshift_x = 1; |
| 183 |
im.cshift_y = 1; |
| 184 |
|
| 185 |
planes[0].id = 1; |
| 186 |
|
| 187 |
return true; |
| 188 |
} |
| 189 |
|
| 190 |
void CRendererVAAPIGL::DeleteTexture(int index) |
| 191 |
{ |
| 192 |
ReleaseBuffer(index); |
| 193 |
|
| 194 |
if (!m_isVAAPIBuffer) |
| 195 |
{ |
| 196 |
DeleteNV12Texture(index); |
| 197 |
return; |
| 198 |
} |
| 199 |
|
| 200 |
CPictureBuffer &buf = m_buffers[index]; |
| 201 |
buf.fields[FIELD_FULL][0].id = 0; |
| 202 |
buf.fields[FIELD_FULL][1].id = 0; |
| 203 |
buf.fields[FIELD_FULL][2].id = 0; |
| 204 |
} |
| 205 |
|
| 206 |
bool CRendererVAAPIGL::UploadTexture(int index) |
| 207 |
{ |
| 208 |
CPictureBuffer &buf = m_buffers[index]; |
| 209 |
CVaapiRenderPicture *pic = dynamic_cast<CVaapiRenderPicture*>(buf.videoBuffer); |
| 210 |
|
| 211 |
if (!pic || !pic->valid) |
| 212 |
{ |
| 213 |
return false; |
| 214 |
} |
| 215 |
|
| 216 |
if (!m_isVAAPIBuffer) |
| 217 |
{ |
| 218 |
if (!m_buffers[index].loaded) |
| 219 |
{ |
| 220 |
YuvImage &dst = m_buffers[index].image; |
| 221 |
YuvImage src; |
| 222 |
pic->GetPlanes(src.plane); |
| 223 |
pic->GetStrides(src.stride); |
| 224 |
UnBindPbo(m_buffers[index]); |
| 225 |
CVideoBuffer::CopyNV12Picture(&dst, &src); |
| 226 |
BindPbo(m_buffers[index]); |
| 227 |
} |
| 228 |
CalculateTextureSourceRects(index, 3); |
| 229 |
return UploadNV12Texture(index); |
| 230 |
} |
| 231 |
|
| 232 |
m_vaapiTextures[index]->Map(pic); |
| 233 |
|
| 234 |
const YuvImage& im = buf.image; |
| 235 |
CYuvPlane (&planes)[3] = buf.fields[0]; |
| 236 |
|
| 237 |
auto size = m_vaapiTextures[index]->GetTextureSize(); |
| 238 |
planes[0].texwidth = size.Width(); |
| 239 |
planes[0].texheight = size.Height(); |
| 240 |
|
| 241 |
planes[1].texwidth = planes[0].texwidth >> im.cshift_x; |
| 242 |
planes[1].texheight = planes[0].texheight >> im.cshift_y; |
| 243 |
planes[2].texwidth = planes[1].texwidth; |
| 244 |
planes[2].texheight = planes[1].texheight; |
| 245 |
|
| 246 |
for (int p = 0; p < 3; p++) |
| 247 |
{ |
| 248 |
planes[p].pixpertex_x = 1; |
| 249 |
planes[p].pixpertex_y = 1; |
| 250 |
} |
| 251 |
|
| 252 |
// set textures |
| 253 |
planes[0].id = m_vaapiTextures[index]->GetTextureY(); |
| 254 |
planes[1].id = m_vaapiTextures[index]->GetTextureVU(); |
| 255 |
planes[2].id = m_vaapiTextures[index]->GetTextureVU(); |
| 256 |
|
| 257 |
for (int p=0; p<2; p++) |
| 258 |
{ |
| 259 |
glBindTexture(m_textureTarget, planes[p].id); |
| 260 |
glTexParameteri(m_textureTarget, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 261 |
glTexParameteri(m_textureTarget, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 262 |
glTexParameteri(m_textureTarget, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 263 |
glTexParameteri(m_textureTarget, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 264 |
|
| 265 |
glBindTexture(m_textureTarget, 0); |
| 266 |
VerifyGLState(); |
| 267 |
} |
| 268 |
|
| 269 |
CalculateTextureSourceRects(index, 3); |
| 270 |
return true; |
| 271 |
} |
| 272 |
|
| 273 |
void CRendererVAAPIGL::AfterRenderHook(int idx) |
| 274 |
{ |
| 275 |
if (glIsSync(m_fences[idx])) |
| 276 |
{ |
| 277 |
glDeleteSync(m_fences[idx]); |
| 278 |
m_fences[idx] = {}; |
| 279 |
} |
| 280 |
m_fences[idx] = glFenceSync(GL_SYNC_GPU_COMMANDS_COMPLETE, 0); |
| 281 |
} |
| 282 |
|
| 283 |
bool CRendererVAAPIGL::NeedBuffer(int idx) |
| 284 |
{ |
| 285 |
if (glIsSync(m_fences[idx])) |
| 286 |
{ |
| 287 |
GLint state; |
| 288 |
GLsizei length; |
| 289 |
glGetSynciv(m_fences[idx], GL_SYNC_STATUS, 1, &length, &state); |
| 290 |
if (state == GL_SIGNALED) |
| 291 |
{ |
| 292 |
glDeleteSync(m_fences[idx]); |
| 293 |
m_fences[idx] = {}; |
| 294 |
} |
| 295 |
else |
| 296 |
{ |
| 297 |
return true; |
| 298 |
} |
| 299 |
} |
| 300 |
|
| 301 |
return false; |
| 302 |
} |
| 303 |
|
| 304 |
void CRendererVAAPIGL::ReleaseBuffer(int idx) |
| 305 |
{ |
| 306 |
if (glIsSync(m_fences[idx])) |
| 307 |
{ |
| 308 |
glDeleteSync(m_fences[idx]); |
| 309 |
m_fences[idx] = {}; |
| 310 |
} |
| 311 |
if (m_isVAAPIBuffer) |
| 312 |
{ |
| 313 |
m_vaapiTextures[idx]->Unmap(); |
| 314 |
} |
| 315 |
CLinuxRendererGL::ReleaseBuffer(idx); |
| 316 |
} |