Opentk/Source/OpenTK/Platform/Linux/LinuxFactory.cs

249 lines
8.1 KiB
C#
Raw Normal View History

2014-06-23 20:22:04 +00:00
#region License
//
// LinuxFactory.cs
//
// Author:
// Stefanos A. <stapostol@gmail.com>
//
// Copyright (c) 2006-2014 Stefanos Apostolopoulos
//
// 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.
//
#endregion
using System;
using System.Diagnostics;
using System.IO;
2014-06-23 20:22:04 +00:00
using System.Runtime.InteropServices;
using OpenTK.Graphics;
using OpenTK.Input;
2014-06-24 17:27:38 +00:00
using OpenTK.Platform.Egl;
2014-06-23 20:22:04 +00:00
namespace OpenTK.Platform.Linux
{
2014-06-24 17:27:38 +00:00
using Egl = OpenTK.Platform.Egl.Egl;
2014-06-23 20:22:04 +00:00
// Linux KMS platform
class LinuxFactory : PlatformFactoryBase
{
2014-06-26 20:52:17 +00:00
int _fd;
2014-06-25 07:01:35 +00:00
IntPtr gbm_device;
2014-06-26 20:52:17 +00:00
IntPtr egl_display;
2014-06-23 20:22:04 +00:00
2014-06-24 17:27:38 +00:00
IJoystickDriver2 JoystickDriver;
LinuxInput MouseKeyboardDriver;
2014-06-24 17:27:38 +00:00
const string gpu_path = "/dev/dri"; // card0, card1, ...
2014-06-23 20:22:04 +00:00
public LinuxFactory()
2014-06-26 20:52:17 +00:00
{
Debug.Print("[KMS] Using Linux/KMS backend.");
}
#region Private Members
int gpu_fd
{
get
{
lock (this)
{
if (_fd == 0)
{
_fd = CreateDisplay(out gbm_device, out egl_display);
}
return _fd;
}
}
}
static int CreateDisplay(out IntPtr gbm_device, out IntPtr egl_display)
2014-06-24 17:27:38 +00:00
{
// Query all GPUs until we find one that has a connected display.
// This is necessary in multi-gpu systems, where only one GPU
// can output a signal.
// Todo: allow OpenTK to drive multiple GPUs
// Todo: allow OpenTK to run on an offscreen GPU
// Todo: allow the user to pick a GPU
2014-06-26 20:52:17 +00:00
int fd = 0;
gbm_device = IntPtr.Zero;
egl_display = IntPtr.Zero;
var files = Directory.GetFiles(gpu_path);
foreach (var gpu in files)
{
if (Path.GetFileName(gpu).StartsWith("card"))
{
2014-06-26 20:52:17 +00:00
int test_fd = SetupDisplay(gpu, out gbm_device, out egl_display);
if (test_fd >= 0)
{
try
{
if (LinuxDisplayDriver.QueryDisplays(test_fd, null))
{
fd = test_fd;
break;
}
}
catch (Exception e)
{
Debug.WriteLine(e.ToString());
}
Debug.Print("[KMS] GPU '{0}' is not connected, skipping.", gpu);
Libc.close(test_fd);
}
}
}
if (fd == 0)
{
Debug.Print("[Error] No valid GPU found, bailing out.");
throw new PlatformNotSupportedException();
}
2014-06-24 17:27:38 +00:00
2014-06-26 20:52:17 +00:00
return fd;
}
2014-06-24 17:27:38 +00:00
2014-06-26 20:52:17 +00:00
static int SetupDisplay(string gpu, out IntPtr gbm_device, out IntPtr egl_display)
2014-06-23 20:22:04 +00:00
{
Debug.Print("[KMS] Attempting to use gpu '{0}'.", gpu);
2014-06-26 20:52:17 +00:00
gbm_device = IntPtr.Zero;
egl_display = IntPtr.Zero;
int fd = Libc.open(gpu, OpenFlags.ReadWrite | OpenFlags.CloseOnExec);
2014-06-23 20:22:04 +00:00
if (fd < 0)
{
Debug.Print("[KMS] Failed to open gpu");
return fd;
2014-06-23 20:22:04 +00:00
}
2014-06-24 17:27:38 +00:00
Debug.Print("[KMS] GPU '{0}' opened as fd:{1}", gpu, fd);
2014-06-23 20:22:04 +00:00
2014-06-25 07:01:35 +00:00
gbm_device = Gbm.CreateDevice(fd);
if (gbm_device == IntPtr.Zero)
2014-06-23 20:22:04 +00:00
{
2014-06-24 17:27:38 +00:00
throw new NotSupportedException("[KMS] Failed to create GBM device");
2014-06-23 20:22:04 +00:00
}
2014-06-25 07:01:35 +00:00
Debug.Print("[KMS] GBM {0:x} created successfully; ", gbm_device);
2014-06-23 20:22:04 +00:00
2014-06-26 20:52:17 +00:00
egl_display = Egl.GetDisplay(gbm_device);
if (egl_display == IntPtr.Zero)
2014-06-23 20:22:04 +00:00
{
2014-06-24 17:27:38 +00:00
throw new NotSupportedException("[KMS] Failed to create EGL display");
2014-06-23 20:22:04 +00:00
}
2014-06-26 20:52:17 +00:00
Debug.Print("[KMS] EGL display {0:x} created successfully", egl_display);
2014-06-23 20:22:04 +00:00
int major, minor;
2014-06-26 20:52:17 +00:00
if (!Egl.Initialize(egl_display, out major, out minor))
2014-06-23 20:22:04 +00:00
{
ErrorCode error = Egl.GetError();
2014-06-24 17:27:38 +00:00
throw new NotSupportedException("[KMS] Failed to initialize EGL display. Error code: " + error);
2014-06-23 20:22:04 +00:00
}
2014-06-26 20:52:17 +00:00
Debug.Print("[KMS] EGL {0}.{1} initialized successfully on display {2:x}", major, minor, egl_display);
return fd;
2014-06-23 20:22:04 +00:00
}
2014-06-24 17:27:38 +00:00
#endregion
#region Protected Members
protected override void Dispose(bool manual)
{
2014-06-26 20:52:17 +00:00
if (egl_display != IntPtr.Zero)
{
Debug.Print("[KMS] Terminating EGL.");
2014-06-26 20:52:17 +00:00
Egl.Terminate(egl_display);
egl_display = IntPtr.Zero;
}
if (gbm_device != IntPtr.Zero)
{
Debug.Print("[KMS] Destroying GBM device.");
Gbm.DestroyDevice(gbm_device);
gbm_device = IntPtr.Zero;
}
2014-06-26 20:52:17 +00:00
if (_fd >= 0)
{
Debug.Print("[KMS] Closing GPU fd.");
2014-06-26 20:52:17 +00:00
Libc.close(_fd);
}
base.Dispose(manual);
}
#endregion
2014-06-24 17:27:38 +00:00
#region IPlatformFactory Members
public override INativeWindow CreateNativeWindow(int x, int y, int width, int height, string title, GraphicsMode mode, GameWindowFlags options, DisplayDevice display_device)
2014-06-23 20:22:04 +00:00
{
2014-06-26 20:52:17 +00:00
return new LinuxNativeWindow(egl_display, gbm_device, gpu_fd, x, y, width, height, title, mode, options, display_device);
2014-06-23 20:22:04 +00:00
}
public override IDisplayDeviceDriver CreateDisplayDeviceDriver()
{
2014-06-26 20:52:17 +00:00
return new LinuxDisplayDriver(gpu_fd);
2014-06-23 20:22:04 +00:00
}
public override IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, bool directRendering, int major, int minor, GraphicsContextFlags flags)
{
return new LinuxGraphicsContext(mode, (LinuxWindowInfo)window, shareContext, major, minor, flags);
2014-06-23 20:22:04 +00:00
}
public override GraphicsContext.GetCurrentContextDelegate CreateGetCurrentGraphicsContext()
{
2014-06-24 17:27:38 +00:00
return (GraphicsContext.GetCurrentContextDelegate)delegate
{
return new ContextHandle(Egl.GetCurrentContext());
};
2014-06-23 20:22:04 +00:00
}
public override IKeyboardDriver2 CreateKeyboardDriver()
{
2014-06-26 20:52:17 +00:00
lock (this)
{
MouseKeyboardDriver = MouseKeyboardDriver ?? new LinuxInput();
return MouseKeyboardDriver;
2014-06-26 20:52:17 +00:00
}
2014-06-23 20:22:04 +00:00
}
public override IMouseDriver2 CreateMouseDriver()
{
lock (this)
{
MouseKeyboardDriver = MouseKeyboardDriver ?? new LinuxInput();
return MouseKeyboardDriver;
}
2014-06-23 20:22:04 +00:00
}
public override IJoystickDriver2 CreateJoystickDriver()
{
2014-06-24 17:27:38 +00:00
lock (this)
{
JoystickDriver = JoystickDriver ?? new LinuxJoystick();
return JoystickDriver;
}
2014-06-23 20:22:04 +00:00
}
2014-06-24 17:27:38 +00:00
#endregion
2014-06-23 20:22:04 +00:00
}
}