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_heif.h"
31 #include "image_load_ffmpegthumbnailer.h"
32 #include "image_load_collection.h"
33 #include "image_load_webp.h"
37 #include "ui_fileops.h"
38 #include "gq-marshal.h"
43 #define IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT 4096
44 #define IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT 1
47 /**************************************************************************************/
48 /* image loader class */
52 SIGNAL_AREA_READY = 0,
60 static guint signals[SIGNAL_COUNT] = { 0 };
62 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
63 static void image_loader_class_init(ImageLoaderClass *class);
64 static void image_loader_finalize(GObject *object);
65 static void image_loader_stop(ImageLoader *il);
67 GType image_loader_get_type(void)
69 static GType type = 0;
72 static const GTypeInfo info = {
73 sizeof(ImageLoaderClass),
75 NULL, /* base_finalize */
76 (GClassInitFunc)image_loader_class_init, /* class_init */
77 NULL, /* class_finalize */
78 NULL, /* class_data */
81 (GInstanceInitFunc)image_loader_init, /* instance_init */
82 NULL /* value_table */
84 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
89 static void image_loader_init(GTypeInstance *instance, gpointer g_class)
91 ImageLoader *il = (ImageLoader *)instance;
95 il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
102 il->idle_done_id = 0;
104 il->idle_read_loop_count = IMAGE_LOADER_IDLE_READ_LOOP_COUNT_DEFAULT;
105 il->read_buffer_size = IMAGE_LOADER_READ_BUFFER_SIZE_DEFAULT;
106 il->mapped_file = NULL;
108 il->requested_width = 0;
109 il->requested_height = 0;
110 il->actual_width = 0;
111 il->actual_height = 0;
114 il->can_destroy = TRUE;
117 #if GLIB_CHECK_VERSION(2,32,0)
118 il->data_mutex = g_new(GMutex, 1);
119 g_mutex_init(il->data_mutex);
120 il->can_destroy_cond = g_new(GCond, 1);
121 g_cond_init(il->can_destroy_cond);
123 il->data_mutex = g_mutex_new();
124 il->can_destroy_cond = g_cond_new();
127 DEBUG_1("new image loader %p, bufsize=%" G_GSIZE_FORMAT " idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
130 static void image_loader_class_init(ImageLoaderClass *class)
132 GObjectClass *gobject_class = G_OBJECT_CLASS (class);
134 // gobject_class->set_property = image_loader_set_property;
135 // gobject_class->get_property = image_loader_get_property;
137 gobject_class->finalize = image_loader_finalize;
140 signals[SIGNAL_AREA_READY] =
141 g_signal_new("area_ready",
142 G_OBJECT_CLASS_TYPE(gobject_class),
144 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
146 gq_marshal_VOID__INT_INT_INT_INT,
153 signals[SIGNAL_ERROR] =
154 g_signal_new("error",
155 G_OBJECT_CLASS_TYPE(gobject_class),
157 G_STRUCT_OFFSET(ImageLoaderClass, error),
159 g_cclosure_marshal_VOID__VOID,
162 signals[SIGNAL_DONE] =
164 G_OBJECT_CLASS_TYPE(gobject_class),
166 G_STRUCT_OFFSET(ImageLoaderClass, done),
168 g_cclosure_marshal_VOID__VOID,
171 signals[SIGNAL_PERCENT] =
172 g_signal_new("percent",
173 G_OBJECT_CLASS_TYPE(gobject_class),
175 G_STRUCT_OFFSET(ImageLoaderClass, percent),
177 g_cclosure_marshal_VOID__DOUBLE,
181 signals[SIGNAL_SIZE] =
182 g_signal_new("size_prepared",
183 G_OBJECT_CLASS_TYPE(gobject_class),
185 G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
187 gq_marshal_VOID__INT_INT,
194 static void image_loader_finalize(GObject *object)
196 ImageLoader *il = (ImageLoader *)object;
198 image_loader_stop(il);
200 if (il->error) DEBUG_1("%s", image_loader_get_error(il));
202 DEBUG_1("freeing image loader %p bytes_read=%" G_GSIZE_FORMAT, il, il->bytes_read);
204 if (il->idle_done_id)
206 g_source_remove(il->idle_done_id);
207 il->idle_done_id = 0;
210 while (g_source_remove_by_user_data(il))
212 DEBUG_2("pending signals detected");
215 while (il->area_param_list)
217 DEBUG_1("pending area_ready signals detected");
218 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
219 g_free(il->area_param_list->data);
220 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
223 while (il->area_param_delayed_list)
225 g_free(il->area_param_delayed_list->data);
226 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
229 if (il->pixbuf) g_object_unref(il->pixbuf);
231 if (il->error) g_error_free(il->error);
233 file_data_unref(il->fd);
235 #if GLIB_CHECK_VERSION(2,32,0)
236 g_mutex_clear(il->data_mutex);
237 g_free(il->data_mutex);
238 g_cond_clear(il->can_destroy_cond);
239 g_free(il->can_destroy_cond);
241 g_mutex_free(il->data_mutex);
242 g_cond_free(il->can_destroy_cond);
247 void image_loader_free(ImageLoader *il)
250 g_object_unref(G_OBJECT(il));
254 ImageLoader *image_loader_new(FileData *fd)
258 if (!fd) return NULL;
260 il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
262 il->fd = file_data_ref(fd);
267 /**************************************************************************************/
268 /* send signals via idle calbacks - the callback are executed in the main thread */
270 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
271 struct _ImageLoaderAreaParam {
280 static gboolean image_loader_emit_area_ready_cb(gpointer data)
282 ImageLoaderAreaParam *par = data;
283 ImageLoader *il = par->il;
285 g_mutex_lock(il->data_mutex);
286 il->area_param_list = g_list_remove(il->area_param_list, par);
292 g_mutex_unlock(il->data_mutex);
294 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, x, y, w, h);
299 static gboolean image_loader_emit_done_cb(gpointer data)
301 ImageLoader *il = data;
302 g_signal_emit(il, signals[SIGNAL_DONE], 0);
306 static gboolean image_loader_emit_error_cb(gpointer data)
308 ImageLoader *il = data;
309 g_signal_emit(il, signals[SIGNAL_ERROR], 0);
313 static gboolean image_loader_emit_percent_cb(gpointer data)
315 ImageLoader *il = data;
316 g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
320 static gboolean image_loader_emit_size_cb(gpointer data)
323 ImageLoader *il = data;
324 g_mutex_lock(il->data_mutex);
325 width = il->actual_width;
326 height = il->actual_height;
327 g_mutex_unlock(il->data_mutex);
328 g_signal_emit(il, signals[SIGNAL_SIZE], 0, width, height);
333 /* DONE and ERROR are emited only once, thus they can have normal priority
334 PERCENT and AREA_READY should be processed ASAP
337 static void image_loader_emit_done(ImageLoader *il)
339 g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
342 static void image_loader_emit_error(ImageLoader *il)
344 g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
347 static void image_loader_emit_percent(ImageLoader *il)
349 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
352 static void image_loader_emit_size(ImageLoader *il)
354 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_size_cb, il, NULL);
357 static ImageLoaderAreaParam *image_loader_queue_area_ready(ImageLoader *il, GList **list, guint x, guint y, guint w, guint h)
361 ImageLoaderAreaParam *prev_par = (*list)->data;
362 if (prev_par->x == x && prev_par->w == w &&
363 prev_par->y + prev_par->h == y)
365 /* we can merge the notifications */
369 if (prev_par->x == x && prev_par->w == w &&
370 y + h == prev_par->y)
372 /* we can merge the notifications */
377 if (prev_par->y == y && prev_par->h == h &&
378 prev_par->x + prev_par->w == x)
380 /* we can merge the notifications */
384 if (prev_par->y == y && prev_par->h == h &&
385 x + w == prev_par->x)
387 /* we can merge the notifications */
394 ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
401 *list = g_list_prepend(*list, par);
405 /* this function expects that il->data_mutex is locked by caller */
406 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
408 ImageLoaderAreaParam *par = image_loader_queue_area_ready(il, &il->area_param_list, x, y, w, h);
412 g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
416 /**************************************************************************************/
417 /* the following functions may be executed in separate thread */
419 /* this function expects that il->data_mutex is locked by caller */
420 static void image_loader_queue_delayed_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
422 image_loader_queue_area_ready(il, &il->area_param_delayed_list, x, y, w, h);
427 static gboolean image_loader_get_stopping(ImageLoader *il)
430 if (!il) return FALSE;
432 g_mutex_lock(il->data_mutex);
434 g_mutex_unlock(il->data_mutex);
440 static void image_loader_sync_pixbuf(ImageLoader *il)
444 g_mutex_lock(il->data_mutex);
448 g_mutex_unlock(il->data_mutex);
452 pb = il->backend.get_pixbuf(il->loader);
454 if (pb == il->pixbuf)
456 g_mutex_unlock(il->data_mutex);
460 if (g_ascii_strcasecmp(".jps", il->fd->extension) == 0)
462 g_object_set_data(G_OBJECT(pb), "stereo_data", GINT_TO_POINTER(STEREO_PIXBUF_CROSS));
465 if (il->pixbuf) g_object_unref(il->pixbuf);
468 if (il->pixbuf) g_object_ref(il->pixbuf);
470 g_mutex_unlock(il->data_mutex);
473 static void image_loader_area_updated_cb(gpointer loader,
474 guint x, guint y, guint w, guint h,
477 ImageLoader *il = data;
479 if (!image_loader_get_pixbuf(il))
481 image_loader_sync_pixbuf(il);
482 if (!image_loader_get_pixbuf(il))
484 log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
488 g_mutex_lock(il->data_mutex);
489 if (il->delay_area_ready)
490 image_loader_queue_delayed_area_ready(il, x, y, w, h);
492 image_loader_emit_area_ready(il, x, y, w, h);
494 if (il->stopping) il->backend.abort(il->loader);
496 g_mutex_unlock(il->data_mutex);
499 static void image_loader_area_prepared_cb(gpointer loader, gpointer data)
501 ImageLoader *il = data;
507 http://bugzilla.gnome.org/show_bug.cgi?id=547669
508 http://bugzilla.gnome.org/show_bug.cgi?id=589334
510 gchar *format = il->backend.get_format_name(loader);
511 if (strcmp(format, "svg") == 0 ||
512 strcmp(format, "xpm") == 0)
520 pb = il->backend.get_pixbuf(loader);
522 h = gdk_pixbuf_get_height(pb);
523 rs = gdk_pixbuf_get_rowstride(pb);
524 pix = gdk_pixbuf_get_pixels(pb);
526 memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
530 static void image_loader_size_cb(gpointer loader,
531 gint width, gint height, gpointer data)
533 ImageLoader *il = data;
535 gboolean scale = FALSE;
538 g_mutex_lock(il->data_mutex);
539 il->actual_width = width;
540 il->actual_height = height;
541 if (il->requested_width < 1 || il->requested_height < 1)
543 g_mutex_unlock(il->data_mutex);
544 image_loader_emit_size(il);
547 g_mutex_unlock(il->data_mutex);
549 #ifdef HAVE_FFMPEGTHUMBNAILER
550 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
553 mime_types = il->backend.get_format_mime_types(loader);
555 while (mime_types[n] && !scale)
557 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
560 g_strfreev(mime_types);
564 image_loader_emit_size(il);
568 g_mutex_lock(il->data_mutex);
571 if (width > il->requested_width || height > il->requested_height)
574 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
576 nw = il->requested_width;
577 nh = (gdouble)nw / width * height;
582 nh = il->requested_height;
583 nw = (gdouble)nh / height * width;
587 il->actual_width = nw;
588 il->actual_height = nh;
589 il->backend.set_size(loader, nw, nh);
593 g_mutex_unlock(il->data_mutex);
594 image_loader_emit_size(il);
597 static void image_loader_stop_loader(ImageLoader *il)
603 /* some loaders do not have a pixbuf till close, order is important here */
604 il->backend.close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
605 image_loader_sync_pixbuf(il);
606 il->backend.free(il->loader);
609 g_mutex_lock(il->data_mutex);
611 g_mutex_unlock(il->data_mutex);
614 static void image_loader_setup_loader(ImageLoader *il)
618 g_mutex_lock(il->data_mutex);
619 #ifdef HAVE_FFMPEGTHUMBNAILER
620 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
622 DEBUG_1("Using custom ffmpegthumbnailer loader");
623 image_loader_backend_set_ft(&il->backend);
628 if (il->bytes_total >= 4 &&
629 (memcmp(il->mapped_file + 0, "%PDF", 4) == 0))
631 DEBUG_1("Using custom pdf loader");
632 image_loader_backend_set_pdf(&il->backend);
637 if (il->bytes_total >= 10 &&
638 (memcmp(il->mapped_file + 4, "ftypheic", 8) == 0))
640 DEBUG_1("Using custom heif loader");
641 image_loader_backend_set_heif(&il->backend);
646 if (il->bytes_total >= 12 &&
647 (memcmp(il->mapped_file, "RIFF", 4) == 0) &&
648 (memcmp(il->mapped_file + 8, "WEBP", 4) == 0))
650 DEBUG_1("Using custom webp loader");
651 image_loader_backend_set_webp(&il->backend);
656 if (il->bytes_total >= 16 &&
657 (memcmp(il->mapped_file, "AT&TFORM", 8) == 0) &&
658 (memcmp(il->mapped_file + 12, "DJV", 3) == 0))
660 DEBUG_1("Using custom djvu loader");
661 image_loader_backend_set_djvu(&il->backend);
666 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
668 DEBUG_1("Using custom jpeg loader");
669 image_loader_backend_set_jpeg(&il->backend);
674 if (il->bytes_total >= 10 &&
675 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
676 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
677 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
678 memcmp(il->mapped_file, "II*\0", 4) == 0))
680 DEBUG_1("Using custom tiff loader");
681 image_loader_backend_set_tiff(&il->backend);
685 if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
687 DEBUG_1("Using dds loader");
688 image_loader_backend_set_dds(&il->backend);
691 if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
693 DEBUG_1("Using custom collection loader");
694 image_loader_backend_set_collection(&il->backend);
697 image_loader_backend_set_default(&il->backend);
699 il->loader = il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
702 format = il->backend.get_format_name(il->loader);
703 if (g_strcmp0(format, "tiff") == 0)
705 il->backend.set_page_num(il->loader, il->fd->page_num);
711 format = il->backend.get_format_name(il->loader);
712 if (g_strcmp0(format, "pdf") == 0)
714 il->backend.set_page_num(il->loader, il->fd->page_num);
720 format = il->backend.get_format_name(il->loader);
721 if (g_strcmp0(format, "djvu") == 0)
723 il->backend.set_page_num(il->loader, il->fd->page_num);
728 g_mutex_unlock(il->data_mutex);
732 static void image_loader_done(ImageLoader *il)
734 image_loader_stop_loader(il);
736 image_loader_emit_done(il);
739 static void image_loader_error(ImageLoader *il)
741 image_loader_stop_loader(il);
743 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
745 image_loader_emit_error(il);
748 static gboolean image_loader_continue(ImageLoader *il)
753 if (!il) return FALSE;
755 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
756 while (c > 0 && !image_loader_get_stopping(il))
758 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
762 image_loader_done(il);
766 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
768 image_loader_error(il);
777 if (il->bytes_total > 0)
779 image_loader_emit_percent(il);
785 static gboolean image_loader_begin(ImageLoader *il)
790 if (il->pixbuf) return FALSE;
792 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
793 if (b < 1) return FALSE;
795 image_loader_setup_loader(il);
797 g_assert(il->bytes_read == 0);
798 if (il->backend.load) {
800 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
802 image_loader_stop_loader(il);
806 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
808 image_loader_stop_loader(il);
813 format = il->backend.get_format_name(il->loader);
814 if (g_strcmp0(format, "pdf") == 0)
816 gint i = il->backend.get_page_total(il->loader);
817 file_data_set_page_total(il->fd, i);
822 format = il->backend.get_format_name(il->loader);
823 if (g_strcmp0(format, "djvu") == 0)
825 gint i = il->backend.get_page_total(il->loader);
826 file_data_set_page_total(il->fd, i);
831 format = il->backend.get_format_name(il->loader);
832 if (g_strcmp0(format, "tiff") == 0)
834 gint i = il->backend.get_page_total(il->loader);
835 file_data_set_page_total(il->fd, i);
842 /* read until size is known */
843 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
845 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
846 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
848 image_loader_stop_loader(il);
853 if (!il->pixbuf) image_loader_sync_pixbuf(il);
855 if (il->bytes_read == il->bytes_total || b < 1)
857 /* done, handle (broken) loaders that do not have pixbuf till close */
858 image_loader_stop_loader(il);
860 if (!il->pixbuf) return FALSE;
862 image_loader_done(il);
868 image_loader_stop_loader(il);
875 /**************************************************************************************/
876 /* the following functions are always executed in the main thread */
879 static gboolean image_loader_setup_source(ImageLoader *il)
884 if (!il || il->loader || il->mapped_file) return FALSE;
886 il->mapped_file = NULL;
890 ExifData *exif = exif_read_fd(il->fd);
892 if (options->thumbnails.use_exif)
893 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
895 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
900 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
902 exif_free_fd(il->fd, exif);
906 if (!il->mapped_file)
911 pathl = path_from_utf8(il->fd->path);
912 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
914 if (load_fd == -1) return FALSE;
916 if (fstat(load_fd, &st) == 0)
918 il->bytes_total = st.st_size;
926 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
928 if (il->mapped_file == MAP_FAILED)
939 static void image_loader_stop_source(ImageLoader *il)
947 exif_free_preview(il->mapped_file);
951 munmap(il->mapped_file, il->bytes_total);
953 il->mapped_file = NULL;
957 static void image_loader_stop(ImageLoader *il)
963 g_source_remove(il->idle_id);
969 /* stop loader in the other thread */
970 g_mutex_lock(il->data_mutex);
972 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
973 g_mutex_unlock(il->data_mutex);
976 image_loader_stop_loader(il);
977 image_loader_stop_source(il);
981 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
983 g_mutex_lock(il->data_mutex);
984 il->delay_area_ready = enable;
989 list = g_list_reverse(il->area_param_delayed_list);
990 il->area_param_delayed_list = NULL;
991 g_mutex_unlock(il->data_mutex);
997 ImageLoaderAreaParam *par = work->data;
1000 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
1008 g_mutex_unlock(il->data_mutex);
1013 /**************************************************************************************/
1014 /* execution via idle calls */
1016 static gboolean image_loader_idle_cb(gpointer data)
1018 gboolean ret = FALSE;
1019 ImageLoader *il = data;
1023 ret = image_loader_continue(il);
1028 image_loader_stop_source(il);
1035 static gboolean image_loader_start_idle(ImageLoader *il)
1039 if (!il) return FALSE;
1041 if (!il->fd) return FALSE;
1043 if (!image_loader_setup_source(il)) return FALSE;
1045 ret = image_loader_begin(il);
1047 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
1051 /**************************************************************************************/
1052 /* execution via thread */
1055 static GThreadPool *image_loader_thread_pool = NULL;
1057 static GCond *image_loader_prio_cond = NULL;
1058 static GMutex *image_loader_prio_mutex = NULL;
1059 static gint image_loader_prio_num = 0;
1062 static void image_loader_thread_enter_high(void)
1064 g_mutex_lock(image_loader_prio_mutex);
1065 image_loader_prio_num++;
1066 g_mutex_unlock(image_loader_prio_mutex);
1069 static void image_loader_thread_leave_high(void)
1071 g_mutex_lock(image_loader_prio_mutex);
1072 image_loader_prio_num--;
1073 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
1074 g_mutex_unlock(image_loader_prio_mutex);
1077 static void image_loader_thread_wait_high(void)
1079 g_mutex_lock(image_loader_prio_mutex);
1080 while (image_loader_prio_num)
1082 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
1085 g_mutex_unlock(image_loader_prio_mutex);
1089 static void image_loader_thread_run(gpointer data, gpointer user_data)
1091 ImageLoader *il = data;
1095 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1097 /* low prio, wait untill high prio tasks finishes */
1098 image_loader_thread_wait_high();
1103 image_loader_thread_enter_high();
1106 err = !image_loader_begin(il);
1111 loader failed, we have to send signal
1112 (idle mode returns the image_loader_begin return value directly)
1113 (success is always reported indirectly from image_loader_begin)
1115 image_loader_emit_error(il);
1120 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1122 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1124 /* low prio, wait untill high prio tasks finishes */
1125 image_loader_thread_wait_high();
1127 cont = image_loader_continue(il);
1129 image_loader_stop_loader(il);
1131 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1134 image_loader_thread_leave_high();
1137 g_mutex_lock(il->data_mutex);
1138 il->can_destroy = TRUE;
1139 g_cond_signal(il->can_destroy_cond);
1140 g_mutex_unlock(il->data_mutex);
1145 static gboolean image_loader_start_thread(ImageLoader *il)
1147 if (!il) return FALSE;
1149 if (!il->fd) return FALSE;
1153 if (!image_loader_setup_source(il)) return FALSE;
1155 if (!image_loader_thread_pool)
1157 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
1158 #if GLIB_CHECK_VERSION(2,32,0)
1159 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1160 g_cond_init(image_loader_prio_cond);
1161 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1162 g_mutex_init(image_loader_prio_mutex);
1164 image_loader_prio_cond = g_cond_new();
1165 image_loader_prio_mutex = g_mutex_new();
1169 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1171 g_thread_pool_push(image_loader_thread_pool, il, NULL);
1172 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1176 #endif /* HAVE_GTHREAD */
1179 /**************************************************************************************/
1180 /* public interface */
1183 gboolean image_loader_start(ImageLoader *il)
1185 if (!il) return FALSE;
1187 if (!il->fd) return FALSE;
1190 return image_loader_start_thread(il);
1192 return image_loader_start_idle(il);
1197 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1198 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1201 if (!il) return NULL;
1203 g_mutex_lock(il->data_mutex);
1205 g_mutex_unlock(il->data_mutex);
1209 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1213 g_mutex_lock(il->data_mutex);
1214 il->requested_width = width;
1215 il->requested_height = height;
1216 g_mutex_unlock(il->data_mutex);
1219 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1223 g_mutex_lock(il->data_mutex);
1224 il->idle_read_loop_count = count ? count : 1;
1225 g_mutex_unlock(il->data_mutex);
1228 void image_loader_set_priority(ImageLoader *il, gint priority)
1232 if (il->thread) return; /* can't change prio if the thread already runs */
1233 il->idle_priority = priority;
1237 gdouble image_loader_get_percent(ImageLoader *il)
1240 if (!il) return 0.0;
1242 g_mutex_lock(il->data_mutex);
1243 if (il->bytes_total == 0)
1249 ret = (gdouble)il->bytes_read / il->bytes_total;
1251 g_mutex_unlock(il->data_mutex);
1255 gboolean image_loader_get_is_done(ImageLoader *il)
1258 if (!il) return FALSE;
1260 g_mutex_lock(il->data_mutex);
1262 g_mutex_unlock(il->data_mutex);
1267 FileData *image_loader_get_fd(ImageLoader *il)
1270 if (!il) return NULL;
1272 g_mutex_lock(il->data_mutex);
1274 g_mutex_unlock(il->data_mutex);
1279 gboolean image_loader_get_shrunk(ImageLoader *il)
1282 if (!il) return FALSE;
1284 g_mutex_lock(il->data_mutex);
1286 g_mutex_unlock(il->data_mutex);
1290 const gchar *image_loader_get_error(ImageLoader *il)
1292 const gchar *ret = NULL;
1293 if (!il) return NULL;
1294 g_mutex_lock(il->data_mutex);
1295 if (il->error) ret = il->error->message;
1296 g_mutex_unlock(il->data_mutex);
1301 /* FIXME - this can be rather slow and blocks until the size is known */
1302 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1307 il = image_loader_new(fd);
1309 success = image_loader_start_idle(il);
1311 if (success && il->pixbuf)
1313 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1314 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1318 if (width) *width = -1;
1319 if (height) *height = -1;
1322 image_loader_free(il);
1326 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */