2014-06-24 17:27:38 +00:00
|
|
|
#region License
|
|
|
|
//
|
|
|
|
// LinuxDisplayDriver.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.Collections.Generic;
|
|
|
|
using System.Diagnostics;
|
|
|
|
using OpenTK;
|
|
|
|
using OpenTK.Graphics;
|
|
|
|
|
|
|
|
namespace OpenTK.Platform.Linux
|
|
|
|
{
|
2014-06-26 13:40:00 +00:00
|
|
|
// Stores platform-specific information about a display
|
|
|
|
class LinuxDisplay
|
2014-06-24 17:27:38 +00:00
|
|
|
{
|
2014-06-26 13:40:00 +00:00
|
|
|
public IntPtr Connector;
|
|
|
|
public IntPtr Crtc;
|
|
|
|
public IntPtr Encoder;
|
|
|
|
|
|
|
|
unsafe public ModeConnector* pConnector { get { return (ModeConnector*)Connector; } }
|
|
|
|
unsafe public ModeCrtc* pCrtc { get { return (ModeCrtc*)Crtc; } }
|
|
|
|
unsafe public ModeEncoder* pEncoder { get { return (ModeEncoder*)Encoder; } }
|
|
|
|
/*
|
|
|
|
public ModeInfo Mode
|
2014-06-24 17:27:38 +00:00
|
|
|
{
|
2014-06-26 13:40:00 +00:00
|
|
|
get
|
2014-06-24 17:27:38 +00:00
|
|
|
{
|
2014-06-26 13:40:00 +00:00
|
|
|
if (Crtc == IntPtr.Zero)
|
|
|
|
throw new InvalidOperationException();
|
2014-06-24 17:27:38 +00:00
|
|
|
|
2014-06-26 13:40:00 +00:00
|
|
|
unsafe
|
|
|
|
{
|
|
|
|
return pCrtc->mode;
|
2014-06-24 17:27:38 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-26 13:40:00 +00:00
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
public ModeInfo OriginalMode;
|
2014-06-24 17:27:38 +00:00
|
|
|
|
2014-06-26 13:40:00 +00:00
|
|
|
public int Id
|
|
|
|
{
|
|
|
|
get
|
2014-06-24 17:27:38 +00:00
|
|
|
{
|
2014-06-26 13:40:00 +00:00
|
|
|
if (Crtc == IntPtr.Zero)
|
|
|
|
throw new InvalidOperationException();
|
2014-06-24 17:27:38 +00:00
|
|
|
|
2014-06-26 13:40:00 +00:00
|
|
|
unsafe
|
|
|
|
{
|
|
|
|
return (int)pCrtc->crtc_id;
|
2014-06-24 17:27:38 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-26 13:40:00 +00:00
|
|
|
}
|
2014-06-24 17:27:38 +00:00
|
|
|
|
2014-06-26 13:40:00 +00:00
|
|
|
public LinuxDisplay(IntPtr c, IntPtr e, IntPtr r)
|
|
|
|
{
|
|
|
|
Connector = c;
|
|
|
|
Encoder = e;
|
|
|
|
Crtc = r;
|
|
|
|
unsafe
|
2014-06-24 17:27:38 +00:00
|
|
|
{
|
2014-06-26 13:40:00 +00:00
|
|
|
OriginalMode = pCrtc->mode; // in case we change resolution later on
|
2014-06-24 17:27:38 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-26 13:40:00 +00:00
|
|
|
}
|
2014-06-24 17:27:38 +00:00
|
|
|
|
2014-06-26 13:40:00 +00:00
|
|
|
class LinuxDisplayDriver : DisplayDeviceBase
|
|
|
|
{
|
2014-06-24 17:27:38 +00:00
|
|
|
readonly int FD;
|
|
|
|
readonly Dictionary<int, int> DisplayIds =
|
|
|
|
new Dictionary<int, int>();
|
|
|
|
|
|
|
|
public LinuxDisplayDriver(int fd)
|
|
|
|
{
|
2014-06-26 13:40:00 +00:00
|
|
|
Debug.Print("[KMS] Creating LinuxDisplayDriver for fd:{0}", fd);
|
|
|
|
Debug.Indent();
|
|
|
|
try
|
|
|
|
{
|
|
|
|
FD = fd;
|
2014-06-26 19:46:04 +00:00
|
|
|
UpdateDisplays(fd);
|
2014-06-26 13:40:00 +00:00
|
|
|
}
|
|
|
|
finally
|
|
|
|
{
|
|
|
|
Debug.Unindent();
|
|
|
|
}
|
2014-06-24 17:27:38 +00:00
|
|
|
}
|
|
|
|
|
2014-06-26 19:46:04 +00:00
|
|
|
/// \internal
|
|
|
|
/// <summary>
|
|
|
|
/// Queries the specified GPU for connected displays and, optionally,
|
|
|
|
/// returns the list of displays.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns><c>true</c>, if at least one display is connected, <c>false</c> otherwise.</returns>
|
|
|
|
/// <param name="fd">The fd for the GPU to query, obtained through open("/dev/dri/card0").</param>
|
|
|
|
/// <param name="displays">
|
|
|
|
/// If not null, this will contain a list <see cref="LinuxDisplay"/> instances,
|
|
|
|
/// one for each connected display.
|
|
|
|
/// </param>
|
|
|
|
internal static bool QueryDisplays(int fd, List<LinuxDisplay> displays)
|
2014-06-24 17:27:38 +00:00
|
|
|
{
|
|
|
|
unsafe
|
|
|
|
{
|
2014-06-26 19:46:04 +00:00
|
|
|
bool has_displays = false;
|
|
|
|
if (displays != null)
|
2014-06-24 17:27:38 +00:00
|
|
|
{
|
2014-06-26 19:46:04 +00:00
|
|
|
displays.Clear();
|
|
|
|
}
|
2014-06-24 17:27:38 +00:00
|
|
|
|
2014-06-26 19:46:04 +00:00
|
|
|
ModeRes* resources = (ModeRes*)Drm.ModeGetResources(fd);
|
|
|
|
if (resources == null)
|
|
|
|
{
|
|
|
|
Debug.Print("[KMS] Drm.ModeGetResources failed.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
Debug.Print("[KMS] DRM found {0} connectors", resources->count_connectors);
|
2014-06-24 17:27:38 +00:00
|
|
|
|
2014-06-26 19:46:04 +00:00
|
|
|
// Search for a valid connector
|
|
|
|
ModeConnector* connector = null;
|
|
|
|
for (int i = 0; i < resources->count_connectors; i++)
|
|
|
|
{
|
|
|
|
connector = (ModeConnector*)Drm.ModeGetConnector(fd,
|
|
|
|
*(resources->connectors + i));
|
|
|
|
if (connector != null)
|
2014-06-24 17:27:38 +00:00
|
|
|
{
|
2014-06-26 19:46:04 +00:00
|
|
|
bool success = false;
|
|
|
|
LinuxDisplay display = null;
|
|
|
|
try
|
2014-06-24 17:27:38 +00:00
|
|
|
{
|
|
|
|
if (connector->connection == ModeConnection.Connected &&
|
2014-06-26 19:46:04 +00:00
|
|
|
connector->count_modes > 0)
|
2014-06-24 17:27:38 +00:00
|
|
|
{
|
2014-06-26 19:46:04 +00:00
|
|
|
success = QueryDisplay(fd, connector, out display);
|
|
|
|
has_displays |= success;
|
2014-06-24 17:27:38 +00:00
|
|
|
}
|
|
|
|
}
|
2014-06-26 19:46:04 +00:00
|
|
|
catch (Exception e)
|
|
|
|
{
|
|
|
|
Debug.Print("[KMS] Failed to add display. Error: {0}", e);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (success && displays != null)
|
|
|
|
{
|
|
|
|
displays.Add(display);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Drm.ModeFreeConnector((IntPtr)connector);
|
|
|
|
connector = null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return has_displays;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateDisplays(int fd)
|
|
|
|
{
|
|
|
|
unsafe
|
|
|
|
{
|
|
|
|
lock (this)
|
|
|
|
{
|
|
|
|
AvailableDevices.Clear();
|
|
|
|
DisplayIds.Clear();
|
|
|
|
|
|
|
|
List<LinuxDisplay> displays = new List<LinuxDisplay>();
|
|
|
|
if (QueryDisplays(fd, displays))
|
|
|
|
{
|
|
|
|
foreach (LinuxDisplay display in displays)
|
|
|
|
{
|
|
|
|
AddDisplay(display);
|
|
|
|
}
|
2014-06-24 17:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (AvailableDevices.Count == 0)
|
|
|
|
{
|
|
|
|
Debug.Print("[KMS] Failed to find any active displays");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-06-26 19:46:04 +00:00
|
|
|
unsafe static ModeEncoder* GetEncoder(int fd, ModeConnector* c)
|
2014-06-24 17:27:38 +00:00
|
|
|
{
|
|
|
|
ModeEncoder* encoder = null;
|
|
|
|
for (int i = 0; i < c->count_encoders && encoder == null; i++)
|
|
|
|
{
|
|
|
|
ModeEncoder* e = (ModeEncoder*)Drm.ModeGetEncoder(
|
2014-06-26 19:46:04 +00:00
|
|
|
fd, *(c->encoders + i));
|
2014-06-24 17:27:38 +00:00
|
|
|
if (e != null)
|
|
|
|
{
|
|
|
|
if (e->encoder_id == c->encoder_id)
|
|
|
|
{
|
|
|
|
encoder = e;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Drm.ModeFreeEncoder((IntPtr)e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (encoder != null)
|
|
|
|
{
|
|
|
|
Debug.Print("[KMS] Encoder {0} found for connector {1}",
|
|
|
|
encoder->encoder_id, c->connector_id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Debug.Print("[KMS] Failed to find encoder for connector {0}", c->connector_id);
|
|
|
|
}
|
|
|
|
|
2014-06-26 19:46:04 +00:00
|
|
|
return encoder;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe static ModeCrtc* GetCrtc(int fd, ModeEncoder* encoder)
|
|
|
|
{
|
|
|
|
ModeCrtc* crtc = (ModeCrtc*)Drm.ModeGetCrtc(fd, encoder->crtc_id);
|
2014-06-24 17:27:38 +00:00
|
|
|
if (crtc != null)
|
|
|
|
{
|
|
|
|
Debug.Print("[KMS] CRTC {0} found for encoder {1}",
|
|
|
|
encoder->crtc_id, encoder->encoder_id);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Debug.Print("[KMS] Failed to find crtc {0} for encoder {1}",
|
|
|
|
encoder->crtc_id, encoder->encoder_id);
|
|
|
|
}
|
2014-06-26 19:46:04 +00:00
|
|
|
return crtc;
|
|
|
|
}
|
2014-06-24 17:27:38 +00:00
|
|
|
|
2014-06-26 19:46:04 +00:00
|
|
|
unsafe static void GetModes(LinuxDisplay display, DisplayResolution[] modes, out DisplayResolution current)
|
|
|
|
{
|
2014-06-26 13:40:00 +00:00
|
|
|
int mode_count = display.pConnector->count_modes;
|
|
|
|
Debug.Print("[KMS] Display supports {0} modes", mode_count);
|
|
|
|
for (int i = 0; i < mode_count; i++)
|
2014-06-24 17:27:38 +00:00
|
|
|
{
|
2014-06-26 13:40:00 +00:00
|
|
|
ModeInfo* mode = display.pConnector->modes + i;
|
|
|
|
if (mode != null)
|
|
|
|
{
|
|
|
|
Debug.Print("Mode {0}: {1}x{2} @{3}", i,
|
|
|
|
mode->hdisplay, mode->vdisplay, mode->vrefresh);
|
|
|
|
DisplayResolution res = GetDisplayResolution(mode);
|
2014-06-26 19:46:04 +00:00
|
|
|
modes[i] = res;
|
2014-06-26 13:40:00 +00:00
|
|
|
}
|
2014-06-24 17:27:38 +00:00
|
|
|
}
|
|
|
|
|
2014-06-26 19:46:04 +00:00
|
|
|
if (display.pCrtc->mode_valid != 0)
|
|
|
|
{
|
|
|
|
ModeInfo cmode = display.pCrtc->mode;
|
|
|
|
current = GetDisplayResolution(&cmode);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
current = GetDisplayResolution(display.pConnector->modes);
|
|
|
|
}
|
|
|
|
Debug.Print("Current mode: {0}", current.ToString());
|
|
|
|
}
|
|
|
|
|
|
|
|
System.Drawing.Rectangle GetBounds(DisplayResolution current)
|
|
|
|
{
|
2014-06-24 17:27:38 +00:00
|
|
|
// Note: since we are not running a display manager, we are free
|
|
|
|
// to choose the display layout for multiple displays ourselves.
|
|
|
|
// We choose the simplest layout: displays are laid out side-by-side
|
|
|
|
// from left to right. Primary display is the first display we encounter.
|
2014-06-26 19:46:04 +00:00
|
|
|
int x = AvailableDevices.Count == 0 ?
|
|
|
|
0 : AvailableDevices[AvailableDevices.Count - 1].Bounds.Right;
|
|
|
|
int y = 0;
|
|
|
|
|
|
|
|
return new System.Drawing.Rectangle(
|
|
|
|
x, y, current.Width, current.Height);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UpdateDisplayIndices(LinuxDisplay display, DisplayDevice device)
|
|
|
|
{
|
|
|
|
if (!DisplayIds.ContainsKey(display.Id))
|
|
|
|
{
|
|
|
|
Debug.Print("[KMS] Adding display {0} as {1}", display.Id, AvailableDevices.Count);
|
|
|
|
DisplayIds.Add(display.Id, AvailableDevices.Count);
|
|
|
|
}
|
|
|
|
int index = DisplayIds[display.Id];
|
|
|
|
if (index >= AvailableDevices.Count)
|
|
|
|
{
|
|
|
|
AvailableDevices.Add(device);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
AvailableDevices[index] = device;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe static bool QueryDisplay(int fd, ModeConnector* c, out LinuxDisplay display)
|
|
|
|
{
|
|
|
|
display = null;
|
|
|
|
|
|
|
|
// Find corresponding encoder
|
|
|
|
ModeEncoder* encoder = GetEncoder(fd, c);
|
|
|
|
if (encoder == null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
ModeCrtc* crtc = GetCrtc(fd, encoder);
|
|
|
|
if (crtc == null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
display = new LinuxDisplay((IntPtr)c, (IntPtr)encoder, (IntPtr)crtc);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe void AddDisplay(LinuxDisplay display)
|
|
|
|
{
|
|
|
|
DisplayResolution[] modes = new DisplayResolution[display.pConnector->count_modes];
|
|
|
|
DisplayResolution current;
|
|
|
|
GetModes(display, modes, out current);
|
|
|
|
|
2014-06-25 07:01:35 +00:00
|
|
|
bool is_primary = AvailableDevices.Count == 0;
|
|
|
|
DisplayDevice device = new DisplayDevice(current, is_primary,
|
2014-06-26 19:46:04 +00:00
|
|
|
modes, GetBounds(current), display);
|
2014-06-24 17:27:38 +00:00
|
|
|
|
2014-06-26 19:46:04 +00:00
|
|
|
if (is_primary)
|
2014-06-24 17:27:38 +00:00
|
|
|
{
|
|
|
|
Primary = device;
|
|
|
|
}
|
|
|
|
|
2014-06-26 19:46:04 +00:00
|
|
|
UpdateDisplayIndices(display, device);
|
|
|
|
|
2014-06-26 13:40:00 +00:00
|
|
|
Debug.Print("[KMS] Added DisplayDevice {0}", device);
|
2014-06-24 17:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe static DisplayResolution GetDisplayResolution(ModeInfo* mode)
|
|
|
|
{
|
|
|
|
return new DisplayResolution(
|
|
|
|
0, 0,
|
2014-06-26 13:40:00 +00:00
|
|
|
mode->hdisplay, mode->vdisplay,
|
2014-06-24 17:27:38 +00:00
|
|
|
32, // This is actually part of the framebuffer, not the DisplayResolution
|
2014-06-26 13:40:00 +00:00
|
|
|
mode->vrefresh);
|
2014-06-24 17:27:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
unsafe static ModeInfo* GetModeInfo(LinuxDisplay display, DisplayResolution resolution)
|
|
|
|
{
|
2014-06-26 13:40:00 +00:00
|
|
|
for (int i = 0; i < display.pConnector->count_modes; i++)
|
2014-06-24 17:27:38 +00:00
|
|
|
{
|
2014-06-26 13:40:00 +00:00
|
|
|
ModeInfo* mode = display.pConnector->modes + i;
|
2014-06-24 17:27:38 +00:00
|
|
|
if (mode != null &&
|
2014-06-26 13:40:00 +00:00
|
|
|
mode->hdisplay == resolution.Width &&
|
|
|
|
mode->vdisplay == resolution.Height)
|
2014-06-24 17:27:38 +00:00
|
|
|
{
|
|
|
|
return mode;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
#region IDisplayDeviceDriver
|
|
|
|
|
|
|
|
public override bool TryChangeResolution(DisplayDevice device, DisplayResolution resolution)
|
|
|
|
{
|
|
|
|
unsafe
|
|
|
|
{
|
|
|
|
LinuxDisplay display = (LinuxDisplay)device.Id;
|
|
|
|
ModeInfo* mode = GetModeInfo(display, resolution);
|
2014-06-26 13:40:00 +00:00
|
|
|
int connector_id = display.pConnector->connector_id;
|
2014-06-24 17:27:38 +00:00
|
|
|
if (mode != null)
|
|
|
|
{
|
2014-06-26 13:40:00 +00:00
|
|
|
return Drm.ModeSetCrtc(FD, display.Id, 0, 0, 0,
|
2014-06-24 17:27:38 +00:00
|
|
|
&connector_id, 1, mode) == 0;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public override bool TryRestoreResolution(DisplayDevice device)
|
|
|
|
{
|
|
|
|
unsafe
|
|
|
|
{
|
|
|
|
LinuxDisplay display = (LinuxDisplay)device.Id;
|
|
|
|
ModeInfo mode = display.OriginalMode;
|
2014-06-26 13:40:00 +00:00
|
|
|
int connector_id = display.pConnector->connector_id;
|
|
|
|
return Drm.ModeSetCrtc(FD, display.Id, 0, 0, 0,
|
2014-06-24 17:27:38 +00:00
|
|
|
&connector_id, 1, &mode) == 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|