[gnome-calendar] project: use cairo_surface_t when possible



commit cf6cdfd2aa74a65265a5d4e19e71402db2ae9429
Author: Georges Basile Stavracas Neto <georges stavracas gmail com>
Date:   Fri Mar 17 09:02:22 2017 -0300

    project: use cairo_surface_t when possible
    
    We can avoid an entire roundtrip from surface > pixbuf > surface
    if we simply pass the surface to GtkImage. We also save quite
    a bunch of debug lines.

 src/gcal-edit-dialog.c           |   22 +++++++++++++---------
 src/gcal-quick-add-popover.c     |   24 ++++++++++++------------
 src/gcal-search-view.c           |    8 ++++----
 src/gcal-shell-search-provider.c |    7 ++++++-
 src/gcal-source-dialog.c         |   16 ++++++++--------
 src/gcal-utils.c                 |   29 ++++++++++-------------------
 src/gcal-utils.h                 |    4 ++--
 src/gcal-window.c                |    8 ++++----
 8 files changed, 59 insertions(+), 59 deletions(-)
---
diff --git a/src/gcal-edit-dialog.c b/src/gcal-edit-dialog.c
index f649455..59fc74a 100644
--- a/src/gcal-edit-dialog.c
+++ b/src/gcal-edit-dialog.c
@@ -153,13 +153,15 @@ fill_sources_menu (GcalEditDialog *dialog)
       ESource *source;
       GMenuItem *item;
       GdkRGBA color;
+      cairo_surface_t *surface;
       GdkPixbuf *pix;
 
       source = E_SOURCE (aux->data);
 
       /* retrieve color */
       get_color_name_from_source (source, &color);
-      pix = get_circle_pixbuf_from_color (&color, 16);;
+      surface = get_circle_surface_from_color (&color, 16);
+      pix = gdk_pixbuf_get_from_surface (surface, 0, 0, 16, 16);
 
       /* menu item */
       item = g_menu_item_new (e_source_get_display_name (source), "select-calendar");
@@ -178,6 +180,7 @@ fill_sources_menu (GcalEditDialog *dialog)
 
       g_menu_append_item (dialog->sources_menu, item);
 
+      g_clear_pointer (&surface, cairo_surface_destroy);
       g_object_unref (pix);
       g_object_unref (item);
     }
@@ -217,18 +220,19 @@ on_calendar_selected (GSimpleAction *action,
       if (g_strcmp0 (e_source_get_uid (source), uid) == 0)
       {
         GdkRGBA color;
-        GdkPixbuf *pix;
+        cairo_surface_t *surface;
 
         /* retrieve color */
         get_color_name_from_source (source, &color);
 
-        pix = get_circle_pixbuf_from_color (&color, 16);
-        gtk_image_set_from_pixbuf (GTK_IMAGE (self->source_image), pix);
-        g_object_unref (pix);
+        surface = get_circle_surface_from_color (&color, 16);
+        gtk_image_set_from_surface (GTK_IMAGE (self->source_image), surface);
 
         self->selected_source = source;
 
         gtk_label_set_label (GTK_LABEL (self->subtitle_label), e_source_get_display_name (source));
+
+        g_clear_pointer (&surface, cairo_surface_destroy);
         break;
       }
     }
