/// <summary> /// Initializes a new instance of the <see cref="NativeDeviceContext"/> class. /// </summary> /// <param name='handle'> /// A <see cref="GCHandle"/> which is used to create the device context. /// </param> /// <exception cref='ArgumentNullException'> /// Is thrown when an argument passed to a method is invalid because it is <see langword="null" /> . /// </exception> /// <exception cref='InvalidOperationException'> /// Is thrown when an operation cannot be performed. /// </exception> public NativeDeviceContext(IntPtr handle) { if (handle == IntPtr.Zero) { throw new ArgumentException("handle is IntPtr.Zero"); } #if false // "Force" handle creation if (!window.IsHandleCreated && window.Handle != IntPtr.Zero) { throw new InvalidOperationException("invalid handle"); } #endif if ((_Display = Egl.GetDisplay(new IntPtr(Egl.DEFAULT_DISPLAY))) == IntPtr.Zero) { throw new InvalidOperationException("unable to get display handle"); } int[] major = new int[1], minor = new int[1]; if (Egl.Initialize(_Display, major, minor) == false) { throw new InvalidOperationException("unable to initialize the display"); } _NativeWindow = handle; _Version = new KhronosVersion(major[0], minor[0], KhronosVersion.ApiEgl); }
/// <summary> /// Initializes a new instance of the <see cref="NativeDeviceContext"/> class. /// </summary> /// <param name='window'> /// Window. /// </param> /// <exception cref='ArgumentNullException'> /// Is thrown when an argument passed to a method is invalid because it is <see langword="null" /> . /// </exception> /// <exception cref='InvalidOperationException'> /// Is thrown when an operation cannot be performed. /// </exception> public NativeDeviceContext(Control window) { if (window == null) { throw new ArgumentNullException("window"); } _Display = Egl.GetDisplay(IntPtr.Zero); Egl.Initialize(_Display, null, null); }
/// <summary> /// This is called immediately after the surface is first created. /// </summary> /// <param name="holder"> /// The SurfaceHolder whose surface is being created. /// </param> public void SurfaceCreated(ISurfaceHolder holder) { // Problem with static constructors? Ensure manual initialization Egl.Initialize(); Gl.Initialize(); // Get actual native window handle _NativeWindowHandle = ANativeWindow_fromSurface(JNIEnv.Handle, holder.Surface.Handle); // Create device context _DeviceContext = DeviceContext.Create(IntPtr.Zero, _NativeWindowHandle); _DeviceContext.IncRef(); // Set pixel format DevicePixelFormatCollection pixelFormats = _DeviceContext.PixelsFormats; DevicePixelFormat controlReqFormat = new DevicePixelFormat(); controlReqFormat.RgbaUnsigned = true; controlReqFormat.RenderWindow = true; controlReqFormat.ColorBits = 24; //controlReqFormat.DepthBits = (int)DepthBits; //controlReqFormat.StencilBits = (int)StencilBits; //controlReqFormat.MultisampleBits = (int)MultisampleBits; //controlReqFormat.DoubleBuffer = true; List <DevicePixelFormat> matchingPixelFormats = pixelFormats.Choose(controlReqFormat); if (matchingPixelFormats.Count == 0) { throw new InvalidOperationException("unable to find a suitable pixel format"); } _DeviceContext.SetPixelFormat(matchingPixelFormats[0]); // Create OpenGL context using compatibility profile if ((_RenderContext = _DeviceContext.CreateContext(IntPtr.Zero)) == IntPtr.Zero) { throw new InvalidOperationException("unable to create render context"); } // Make context current if (_DeviceContext.MakeCurrent(_RenderContext) == false) { throw new InvalidOperationException("unable to make context current"); } // Raise relative event OnContextCreated(); StartRendering(30.0f); }
/// <summary> /// Default constructor. /// </summary> /// <param name="display"> /// A <see cref="IntPtr"/> that specifies the display handle to be passed to <see cref="Egl.GetDisplay(IntPtr)"/>. /// </param> protected NativeSurface(IntPtr display) { if ((_Display = Egl.GetDisplay(display)) == IntPtr.Zero) { throw new InvalidOperationException("unable to get display handle"); } int[] major = new int[1], minor = new int[1]; if (Egl.Initialize(_Display, major, minor) == false) { throw new InvalidOperationException("unable to initialize the display"); } _EglVersion = new KhronosVersion(major[0], minor[0], KhronosVersion.ApiEgl); }
/// <summary> /// Default constructor. /// </summary> protected NativeSurface() { try { if ((_Display = Egl.GetDisplay(new IntPtr(Egl.DEFAULT_DISPLAY))) == IntPtr.Zero) { throw new InvalidOperationException("unable to get display handle"); } int[] major = new int[1], minor = new int[1]; if (Egl.Initialize(_Display, major, minor) == false) { throw new InvalidOperationException("unable to initialize the display"); } _EglVersion = new KhronosVersion(major[0], minor[0], KhronosVersion.ApiEgl); } catch { Dispose(); throw; } }