| 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 "WinSystemGbmGLContext.h" |
| 10 |
|
| 11 |
#include "OptionalsReg.h" |
| 12 |
#include "cores/RetroPlayer/process/gbm/RPProcessInfoGbm.h" |
| 13 |
#include "cores/RetroPlayer/rendering/VideoRenderers/RPRendererDMA.h" |
| 14 |
#include "cores/RetroPlayer/rendering/VideoRenderers/RPRendererOpenGL.h" |
| 15 |
#include "cores/VideoPlayer/DVDCodecs/DVDFactoryCodec.h" |
| 16 |
#include "cores/VideoPlayer/VideoRenderers/LinuxRendererGL.h" |
| 17 |
#include "cores/VideoPlayer/VideoRenderers/RenderFactory.h" |
| 18 |
#include "rendering/gl/ScreenshotSurfaceGL.h" |
| 19 |
#include "utils/BufferObjectFactory.h" |
| 20 |
#include "utils/DMAHeapBufferObject.h" |
| 21 |
#include "utils/DumbBufferObject.h" |
| 22 |
#include "utils/GBMBufferObject.h" |
| 23 |
#include "utils/UDMABufferObject.h" |
| 24 |
#include "utils/XTimeUtils.h" |
| 25 |
#include "utils/log.h" |
| 26 |
#include "windowing/WindowSystemFactory.h" |
| 27 |
|
| 28 |
#include <mutex> |
| 29 |
|
| 30 |
#include <EGL/eglext.h> |
| 31 |
|
| 32 |
using namespace KODI::WINDOWING::GBM; |
| 33 |
|
| 34 |
using namespace std::chrono_literals; |
| 35 |
|
| 36 |
CWinSystemGbmGLContext::CWinSystemGbmGLContext() |
| 37 |
: CWinSystemGbmEGLContext(EGL_PLATFORM_GBM_MESA, "EGL_MESA_platform_gbm") |
| 38 |
{} |
| 39 |
|
| 40 |
void CWinSystemGbmGLContext::Register() |
| 41 |
{ |
| 42 |
CWindowSystemFactory::RegisterWindowSystem(CreateWinSystem, "gbm"); |
| 43 |
} |
| 44 |
|
| 45 |
std::unique_ptr<CWinSystemBase> CWinSystemGbmGLContext::CreateWinSystem() |
| 46 |
{ |
| 47 |
return std::make_unique<CWinSystemGbmGLContext>(); |
| 48 |
} |
| 49 |
|
| 50 |
bool CWinSystemGbmGLContext::InitWindowSystem() |
| 51 |
{ |
| 52 |
VIDEOPLAYER::CRendererFactory::ClearRenderer(); |
| 53 |
CDVDFactoryCodec::ClearHWAccels(); |
| 54 |
CLinuxRendererGL::Register(); |
| 55 |
RETRO::CRPProcessInfoGbm::Register(); |
| 56 |
RETRO::CRPProcessInfoGbm::RegisterRendererFactory(new RETRO::CRendererFactoryDMA); |
| 57 |
RETRO::CRPProcessInfoGbm::RegisterRendererFactory(new RETRO::CRendererFactoryOpenGL); |
| 58 |
|
| 59 |
if (!CWinSystemGbmEGLContext::InitWindowSystemEGL(EGL_OPENGL_BIT, EGL_OPENGL_API)) |
| 60 |
{ |
| 61 |
return false; |
| 62 |
} |
| 63 |
|
| 64 |
bool general, deepColor; |
| 65 |
m_vaapiProxy.reset(VaapiProxyCreate(m_DRM->GetRenderNodeFileDescriptor())); |
| 66 |
VaapiProxyConfig(m_vaapiProxy.get(), m_eglContext.GetEGLDisplay()); |
| 67 |
VAAPIRegisterRenderGL(m_vaapiProxy.get(), general, deepColor); |
| 68 |
|
| 69 |
if (general) |
| 70 |
{ |
| 71 |
VAAPIRegister(m_vaapiProxy.get(), deepColor); |
| 72 |
} |
| 73 |
|
| 74 |
CScreenshotSurfaceGL::Register(); |
| 75 |
|
| 76 |
CBufferObjectFactory::ClearBufferObjects(); |
| 77 |
CDumbBufferObject::Register(); |
| 78 |
#if defined(HAS_GBM_BO_MAP) |
| 79 |
CGBMBufferObject::Register(); |
| 80 |
#endif |
| 81 |
#if defined(HAVE_LINUX_MEMFD) && defined(HAVE_LINUX_UDMABUF) |
| 82 |
CUDMABufferObject::Register(); |
| 83 |
#endif |
| 84 |
#if defined(HAVE_LINUX_DMA_HEAP) |
| 85 |
CDMAHeapBufferObject::Register(); |
| 86 |
#endif |
| 87 |
|
| 88 |
return true; |
| 89 |
} |
| 90 |
|
| 91 |
bool CWinSystemGbmGLContext::SetFullScreen(bool fullScreen, RESOLUTION_INFO& res, bool blankOtherDisplays) |
| 92 |
{ |
| 93 |
if (res.iWidth != m_nWidth || |
| 94 |
res.iHeight != m_nHeight) |
| 95 |
{ |
| 96 |
CLog::Log(LOGDEBUG, "CWinSystemGbmGLContext::{} - resolution changed, creating a new window", |
| 97 |
__FUNCTION__); |
| 98 |
CreateNewWindow("", fullScreen, res); |
| 99 |
} |
| 100 |
|
| 101 |
if (!m_eglContext.TrySwapBuffers()) |
| 102 |
{ |
| 103 |
CEGLUtils::Log(LOGERROR, "eglSwapBuffers failed"); |
| 104 |
throw std::runtime_error("eglSwapBuffers failed"); |
| 105 |
} |
| 106 |
|
| 107 |
CWinSystemGbm::SetFullScreen(fullScreen, res, blankOtherDisplays); |
| 108 |
CRenderSystemGL::ResetRenderSystem(res.iWidth, res.iHeight); |
| 109 |
|
| 110 |
return true; |
| 111 |
} |
| 112 |
|
| 113 |
void CWinSystemGbmGLContext::PresentRender(bool rendered, bool videoLayer) |
| 114 |
{ |
| 115 |
if (!m_bRenderCreated) |
| 116 |
return; |
| 117 |
|
| 118 |
if (rendered || videoLayer) |
| 119 |
{ |
| 120 |
if (rendered) |
| 121 |
{ |
| 122 |
#if defined(EGL_ANDROID_native_fence_sync) && defined(EGL_KHR_fence_sync) |
| 123 |
if (m_eglFence) |
| 124 |
{ |
| 125 |
int fd = m_DRM->TakeOutFenceFd(); |
| 126 |
if (fd != -1) |
| 127 |
{ |
| 128 |
m_eglFence->CreateKMSFence(fd); |
| 129 |
m_eglFence->WaitSyncGPU(); |
| 130 |
} |
| 131 |
|
| 132 |
m_eglFence->CreateGPUFence(); |
| 133 |
} |
| 134 |
#endif |
| 135 |
|
| 136 |
if (!m_eglContext.TrySwapBuffers()) |
| 137 |
{ |
| 138 |
CEGLUtils::Log(LOGERROR, "eglSwapBuffers failed"); |
| 139 |
throw std::runtime_error("eglSwapBuffers failed"); |
| 140 |
} |
| 141 |
|
| 142 |
#if defined(EGL_ANDROID_native_fence_sync) && defined(EGL_KHR_fence_sync) |
| 143 |
if (m_eglFence) |
| 144 |
{ |
| 145 |
int fd = m_eglFence->FlushFence(); |
| 146 |
m_DRM->SetInFenceFd(fd); |
| 147 |
|
| 148 |
m_eglFence->WaitSyncCPU(); |
| 149 |
} |
| 150 |
#endif |
| 151 |
} |
| 152 |
CWinSystemGbm::FlipPage(rendered, videoLayer, static_cast<bool>(m_eglFence)); |
| 153 |
|
| 154 |
if (m_dispReset && m_dispResetTimer.IsTimePast()) |
| 155 |
{ |
| 156 |
CLog::Log(LOGDEBUG, "CWinSystemGbmGLContext::{} - Sending display reset to all clients", |
| 157 |
__FUNCTION__); |
| 158 |
m_dispReset = false; |
| 159 |
std::unique_lock<CCriticalSection> lock(m_resourceSection); |
| 160 |
|
| 161 |
for (auto resource : m_resources) |
| 162 |
resource->OnResetDisplay(); |
| 163 |
} |
| 164 |
} |
| 165 |
else |
| 166 |
{ |
| 167 |
KODI::TIME::Sleep(10ms); |
| 168 |
} |
| 169 |
} |
| 170 |
|
| 171 |
bool CWinSystemGbmGLContext::CreateContext() |
| 172 |
{ |
| 173 |
const EGLint glMajor = 3; |
| 174 |
const EGLint glMinor = 2; |
| 175 |
|
| 176 |
CEGLAttributesVec contextAttribs; |
| 177 |
contextAttribs.Add({{EGL_CONTEXT_MAJOR_VERSION_KHR, glMajor}, |
| 178 |
{EGL_CONTEXT_MINOR_VERSION_KHR, glMinor}, |
| 179 |
{EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR}}); |
| 180 |
|
| 181 |
if (!m_eglContext.CreateContext(contextAttribs)) |
| 182 |
{ |
| 183 |
CEGLAttributesVec fallbackContextAttribs; |
| 184 |
fallbackContextAttribs.Add({{EGL_CONTEXT_CLIENT_VERSION, 2}}); |
| 185 |
|
| 186 |
if (!m_eglContext.CreateContext(fallbackContextAttribs)) |
| 187 |
{ |
| 188 |
CLog::Log(LOGERROR, "EGL context creation failed"); |
| 189 |
return false; |
| 190 |
} |
| 191 |
else |
| 192 |
{ |
| 193 |
CLog::Log(LOGWARNING, "Your OpenGL drivers do not support OpenGL {}.{} core profile. Kodi will run in compatibility mode, but performance may suffer.", glMajor, glMinor); |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
return true; |
| 198 |
} |