diff --git a/Source/OpenTK/Platform/MacOS/AglContext.cs b/Source/OpenTK/Platform/MacOS/AglContext.cs index 927d671c..da65cfb3 100644 --- a/Source/OpenTK/Platform/MacOS/AglContext.cs +++ b/Source/OpenTK/Platform/MacOS/AglContext.cs @@ -26,26 +26,34 @@ namespace OpenTK.Platform.MacOS class AglContext : IGraphicsContext, IGraphicsContextInternal { + IntPtr storedContextRef; IntPtr contextRef; + bool mVSync = false; IntPtr displayID; GraphicsMode mode; CarbonWindowInfo carbonWindow; - IGraphicsContext shareContext; - + IntPtr shareContextRef; + static AglContext() { if (GraphicsContext.GetCurrentContext == null) GraphicsContext.GetCurrentContext = AglContext.GetCurrentContext; } - public AglContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext) + public AglContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, bool fullscreen) { + Debug.Print("Context Type: {0}", shareContext); + Debug.Print("Window info: {0}", window); + this.mode = mode; this.carbonWindow = (CarbonWindowInfo)window; - this.shareContext = shareContext; - - CreateContext(mode, carbonWindow, shareContext); + + + if (shareContext is AglContext) + shareContextRef = ((AglContext)shareContext).contextRef; + + CreateContext(mode, carbonWindow, shareContextRef, fullscreen); } @@ -62,11 +70,12 @@ namespace OpenTK.Platform.MacOS aglAttributes.Add((int)pixelFormatAttribute); aglAttributes.Add(value); } - void CreateContext(GraphicsMode mode, CarbonWindowInfo carbonWindow, IGraphicsContext shareContext) + void CreateContext(GraphicsMode mode, CarbonWindowInfo carbonWindow, + IntPtr shareContextRef, bool fullscreen) { List aglAttributes = new List(); - - Debug.Print("AGL attributes:"); + + Debug.Print("AGL pixel format attributes:"); Debug.Indent(); AddPixelAttrib(aglAttributes, Agl.PixelFormatAttribute.AGL_RGBA); @@ -89,7 +98,11 @@ namespace OpenTK.Platform.MacOS AddPixelAttrib(aglAttributes, Agl.PixelFormatAttribute.AGL_ACCUM_BLUE_SIZE, mode.AccumulatorFormat.Blue); AddPixelAttrib(aglAttributes, Agl.PixelFormatAttribute.AGL_ACCUM_ALPHA_SIZE, mode.AccumulatorFormat.Alpha); } - AddPixelAttrib(aglAttributes, Agl.PixelFormatAttribute.AGL_FULLSCREEN); + + if (fullscreen) + { + AddPixelAttrib(aglAttributes, Agl.PixelFormatAttribute.AGL_FULLSCREEN); + } AddPixelAttrib(aglAttributes, Agl.PixelFormatAttribute.AGL_NONE); Debug.Unindent(); @@ -100,31 +113,31 @@ namespace OpenTK.Platform.MacOS Debug.WriteLine(""); AGLPixelFormat myAGLPixelFormat; - IntPtr gdevice; - - OSStatus status = Carbon.API.DMGetGDeviceByDisplayID( - QuartzDisplayDeviceDriver.MainDisplay, out gdevice, false); - if (status != OSStatus.NoError) - throw new MacOSException(status, "DMGetGDeviceByDisplayID failed."); // Choose a pixel format with the attributes we specified. - myAGLPixelFormat = Agl.aglChoosePixelFormat( - ref gdevice, 1, - //IntPtr.Zero, 0, - aglAttributes.ToArray()); + if (fullscreen) + { + IntPtr gdevice; + + OSStatus status = Carbon.API.DMGetGDeviceByDisplayID( + QuartzDisplayDeviceDriver.MainDisplay, out gdevice, false); + + if (status != OSStatus.NoError) + throw new MacOSException(status, "DMGetGDeviceByDisplayID failed."); + myAGLPixelFormat = Agl.aglChoosePixelFormat( + ref gdevice, 1, + aglAttributes.ToArray()); + } + else + { + myAGLPixelFormat = Agl.aglChoosePixelFormat( + IntPtr.Zero, 0, + aglAttributes.ToArray()); + } + MyAGLReportError("aglChoosePixelFormat"); - - IntPtr shareContextRef = IntPtr.Zero; - if (shareContext != null) - { - Debug.Print("shareContext type is {0}", shareContext.GetType()); - } - - if (shareContext != null && shareContext is AglContext) - shareContextRef = ((AglContext)shareContext).contextRef; - // create the context and share it with the share reference. this.contextRef = Agl.aglCreateContext(myAGLPixelFormat, shareContextRef); MyAGLReportError("aglCreateContext"); @@ -205,7 +218,6 @@ namespace OpenTK.Platform.MacOS SetBufferRect(carbonWindow); - //Agl.aglSetCurrentContext(contextRef); Agl.aglUpdateContext(contextRef); } @@ -215,7 +227,8 @@ namespace OpenTK.Platform.MacOS if (err != Agl.AglError.NoError) throw new MacOSException((OSStatus)err, string.Format( - "AGL Error from function {0}: {1} {2}", err, Agl.ErrorString(err))); + "AGL Error from function {0}: {1} {2}", + function, err, Agl.ErrorString(err))); } static ContextHandle GetCurrentContext() @@ -225,11 +238,37 @@ namespace OpenTK.Platform.MacOS internal void SetFullScreen() { - Agl.aglSetFullScreen(contextRef, 640, 480, 60, 0); + if (storedContextRef == IntPtr.Zero) + { + storedContextRef = contextRef; + } + else + { + Agl.aglDestroyContext(contextRef); + } + + // TODO: this may be a problem if we are switching from one + // full screen mode to another. + try + { + CreateContext(mode, carbonWindow, storedContextRef, true); + Agl.aglSetFullScreen(contextRef, 0, 0, 0, 0); + } + catch (MacOSException e) + { + contextRef = storedContextRef; + storedContextRef = IntPtr.Zero; + + throw; + } } internal void UnsetFullScreen() { - SetDrawable(carbonWindow); + if (storedContextRef == IntPtr.Zero) + return; + + Agl.aglDestroyContext(contextRef); + contextRef = storedContextRef; } diff --git a/Source/OpenTK/Platform/MacOS/CarbonBindings/Agl.cs b/Source/OpenTK/Platform/MacOS/CarbonBindings/Agl.cs index 7484f762..312c941b 100644 --- a/Source/OpenTK/Platform/MacOS/CarbonBindings/Agl.cs +++ b/Source/OpenTK/Platform/MacOS/CarbonBindings/Agl.cs @@ -281,6 +281,18 @@ namespace OpenTK.Platform.MacOS ** Pixel format functions */ [DllImport(agl)] internal static extern AGLPixelFormat aglChoosePixelFormat(ref AGLDevice gdevs, int ndev, int []attribs); + /// + /// Use this overload only with IntPtr.Zero for the first argument. + /// + /// + /// + /// + /// + /// + /// + /// + /// + [DllImport(agl)] internal static extern AGLPixelFormat aglChoosePixelFormat(IntPtr gdevs, int ndev, int []attribs); [DllImport(agl)] internal static extern void aglDestroyPixelFormat(AGLPixelFormat pix); [DllImport(agl)] internal static extern AGLPixelFormat aglNextPixelFormat(AGLPixelFormat pix); [DllImport(agl)] static extern byte aglDescribePixelFormat(AGLPixelFormat pix, int attrib, out int value); diff --git a/Source/OpenTK/Platform/MacOS/CarbonGLNative.cs b/Source/OpenTK/Platform/MacOS/CarbonGLNative.cs index 3f2b2b6e..ea9394df 100644 --- a/Source/OpenTK/Platform/MacOS/CarbonGLNative.cs +++ b/Source/OpenTK/Platform/MacOS/CarbonGLNative.cs @@ -632,9 +632,13 @@ namespace OpenTK.Platform.MacOS case WindowState.Fullscreen: ((AglContext)context.Implementation).SetFullScreen(); context.Update(WindowInfo); + break; case WindowState.Maximized: + // hack because mac os has no concept of maximized. Instead windows are "zoomed" + // meaning they are maximized up to their reported ideal size. So we report a + // large ideal size. idealSize = new Point(9000, 9000); API.ZoomWindowIdeal(window.WindowRef, WindowPartCode.inZoomOut, ref idealSize); break; diff --git a/Source/OpenTK/Platform/MacOS/MacOSFactory.cs b/Source/OpenTK/Platform/MacOS/MacOSFactory.cs index 39a85c4e..7dab7c33 100644 --- a/Source/OpenTK/Platform/MacOS/MacOSFactory.cs +++ b/Source/OpenTK/Platform/MacOS/MacOSFactory.cs @@ -27,7 +27,7 @@ namespace OpenTK.Platform.MacOS public IGraphicsContext CreateGLContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext, bool DirectRendering) { - return new AglContext(mode, window, shareContext); + return new AglContext(mode, window, shareContext, false); } public IGraphicsMode CreateGraphicsMode() diff --git a/Source/OpenTK/Platform/MacOS/QuartzDisplayDeviceDriver.cs b/Source/OpenTK/Platform/MacOS/QuartzDisplayDeviceDriver.cs index 58756cc7..2c634a16 100644 --- a/Source/OpenTK/Platform/MacOS/QuartzDisplayDeviceDriver.cs +++ b/Source/OpenTK/Platform/MacOS/QuartzDisplayDeviceDriver.cs @@ -20,16 +20,6 @@ namespace OpenTK.Platform.MacOS static QuartzDisplayDeviceDriver() { - //lock (display_lock) - //{ - // List resolutions = new List(); - - // DisplayResolution primaryRes = new DisplayResolution(1024, 768, 32, 60); - // resolutions.Add(primaryRes); - - // object o = new Graphics.DisplayDevice(primaryRes, true, resolutions); - //} - lock (display_lock) { // To minimize the need to add static methods to OpenTK.Graphics.DisplayDevice @@ -143,6 +133,8 @@ namespace OpenTK.Platform.MacOS bpp == resolution.BitsPerPixel && freq == resolution.RefreshRate) { + CG.DisplayCapture(display); + CG.DisplaySwitchToMode(display, displayModes[j]); return true; @@ -158,7 +150,8 @@ namespace OpenTK.Platform.MacOS if (storedModes.ContainsKey(display)) { - CG.DisplaySwitchToMode(display, storedModes[display]); + //CG.DisplaySwitchToMode(display, storedModes[display]); + CG.DisplayRelease(display); return true; }