-
Notifications
You must be signed in to change notification settings - Fork 691
/
x11bindings_Glx.cs
141 lines (109 loc) · 4.96 KB
/
x11bindings_Glx.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
// The MIT License (MIT)
//
// Copyright (c) .NET Foundation and Contributors
// All Rights Reserved
//
// 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.
// https://github.com/AvaloniaUI/Avalonia/blob/5fa3ffaeab7e5cd2662ef02d03e34b9d4cb1a489/src/Avalonia.X11/Glx/Glx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
// ReSharper disable UnassignedGetOnlyAutoProperty
namespace Uno.WinUI.Runtime.Skia.X11
{
unsafe internal class GlxInterface
{
private const string libGL = "libGL.so.1";
private static readonly char[] _separators = { ',', ' ' };
private static readonly int[] _null = { 0 };
[DllImport(libGL)]
public static extern bool glXQueryVersion(IntPtr dpy, out int maj, out int min);
[DllImport(libGL)]
public static extern bool glXQueryExtension(IntPtr dpy, out int errorBase, out int eventBase);
[DllImport(libGL)]
public static extern bool glXMakeContextCurrent(IntPtr display, IntPtr draw, IntPtr read, IntPtr context);
[DllImport(libGL)]
public static extern bool glXMakeCurrent(IntPtr display, IntPtr drawable, IntPtr context);
[DllImport(libGL)]
public static extern IntPtr glXGetCurrentContext();
[DllImport(libGL)]
public static extern IntPtr glXGetCurrentDisplay();
[DllImport(libGL)]
public static extern IntPtr glXGetCurrentDrawable();
[DllImport(libGL)]
public static extern IntPtr glXGetCurrentReadDrawable();
[DllImport(libGL)]
public static extern IntPtr glXCreatePbuffer(IntPtr dpy, IntPtr fbc, int[] attrib_list);
[DllImport(libGL)]
public static extern IntPtr glXDestroyPbuffer(IntPtr dpy, IntPtr fb);
[DllImport(libGL)]
public static extern XVisualInfo* glXChooseVisual(IntPtr dpy, int screen, int[] attribList);
[DllImport(libGL)]
public static extern IntPtr glXCreateContext(IntPtr dpy, XVisualInfo* vis, IntPtr shareList, bool direct);
[DllImport(libGL)]
public static extern IntPtr glXCreateNewContext(IntPtr dpy, IntPtr config, int renderType, IntPtr shareList, int direct);
[DllImport(libGL)]
public static extern IntPtr glXCreateContextAttribsARB(IntPtr dpy, IntPtr fbconfig, IntPtr shareList,
bool direct, int[] attribs);
[DllImport(libGL)]
public static extern IntPtr glXGetProcAddress(string buffer);
[DllImport(libGL)]
public static extern void glXDestroyContext(IntPtr dpy, IntPtr ctx);
[DllImport(libGL)]
public static extern IntPtr* glXChooseFBConfig(IntPtr dpy, int screen, int[] attrib_list, out int nelements);
public IntPtr* glXChooseFBConfig(IntPtr dpy, int screen, IEnumerable<int> attribs, out int nelements)
{
var arr = attribs.Concat(_null).ToArray();
return glXChooseFBConfig(dpy, screen, arr, out nelements);
}
[DllImport(libGL)]
public static extern XVisualInfo* glXGetVisualFromFBConfig(IntPtr dpy, IntPtr config);
[DllImport(libGL)]
public static extern int glXGetFBConfigAttrib(IntPtr dpy, IntPtr config, int attribute, out int value);
[DllImport(libGL)]
public static extern void glXSwapBuffers(IntPtr dpy, IntPtr drawable);
[DllImport(libGL)]
public static extern void glXWaitX();
[DllImport(libGL)]
public static extern void glXWaitGL();
[DllImport(libGL)]
public static extern int glGetError();
[DllImport(libGL)]
public static extern IntPtr glXQueryExtensionsString(IntPtr display, int screen);
// Ignores egl functions.
// On some Linux systems, glXGetProcAddress will return valid pointers for even EGL functions.
// This makes Skia try to load some data from EGL,
// which can then cause segmentation faults because they return garbage.
public static IntPtr SafeGetProcAddress(string proc)
{
if (proc.StartsWith("egl", StringComparison.InvariantCulture))
{
return IntPtr.Zero;
}
return glXGetProcAddress(proc);
}
public string[]? GetExtensions(IntPtr display)
{
var s = Marshal.PtrToStringAnsi(glXQueryExtensionsString(display, 0));
return s?.Split(_separators, StringSplitOptions.RemoveEmptyEntries)
.Select(x => x.Trim()).ToArray();
}
}
}