PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/Mesa-6.5.1/src/glx/x11/dri_glx.c

#
C | 472 lines | 269 code | 66 blank | 137 comment | 57 complexity | 8ba06f23ba6f5bf9220877bc2ef7219d MD5 | raw file
Possible License(s): LGPL-2.0
  1. /**************************************************************************
  2. Copyright 1998-1999 Precision Insight, Inc., Cedar Park, Texas.
  3. All Rights Reserved.
  4. Permission is hereby granted, free of charge, to any person obtaining a
  5. copy of this software and associated documentation files (the
  6. "Software"), to deal in the Software without restriction, including
  7. without limitation the rights to use, copy, modify, merge, publish,
  8. distribute, sub license, and/or sell copies of the Software, and to
  9. permit persons to whom the Software is furnished to do so, subject to
  10. the following conditions:
  11. The above copyright notice and this permission notice (including the
  12. next paragraph) shall be included in all copies or substantial portions
  13. of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  15. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
  17. IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
  18. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  19. TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  20. SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. **************************************************************************/
  22. /* $XFree86: xc/lib/GL/dri/dri_glx.c,v 1.14 2003/07/16 00:54:00 dawes Exp $ */
  23. /*
  24. * Authors:
  25. * Kevin E. Martin <kevin@precisioninsight.com>
  26. * Brian Paul <brian@precisioninsight.com>
  27. *
  28. */
  29. #ifdef GLX_DIRECT_RENDERING
  30. #include <unistd.h>
  31. #include <X11/Xlibint.h>
  32. #include <X11/extensions/Xext.h>
  33. #include <X11/extensions/extutil.h>
  34. #include "glxclient.h"
  35. #include "xf86dri.h"
  36. #include "sarea.h"
  37. #include <stdio.h>
  38. #include <dlfcn.h>
  39. #include "dri_glx.h"
  40. #include <sys/types.h>
  41. #include <stdarg.h>
  42. #ifndef RTLD_NOW
  43. #define RTLD_NOW 0
  44. #endif
  45. #ifndef RTLD_GLOBAL
  46. #define RTLD_GLOBAL 0
  47. #endif
  48. #ifndef DEFAULT_DRIVER_DIR
  49. /* this is normally defined in Mesa/configs/default with DRI_DRIVER_SEARCH_PATH */
  50. #define DEFAULT_DRIVER_DIR "/usr/X11R6/lib/modules/dri"
  51. #endif
  52. static __DRIdriver *Drivers = NULL;
  53. /*
  54. * printf wrappers
  55. */
  56. static void InfoMessageF(const char *f, ...)
  57. {
  58. va_list args;
  59. const char *env;
  60. if ((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) {
  61. fprintf(stderr, "libGL: ");
  62. va_start(args, f);
  63. vfprintf(stderr, f, args);
  64. va_end(args);
  65. }
  66. }
  67. static void ErrorMessageF(const char *f, ...)
  68. {
  69. va_list args;
  70. if (getenv("LIBGL_DEBUG")) {
  71. fprintf(stderr, "libGL error: ");
  72. va_start(args, f);
  73. vfprintf(stderr, f, args);
  74. va_end(args);
  75. }
  76. }
  77. /**
  78. * Extract the ith directory path out of a colon-separated list of paths. No
  79. * more than \c dirLen characters, including the terminating \c NUL, will be
  80. * written to \c dir.
  81. *
  82. * \param index Index of path to extract (starting at zero)
  83. * \param paths The colon-separated list of paths
  84. * \param dirLen Maximum length of result to store in \c dir
  85. * \param dir Buffer to hold the extracted directory path
  86. *
  87. * \returns
  88. * The number of characters that would have been written to \c dir had there
  89. * been enough room. This does not include the terminating \c NUL. When
  90. * extraction fails, zero will be returned.
  91. *
  92. * \todo
  93. * It seems like this function could be rewritten to use \c strchr.
  94. */
  95. static size_t
  96. ExtractDir(int index, const char *paths, int dirLen, char *dir)
  97. {
  98. int i, len;
  99. const char *start, *end;
  100. /* find ith colon */
  101. start = paths;
  102. i = 0;
  103. while (i < index) {
  104. if (*start == ':') {
  105. i++;
  106. start++;
  107. }
  108. else if (*start == 0) {
  109. /* end of string and couldn't find ith colon */
  110. dir[0] = 0;
  111. return 0;
  112. }
  113. else {
  114. start++;
  115. }
  116. }
  117. while (*start == ':')
  118. start++;
  119. /* find next colon, or end of string */
  120. end = start + 1;
  121. while (*end != ':' && *end != 0) {
  122. end++;
  123. }
  124. /* copy string between <start> and <end> into result string */
  125. len = end - start;
  126. if (len > dirLen - 1)
  127. len = dirLen - 1;
  128. strncpy(dir, start, len);
  129. dir[len] = 0;
  130. return( end - start );
  131. }
  132. /**
  133. * Versioned name of the expected \c __driCreateNewScreen function.
  134. *
  135. * The version of the last incompatible loader/driver inteface change is
  136. * appended to the name of the \c __driCreateNewScreen function. This
  137. * prevents loaders from trying to load drivers that are too old.
  138. *
  139. * \todo
  140. * Create a macro or something so that this is automatically updated.
  141. */
  142. static const char createNewScreenName[] = "__driCreateNewScreen_20050727";
  143. /**
  144. * Try to \c dlopen the named driver.
  145. *
  146. * This function adds the "_dri.so" suffix to the driver name and searches the
  147. * directories specified by the \c LIBGL_DRIVERS_PATH environment variable in
  148. * order to find the driver.
  149. *
  150. * \param driverName - a name like "tdfx", "i810", "mga", etc.
  151. *
  152. * \returns
  153. * A handle from \c dlopen, or \c NULL if driver file not found.
  154. */
  155. static __DRIdriver *OpenDriver(const char *driverName)
  156. {
  157. void *glhandle = NULL;
  158. char *libPaths = NULL;
  159. char libDir[1000];
  160. int i;
  161. __DRIdriver *driver;
  162. /* First, search Drivers list to see if we've already opened this driver */
  163. for (driver = Drivers; driver; driver = driver->next) {
  164. if (strcmp(driver->name, driverName) == 0) {
  165. /* found it */
  166. return driver;
  167. }
  168. }
  169. /* Attempt to make sure libGL symbols will be visible to the driver */
  170. glhandle = dlopen("libGL.so.1", RTLD_NOW | RTLD_GLOBAL);
  171. if (geteuid() == getuid()) {
  172. /* don't allow setuid apps to use LIBGL_DRIVERS_PATH */
  173. libPaths = getenv("LIBGL_DRIVERS_PATH");
  174. if (!libPaths)
  175. libPaths = getenv("LIBGL_DRIVERS_DIR"); /* deprecated */
  176. }
  177. if (!libPaths)
  178. libPaths = DEFAULT_DRIVER_DIR;
  179. for ( i = 0 ; ExtractDir(i, libPaths, 1000, libDir) != 0 ; i++ ) {
  180. char realDriverName[200];
  181. void *handle = NULL;
  182. /* If TLS support is enabled, try to open the TLS version of the driver
  183. * binary first. If that fails, try the non-TLS version.
  184. */
  185. #ifdef GLX_USE_TLS
  186. snprintf(realDriverName, 200, "%s/tls/%s_dri.so", libDir, driverName);
  187. InfoMessageF("OpenDriver: trying %s\n", realDriverName);
  188. handle = dlopen(realDriverName, RTLD_NOW | RTLD_GLOBAL);
  189. #endif
  190. if ( handle == NULL ) {
  191. snprintf(realDriverName, 200, "%s/%s_dri.so", libDir, driverName);
  192. InfoMessageF("OpenDriver: trying %s\n", realDriverName);
  193. handle = dlopen(realDriverName, RTLD_NOW | RTLD_GLOBAL);
  194. }
  195. if ( handle != NULL ) {
  196. /* allocate __DRIdriver struct */
  197. driver = (__DRIdriver *) Xmalloc(sizeof(__DRIdriver));
  198. if (!driver)
  199. break; /* out of memory! */
  200. /* init the struct */
  201. driver->name = __glXstrdup(driverName);
  202. if (!driver->name) {
  203. Xfree(driver);
  204. driver = NULL;
  205. break; /* out of memory! */
  206. }
  207. driver->createNewScreenFunc = (PFNCREATENEWSCREENFUNC)
  208. dlsym(handle, createNewScreenName);
  209. if ( driver->createNewScreenFunc == NULL ) {
  210. /* If the driver doesn't have this symbol then something's
  211. * really, really wrong.
  212. */
  213. ErrorMessageF("%s not defined in %s_dri.so!\n"
  214. "Your driver may be too old for this libGL.\n",
  215. createNewScreenName, driverName);
  216. Xfree(driver);
  217. driver = NULL;
  218. dlclose(handle);
  219. continue;
  220. }
  221. driver->handle = handle;
  222. /* put at head of linked list */
  223. driver->next = Drivers;
  224. Drivers = driver;
  225. break;
  226. }
  227. else {
  228. ErrorMessageF("dlopen %s failed (%s)\n", realDriverName, dlerror());
  229. }
  230. }
  231. if (!driver)
  232. ErrorMessageF("unable to load driver: %s_dri.so\n", driverName);
  233. if (glhandle)
  234. dlclose(glhandle);
  235. return driver;
  236. }
  237. /*
  238. * Given a display pointer and screen number, determine the name of
  239. * the DRI driver for the screen. (I.e. "r128", "tdfx", etc).
  240. * Return True for success, False for failure.
  241. */
  242. static Bool GetDriverName(Display *dpy, int scrNum, char **driverName)
  243. {
  244. int directCapable;
  245. Bool b;
  246. int driverMajor, driverMinor, driverPatch;
  247. *driverName = NULL;
  248. if (!XF86DRIQueryDirectRenderingCapable(dpy, scrNum, &directCapable)) {
  249. ErrorMessageF("XF86DRIQueryDirectRenderingCapable failed\n");
  250. return False;
  251. }
  252. if (!directCapable) {
  253. ErrorMessageF("XF86DRIQueryDirectRenderingCapable returned false\n");
  254. return False;
  255. }
  256. b = XF86DRIGetClientDriverName(dpy, scrNum, &driverMajor, &driverMinor,
  257. &driverPatch, driverName);
  258. if (!b) {
  259. ErrorMessageF("Cannot determine driver name for screen %d\n", scrNum);
  260. return False;
  261. }
  262. InfoMessageF("XF86DRIGetClientDriverName: %d.%d.%d %s (screen %d)\n",
  263. driverMajor, driverMinor, driverPatch, *driverName, scrNum);
  264. return True;
  265. }
  266. /*
  267. * Given a display pointer and screen number, return a __DRIdriver handle.
  268. * Return NULL if anything goes wrong.
  269. */
  270. __DRIdriver *driGetDriver(Display *dpy, int scrNum)
  271. {
  272. char *driverName;
  273. if (GetDriverName(dpy, scrNum, &driverName)) {
  274. __DRIdriver *ret;
  275. ret = OpenDriver(driverName);
  276. if (driverName)
  277. Xfree(driverName);
  278. return ret;
  279. }
  280. return NULL;
  281. }
  282. /*
  283. * Exported function for querying the DRI driver for a given screen.
  284. *
  285. * The returned char pointer points to a static array that will be
  286. * overwritten by subsequent calls.
  287. */
  288. const char *glXGetScreenDriver (Display *dpy, int scrNum) {
  289. static char ret[32];
  290. char *driverName;
  291. if (GetDriverName(dpy, scrNum, &driverName)) {
  292. int len;
  293. if (!driverName)
  294. return NULL;
  295. len = strlen (driverName);
  296. if (len >= 31)
  297. return NULL;
  298. memcpy (ret, driverName, len+1);
  299. Xfree(driverName);
  300. return ret;
  301. }
  302. return NULL;
  303. }
  304. /*
  305. * Exported function for obtaining a driver's option list (UTF-8 encoded XML).
  306. *
  307. * The returned char pointer points directly into the driver. Therefore
  308. * it should be treated as a constant.
  309. *
  310. * If the driver was not found or does not support configuration NULL is
  311. * returned.
  312. *
  313. * Note: The driver remains opened after this function returns.
  314. */
  315. const char *glXGetDriverConfig (const char *driverName) {
  316. __DRIdriver *driver = OpenDriver (driverName);
  317. if (driver)
  318. return dlsym (driver->handle, "__driConfigOptions");
  319. else
  320. return NULL;
  321. }
  322. /* This function isn't currently used.
  323. */
  324. static void driDestroyDisplay(Display *dpy, void *private)
  325. {
  326. __DRIdisplayPrivate *pdpyp = (__DRIdisplayPrivate *)private;
  327. if (pdpyp) {
  328. const int numScreens = ScreenCount(dpy);
  329. int i;
  330. for (i = 0; i < numScreens; i++) {
  331. if (pdpyp->libraryHandles[i])
  332. dlclose(pdpyp->libraryHandles[i]);
  333. }
  334. Xfree(pdpyp->libraryHandles);
  335. Xfree(pdpyp);
  336. }
  337. }
  338. /*
  339. * Allocate, initialize and return a __DRIdisplayPrivate object.
  340. * This is called from __glXInitialize() when we are given a new
  341. * display pointer.
  342. */
  343. void *driCreateDisplay(Display *dpy, __DRIdisplay *pdisp)
  344. {
  345. const int numScreens = ScreenCount(dpy);
  346. __DRIdisplayPrivate *pdpyp;
  347. int eventBase, errorBase;
  348. int major, minor, patch;
  349. int scrn;
  350. /* Initialize these fields to NULL in case we fail.
  351. * If we don't do this we may later get segfaults trying to free random
  352. * addresses when the display is closed.
  353. */
  354. pdisp->private = NULL;
  355. pdisp->destroyDisplay = NULL;
  356. if (!XF86DRIQueryExtension(dpy, &eventBase, &errorBase)) {
  357. return NULL;
  358. }
  359. if (!XF86DRIQueryVersion(dpy, &major, &minor, &patch)) {
  360. return NULL;
  361. }
  362. pdpyp = (__DRIdisplayPrivate *)Xmalloc(sizeof(__DRIdisplayPrivate));
  363. if (!pdpyp) {
  364. return NULL;
  365. }
  366. pdpyp->driMajor = major;
  367. pdpyp->driMinor = minor;
  368. pdpyp->driPatch = patch;
  369. pdisp->destroyDisplay = driDestroyDisplay;
  370. /* allocate array of pointers to createNewScreen funcs */
  371. pdisp->createNewScreen = (PFNCREATENEWSCREENFUNC *)
  372. Xmalloc(numScreens * sizeof(void *));
  373. if (!pdisp->createNewScreen) {
  374. Xfree(pdpyp);
  375. return NULL;
  376. }
  377. /* allocate array of library handles */
  378. pdpyp->libraryHandles = (void **) Xmalloc(numScreens * sizeof(void*));
  379. if (!pdpyp->libraryHandles) {
  380. Xfree(pdisp->createNewScreen);
  381. Xfree(pdpyp);
  382. return NULL;
  383. }
  384. /* dynamically discover DRI drivers for all screens, saving each
  385. * driver's "__driCreateScreen" function pointer. That's the bootstrap
  386. * entrypoint for all DRI drivers.
  387. */
  388. for (scrn = 0; scrn < numScreens; scrn++) {
  389. __DRIdriver *driver = driGetDriver(dpy, scrn);
  390. if (driver) {
  391. pdisp->createNewScreen[scrn] = driver->createNewScreenFunc;
  392. pdpyp->libraryHandles[scrn] = driver->handle;
  393. }
  394. else {
  395. pdisp->createNewScreen[scrn] = NULL;
  396. pdpyp->libraryHandles[scrn] = NULL;
  397. }
  398. }
  399. return (void *)pdpyp;
  400. }
  401. #endif /* GLX_DIRECT_RENDERING */