Clone of mesa.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pbutil.c 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. /*
  2. * OpenGL pbuffers utility functions.
  3. *
  4. * Brian Paul
  5. * Original code: April 1997
  6. * Updated on 5 October 2002
  7. * Updated again on 3 January 2005 to use GLX 1.3 functions in preference
  8. * to the GLX_SGIX_fbconfig/pbuffer extensions.
  9. */
  10. #include <stdio.h>
  11. #include <string.h>
  12. #include "pbutil.h"
  13. /**
  14. * Test if we pixel buffers are available for a particular X screen.
  15. * Input: dpy - the X display
  16. * screen - screen number
  17. * Return: 0 = fbconfigs not available.
  18. * 1 = fbconfigs are available via GLX 1.3.
  19. * 2 = fbconfigs and pbuffers are available via GLX_SGIX_fbconfig
  20. */
  21. int
  22. QueryFBConfig(Display *dpy, int screen)
  23. {
  24. #if defined(GLX_VERSION_1_3)
  25. {
  26. /* GLX 1.3 supports pbuffers */
  27. int glxVersionMajor, glxVersionMinor;
  28. if (!glXQueryVersion(dpy, &glxVersionMajor, &glxVersionMinor)) {
  29. /* GLX not available! */
  30. return 0;
  31. }
  32. if (glxVersionMajor * 100 + glxVersionMinor >= 103) {
  33. return 1;
  34. }
  35. /* fall-through */
  36. }
  37. #endif
  38. /* Try the SGIX extensions */
  39. {
  40. char *extensions;
  41. extensions = (char *) glXQueryServerString(dpy, screen, GLX_EXTENSIONS);
  42. if (extensions && strstr(extensions,"GLX_SGIX_fbconfig")) {
  43. return 2;
  44. }
  45. }
  46. return 0;
  47. }
  48. /**
  49. * Test if we pixel buffers are available for a particular X screen.
  50. * Input: dpy - the X display
  51. * screen - screen number
  52. * Return: 0 = pixel buffers not available.
  53. * 1 = pixel buffers are available via GLX 1.3.
  54. * 2 = pixel buffers are available via GLX_SGIX_fbconfig/pbuffer.
  55. */
  56. int
  57. QueryPbuffers(Display *dpy, int screen)
  58. {
  59. int ret;
  60. ret = QueryFBConfig(dpy, screen);
  61. if (ret == 2) {
  62. char *extensions;
  63. extensions = (char *) glXQueryServerString(dpy, screen, GLX_EXTENSIONS);
  64. if (extensions && strstr(extensions, "GLX_SGIX_pbuffer"))
  65. return 2;
  66. else
  67. return 0;
  68. }
  69. else
  70. return ret;
  71. }
  72. FBCONFIG *
  73. ChooseFBConfig(Display *dpy, int screen, const int attribs[], int *nConfigs)
  74. {
  75. int fbcSupport = QueryPbuffers(dpy, screen);
  76. #if defined(GLX_VERSION_1_3)
  77. if (fbcSupport == 1) {
  78. return glXChooseFBConfig(dpy, screen, attribs, nConfigs);
  79. }
  80. #endif
  81. #if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer)
  82. if (fbcSupport == 2) {
  83. return glXChooseFBConfigSGIX(dpy, screen, (int *) attribs, nConfigs);
  84. }
  85. #endif
  86. return NULL;
  87. }
  88. FBCONFIG *
  89. GetAllFBConfigs(Display *dpy, int screen, int *nConfigs)
  90. {
  91. int fbcSupport = QueryFBConfig(dpy, screen);
  92. #if defined(GLX_VERSION_1_3)
  93. if (fbcSupport == 1) {
  94. return glXGetFBConfigs(dpy, screen, nConfigs);
  95. }
  96. #endif
  97. #if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer)
  98. if (fbcSupport == 2) {
  99. /* The GLX_SGIX_fbconfig extensions says to pass NULL to get list
  100. * of all available configurations.
  101. */
  102. return glXChooseFBConfigSGIX(dpy, screen, NULL, nConfigs);
  103. }
  104. #endif
  105. return NULL;
  106. }
  107. XVisualInfo *
  108. GetVisualFromFBConfig(Display *dpy, int screen, FBCONFIG config)
  109. {
  110. int fbcSupport = QueryFBConfig(dpy, screen);
  111. #if defined(GLX_VERSION_1_3)
  112. if (fbcSupport == 1) {
  113. return glXGetVisualFromFBConfig(dpy, config);
  114. }
  115. #endif
  116. #if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer)
  117. if (fbcSupport == 2) {
  118. return glXGetVisualFromFBConfigSGIX(dpy, config);
  119. }
  120. #endif
  121. return NULL;
  122. }
  123. /**
  124. * Either use glXGetFBConfigAttrib() or glXGetFBConfigAttribSGIX()
  125. * to query an fbconfig attribute.
  126. */
  127. static int
  128. GetFBConfigAttrib(Display *dpy, int screen,
  129. #if defined(GLX_VERSION_1_3)
  130. const GLXFBConfig config,
  131. #elif defined(GLX_SGIX_fbconfig)
  132. const GLXFBConfigSGIX config,
  133. #endif
  134. int attrib
  135. )
  136. {
  137. int fbcSupport = QueryFBConfig(dpy, screen);
  138. int value = 0;
  139. #if defined(GLX_VERSION_1_3)
  140. if (fbcSupport == 1) {
  141. /* ok */
  142. if (glXGetFBConfigAttrib(dpy, config, attrib, &value) != 0) {
  143. value = 0;
  144. }
  145. return value;
  146. }
  147. /* fall-through */
  148. #endif
  149. #if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer)
  150. if (fbcSupport == 2) {
  151. if (glXGetFBConfigAttribSGIX(dpy, config, attrib, &value) != 0) {
  152. value = 0;
  153. }
  154. return value;
  155. }
  156. #endif
  157. return value;
  158. }
  159. /**
  160. * Print parameters for a GLXFBConfig to stdout.
  161. * Input: dpy - the X display
  162. * screen - the X screen number
  163. * fbConfig - the fbconfig handle
  164. * horizFormat - if true, print in horizontal format
  165. */
  166. void
  167. PrintFBConfigInfo(Display *dpy, int screen, FBCONFIG config, Bool horizFormat)
  168. {
  169. PBUFFER pBuffer;
  170. int width=2, height=2;
  171. int bufferSize, level, doubleBuffer, stereo, auxBuffers;
  172. int redSize, greenSize, blueSize, alphaSize;
  173. int depthSize, stencilSize;
  174. int accumRedSize, accumBlueSize, accumGreenSize, accumAlphaSize;
  175. int sampleBuffers, samples;
  176. int drawableType, renderType, xRenderable, xVisual, id;
  177. int maxWidth, maxHeight, maxPixels;
  178. int optWidth, optHeight;
  179. int floatComponents = 0;
  180. /* do queries using the GLX 1.3 tokens (same as the SGIX tokens) */
  181. bufferSize = GetFBConfigAttrib(dpy, screen, config, GLX_BUFFER_SIZE);
  182. level = GetFBConfigAttrib(dpy, screen, config, GLX_LEVEL);
  183. doubleBuffer = GetFBConfigAttrib(dpy, screen, config, GLX_DOUBLEBUFFER);
  184. stereo = GetFBConfigAttrib(dpy, screen, config, GLX_STEREO);
  185. auxBuffers = GetFBConfigAttrib(dpy, screen, config, GLX_AUX_BUFFERS);
  186. redSize = GetFBConfigAttrib(dpy, screen, config, GLX_RED_SIZE);
  187. greenSize = GetFBConfigAttrib(dpy, screen, config, GLX_GREEN_SIZE);
  188. blueSize = GetFBConfigAttrib(dpy, screen, config, GLX_BLUE_SIZE);
  189. alphaSize = GetFBConfigAttrib(dpy, screen, config, GLX_ALPHA_SIZE);
  190. depthSize = GetFBConfigAttrib(dpy, screen, config, GLX_DEPTH_SIZE);
  191. stencilSize = GetFBConfigAttrib(dpy, screen, config, GLX_STENCIL_SIZE);
  192. accumRedSize = GetFBConfigAttrib(dpy, screen, config, GLX_ACCUM_RED_SIZE);
  193. accumGreenSize = GetFBConfigAttrib(dpy, screen, config, GLX_ACCUM_GREEN_SIZE);
  194. accumBlueSize = GetFBConfigAttrib(dpy, screen, config, GLX_ACCUM_BLUE_SIZE);
  195. accumAlphaSize = GetFBConfigAttrib(dpy, screen, config, GLX_ACCUM_ALPHA_SIZE);
  196. sampleBuffers = GetFBConfigAttrib(dpy, screen, config, GLX_SAMPLE_BUFFERS);
  197. samples = GetFBConfigAttrib(dpy, screen, config, GLX_SAMPLES);
  198. drawableType = GetFBConfigAttrib(dpy, screen, config, GLX_DRAWABLE_TYPE);
  199. renderType = GetFBConfigAttrib(dpy, screen, config, GLX_RENDER_TYPE);
  200. xRenderable = GetFBConfigAttrib(dpy, screen, config, GLX_X_RENDERABLE);
  201. xVisual = GetFBConfigAttrib(dpy, screen, config, GLX_X_VISUAL_TYPE);
  202. if (!xRenderable || !(drawableType & GLX_WINDOW_BIT_SGIX))
  203. xVisual = -1;
  204. id = GetFBConfigAttrib(dpy, screen, config, GLX_FBCONFIG_ID);
  205. maxWidth = GetFBConfigAttrib(dpy, screen, config, GLX_MAX_PBUFFER_WIDTH);
  206. maxHeight = GetFBConfigAttrib(dpy, screen, config, GLX_MAX_PBUFFER_HEIGHT);
  207. maxPixels = GetFBConfigAttrib(dpy, screen, config, GLX_MAX_PBUFFER_PIXELS);
  208. #if defined(GLX_SGIX_pbuffer)
  209. optWidth = GetFBConfigAttrib(dpy, screen, config, GLX_OPTIMAL_PBUFFER_WIDTH_SGIX);
  210. optHeight = GetFBConfigAttrib(dpy, screen, config, GLX_OPTIMAL_PBUFFER_HEIGHT_SGIX);
  211. #else
  212. optWidth = optHeight = 0;
  213. #endif
  214. #if defined(GLX_NV_float_buffer)
  215. floatComponents = GetFBConfigAttrib(dpy, screen, config, GLX_FLOAT_COMPONENTS_NV);
  216. #endif
  217. /* See if we can create a pbuffer with this config */
  218. pBuffer = CreatePbuffer(dpy, screen, config, width, height, False, False);
  219. if (horizFormat) {
  220. printf("0x%-9x ", id);
  221. if (xVisual==GLX_STATIC_GRAY) printf("StaticGray ");
  222. else if (xVisual==GLX_GRAY_SCALE) printf("GrayScale ");
  223. else if (xVisual==GLX_STATIC_COLOR) printf("StaticColor ");
  224. else if (xVisual==GLX_PSEUDO_COLOR) printf("PseudoColor ");
  225. else if (xVisual==GLX_TRUE_COLOR) printf("TrueColor ");
  226. else if (xVisual==GLX_DIRECT_COLOR) printf("DirectColor ");
  227. else printf(" -none- ");
  228. printf(" %3d %3d %s %s %s %2s ", bufferSize, level,
  229. (renderType & GLX_RGBA_BIT_SGIX) ? "y" : ".",
  230. (renderType & GLX_COLOR_INDEX_BIT_SGIX) ? "y" : ".",
  231. doubleBuffer ? "y" : ".",
  232. stereo ? "y" : ".");
  233. printf("%2d %2d %2d %2d ", redSize, greenSize, blueSize, alphaSize);
  234. printf("%2d %2d ", depthSize, stencilSize);
  235. printf("%2d %2d %2d %2d", accumRedSize, accumGreenSize, accumBlueSize,
  236. accumAlphaSize);
  237. printf(" %2d %2d", sampleBuffers, samples);
  238. printf(" %s %c", pBuffer ? "y" : ".",
  239. ".y"[floatComponents]);
  240. printf("\n");
  241. }
  242. else {
  243. printf("Id 0x%x\n", id);
  244. printf(" Buffer Size: %d\n", bufferSize);
  245. printf(" Level: %d\n", level);
  246. printf(" Double Buffer: %s\n", doubleBuffer ? "yes" : "no");
  247. printf(" Stereo: %s\n", stereo ? "yes" : "no");
  248. printf(" Aux Buffers: %d\n", auxBuffers);
  249. printf(" Red Size: %d\n", redSize);
  250. printf(" Green Size: %d\n", greenSize);
  251. printf(" Blue Size: %d\n", blueSize);
  252. printf(" Alpha Size: %d\n", alphaSize);
  253. printf(" Depth Size: %d\n", depthSize);
  254. printf(" Stencil Size: %d\n", stencilSize);
  255. printf(" Accum Red Size: %d\n", accumRedSize);
  256. printf(" Accum Green Size: %d\n", accumGreenSize);
  257. printf(" Accum Blue Size: %d\n", accumBlueSize);
  258. printf(" Accum Alpha Size: %d\n", accumAlphaSize);
  259. printf(" Sample Buffers: %d\n", sampleBuffers);
  260. printf(" Samples/Pixel: %d\n", samples);
  261. printf(" Drawable Types: ");
  262. if (drawableType & GLX_WINDOW_BIT) printf("Window ");
  263. if (drawableType & GLX_PIXMAP_BIT) printf("Pixmap ");
  264. if (drawableType & GLX_PBUFFER_BIT) printf("PBuffer");
  265. printf("\n");
  266. printf(" Render Types: ");
  267. if (renderType & GLX_RGBA_BIT_SGIX) printf("RGBA ");
  268. if (renderType & GLX_COLOR_INDEX_BIT_SGIX) printf("CI ");
  269. printf("\n");
  270. printf(" X Renderable: %s\n", xRenderable ? "yes" : "no");
  271. printf(" Pbuffer: %s\n", pBuffer ? "yes" : "no");
  272. printf(" Max Pbuffer width: %d\n", maxWidth);
  273. printf(" Max Pbuffer height: %d\n", maxHeight);
  274. printf(" Max Pbuffer pixels: %d\n", maxPixels);
  275. printf(" Optimum Pbuffer width: %d\n", optWidth);
  276. printf(" Optimum Pbuffer height: %d\n", optHeight);
  277. printf(" Float Components: %s\n", floatComponents ? "yes" : "no");
  278. }
  279. if (pBuffer) {
  280. DestroyPbuffer(dpy, screen, pBuffer);
  281. }
  282. }
  283. GLXContext
  284. CreateContext(Display *dpy, int screen, FBCONFIG config)
  285. {
  286. int fbcSupport = QueryFBConfig(dpy, screen);
  287. #if defined(GLX_VERSION_1_3)
  288. if (fbcSupport == 1) {
  289. /* GLX 1.3 */
  290. GLXContext c;
  291. c = glXCreateNewContext(dpy, config, GLX_RGBA_TYPE, NULL, True);
  292. if (!c) {
  293. /* try indirect */
  294. c = glXCreateNewContext(dpy, config, GLX_RGBA_TYPE, NULL, False);
  295. }
  296. return c;
  297. }
  298. #endif
  299. #if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer)
  300. if (fbcSupport == 2) {
  301. GLXContext c;
  302. c = glXCreateContextWithConfigSGIX(dpy, config, GLX_RGBA_TYPE_SGIX, NULL, True);
  303. if (!c) {
  304. c = glXCreateContextWithConfigSGIX(dpy, config, GLX_RGBA_TYPE_SGIX, NULL, False);
  305. }
  306. return c;
  307. }
  308. #endif
  309. return 0;
  310. }
  311. void
  312. DestroyContext(Display *dpy, GLXContext ctx)
  313. {
  314. glXDestroyContext(dpy, ctx);
  315. }
  316. /* This is only used by CreatePbuffer() */
  317. static int XErrorFlag = 0;
  318. static int HandleXError(Display *dpy, XErrorEvent *event)
  319. {
  320. XErrorFlag = 1;
  321. return 0;
  322. }
  323. /**
  324. * Create a Pbuffer. Use an X error handler to deal with potential
  325. * BadAlloc errors.
  326. *
  327. * Input: dpy - the X display
  328. * fbConfig - an FBConfig as returned by glXChooseFBConfigSGIX().
  329. * width, height - size of pixel buffer to request, in pixels.
  330. * pbAttribs - list of optional pixel buffer attributes
  331. * Return: a Pbuffer or None.
  332. */
  333. PBUFFER
  334. CreatePbuffer(Display *dpy, int screen, FBCONFIG config,
  335. int width, int height, Bool largest, Bool preserve)
  336. {
  337. int (*oldHandler)(Display *, XErrorEvent *);
  338. PBUFFER pBuffer = None;
  339. int pbSupport = QueryPbuffers(dpy, screen);
  340. /* Catch X protocol errors with our own error handler */
  341. oldHandler = XSetErrorHandler(HandleXError);
  342. XErrorFlag = 0;
  343. #if defined(GLX_VERSION_1_3)
  344. if (pbSupport == 1) {
  345. /* GLX 1.3 */
  346. int attribs[100], i = 0;
  347. attribs[i++] = GLX_PBUFFER_WIDTH;
  348. attribs[i++] = width;
  349. attribs[i++] = GLX_PBUFFER_HEIGHT;
  350. attribs[i++] = height;
  351. attribs[i++] = GLX_PRESERVED_CONTENTS;
  352. attribs[i++] = preserve;
  353. attribs[i++] = GLX_LARGEST_PBUFFER;
  354. attribs[i++] = largest;
  355. attribs[i++] = 0;
  356. pBuffer = glXCreatePbuffer(dpy, config, attribs);
  357. }
  358. else
  359. #endif
  360. #if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer)
  361. if (pbSupport == 2) {
  362. int attribs[100], i = 0;
  363. attribs[i++] = GLX_PRESERVED_CONTENTS;
  364. attribs[i++] = preserve;
  365. attribs[i++] = GLX_LARGEST_PBUFFER;
  366. attribs[i++] = largest;
  367. attribs[i++] = 0;
  368. pBuffer = glXCreateGLXPbufferSGIX(dpy, config, width, height, attribs);
  369. }
  370. else
  371. #endif
  372. {
  373. pBuffer = None;
  374. }
  375. XSync(dpy, False);
  376. /* Restore original X error handler */
  377. (void) XSetErrorHandler(oldHandler);
  378. /* Return pbuffer (may be None) */
  379. if (!XErrorFlag && pBuffer != None) {
  380. /*printf("config %d worked!\n", i);*/
  381. return pBuffer;
  382. }
  383. else {
  384. return None;
  385. }
  386. }
  387. void
  388. DestroyPbuffer(Display *dpy, int screen, PBUFFER pbuffer)
  389. {
  390. int pbSupport = QueryPbuffers(dpy, screen);
  391. #if defined(GLX_VERSION_1_3)
  392. if (pbSupport == 1) {
  393. glXDestroyPbuffer(dpy, pbuffer);
  394. return;
  395. }
  396. #endif
  397. #if defined(GLX_SGIX_fbconfig) && defined(GLX_SGIX_pbuffer)
  398. if (pbSupport == 2) {
  399. glXDestroyGLXPbufferSGIX(dpy, pbuffer);
  400. return;
  401. }
  402. #endif
  403. }