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 13KB

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