Skip to content

Commit

Permalink
Refactor linux natives
Browse files Browse the repository at this point in the history
  • Loading branch information
Neakita committed Sep 20, 2024
1 parent 1b3eb07 commit 11c0b02
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
22 changes: 11 additions & 11 deletions SightKeeper.Application.Linux/Natives/LibX.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
using System.Runtime.InteropServices;

namespace SightKeeper.Application.Linux.Natives;
internal static partial class LibX
internal static unsafe partial class LibX
{
private const string libX11 = "libX11.so.6";

[DllImport(libX11)]
public static extern IntPtr XRootWindow(IntPtr display, int screen_number);
[LibraryImport(libX11)]
public static partial IntPtr XRootWindow(IntPtr display, int screen_number);

[DllImport(libX11)]
public static extern uint XDefaultDepth(IntPtr display, int screen_number);
[LibraryImport(libX11)]
public static partial uint XDefaultDepth(IntPtr display, int screen_number);

[DllImport(libX11)]
public static extern int XDestroyImage(IntPtr image);
[LibraryImport(libX11)]
public static partial int XDestroyImage(IntPtr image);

[DllImport(libX11)]
public static extern int XSync(IntPtr display, bool discard);
[LibraryImport(libX11)]
public static partial int XSync(IntPtr display, [MarshalAs(UnmanagedType.Bool)] bool discard);

[DllImport(libX11)]
public static extern unsafe ref XImage* XGetImage(IntPtr display, IntPtr drawable, int x, int y, uint width, uint height, ulong plane_mask, PixmapFormat format);
[LibraryImport(libX11)]
public static partial XImage* XGetImage(IntPtr display, IntPtr drawable, int x, int y, uint width, uint height, ulong plane_mask, PixmapFormat format);

[LibraryImport(libX11, StringMarshalling = StringMarshalling.Utf8)] // not sure about StringMarshalling
public static partial IntPtr XOpenDisplay(string? display);
Expand Down
12 changes: 6 additions & 6 deletions SightKeeper.Application.Linux/Natives/LibXExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ internal static unsafe partial class LibXExt
[LibraryImport(DllName)]
public static partial int XShmGetImage(IntPtr display, UIntPtr drawable, XImage* image, int x, int y, UIntPtr plane_mask);

[DllImport(DllName, SetLastError = true)]
public static extern int XShmQueryExtension(IntPtr display);
[LibraryImport(DllName, SetLastError = true)]
public static partial int XShmQueryExtension(IntPtr display);

[DllImport(DllName, SetLastError = true)]
public static extern int XShmAttach(IntPtr display, XShmSegmentInfo* shminfo);
[LibraryImport(DllName, SetLastError = true)]
public static partial int XShmAttach(IntPtr display, XShmSegmentInfo* shminfo);

[DllImport(DllName, SetLastError = true)]
public static extern IntPtr XShmCreateImage(IntPtr display, IntPtr visual, uint depth, int format, IntPtr data, XShmSegmentInfo* shminfo, uint width, uint height);
[LibraryImport(DllName, SetLastError = true)]
public static partial IntPtr XShmCreateImage(IntPtr display, IntPtr visual, uint depth, int format, IntPtr data, XShmSegmentInfo* shminfo, uint width, uint height);
}
6 changes: 2 additions & 4 deletions SightKeeper.Application.Linux/XLibShm.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
using SightKeeper.Application.Linux.Natives;
using SixLabors.ImageSharp.PixelFormats;

namespace SightKeeper.Application.Linux;

// https://stackoverflow.com/questions/34176795/any-efficient-way-of-converting-ximage-data-to-pixel-map-e-g-array-of-rgb-quad
internal static class XLibShm
{
private const int IPC_PRIVATE = 0; // or long?
private const int BPP = 4; // Bytes per pixel?
private const int IPC_CREAT = 01000; // wha?
private const int IPC_RMID = 0;

public static unsafe void createimage(IntPtr dsp, ShmImage* image, int width, int height)
{
// Create a shared memory area
image->shminfo.shmid = LibC.shmget(LibC.IPC_PRIVATE, width * height * BPP, LibC.IPC_CREAT | 0600);
image->shminfo.shmid = LibC.shmget(LibC.IPC_PRIVATE, width * height * sizeof(Bgra32), LibC.IPC_CREAT | 0600);
if (image->shminfo.shmid == -1)
{
throw new Exception();
Expand Down
5 changes: 5 additions & 0 deletions SightKeeper.Avalonia/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public static void Main(string[] args)
if (File.Exists("Test.png"))
File.Delete("Test.png");
Image.Load(initial).Save("Test.png");
// warmup
for (int i = 0; i < 200; i++)
{
using var stream = screenCapture.Capture(new Vector2<ushort>(640, 640), null);
}
DateTime start = DateTime.UtcNow;
const int samples = 1000;
for (int i = 0; i < samples; i++)
Expand Down

0 comments on commit 11c0b02

Please sign in to comment.