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