2 * Copyright (C) 2004 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include "image-load.h"
24 #include "image_load_gdk.h"
25 #include "image_load_jpeg.h"
26 #include "image_load_tiff.h"
27 #include "image_load_dds.h"
28 #include "image_load_djvu.h"
29 #include "image_load_pdf.h"
30 #include "image_load_psd.h"
31 #include "image_load_heif.h"
32 #include "image_load_ffmpegthumbnailer.h"
33 #include "image_load_collection.h"
34 #include "image_load_webp.h"
38 #include "ui_fileops.h"
39 #include "gq-marshal.h"
44 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT 4096
45 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT 1
48 /**************************************************************************************/
49 /* image loader class */
53 SIGNAL_AREA_READY = 0,
61 static guint signals[SIGNAL_COUNT] = { 0 };
63 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
64 static void image_loader_class_init(ImageLoaderClass *class);
65 static void image_loader_finalize(GObject *object);
66 static void image_loader_stop(ImageLoader *il);
68 GType image_loader_get_type(void)
70 static GType type = 0;
73 static const GTypeInfo info = {
74 sizeof(ImageLoaderClass),
76 NULL, /* base_finalize */
77 (GClassInitFunc)image_loader_class_init, /* class_init */
78 NULL, /* class_finalize */
79 NULL, /* class_data */
82 (GInstanceInitFunc)image_loader_init, /* instance_init */
83 NULL /* value_table */
85 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
90 static void image_loader_init(GTypeInstance *instance, gpointer g_class)
92 ImageLoader *il = (ImageLoader *)instance;
96 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
103 il->idle_done_id = 0;
105 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
106 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
107 il->mapped_file = NULL;
109 il->requested_width = 0;
110 il->requested_height = 0;
111 il->actual_width = 0;
112 il->actual_height = 0;
115 il->can_destroy = TRUE;
118 #if GLIB_CHECK_VERSION(2,32,0)
119 il->data_mutex = g_new(GMutex, 1);
120 g_mutex_init(il->data_mutex);
121 il->can_destroy_cond = g_new(GCond, 1);
122 g_cond_init(il->can_destroy_cond);
124 il->data_mutex = g_mutex_new();
125 il->can_destroy_cond = g_cond_new();
128 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
131 static void image_loader_class_init(ImageLoaderClass *class)
133 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
135 // gobject_class->set_property = image_loader_set_property;
136 // gobject_class->get_property = image_loader_get_property;
138 gobject_class->finalize = image_loader_finalize;
141 signals[SIGNAL_AREA_READY] =
142 g_signal_new("area_ready",
143 G_OBJECT_CLASS_TYPE(gobject_class),
145 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
147 gq_marshal_VOID__INT_INT_INT_INT,
154 signals[SIGNAL_ERROR] =
155 g_signal_new("error",
156 G_OBJECT_CLASS_TYPE(gobject_class),
158 G_STRUCT_OFFSET(ImageLoaderClass, error),
160 g_cclosure_marshal_VOID__VOID,
163 signals[SIGNAL_DONE] =
165 G_OBJECT_CLASS_TYPE(gobject_class),
167 G_STRUCT_OFFSET(ImageLoaderClass, done),
169 g_cclosure_marshal_VOID__VOID,
172 signals[SIGNAL_PERCENT] =
173 g_signal_new("percent",
174 G_OBJECT_CLASS_TYPE(gobject_class),
176 G_STRUCT_OFFSET(ImageLoaderClass, percent),
178 g_cclosure_marshal_VOID__DOUBLE,
182 signals[SIGNAL_SIZE] =
183 g_signal_new("size_prepared",
184 G_OBJECT_CLASS_TYPE(gobject_class),
186 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
188 gq_marshal_VOID__INT_INT,
195 static void image_loader_finalize(GObject *object)
197 ImageLoader *il = (ImageLoader *)object;
199 image_loader_stop(il);
201 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
203 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, il, il->bytes_read);
205 if (il->idle_done_id)
207 g_source_remove(il->idle_done_id);
208 il->idle_done_id = 0;
211 while (g_source_remove_by_user_data(il))
213 DEBUG_2("pending signals detected");
216 while (il->area_param_list)
218 DEBUG_1("pending area_ready signals detected");
219 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
220 g_free(il->area_param_list->data);
221 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
224 while (il->area_param_delayed_list)
226 g_free(il->area_param_delayed_list->data);
227 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
230 if (il->pixbuf) g_object_unref(il->pixbuf);
232 if (il->error) g_error_free(il->error);
234 file_data_unref(il->fd);
236 #if GLIB_CHECK_VERSION(2,32,0)
237 g_mutex_clear(il->data_mutex);
238 g_free(il->data_mutex);
239 g_cond_clear(il->can_destroy_cond);
240 g_free(il->can_destroy_cond);
242 g_mutex_free(il->data_mutex);
243 g_cond_free(il->can_destroy_cond);
248 void image_loader_free(ImageLoader *il)
251 g_object_unref(G_OBJECT(il));
255 ImageLoader *image_loader_new(FileData *fd)
259 if (!fd) return NULL;
261 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
263 il->fd = file_data_ref(fd);
268 /**************************************************************************************/
269 /* send signals via idle calbacks - the callback are executed in the main thread */
271 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
272 struct _ImageLoaderAreaParam {
281 static gboolean image_loader_emit_area_ready_cb(gpointer data)
283 ImageLoaderAreaParam *par = data;
284 ImageLoader *il = par->il;
286 g_mutex_lock(il->data_mutex);
287 il->area_param_list = g_list_remove(il->area_param_list, par);
293 g_mutex_unlock(il->data_mutex);
295 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
300 static gboolean image_loader_emit_done_cb(gpointer data)
302 ImageLoader *il = data;
303 g_signal_emit(il, signals[SIGNAL_DONE], 0);
307 static gboolean image_loader_emit_error_cb(gpointer data)
309 ImageLoader *il = data;
310 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
314 static gboolean image_loader_emit_percent_cb(gpointer data)
316 ImageLoader *il = data;
317 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
321 static gboolean image_loader_emit_size_cb(gpointer data)
324 ImageLoader *il = data;
325 g_mutex_lock(il->data_mutex);
326 width = il->actual_width;
327 height = il->actual_height;
328 g_mutex_unlock(il->data_mutex);
329 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
334 /* DONE and ERROR are emited only once, thus they can have normal priority
335 PERCENT and AREA_READY should be processed ASAP
338 static void image_loader_emit_done(ImageLoader *il)
340 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
343 static void image_loader_emit_error(ImageLoader *il)
345 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
348 static void image_loader_emit_percent(ImageLoader *il)
350 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
353 static void image_loader_emit_size(ImageLoader *il)
355 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, NULL);
358 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
362 ImageLoaderAreaParam *prev_par = (*list)->data;
363 if (prev_par->x == x && prev_par->w == w &&
364 prev_par->y + prev_par->h == y)
366 /* we can merge the notifications */
370 if (prev_par->x == x && prev_par->w == w &&
371 y + h == prev_par->y)
373 /* we can merge the notifications */
378 if (prev_par->y == y && prev_par->h == h &&
379 prev_par->x + prev_par->w == x)
381 /* we can merge the notifications */
385 if (prev_par->y == y && prev_par->h == h &&
386 x + w == prev_par->x)
388 /* we can merge the notifications */
395 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
402 *list = g_list_prepend(*list, par);
406 /* this function expects that il->data_mutex is locked by caller */
407 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
409 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
413 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
417 /**************************************************************************************/
418 /* the following functions may be executed in separate thread */
420 /* this function expects that il->data_mutex is locked by caller */
421 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
423 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
428 static gboolean image_loader_get_stopping(ImageLoader *il)
431 if (!il) return FALSE;
433 g_mutex_lock(il->data_mutex);
435 g_mutex_unlock(il->data_mutex);
441 static void image_loader_sync_pixbuf(ImageLoader *il)
445 g_mutex_lock(il->data_mutex);
449 g_mutex_unlock(il->data_mutex);
453 pb = il->backend.get_pixbuf(il->loader);
455 if (pb == il->pixbuf)
457 g_mutex_unlock(il->data_mutex);
461 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
463 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
466 if (il->pixbuf) g_object_unref(il->pixbuf);
469 if (il->pixbuf) g_object_ref(il->pixbuf);
471 g_mutex_unlock(il->data_mutex);
474 static void image_loader_area_updated_cb(gpointer loader,
475 guint x, guint y, guint w, guint h,
478 ImageLoader *il = data;
480 if (!image_loader_get_pixbuf(il))
482 image_loader_sync_pixbuf(il);
483 if (!image_loader_get_pixbuf(il))
485 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
489 g_mutex_lock(il->data_mutex);
490 if (il->delay_area_ready)
491 image_loader_queue_delayed_area_ready(il, x, y, w, h);
493 image_loader_emit_area_ready(il, x, y, w, h);
495 if (il->stopping) il->backend.abort(il->loader);
497 g_mutex_unlock(il->data_mutex);
500 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
502 ImageLoader *il = data;
508 http://bugzilla.gnome.org/show_bug.cgi?id=547669
509 http://bugzilla.gnome.org/show_bug.cgi?id=589334
511 gchar *format = il->backend.get_format_name(loader);
512 if (strcmp(format, "svg") == 0 ||
513 strcmp(format, "xpm") == 0)
521 pb = il->backend.get_pixbuf(loader);
523 h = gdk_pixbuf_get_height(pb);
524 rs = gdk_pixbuf_get_rowstride(pb);
525 pix = gdk_pixbuf_get_pixels(pb);
527 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
531 static void image_loader_size_cb(gpointer loader,
532 gint width, gint height, gpointer data)
534 ImageLoader *il = data;
536 gboolean scale = FALSE;
539 g_mutex_lock(il->data_mutex);
540 il->actual_width = width;
541 il->actual_height = height;
542 if (il->requested_width < 1 || il->requested_height < 1)
544 g_mutex_unlock(il->data_mutex);
545 image_loader_emit_size(il);
548 g_mutex_unlock(il->data_mutex);
550 #ifdef HAVE_FFMPEGTHUMBNAILER
551 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
554 mime_types = il->backend.get_format_mime_types(loader);
556 while (mime_types[n] && !scale)
558 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
561 g_strfreev(mime_types);
565 image_loader_emit_size(il);
569 g_mutex_lock(il->data_mutex);
572 if (width > il->requested_width || height > il->requested_height)
575 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
577 nw = il->requested_width;
578 nh = (gdouble)nw / width * height;
583 nh = il->requested_height;
584 nw = (gdouble)nh / height * width;
588 il->actual_width = nw;
589 il->actual_height = nh;
590 il->backend.set_size(loader, nw, nh);
594 g_mutex_unlock(il->data_mutex);
595 image_loader_emit_size(il);
598 static void image_loader_stop_loader(ImageLoader *il)
604 /* some loaders do not have a pixbuf till close, order is important here */
605 il->backend.close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
606 image_loader_sync_pixbuf(il);
607 il->backend.free(il->loader);
610 g_mutex_lock(il->data_mutex);
612 g_mutex_unlock(il->data_mutex);
615 static void image_loader_setup_loader(ImageLoader *il)
619 g_mutex_lock(il->data_mutex);
620 #ifdef HAVE_FFMPEGTHUMBNAILER
621 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
623 DEBUG_1("Using custom ffmpegthumbnailer loader");
624 image_loader_backend_set_ft(&il->backend);
629 if (il->bytes_total >= 4 &&
630 (memcmp(il->mapped_file + 0, "%PDF", 4) == 0))
632 DEBUG_1("Using custom pdf loader");
633 image_loader_backend_set_pdf(&il->backend);
638 if (il->bytes_total >= 10 &&
639 (memcmp(il->mapped_file + 4, "ftypheic", 8) == 0))
641 DEBUG_1("Using custom heif loader");
642 image_loader_backend_set_heif(&il->backend);
647 if (il->bytes_total >= 12 &&
648 (memcmp(il->mapped_file, "RIFF", 4) == 0) &&
649 (memcmp(il->mapped_file + 8, "WEBP", 4) == 0))
651 DEBUG_1("Using custom webp loader");
652 image_loader_backend_set_webp(&il->backend);
657 if (il->bytes_total >= 16 &&
658 (memcmp(il->mapped_file, "AT&TFORM", 8) == 0) &&
659 (memcmp(il->mapped_file + 12, "DJV", 3) == 0))
661 DEBUG_1("Using custom djvu loader");
662 image_loader_backend_set_djvu(&il->backend);
667 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
669 DEBUG_1("Using custom jpeg loader");
670 image_loader_backend_set_jpeg(&il->backend);
675 if (il->bytes_total >= 10 &&
676 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
677 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
678 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
679 memcmp(il->mapped_file, "II*\0", 4) == 0))
681 DEBUG_1("Using custom tiff loader");
682 image_loader_backend_set_tiff(&il->backend);
686 if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
688 DEBUG_1("Using dds loader");
689 image_loader_backend_set_dds(&il->backend);
692 if (il->bytes_total >= 6 &&
693 (memcmp(il->mapped_file, "8BPS\0\x01", 6) == 0))
695 DEBUG_1("Using custom psd loader");
696 image_loader_backend_set_psd(&il->backend);
699 if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
701 DEBUG_1("Using custom collection loader");
702 image_loader_backend_set_collection(&il->backend);
705 image_loader_backend_set_default(&il->backend);
707 il->loader = il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
710 format = il->backend.get_format_name(il->loader);
711 if (g_strcmp0(format, "tiff") == 0)
713 il->backend.set_page_num(il->loader, il->fd->page_num);
719 format = il->backend.get_format_name(il->loader);
720 if (g_strcmp0(format, "pdf") == 0)
722 il->backend.set_page_num(il->loader, il->fd->page_num);
728 format = il->backend.get_format_name(il->loader);
729 if (g_strcmp0(format, "djvu") == 0)
731 il->backend.set_page_num(il->loader, il->fd->page_num);
736 g_mutex_unlock(il->data_mutex);
740 static void image_loader_done(ImageLoader *il)
742 image_loader_stop_loader(il);
744 image_loader_emit_done(il);
747 static void image_loader_error(ImageLoader *il)
749 image_loader_stop_loader(il);
751 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
753 image_loader_emit_error(il);
756 static gboolean image_loader_continue(ImageLoader *il)
761 if (!il) return FALSE;
763 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
764 while (c > 0 && !image_loader_get_stopping(il))
766 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
770 image_loader_done(il);
774 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
776 image_loader_error(il);
785 if (il->bytes_total > 0)
787 image_loader_emit_percent(il);
793 static gboolean image_loader_begin(ImageLoader *il)
798 if (il->pixbuf) return FALSE;
800 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
801 if (b < 1) return FALSE;
803 image_loader_setup_loader(il);
805 g_assert(il->bytes_read == 0);
806 if (il->backend.load) {
808 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
810 image_loader_stop_loader(il);
814 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
816 image_loader_stop_loader(il);
821 format = il->backend.get_format_name(il->loader);
822 if (g_strcmp0(format, "pdf") == 0)
824 gint i = il->backend.get_page_total(il->loader);
825 file_data_set_page_total(il->fd, i);
830 format = il->backend.get_format_name(il->loader);
831 if (g_strcmp0(format, "djvu") == 0)
833 gint i = il->backend.get_page_total(il->loader);
834 file_data_set_page_total(il->fd, i);
839 format = il->backend.get_format_name(il->loader);
840 if (g_strcmp0(format, "tiff") == 0)
842 gint i = il->backend.get_page_total(il->loader);
843 file_data_set_page_total(il->fd, i);
850 /* read until size is known */
851 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
853 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
854 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
856 image_loader_stop_loader(il);
861 if (!il->pixbuf) image_loader_sync_pixbuf(il);
863 if (il->bytes_read == il->bytes_total || b < 1)
865 /* done, handle (broken) loaders that do not have pixbuf till close */
866 image_loader_stop_loader(il);
868 if (!il->pixbuf) return FALSE;
870 image_loader_done(il);
876 image_loader_stop_loader(il);
883 /**************************************************************************************/
884 /* the following functions are always executed in the main thread */
887 static gboolean image_loader_setup_source(ImageLoader *il)
892 if (!il || il->loader || il->mapped_file) return FALSE;
894 il->mapped_file = NULL;
898 ExifData *exif = exif_read_fd(il->fd);
900 if (options->thumbnails.use_exif)
901 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
903 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
908 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
910 exif_free_fd(il->fd, exif);
914 if (!il->mapped_file)
919 pathl = path_from_utf8(il->fd->path);
920 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
922 if (load_fd == -1) return FALSE;
924 if (fstat(load_fd, &st) == 0)
926 il->bytes_total = st.st_size;
934 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
936 if (il->mapped_file == MAP_FAILED)
947 static void image_loader_stop_source(ImageLoader *il)
955 exif_free_preview(il->mapped_file);
959 munmap(il->mapped_file, il->bytes_total);
961 il->mapped_file = NULL;
965 static void image_loader_stop(ImageLoader *il)
971 g_source_remove(il->idle_id);
977 /* stop loader in the other thread */
978 g_mutex_lock(il->data_mutex);
980 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
981 g_mutex_unlock(il->data_mutex);
984 image_loader_stop_loader(il);
985 image_loader_stop_source(il);
989 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
991 g_mutex_lock(il->data_mutex);
992 il->delay_area_ready = enable;
997 list = g_list_reverse(il->area_param_delayed_list);
998 il->area_param_delayed_list = NULL;
999 g_mutex_unlock(il->data_mutex);
1005 ImageLoaderAreaParam *par = work->data;
1008 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
1016 g_mutex_unlock(il->data_mutex);
1021 /**************************************************************************************/
1022 /* execution via idle calls */
1024 static gboolean image_loader_idle_cb(gpointer data)
1026 gboolean ret = FALSE;
1027 ImageLoader *il = data;
1031 ret = image_loader_continue(il);
1036 image_loader_stop_source(il);
1043 static gboolean image_loader_start_idle(ImageLoader *il)
1047 if (!il) return FALSE;
1049 if (!il->fd) return FALSE;
1051 if (!image_loader_setup_source(il)) return FALSE;
1053 ret = image_loader_begin(il);
1055 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
1059 /**************************************************************************************/
1060 /* execution via thread */
1063 static GThreadPool *image_loader_thread_pool = NULL;
1065 static GCond *image_loader_prio_cond = NULL;
1066 static GMutex *image_loader_prio_mutex = NULL;
1067 static gint image_loader_prio_num = 0;
1070 static void image_loader_thread_enter_high(void)
1072 g_mutex_lock(image_loader_prio_mutex);
1073 image_loader_prio_num++;
1074 g_mutex_unlock(image_loader_prio_mutex);
1077 static void image_loader_thread_leave_high(void)
1079 g_mutex_lock(image_loader_prio_mutex);
1080 image_loader_prio_num--;
1081 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
1082 g_mutex_unlock(image_loader_prio_mutex);
1085 static void image_loader_thread_wait_high(void)
1087 g_mutex_lock(image_loader_prio_mutex);
1088 while (image_loader_prio_num)
1090 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
1093 g_mutex_unlock(image_loader_prio_mutex);
1097 static void image_loader_thread_run(gpointer data, gpointer user_data)
1099 ImageLoader *il = data;
1103 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1105 /* low prio, wait untill high prio tasks finishes */
1106 image_loader_thread_wait_high();
1111 image_loader_thread_enter_high();
1114 err = !image_loader_begin(il);
1119 loader failed, we have to send signal
1120 (idle mode returns the image_loader_begin return value directly)
1121 (success is always reported indirectly from image_loader_begin)
1123 image_loader_emit_error(il);
1128 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1130 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1132 /* low prio, wait untill high prio tasks finishes */
1133 image_loader_thread_wait_high();
1135 cont = image_loader_continue(il);
1137 image_loader_stop_loader(il);
1139 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1142 image_loader_thread_leave_high();
1145 g_mutex_lock(il->data_mutex);
1146 il->can_destroy = TRUE;
1147 g_cond_signal(il->can_destroy_cond);
1148 g_mutex_unlock(il->data_mutex);
1153 static gboolean image_loader_start_thread(ImageLoader *il)
1155 if (!il) return FALSE;
1157 if (!il->fd) return FALSE;
1161 if (!image_loader_setup_source(il)) return FALSE;
1163 if (!image_loader_thread_pool)
1165 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
1166 #if GLIB_CHECK_VERSION(2,32,0)
1167 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1168 g_cond_init(image_loader_prio_cond);
1169 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1170 g_mutex_init(image_loader_prio_mutex);
1172 image_loader_prio_cond = g_cond_new();
1173 image_loader_prio_mutex = g_mutex_new();
1177 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1179 g_thread_pool_push(image_loader_thread_pool, il, NULL);
1180 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1184 #endif /* HAVE_GTHREAD */
1187 /**************************************************************************************/
1188 /* public interface */
1191 gboolean image_loader_start(ImageLoader *il)
1193 if (!il) return FALSE;
1195 if (!il->fd) return FALSE;
1198 return image_loader_start_thread(il);
1200 return image_loader_start_idle(il);
1205 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1206 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1209 if (!il) return NULL;
1211 g_mutex_lock(il->data_mutex);
1213 g_mutex_unlock(il->data_mutex);
1217 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1221 g_mutex_lock(il->data_mutex);
1222 il->requested_width = width;
1223 il->requested_height = height;
1224 g_mutex_unlock(il->data_mutex);
1227 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1231 g_mutex_lock(il->data_mutex);
1232 il->idle_read_loop_count = count ? count : 1;
1233 g_mutex_unlock(il->data_mutex);
1236 void image_loader_set_priority(ImageLoader *il, gint priority)
1240 if (il->thread) return; /* can't change prio if the thread already runs */
1241 il->idle_priority = priority;
1245 gdouble image_loader_get_percent(ImageLoader *il)
1248 if (!il) return 0.0;
1250 g_mutex_lock(il->data_mutex);
1251 if (il->bytes_total == 0)
1257 ret = (gdouble)il->bytes_read / il->bytes_total;
1259 g_mutex_unlock(il->data_mutex);
1263 gboolean image_loader_get_is_done(ImageLoader *il)
1266 if (!il) return FALSE;
1268 g_mutex_lock(il->data_mutex);
1270 g_mutex_unlock(il->data_mutex);
1275 FileData *image_loader_get_fd(ImageLoader *il)
1278 if (!il) return NULL;
1280 g_mutex_lock(il->data_mutex);
1282 g_mutex_unlock(il->data_mutex);
1287 gboolean image_loader_get_shrunk(ImageLoader *il)
1290 if (!il) return FALSE;
1292 g_mutex_lock(il->data_mutex);
1294 g_mutex_unlock(il->data_mutex);
1298 const gchar *image_loader_get_error(ImageLoader *il)
1300 const gchar *ret = NULL;
1301 if (!il) return NULL;
1302 g_mutex_lock(il->data_mutex);
1303 if (il->error) ret = il->error->message;
1304 g_mutex_unlock(il->data_mutex);
1309 /* FIXME - this can be rather slow and blocks until the size is known */
1310 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1315 il = image_loader_new(fd);
1317 success = image_loader_start_idle(il);
1319 if (success && il->pixbuf)
1321 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1322 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1326 if (width) *width = -1;
1327 if (height) *height = -1;
1330 image_loader_free(il);
1334 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */