gl-x11-glx.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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-nix.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. static struct gl_windowinfo *
  183. gl_x11_glx_windowinfo_create(const struct gs_init_data *info)
  184. {
  185. UNUSED_PARAMETER(info);
  186. return bmalloc(sizeof(struct gl_windowinfo));
  187. }
  188. static void gl_x11_glx_windowinfo_destroy(struct gl_windowinfo *info)
  189. {
  190. bfree(info);
  191. }
  192. static Display *open_windowless_display(void)
  193. {
  194. Display *display = XOpenDisplay(NULL);
  195. xcb_connection_t *xcb_conn;
  196. xcb_screen_iterator_t screen_iterator;
  197. xcb_screen_t *screen;
  198. int screen_num;
  199. if (!display) {
  200. blog(LOG_ERROR, "Unable to open new X connection!");
  201. return NULL;
  202. }
  203. xcb_conn = XGetXCBConnection(display);
  204. if (!xcb_conn) {
  205. blog(LOG_ERROR, "Unable to get XCB connection to main display");
  206. goto error;
  207. }
  208. screen_iterator = xcb_setup_roots_iterator(xcb_get_setup(xcb_conn));
  209. screen = screen_iterator.data;
  210. if (!screen) {
  211. blog(LOG_ERROR, "Unable to get screen root");
  212. goto error;
  213. }
  214. screen_num = get_screen_num_from_root(xcb_conn, screen->root);
  215. if (screen_num == -1) {
  216. blog(LOG_ERROR, "Unable to get screen number from root");
  217. goto error;
  218. }
  219. if (!gladLoadGLX(display, screen_num)) {
  220. blog(LOG_ERROR, "Unable to load GLX entry functions.");
  221. goto error;
  222. }
  223. return display;
  224. error:
  225. if (display)
  226. XCloseDisplay(display);
  227. return NULL;
  228. }
  229. static int x_error_handler(Display *display, XErrorEvent *error)
  230. {
  231. char str1[512];
  232. char str2[512];
  233. char str3[512];
  234. XGetErrorText(display, error->error_code, str1, sizeof(str1));
  235. XGetErrorText(display, error->request_code, str2, sizeof(str2));
  236. XGetErrorText(display, error->minor_code, str3, sizeof(str3));
  237. blog(LOG_ERROR,
  238. "X Error: %s, Major opcode: %s, "
  239. "Minor opcode: %s, Serial: %lu",
  240. str1, str2, str3, error->serial);
  241. return 0;
  242. }
  243. static struct gl_platform *gl_x11_glx_platform_create(gs_device_t *device,
  244. uint32_t adapter)
  245. {
  246. /* There's some trickery here... we're mixing libX11, xcb, and GLX
  247. For an explanation see here: http://xcb.freedesktop.org/MixingCalls/
  248. Essentially, GLX requires Xlib. Everything else we use xcb. */
  249. struct gl_platform *plat = bmalloc(sizeof(struct gl_platform));
  250. Display *display = open_windowless_display();
  251. if (!display) {
  252. goto fail_display_open;
  253. }
  254. XSetEventQueueOwner(display, XCBOwnsEventQueue);
  255. XSetErrorHandler(x_error_handler);
  256. /* We assume later that cur_swap is already set. */
  257. device->plat = plat;
  258. plat->display = display;
  259. if (!gl_context_create(plat)) {
  260. blog(LOG_ERROR, "Failed to create context!");
  261. goto fail_context_create;
  262. }
  263. if (!glXMakeContextCurrent(plat->display, plat->pbuffer, plat->pbuffer,
  264. plat->context)) {
  265. blog(LOG_ERROR, "Failed to make context current.");
  266. goto fail_make_current;
  267. }
  268. if (!gladLoadGL()) {
  269. blog(LOG_ERROR, "Failed to load OpenGL entry functions.");
  270. goto fail_load_gl;
  271. }
  272. goto success;
  273. fail_make_current:
  274. gl_context_destroy(plat);
  275. fail_context_create:
  276. fail_load_gl:
  277. XCloseDisplay(display);
  278. fail_display_open:
  279. bfree(plat);
  280. plat = NULL;
  281. success:
  282. UNUSED_PARAMETER(adapter);
  283. return plat;
  284. }
  285. static void gl_x11_glx_platform_destroy(struct gl_platform *plat)
  286. {
  287. if (!plat) /* In what case would platform be invalid here? */
  288. return;
  289. gl_context_destroy(plat);
  290. }
  291. static bool gl_x11_glx_platform_init_swapchain(struct gs_swap_chain *swap)
  292. {
  293. Display *display = swap->device->plat->display;
  294. xcb_connection_t *xcb_conn = XGetXCBConnection(display);
  295. xcb_window_t wid = xcb_generate_id(xcb_conn);
  296. xcb_window_t parent = swap->info.window.id;
  297. xcb_get_geometry_reply_t *geometry =
  298. get_window_geometry(xcb_conn, parent);
  299. bool status = false;
  300. int screen_num;
  301. int visual;
  302. GLXFBConfig *fb_config;
  303. if (!geometry)
  304. goto fail_geometry_request;
  305. screen_num = get_screen_num_from_root(xcb_conn, geometry->root);
  306. if (screen_num == -1) {
  307. goto fail_screen;
  308. }
  309. /* ...fetch the best match... */
  310. {
  311. int num_configs;
  312. fb_config = glXChooseFBConfig(display, screen_num,
  313. ctx_visual_attribs, &num_configs);
  314. if (!fb_config || !num_configs) {
  315. blog(LOG_ERROR, "Failed to find FBConfig!");
  316. goto fail_fb_config;
  317. }
  318. }
  319. /* ...then fetch matching visual info for xcb. */
  320. {
  321. int error = glXGetFBConfigAttrib(display, fb_config[0],
  322. GLX_VISUAL_ID, &visual);
  323. if (error) {
  324. blog(LOG_ERROR, "Bad call to GetFBConfigAttrib!");
  325. goto fail_visual_id;
  326. }
  327. }
  328. xcb_colormap_t colormap = xcb_generate_id(xcb_conn);
  329. uint32_t mask = XCB_CW_BORDER_PIXEL | XCB_CW_COLORMAP;
  330. uint32_t mask_values[] = {0, colormap, 0};
  331. xcb_create_colormap(xcb_conn, XCB_COLORMAP_ALLOC_NONE, colormap, parent,
  332. visual);
  333. xcb_create_window(xcb_conn, 24 /* Hardcoded? */, wid, parent, 0, 0,
  334. geometry->width, geometry->height, 0, 0, visual, mask,
  335. mask_values);
  336. swap->wi->config = fb_config[0];
  337. swap->wi->window = wid;
  338. xcb_map_window(xcb_conn, wid);
  339. XFree(fb_config);
  340. status = true;
  341. goto success;
  342. fail_visual_id:
  343. XFree(fb_config);
  344. fail_fb_config:
  345. fail_screen:
  346. fail_geometry_request:
  347. success:
  348. free(geometry);
  349. return status;
  350. }
  351. static void gl_x11_glx_platform_cleanup_swapchain(struct gs_swap_chain *swap)
  352. {
  353. UNUSED_PARAMETER(swap);
  354. /* Really nothing to clean up? */
  355. }
  356. static void gl_x11_glx_device_enter_context(gs_device_t *device)
  357. {
  358. GLXContext context = device->plat->context;
  359. Display *display = device->plat->display;
  360. if (device->cur_swap) {
  361. XID window = device->cur_swap->wi->window;
  362. if (!glXMakeContextCurrent(display, window, window, context)) {
  363. blog(LOG_ERROR, "Failed to make context current.");
  364. }
  365. } else {
  366. GLXPbuffer pbuf = device->plat->pbuffer;
  367. if (!glXMakeContextCurrent(display, pbuf, pbuf, context)) {
  368. blog(LOG_ERROR, "Failed to make context current.");
  369. }
  370. }
  371. }
  372. static void gl_x11_glx_device_leave_context(gs_device_t *device)
  373. {
  374. Display *display = device->plat->display;
  375. if (!glXMakeContextCurrent(display, None, None, NULL)) {
  376. blog(LOG_ERROR, "Failed to reset current context.");
  377. }
  378. }
  379. static void *gl_x11_glx_device_get_device_obj(gs_device_t *device)
  380. {
  381. return device->plat->context;
  382. }
  383. static void gl_x11_glx_getclientsize(const struct gs_swap_chain *swap,
  384. uint32_t *width, uint32_t *height)
  385. {
  386. xcb_connection_t *xcb_conn =
  387. XGetXCBConnection(swap->device->plat->display);
  388. xcb_window_t window = swap->wi->window;
  389. xcb_get_geometry_reply_t *geometry =
  390. get_window_geometry(xcb_conn, window);
  391. if (geometry) {
  392. *width = geometry->width;
  393. *height = geometry->height;
  394. }
  395. free(geometry);
  396. }
  397. static void gl_x11_glx_clear_context(gs_device_t *device)
  398. {
  399. Display *display = device->plat->display;
  400. if (!glXMakeContextCurrent(display, None, None, NULL)) {
  401. blog(LOG_ERROR, "Failed to reset current context.");
  402. }
  403. }
  404. static void gl_x11_glx_update(gs_device_t *device)
  405. {
  406. Display *display = device->plat->display;
  407. xcb_window_t window = device->cur_swap->wi->window;
  408. uint32_t values[] = {device->cur_swap->info.cx,
  409. device->cur_swap->info.cy};
  410. xcb_configure_window(XGetXCBConnection(display), window,
  411. XCB_CONFIG_WINDOW_WIDTH | XCB_CONFIG_WINDOW_HEIGHT,
  412. values);
  413. }
  414. static void gl_x11_glx_device_load_swapchain(gs_device_t *device,
  415. gs_swapchain_t *swap)
  416. {
  417. if (device->cur_swap == swap)
  418. return;
  419. Display *dpy = device->plat->display;
  420. GLXContext ctx = device->plat->context;
  421. device->cur_swap = swap;
  422. if (swap) {
  423. XID window = swap->wi->window;
  424. if (!glXMakeContextCurrent(dpy, window, window, ctx)) {
  425. blog(LOG_ERROR, "Failed to make context current.");
  426. }
  427. } else {
  428. GLXPbuffer pbuf = device->plat->pbuffer;
  429. if (!glXMakeContextCurrent(dpy, pbuf, pbuf, ctx)) {
  430. blog(LOG_ERROR, "Failed to make context current.");
  431. }
  432. }
  433. }
  434. enum swap_type {
  435. SWAP_TYPE_NORMAL,
  436. SWAP_TYPE_EXT,
  437. SWAP_TYPE_MESA,
  438. SWAP_TYPE_SGI,
  439. };
  440. static void gl_x11_glx_device_present(gs_device_t *device)
  441. {
  442. static bool initialized = false;
  443. static enum swap_type swap_type = SWAP_TYPE_NORMAL;
  444. Display *display = device->plat->display;
  445. XID window = device->cur_swap->wi->window;
  446. if (!initialized) {
  447. if (GLAD_GLX_EXT_swap_control)
  448. swap_type = SWAP_TYPE_EXT;
  449. else if (GLAD_GLX_MESA_swap_control)
  450. swap_type = SWAP_TYPE_MESA;
  451. else if (GLAD_GLX_SGI_swap_control)
  452. swap_type = SWAP_TYPE_SGI;
  453. initialized = true;
  454. }
  455. xcb_connection_t *xcb_conn = XGetXCBConnection(display);
  456. xcb_generic_event_t *xcb_event;
  457. while ((xcb_event = xcb_poll_for_event(xcb_conn))) {
  458. /* TODO: Handle XCB events. */
  459. free(xcb_event);
  460. }
  461. switch (swap_type) {
  462. case SWAP_TYPE_EXT:
  463. glXSwapIntervalEXT(display, window, 0);
  464. break;
  465. case SWAP_TYPE_MESA:
  466. glXSwapIntervalMESA(0);
  467. break;
  468. case SWAP_TYPE_SGI:
  469. glXSwapIntervalSGI(0);
  470. break;
  471. case SWAP_TYPE_NORMAL:;
  472. }
  473. glXSwapBuffers(display, window);
  474. }
  475. static struct gs_texture *gl_x11_glx_device_texture_create_from_dmabuf(
  476. gs_device_t *device, unsigned int width, unsigned int height,
  477. uint32_t drm_format, enum gs_color_format color_format,
  478. uint32_t n_planes, const int *fds, const uint32_t *strides,
  479. const uint32_t *offsets, const uint64_t *modifiers)
  480. {
  481. UNUSED_PARAMETER(device);
  482. UNUSED_PARAMETER(width);
  483. UNUSED_PARAMETER(height);
  484. UNUSED_PARAMETER(drm_format);
  485. UNUSED_PARAMETER(color_format);
  486. UNUSED_PARAMETER(n_planes);
  487. UNUSED_PARAMETER(fds);
  488. UNUSED_PARAMETER(strides);
  489. UNUSED_PARAMETER(offsets);
  490. UNUSED_PARAMETER(modifiers);
  491. return NULL;
  492. }
  493. static const struct gl_winsys_vtable glx_winsys_vtable = {
  494. .windowinfo_create = gl_x11_glx_windowinfo_create,
  495. .windowinfo_destroy = gl_x11_glx_windowinfo_destroy,
  496. .platform_create = gl_x11_glx_platform_create,
  497. .platform_destroy = gl_x11_glx_platform_destroy,
  498. .platform_init_swapchain = gl_x11_glx_platform_init_swapchain,
  499. .platform_cleanup_swapchain = gl_x11_glx_platform_cleanup_swapchain,
  500. .device_enter_context = gl_x11_glx_device_enter_context,
  501. .device_leave_context = gl_x11_glx_device_leave_context,
  502. .device_get_device_obj = gl_x11_glx_device_get_device_obj,
  503. .getclientsize = gl_x11_glx_getclientsize,
  504. .clear_context = gl_x11_glx_clear_context,
  505. .update = gl_x11_glx_update,
  506. .device_load_swapchain = gl_x11_glx_device_load_swapchain,
  507. .device_present = gl_x11_glx_device_present,
  508. .device_texture_create_from_dmabuf =
  509. gl_x11_glx_device_texture_create_from_dmabuf,
  510. };
  511. const struct gl_winsys_vtable *gl_x11_glx_get_winsys_vtable(void)
  512. {
  513. return &glx_winsys_vtable;
  514. }