2013-01-04 11:41:51 +00:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2013-01-10 13:13:59 +00:00
|
|
|
#include "stdafx.h"
|
2013-01-04 11:41:51 +00:00
|
|
|
#include "GSWndOGL.h"
|
|
|
|
|
2016-05-01 09:43:18 +00:00
|
|
|
#if defined(__unix__)
|
2013-01-04 11:41:51 +00:00
|
|
|
GSWndOGL::GSWndOGL()
|
2017-07-03 20:36:31 +00:00
|
|
|
: m_NativeWindow(0), m_NativeDisplay(nullptr), m_context(0), m_has_late_vsync(false), m_swapinterval_ext(nullptr), m_swapinterval_mesa(nullptr)
|
2013-01-04 11:41:51 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2013-05-18 16:09:09 +00:00
|
|
|
static bool ctxError = false;
|
|
|
|
static int ctxErrorHandler(Display *dpy, XErrorEvent *ev)
|
|
|
|
{
|
|
|
|
ctxError = true;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-06-16 09:40:38 +00:00
|
|
|
void GSWndOGL::CreateContext(int major, int minor)
|
2013-01-04 11:41:51 +00:00
|
|
|
{
|
2013-01-04 21:33:24 +00:00
|
|
|
if ( !m_NativeDisplay || !m_NativeWindow )
|
2013-01-04 11:41:51 +00:00
|
|
|
{
|
|
|
|
fprintf( stderr, "Wrong X11 display/window\n" );
|
2013-06-16 09:40:38 +00:00
|
|
|
throw GSDXRecoverableError();
|
2013-01-04 11:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get visual information
|
|
|
|
static int attrListDbl[] =
|
|
|
|
{
|
|
|
|
// GLX_X_RENDERABLE: If True is specified, then only frame buffer configurations that have associated X
|
|
|
|
// visuals (and can be used to render to Windows and/or GLX pixmaps) will be considered. The default value is GLX_DONT_CARE.
|
|
|
|
GLX_X_RENDERABLE , True,
|
|
|
|
GLX_RED_SIZE , 8,
|
|
|
|
GLX_GREEN_SIZE , 8,
|
|
|
|
GLX_BLUE_SIZE , 8,
|
2017-04-21 17:08:27 +00:00
|
|
|
GLX_DEPTH_SIZE , 0,
|
2013-01-04 11:41:51 +00:00
|
|
|
GLX_DOUBLEBUFFER , True,
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2013-05-23 17:03:18 +00:00
|
|
|
PFNGLXCHOOSEFBCONFIGPROC glX_ChooseFBConfig = (PFNGLXCHOOSEFBCONFIGPROC) glXGetProcAddress((GLubyte *) "glXChooseFBConfig");
|
2013-01-04 11:41:51 +00:00
|
|
|
int fbcount = 0;
|
2013-05-23 17:03:18 +00:00
|
|
|
GLXFBConfig *fbc = glX_ChooseFBConfig(m_NativeDisplay, DefaultScreen(m_NativeDisplay), attrListDbl, &fbcount);
|
2013-06-16 09:40:38 +00:00
|
|
|
if (!fbc || fbcount < 1) {
|
|
|
|
throw GSDXRecoverableError();
|
|
|
|
}
|
2013-01-04 11:41:51 +00:00
|
|
|
|
2013-05-23 17:03:18 +00:00
|
|
|
PFNGLXCREATECONTEXTATTRIBSARBPROC glX_CreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC)glXGetProcAddress((const GLubyte*) "glXCreateContextAttribsARB");
|
2013-06-16 09:40:38 +00:00
|
|
|
if (!glX_CreateContextAttribsARB) {
|
|
|
|
throw GSDXRecoverableError();
|
|
|
|
}
|
2013-01-04 11:41:51 +00:00
|
|
|
|
2013-05-18 16:09:09 +00:00
|
|
|
// Install a dummy handler to handle gracefully (aka not segfault) the support of GL version
|
|
|
|
int (*oldHandler)(Display*, XErrorEvent*) = XSetErrorHandler(&ctxErrorHandler);
|
|
|
|
// Be sure the handler is installed
|
2013-05-18 16:19:23 +00:00
|
|
|
XSync( m_NativeDisplay, false);
|
2013-05-18 16:09:09 +00:00
|
|
|
|
2013-01-04 11:41:51 +00:00
|
|
|
// Create a context
|
|
|
|
int context_attribs[] =
|
|
|
|
{
|
|
|
|
GLX_CONTEXT_MAJOR_VERSION_ARB, major,
|
|
|
|
GLX_CONTEXT_MINOR_VERSION_ARB, minor,
|
2013-08-05 20:25:25 +00:00
|
|
|
#ifdef ENABLE_OGL_DEBUG
|
2013-01-04 11:41:51 +00:00
|
|
|
GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB,
|
2015-12-29 21:27:24 +00:00
|
|
|
#else
|
|
|
|
// Open Source isn't happy with an unsupported flags...
|
|
|
|
//GLX_CONTEXT_FLAGS_ARB, GL_CONTEXT_FLAG_NO_ERROR_BIT_KHR,
|
2013-08-05 20:25:25 +00:00
|
|
|
#endif
|
|
|
|
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
|
2013-01-04 11:41:51 +00:00
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2013-06-30 15:27:50 +00:00
|
|
|
m_context = glX_CreateContextAttribsARB(m_NativeDisplay, fbc[0], 0, true, context_attribs);
|
|
|
|
XFree(fbc);
|
2013-01-04 11:41:51 +00:00
|
|
|
|
2013-05-18 16:09:09 +00:00
|
|
|
// Don't forget to reinstall the older Handler
|
|
|
|
XSetErrorHandler(oldHandler);
|
|
|
|
|
|
|
|
// Get latest error
|
2013-05-18 16:19:23 +00:00
|
|
|
XSync( m_NativeDisplay, false);
|
2013-05-18 16:09:09 +00:00
|
|
|
|
|
|
|
if (!m_context || ctxError) {
|
|
|
|
fprintf(stderr, "Failed to create the opengl context. Check your drivers support openGL %d.%d. Hint: opensource drivers don't\n", major, minor );
|
2013-06-16 09:40:38 +00:00
|
|
|
throw GSDXRecoverableError();
|
2013-05-18 16:09:09 +00:00
|
|
|
}
|
2013-01-04 11:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSWndOGL::AttachContext()
|
|
|
|
{
|
|
|
|
if (!IsContextAttached()) {
|
|
|
|
//fprintf(stderr, "Attach the context\n");
|
2013-01-04 21:33:24 +00:00
|
|
|
glXMakeCurrent(m_NativeDisplay, m_NativeWindow, m_context);
|
2013-01-04 11:41:51 +00:00
|
|
|
m_ctx_attached = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSWndOGL::DetachContext()
|
|
|
|
{
|
|
|
|
if (IsContextAttached()) {
|
|
|
|
//fprintf(stderr, "Detach the context\n");
|
2013-01-04 21:33:24 +00:00
|
|
|
glXMakeCurrent(m_NativeDisplay, None, NULL);
|
2013-01-04 11:41:51 +00:00
|
|
|
m_ctx_attached = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-03 18:02:16 +00:00
|
|
|
void GSWndOGL::PopulateWndGlFunction()
|
|
|
|
{
|
|
|
|
m_swapinterval_ext = (PFNGLXSWAPINTERVALEXTPROC) glXGetProcAddress((const GLubyte*) "glXSwapIntervalEXT");
|
|
|
|
m_swapinterval_mesa = (PFNGLXSWAPINTERVALMESAPROC)glXGetProcAddress((const GLubyte*) "glXSwapIntervalMESA");
|
2017-07-03 20:36:31 +00:00
|
|
|
|
|
|
|
const char* ext = glXQueryExtensionsString(m_NativeDisplay, DefaultScreen(m_NativeDisplay));
|
|
|
|
m_has_late_vsync = m_swapinterval_ext && ext && strstr(ext, "GLX_EXT_swap_control");
|
2017-07-03 18:02:16 +00:00
|
|
|
}
|
|
|
|
|
2013-01-04 11:41:51 +00:00
|
|
|
bool GSWndOGL::Attach(void* handle, bool managed)
|
|
|
|
{
|
2013-01-04 21:33:24 +00:00
|
|
|
m_NativeWindow = *(Window*)handle;
|
2013-01-04 11:41:51 +00:00
|
|
|
m_managed = managed;
|
|
|
|
|
2013-01-04 21:33:24 +00:00
|
|
|
m_NativeDisplay = XOpenDisplay(NULL);
|
2013-01-04 11:41:51 +00:00
|
|
|
|
2017-07-03 18:02:16 +00:00
|
|
|
FullContextInit();
|
2013-01-04 11:41:51 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void GSWndOGL::Detach()
|
|
|
|
{
|
|
|
|
// Actually the destructor is not called when there is only a GSclose/GSshutdown
|
|
|
|
// The window still need to be closed
|
|
|
|
DetachContext();
|
2013-01-04 21:33:24 +00:00
|
|
|
if (m_context) glXDestroyContext(m_NativeDisplay, m_context);
|
2013-01-04 11:41:51 +00:00
|
|
|
|
2013-01-04 21:33:24 +00:00
|
|
|
if (m_NativeDisplay) {
|
|
|
|
XCloseDisplay(m_NativeDisplay);
|
|
|
|
m_NativeDisplay = NULL;
|
2013-01-04 11:41:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-26 15:26:46 +00:00
|
|
|
bool GSWndOGL::Create(const std::string& title, int w, int h)
|
2013-01-04 11:41:51 +00:00
|
|
|
{
|
2013-06-16 09:40:38 +00:00
|
|
|
if(m_NativeWindow)
|
|
|
|
throw GSDXRecoverableError();
|
2013-01-04 11:41:51 +00:00
|
|
|
|
|
|
|
if(w <= 0 || h <= 0) {
|
2016-05-24 19:52:06 +00:00
|
|
|
w = theApp.GetConfigI("ModeWidth");
|
|
|
|
h = theApp.GetConfigI("ModeHeight");
|
2013-01-04 11:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m_managed = true;
|
|
|
|
|
|
|
|
// note this part must be only executed when replaying .gs debug file
|
2013-01-04 21:33:24 +00:00
|
|
|
m_NativeDisplay = XOpenDisplay(NULL);
|
2013-01-04 11:41:51 +00:00
|
|
|
|
2013-05-23 17:03:18 +00:00
|
|
|
m_NativeWindow = XCreateSimpleWindow(m_NativeDisplay, DefaultRootWindow(m_NativeDisplay), 0, 0, w, h, 0, 0, 0);
|
|
|
|
XMapWindow (m_NativeDisplay, m_NativeWindow);
|
|
|
|
|
2013-07-28 14:40:43 +00:00
|
|
|
if (m_NativeWindow == 0)
|
|
|
|
throw GSDXRecoverableError();
|
|
|
|
|
2017-07-03 18:02:16 +00:00
|
|
|
FullContextInit();
|
2013-06-16 08:43:50 +00:00
|
|
|
|
2013-06-16 09:40:38 +00:00
|
|
|
return true;
|
2013-01-04 11:41:51 +00:00
|
|
|
}
|
|
|
|
|
2013-08-03 08:29:01 +00:00
|
|
|
void* GSWndOGL::GetProcAddress(const char* name, bool opt)
|
2013-06-16 08:43:50 +00:00
|
|
|
{
|
|
|
|
void* ptr = (void*)glXGetProcAddress((const GLubyte*)name);
|
|
|
|
if (ptr == NULL) {
|
2019-02-09 10:10:00 +00:00
|
|
|
if (theApp.GetConfigB("debug_opengl"))
|
|
|
|
fprintf(stderr, "Failed to find %s\n", name);
|
|
|
|
|
2013-08-03 08:29:01 +00:00
|
|
|
if (!opt)
|
|
|
|
throw GSDXRecoverableError();
|
2013-06-16 08:43:50 +00:00
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
|
2013-01-04 11:41:51 +00:00
|
|
|
void* GSWndOGL::GetDisplay()
|
|
|
|
{
|
|
|
|
// note this part must be only executed when replaying .gs debug file
|
2013-01-04 21:33:24 +00:00
|
|
|
return (void*)m_NativeDisplay;
|
2013-01-04 11:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GSVector4i GSWndOGL::GetClientRect()
|
|
|
|
{
|
|
|
|
unsigned int h = 480;
|
|
|
|
unsigned int w = 640;
|
|
|
|
|
|
|
|
unsigned int borderDummy;
|
|
|
|
unsigned int depthDummy;
|
|
|
|
Window winDummy;
|
|
|
|
int xDummy;
|
|
|
|
int yDummy;
|
|
|
|
|
2013-01-04 21:33:24 +00:00
|
|
|
if (!m_NativeDisplay) m_NativeDisplay = XOpenDisplay(NULL);
|
|
|
|
XGetGeometry(m_NativeDisplay, m_NativeWindow, &winDummy, &xDummy, &yDummy, &w, &h, &borderDummy, &depthDummy);
|
2013-01-04 11:41:51 +00:00
|
|
|
|
|
|
|
return GSVector4i(0, 0, (int)w, (int)h);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns FALSE if the window has no title, or if th window title is under the strict
|
|
|
|
// management of the emulator.
|
|
|
|
|
|
|
|
bool GSWndOGL::SetWindowText(const char* title)
|
|
|
|
{
|
|
|
|
if (!m_managed) return true;
|
|
|
|
|
|
|
|
XTextProperty prop;
|
|
|
|
|
|
|
|
memset(&prop, 0, sizeof(prop));
|
|
|
|
|
|
|
|
char* ptitle = (char*)title;
|
|
|
|
if (XStringListToTextProperty(&ptitle, 1, &prop)) {
|
2013-01-04 21:33:24 +00:00
|
|
|
XSetWMName(m_NativeDisplay, m_NativeWindow, &prop);
|
2013-01-04 11:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
XFree(prop.value);
|
2013-01-04 21:33:24 +00:00
|
|
|
XFlush(m_NativeDisplay);
|
2013-01-04 11:41:51 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-08-21 22:02:29 +00:00
|
|
|
void GSWndOGL::SetSwapInterval()
|
2013-01-04 11:41:51 +00:00
|
|
|
{
|
|
|
|
// m_swapinterval uses an integer as parameter
|
|
|
|
// 0 -> disable vsync
|
|
|
|
// n -> wait n frame
|
2017-08-21 22:02:29 +00:00
|
|
|
if (m_swapinterval_ext) m_swapinterval_ext(m_NativeDisplay, m_NativeWindow, m_vsync);
|
|
|
|
else if (m_swapinterval_mesa) m_swapinterval_mesa(m_vsync);
|
2016-06-04 11:17:59 +00:00
|
|
|
else fprintf(stderr, "Failed to set VSync\n");
|
2013-01-04 11:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSWndOGL::Flip()
|
|
|
|
{
|
2017-08-21 22:02:29 +00:00
|
|
|
if (m_vsync_change_requested.exchange(false))
|
|
|
|
SetSwapInterval();
|
|
|
|
|
2013-01-04 21:33:24 +00:00
|
|
|
glXSwapBuffers(m_NativeDisplay, m_NativeWindow);
|
2013-01-04 11:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSWndOGL::Show()
|
|
|
|
{
|
2013-01-04 21:33:24 +00:00
|
|
|
XMapRaised(m_NativeDisplay, m_NativeWindow);
|
|
|
|
XFlush(m_NativeDisplay);
|
2013-01-04 11:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSWndOGL::Hide()
|
|
|
|
{
|
2013-01-04 21:33:24 +00:00
|
|
|
XUnmapWindow(m_NativeDisplay, m_NativeWindow);
|
|
|
|
XFlush(m_NativeDisplay);
|
2013-01-04 11:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void GSWndOGL::HideFrame()
|
|
|
|
{
|
|
|
|
// TODO
|
|
|
|
}
|
2013-01-04 21:33:24 +00:00
|
|
|
|
2013-01-04 11:41:51 +00:00
|
|
|
#endif
|