File: GSWndEGL.cpp

package info (click to toggle)
pcsx2 1.6.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 28,464 kB
  • sloc: cpp: 299,796; ansic: 23,973; lisp: 2,689; asm: 908; perl: 852; sh: 789; xml: 116; makefile: 60
file content (409 lines) | stat: -rw-r--r-- 10,385 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
/*
 *	Copyright (C) 2007-2012 Gabest
 *	http://www.gabest.org
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA USA.
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

#include "stdafx.h"
#include "GSWndEGL.h"

#if defined(__unix__) && defined(EGL_SUPPORTED)

// static method
int GSWndEGL::SelectPlatform()
{
	// Check the supported extension
	const char *client_extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
	if (!client_extensions) {
		fprintf(stderr, "EGL: Client extension not supported\n");
		return 0;
	}
	fprintf(stdout, "EGL: Supported extensions: %s\n", client_extensions);

	// Check platform extensions are supported (Note: there are core in 1.5)
	if (!strstr(client_extensions, "EGL_EXT_platform_base")) {
		fprintf(stderr, "EGL: Dynamic platform selection isn't supported\n");
		return 0;
	}

	// Finally we can select the platform
#if GS_EGL_X11
	if (strstr(client_extensions, "EGL_EXT_platform_x11")) {
		fprintf(stdout, "EGL: select X11 platform\n");
		return EGL_PLATFORM_X11_KHR;
	}
#endif
#if GS_EGL_WL
	if (strstr(client_extensions, "EGL_EXT_platform_wayland")) {
		fprintf(stdout, "EGL: select Wayland platform\n");
		return EGL_PLATFORM_WAYLAND_KHR;
	}
#endif

	fprintf(stderr, "EGL: no compatible platform found\n");

	return 0;
}


GSWndEGL::GSWndEGL(int platform)
	: m_native_window(nullptr), m_platform(platform)
{
}

void GSWndEGL::CreateContext(int major, int minor)
{
	EGLConfig eglConfig;
	EGLint numConfigs = 0;
	EGLint contextAttribs[] =
	{
		EGL_CONTEXT_MAJOR_VERSION_KHR, major,
		EGL_CONTEXT_MINOR_VERSION_KHR, minor,
#ifdef ENABLE_OGL_DEBUG
		EGL_CONTEXT_FLAGS_KHR, EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR,
#else
		// Open Source isn't happy with an unsupported flags...
		//EGL_CONTEXT_FLAGS_KHR, GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR,
#endif
		EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR,
		EGL_NONE
	};
	EGLint NullContextAttribs[] = { EGL_NONE };
	EGLint attrList[] = {
		EGL_RED_SIZE, 8,
		EGL_GREEN_SIZE, 8,
		EGL_BLUE_SIZE, 8,
		EGL_DEPTH_SIZE, 0,
		EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
		EGL_NONE
	};

	BindAPI();

	eglChooseConfig(m_eglDisplay, attrList, &eglConfig, 1, &numConfigs);
	if ( numConfigs == 0 )
	{
		fprintf(stderr,"EGL: Failed to get a frame buffer config! (0x%x)\n", eglGetError() );
		throw GSDXRecoverableError();
	}

	m_eglSurface = eglCreatePlatformWindowSurface(m_eglDisplay, eglConfig, m_native_window, nullptr);
	if ( m_eglSurface == EGL_NO_SURFACE )
	{
		fprintf(stderr,"EGL: Failed to get a window surface\n");
		throw GSDXRecoverableError();
	}

	m_eglContext = eglCreateContext(m_eglDisplay, eglConfig, EGL_NO_CONTEXT, contextAttribs);
	EGLint status = eglGetError();
	if (status == EGL_BAD_ATTRIBUTE || status == EGL_BAD_MATCH) {
		// Radeon/Gallium don't support advance attribute. Fallback to random value
		// Note: Intel gives an EGL_BAD_MATCH. I don't know why but let's by stubborn and retry.
		fprintf(stderr, "EGL: warning your driver doesn't support advance openGL context attributes\n");
		m_eglContext = eglCreateContext(m_eglDisplay, eglConfig, EGL_NO_CONTEXT, NullContextAttribs);
		status = eglGetError();
	}
	if ( m_eglContext == EGL_NO_CONTEXT )
	{
		fprintf(stderr,"EGL: Failed to create the context\n");
		fprintf(stderr,"EGL STATUS: %x\n", status);
		throw GSDXRecoverableError();
	}

	if ( !eglMakeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext) )
	{
		throw GSDXRecoverableError();
	}
}

void GSWndEGL::AttachContext()
{
	if (!IsContextAttached()) {
		// The setting of the API is local to a thread. This function
		// can be called from 2 threads.
		BindAPI();

		//fprintf(stderr, "Attach the context\n");
		eglMakeCurrent(m_eglDisplay, m_eglSurface, m_eglSurface, m_eglContext);
		m_ctx_attached = true;
	}
}

void GSWndEGL::DetachContext()
{
	if (IsContextAttached()) {
		//fprintf(stderr, "Detach the context\n");
		eglMakeCurrent(m_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT);
		m_ctx_attached = false;
	}
}

void GSWndEGL::PopulateWndGlFunction()
{
}

void GSWndEGL::BindAPI()
{
	eglBindAPI(EGL_OPENGL_API);
	EGLenum api = eglQueryAPI();
	if (api != EGL_OPENGL_API) {
		fprintf(stderr,"EGL: Failed to bind the OpenGL API got 0x%x instead\n", api);
		throw GSDXRecoverableError();
	}
}

bool GSWndEGL::Attach(void* handle, bool managed)
{
	m_managed = managed;

	m_native_window = AttachNativeWindow(handle);

	OpenEGLDisplay();

	FullContextInit();

	return true;
}

void GSWndEGL::Detach()
{
	// Actually the destructor is not called when there is only a GSclose/GSshutdown
	// The window still need to be closed
	DetachContext();
	eglDestroyContext(m_eglDisplay, m_eglContext);
	m_eglContext = nullptr;

	eglDestroySurface(m_eglDisplay, m_eglSurface);
	m_eglSurface = nullptr;

	CloseEGLDisplay();

	DestroyNativeResources();
}

bool GSWndEGL::Create(const std::string& title, int w, int h)
{
	if(w <= 0 || h <= 0) {
		w = theApp.GetConfigI("ModeWidth");
		h = theApp.GetConfigI("ModeHeight");
	}

	m_managed = true;

	OpenEGLDisplay();

	m_native_window = CreateNativeWindow(w, h);

	FullContextInit();

	return true;
}

void* GSWndEGL::GetProcAddress(const char* name, bool opt)
{
	void* ptr = (void*)eglGetProcAddress(name);
	if (ptr == nullptr) {
		if (theApp.GetConfigB("debug_opengl"))
			fprintf(stderr, "Failed to find %s\n", name);

		if (!opt)
			throw GSDXRecoverableError();
	}
	return ptr;
}

GSVector4i GSWndEGL::GetClientRect()
{
	EGLint h = 0;
	EGLint w = 0;
	eglQuerySurface(m_eglDisplay, m_eglSurface, EGL_HEIGHT, &h);
	eglQuerySurface(m_eglDisplay, m_eglSurface, EGL_WIDTH, &w);

	return GSVector4i(0, 0, w, h);
}

void GSWndEGL::SetSwapInterval()
{
	// 0 -> disable vsync
	// n -> wait n frame
	eglSwapInterval(m_eglDisplay, m_vsync);
}

void GSWndEGL::Flip()
{
	if (m_vsync_change_requested.exchange(false))
		SetSwapInterval();

	eglSwapBuffers(m_eglDisplay, m_eglSurface);
}

void GSWndEGL::CloseEGLDisplay()
{
	eglReleaseThread();
	eglTerminate(m_eglDisplay);
}

void GSWndEGL::OpenEGLDisplay()
{
	// We only need a native display when we manage the window ourself.
	// By default, EGL will create its own native display. This way the driver knows
	// that display will be thread safe and so it can enable multithread optimization.
	void *native_display = (m_managed) ? CreateNativeDisplay() : nullptr;

	// Create an EGL display from the native display
	m_eglDisplay = eglGetPlatformDisplay(m_platform, native_display, nullptr);
	if (m_eglDisplay == EGL_NO_DISPLAY) {
		fprintf(stderr,"EGL: Failed to open a display! (0x%x)\n", eglGetError() );
		throw GSDXRecoverableError();
	}

	if (!eglInitialize(m_eglDisplay, nullptr, nullptr)) {
		fprintf(stderr,"EGL: Failed to initialize the display! (0x%x)\n", eglGetError() );
		throw GSDXRecoverableError();
	}
}

//////////////////////////////////////////////////////////////////////
// X11 platform
//////////////////////////////////////////////////////////////////////
#if GS_EGL_X11

GSWndEGL_X11::GSWndEGL_X11()
	: GSWndEGL(EGL_PLATFORM_X11_KHR), m_NativeDisplay(nullptr), m_NativeWindow(0)
{
}

void *GSWndEGL_X11::CreateNativeDisplay()
{
	if (m_NativeDisplay == nullptr)
		m_NativeDisplay = XOpenDisplay(nullptr);

	return (void*)m_NativeDisplay;
}

void *GSWndEGL_X11::CreateNativeWindow(int w, int h)
{
	const int depth = 0, x = 0, y = 0, border_width = 1;
#if 0
	// Old X code in case future code will still require it
	m_NativeWindow = XCreateSimpleWindow(m_NativeDisplay, DefaultRootWindow(m_NativeDisplay), 0, 0, w, h, 0, 0, 0);
	XMapWindow (m_NativeDisplay, m_NativeWindow);
#endif

	if (m_NativeDisplay == nullptr) {
		fprintf(stderr, "EGL X11: display wasn't created before the window\n");
		throw GSDXRecoverableError();
	}

	xcb_connection_t *c = XGetXCBConnection(m_NativeDisplay);

	const xcb_setup_t *setup = xcb_get_setup(c);

	xcb_screen_t *screen = (xcb_setup_roots_iterator (setup)).data;

	m_NativeWindow = xcb_generate_id(c);

	if (m_NativeWindow == 0) {
		fprintf(stderr, "EGL X11: failed to create the native window\n");
		throw GSDXRecoverableError();
	}

	xcb_create_window (c, depth, m_NativeWindow, screen->root, x, y, w, h,
			border_width, InputOutput, screen->root_visual, 0, nullptr);

	xcb_map_window (c, m_NativeWindow);

	xcb_flush(c);

	return (void*)&m_NativeWindow;
}

void *GSWndEGL_X11::AttachNativeWindow(void *handle)
{
	m_NativeWindow = *(Window*)handle;
	return handle;
}

void GSWndEGL_X11::DestroyNativeResources()
{
	if (m_NativeDisplay) {
		XCloseDisplay(m_NativeDisplay);
		m_NativeDisplay = nullptr;
	}
}

bool GSWndEGL_X11::SetWindowText(const char* title)
{
	if (!m_managed) return true;

	xcb_connection_t *c = XGetXCBConnection(m_NativeDisplay);

	xcb_change_property(c, XCB_PROP_MODE_REPLACE, m_NativeWindow,
			XCB_ATOM_WM_NAME, XCB_ATOM_STRING, 8,
			strlen (title), title);

	return true;
}

#endif

//////////////////////////////////////////////////////////////////////
// Wayland platform (just a place holder)
//////////////////////////////////////////////////////////////////////
#if GS_EGL_WL

GSWndEGL_WL::GSWndEGL_WL()
	: GSWndEGL(EGL_PLATFORM_WAYLAND_KHR), m_NativeDisplay(nullptr), m_NativeWindow(nullptr)
{
}

void *GSWndEGL_WL::CreateNativeDisplay()
{
	if (m_NativeDisplay == nullptr)
		m_NativeDisplay = wl_display_connect(NULL);

	return (void*)m_NativeDisplay;
}

void *GSWndEGL_WL::CreateNativeWindow(int w, int h)
{
	return nullptr;
}

void *GSWndEGL_WL::AttachNativeWindow(void *handle)
{
	m_NativeWindow = (wl_egl_window*)handle;
	return handle;
}

void GSWndEGL_WL::DestroyNativeResources()
{
	if (m_NativeDisplay) {
		wl_display_disconnect(m_NativeDisplay);
		m_NativeDisplay = nullptr;
	}
}

bool GSWndEGL_WL::SetWindowText(const char* title)
{
	return true;
}

#endif

#endif