RetroArch/gfx/drivers_context/wgl_ctx.cpp

475 lines
11 KiB
C++
Raw Normal View History

/* RetroArch - A frontend for libretro.
2014-01-01 00:50:59 +00:00
* Copyright (C) 2010-2014 - Hans-Kristian Arntzen
2015-01-07 17:06:50 +00:00
* Copyright (C) 2011-2015 - Daniel De Matteis
*
* RetroArch 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 Found-
* ation, either version 3 of the License, or (at your option) any later version.
*
* RetroArch 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 RetroArch.
* If not, see <http://www.gnu.org/licenses/>.
*/
2015-10-23 05:59:53 +00:00
#include <math.h>
2015-01-07 17:06:50 +00:00
/* Win32/WGL context. */
2015-01-07 17:06:50 +00:00
/* necessary for mingw32 multimon defines: */
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0500 //_WIN32_WINNT_WIN2K
#endif
2015-09-05 12:10:16 +00:00
#include <string.h>
#include <math.h>
2015-09-05 12:10:16 +00:00
#include <windows.h>
#include <commdlg.h>
2015-09-05 12:38:55 +00:00
#include <dynamic/dylib.h>
#include "../../driver.h"
2015-04-10 04:33:18 +00:00
#include "../../dynamic.h"
#include "../../runloop.h"
#include "../video_context_driver.h"
2015-11-17 07:01:33 +00:00
#include "../common/gl_common.h"
2015-04-09 03:16:02 +00:00
#include "../common/win32_common.h"
2015-11-17 07:01:33 +00:00
#ifndef WGL_CONTEXT_MAJOR_VERSION_ARB
#define WGL_CONTEXT_MAJOR_VERSION_ARB 0x2091
#endif
#ifndef WGL_CONTEXT_MINOR_VERSION_ARB
#define WGL_CONTEXT_MINOR_VERSION_ARB 0x2092
#endif
#ifndef WGL_CONTEXT_PROFILE_MASK_ARB
#define WGL_CONTEXT_PROFILE_MASK_ARB 0x9126
#endif
#ifndef WGL_CONTEXT_CORE_PROFILE_BIT_ARB
#define WGL_CONTEXT_CORE_PROFILE_BIT_ARB 0x0001
#endif
#ifndef WGL_CONTEXT_FLAGS_ARB
#define WGL_CONTEXT_FLAGS_ARB 0x2094
#endif
#ifndef WGL_CONTEXT_DEBUG_BIT_ARB
#define WGL_CONTEXT_DEBUG_BIT_ARB 0x0001
2015-09-05 12:10:16 +00:00
#endif
2015-11-20 13:32:46 +00:00
typedef HGLRC (APIENTRY *wglCreateContextAttribsProc)(HDC, HGLRC, const int*);
static BOOL (APIENTRY *p_swap_interval)(int);
2014-04-19 14:21:37 +00:00
static bool g_use_hw_ctx;
static HGLRC g_hrc;
2014-04-19 14:21:37 +00:00
static HGLRC g_hw_hrc;
static HDC g_hdc;
2015-11-11 04:32:00 +00:00
2013-06-23 11:05:33 +00:00
static unsigned g_major;
static unsigned g_minor;
static unsigned g_interval;
2015-09-05 12:38:55 +00:00
static dylib_t dll_handle = NULL; /* Handle to OpenGL32.dll */
2015-02-08 00:24:44 +00:00
2013-06-23 11:05:33 +00:00
static wglCreateContextAttribsProc pcreate_context;
static void setup_pixel_format(HDC hdc)
{
PIXELFORMATDESCRIPTOR pfd = {0};
pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pfd.nVersion = 1;
pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pfd.iPixelType = PFD_TYPE_RGBA;
pfd.cColorBits = 32;
pfd.cDepthBits = 0;
pfd.cStencilBits = 0;
pfd.iLayerType = PFD_MAIN_PLANE;
SetPixelFormat(hdc, ChoosePixelFormat(hdc, &pfd), &pfd);
}
2015-11-17 22:14:59 +00:00
void create_gl_context(HWND hwnd, bool *quit)
{
bool core_context;
const struct retro_hw_render_callback *hw_render =
(const struct retro_hw_render_callback*)video_driver_callback();
driver_t *driver = driver_get_ptr();
bool debug = hw_render->debug_context;
2015-02-08 00:24:44 +00:00
#ifdef _WIN32
2015-09-05 12:38:55 +00:00
dll_handle = dylib_load("OpenGL32.dll");
2015-02-08 00:24:44 +00:00
#endif
g_hdc = GetDC(hwnd);
setup_pixel_format(g_hdc);
2014-04-19 14:21:37 +00:00
#ifdef GL_DEBUG
debug = true;
2014-04-19 14:21:37 +00:00
#endif
core_context = (g_major * 1000 + g_minor) >= 3001;
2014-04-19 14:21:37 +00:00
if (g_hrc)
{
RARCH_LOG("[WGL]: Using cached GL context.\n");
driver->video_cache_context_ack = true;
}
else
2014-04-19 14:21:37 +00:00
{
g_hrc = wglCreateContext(g_hdc);
2014-10-27 13:35:23 +00:00
/* We'll create shared context later if not. */
if (g_hrc && !core_context && !debug)
2014-04-19 14:21:37 +00:00
{
g_hw_hrc = wglCreateContext(g_hdc);
if (g_hw_hrc)
{
if (!wglShareLists(g_hrc, g_hw_hrc))
{
RARCH_LOG("[WGL]: Failed to share contexts.\n");
2015-11-17 22:14:59 +00:00
*quit = true;
2014-04-19 14:21:37 +00:00
}
}
else
2015-11-17 22:14:59 +00:00
*quit = true;
2014-04-19 14:21:37 +00:00
}
}
if (g_hrc)
{
if (wglMakeCurrent(g_hdc, g_hrc))
g_inited = true;
else
2015-11-17 22:14:59 +00:00
*quit = true;
}
else
2013-06-23 11:05:33 +00:00
{
2015-11-17 22:14:59 +00:00
*quit = true;
return;
2013-06-23 11:05:33 +00:00
}
if (core_context || debug)
2013-06-23 11:05:33 +00:00
{
int attribs[16];
int *aptr = attribs;
if (core_context)
{
*aptr++ = WGL_CONTEXT_MAJOR_VERSION_ARB;
*aptr++ = g_major;
*aptr++ = WGL_CONTEXT_MINOR_VERSION_ARB;
*aptr++ = g_minor;
2014-10-27 13:35:23 +00:00
/* Technically, we don't have core/compat until 3.2.
* Version 3.1 is either compat or not depending
* on GL_ARB_compatibility.
*/
if ((g_major * 1000 + g_minor) >= 3002)
{
*aptr++ = WGL_CONTEXT_PROFILE_MASK_ARB;
*aptr++ = WGL_CONTEXT_CORE_PROFILE_BIT_ARB;
}
}
if (debug)
{
*aptr++ = WGL_CONTEXT_FLAGS_ARB;
*aptr++ = WGL_CONTEXT_DEBUG_BIT_ARB;
}
*aptr = 0;
2013-06-23 11:05:33 +00:00
if (!pcreate_context)
2014-10-27 13:35:23 +00:00
pcreate_context = (wglCreateContextAttribsProc)
wglGetProcAddress("wglCreateContextAttribsARB");
2013-06-23 11:05:33 +00:00
if (pcreate_context)
{
HGLRC context = pcreate_context(g_hdc, NULL, attribs);
if (context)
{
wglMakeCurrent(NULL, NULL);
wglDeleteContext(g_hrc);
g_hrc = context;
if (!wglMakeCurrent(g_hdc, g_hrc))
2015-11-17 22:14:59 +00:00
*quit = true;
2013-06-23 11:05:33 +00:00
}
else
RARCH_ERR("[WGL]: Failed to create core context. Falling back to legacy context.\n");
2014-04-19 14:21:37 +00:00
if (g_use_hw_ctx)
{
g_hw_hrc = pcreate_context(g_hdc, context, attribs);
if (!g_hw_hrc)
{
RARCH_ERR("[WGL]: Failed to create shared context.\n");
2015-11-17 22:14:59 +00:00
*quit = true;
2014-04-19 14:21:37 +00:00
}
}
2013-06-23 11:05:33 +00:00
}
else
RARCH_ERR("[WGL]: wglCreateContextAttribsARB not supported.\n");
}
}
2015-11-11 17:29:06 +00:00
void *dinput_wgl;
2013-11-05 19:49:04 +00:00
static void gfx_ctx_wgl_swap_interval(void *data, unsigned interval)
{
2014-03-09 16:11:06 +00:00
(void)data;
g_interval = interval;
if (!g_hrc)
return;
if (!p_swap_interval)
return;
RARCH_LOG("[WGL]: wglSwapInterval(%u)\n", g_interval);
if (!p_swap_interval(g_interval))
RARCH_WARN("[WGL]: wglSwapInterval() failed.\n");
}
static void gfx_ctx_wgl_check_window(void *data, bool *quit,
bool *resize, unsigned *width, unsigned *height, unsigned frame_count)
{
win32_check_window(quit, resize, width, height);
}
static void gfx_ctx_wgl_swap_buffers(void *data)
{
2014-03-09 16:11:06 +00:00
(void)data;
SwapBuffers(g_hdc);
}
static void gfx_ctx_wgl_set_resize(void *data,
unsigned width, unsigned height)
{
2014-03-09 16:11:06 +00:00
(void)data;
(void)width;
(void)height;
}
static void gfx_ctx_wgl_update_window_title(void *data)
{
2015-06-13 00:10:06 +00:00
char buf[128] = {0};
char buf_fps[128] = {0};
2015-03-20 21:08:36 +00:00
settings_t *settings = config_get_ptr();
2015-11-17 08:41:18 +00:00
HWND window = win32_get_window();
(void)data;
if (video_monitor_get_fps(buf, sizeof(buf),
2015-03-07 22:03:56 +00:00
buf_fps, sizeof(buf_fps)))
2015-11-17 08:41:18 +00:00
SetWindowText(window, buf);
2015-03-20 21:08:36 +00:00
if (settings->fps_show)
rarch_main_msg_queue_push(buf_fps, 1, 1, false);
}
static void gfx_ctx_wgl_get_video_size(void *data, unsigned *width, unsigned *height)
{
2014-03-09 16:11:06 +00:00
(void)data;
2015-11-17 08:41:18 +00:00
HWND window = win32_get_window();
2015-11-17 08:41:18 +00:00
if (!window)
{
unsigned mon_id;
2015-01-16 15:22:19 +00:00
RECT mon_rect;
MONITORINFOEX current_mon;
2015-01-16 15:22:19 +00:00
HMONITOR hm_to_use = NULL;
win32_monitor_info(&current_mon, &hm_to_use, &mon_id);
2015-01-16 15:22:19 +00:00
mon_rect = current_mon.rcMonitor;
*width = mon_rect.right - mon_rect.left;
*height = mon_rect.bottom - mon_rect.top;
}
else
{
*width = g_resize_width;
*height = g_resize_height;
}
}
static bool gfx_ctx_wgl_init(void *data)
{
WNDCLASSEX wndclass = {0};
2014-03-09 16:11:06 +00:00
(void)data;
if (g_inited)
return false;
2015-11-17 09:16:16 +00:00
win32_window_reset();
win32_monitor_init();
2015-11-19 07:30:37 +00:00
if (!win32_window_init(&wndclass, true, NULL))
2015-11-11 17:15:21 +00:00
return false;
return true;
}
static void gfx_ctx_wgl_destroy(void *data)
{
driver_t *driver = driver_get_ptr();
2015-11-17 08:41:18 +00:00
HWND window = win32_get_window();
2014-03-09 16:11:06 +00:00
(void)data;
if (g_hrc)
{
2013-06-30 21:22:37 +00:00
glFinish();
wglMakeCurrent(NULL, NULL);
if (!driver->video_cache_context)
{
2014-04-19 14:21:37 +00:00
if (g_hw_hrc)
wglDeleteContext(g_hw_hrc);
wglDeleteContext(g_hrc);
g_hrc = NULL;
2014-04-19 14:21:37 +00:00
g_hw_hrc = NULL;
}
}
2015-11-17 08:41:18 +00:00
if (window && g_hdc)
{
2015-11-17 08:41:18 +00:00
ReleaseDC(window, g_hdc);
g_hdc = NULL;
}
2015-11-17 08:41:18 +00:00
if (window)
{
2015-11-17 08:41:18 +00:00
win32_monitor_from_window(window, true);
2015-11-17 08:44:26 +00:00
win32_destroy_window();
}
if (g_restore_desktop)
{
win32_monitor_get_info();
g_restore_desktop = false;
}
g_inited = false;
2013-06-23 11:05:33 +00:00
g_major = g_minor = 0;
p_swap_interval = NULL;
}
2015-11-20 13:32:46 +00:00
static bool gfx_ctx_wgl_set_video_mode(void *data,
unsigned width, unsigned height,
bool fullscreen)
{
if (!win32_set_video_mode(data, width, height, fullscreen))
goto error;
p_swap_interval = (BOOL (APIENTRY *)(int))wglGetProcAddress("wglSwapIntervalEXT");
gfx_ctx_wgl_swap_interval(data, g_interval);
return true;
error:
gfx_ctx_wgl_destroy(data);
return false;
}
static void gfx_ctx_wgl_input_driver(void *data,
const input_driver_t **input, void **input_data)
{
2014-03-09 16:11:06 +00:00
(void)data;
dinput_wgl = input_dinput.init();
2014-09-14 17:30:44 +00:00
*input = dinput_wgl ? &input_dinput : NULL;
*input_data = dinput_wgl;
}
static bool gfx_ctx_wgl_has_focus(void *data)
{
2015-11-17 07:58:49 +00:00
return win32_has_focus();
}
2015-01-18 21:32:14 +00:00
static bool gfx_ctx_wgl_suppress_screensaver(void *data, bool enable)
{
return win32_suppress_screensaver(data, enable);
2015-01-18 21:32:14 +00:00
}
static bool gfx_ctx_wgl_has_windowed(void *data)
{
(void)data;
return true;
}
static gfx_ctx_proc_t gfx_ctx_wgl_get_proc_address(const char *symbol)
{
2015-02-08 00:24:44 +00:00
void *func = (void *)wglGetProcAddress(symbol);
if (func)
return (gfx_ctx_proc_t)wglGetProcAddress(symbol);
2015-09-05 12:38:55 +00:00
return (gfx_ctx_proc_t)GetProcAddress((HINSTANCE)dll_handle, symbol);
}
static bool gfx_ctx_wgl_get_metrics(void *data,
enum display_metric_types type, float *value)
{
2015-04-09 03:19:29 +00:00
return win32_get_metrics(data, type, value);
}
static bool gfx_ctx_wgl_bind_api(void *data,
enum gfx_ctx_api api, unsigned major, unsigned minor)
{
2014-03-09 16:11:06 +00:00
(void)data;
2013-06-23 11:05:33 +00:00
g_major = major;
g_minor = minor;
return api == GFX_CTX_OPENGL_API;
}
static void gfx_ctx_wgl_show_mouse(void *data, bool state)
2013-03-29 12:46:11 +00:00
{
2014-03-09 16:11:06 +00:00
(void)data;
2015-04-10 07:30:18 +00:00
win32_show_cursor(state);
2013-03-29 12:46:11 +00:00
}
static void gfx_ctx_wgl_bind_hw_render(void *data, bool enable)
2014-04-19 14:21:37 +00:00
{
2014-04-19 14:25:27 +00:00
g_use_hw_ctx = enable;
2014-04-19 14:21:37 +00:00
if (g_hdc)
wglMakeCurrent(g_hdc, enable ? g_hw_hrc : g_hrc);
}
const gfx_ctx_driver_t gfx_ctx_wgl = {
gfx_ctx_wgl_init,
gfx_ctx_wgl_destroy,
gfx_ctx_wgl_bind_api,
gfx_ctx_wgl_swap_interval,
gfx_ctx_wgl_set_video_mode,
gfx_ctx_wgl_get_video_size,
NULL, /* get_video_output_size */
NULL, /* get_video_output_prev */
NULL, /* get_video_output_next */
gfx_ctx_wgl_get_metrics,
NULL,
gfx_ctx_wgl_update_window_title,
gfx_ctx_wgl_check_window,
gfx_ctx_wgl_set_resize,
gfx_ctx_wgl_has_focus,
2015-01-18 21:32:14 +00:00
gfx_ctx_wgl_suppress_screensaver,
gfx_ctx_wgl_has_windowed,
gfx_ctx_wgl_swap_buffers,
gfx_ctx_wgl_input_driver,
gfx_ctx_wgl_get_proc_address,
NULL,
NULL,
gfx_ctx_wgl_show_mouse,
"wgl",
gfx_ctx_wgl_bind_hw_render,
};