Panda3D
 All Classes Functions Variables Enumerations
posixGraphicsStateGuardian.cxx
1 // Filename: posixGraphicsStateGuardian.cxx
2 // Created by: drose (14Jan12)
3 //
4 ////////////////////////////////////////////////////////////////////
5 //
6 // PANDA 3D SOFTWARE
7 // Copyright (c) Carnegie Mellon University. All rights reserved.
8 //
9 // All use of this software is subject to the terms of the revised BSD
10 // license. You should have received a copy of this license along
11 // with this source code in a file named "LICENSE."
12 //
13 ////////////////////////////////////////////////////////////////////
14 
15 #include "posixGraphicsStateGuardian.h"
16 #include "config_glxdisplay.h"
17 #include <dlfcn.h>
18 
19 TypeHandle PosixGraphicsStateGuardian::_type_handle;
20 
21 ////////////////////////////////////////////////////////////////////
22 // Function: PosixGraphicsStateGuardian::Constructor
23 // Access: Public
24 // Description:
25 ////////////////////////////////////////////////////////////////////
26 PosixGraphicsStateGuardian::
27 PosixGraphicsStateGuardian(GraphicsEngine *engine, GraphicsPipe *pipe) :
28  GLGraphicsStateGuardian(engine, pipe)
29 {
30  _libgl_handle = NULL;
31 }
32 
33 ////////////////////////////////////////////////////////////////////
34 // Function: PosixGraphicsStateGuardian::Destructor
35 // Access: Public
36 // Description:
37 ////////////////////////////////////////////////////////////////////
38 PosixGraphicsStateGuardian::
39 ~PosixGraphicsStateGuardian() {
40  if (_libgl_handle != (void *)NULL) {
41  dlclose(_libgl_handle);
42  }
43 }
44 
45 ////////////////////////////////////////////////////////////////////
46 // Function: PosixGraphicsStateGuardian::do_get_extension_func
47 // Access: Public, Virtual
48 // Description: Returns the pointer to the GL extension function with
49 // the indicated name. It is the responsibility of the
50 // caller to ensure that the required extension is
51 // defined in the OpenGL runtime prior to calling this;
52 // it is an error to call this for a function that is
53 // not defined.
54 ////////////////////////////////////////////////////////////////////
55 void *PosixGraphicsStateGuardian::
56 do_get_extension_func(const char *name) {
57  nassertr(name != NULL, NULL);
58 
59  if (glx_get_os_address) {
60  return get_system_func(name);
61  }
62 
63  return NULL;
64 }
65 
66 ////////////////////////////////////////////////////////////////////
67 // Function: PosixGraphicsStateGuardian::get_system_func
68 // Access: Protected
69 // Description: Support for get_extension_func(), above, that uses
70 // system calls to find a GL or GLX function (in the
71 // absence of a working glxGetProcAddress() function to
72 // call).
73 ////////////////////////////////////////////////////////////////////
74 void *PosixGraphicsStateGuardian::
75 get_system_func(const char *name) {
76  if (_libgl_handle == (void *)NULL) {
77  // We open the current executable, rather than naming a particular
78  // library. Presumably libGL.so (or whatever the library should
79  // be called) is already available in the current executable
80  // address space, so this is more portable than insisting on a
81  // particular shared library name.
82  _libgl_handle = dlopen(NULL, RTLD_LAZY);
83  nassertr(_libgl_handle != (void *)NULL, NULL);
84 
85  // If that doesn't locate the symbol we expected, then fall back
86  // to loading the GL library by its usual name.
87  if (dlsym(_libgl_handle, name) == NULL) {
88  dlclose(_libgl_handle);
89  glxdisplay_cat.warning()
90  << name << " not found in executable; looking in libGL.so instead.\n";
91  _libgl_handle = dlopen("libGL.so", RTLD_LAZY);
92  nassertr(_libgl_handle != (void *)NULL, NULL);
93  }
94  }
95 
96  return dlsym(_libgl_handle, name);
97 }
An object to create GraphicsOutputs that share a particular 3-D API.
Definition: graphicsPipe.h:58
This class is the main interface to controlling the render process.
TypeHandle is the identifier used to differentiate C++ class types.
Definition: typeHandle.h:85