Opentk/Source/OpenTK/Platform/Egl/EglContext.cs

187 lines
6.5 KiB
C#
Raw Normal View History

#region License
//
// The Open Toolkit Library License
//
// Copyright (c) 2006 - 2009 the Open Toolkit library.
//
// 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.Diagnostics;
using OpenTK.Graphics;
namespace OpenTK.Platform.Egl
{
class EglContext : EmbeddedGraphicsContext
{
#region Fields
EglWindowInfo WindowInfo;
2010-02-03 19:04:42 +00:00
IntPtr HandleAsEGLContext { get { return Handle.Handle; } set { Handle = new ContextHandle(value); } }
int swap_interval = 1; // Default interval is defined as 1 in EGL.
#endregion
#region Constructors
public EglContext(GraphicsMode mode, EglWindowInfo window, IGraphicsContext sharedContext,
int major, int minor, GraphicsContextFlags flags)
{
if (mode == null)
throw new ArgumentNullException("mode");
if (window == null)
throw new ArgumentNullException("window");
EglContext shared = (EglContext)sharedContext;
int dummy_major, dummy_minor;
if (!Egl.Initialize(window.Display, out dummy_major, out dummy_minor))
throw new GraphicsContextException(String.Format("Failed to initialize EGL, error {0}.", Egl.GetError()));
WindowInfo = window;
// Select an EGLConfig that matches the desired mode. We cannot use the 'mode'
// parameter directly, since it may have originated on a different system (e.g. GLX)
// and it may not support the desired renderer.
Mode = new EglGraphicsMode().SelectGraphicsMode(mode.ColorFormat,
mode.Depth, mode.Stencil, mode.Samples, mode.AccumulatorFormat,
mode.Buffers, mode.Stereo,
major > 1 ? RenderableFlags.ES2 : RenderableFlags.ES);
if (!Mode.Index.HasValue)
throw new GraphicsModeException("Invalid or unsupported GraphicsMode.");
2010-02-03 19:04:42 +00:00
IntPtr config = Mode.Index.Value;
2010-02-03 19:04:42 +00:00
if (window.Surface == IntPtr.Zero)
window.CreateWindowSurface(config);
int[] attrib_list = new int[] { Egl.CONTEXT_CLIENT_VERSION, major, Egl.NONE };
2010-02-03 19:04:42 +00:00
HandleAsEGLContext = Egl.CreateContext(window.Display, config, shared != null ? shared.HandleAsEGLContext : IntPtr.Zero, attrib_list);
MakeCurrent(window);
}
2009-11-04 17:00:52 +00:00
public EglContext(ContextHandle handle, EglWindowInfo window, IGraphicsContext sharedContext,
int major, int minor, GraphicsContextFlags flags)
{
if (handle == ContextHandle.Zero)
throw new ArgumentException("handle");
if (window == null)
throw new ArgumentNullException("window");
Handle = handle;
}
#endregion
#region IGraphicsContext Members
public override void SwapBuffers()
{
Egl.SwapBuffers(WindowInfo.Display, WindowInfo.Surface);
}
public override void MakeCurrent(IWindowInfo window)
{
// Ignore 'window', unless it actually is an EGL window. In other words,
// trying to make the EglContext current on a non-EGL window will do,
// nothing (the EglContext will remain current on the previous EGL window
// or the window it was constructed on (which may not be EGL)).
if (window is EglWindowInfo)
WindowInfo = (EglWindowInfo)window;
Egl.MakeCurrent(WindowInfo.Display, WindowInfo.Surface, WindowInfo.Surface, HandleAsEGLContext);
}
public override bool IsCurrent
{
2010-02-03 19:04:42 +00:00
get { return Egl.GetCurrentContext() == HandleAsEGLContext; }
}
public override int SwapInterval
{
get
{
// Egl.GetSwapInterval does not exist, so store and return the current interval.
// The default interval is defined as 1 (true).
return swap_interval;
}
set
{
if (value < 0)
{
// EGL does not offer EXT_swap_control_tear yet
value = 1;
}
if (Egl.SwapInterval(WindowInfo.Display, value))
swap_interval = value;
else
Debug.Print("[Warning] Egl.SwapInterval({0}, {1}) failed.", WindowInfo.Display, value);
}
}
#endregion
#region IGraphicsContextInternal Members
public override IntPtr GetAddress(IntPtr function)
{
return Egl.GetProcAddress(function);
}
#endregion
#region IDisposable Members
public override void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
// Todo: cross-reference the specs. What should happen if the context is destroyed from a different
// thread?
void Dispose(bool manual)
{
if (!IsDisposed)
{
if (manual)
{
2010-02-03 19:04:42 +00:00
Egl.MakeCurrent(WindowInfo.Display, WindowInfo.Surface, WindowInfo.Surface, IntPtr.Zero);
Egl.DestroyContext(WindowInfo.Display, HandleAsEGLContext);
}
else
{
2010-02-03 19:04:42 +00:00
Debug.Print("[Warning] {0}:{1} was not disposed.", this.GetType().Name, HandleAsEGLContext);
}
IsDisposed = true;
}
}
~EglContext()
{
Dispose(false);
}
#endregion
}
}