From: Gerd Hoffmann <kraxel@redhat.com>
To: qemu-devel@nongnu.org
Cc: Gerd Hoffmann <kraxel@redhat.com>
Subject: [Qemu-devel] [PULL 1/6] egl-helpers: add helpers to handle opengl framebuffers
Date: Wed, 21 Jun 2017 15:23:35 +0200 [thread overview]
Message-ID: <20170621132340.27686-2-kraxel@redhat.com> (raw)
In-Reply-To: <20170621132340.27686-1-kraxel@redhat.com>
Add a collection of egl_fb_*() helper functions to manage and use opengl
framebuffers, which is a common pattern in UI code with opengl support.
Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
Message-id: 20170614084149.31314-2-kraxel@redhat.com
---
include/ui/egl-helpers.h | 15 ++++++++++
ui/egl-helpers.c | 76 ++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 91 insertions(+)
diff --git a/include/ui/egl-helpers.h b/include/ui/egl-helpers.h
index c785d60e91..be8908737c 100644
--- a/include/ui/egl-helpers.h
+++ b/include/ui/egl-helpers.h
@@ -8,6 +8,21 @@
extern EGLDisplay *qemu_egl_display;
extern EGLConfig qemu_egl_config;
+typedef struct egl_fb {
+ int width;
+ int height;
+ GLuint texture;
+ GLuint framebuffer;
+ bool delete_texture;
+} egl_fb;
+
+void egl_fb_destroy(egl_fb *fb);
+void egl_fb_setup_default(egl_fb *fb, int width, int height);
+void egl_fb_create_for_tex(egl_fb *fb, int width, int height, GLuint texture);
+void egl_fb_create_new_tex(egl_fb *fb, int width, int height);
+void egl_fb_blit(egl_fb *dst, egl_fb *src, bool flip);
+void egl_fb_read(void *dst, egl_fb *src);
+
#ifdef CONFIG_OPENGL_DMABUF
extern int qemu_egl_rn_fd;
diff --git a/ui/egl-helpers.c b/ui/egl-helpers.c
index 4a4d3370ee..bb19a5eeca 100644
--- a/ui/egl-helpers.c
+++ b/ui/egl-helpers.c
@@ -24,6 +24,82 @@
EGLDisplay *qemu_egl_display;
EGLConfig qemu_egl_config;
+/* ------------------------------------------------------------------ */
+
+void egl_fb_destroy(egl_fb *fb)
+{
+ if (!fb->framebuffer) {
+ return;
+ }
+
+ if (fb->delete_texture) {
+ glDeleteTextures(1, &fb->texture);
+ fb->delete_texture = false;
+ }
+ glDeleteFramebuffers(1, &fb->framebuffer);
+
+ fb->width = 0;
+ fb->height = 0;
+ fb->texture = 0;
+ fb->framebuffer = 0;
+}
+
+void egl_fb_setup_default(egl_fb *fb, int width, int height)
+{
+ fb->width = width;
+ fb->height = height;
+ fb->framebuffer = 0; /* default framebuffer */
+}
+
+void egl_fb_create_for_tex(egl_fb *fb, int width, int height, GLuint texture)
+{
+ fb->width = width;
+ fb->height = height;
+ fb->texture = texture;
+ if (!fb->framebuffer) {
+ glGenFramebuffers(1, &fb->framebuffer);
+ }
+
+ glBindFramebuffer(GL_FRAMEBUFFER_EXT, fb->framebuffer);
+ glFramebufferTexture2DEXT(GL_FRAMEBUFFER_EXT, GL_COLOR_ATTACHMENT0_EXT,
+ GL_TEXTURE_2D, fb->texture, 0);
+}
+
+void egl_fb_create_new_tex(egl_fb *fb, int width, int height)
+{
+ GLuint texture;
+
+ glGenTextures(1, &texture);
+ glBindTexture(GL_TEXTURE_2D, texture);
+ glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height,
+ 0, GL_BGRA, GL_UNSIGNED_BYTE, 0);
+
+ egl_fb_create_for_tex(fb, width, height, texture);
+ fb->delete_texture = true;
+}
+
+void egl_fb_blit(egl_fb *dst, egl_fb *src, bool flip)
+{
+ GLuint y1, y2;
+
+ glBindFramebuffer(GL_READ_FRAMEBUFFER, src->framebuffer);
+ glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dst->framebuffer);
+ glViewport(0, 0, dst->width, dst->height);
+ y1 = flip ? src->height : 0;
+ y2 = flip ? 0 : src->height;
+ glBlitFramebuffer(0, y1, src->width, y2,
+ 0, 0, dst->width, dst->height,
+ GL_COLOR_BUFFER_BIT, GL_LINEAR);
+}
+
+void egl_fb_read(void *dst, egl_fb *src)
+{
+ glBindFramebuffer(GL_READ_FRAMEBUFFER, src->framebuffer);
+ glReadBuffer(GL_COLOR_ATTACHMENT0_EXT);
+ glReadPixels(0, 0, src->width, src->height,
+ GL_BGRA, GL_UNSIGNED_BYTE, dst);
+}
+
/* ---------------------------------------------------------------------- */
#ifdef CONFIG_OPENGL_DMABUF
--
2.9.3
next prev parent reply other threads:[~2017-06-21 13:23 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-06-21 13:23 [Qemu-devel] [PULL 0/6] Queue/ui patches Gerd Hoffmann
2017-06-21 13:23 ` Gerd Hoffmann [this message]
2017-06-21 13:23 ` [Qemu-devel] [PULL 2/6] egl-headless: use framebuffer helper functions Gerd Hoffmann
2017-06-21 13:23 ` [Qemu-devel] [PULL 3/6] sdl2: " Gerd Hoffmann
2017-06-21 13:23 ` [Qemu-devel] [PULL 4/6] gtk: " Gerd Hoffmann
2017-06-21 13:23 ` [Qemu-devel] [PULL 5/6] console: remove do_safe_dpy_refresh Gerd Hoffmann
2017-07-18 13:07 ` Laurent Vivier
2017-07-18 13:56 ` Laurent Vivier
2017-07-18 14:37 ` Dr. David Alan Gilbert
2017-07-23 13:05 ` Paolo Bonzini
2017-06-21 13:23 ` [Qemu-devel] [PULL 6/6] ui: Remove inclusion of "hw/qdev.h" Gerd Hoffmann
2017-06-22 13:32 ` [Qemu-devel] [PULL 0/6] Queue/ui patches Peter Maydell
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20170621132340.27686-2-kraxel@redhat.com \
--to=kraxel@redhat.com \
--cc=qemu-devel@nongnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).