@@ -1062,7 +1066,7 @@ gcal_edit_dialog_set_event (GcalEditDialog *dialog,
     {
       GDateTime *date_start;
       GDateTime *date_end;
-      GdkPixbuf *pix;
+      cairo_surface_t *surface;
       ESource *source;
       const gchar *summary;
       gboolean all_day;
@@ -1095,9 +1099,9 @@ gcal_edit_dialog_set_event (GcalEditDialog *dialog,
         gtk_entry_set_text (GTK_ENTRY (dialog->summary_entry), summary);
 
       /* dialog titlebar's title & subtitle */
-      pix = get_circle_pixbuf_from_color (gcal_event_get_color (event), 16);
-      gtk_image_set_from_pixbuf (GTK_IMAGE (dialog->source_image), pix);
-      g_object_unref (pix);
+      surface = get_circle_surface_from_color (gcal_event_get_color (event), 16);
+      gtk_image_set_from_surface (GTK_IMAGE (dialog->source_image), surface);
+      g_clear_pointer (&surface, cairo_surface_destroy);
 
       gtk_label_set_label (GTK_LABEL (dialog->subtitle_label), e_source_get_display_name (source));
 
diff --git a/src/gcal-quick-add-popover.c b/src/gcal-quick-add-popover.c
index a319274..479420c 100644
--- a/src/gcal-quick-add-popover.c
+++ b/src/gcal-quick-add-popover.c
@@ -73,8 +73,8 @@ create_row_for_source (GcalManager *manager,
                        ESource     *source,
                        gboolean     writable)
 {
+  cairo_surface_t *surface;
   GtkWidget *row, *box, *icon, *label, *selected_icon;
-  GdkPixbuf *circle_pixbuf;
   GdkRGBA color;
   gchar *tooltip, *parent_name;
 
@@ -87,8 +87,8 @@ create_row_for_source (GcalManager *manager,
 
   /* The icon with the source color */
   get_color_name_from_source (source, &color);
-  circle_pixbuf = get_circle_pixbuf_from_color (&color, 16);
-  icon = gtk_image_new_from_pixbuf (circle_pixbuf);
+  surface = get_circle_surface_from_color (&color, 16);
+  icon = gtk_image_new_from_surface (surface);
 
   gtk_container_add (GTK_CONTAINER (box), icon);
 
@@ -135,7 +135,7 @@ create_row_for_source (GcalManager *manager,
   gtk_widget_show (box);
   gtk_widget_show (row);
 
-  g_clear_object (&circle_pixbuf);
+  g_clear_pointer (&surface, cairo_surface_destroy);
   g_free (parent_name);
   g_free (tooltip);
 
@@ -176,8 +176,8 @@ static void
 select_row (GcalQuickAddPopover *self,
             GtkListBoxRow       *row)
 {
+  cairo_surface_t *surface;
   GtkWidget *icon;
-  GdkPixbuf *circle_pixbuf;
   ESource *source;
   GdkRGBA color;
 
@@ -200,8 +200,8 @@ select_row (GcalQuickAddPopover *self,
   gtk_label_set_label (GTK_LABEL (self->calendar_name_label), e_source_get_display_name (source));
 
   get_color_name_from_source (source, &color);
-  circle_pixbuf = get_circle_pixbuf_from_color (&color, 16);
-  gtk_image_set_from_pixbuf (GTK_IMAGE (self->color_image), circle_pixbuf);
+  surface = get_circle_surface_from_color (&color, 16);
+  gtk_image_set_from_surface (GTK_IMAGE (self->color_image), surface);
 
   /* Return to the events page */
   gtk_stack_set_visible_child_name (GTK_STACK (self->stack), "events");
@@ -210,7 +210,7 @@ select_row (GcalQuickAddPopover *self,
   if (gtk_widget_get_visible (GTK_WIDGET (self)))
     gtk_entry_grab_focus_without_selecting (GTK_ENTRY (self->summary_entry));
 
-  g_clear_object (&circle_pixbuf);
+  g_clear_pointer (&surface, cairo_surface_destroy);
 }
 
 static void
@@ -348,8 +348,8 @@ on_source_changed (GcalManager         *manager,
                    ESource             *source,
                    GcalQuickAddPopover *self)
 {
+  cairo_surface_t *surface;
   GtkWidget *row, *color_icon, *name_label;
-  GdkPixbuf *circle_pixbuf;
   GdkRGBA color;
 
   row = get_row_for_source (self, source);
@@ -378,10 +378,10 @@ on_source_changed (GcalManager         *manager,
 
   /* Setup the source color, in case it changed */
   get_color_name_from_source (source, &color);
-  circle_pixbuf = get_circle_pixbuf_from_color (&color, 16);
-  gtk_image_set_from_pixbuf (GTK_IMAGE (color_icon), circle_pixbuf);
+  surface = get_circle_surface_from_color (&color, 16);
+  gtk_image_set_from_surface (GTK_IMAGE (color_icon), surface);
 
-  g_clear_object (&circle_pixbuf);
+  g_clear_pointer (&surface, cairo_surface_destroy);
 
   /* Also setup the row name, in case we just changed the source name */
   gtk_label_set_text (GTK_LABEL (name_label), e_source_get_display_name (source));
diff --git a/src/gcal-search-view.c b/src/gcal-search-view.c
index 8ef05fb..efc5e07 100644
--- a/src/gcal-search-view.c
+++ b/src/gcal-search-view.c
@@ -297,7 +297,7 @@ make_row_for_event (GcalSearchView *view,
 {
   GDateTime *local_datetime;
   GDateTime *date_start;
-  GdkPixbuf *pixbuf;
+  cairo_surface_t *surface;
 
   GtkWidget *row;
   GtkWidget *grid;
@@ -311,10 +311,10 @@ make_row_for_event (GcalSearchView *view,
   GtkWidget *image;
 
   /* get event color */
-  pixbuf = gcal_get_pixbuf_from_color (gcal_event_get_color (event), 16);
+  surface = gcal_get_surface_from_color (gcal_event_get_color (event), 16);
 
   /* make an image of the color */
-  image = gtk_image_new_from_pixbuf (pixbuf);
+  image = gtk_image_new_from_surface (surface);
 
   /* grid & box*/
   row = gtk_list_box_row_new ();
@@ -397,7 +397,7 @@ make_row_for_event (GcalSearchView *view,
   gtk_widget_show_all (row);
 
   g_clear_pointer (&local_datetime, g_date_time_unref);
-  g_object_unref (pixbuf);
+  g_clear_pointer (&surface, cairo_surface_destroy);
 
   return row;
 }
diff --git a/src/gcal-shell-search-provider.c b/src/gcal-shell-search-provider.c
index 5429c62..9e08a94 100644
--- a/src/gcal-shell-search-provider.c
+++ b/src/gcal-shell-search-provider.c
@@ -203,6 +203,8 @@ get_result_metas_cb (GcalShellSearchProvider  *search_provider,
   g_variant_builder_init (&abuilder, G_VARIANT_TYPE ("aa{sv}"));
   for (i = 0; i < g_strv_length (results); i++)
     {
+      cairo_surface_t *surface;
+
       uuid = results[i];
       event = g_hash_table_lookup (priv->events, uuid);
 
@@ -210,7 +212,8 @@ get_result_metas_cb (GcalShellSearchProvider  *search_provider,
       g_variant_builder_add (&builder, "{sv}", "id", g_variant_new_string (uuid));
       g_variant_builder_add (&builder, "{sv}", "name", g_variant_new_string (gcal_event_get_summary 
(event)));
 
-      gicon = get_circle_pixbuf_from_color (gcal_event_get_color (event), 128);
+      surface = get_circle_surface_from_color (gcal_event_get_color (event), 96);
+      gicon = gdk_pixbuf_get_from_surface (surface, 0, 0, 96, 96);
       icon_variant = g_icon_serialize (G_ICON (gicon));
       g_variant_builder_add (&builder, "{sv}", "icon", icon_variant);
       g_object_unref (gicon);
@@ -228,6 +231,8 @@ get_result_metas_cb (GcalShellSearchProvider  *search_provider,
 
       g_variant_builder_add (&builder, "{sv}", "description", g_variant_new_string (desc));
       g_variant_builder_add_value (&abuilder, g_variant_builder_end (&builder));
+
+      g_clear_pointer (&surface, cairo_surface_destroy);
     }
   g_dbus_method_invocation_return_value (invocation, g_variant_new ("(aa{sv})", &abuilder));
 
diff --git a/src/gcal-source-dialog.c b/src/gcal-source-dialog.c
index 3a68359..67f1144 100644
--- a/src/gcal-source-dialog.c
+++ b/src/gcal-source-dialog.c
@@ -569,7 +569,7 @@ source_color_changed (GObject    *source,
                       GtkImage *icon)
 {
   ESourceSelectable *extension;
-  GdkPixbuf *pixbuf;
+  cairo_surface_t *surface;
   GdkRGBA out_color;
 
   extension = E_SOURCE_SELECTABLE (source);
@@ -582,9 +582,9 @@ source_color_changed (GObject    *source,
 
   if (gdk_rgba_parse (&out_color, e_source_selectable_get_color (extension)))
     {
-      pixbuf = get_circle_pixbuf_from_color (&out_color, 24);
-      gtk_image_set_from_pixbuf (GTK_IMAGE (icon), pixbuf);
-      g_object_unref (pixbuf);
+      surface = get_circle_surface_from_color (&out_color, 24);
+      gtk_image_set_from_surface (GTK_IMAGE (icon), surface);
+      g_clear_pointer (&surface, cairo_surface_destroy);
     }
 }
 
@@ -610,10 +610,10 @@ static GtkWidget*
 make_row_from_source (GcalSourceDialog *dialog,
                       ESource          *source)
 {
+  cairo_surface_t *surface;
   GtkBuilder *builder;
   GtkWidget *bottom_label;
   GtkWidget *top_label;
-  GdkPixbuf *pixbuf;
   GtkWidget *icon;
   GtkWidget *row;
   GdkRGBA color;
@@ -632,9 +632,9 @@ make_row_from_source (GcalSourceDialog *dialog,
 
   /* source color icon */
   get_color_name_from_source (source, &color);
-  pixbuf = get_circle_pixbuf_from_color (&color, 24);
+  surface = get_circle_surface_from_color (&color, 24);
   icon = GTK_WIDGET (gtk_builder_get_object (builder, "icon"));
-  gtk_image_set_from_pixbuf (GTK_IMAGE (icon), pixbuf);
+  gtk_image_set_from_surface (GTK_IMAGE (icon), surface);
 
   /* source name label */
   top_label = GTK_WIDGET (gtk_builder_get_object (builder, "title"));
@@ -650,8 +650,8 @@ make_row_from_source (GcalSourceDialog *dialog,
   bottom_label = GTK_WIDGET (gtk_builder_get_object (builder, "subtitle"));
   gtk_label_set_label (GTK_LABEL (bottom_label), parent_name);
 
+  g_clear_pointer (&surface, cairo_surface_destroy);
   g_object_unref (builder);
-  g_object_unref (pixbuf);
   g_free (parent_name);
 
   return row;
diff --git a/src/gcal-utils.c b/src/gcal-utils.c
index cbcdb7e..ddec389 100644
--- a/src/gcal-utils.c
+++ b/src/gcal-utils.c
@@ -187,13 +187,12 @@ gcal_get_month_name (gint i)
  *
  * Returns: (Transfer full): An instance of {@link GdkPixbuf} to be freed with g_object_unref()
  **/
-GdkPixbuf*
-gcal_get_pixbuf_from_color (GdkRGBA  *color,
-                            gint      size)
+cairo_surface_t*
+gcal_get_surface_from_color (GdkRGBA  *color,
+                             gint      size)
 {
   cairo_surface_t *surface;
   cairo_t *cr;
-  GdkPixbuf *pix;
 
   /* TODO: review size here, maybe not hardcoded */
   surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, size, size);
@@ -207,22 +206,17 @@ gcal_get_pixbuf_from_color (GdkRGBA  *color,
   cairo_rectangle (cr, 0, 0, size, size);
   cairo_fill (cr);
   cairo_destroy (cr);
-  pix = gdk_pixbuf_get_from_surface (surface,
-                                     0, 0,
-                                     size, size);
-  cairo_surface_destroy (surface);
-  return pix;
+
+  return surface;
 }
 
-GdkPixbuf*
-get_circle_pixbuf_from_color (GdkRGBA *color,
-                              gint     size)
+cairo_surface_t*
+get_circle_surface_from_color (GdkRGBA *color,
+                               gint     size)
 {
   cairo_surface_t *surface;
   cairo_t *cr;
-  GdkPixbuf *pix;
 
-  /* TODO: review size here, maybe not hardcoded */
   surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, size, size);
   cr = cairo_create (surface);
 
@@ -234,11 +228,8 @@ get_circle_pixbuf_from_color (GdkRGBA *color,
   cairo_arc (cr, size / 2.0, size / 2.0, size / 2.0, 0., 2 * M_PI);
   cairo_fill (cr);
   cairo_destroy (cr);
-  pix = gdk_pixbuf_get_from_surface (surface,
-                                     0, 0,
-                                     size, size);
-  cairo_surface_destroy (surface);
-  return pix;
+
+  return surface;
 }
 
 void
diff --git a/src/gcal-utils.h b/src/gcal-utils.h
index 8c3a249..c39f355 100644
--- a/src/gcal-utils.h
+++ b/src/gcal-utils.h
@@ -73,10 +73,10 @@ gchar*          gcal_get_weekday                                (gint
 
 gchar*          gcal_get_month_name                             (gint                   i);
 
-GdkPixbuf*      gcal_get_pixbuf_from_color                      (GdkRGBA               *color,
+cairo_surface_t*gcal_get_surface_from_color                     (GdkRGBA               *color,
                                                                  gint                   size);
 
-GdkPixbuf*      get_circle_pixbuf_from_color                    (GdkRGBA               *color,
+cairo_surface_t*get_circle_surface_from_color                   (GdkRGBA               *color,
                                                                  gint                   size);
 
 void            get_color_name_from_source                      (ESource               *source,
diff --git a/src/gcal-window.c b/src/gcal-window.c
index 49edcf1..eaf9132 100644
--- a/src/gcal-window.c
+++ b/src/gcal-window.c
@@ -888,7 +888,7 @@ make_row_from_source (GcalWindow *window,
 {
   GtkWidget *label, *icon, *checkbox, *box, *row;
   GtkStyleContext *context;
-  GdkPixbuf *pixbuf;
+  cairo_surface_t *surface;
   GdkRGBA color;
 
   row = gtk_list_box_row_new ();
@@ -905,8 +905,8 @@ make_row_from_source (GcalWindow *window,
 
   /* source color icon */
   get_color_name_from_source (source, &color);
-  pixbuf = get_circle_pixbuf_from_color (&color, 16);
-  icon = gtk_image_new_from_pixbuf (pixbuf);
+  surface = get_circle_surface_from_color (&color, 16);
+  icon = gtk_image_new_from_surface (surface);
 
   gtk_style_context_add_class (gtk_widget_get_style_context (icon), "calendar-color-image");
 
@@ -930,7 +930,7 @@ make_row_from_source (GcalWindow *window,
 
   gtk_widget_show_all (row);
 
-  g_object_unref (pixbuf);
+  g_clear_pointer (&surface, cairo_surface_destroy);
 
   return row;
 }


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]