forked from mikke89/RmlUi
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathRmlUi_Backend_Win32_GL2.cpp
448 lines (377 loc) · 14.2 KB
/
RmlUi_Backend_Win32_GL2.cpp
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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
/*
* This source file is part of RmlUi, the HTML/CSS Interface Middleware
*
* For the latest information, see http://github.com/mikke89/RmlUi
*
* Copyright (c) 2008-2010 CodePoint Ltd, Shift Technology Ltd
* Copyright (c) 2019 The RmlUi Team, and contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include "RmlUi_Backend.h"
#include "RmlUi_Include_Windows.h"
#include "RmlUi_Platform_Win32.h"
#include "RmlUi_Renderer_GL2.h"
#include <RmlUi/Core/Context.h>
#include <RmlUi/Core/Core.h>
#include <RmlUi/Core/Input.h>
#include <RmlUi/Core/Profiling.h>
/**
High DPI support using Windows Per Monitor V2 DPI awareness.
Requires Windows 10, version 1703.
*/
#ifndef DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2
#define DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 ((HANDLE)-4)
#endif
#ifndef WM_DPICHANGED
#define WM_DPICHANGED 0x02E0
#endif
// Declare pointers to the DPI aware Windows API functions.
using ProcSetProcessDpiAwarenessContext = BOOL(WINAPI*)(HANDLE value);
using ProcGetDpiForWindow = UINT(WINAPI*)(HWND hwnd);
using ProcAdjustWindowRectExForDpi = BOOL(WINAPI*)(LPRECT lpRect, DWORD dwStyle, BOOL bMenu, DWORD dwExStyle, UINT dpi);
static bool has_dpi_support = false;
static ProcSetProcessDpiAwarenessContext procSetProcessDpiAwarenessContext = NULL;
static ProcGetDpiForWindow procGetDpiForWindow = NULL;
static ProcAdjustWindowRectExForDpi procAdjustWindowRectExForDpi = NULL;
// Make ourselves DPI aware on supported Windows versions.
static void InitializeDpiSupport()
{
// Cast function pointers to void* first for MinGW not to emit errors.
procSetProcessDpiAwarenessContext =
(ProcSetProcessDpiAwarenessContext)(void*)GetProcAddress(GetModuleHandle(TEXT("User32.dll")), "SetProcessDpiAwarenessContext");
procGetDpiForWindow = (ProcGetDpiForWindow)(void*)GetProcAddress(GetModuleHandle(TEXT("User32.dll")), "GetDpiForWindow");
procAdjustWindowRectExForDpi =
(ProcAdjustWindowRectExForDpi)(void*)GetProcAddress(GetModuleHandle(TEXT("User32.dll")), "AdjustWindowRectExForDpi");
if (!has_dpi_support && procSetProcessDpiAwarenessContext != NULL && procGetDpiForWindow != NULL && procAdjustWindowRectExForDpi != NULL)
{
// Activate Per Monitor V2.
if (procSetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2))
has_dpi_support = true;
}
}
static UINT GetWindowDpi(HWND window_handle)
{
if (has_dpi_support)
{
UINT dpi = procGetDpiForWindow(window_handle);
if (dpi != 0)
return dpi;
}
return USER_DEFAULT_SCREEN_DPI;
}
static float GetDensityIndependentPixelRatio(HWND window_handle)
{
return float(GetWindowDpi(window_handle)) / float(USER_DEFAULT_SCREEN_DPI);
}
static void DisplayError(HWND window_handle, const Rml::String& msg)
{
MessageBoxW(window_handle, RmlWin32::ConvertToUTF16(msg).c_str(), L"Backend Error", MB_OK);
}
// Create the window but don't show it yet. Returns the pixel size of the window, which may be different than the passed size due to DPI settings.
static HWND InitializeWindow(HINSTANCE instance_handle, const std::wstring& name, int& inout_width, int& inout_height, bool allow_resize);
// Attach the OpenGL context.
static bool AttachToNative(HWND window_handle, HDC& out_device_context, HGLRC& out_render_context);
// Detach the OpenGL context.
static void DetachFromNative(HWND window_handle, HDC device_context, HGLRC render_context);
/**
Global data used by this backend.
Lifetime governed by the calls to Backend::Initialize() and Backend::Shutdown().
*/
struct BackendData {
SystemInterface_Win32 system_interface;
RenderInterface_GL2 render_interface;
HINSTANCE instance_handle = nullptr;
std::wstring instance_name;
HWND window_handle = nullptr;
HDC device_context = nullptr;
HGLRC render_context = nullptr;
bool context_dimensions_dirty = true;
Rml::Vector2i window_dimensions;
bool running = true;
// Arguments set during event processing and nulled otherwise.
Rml::Context* context = nullptr;
KeyDownCallback key_down_callback = nullptr;
};
static Rml::UniquePtr<BackendData> data;
bool Backend::Initialize(const char* window_name, int width, int height, bool allow_resize)
{
RMLUI_ASSERT(!data);
const std::wstring name = RmlWin32::ConvertToUTF16(Rml::String(window_name));
data = Rml::MakeUnique<BackendData>();
data->instance_handle = GetModuleHandle(nullptr);
data->instance_name = name;
InitializeDpiSupport();
// Initialize the window but don't show it yet.
HWND window_handle = InitializeWindow(data->instance_handle, name, width, height, allow_resize);
if (!window_handle)
return false;
// Attach the OpenGL context.
if (!AttachToNative(window_handle, data->device_context, data->render_context))
{
::CloseWindow(window_handle);
return false;
}
data->window_handle = window_handle;
data->system_interface.SetWindow(window_handle);
// Now we are ready to show the window.
::ShowWindow(window_handle, SW_SHOW);
::SetForegroundWindow(window_handle);
::SetFocus(window_handle);
return true;
}
void Backend::Shutdown()
{
RMLUI_ASSERT(data);
DetachFromNative(data->window_handle, data->device_context, data->render_context);
::DestroyWindow(data->window_handle);
::UnregisterClassW((LPCWSTR)data->instance_name.data(), data->instance_handle);
data.reset();
}
Rml::SystemInterface* Backend::GetSystemInterface()
{
RMLUI_ASSERT(data);
return &data->system_interface;
}
Rml::RenderInterface* Backend::GetRenderInterface()
{
RMLUI_ASSERT(data);
return &data->render_interface;
}
bool Backend::ProcessEvents(Rml::Context* context, KeyDownCallback key_down_callback)
{
RMLUI_ASSERT(data && context);
// The initial window size may have been affected by system DPI settings, apply the actual pixel size and dp-ratio to the context.
if (data->context_dimensions_dirty)
{
data->context_dimensions_dirty = false;
const float dp_ratio = GetDensityIndependentPixelRatio(data->window_handle);
context->SetDimensions(data->window_dimensions);
context->SetDensityIndependentPixelRatio(dp_ratio);
}
data->context = context;
data->key_down_callback = key_down_callback;
MSG message;
while (PeekMessage(&message, nullptr, 0, 0, PM_NOREMOVE))
{
GetMessage(&message, nullptr, 0, 0);
// Dispatch the message to our local event handler below.
TranslateMessage(&message);
DispatchMessage(&message);
}
data->context = nullptr;
data->key_down_callback = nullptr;
return data->running;
}
void Backend::RequestExit()
{
RMLUI_ASSERT(data);
data->running = false;
}
void Backend::BeginFrame()
{
RMLUI_ASSERT(data);
data->render_interface.BeginFrame();
data->render_interface.Clear();
}
void Backend::PresentFrame()
{
RMLUI_ASSERT(data);
data->render_interface.EndFrame();
// Flips the OpenGL buffers.
SwapBuffers(data->device_context);
// Optional, used to mark frames during performance profiling.
RMLUI_FrameMark;
}
// Local event handler for window and input events.
static LRESULT CALLBACK WindowProcedureHandler(HWND window_handle, UINT message, WPARAM w_param, LPARAM l_param)
{
RMLUI_ASSERT(data);
switch (message)
{
case WM_CLOSE:
{
data->running = false;
return 0;
}
break;
case WM_SIZE:
{
const int width = LOWORD(l_param);
const int height = HIWORD(l_param);
data->window_dimensions.x = width;
data->window_dimensions.y = height;
data->render_interface.SetViewport(width, height);
if (data->context)
data->context->SetDimensions(data->window_dimensions);
return 0;
}
break;
case WM_DPICHANGED:
{
RECT* new_pos = (RECT*)l_param;
SetWindowPos(window_handle, NULL, new_pos->left, new_pos->top, new_pos->right - new_pos->left, new_pos->bottom - new_pos->top,
SWP_NOZORDER | SWP_NOACTIVATE);
if (data->context && has_dpi_support)
data->context->SetDensityIndependentPixelRatio(GetDensityIndependentPixelRatio(window_handle));
return 0;
}
break;
case WM_KEYDOWN:
{
// Override the default key event callback to add global shortcuts for the samples.
Rml::Context* context = data->context;
KeyDownCallback key_down_callback = data->key_down_callback;
const Rml::Input::KeyIdentifier rml_key = RmlWin32::ConvertKey((int)w_param);
const int rml_modifier = RmlWin32::GetKeyModifierState();
const float native_dp_ratio = GetDensityIndependentPixelRatio(window_handle);
// See if we have any global shortcuts that take priority over the context.
if (key_down_callback && !key_down_callback(context, rml_key, rml_modifier, native_dp_ratio, true))
return 0;
// Otherwise, hand the event over to the context by calling the input handler as normal.
if (!RmlWin32::WindowProcedure(context, window_handle, message, w_param, l_param))
return 0;
// The key was not consumed by the context either, try keyboard shortcuts of lower priority.
if (key_down_callback && !key_down_callback(context, rml_key, rml_modifier, native_dp_ratio, false))
return 0;
return 0;
}
break;
default:
{
// Submit it to the platform handler for default input handling.
if (!RmlWin32::WindowProcedure(data->context, window_handle, message, w_param, l_param))
return 0;
}
break;
}
// All unhandled messages go to DefWindowProc.
return DefWindowProc(window_handle, message, w_param, l_param);
}
static HWND InitializeWindow(HINSTANCE instance_handle, const std::wstring& name, int& inout_width, int& inout_height, bool allow_resize)
{
// Fill out the window class struct.
WNDCLASSW window_class;
window_class.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
window_class.lpfnWndProc = &WindowProcedureHandler; // Attach our local event handler.
window_class.cbClsExtra = 0;
window_class.cbWndExtra = 0;
window_class.hInstance = instance_handle;
window_class.hIcon = LoadIcon(nullptr, IDI_WINLOGO);
window_class.hCursor = LoadCursor(nullptr, IDC_ARROW);
window_class.hbrBackground = nullptr;
window_class.lpszMenuName = nullptr;
window_class.lpszClassName = name.data();
if (!RegisterClassW(&window_class))
{
DisplayError(NULL, "Could not register window class.");
return nullptr;
}
HWND window_handle = CreateWindowExW(WS_EX_APPWINDOW | WS_EX_WINDOWEDGE,
name.data(), // Window class name.
name.data(), WS_CLIPSIBLINGS | WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW, 0, 0, // Window position.
0, 0, // Window size.
nullptr, nullptr, instance_handle, nullptr);
if (!window_handle)
{
DisplayError(NULL, "Could not create window.");
return nullptr;
}
UINT window_dpi = GetWindowDpi(window_handle);
inout_width = (inout_width * (int)window_dpi) / USER_DEFAULT_SCREEN_DPI;
inout_height = (inout_height * (int)window_dpi) / USER_DEFAULT_SCREEN_DPI;
DWORD style = (allow_resize ? WS_OVERLAPPEDWINDOW : (WS_OVERLAPPEDWINDOW & ~WS_SIZEBOX & ~WS_MAXIMIZEBOX));
DWORD extended_style = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
// Adjust the window size to take the edges into account.
RECT window_rect;
window_rect.top = 0;
window_rect.left = 0;
window_rect.right = inout_width;
window_rect.bottom = inout_height;
if (has_dpi_support)
procAdjustWindowRectExForDpi(&window_rect, style, FALSE, extended_style, window_dpi);
else
AdjustWindowRectEx(&window_rect, style, FALSE, extended_style);
SetWindowLong(window_handle, GWL_EXSTYLE, extended_style);
SetWindowLong(window_handle, GWL_STYLE, style);
// Resize the window.
SetWindowPos(window_handle, HWND_TOP, 0, 0, window_rect.right - window_rect.left, window_rect.bottom - window_rect.top, SWP_NOACTIVATE);
return window_handle;
}
static bool AttachToNative(HWND window_handle, HDC& out_device_context, HGLRC& out_render_context)
{
HDC device_context = GetDC(window_handle);
if (!device_context)
{
DisplayError(window_handle, "Could not get device context.");
return false;
}
PIXELFORMATDESCRIPTOR pixel_format_descriptor = {};
pixel_format_descriptor.nSize = sizeof(PIXELFORMATDESCRIPTOR);
pixel_format_descriptor.nVersion = 1;
pixel_format_descriptor.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
pixel_format_descriptor.iPixelType = PFD_TYPE_RGBA;
pixel_format_descriptor.cColorBits = 32;
pixel_format_descriptor.cRedBits = 8;
pixel_format_descriptor.cGreenBits = 8;
pixel_format_descriptor.cBlueBits = 8;
pixel_format_descriptor.cAlphaBits = 8;
pixel_format_descriptor.cDepthBits = 24;
pixel_format_descriptor.cStencilBits = 8;
int pixel_format = ChoosePixelFormat(device_context, &pixel_format_descriptor);
if (!pixel_format)
{
DisplayError(window_handle, "Could not choose 32-bit pixel format.");
return false;
}
if (!SetPixelFormat(device_context, pixel_format, &pixel_format_descriptor))
{
DisplayError(window_handle, "Could not set pixel format.");
return false;
}
HGLRC render_context = wglCreateContext(device_context);
if (!render_context)
{
DisplayError(window_handle, "Could not create OpenGL rendering context.");
return false;
}
// Activate the rendering context.
if (!wglMakeCurrent(device_context, render_context))
{
DisplayError(window_handle, "Unable to make rendering context current.");
return false;
}
out_device_context = device_context;
out_render_context = render_context;
return true;
}
static void DetachFromNative(HWND window_handle, HDC device_context, HGLRC render_context)
{
// Shutdown OpenGL
if (render_context)
{
wglMakeCurrent(nullptr, nullptr);
wglDeleteContext(render_context);
}
if (device_context)
{
ReleaseDC(window_handle, device_context);
}
}