From 8444644b8e272edc24ff6fc75b7cefee24eb1fba Mon Sep 17 00:00:00 2001 From: Matthew Leibowitz Date: Tue, 14 Feb 2017 22:32:10 +0200 Subject: [PATCH] Added a macOS test GL context --- .../SkiaSharp.Desktop.Tests.csproj | 7 +- tests/Tests/CglContext.cs | 134 ++++++++++++++++++ ...lInterfaceTest.cs => GRGlInterfaceTest.cs} | 2 +- tests/Tests/SKTest.cs | 2 +- 4 files changed, 141 insertions(+), 4 deletions(-) create mode 100644 tests/Tests/CglContext.cs rename tests/Tests/{GGlInterfaceTest.cs => GRGlInterfaceTest.cs} (90%) diff --git a/tests/SkiaSharp.Desktop.Tests/SkiaSharp.Desktop.Tests.csproj b/tests/SkiaSharp.Desktop.Tests/SkiaSharp.Desktop.Tests.csproj index 063168e2..dc2671e7 100644 --- a/tests/SkiaSharp.Desktop.Tests/SkiaSharp.Desktop.Tests.csproj +++ b/tests/SkiaSharp.Desktop.Tests/SkiaSharp.Desktop.Tests.csproj @@ -101,6 +101,9 @@ + + CglContext.cs + GlxContext.cs @@ -110,8 +113,8 @@ GRContextTest.cs - - GGlInterfaceTest.cs + + GRGlInterfaceTest.cs SKStringTest.cs diff --git a/tests/Tests/CglContext.cs b/tests/Tests/CglContext.cs new file mode 100644 index 00000000..6520a635 --- /dev/null +++ b/tests/Tests/CglContext.cs @@ -0,0 +1,134 @@ +using System; +using System.Collections.Generic; +using System.Runtime.InteropServices; + +namespace SkiaSharp.Tests +{ + internal class CglContext : GlContext + { + private IntPtr fContext; + + public CglContext() + { + var attributes = new [] { + CGLPixelFormatAttribute.kCGLPFAOpenGLProfile, (CGLPixelFormatAttribute)CGLOpenGLProfile.kCGLOGLPVersion_3_2_Core, + CGLPixelFormatAttribute.kCGLPFADoubleBuffer, + CGLPixelFormatAttribute.kCGLPFANone + }; + + IntPtr pixFormat; + int npix; + + Cgl.CGLChoosePixelFormat(attributes, out pixFormat, out npix); + + if (pixFormat == IntPtr.Zero) { + throw new Exception("CGLChoosePixelFormat failed."); + } + + Cgl.CGLCreateContext(pixFormat, IntPtr.Zero, out fContext); + Cgl.CGLReleasePixelFormat(pixFormat); + + if (fContext == IntPtr.Zero) { + throw new Exception("CGLCreateContext failed."); + } + } + + public override void MakeCurrent() + { + Cgl.CGLSetCurrentContext(fContext); + } + + public override void SwapBuffers() + { + Cgl.CGLFlushDrawable(fContext); + } + + public override void Destroy() + { + if (fContext != IntPtr.Zero) { + Cgl.CGLReleaseContext(fContext); + fContext = IntPtr.Zero; + } + } + } + + internal enum CGLOpenGLProfile { + kCGLOGLPVersion_Legacy = 0x1000, + kCGLOGLPVersion_3_2_Core = 0x3200, + kCGLOGLPVersion_GL3_Core = 0x3200, + kCGLOGLPVersion_GL4_Core = 0x4100, + } + + internal enum CGLPixelFormatAttribute { + kCGLPFANone = 0, + kCGLPFAAllRenderers = 1, + kCGLPFATripleBuffer = 3, + kCGLPFADoubleBuffer = 5, + kCGLPFAColorSize = 8, + kCGLPFAAlphaSize = 11, + kCGLPFADepthSize = 12, + kCGLPFAStencilSize = 13, + kCGLPFAMinimumPolicy = 51, + kCGLPFAMaximumPolicy = 52, + kCGLPFASampleBuffers = 55, + kCGLPFASamples = 56, + kCGLPFAColorFloat = 58, + kCGLPFAMultisample = 59, + kCGLPFASupersample = 60, + kCGLPFASampleAlpha = 61, + kCGLPFARendererID = 70, + kCGLPFANoRecovery = 72, + kCGLPFAAccelerated = 73, + kCGLPFAClosestPolicy = 74, + kCGLPFABackingStore = 76, + kCGLPFABackingVolatile = 77, + kCGLPFADisplayMask = 84, + kCGLPFAAllowOfflineRenderers = 96, + kCGLPFAAcceleratedCompute = 97, + kCGLPFAOpenGLProfile = 99, + kCGLPFASupportsAutomaticGraphicsSwitching = 101, + kCGLPFAVirtualScreenCount = 128, + } + + internal enum CGLError { + kCGLNoError = 0, + kCGLBadAttribute = 10000, + kCGLBadProperty = 10001, + kCGLBadPixelFormat = 10002, + kCGLBadRendererInfo = 10003, + kCGLBadContext = 10004, + kCGLBadDrawable = 10005, + kCGLBadDisplay = 10006, + kCGLBadState = 10007, + kCGLBadValue = 10008, + kCGLBadMatch = 10009, + kCGLBadEnumeration = 10010, + kCGLBadOffScreen = 10011, + kCGLBadFullScreen = 10012, + kCGLBadWindow = 10013, + kCGLBadAddress = 10014, + kCGLBadCodeModule = 10015, + kCGLBadAlloc = 10016, + kCGLBadConnection = 10017, + } + + internal class Cgl + { + private const string libGL = "/System/Library/Frameworks/OpenGL.framework/Versions/A/OpenGL"; + + [DllImport(libGL)] + public extern static void CGLGetVersion(out int majorvers, out int minorvers); + [DllImport(libGL)] + public extern static CGLError CGLChoosePixelFormat([In] CGLPixelFormatAttribute[] attribs, out IntPtr pix, out int npix); + [DllImport(libGL)] + public extern static CGLError CGLCreateContext(IntPtr pix, IntPtr share, out IntPtr ctx); + [DllImport(libGL)] + public extern static CGLError CGLReleasePixelFormat(IntPtr pix); + [DllImport(libGL)] + public extern static CGLError CGLSetCurrentContext(IntPtr ctx); + [DllImport(libGL)] + public extern static void CGLReleaseContext(IntPtr ctx); + [DllImport(libGL)] + public extern static CGLError CGLFlushDrawable(IntPtr ctx); + } +} diff --git a/tests/Tests/GGlInterfaceTest.cs b/tests/Tests/GRGlInterfaceTest.cs similarity index 90% rename from tests/Tests/GGlInterfaceTest.cs rename to tests/Tests/GRGlInterfaceTest.cs index c57706a5..55518ea7 100644 --- a/tests/Tests/GGlInterfaceTest.cs +++ b/tests/Tests/GRGlInterfaceTest.cs @@ -5,7 +5,7 @@ using Xunit; namespace SkiaSharp.Tests { - public class GGlInterfaceTest : SKTest + public class GRGlInterfaceTest : SKTest { [Fact] public void CreateDefaultInterfaceIsValid() diff --git a/tests/Tests/SKTest.cs b/tests/Tests/SKTest.cs index b456875c..8e39f13a 100644 --- a/tests/Tests/SKTest.cs +++ b/tests/Tests/SKTest.cs @@ -59,7 +59,7 @@ namespace SkiaSharp.Tests if (IsLinux) { return new GlxContext(); } else if (IsMac) { - return null; + return new CglContext(); } else if (IsWindows) { return null; } else {