00001 #region License
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 #endregion
00027
00028 using System;
00029
00030 using OpenTK.Graphics;
00031 using System.Diagnostics;
00032 using OpenTK.Platform.Windows;
00033
00034 namespace OpenTK.Platform.Egl
00035 {
00036 class EglContext : EmbeddedGraphicsContext
00037 {
00038 #region Fields
00039
00040 EglWindowInfo WindowInfo;
00041 IntPtr HandleAsEGLContext { get { return Handle.Handle; } set { Handle = new ContextHandle(value); } }
00042 bool vsync = true;
00043
00044 #endregion
00045
00046 #region Constructors
00047
00048 public EglContext(GraphicsMode mode, EglWindowInfo window, IGraphicsContext sharedContext,
00049 int major, int minor, GraphicsContextFlags flags)
00050 {
00051 if (mode == null)
00052 throw new ArgumentNullException("mode");
00053 if (window == null)
00054 throw new ArgumentNullException("window");
00055
00056 EglContext shared = (EglContext)sharedContext;
00057
00058 int dummy_major, dummy_minor;
00059 if (!Egl.Initialize(window.Display, out dummy_major, out dummy_minor))
00060 throw new GraphicsContextException(String.Format("Failed to initialize EGL, error {0}.", Egl.GetError()));
00061
00062 WindowInfo = window;
00063
00064 Mode = new EglGraphicsMode().SelectGraphicsMode(mode.ColorFormat, mode.Depth, mode.Stencil, mode.Samples, mode.AccumulatorFormat, mode.Buffers, mode.Stereo);
00065 if (!Mode.Index.HasValue)
00066 throw new GraphicsModeException("Invalid or unsupported GraphicsMode.");
00067 IntPtr config = Mode.Index.Value;
00068
00069 if (window.Surface == IntPtr.Zero)
00070 window.CreateWindowSurface(config);
00071
00072 int[] attrib_list = new int[] { Egl.CONTEXT_CLIENT_VERSION, major, Egl.NONE };
00073 HandleAsEGLContext = Egl.CreateContext(window.Display, config, shared != null ? shared.HandleAsEGLContext : IntPtr.Zero, attrib_list);
00074
00075 MakeCurrent(window);
00076 }
00077
00078 public EglContext(ContextHandle handle, EglWindowInfo window, IGraphicsContext sharedContext,
00079 int major, int minor, GraphicsContextFlags flags)
00080 {
00081 if (handle == ContextHandle.Zero)
00082 throw new ArgumentException("handle");
00083 if (window == null)
00084 throw new ArgumentNullException("window");
00085
00086 Handle = handle;
00087 }
00088
00089 #endregion
00090
00091 #region IGraphicsContext Members
00092
00093 public override void SwapBuffers()
00094 {
00095 Egl.SwapBuffers(WindowInfo.Display, WindowInfo.Surface);
00096 }
00097
00098 public override void MakeCurrent(IWindowInfo window)
00099 {
00100
00101
00102
00103
00104 if (window is EglWindowInfo)
00105 WindowInfo = (EglWindowInfo)window;
00106 Egl.MakeCurrent(WindowInfo.Display, WindowInfo.Surface, WindowInfo.Surface, HandleAsEGLContext);
00107 }
00108
00109 public override bool IsCurrent
00110 {
00111 get { return Egl.GetCurrentContext() == HandleAsEGLContext; }
00112 }
00113
00114 public override bool VSync
00115 {
00116 get
00117 {
00118
00119
00120 return vsync;
00121 }
00122 set
00123 {
00124 if (Egl.SwapInterval(WindowInfo.Display, value ? 1 : 0))
00125 vsync = value;
00126 else
00127 Debug.Print("[Warning] Egl.SwapInterval({0}, {1}) failed.", WindowInfo.Display, value);
00128 }
00129 }
00130
00131 #endregion
00132
00133 #region IGraphicsContextInternal Members
00134
00135 public override IntPtr GetAddress(string function)
00136 {
00137 return Egl.GetProcAddress(function);
00138 }
00139
00140 #endregion
00141
00142 #region IDisposable Members
00143
00144 public override void Dispose()
00145 {
00146 Dispose(true);
00147 GC.SuppressFinalize(this);
00148 }
00149
00150
00151
00152 void Dispose(bool manual)
00153 {
00154 if (!IsDisposed)
00155 {
00156 if (manual)
00157 {
00158 Egl.MakeCurrent(WindowInfo.Display, WindowInfo.Surface, WindowInfo.Surface, IntPtr.Zero);
00159 Egl.DestroyContext(WindowInfo.Display, HandleAsEGLContext);
00160 }
00161 else
00162 {
00163 Debug.Print("[Warning] {0}:{1} was not disposed.", this.GetType().Name, HandleAsEGLContext);
00164 }
00165 IsDisposed = true;
00166 }
00167 }
00168
00169 ~EglContext()
00170 {
00171 Dispose(false);
00172 }
00173
00174 #endregion
00175 }
00176 }