So all we did with the surface in SDL1 was split into window,
surface, renderer, and texture. Instead of drawing into the
surface and then flipping, you build your pixels, then update
a texture, and then copy the texture to the renderer.

Here we use an empty RGB surface to hold our Pixels, which enables
us to keep most of the code the same. The SDL1 code has been adjusted
to refer to `surface` instead of `window` when trying to access the
properties of the surface.

The configure script is changed to prefer SDL2 over SDL1 if available,
the detection of SDL2 is done using the pkg-config file. This does
not allow explicit choosing of SDL2 vs SDL1, but seems like it will
be less confusing as time goes on and you get SDL3 and so on.

Bug-Debian: https://bugs.debian.org/1038035
Signed-off-by: Julian Andres Klode <julian.kl...@canonical.com>
---
 configure.ac                |  22 +++++---
 grub-core/Makefile.core.def |   5 +-
 grub-core/video/emu/sdl.c   | 108 +++++++++++++++++++++++++++++-------
 include/grub/sdl.h          |  16 +++++-
 4 files changed, 119 insertions(+), 32 deletions(-)

diff --git a/configure.ac b/configure.ac
index d9f088d12..ed5673534 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1578,15 +1578,19 @@ if test "$platform" = emu; then
   fi
   [if [ x"$grub_emu_sdl_excuse" = x ]; then
     # Check for libSDL libraries.]
-AC_CHECK_LIB([SDL], [SDL_Init], [LIBSDL="-lSDL"],
-    [grub_emu_sdl_excuse=["libSDL libraries are required to build \`grub-emu' 
with SDL support"]])
-    AC_SUBST([LIBSDL])
-  [fi]
-
-  [if [ x"$grub_emu_sdl_excuse" = x ]; then
-    # Check for headers.]
-    AC_CHECK_HEADERS([SDL/SDL.h], [],
-      [grub_emu_sdl_excuse=["libSDL header file is required to build 
\`grub-emu' with SDL support"]])
+  PKG_CHECK_MODULES([SDL2], [sdl2], [
+            AC_DEFINE([HAVE_SDL2], [1], [Define to 1 if you have SDL2 
library.])
+            AC_SUBST(HAVE_SDL2)], [
+    AC_CHECK_LIB([SDL], [SDL_Init], [LIBSDL="-lSDL"],
+        [grub_emu_sdl_excuse=["libSDL libraries are required to build 
\`grub-emu' with SDL support"]])
+        AC_SUBST([LIBSDL])
+
+      [if [ x"$grub_emu_sdl_excuse" = x ]; then
+        # Check for headers.]
+        AC_CHECK_HEADERS([SDL/SDL.h], [],
+          [grub_emu_sdl_excuse=["libSDL header file is required to build 
\`grub-emu' with SDL support"]])
+      [fi]
+  ])
   [fi]
 
   if test x"enable_grub_emu_sdl" = xyes && test x"$grub_emu_sdl_excuse" != x ; 
then
diff --git a/grub-core/Makefile.core.def b/grub-core/Makefile.core.def
index e458aa665..100829853 100644
--- a/grub-core/Makefile.core.def
+++ b/grub-core/Makefile.core.def
@@ -418,7 +418,7 @@ program = {
 
   ldadd = 'kernel.exec$(EXEEXT)';
   ldadd = '$(MODULE_FILES)';
-  ldadd = 'lib/gnulib/libgnu.a $(LIBINTL) $(LIBUTIL) $(LIBSDL) $(LIBUSB) 
$(LIBPCIACCESS) $(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
+  ldadd = 'lib/gnulib/libgnu.a $(LIBINTL) $(LIBUTIL) $(LIBSDL) $(SDL2_LIBS) 
$(LIBUSB) $(LIBPCIACCESS) $(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
 
   enable = emu;
 };
@@ -430,7 +430,7 @@ program = {
   emu_nodist = symlist.c;
 
   ldadd = 'kernel.exec$(EXEEXT)';
-  ldadd = 'lib/gnulib/libgnu.a $(LIBINTL) $(LIBUTIL) $(LIBSDL) $(LIBUSB) 
$(LIBPCIACCESS) $(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
+  ldadd = 'lib/gnulib/libgnu.a $(LIBINTL) $(LIBUTIL) $(LIBSDL) $(SDL2_LIBS) 
$(LIBUSB) $(LIBPCIACCESS) $(LIBDEVMAPPER) $(LIBZFS) $(LIBNVPAIR) $(LIBGEOM)';
 
   enable = emu;
 };
@@ -2323,6 +2323,7 @@ module = {
   emu = video/emu/sdl.c;
   enable = emu;
   condition = COND_GRUB_EMU_SDL;
+  cflags = '$(SDL2_CFLAGS)';
 };
 
 module = {
diff --git a/grub-core/video/emu/sdl.c b/grub-core/video/emu/sdl.c
index 0ebab6f57..93fb83da0 100644
--- a/grub-core/video/emu/sdl.c
+++ b/grub-core/video/emu/sdl.c
@@ -18,6 +18,9 @@
 
 #define grub_video_render_target grub_video_fbrender_target
 
+#include <config-util.h>
+#include <config.h>
+
 #include <grub/err.h>
 #include <grub/types.h>
 #include <grub/dl.h>
@@ -25,11 +28,23 @@
 #include <grub/mm.h>
 #include <grub/video.h>
 #include <grub/video_fb.h>
+#ifdef HAVE_SDL2
+#include <SDL2/SDL.h>
+#else
 #include <SDL/SDL.h>
+#endif
 
 GRUB_MOD_LICENSE ("GPLv3+");
 
+#ifdef HAVE_SDL2
+static SDL_Window *window = 0;
+static SDL_Surface *surface = 0;
+static SDL_Texture *texture = 0;
+static SDL_Renderer *renderer = 0;
+#else
 static SDL_Surface *window = 0;
+static SDL_Surface *surface = 0;
+#endif
 static struct grub_video_render_target *sdl_render_target;
 static struct grub_video_mode_info mode_info;
 
@@ -91,6 +106,34 @@ grub_video_sdl_setup (unsigned int width, unsigned int 
height,
       height = 600;
     }
 
+#ifdef HAVE_SDL2
+  (void) mode_mask; // We can't specify this in SDL2
+  window = SDL_CreateWindow ("grub-emu",
+                            SDL_WINDOWPOS_UNDEFINED,
+                            SDL_WINDOWPOS_UNDEFINED,
+                            width, height, flags);
+  if (! window)
+    return grub_error (GRUB_ERR_BAD_DEVICE, "Couldn't open window: %s",
+                      SDL_GetError ());
+  renderer = SDL_CreateRenderer (window, -1, 0);
+  if (! renderer)
+    return grub_error (GRUB_ERR_BAD_DEVICE, "Couldn't open renderer: %s",
+                      SDL_GetError ());
+  texture = SDL_CreateTexture (renderer,
+                              SDL_PIXELFORMAT_ARGB8888,
+                              SDL_TEXTUREACCESS_STREAMING,
+                              width, height);
+  if (! texture)
+    return grub_error (GRUB_ERR_BAD_DEVICE, "Couldn't create texture: %s",
+                      SDL_GetError ());
+
+  // An empty surface that acts as the pixel buffer, the texture will receive 
the pixels
+  // from here.
+  surface = SDL_CreateRGBSurface (0, width, height, depth, 0, 0, 0, 0);
+  if (! surface)
+    return grub_error (GRUB_ERR_BAD_DEVICE, "Couldn't open surface: %s",
+                      SDL_GetError ());
+#else
   if ((mode_type & GRUB_VIDEO_MODE_TYPE_DOUBLE_BUFFERED)
       || !(mode_mask & GRUB_VIDEO_MODE_TYPE_DOUBLE_BUFFERED))
     flags |= SDL_DOUBLEBUF;
@@ -102,51 +145,55 @@ grub_video_sdl_setup (unsigned int width, unsigned int 
height,
     return grub_error (GRUB_ERR_BAD_DEVICE, "Couldn't open window: %s",
                       SDL_GetError ());
 
+  surface = window;
+#endif
   grub_memset (&sdl_render_target, 0, sizeof (sdl_render_target));
 
-  mode_info.width = window->w;
-  mode_info.height = window->h;
   mode_info.mode_type = 0;
-  if (window->flags & SDL_DOUBLEBUF)
+  mode_info.width = surface->w;
+  mode_info.height = surface->h;
+#ifndef HAVE_SDL2
+  if (surface->flags & SDL_DOUBLEBUF)
     mode_info.mode_type
       |= GRUB_VIDEO_MODE_TYPE_DOUBLE_BUFFERED;
-  if (window->format->palette)
+#endif
+  if (surface->format->palette)
     mode_info.mode_type |= GRUB_VIDEO_MODE_TYPE_INDEX_COLOR;
   else
     mode_info.mode_type |= GRUB_VIDEO_MODE_TYPE_RGB;
 
-  mode_info.bpp = window->format->BitsPerPixel;
-  mode_info.bytes_per_pixel = window->format->BytesPerPixel;
-  mode_info.pitch = window->pitch;
+  mode_info.bpp = surface->format->BitsPerPixel;
+  mode_info.bytes_per_pixel = surface->format->BytesPerPixel;
+  mode_info.pitch = surface->pitch;
 
   /* In index color mode, number of colors.  In RGB mode this is 256.  */
-  if (window->format->palette)
+  if (surface->format->palette)
     mode_info.number_of_colors
-      = 1 << window->format->BitsPerPixel;
+      = 1 << surface->format->BitsPerPixel;
   else
     mode_info.number_of_colors = 256;
 
-  if (! window->format->palette)
+  if (! surface->format->palette)
     {
       mode_info.red_mask_size
-       = get_mask_size (window->format->Rmask >> window->format->Rshift);
-      mode_info.red_field_pos = window->format->Rshift;
+       = get_mask_size (surface->format->Rmask >> surface->format->Rshift);
+      mode_info.red_field_pos = surface->format->Rshift;
       mode_info.green_mask_size
-       = get_mask_size (window->format->Gmask >> window->format->Gshift);
-      mode_info.green_field_pos = window->format->Gshift;
+       = get_mask_size (surface->format->Gmask >> surface->format->Gshift);
+      mode_info.green_field_pos = surface->format->Gshift;
       mode_info.blue_mask_size
-       = get_mask_size (window->format->Bmask >> window->format->Bshift);
-      mode_info.blue_field_pos = window->format->Bshift;
+       = get_mask_size (surface->format->Bmask >> surface->format->Bshift);
+      mode_info.blue_field_pos = surface->format->Bshift;
       mode_info.reserved_mask_size
-       = get_mask_size (window->format->Amask >> window->format->Ashift);
-      mode_info.reserved_field_pos = window->format->Ashift;
+       = get_mask_size (surface->format->Amask >> surface->format->Ashift);
+      mode_info.reserved_field_pos = surface->format->Ashift;
       mode_info.blit_format
        = grub_video_get_blit_format (&mode_info);
     }
 
   err = grub_video_fb_create_render_target_from_pointer (&sdl_render_target,
                                                         &mode_info,
-                                                        window->pixels);
+                                                        surface->pixels);
   if (err)
     return err;
 
@@ -163,7 +210,7 @@ grub_video_sdl_set_palette (unsigned int start, unsigned 
int count,
                             struct grub_video_palette_data *palette_data)
 {
   unsigned i;
-  if (window->format->palette)
+  if (surface->format->palette)
     {
       SDL_Color *tmp;
       if (start >= mode_info.number_of_colors)
@@ -178,9 +225,17 @@ grub_video_sdl_set_palette (unsigned int start, unsigned 
int count,
          tmp[i].r = palette_data[i].r;
          tmp[i].g = palette_data[i].g;
          tmp[i].b = palette_data[i].b;
+#ifdef HAVE_SDL2
+         tmp[i].a = palette_data[i].a;
+#else
          tmp[i].unused = palette_data[i].a;
+#endif
        }
+#ifdef HAVE_SDL2
+      SDL_SetPaletteColors (surface->format->palette, tmp, 0 /* firstcolor */, 
count);
+#else
       SDL_SetColors (window, tmp, start, count);
+#endif
       grub_free (tmp);
     }
 
@@ -190,9 +245,22 @@ grub_video_sdl_set_palette (unsigned int start, unsigned 
int count,
 static grub_err_t
 grub_video_sdl_swap_buffers (void)
 {
+#ifdef HAVE_SDL2
+  if (SDL_UpdateTexture (texture, NULL, surface->pixels, surface->w * sizeof 
(Uint32)) < 0)
+    return grub_error (GRUB_ERR_BAD_DEVICE, "couldn't update texture: %s",
+                      SDL_GetError ());
+  if (SDL_RenderClear (renderer) < 0)
+    return grub_error (GRUB_ERR_BAD_DEVICE, "couldn't clear renderer: %s",
+                      SDL_GetError ());
+  if (SDL_RenderCopy (renderer, texture, NULL, NULL) < 0)
+    return grub_error (GRUB_ERR_BAD_DEVICE, "couldn't copy texture to 
renderer: %s",
+                      SDL_GetError ());
+  SDL_RenderPresent (renderer);
+#else
   if (SDL_Flip (window) < 0)
     return grub_error (GRUB_ERR_BAD_DEVICE, "couldn't swap buffers: %s",
                       SDL_GetError ());
+#endif
   return GRUB_ERR_NONE;
 }
 
diff --git a/include/grub/sdl.h b/include/grub/sdl.h
index e4efdc9b1..8f10b8817 100644
--- a/include/grub/sdl.h
+++ b/include/grub/sdl.h
@@ -15,10 +15,24 @@
  *  You should have received a copy of the GNU General Public License
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
  */
+#include <config-util.h>
 
 void EXPORT_FUNC (SDL_Quit) (void);
-void EXPORT_FUNC (SDL_SetColors) (void);
 void EXPORT_FUNC (SDL_Init) (void);
 void EXPORT_FUNC (SDL_GetError) (void);
+#ifdef HAVE_SDL2
+void EXPORT_FUNC (SDL_CreateWindow) (void);
+void EXPORT_FUNC (SDL_GetWindowSurface) (void);
+void EXPORT_FUNC (SDL_CreateRenderer) (void);
+void EXPORT_FUNC (SDL_CreateRGBSurface) (void);
+void EXPORT_FUNC (SDL_CreateTexture) (void);
+void EXPORT_FUNC (SDL_UpdateTexture) (void);
+void EXPORT_FUNC (SDL_SetPaletteColors) (void);
+void EXPORT_FUNC (SDL_RenderClear) (void);
+void EXPORT_FUNC (SDL_RenderCopy) (void);
+void EXPORT_FUNC (SDL_RenderPresent) (void);
+#else
 void EXPORT_FUNC (SDL_Flip) (void);
+void EXPORT_FUNC (SDL_SetColors) (void);
 void EXPORT_FUNC (SDL_SetVideoMode) (void);
+#endif
-- 
2.40.1


_______________________________________________
Grub-devel mailing list
Grub-devel@gnu.org
https://lists.gnu.org/mailman/listinfo/grub-devel

Reply via email to