| 1 |
/* |
| 2 |
* Copyright (C) 2005-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 "WinSystemX11GLContext.h" |
| 10 |
|
| 11 |
#include "GLContextEGL.h" |
| 12 |
#include "OptionalsReg.h" |
| 13 |
#include "ServiceBroker.h" |
| 14 |
#include "VideoSyncOML.h" |
| 15 |
#include "X11DPMSSupport.h" |
| 16 |
#include "application/AppParams.h" |
| 17 |
#include "application/ApplicationComponents.h" |
| 18 |
#include "application/ApplicationSkinHandling.h" |
| 19 |
#include "cores/RetroPlayer/process/X11/RPProcessInfoX11.h" |
| 20 |
#include "cores/RetroPlayer/rendering/VideoRenderers/RPRendererOpenGL.h" |
| 21 |
#include "cores/VideoPlayer/DVDCodecs/DVDFactoryCodec.h" |
| 22 |
#include "cores/VideoPlayer/Process/X11/ProcessInfoX11.h" |
| 23 |
#include "cores/VideoPlayer/VideoReferenceClock.h" |
| 24 |
#include "cores/VideoPlayer/VideoRenderers/LinuxRendererGL.h" |
| 25 |
#include "cores/VideoPlayer/VideoRenderers/RenderFactory.h" |
| 26 |
#include "guilib/DispResource.h" |
| 27 |
#include "rendering/gl/ScreenshotSurfaceGL.h" |
| 28 |
#include "windowing/GraphicContext.h" |
| 29 |
#include "windowing/WindowSystemFactory.h" |
| 30 |
|
| 31 |
#include <memory> |
| 32 |
#include <mutex> |
| 33 |
#include <vector> |
| 34 |
|
| 35 |
#include <X11/Xlib.h> |
| 36 |
#include <X11/Xutil.h> |
| 37 |
|
| 38 |
using namespace KODI; |
| 39 |
using namespace KODI::WINDOWING::X11; |
| 40 |
|
| 41 |
|
| 42 |
void CWinSystemX11GLContext::Register() |
| 43 |
{ |
| 44 |
KODI::WINDOWING::CWindowSystemFactory::RegisterWindowSystem(CreateWinSystem, "x11"); |
| 45 |
} |
| 46 |
|
| 47 |
std::unique_ptr<CWinSystemBase> CWinSystemX11GLContext::CreateWinSystem() |
| 48 |
{ |
| 49 |
return std::make_unique<CWinSystemX11GLContext>(); |
| 50 |
} |
| 51 |
|
| 52 |
CWinSystemX11GLContext::~CWinSystemX11GLContext() |
| 53 |
{ |
| 54 |
delete m_pGLContext; |
| 55 |
} |
| 56 |
|
| 57 |
void CWinSystemX11GLContext::PresentRenderImpl(bool rendered) |
| 58 |
{ |
| 59 |
if (rendered) |
| 60 |
m_pGLContext->SwapBuffers(); |
| 61 |
|
| 62 |
if (m_delayDispReset && m_dispResetTimer.IsTimePast()) |
| 63 |
{ |
| 64 |
m_delayDispReset = false; |
| 65 |
std::unique_lock<CCriticalSection> lock(m_resourceSection); |
| 66 |
// tell any shared resources |
| 67 |
for (std::vector<IDispResource *>::iterator i = m_resources.begin(); i != m_resources.end(); ++i) |
| 68 |
(*i)->OnResetDisplay(); |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
void CWinSystemX11GLContext::SetVSyncImpl(bool enable) |
| 73 |
{ |
| 74 |
m_pGLContext->SetVSync(enable); |
| 75 |
} |
| 76 |
|
| 77 |
bool CWinSystemX11GLContext::IsExtSupported(const char* extension) const |
| 78 |
{ |
| 79 |
if(strncmp(extension, m_pGLContext->ExtPrefix().c_str(), 4) != 0) |
| 80 |
return CRenderSystemGL::IsExtSupported(extension); |
| 81 |
|
| 82 |
return m_pGLContext->IsExtSupported(extension); |
| 83 |
} |
| 84 |
|
| 85 |
XID CWinSystemX11GLContext::GetWindow() const |
| 86 |
{ |
| 87 |
return GLXGetWindow(m_pGLContext); |
| 88 |
} |
| 89 |
|
| 90 |
void* CWinSystemX11GLContext::GetGlxContext() const |
| 91 |
{ |
| 92 |
return GLXGetContext(m_pGLContext); |
| 93 |
} |
| 94 |
|
| 95 |
EGLDisplay CWinSystemX11GLContext::GetEGLDisplay() const |
| 96 |
{ |
| 97 |
return static_cast<CGLContextEGL*>(m_pGLContext)->m_eglDisplay; |
| 98 |
} |
| 99 |
|
| 100 |
EGLSurface CWinSystemX11GLContext::GetEGLSurface() const |
| 101 |
{ |
| 102 |
return static_cast<CGLContextEGL*>(m_pGLContext)->m_eglSurface; |
| 103 |
} |
| 104 |
|
| 105 |
EGLContext CWinSystemX11GLContext::GetEGLContext() const |
| 106 |
{ |
| 107 |
return static_cast<CGLContextEGL*>(m_pGLContext)->m_eglContext; |
| 108 |
} |
| 109 |
|
| 110 |
EGLConfig CWinSystemX11GLContext::GetEGLConfig() const |
| 111 |
{ |
| 112 |
return static_cast<CGLContextEGL*>(m_pGLContext)->m_eglConfig; |
| 113 |
} |
| 114 |
|
| 115 |
bool CWinSystemX11GLContext::SetWindow(int width, int height, bool fullscreen, const std::string &output, int *winstate) |
| 116 |
{ |
| 117 |
int newwin = 0; |
| 118 |
|
| 119 |
CWinSystemX11::SetWindow(width, height, fullscreen, output, &newwin); |
| 120 |
if (newwin) |
| 121 |
{ |
| 122 |
RefreshGLContext(m_currentOutput.compare(output) != 0); |
| 123 |
XSync(m_dpy, False); |
| 124 |
CServiceBroker::GetWinSystem()->GetGfxContext().Clear(0); |
| 125 |
CServiceBroker::GetWinSystem()->GetGfxContext().Flip(true, false); |
| 126 |
ResetVSync(); |
| 127 |
|
| 128 |
m_windowDirty = false; |
| 129 |
m_bIsInternalXrr = false; |
| 130 |
|
| 131 |
if (!m_delayDispReset) |
| 132 |
{ |
| 133 |
std::unique_lock<CCriticalSection> lock(m_resourceSection); |
| 134 |
// tell any shared resources |
| 135 |
for (std::vector<IDispResource *>::iterator i = m_resources.begin(); i != m_resources.end(); ++i) |
| 136 |
(*i)->OnResetDisplay(); |
| 137 |
} |
| 138 |
} |
| 139 |
return true; |
| 140 |
} |
| 141 |
|
| 142 |
bool CWinSystemX11GLContext::CreateNewWindow(const std::string& name, bool fullScreen, RESOLUTION_INFO& res) |
| 143 |
{ |
| 144 |
if(!CWinSystemX11::CreateNewWindow(name, fullScreen, res)) |
| 145 |
return false; |
| 146 |
|
| 147 |
m_pGLContext->QueryExtensions(); |
| 148 |
return true; |
| 149 |
} |
| 150 |
|
| 151 |
bool CWinSystemX11GLContext::ResizeWindow(int newWidth, int newHeight, int newLeft, int newTop) |
| 152 |
{ |
| 153 |
m_newGlContext = false; |
| 154 |
CWinSystemX11::ResizeWindow(newWidth, newHeight, newLeft, newTop); |
| 155 |
CRenderSystemGL::ResetRenderSystem(newWidth, newHeight); |
| 156 |
|
| 157 |
if (m_newGlContext) |
| 158 |
{ |
| 159 |
auto& components = CServiceBroker::GetAppComponents(); |
| 160 |
const auto appSkin = components.GetComponent<CApplicationSkinHandling>(); |
| 161 |
appSkin->ReloadSkin(); |
| 162 |
} |
| 163 |
|
| 164 |
return true; |
| 165 |
} |
| 166 |
|
| 167 |
void CWinSystemX11GLContext::FinishWindowResize(int newWidth, int newHeight) |
| 168 |
{ |
| 169 |
m_newGlContext = false; |
| 170 |
CWinSystemX11::FinishWindowResize(newWidth, newHeight); |
| 171 |
CRenderSystemGL::ResetRenderSystem(newWidth, newHeight); |
| 172 |
|
| 173 |
if (m_newGlContext) |
| 174 |
{ |
| 175 |
auto& components = CServiceBroker::GetAppComponents(); |
| 176 |
const auto appSkin = components.GetComponent<CApplicationSkinHandling>(); |
| 177 |
appSkin->ReloadSkin(); |
| 178 |
} |
| 179 |
} |
| 180 |
|
| 181 |
bool CWinSystemX11GLContext::SetFullScreen(bool fullScreen, RESOLUTION_INFO& res, bool blankOtherDisplays) |
| 182 |
{ |
| 183 |
m_newGlContext = false; |
| 184 |
CWinSystemX11::SetFullScreen(fullScreen, res, blankOtherDisplays); |
| 185 |
CRenderSystemGL::ResetRenderSystem(res.iWidth, res.iHeight); |
| 186 |
|
| 187 |
if (m_newGlContext) |
| 188 |
{ |
| 189 |
auto& components = CServiceBroker::GetAppComponents(); |
| 190 |
const auto appSkin = components.GetComponent<CApplicationSkinHandling>(); |
| 191 |
appSkin->ReloadSkin(); |
| 192 |
} |
| 193 |
|
| 194 |
return true; |
| 195 |
} |
| 196 |
|
| 197 |
bool CWinSystemX11GLContext::DestroyWindowSystem() |
| 198 |
{ |
| 199 |
if (m_pGLContext) |
| 200 |
m_pGLContext->Destroy(); |
| 201 |
return CWinSystemX11::DestroyWindowSystem(); |
| 202 |
} |
| 203 |
|
| 204 |
bool CWinSystemX11GLContext::DestroyWindow() |
| 205 |
{ |
| 206 |
if (m_pGLContext) |
| 207 |
m_pGLContext->Detach(); |
| 208 |
return CWinSystemX11::DestroyWindow(); |
| 209 |
} |
| 210 |
|
| 211 |
XVisualInfo* CWinSystemX11GLContext::GetVisual() |
| 212 |
{ |
| 213 |
int count = 0; |
| 214 |
XVisualInfo vTemplate; |
| 215 |
XVisualInfo *visual = nullptr; |
| 216 |
|
| 217 |
int vMask = VisualScreenMask | VisualDepthMask | VisualClassMask; |
| 218 |
|
| 219 |
vTemplate.screen = m_screen; |
| 220 |
vTemplate.depth = 24; |
| 221 |
vTemplate.c_class = TrueColor; |
| 222 |
|
| 223 |
visual = XGetVisualInfo(m_dpy, vMask, &vTemplate, &count); |
| 224 |
|
| 225 |
if (!visual) |
| 226 |
{ |
| 227 |
vTemplate.depth = 30; |
| 228 |
visual = XGetVisualInfo(m_dpy, vMask, &vTemplate, &count); |
| 229 |
} |
| 230 |
|
| 231 |
return visual; |
| 232 |
} |
| 233 |
|
| 234 |
bool CWinSystemX11GLContext::RefreshGLContext(bool force) |
| 235 |
{ |
| 236 |
bool success = false; |
| 237 |
if (m_pGLContext) |
| 238 |
{ |
| 239 |
if (force) |
| 240 |
{ |
| 241 |
auto& components = CServiceBroker::GetAppComponents(); |
| 242 |
const auto appSkin = components.GetComponent<CApplicationSkinHandling>(); |
| 243 |
appSkin->UnloadSkin(); |
| 244 |
CRenderSystemGL::DestroyRenderSystem(); |
| 245 |
} |
| 246 |
success = m_pGLContext->Refresh(force, m_screen, m_glWindow, m_newGlContext); |
| 247 |
if (!success) |
| 248 |
{ |
| 249 |
success = m_pGLContext->CreatePB(); |
| 250 |
m_newGlContext = true; |
| 251 |
} |
| 252 |
if (force) |
| 253 |
CRenderSystemGL::InitRenderSystem(); |
| 254 |
return success; |
| 255 |
} |
| 256 |
|
| 257 |
m_dpms = std::make_shared<CX11DPMSSupport>(); |
| 258 |
VIDEOPLAYER::CProcessInfoX11::Register(); |
| 259 |
RETRO::CRPProcessInfoX11::Register(); |
| 260 |
RETRO::CRPProcessInfoX11::RegisterRendererFactory(new RETRO::CRendererFactoryOpenGL); |
| 261 |
CDVDFactoryCodec::ClearHWAccels(); |
| 262 |
VIDEOPLAYER::CRendererFactory::ClearRenderer(); |
| 263 |
CLinuxRendererGL::Register(); |
| 264 |
|
| 265 |
CScreenshotSurfaceGL::Register(); |
| 266 |
|
| 267 |
std::string gpuvendor; |
| 268 |
const char* vend = (const char*) glGetString(GL_VENDOR); |
| 269 |
if (vend) |
| 270 |
gpuvendor = vend; |
| 271 |
std::transform(gpuvendor.begin(), gpuvendor.end(), gpuvendor.begin(), ::tolower); |
| 272 |
bool isNvidia = (gpuvendor.compare(0, 6, "nvidia") == 0); |
| 273 |
bool isIntel = (gpuvendor.compare(0, 5, "intel") == 0); |
| 274 |
|
| 275 |
std::string_view gli = CServiceBroker::GetAppParams()->GetGlInterface(); |
| 276 |
|
| 277 |
if (gli != "glx") |
| 278 |
{ |
| 279 |
m_pGLContext = new CGLContextEGL(m_dpy, EGL_OPENGL_API); |
| 280 |
success = m_pGLContext->Refresh(force, m_screen, m_glWindow, m_newGlContext); |
| 281 |
if (success) |
| 282 |
{ |
| 283 |
if (!isNvidia) |
| 284 |
{ |
| 285 |
m_vaapiProxy.reset(VaapiProxyCreate()); |
| 286 |
VaapiProxyConfig(m_vaapiProxy.get(), GetDisplay(), |
| 287 |
static_cast<CGLContextEGL*>(m_pGLContext)->m_eglDisplay); |
| 288 |
bool general = false; |
| 289 |
bool deepColor = false; |
| 290 |
VAAPIRegisterRenderGL(m_vaapiProxy.get(), general, deepColor); |
| 291 |
if (general) |
| 292 |
{ |
| 293 |
VAAPIRegister(m_vaapiProxy.get(), deepColor); |
| 294 |
return true; |
| 295 |
} |
| 296 |
if (isIntel || gli == "egl") |
| 297 |
return true; |
| 298 |
} |
| 299 |
} |
| 300 |
else if (gli == "egl-pb") |
| 301 |
{ |
| 302 |
success = m_pGLContext->CreatePB(); |
| 303 |
if (success) |
| 304 |
return true; |
| 305 |
} |
| 306 |
} |
| 307 |
|
| 308 |
delete m_pGLContext; |
| 309 |
|
| 310 |
// fallback for vdpau |
| 311 |
m_pGLContext = GLXContextCreate(m_dpy); |
| 312 |
success = m_pGLContext->Refresh(force, m_screen, m_glWindow, m_newGlContext); |
| 313 |
if (success) |
| 314 |
{ |
| 315 |
VDPAURegister(); |
| 316 |
VDPAURegisterRender(); |
| 317 |
} |
| 318 |
return success; |
| 319 |
} |
| 320 |
|
| 321 |
std::unique_ptr<CVideoSync> CWinSystemX11GLContext::GetVideoSync(CVideoReferenceClock* clock) |
| 322 |
{ |
| 323 |
std::unique_ptr<CVideoSync> pVSync; |
| 324 |
|
| 325 |
if (dynamic_cast<CGLContextEGL*>(m_pGLContext)) |
| 326 |
{ |
| 327 |
pVSync = std::make_unique<CVideoSyncOML>(clock, *this); |
| 328 |
} |
| 329 |
else |
| 330 |
{ |
| 331 |
pVSync.reset(GLXVideoSyncCreate(clock, *this)); |
| 332 |
} |
| 333 |
|
| 334 |
return pVSync; |
| 335 |
} |
| 336 |
|
| 337 |
float CWinSystemX11GLContext::GetFrameLatencyAdjustment() |
| 338 |
{ |
| 339 |
if (m_pGLContext) |
| 340 |
{ |
| 341 |
uint64_t msc, interval; |
| 342 |
float micros = m_pGLContext->GetVblankTiming(msc, interval); |
| 343 |
return micros / 1000; |
| 344 |
} |
| 345 |
return 0; |
| 346 |
} |
| 347 |
|
| 348 |
uint64_t CWinSystemX11GLContext::GetVblankTiming(uint64_t &msc, uint64_t &interval) |
| 349 |
{ |
| 350 |
if (m_pGLContext) |
| 351 |
{ |
| 352 |
float micros = m_pGLContext->GetVblankTiming(msc, interval); |
| 353 |
return micros; |
| 354 |
} |
| 355 |
msc = 0; |
| 356 |
interval = 0; |
| 357 |
return 0; |
| 358 |
} |
| 359 |
|
| 360 |
void CWinSystemX11GLContext::delete_CVaapiProxy::operator()(CVaapiProxy *p) const |
| 361 |
{ |
| 362 |
VaapiProxyDelete(p); |
| 363 |
} |