1
0

gl-x11-glx.c 16 KB

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