[Mac] Add CocoaContext.GetGraphicsMode via CGL
authorthefiddler <stapostol@gmail.com>
Tue, 22 Apr 2014 22:03:07 +0000 (00:03 +0200)
committerthefiddler <stapostol@gmail.com>
Thu, 24 Apr 2014 11:45:06 +0000 (13:45 +0200)
Once the context is constructed, we use GetGraphicsMode to retrieve the
exact GraphicsMode that was selected by CocoaContext.

Source/OpenTK/OpenTK.csproj
Source/OpenTK/Platform/MacOS/CarbonBindings/Cgl.cs [new file with mode: 0644]
Source/OpenTK/Platform/MacOS/Cgl.cs [deleted file]
Source/OpenTK/Platform/MacOS/CocoaContext.cs

index ee17f23..2726c49 100644 (file)
     <Compile Include="Platform\SDL2\Sdl2Mouse.cs" />
     <Compile Include="Platform\SDL2\Sdl2NativeWindow.cs" />
     <Compile Include="Platform\SDL2\Sdl2WindowInfo.cs" />
-    <Compile Include="Platform\MacOS\Cgl.cs" />
     <Compile Include="Platform\SDL2\Sdl2.cs" />
     <Compile Include="Platform\Egl\EglSdl2PlatformFactory.cs" />
     <Compile Include="Platform\SDL2\Sdl2JoystickDriver.cs" />
     <Compile Include="Platform\MacOS\Cocoa\NSEventModifierMask.cs" />
     <Compile Include="Platform\MacOS\Cocoa\NSTrackingAreaOptions.cs" />
     <Compile Include="Platform\MacOS\Cocoa\NSApplicationPresentationOptions.cs" />
+    <Compile Include="Platform\MacOS\CarbonBindings\Cgl.cs" />
   </ItemGroup>
   <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
   <PropertyGroup>
