forked from OpenRA/OpenRA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSdl2PlatformWindow.cs
545 lines (469 loc) · 16.9 KB
/
Sdl2PlatformWindow.cs
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#region Copyright & License Information
/*
* Copyright 2007-2020 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.Linq;
using System.Runtime.InteropServices;
using OpenRA.Primitives;
using SDL2;
namespace OpenRA.Platforms.Default
{
sealed class Sdl2PlatformWindow : ThreadAffine, IPlatformWindow
{
readonly IGraphicsContext context;
readonly Sdl2Input input;
public IGraphicsContext Context { get { return context; } }
readonly IntPtr window;
bool disposed;
readonly object syncObject = new object();
Size windowSize;
Size surfaceSize;
float windowScale = 1f;
int2? lockedMousePosition;
float scaleModifier;
readonly GLProfile profile;
readonly GLProfile[] supportedProfiles;
internal IntPtr Window
{
get
{
lock (syncObject)
return window;
}
}
public Size NativeWindowSize
{
get
{
lock (syncObject)
return windowSize;
}
}
public Size EffectiveWindowSize
{
get
{
lock (syncObject)
return new Size((int)(windowSize.Width / scaleModifier), (int)(windowSize.Height / scaleModifier));
}
}
public float NativeWindowScale
{
get
{
lock (syncObject)
return windowScale;
}
}
public float EffectiveWindowScale
{
get
{
lock (syncObject)
return windowScale * scaleModifier;
}
}
public Size SurfaceSize
{
get
{
lock (syncObject)
return surfaceSize;
}
}
public int CurrentDisplay
{
get
{
return SDL.SDL_GetWindowDisplayIndex(window);
}
}
public int DisplayCount
{
get
{
return SDL.SDL_GetNumVideoDisplays();
}
}
public bool HasInputFocus { get; internal set; }
public GLProfile GLProfile
{
get
{
lock (syncObject)
return profile;
}
}
public GLProfile[] SupportedGLProfiles
{
get
{
lock (syncObject)
return supportedProfiles;
}
}
public event Action<float, float, float, float> OnWindowScaleChanged = (oldNative, oldEffective, newNative, newEffective) => { };
[DllImport("user32.dll")]
static extern bool SetProcessDPIAware();
public Sdl2PlatformWindow(Size requestEffectiveWindowSize, WindowMode windowMode,
float scaleModifier, int batchSize, int videoDisplay, GLProfile requestProfile, bool enableLegacyGL)
{
// Lock the Window/Surface properties until initialization is complete
lock (syncObject)
{
this.scaleModifier = scaleModifier;
// Disable legacy scaling on Windows
if (Platform.CurrentPlatform == PlatformType.Windows)
SetProcessDPIAware();
// Decide which OpenGL profile to use.
// Prefer standard GL over GLES provided by the native driver
var testProfiles = new List<GLProfile> { GLProfile.ANGLE, GLProfile.Modern, GLProfile.Embedded };
if (enableLegacyGL)
testProfiles.Add(GLProfile.Legacy);
supportedProfiles = testProfiles
.Where(CanCreateGLWindow)
.ToArray();
if (!supportedProfiles.Any())
throw new InvalidOperationException("No supported OpenGL profiles were found.");
profile = supportedProfiles.Contains(requestProfile) ? requestProfile : supportedProfiles.First();
// Note: This must be called after the CanCreateGLWindow checks above,
// which needs to create and destroy its own SDL contexts as a workaround for specific buggy drivers
SDL.SDL_Init(SDL.SDL_INIT_VIDEO);
SetSDLAttributes(profile);
Console.WriteLine("Using SDL 2 with OpenGL ({0}) renderer", profile);
if (videoDisplay < 0 || videoDisplay >= DisplayCount)
videoDisplay = 0;
SDL.SDL_GetCurrentDisplayMode(videoDisplay, out var display);
// Windows and Linux define window sizes in native pixel units.
// Query the display/dpi scale so we can convert our requested effective size to pixels.
// This is not necessary on macOS, which defines window sizes in effective units ("points").
if (Platform.CurrentPlatform == PlatformType.Windows)
{
// Launch the game with OPENRA_DISPLAY_SCALE to force a specific scaling factor
// Otherwise fall back to Windows's DPI configuration
var scaleVariable = Environment.GetEnvironmentVariable("OPENRA_DISPLAY_SCALE");
if (scaleVariable == null || !float.TryParse(scaleVariable, NumberStyles.Float, NumberFormatInfo.InvariantInfo, out windowScale) || windowScale <= 0)
if (SDL.SDL_GetDisplayDPI(videoDisplay, out var ddpi, out _, out _) == 0)
windowScale = ddpi / 96;
}
else if (Platform.CurrentPlatform == PlatformType.Linux)
{
// Launch the game with OPENRA_DISPLAY_SCALE to force a specific scaling factor
// Otherwise fall back to GDK_SCALE or parsing the x11 DPI configuration
var scaleVariable = Environment.GetEnvironmentVariable("OPENRA_DISPLAY_SCALE") ?? Environment.GetEnvironmentVariable("GDK_SCALE");
if (scaleVariable == null || !float.TryParse(scaleVariable, NumberStyles.Float, NumberFormatInfo.InvariantInfo, out windowScale) || windowScale <= 0)
{
// Attempt to automatically detect DPI
try
{
var psi = new ProcessStartInfo("/usr/bin/xrdb", "-query");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
var p = Process.Start(psi);
var lines = p.StandardOutput.ReadToEnd().Split('\n');
foreach (var line in lines)
if (line.StartsWith("Xft.dpi") && int.TryParse(line.Substring(8), out var dpi))
windowScale = dpi / 96f;
}
catch { }
}
}
Console.WriteLine("Desktop resolution: {0}x{1}", display.w, display.h);
if (requestEffectiveWindowSize.Width == 0 && requestEffectiveWindowSize.Height == 0)
{
Console.WriteLine("No custom resolution provided, using desktop resolution");
surfaceSize = windowSize = new Size(display.w, display.h);
}
else
surfaceSize = windowSize = new Size((int)(requestEffectiveWindowSize.Width * windowScale), (int)(requestEffectiveWindowSize.Height * windowScale));
Console.WriteLine("Using resolution: {0}x{1}", windowSize.Width, windowSize.Height);
var windowFlags = SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL | SDL.SDL_WindowFlags.SDL_WINDOW_ALLOW_HIGHDPI;
// HiDPI doesn't work properly on OSX with (legacy) fullscreen mode
if (Platform.CurrentPlatform == PlatformType.OSX && windowMode == WindowMode.Fullscreen)
SDL.SDL_SetHint(SDL.SDL_HINT_VIDEO_HIGHDPI_DISABLED, "1");
window = SDL.SDL_CreateWindow("OpenRA", SDL.SDL_WINDOWPOS_CENTERED_DISPLAY(videoDisplay), SDL.SDL_WINDOWPOS_CENTERED_DISPLAY(videoDisplay),
windowSize.Width, windowSize.Height, windowFlags);
// Work around an issue in macOS's GL backend where the window remains permanently black
// (if dark mode is enabled) unless we drain the event queue before initializing GL
if (Platform.CurrentPlatform == PlatformType.OSX)
{
while (SDL.SDL_PollEvent(out var e) != 0)
{
// We can safely ignore all mouse/keyboard events and window size changes
// (these will be caught in the window setup below), but do need to process focus
if (e.type == SDL.SDL_EventType.SDL_WINDOWEVENT)
{
switch (e.window.windowEvent)
{
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_LOST:
HasInputFocus = false;
break;
case SDL.SDL_WindowEventID.SDL_WINDOWEVENT_FOCUS_GAINED:
HasInputFocus = true;
break;
}
}
}
}
// Enable high resolution rendering for Retina displays
if (Platform.CurrentPlatform == PlatformType.OSX)
{
// OSX defines the window size in "points", with a device-dependent number of pixels per point.
// The window scale is simply the ratio of GL pixels / window points.
SDL.SDL_GL_GetDrawableSize(Window, out var width, out var height);
surfaceSize = new Size(width, height);
windowScale = width * 1f / windowSize.Width;
}
else
windowSize = new Size((int)(surfaceSize.Width / windowScale), (int)(surfaceSize.Height / windowScale));
Console.WriteLine("Using window scale {0:F2}", windowScale);
if (Game.Settings.Game.LockMouseWindow)
GrabWindowMouseFocus();
else
ReleaseWindowMouseFocus();
if (windowMode == WindowMode.Fullscreen)
{
SDL.SDL_SetWindowFullscreen(Window, (uint)SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN);
// Fullscreen mode on OSX will ignore the configured display resolution
// and instead always picks an arbitrary scaled resolution choice that may
// not match the window size, leading to graphical and input issues.
// We work around this by force disabling HiDPI and resetting the window and
// surface sizes to match the size that is forced by SDL.
// This is usually not what the player wants, but is the best we can consistently do.
if (Platform.CurrentPlatform == PlatformType.OSX)
{
SDL.SDL_GetWindowSize(Window, out var width, out var height);
windowSize = surfaceSize = new Size(width, height);
windowScale = 1;
}
}
else if (windowMode == WindowMode.PseudoFullscreen)
{
SDL.SDL_SetWindowFullscreen(Window, (uint)SDL.SDL_WindowFlags.SDL_WINDOW_FULLSCREEN_DESKTOP);
SDL.SDL_SetHint(SDL.SDL_HINT_VIDEO_MINIMIZE_ON_FOCUS_LOSS, "0");
}
}
// Run graphics rendering on a dedicated thread.
// The calling thread will then have more time to process other tasks, since rendering happens in parallel.
// If the calling thread is the main game thread, this means it can run more logic and render ticks.
// This is disabled on Windows because it breaks the ability to minimize/restore the window from the taskbar for reasons that we dont understand.
var threadedRenderer = Platform.CurrentPlatform != PlatformType.Windows || !Game.Settings.Graphics.DisableWindowsRenderThread;
if (!threadedRenderer)
{
var ctx = new Sdl2GraphicsContext(this);
ctx.InitializeOpenGL();
context = ctx;
}
else
context = new ThreadedGraphicsContext(new Sdl2GraphicsContext(this), batchSize);
context.SetVSyncEnabled(Game.Settings.Graphics.VSync);
SDL.SDL_SetModState(SDL.SDL_Keymod.KMOD_NONE);
input = new Sdl2Input();
}
byte[] DoublePixelData(byte[] data, Size size)
{
var scaledData = new byte[4 * data.Length];
for (var y = 0; y < size.Height; y++)
{
for (var x = 0; x < size.Width; x++)
{
var a = 4 * (y * size.Width + x);
var b = 8 * (2 * y * size.Width + x);
var c = b + 8 * size.Width;
for (var i = 0; i < 4; i++)
scaledData[b + i] = scaledData[b + 4 + i] = scaledData[c + i] = scaledData[c + 4 + i] = data[a + i];
}
}
return scaledData;
}
public IHardwareCursor CreateHardwareCursor(string name, Size size, byte[] data, int2 hotspot, bool pixelDouble)
{
VerifyThreadAffinity();
try
{
// Pixel double the cursor on non-OSX if the window scale is large enough
// OSX does this for us automatically
if (Platform.CurrentPlatform != PlatformType.OSX && NativeWindowScale > 1.5f)
{
data = DoublePixelData(data, size);
size = new Size(2 * size.Width, 2 * size.Height);
hotspot *= 2;
}
// Scale all but the "default" cursor if requested by the player
if (pixelDouble)
{
data = DoublePixelData(data, size);
size = new Size(2 * size.Width, 2 * size.Height);
hotspot *= 2;
}
return new Sdl2HardwareCursor(size, data, hotspot);
}
catch (Exception ex)
{
throw new Sdl2HardwareCursorException("Failed to create hardware cursor `{0}` - {1}".F(name, ex.Message), ex);
}
}
public void SetHardwareCursor(IHardwareCursor cursor)
{
VerifyThreadAffinity();
var c = cursor as Sdl2HardwareCursor;
if (c == null)
SDL.SDL_ShowCursor((int)SDL.SDL_bool.SDL_FALSE);
else
{
SDL.SDL_ShowCursor((int)SDL.SDL_bool.SDL_TRUE);
SDL.SDL_SetCursor(c.Cursor);
}
}
public void SetRelativeMouseMode(bool mode)
{
if (mode)
{
SDL.SDL_GetMouseState(out var x, out var y);
lockedMousePosition = new int2(x, y);
}
else
{
if (lockedMousePosition.HasValue)
SDL.SDL_WarpMouseInWindow(window, lockedMousePosition.Value.X, lockedMousePosition.Value.Y);
lockedMousePosition = null;
}
}
internal void WindowSizeChanged()
{
// The ratio between pixels and points can change when moving between displays in OSX
// We need to recalculate our scale to account for the potential change in the actual rendered area
if (Platform.CurrentPlatform == PlatformType.OSX)
{
SDL.SDL_GL_GetDrawableSize(Window, out var width, out var height);
if (width != SurfaceSize.Width || height != SurfaceSize.Height)
{
float oldScale;
lock (syncObject)
{
oldScale = windowScale;
surfaceSize = new Size(width, height);
windowScale = width * 1f / windowSize.Width;
}
OnWindowScaleChanged(oldScale, oldScale * scaleModifier, windowScale, windowScale * scaleModifier);
}
}
}
public void Dispose()
{
if (disposed)
return;
disposed = true;
context?.Dispose();
if (Window != IntPtr.Zero)
SDL.SDL_DestroyWindow(Window);
SDL.SDL_Quit();
}
public void GrabWindowMouseFocus()
{
VerifyThreadAffinity();
SDL.SDL_SetWindowGrab(Window, SDL.SDL_bool.SDL_TRUE);
}
public void ReleaseWindowMouseFocus()
{
VerifyThreadAffinity();
SDL.SDL_SetWindowGrab(Window, SDL.SDL_bool.SDL_FALSE);
}
public void PumpInput(IInputHandler inputHandler)
{
VerifyThreadAffinity();
input.PumpInput(this, inputHandler, lockedMousePosition);
if (lockedMousePosition.HasValue)
SDL.SDL_WarpMouseInWindow(window, lockedMousePosition.Value.X, lockedMousePosition.Value.Y);
}
public string GetClipboardText()
{
VerifyThreadAffinity();
return input.GetClipboardText();
}
public bool SetClipboardText(string text)
{
VerifyThreadAffinity();
return input.SetClipboardText(text);
}
static void SetSDLAttributes(GLProfile profile)
{
SDL.SDL_GL_ResetAttributes();
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_DOUBLEBUFFER, 1);
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_RED_SIZE, 8);
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_GREEN_SIZE, 8);
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_BLUE_SIZE, 8);
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_ALPHA_SIZE, 0);
var useAngle = profile == GLProfile.ANGLE ? "1" : "0";
SDL.SDL_SetHint("SDL_OPENGL_ES_DRIVER", useAngle);
switch (profile)
{
case GLProfile.Modern:
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MINOR_VERSION, 2);
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_PROFILE_MASK, (int)SDL.SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_CORE);
break;
case GLProfile.ANGLE:
case GLProfile.Embedded:
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MINOR_VERSION, 0);
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_PROFILE_MASK, (int)SDL.SDL_GLprofile.SDL_GL_CONTEXT_PROFILE_ES);
break;
case GLProfile.Legacy:
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL.SDL_GL_SetAttribute(SDL.SDL_GLattr.SDL_GL_CONTEXT_MINOR_VERSION, 1);
break;
}
}
static bool CanCreateGLWindow(GLProfile profile)
{
// Implementation inspired by TestIndividualGLVersion from Veldrid
// Need to create and destroy its own SDL contexts as a workaround for specific buggy drivers
SDL.SDL_Init(SDL.SDL_INIT_VIDEO);
SetSDLAttributes(profile);
var flags = SDL.SDL_WindowFlags.SDL_WINDOW_HIDDEN | SDL.SDL_WindowFlags.SDL_WINDOW_OPENGL;
var window = SDL.SDL_CreateWindow("", 0, 0, 1, 1, flags);
if (window == IntPtr.Zero || !string.IsNullOrEmpty(SDL.SDL_GetError()))
{
SDL.SDL_ClearError();
SDL.SDL_Quit();
return false;
}
var context = SDL.SDL_GL_CreateContext(window);
if (context == IntPtr.Zero || SDL.SDL_GL_MakeCurrent(window, context) < 0)
{
SDL.SDL_ClearError();
SDL.SDL_DestroyWindow(window);
SDL.SDL_Quit();
return false;
}
// Distinguish between ANGLE and native GLES
var success = true;
if (profile == GLProfile.ANGLE || profile == GLProfile.Embedded)
{
var isAngle = SDL.SDL_GL_ExtensionSupported("GL_ANGLE_texture_usage") == SDL.SDL_bool.SDL_TRUE;
success = isAngle ^ (profile != GLProfile.ANGLE);
}
SDL.SDL_GL_DeleteContext(context);
SDL.SDL_DestroyWindow(window);
SDL.SDL_Quit();
return success;
}
public void SetScaleModifier(float scale)
{
var oldScaleModifier = scaleModifier;
scaleModifier = scale;
OnWindowScaleChanged(windowScale, windowScale * oldScaleModifier, windowScale, windowScale * scaleModifier);
}
}
}