From 8a1149a14048dc349677a9fa2f093ec4a6797bdf Mon Sep 17 00:00:00 2001 From: Matthew Leibowitz Date: Fri, 17 Feb 2017 03:46:45 +0200 Subject: [PATCH] Added a test for assembling the GL interface --- tests/Tests/GRGlInterfaceTest.cs | 52 ++++++++++++++++++++++++++++++++ tests/Tests/SKTest.cs | 33 ++++++++++++++++++++ 2 files changed, 85 insertions(+) diff --git a/tests/Tests/GRGlInterfaceTest.cs b/tests/Tests/GRGlInterfaceTest.cs index 90f2c176..f1055f92 100644 --- a/tests/Tests/GRGlInterfaceTest.cs +++ b/tests/Tests/GRGlInterfaceTest.cs @@ -20,5 +20,57 @@ namespace SkiaSharp.Tests Assert.True(glInterface.Validate()); } } + + [Test] + public void AssembleInterfaceIsValid() + { + using (var ctx = CreateGlContext()) { + ctx.MakeCurrent(); + + if (IsMac) { + var lib = MacDynamicLibraries.dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/A/Libraries/libGL.dylib", 1); + + var glInterface = GRGlInterface.AssembleGlInterface((context, name) => { + return MacDynamicLibraries.dlsym(lib, name); + }); + + Assert.NotNull(glInterface); + Assert.True(glInterface.Validate()); + + MacDynamicLibraries.dlclose(lib); + } else if (IsWindows) { + var lib = WindowsDynamicLibraries.LoadLibrary("opengl32.dll"); + + var glInterface = GRGlInterface.AssembleGlInterface((context, name) => { + var ptr = WindowsDynamicLibraries.GetProcAddress(lib, name); + if (ptr == IntPtr.Zero) { + ptr = wglGetProcAddress(name); + } + return ptr; + }); + + Assert.NotNull(glInterface); + Assert.True(glInterface.Validate()); + + WindowsDynamicLibraries.FreeLibrary(lib); + } else if (IsLinux) { + var lib = LinuxDynamicLibraries.dlopen("libGL.so.1", 1); + + var glInterface = GRGlInterface.AssembleGlInterface((context, name) => { + return LinuxDynamicLibraries.dlsym(lib, name); + }); + + Assert.NotNull(glInterface); + Assert.True(glInterface.Validate()); + + LinuxDynamicLibraries.dlclose(lib); + } else { + // more platforms !!! + } + } + } + + [DllImport("opengl32.dll", CallingConvention = CallingConvention.Winapi)] + public static extern IntPtr wglGetProcAddress([MarshalAs(UnmanagedType.LPStr)] string lpszProc); } } diff --git a/tests/Tests/SKTest.cs b/tests/Tests/SKTest.cs index 41127aee..14baa46b 100644 --- a/tests/Tests/SKTest.cs +++ b/tests/Tests/SKTest.cs @@ -53,6 +53,39 @@ namespace SkiaSharp.Tests protected static bool IsWindows => !IsUnix; #endif + public static class MacDynamicLibraries + { + private const string SystemLibrary = "/usr/lib/libSystem.dylib"; + [DllImport(SystemLibrary)] + public static extern IntPtr dlopen(string path, int mode); + [DllImport(SystemLibrary)] + public static extern IntPtr dlsym(IntPtr handle, string symbol); + [DllImport(SystemLibrary)] + public static extern void dlclose(IntPtr handle); + } + + public static class LinuxDynamicLibraries + { + private const string SystemLibrary = "libdl.so"; + [DllImport(SystemLibrary)] + public static extern IntPtr dlopen(string path, int mode); + [DllImport(SystemLibrary)] + public static extern IntPtr dlsym(IntPtr handle, string symbol); + [DllImport(SystemLibrary)] + public static extern void dlclose(IntPtr handle); + } + + public static class WindowsDynamicLibraries + { + private const string SystemLibrary = "Kernel32.dll"; + [DllImport (SystemLibrary, SetLastError = true, CharSet = CharSet.Ansi)] + public static extern IntPtr LoadLibrary(string lpFileName); + [DllImport (SystemLibrary, SetLastError = true, CharSet = CharSet.Ansi)] + public static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName); + [DllImport (SystemLibrary, SetLastError = true, CharSet = CharSet.Ansi)] + public static extern void FreeLibrary(IntPtr hModule); + } + protected GlContext CreateGlContext() { if (IsLinux) {