diff --git a/Source/OpenTK/Platform/MacOS/CarbonBindings/Cgl.cs b/Source/OpenTK/Platform/MacOS/CarbonBindings/Cgl.cs
new file mode 100644 (file)
index 0000000..0e84266
--- /dev/null
@@ -0,0 +1,143 @@
+// #region License
+//
+// Cgl.cs
+//
+// Author:
+//       Stefanos A. <stapostol@gmail.com>
+//
+// Copyright (c) 2006-2014 Stefanos Apostolopoulos
+//
+// 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.
+//
+// #endregion
+using System;
+using System.Runtime.InteropServices;
+
+namespace OpenTK.Platform.MacOS
+{
+    using CGLPixelFormat = IntPtr;
+    using CGLContext = IntPtr;
+
+    static class Cgl
+    {
+        internal enum PixelFormatBool
+        {
+            None = 0,
+            AllRenderers = 1,
+            Doublebuffer = 5,
+            Stereo = 6,
+            AuxBuffers = 7,
+            MinimumPolicy = 51,
+            MaximumPolicy = 52,
+            Offscreen = 53,
+            AuxDepthStencil = 57,
+            ColorFloat = 58,
+            Multisample = 59,
+            Supersample = 60,
+            SampleALpha = 61,
+            SingleRenderer = 71,
+            NoRecovery = 72,
+            Accelerated = 73,
+            ClosestPolicy = 74,
+            BackingStore = 76,
+            Window = 80,
+            Compliant = 83,
+            PBuffer = 90,
+            RemotePBuffer = 91,
+        }
+
+        internal enum PixelFormatInt
+        {
+            ColorSize = 8,
+            AlphaSize = 11,
+            DepthSize = 12,
+            StencilSize = 13,
+            AccumSize = 14,
+            SampleBuffers = 55,
+            Samples = 56,
+            RendererID = 70,
+            DisplayMask = 84,
+            OpenGLProfile = 99,
+            VScreenCount = 128,
+        }
+
+        internal enum OpenGLProfileVersion
+        {
+            Legacy = 0x100,
+            Core3_2 = 0x3200,
+        }
+
+        internal enum ParameterNames
+        {
+            SwapInterval = 222,
+        }
+
+        internal enum Error
+        {
+            None = 0x000,
+        }
+
+        const string cgl = "/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL";
+        const string cgs = "/System/Library/Frameworks/Carbon.framework/Versions/Current/Carbon";
+
+        [DllImport(cgl, EntryPoint = "CGLGetError")]
+        internal static extern Error GetError();
+        [DllImport(cgl, EntryPoint = "CGLErrorString")]
+        private static extern IntPtr CGLErrorString(Error code);
+        internal static string ErrorString(Error code)
+        {
+            return Marshal.PtrToStringAnsi(CGLErrorString(code));
+        }
+
+        [DllImport(cgl, EntryPoint = "CGLChoosePixelFormat")]
+        internal static extern Error ChoosePixelFormat(int []attribs, ref CGLPixelFormat format, ref int numPixelFormats);
+        [DllImport(cgl, EntryPoint = "CGLDescribePixelFormat")]
+        internal static extern Error DescribePixelFormat(CGLPixelFormat pix, int pix_num, PixelFormatInt attrib, out int value);
+        [DllImport(cgl, EntryPoint = "CGLDescribePixelFormat")]
+        internal static extern Error DescribePixelFormat(CGLPixelFormat pix, int pix_num, PixelFormatBool attrib, out bool value);
+        [DllImport(cgl, EntryPoint = "CGLGetPixelFormat")]
+        internal static extern CGLPixelFormat GetPixelFormat(CGLContext context);
+        [DllImport(cgl, EntryPoint = "CGLCreateContext")]
+        internal static extern Error CreateContext(CGLPixelFormat format, CGLContext share, ref CGLContext context);
+        [DllImport(cgl, EntryPoint = "CGLDestroyPixelFormat")]
+        internal static extern Error DestroyPixelFormat(CGLPixelFormat format);
+        [DllImport(cgl, EntryPoint = "CGLGetCurrentContext")]
+        internal static extern CGLContext GetCurrentContext();
+        [DllImport(cgl, EntryPoint = "CGLSetCurrentContext")]
+        internal static extern Error SetCurrentContext(CGLContext context);
+        [DllImport(cgl, EntryPoint = "CGLDestroyContext")]
+        internal static extern Error DestroyContext(CGLContext context);
+        [DllImport(cgl, EntryPoint = "CGLSetParameter")]
+        internal static extern Error SetParameter(CGLContext context, int parameter, ref int value);
+        [DllImport(cgl, EntryPoint = "CGLFlushDrawable")]
+        internal static extern Error FlushDrawable(CGLContext context);
+
+        [DllImport(cgl, EntryPoint = "CGLSetSurface")]
+        internal static extern Error SetSurface(CGLContext context, int conId, int winId, int surfId);
+        [DllImport(cgl, EntryPoint = "CGLUpdateContext")]
+        internal static extern Error UpdateContext(CGLContext context);
+
+        [DllImport(cgs, EntryPoint = "CGSMainConnectionID")]
+        internal static extern int MainConnectionID();
+        [DllImport(cgs, EntryPoint = "CGSGetSurfaceCount")]
+        internal static extern Error GetSurfaceCount(int conId, int winId, ref int count);
+        [DllImport(cgs, EntryPoint = "CGSGetSurfaceList")]
+        internal static extern Error GetSurfaceList(int conId, int winId, int count, ref int ids, ref int filled);
+    }
+}
diff --git a/Source/OpenTK/Platform/MacOS/Cgl.cs b/Source/OpenTK/Platform/MacOS/Cgl.cs
deleted file mode 100644 (file)
index db7d3df..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-using System;
-using System.Runtime.InteropServices;
-
-namespace OpenTK.Platform.MacOS
-{
-    using CGLContextObj = IntPtr;
-
-    static class Cgl
-    {
-        const string lib = "/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL";
-
-        [DllImport(lib, EntryPoint = "CGLGetCurrentContext")]
-        public static extern CGLContextObj GetCurrentContext();
-    }
-}
-
index 64ab589..dbfccb2 100644 (file)
@@ -149,11 +149,31 @@ namespace OpenTK
 
             // Finalize
             Handle = new ContextHandle(context);
-            Mode = mode;
+            Mode = GetGraphicsMode(context);
+
             Update(cocoaWindow);
             MakeCurrent(cocoaWindow);
         }
 
+        private GraphicsMode GetGraphicsMode(IntPtr context)
+        {
+            IntPtr cgl_context = Cocoa.SendIntPtr(context, Selector.Get("CGLContextObj"));
+            IntPtr cgl_format = Cgl.GetPixelFormat(cgl_context);
+
+            int id = 0; // CGL does not support the concept of a pixel format id
+            int color, depth, stencil, samples, accum;
+            bool doublebuffer, stereo;
+            Cgl.DescribePixelFormat(cgl_format, 0, Cgl.PixelFormatInt.ColorSize, out color);
+            Cgl.DescribePixelFormat(cgl_format, 0, Cgl.PixelFormatInt.DepthSize, out depth);
+            Cgl.DescribePixelFormat(cgl_format, 0, Cgl.PixelFormatInt.StencilSize, out stencil);
+            Cgl.DescribePixelFormat(cgl_format, 0, Cgl.PixelFormatInt.Samples, out samples);
+            Cgl.DescribePixelFormat(cgl_format, 0, Cgl.PixelFormatInt.AccumSize, out accum);
+            Cgl.DescribePixelFormat(cgl_format, 0, Cgl.PixelFormatBool.Doublebuffer, out doublebuffer);
+            Cgl.DescribePixelFormat(cgl_format, 0, Cgl.PixelFormatBool.Stereo, out stereo);
+
+            return new GraphicsMode((IntPtr)id, color, depth, stencil, samples, accum, doublebuffer ? 2 : 1, stereo);
+        }
+
         public override void SwapBuffers()
         {
             Cocoa.SendVoid(Handle.Handle, selFlushBuffer);