|
| 1 | +""" |
| 2 | +Linux implementation of the opengl frame |
| 3 | +""" |
| 4 | +from __future__ import print_function |
| 5 | +import logging |
| 6 | +from ctypes import c_int, c_char_p, c_void_p, cdll, POINTER, util, \ |
| 7 | + pointer, CFUNCTYPE, c_bool |
| 8 | +from OpenGL import GL, GLX |
| 9 | +from pyopengltk.base import BaseOpenGLFrame |
| 10 | + |
| 11 | +try: |
| 12 | + from OpenGL.raw._GLX import Display |
| 13 | +except: |
| 14 | + from OpenGL.raw.GLX._types import Display |
| 15 | + |
| 16 | +_log = logging.getLogger(__name__) |
| 17 | + |
| 18 | + |
| 19 | +_x11lib = cdll.LoadLibrary(util.find_library("X11")) |
| 20 | +XOpenDisplay = _x11lib.XOpenDisplay |
| 21 | +XOpenDisplay.argtypes = [c_char_p] |
| 22 | +XOpenDisplay.restype = POINTER(Display) |
| 23 | + |
| 24 | +Colormap = c_void_p |
| 25 | +# Attributes for old style creation |
| 26 | +att = [ |
| 27 | + GLX.GLX_RGBA, GLX.GLX_DOUBLEBUFFER, |
| 28 | + GLX.GLX_RED_SIZE, 4, |
| 29 | + GLX.GLX_GREEN_SIZE, 4, |
| 30 | + GLX.GLX_BLUE_SIZE, 4, |
| 31 | + GLX.GLX_DEPTH_SIZE, 16, |
| 32 | + 0, |
| 33 | +] |
| 34 | +# Attributes for newer style creations |
| 35 | +fbatt = [ |
| 36 | + GLX.GLX_X_RENDERABLE, 1, |
| 37 | + GLX.GLX_DRAWABLE_TYPE, GLX.GLX_WINDOW_BIT, |
| 38 | + GLX.GLX_RENDER_TYPE, GLX.GLX_RGBA_BIT, |
| 39 | + GLX.GLX_RED_SIZE, 1, |
| 40 | + GLX.GLX_GREEN_SIZE, 1, |
| 41 | + GLX.GLX_BLUE_SIZE, 1, |
| 42 | + GLX.GLX_DOUBLEBUFFER, 1, |
| 43 | + 0, |
| 44 | +] |
| 45 | + |
| 46 | + |
| 47 | +# Inherits the base and fills in the 3 platform dependent functions |
| 48 | +class OpenGLFrame(BaseOpenGLFrame): |
| 49 | + |
| 50 | + def tkCreateContext(self): |
| 51 | + self.__window = XOpenDisplay(self.winfo_screen().encode('utf-8')) |
| 52 | + # Check glx version: |
| 53 | + major = c_int(0) |
| 54 | + minor = c_int(0) |
| 55 | + GLX.glXQueryVersion(self.__window, major, minor) |
| 56 | + print("GLX version: %d.%d" % (major.value, minor.value)) |
| 57 | + if major.value == 1 and minor.value < 3: # e.g. 1.2 and down |
| 58 | + visual = GLX.glXChooseVisual(self.__window, 0, (GL.GLint * len(att))(* att)) |
| 59 | + if not visual: |
| 60 | + _log.error("glXChooseVisual call failed") |
| 61 | + self.__context = GLX.glXCreateContext(self.__window, |
| 62 | + visual, |
| 63 | + None, |
| 64 | + GL.GL_TRUE) |
| 65 | + GLX.glXMakeCurrent(self.__window, self._wid, self.__context) |
| 66 | + return # OUT HERE FOR 1.2 and less |
| 67 | + else: |
| 68 | + # 1.3 or higher |
| 69 | + # which screen - should it be winfo_screen instead ?? |
| 70 | + XDefaultScreen = _x11lib.XDefaultScreen |
| 71 | + XDefaultScreen.argtypes = [POINTER(Display)] |
| 72 | + XDefaultScreen.restype = c_int |
| 73 | + screen = XDefaultScreen(self.__window) |
| 74 | + print("Screen is ", screen) |
| 75 | + # Look at framebuffer configs |
| 76 | + ncfg = GL.GLint(0) |
| 77 | + cfgs = GLX.glXChooseFBConfig( |
| 78 | + self.__window, |
| 79 | + screen, |
| 80 | + (GL.GLint * len(fbatt))(* fbatt), |
| 81 | + ncfg, |
| 82 | + ) |
| 83 | + print("Number of FBconfigs", ncfg.value) |
| 84 | + # |
| 85 | + # Try to match to the current window |
| 86 | + # ... might also be possible to set this for the frame |
| 87 | + # ... but for now we just take what Tk gave us |
| 88 | + ideal = int(self.winfo_visualid(), 16) # convert from hex |
| 89 | + best = -1 |
| 90 | + for i in range(ncfg.value): |
| 91 | + vis = GLX.glXGetVisualFromFBConfig(self.__window, cfgs[i]) |
| 92 | + if ideal == vis.contents.visualid: |
| 93 | + best = i |
| 94 | + print("Got a matching visual: index %d %d xid %s" % ( |
| 95 | + best, vis.contents.visualid, hex(ideal))) |
| 96 | + if best < 0: |
| 97 | + print("oh dear - visual does not match") |
| 98 | + # Take the first in the list (should be another I guess) |
| 99 | + best = 0 |
| 100 | + # Here we insist on RGBA - but didn't check earlier |
| 101 | + self.__context = GLX.glXCreateNewContext( |
| 102 | + self.__window, |
| 103 | + cfgs[best], |
| 104 | + GLX.GLX_RGBA_TYPE, |
| 105 | + None, # share list |
| 106 | + GL.GL_TRUE, # direct |
| 107 | + ) |
| 108 | + print("Is Direct?: ", GLX.glXIsDirect(self.__window, self.__context)) |
| 109 | + # Not creating another window ... some tutorials do |
| 110 | + # print("wid: ", self._wid) |
| 111 | + # self._wid = GLX.glXCreateWindow(self.__window, cfgs[best], self._wid, None) |
| 112 | + # print("wid: ", self._wid) |
| 113 | + GLX.glXMakeContextCurrent(self.__window, self._wid, self._wid, self.__context) |
| 114 | + print("Done making a first context") |
| 115 | + extensions = GLX.glXQueryExtensionsString(self.__window, screen) |
| 116 | + # Here we quit - getting a modern context needs further work below |
| 117 | + return |
| 118 | + if "GLX_ARB_create_context" in extensions: |
| 119 | + # We can try to upgrade it ?? |
| 120 | + print("Trying to upgrade context") |
| 121 | + s = "glXCreateContextAttribsARB" |
| 122 | + p = GLX.glXGetProcAddress(c_char_p(s)) |
| 123 | + |
| 124 | + print(p) |
| 125 | + if not p: |
| 126 | + p = GLX.glXGetProcAddressARB((GL.GLubyte * len(s)).from_buffer_copy(s)) |
| 127 | + print(p) |
| 128 | + if p: |
| 129 | + print(" p is true") |
| 130 | + p.restype = GLX.GLXContext |
| 131 | + p.argtypes = [ |
| 132 | + POINTER(Display), |
| 133 | + GLX.GLXFBConfig, |
| 134 | + GLX.GLXContext, |
| 135 | + c_bool, |
| 136 | + POINTER(c_int), |
| 137 | + ] |
| 138 | + arb_attrs = fbatt[:-1] + [] |
| 139 | + |
| 140 | + # GLX.GLX_CONTEXT_MAJOR_VERSION_ARB , 3 |
| 141 | + # GLX.GLX_CONTEXT_MINOR_VERSION_ARB , 1, |
| 142 | + # 0 ] |
| 143 | + # |
| 144 | + # GLX.GLX_CONTEXT_FLAGS_ARB |
| 145 | + # GLX.GLX_CONTEXT_PROFILE_MASK_ARB |
| 146 | + # ] |
| 147 | +# import pdb |
| 148 | +# pdb.set_trace() |
| 149 | + self.__context = p( |
| 150 | + self.__window, cfgs[best], None, GL.GL_TRUE, |
| 151 | + (GL.GLint * len(arb_attrs))(* arb_attrs), |
| 152 | + ) |
| 153 | + |
| 154 | + def tkMakeCurrent(self): |
| 155 | + if self.winfo_ismapped(): |
| 156 | + GLX.glXMakeCurrent(self.__window, self._wid, self.__context) |
| 157 | + |
| 158 | + def tkSwapBuffers(self): |
| 159 | + if self.winfo_ismapped(): |
| 160 | + GLX.glXSwapBuffers(self.__window, self._wid) |
0 commit comments