X11GLContext.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. * $Id$
  3. *
  4. * GtkGL# - OpenGL Graphics Library for the Gtk# Toolkit
  5. *
  6. * Copyright (c) 2002-2004 The Olympum Group, http://www.olympum.com/
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * - Redistributions of source code must retain the above copyright notice, this
  13. * list of conditions and the following disclaimer.
  14. *
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. *
  19. * - Neither the name of The Olympum Group nor the names of its contributors
  20. * may be used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  28. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  29. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  30. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  31. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  32. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  33. * DAMAGE.
  34. */
  35. namespace Gdk
  36. {
  37. using System;
  38. using System.Runtime.InteropServices;
  39. using System.Security;
  40. internal sealed class X11GLContext : GLContext
  41. {
  42. private const string GL_DLL = "opengl32.dll";
  43. [DllImport(GL_DLL), SuppressUnmanagedCodeSecurityAttribute]
  44. static extern bool glXMakeCurrent (IntPtr display,
  45. int drawableID,
  46. IntPtr glxcontext);
  47. [DllImport(GL_DLL), SuppressUnmanagedCodeSecurityAttribute]
  48. static extern void glXSwapBuffers (IntPtr display,
  49. int drawableID);
  50. [DllImport(GL_DLL), SuppressUnmanagedCodeSecurityAttribute]
  51. static extern IntPtr glXCreateContext (IntPtr display,
  52. IntPtr visualInfo,
  53. IntPtr shareList,
  54. bool direct);
  55. [DllImport(GL_DLL), SuppressUnmanagedCodeSecurityAttribute]
  56. static extern IntPtr glXChooseVisual (IntPtr display,
  57. int screen,
  58. int[] attribList);
  59. /*
  60. [DllImport(GL_DLL)]
  61. static extern void glXDestroyContext(IntPtr display,
  62. IntPtr context);
  63. */
  64. [DllImport("X11"), SuppressUnmanagedCodeSecurityAttribute]
  65. static extern void XFree (IntPtr reference);
  66. [DllImport("gdk-x11-2.0"), SuppressUnmanagedCodeSecurityAttribute]
  67. static extern IntPtr gdk_x11_get_default_xdisplay();
  68. [DllImport("gdk-x11-2.0"), SuppressUnmanagedCodeSecurityAttribute]
  69. static extern int gdk_x11_drawable_get_xid(IntPtr drawable);
  70. [DllImport("gdk-x11-2.0"), SuppressUnmanagedCodeSecurityAttribute]
  71. static extern int gdk_x11_get_default_screen();
  72. IntPtr xdisplay;
  73. IntPtr glxcontext;
  74. internal X11GLContext (int[] attributeList,
  75. X11GLContext share,
  76. IntPtr gdkDrawable)
  77. {
  78. // choose the visual based on attribute list
  79. xdisplay = gdk_x11_get_default_xdisplay();
  80. if (xdisplay == IntPtr.Zero)
  81. throw new SystemException("Couldn't get default display.");
  82. IntPtr visualInfo = glXChooseVisual (xdisplay,
  83. gdk_x11_get_default_screen(),
  84. attributeList);
  85. if (visualInfo == IntPtr.Zero)
  86. throw new SystemException ("No suitable glx-visual found.");
  87. // create glxcontext using visual
  88. try {
  89. IntPtr glxshare = IntPtr.Zero;
  90. if (share != null)
  91. glxshare = share.glxcontext;
  92. bool directRendering = true;
  93. //bool directRendering = false;
  94. glxcontext = glXCreateContext (xdisplay,
  95. visualInfo,
  96. glxshare,
  97. directRendering);
  98. if (glxcontext == IntPtr.Zero)
  99. throw new SystemException ("Failed to create glx-context.");
  100. } finally {
  101. XFree (visualInfo);
  102. }
  103. }
  104. ~X11GLContext ()
  105. {
  106. if(glxcontext != IntPtr.Zero) {
  107. //glXDestroyContext (xdisplay, glxcontext);
  108. glxcontext = IntPtr.Zero;
  109. }
  110. }
  111. public override bool MakeCurrent (IntPtr gdkDrawable)
  112. {
  113. int id = gdk_x11_drawable_get_xid(gdkDrawable);
  114. try {
  115. return glXMakeCurrent(xdisplay, id, glxcontext);
  116. } catch (Exception ) {
  117. return false;
  118. }
  119. }
  120. public override void SwapBuffers (IntPtr gdkDrawable)
  121. {
  122. int id = gdk_x11_drawable_get_xid(gdkDrawable);
  123. glXSwapBuffers(xdisplay, id);
  124. }
  125. }
  126. }