Fix 4 warnings due to function prototype mismatches.
[geeqie.git] / src / renderer-tiles.c
1 /*
2  * Geeqie
3  * (C) 2006 John Ellis
4  * Copyright (C) 2008 - 2012 The Geeqie Team
5  *
6  * Author: John Ellis
7  * Author: Vladimir Nadvornik
8  *
9  * This software is released under the GNU General Public License (GNU GPL).
10  * Please read the included file COPYING for more information.
11  * This software comes with no warranty of any kind, use at your own risk!
12  */
13
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include <string.h>
17 #include <math.h>
18
19 #include "main.h"
20 #include "pixbuf-renderer.h"
21 #include "renderer-tiles.h"
22
23 #include "intl.h"
24 #include "layout.h"
25
26 #include <gtk/gtk.h>
27
28
29 /* comment this out if not using this from within Geeqie
30  * defining GQ_BUILD does these things:
31  *   - Sets the shift-click scroller pixbuf to a nice icon instead of a black box
32  */
33 #define GQ_BUILD 1
34
35 #ifdef GQ_BUILD
36 #include "main.h"
37 #include "pixbuf_util.h"
38 #include "exif.h"
39 #else
40 typedef enum {
41         EXIF_ORIENTATION_UNKNOWN        = 0,
42         EXIF_ORIENTATION_TOP_LEFT       = 1,
43         EXIF_ORIENTATION_TOP_RIGHT      = 2,
44         EXIF_ORIENTATION_BOTTOM_RIGHT   = 3,
45         EXIF_ORIENTATION_BOTTOM_LEFT    = 4,
46         EXIF_ORIENTATION_LEFT_TOP       = 5,
47         EXIF_ORIENTATION_RIGHT_TOP      = 6,
48         EXIF_ORIENTATION_RIGHT_BOTTOM   = 7,
49         EXIF_ORIENTATION_LEFT_BOTTOM    = 8
50 } ExifOrientationType;
51 #endif
52
53
54 /* size to use when breaking up image pane for rendering */
55 #define PR_TILE_SIZE 128
56
57 typedef struct _ImageTile ImageTile;
58 typedef struct _QueueData QueueData;
59
60 struct _ImageTile
61 {
62         cairo_surface_t *surface;       /* off screen buffer */
63         GdkPixbuf *pixbuf;      /* pixbuf area for zooming */
64         gint x;                 /* x offset into image */
65         gint y;                 /* y offset into image */
66         gint w;                 /* width that is visible (may be less if at edge of image) */
67         gint h;                 /* height '' */
68
69         gboolean blank;
70
71 /* render_todo: (explanation)
72         NONE    do nothing
73         AREA    render area of tile, usually only used when loading an image
74                 note: will jump to an ALL if render_done is not ALL.
75         ALL     render entire tile, if never done before w/ ALL, for expose events *only*
76 */
77
78         ImageRenderType render_todo;    /* what to do (see above) */
79         ImageRenderType render_done;    /* highest that has been done before on tile */
80
81         QueueData *qd;
82         QueueData *qd2;
83
84         guint size;             /* est. memory used by pixmap and pixbuf */
85 };
86
87 struct _QueueData
88 {
89         ImageTile *it;
90         gint x;
91         gint y;
92         gint w;
93         gint h;
94         gboolean new_data;
95 };
96
97 typedef struct _OverlayData OverlayData;
98 struct _OverlayData
99 {
100         gint id;
101
102         GdkPixbuf *pixbuf;
103         GdkWindow *window;
104
105         gint x;
106         gint y;
107
108         OverlayRendererFlags flags;
109 };
110
111 typedef struct _RendererTiles RendererTiles;
112
113 struct _RendererTiles
114 {
115         RendererFuncs f;
116         PixbufRenderer *pr;
117
118         gint tile_cache_max;            /* max mb to use for offscreen buffer */
119
120         gint tile_width;
121         gint tile_height;
122         gint tile_cols;         /* count of tile columns */
123         GList *tiles;           /* list of buffer tiles */
124         gint tile_cache_size;   /* allocated size of pixmaps/pixbufs */
125         GList *draw_queue;      /* list of areas to redraw */
126         GList *draw_queue_2pass;/* list when 2 pass is enabled */
127
128         GList *overlay_list;
129         cairo_surface_t *overlay_buffer;
130         
131         guint draw_idle_id; /* event source id */
132
133         GdkPixbuf *spare_tile;
134         
135         gint stereo_mode;
136         gint stereo_off_x;
137         gint stereo_off_y;
138         
139         gint x_scroll;  /* allow local adjustment and mirroring */
140         gint y_scroll;
141         
142 };
143
144
145
146 static void rt_border_draw(RendererTiles *rt, gint x, gint y, gint w, gint h);
147 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h, ImageTile *it);
148
149
150 static void rt_tile_free_all(RendererTiles *rt);
151 static void rt_tile_invalidate_region(RendererTiles *rt, gint x, gint y, gint w, gint h);
152 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it);
153 static void rt_queue_clear(RendererTiles *rt);
154 static void rt_queue_merge(QueueData *parent, QueueData *qd);
155 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
156                      gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing);
157
158 static void rt_hierarchy_changed_cb(GtkWidget *widget, GtkWidget *previous_toplevel, gpointer data);
159 static gint rt_queue_draw_idle_cb(gpointer data);
160 static void renderer_redraw(void *renderer, gint x, gint y, gint w, gint h,
161                      gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing);
162
163 #define GET_RIGHT_PIXBUF_OFFSET(rt) \
164         (( (rt->stereo_mode & PR_STEREO_RIGHT) && !(rt->stereo_mode & PR_STEREO_SWAP)) || \
165          (!(rt->stereo_mode & PR_STEREO_RIGHT) &&  (rt->stereo_mode & PR_STEREO_SWAP)) ?  \
166           rt->pr->stereo_pixbuf_offset_right : rt->pr->stereo_pixbuf_offset_left )
167
168 #define GET_LEFT_PIXBUF_OFFSET(rt) \
169         ((!(rt->stereo_mode & PR_STEREO_RIGHT) && !(rt->stereo_mode & PR_STEREO_SWAP)) || \
170          ( (rt->stereo_mode & PR_STEREO_RIGHT) &&  (rt->stereo_mode & PR_STEREO_SWAP)) ?  \
171           rt->pr->stereo_pixbuf_offset_right : rt->pr->stereo_pixbuf_offset_left )
172
173
174 static void rt_sync_scroll(RendererTiles *rt)
175 {
176         PixbufRenderer *pr = rt->pr;
177         
178         rt->x_scroll = (rt->stereo_mode & PR_STEREO_MIRROR) ? 
179                        pr->width - pr->vis_width - pr->x_scroll 
180                        : pr->x_scroll;
181         
182         rt->y_scroll = (rt->stereo_mode & PR_STEREO_FLIP) ? 
183                        pr->height - pr->vis_height - pr->y_scroll 
184                        : pr->y_scroll;
185 }
186
187 /*
188  *-------------------------------------------------------------------
189  * borders
190  *-------------------------------------------------------------------
191  */
192
193 static void rt_border_draw(RendererTiles *rt, gint x, gint y, gint w, gint h)
194 {
195         PixbufRenderer *pr = rt->pr;
196         GtkWidget *box;
197         GdkWindow *window;
198         gint rx, ry, rw, rh;
199         cairo_t *cr;
200
201         box = GTK_WIDGET(pr);
202         window = gtk_widget_get_window(box);
203
204         if (!window) return;
205
206         cr = gdk_cairo_create(window);
207
208
209         if (!pr->pixbuf && !pr->source_tiles_enabled)
210                 {
211                 if (pr_clip_region(x, y, w, h,
212                                    0, 0,
213                                    pr->viewport_width, pr->viewport_height,
214                                    &rx, &ry, &rw, &rh))
215                         {
216                         cairo_set_source_rgb(cr, 0, 0, 0);
217                         cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
218                         cairo_fill(cr);
219                         cairo_destroy(cr);
220                         rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
221                         }
222                 return;
223                 }
224
225         if (pr->vis_width < pr->viewport_width)
226                 {
227                 if (pr->x_offset > 0 &&
228                     pr_clip_region(x, y, w, h,
229                                    0, 0,
230                                    pr->x_offset, pr->viewport_height,
231                                    &rx, &ry, &rw, &rh))
232                         {
233                         cairo_set_source_rgb(cr, 0, 0, 0);
234                         cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
235                         cairo_fill(cr);
236                         rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
237                         }
238                 if (pr->viewport_width - pr->vis_width - pr->x_offset > 0 &&
239                     pr_clip_region(x, y, w, h,
240                                    pr->x_offset + pr->vis_width, 0,
241                                    pr->viewport_width - pr->vis_width - pr->x_offset, pr->viewport_height,
242                                    &rx, &ry, &rw, &rh))
243                         {
244                         cairo_set_source_rgb(cr, 0, 0, 0);
245                         cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
246                         cairo_fill(cr);
247                         rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
248                         }
249                 }
250         if (pr->vis_height < pr->viewport_height)
251                 {
252                 if (pr->y_offset > 0 &&
253                     pr_clip_region(x, y, w, h,
254                                    pr->x_offset, 0,
255                                    pr->vis_width, pr->y_offset,
256                                    &rx, &ry, &rw, &rh))
257                         {
258                         cairo_set_source_rgb(cr, 0, 0, 0);
259                         cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
260                         cairo_fill(cr);
261                         rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
262                         }
263                 if (pr->viewport_height - pr->vis_height - pr->y_offset > 0 &&
264                     pr_clip_region(x, y, w, h,
265                                    pr->x_offset, pr->y_offset + pr->vis_height,
266                                    pr->vis_width, pr->viewport_height - pr->vis_height - pr->y_offset,
267                                    &rx, &ry, &rw, &rh))
268                         {
269                         cairo_set_source_rgb(cr, 0, 0, 0);
270                         cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
271                         cairo_fill(cr);
272                         rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
273                         }
274                 }
275         cairo_destroy(cr);
276 }
277
278 static void rt_border_clear(RendererTiles *rt)
279 {
280         PixbufRenderer *pr = rt->pr;
281         rt_border_draw(rt, 0, 0, pr->viewport_width, pr->viewport_height);
282 }
283
284
285 /*
286  *-------------------------------------------------------------------
287  * display tiles
288  *-------------------------------------------------------------------
289  */
290
291 static ImageTile *rt_tile_new(gint x, gint y, gint width, gint height)
292 {
293         ImageTile *it;
294
295         it = g_new0(ImageTile, 1);
296
297         it->x = x;
298         it->y = y;
299         it->w = width;
300         it->h = height;
301
302         it->render_done = TILE_RENDER_NONE;
303
304         return it;
305 }
306
307 static void rt_tile_free(ImageTile *it)
308 {
309         if (!it) return;
310
311         if (it->pixbuf) g_object_unref(it->pixbuf);
312         if (it->surface) cairo_surface_destroy(it->surface);
313
314         g_free(it);
315 }
316
317 static void rt_tile_free_all(RendererTiles *rt)
318 {
319         GList *work;
320
321         work = rt->tiles;
322         while (work)
323                 {
324                 ImageTile *it;
325
326                 it = work->data;
327                 work = work->next;
328
329                 rt_tile_free(it);
330                 }
331
332         g_list_free(rt->tiles);
333         rt->tiles = NULL;
334         rt->tile_cache_size = 0;
335 }
336
337 static ImageTile *rt_tile_add(RendererTiles *rt, gint x, gint y)
338 {
339         PixbufRenderer *pr = rt->pr;
340         ImageTile *it;
341
342         it = rt_tile_new(x, y, rt->tile_width, rt->tile_height);
343
344         if (it->x + it->w > pr->width) it->w = pr->width - it->x;
345         if (it->y + it->h > pr->height) it->h = pr->height - it->y;
346
347         rt->tiles = g_list_prepend(rt->tiles, it);
348         rt->tile_cache_size += it->size;
349
350         return it;
351 }
352
353 static void rt_tile_remove(RendererTiles *rt, ImageTile *it)
354 {
355         if (it->qd)
356                 {
357                 QueueData *qd = it->qd;
358
359                 it->qd = NULL;
360                 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
361                 g_free(qd);
362                 }
363
364         if (it->qd2)
365                 {
366                 QueueData *qd = it->qd2;
367
368                 it->qd2 = NULL;
369                 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
370                 g_free(qd);
371                 }
372
373         rt->tiles = g_list_remove(rt->tiles, it);
374         rt->tile_cache_size -= it->size;
375
376         rt_tile_free(it);
377 }
378
379 static void rt_tile_free_space(RendererTiles *rt, guint space, ImageTile *it)
380 {
381         PixbufRenderer *pr = rt->pr;
382         GList *work;
383         guint tile_max;
384
385         work = g_list_last(rt->tiles);
386
387         if (pr->source_tiles_enabled && pr->scale < 1.0)
388                 {
389                 gint tiles;
390
391                 tiles = (pr->vis_width / rt->tile_width + 1) * (pr->vis_height / rt->tile_height + 1);
392                 tile_max = MAX(tiles * rt->tile_width * rt->tile_height * 3,
393                                (gint)((gdouble)rt->tile_cache_max * 1048576.0 * pr->scale));
394                 }
395         else
396                 {
397                 tile_max = rt->tile_cache_max * 1048576;
398                 }
399
400         while (work && rt->tile_cache_size + space > tile_max)
401                 {
402                 ImageTile *needle;
403
404                 needle = work->data;
405                 work = work->prev;
406                 if (needle != it &&
407                     ((!needle->qd && !needle->qd2) || !rt_tile_is_visible(rt, needle))) rt_tile_remove(rt, needle);
408                 }
409 }
410
411 static void rt_tile_invalidate_all(RendererTiles *rt)
412 {
413         PixbufRenderer *pr = rt->pr;
414         GList *work;
415
416         work = rt->tiles;
417         while (work)
418                 {
419                 ImageTile *it;
420
421                 it = work->data;
422                 work = work->next;
423
424                 it->render_done = TILE_RENDER_NONE;
425                 it->render_todo = TILE_RENDER_ALL;
426                 it->blank = FALSE;
427
428                 it->w = MIN(rt->tile_width, pr->width - it->x);
429                 it->h = MIN(rt->tile_height, pr->height - it->y);
430                 }
431 }
432
433 static void rt_tile_invalidate_region(RendererTiles *rt, gint x, gint y, gint w, gint h)
434 {
435         gint x1, x2;
436         gint y1, y2;
437         GList *work;
438
439         x1 = ROUND_DOWN(x, rt->tile_width);
440         x2 = ROUND_UP(x + w, rt->tile_width);
441
442         y1 = ROUND_DOWN(y, rt->tile_height);
443         y2 = ROUND_UP(y + h, rt->tile_height);
444
445         work = rt->tiles;
446         while (work)
447                 {
448                 ImageTile *it;
449
450                 it = work->data;
451                 work = work->next;
452
453                 if (it->x < x2 && it->x + it->w > x1 &&
454                     it->y < y2 && it->y + it->h > y1)
455                         {
456                         it->render_done = TILE_RENDER_NONE;
457                         it->render_todo = TILE_RENDER_ALL;
458                         }
459                 }
460 }
461
462 static ImageTile *rt_tile_get(RendererTiles *rt, gint x, gint y, gboolean only_existing)
463 {
464         GList *work;
465
466         work = rt->tiles;
467         while (work)
468                 {
469                 ImageTile *it;
470
471                 it = work->data;
472                 if (it->x == x && it->y == y)
473                         {
474                         rt->tiles = g_list_delete_link(rt->tiles, work);
475                         rt->tiles = g_list_prepend(rt->tiles, it);
476                         return it;
477                         }
478
479                 work = work->next;
480                 }
481
482         if (only_existing) return NULL;
483
484         return rt_tile_add(rt, x, y);
485 }
486
487 static gint pixmap_calc_size(cairo_surface_t *surface)
488 {
489 //      gint w, h, d;
490
491 //      d = gdk_drawable_get_depth(pixmap);
492 //      gdk_drawable_get_size(pixmap, &w, &h);
493         return PR_TILE_SIZE * PR_TILE_SIZE * 4 / 8;
494 }
495
496 static void rt_tile_prepare(RendererTiles *rt, ImageTile *it)
497 {
498         PixbufRenderer *pr = rt->pr;
499         if (!it->surface)
500                 {
501                 cairo_surface_t *surface;
502                 guint size;
503
504                 surface = gdk_window_create_similar_surface(gtk_widget_get_window((GtkWidget *)pr),
505                                                             CAIRO_CONTENT_COLOR,
506                                                             rt->tile_width, rt->tile_height);
507
508                 size = pixmap_calc_size(surface);
509                 rt_tile_free_space(rt, size, it);
510
511                 it->surface = surface;
512                 it->size += size;
513                 rt->tile_cache_size += size;
514                 }
515
516         if (!it->pixbuf)
517                 {
518                 GdkPixbuf *pixbuf;
519                 guint size;
520                 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, rt->tile_width, rt->tile_height);
521
522                 size = gdk_pixbuf_get_rowstride(pixbuf) * rt->tile_height;
523                 rt_tile_free_space(rt, size, it);
524
525                 it->pixbuf = pixbuf;
526                 it->size += size;
527                 rt->tile_cache_size += size;
528                 }
529 }
530
531 /*
532  *-------------------------------------------------------------------
533  * overlays
534  *-------------------------------------------------------------------
535  */
536
537 static void rt_overlay_get_position(RendererTiles *rt, OverlayData *od,
538                                     gint *x, gint *y, gint *w, gint *h)
539 {
540         PixbufRenderer *pr = rt->pr;
541         gint px, py, pw, ph;
542
543         pw = gdk_pixbuf_get_width(od->pixbuf);
544         ph = gdk_pixbuf_get_height(od->pixbuf);
545         px = od->x;
546         py = od->y;
547
548         if (od->flags & OVL_RELATIVE)
549                 {
550                 if (px < 0) px = pr->viewport_width - pw + px;
551                 if (py < 0) py = pr->viewport_height - ph + py;
552                 }
553
554         if (x) *x = px;
555         if (y) *y = py;
556         if (w) *w = pw;
557         if (h) *h = ph;
558 }
559
560 static void rt_overlay_init_window(RendererTiles *rt, OverlayData *od)
561 {
562         PixbufRenderer *pr = rt->pr;
563         gint px, py, pw, ph;
564         GdkWindowAttr attributes;
565         gint attributes_mask;
566
567         rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
568
569         attributes.window_type = GDK_WINDOW_CHILD;
570         attributes.wclass = GDK_INPUT_OUTPUT;
571         attributes.width = pw;
572         attributes.height = ph;
573         attributes.event_mask = GDK_EXPOSURE_MASK;
574         attributes_mask = 0;
575
576         od->window = gdk_window_new(gtk_widget_get_window(GTK_WIDGET(pr)), &attributes, attributes_mask);
577         gdk_window_set_user_data(od->window, pr);
578         gdk_window_move(od->window, px + rt->stereo_off_x, py + rt->stereo_off_y);
579         gdk_window_show(od->window);
580 }
581
582 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h,
583                             ImageTile *it)
584 {
585         PixbufRenderer *pr = rt->pr;
586         GtkWidget *box;
587         GList *work;
588
589         box = GTK_WIDGET(pr);
590
591         work = rt->overlay_list;
592         while (work)
593                 {
594                 OverlayData *od;
595                 gint px, py, pw, ph;
596                 gint rx, ry, rw, rh;
597
598                 od = work->data;
599                 work = work->next;
600
601                 if (!od->window) rt_overlay_init_window(rt, od);
602                 
603                 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
604                 if (pr_clip_region(x, y, w, h, px, py, pw, ph, &rx, &ry, &rw, &rh))
605                         {
606                         if (!rt->overlay_buffer)
607                                 {
608                                 rt->overlay_buffer = gdk_window_create_similar_surface(gtk_widget_get_window((GtkWidget *)pr),
609                                                             CAIRO_CONTENT_COLOR,
610                                                             rt->tile_width, rt->tile_height);
611                                 }
612
613                         if (it)
614                                 {
615                                 cairo_t *cr;
616
617                                 cr = cairo_create(rt->overlay_buffer);
618                                 cairo_set_source_surface(cr, it->surface, (pr->x_offset + (it->x - rt->x_scroll)) - rx, (pr->y_offset + (it->y - rt->y_scroll)) - ry);
619                                 cairo_rectangle(cr, 0, 0, rw, rh);
620                                 cairo_fill_preserve(cr);
621                                 
622                                 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - rx, py - ry);
623                                 cairo_fill (cr);
624                                 cairo_destroy (cr);
625                                 
626                                 cr = gdk_cairo_create(od->window);
627                                 cairo_set_source_surface(cr, rt->overlay_buffer, rx - px, ry - py);
628                                 cairo_rectangle (cr, rx - px, ry - py, rw, rh);
629                                 cairo_fill (cr);
630                                 cairo_destroy (cr);
631                                 }
632                         else
633                                 {
634                                 /* no ImageTile means region may be larger than our scratch buffer */
635                                 gint sx, sy;
636
637                                 for (sx = rx; sx < rx + rw; sx += rt->tile_width)
638                                     for (sy = ry; sy < ry + rh; sy += rt->tile_height)
639                                         {
640                                         gint sw, sh;
641                                         cairo_t *cr;
642
643                                         sw = MIN(rx + rw - sx, rt->tile_width);
644                                         sh = MIN(ry + rh - sy, rt->tile_height);
645
646                                         cr = cairo_create(rt->overlay_buffer);
647                                         cairo_set_source_rgb(cr, 0, 0, 0);
648                                         cairo_rectangle(cr, 0, 0, sw, sh);
649                                         cairo_fill_preserve(cr);
650                                 
651                                         gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - sx, py - sy);
652                                         cairo_fill (cr);
653                                         cairo_destroy (cr);
654                                 
655                                         cr = gdk_cairo_create(od->window);
656                                         cairo_set_source_surface(cr, rt->overlay_buffer, sx - px, sy - py);
657                                         cairo_rectangle (cr, sx - px, sy - py, sw, sh);
658                                         cairo_fill(cr);
659                                         cairo_destroy(cr);
660                                         }
661                                 }
662                         }
663                 }
664 }
665
666 static void rt_overlay_queue_draw(RendererTiles *rt, OverlayData *od, gint x1, gint y1, gint x2, gint y2)
667 {
668         PixbufRenderer *pr = rt->pr;
669         gint x, y, w, h;
670
671         rt_overlay_get_position(rt, od, &x, &y, &w, &h);
672         
673         /* add borders */
674         x -= x1;
675         y -= y1;
676         w += x1 + x2;
677         h += y1 + y2;
678         
679         rt_queue(rt, rt->x_scroll - pr->x_offset + x,
680                  rt->y_scroll - pr->y_offset + y,
681                  w, h,
682                  FALSE, TILE_RENDER_ALL, FALSE, FALSE);
683
684         rt_border_draw(rt, x, y, w, h);
685 }
686
687 static void rt_overlay_queue_all(RendererTiles *rt, gint x1, gint y1, gint x2, gint y2)
688 {
689         GList *work;
690
691         work = rt->overlay_list;
692         while (work)
693                 {
694                 OverlayData *od = work->data;
695                 work = work->next;
696
697                 rt_overlay_queue_draw(rt, od, x1, y1, x2, y2);
698                 }
699 }
700
701 static void rt_overlay_update_sizes(RendererTiles *rt)
702 {
703         GList *work;
704
705         work = rt->overlay_list;
706         while (work)
707                 {
708                 OverlayData *od = work->data;
709                 work = work->next;
710                 
711                 if (!od->window) rt_overlay_init_window(rt, od);
712                 
713                 if (od->flags & OVL_RELATIVE)
714                         {
715                         gint x, y, w, h;
716
717                         rt_overlay_get_position(rt, od, &x, &y, &w, &h);
718                         gdk_window_move_resize(od->window, x + rt->stereo_off_x, y + rt->stereo_off_y, w, h);
719                         }
720                 }
721 }
722
723 static OverlayData *rt_overlay_find(RendererTiles *rt, gint id)
724 {
725         GList *work;
726
727         work = rt->overlay_list;
728         while (work)
729                 {
730                 OverlayData *od = work->data;
731                 work = work->next;
732
733                 if (od->id == id) return od;
734                 }
735
736         return NULL;
737 }
738
739
740 gint renderer_tiles_overlay_add(void *renderer, GdkPixbuf *pixbuf, gint x, gint y,
741                                  OverlayRendererFlags flags)
742 {
743         RendererTiles *rt = (RendererTiles *) renderer;
744         PixbufRenderer *pr = rt->pr;
745         OverlayData *od;
746         gint id;
747
748         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
749         g_return_val_if_fail(pixbuf != NULL, -1);
750
751         id = 1;
752         while (rt_overlay_find(rt, id)) id++;
753
754         od = g_new0(OverlayData, 1);
755         od->id = id;
756         od->pixbuf = pixbuf;
757         g_object_ref(G_OBJECT(od->pixbuf));
758         od->x = x;
759         od->y = y;
760         od->flags = flags;
761
762         rt_overlay_init_window(rt, od);
763         
764         rt->overlay_list = g_list_append(rt->overlay_list, od);
765
766         rt_overlay_queue_draw(rt, od, 0, 0, 0, 0);
767
768         return od->id;
769 }
770
771 static void rt_overlay_free(RendererTiles *rt, OverlayData *od)
772 {
773         rt->overlay_list = g_list_remove(rt->overlay_list, od);
774
775         if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
776         if (od->window) gdk_window_destroy(od->window);
777         g_free(od);
778
779         if (!rt->overlay_list && rt->overlay_buffer)
780                 {
781                 cairo_surface_destroy(rt->overlay_buffer);
782                 rt->overlay_buffer = NULL;
783                 }
784 }
785
786 static void rt_overlay_list_clear(RendererTiles *rt)
787 {
788         while (rt->overlay_list)
789                 {
790                 OverlayData *od;
791
792                 od = rt->overlay_list->data;
793                 rt_overlay_free(rt, od);
794                 }
795 }
796
797 static void rt_overlay_list_reset_window(RendererTiles *rt)
798 {
799         GList *work;
800
801         if (rt->overlay_buffer) cairo_surface_destroy(rt->overlay_buffer);
802         rt->overlay_buffer = NULL;
803
804         work = rt->overlay_list;
805         while (work)
806                 {
807                 OverlayData *od = work->data;
808                 work = work->next;
809                 if (od->window) gdk_window_destroy(od->window);
810                 od->window = NULL;
811                 }
812 }
813
814 void renderer_tiles_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint x, gint y)
815 {
816         RendererTiles *rt = (RendererTiles *) renderer;
817         PixbufRenderer *pr = rt->pr;
818         OverlayData *od;
819
820         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
821
822         od = rt_overlay_find(rt, id);
823         if (!od) return;
824
825         if (pixbuf)
826                 {
827                 gint px, py, pw, ph;
828
829                 g_object_ref(G_OBJECT(pixbuf));
830                 g_object_unref(G_OBJECT(od->pixbuf));
831                 od->pixbuf = pixbuf;
832
833                 od->x = x;
834                 od->y = y;
835
836                 if (!od->window) rt_overlay_init_window(rt, od);
837
838                 rt_overlay_queue_draw(rt, od, 0, 0, 0, 0);
839                 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
840                 gdk_window_move_resize(od->window, px + rt->stereo_off_x, py + rt->stereo_off_y, pw, ph);
841                 }
842         else
843                 {
844                 rt_overlay_queue_draw(rt, od, 0, 0, 0, 0);
845                 rt_overlay_free(rt, od);
846                 }
847 }
848
849 gboolean renderer_tiles_overlay_get(void *renderer, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
850 {
851         RendererTiles *rt = (RendererTiles *) renderer;
852         PixbufRenderer *pr = rt->pr;
853         OverlayData *od;
854
855         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
856
857         od = rt_overlay_find(rt, id);
858         if (!od) return FALSE;
859
860         if (pixbuf) *pixbuf = od->pixbuf;
861         if (x) *x = od->x;
862         if (y) *y = od->y;
863
864         return TRUE;
865 }
866
867 static void rt_hierarchy_changed_cb(GtkWidget *widget, GtkWidget *previous_toplevel, gpointer data)
868 {
869         RendererTiles *rt = data;
870         rt_overlay_list_reset_window(rt);
871 }
872
873 /*
874  *-------------------------------------------------------------------
875  * drawing
876  *-------------------------------------------------------------------
877  */
878
879 static GdkPixbuf *rt_get_spare_tile(RendererTiles *rt)
880 {
881         if (!rt->spare_tile) rt->spare_tile = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, rt->tile_width, rt->tile_height);
882         return rt->spare_tile;
883 }
884
885 #define COLOR_BYTES 3   /* rgb */
886
887 static void rt_tile_rotate_90_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
888 {
889         GdkPixbuf *src = *tile;
890         GdkPixbuf *dest;
891         gint srs, drs;
892         guchar *s_pix, *d_pix;
893         guchar *sp, *dp;
894         guchar *ip, *spi, *dpi;
895         gint i, j;
896         gint tw = rt->tile_width;
897
898         srs = gdk_pixbuf_get_rowstride(src);
899         s_pix = gdk_pixbuf_get_pixels(src);
900         spi = s_pix + (x * COLOR_BYTES);
901
902         dest = rt_get_spare_tile(rt);
903         drs = gdk_pixbuf_get_rowstride(dest);
904         d_pix = gdk_pixbuf_get_pixels(dest);
905         dpi = d_pix + (tw - 1) * COLOR_BYTES;
906
907         for (i = y; i < y + h; i++)
908                 {
909                 sp = spi + (i * srs);
910                 ip = dpi - (i * COLOR_BYTES);
911                 for (j = x; j < x + w; j++)
912                         {
913                         dp = ip + (j * drs);
914                         memcpy(dp, sp, COLOR_BYTES);
915                         sp += COLOR_BYTES;
916                         }
917                 }
918
919         rt->spare_tile = src;
920         *tile = dest;
921 }
922
923 static void rt_tile_rotate_90_counter_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
924 {
925         GdkPixbuf *src = *tile;
926         GdkPixbuf *dest;
927         gint srs, drs;
928         guchar *s_pix, *d_pix;
929         guchar *sp, *dp;
930         guchar *ip, *spi, *dpi;
931         gint i, j;
932         gint th = rt->tile_height;
933
934         srs = gdk_pixbuf_get_rowstride(src);
935         s_pix = gdk_pixbuf_get_pixels(src);
936         spi = s_pix + (x * COLOR_BYTES);
937
938         dest = rt_get_spare_tile(rt);
939         drs = gdk_pixbuf_get_rowstride(dest);
940         d_pix = gdk_pixbuf_get_pixels(dest);
941         dpi = d_pix + (th - 1) * drs;
942
943         for (i = y; i < y + h; i++)
944                 {
945                 sp = spi + (i * srs);
946                 ip = dpi + (i * COLOR_BYTES);
947                 for (j = x; j < x + w; j++)
948                         {
949                         dp = ip - (j * drs);
950                         memcpy(dp, sp, COLOR_BYTES);
951                         sp += COLOR_BYTES;
952                         }
953                 }
954
955         rt->spare_tile = src;
956         *tile = dest;
957 }
958
959 static void rt_tile_mirror_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
960 {
961         GdkPixbuf *src = *tile;
962         GdkPixbuf *dest;
963         gint srs, drs;
964         guchar *s_pix, *d_pix;
965         guchar *sp, *dp;
966         guchar *spi, *dpi;
967         gint i, j;
968
969         gint tw = rt->tile_width;
970
971         srs = gdk_pixbuf_get_rowstride(src);
972         s_pix = gdk_pixbuf_get_pixels(src);
973         spi = s_pix + (x * COLOR_BYTES);
974
975         dest = rt_get_spare_tile(rt);
976         drs = gdk_pixbuf_get_rowstride(dest);
977         d_pix = gdk_pixbuf_get_pixels(dest);
978         dpi =  d_pix + (tw - x - 1) * COLOR_BYTES;
979
980         for (i = y; i < y + h; i++)
981                 {
982                 sp = spi + (i * srs);
983                 dp = dpi + (i * drs);
984                 for (j = 0; j < w; j++)
985                         {
986                         memcpy(dp, sp, COLOR_BYTES);
987                         sp += COLOR_BYTES;
988                         dp -= COLOR_BYTES;
989                         }
990                 }
991
992         rt->spare_tile = src;
993         *tile = dest;
994 }
995
996 static void rt_tile_mirror_and_flip(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
997 {
998         GdkPixbuf *src = *tile;
999         GdkPixbuf *dest;
1000         gint srs, drs;
1001         guchar *s_pix, *d_pix;
1002         guchar *sp, *dp;
1003         guchar *spi, *dpi;
1004         gint i, j;
1005         gint tw = rt->tile_width;
1006         gint th = rt->tile_height;
1007
1008         srs = gdk_pixbuf_get_rowstride(src);
1009         s_pix = gdk_pixbuf_get_pixels(src);
1010         spi = s_pix + (x * COLOR_BYTES);
1011
1012         dest = rt_get_spare_tile(rt);
1013         drs = gdk_pixbuf_get_rowstride(dest);
1014         d_pix = gdk_pixbuf_get_pixels(dest);
1015         dpi = d_pix + (th - 1) * drs + (tw - 1) * COLOR_BYTES;
1016
1017         for (i = y; i < y + h; i++)
1018                 {
1019                 sp = s_pix + (i * srs) + (x * COLOR_BYTES);
1020                 dp = dpi - (i * drs) - (x * COLOR_BYTES);
1021                 for (j = 0; j < w; j++)
1022                         {
1023                         memcpy(dp, sp, COLOR_BYTES);
1024                         sp += COLOR_BYTES;
1025                         dp -= COLOR_BYTES;
1026                         }
1027                 }
1028
1029         rt->spare_tile = src;
1030         *tile = dest;
1031 }
1032
1033 static void rt_tile_flip_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1034 {
1035         GdkPixbuf *src = *tile;
1036         GdkPixbuf *dest;
1037         gint srs, drs;
1038         guchar *s_pix, *d_pix;
1039         guchar *sp, *dp;
1040         guchar *spi, *dpi;
1041         gint i;
1042         gint th = rt->tile_height;
1043
1044         srs = gdk_pixbuf_get_rowstride(src);
1045         s_pix = gdk_pixbuf_get_pixels(src);
1046         spi = s_pix + (x * COLOR_BYTES);
1047
1048         dest = rt_get_spare_tile(rt);
1049         drs = gdk_pixbuf_get_rowstride(dest);
1050         d_pix = gdk_pixbuf_get_pixels(dest);
1051         dpi = d_pix + (th - 1) * drs + (x * COLOR_BYTES);
1052
1053         for (i = y; i < y + h; i++)
1054                 {
1055                 sp = spi + (i * srs);
1056                 dp = dpi - (i * drs);
1057                 memcpy(dp, sp, w * COLOR_BYTES);
1058                 }
1059
1060         rt->spare_tile = src;
1061         *tile = dest;
1062 }
1063
1064 static void rt_tile_apply_orientation(RendererTiles *rt, gint orientation, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h)
1065 {
1066         switch (orientation)
1067                 {
1068                 case EXIF_ORIENTATION_TOP_LEFT:
1069                         /* normal -- nothing to do */
1070                         break;
1071                 case EXIF_ORIENTATION_TOP_RIGHT:
1072                         /* mirrored */
1073                         {
1074                                 rt_tile_mirror_only(rt, pixbuf, x, y, w, h);
1075                         }
1076                         break;
1077                 case EXIF_ORIENTATION_BOTTOM_RIGHT:
1078                         /* upside down */
1079                         {
1080                                 rt_tile_mirror_and_flip(rt, pixbuf, x, y, w, h);
1081                         }
1082                         break;
1083                 case EXIF_ORIENTATION_BOTTOM_LEFT:
1084                         /* flipped */
1085                         {
1086                                 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1087                         }
1088                         break;
1089                 case EXIF_ORIENTATION_LEFT_TOP:
1090                         {
1091                                 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1092                                 rt_tile_rotate_90_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1093                         }
1094                         break;
1095                 case EXIF_ORIENTATION_RIGHT_TOP:
1096                         /* rotated -90 (270) */
1097                         {
1098                                 rt_tile_rotate_90_clockwise(rt, pixbuf, x, y, w, h);
1099                         }
1100                         break;
1101                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1102                         {
1103                                 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1104                                 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1105                         }
1106                         break;
1107                 case EXIF_ORIENTATION_LEFT_BOTTOM:
1108                         /* rotated 90 */
1109                         {
1110                                 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, y, w, h);
1111                         }
1112                         break;
1113                 default:
1114                         /* The other values are out of range */
1115                         break;
1116                 }
1117 }
1118
1119 static gboolean rt_source_tile_render(RendererTiles *rt, ImageTile *it,
1120                                       gint x, gint y, gint w, gint h,
1121                                       gboolean new_data, gboolean fast)
1122 {
1123         PixbufRenderer *pr = rt->pr;
1124         GtkWidget *box;
1125         GList *list;
1126         GList *work;
1127         gboolean draw = FALSE;
1128
1129         box = GTK_WIDGET(pr);
1130
1131         if (pr->zoom == 1.0 || pr->scale == 1.0)
1132                 {
1133                 list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE);
1134                 work = list;
1135                 while (work)
1136                         {
1137                         SourceTile *st;
1138                         gint rx, ry, rw, rh;
1139
1140                         st = work->data;
1141                         work = work->next;
1142
1143                         if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1144                                            it->x + x, it->y + y, w, h,
1145                                            &rx, &ry, &rw, &rh))
1146                                 {
1147                                 cairo_t *cr;
1148                                 cr = cairo_create(it->surface);
1149                                 cairo_rectangle (cr, rx - it->x, ry - it->y, rw, rh);
1150
1151                                 if (st->blank)
1152                                         {
1153                                         cairo_set_source_rgb(cr, 0, 0, 0);
1154                                         }
1155                                 else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
1156                                         {
1157                                         gdk_cairo_set_source_pixbuf(cr, st->pixbuf, -it->x + st->x, -it->y + st->y);
1158                                         }
1159                                 cairo_fill (cr);
1160                                 cairo_destroy (cr);
1161                                 }
1162                         }
1163                 }
1164         else
1165                 {
1166                 gdouble scale_x, scale_y;
1167                 gint sx, sy, sw, sh;
1168
1169                 if (pr->image_width == 0 || pr->image_height == 0) return FALSE;
1170                 scale_x = (gdouble)pr->width / pr->image_width;
1171                 scale_y = (gdouble)pr->height / pr->image_height;
1172
1173                 sx = (gdouble)(it->x + x) / scale_x;
1174                 sy = (gdouble)(it->y + y) / scale_y;
1175                 sw = (gdouble)w / scale_x;
1176                 sh = (gdouble)h / scale_y;
1177
1178                 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1179
1180 #if 0
1181                 /* draws red over draw region, to check for leaks (regions not filled) */
1182                 pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255);
1183 #endif
1184
1185                 list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE);
1186                 work = list;
1187                 while (work)
1188                         {
1189                         SourceTile *st;
1190                         gint rx, ry, rw, rh;
1191                         gint stx, sty, stw, sth;
1192
1193                         st = work->data;
1194                         work = work->next;
1195
1196                         stx = floor((gdouble)st->x * scale_x);
1197                         sty = floor((gdouble)st->y * scale_y);
1198                         stw = ceil((gdouble)(st->x + pr->source_tile_width) * scale_x) - stx;
1199                         sth = ceil((gdouble)(st->y + pr->source_tile_height) * scale_y) - sty;
1200
1201                         if (pr_clip_region(stx, sty, stw, sth,
1202                                            it->x + x, it->y + y, w, h,
1203                                            &rx, &ry, &rw, &rh))
1204                                 {
1205
1206                                 if (st->blank)
1207                                         {
1208                                         cairo_t *cr;
1209                                         cr = cairo_create(it->surface);
1210                                         cairo_rectangle (cr, rx - st->x, ry - st->y, rw, rh);
1211                                         cairo_set_source_rgb(cr, 0, 0, 0);
1212                                         cairo_fill (cr);
1213                                         cairo_destroy (cr);
1214                                         }
1215                                 else
1216                                         {
1217                                         gdouble offset_x;
1218                                         gdouble offset_y;
1219
1220                                         /* may need to use unfloored stx,sty values here */
1221                                         offset_x = (gdouble)(stx - it->x);
1222                                         offset_y = (gdouble)(sty - it->y);
1223
1224                                         gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh,
1225                                                  (gdouble) 0.0 + offset_x,
1226                                                  (gdouble) 0.0 + offset_y,
1227                                                  scale_x, scale_y,
1228                                                  (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1229                                         draw = TRUE;
1230                                         }
1231                                 }
1232                         }
1233                 }
1234
1235         g_list_free(list);
1236
1237         return draw;
1238 }
1239
1240 static void rt_tile_get_region(gboolean has_alpha, 
1241                                const GdkPixbuf *src, GdkPixbuf *dest,
1242                                int pb_x, int pb_y, int pb_w, int pb_h,
1243                                double offset_x, double offset_y, double scale_x, double scale_y,
1244                                GdkInterpType interp_type,
1245                                int check_x, int check_y)
1246 {
1247         if (!has_alpha)
1248                 {
1249                 if (scale_x == 1.0 && scale_y == 1.0)
1250                         {
1251                         gdk_pixbuf_copy_area(src,
1252                                              -offset_x + pb_x, -offset_y + pb_y,
1253                                              pb_w, pb_h,
1254                                              dest,
1255                                              pb_x, pb_y);
1256                         } 
1257                 else
1258                         {
1259                         gdk_pixbuf_scale(src, dest, 
1260                                          pb_x, pb_y, pb_w, pb_h,
1261                                          offset_x,
1262                                          offset_y,
1263                                          scale_x, scale_y,
1264                                          interp_type);
1265                         }
1266                 }
1267         else
1268                 {
1269                 gdk_pixbuf_composite_color(src, dest, 
1270                                          pb_x, pb_y, pb_w, pb_h,
1271                                          offset_x,
1272                                          offset_y,
1273                                          scale_x, scale_y,
1274                                          interp_type,
1275                                          255, check_x, check_y,
1276                                          PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
1277                 }
1278 }
1279
1280
1281 static gint rt_get_orientation(RendererTiles *rt)
1282 {
1283         PixbufRenderer *pr = rt->pr;
1284
1285         gint orientation = pr->orientation;
1286         static const gint mirror[]       = {1,   2, 1, 4, 3, 6, 5, 8, 7};
1287         static const gint flip[]         = {1,   4, 3, 2, 1, 8, 7, 6, 5};
1288
1289         if (rt->stereo_mode & PR_STEREO_MIRROR) orientation = mirror[orientation];
1290         if (rt->stereo_mode & PR_STEREO_FLIP) orientation = flip[orientation];
1291         return orientation;
1292 }
1293
1294
1295 static void rt_tile_render(RendererTiles *rt, ImageTile *it,
1296                            gint x, gint y, gint w, gint h,
1297                            gboolean new_data, gboolean fast)
1298 {
1299         PixbufRenderer *pr = rt->pr;
1300         GtkWidget *box;
1301         gboolean has_alpha;
1302         gboolean draw = FALSE;
1303         gint orientation = rt_get_orientation(rt);
1304
1305         if (it->render_todo == TILE_RENDER_NONE && it->surface && !new_data) return;
1306
1307         if (it->render_done != TILE_RENDER_ALL)
1308                 {
1309                 x = 0;
1310                 y = 0;
1311                 w = it->w;
1312                 h = it->h;
1313                 if (!fast) it->render_done = TILE_RENDER_ALL;
1314                 }
1315         else if (it->render_todo != TILE_RENDER_AREA)
1316                 {
1317                 if (!fast) it->render_todo = TILE_RENDER_NONE;
1318                 return;
1319                 }
1320
1321         if (!fast) it->render_todo = TILE_RENDER_NONE;
1322
1323         if (new_data) it->blank = FALSE;
1324
1325         rt_tile_prepare(rt, it);
1326         has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
1327
1328         box = GTK_WIDGET(pr);
1329
1330         /* FIXME checker colors for alpha should be configurable,
1331          * also should be drawn for blank = TRUE
1332          */
1333
1334         if (it->blank)
1335                 {
1336                 /* no data, do fast rect fill */
1337                 cairo_t *cr;
1338                 cr = cairo_create(it->surface);
1339                 cairo_rectangle (cr, 0, 0, it->w, it->h);
1340                 cairo_set_source_rgb(cr, 0, 0, 0);
1341                 cairo_fill (cr);
1342                 cairo_destroy (cr);
1343 #if 0
1344                 gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1345                                    0, 0, it->w, it->h);
1346 #endif
1347                 }
1348         else if (pr->source_tiles_enabled)
1349                 {
1350                 draw = rt_source_tile_render(rt, it, x, y, w, h, new_data, fast);
1351                 }
1352         else if ((pr->zoom == 1.0 || pr->scale == 1.0) &&
1353                  pr->aspect_ratio == 1.0 &&
1354                  !has_alpha &&
1355                  orientation == EXIF_ORIENTATION_TOP_LEFT && 
1356                  !(pr->func_post_process && !(pr->post_process_slow && fast)) &&
1357                  !(rt->stereo_mode & PR_STEREO_ANAGLYPH))
1358                 {
1359                 /* special case: faster, simple, scale 1.0, base orientation, no postprocessing */
1360                 cairo_t *cr;
1361                 cr = cairo_create(it->surface);
1362                 cairo_rectangle (cr, x, y, w, h);
1363                 gdk_cairo_set_source_pixbuf(cr, pr->pixbuf, -it->x - GET_RIGHT_PIXBUF_OFFSET(rt), -it->y);
1364                 cairo_fill (cr);
1365                 cairo_destroy (cr);
1366                 }
1367         else
1368                 {
1369                 gdouble scale_x, scale_y;
1370                 gdouble src_x, src_y;
1371                 gint pb_x, pb_y;
1372                 gint pb_w, pb_h;
1373
1374                 if (pr->image_width == 0 || pr->image_height == 0) return;
1375
1376                 scale_x = (gdouble)pr->width / pr->image_width;
1377                 scale_y = (gdouble)pr->height / pr->image_height;
1378
1379                 pr_tile_coords_map_orientation(orientation, it->x, it->y,
1380                                             pr->width, pr->height,
1381                                             rt->tile_width, rt->tile_height,
1382                                             &src_x, &src_y);
1383                 pr_tile_region_map_orientation(orientation, x, y,
1384                                             rt->tile_width, rt->tile_height,
1385                                             w, h,
1386                                             &pb_x, &pb_y,
1387                                             &pb_w, &pb_h);
1388
1389                 switch (orientation)
1390                         {
1391                         gdouble tmp;
1392                         case EXIF_ORIENTATION_LEFT_TOP:
1393                         case EXIF_ORIENTATION_RIGHT_TOP:
1394                         case EXIF_ORIENTATION_RIGHT_BOTTOM:
1395                         case EXIF_ORIENTATION_LEFT_BOTTOM:
1396                                 tmp = scale_x;
1397                                 scale_x = scale_y;
1398                                 scale_y = tmp;
1399                                 break;
1400                         default:
1401                                 /* nothing to do */
1402                                 break;
1403                         }
1404                 
1405                 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
1406                  * small sizes for anything but GDK_INTERP_NEAREST
1407                  */
1408                 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1409
1410                 rt_tile_get_region(has_alpha,
1411                                    pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
1412                                    (gdouble) 0.0 - src_x - GET_RIGHT_PIXBUF_OFFSET(rt) * scale_x,
1413                                    (gdouble) 0.0 - src_y,
1414                                    scale_x, scale_y,
1415                                    (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1416                                    it->x + pb_x, it->y + pb_y);
1417                 if (rt->stereo_mode & PR_STEREO_ANAGLYPH && 
1418                     (pr->stereo_pixbuf_offset_right > 0 || pr->stereo_pixbuf_offset_left > 0))
1419                         {
1420                         GdkPixbuf *right_pb = rt_get_spare_tile(rt);
1421                         rt_tile_get_region(has_alpha,
1422                                            pr->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h,
1423                                            (gdouble) 0.0 - src_x - GET_LEFT_PIXBUF_OFFSET(rt) * scale_x,
1424                                            (gdouble) 0.0 - src_y,
1425                                            scale_x, scale_y,
1426                                            (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1427                                            it->x + pb_x, it->y + pb_y);
1428                         pr_create_anaglyph(rt->stereo_mode, it->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h);
1429                         /* do not care about freeing spare_tile, it will be reused */
1430                         }
1431                 rt_tile_apply_orientation(rt, orientation, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
1432                 draw = TRUE;
1433                 }
1434
1435         if (draw && it->pixbuf && !it->blank)
1436                 {
1437                 cairo_t *cr;
1438
1439                 if (pr->func_post_process && !(pr->post_process_slow && fast))
1440                         pr->func_post_process(pr, &it->pixbuf, x, y, w, h, pr->post_process_user_data);
1441
1442                 cr = cairo_create(it->surface);
1443                 cairo_rectangle (cr, x, y, w, h);
1444                 gdk_cairo_set_source_pixbuf(cr, it->pixbuf, 0, 0);
1445                 cairo_fill (cr);
1446                 cairo_destroy (cr);
1447                 }
1448 }
1449
1450
1451 static void rt_tile_expose(RendererTiles *rt, ImageTile *it,
1452                            gint x, gint y, gint w, gint h,
1453                            gboolean new_data, gboolean fast)
1454 {
1455         PixbufRenderer *pr = rt->pr;
1456         GtkWidget *box;
1457         GdkWindow *window;
1458         cairo_t *cr;
1459
1460         /* clamp to visible */
1461         if (it->x + x < rt->x_scroll)
1462                 {
1463                 w -= rt->x_scroll - it->x - x;
1464                 x = rt->x_scroll - it->x;
1465                 }
1466         if (it->x + x + w > rt->x_scroll + pr->vis_width)
1467                 {
1468                 w = rt->x_scroll + pr->vis_width - it->x - x; 
1469                 }
1470         if (w < 1) return;
1471         if (it->y + y < rt->y_scroll)
1472                 {
1473                 h -= rt->y_scroll - it->y - y;
1474                 y = rt->y_scroll - it->y;
1475                 }
1476         if (it->y + y + h > rt->y_scroll + pr->vis_height)
1477                 {
1478                 h = rt->y_scroll + pr->vis_height - it->y - y; 
1479                 }
1480         if (h < 1) return;
1481
1482         rt_tile_render(rt, it, x, y, w, h, new_data, fast);
1483
1484         box = GTK_WIDGET(pr);
1485         window = gtk_widget_get_window(box);
1486
1487         cr = gdk_cairo_create(window);
1488         cairo_set_source_surface(cr, it->surface, pr->x_offset + (it->x - rt->x_scroll) + rt->stereo_off_x, pr->y_offset + (it->y - rt->y_scroll) + rt->stereo_off_y);
1489         cairo_rectangle (cr, pr->x_offset + (it->x - rt->x_scroll) + x + rt->stereo_off_x, pr->y_offset + (it->y - rt->y_scroll) + y + rt->stereo_off_y, w, h);
1490         cairo_fill (cr);
1491         cairo_destroy (cr);
1492
1493 #if 0
1494 #if GTK_CHECK_VERSION(2,20,0)
1495         gdk_draw_drawable(window, box->style->fg_gc[gtk_widget_get_state(box)],
1496 #else
1497         gdk_draw_drawable(window, box->style->fg_gc[GTK_WIDGET_STATE(box)],
1498 #endif
1499                           it->pixmap, x, y,
1500                           pr->x_offset + (it->x - rt->x_scroll) + x + rt->stereo_off_x, pr->y_offset + (it->y - rt->y_scroll) + y + rt->stereo_off_y, w, h);
1501 #endif
1502
1503         if (rt->overlay_list)
1504                 {
1505                 rt_overlay_draw(rt, pr->x_offset + (it->x - rt->x_scroll) + x,
1506                                 pr->y_offset + (it->y - rt->y_scroll) + y,
1507                                 w, h,
1508                                 it);
1509                 }
1510 }
1511
1512
1513 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it)
1514 {
1515         PixbufRenderer *pr = rt->pr;
1516         return (it->x + it->w >= rt->x_scroll && it->x < rt->x_scroll + pr->vis_width &&
1517                 it->y + it->h >= rt->y_scroll && it->y < rt->y_scroll + pr->vis_height);
1518 }
1519
1520 /*
1521  *-------------------------------------------------------------------
1522  * draw queue
1523  *-------------------------------------------------------------------
1524  */
1525
1526 static gint rt_get_queued_area(GList *work)
1527 {
1528         gint area = 0;
1529         
1530         while (work) 
1531                 {
1532                 QueueData *qd = work->data;
1533                 area += qd->w * qd->h;
1534                 work = work->next;
1535                 }
1536         return area;
1537 }
1538
1539
1540 static gboolean rt_queue_schedule_next_draw(RendererTiles *rt, gboolean force_set)
1541 {
1542         PixbufRenderer *pr = rt->pr;
1543         gfloat percent;
1544         gint visible_area = pr->vis_width * pr->vis_height;
1545         
1546         if (!pr->loading)
1547                 {
1548                 /* 2pass prio */ 
1549                 DEBUG_2("redraw priority: 2pass");
1550                 rt->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, rt_queue_draw_idle_cb, rt, NULL);
1551                 return FALSE;
1552                 }
1553         
1554         if (visible_area == 0)
1555                 {
1556                 /* not known yet */
1557                 percent = 100.0;
1558                 }
1559         else
1560                 {
1561                 percent = 100.0 * rt_get_queued_area(rt->draw_queue) / visible_area;
1562                 }
1563         
1564         if (percent > 10.0)
1565                 {
1566                 /* we have enough data for starting intensive redrawing */
1567                 DEBUG_2("redraw priority: high %.2f %%", percent);
1568                 rt->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW, rt_queue_draw_idle_cb, rt, NULL);
1569                 return FALSE;
1570                 }
1571         
1572         if (percent < 1.0 || force_set)
1573                 {
1574                 /* queue is (almost) empty, wait  50 ms*/
1575                 DEBUG_2("redraw priority: wait %.2f %%", percent);
1576                 rt->draw_idle_id = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, 50, rt_queue_draw_idle_cb, rt, NULL);
1577                 return FALSE;
1578                 }
1579         
1580         /* keep the same priority as before */
1581         DEBUG_2("redraw priority: no change %.2f %%", percent);
1582         return TRUE;
1583 }
1584                 
1585
1586 static gboolean rt_queue_draw_idle_cb(gpointer data)
1587 {
1588         RendererTiles *rt = data;
1589         PixbufRenderer *pr = rt->pr;
1590         QueueData *qd;
1591         gboolean fast;
1592
1593
1594         if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
1595             (!rt->draw_queue && !rt->draw_queue_2pass) ||
1596             !rt->draw_idle_id)
1597                 {
1598                 pr_render_complete_signal(pr);
1599
1600                 rt->draw_idle_id = 0;
1601                 return FALSE;
1602                 }
1603
1604         if (rt->draw_queue)
1605                 {
1606                 qd = rt->draw_queue->data;
1607                 fast = (pr->zoom_2pass && ((pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0) || pr->post_process_slow));
1608                 }
1609         else
1610                 {
1611                 if (pr->loading)
1612                         {
1613                         /* still loading, wait till done (also drops the higher priority) */
1614
1615                         return rt_queue_schedule_next_draw(rt, FALSE);
1616                         }
1617
1618                 qd = rt->draw_queue_2pass->data;
1619                 fast = FALSE;
1620                 }
1621
1622 #if GTK_CHECK_VERSION(2,20,0)
1623         if (gtk_widget_get_realized(pr))
1624 #else
1625         if (GTK_WIDGET_REALIZED(pr))
1626 #endif
1627                 {
1628                 if (rt_tile_is_visible(rt, qd->it))
1629                         {
1630                         rt_tile_expose(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1631                         }
1632                 else if (qd->new_data)
1633                         {
1634                         /* if new pixel data, and we already have a pixmap, update the tile */
1635                         qd->it->blank = FALSE;
1636                         if (qd->it->surface && qd->it->render_done == TILE_RENDER_ALL)
1637                                 {
1638                                 rt_tile_render(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1639                                 }
1640                         }
1641                 }
1642
1643         if (rt->draw_queue)
1644                 {
1645                 qd->it->qd = NULL;
1646                 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
1647                 if (fast)
1648                         {
1649                         if (qd->it->qd2)
1650                                 {
1651                                 rt_queue_merge(qd->it->qd2, qd);
1652                                 g_free(qd);
1653                                 }
1654                         else
1655                                 {
1656                                 qd->it->qd2 = qd;
1657                                 rt->draw_queue_2pass = g_list_append(rt->draw_queue_2pass, qd);
1658                                 }
1659                         }
1660                 else
1661                         {
1662                         g_free(qd);
1663                         }
1664                 }
1665         else
1666                 {
1667                 qd->it->qd2 = NULL;
1668                 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
1669                 g_free(qd);
1670                 }
1671
1672         if (!rt->draw_queue && !rt->draw_queue_2pass)
1673                 {
1674                 pr_render_complete_signal(pr);
1675
1676                 rt->draw_idle_id = 0;
1677                 return FALSE;
1678                 }
1679
1680                 return rt_queue_schedule_next_draw(rt, FALSE);
1681 }
1682
1683 static void rt_queue_list_free(GList *list)
1684 {
1685         GList *work;
1686
1687         work = list;
1688         while (work)
1689                 {
1690                 QueueData *qd;
1691
1692                 qd = work->data;
1693                 work = work->next;
1694
1695                 qd->it->qd = NULL;
1696                 qd->it->qd2 = NULL;
1697                 g_free(qd);
1698                 }
1699
1700         g_list_free(list);
1701 }
1702
1703 static void rt_queue_clear(RendererTiles *rt)
1704 {
1705         rt_queue_list_free(rt->draw_queue);
1706         rt->draw_queue = NULL;
1707
1708         rt_queue_list_free(rt->draw_queue_2pass);
1709         rt->draw_queue_2pass = NULL;
1710
1711         if (rt->draw_idle_id)
1712                 {
1713                 g_source_remove(rt->draw_idle_id);
1714                 rt->draw_idle_id = 0;
1715                 }
1716         rt_sync_scroll(rt);
1717 }
1718
1719 static void rt_queue_merge(QueueData *parent, QueueData *qd)
1720 {
1721         if (parent->x + parent->w < qd->x + qd->w)
1722                 {
1723                 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
1724                 }
1725         if (parent->x > qd->x)
1726                 {
1727                 parent->w += parent->x - qd->x;
1728                 parent->x = qd->x;
1729                 }
1730
1731         if (parent->y + parent->h < qd->y + qd->h)
1732                 {
1733                 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
1734                 }
1735         if (parent->y > qd->y)
1736                 {
1737                 parent->h += parent->y - qd->y;
1738                 parent->y = qd->y;
1739                 }
1740
1741         parent->new_data |= qd->new_data;
1742 }
1743
1744 static gboolean rt_clamp_to_visible(RendererTiles *rt, gint *x, gint *y, gint *w, gint *h)
1745 {
1746         PixbufRenderer *pr = rt->pr;
1747         gint nx, ny;
1748         gint nw, nh;
1749         gint vx, vy;
1750         gint vw, vh;
1751
1752         vw = pr->vis_width;
1753         vh = pr->vis_height;
1754
1755         vx = rt->x_scroll;
1756         vy = rt->y_scroll;
1757
1758         if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
1759
1760         /* now clamp it */
1761         nx = CLAMP(*x, vx, vx + vw);
1762         nw = CLAMP(*w - (nx - *x), 1, vw);
1763
1764         ny = CLAMP(*y, vy, vy + vh);
1765         nh = CLAMP(*h - (ny - *y), 1, vh);
1766
1767         *x = nx;
1768         *y = ny;
1769         *w = nw;
1770         *h = nh;
1771
1772         return TRUE;
1773 }
1774
1775 static gboolean rt_queue_to_tiles(RendererTiles *rt, gint x, gint y, gint w, gint h,
1776                                   gboolean clamp, ImageRenderType render,
1777                                   gboolean new_data, gboolean only_existing)
1778 {
1779         PixbufRenderer *pr = rt->pr;
1780         gint i, j;
1781         gint x1, x2;
1782         gint y1, y2;
1783
1784         if (clamp && !rt_clamp_to_visible(rt, &x, &y, &w, &h)) return FALSE;
1785
1786         x1 = ROUND_DOWN(x, rt->tile_width);
1787         x2 = ROUND_UP(x + w, rt->tile_width);
1788
1789         y1 = ROUND_DOWN(y, rt->tile_height);
1790         y2 = ROUND_UP(y + h, rt->tile_height);
1791
1792         for (j = y1; j <= y2; j += rt->tile_height)
1793                 {
1794                 for (i = x1; i <= x2; i += rt->tile_width)
1795                         {
1796                         ImageTile *it;
1797
1798                         it = rt_tile_get(rt, i, j,
1799                                          (only_existing &&
1800                                           (i + rt->tile_width < rt->x_scroll ||
1801                                            i > rt->x_scroll + pr->vis_width ||
1802                                            j + rt->tile_height < rt->y_scroll ||
1803                                            j > rt->y_scroll + pr->vis_height)));
1804                         if (it)
1805                                 {
1806                                 QueueData *qd;
1807
1808                                 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
1809                                     (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
1810                                         {
1811                                         it->render_todo = render;
1812                                         }
1813
1814                                 qd = g_new(QueueData, 1);
1815                                 qd->it = it;
1816                                 qd->new_data = new_data;
1817
1818                                 if (i < x)
1819                                         {
1820                                         qd->x = x - i;
1821                                         }
1822                                 else
1823                                         {
1824                                         qd->x = 0;
1825                                         }
1826                                 qd->w = x + w - i - qd->x;
1827                                 if (qd->x + qd->w > rt->tile_width) qd->w = rt->tile_width - qd->x;
1828
1829                                 if (j < y)
1830                                         {
1831                                         qd->y = y - j;
1832                                         }
1833                                 else
1834                                         {
1835                                         qd->y = 0;
1836                                         }
1837                                 qd->h = y + h - j - qd->y;
1838                                 if (qd->y + qd->h > rt->tile_height) qd->h = rt->tile_height - qd->y;
1839
1840                                 if (qd->w < 1 || qd->h < 1)
1841                                         {
1842                                         g_free(qd);
1843                                         }
1844                                 else if (it->qd)
1845                                         {
1846                                         rt_queue_merge(it->qd, qd);
1847                                         g_free(qd);
1848                                         }
1849                                 else
1850                                         {
1851                                         it->qd = qd;
1852                                         rt->draw_queue = g_list_append(rt->draw_queue, qd);
1853                                         }
1854                                 }
1855                         }
1856                 }
1857
1858         return TRUE;
1859 }
1860
1861 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
1862                      gboolean clamp, ImageRenderType render,
1863                      gboolean new_data, gboolean only_existing)
1864 {
1865         PixbufRenderer *pr = rt->pr;
1866         gint nx, ny;
1867
1868         rt_sync_scroll(rt);
1869
1870         nx = CLAMP(x, 0, pr->width - 1);
1871         ny = CLAMP(y, 0, pr->height - 1);
1872         w -= (nx - x);
1873         h -= (ny - y);
1874         w = CLAMP(w, 0, pr->width - nx);
1875         h = CLAMP(h, 0, pr->height - ny);
1876         if (w < 1 || h < 1) return;
1877
1878         if (rt_queue_to_tiles(rt, nx, ny, w, h, clamp, render, new_data, only_existing) &&
1879             ((!rt->draw_queue && !rt->draw_queue_2pass) || !rt->draw_idle_id))
1880                 {
1881                 if (rt->draw_idle_id)
1882                         {
1883                         g_source_remove(rt->draw_idle_id);
1884                         rt->draw_idle_id = 0;
1885                         }
1886                 rt_queue_schedule_next_draw(rt, TRUE);
1887                 }
1888 }
1889
1890 static void rt_scroll(void *renderer, gint x_off, gint y_off)
1891 {
1892         RendererTiles *rt = (RendererTiles *) renderer;
1893         PixbufRenderer *pr = rt->pr;
1894
1895         rt_sync_scroll(rt);
1896         if (rt->stereo_mode & PR_STEREO_MIRROR) x_off = -x_off;
1897         if (rt->stereo_mode & PR_STEREO_FLIP) y_off = -y_off; 
1898
1899         gint w = pr->vis_width - abs(x_off);
1900         gint h = pr->vis_height - abs(y_off);
1901
1902         if (w < 1 || h < 1)
1903                 {
1904                 /* scrolled completely to new material */
1905                 rt_queue(rt, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1906                 return;
1907                 }
1908         else
1909                 {
1910                 gint x1, y1;
1911                 gint x2, y2;
1912                 GtkWidget *box;
1913                 GdkWindow *window;
1914 //              GdkGC *gc;
1915                 GdkEvent *event;
1916                 cairo_t *cr;
1917                 cairo_surface_t *surface;
1918
1919                 if (x_off < 0)
1920                         {
1921                         x1 = abs(x_off);
1922                         x2 = 0;
1923                         }
1924                 else
1925                         {
1926                         x1 = 0;
1927                         x2 = abs(x_off);
1928                         }
1929
1930                 if (y_off < 0)
1931                         {
1932                         y1 = abs(y_off);
1933                         y2 = 0;
1934                         }
1935                 else
1936                         {
1937                         y1 = 0;
1938                         y2 = abs(y_off);
1939                         }
1940
1941                 box = GTK_WIDGET(pr);
1942                 window = gtk_widget_get_window(box);
1943
1944                 cr = gdk_cairo_create(window);
1945                 surface = cairo_get_target(cr);
1946                 /* clipping restricts the intermediate surface's size, so it's a good idea
1947                  * to use it. */
1948                 cairo_rectangle(cr, x1 + pr->x_offset + rt->stereo_off_x, y1 + pr->y_offset + rt->stereo_off_y, w, h);
1949                 cairo_clip (cr);
1950                 /* Now push a group to change the target */
1951                 cairo_push_group (cr);
1952                 cairo_set_source_surface(cr, surface, x1 - x2, y1 - y2);
1953                 cairo_paint(cr);
1954                 /* Now copy the intermediate target back */
1955                 cairo_pop_group_to_source(cr);
1956                 cairo_paint(cr);
1957                 cairo_destroy(cr);
1958 #if 0
1959                 gc = gdk_gc_new(window);
1960                 gdk_gc_set_exposures(gc, TRUE);
1961                 gdk_draw_drawable(window, gc,
1962                                   window,
1963                                   x2 + pr->x_offset + rt->stereo_off_x, y2 + pr->y_offset + rt->stereo_off_y,
1964                                   x1 + pr->x_offset + rt->stereo_off_x, y1 + pr->y_offset + rt->stereo_off_y, w, h);
1965                 g_object_unref(gc);
1966 #endif
1967
1968                 rt_overlay_queue_all(rt, x2, y2, x1, y1);
1969
1970                 w = pr->vis_width - w;
1971                 h = pr->vis_height - h;
1972
1973                 if (w > 0)
1974                         {
1975                         rt_queue(rt,
1976                                     x_off > 0 ? rt->x_scroll + (pr->vis_width - w) : rt->x_scroll, rt->y_scroll,
1977                                     w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1978                         }
1979                 if (h > 0)
1980                         {
1981                         /* FIXME, to optimize this, remove overlap */
1982                         rt_queue(rt,
1983                                     rt->x_scroll, y_off > 0 ? rt->y_scroll + (pr->vis_height - h) : rt->y_scroll,
1984                                     pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1985                         }
1986
1987                 /* process exposures here, "expose_event" seems to miss a few with obstructed windows */
1988 #if ! GTK_CHECK_VERSION(2,18,0)
1989                 while ((event = gdk_event_get_graphics_expose(window)) != NULL)
1990                         {
1991                         renderer_redraw((void *) rt, event->expose.area.x, event->expose.area.y, event->expose.area.width, event->expose.area.height,
1992                               FALSE, TILE_RENDER_ALL, FALSE, FALSE);
1993  
1994                         if (event->expose.count == 0)
1995                                 {
1996                                 gdk_event_free(event);
1997                                 break;
1998                                 }
1999                         gdk_event_free(event);
2000                         }
2001 #endif
2002                 }
2003 }
2004
2005 static void renderer_area_changed(void *renderer, gint src_x, gint src_y, gint src_w, gint src_h)
2006 {
2007         RendererTiles *rt = (RendererTiles *)renderer;
2008         PixbufRenderer *pr = rt->pr;
2009         gint x, y, width, height,  x1, y1, x2, y2;
2010
2011         gint orientation = rt_get_orientation(rt);
2012         pr_coords_map_orientation_reverse(orientation,
2013                                      src_x - GET_RIGHT_PIXBUF_OFFSET(rt), src_y,
2014                                      pr->image_width, pr->image_height,
2015                                      src_w, src_h,
2016                                      &x, &y,
2017                                      &width, &height);
2018
2019         if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
2020                 {
2021                 /* increase region when using a zoom quality that may access surrounding pixels */
2022                 y -= 1;
2023                 height += 2;
2024                 }
2025
2026         x1 = (gint)floor((gdouble)x * pr->scale);
2027         y1 = (gint)floor((gdouble)y * pr->scale * pr->aspect_ratio);
2028         x2 = (gint)ceil((gdouble)(x + width) * pr->scale);
2029         y2 = (gint)ceil((gdouble)(y + height) * pr->scale * pr->aspect_ratio);
2030
2031         rt_queue(rt, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
2032 }
2033
2034 static void renderer_redraw(void *renderer, gint x, gint y, gint w, gint h,
2035                      gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing)
2036 {
2037         RendererTiles *rt = (RendererTiles *)renderer;
2038         PixbufRenderer *pr = rt->pr;
2039
2040         x -= rt->stereo_off_x;
2041         y -= rt->stereo_off_y;
2042         
2043         rt_border_draw(rt, x, y, w, h);
2044
2045         x = MAX(0, x - pr->x_offset + pr->x_scroll);
2046         y = MAX(0, y - pr->y_offset + pr->y_scroll);
2047
2048         rt_queue(rt,
2049                  x, y,
2050                  MIN(w, pr->width - x),
2051                  MIN(h, pr->height - y),
2052                  clamp, render, new_data, only_existing);
2053 }
2054
2055 static void renderer_queue_clear(void *renderer)
2056 {
2057         rt_queue_clear((RendererTiles *)renderer);
2058 }
2059
2060 static void renderer_border_clear(void *renderer)
2061 {
2062         rt_border_clear((RendererTiles *)renderer);
2063 }
2064
2065
2066 static void renderer_invalidate_all(void *renderer)
2067 {
2068         rt_tile_invalidate_all((RendererTiles *)renderer);
2069 }
2070
2071 static void renderer_invalidate_region(void *renderer, gint x, gint y, gint w, gint h)
2072 {
2073         rt_tile_invalidate_region((RendererTiles *)renderer, x, y, w, h);
2074 }
2075
2076 static void renderer_overlay_draw(void *renderer, gint x, gint y, gint w, gint h)
2077 {
2078         rt_overlay_draw((RendererTiles *)renderer, x, y, w, h, NULL);
2079 }
2080
2081 static void renderer_update_sizes(void *renderer)
2082 {
2083         RendererTiles *rt = (RendererTiles *)renderer;
2084
2085         rt->stereo_off_x = 0;
2086         rt->stereo_off_y = 0;
2087         
2088         if (rt->stereo_mode & PR_STEREO_RIGHT) 
2089                 {
2090                 if (rt->stereo_mode & PR_STEREO_HORIZ) 
2091                         {
2092                         rt->stereo_off_x = rt->pr->viewport_width;
2093                         }
2094                 else if (rt->stereo_mode & PR_STEREO_VERT) 
2095                         {
2096                         rt->stereo_off_y = rt->pr->viewport_height;
2097                         }
2098                 else if (rt->stereo_mode & PR_STEREO_FIXED) 
2099                         {
2100                         rt->stereo_off_x = rt->pr->stereo_fixed_x_right;
2101                         rt->stereo_off_y = rt->pr->stereo_fixed_y_right;
2102                         }
2103                 }
2104         else
2105                 {
2106                 if (rt->stereo_mode & PR_STEREO_FIXED) 
2107                         {
2108                         rt->stereo_off_x = rt->pr->stereo_fixed_x_left;
2109                         rt->stereo_off_y = rt->pr->stereo_fixed_y_left;
2110                         }
2111                 }
2112         DEBUG_1("update size: %p  %d %d   %d %d", rt, rt->stereo_off_x, rt->stereo_off_y, rt->pr->viewport_width, rt->pr->viewport_height);
2113         rt_sync_scroll(rt);
2114         rt_overlay_update_sizes(rt);
2115 }
2116
2117 static void renderer_stereo_set(void *renderer, gint stereo_mode)
2118 {
2119         RendererTiles *rt = (RendererTiles *)renderer;
2120
2121         rt->stereo_mode = stereo_mode;
2122 }
2123
2124 static void renderer_free(void *renderer)
2125 {
2126         RendererTiles *rt = (RendererTiles *)renderer;
2127         rt_queue_clear(rt);
2128         rt_tile_free_all(rt);
2129         if (rt->spare_tile) g_object_unref(rt->spare_tile);
2130         if (rt->overlay_buffer) g_object_unref(rt->overlay_buffer);
2131         rt_overlay_list_clear(rt);
2132         /* disconnect "hierarchy-changed" */
2133         g_signal_handlers_disconnect_matched(G_OBJECT(rt->pr), G_SIGNAL_MATCH_DATA,
2134                                                      0, 0, 0, NULL, rt);
2135         g_free(rt);
2136 }
2137
2138 RendererFuncs *renderer_tiles_new(PixbufRenderer *pr)
2139 {
2140         RendererTiles *rt = g_new0(RendererTiles, 1);
2141         
2142         rt->pr = pr;
2143         
2144         rt->f.redraw = renderer_redraw;
2145         rt->f.area_changed = renderer_area_changed;
2146         rt->f.queue_clear = renderer_queue_clear;
2147         rt->f.border_clear = renderer_border_clear;
2148         rt->f.free = renderer_free;
2149         rt->f.invalidate_all = renderer_invalidate_all;
2150         rt->f.invalidate_region = renderer_invalidate_region;
2151         rt->f.scroll = rt_scroll;
2152         rt->f.update_sizes = renderer_update_sizes;
2153
2154
2155         rt->f.overlay_add = renderer_tiles_overlay_add;
2156         rt->f.overlay_set = renderer_tiles_overlay_set;
2157         rt->f.overlay_get = renderer_tiles_overlay_get;
2158         rt->f.overlay_draw = renderer_overlay_draw;
2159
2160         rt->f.stereo_set = renderer_stereo_set;
2161         
2162         rt->tile_width = PR_TILE_SIZE;
2163         rt->tile_height = PR_TILE_SIZE;
2164
2165         rt->tiles = NULL;
2166         rt->tile_cache_size = 0;
2167
2168         rt->tile_cache_max = PR_CACHE_SIZE_DEFAULT;
2169
2170         rt->draw_idle_id = 0;
2171         
2172         rt->stereo_mode = 0;
2173         rt->stereo_off_x = 0;
2174         rt->stereo_off_y = 0;
2175
2176         g_signal_connect(G_OBJECT(pr), "hierarchy-changed",
2177                          G_CALLBACK(rt_hierarchy_changed_cb), rt);
2178
2179         return (RendererFuncs *) rt;
2180 }
2181
2182
2183 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */