improved debug messages
[geeqie.git] / src / image-load.c
1 /*
2  * Geeqie
3  * (C) 2004 John Ellis
4  * Copyright (C) 2008 - 2009 The Geeqie Team
5  *
6  * Author: John Ellis
7  *
8  * This software is released under the GNU General Public License (GNU GPL).
9  * Please read the included file COPYING for more information.
10  * This software comes with no warranty of any kind, use at your own risk!
11  */
12
13
14 #include "main.h"
15 #include "image-load.h"
16
17 #include "exif.h"
18 #include "filedata.h"
19 #include "ui_fileops.h"
20 #include "gq-marshal.h"
21
22 #include <fcntl.h>
23 #include <sys/mman.h>
24
25
26 /**************************************************************************************/
27 /* image loader class */
28
29
30 enum {
31         SIGNAL_AREA_READY = 0,
32         SIGNAL_ERROR,
33         SIGNAL_DONE,
34         SIGNAL_PERCENT,
35         SIGNAL_COUNT
36 };
37
38 static guint signals[SIGNAL_COUNT] = { 0 };
39
40 static void image_loader_init(GTypeInstance *instance, gpointer g_class);
41 static void image_loader_class_init(ImageLoaderClass *class);
42 static void image_loader_finalize(GObject *object);
43 static void image_loader_stop(ImageLoader *il);
44
45 GType image_loader_get_type(void)
46 {
47         static GType type = 0;
48         if (type == 0) 
49                 {
50                 static const GTypeInfo info = {
51                         sizeof(ImageLoaderClass),
52                         NULL,   /* base_init */
53                         NULL,   /* base_finalize */
54                         (GClassInitFunc)image_loader_class_init, /* class_init */
55                         NULL,   /* class_finalize */
56                         NULL,   /* class_data */
57                         sizeof(ImageLoader),
58                         0,      /* n_preallocs */
59                         (GInstanceInitFunc)image_loader_init, /* instance_init */
60                         NULL    /* value_table */
61                         };
62                 type = g_type_register_static(G_TYPE_OBJECT, "ImageLoaderType", &info, 0);
63                 }
64         return type;
65 }
66
67 static void image_loader_init(GTypeInstance *instance, gpointer g_class)
68 {
69         ImageLoader *il = (ImageLoader *)instance;
70
71         il->pixbuf = NULL;
72         il->idle_id = -1;
73         il->idle_priority = G_PRIORITY_DEFAULT_IDLE;
74         il->done = FALSE;
75         il->loader = NULL;
76
77         il->bytes_read = 0;
78         il->bytes_total = 0;
79
80         il->idle_done_id = -1;
81
82         il->idle_read_loop_count = options->image.idle_read_loop_count;
83         il->read_buffer_size = options->image.read_buffer_size;
84         il->mapped_file = NULL;
85
86         il->requested_width = 0;
87         il->requested_height = 0;
88         il->shrunk = FALSE;
89
90         il->can_destroy = TRUE;
91
92 #ifdef HAVE_GTHREAD
93         il->data_mutex = g_mutex_new();
94         il->can_destroy_cond = g_cond_new();
95 #endif
96         DEBUG_1("new image loader %p, bufsize=%u idle_loop=%u", il, il->read_buffer_size, il->idle_read_loop_count);
97 }
98
99 static void image_loader_class_init(ImageLoaderClass *class)
100 {
101         GObjectClass *gobject_class = G_OBJECT_CLASS (class);
102
103 //      gobject_class->set_property = image_loader_set_property;
104 //      gobject_class->get_property = image_loader_get_property;
105
106         gobject_class->finalize = image_loader_finalize;
107
108
109         signals[SIGNAL_AREA_READY] =
110                 g_signal_new("area_ready",
111                              G_OBJECT_CLASS_TYPE(gobject_class),
112                              G_SIGNAL_RUN_LAST,
113                              G_STRUCT_OFFSET(ImageLoaderClass, area_ready),
114                              NULL, NULL,
115                              gq_marshal_VOID__INT_INT_INT_INT,
116                              G_TYPE_NONE, 4,
117                              G_TYPE_INT,
118                              G_TYPE_INT,
119                              G_TYPE_INT,
120                              G_TYPE_INT);
121
122         signals[SIGNAL_ERROR] =
123                 g_signal_new("error",
124                              G_OBJECT_CLASS_TYPE(gobject_class),
125                              G_SIGNAL_RUN_LAST,
126                              G_STRUCT_OFFSET(ImageLoaderClass, error),
127                              NULL, NULL,
128                              g_cclosure_marshal_VOID__VOID,
129                              G_TYPE_NONE, 0);
130
131         signals[SIGNAL_DONE] =
132                 g_signal_new("done",
133                              G_OBJECT_CLASS_TYPE(gobject_class),
134                              G_SIGNAL_RUN_LAST,
135                              G_STRUCT_OFFSET(ImageLoaderClass, done),
136                              NULL, NULL,
137                              g_cclosure_marshal_VOID__VOID,
138                              G_TYPE_NONE, 0);
139
140         signals[SIGNAL_PERCENT] =
141                 g_signal_new("percent",
142                              G_OBJECT_CLASS_TYPE(gobject_class),
143                              G_SIGNAL_RUN_LAST,
144                              G_STRUCT_OFFSET(ImageLoaderClass, percent),
145                              NULL, NULL,
146                              g_cclosure_marshal_VOID__DOUBLE,
147                              G_TYPE_NONE, 1,
148                              G_TYPE_DOUBLE);
149
150 }
151
152 static void image_loader_finalize(GObject *object)
153 {
154         ImageLoader *il = (ImageLoader *)object;
155
156         image_loader_stop(il);
157
158         if (il->error) DEBUG_1("%s", image_loader_get_error(il));
159
160         DEBUG_1("freeing image loader %p bytes_read=%d", il, il->bytes_read);
161
162         if (il->idle_done_id != -1) g_source_remove(il->idle_done_id);
163
164         while (g_source_remove_by_user_data(il)) 
165                 {
166                 DEBUG_2("pending signals detected");
167                 }
168         
169         while (il->area_param_list) 
170                 {
171                 DEBUG_1("pending area_ready signals detected");
172                 while (g_source_remove_by_user_data(il->area_param_list->data)) {}
173                 g_free(il->area_param_list->data);
174                 il->area_param_list = g_list_delete_link(il->area_param_list, il->area_param_list);
175                 }
176
177         while (il->area_param_delayed_list) 
178                 {
179                 g_free(il->area_param_delayed_list->data);
180                 il->area_param_delayed_list = g_list_delete_link(il->area_param_delayed_list, il->area_param_delayed_list);
181                 }
182
183         if (il->pixbuf) g_object_unref(il->pixbuf);
184         
185         if (il->error) g_error_free(il->error);
186
187         file_data_unref(il->fd);
188 #ifdef HAVE_GTHREAD
189         g_mutex_free(il->data_mutex);
190         g_cond_free(il->can_destroy_cond);
191 #endif
192 }
193
194 void image_loader_free(ImageLoader *il)
195 {
196         if (!il) return;
197         g_object_unref(G_OBJECT(il));
198 }
199
200
201 ImageLoader *image_loader_new(FileData *fd)
202 {
203         ImageLoader *il;
204
205         if (!fd) return NULL;
206
207         il = (ImageLoader *) g_object_new(TYPE_IMAGE_LOADER, NULL);
208         
209         il->fd = file_data_ref(fd);
210         
211         return il;
212 }
213
214 /**************************************************************************************/
215 /* send signals via idle calbacks - the callback are executed in the main thread */
216
217 typedef struct _ImageLoaderAreaParam ImageLoaderAreaParam;
218 struct _ImageLoaderAreaParam {
219         ImageLoader *il;
220         guint x;
221         guint y;
222         guint w;
223         guint h;
224 };
225
226
227 static gboolean image_loader_emit_area_ready_cb(gpointer data)
228 {
229         ImageLoaderAreaParam *par = data;
230         ImageLoader *il = par->il;
231         g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
232         g_mutex_lock(il->data_mutex);
233         il->area_param_list = g_list_remove(il->area_param_list, par);
234         g_free(par);
235         g_mutex_unlock(il->data_mutex);
236         
237         return FALSE;
238 }
239
240 static gboolean image_loader_emit_done_cb(gpointer data)
241 {
242         ImageLoader *il = data;
243         g_signal_emit(il, signals[SIGNAL_DONE], 0);
244         return FALSE;
245 }
246
247 static gboolean image_loader_emit_error_cb(gpointer data)
248 {
249         ImageLoader *il = data;
250         g_signal_emit(il, signals[SIGNAL_ERROR], 0);
251         return FALSE;
252 }
253
254 static gboolean image_loader_emit_percent_cb(gpointer data)
255 {
256         ImageLoader *il = data;
257         g_signal_emit(il, signals[SIGNAL_PERCENT], 0, image_loader_get_percent(il));
258         return FALSE;
259 }
260
261 /* DONE and ERROR are emited only once, thus they can have normal priority
262    PERCENT and AREA_READY should be processed ASAP
263 */
264
265 static void image_loader_emit_done(ImageLoader *il)
266 {
267         g_idle_add_full(il->idle_priority, image_loader_emit_done_cb, il, NULL);
268 }
269
270 static void image_loader_emit_error(ImageLoader *il)
271 {
272         g_idle_add_full(il->idle_priority, image_loader_emit_error_cb, il, NULL);
273 }
274
275 static void image_loader_emit_percent(ImageLoader *il)
276 {
277         g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_percent_cb, il, NULL);
278 }
279
280 /* this function expects that il->data_mutex is locked by caller */
281 static void image_loader_emit_area_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
282 {
283         ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
284         par->il = il;
285         par->x = x;
286         par->y = y;
287         par->w = w;
288         par->h = h;
289         
290         il->area_param_list = g_list_prepend(il->area_param_list, par);
291         
292         g_idle_add_full(G_PRIORITY_HIGH, image_loader_emit_area_ready_cb, par, NULL);
293 }
294
295 /**************************************************************************************/
296 /* the following functions may be executed in separate thread */
297
298 /* this function expects that il->data_mutex is locked by caller */
299 static void image_loader_queue_delayed_erea_ready(ImageLoader *il, guint x, guint y, guint w, guint h)
300 {
301         ImageLoaderAreaParam *par = g_new0(ImageLoaderAreaParam, 1);
302         par->il = il;
303         par->x = x;
304         par->y = y;
305         par->w = w;
306         par->h = h;
307         
308         il->area_param_delayed_list = g_list_prepend(il->area_param_delayed_list, par);
309 }
310
311
312
313 static gboolean image_loader_get_stopping(ImageLoader *il)
314 {
315         gboolean ret;
316         if (!il) return FALSE;
317
318         g_mutex_lock(il->data_mutex);
319         ret = il->stopping;
320         g_mutex_unlock(il->data_mutex);
321
322         return ret;
323 }
324
325
326 static void image_loader_sync_pixbuf(ImageLoader *il)
327 {
328         GdkPixbuf *pb;
329         
330         g_mutex_lock(il->data_mutex);
331         
332         if (!il->loader) 
333                 {
334                 g_mutex_unlock(il->data_mutex);
335                 return;
336                 }
337
338         pb = gdk_pixbuf_loader_get_pixbuf(il->loader);
339
340         if (pb == il->pixbuf)
341                 {
342                 g_mutex_unlock(il->data_mutex);
343                 return;
344                 }
345
346         if (il->pixbuf) g_object_unref(il->pixbuf);
347
348         il->pixbuf = pb;
349         if (il->pixbuf) g_object_ref(il->pixbuf);
350
351         g_mutex_unlock(il->data_mutex);
352 }
353
354 static void image_loader_area_updated_cb(GdkPixbufLoader *loader,
355                                  guint x, guint y, guint w, guint h,
356                                  gpointer data)
357 {
358         ImageLoader *il = data;
359
360         if (!image_loader_get_pixbuf(il))
361                 {
362                 image_loader_sync_pixbuf(il);
363                 if (!image_loader_get_pixbuf(il))
364                         {
365                         log_printf("critical: area_ready signal with NULL pixbuf (out of mem?)\n");
366                         }
367                 }
368
369         g_mutex_lock(il->data_mutex);
370         if (il->delay_area_ready)
371                 image_loader_queue_delayed_erea_ready(il, x, y, w, h);
372         else
373                 image_loader_emit_area_ready(il, x, y, w, h);
374         g_mutex_unlock(il->data_mutex);
375 }
376
377 static void image_loader_area_prepared_cb(GdkPixbufLoader *loader, gpointer data)
378 {
379         GdkPixbuf *pb;
380         guchar *pix;
381         size_t h, rs;
382         
383         /* a workaround for http://bugzilla.gnome.org/show_bug.cgi?id=547669 */
384         gchar *format = gdk_pixbuf_format_get_name(gdk_pixbuf_loader_get_format(loader));
385         if (strcmp(format, "svg") == 0)
386                 {
387                 g_free(format);
388                 return;
389                 }
390         
391         g_free(format);
392
393         pb = gdk_pixbuf_loader_get_pixbuf(loader);
394         
395         h = gdk_pixbuf_get_height(pb);
396         rs = gdk_pixbuf_get_rowstride(pb);
397         pix = gdk_pixbuf_get_pixels(pb);
398         
399         memset(pix, 0, rs * h); /*this should be faster than pixbuf_fill */
400
401 }
402
403 static void image_loader_size_cb(GdkPixbufLoader *loader,
404                                  gint width, gint height, gpointer data)
405 {
406         ImageLoader *il = data;
407         GdkPixbufFormat *format;
408         gchar **mime_types;
409         gboolean scale = FALSE;
410         gint n;
411
412         g_mutex_lock(il->data_mutex);
413         if (il->requested_width < 1 || il->requested_height < 1) 
414                 {
415                 g_mutex_unlock(il->data_mutex);
416                 return;
417                 }
418         g_mutex_unlock(il->data_mutex);
419
420         format = gdk_pixbuf_loader_get_format(loader);
421         if (!format) return;
422
423         mime_types = gdk_pixbuf_format_get_mime_types(format);
424         n = 0;
425         while (mime_types[n])
426                 {
427                 if (strstr(mime_types[n], "jpeg")) scale = TRUE;
428                 n++;
429                 }
430         g_strfreev(mime_types);
431
432         if (!scale) return;
433
434         g_mutex_lock(il->data_mutex);
435
436         if (width > il->requested_width || height > il->requested_height)
437                 {
438                 gint nw, nh;
439
440                 if (((gdouble)il->requested_width / width) < ((gdouble)il->requested_height / height))
441                         {
442                         nw = il->requested_width;
443                         nh = (gdouble)nw / width * height;
444                         if (nh < 1) nh = 1;
445                         }
446                 else
447                         {
448                         nh = il->requested_height;
449                         nw = (gdouble)nh / height * width;
450                         if (nw < 1) nw = 1;
451                         }
452
453                 gdk_pixbuf_loader_set_size(loader, nw, nh);
454                 il->shrunk = TRUE;
455                 }
456         g_mutex_unlock(il->data_mutex);
457
458 }
459
460 static void image_loader_stop_loader(ImageLoader *il)
461 {
462         if (!il) return;
463
464         if (il->loader)
465                 {
466                 /* some loaders do not have a pixbuf till close, order is important here */
467                 gdk_pixbuf_loader_close(il->loader, il->error ? NULL : &il->error); /* we are interested in the first error only */
468                 image_loader_sync_pixbuf(il);
469                 g_object_unref(G_OBJECT(il->loader));
470                 il->loader = NULL;
471                 }
472         g_mutex_lock(il->data_mutex);
473         il->done = TRUE;
474         g_mutex_unlock(il->data_mutex);
475 }
476
477 static void image_loader_setup_loader(ImageLoader *il)
478 {
479         g_mutex_lock(il->data_mutex);
480         il->loader = gdk_pixbuf_loader_new();
481
482         g_signal_connect(G_OBJECT(il->loader), "area_updated",
483                          G_CALLBACK(image_loader_area_updated_cb), il);
484         g_signal_connect(G_OBJECT(il->loader), "size_prepared",
485                          G_CALLBACK(image_loader_size_cb), il);
486         g_signal_connect(G_OBJECT(il->loader), "area_prepared",
487                          G_CALLBACK(image_loader_area_prepared_cb), il);
488         g_mutex_unlock(il->data_mutex);
489 }
490
491
492 static void image_loader_done(ImageLoader *il)
493 {
494         image_loader_stop_loader(il);
495
496         image_loader_emit_done(il);
497 }
498
499 static void image_loader_error(ImageLoader *il)
500 {
501         image_loader_stop_loader(il);
502
503         DEBUG_1("pixbuf_loader reported load error for: %s", il->fd->path);
504
505         image_loader_emit_error(il);
506 }
507
508 static gboolean image_loader_continue(ImageLoader *il)
509 {
510         gint b;
511         gint c;
512
513         if (!il) return FALSE;
514
515         c = il->idle_read_loop_count ? il->idle_read_loop_count : 1;
516         while (c > 0 && !image_loader_get_stopping(il))
517                 {
518                 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
519
520                 if (b == 0)
521                         {
522                         image_loader_done(il);
523                         return FALSE;
524                         }
525
526                 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
527                         {
528                         image_loader_error(il);
529                         return FALSE;
530                         }
531
532                 il->bytes_read += b;
533
534                 c--;
535                 }
536
537         if (il->bytes_total > 0)
538                 {
539                 image_loader_emit_percent(il);
540                 }
541
542         return TRUE;
543 }
544
545 static gboolean image_loader_begin(ImageLoader *il)
546 {
547         gssize b;
548         
549         if (il->pixbuf) return FALSE;
550
551         b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
552         if (b < 1) return FALSE;
553
554         image_loader_setup_loader(il);
555
556         if (!gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, &il->error))
557                 {
558                 image_loader_stop_loader(il);
559                 return FALSE;
560                 }
561
562         il->bytes_read += b;
563
564         /* read until size is known */
565         while (il->loader && !gdk_pixbuf_loader_get_pixbuf(il->loader) && b > 0 && !image_loader_get_stopping(il))
566                 {
567                 b = MIN(il->read_buffer_size, il->bytes_total - il->bytes_read);
568                 if (b < 0 || (b > 0 && !gdk_pixbuf_loader_write(il->loader, il->mapped_file + il->bytes_read, b, &il->error)))
569                         {
570                         image_loader_stop_loader(il);
571                         return FALSE;
572                         }
573                 il->bytes_read += b;
574                 }
575         if (!il->pixbuf) image_loader_sync_pixbuf(il);
576
577         if (il->bytes_read == il->bytes_total || b < 1)
578                 {
579                 /* done, handle (broken) loaders that do not have pixbuf till close */
580                 image_loader_stop_loader(il);
581
582                 if (!il->pixbuf) return FALSE;
583
584                 image_loader_done(il);
585                 return TRUE;
586                 }
587
588         if (!il->pixbuf)
589                 {
590                 image_loader_stop_loader(il);
591                 return FALSE;
592                 }
593
594         return TRUE;
595 }
596
597 /**************************************************************************************/
598 /* the following functions are always executed in the main thread */
599
600
601 static gboolean image_loader_setup_source(ImageLoader *il)
602 {
603         struct stat st;
604         gchar *pathl;
605
606         if (!il || il->loader || il->mapped_file) return FALSE;
607
608         il->mapped_file = NULL;
609
610         if (il->fd)
611                 {
612                 ExifData *exif = exif_read_fd(il->fd);
613
614                 if (options->thumbnails.use_exif)
615                         il->mapped_file = exif_get_preview(exif, &il->bytes_total, il->requested_width, il->requested_height);
616                 else
617                         il->mapped_file = exif_get_preview(exif, &il->bytes_total, 0, 0); /* get the largest available preview image or NULL for normal images*/
618
619                 if (il->mapped_file)
620                         {
621                         il->preview = TRUE;
622                         DEBUG_1("Usable reduced size (preview) image loaded from file %s", il->fd->path);
623                         }
624                 exif_free_fd(il->fd, exif);
625                 }
626
627         
628         if (!il->mapped_file)
629                 {
630                 /* normal file */
631                 gint load_fd;
632         
633                 pathl = path_from_utf8(il->fd->path);
634                 load_fd = open(pathl, O_RDONLY | O_NONBLOCK);
635                 g_free(pathl);
636                 if (load_fd == -1) return FALSE;
637
638                 if (fstat(load_fd, &st) == 0)
639                         {
640                         il->bytes_total = st.st_size;
641                         }
642                 else
643                         {
644                         close(load_fd);
645                         return FALSE;
646                         }
647                 
648                 il->mapped_file = mmap(0, il->bytes_total, PROT_READ|PROT_WRITE, MAP_PRIVATE, load_fd, 0);
649                 close(load_fd);
650                 if (il->mapped_file == MAP_FAILED)
651                         {
652                         il->mapped_file = 0;
653                         return FALSE;
654                         }
655                 il->preview = FALSE;
656                 }
657                 
658         return TRUE;
659 }
660
661 static void image_loader_stop_source(ImageLoader *il)
662 {
663         if (!il) return;
664         
665         if (il->mapped_file)
666                 {
667                 if (il->preview)
668                         {
669                         exif_free_preview(il->mapped_file);
670                         }
671                 else
672                         {
673                         munmap(il->mapped_file, il->bytes_total);
674                         }
675                 il->mapped_file = NULL;
676                 }
677 }
678
679 static void image_loader_stop(ImageLoader *il)
680 {
681         if (!il) return;
682
683         if (il->idle_id != -1)
684                 {
685                 g_source_remove(il->idle_id);
686                 il->idle_id = -1;
687                 }
688                 
689         if (il->thread)
690                 {
691                 /* stop loader in the other thread */
692                 g_mutex_lock(il->data_mutex);
693                 il->stopping = TRUE;
694                 while (!il->can_destroy) g_cond_wait(il->can_destroy_cond, il->data_mutex);
695                 g_mutex_unlock(il->data_mutex);
696                 }
697
698         image_loader_stop_loader(il);
699         image_loader_stop_source(il);
700         
701 }
702
703 void image_loader_delay_area_ready(ImageLoader *il, gboolean enable)
704 {
705         g_mutex_lock(il->data_mutex);
706         il->delay_area_ready = enable;
707         if (!enable)
708                 {
709                 /* send delayed */
710                 GList *list, *work;
711                 list = g_list_reverse(il->area_param_delayed_list);
712                 il->area_param_delayed_list = NULL;
713                 g_mutex_unlock(il->data_mutex);
714
715                 work = list;
716
717                 while (work)
718                         {
719                         ImageLoaderAreaParam *par = work->data;
720                         work = work->next;
721                         
722                         g_signal_emit(il, signals[SIGNAL_AREA_READY], 0, par->x, par->y, par->w, par->h);
723                         g_free(par);
724                         }
725                 g_list_free(list);
726                 }
727         else
728                 {
729                 /* just unlock */
730                 g_mutex_unlock(il->data_mutex);
731                 }
732 }
733
734
735 /**************************************************************************************/
736 /* execution via idle calls */
737
738 static gboolean image_loader_idle_cb(gpointer data)
739 {
740         gboolean ret = FALSE;
741         ImageLoader *il = data;
742
743         if (il->idle_id != -1)
744                 {
745                 ret = image_loader_continue(il);
746                 }
747         
748         if (!ret)
749                 {
750                 image_loader_stop_source(il);
751                 }
752         
753         return ret;
754 }
755
756
757 static gboolean image_loader_start_idle(ImageLoader *il)
758 {
759         gboolean ret;
760         
761         if (!il) return FALSE;
762
763         if (!il->fd) return FALSE;
764
765         if (!image_loader_setup_source(il)) return FALSE;
766         
767         ret = image_loader_begin(il);
768
769         if (ret && !il->done) il->idle_id = g_idle_add_full(il->idle_priority, image_loader_idle_cb, il, NULL);
770         return ret;
771 }
772
773 /**************************************************************************************/
774 /* execution via thread */
775 static GThreadPool *image_loader_thread_pool = NULL;
776
777 static GCond *image_loader_prio_cond = NULL;
778 static GMutex *image_loader_prio_mutex = NULL;
779 static gint image_loader_prio_num = 0;
780
781
782 static void image_loader_thread_enter_high(void)
783 {
784         g_mutex_lock(image_loader_prio_mutex);
785         image_loader_prio_num++;
786         g_mutex_unlock(image_loader_prio_mutex);
787 }
788
789 static void image_loader_thread_leave_high(void)
790 {
791         g_mutex_lock(image_loader_prio_mutex);
792         image_loader_prio_num--;
793         if (image_loader_prio_num == 0) g_cond_broadcast(image_loader_prio_cond); /* wake up all low prio threads */
794         g_mutex_unlock(image_loader_prio_mutex);
795 }
796
797 static void image_loader_thread_wait_high(void)
798 {
799         g_mutex_lock(image_loader_prio_mutex);
800         while (image_loader_prio_num) 
801                 {
802                 g_cond_wait(image_loader_prio_cond, image_loader_prio_mutex);
803                 }
804
805         g_mutex_unlock(image_loader_prio_mutex);
806 }
807
808
809 static void image_loader_thread_run(gpointer data, gpointer user_data)
810 {
811         ImageLoader *il = data;
812         gboolean cont;
813         
814         if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE) 
815                 {
816                 /* low prio, wait untill high prio tasks finishes */
817                 image_loader_thread_wait_high();
818                 }
819         else
820                 {
821                 /* high prio */
822                 image_loader_thread_enter_high();
823                 }
824         
825         cont = image_loader_begin(il);
826         
827         if (!cont && !image_loader_get_pixbuf(il))
828                 {
829                 /* 
830                 loader failed, we have to send signal 
831                 (idle mode returns the image_loader_begin return value directly)
832                 (success is always reported indirectly from image_loader_begin)
833                 */
834                 image_loader_emit_error(il);
835                 }
836         
837         while (cont && !image_loader_get_is_done(il) && !image_loader_get_stopping(il))
838                 {
839                 if (il->idle_priority > G_PRIORITY_DEFAULT_IDLE) 
840                         {
841                         /* low prio, wait untill high prio tasks finishes */
842                         image_loader_thread_wait_high();
843                         }
844                 cont = image_loader_continue(il);
845                 }
846         image_loader_stop_loader(il);
847
848         if (il->idle_priority <= G_PRIORITY_DEFAULT_IDLE) 
849                 {
850                 /* high prio */
851                 image_loader_thread_leave_high();
852                 }
853
854         g_mutex_lock(il->data_mutex);
855         il->can_destroy = TRUE;
856         g_cond_signal(il->can_destroy_cond);
857         g_mutex_unlock(il->data_mutex);
858
859 }
860
861
862 static gboolean image_loader_start_thread(ImageLoader *il)
863 {
864         if (!il) return FALSE;
865
866         if (!il->fd) return FALSE;
867
868         il->thread = TRUE;
869         
870         if (!image_loader_setup_source(il)) return FALSE;
871
872         if (!image_loader_thread_pool) 
873                 {
874                 image_loader_thread_pool = g_thread_pool_new(image_loader_thread_run, NULL, -1, FALSE, NULL);
875                 image_loader_prio_cond = g_cond_new();
876                 image_loader_prio_mutex = g_mutex_new();
877                 }
878
879         il->can_destroy = FALSE; /* ImageLoader can't be freed until image_loader_thread_run finishes */
880
881         g_thread_pool_push(image_loader_thread_pool, il, NULL);
882         DEBUG_1("Thread pool num threads: %d", g_thread_pool_get_num_threads(image_loader_thread_pool));
883                 
884         return TRUE;
885 }
886
887
888 /**************************************************************************************/
889 /* public interface */
890
891
892 gboolean image_loader_start(ImageLoader *il)
893 {
894         if (!il) return FALSE;
895
896         if (!il->fd) return FALSE;
897
898 #ifdef HAVE_GTHREAD
899         return image_loader_start_thread(il);
900 #else
901         return image_loader_start_idle(il);
902 #endif
903 }
904
905
906 /* don't forget to gdk_pixbuf_ref() it if you want to use it after image_loader_free() */
907 GdkPixbuf *image_loader_get_pixbuf(ImageLoader *il)
908 {
909         GdkPixbuf *ret;
910         if (!il) return NULL;
911         
912         g_mutex_lock(il->data_mutex);
913         ret = il->pixbuf;
914         g_mutex_unlock(il->data_mutex);
915         return ret;
916 }
917
918 gchar *image_loader_get_format(ImageLoader *il)
919 {
920         GdkPixbufFormat *format;
921         gchar **mimev;
922         gchar *mime;
923
924         if (!il || !il->loader) return NULL;
925
926         format = gdk_pixbuf_loader_get_format(il->loader);
927         if (!format) return NULL;
928
929         mimev = gdk_pixbuf_format_get_mime_types(format);
930         if (!mimev) return NULL;
931
932         /* return first member of mimev, as GdkPixbufLoader has no way to tell us which exact one ? */
933         mime = g_strdup(mimev[0]);
934         g_strfreev(mimev);
935
936         return mime;
937 }
938
939 void image_loader_set_requested_size(ImageLoader *il, gint width, gint height)
940 {
941         if (!il) return;
942
943         g_mutex_lock(il->data_mutex);
944         il->requested_width = width;
945         il->requested_height = height;
946         g_mutex_unlock(il->data_mutex);
947 }
948
949 void image_loader_set_buffer_size(ImageLoader *il, guint count)
950 {
951         if (!il) return;
952
953         g_mutex_lock(il->data_mutex);
954         il->idle_read_loop_count = count ? count : 1;
955         g_mutex_unlock(il->data_mutex);
956 }
957
958 void image_loader_set_priority(ImageLoader *il, gint priority)
959 {
960         if (!il) return;
961
962         if (il->thread) return; /* can't change prio if the thread already runs */
963         il->idle_priority = priority;
964 }
965
966
967 gdouble image_loader_get_percent(ImageLoader *il)
968 {
969         gdouble ret;
970         if (!il) return 0.0;
971         
972         g_mutex_lock(il->data_mutex);
973         if (il->bytes_total == 0) 
974                 {
975                 ret = 0.0;
976                 }
977         else
978                 {
979                 ret = (gdouble)il->bytes_read / il->bytes_total;
980                 }
981         g_mutex_unlock(il->data_mutex);
982         return ret;
983 }
984
985 gboolean image_loader_get_is_done(ImageLoader *il)
986 {
987         gboolean ret;
988         if (!il) return FALSE;
989
990         g_mutex_lock(il->data_mutex);
991         ret = il->done;
992         g_mutex_unlock(il->data_mutex);
993
994         return ret;
995 }
996
997 FileData *image_loader_get_fd(ImageLoader *il)
998 {
999         FileData *ret;
1000         if (!il) return NULL;
1001
1002         g_mutex_lock(il->data_mutex);
1003         ret = il->fd;
1004         g_mutex_unlock(il->data_mutex);
1005
1006         return ret;
1007 }
1008
1009 gboolean image_loader_get_shrunk(ImageLoader *il)
1010 {
1011         gboolean ret;
1012         if (!il) return FALSE;
1013
1014         g_mutex_lock(il->data_mutex);
1015         ret = il->shrunk;
1016         g_mutex_unlock(il->data_mutex);
1017         return ret;
1018 }
1019
1020 const gchar *image_loader_get_error(ImageLoader *il)
1021 {
1022         const gchar *ret = NULL;
1023         if (!il) return NULL;
1024         g_mutex_lock(il->data_mutex);
1025         if (il->error) ret = il->error->message;
1026         g_mutex_unlock(il->data_mutex);
1027         return ret;
1028 }
1029
1030
1031 /* FIXME - this can be rather slow and blocks until the size is known */
1032 gboolean image_load_dimensions(FileData *fd, gint *width, gint *height)
1033 {
1034         ImageLoader *il;
1035         gboolean success;
1036
1037         il = image_loader_new(fd);
1038
1039         success = image_loader_start_idle(il);
1040
1041         if (success && il->pixbuf)
1042                 {
1043                 if (width) *width = gdk_pixbuf_get_width(il->pixbuf);
1044                 if (height) *height = gdk_pixbuf_get_height(il->pixbuf);;
1045                 }
1046         else
1047                 {
1048                 if (width) *width = -1;
1049                 if (height) *height = -1;
1050                 }
1051
1052         image_loader_free(il);
1053
1054         return success;
1055 }
1056 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */