gl-wayland-egl.c 9.9 KB

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