mirror of
https://git.savannah.gnu.org/git/grub.git
synced 2026-10-20 22:48:22 +00:00
emu: Add SDL2 support
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. This approaches the configuration by adding a new --enable-grub-emu-sdl2 argument. If set to yes, or auto detected, it disables SDL1 support automatically. This duplicates the sdl module block in Makefile.core.def which may be something to be aware of, but we also don't want to build separate module. Fixes: https://bugs.debian.org/1038035 Signed-off-by: Julian Andres Klode <julian.klode@canonical.com> Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
This commit is contained in:
1 parent
d131335d4e
commit
17d6ac1a7d
5 files changed
+151
-24
No files matched your search
+89
-21
@@ -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,22 @@
|
||||
#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 = NULL;
|
||||
static SDL_Texture *texture = NULL;
|
||||
static SDL_Renderer *renderer = NULL;
|
||||
#else
|
||||
static SDL_Surface *window = NULL;
|
||||
#endif
|
||||
static SDL_Surface *surface = NULL;
|
||||
static struct grub_video_render_target *sdl_render_target;
|
||||
static struct grub_video_mode_info mode_info;
|
||||
|
||||
@@ -72,7 +86,7 @@ get_mask_size (grub_uint32_t mask)
|
||||
|
||||
static grub_err_t
|
||||
grub_video_sdl_setup (unsigned int width, unsigned int height,
|
||||
unsigned int mode_type, unsigned int mode_mask)
|
||||
unsigned int mode_type, unsigned int mode_mask __attribute__ ((unused)))
|
||||
{
|
||||
int depth;
|
||||
int flags = 0;
|
||||
@@ -91,6 +105,35 @@ grub_video_sdl_setup (unsigned int width, unsigned int height,
|
||||
height = 600;
|
||||
}
|
||||
|
||||
#ifdef HAVE_SDL2
|
||||
window = SDL_CreateWindow ("grub-emu",
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
SDL_WINDOWPOS_UNDEFINED,
|
||||
width, height, flags);
|
||||
if(window == NULL)
|
||||
return grub_error (GRUB_ERR_BAD_DEVICE, "could not open window: %s",
|
||||
SDL_GetError ());
|
||||
renderer = SDL_CreateRenderer (window, -1, 0);
|
||||
if (renderer == NULL)
|
||||
return grub_error (GRUB_ERR_BAD_DEVICE, "could not open renderer: %s",
|
||||
SDL_GetError ());
|
||||
texture = SDL_CreateTexture (renderer,
|
||||
SDL_PIXELFORMAT_ARGB8888,
|
||||
SDL_TEXTUREACCESS_STREAMING,
|
||||
width, height);
|
||||
if (texture == NULL)
|
||||
return grub_error (GRUB_ERR_BAD_DEVICE, "could not 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 == NULL)
|
||||
return grub_error (GRUB_ERR_BAD_DEVICE, "could not 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, "could not 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, "could not update texture: %s",
|
||||
SDL_GetError ());
|
||||
if (SDL_RenderClear (renderer) < 0)
|
||||
return grub_error (GRUB_ERR_BAD_DEVICE, "could not clear renderer: %s",
|
||||
SDL_GetError ());
|
||||
if (SDL_RenderCopy (renderer, texture, NULL, NULL) < 0)
|
||||
return grub_error (GRUB_ERR_BAD_DEVICE, "could not copy texture to renderer: %s",
|
||||
SDL_GetError ());
|
||||
SDL_RenderPresent (renderer);
|
||||
#else
|
||||
if (SDL_Flip (window) < 0)
|
||||
return grub_error (GRUB_ERR_BAD_DEVICE, "could not swap buffers: %s",
|
||||
SDL_GetError ());
|
||||
#endif
|
||||
return GRUB_ERR_NONE;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user