qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [RfC PATCH 2/3] sdl2: add support for display rendering


From: Max Reitz
Subject: Re: [Qemu-devel] [RfC PATCH 2/3] sdl2: add support for display rendering using opengl.
Date: Thu, 11 Dec 2014 16:57:22 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0

On 2014-12-11 at 12:05, Gerd Hoffmann wrote:
Add new sdl2-gl.c file, with display
rendering functions using opengl.

Signed-off-by: Gerd Hoffmann <address@hidden>
---
  include/ui/sdl2.h |  10 ++++
  ui/Makefile.objs  |   4 ++
  ui/sdl2-2d.c      |   6 +++
  ui/sdl2-gl.c      | 143 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
  ui/sdl2.c         |  50 ++++++++++++++++---
  5 files changed, 206 insertions(+), 7 deletions(-)
  create mode 100644 ui/sdl2-gl.c

diff --git a/ui/sdl2-gl.c b/ui/sdl2-gl.c
new file mode 100644
index 0000000..30018d4
--- /dev/null
+++ b/ui/sdl2-gl.c
@@ -0,0 +1,143 @@
+/*
+ * QEMU SDL display driver
+ *
+ * Copyright (c) 2003 Fabrice Bellard
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to 
deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+/* Ported SDL 1.2 code to 2.0 by Dave Airlie. */
+
+/* Avoid compiler warning because macro is redefined in SDL_syswm.h. */
+#undef WIN32_LEAN_AND_MEAN
+
+#include <SDL.h>
+#include <SDL_syswm.h>
+#include <SDL_opengl.h>
+
+#include "qemu-common.h"
+#include "ui/console.h"
+#include "ui/input.h"
+#include "ui/sdl2.h"
+#include "sysemu/sysemu.h"
+
+static void sdl2_gl_render_surface(struct sdl2_console *scon)
+{
+    int gw, gh, ww, wh, stripe;
+    float sw, sh;
+    GLuint tex;
+
+    gw = surface_width(scon->surface);
+    gh = surface_height(scon->surface);
+    SDL_GetWindowSize(scon->real_window, &ww, &wh);
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
+
+    sw = (float)ww/gw;
+    sh = (float)wh/gh;
+    if (sw < sh) {
+        stripe = wh - wh*sw/sh;
+        glViewport(0, stripe / 2, ww, wh - stripe);
+    } else {
+        stripe = ww - ww*sh/sw;
+        glViewport(stripe / 2, 0, ww - stripe, wh);
+    }
+
+    glMatrixMode(GL_PROJECTION);
+    glLoadIdentity();

It's been a surprisingly long time since I last saw the OpenGL builtin matrix stack. :-)

+
+    glMatrixMode(GL_MODELVIEW);
+    glLoadIdentity();
+
+    glClearColor(0.0, 0.0, 0.0, 0);

The alpha value is a float, too. So I'd either write 0 for everything and let the compiler handle the implicit conversion or (better, in my opinion) use 0.f (or 0.0f). Which brings me to that I'd rather not use doubles when the function takes floats...

+    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

But you don't need glClearColor() at all, and you don't need this glClear() either. The depth test is disabled and the quad fills the whole screen, thus you can just leave the depth and color buffer as they are.

+
+    glGenTextures(1, &tex);
+    glBindTexture(GL_TEXTURE_2D, tex);
+    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, gw, gh,
+                 0, GL_BGRA_EXT,GL_UNSIGNED_BYTE,

I feared this extensions might not be widespread enough, but EXT_bgra is from 1997 so it should be fine. :-)

+                 surface_data(scon->surface));
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
+    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
+
+    glEnable(GL_TEXTURE_2D);

Shouldn't you call this before doing the first operation on that target? (that is, before the glBindTexture())

+    glBegin(GL_QUADS);
+    glTexCoord2f(0, 1);  glVertex3f(-1, -1, 0);
+    glTexCoord2f(0, 0);  glVertex3f(-1, 1, 0);
+    glTexCoord2f(1, 0);  glVertex3f(1, 1, 0);
+    glTexCoord2f(1, 1);  glVertex3f(1, -1, 0);
+    glEnd();

I've been trained to hate direct mode, but it should be fine for just this quad.

First, you may consider using glVertex2f().

Second, as hinted above, I don't like giving ints where floats are expected. So I'd like this to be "glTexCoord2f(0.f, 1.f)" etc., or (maybe even better because it prevents a discussion about whether to use 0.f or 0 :-)) just glTexCoord2i() and glVertex2i().

+
+    SDL_GL_SwapWindow(scon->real_window);
+
+    glDisable(GL_TEXTURE_2D);
+    glDeleteTextures(1, &tex);
+}

Also, it hurts to always enable textures, generate one, load it, disable textures and delete them just to render a single quad with a texture loaded from main memory... (not to mention glViewport(), the matrix operations and SDL_GL_MakeCurrent())

Would it be possible to just enable GL_TEXTURE_2D once, store the GL ID of the texture in the sdl2_console object and either use glTexImage2D() or, technically better, glTexSubImage2D() here?

Using glTexSubImage2D() would give us the advantage of being able to perform partial updates on the texture; but it seems to fit pretty bad into the existing code. To make it fit, I'd call glTexSubImage2D() directly in sdl2_gl_update() and just draw the quad here.

Also, I'd move glViewport() into some function which is called on resize (not sdl2_gl_redraw()), and the matrix operations into some initialization code. We probably cannot do a whole lot about SDL_GL_MakeCurrent(), though, which is too bad (depending on what it actually does, the SDL Wiki is not very clear about it...).

Max



reply via email to

[Prev in Thread] Current Thread [Next in Thread]