Skip to contentSkip to conversation

sdl zooming

PATCH In review Stefano Stabellini 4 participants 8 messages Jun 24, 2009 – Jul 2, 2009 Kbuild

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 sdl zooming

Hi all,
this patch implements zooming capabilities for the sdl interface. A new sdl_zoom_blit function is added that is able to scale and blit a portion of a surface into another.
This way we can enable SDL_RESIZABLE and have a real_screen surface with a different size than the guest surface and let sdl_zoom_blit take care of the problem.

---

Makefile +4 -2
index 781e7ed..58faff9 100644
@@ -162,7 +162,7 @@ OBJS+=$(addprefix audio/, $(AUDIO_OBJS))
 
 OBJS+=keymaps.o
 ifdef CONFIG_SDL
-OBJS+=sdl.o x_keymap.o
+OBJS+=sdl.o sdl_zoom.o x_keymap.o
 endif
 ifdef CONFIG_CURSES
 OBJS+=curses.o
@@ -206,7 +206,9 @@ cocoa.o: cocoa.m
 
 keymaps.o: keymaps.c keymaps.h
 
-sdl.o: sdl.c keymaps.h sdl_keysym.h
+sdl_zoom.o: sdl_zoom.c sdl_zoom.h sdl_zoom_template.h
+
+sdl.o: sdl.c keymaps.h sdl_keysym.h sdl_zoom.h
 
 sdl.o audio/sdlaudio.o: CFLAGS += $(SDL_CFLAGS)
 
console.h +3 -1
index 4172175..3518339 100644
@@ -75,6 +75,7 @@ void kbd_put_keysym(int keysym);
 
 #define QEMU_BIG_ENDIAN_FLAG    0x01
 #define QEMU_ALLOCATED_FLAG     0x02
