Panda3D
|
00001 // Filename: posixGraphicsStateGuardian.cxx 00002 // Created by: drose (14Jan12) 00003 // 00004 //////////////////////////////////////////////////////////////////// 00005 // 00006 // PANDA 3D SOFTWARE 00007 // Copyright (c) Carnegie Mellon University. All rights reserved. 00008 // 00009 // All use of this software is subject to the terms of the revised BSD 00010 // license. You should have received a copy of this license along 00011 // with this source code in a file named "LICENSE." 00012 // 00013 //////////////////////////////////////////////////////////////////// 00014 00015 #include "posixGraphicsStateGuardian.h" 00016 #include "config_glxdisplay.h" 00017 #include <dlfcn.h> 00018 00019 TypeHandle PosixGraphicsStateGuardian::_type_handle; 00020 00021 //////////////////////////////////////////////////////////////////// 00022 // Function: PosixGraphicsStateGuardian::Constructor 00023 // Access: Public 00024 // Description: 00025 //////////////////////////////////////////////////////////////////// 00026 PosixGraphicsStateGuardian:: 00027 PosixGraphicsStateGuardian(GraphicsEngine *engine, GraphicsPipe *pipe) : 00028 GLGraphicsStateGuardian(engine, pipe) 00029 { 00030 _libgl_handle = NULL; 00031 } 00032 00033 //////////////////////////////////////////////////////////////////// 00034 // Function: PosixGraphicsStateGuardian::Destructor 00035 // Access: Public 00036 // Description: 00037 //////////////////////////////////////////////////////////////////// 00038 PosixGraphicsStateGuardian:: 00039 ~PosixGraphicsStateGuardian() { 00040 if (_libgl_handle != (void *)NULL) { 00041 dlclose(_libgl_handle); 00042 } 00043 } 00044 00045 //////////////////////////////////////////////////////////////////// 00046 // Function: PosixGraphicsStateGuardian::do_get_extension_func 00047 // Access: Public, Virtual 00048 // Description: Returns the pointer to the GL extension function with 00049 // the indicated name. It is the responsibility of the 00050 // caller to ensure that the required extension is 00051 // defined in the OpenGL runtime prior to calling this; 00052 // it is an error to call this for a function that is 00053 // not defined. 00054 //////////////////////////////////////////////////////////////////// 00055 void *PosixGraphicsStateGuardian:: 00056 do_get_extension_func(const char *prefix, const char *name) { 00057 nassertr(prefix != NULL, NULL); 00058 nassertr(name != NULL, NULL); 00059 string fullname = string(prefix) + string(name); 00060 00061 if (glx_get_os_address) { 00062 return get_system_func(fullname.c_str()); 00063 } 00064 00065 return NULL; 00066 } 00067 00068 //////////////////////////////////////////////////////////////////// 00069 // Function: PosixGraphicsStateGuardian::get_system_func 00070 // Access: Protected 00071 // Description: Support for get_extension_func(), above, that uses 00072 // system calls to find a GL or GLX function (in the 00073 // absence of a working glxGetProcAddress() function to 00074 // call). 00075 //////////////////////////////////////////////////////////////////// 00076 void *PosixGraphicsStateGuardian:: 00077 get_system_func(const char *name) { 00078 if (_libgl_handle == (void *)NULL) { 00079 // We open the current executable, rather than naming a particular 00080 // library. Presumably libGL.so (or whatever the library should 00081 // be called) is already available in the current executable 00082 // address space, so this is more portable than insisting on a 00083 // particular shared library name. 00084 _libgl_handle = dlopen(NULL, RTLD_LAZY); 00085 nassertr(_libgl_handle != (void *)NULL, NULL); 00086 00087 // If that doesn't locate the symbol we expected, then fall back 00088 // to loading the GL library by its usual name. 00089 if (dlsym(_libgl_handle, name) == NULL) { 00090 dlclose(_libgl_handle); 00091 glxdisplay_cat.warning() 00092 << name << " not found in executable; looking in libGL.so instead.\n"; 00093 _libgl_handle = dlopen("libGL.so", RTLD_LAZY); 00094 nassertr(_libgl_handle != (void *)NULL, NULL); 00095 } 00096 } 00097 00098 return dlsym(_libgl_handle, name); 00099 }