Skip to content

Commit f3253a3

Browse files
committed
perf(templates): lazy-load gallery and decode thumbnails at reduced size
for comparison, just idling consumes about 150MiB+ of RAM and uses around 200MiB+ when a photo or svg is loaded (even larger when a GIF is loaded, also this is without additional layers and filters applied and with auto layer optimization enabled)
1 parent c431c34 commit f3253a3

2 files changed

Lines changed: 29 additions & 22 deletions

File tree

‎src/meme-window-templates.c‎

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "meme-canvas.h"
2525
#include <glib/gstdio.h>
2626
#include <stdio.h>
27+
#define TEMPLATE_THUMB_SIZE 240
2728

2829
static void set_template_select_mode (MemeWindow *self, gboolean active);
2930
static void update_template_gallery_empty_state (MemeWindow *self);
@@ -33,6 +34,10 @@ void on_open_template_window_clicked (MemeWindow *self) {
3334
//its just like seeing her, for the first time again.
3435
if (self->template_select_mode)
3536
set_template_select_mode (self, FALSE);
37+
if (!g_object_get_data (G_OBJECT (self->template_gallery), "populated")) {
38+
g_object_set_data (G_OBJECT (self->template_gallery), "populated", GINT_TO_POINTER (1));
39+
populate_template_gallery (self);
40+
}
3641
adw_dialog_present (self->template_window, GTK_WIDGET (self));
3742
}
3843

@@ -45,36 +50,39 @@ static gboolean is_user_template (const char *path) {
4550
return g_str_has_prefix (path, user_dir);
4651
}
4752

53+
static GtkWidget * make_thumbnail_picture (const char *full_path) {
54+
GdkPixbuf *pb;
55+
GtkWidget *picture;
56+
57+
if (g_str_has_prefix (full_path, "resource://"))
58+
pb = gdk_pixbuf_new_from_resource_at_scale (full_path + 11,
59+
TEMPLATE_THUMB_SIZE, TEMPLATE_THUMB_SIZE, TRUE, NULL);
60+
else
61+
pb = gdk_pixbuf_new_from_file_at_scale (full_path,
62+
TEMPLATE_THUMB_SIZE, TEMPLATE_THUMB_SIZE, TRUE, NULL);
63+
if (!pb)
64+
return gtk_picture_new ();
65+
{
66+
GdkTexture *texture = gdk_texture_new_for_pixbuf (pb);
67+
picture = gtk_picture_new_for_paintable (GDK_PAINTABLE (texture));
68+
g_object_unref (texture);
69+
g_object_unref (pb);
70+
}
71+
return picture;
72+
}
73+
4874
static void add_file_to_gallery (MemeWindow *self, const char *full_path) {
4975
GtkWidget *picture;
5076
gint64 *mtime = g_new0(gint64, 1);
5177

78+
picture = make_thumbnail_picture (full_path);
79+
5280
if (g_str_has_prefix (full_path, "resource://")) {
53-
picture = gtk_picture_new_for_resource (full_path + 11);
5481
*mtime = 0; // Built-in resources are technically the oldest
55-
} else if (g_str_has_suffix (full_path, ".gif")) {
56-
GStatBuf stat_buf;
57-
GdkPixbuf *frame = gdk_pixbuf_new_from_file (full_path, NULL);
58-
59-
if (frame) {
60-
GdkTexture *texture = gdk_texture_new_for_pixbuf (frame);
61-
picture = gtk_picture_new_for_paintable (GDK_PAINTABLE (texture));
62-
g_object_unref (texture);
63-
g_object_unref (frame);
64-
} else {
65-
picture = gtk_picture_new ();
66-
}
67-
68-
if (g_stat (full_path, &stat_buf) == 0) {
69-
*mtime = stat_buf.st_mtime;
70-
}
7182
} else {
7283
GStatBuf stat_buf;
73-
picture = gtk_picture_new_for_filename (full_path);
74-
75-
if (g_stat(full_path, &stat_buf) == 0) {
84+
if (g_stat (full_path, &stat_buf) == 0)
7685
*mtime = stat_buf.st_mtime;
77-
}
7886
}
7987

8088
gtk_picture_set_can_shrink (GTK_PICTURE (picture), TRUE);

‎src/meme-window.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -391,7 +391,6 @@ static void meme_window_init (MemeWindow *self) {
391391
g_signal_connect_swapped (self->footer_exit_draw_button, "clicked", G_CALLBACK (on_exit_draw_editing_clicked), self);
392392
g_signal_connect (self->bw_button, "toggled", G_CALLBACK (on_deep_fry_toggled), self);
393393
g_signal_connect (self->footer_bw_button, "toggled", G_CALLBACK (on_deep_fry_toggled), self);
394-
populate_template_gallery (self);
395394

396395
motion = gtk_event_controller_motion_new ();
397396
gtk_widget_add_controller (GTK_WIDGET (self->meme_preview), motion);

0 commit comments

Comments
 (0)