[Xfce4-commits] <ristretto:master> Set the monitor_chooser monitor background as cairo_surface instead of gdkpixbuf.

Stephan Arts noreply at xfce.org
Mon Nov 28 19:44:02 CET 2011


Updating branch refs/heads/master
         to 4696e17dd388c7bce158ba5408a408cf1c45a128 (commit)
       from f6f45c1e4abf8db82e5bb09a794152366aa05887 (commit)

commit 4696e17dd388c7bce158ba5408a408cf1c45a128
Author: Stephan Arts <stephan at xfce.org>
Date:   Sat Nov 26 06:31:34 2011 +0100

    Set the monitor_chooser monitor background as cairo_surface instead of gdkpixbuf.

 src/gnome_wallpaper_manager.c |  130 +++++++++++++++++++----------------------
 src/monitor_chooser.c         |   34 ++++-------
 src/monitor_chooser.h         |    8 +-
 src/xfce_wallpaper_manager.c  |  130 ++++++++++++++++++-----------------------
 4 files changed, 133 insertions(+), 169 deletions(-)

diff --git a/src/gnome_wallpaper_manager.c b/src/gnome_wallpaper_manager.c
index 2db8e01..68fc47f 100644
--- a/src/gnome_wallpaper_manager.c
+++ b/src/gnome_wallpaper_manager.c
@@ -405,7 +405,7 @@ cb_monitor_chooser_changed (
         RsttoMonitorChooser *monitor_chooser,
         RsttoGnomeWallpaperManager *manager)
 {
-    rstto_monitor_chooser_set_pixbuf (
+    rstto_monitor_chooser_set_image_surface (
             monitor_chooser,
             manager->priv->monitor,
             NULL,
@@ -420,13 +420,17 @@ static void
 configure_monitor_chooser_pixbuf (
     RsttoGnomeWallpaperManager *manager )
 {
-    GdkPixbuf *monitor_pixbuf = NULL;
+    cairo_surface_t *image_surface = NULL;
+    cairo_t *ctx;
+    //GdkColor bg_color;
+
     GdkPixbuf *tmp_pixbuf = NULL;
+
     gint monitor_width = 0;
     gint monitor_height = 0;
 
-    gint pixbuf_width = 0;
-    gint pixbuf_height = 0;
+    gint surface_width = 0;
+    gint surface_height = 0;
     gint dest_x = 0;
     gint dest_y = 0;
     gint dest_width = 0;
@@ -439,117 +443,102 @@ configure_monitor_chooser_pixbuf (
         tmp_pixbuf = gdk_pixbuf_copy (manager->priv->pixbuf);
         if ( NULL != tmp_pixbuf )
         {
+
             rstto_monitor_chooser_get_dimensions (
                     RSTTO_MONITOR_CHOOSER (manager->priv->monitor_chooser),
                     manager->priv->monitor,
                     &monitor_width,
                     &monitor_height);
 
-            pixbuf_width = monitor_width * 0.2;
-            pixbuf_height = monitor_height * 0.2;
+            surface_width = monitor_width * 0.2;
+            surface_height = monitor_height * 0.2;
+
+            image_surface = cairo_image_surface_create (
+                    CAIRO_FORMAT_ARGB32,
+                    surface_width,
+                    surface_height);
+            ctx = cairo_create ( image_surface );
 
-            monitor_pixbuf = gdk_pixbuf_new (
-                    GDK_COLORSPACE_RGB,     /* Colorspace       */
-                    FALSE,                  /* has-alpha        */
-                    8,                      /* bits per sample  */
-                    pixbuf_width,           /* width            */
-                    pixbuf_height);         /* height           */
+            //gdk_cairo_set_source_color ( ctx, bg_color );
+            cairo_set_source_rgb (ctx, 0, 0, 0);
+            cairo_paint (ctx);
+
+            x_scale = (gdouble)surface_width / (gdouble)gdk_pixbuf_get_width (tmp_pixbuf);
+            y_scale = (gdouble)surface_height / (gdouble)gdk_pixbuf_get_height (tmp_pixbuf);
 
             switch (manager->priv->style)
             {
                 case MONITOR_STYLE_ZOOMED:
-                    dest_x = 0;
-                    dest_y = 0;
-                    dest_width = pixbuf_width;
-                    dest_height = pixbuf_height;
-                    x_scale = (gdouble)dest_width / (gdouble)gdk_pixbuf_get_width (tmp_pixbuf);
-                    y_scale = (gdouble)dest_height / (gdouble)gdk_pixbuf_get_height (tmp_pixbuf);
                     if (x_scale > y_scale)
                     {
+                        dest_width = (gint)((gdouble)gdk_pixbuf_get_width (tmp_pixbuf) * x_scale);
+                        dest_height = (gint)((gdouble)gdk_pixbuf_get_height (tmp_pixbuf) * x_scale);
+                        dest_x = (gint)((gdouble)(surface_width - dest_width) / 2);
+                        dest_y = (gint)((gdouble)(surface_height - dest_height) / 2);
                         y_scale = x_scale;
                     }
                     else
                     {
+                        dest_width = (gint)((gdouble)gdk_pixbuf_get_width (tmp_pixbuf) * y_scale);
+                        dest_height = (gint)((gdouble)gdk_pixbuf_get_height (tmp_pixbuf) * y_scale);
+                        dest_x = (gint)((gdouble)(surface_width - dest_width) / 2);
+                        dest_y = (gint)((gdouble)(surface_height - dest_height) / 2);
                         x_scale = y_scale;
                     }
                     break;
+                case MONITOR_STYLE_STRETCHED:
+                    dest_x = 0.0;
+                    dest_y = 0.0;
+                    break;
                 case MONITOR_STYLE_AUTOMATIC:
                 case MONITOR_STYLE_SCALED:
-                    x_scale = (gdouble)pixbuf_width / (gdouble)gdk_pixbuf_get_width (tmp_pixbuf);
-                    y_scale = (gdouble)pixbuf_height / (gdouble)gdk_pixbuf_get_height (tmp_pixbuf);
-                    if (x_scale > y_scale)
+                    if (x_scale < y_scale)
                     {
-                        x_scale = y_scale;
+                        dest_width = (gint)((gdouble)gdk_pixbuf_get_width (tmp_pixbuf) * x_scale);
+                        dest_height = (gint)((gdouble)gdk_pixbuf_get_height (tmp_pixbuf) * x_scale);
+                        dest_x = (gint)((gdouble)(surface_width - dest_width) / 2);
+                        dest_y = (gint)((gdouble)(surface_height - dest_height) / 2);
+                        y_scale = x_scale;
                     }
                     else
                     {
-                        y_scale = x_scale;
+                        dest_width = (gint)((gdouble)gdk_pixbuf_get_width (tmp_pixbuf) * y_scale);
+                        dest_height = (gint)((gdouble)gdk_pixbuf_get_height (tmp_pixbuf) * y_scale);
+                        dest_x = (gint)((gdouble)(surface_width - dest_width) / 2);
+                        dest_y = (gint)((gdouble)(surface_height - dest_height) / 2);
+                        x_scale = y_scale;
                     }
-                    dest_width = x_scale * (gdouble)gdk_pixbuf_get_width (tmp_pixbuf);
-                    dest_height = y_scale * (gdouble)gdk_pixbuf_get_height (tmp_pixbuf);
-                    dest_x = (gdouble)(pixbuf_width - gdk_pixbuf_get_width (tmp_pixbuf)*x_scale) / 2;
-                    dest_y = (gdouble)(pixbuf_height - gdk_pixbuf_get_height (tmp_pixbuf)*y_scale) / 2;
-                    break;
-                case MONITOR_STYLE_STRETCHED:
-                    dest_x = 0;
-                    dest_y = 0;
-                    dest_width = pixbuf_width;
-                    dest_height = pixbuf_height;
-                    x_scale = (gdouble)dest_width / (gdouble)gdk_pixbuf_get_width (tmp_pixbuf);
-                    y_scale = (gdouble)dest_height / (gdouble)gdk_pixbuf_get_height (tmp_pixbuf);
                     break;
                 default:
-                    x_scale = (gdouble)pixbuf_width / (gdouble)gdk_pixbuf_get_width (tmp_pixbuf);
-                    y_scale = (gdouble)pixbuf_height / (gdouble)gdk_pixbuf_get_height (tmp_pixbuf);
-                    if (x_scale > y_scale)
-                    {
-                        x_scale = y_scale;
-                    }
-                    else
-                    {
-                        y_scale = x_scale;
-                    }
-                    dest_width = x_scale * (gdouble)gdk_pixbuf_get_width (tmp_pixbuf);
-                    dest_height = y_scale * (gdouble)gdk_pixbuf_get_height (tmp_pixbuf);
-                    dest_x = (gdouble)(pixbuf_width - dest_width) / 2;
-                    dest_y = (gdouble)(pixbuf_height - dest_height) / 2;
                     gdk_pixbuf_saturate_and_pixelate (
                         tmp_pixbuf,
                         tmp_pixbuf,
                         0.0,
                         TRUE);
+                    break;
             }
+            cairo_scale (ctx, x_scale, y_scale);
+            gdk_cairo_set_source_pixbuf (
+                    ctx,
+                    tmp_pixbuf,
+                    dest_x/x_scale,
+                    dest_y/y_scale);
+            cairo_paint (ctx);
+            cairo_destroy (ctx);
 
-            gdk_pixbuf_composite (
-                tmp_pixbuf,
-                monitor_pixbuf,
-                dest_x,
-                dest_y,
-                dest_width,
-                dest_height,
-                0.0,
-                0.0,
-                x_scale,
-                y_scale,
-                GDK_INTERP_BILINEAR,
-                255);
 
             g_object_unref (tmp_pixbuf);
         }
     }
 
-    rstto_monitor_chooser_set_pixbuf (
-            RSTTO_MONITOR_CHOOSER(manager->priv->monitor_chooser),
+    rstto_monitor_chooser_set_image_surface (
+            RSTTO_MONITOR_CHOOSER (manager->priv->monitor_chooser),
             manager->priv->monitor,
-            monitor_pixbuf,
+            image_surface,
             NULL);
-
-    if ( NULL != monitor_pixbuf )
-    {
-       g_object_unref (monitor_pixbuf);
-    }
 }
 
+
 static void
 cb_style_combo_changed (
         GtkComboBox *style_combo,
@@ -563,3 +552,4 @@ cb_style_combo_changed (
 
     configure_monitor_chooser_pixbuf (manager);
 }
+
diff --git a/src/monitor_chooser.c b/src/monitor_chooser.c
index 8a784c8..f18b770 100644
--- a/src/monitor_chooser.c
+++ b/src/monitor_chooser.c
@@ -29,7 +29,8 @@
 typedef struct {
     gint width;
     gint height;
-    GdkPixbuf *pixbuf;
+
+    cairo_surface_t *image_surface;
 } Monitor;
 
 typedef struct {
@@ -404,7 +405,6 @@ paint_monitor ( cairo_t *cr,
     /*******************************************/
     PangoLayout *layout;
     PangoFontDescription *font_description;
-    GdkPixbuf *dst_pixbuf = NULL;
 
     g_return_if_fail (NULL != monitor);
     
@@ -533,18 +533,18 @@ paint_monitor ( cairo_t *cr,
     cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 1.0);
     cairo_fill_preserve (cr);
 
-    if (monitor->pixbuf)
+    if (monitor->image_surface)
     {
         cairo_clip_preserve (cr);
 
-        hscale = monitor_width / (gdk_pixbuf_get_width(monitor->pixbuf));
-        vscale = monitor_height / (gdk_pixbuf_get_height(monitor->pixbuf));
+        hscale = monitor_width / (cairo_image_surface_get_width(monitor->image_surface));
+        vscale = monitor_height / (cairo_image_surface_get_height(monitor->image_surface));
 
         cairo_scale (cr, hscale, vscale);
 
-        gdk_cairo_set_source_pixbuf (
+        cairo_set_source_surface (
                 cr,
-                monitor->pixbuf,
+                monitor->image_surface,
                 monitor_x/hscale,
                 monitor_y/vscale);
         cairo_paint(cr);
@@ -553,12 +553,6 @@ paint_monitor ( cairo_t *cr,
         cairo_scale (cr, 1/hscale, 1/vscale);
     }
 
-    if (NULL != dst_pixbuf)
-    {
-        g_object_unref (dst_pixbuf);
-        dst_pixbuf = NULL;
-    }
-
     if (FALSE == active)
     {
         cairo_set_source_rgba (cr, 1.0, 1.0, 1.0, 0.6);
@@ -645,10 +639,10 @@ rstto_monitor_chooser_add (
 }
 
 gint
-rstto_monitor_chooser_set_pixbuf (
+rstto_monitor_chooser_set_image_surface (
         RsttoMonitorChooser *chooser,
         gint monitor_id,
-        GdkPixbuf *pixbuf,
+        cairo_surface_t *surface,
         GError **error)
 {
     Monitor *monitor;
@@ -660,17 +654,13 @@ rstto_monitor_chooser_set_pixbuf (
 
     if (monitor)
     {
-        if (monitor->pixbuf)
+        if (monitor->image_surface)
         {
-            g_object_unref (monitor->pixbuf);
+            cairo_surface_destroy(monitor->image_surface);
         }
 
-        monitor->pixbuf = pixbuf;
+        monitor->image_surface = surface;
 
-        if (monitor->pixbuf)
-        {
-            g_object_ref (monitor->pixbuf);
-        }
         retval = monitor_id;
     }
     if (GTK_WIDGET_REALIZED (GTK_WIDGET(chooser)))
diff --git a/src/monitor_chooser.h b/src/monitor_chooser.h
index 9bfe28f..9958c17 100644
--- a/src/monitor_chooser.h
+++ b/src/monitor_chooser.h
@@ -69,11 +69,11 @@ rstto_monitor_chooser_add (
         gint height);
 
 gint
-rstto_monitor_chooser_set_pixbuf (
-        RsttoMonitorChooser *,
+rstto_monitor_chooser_set_image_surface (
+        RsttoMonitorChooser *chooser,
         gint monitor_id,
-        GdkPixbuf *pixbuf,
-        GError **);
+        cairo_surface_t *surface,
+        GError **error);
 
 gint
 rstto_monitor_chooser_get_selected (
diff --git a/src/xfce_wallpaper_manager.c b/src/xfce_wallpaper_manager.c
index d7887cd..f7e3470 100644
--- a/src/xfce_wallpaper_manager.c
+++ b/src/xfce_wallpaper_manager.c
@@ -635,7 +635,7 @@ cb_monitor_chooser_changed (
         RsttoMonitorChooser *monitor_chooser,
         RsttoXfceWallpaperManager *manager)
 {
-    rstto_monitor_chooser_set_pixbuf (
+    rstto_monitor_chooser_set_image_surface (
             monitor_chooser,
             manager->priv->monitor,
             NULL,
@@ -724,15 +724,19 @@ static void
 configure_monitor_chooser_pixbuf (
     RsttoXfceWallpaperManager *manager )
 {
-    GdkPixbuf *monitor_pixbuf = NULL;
+    cairo_surface_t *image_surface = NULL;
+    cairo_t *ctx;
+    //GdkColor bg_color;
+
     GdkPixbuf *tmp_pixbuf = NULL;
+
     gint monitor_width = 0;
     gint monitor_height = 0;
     gdouble saturation = gtk_adjustment_get_value (GTK_ADJUSTMENT(manager->priv->saturation_adjustment));
     gdouble brightness = gtk_adjustment_get_value (GTK_ADJUSTMENT(manager->priv->brightness_adjustment));
 
-    gint pixbuf_width = 0;
-    gint pixbuf_height = 0;
+    gint surface_width = 0;
+    gint surface_height = 0;
     gint dest_x = 0;
     gint dest_y = 0;
     gint dest_width = 0;
@@ -760,110 +764,90 @@ configure_monitor_chooser_pixbuf (
                     &monitor_width,
                     &monitor_height);
 
-            pixbuf_width = monitor_width * 0.2;
-            pixbuf_height = monitor_height * 0.2;
+            surface_width = monitor_width * 0.2;
+            surface_height = monitor_height * 0.2;
+
+            image_surface = cairo_image_surface_create (
+                    CAIRO_FORMAT_ARGB32,
+                    surface_width,
+                    surface_height);
+            ctx = cairo_create ( image_surface );
+
+            //gdk_cairo_set_source_color ( ctx, bg_color );
+            cairo_set_source_rgb (ctx, 0, 0, 0);
+            cairo_paint (ctx);
 
-            monitor_pixbuf = gdk_pixbuf_new (
-                    GDK_COLORSPACE_RGB,     /* Colorspace       */
-                    FALSE,                  /* has-alpha        */
-                    8,                      /* bits per sample  */
-                    pixbuf_width,           /* width            */
-                    pixbuf_height);         /* height           */
-            gdk_pixbuf_fill (
-                    monitor_pixbuf,
-                    0x00000000);
+            x_scale = (gdouble)surface_width / (gdouble)gdk_pixbuf_get_width (tmp_pixbuf);
+            y_scale = (gdouble)surface_height / (gdouble)gdk_pixbuf_get_height (tmp_pixbuf);
 
             switch (manager->priv->style)
             {
                 case MONITOR_STYLE_ZOOMED:
-                    dest_x = 0;
-                    dest_y = 0;
-                    dest_width = pixbuf_width;
-                    dest_height = pixbuf_height;
-                    x_scale = (gdouble)dest_width / (gdouble)gdk_pixbuf_get_width (tmp_pixbuf);
-                    y_scale = (gdouble)dest_height / (gdouble)gdk_pixbuf_get_height (tmp_pixbuf);
                     if (x_scale > y_scale)
                     {
+                        dest_width = (gint)((gdouble)gdk_pixbuf_get_width (tmp_pixbuf) * x_scale);
+                        dest_height = (gint)((gdouble)gdk_pixbuf_get_height (tmp_pixbuf) * x_scale);
+                        dest_x = (gint)((gdouble)(surface_width - dest_width) / 2);
+                        dest_y = (gint)((gdouble)(surface_height - dest_height) / 2);
                         y_scale = x_scale;
                     }
                     else
                     {
+                        dest_width = (gint)((gdouble)gdk_pixbuf_get_width (tmp_pixbuf) * y_scale);
+                        dest_height = (gint)((gdouble)gdk_pixbuf_get_height (tmp_pixbuf) * y_scale);
+                        dest_x = (gint)((gdouble)(surface_width - dest_width) / 2);
+                        dest_y = (gint)((gdouble)(surface_height - dest_height) / 2);
                         x_scale = y_scale;
                     }
                     break;
+                case MONITOR_STYLE_STRETCHED:
+                    dest_x = 0.0;
+                    dest_y = 0.0;
+                    break;
                 case MONITOR_STYLE_AUTOMATIC:
                 case MONITOR_STYLE_SCALED:
-                    x_scale = (gdouble)pixbuf_width / (gdouble)gdk_pixbuf_get_width (tmp_pixbuf);
-                    y_scale = (gdouble)pixbuf_height / (gdouble)gdk_pixbuf_get_height (tmp_pixbuf);
-                    if (x_scale > y_scale)
+                    if (x_scale < y_scale)
                     {
-                        x_scale = y_scale;
+                        dest_width = (gint)((gdouble)gdk_pixbuf_get_width (tmp_pixbuf) * x_scale);
+                        dest_height = (gint)((gdouble)gdk_pixbuf_get_height (tmp_pixbuf) * x_scale);
+                        dest_x = (gint)((gdouble)(surface_width - dest_width) / 2);
+                        dest_y = (gint)((gdouble)(surface_height - dest_height) / 2);
+                        y_scale = x_scale;
                     }
                     else
                     {
-                        y_scale = x_scale;
+                        dest_width = (gint)((gdouble)gdk_pixbuf_get_width (tmp_pixbuf) * y_scale);
+                        dest_height = (gint)((gdouble)gdk_pixbuf_get_height (tmp_pixbuf) * y_scale);
+                        dest_x = (gint)((gdouble)(surface_width - dest_width) / 2);
+                        dest_y = (gint)((gdouble)(surface_height - dest_height) / 2);
+                        x_scale = y_scale;
                     }
-                    dest_width = x_scale * (gdouble)gdk_pixbuf_get_width (tmp_pixbuf);
-                    dest_height = y_scale * (gdouble)gdk_pixbuf_get_height (tmp_pixbuf);
-                    dest_x = (gdouble)(pixbuf_width - gdk_pixbuf_get_width (tmp_pixbuf)*x_scale) / 2;
-                    dest_y = (gdouble)(pixbuf_height - gdk_pixbuf_get_height (tmp_pixbuf)*y_scale) / 2;
-                    break;
-                case MONITOR_STYLE_STRETCHED:
-                    dest_x = 0;
-                    dest_y = 0;
-                    dest_width = pixbuf_width;
-                    dest_height = pixbuf_height;
-                    x_scale = (gdouble)dest_width / (gdouble)gdk_pixbuf_get_width (tmp_pixbuf);
-                    y_scale = (gdouble)dest_height / (gdouble)gdk_pixbuf_get_height (tmp_pixbuf);
                     break;
                 default:
-                    x_scale = (gdouble)pixbuf_width / (gdouble)gdk_pixbuf_get_width (tmp_pixbuf);
-                    y_scale = (gdouble)pixbuf_height / (gdouble)gdk_pixbuf_get_height (tmp_pixbuf);
-                    if (x_scale > y_scale)
-                    {
-                        x_scale = y_scale;
-                    }
-                    else
-                    {
-                        y_scale = x_scale;
-                    }
-                    dest_width = x_scale * (gdouble)gdk_pixbuf_get_width (tmp_pixbuf);
-                    dest_height = y_scale * (gdouble)gdk_pixbuf_get_height (tmp_pixbuf);
-                    dest_x = (gdouble)(pixbuf_width - gdk_pixbuf_get_width (tmp_pixbuf)*x_scale) / 2;
-                    dest_y = (gdouble)(pixbuf_height - gdk_pixbuf_get_height (tmp_pixbuf)*y_scale) / 2;
                     gdk_pixbuf_saturate_and_pixelate (
                         tmp_pixbuf,
                         tmp_pixbuf,
                         0.0,
                         TRUE);
+                    break;
             }
+            cairo_scale (ctx, x_scale, y_scale);
+            gdk_cairo_set_source_pixbuf (
+                    ctx,
+                    tmp_pixbuf,
+                    dest_x/x_scale,
+                    dest_y/y_scale);
+            cairo_paint (ctx);
+            cairo_destroy (ctx);
 
-            gdk_pixbuf_composite (
-                tmp_pixbuf,
-                monitor_pixbuf,
-                dest_x,
-                dest_y,
-                dest_width,
-                dest_height,
-                0.0,
-                0.0,
-                x_scale,
-                y_scale,
-                GDK_INTERP_BILINEAR,
-                255);
 
             g_object_unref (tmp_pixbuf);
         }
     }
 
-    rstto_monitor_chooser_set_pixbuf (
-            RSTTO_MONITOR_CHOOSER(manager->priv->monitor_chooser),
+    rstto_monitor_chooser_set_image_surface (
+            RSTTO_MONITOR_CHOOSER (manager->priv->monitor_chooser),
             manager->priv->monitor,
-            monitor_pixbuf,
+            image_surface,
             NULL);
-
-    if ( NULL != monitor_pixbuf )
-    {
-       g_object_unref (monitor_pixbuf);
-    }
 }


More information about the Xfce4-commits mailing list