summaryrefslogtreecommitdiffstatshomepage
path: root/src/osd/modules/render/winglcontext.h
blob: 48d1462d2f68520262d62fdd9a43b6f223004f56 (plain) (blame)
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
// license:BSD-3-Clause
// copyright-holders:Olivier Galibert, R. Belmont
//============================================================
//
//  winglcontext.h - Windows-specific GL context
//
//  SDLMAME by Olivier Galibert and R. Belmont
//
//============================================================
#ifndef MAME_RENDER_WINGLCONTEXT_H
#define MAME_RENDER_WINGLCONTEXT_H

#pragma once

#include "modules/opengl/osd_opengl.h"
#include "modules/lib/osdlib.h"

#include "strconv.h"

#include <cstring>
#include <string>


class win_gl_context : public osd_gl_context
{
public:
	using osd_gl_context::get_proc_address;

	win_gl_context(HWND window) : m_context(nullptr), m_window(window), m_hdc(nullptr)
	{
		// open DLL and bind required functions
		opengl32_dll = osd::dynamic_module::open({ "opengl32.dll" });
		pfn_wglGetProcAddress = opengl32_dll->bind<wglGetProcAddress_fn>("wglGetProcAddress");
		pfn_wglCreateContext = opengl32_dll->bind<wglCreateContext_fn>("wglCreateContext");
		pfn_wglDeleteContext = opengl32_dll->bind<wglDeleteContext_fn>("wglDeleteContext");
		pfn_wglMakeCurrent = opengl32_dll->bind<wglMakeCurrent_fn>("wglMakeCurrent");
		if (!pfn_wglGetProcAddress || !pfn_wglCreateContext || !pfn_wglDeleteContext || !pfn_wglMakeCurrent)
			return;

		m_hdc = GetDC(window);
		if (!m_hdc)
		{
			get_last_error_string();
			return;
		}

		if (setupPixelFormat())
		{
			m_context = (*pfn_wglCreateContext)(m_hdc);
			if (!m_context)
				get_last_error_string();
		}
		if (!m_context)
			return;

		(*pfn_wglMakeCurrent)(m_hdc, m_context);

		get_proc_address(pfn_wglGetExtensionsStringEXT, "wglGetExtensionsStringEXT");
		if (WGLExtensionSupported("WGL_EXT_swap_control"))
		{
			get_proc_address(pfn_wglSwapIntervalEXT, "wglSwapIntervalEXT");
			get_proc_address(pfn_wglGetSwapIntervalEXT, "wglGetSwapIntervalEXT");
		}
		else
		{
			pfn_wglSwapIntervalEXT = nullptr;
			pfn_wglGetSwapIntervalEXT = nullptr;
		}
	}

	virtual ~win_gl_context()
	{
		if (m_context)
			(*pfn_wglDeleteContext)(m_context);

		if (m_hdc)
			ReleaseDC(m_window, m_hdc);
	}

	virtual explicit operator bool() const override
	{
		return bool(m_context);
	}

	virtual void make_current() override
	{
		(*pfn_wglMakeCurrent)(m_hdc, m_context);
	}

	virtual const char *last_error_message() override
	{
		if (!m_error.empty())
			return m_error.c_str();
		else
			return nullptr;
	}

	virtual void *get_proc_address(const char *proc) override
	{
		return reinterpret_cast<void *>(uintptr_t((*pfn_wglGetProcAddress)(proc)));
	}

	virtual bool set_swap_interval(const int swap) override
	{
		if (!pfn_wglSwapIntervalEXT)
			return false;

		pfn_wglSwapIntervalEXT(swap ? 1 : 0);
		return true;
	}

	virtual void swap_buffer() override
	{
		SwapBuffers(m_hdc);
		//wglSwapLayerBuffers(GetDC(window().m_hwnd), WGL_SWAP_MAIN_PLANE);
	}

private:
	// Typedefs for dynamically loaded functions
	typedef PROC (WINAPI *wglGetProcAddress_fn)(LPCSTR);
	typedef HGLRC (WINAPI *wglCreateContext_fn)(HDC);
	typedef BOOL (WINAPI *wglDeleteContext_fn)(HGLRC);
	typedef BOOL (WINAPI *wglMakeCurrent_fn)(HDC, HGLRC);

	typedef const char * (WINAPI *wglGetExtensionsStringEXT_fn)();
	typedef BOOL (WINAPI *wglSwapIntervalEXT_fn)(int);
	typedef int (WINAPI *wglGetSwapIntervalEXT_fn)();

	bool setupPixelFormat()
	{
		PIXELFORMATDESCRIPTOR pfd = {
				sizeof(PIXELFORMATDESCRIPTOR),  // size
				1,                              // version
				PFD_SUPPORT_OPENGL |
				PFD_DRAW_TO_WINDOW |
				PFD_DOUBLEBUFFER,               // support double-buffering
				PFD_TYPE_RGBA,                  // color type
				32,                             // prefered color depth
				0, 0, 0, 0, 0, 0,               // color bits (ignored)
				0,                              // no alpha buffer
				0,                              // alpha bits (ignored)
				0,                              // no accumulation buffer
				0, 0, 0, 0,                     // accum bits (ignored)
				16,                             // depth buffer
				0,                              // no stencil buffer
				0,                              // no auxiliary buffers
				PFD_MAIN_PLANE,                 // main layer
				0,                              // reserved
				0, 0, 0,                        // no layer, visible, damage masks
			};

		const int pixelFormat = ChoosePixelFormat(m_hdc, &pfd);
		if (pixelFormat == 0)
		{
			get_last_error_string();
			return false;
		}

		if (SetPixelFormat(m_hdc, pixelFormat, &pfd) != TRUE)
		{
			get_last_error_string();
			return false;
		}

		return true;
	}

	bool WGLExtensionSupported(const char *extension_name)
	{
		if (!pfn_wglGetExtensionsStringEXT)
			return false;

		return strstr(pfn_wglGetExtensionsStringEXT(), extension_name) != nullptr;
	}

	void get_last_error_string()
	{
		LPTSTR buffer = nullptr;
		const auto result = FormatMessage(
				FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_ALLOCATE_BUFFER,
				nullptr,
				GetLastError(),
				0,
				LPTSTR(&buffer),
				0,
				nullptr);
		if (!result || !buffer)
		{
			m_error.clear();
			return;
		}
		try { m_error = osd::text::from_tstring(buffer); }
		catch (...) { m_error.clear(); }
		LocalFree(buffer);
	}

	HGLRC m_context;
	HWND const m_window;
	HDC m_hdc;
	std::string m_error;

	osd::dynamic_module::ptr     opengl32_dll;
	wglGetProcAddress_fn         pfn_wglGetProcAddress;
	wglCreateContext_fn          pfn_wglCreateContext;
	wglDeleteContext_fn          pfn_wglDeleteContext;
	wglMakeCurrent_fn            pfn_wglMakeCurrent;

	wglGetExtensionsStringEXT_fn pfn_wglGetExtensionsStringEXT;
	wglSwapIntervalEXT_fn        pfn_wglSwapIntervalEXT;
	wglGetSwapIntervalEXT_fn     pfn_wglGetSwapIntervalEXT;
};

#endif // MAME_RENDER_WINGLCONTEXT_H