Debian Codegraph @

xbmc/windowing/wayland/WinSystemWaylandEGLContext.cpp

raw
1 /*
2 * Copyright (C) 2017-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 "WinSystemWaylandEGLContext.h"
10
11 #include "Connection.h"
12 #include "cores/VideoPlayer/DVDCodecs/DVDFactoryCodec.h"
13 #include "cores/VideoPlayer/VideoRenderers/RenderFactory.h"
14 #include "utils/log.h"
15 #include "windowing/GraphicContext.h"
16
17 #include <EGL/eglext.h>
18
19 using namespace KODI::WINDOWING::WAYLAND;
20 using namespace KODI::WINDOWING::LINUX;
21
22 CWinSystemWaylandEGLContext::CWinSystemWaylandEGLContext()
23 : CWinSystemEGL{EGL_PLATFORM_WAYLAND_EXT, "EGL_EXT_platform_wayland"}
24 {}
25
26 bool CWinSystemWaylandEGLContext::InitWindowSystemEGL(EGLint renderableType, EGLint apiType)
27 {
28 VIDEOPLAYER::CRendererFactory::ClearRenderer();
29 CDVDFactoryCodec::ClearHWAccels();
30 if (!CWinSystemWaylandImpl::InitWindowSystem())
31 {
32 return false;
33 }
34
35 if (!m_eglContext.CreatePlatformDisplay(GetConnection()->GetDisplay(), GetConnection()->GetDisplay()))
36 {
37 return false;
38 }
39
40 if (!m_eglContext.InitializeDisplay(apiType))
41 {
42 return false;
43 }
44
45 if (!m_eglContext.ChooseConfig(renderableType))
46 {
47 return false;
48 }
49
50 return true;
51 }
52
53 bool CWinSystemWaylandEGLContext::CreateNewWindow(const std::string& name,
54 bool fullScreen,
55 RESOLUTION_INFO& res)
56 {
57 if (!CWinSystemWaylandImpl::CreateNewWindow(name, fullScreen, res))
58 {
59 return false;
60 }
61
62 if (!CreateContext())
63 {
64 return false;
65 }
66
67 m_nativeWindow = wayland::egl_window_t{GetMainSurface(), GetBufferSize().Width(), GetBufferSize().Height()};
68
69 // CWinSystemWayland::CreateNewWindow sets internal m_bufferSize
70 // to the resolution that should be used for the initial surface size
71 // - the compositor might want something other than the resolution given
72 if (!m_eglContext.CreatePlatformSurface(
73 m_nativeWindow.c_ptr(), reinterpret_cast<khronos_uintptr_t>(m_nativeWindow.c_ptr())))
74 {
75 return false;
76 }
77
78 if (!m_eglContext.BindContext())
79 {
80 return false;
81 }
82
83 // Never enable the vsync of the EGL implementation, we handle that ourselves
84 // in WinSystemWayland
85 m_eglContext.SetVSync(false);
86
87 return true;
88 }
89
90 bool CWinSystemWaylandEGLContext::DestroyWindow()
91 {
92 m_eglContext.DestroySurface();
93 m_nativeWindow = {};
94
95 return CWinSystemWaylandImpl::DestroyWindow();
96 }
97
98 bool CWinSystemWaylandEGLContext::DestroyWindowSystem()
99 {
100 m_eglContext.Destroy();
101
102 return CWinSystemWaylandImpl::DestroyWindowSystem();
103 }
104
105 CSizeInt CWinSystemWaylandEGLContext::GetNativeWindowAttachedSize()
106 {
107 int width, height;
108 m_nativeWindow.get_attached_size(width, height);
109 return {width, height};
110 }
111
112 void CWinSystemWaylandEGLContext::SetContextSize(CSizeInt size)
113 {
114 // Change EGL surface size if necessary
115 if (GetNativeWindowAttachedSize() != size)
116 {
117 CLog::LogF(LOGDEBUG, "Updating egl_window size to {}x{}", size.Width(), size.Height());
118 m_nativeWindow.resize(size.Width(), size.Height(), 0, 0);
119 }
120 }
121
122 void CWinSystemWaylandEGLContext::PresentFrame(bool rendered)
123 {
124 PrepareFramePresentation();
125
126 if (rendered)
127 {
128 if (!m_eglContext.TrySwapBuffers())
129 {
130 // For now we just hard fail if this fails
131 // Theoretically, EGL_CONTEXT_LOST could be handled, but it needs to be checked
132 // whether egl implementations actually use it (mesa does not)
133 CEGLUtils::Log(LOGERROR, "eglSwapBuffers failed");
134 throw std::runtime_error("eglSwapBuffers failed");
135 }
136 // eglSwapBuffers() (hopefully) calls commit on the surface and flushes
137 // ... well mesa does anyway
138 }
139 else
140 {
141 // For presentation feedback: Get notification of the next vblank even
142 // when contents did not change
143 GetMainSurface().commit();
144 // Make sure it reaches the compositor
145 GetConnection()->GetDisplay().flush();
146 }
147
148 FinishFramePresentation();
149 }