egl-helpers.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674
  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 "qemu/drm.h"
  19. #include "qemu/error-report.h"
  20. #include "ui/console.h"
  21. #include "ui/egl-helpers.h"
  22. #include "system/system.h"
  23. #include "qapi/error.h"
  24. #include "trace.h"
  25. EGLDisplay *qemu_egl_display;
  26. EGLConfig qemu_egl_config;
  27. DisplayGLMode qemu_egl_mode;
  28. bool qemu_egl_angle_d3d;
  29. /* ------------------------------------------------------------------ */
  30. const char *qemu_egl_get_error_string(void)
  31. {
  32. EGLint error = eglGetError();
  33. switch (error) {
  34. case EGL_SUCCESS:
  35. return "EGL_SUCCESS";
  36. case EGL_NOT_INITIALIZED:
  37. return "EGL_NOT_INITIALIZED";
  38. case EGL_BAD_ACCESS:
  39. return "EGL_BAD_ACCESS";
  40. case EGL_BAD_ALLOC:
  41. return "EGL_BAD_ALLOC";
  42. case EGL_BAD_ATTRIBUTE:
  43. return "EGL_BAD_ATTRIBUTE";
  44. case EGL_BAD_CONTEXT:
  45. return "EGL_BAD_CONTEXT";
  46. case EGL_BAD_CONFIG:
  47. return "EGL_BAD_CONFIG";
  48. case EGL_BAD_CURRENT_SURFACE:
  49. return "EGL_BAD_CURRENT_SURFACE";
  50. case EGL_BAD_DISPLAY:
  51. return "EGL_BAD_DISPLAY";
  52. case EGL_BAD_SURFACE:
  53. return "EGL_BAD_SURFACE";
  54. case EGL_BAD_MATCH:
  55. return "EGL_BAD_MATCH";
  56. case EGL_BAD_PARAMETER:
  57. return "EGL_BAD_PARAMETER";
  58. case EGL_BAD_NATIVE_PIXMAP:
  59. return "EGL_BAD_NATIVE_PIXMAP";
  60. case EGL_BAD_NATIVE_WINDOW:
  61. return "EGL_BAD_NATIVE_WINDOW";
  62. case EGL_CONTEXT_LOST:
  63. return "EGL_CONTEXT_LOST";
  64. default:
  65. return "Unknown EGL error";
  66. }
  67. }
  68. static void egl_fb_delete_texture(egl_fb *fb)
  69. {
  70. if (!fb->delete_texture) {
  71. return;
  72. }
  73. glDeleteTextures(1, &fb->texture);
  74. fb->delete_texture = false;
  75. }
  76. void egl_fb_destroy(egl_fb *fb)
  77. {
  78. if (!fb->framebuffer) {
  79. return;
  80. }
  81. egl_fb_delete_texture(fb);
  82. glDeleteFramebuffers(1, &fb->framebuffer);
  83. fb->width = 0;
  84. fb->height = 0;
  85. fb->texture = 0;
  86. fb->framebuffer = 0;
  87. }
  88. void egl_fb_setup_default(egl_fb *fb, int width, int height)
  89. {
  90. fb->width = width;
  91. fb->height = height;
  92. fb->framebuffer = 0; /* default framebuffer */
  93. }
  94. void egl_fb_setup_for_tex(egl_fb *fb, int width, int height,
  95. GLuint texture, bool delete)
  96. {
  97. egl_fb_delete_texture(fb);
  98. fb->width = width;
  99. fb->height = height;
  100. fb->texture = texture;
  101. fb->delete_texture = delete;
  102. if (!fb->framebuffer) {
  103. glGenFramebuffers(1, &fb->framebuffer);
  104. }
  105. glBindFramebuffer(GL_FRAMEBUFFER_EXT, fb->framebuffer);
  106. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
  107. GL_TEXTURE_2D, fb->texture, 0);
  108. }
  109. void egl_fb_setup_new_tex(egl_fb *fb, int width, int height)
  110. {
  111. GLuint texture;
  112. glGenTextures(1, &texture);
  113. glBindTexture(GL_TEXTURE_2D, texture);
  114. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height,
  115. 0, GL_BGRA, GL_UNSIGNED_BYTE, 0);
  116. egl_fb_setup_for_tex(fb, width, height, texture, true);
  117. }
  118. void egl_fb_blit(egl_fb *dst, egl_fb *src, bool flip)
  119. {
  120. GLuint x1 = 0;
  121. GLuint y1 = 0;
  122. GLuint x2, y2;
  123. GLuint w = src->width;
  124. GLuint h = src->height;
  125. glBindFramebuffer(GL_READ_FRAMEBUFFER, src->framebuffer);
  126. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dst->framebuffer);
  127. glViewport(0, 0, dst->width, dst->height);
  128. if (src->dmabuf) {
  129. x1 = qemu_dmabuf_get_x(src->dmabuf);
  130. y1 = qemu_dmabuf_get_y(src->dmabuf);
  131. w = qemu_dmabuf_get_width(src->dmabuf);
  132. h = qemu_dmabuf_get_height(src->dmabuf);
  133. }
  134. w = (x1 + w) > src->width ? src->width - x1 : w;
  135. h = (y1 + h) > src->height ? src->height - y1 : h;
  136. y2 = flip ? y1 : h + y1;
  137. y1 = flip ? h + y1 : y1;
  138. x2 = x1 + w;
  139. glBlitFramebuffer(x1, y1, x2, y2,
  140. 0, 0, dst->width, dst->height,
  141. GL_COLOR_BUFFER_BIT, GL_LINEAR);
  142. }
  143. void egl_fb_read(DisplaySurface *dst, egl_fb *src)
  144. {
  145. glBindFramebuffer(GL_READ_FRAMEBUFFER, src->framebuffer);
  146. glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
  147. glReadPixels(0, 0, surface_width(dst), surface_height(dst),
  148. GL_BGRA, GL_UNSIGNED_BYTE, surface_data(dst));
  149. }
  150. void egl_fb_read_rect(DisplaySurface *dst, egl_fb *src, int x, int y, int w, int h)
  151. {
  152. assert(surface_width(dst) == src->width);
  153. assert(surface_height(dst) == src->height);
  154. assert(surface_format(dst) == PIXMAN_x8r8g8b8);
  155. glBindFramebuffer(GL_READ_FRAMEBUFFER, src->framebuffer);
  156. glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
  157. glPixelStorei(GL_PACK_ROW_LENGTH, surface_stride(dst) / 4);
  158. glReadPixels(x, y, w, h,
  159. GL_BGRA, GL_UNSIGNED_BYTE, surface_data(dst) + x * 4);
  160. glPixelStorei(GL_PACK_ROW_LENGTH, 0);
  161. }
  162. void egl_texture_blit(QemuGLShader *gls, egl_fb *dst, egl_fb *src, bool flip)
  163. {
  164. glBindFramebuffer(GL_FRAMEBUFFER_EXT, dst->framebuffer);
  165. glViewport(0, 0, dst->width, dst->height);
  166. glEnable(GL_TEXTURE_2D);
  167. glBindTexture(GL_TEXTURE_2D, src->texture);
  168. qemu_gl_run_texture_blit(gls, flip);
  169. }
  170. void egl_texture_blend(QemuGLShader *gls, egl_fb *dst, egl_fb *src, bool flip,
  171. int x, int y, double scale_x, double scale_y)
  172. {
  173. glBindFramebuffer(GL_FRAMEBUFFER_EXT, dst->framebuffer);
  174. int w = scale_x * src->width;
  175. int h = scale_y * src->height;
  176. if (flip) {
  177. glViewport(x, y, w, h);
  178. } else {
  179. glViewport(x, dst->height - h - y, w, h);
  180. }
  181. glEnable(GL_TEXTURE_2D);
  182. glBindTexture(GL_TEXTURE_2D, src->texture);
  183. glEnable(GL_BLEND);
  184. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  185. qemu_gl_run_texture_blit(gls, flip);
  186. glDisable(GL_BLEND);
  187. }
  188. /* ---------------------------------------------------------------------- */
  189. EGLContext qemu_egl_rn_ctx;
  190. #ifdef CONFIG_GBM
  191. int qemu_egl_rn_fd;
  192. struct gbm_device *qemu_egl_rn_gbm_dev;
  193. int egl_rendernode_init(const char *rendernode, DisplayGLMode mode)
  194. {
  195. qemu_egl_rn_fd = -1;
  196. int rc;
  197. qemu_egl_rn_fd = qemu_drm_rendernode_open(rendernode);
  198. if (qemu_egl_rn_fd == -1) {
  199. error_report("egl: no drm render node available");
  200. goto err;
  201. }
  202. qemu_egl_rn_gbm_dev = gbm_create_device(qemu_egl_rn_fd);
  203. if (!qemu_egl_rn_gbm_dev) {
  204. error_report("egl: gbm_create_device failed");
  205. goto err;
  206. }
  207. rc = qemu_egl_init_dpy_mesa((EGLNativeDisplayType)qemu_egl_rn_gbm_dev,
  208. mode);
  209. if (rc != 0) {
  210. /* qemu_egl_init_dpy_mesa reports error */
  211. goto err;
  212. }
  213. if (!epoxy_has_egl_extension(qemu_egl_display,
  214. "EGL_KHR_surfaceless_context")) {
  215. error_report("egl: EGL_KHR_surfaceless_context not supported");
  216. goto err;
  217. }
  218. if (!epoxy_has_egl_extension(qemu_egl_display,
  219. "EGL_MESA_image_dma_buf_export")) {
  220. error_report("egl: EGL_MESA_image_dma_buf_export not supported");
  221. goto err;
  222. }
  223. qemu_egl_rn_ctx = qemu_egl_init_ctx();
  224. if (!qemu_egl_rn_ctx) {
  225. error_report("egl: egl_init_ctx failed");
  226. goto err;
  227. }
  228. return 0;
  229. err:
  230. if (qemu_egl_rn_gbm_dev) {
  231. gbm_device_destroy(qemu_egl_rn_gbm_dev);
  232. }
  233. if (qemu_egl_rn_fd != -1) {
  234. close(qemu_egl_rn_fd);
  235. }
  236. return -1;
  237. }
  238. int egl_get_fd_for_texture(uint32_t tex_id, EGLint *stride, EGLint *fourcc,
  239. EGLuint64KHR *modifier)
  240. {
  241. EGLImageKHR image;
  242. EGLint num_planes, fd;
  243. image = eglCreateImageKHR(qemu_egl_display, eglGetCurrentContext(),
  244. EGL_GL_TEXTURE_2D_KHR,
  245. (EGLClientBuffer)(unsigned long)tex_id,
  246. NULL);
  247. if (!image) {
  248. return -1;
  249. }
  250. eglExportDMABUFImageQueryMESA(qemu_egl_display, image, fourcc,
  251. &num_planes, modifier);
  252. if (num_planes != 1) {
  253. eglDestroyImageKHR(qemu_egl_display, image);
  254. return -1;
  255. }
  256. eglExportDMABUFImageMESA(qemu_egl_display, image, &fd, stride, NULL);
  257. eglDestroyImageKHR(qemu_egl_display, image);
  258. return fd;
  259. }
  260. void egl_dmabuf_import_texture(QemuDmaBuf *dmabuf)
  261. {
  262. EGLImageKHR image = EGL_NO_IMAGE_KHR;
  263. EGLint attrs[64];
  264. int i = 0;
  265. uint64_t modifier;
  266. uint32_t texture = qemu_dmabuf_get_texture(dmabuf);
  267. if (texture != 0) {
  268. return;
  269. }
  270. attrs[i++] = EGL_WIDTH;
  271. attrs[i++] = qemu_dmabuf_get_backing_width(dmabuf);
  272. attrs[i++] = EGL_HEIGHT;
  273. attrs[i++] = qemu_dmabuf_get_backing_height(dmabuf);
  274. attrs[i++] = EGL_LINUX_DRM_FOURCC_EXT;
  275. attrs[i++] = qemu_dmabuf_get_fourcc(dmabuf);
  276. attrs[i++] = EGL_DMA_BUF_PLANE0_FD_EXT;
  277. attrs[i++] = qemu_dmabuf_get_fd(dmabuf);
  278. attrs[i++] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
  279. attrs[i++] = qemu_dmabuf_get_stride(dmabuf);
  280. attrs[i++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
  281. attrs[i++] = 0;
  282. #ifdef EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT
  283. modifier = qemu_dmabuf_get_modifier(dmabuf);
  284. if (modifier) {
  285. attrs[i++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT;
  286. attrs[i++] = (modifier >> 0) & 0xffffffff;
  287. attrs[i++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT;
  288. attrs[i++] = (modifier >> 32) & 0xffffffff;
  289. }
  290. #endif
  291. attrs[i++] = EGL_NONE;
  292. image = eglCreateImageKHR(qemu_egl_display,
  293. EGL_NO_CONTEXT,
  294. EGL_LINUX_DMA_BUF_EXT,
  295. NULL, attrs);
  296. if (image == EGL_NO_IMAGE_KHR) {
  297. error_report("eglCreateImageKHR failed");
  298. return;
  299. }
  300. glGenTextures(1, &texture);
  301. qemu_dmabuf_set_texture(dmabuf, texture);
  302. glBindTexture(GL_TEXTURE_2D, texture);
  303. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  304. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  305. glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
  306. eglDestroyImageKHR(qemu_egl_display, image);
  307. }
  308. void egl_dmabuf_release_texture(QemuDmaBuf *dmabuf)
  309. {
  310. uint32_t texture;
  311. texture = qemu_dmabuf_get_texture(dmabuf);
  312. if (texture == 0) {
  313. return;
  314. }
  315. glDeleteTextures(1, &texture);
  316. qemu_dmabuf_set_texture(dmabuf, 0);
  317. }
  318. void egl_dmabuf_create_sync(QemuDmaBuf *dmabuf)
  319. {
  320. EGLSyncKHR sync;
  321. if (epoxy_has_egl_extension(qemu_egl_display,
  322. "EGL_KHR_fence_sync") &&
  323. epoxy_has_egl_extension(qemu_egl_display,
  324. "EGL_ANDROID_native_fence_sync")) {
  325. sync = eglCreateSyncKHR(qemu_egl_display,
  326. EGL_SYNC_NATIVE_FENCE_ANDROID, NULL);
  327. if (sync != EGL_NO_SYNC_KHR) {
  328. qemu_dmabuf_set_sync(dmabuf, sync);
  329. }
  330. }
  331. }
  332. void egl_dmabuf_create_fence(QemuDmaBuf *dmabuf)
  333. {
  334. void *sync = qemu_dmabuf_get_sync(dmabuf);
  335. int fence_fd;
  336. if (sync) {
  337. fence_fd = eglDupNativeFenceFDANDROID(qemu_egl_display,
  338. sync);
  339. qemu_dmabuf_set_fence_fd(dmabuf, fence_fd);
  340. eglDestroySyncKHR(qemu_egl_display, sync);
  341. qemu_dmabuf_set_sync(dmabuf, NULL);
  342. }
  343. }
  344. #endif /* CONFIG_GBM */
  345. /* ---------------------------------------------------------------------- */
  346. EGLSurface qemu_egl_init_surface_x11(EGLContext ectx, EGLNativeWindowType win)
  347. {
  348. EGLSurface esurface;
  349. EGLBoolean b;
  350. esurface = eglCreateWindowSurface(qemu_egl_display,
  351. qemu_egl_config,
  352. win, NULL);
  353. if (esurface == EGL_NO_SURFACE) {
  354. error_report("egl: eglCreateWindowSurface failed");
  355. return NULL;
  356. }
  357. b = eglMakeCurrent(qemu_egl_display, esurface, esurface, ectx);
  358. if (b == EGL_FALSE) {
  359. error_report("egl: eglMakeCurrent failed");
  360. return NULL;
  361. }
  362. return esurface;
  363. }
  364. /* ---------------------------------------------------------------------- */
  365. #if defined(CONFIG_X11) || defined(CONFIG_GBM) || defined(WIN32)
  366. /*
  367. * Taken from glamor_egl.h from the Xorg xserver, which is MIT licensed
  368. *
  369. * Create an EGLDisplay from a native display type. This is a little quirky
  370. * for a few reasons.
  371. *
  372. * 1: GetPlatformDisplayEXT and GetPlatformDisplay are the API you want to
  373. * use, but have different function signatures in the third argument; this
  374. * happens not to matter for us, at the moment, but it means epoxy won't alias
  375. * them together.
  376. *
  377. * 2: epoxy 1.3 and earlier don't understand EGL client extensions, which
  378. * means you can't call "eglGetPlatformDisplayEXT" directly, as the resolver
  379. * will crash.
  380. *
  381. * 3: You can't tell whether you have EGL 1.5 at this point, because
  382. * eglQueryString(EGL_VERSION) is a property of the display, which we don't
  383. * have yet. So you have to query for extensions no matter what. Fortunately
  384. * epoxy_has_egl_extension _does_ let you query for client extensions, so
  385. * we don't have to write our own extension string parsing.
  386. *
  387. * 4. There is no EGL_KHR_platform_base to complement the EXT one, thus one
  388. * needs to know EGL 1.5 is supported in order to use the eglGetPlatformDisplay
  389. * function pointer.
  390. * We can workaround this (circular dependency) by probing for the EGL 1.5
  391. * platform extensions (EGL_KHR_platform_gbm and friends) yet it doesn't seem
  392. * like mesa will be able to advertise these (even though it can do EGL 1.5).
  393. */
  394. static EGLDisplay qemu_egl_get_display(EGLNativeDisplayType native,
  395. EGLenum platform)
  396. {
  397. EGLDisplay dpy = EGL_NO_DISPLAY;
  398. /* In practise any EGL 1.5 implementation would support the EXT extension */
  399. if (epoxy_has_egl_extension(NULL, "EGL_EXT_platform_base")) {
  400. if (platform != 0) {
  401. dpy = eglGetPlatformDisplayEXT(platform, native, NULL);
  402. }
  403. }
  404. if (dpy == EGL_NO_DISPLAY) {
  405. /* fallback */
  406. dpy = eglGetDisplay(native);
  407. }
  408. return dpy;
  409. }
  410. static int qemu_egl_init_dpy(EGLNativeDisplayType dpy,
  411. EGLenum platform,
  412. DisplayGLMode mode)
  413. {
  414. static const EGLint conf_att_core[] = {
  415. EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  416. EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
  417. EGL_RED_SIZE, 5,
  418. EGL_GREEN_SIZE, 5,
  419. EGL_BLUE_SIZE, 5,
  420. EGL_ALPHA_SIZE, 0,
  421. EGL_NONE,
  422. };
  423. static const EGLint conf_att_gles[] = {
  424. EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  425. EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
  426. EGL_RED_SIZE, 5,
  427. EGL_GREEN_SIZE, 5,
  428. EGL_BLUE_SIZE, 5,
  429. EGL_ALPHA_SIZE, 0,
  430. EGL_NONE,
  431. };
  432. EGLint major, minor;
  433. EGLBoolean b;
  434. EGLint n;
  435. bool gles = (mode == DISPLAY_GL_MODE_ES);
  436. qemu_egl_display = qemu_egl_get_display(dpy, platform);
  437. if (qemu_egl_display == EGL_NO_DISPLAY) {
  438. error_report("egl: eglGetDisplay failed: %s", qemu_egl_get_error_string());
  439. return -1;
  440. }
  441. b = eglInitialize(qemu_egl_display, &major, &minor);
  442. if (b == EGL_FALSE) {
  443. error_report("egl: eglInitialize failed: %s", qemu_egl_get_error_string());
  444. return -1;
  445. }
  446. b = eglBindAPI(gles ? EGL_OPENGL_ES_API : EGL_OPENGL_API);
  447. if (b == EGL_FALSE) {
  448. error_report("egl: eglBindAPI failed (%s mode): %s",
  449. gles ? "gles" : "core", qemu_egl_get_error_string());
  450. return -1;
  451. }
  452. b = eglChooseConfig(qemu_egl_display,
  453. gles ? conf_att_gles : conf_att_core,
  454. &qemu_egl_config, 1, &n);
  455. if (b == EGL_FALSE || n != 1) {
  456. error_report("egl: eglChooseConfig failed (%s mode): %s",
  457. gles ? "gles" : "core", qemu_egl_get_error_string());
  458. return -1;
  459. }
  460. qemu_egl_mode = gles ? DISPLAY_GL_MODE_ES : DISPLAY_GL_MODE_CORE;
  461. return 0;
  462. }
  463. #endif
  464. #if defined(CONFIG_X11) || defined(CONFIG_GBM)
  465. int qemu_egl_init_dpy_x11(EGLNativeDisplayType dpy, DisplayGLMode mode)
  466. {
  467. #ifdef EGL_KHR_platform_x11
  468. return qemu_egl_init_dpy(dpy, EGL_PLATFORM_X11_KHR, mode);
  469. #else
  470. return qemu_egl_init_dpy(dpy, 0, mode);
  471. #endif
  472. }
  473. int qemu_egl_init_dpy_mesa(EGLNativeDisplayType dpy, DisplayGLMode mode)
  474. {
  475. #ifdef EGL_MESA_platform_gbm
  476. return qemu_egl_init_dpy(dpy, EGL_PLATFORM_GBM_MESA, mode);
  477. #else
  478. return qemu_egl_init_dpy(dpy, 0, mode);
  479. #endif
  480. }
  481. #endif
  482. #ifdef WIN32
  483. int qemu_egl_init_dpy_win32(EGLNativeDisplayType dpy, DisplayGLMode mode)
  484. {
  485. /* prefer GL ES, as that's what ANGLE supports */
  486. if (mode == DISPLAY_GL_MODE_ON) {
  487. mode = DISPLAY_GL_MODE_ES;
  488. }
  489. if (qemu_egl_init_dpy(dpy, 0, mode) < 0) {
  490. return -1;
  491. }
  492. #ifdef EGL_D3D11_DEVICE_ANGLE
  493. if (epoxy_has_egl_extension(qemu_egl_display, "EGL_EXT_device_query")) {
  494. EGLDeviceEXT device;
  495. void *d3d11_device;
  496. if (!eglQueryDisplayAttribEXT(qemu_egl_display,
  497. EGL_DEVICE_EXT,
  498. (EGLAttrib *)&device)) {
  499. return 0;
  500. }
  501. if (!eglQueryDeviceAttribEXT(device,
  502. EGL_D3D11_DEVICE_ANGLE,
  503. (EGLAttrib *)&d3d11_device)) {
  504. return 0;
  505. }
  506. trace_egl_init_d3d11_device(device);
  507. qemu_egl_angle_d3d = device != NULL;
  508. }
  509. #endif
  510. return 0;
  511. }
  512. #endif
  513. bool qemu_egl_has_dmabuf(void)
  514. {
  515. if (qemu_egl_display == EGL_NO_DISPLAY) {
  516. return false;
  517. }
  518. return epoxy_has_egl_extension(qemu_egl_display,
  519. "EGL_EXT_image_dma_buf_import");
  520. }
  521. EGLContext qemu_egl_init_ctx(void)
  522. {
  523. static const EGLint ctx_att_core[] = {
  524. EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT,
  525. EGL_NONE
  526. };
  527. static const EGLint ctx_att_gles[] = {
  528. EGL_CONTEXT_CLIENT_VERSION, 2,
  529. EGL_NONE
  530. };
  531. bool gles = (qemu_egl_mode == DISPLAY_GL_MODE_ES);
  532. EGLContext ectx;
  533. EGLBoolean b;
  534. ectx = eglCreateContext(qemu_egl_display, qemu_egl_config, EGL_NO_CONTEXT,
  535. gles ? ctx_att_gles : ctx_att_core);
  536. if (ectx == EGL_NO_CONTEXT) {
  537. error_report("egl: eglCreateContext failed");
  538. return NULL;
  539. }
  540. b = eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, ectx);
  541. if (b == EGL_FALSE) {
  542. error_report("egl: eglMakeCurrent failed");
  543. return NULL;
  544. }
  545. return ectx;
  546. }
  547. bool egl_init(const char *rendernode, DisplayGLMode mode, Error **errp)
  548. {
  549. ERRP_GUARD();
  550. if (mode == DISPLAY_GL_MODE_OFF) {
  551. error_setg(errp, "egl: turning off GL doesn't make sense");
  552. return false;
  553. }
  554. #ifdef WIN32
  555. if (qemu_egl_init_dpy_win32(EGL_DEFAULT_DISPLAY, mode) < 0) {
  556. error_setg(errp, "egl: init failed");
  557. return false;
  558. }
  559. qemu_egl_rn_ctx = qemu_egl_init_ctx();
  560. if (!qemu_egl_rn_ctx) {
  561. error_setg(errp, "egl: egl_init_ctx failed");
  562. return false;
  563. }
  564. #elif defined(CONFIG_GBM)
  565. if (egl_rendernode_init(rendernode, mode) < 0) {
  566. error_setg(errp, "egl: render node init failed");
  567. return false;
  568. }
  569. #endif
  570. if (!qemu_egl_rn_ctx) {
  571. error_setg(errp, "egl: not available on this platform");
  572. return false;
  573. }
  574. display_opengl = 1;
  575. return true;
  576. }