1
0

gl-wayland-egl.c 9.1 KB

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