[libadwaita/wip/exalm/avatar-texture: 10/11] avatar: Replace draw_to_pixbuf() with draw_to_texture()




commit 803c53fd83d3936a98c7d2a4d15c81aea6a60925
Author: Alexander Mikhaylenko <alexm gnome org>
Date:   Mon Sep 20 19:33:39 2021 +0500

    avatar: Replace draw_to_pixbuf() with draw_to_texture()
    
    Textures implement GIcon now, so we can finally drop pixbufs.
    
    While we're here, remove the size parameter, it didn't work anyway.
    
    See https://gitlab.gnome.org/GNOME/libadwaita/-/issues/29

 doc/migrating-libhandy-1-4-to-libadwaita.md |  9 ++++--
 examples/adw-demo-window.c                  | 14 ++++-----
 src/adw-avatar.c                            | 45 ++++++++++-------------------
 src/adw-avatar.h                            |  6 ++--
 4 files changed, 30 insertions(+), 44 deletions(-)
---
diff --git a/doc/migrating-libhandy-1-4-to-libadwaita.md b/doc/migrating-libhandy-1-4-to-libadwaita.md
index fa3ecb1d..d34d4e7f 100644
--- a/doc/migrating-libhandy-1-4-to-libadwaita.md
+++ b/doc/migrating-libhandy-1-4-to-libadwaita.md
@@ -281,8 +281,13 @@ The "policy" property has been removed, the behavior is similar to the removed
 The `HdyAvatar:loadable-icon` property has been removed along with its getter
 and setter. It can be replaced by [property@Adw.Avatar:custom-image].
 
-The `hdy_avatar_draw_to_pixbuf_async()` function has been removed, use the
-regular [method@Adw.Avatar.draw_to_pixbuf] instead.
+The `hdy_avatar_draw_to_pixbuf()` and `hdy_avatar_draw_to_pixbuf_async()`
+functions has been removed, use the newly added
+[method@Adw.Avatar.draw_to_texture] instead. [class@Gdk.Texture] implements
+`[iface Gio Icon], so it should just work for that case.
+
+[method@Adw.Avatar.draw_to_texture] does not have the `size` parameter. Instead,
+it uses the avatar's current size, with no replacement.
 
 ## Adapt to Stylesheet Changes
 
diff --git a/examples/adw-demo-window.c b/examples/adw-demo-window.c
index f48c9db7..a308132a 100644
--- a/examples/adw-demo-window.c
+++ b/examples/adw-demo-window.c
@@ -261,17 +261,13 @@ file_chooser_response_cb (AdwDemoWindow  *self,
 {
   if (response_id == GTK_RESPONSE_ACCEPT) {
     g_autoptr (GFile) file = gtk_file_chooser_get_file (GTK_FILE_CHOOSER (chooser));
-    g_autoptr (GdkPixbuf) pixbuf =
-      adw_avatar_draw_to_pixbuf (self->avatar,
-                                 adw_avatar_get_size (self->avatar),
-                                 gtk_widget_get_scale_factor (GTK_WIDGET (self)));
+    g_autoptr (GdkTexture) texture =
+      adw_avatar_draw_to_texture (self->avatar,
+                                  gtk_widget_get_scale_factor (GTK_WIDGET (self)));
 
-    if (pixbuf != NULL) {
-      g_autofree char *path = NULL;
+    g_autofree char *path = g_file_get_path (file);
 
-      path = g_file_get_path (file);
-      gdk_pixbuf_save (pixbuf, path, "png", NULL, NULL);
-    }
+    gdk_texture_save_to_png (texture, path);
   }
 
   g_object_unref (chooser);
diff --git a/src/adw-avatar.c b/src/adw-avatar.c
index 4c6d3644..e624c53d 100644
--- a/src/adw-avatar.c
+++ b/src/adw-avatar.c
@@ -683,54 +683,41 @@ adw_avatar_set_size (AdwAvatar *self,
 }
 
 /**
- * adw_avatar_draw_to_pixbuf:
+ * adw_avatar_draw_to_texture:
  * @self: a `AdwAvatar`
- * @size: The size of the pixbuf
  * @scale_factor: The scale factor
  *
- * Renders @self into a [class@GdkPixbuf.Pixbuf] at @size and @scale_factor.
+ * Renders @self into a [class@Gdk.Texture] at @scale_factor.
  *
  * This can be used to export the fallback avatar.
  *
- * Returns: (transfer full): the pixbuf
+ * Returns: (transfer full): the texture
  *
  * Since: 1.0
  */
-GdkPixbuf *
-adw_avatar_draw_to_pixbuf (AdwAvatar *self,
-                           int        size,
-                           int        scale_factor)
+GdkTexture *
+adw_avatar_draw_to_texture (AdwAvatar *self,
+                            int        scale_factor)
 {
-  GtkSnapshot *snapshot;
   g_autoptr (GskRenderNode) node = NULL;
-  g_autoptr (cairo_surface_t) surface = NULL;
-  g_autoptr (cairo_t) cr = NULL;
-  graphene_rect_t bounds;
+  GtkSnapshot *snapshot;
+  GtkNative *native;
+  GskRenderer *renderer;
+  int size;
 
   g_return_val_if_fail (ADW_IS_AVATAR (self), NULL);
-  g_return_val_if_fail (size > 0, NULL);
   g_return_val_if_fail (scale_factor > 0, NULL);
 
+  size = self->size * scale_factor;
+
   snapshot = gtk_snapshot_new ();
+  gtk_snapshot_scale (snapshot, scale_factor, scale_factor);
   GTK_WIDGET_GET_CLASS (self)->snapshot (GTK_WIDGET (self), snapshot);
 
   node = gtk_snapshot_free_to_node (snapshot);
 
-  gsk_render_node_get_bounds (node, &bounds);
-  graphene_rect_round_to_pixel (&bounds);
-  graphene_rect_scale (&bounds, scale_factor, scale_factor, &bounds);
-
-  surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
-                                        bounds.size.width,
-                                        bounds.size.height);
-  cairo_surface_set_device_scale (surface, scale_factor, scale_factor);
-  cr = cairo_create (surface);
-
-  cairo_translate (cr, -bounds.origin.x, -bounds.origin.y);
-
-  gsk_render_node_draw (node, cr);
+  native = gtk_widget_get_native (GTK_WIDGET (self));
+  renderer = gtk_native_get_renderer (native);
 
-  return gdk_pixbuf_get_from_surface (surface, 0, 0,
-                                      bounds.size.width,
-                                      bounds.size.height);
+  return gsk_renderer_render_texture (renderer, node, &GRAPHENE_RECT_INIT (0, 0, size, size));
 }
diff --git a/src/adw-avatar.h b/src/adw-avatar.h
index 2d39dab8..7a0e1fcc 100644
--- a/src/adw-avatar.h
+++ b/src/adw-avatar.h
@@ -12,7 +12,6 @@
 
 #include "adw-version.h"
 
-#include <gdk-pixbuf/gdk-pixbuf.h>
 #include <gtk/gtk.h>
 
 G_BEGIN_DECLS
@@ -58,8 +57,7 @@ void adw_avatar_set_size (AdwAvatar *self,
                           int        size);
 
 ADW_AVAILABLE_IN_ALL
-GdkPixbuf *adw_avatar_draw_to_pixbuf (AdwAvatar *self,
-                                      int        size,
-                                      int        scale_factor) G_GNUC_WARN_UNUSED_RESULT;
+GdkTexture *adw_avatar_draw_to_texture (AdwAvatar *self,
+                                        int        scale_factor) G_GNUC_WARN_UNUSED_RESULT;
 
 G_END_DECLS


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