gl-wayland-egl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428
  1. /******************************************************************************
  2. Copyright (C) 2019 by Jason Francis <[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. #include "gl-wayland-egl.h"
  15. #include <wayland-client.h>
  16. #include <wayland-egl.h>
  17. #include "gl-egl-common.h"
  18. #include <glad/glad_egl.h>
  19. static const EGLint config_attribs_native[] = {EGL_SURFACE_TYPE,
  20. EGL_WINDOW_BIT,
  21. EGL_RENDERABLE_TYPE,
  22. EGL_OPENGL_BIT,
  23. EGL_STENCIL_SIZE,
  24. 0,
  25. EGL_DEPTH_SIZE,
  26. 0,
  27. EGL_BUFFER_SIZE,
  28. 32,
  29. EGL_ALPHA_SIZE,
  30. 8,
  31. EGL_NATIVE_RENDERABLE,
  32. EGL_TRUE,
  33. EGL_NONE};
  34. static const EGLint config_attribs[] = {EGL_SURFACE_TYPE,
  35. EGL_WINDOW_BIT,
  36. EGL_RENDERABLE_TYPE,
  37. EGL_OPENGL_BIT,
  38. EGL_STENCIL_SIZE,
  39. 0,
  40. EGL_DEPTH_SIZE,
  41. 0,
  42. EGL_BUFFER_SIZE,
  43. 32,
  44. EGL_ALPHA_SIZE,
  45. 8,
  46. EGL_NONE};
  47. static const EGLint ctx_attribs[] = {
  48. #ifdef _DEBUG
  49. EGL_CONTEXT_OPENGL_DEBUG,
  50. EGL_TRUE,
  51. #endif
  52. EGL_CONTEXT_OPENGL_PROFILE_MASK,
  53. EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT,
  54. EGL_CONTEXT_MAJOR_VERSION,
  55. 3,
  56. EGL_CONTEXT_MINOR_VERSION,
  57. 3,
  58. EGL_NONE};
  59. static const EGLint khr_ctx_attribs[] = {
  60. #ifdef _DEBUG
  61. EGL_CONTEXT_FLAGS_KHR,
  62. EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR,
  63. #endif
  64. EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR,
  65. EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR,
  66. EGL_CONTEXT_MAJOR_VERSION_KHR,
  67. 3,
  68. EGL_CONTEXT_MINOR_VERSION_KHR,
  69. 3,
  70. EGL_NONE};
  71. struct gl_windowinfo {
  72. struct wl_egl_window *window;
  73. EGLSurface egl_surface;
  74. };
  75. struct gl_platform {
  76. struct wl_display *wl_display;
  77. EGLDisplay display;
  78. EGLConfig config;
  79. EGLContext context;
  80. };
  81. struct gl_windowinfo *
  82. gl_wayland_egl_windowinfo_create(const struct gs_init_data *info)
  83. {
  84. struct wl_egl_window *window =
  85. wl_egl_window_create(info->window.display, info->cx, info->cy);
  86. if (window == NULL) {
  87. blog(LOG_ERROR, "wl_egl_window_create failed");
  88. return NULL;
  89. }
  90. struct gl_windowinfo *wi = bmalloc(sizeof(struct gl_windowinfo));
  91. wi->window = window;
  92. return wi;
  93. }
  94. static void gl_wayland_egl_windowinfo_destroy(struct gl_windowinfo *info)
  95. {
  96. wl_egl_window_destroy(info->window);
  97. bfree(info);
  98. }
  99. static bool egl_make_current(EGLDisplay display, EGLSurface surface,
  100. EGLContext context)
  101. {
  102. if (eglBindAPI(EGL_OPENGL_API) == EGL_FALSE) {
  103. blog(LOG_ERROR, "eglBindAPI failed");
  104. }
  105. if (!eglMakeCurrent(display, surface, surface, context)) {
  106. blog(LOG_ERROR, "eglMakeCurrent failed");
  107. return false;
  108. }
  109. if (surface != EGL_NO_SURFACE)
  110. glDrawBuffer(GL_BACK);
  111. return true;
  112. }
  113. static bool egl_context_create(struct gl_platform *plat, const EGLint *attribs)
  114. {
  115. bool success = false;
  116. EGLint num_config;
  117. if (eglBindAPI(EGL_OPENGL_API) == EGL_FALSE) {
  118. blog(LOG_ERROR, "eglBindAPI failed");
  119. }
  120. EGLBoolean result = eglChooseConfig(plat->display,
  121. config_attribs_native,
  122. &plat->config, 1, &num_config);
  123. if (result != EGL_TRUE || num_config == 0) {
  124. result = eglChooseConfig(plat->display, config_attribs,
  125. &plat->config, 1, &num_config);
  126. if (result != EGL_TRUE || num_config == 0) {
  127. blog(LOG_ERROR, "eglChooseConfig failed");
  128. goto error;
  129. }
  130. }
  131. plat->context = eglCreateContext(plat->display, plat->config,
  132. EGL_NO_CONTEXT, attribs);
  133. if (plat->context == EGL_NO_CONTEXT) {
  134. blog(LOG_ERROR, "eglCreateContext failed");
  135. goto error;
  136. }
  137. success =
  138. egl_make_current(plat->display, EGL_NO_SURFACE, plat->context);
  139. error:
  140. return success;
  141. }
  142. static void egl_context_destroy(struct gl_platform *plat)
  143. {
  144. egl_make_current(plat->display, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  145. eglDestroyContext(plat->display, plat->context);
  146. }
  147. static bool extension_supported(const char *extensions, const char *search)
  148. {
  149. const char *result = strstr(extensions, search);
  150. unsigned long len = strlen(search);
  151. return result != NULL &&
  152. (result == extensions || *(result - 1) == ' ') &&
  153. (result[len] == ' ' || result[len] == '\0');
  154. }
  155. static struct gl_platform *gl_wayland_egl_platform_create(gs_device_t *device,
  156. uint32_t adapter)
  157. {
  158. struct gl_platform *plat = bmalloc(sizeof(struct gl_platform));
  159. plat->wl_display = obs_get_nix_platform_display();
  160. device->plat = plat;
  161. plat->display = eglGetDisplay(plat->wl_display);
  162. if (plat->display == EGL_NO_DISPLAY) {
  163. blog(LOG_ERROR, "eglGetDisplay failed");
  164. goto fail_display_init;
  165. }
  166. EGLint major;
  167. EGLint minor;
  168. if (eglInitialize(plat->display, &major, &minor) == EGL_FALSE) {
  169. blog(LOG_ERROR, "eglInitialize failed");
  170. goto fail_display_init;
  171. }
  172. blog(LOG_INFO, "Initialized EGL %d.%d", major, minor);
  173. const char *extensions = eglQueryString(plat->display, EGL_EXTENSIONS);
  174. blog(LOG_DEBUG, "Supported EGL Extensions: %s", extensions);
  175. const EGLint *attribs = ctx_attribs;
  176. if (major == 1 && minor == 4) {
  177. if (extension_supported(extensions, "EGL_KHR_create_context")) {
  178. attribs = khr_ctx_attribs;
  179. } else {
  180. blog(LOG_ERROR,
  181. "EGL_KHR_create_context extension is required to use EGL 1.4.");
  182. goto fail_context_create;
  183. }
  184. } else if (major < 1 || (major == 1 && minor < 4)) {
  185. blog(LOG_ERROR, "EGL 1.4 or higher is required.");
  186. goto fail_context_create;
  187. }
  188. if (!egl_context_create(plat, attribs)) {
  189. goto fail_context_create;
  190. }
  191. if (!gladLoadGL()) {
  192. blog(LOG_ERROR, "Failed to load OpenGL entry functions.");
  193. goto fail_load_gl;
  194. }
  195. if (!gladLoadEGL()) {
  196. blog(LOG_ERROR, "Unable to load EGL entry functions.");
  197. goto fail_load_egl;
  198. }
  199. goto success;
  200. fail_load_egl:
  201. fail_load_gl:
  202. egl_context_destroy(plat);
  203. fail_context_create:
  204. eglTerminate(plat->display);
  205. fail_display_init:
  206. bfree(plat);
  207. plat = NULL;
  208. success:
  209. UNUSED_PARAMETER(adapter);
  210. return plat;
  211. }
  212. static void gl_wayland_egl_platform_destroy(struct gl_platform *plat)
  213. {
  214. if (plat) {
  215. egl_context_destroy(plat);
  216. eglTerminate(plat->display);
  217. bfree(plat);
  218. }
  219. }
  220. static bool gl_wayland_egl_platform_init_swapchain(struct gs_swap_chain *swap)
  221. {
  222. struct gl_platform *plat = swap->device->plat;
  223. EGLSurface egl_surface = eglCreateWindowSurface(
  224. plat->display, plat->config, swap->wi->window, NULL);
  225. if (egl_surface == EGL_NO_SURFACE) {
  226. blog(LOG_ERROR, "eglCreateWindowSurface failed");
  227. return false;
  228. }
  229. swap->wi->egl_surface = egl_surface;
  230. return true;
  231. }
  232. static void
  233. gl_wayland_egl_platform_cleanup_swapchain(struct gs_swap_chain *swap)
  234. {
  235. struct gl_platform *plat = swap->device->plat;
  236. eglDestroySurface(plat->display, swap->wi->egl_surface);
  237. }
  238. static void gl_wayland_egl_device_enter_context(gs_device_t *device)
  239. {
  240. struct gl_platform *plat = device->plat;
  241. EGLSurface surface = EGL_NO_SURFACE;
  242. if (device->cur_swap != NULL)
  243. surface = device->cur_swap->wi->egl_surface;
  244. egl_make_current(plat->display, surface, plat->context);
  245. }
  246. static void gl_wayland_egl_device_leave_context(gs_device_t *device)
  247. {
  248. struct gl_platform *plat = device->plat;
  249. egl_make_current(plat->display, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  250. }
  251. static void *gl_wayland_egl_device_get_device_obj(gs_device_t *device)
  252. {
  253. return device->plat->context;
  254. }
  255. static void gl_wayland_egl_getclientsize(const struct gs_swap_chain *swap,
  256. uint32_t *width, uint32_t *height)
  257. {
  258. wl_egl_window_get_attached_size(swap->wi->window, (void *)width,
  259. (void *)height);
  260. }
  261. static void gl_wayland_egl_clear_context(gs_device_t *device)
  262. {
  263. struct gl_platform *plat = device->plat;
  264. egl_make_current(plat->display, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  265. }
  266. static void gl_wayland_egl_update(gs_device_t *device)
  267. {
  268. wl_egl_window_resize(device->cur_swap->wi->window,
  269. device->cur_swap->info.cx,
  270. device->cur_swap->info.cy, 0, 0);
  271. }
  272. static void gl_wayland_egl_device_load_swapchain(gs_device_t *device,
  273. gs_swapchain_t *swap)
  274. {
  275. if (device->cur_swap == swap)
  276. return;
  277. device->cur_swap = swap;
  278. struct gl_platform *plat = device->plat;
  279. if (swap == NULL) {
  280. egl_make_current(plat->display, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  281. } else {
  282. egl_make_current(plat->display, swap->wi->egl_surface,
  283. plat->context);
  284. }
  285. }
  286. static void gl_wayland_egl_device_present(gs_device_t *device)
  287. {
  288. struct gl_platform *plat = device->plat;
  289. struct gl_windowinfo *wi = device->cur_swap->wi;
  290. if (eglSwapInterval(plat->display, 0) == EGL_FALSE) {
  291. blog(LOG_ERROR, "eglSwapInterval failed");
  292. }
  293. if (eglSwapBuffers(plat->display, wi->egl_surface) == EGL_FALSE) {
  294. blog(LOG_ERROR, "eglSwapBuffers failed");
  295. }
  296. }
  297. static struct gs_texture *gl_wayland_egl_device_texture_create_from_dmabuf(
  298. gs_device_t *device, unsigned int width, unsigned int height,
  299. uint32_t drm_format, enum gs_color_format color_format,
  300. uint32_t n_planes, const int *fds, const uint32_t *strides,
  301. const uint32_t *offsets, const uint64_t *modifiers)
  302. {
  303. struct gl_platform *plat = device->plat;
  304. return gl_egl_create_dmabuf_image(plat->display, width, height,
  305. drm_format, color_format, n_planes,
  306. fds, strides, offsets, modifiers);
  307. }
  308. static bool gl_wayland_egl_device_query_dmabuf_capabilities(
  309. gs_device_t *device, enum gs_dmabuf_flags *dmabuf_flags,
  310. uint32_t **drm_formats, size_t *n_formats)
  311. {
  312. struct gl_platform *plat = device->plat;
  313. return gl_egl_query_dmabuf_capabilities(plat->display, dmabuf_flags,
  314. drm_formats, n_formats);
  315. }
  316. static bool gl_wayland_egl_device_query_dmabuf_modifiers_for_format(
  317. gs_device_t *device, uint32_t drm_format, uint64_t **modifiers,
  318. size_t *n_modifiers)
  319. {
  320. struct gl_platform *plat = device->plat;
  321. return gl_egl_query_dmabuf_modifiers_for_format(
  322. plat->display, drm_format, modifiers, n_modifiers);
  323. }
  324. static struct gs_texture *gl_wayland_egl_device_texture_create_from_pixmap(
  325. gs_device_t *device, uint32_t width, uint32_t height,
  326. enum gs_color_format color_format, uint32_t target, void *pixmap)
  327. {
  328. UNUSED_PARAMETER(device);
  329. UNUSED_PARAMETER(width);
  330. UNUSED_PARAMETER(height);
  331. UNUSED_PARAMETER(color_format);
  332. UNUSED_PARAMETER(target);
  333. UNUSED_PARAMETER(pixmap);
  334. return NULL;
  335. }
  336. static const struct gl_winsys_vtable egl_wayland_winsys_vtable = {
  337. .windowinfo_create = gl_wayland_egl_windowinfo_create,
  338. .windowinfo_destroy = gl_wayland_egl_windowinfo_destroy,
  339. .platform_create = gl_wayland_egl_platform_create,
  340. .platform_destroy = gl_wayland_egl_platform_destroy,
  341. .platform_init_swapchain = gl_wayland_egl_platform_init_swapchain,
  342. .platform_cleanup_swapchain = gl_wayland_egl_platform_cleanup_swapchain,
  343. .device_enter_context = gl_wayland_egl_device_enter_context,
  344. .device_leave_context = gl_wayland_egl_device_leave_context,
  345. .device_get_device_obj = gl_wayland_egl_device_get_device_obj,
  346. .getclientsize = gl_wayland_egl_getclientsize,
  347. .clear_context = gl_wayland_egl_clear_context,
  348. .update = gl_wayland_egl_update,
  349. .device_load_swapchain = gl_wayland_egl_device_load_swapchain,
  350. .device_present = gl_wayland_egl_device_present,
  351. .device_texture_create_from_dmabuf =
  352. gl_wayland_egl_device_texture_create_from_dmabuf,
  353. .device_query_dmabuf_capabilities =
  354. gl_wayland_egl_device_query_dmabuf_capabilities,
  355. .device_query_dmabuf_modifiers_for_format =
  356. gl_wayland_egl_device_query_dmabuf_modifiers_for_format,
  357. .device_texture_create_from_pixmap =
  358. gl_wayland_egl_device_texture_create_from_pixmap,
  359. };
  360. const struct gl_winsys_vtable *gl_wayland_egl_get_winsys_vtable(void)
  361. {
  362. return &egl_wayland_winsys_vtable;
  363. }