Debian Codegraph @

Source/Core/Common/GL/GLInterface/EGL.cpp

raw symbols SCIP
1 // Copyright 2012 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "Common/GL/GLInterface/EGL.h"
5
6 #include <array>
7 #include <sstream>
8 #include <vector>
9
10 #include "Common/Logging/Log.h"
11
12 #ifndef EGL_KHR_create_context
13 #define EGL_KHR_create_context 1
14 #define EGL_CONTEXT_MAJOR_VERSION_KHR 0x3098
15 #define EGL_CONTEXT_MINOR_VERSION_KHR 0x30FB
16 #define EGL_CONTEXT_FLAGS_KHR 0x30FC
17 #define EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR 0x30FD
18 #define EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR 0x31BD
19 #define EGL_NO_RESET_NOTIFICATION_KHR 0x31BE
20 #define EGL_LOSE_CONTEXT_ON_RESET_KHR 0x31BF
21 #define EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR 0x00000001
22 #define EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR 0x00000002
23 #define EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR 0x00000004
24 #define EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR 0x00000001
25 #define EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR 0x00000002
26 #define EGL_OPENGL_ES3_BIT_KHR 0x00000040
27 #endif /* EGL_KHR_create_context */
28
29 GLContextEGL::~GLContextEGL()
30 {
31 DestroyWindowSurface();
32 DestroyContext();
33 }
34
35 bool GLContextEGL::IsHeadless() const
36 {
37 return m_wsi.type == WindowSystemType::Headless;
38 }
39
40 void GLContextEGL::Swap()
41 {
42 if (m_egl_surface != EGL_NO_SURFACE)
43 eglSwapBuffers(m_egl_display, m_egl_surface);
44 }
45 void GLContextEGL::SwapInterval(int interval)
46 {
47 eglSwapInterval(m_egl_display, interval);
48 }
49
50 void* GLContextEGL::GetFuncAddress(const std::string& name)
51 {
52 return (void*)eglGetProcAddress(name.c_str());
53 }
54
55 void GLContextEGL::DetectMode()
56 {
57 EGLint num_configs;
58 bool supportsGL = false, supportsGLES3 = false;
59 std::array<int, 3> renderable_types{{EGL_OPENGL_BIT, EGL_OPENGL_ES3_BIT_KHR}};
60
61 for (auto renderable_type : renderable_types)
62 {
63 // attributes for a visual in RGBA format with at least
64 // 8 bits per color
65 int attribs[] = {EGL_RED_SIZE,
66 8,
67 EGL_GREEN_SIZE,
68 8,
69 EGL_BLUE_SIZE,
70 8,
71 EGL_RENDERABLE_TYPE,
72 renderable_type,
73 EGL_SURFACE_TYPE,
74 IsHeadless() ? 0 : EGL_WINDOW_BIT,
75 EGL_NONE};
76
77 // Get how many configs there are
78 if (!eglChooseConfig(m_egl_display, attribs, nullptr, 0, &num_configs))
79 {
80 INFO_LOG_FMT(VIDEO, "Error: couldn't get an EGL visual config");
81 continue;
82 }
83
84 EGLConfig* config = new EGLConfig[num_configs];
85
86 // Get all the configurations
87 if (!eglChooseConfig(m_egl_display, attribs, config, num_configs, &num_configs))
88 {
89 INFO_LOG_FMT(VIDEO, "Error: couldn't get an EGL visual config");
90 delete[] config;
91 continue;
92 }
93
94 for (int i = 0; i < num_configs; ++i)
95 {
96 EGLint attribVal;
97 bool ret;
98 ret = eglGetConfigAttrib(m_egl_display, config[i], EGL_RENDERABLE_TYPE, &attribVal);
99 if (ret)
100 {
101 if (attribVal & EGL_OPENGL_BIT)
102 supportsGL = true;
103 if (attribVal & EGL_OPENGL_ES3_BIT_KHR)
104 supportsGLES3 = true;
105 }
106 }
107 delete[] config;
108 }
109
110 if (supportsGL)
111 {
112 INFO_LOG_FMT(VIDEO, "Using OpenGL");
113 m_opengl_mode = Mode::OpenGL;
114 }
115 else if (supportsGLES3)
116 {
117 INFO_LOG_FMT(VIDEO, "Using OpenGL|ES");
118 m_opengl_mode = Mode::OpenGLES;
119 }
120 else
121 {
122 // Errored before we found a mode
123 ERROR_LOG_FMT(VIDEO, "Error: Failed to detect OpenGL flavour, falling back to OpenGL");
124 // This will fail to create a context, as it'll try to use the same attribs we just failed to
125 // find a matching config with
126 m_opengl_mode = Mode::OpenGL;
127 }
128 }
129
130 EGLDisplay GLContextEGL::OpenEGLDisplay()
131 {
132 return eglGetDisplay(static_cast<EGLNativeDisplayType>(m_wsi.render_surface));
133 }
134
135 EGLNativeWindowType GLContextEGL::GetEGLNativeWindow(EGLConfig config)
136 {
137 return reinterpret_cast<EGLNativeWindowType>(m_wsi.display_connection);
138 }
139
140 // Create rendering window.
141 // Call browser: Core.cpp:EmuThread() > main.cpp:Video_Initialize()
142 bool GLContextEGL::Initialize(const WindowSystemInfo& wsi, bool stereo, bool core)
143 {
144 EGLint egl_major, egl_minor;
145 bool supports_core_profile = false;
146
147 m_wsi = wsi;
148 m_egl_display = OpenEGLDisplay();
149 if (!m_egl_display)
150 {
151 INFO_LOG_FMT(VIDEO, "Error: eglGetDisplay() failed");
152 return false;
153 }
154
155 if (!eglInitialize(m_egl_display, &egl_major, &egl_minor))
156 {
157 INFO_LOG_FMT(VIDEO, "Error: eglInitialize() failed");
158 return false;
159 }
160
161 /* Detection code */
162 EGLint num_configs;
163
164 if (m_opengl_mode == Mode::Detect)
165 DetectMode();
166
167 // attributes for a visual in RGBA format with at least
168 // 8 bits per color
169 int attribs[] = {EGL_RENDERABLE_TYPE,
170 0,
171 EGL_RED_SIZE,
172 8,
173 EGL_GREEN_SIZE,
174 8,
175 EGL_BLUE_SIZE,
176 8,
177 EGL_SURFACE_TYPE,
178 IsHeadless() ? 0 : EGL_WINDOW_BIT,
179 EGL_NONE};
180
181 std::vector<EGLint> ctx_attribs;
182 switch (m_opengl_mode)
183 {
184 case Mode::OpenGL:
185 attribs[1] = EGL_OPENGL_BIT;
186 ctx_attribs = {EGL_NONE};
187 break;
188 case Mode::OpenGLES:
189 attribs[1] = EGL_OPENGL_ES3_BIT_KHR;
190 ctx_attribs = {EGL_CONTEXT_CLIENT_VERSION, 3, EGL_NONE};
191 break;
192 default:
193 ERROR_LOG_FMT(VIDEO, "Unknown opengl mode set");
194 return false;
195 }
196
197 if (!eglChooseConfig(m_egl_display, attribs, &m_config, 1, &num_configs))
198 {
199 INFO_LOG_FMT(VIDEO, "Error: couldn't get an EGL visual config");
200 return false;
201 }
202
203 if (m_opengl_mode == Mode::OpenGL)
204 eglBindAPI(EGL_OPENGL_API);
205 else
206 eglBindAPI(EGL_OPENGL_ES_API);
207
208 std::string tmp;
209 std::istringstream buffer(eglQueryString(m_egl_display, EGL_EXTENSIONS));
210 while (buffer >> tmp)
211 {
212 if (tmp == "EGL_KHR_surfaceless_context")
213 m_supports_surfaceless = true;
214 else if (tmp == "EGL_KHR_create_context")
215 supports_core_profile = true;
216 }
217
218 if (supports_core_profile && core && m_opengl_mode == Mode::OpenGL)
219 {
220 for (const auto& version : s_desktop_opengl_versions)
221 {
222 std::vector<EGLint> core_attribs = {EGL_CONTEXT_MAJOR_VERSION_KHR,
223 version.first,
224 EGL_CONTEXT_MINOR_VERSION_KHR,
225 version.second,
226 EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR,
227 EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR,
228 EGL_CONTEXT_FLAGS_KHR,
229 EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR,
230 EGL_NONE};
231
232 m_egl_context = eglCreateContext(m_egl_display, m_config, EGL_NO_CONTEXT, &core_attribs[0]);
233 if (m_egl_context)
234 {
235 m_attribs = std::move(core_attribs);
236 break;
237 }
238 }
239 }
240
241 if (!m_egl_context)
242 {
243 m_egl_context = eglCreateContext(m_egl_display, m_config, EGL_NO_CONTEXT, &ctx_attribs[0]);
244 m_attribs = std::move(ctx_attribs);
245 }
246
247 if (!m_egl_context)
248 {
249 INFO_LOG_FMT(VIDEO, "Error: eglCreateContext failed");
250 return false;
251 }
252
253 if (!CreateWindowSurface())
254 {
255 ERROR_LOG_FMT(VIDEO, "Error: CreateWindowSurface failed {:#06x}", eglGetError());
256 return false;
257 }
258
259 return MakeCurrent();
260 }
261
262 std::unique_ptr<GLContext> GLContextEGL::CreateSharedContext()
263 {
264 eglBindAPI(m_opengl_mode == Mode::OpenGL ? EGL_OPENGL_API : EGL_OPENGL_ES_API);
265 EGLContext new_egl_context =
266 eglCreateContext(m_egl_display, m_config, m_egl_context, m_attribs.data());
267 if (!new_egl_context)
268 {
269 INFO_LOG_FMT(VIDEO, "Error: eglCreateContext failed {:#06x}", eglGetError());
270 return nullptr;
271 }
272
273 std::unique_ptr<GLContextEGL> new_context = std::make_unique<GLContextEGL>();
274 new_context->m_opengl_mode = m_opengl_mode;
275 new_context->m_egl_context = new_egl_context;
276 new_context->m_wsi.display_connection = m_wsi.display_connection;
277 new_context->m_egl_display = m_egl_display;
278 new_context->m_config = m_config;
279 new_context->m_supports_surfaceless = m_supports_surfaceless;
280 new_context->m_is_shared = true;
281 if (!new_context->CreateWindowSurface())
282 {
283 ERROR_LOG_FMT(VIDEO, "Error: CreateWindowSurface failed {:#06x}", eglGetError());
284 return nullptr;
285 }
286
287 return new_context;
288 }
289
290 bool GLContextEGL::CreateWindowSurface()
291 {
292 if (!IsHeadless())
293 {
294 EGLNativeWindowType native_window = GetEGLNativeWindow(m_config);
295 m_egl_surface = eglCreateWindowSurface(m_egl_display, m_config, native_window, nullptr);
296 if (!m_egl_surface)
297 {
298 INFO_LOG_FMT(VIDEO, "Error: eglCreateWindowSurface failed");
299 return false;
300 }
301
302 // Get dimensions from the surface.
303 EGLint surface_width = 1, surface_height = 1;
304 if (!eglQuerySurface(m_egl_display, m_egl_surface, EGL_WIDTH, &surface_width) ||
305 !eglQuerySurface(m_egl_display, m_egl_surface, EGL_HEIGHT, &surface_height))
306 {
307 WARN_LOG_FMT(VIDEO,
308 "Failed to get surface dimensions via eglQuerySurface. Size may be incorrect.");
309 }
310 m_backbuffer_width = static_cast<int>(surface_width);
311 m_backbuffer_height = static_cast<int>(surface_height);
312 }
313 else if (!m_supports_surfaceless)
314 {
315 EGLint attrib_list[] = {
316 EGL_NONE,
317 };
318 m_egl_surface = eglCreatePbufferSurface(m_egl_display, m_config, attrib_list);
319 if (!m_egl_surface)
320 {
321 INFO_LOG_FMT(VIDEO, "Error: eglCreatePbufferSurface failed");
322 return false;
323 }
324 }
325 else
326 {
327 m_egl_surface = EGL_NO_SURFACE;
328 }
329 return true;
330 }
331
332 void GLContextEGL::DestroyWindowSurface()
333 {
334 if (m_egl_surface == EGL_NO_SURFACE)
335 return;
336
337 if (eglGetCurrentSurface(EGL_DRAW) == m_egl_surface)
338 eglMakeCurrent(m_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
339 if (!eglDestroySurface(m_egl_display, m_egl_surface))
340 NOTICE_LOG_FMT(VIDEO, "Could not destroy window surface.");
341 m_egl_surface = EGL_NO_SURFACE;
342 }
343
344 bool GLContextEGL::MakeCurrent()
345 {
346 return eglMakeCurrent(m_egl_display, m_egl_surface, m_egl_surface, m_egl_context);
347 }
348
349 void GLContextEGL::UpdateSurface(void* window_handle)
350 {
351 m_wsi.render_surface = window_handle;
352 ClearCurrent();
353 DestroyWindowSurface();
354 CreateWindowSurface();
355 MakeCurrent();
356 }
357
358 bool GLContextEGL::ClearCurrent()
359 {
360 return eglMakeCurrent(m_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
361 }
362
363 // Close backend
364 void GLContextEGL::DestroyContext()
365 {
366 if (!m_egl_context)
367 return;
368
369 if (eglGetCurrentContext() == m_egl_context)
370 eglMakeCurrent(m_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
371 if (!eglDestroyContext(m_egl_display, m_egl_context))
372 NOTICE_LOG_FMT(VIDEO, "Could not destroy drawing context.");
373 if (!m_is_shared && !eglTerminate(m_egl_display))
374 NOTICE_LOG_FMT(VIDEO, "Could not destroy display connection.");
375 m_egl_context = EGL_NO_CONTEXT;
376 m_egl_display = EGL_NO_DISPLAY;
377 }