2
0

egl-helpers.c 21 KB

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