Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(LinuxFramebuffer): Allow set DRM output Connector #14878

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/Linux/Avalonia.LinuxFramebuffer/DrmConnectorType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace Avalonia.LinuxFramebuffer;

/// <summary>
/// specific the type of connector is HDMI-A, DVI, DisplayPort, etc.
/// </summary>
public enum DrmConnectorType : uint
{
None,
VGA,
DVI_I,
DVI_D,
DVI_A,
Composite,
S_Video,
LVDS,
Component,
DIN,
DisplayPort,
HDMI_A,
HDMI_B,
TV,
eDP,
Virtual,
DSI,
}
16 changes: 15 additions & 1 deletion src/Linux/Avalonia.LinuxFramebuffer/DrmOutputOptions.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using Avalonia.LinuxFramebuffer.Output;
using Avalonia.Media;

namespace Avalonia.LinuxFramebuffer
{
/// <summary>
/// DRM Output Options
/// </summary>
public class DrmOutputOptions
{
/// <summary>
Expand All @@ -28,5 +30,17 @@ public class DrmOutputOptions
/// If NULL preferred mode will be used.
/// </summary>
public PixelSize? VideoMode { get; set; }

/// <summary>
/// Specific whether our connector is HDMI-A, DVI, DisplayPort, etc.
/// If NULL preferred connector will be used.
/// </summary>
public DrmConnectorType? ConnectorType { get; init; }

/// <summary>
/// Specific whether connector id using for <see cref="ConnectorType"/>
/// If NULL preferred connector id will be used
/// </summary>
public uint? ConnectorType_Id { get; init; }
}
}
20 changes: 20 additions & 0 deletions src/Linux/Avalonia.LinuxFramebuffer/EnumerableExtesions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using System;
using System.Collections.Generic;

namespace Avalonia.LinuxFramebuffer;

internal static class EnumerableExtesions
{
public static IEnumerable<TSource> Where<TSource, TArg>(this IEnumerable<TSource> source, Func<TSource, TArg, bool> predicate, TArg arg)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need this extension here? Simple one pass foreach can work there just fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I would like to avoid closure allocation

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this code executed once? Either way, with foreach the same closure can be avoided as well.

Copy link
Member

@maxkatz6 maxkatz6 Mar 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, in Linq Where operator has number of optimizations depending on the input collection type. Custom method implementation probably doesn't help as much. https://github.com/dotnet/runtime/blob/main/src/libraries/System.Linq/src/System/Linq/Where.cs#L36C28-L36C46

{
var enumerator = source.GetEnumerator();
while (enumerator.MoveNext())
{
var current = enumerator.Current;
if (predicate(current, arg))
{
yield return current;
}
}
}
}
50 changes: 26 additions & 24 deletions src/Linux/Avalonia.LinuxFramebuffer/Output/DrmBindings.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using static Avalonia.LinuxFramebuffer.NativeUnsafeMethods;
using static Avalonia.LinuxFramebuffer.Output.LibDrm;

namespace Avalonia.LinuxFramebuffer.Output
{
[DebuggerDisplay("{Name}")]
public unsafe class DrmConnector
{
private static string[] KnownConnectorTypes =
Expand All @@ -25,18 +27,21 @@ public unsafe class DrmConnector
internal uint EncoderId { get; }
internal List<uint> EncoderIds { get; } = new List<uint>();
public List<DrmModeInfo> Modes { get; } = new List<DrmModeInfo>();
public DrmConnectorType ConnectorType { get; }
public uint ConnectorType_Id { get; }
internal DrmConnector(drmModeConnector* conn)
{
Connection = conn->connection;
Id = conn->connector_id;
SizeMm = new Size(conn->mmWidth, conn->mmHeight);
SubPixel = conn->subpixel;
for (var c = 0; c < conn->count_encoders;c++)
for (var c = 0; c < conn->count_encoders; c++)
EncoderIds.Add(conn->encoders[c]);
EncoderId = conn->encoder_id;
for(var c=0; c<conn->count_modes; c++)
for (var c = 0; c < conn->count_modes; c++)
Modes.Add(new DrmModeInfo(ref conn->modes[c]));

ConnectorType = (DrmConnectorType)conn->connector_type;
ConnectorType_Id = conn->connector_type_id;
if (conn->connector_type > KnownConnectorTypes.Length - 1)
Name = $"Unknown({conn->connector_type})-{conn->connector_type_id}";
else
Expand Down Expand Up @@ -77,42 +82,38 @@ public DrmEncoder(drmModeEncoder encoder, drmModeCrtc[] crtcs)
}
}
}




public unsafe class DrmResources
{
public List<DrmConnector> Connectors { get; }= new List<DrmConnector>();
public List<DrmConnector> Connectors { get; } = new List<DrmConnector>();
internal Dictionary<uint, DrmEncoder> Encoders { get; } = new Dictionary<uint, DrmEncoder>();
public DrmResources(int fd, bool connectorsForceProbe = false)
{
var res = drmModeGetResources(fd);
if (res == null)
throw new Win32Exception("drmModeGetResources failed");

var crtcs = new drmModeCrtc[res->count_crtcs];
for (var c = 0; c < res->count_crtcs; c++)
{
var crtc = drmModeGetCrtc(fd, res->crtcs[c]);
crtcs[c] = *crtc;
drmModeFreeCrtc(crtc);
}

for (var c = 0; c < res->count_encoders; c++)
{
var enc = drmModeGetEncoder(fd, res->encoders[c]);
Encoders[res->encoders[c]] = new DrmEncoder(*enc, crtcs);
drmModeFreeEncoder(enc);
}

for (var c = 0; c < res->count_connectors; c++)
{
var conn = connectorsForceProbe ? drmModeGetConnector(fd, res->connectors[c]) : drmModeGetConnectorCurrent(fd, res->connectors[c]);
Connectors.Add(new DrmConnector(conn));
drmModeFreeConnector(conn);
}


}

internal void Dump()
Expand All @@ -133,38 +134,39 @@ void Print(int off, string s)
Print(2, "Modes");
foreach (var m in conn.Modes)
Print(3, $"{m.Name} {(m.IsPreferred ? "PREFERRED" : "")}");


}
}
}

public unsafe class DrmCard : IDisposable
{
public int Fd { get; private set; }
public DrmCard(string path = null)
{
if(path == null)
if (path == null)
{
var files = Directory.GetFiles("/dev/dri/");

foreach(var file in files)
foreach (var file in files)
{
var match = Regex.Match(file, "card[0-9]+");

if(match.Success)
if (match.Success)
{
Fd = open(file, 2, 0);
if(Fd != -1) break;
}
if (Fd != -1)
break;
}
}

if(Fd == -1) throw new Win32Exception("Couldn't open /dev/dri/card[0-9]+");
if (Fd == -1)
throw new Win32Exception("Couldn't open /dev/dri/card[0-9]+");
}
else
else
{
Fd = open(path, 2, 0);
if(Fd == -1) throw new Win32Exception($"Couldn't open {path}");
if (Fd == -1)
throw new Win32Exception($"Couldn't open {path}");
}
}

Expand Down
Loading