egl-helpers.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /*
  2. * Copyright (C) 2015-2016 Gerd Hoffmann <kraxel@redhat.com>
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "qemu/osdep.h"
  18. #include <glob.h>
  19. #include <dirent.h>
  20. #include "qemu/error-report.h"
  21. #include "ui/egl-helpers.h"
  22. EGLDisplay *qemu_egl_display;
  23. EGLConfig qemu_egl_config;
  24. /* ---------------------------------------------------------------------- */
  25. static bool egl_gles;
  26. static int egl_debug;
  27. #define egl_dbg(_x ...) \
  28. do { \
  29. if (egl_debug) { \
  30. fprintf(stderr, "egl: " _x); \
  31. } \
  32. } while (0);
  33. /* ---------------------------------------------------------------------- */
  34. #ifdef CONFIG_OPENGL_DMABUF
  35. int qemu_egl_rn_fd;
  36. struct gbm_device *qemu_egl_rn_gbm_dev;
  37. EGLContext qemu_egl_rn_ctx;
  38. static int qemu_egl_rendernode_open(const char *rendernode)
  39. {
  40. DIR *dir;
  41. struct dirent *e;
  42. int r, fd;
  43. char *p;
  44. if (rendernode) {
  45. return open(rendernode, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK);
  46. }
  47. dir = opendir("/dev/dri");
  48. if (!dir) {
  49. return -1;
  50. }
  51. fd = -1;
  52. while ((e = readdir(dir))) {
  53. if (e->d_type != DT_CHR) {
  54. continue;
  55. }
  56. if (strncmp(e->d_name, "renderD", 7)) {
  57. continue;
  58. }
  59. p = g_strdup_printf("/dev/dri/%s", e->d_name);
  60. r = open(p, O_RDWR | O_CLOEXEC | O_NOCTTY | O_NONBLOCK);
  61. if (r < 0) {
  62. g_free(p);
  63. continue;
  64. }
  65. fd = r;
  66. g_free(p);
  67. break;
  68. }
  69. closedir(dir);
  70. if (fd < 0) {
  71. return -1;
  72. }
  73. return fd;
  74. }
  75. int egl_rendernode_init(const char *rendernode)
  76. {
  77. qemu_egl_rn_fd = -1;
  78. qemu_egl_rn_fd = qemu_egl_rendernode_open(rendernode);
  79. if (qemu_egl_rn_fd == -1) {
  80. error_report("egl: no drm render node available");
  81. goto err;
  82. }
  83. qemu_egl_rn_gbm_dev = gbm_create_device(qemu_egl_rn_fd);
  84. if (!qemu_egl_rn_gbm_dev) {
  85. error_report("egl: gbm_create_device failed");
  86. goto err;
  87. }
  88. qemu_egl_init_dpy((EGLNativeDisplayType)qemu_egl_rn_gbm_dev, false, false);
  89. if (!epoxy_has_egl_extension(qemu_egl_display,
  90. "EGL_KHR_surfaceless_context")) {
  91. error_report("egl: EGL_KHR_surfaceless_context not supported");
  92. goto err;
  93. }
  94. if (!epoxy_has_egl_extension(qemu_egl_display,
  95. "EGL_MESA_image_dma_buf_export")) {
  96. error_report("egl: EGL_MESA_image_dma_buf_export not supported");
  97. goto err;
  98. }
  99. qemu_egl_rn_ctx = qemu_egl_init_ctx();
  100. if (!qemu_egl_rn_ctx) {
  101. error_report("egl: egl_init_ctx failed");
  102. goto err;
  103. }
  104. return 0;
  105. err:
  106. if (qemu_egl_rn_gbm_dev) {
  107. gbm_device_destroy(qemu_egl_rn_gbm_dev);
  108. }
  109. if (qemu_egl_rn_fd != -1) {
  110. close(qemu_egl_rn_fd);
  111. }
  112. return -1;
  113. }
  114. int egl_get_fd_for_texture(uint32_t tex_id, EGLint *stride, EGLint *fourcc)
  115. {
  116. EGLImageKHR image;
  117. EGLint num_planes, fd;
  118. image = eglCreateImageKHR(qemu_egl_display, eglGetCurrentContext(),
  119. EGL_GL_TEXTURE_2D_KHR,
  120. (EGLClientBuffer)(unsigned long)tex_id,
  121. NULL);
  122. if (!image) {
  123. return -1;
  124. }
  125. eglExportDMABUFImageQueryMESA(qemu_egl_display, image, fourcc,
  126. &num_planes, NULL);
  127. if (num_planes != 1) {
  128. eglDestroyImageKHR(qemu_egl_display, image);
  129. return -1;
  130. }
  131. eglExportDMABUFImageMESA(qemu_egl_display, image, &fd, stride, NULL);
  132. eglDestroyImageKHR(qemu_egl_display, image);
  133. return fd;
  134. }
  135. #endif /* CONFIG_OPENGL_DMABUF */
  136. /* ---------------------------------------------------------------------- */
  137. EGLSurface qemu_egl_init_surface_x11(EGLContext ectx, Window win)
  138. {
  139. EGLSurface esurface;
  140. EGLBoolean b;
  141. egl_dbg("eglCreateWindowSurface (x11 win id 0x%lx) ...\n",
  142. (unsigned long) win);
  143. esurface = eglCreateWindowSurface(qemu_egl_display,
  144. qemu_egl_config,
  145. (EGLNativeWindowType)win, NULL);
  146. if (esurface == EGL_NO_SURFACE) {
  147. error_report("egl: eglCreateWindowSurface failed");
  148. return NULL;
  149. }
  150. b = eglMakeCurrent(qemu_egl_display, esurface, esurface, ectx);
  151. if (b == EGL_FALSE) {
  152. error_report("egl: eglMakeCurrent failed");
  153. return NULL;
  154. }
  155. return esurface;
  156. }
  157. /* ---------------------------------------------------------------------- */
  158. /*
  159. * Taken from glamor_egl.h from the Xorg xserver, which is MIT licensed
  160. *
  161. * Create an EGLDisplay from a native display type. This is a little quirky
  162. * for a few reasons.
  163. *
  164. * 1: GetPlatformDisplayEXT and GetPlatformDisplay are the API you want to
  165. * use, but have different function signatures in the third argument; this
  166. * happens not to matter for us, at the moment, but it means epoxy won't alias
  167. * them together.
  168. *
  169. * 2: epoxy 1.3 and earlier don't understand EGL client extensions, which
  170. * means you can't call "eglGetPlatformDisplayEXT" directly, as the resolver
  171. * will crash.
  172. *
  173. * 3: You can't tell whether you have EGL 1.5 at this point, because
  174. * eglQueryString(EGL_VERSION) is a property of the display, which we don't
  175. * have yet. So you have to query for extensions no matter what. Fortunately
  176. * epoxy_has_egl_extension _does_ let you query for client extensions, so
  177. * we don't have to write our own extension string parsing.
  178. *
  179. * 4. There is no EGL_KHR_platform_base to complement the EXT one, thus one
  180. * needs to know EGL 1.5 is supported in order to use the eglGetPlatformDisplay
  181. * function pointer.
  182. * We can workaround this (circular dependency) by probing for the EGL 1.5
  183. * platform extensions (EGL_KHR_platform_gbm and friends) yet it doesn't seem
  184. * like mesa will be able to advertise these (even though it can do EGL 1.5).
  185. */
  186. static EGLDisplay qemu_egl_get_display(void *native)
  187. {
  188. EGLDisplay dpy = EGL_NO_DISPLAY;
  189. #ifdef EGL_MESA_platform_gbm
  190. /* In practise any EGL 1.5 implementation would support the EXT extension */
  191. if (epoxy_has_egl_extension(NULL, "EGL_EXT_platform_base")) {
  192. PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplayEXT =
  193. (void *) eglGetProcAddress("eglGetPlatformDisplayEXT");
  194. if (getPlatformDisplayEXT) {
  195. dpy = getPlatformDisplayEXT(EGL_PLATFORM_GBM_MESA, native, NULL);
  196. }
  197. }
  198. #endif
  199. if (dpy == EGL_NO_DISPLAY) {
  200. /* fallback */
  201. dpy = eglGetDisplay(native);
  202. }
  203. return dpy;
  204. }
  205. int qemu_egl_init_dpy(EGLNativeDisplayType dpy, bool gles, bool debug)
  206. {
  207. static const EGLint conf_att_gl[] = {
  208. EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  209. EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
  210. EGL_RED_SIZE, 5,
  211. EGL_GREEN_SIZE, 5,
  212. EGL_BLUE_SIZE, 5,
  213. EGL_ALPHA_SIZE, 0,
  214. EGL_NONE,
  215. };
  216. static const EGLint conf_att_gles[] = {
  217. EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  218. EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
  219. EGL_RED_SIZE, 5,
  220. EGL_GREEN_SIZE, 5,
  221. EGL_BLUE_SIZE, 5,
  222. EGL_ALPHA_SIZE, 0,
  223. EGL_NONE,
  224. };
  225. EGLint major, minor;
  226. EGLBoolean b;
  227. EGLint n;
  228. if (debug) {
  229. egl_debug = 1;
  230. setenv("EGL_LOG_LEVEL", "debug", true);
  231. setenv("LIBGL_DEBUG", "verbose", true);
  232. }
  233. egl_dbg("qemu_egl_get_display (dpy %p) ...\n", dpy);
  234. qemu_egl_display = qemu_egl_get_display(dpy);
  235. if (qemu_egl_display == EGL_NO_DISPLAY) {
  236. error_report("egl: eglGetDisplay failed");
  237. return -1;
  238. }
  239. egl_dbg("eglInitialize ...\n");
  240. b = eglInitialize(qemu_egl_display, &major, &minor);
  241. if (b == EGL_FALSE) {
  242. error_report("egl: eglInitialize failed");
  243. return -1;
  244. }
  245. egl_dbg("eglBindAPI ...\n");
  246. b = eglBindAPI(gles ? EGL_OPENGL_ES_API : EGL_OPENGL_API);
  247. if (b == EGL_FALSE) {
  248. error_report("egl: eglBindAPI failed");
  249. return -1;
  250. }
  251. egl_dbg("eglChooseConfig ...\n");
  252. b = eglChooseConfig(qemu_egl_display,
  253. gles ? conf_att_gles : conf_att_gl,
  254. &qemu_egl_config, 1, &n);
  255. if (b == EGL_FALSE || n != 1) {
  256. error_report("egl: eglChooseConfig failed");
  257. return -1;
  258. }
  259. egl_gles = gles;
  260. return 0;
  261. }
  262. EGLContext qemu_egl_init_ctx(void)
  263. {
  264. static const EGLint ctx_att_gl[] = {
  265. EGL_NONE
  266. };
  267. static const EGLint ctx_att_gles[] = {
  268. EGL_CONTEXT_CLIENT_VERSION, 2,
  269. EGL_NONE
  270. };
  271. EGLContext ectx;
  272. EGLBoolean b;
  273. egl_dbg("eglCreateContext ...\n");
  274. ectx = eglCreateContext(qemu_egl_display, qemu_egl_config, EGL_NO_CONTEXT,
  275. egl_gles ? ctx_att_gles : ctx_att_gl);
  276. if (ectx == EGL_NO_CONTEXT) {
  277. error_report("egl: eglCreateContext failed");
  278. return NULL;
  279. }
  280. b = eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, ectx);
  281. if (b == EGL_FALSE) {
  282. error_report("egl: eglMakeCurrent failed");
  283. return NULL;
  284. }
  285. return ectx;
  286. }