On 2015-01-12 at 07:35, Gerd Hoffmann wrote:
Add new sdl2-gl.c file, with display
rendering functions using opengl.

Signed-off-by: Gerd Hoffmann <kra...@redhat.com>
---
  include/ui/console.h |   1 +
  include/ui/sdl2.h    |  10 +++++
  ui/Makefile.objs     |   4 ++
  ui/sdl.c             |  11 +++++
  ui/sdl2-2d.c         |   7 ++++
  ui/sdl2-gl.c         | 114 +++++++++++++++++++++++++++++++++++++++++++++++++++
  ui/sdl2.c            |  67 ++++++++++++++++++++++++++----
  vl.c                 |  11 +++++
  8 files changed, 218 insertions(+), 7 deletions(-)
  create mode 100644 ui/sdl2-gl.c

diff --git a/include/ui/console.h b/include/ui/console.h
index 5cb169c..7496dee 100644
--- a/include/ui/console.h
+++ b/include/ui/console.h
@@ -344,6 +344,7 @@ void surface_gl_setup_viewport(DisplaySurface *surface,
  #endif
/* sdl.c */
+void sdl_display_early_init(int opengl);
  void sdl_display_init(DisplayState *ds, int full_screen, int no_frame);
/* cocoa.m */
diff --git a/include/ui/sdl2.h b/include/ui/sdl2.h
index f56c596..5c962da 100644
--- a/include/ui/sdl2.h
+++ b/include/ui/sdl2.h
@@ -11,6 +11,9 @@ struct sdl2_console {
      int last_vm_running; /* per console for caption reasons */
      int x, y;
      int hidden;
+    int opengl;
+    int updates;
+    SDL_GLContext winctx;
  };
void sdl2_window_create(struct sdl2_console *scon);
@@ -29,4 +32,11 @@ void sdl2_2d_switch(DisplayChangeListener *dcl,
  void sdl2_2d_refresh(DisplayChangeListener *dcl);
  void sdl2_2d_redraw(struct sdl2_console *scon);
+void sdl2_gl_update(DisplayChangeListener *dcl,
+                    int x, int y, int w, int h);
+void sdl2_gl_switch(DisplayChangeListener *dcl,
+                    DisplaySurface *new_surface);
+void sdl2_gl_refresh(DisplayChangeListener *dcl);
+void sdl2_gl_redraw(struct sdl2_console *scon);
+
  #endif /* SDL2_H */
diff --git a/ui/Makefile.objs b/ui/Makefile.objs
index 3173778..c1f4299 100644
--- a/ui/Makefile.objs
+++ b/ui/Makefile.objs
@@ -21,6 +21,10 @@ sdl.mo-objs := sdl.o sdl_zoom.o
  endif
  ifeq ($(CONFIG_SDLABI),2.0)
  sdl.mo-objs := sdl2.o sdl2-input.o sdl2-2d.o
+ifeq ($(CONFIG_OPENGL),y)
+sdl.mo-objs += sdl2-gl.o
+libs_softmmu += $(OPENGL_LIBS)
+endif
  endif
  sdl.mo-cflags := $(SDL_CFLAGS)
diff --git a/ui/sdl.c b/ui/sdl.c
index 3e9d810..1fe002d 100644
--- a/ui/sdl.c
+++ b/ui/sdl.c
@@ -873,6 +873,17 @@ static const DisplayChangeListenerOps dcl_ops = {
      .dpy_cursor_define = sdl_mouse_define,
  };
+void sdl_display_early_init(int opengl)
+{
+    if (opengl == 1 /* on */) {
+        fprintf(stderr,
+                "SDL1 display code has no opengl support.\n"
+                "Please recompile qemu with SDL2, using\n"
+                "./configure --enable-sdl --with-sdlabi=2.0\n");
+        exit(1);
+    }
+}
+
  void sdl_display_init(DisplayState *ds, int full_screen, int no_frame)
  {
      int flags;
diff --git a/ui/sdl2-2d.c b/ui/sdl2-2d.c
index 9264817..85f1be4 100644
--- a/ui/sdl2-2d.c
+++ b/ui/sdl2-2d.c
@@ -42,6 +42,8 @@ void sdl2_2d_update(DisplayChangeListener *dcl,
      DisplaySurface *surf = qemu_console_surface(dcl->con);
      SDL_Rect rect;
+ assert(!scon->opengl);
+
      if (!surf) {
          return;
      }
@@ -67,6 +69,8 @@ void sdl2_2d_switch(DisplayChangeListener *dcl,
      DisplaySurface *old_surface = scon->surface;
      int format = 0;
+ assert(!scon->opengl);
+
      scon->surface = new_surface;
if (scon->texture) {
@@ -107,12 +111,15 @@ void sdl2_2d_refresh(DisplayChangeListener *dcl)
  {
      struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
+ assert(!scon->opengl);
      graphic_hw_update(dcl->con);
      sdl2_poll_events(scon);
  }
void sdl2_2d_redraw(struct sdl2_console *scon)
  {
+    assert(!scon->opengl);
+
      if (!scon->surface) {
          return;
      }
diff --git a/ui/sdl2-gl.c b/ui/sdl2-gl.c
new file mode 100644
index 0000000..b8620ca
--- /dev/null
+++ b/ui/sdl2-gl.c
@@ -0,0 +1,114 @@
+/*
+ * QEMU SDL display driver
+ *
+ * Copyright (c) 2003 Fabrice Bellard

Just as mentioned by Paolo on patch 2, this may be supposed to mention you instead.

+ *
+ * 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. */

And this line seems wrong.

+
+/* 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 ww, wh;
+
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
+
+    SDL_GetWindowSize(scon->real_window, &ww, &wh);
+    surface_gl_setup_viewport(scon->surface, ww, wh);
+
+    surface_gl_render_texture(scon->surface);
+    SDL_GL_SwapWindow(scon->real_window);
+}
+
+void sdl2_gl_update(DisplayChangeListener *dcl,
+                    int x, int y, int w, int h)
+{
+    struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
+
+    assert(scon->opengl);
+
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
+    surface_gl_update_texture(scon->surface, x, y, w, h);
+    scon->updates++;
+}
+
+void sdl2_gl_switch(DisplayChangeListener *dcl,
+                    DisplaySurface *new_surface)
+{
+    struct sdl2_console *scon = container_of(dcl, struct sdl2_console, dcl);
+    DisplaySurface *old_surface = scon->surface;
+
+    assert(scon->opengl);
+
+    SDL_GL_MakeCurrent(scon->real_window, scon->winctx);
+    surface_gl_destroy_texture(scon->surface);

Can a surface be in use by multiple DCLs? If so, this would probably need reference counting.

+
+    scon->surface = new_surface;
+
+    if (!new_surface) {
+        sdl2_window_destroy(scon);
+        return;
+    }
+
+    if (!scon->real_window) {
+        sdl2_window_create(scon);
+    } else if (old_surface &&
+               ((surface_width(old_surface)  != surface_width(new_surface)) ||
+                (surface_height(old_surface) != surface_height(new_surface)))) 
{
+        sdl2_window_resize(scon);

Hm, now that I think about it... If the window is scaled, this will reset the scaling to 100 %. Fine, why not. However, if the new surface has the same dimensions as the old surface, the window will not be scaled. That would seem strange to me.

Apart from this and the question about surfaces used by multiple DCLs at the same time, the patch looks good to me (the option parsing in select_display() in vl.c looks like nightmare fuel, but it's just in line with its environment).

Max

Reply via email to