gl-wayland-egl.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  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 *gl_wayland_egl_windowinfo_create(const struct gs_init_data *info)
  82. {
  83. struct wl_egl_window *window = wl_egl_window_create(info->window.display, info->cx, info->cy);
  84. if (window == NULL) {
  85. blog(LOG_ERROR, "wl_egl_window_create failed");
  86. return NULL;
  87. }
  88. struct gl_windowinfo *wi = bmalloc(sizeof(struct gl_windowinfo));
  89. wi->window = window;
  90. return wi;
  91. }
  92. static void gl_wayland_egl_windowinfo_destroy(struct gl_windowinfo *info)
  93. {
  94. wl_egl_window_destroy(info->window);
  95. bfree(info);
  96. }
  97. static bool egl_make_current(EGLDisplay display, EGLSurface surface, EGLContext context)
  98. {
  99. if (eglBindAPI(EGL_OPENGL_API) == EGL_FALSE) {
  100. blog(LOG_ERROR, "eglBindAPI failed");
  101. }
  102. if (!eglMakeCurrent(display, surface, surface, context)) {
  103. blog(LOG_ERROR, "eglMakeCurrent failed");
  104. return false;
  105. }
  106. if (surface != EGL_NO_SURFACE)
  107. glDrawBuffer(GL_BACK);
  108. return true;
  109. }
  110. static bool egl_context_create(struct gl_platform *plat, const EGLint *attribs)
  111. {
  112. bool success = false;
  113. EGLint num_config;
  114. if (eglBindAPI(EGL_OPENGL_API) == EGL_FALSE) {
  115. blog(LOG_ERROR, "eglBindAPI failed");
  116. }
  117. EGLBoolean result = eglChooseConfig(plat->display, config_attribs_native, &plat->config, 1, &num_config);
  118. if (result != EGL_TRUE || num_config == 0) {
  119. result = eglChooseConfig(plat->display, config_attribs, &plat->config, 1, &num_config);
  120. if (result != EGL_TRUE || num_config == 0) {
  121. blog(LOG_ERROR, "eglChooseConfig failed");
  122. goto error;
  123. }
  124. }
  125. plat->context = eglCreateContext(plat->display, plat->config, EGL_NO_CONTEXT, attribs);
  126. if (plat->context == EGL_NO_CONTEXT) {
  127. blog(LOG_ERROR, "eglCreateContext failed");
  128. goto error;
  129. }
  130. success = egl_make_current(plat->display, EGL_NO_SURFACE, plat->context);
  131. error:
  132. return success;
  133. }
  134. static void egl_context_destroy(struct gl_platform *plat)
  135. {
  136. egl_make_current(plat->display, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  137. eglDestroyContext(plat->display, plat->context);
  138. }
  139. static bool extension_supported(const char *extensions, const char *search)
  140. {
  141. const char *result = strstr(extensions, search);
  142. unsigned long len = strlen(search);
  143. return result != NULL && (result == extensions || *(result - 1) == ' ') &&
  144. (result[len] == ' ' || result[len] == '\0');
  145. }
  146. static struct gl_platform *gl_wayland_egl_platform_create(gs_device_t *device, uint32_t adapter)
  147. {
  148. struct gl_platform *plat = bmalloc(sizeof(struct gl_platform));
  149. plat->wl_display = obs_get_nix_platform_display();
  150. device->plat = plat;
  151. const EGLAttrib plat_attribs[] = {EGL_NONE};
  152. plat->display = eglGetPlatformDisplay(EGL_PLATFORM_WAYLAND_EXT, plat->wl_display, plat_attribs);
  153. if (plat->display == EGL_NO_DISPLAY) {
  154. blog(LOG_ERROR, "eglGetDisplay failed");
  155. goto fail_display_init;
  156. }
  157. EGLint major;
  158. EGLint minor;
  159. if (eglInitialize(plat->display, &major, &minor) == EGL_FALSE) {
  160. blog(LOG_ERROR, "eglInitialize failed");
  161. goto fail_display_init;
  162. }
  163. blog(LOG_INFO, "Initialized EGL %d.%d", major, minor);
  164. const char *extensions = eglQueryString(plat->display, EGL_EXTENSIONS);
  165. blog(LOG_DEBUG, "Supported EGL Extensions: %s", extensions);
  166. const EGLint *attribs = ctx_attribs;
  167. if (major == 1 && minor == 4) {
  168. if (extension_supported(extensions, "EGL_KHR_create_context")) {
  169. attribs = khr_ctx_attribs;
  170. } else {
  171. blog(LOG_ERROR, "EGL_KHR_create_context extension is required to use EGL 1.4.");
  172. goto fail_context_create;
  173. }
  174. } else if (major < 1 || (major == 1 && minor < 4)) {
  175. blog(LOG_ERROR, "EGL 1.4 or higher is required.");
  176. goto fail_context_create;
  177. }
  178. if (!egl_context_create(plat, attribs)) {
  179. goto fail_context_create;
  180. }
  181. if (!gladLoadGL()) {
  182. blog(LOG_ERROR, "Failed to load OpenGL entry functions.");
  183. goto fail_load_gl;
  184. }
  185. if (!gladLoadEGL()) {
  186. blog(LOG_ERROR, "Unable to load EGL entry functions.");
  187. goto fail_load_egl;
  188. }
  189. goto success;
  190. fail_load_egl:
  191. fail_load_gl:
  192. egl_context_destroy(plat);
  193. fail_context_create:
  194. eglTerminate(plat->display);
  195. fail_display_init:
  196. bfree(plat);
  197. plat = NULL;
  198. success:
  199. UNUSED_PARAMETER(adapter);
  200. return plat;
  201. }
  202. static void gl_wayland_egl_platform_destroy(struct gl_platform *plat)
  203. {
  204. if (plat) {
  205. egl_context_destroy(plat);
  206. eglTerminate(plat->display);
  207. bfree(plat);
  208. }
  209. }
  210. static bool gl_wayland_egl_platform_init_swapchain(struct gs_swap_chain *swap)
  211. {
  212. struct gl_platform *plat = swap->device->plat;
  213. EGLSurface egl_surface = eglCreateWindowSurface(plat->display, plat->config, swap->wi->window, NULL);
  214. if (egl_surface == EGL_NO_SURFACE) {
  215. blog(LOG_ERROR, "eglCreateWindowSurface failed");
  216. return false;
  217. }
  218. swap->wi->egl_surface = egl_surface;
  219. return true;
  220. }
  221. static void gl_wayland_egl_platform_cleanup_swapchain(struct gs_swap_chain *swap)
  222. {
  223. struct gl_platform *plat = swap->device->plat;
  224. eglDestroySurface(plat->display, swap->wi->egl_surface);
  225. }
  226. static void gl_wayland_egl_device_enter_context(gs_device_t *device)
  227. {
  228. struct gl_platform *plat = device->plat;
  229. EGLSurface surface = EGL_NO_SURFACE;
  230. if (device->cur_swap != NULL)
  231. surface = device->cur_swap->wi->egl_surface;
  232. egl_make_current(plat->display, surface, plat->context);
  233. }
  234. static void gl_wayland_egl_device_leave_context(gs_device_t *device)
  235. {
  236. struct gl_platform *plat = device->plat;
  237. egl_make_current(plat->display, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  238. }
  239. static void *gl_wayland_egl_device_get_device_obj(gs_device_t *device)
  240. {
  241. return device->plat->context;
  242. }
  243. static void gl_wayland_egl_getclientsize(const struct gs_swap_chain *swap, uint32_t *width, uint32_t *height)
  244. {
  245. wl_egl_window_get_attached_size(swap->wi->window, (void *)width, (void *)height);
  246. }
  247. static void gl_wayland_egl_clear_context(gs_device_t *device)
  248. {
  249. struct gl_platform *plat = device->plat;
  250. egl_make_current(plat->display, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  251. }
  252. static void gl_wayland_egl_update(gs_device_t *device)
  253. {
  254. wl_egl_window_resize(device->cur_swap->wi->window, device->cur_swap->info.cx, device->cur_swap->info.cy, 0, 0);
  255. }
  256. static void gl_wayland_egl_device_load_swapchain(gs_device_t *device, gs_swapchain_t *swap)
  257. {
  258. if (device->cur_swap == swap)
  259. return;
  260. device->cur_swap = swap;
  261. struct gl_platform *plat = device->plat;
  262. if (swap == NULL) {
  263. egl_make_current(plat->display, EGL_NO_SURFACE, EGL_NO_CONTEXT);
  264. } else {
  265. egl_make_current(plat->display, swap->wi->egl_surface, plat->context);
  266. }
  267. }
  268. static void gl_wayland_egl_device_present(gs_device_t *device)
  269. {
  270. struct gl_platform *plat = device->plat;
  271. struct gl_windowinfo *wi = device->cur_swap->wi;
  272. if (eglSwapInterval(plat->display, 0) == EGL_FALSE) {
  273. blog(LOG_ERROR, "eglSwapInterval failed");
  274. }
  275. if (eglSwapBuffers(plat->display, wi->egl_surface) == EGL_FALSE) {
  276. blog(LOG_ERROR, "eglSwapBuffers failed");
  277. }
  278. }
  279. static struct gs_texture *
  280. gl_wayland_egl_device_texture_create_from_dmabuf(gs_device_t *device, unsigned int width, unsigned int height,
  281. uint32_t drm_format, enum gs_color_format color_format,
  282. uint32_t n_planes, const int *fds, const uint32_t *strides,
  283. const uint32_t *offsets, const uint64_t *modifiers)
  284. {
  285. struct gl_platform *plat = device->plat;
  286. return gl_egl_create_dmabuf_image(plat->display, width, height, drm_format, color_format, n_planes, fds,
  287. strides, offsets, modifiers);
  288. }
  289. static bool gl_wayland_egl_device_query_dmabuf_capabilities(gs_device_t *device, enum gs_dmabuf_flags *dmabuf_flags,
  290. uint32_t **drm_formats, size_t *n_formats)
  291. {
  292. struct gl_platform *plat = device->plat;
  293. return gl_egl_query_dmabuf_capabilities(plat->display, dmabuf_flags, drm_formats, n_formats);
  294. }
  295. static bool gl_wayland_egl_device_query_dmabuf_modifiers_for_format(gs_device_t *device, uint32_t drm_format,
  296. uint64_t **modifiers, size_t *n_modifiers)
  297. {
  298. struct gl_platform *plat = device->plat;
  299. return gl_egl_query_dmabuf_modifiers_for_format(plat->display, drm_format, modifiers, n_modifiers);
  300. }
  301. static struct gs_texture *gl_wayland_egl_device_texture_create_from_pixmap(gs_device_t *device, uint32_t width,
  302. uint32_t height,
  303. enum gs_color_format color_format,
  304. uint32_t target, void *pixmap)
  305. {
  306. UNUSED_PARAMETER(device);
  307. UNUSED_PARAMETER(width);
  308. UNUSED_PARAMETER(height);
  309. UNUSED_PARAMETER(color_format);
  310. UNUSED_PARAMETER(target);
  311. UNUSED_PARAMETER(pixmap);
  312. return NULL;
  313. }
  314. static bool gl_wayland_egl_enum_adapters(gs_device_t *device,
  315. bool (*callback)(void *param, const char *name, uint32_t id), void *param)
  316. {
  317. return gl_egl_enum_adapters(device->plat->display, callback, param);
  318. }
  319. static const struct gl_winsys_vtable egl_wayland_winsys_vtable = {
  320. .windowinfo_create = gl_wayland_egl_windowinfo_create,
  321. .windowinfo_destroy = gl_wayland_egl_windowinfo_destroy,
  322. .platform_create = gl_wayland_egl_platform_create,
  323. .platform_destroy = gl_wayland_egl_platform_destroy,
  324. .platform_init_swapchain = gl_wayland_egl_platform_init_swapchain,
  325. .platform_cleanup_swapchain = gl_wayland_egl_platform_cleanup_swapchain,
  326. .device_enter_context = gl_wayland_egl_device_enter_context,
  327. .device_leave_context = gl_wayland_egl_device_leave_context,
  328. .device_get_device_obj = gl_wayland_egl_device_get_device_obj,
  329. .getclientsize = gl_wayland_egl_getclientsize,
  330. .clear_context = gl_wayland_egl_clear_context,
  331. .update = gl_wayland_egl_update,
  332. .device_load_swapchain = gl_wayland_egl_device_load_swapchain,
  333. .device_present = gl_wayland_egl_device_present,
  334. .device_texture_create_from_dmabuf = gl_wayland_egl_device_texture_create_from_dmabuf,
  335. .device_query_dmabuf_capabilities = gl_wayland_egl_device_query_dmabuf_capabilities,
  336. .device_query_dmabuf_modifiers_for_format = gl_wayland_egl_device_query_dmabuf_modifiers_for_format,
  337. .device_texture_create_from_pixmap = gl_wayland_egl_device_texture_create_from_pixmap,
  338. .device_enum_adapters = gl_wayland_egl_enum_adapters,
  339. };
  340. const struct gl_winsys_vtable *gl_wayland_egl_get_winsys_vtable(void)
  341. {
  342. return &egl_wayland_winsys_vtable;
  343. }