gl-x11.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. /******************************************************************************
  2. Copyright (C) 2014 by Zachary Lund <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. /* Version 2 of the GLX backend...
  15. * Difference from version 1 is that we use XCB to help alleviate
  16. * pains in a threaded environment that is prone to error.
  17. * These errors must be readable and handled for the sake of,
  18. * not only the users' sanity, but my own.
  19. *
  20. * With that said, we have more error checking capabilities...
  21. * and not all of them are used to help simplify current code.
  22. *
  23. * TODO: Implement more complete error checking.
  24. * NOTE: GLX loading functions are placed illogically
  25. * for the sake of convenience.
  26. */
  27. #include <X11/Xlib.h>
  28. #include <X11/Xlib-xcb.h>
  29. #include <xcb/xcb.h>
  30. #include <stdio.h>
  31. #include "gl-subsystem.h"
  32. #include <glad/glad_glx.h>
  33. static const int ctx_attribs[] = {
  34. #ifdef _DEBUG
  35. GLX_CONTEXT_FLAGS_ARB,
  36. GLX_CONTEXT_DEBUG_BIT_ARB,
  37. #endif
  38. GLX_CONTEXT_PROFILE_MASK_ARB,
  39. GLX_CONTEXT_CORE_PROFILE_BIT_ARB,
  40. GLX_CONTEXT_MAJOR_VERSION_ARB,
  41. 3,
  42. GLX_CONTEXT_MINOR_VERSION_ARB,
  43. 3,
  44. None,
  45. };
  46. static int ctx_pbuffer_attribs[] = {GLX_PBUFFER_WIDTH, 2, GLX_PBUFFER_HEIGHT, 2,
  47. None};
  48. static int ctx_visual_attribs[] = {GLX_STENCIL_SIZE,
  49. 0,
  50. GLX_DEPTH_SIZE,
  51. 0,
  52. GLX_BUFFER_SIZE,
  53. 32,
  54. GLX_ALPHA_SIZE,
  55. 8,
  56. GLX_DOUBLEBUFFER,
  57. true,
  58. GLX_X_RENDERABLE,
  59. true,
  60. None};
  61. struct gl_windowinfo {
  62. /* We store this value since we can fetch a lot
  63. * of information not only concerning the config
  64. * but the visual, and various other settings
  65. * for the context.
  66. */
  67. GLXFBConfig config;
  68. /* Windows in X11 are defined with integers (XID).
  69. * xcb_window_t is a define for this... they are
  70. * compatible with Xlib as well.
  71. */
  72. xcb_window_t window;
  73. /* We can't fetch screen without a request so we cache it. */
  74. int screen;
  75. };
  76. struct gl_platform {
  77. Display *display;
  78. GLXContext context;
  79. GLXPbuffer pbuffer;
  80. };
  81. /*
  82. * Since we cannot take advantage of the asynchronous nature of xcb,
  83. * all of the helper functions are synchronous but thread-safe.
  84. *
  85. * They check for errors and will return 0 on problems
  86. * with the exception of when 0 is a valid return value... in which case
  87. * read the specific function comments.
  88. */
  89. /* Returns -1 on invalid screen. */
  90. static int get_screen_num_from_xcb_screen(xcb_connection_t *xcb_conn,
  91. xcb_screen_t *screen)
  92. {
  93. xcb_screen_iterator_t iter =
  94. xcb_setup_roots_iterator(xcb_get_setup(xcb_conn));
  95. int screen_num = 0;
  96. for (; iter.rem; xcb_screen_next(&iter), ++screen_num)
  97. if (iter.data == screen)
  98. return screen_num;
  99. return -1;
  100. }
  101. static xcb_screen_t *get_screen_from_root(xcb_connection_t *xcb_conn,
  102. xcb_window_t root)
  103. {
  104. xcb_screen_iterator_t iter =
  105. xcb_setup_roots_iterator(xcb_get_setup(xcb_conn));
  106. while (iter.rem) {
  107. if (iter.data->root == root)
  108. return iter.data;
  109. xcb_screen_next(&iter);
  110. }
  111. return 0;
  112. }
  113. static inline int get_screen_num_from_root(xcb_connection_t *xcb_conn,
  114. xcb_window_t root)
  115. {
  116. xcb_screen_t *screen = get_screen_from_root(xcb_conn, root);
  117. if (!screen)
  118. return -1;
  119. return get_screen_num_from_xcb_screen(xcb_conn, screen);
  120. }
  121. static xcb_get_geometry_reply_t *get_window_geometry(xcb_connection_t *xcb_conn,
  122. xcb_drawable_t drawable)
  123. {
  124. xcb_get_geometry_cookie_t cookie;
  125. xcb_generic_error_t *error;
  126. xcb_get_geometry_reply_t *reply;
  127. cookie = xcb_get_geometry(xcb_conn, drawable);
  128. reply = xcb_get_geometry_reply(xcb_conn, cookie, &error);
  129. if (error) {
  130. blog(LOG_ERROR, "Failed to fetch parent window geometry!");
  131. free(error);
  132. free(reply);
  133. return 0;
  134. }
  135. free(error);
  136. return reply;
  137. }
  138. static bool gl_context_create(struct gl_platform *plat)
  139. {
  140. Display *display = plat->display;
  141. int frame_buf_config_count = 0;
  142. GLXFBConfig *config = NULL;
  143. GLXContext context;
  144. bool success = false;
  145. if (!GLAD_GLX_ARB_create_context) {
  146. blog(LOG_ERROR, "ARB_GLX_create_context not supported!");
  147. return false;
  148. }
  149. config = glXChooseFBConfig(display, DefaultScreen(display),
  150. ctx_visual_attribs, &frame_buf_config_count);
  151. if (!config) {
  152. blog(LOG_ERROR, "Failed to create OpenGL frame buffer config");
  153. return false;
  154. }
  155. context = glXCreateContextAttribsARB(display, config[0], NULL, true,
  156. ctx_attribs);
  157. if (!context) {
  158. blog(LOG_ERROR, "Failed to create OpenGL context.");
  159. goto error;
  160. }
  161. plat->context = context;
  162. plat->display = display;
  163. plat->pbuffer =
  164. glXCreatePbuffer(display, config[0], ctx_pbuffer_attribs);
  165. if (!plat->pbuffer) {
  166. blog(LOG_ERROR, "Failed to create OpenGL pbuffer");
  167. goto error;
  168. }
  169. success = true;
  170. error:
  171. XFree(config);
  172. XSync(display, false);
  173. return success;
  174. }
  175. static void gl_context_destroy(struct gl_platform *plat)
  176. {
  177. Display *display = plat->display;
  178. glXMakeContextCurrent(display, None, None, NULL);
  179. glXDestroyContext(display, plat->context);
  180. bfree(plat);
  181. }
  182. extern struct gl_windowinfo *
  183. gl_windowinfo_create(const struct gs_init_data *info)
  184. {
  185. UNUSED_PARAMETER(info);
  186. return bmalloc(sizeof(struct gl_windowinfo));
  187. }
  188. extern void gl_windowinfo_destroy(struct gl_windowinfo *info)
  189. {
  190. UNUSED_PARAMETER(info);
  191. bfree(info);
  192. }
  193. static Display *open_windowless_display(void)
  194. {
  195. Display *display = XOpenDisplay(NULL);
  196. xcb_connection_t *xcb_conn;
  197. xcb_screen_iterator_t screen_iterator;
  198. xcb_screen_t *screen;
  199. int screen_num;
  200. if (!display) {
  201. blog(LOG_ERROR, "Unable to open new X connection!");
  202. return NULL;
  203. }
  204. xcb_conn = XGetXCBConnection(display);
  205. if (!xcb_conn) {
  206. blog(LOG_ERROR, "Unable to get XCB connection to main display");
  207. goto error;
  208. }
  209. screen_iterator = xcb_setup_roots_iterator(xcb_get_setup(xcb_conn));
  210. screen = screen_iterator.data;
  211. if (!screen) {
  212. blog(LOG_ERROR, "Unable to get screen root");
  213. goto error;
  214. }
  215. screen_num = get_screen_num_from_root(xcb_conn, screen->root);
  216. if (screen_num == -1) {
  217. blog(LOG_ERROR, "Unable to get screen number from root");
  218. goto error;
  219. }
  220. if (!gladLoadGLX(display, screen_num)) {
  221. blog(LOG_ERROR, "Unable to load GLX entry functions.");
  222. goto error;
  223. }
  224. return display;
  225. error:
  226. if (display)
  227. XCloseDisplay(display);
  228. return NULL;
  229. }
  230. static int x_error_handler(Display *display, XErrorEvent *error)
  231. {
  232. char str1[512];
  233. char str2[512];
  234. char str3[512];
  235. XGetErrorText(display, error->error_code, str1, sizeof(str1));
  236. XGetErrorText(display, error->request_code, str2, sizeof(str2));
  237. XGetErrorText(display, error->minor_code, str3, sizeof(str3));
  238. blog(LOG_ERROR,
  239. "X Error: %s, Major opcode: %s, "
  240. "Minor opcode: %s, Serial: %lu",
  241. str1, str2, str3, error->serial);
  242. return 0;
  243. }
  244. extern struct gl_platform *gl_platform_create(gs_device_t *device,
  245. uint32_t adapter)
  246. {
  247. /* There's some trickery here... we're mixing libX11, xcb, and GLX
  248. For an explanation see here: http://xcb.freedesktop.org/MixingCalls/
  249. Essentially, GLX requires Xlib. Everything else we use xcb. */
  250. struct gl_platform *plat = bmalloc(sizeof(struct gl_platform));
  251. Display *display = open_windowless_display();
  252. if (!display) {
  253. goto fail_display_open;
  254. }
  255. XSetEventQueueOwner(display, XCBOwnsEventQueue);
  256. XSetErrorHandler(x_error_handler);
  257. /* We assume later that cur_swap is already set. */
  258. device->plat = plat;
  259. plat->display = display;
  260. if (!gl_context_create(plat)) {
  261. blog(LOG_ERROR, "Failed to create context!");
  262. goto fail_context_create;
  263. }
  264. if (!glXMakeContextCurrent(plat->display, plat->pbuffer, plat->pbuffer,
  265. plat->context)) {
  266. blog(LOG_ERROR, "Failed to make context current.");
  267. goto fail_make_current;
  268. }
  269. if (!gladLoadGL()) {
  270. blog(LOG_ERROR, "Failed to load OpenGL entry functions.");
  271. goto fail_load_gl;
  272. }
  273. goto success;
  274. fail_make_current:
  275. gl_context_destroy(plat);
  276. fail_context_create:
  277. fail_load_gl:
  278. XCloseDisplay(display);
  279. fail_display_open:
  280. bfree(plat);
  281. plat = NULL;
  282. success:
  283. UNUSED_PARAMETER(adapter);
  284. return plat;
  285. }
  286. extern void gl_platform_destroy(struct gl_platform *plat)
  287. {
  288. if (!plat) /* In what case would platform be invalid here? */
  289. return;
  290. gl_context_destroy(plat);
  291. }
  292. extern bool gl_platform_init_swapchain(struct gs_swap_chain *swap)
  293. {
  294. Display *display = swap->device->plat->display;
  295. xcb_connection_t *xcb_conn = XGetXCBConnection(display);
  296. xcb_window_t wid = xcb_generate_id(xcb_conn);
  297. xcb_window_t parent = swap->info.window.id;
  298. xcb_get_geometry_reply_t *geometry =
  299. get_window_geometry(xcb_conn, parent);
  300. bool status = false;
  301. int screen_num;
  302. int visual;
  303. GLXFBConfig *fb_config;
  304. if (!geometry)
  305. goto fail_geometry_request;
  306. screen_num = get_screen_num_from_root(xcb_conn, geometry->root);
  307. if (screen_num == -1) {
  308. goto fail_screen;
  309. }
  310. /* ...fetch the best match... */
  311. {
  312. int num_configs;
  313. fb_config = glXChooseFBConfig(display, screen_num,
  314. ctx_visual_attribs, &num_configs);
  315. if (!fb_config || !num_configs) {
  316. blog(LOG_ERROR, "Failed to find FBConfig!");
  317. goto fail_fb_config;
  318. }
  319. }
  320. /* ...then fetch matching visual info for xcb. */
  321. {
  322. int error = glXGetFBConfigAttrib(display, fb_config[0],
  323. GLX_VISUAL_ID, &visual);
  324. if (error) {
  325. blog(LOG_ERROR, "Bad call to GetFBConfigAttrib!");
  326. goto fail_visual_id;
  327. }
  328. }
  329. xcb_colormap_t colormap = xcb_generate_id(xcb_conn);
  330. uint32_t mask = XCB_CW_BORDER_PIXEL | XCB_CW_COLORMAP;
  331. uint32_t mask_values[] = {0, colormap, 0};
  332. xcb_create_colormap(xcb_conn, XCB_COLORMAP_ALLOC_NONE, colormap, parent,
  333. visual);
  334. xcb_create_window(xcb_conn, 24 /* Hardcoded? */, wid, parent, 0, 0,
  335. geometry->width, geometry->height, 0, 0, visual, mask,
  336. mask_values);
  337. swap->wi->config = fb_config[0];
  338. swap->wi->window = wid;
  339. xcb_map_window(xcb_conn, wid);
  340. XFree(fb_config);
  341. status = true;
  342. goto success;
  343. fail_visual_id:
  344. XFree(fb_config);
  345. fail_fb_config:
  346. fail_screen:
  347. fail_geometry_request:
  348. success:
  349. free(geometry);
  350. return status;
  351. }
  352. extern void gl_platform_cleanup_swapchain(struct gs_swap_chain *swap)
  353. {
  354. UNUSED_PARAMETER(swap);
  355. /* Really nothing to clean up? */
  356. }
  357. extern void device_enter_context(gs_device_t *device)
  358. {
  359. GLXContext context = device->plat->context;
  360. Display *display = device->plat->display;
  361. if (device->cur_swap) {
  362. XID window = device->cur_swap->wi->window;
  363. if (!glXMakeContextCurrent(display, window, window, context)) {
  364. blog(LOG_ERROR, "Failed to make context current.");
  365. }
  366. } else {
  367. GLXPbuffer pbuf = device->plat->pbuffer;
  368. if (!glXMakeContextCurrent(display, pbuf, pbuf, context)) {
  369. blog(LOG_ERROR, "Failed to make context current.");
  370. }
  371. }
  372. }
  373. extern void device_leave_context(gs_device_t *device)
  374. {
  375. Display *display = device->plat->display;
  376. if (!glXMakeContextCurrent(display, None, None, NULL)) {
  377. blog(LOG_ERROR, "Failed to reset current context.");
  378. }
  379. }
  380. void *device_get_device_obj(gs_device_t *device)
  381. {
  382. return device->plat->context;
  383. }
  384. extern void gl_getclientsize(const struct gs_swap_chain *swap, uint32_t *width,
  385. uint32_t *height)
  386. {
  387. xcb_connection_t *xcb_conn =
  388. XGetXCBConnection(swap->device->plat->display);
  389. xcb_window_t window = swap->wi->window;
  390. xcb_get_geometry_reply_t *geometry =
  391. get_window_geometry(xcb_conn, window);
  392. if (geometry) {
  393. *width = geometry->width;
  394. *height = geometry->height;
  395. }
  396. free(geometry);
  397. }
  398. extern void gl_update(gs_device_t *device)
  399. {
  400. Display *display = device->plat->display;
  401. xcb_window_t window = device->cur_swap->wi->window;
  402. uint32_t values[] = {device->cur_swap->info.cx,
  403. device->cur_swap->info.cy};
  404. xcb_configure_window(XGetXCBConnection(display), window,
  405. XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
  406. values);
  407. }
  408. extern void device_load_swapchain(gs_device_t *device, gs_swapchain_t *swap)
  409. {
  410. if (device->cur_swap == swap)
  411. return;
  412. Display *dpy = device->plat->display;
  413. GLXContext ctx = device->plat->context;
  414. device->cur_swap = swap;
  415. if (swap) {
  416. XID window = swap->wi->window;
  417. if (!glXMakeContextCurrent(dpy, window, window, ctx)) {
  418. blog(LOG_ERROR, "Failed to make context current.");
  419. }
  420. } else {
  421. GLXPbuffer pbuf = device->plat->pbuffer;
  422. if (!glXMakeContextCurrent(dpy, pbuf, pbuf, ctx)) {
  423. blog(LOG_ERROR, "Failed to make context current.");
  424. }
  425. }
  426. }
  427. enum swap_type {
  428. SWAP_TYPE_NORMAL,
  429. SWAP_TYPE_EXT,
  430. SWAP_TYPE_MESA,
  431. SWAP_TYPE_SGI,
  432. };
  433. extern void device_present(gs_device_t *device)
  434. {
  435. static bool initialized = false;
  436. static enum swap_type swap_type = SWAP_TYPE_NORMAL;
  437. Display *display = device->plat->display;
  438. XID window = device->cur_swap->wi->window;
  439. if (!initialized) {
  440. if (GLAD_GLX_EXT_swap_control)
  441. swap_type = SWAP_TYPE_EXT;
  442. else if (GLAD_GLX_MESA_swap_control)
  443. swap_type = SWAP_TYPE_MESA;
  444. else if (GLAD_GLX_SGI_swap_control)
  445. swap_type = SWAP_TYPE_SGI;
  446. initialized = true;
  447. }
  448. xcb_connection_t *xcb_conn = XGetXCBConnection(display);
  449. xcb_generic_event_t *xcb_event;
  450. while ((xcb_event = xcb_poll_for_event(xcb_conn))) {
  451. /* TODO: Handle XCB events. */
  452. free(xcb_event);
  453. }
  454. switch (swap_type) {
  455. case SWAP_TYPE_EXT:
  456. glXSwapIntervalEXT(display, window, 0);
  457. break;
  458. case SWAP_TYPE_MESA:
  459. glXSwapIntervalMESA(0);
  460. break;
  461. case SWAP_TYPE_SGI:
  462. glXSwapIntervalSGI(0);
  463. break;
  464. case SWAP_TYPE_NORMAL:;
  465. }
  466. glXSwapBuffers(display, window);
  467. }