egl-helpers.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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. EGLDisplay *qemu_egl_display;
  23. EGLConfig qemu_egl_config;
  24. DisplayGLMode qemu_egl_mode;
  25. /* ------------------------------------------------------------------ */
  26. static void egl_fb_delete_texture(egl_fb *fb)
  27. {
  28. if (!fb->delete_texture) {
  29. return;
  30. }
  31. glDeleteTextures(1, &fb->texture);
  32. fb->delete_texture = false;
  33. }
  34. void egl_fb_destroy(egl_fb *fb)
  35. {
  36. if (!fb->framebuffer) {
  37. return;
  38. }
  39. egl_fb_delete_texture(fb);
  40. glDeleteFramebuffers(1, &fb->framebuffer);
  41. fb->width = 0;
  42. fb->height = 0;
  43. fb->texture = 0;
  44. fb->framebuffer = 0;
  45. }
  46. void egl_fb_setup_default(egl_fb *fb, int width, int height)
  47. {
  48. fb->width = width;
  49. fb->height = height;
  50. fb->framebuffer = 0; /* default framebuffer */
  51. }
  52. void egl_fb_setup_for_tex(egl_fb *fb, int width, int height,
  53. GLuint texture, bool delete)
  54. {
  55. egl_fb_delete_texture(fb);
  56. fb->width = width;
  57. fb->height = height;
  58. fb->texture = texture;
  59. fb->delete_texture = delete;
  60. if (!fb->framebuffer) {
  61. glGenFramebuffers(1, &fb->framebuffer);
  62. }
  63. glBindFramebuffer(GL_FRAMEBUFFER_EXT, fb->framebuffer);
  64. glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
  65. GL_TEXTURE_2D, fb->texture, 0);
  66. }
  67. void egl_fb_setup_new_tex(egl_fb *fb, int width, int height)
  68. {
  69. GLuint texture;
  70. glGenTextures(1, &texture);
  71. glBindTexture(GL_TEXTURE_2D, texture);
  72. glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height,
  73. 0, GL_BGRA, GL_UNSIGNED_BYTE, 0);
  74. egl_fb_setup_for_tex(fb, width, height, texture, true);
  75. }
  76. void egl_fb_blit(egl_fb *dst, egl_fb *src, bool flip)
  77. {
  78. GLuint y1, y2;
  79. glBindFramebuffer(GL_READ_FRAMEBUFFER, src->framebuffer);
  80. glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dst->framebuffer);
  81. glViewport(0, 0, dst->width, dst->height);
  82. y1 = flip ? src->height : 0;
  83. y2 = flip ? 0 : src->height;
  84. glBlitFramebuffer(0, y1, src->width, y2,
  85. 0, 0, dst->width, dst->height,
  86. GL_COLOR_BUFFER_BIT, GL_LINEAR);
  87. }
  88. void egl_fb_read(DisplaySurface *dst, egl_fb *src)
  89. {
  90. glBindFramebuffer(GL_READ_FRAMEBUFFER, src->framebuffer);
  91. glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
  92. glReadPixels(0, 0, surface_width(dst), surface_height(dst),
  93. GL_BGRA, GL_UNSIGNED_BYTE, surface_data(dst));
  94. }
  95. void egl_texture_blit(QemuGLShader *gls, egl_fb *dst, egl_fb *src, bool flip)
  96. {
  97. glBindFramebuffer(GL_FRAMEBUFFER_EXT, dst->framebuffer);
  98. glViewport(0, 0, dst->width, dst->height);
  99. glEnable(GL_TEXTURE_2D);
  100. glBindTexture(GL_TEXTURE_2D, src->texture);
  101. qemu_gl_run_texture_blit(gls, flip);
  102. }
  103. void egl_texture_blend(QemuGLShader *gls, egl_fb *dst, egl_fb *src, bool flip,
  104. int x, int y, double scale_x, double scale_y)
  105. {
  106. glBindFramebuffer(GL_FRAMEBUFFER_EXT, dst->framebuffer);
  107. int w = scale_x * src->width;
  108. int h = scale_y * src->height;
  109. if (flip) {
  110. glViewport(x, y, w, h);
  111. } else {
  112. glViewport(x, dst->height - h - y, w, h);
  113. }
  114. glEnable(GL_TEXTURE_2D);
  115. glBindTexture(GL_TEXTURE_2D, src->texture);
  116. glEnable(GL_BLEND);
  117. glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  118. qemu_gl_run_texture_blit(gls, flip);
  119. glDisable(GL_BLEND);
  120. }
  121. /* ---------------------------------------------------------------------- */
  122. #ifdef CONFIG_OPENGL_DMABUF
  123. int qemu_egl_rn_fd;
  124. struct gbm_device *qemu_egl_rn_gbm_dev;
  125. EGLContext qemu_egl_rn_ctx;
  126. int egl_rendernode_init(const char *rendernode, DisplayGLMode mode)
  127. {
  128. qemu_egl_rn_fd = -1;
  129. int rc;
  130. qemu_egl_rn_fd = qemu_drm_rendernode_open(rendernode);
  131. if (qemu_egl_rn_fd == -1) {
  132. error_report("egl: no drm render node available");
  133. goto err;
  134. }
  135. qemu_egl_rn_gbm_dev = gbm_create_device(qemu_egl_rn_fd);
  136. if (!qemu_egl_rn_gbm_dev) {
  137. error_report("egl: gbm_create_device failed");
  138. goto err;
  139. }
  140. rc = qemu_egl_init_dpy_mesa((EGLNativeDisplayType)qemu_egl_rn_gbm_dev,
  141. mode);
  142. if (rc != 0) {
  143. /* qemu_egl_init_dpy_mesa reports error */
  144. goto err;
  145. }
  146. if (!epoxy_has_egl_extension(qemu_egl_display,
  147. "EGL_KHR_surfaceless_context")) {
  148. error_report("egl: EGL_KHR_surfaceless_context not supported");
  149. goto err;
  150. }
  151. if (!epoxy_has_egl_extension(qemu_egl_display,
  152. "EGL_MESA_image_dma_buf_export")) {
  153. error_report("egl: EGL_MESA_image_dma_buf_export not supported");
  154. goto err;
  155. }
  156. qemu_egl_rn_ctx = qemu_egl_init_ctx();
  157. if (!qemu_egl_rn_ctx) {
  158. error_report("egl: egl_init_ctx failed");
  159. goto err;
  160. }
  161. return 0;
  162. err:
  163. if (qemu_egl_rn_gbm_dev) {
  164. gbm_device_destroy(qemu_egl_rn_gbm_dev);
  165. }
  166. if (qemu_egl_rn_fd != -1) {
  167. close(qemu_egl_rn_fd);
  168. }
  169. return -1;
  170. }
  171. int egl_get_fd_for_texture(uint32_t tex_id, EGLint *stride, EGLint *fourcc,
  172. EGLuint64KHR *modifier)
  173. {
  174. EGLImageKHR image;
  175. EGLint num_planes, fd;
  176. image = eglCreateImageKHR(qemu_egl_display, eglGetCurrentContext(),
  177. EGL_GL_TEXTURE_2D_KHR,
  178. (EGLClientBuffer)(unsigned long)tex_id,
  179. NULL);
  180. if (!image) {
  181. return -1;
  182. }
  183. eglExportDMABUFImageQueryMESA(qemu_egl_display, image, fourcc,
  184. &num_planes, modifier);
  185. if (num_planes != 1) {
  186. eglDestroyImageKHR(qemu_egl_display, image);
  187. return -1;
  188. }
  189. eglExportDMABUFImageMESA(qemu_egl_display, image, &fd, stride, NULL);
  190. eglDestroyImageKHR(qemu_egl_display, image);
  191. return fd;
  192. }
  193. void egl_dmabuf_import_texture(QemuDmaBuf *dmabuf)
  194. {
  195. EGLImageKHR image = EGL_NO_IMAGE_KHR;
  196. EGLint attrs[64];
  197. int i = 0;
  198. if (dmabuf->texture != 0) {
  199. return;
  200. }
  201. attrs[i++] = EGL_WIDTH;
  202. attrs[i++] = dmabuf->width;
  203. attrs[i++] = EGL_HEIGHT;
  204. attrs[i++] = dmabuf->height;
  205. attrs[i++] = EGL_LINUX_DRM_FOURCC_EXT;
  206. attrs[i++] = dmabuf->fourcc;
  207. attrs[i++] = EGL_DMA_BUF_PLANE0_FD_EXT;
  208. attrs[i++] = dmabuf->fd;
  209. attrs[i++] = EGL_DMA_BUF_PLANE0_PITCH_EXT;
  210. attrs[i++] = dmabuf->stride;
  211. attrs[i++] = EGL_DMA_BUF_PLANE0_OFFSET_EXT;
  212. attrs[i++] = 0;
  213. #ifdef EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT
  214. if (dmabuf->modifier) {
  215. attrs[i++] = EGL_DMA_BUF_PLANE0_MODIFIER_LO_EXT;
  216. attrs[i++] = (dmabuf->modifier >> 0) & 0xffffffff;
  217. attrs[i++] = EGL_DMA_BUF_PLANE0_MODIFIER_HI_EXT;
  218. attrs[i++] = (dmabuf->modifier >> 32) & 0xffffffff;
  219. }
  220. #endif
  221. attrs[i++] = EGL_NONE;
  222. image = eglCreateImageKHR(qemu_egl_display,
  223. EGL_NO_CONTEXT,
  224. EGL_LINUX_DMA_BUF_EXT,
  225. NULL, attrs);
  226. if (image == EGL_NO_IMAGE_KHR) {
  227. error_report("eglCreateImageKHR failed");
  228. return;
  229. }
  230. glGenTextures(1, &dmabuf->texture);
  231. glBindTexture(GL_TEXTURE_2D, dmabuf->texture);
  232. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  233. glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
  234. glEGLImageTargetTexture2DOES(GL_TEXTURE_2D, (GLeglImageOES)image);
  235. eglDestroyImageKHR(qemu_egl_display, image);
  236. }
  237. void egl_dmabuf_release_texture(QemuDmaBuf *dmabuf)
  238. {
  239. if (dmabuf->texture == 0) {
  240. return;
  241. }
  242. glDeleteTextures(1, &dmabuf->texture);
  243. dmabuf->texture = 0;
  244. }
  245. #endif /* CONFIG_OPENGL_DMABUF */
  246. /* ---------------------------------------------------------------------- */
  247. EGLSurface qemu_egl_init_surface_x11(EGLContext ectx, EGLNativeWindowType win)
  248. {
  249. EGLSurface esurface;
  250. EGLBoolean b;
  251. esurface = eglCreateWindowSurface(qemu_egl_display,
  252. qemu_egl_config,
  253. win, NULL);
  254. if (esurface == EGL_NO_SURFACE) {
  255. error_report("egl: eglCreateWindowSurface failed");
  256. return NULL;
  257. }
  258. b = eglMakeCurrent(qemu_egl_display, esurface, esurface, ectx);
  259. if (b == EGL_FALSE) {
  260. error_report("egl: eglMakeCurrent failed");
  261. return NULL;
  262. }
  263. return esurface;
  264. }
  265. /* ---------------------------------------------------------------------- */
  266. /*
  267. * Taken from glamor_egl.h from the Xorg xserver, which is MIT licensed
  268. *
  269. * Create an EGLDisplay from a native display type. This is a little quirky
  270. * for a few reasons.
  271. *
  272. * 1: GetPlatformDisplayEXT and GetPlatformDisplay are the API you want to
  273. * use, but have different function signatures in the third argument; this
  274. * happens not to matter for us, at the moment, but it means epoxy won't alias
  275. * them together.
  276. *
  277. * 2: epoxy 1.3 and earlier don't understand EGL client extensions, which
  278. * means you can't call "eglGetPlatformDisplayEXT" directly, as the resolver
  279. * will crash.
  280. *
  281. * 3: You can't tell whether you have EGL 1.5 at this point, because
  282. * eglQueryString(EGL_VERSION) is a property of the display, which we don't
  283. * have yet. So you have to query for extensions no matter what. Fortunately
  284. * epoxy_has_egl_extension _does_ let you query for client extensions, so
  285. * we don't have to write our own extension string parsing.
  286. *
  287. * 4. There is no EGL_KHR_platform_base to complement the EXT one, thus one
  288. * needs to know EGL 1.5 is supported in order to use the eglGetPlatformDisplay
  289. * function pointer.
  290. * We can workaround this (circular dependency) by probing for the EGL 1.5
  291. * platform extensions (EGL_KHR_platform_gbm and friends) yet it doesn't seem
  292. * like mesa will be able to advertise these (even though it can do EGL 1.5).
  293. */
  294. static EGLDisplay qemu_egl_get_display(EGLNativeDisplayType native,
  295. EGLenum platform)
  296. {
  297. EGLDisplay dpy = EGL_NO_DISPLAY;
  298. /* In practise any EGL 1.5 implementation would support the EXT extension */
  299. if (epoxy_has_egl_extension(NULL, "EGL_EXT_platform_base")) {
  300. PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplayEXT =
  301. (void *) eglGetProcAddress("eglGetPlatformDisplayEXT");
  302. if (getPlatformDisplayEXT && platform != 0) {
  303. dpy = getPlatformDisplayEXT(platform, native, NULL);
  304. }
  305. }
  306. if (dpy == EGL_NO_DISPLAY) {
  307. /* fallback */
  308. dpy = eglGetDisplay(native);
  309. }
  310. return dpy;
  311. }
  312. static int qemu_egl_init_dpy(EGLNativeDisplayType dpy,
  313. EGLenum platform,
  314. DisplayGLMode mode)
  315. {
  316. static const EGLint conf_att_core[] = {
  317. EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  318. EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
  319. EGL_RED_SIZE, 5,
  320. EGL_GREEN_SIZE, 5,
  321. EGL_BLUE_SIZE, 5,
  322. EGL_ALPHA_SIZE, 0,
  323. EGL_NONE,
  324. };
  325. static const EGLint conf_att_gles[] = {
  326. EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
  327. EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT,
  328. EGL_RED_SIZE, 5,
  329. EGL_GREEN_SIZE, 5,
  330. EGL_BLUE_SIZE, 5,
  331. EGL_ALPHA_SIZE, 0,
  332. EGL_NONE,
  333. };
  334. EGLint major, minor;
  335. EGLBoolean b;
  336. EGLint n;
  337. bool gles = (mode == DISPLAYGL_MODE_ES);
  338. qemu_egl_display = qemu_egl_get_display(dpy, platform);
  339. if (qemu_egl_display == EGL_NO_DISPLAY) {
  340. error_report("egl: eglGetDisplay failed");
  341. return -1;
  342. }
  343. b = eglInitialize(qemu_egl_display, &major, &minor);
  344. if (b == EGL_FALSE) {
  345. error_report("egl: eglInitialize failed");
  346. return -1;
  347. }
  348. b = eglBindAPI(gles ? EGL_OPENGL_ES_API : EGL_OPENGL_API);
  349. if (b == EGL_FALSE) {
  350. error_report("egl: eglBindAPI failed (%s mode)",
  351. gles ? "gles" : "core");
  352. return -1;
  353. }
  354. b = eglChooseConfig(qemu_egl_display,
  355. gles ? conf_att_gles : conf_att_core,
  356. &qemu_egl_config, 1, &n);
  357. if (b == EGL_FALSE || n != 1) {
  358. error_report("egl: eglChooseConfig failed (%s mode)",
  359. gles ? "gles" : "core");
  360. return -1;
  361. }
  362. qemu_egl_mode = gles ? DISPLAYGL_MODE_ES : DISPLAYGL_MODE_CORE;
  363. return 0;
  364. }
  365. int qemu_egl_init_dpy_x11(EGLNativeDisplayType dpy, DisplayGLMode mode)
  366. {
  367. #ifdef EGL_KHR_platform_x11
  368. return qemu_egl_init_dpy(dpy, EGL_PLATFORM_X11_KHR, mode);
  369. #else
  370. return qemu_egl_init_dpy(dpy, 0, mode);
  371. #endif
  372. }
  373. int qemu_egl_init_dpy_mesa(EGLNativeDisplayType dpy, DisplayGLMode mode)
  374. {
  375. #ifdef EGL_MESA_platform_gbm
  376. return qemu_egl_init_dpy(dpy, EGL_PLATFORM_GBM_MESA, mode);
  377. #else
  378. return qemu_egl_init_dpy(dpy, 0, mode);
  379. #endif
  380. }
  381. EGLContext qemu_egl_init_ctx(void)
  382. {
  383. static const EGLint ctx_att_core[] = {
  384. EGL_CONTEXT_OPENGL_PROFILE_MASK, EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT,
  385. EGL_NONE
  386. };
  387. static const EGLint ctx_att_gles[] = {
  388. EGL_CONTEXT_CLIENT_VERSION, 2,
  389. EGL_NONE
  390. };
  391. bool gles = (qemu_egl_mode == DISPLAYGL_MODE_ES);
  392. EGLContext ectx;
  393. EGLBoolean b;
  394. ectx = eglCreateContext(qemu_egl_display, qemu_egl_config, EGL_NO_CONTEXT,
  395. gles ? ctx_att_gles : ctx_att_core);
  396. if (ectx == EGL_NO_CONTEXT) {
  397. error_report("egl: eglCreateContext failed");
  398. return NULL;
  399. }
  400. b = eglMakeCurrent(qemu_egl_display, EGL_NO_SURFACE, EGL_NO_SURFACE, ectx);
  401. if (b == EGL_FALSE) {
  402. error_report("egl: eglMakeCurrent failed");
  403. return NULL;
  404. }
  405. return ectx;
  406. }