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)
616 g_mutex_lock(il->data_mutex);
617 #ifdef HAVE_FFMPEGTHUMBNAILER
618 if (il->fd->format_class == FORMAT_CLASS_VIDEO)
620 DEBUG_1("Using custom ffmpegthumbnailer loader");
621 image_loader_backend_set_ft(&il->backend);
626 if (il->fd->format_class == FORMAT_CLASS_PDF)
628 DEBUG_1("Using custom pdf loader");
629 image_loader_backend_set_pdf(&il->backend);
634 if (il->bytes_total >= 10 &&
635 (memcmp(il->mapped_file + 4, "ftypheic", 8) == 0))
637 DEBUG_1("Using custom heif loader");
638 image_loader_backend_set_heif(&il->backend);
643 if (il->bytes_total >= 12 &&
644 (memcmp(il->mapped_file, "RIFF", 4) == 0) &&
645 (memcmp(il->mapped_file + 8, "WEBP", 4) == 0))
647 DEBUG_1("Using custom webp loader");
648 image_loader_backend_set_webp(&il->backend);
653 if (il->bytes_total >= 16 &&
654 (memcmp(il->mapped_file + 12, "DJV", 3) == 0))
656 DEBUG_1("Using custom djvu loader");
657 image_loader_backend_set_djvu(&il->backend);
662 if (il->bytes_total >= 2 && il->mapped_file[0] == 0xff && il->mapped_file[1] == 0xd8)
664 DEBUG_1("Using custom jpeg loader");
665 image_loader_backend_set_jpeg(&il->backend);
670 if (il->bytes_total >= 10 &&
671 (memcmp(il->mapped_file, "MM\0*", 4) == 0 ||
672 memcmp(il->mapped_file, "MM\0+\0\x08\0\0", 8) == 0 ||
673 memcmp(il->mapped_file, "II+\0\x08\0\0\0", 8) == 0 ||
674 memcmp(il->mapped_file, "II*\0", 4) == 0))
676 DEBUG_1("Using custom tiff loader");
677 image_loader_backend_set_tiff(&il->backend);
681 if (il->bytes_total >= 3 && il->mapped_file[0] == 0x44 && il->mapped_file[1] == 0x44 && il->mapped_file[2] == 0x53)
683 DEBUG_1("Using dds loader");
684 image_loader_backend_set_dds(&il->backend);
687 if (il->fd->format_class == FORMAT_CLASS_COLLECTION)
689 DEBUG_1("Using custom collection loader");
690 image_loader_backend_set_collection(&il->backend);
693 image_loader_backend_set_default(&il->backend);
695 il->loader = il->backend.loader_new(image_loader_area_updated_cb, image_loader_size_cb, image_loader_area_prepared_cb, il);
698 if (il->fd->format_class == FORMAT_CLASS_PDF)
700 il->backend.set_page_num(il->loader, il->fd->page_num);
705 if (g_strcmp0(il->fd->extension, ".djvu") == 0)
707 il->backend.set_page_num(il->loader, il->fd->page_num);
711 g_mutex_unlock(il->data_mutex);
715 static void image_loader_done(ImageLoader *il)
717 image_loader_stop_loader(il);
719 image_loader_emit_done(il);
722 static void image_loader_error(ImageLoader *il)
724 image_loader_stop_loader(il);
726 DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
728 image_loader_emit_error(il);
731 static gboolean image_loader_continue(ImageLoader *il)
736 if (!il) return FALSE;
738 c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
739 while (c > 0 && !image_loader_get_stopping(il))
741 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
745 image_loader_done(il);
749 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
751 image_loader_error(il);
760 if (il->bytes_total > 0)
762 image_loader_emit_percent(il);
768 static gboolean image_loader_begin(ImageLoader *il)
772 if (il->pixbuf) return FALSE;
774 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
775 if (b < 1) return FALSE;
777 image_loader_setup_loader(il);
779 g_assert(il->bytes_read == 0);
780 if (il->backend.load) {
782 if (!il->backend.load(il->loader, il->mapped_file, b, &il->error))
784 image_loader_stop_loader(il);
788 else if (!il->backend.write(il->loader, il->mapped_file, b, &il->error))
790 image_loader_stop_loader(il);
795 if (il->fd->format_class == FORMAT_CLASS_PDF)
797 gint i = il->backend.get_page_total(il->loader);
798 file_data_set_page_total(il->fd, i);
802 if (g_strcmp0(il->fd->extension, ".djvu") == 0)
804 gint i = il->backend.get_page_total(il->loader);
805 file_data_set_page_total(il->fd, i);
811 /* read until size is known */
812 while (il->loader && !il->backend.get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
814 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
815 if (b < 0 || (b > 0 && !il->backend.write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
817 image_loader_stop_loader(il);
822 if (!il->pixbuf) image_loader_sync_pixbuf(il);
824 if (il->bytes_read == il->bytes_total || b < 1)
826 /* done, handle (broken) loaders that do not have pixbuf till close */
827 image_loader_stop_loader(il);
829 if (!il->pixbuf) return FALSE;
831 image_loader_done(il);
837 image_loader_stop_loader(il);
844 /**************************************************************************************/
845 /* the following functions are always executed in the main thread */
848 static gboolean image_loader_setup_source(ImageLoader *il)
853 if (!il || il->loader || il->mapped_file) return FALSE;
855 il->mapped_file = NULL;
859 ExifData *exif = exif_read_fd(il->fd);
861 if (options->thumbnails.use_exif)
862 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, il->requested_width, il->requested_height);
864 il->mapped_file = exif_get_preview(exif, (guint *)&il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
869 DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
871 exif_free_fd(il->fd, exif);
875 if (!il->mapped_file)
880 pathl = path_from_utf8(il->fd->path);
881 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
883 if (load_fd == -1) return FALSE;
885 if (fstat(load_fd, &st) == 0)
887 il->bytes_total = st.st_size;
895 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
897 if (il->mapped_file == MAP_FAILED)
908 static void image_loader_stop_source(ImageLoader *il)
916 exif_free_preview(il->mapped_file);
920 munmap(il->mapped_file, il->bytes_total);
922 il->mapped_file = NULL;
926 static void image_loader_stop(ImageLoader *il)
932 g_source_remove(il->idle_id);
938 /* stop loader in the other thread */
939 g_mutex_lock(il->data_mutex);
941 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
942 g_mutex_unlock(il->data_mutex);
945 image_loader_stop_loader(il);
946 image_loader_stop_source(il);
950 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
952 g_mutex_lock(il->data_mutex);
953 il->delay_area_ready = enable;
958 list = g_list_reverse(il->area_param_delayed_list);
959 il->area_param_delayed_list = NULL;
960 g_mutex_unlock(il->data_mutex);
966 ImageLoaderAreaParam *par = work->data;
969 g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
977 g_mutex_unlock(il->data_mutex);
982 /**************************************************************************************/
983 /* execution via idle calls */
985 static gboolean image_loader_idle_cb(gpointer data)
987 gboolean ret = FALSE;
988 ImageLoader *il = data;
992 ret = image_loader_continue(il);
997 image_loader_stop_source(il);
1004 static gboolean image_loader_start_idle(ImageLoader *il)
1008 if (!il) return FALSE;
1010 if (!il->fd) return FALSE;
1012 if (!image_loader_setup_source(il)) return FALSE;
1014 ret = image_loader_begin(il);
1016 if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
1020 /**************************************************************************************/
1021 /* execution via thread */
1024 static GThreadPool *image_loader_thread_pool = NULL;
1026 static GCond *image_loader_prio_cond = NULL;
1027 static GMutex *image_loader_prio_mutex = NULL;
1028 static gint image_loader_prio_num = 0;
1031 static void image_loader_thread_enter_high(void)
1033 g_mutex_lock(image_loader_prio_mutex);
1034 image_loader_prio_num++;
1035 g_mutex_unlock(image_loader_prio_mutex);
1038 static void image_loader_thread_leave_high(void)
1040 g_mutex_lock(image_loader_prio_mutex);
1041 image_loader_prio_num--;
1042 if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
1043 g_mutex_unlock(image_loader_prio_mutex);
1046 static void image_loader_thread_wait_high(void)
1048 g_mutex_lock(image_loader_prio_mutex);
1049 while (image_loader_prio_num)
1051 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
1054 g_mutex_unlock(image_loader_prio_mutex);
1058 static void image_loader_thread_run(gpointer data, gpointer user_data)
1060 ImageLoader *il = data;
1064 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1066 /* low prio, wait untill high prio tasks finishes */
1067 image_loader_thread_wait_high();
1072 image_loader_thread_enter_high();
1075 err = !image_loader_begin(il);
1080 loader failed, we have to send signal
1081 (idle mode returns the image_loader_begin return value directly)
1082 (success is always reported indirectly from image_loader_begin)
1084 image_loader_emit_error(il);
1089 while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
1091 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE)
1093 /* low prio, wait untill high prio tasks finishes */
1094 image_loader_thread_wait_high();
1096 cont = image_loader_continue(il);
1098 image_loader_stop_loader(il);
1100 if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE)
1103 image_loader_thread_leave_high();
1106 g_mutex_lock(il->data_mutex);
1107 il->can_destroy = TRUE;
1108 g_cond_signal(il->can_destroy_cond);
1109 g_mutex_unlock(il->data_mutex);
1114 static gboolean image_loader_start_thread(ImageLoader *il)
1116 if (!il) return FALSE;
1118 if (!il->fd) return FALSE;
1122 if (!image_loader_setup_source(il)) return FALSE;
1124 if (!image_loader_thread_pool)
1126 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
1127 #if GLIB_CHECK_VERSION(2,32,0)
1128 if (!image_loader_prio_cond) image_loader_prio_cond = g_new(GCond, 1);
1129 g_cond_init(image_loader_prio_cond);
1130 if (!image_loader_prio_mutex) image_loader_prio_mutex = g_new(GMutex, 1);
1131 g_mutex_init(image_loader_prio_mutex);
1133 image_loader_prio_cond = g_cond_new();
1134 image_loader_prio_mutex = g_mutex_new();
1138 il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
1140 g_thread_pool_push(image_loader_thread_pool, il, NULL);
1141 DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
1145 #endif /* HAVE_GTHREAD */
1148 /**************************************************************************************/
1149 /* public interface */
1152 gboolean image_loader_start(ImageLoader *il)
1154 if (!il) return FALSE;
1156 if (!il->fd) return FALSE;
1159 return image_loader_start_thread(il);
1161 return image_loader_start_idle(il);
1166 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
1167 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
1170 if (!il) return NULL;
1172 g_mutex_lock(il->data_mutex);
1174 g_mutex_unlock(il->data_mutex);
1178 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
1182 g_mutex_lock(il->data_mutex);
1183 il->requested_width = width;
1184 il->requested_height = height;
1185 g_mutex_unlock(il->data_mutex);
1188 void image_loader_set_buffer_size(ImageLoader *il, guint count)
1192 g_mutex_lock(il->data_mutex);
1193 il->idle_read_loop_count = count ? count : 1;
1194 g_mutex_unlock(il->data_mutex);
1197 void image_loader_set_priority(ImageLoader *il, gint priority)
1201 if (il->thread) return; /* can't change prio if the thread already runs */
1202 il->idle_priority = priority;
1206 gdouble image_loader_get_percent(ImageLoader *il)
1209 if (!il) return 0.0;
1211 g_mutex_lock(il->data_mutex);
1212 if (il->bytes_total == 0)
1218 ret = (gdouble)il->bytes_read / il->bytes_total;
1220 g_mutex_unlock(il->data_mutex);
1224 gboolean image_loader_get_is_done(ImageLoader *il)
1227 if (!il) return FALSE;
1229 g_mutex_lock(il->data_mutex);
1231 g_mutex_unlock(il->data_mutex);
1236 FileData *image_loader_get_fd(ImageLoader *il)
1239 if (!il) return NULL;
1241 g_mutex_lock(il->data_mutex);
1243 g_mutex_unlock(il->data_mutex);
1248 gboolean image_loader_get_shrunk(ImageLoader *il)
1251 if (!il) return FALSE;
1253 g_mutex_lock(il->data_mutex);
1255 g_mutex_unlock(il->data_mutex);
1259 const gchar *image_loader_get_error(ImageLoader *il)
1261 const gchar *ret = NULL;
1262 if (!il) return NULL;
1263 g_mutex_lock(il->data_mutex);
1264 if (il->error) ret = il->error->message;
1265 g_mutex_unlock(il->data_mutex);
1270 /* FIXME - this can be rather slow and blocks until the size is known */
1271 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1276 il = image_loader_new(fd);
1278 success = image_loader_start_idle(il);
1280 if (success && il->pixbuf)
1282 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1283 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1287 if (width) *width = -1;
1288 if (height) *height = -1;
1291 image_loader_free(il);
1295 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */