From 6edaf8c3cfff5d26b055606cac312a6f2105f4d2 Mon Sep 17 00:00:00 2001 From: Stefanos A Date: Fri, 8 Nov 2013 18:44:02 +0100 Subject: [PATCH] Deduplicate MacOSGraphicsMode and AglContext The IGraphicsMode interface is gradually being removed and the MacOSFactory will now throw an exception if an instance is requested. AglContext no longer duplicates MacOSGraphicsMode functionality. --- Source/OpenTK/Platform/MacOS/AglContext.cs | 75 +++++-------------- Source/OpenTK/Platform/MacOS/MacOSFactory.cs | 2 +- .../Platform/MacOS/MacOSGraphicsMode.cs | 28 ++++--- 3 files changed, 38 insertions(+), 67 deletions(-) diff --git a/Source/OpenTK/Platform/MacOS/AglContext.cs b/Source/OpenTK/Platform/MacOS/AglContext.cs index afa89627..ee5a468e 100644 --- a/Source/OpenTK/Platform/MacOS/AglContext.cs +++ b/Source/OpenTK/Platform/MacOS/AglContext.cs @@ -47,18 +47,18 @@ namespace OpenTK.Platform.MacOS // Todo: keep track of which display adapter was specified when the context was created. // IntPtr displayID; - GraphicsMode graphics_mode; CarbonWindowInfo carbonWindow; IntPtr shareContextRef; DisplayDevice device; bool mIsFullscreen = false; + readonly MacOSGraphicsMode ModeSelector = new MacOSGraphicsMode(); + public AglContext(GraphicsMode mode, IWindowInfo window, IGraphicsContext shareContext) { Debug.Print("Context Type: {0}", shareContext); Debug.Print("Window info: {0}", window); - - this.graphics_mode = mode; + this.carbonWindow = (CarbonWindowInfo)window; if (shareContext is AglContext) @@ -105,50 +105,7 @@ namespace OpenTK.Platform.MacOS } void CreateContext(GraphicsMode mode, CarbonWindowInfo carbonWindow, IntPtr shareContextRef, bool fullscreen) { - List aglAttributes = new List(); - Debug.Print("AGL pixel format attributes:"); - Debug.Indent(); - - AddPixelAttrib(aglAttributes, Agl.PixelFormatAttribute.AGL_RGBA); - AddPixelAttrib(aglAttributes, Agl.PixelFormatAttribute.AGL_DOUBLEBUFFER); - AddPixelAttrib(aglAttributes, Agl.PixelFormatAttribute.AGL_RED_SIZE, mode.ColorFormat.Red); - AddPixelAttrib(aglAttributes, Agl.PixelFormatAttribute.AGL_GREEN_SIZE, mode.ColorFormat.Green); - AddPixelAttrib(aglAttributes, Agl.PixelFormatAttribute.AGL_BLUE_SIZE, mode.ColorFormat.Blue); - AddPixelAttrib(aglAttributes, Agl.PixelFormatAttribute.AGL_ALPHA_SIZE, mode.ColorFormat.Alpha); - - if (mode.Depth > 0) - AddPixelAttrib(aglAttributes, Agl.PixelFormatAttribute.AGL_DEPTH_SIZE, mode.Depth); - - if (mode.Stencil > 0) - AddPixelAttrib(aglAttributes, Agl.PixelFormatAttribute.AGL_STENCIL_SIZE, mode.Stencil); - - if (mode.AccumulatorFormat.BitsPerPixel > 0) - { - AddPixelAttrib(aglAttributes, Agl.PixelFormatAttribute.AGL_ACCUM_RED_SIZE, mode.AccumulatorFormat.Red); - AddPixelAttrib(aglAttributes, Agl.PixelFormatAttribute.AGL_ACCUM_GREEN_SIZE, mode.AccumulatorFormat.Green); - AddPixelAttrib(aglAttributes, Agl.PixelFormatAttribute.AGL_ACCUM_BLUE_SIZE, mode.AccumulatorFormat.Blue); - AddPixelAttrib(aglAttributes, Agl.PixelFormatAttribute.AGL_ACCUM_ALPHA_SIZE, mode.AccumulatorFormat.Alpha); - } - - if (mode.Samples > 1) - { - AddPixelAttrib(aglAttributes, Agl.PixelFormatAttribute.AGL_SAMPLE_BUFFERS_ARB, 1); - AddPixelAttrib(aglAttributes, Agl.PixelFormatAttribute.AGL_SAMPLES_ARB, mode.Samples); - } - - if (fullscreen) - { - AddPixelAttrib(aglAttributes, Agl.PixelFormatAttribute.AGL_FULLSCREEN); - } - AddPixelAttrib(aglAttributes, Agl.PixelFormatAttribute.AGL_NONE); - - Debug.Unindent(); - - Debug.Write("Attribute array: "); - for (int i = 0; i < aglAttributes.Count; i++) - Debug.Write(aglAttributes[i].ToString() + " "); - Debug.WriteLine(""); AGLPixelFormat myAGLPixelFormat; @@ -166,11 +123,13 @@ namespace OpenTK.Platform.MacOS if (status != OSStatus.NoError) throw new MacOSException(status, "DMGetGDeviceByDisplayID failed."); - myAGLPixelFormat = Agl.aglChoosePixelFormat(ref gdevice, 1, aglAttributes.ToArray()); + myAGLPixelFormat = ModeSelector.SelectPixelFormat( + mode.ColorFormat, mode.Depth, mode.Stencil, mode.Samples, + mode.AccumulatorFormat, mode.Buffers, mode.Stereo, + true, gdevice); Agl.AglError err = Agl.GetError(); - - if (err == Agl.AglError.BadPixelFormat) + if (myAGLPixelFormat == IntPtr.Zero || err == Agl.AglError.BadPixelFormat) { Debug.Print("Failed to create full screen pixel format."); Debug.Print("Trying again to create a non-fullscreen pixel format."); @@ -179,21 +138,23 @@ namespace OpenTK.Platform.MacOS return; } } - else { - myAGLPixelFormat = Agl.aglChoosePixelFormat(IntPtr.Zero, 0, aglAttributes.ToArray()); - + myAGLPixelFormat = ModeSelector.SelectPixelFormat( + mode.ColorFormat, mode.Depth, mode.Stencil, mode.Samples, + mode.AccumulatorFormat, mode.Buffers, mode.Stereo, + false, IntPtr.Zero); MyAGLReportError("aglChoosePixelFormat"); } - Debug.Print("Creating AGL context. Sharing with {0}", shareContextRef); // create the context and share it with the share reference. Handle = new ContextHandle(Agl.aglCreateContext(myAGLPixelFormat, shareContextRef)); MyAGLReportError("aglCreateContext"); - + + Mode = ModeSelector.GetGraphicsModeFromPixelFormat(myAGLPixelFormat); + // Free the pixel format from memory. Agl.aglDestroyPixelFormat(myAGLPixelFormat); MyAGLReportError("aglDestroyPixelFormat"); @@ -205,7 +166,6 @@ namespace OpenTK.Platform.MacOS Update(carbonWindow); MakeCurrent(carbonWindow); - Debug.Print("context: {0}", Handle.Handle); } @@ -290,10 +250,11 @@ namespace OpenTK.Platform.MacOS windowPort = API.GetWindowPort(controlOwner); } - else + { windowPort = API.GetWindowPort(carbonWindow.Handle); - + } + return windowPort; } public override void Update(IWindowInfo window) diff --git a/Source/OpenTK/Platform/MacOS/MacOSFactory.cs b/Source/OpenTK/Platform/MacOS/MacOSFactory.cs index 142ef76d..ad540311 100644 --- a/Source/OpenTK/Platform/MacOS/MacOSFactory.cs +++ b/Source/OpenTK/Platform/MacOS/MacOSFactory.cs @@ -76,7 +76,7 @@ namespace OpenTK.Platform.MacOS public virtual IGraphicsMode CreateGraphicsMode() { - return new MacOSGraphicsMode(); + throw new NotSupportedException(); } public virtual OpenTK.Input.IKeyboardDriver2 CreateKeyboardDriver() diff --git a/Source/OpenTK/Platform/MacOS/MacOSGraphicsMode.cs b/Source/OpenTK/Platform/MacOS/MacOSGraphicsMode.cs index 28d49f2b..604973e2 100644 --- a/Source/OpenTK/Platform/MacOS/MacOSGraphicsMode.cs +++ b/Source/OpenTK/Platform/MacOS/MacOSGraphicsMode.cs @@ -42,15 +42,17 @@ namespace OpenTK.Platform.MacOS public GraphicsMode SelectGraphicsMode(ColorFormat color, int depth, int stencil, int samples, ColorFormat accum, int buffers, bool stereo) { - IntPtr pixelformat = SelectPixelFormat(color, depth, stencil, samples, accum, buffers, stereo); + IntPtr pixelformat = SelectPixelFormat( + color, depth, stencil, samples, accum, buffers, stereo, + false, IntPtr.Zero); return GetGraphicsModeFromPixelFormat(pixelformat); } #endregion - #region Private Members + #region Internal Members - GraphicsMode GetGraphicsModeFromPixelFormat(IntPtr pixelformat) + internal GraphicsMode GetGraphicsModeFromPixelFormat(IntPtr pixelformat) { int r, g, b, a; Agl.aglDescribePixelFormat(pixelformat, Agl.PixelFormatAttribute.AGL_RED_SIZE, out r); @@ -73,8 +75,8 @@ namespace OpenTK.Platform.MacOS depth, stencil, samples, new ColorFormat(ar, ag, ab, aa), buffers + 1, stereo != 0); } - IntPtr SelectPixelFormat(ColorFormat color, int depth, int stencil, int samples, - ColorFormat accum, int buffers, bool stereo) + internal IntPtr SelectPixelFormat(ColorFormat color, int depth, int stencil, int samples, + ColorFormat accum, int buffers, bool stereo, bool fullscreen, IntPtr device) { List attribs = new List(); @@ -140,14 +142,22 @@ namespace OpenTK.Platform.MacOS attribs.Add((int)Agl.PixelFormatAttribute.AGL_STEREO); } + if (fullscreen) + { + attribs.Add((int)Agl.PixelFormatAttribute.AGL_FULLSCREEN); + } + attribs.Add(0); attribs.Add(0); - IntPtr pixelformat = Agl.aglChoosePixelFormat(IntPtr.Zero, 0, attribs.ToArray()); - if (pixelformat == IntPtr.Zero) + IntPtr pixelformat = IntPtr.Zero; + if (device != IntPtr.Zero) { - throw new GraphicsModeException(String.Format( - "[Error] Failed to select GraphicsMode, error {0}.", Agl.GetError())); + pixelformat = Agl.aglChoosePixelFormat(ref device, 0, attribs.ToArray()); + } + else + { + pixelformat = Agl.aglChoosePixelFormat(IntPtr.Zero, 0, attribs.ToArray()); } return pixelformat; }