+#define QEMU_REALPIXELS_FLAG    0x04
 
 struct PixelFormat {
     uint8_t bits_per_pixel;
@@ -172,7 +173,8 @@ static inline int is_surface_bgr(DisplaySurface *surface)
 
 static inline int is_buffer_shared(DisplaySurface *surface)
 {
-    return (!(surface->flags & QEMU_ALLOCATED_FLAG));
+    return (!(surface->flags & QEMU_ALLOCATED_FLAG) &&
+            !(surface->flags & QEMU_REALPIXELS_FLAG));
 }
 
 static inline void register_displaychangelistener(DisplayState *ds, DisplayChangeListener *dcl)
sdl.c +62 -16

146 lines not on this page · raw message

sdl_zoom.c new file +94 -0

94 lines not on this page · raw message

sdl_zoom.h new file +25 -0
new file mode 100644
index 0000000..33dc634
@@ -0,0 +1,25 @@
+/*
+ * SDL_zoom - surface scaling
+ * 
+ * Copyright (c) 2009 Citrix Systems, Inc.
+ *
+ * Derived from: SDL_rotozoom,  LGPL (c) A. Schiffler from the SDL_gfx library.
+ * Modifications by Stefano Stabellini.
+ *
+ * This work is licensed under the terms of the GNU GPL version 2.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef _SDL_zoom_h
+#define _SDL_zoom_h
+
+#include <SDL/SDL.h>
+
+#define SMOOTHING_OFF		0
+#define SMOOTHING_ON		1
+
+int sdl_zoom_blit(SDL_Surface *src_sfc, SDL_Surface *dst_sfc,
+                  int smooth, SDL_Rect *src_rect);
+
+#endif /* _SDL_zoom_h */
sdl_zoom_template.h new file +225 -0

225 lines not on this page · raw message

#1 lore raw
Quoted 10 lines · Stefano Stabellini

Hi all,
this patch implements zooming capabilities for the sdl interface. A new sdl_zoom_blit function is added that is able to scale and blit a portion of a surface into another.
This way we can enable SDL_RESIZABLE and have a real_screen surface with a different size than the guest surface and let sdl_zoom_blit take care of the problem.

Hi Stefano,

First, really neat patch. I was looking at this recently myself. I cannot tell quickly from looking at the code--perhaps not enough caffeine yet this morning. Is this nearest neighbor with some sort of smoothing mechanism on top?

In my experiences with scaling VM displays, bilinear interpolation seems to fit the use case here well as it does best from 1.0-2.0x which is more or less what you'll commonly see.

Bilinear interpolation can be implemented rather effectively on most processors and is usually hardware supported too. That suggests that using a library is the right approach to avoid maintaining platform-specific code.

Cairo seems to be a good choice. It integrates well with SDL and is portable to all of the platforms we care about. It's a very simple API too for scaling. We use it in gtk-vnc and it has proven to be very good for this task.

What do you think?

http://cairographics.org/SDL/

Regards,

Anthony Liguori

#2 lore raw
Quoted 3 lines · Anthony Liguori

Hi Stefano,

First, really neat patch.

Thank you, this time I spent a considerable amount of time in cleaning the patch up and testing it so that it would be easier to merge.

Quoted 4 lines

I was looking at this recently myself. I
cannot tell quickly from looking at the code--perhaps not enough caffeine yet this morning. Is this nearest neighbor with some sort of smoothing mechanism on top?

The smooth==0 is a nearest neighbor algorithm, while the smooth==1 is a bilinear interpolation.

Quoted 10 lines

In my experiences with scaling VM displays, bilinear interpolation seems to fit the use case here well as it does best from 1.0-2.0x which is more or less what you'll commonly see.

Bilinear interpolation can be implemented rather effectively on most processors and is usually hardware supported too. That suggests that using a library is the right approach to avoid maintaining platform-specific code.

Quoted 6 lines

Cairo seems to be a good choice. It integrates well with SDL and is portable to all of the platforms we care about. It's a very simple API too for scaling. We use it in gtk-vnc and it has proven to be very good for this task.

What do you think?

I think cairo is a good choice for a new qemu gtk-cairo frontend, but I don't think that mixing cairo and sdl is a good idea.
Among other things they can have pixel format issues, and it seems to me that cairo is only able to use 32bpp SDL_Surface.
If you really want a library than SDL_gfx should be used instead, but I think is overkill.

#3 lore raw
In reply to Stefano Stabellini
Quoted 7 lines · Stefano Stabellini

Hi all,
this patch implements zooming capabilities for the sdl interface. A new sdl_zoom_blit function is added that is able to scale and blit a portion of a surface into another.
This way we can enable SDL_RESIZABLE and have a real_screen surface with a different size than the guest surface and let sdl_zoom_blit take care of the problem.

As you are already using the template system, for extra performance, you could remove 'smooth' as parameter and instantiate separate functions for both cases with something like:

static int glue(glue(sdl_zoom_rgb, BPP), _ ## SMOOTH)(SDL_Surface *src, SDL_Surface *dst,
SDL_Rect *dst_rect)

Then s/smooth/SMOOTH/g in the function and adjust callers.

#4 lore raw
Quoted 13 lines · Blue Swirl
Quoted 7 lines · Stefano Stabellini

Hi all,
this patch implements zooming capabilities for the sdl interface. A new sdl_zoom_blit function is added that is able to scale and blit a portion of a surface into another.
This way we can enable SDL_RESIZABLE and have a real_screen surface with a different size than the guest surface and let sdl_zoom_blit take care of the problem.

As you are already using the template system, for extra performance, you could remove 'smooth' as parameter and instantiate separate functions for both cases with something like:

Quoted 5 lines

static int glue(glue(sdl_zoom_rgb, BPP), _ ## SMOOTH)(SDL_Surface *src, SDL_Surface *dst,
SDL_Rect *dst_rect)

Then s/smooth/SMOOTH/g in the function and adjust callers.

I am not sure if this change is going to increase the readability of the code or the performances by much, but this is the new version of the patch nonetheless.

At this point I would consider making SMOOTH a compile time only variable.

---

Makefile +4 -2
index 781e7ed..58faff9 100644
@@ -162,7 +162,7 @@ OBJS+=$(addprefix audio/, $(AUDIO_OBJS))
 
 OBJS+=keymaps.o
 ifdef CONFIG_SDL
-OBJS+=sdl.o x_keymap.o
+OBJS+=sdl.o sdl_zoom.o x_keymap.o
 endif
 ifdef CONFIG_CURSES
 OBJS+=curses.o
@@ -206,7 +206,9 @@ cocoa.o: cocoa.m
 
 keymaps.o: keymaps.c keymaps.h
 
-sdl.o: sdl.c keymaps.h sdl_keysym.h
+sdl_zoom.o: sdl_zoom.c sdl_zoom.h sdl_zoom_template.h
+
+sdl.o: sdl.c keymaps.h sdl_keysym.h sdl_zoom.h
 
 sdl.o audio/sdlaudio.o: CFLAGS += $(SDL_CFLAGS)
 
console.h +3 -1
index 4172175..3518339 100644
@@ -75,6 +75,7 @@ void kbd_put_keysym(int keysym);
 
 #define QEMU_BIG_ENDIAN_FLAG    0x01
 #define QEMU_ALLOCATED_FLAG     0x02
+#define QEMU_REALPIXELS_FLAG    0x04
 
 struct PixelFormat {
     uint8_t bits_per_pixel;
@@ -172,7 +173,8 @@ static inline int is_surface_bgr(DisplaySurface *surface)
 
 static inline int is_buffer_shared(DisplaySurface *surface)
 {
-    return (!(surface->flags & QEMU_ALLOCATED_FLAG));
+    return (!(surface->flags & QEMU_ALLOCATED_FLAG) &&
+            !(surface->flags & QEMU_REALPIXELS_FLAG));
 }
 
 static inline void register_displaychangelistener(DisplayState *ds, DisplayChangeListener *dcl)
sdl.c +62 -16

146 lines not on this page · raw message

sdl_zoom.c new file +110 -0

110 lines not on this page · raw message

sdl_zoom.h new file +25 -0
new file mode 100644
index 0000000..33dc634
@@ -0,0 +1,25 @@
+/*
+ * SDL_zoom - surface scaling
+ * 
+ * Copyright (c) 2009 Citrix Systems, Inc.
+ *
+ * Derived from: SDL_rotozoom,  LGPL (c) A. Schiffler from the SDL_gfx library.
+ * Modifications by Stefano Stabellini.
+ *
+ * This work is licensed under the terms of the GNU GPL version 2.
+ * See the COPYING file in the top-level directory.
+ *
+ */
+
+#ifndef _SDL_zoom_h
+#define _SDL_zoom_h
+
+#include <SDL/SDL.h>
+
+#define SMOOTHING_OFF		0
+#define SMOOTHING_ON		1
+
+int sdl_zoom_blit(SDL_Surface *src_sfc, SDL_Surface *dst_sfc,
+                  int smooth, SDL_Rect *src_rect);
+
+#endif /* _SDL_zoom_h */
sdl_zoom_template.h new file +233 -0

233 lines not on this page · raw message

#5 lore raw
Quoted 19 lines · Stefano Stabellini
Quoted 17 lines · Blue Swirl
Quoted 7 lines · Stefano Stabellini

Hi all,
this patch implements zooming capabilities for the sdl interface. A new sdl_zoom_blit function is added that is able to scale and blit a portion of a surface into another.
This way we can enable SDL_RESIZABLE and have a real_screen surface with a different size than the guest surface and let sdl_zoom_blit take care of the problem.

As you are already using the template system, for extra performance, you could remove 'smooth' as parameter and instantiate separate functions for both cases with something like:

static int glue(glue(sdl_zoom_rgb, BPP), _ ## SMOOTH)(SDL_Surface *src, SDL_Surface *dst,
SDL_Rect *dst_rect)

Quoted 8 lines
Quoted 1 line

Then s/smooth/SMOOTH/g in the function and adjust callers.

I am not sure if this change is going to increase the readability of the code or the performances by much, but this is the new version of the patch nonetheless.

Any update on this?

#6 lore raw
Quoted 24 lines · Stefano Stabellini
Quoted 23 lines · Stefano Stabellini
Quoted 18 lines · Blue Swirl
Quoted 7 lines · Stefano Stabellini

Hi all,
 this patch implements zooming capabilities for the sdl interface. A new sdl_zoom_blit function is added that is able to scale and blit a portion of a surface into another.
 This way we can enable SDL_RESIZABLE and have a real_screen surface with a different size than the guest surface and let sdl_zoom_blit take care of the problem.

As you are already using the template system, for extra performance, you could remove 'smooth' as parameter and instantiate separate functions for both cases with something like:

static int glue(glue(sdl_zoom_rgb, BPP), _ ## SMOOTH)(SDL_Surface *src, SDL_Surface *dst,
                                   SDL_Rect *dst_rect)

Then s/smooth/SMOOTH/g in the function and adjust callers.

Quoted 6 lines
Quoted 4 lines

I am not sure if this change is going to increase the readability of the code or the performances by much, but this is the new version of the patch nonetheless.

Any update on this?

It was commited to git...

F.

#7 lore raw
Quoted 5 lines · Filip Navara
Quoted 2 lines

Any update on this?

It was commited to git...

Oops, I really should fix the url of the git repo I am pulling from :) Thanks,

Stefano