dropped deprecated dither_quality code
[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(RendererTiles *rt, GdkPixbuf *pixbuf, gint x, gint y,
741                                  OverlayRendererFlags flags)
742 {
743         PixbufRenderer *pr = rt->pr;
744         OverlayData *od;
745         gint id;
746
747         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
748         g_return_val_if_fail(pixbuf != NULL, -1);
749
750         id = 1;
751         while (rt_overlay_find(rt, id)) id++;
752
753         od = g_new0(OverlayData, 1);
754         od->id = id;
755         od->pixbuf = pixbuf;
756         g_object_ref(G_OBJECT(od->pixbuf));
757         od->x = x;
758         od->y = y;
759         od->flags = flags;
760
761         rt_overlay_init_window(rt, od);
762         
763         rt->overlay_list = g_list_append(rt->overlay_list, od);
764
765         rt_overlay_queue_draw(rt, od, 0, 0, 0, 0);
766
767         return od->id;
768 }
769
770 static void rt_overlay_free(RendererTiles *rt, OverlayData *od)
771 {
772         rt->overlay_list = g_list_remove(rt->overlay_list, od);
773
774         if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
775         if (od->window) gdk_window_destroy(od->window);
776         g_free(od);
777
778         if (!rt->overlay_list && rt->overlay_buffer)
779                 {
780                 cairo_surface_destroy(rt->overlay_buffer);
781                 rt->overlay_buffer = NULL;
782                 }
783 }
784
785 static void rt_overlay_list_clear(RendererTiles *rt)
786 {
787         while (rt->overlay_list)
788                 {
789                 OverlayData *od;
790
791                 od = rt->overlay_list->data;
792                 rt_overlay_free(rt, od);
793                 }
794 }
795
796 static void rt_overlay_list_reset_window(RendererTiles *rt)
797 {
798         GList *work;
799
800         if (rt->overlay_buffer) cairo_surface_destroy(rt->overlay_buffer);
801         rt->overlay_buffer = NULL;
802
803         work = rt->overlay_list;
804         while (work)
805                 {
806                 OverlayData *od = work->data;
807                 work = work->next;
808                 if (od->window) gdk_window_destroy(od->window);
809                 od->window = NULL;
810                 }
811 }
812
813 void renderer_tiles_overlay_set(RendererTiles *rt, gint id, GdkPixbuf *pixbuf, gint x, gint y)
814 {
815         PixbufRenderer *pr = rt->pr;
816         OverlayData *od;
817
818         g_return_if_fail(IS_PIXBUF_RENDERER(pr));
819
820         od = rt_overlay_find(rt, id);
821         if (!od) return;
822
823         if (pixbuf)
824                 {
825                 gint px, py, pw, ph;
826
827                 g_object_ref(G_OBJECT(pixbuf));
828                 g_object_unref(G_OBJECT(od->pixbuf));
829                 od->pixbuf = pixbuf;
830
831                 od->x = x;
832                 od->y = y;
833
834                 if (!od->window) rt_overlay_init_window(rt, od);
835
836                 rt_overlay_queue_draw(rt, od, 0, 0, 0, 0);
837                 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
838                 gdk_window_move_resize(od->window, px + rt->stereo_off_x, py + rt->stereo_off_y, pw, ph);
839                 }
840         else
841                 {
842                 rt_overlay_queue_draw(rt, od, 0, 0, 0, 0);
843                 rt_overlay_free(rt, od);
844                 }
845 }
846
847 gboolean renderer_tiles_overlay_get(RendererTiles *rt, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
848 {
849         PixbufRenderer *pr = rt->pr;
850         OverlayData *od;
851
852         g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
853
854         od = rt_overlay_find(rt, id);
855         if (!od) return FALSE;
856
857         if (pixbuf) *pixbuf = od->pixbuf;
858         if (x) *x = od->x;
859         if (y) *y = od->y;
860
861         return TRUE;
862 }
863
864 static void rt_hierarchy_changed_cb(GtkWidget *widget, GtkWidget *previous_toplevel, gpointer data)
865 {
866         RendererTiles *rt = data;
867         rt_overlay_list_reset_window(rt);
868 }
869
870 /*
871  *-------------------------------------------------------------------
872  * drawing
873  *-------------------------------------------------------------------
874  */
875
876 static GdkPixbuf *rt_get_spare_tile(RendererTiles *rt)
877 {
878         if (!rt->spare_tile) rt->spare_tile = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, rt->tile_width, rt->tile_height);
879         return rt->spare_tile;
880 }
881
882 #define COLOR_BYTES 3   /* rgb */
883
884 static void rt_tile_rotate_90_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
885 {
886         GdkPixbuf *src = *tile;
887         GdkPixbuf *dest;
888         gint srs, drs;
889         guchar *s_pix, *d_pix;
890         guchar *sp, *dp;
891         guchar *ip, *spi, *dpi;
892         gint i, j;
893         gint tw = rt->tile_width;
894
895         srs = gdk_pixbuf_get_rowstride(src);
896         s_pix = gdk_pixbuf_get_pixels(src);
897         spi = s_pix + (x * COLOR_BYTES);
898
899         dest = rt_get_spare_tile(rt);
900         drs = gdk_pixbuf_get_rowstride(dest);
901         d_pix = gdk_pixbuf_get_pixels(dest);
902         dpi = d_pix + (tw - 1) * COLOR_BYTES;
903
904         for (i = y; i < y + h; i++)
905                 {
906                 sp = spi + (i * srs);
907                 ip = dpi - (i * COLOR_BYTES);
908                 for (j = x; j < x + w; j++)
909                         {
910                         dp = ip + (j * drs);
911                         memcpy(dp, sp, COLOR_BYTES);
912                         sp += COLOR_BYTES;
913                         }
914                 }
915
916         rt->spare_tile = src;
917         *tile = dest;
918 }
919
920 static void rt_tile_rotate_90_counter_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
921 {
922         GdkPixbuf *src = *tile;
923         GdkPixbuf *dest;
924         gint srs, drs;
925         guchar *s_pix, *d_pix;
926         guchar *sp, *dp;
927         guchar *ip, *spi, *dpi;
928         gint i, j;
929         gint th = rt->tile_height;
930
931         srs = gdk_pixbuf_get_rowstride(src);
932         s_pix = gdk_pixbuf_get_pixels(src);
933         spi = s_pix + (x * COLOR_BYTES);
934
935         dest = rt_get_spare_tile(rt);
936         drs = gdk_pixbuf_get_rowstride(dest);
937         d_pix = gdk_pixbuf_get_pixels(dest);
938         dpi = d_pix + (th - 1) * drs;
939
940         for (i = y; i < y + h; i++)
941                 {
942                 sp = spi + (i * srs);
943                 ip = dpi + (i * COLOR_BYTES);
944                 for (j = x; j < x + w; j++)
945                         {
946                         dp = ip - (j * drs);
947                         memcpy(dp, sp, COLOR_BYTES);
948                         sp += COLOR_BYTES;
949                         }
950                 }
951
952         rt->spare_tile = src;
953         *tile = dest;
954 }
955
956 static void rt_tile_mirror_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
957 {
958         GdkPixbuf *src = *tile;
959         GdkPixbuf *dest;
960         gint srs, drs;
961         guchar *s_pix, *d_pix;
962         guchar *sp, *dp;
963         guchar *spi, *dpi;
964         gint i, j;
965
966         gint tw = rt->tile_width;
967
968         srs = gdk_pixbuf_get_rowstride(src);
969         s_pix = gdk_pixbuf_get_pixels(src);
970         spi = s_pix + (x * COLOR_BYTES);
971
972         dest = rt_get_spare_tile(rt);
973         drs = gdk_pixbuf_get_rowstride(dest);
974         d_pix = gdk_pixbuf_get_pixels(dest);
975         dpi =  d_pix + (tw - x - 1) * COLOR_BYTES;
976
977         for (i = y; i < y + h; i++)
978                 {
979                 sp = spi + (i * srs);
980                 dp = dpi + (i * drs);
981                 for (j = 0; j < w; j++)
982                         {
983                         memcpy(dp, sp, COLOR_BYTES);
984                         sp += COLOR_BYTES;
985                         dp -= COLOR_BYTES;
986                         }
987                 }
988
989         rt->spare_tile = src;
990         *tile = dest;
991 }
992
993 static void rt_tile_mirror_and_flip(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
994 {
995         GdkPixbuf *src = *tile;
996         GdkPixbuf *dest;
997         gint srs, drs;
998         guchar *s_pix, *d_pix;
999         guchar *sp, *dp;
1000         guchar *spi, *dpi;
1001         gint i, j;
1002         gint tw = rt->tile_width;
1003         gint th = rt->tile_height;
1004
1005         srs = gdk_pixbuf_get_rowstride(src);
1006         s_pix = gdk_pixbuf_get_pixels(src);
1007         spi = s_pix + (x * COLOR_BYTES);
1008
1009         dest = rt_get_spare_tile(rt);
1010         drs = gdk_pixbuf_get_rowstride(dest);
1011         d_pix = gdk_pixbuf_get_pixels(dest);
1012         dpi = d_pix + (th - 1) * drs + (tw - 1) * COLOR_BYTES;
1013
1014         for (i = y; i < y + h; i++)
1015                 {
1016                 sp = s_pix + (i * srs) + (x * COLOR_BYTES);
1017                 dp = dpi - (i * drs) - (x * COLOR_BYTES);
1018                 for (j = 0; j < w; j++)
1019                         {
1020                         memcpy(dp, sp, COLOR_BYTES);
1021                         sp += COLOR_BYTES;
1022                         dp -= COLOR_BYTES;
1023                         }
1024                 }
1025
1026         rt->spare_tile = src;
1027         *tile = dest;
1028 }
1029
1030 static void rt_tile_flip_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1031 {
1032         GdkPixbuf *src = *tile;
1033         GdkPixbuf *dest;
1034         gint srs, drs;
1035         guchar *s_pix, *d_pix;
1036         guchar *sp, *dp;
1037         guchar *spi, *dpi;
1038         gint i;
1039         gint th = rt->tile_height;
1040
1041         srs = gdk_pixbuf_get_rowstride(src);
1042         s_pix = gdk_pixbuf_get_pixels(src);
1043         spi = s_pix + (x * COLOR_BYTES);
1044
1045         dest = rt_get_spare_tile(rt);
1046         drs = gdk_pixbuf_get_rowstride(dest);
1047         d_pix = gdk_pixbuf_get_pixels(dest);
1048         dpi = d_pix + (th - 1) * drs + (x * COLOR_BYTES);
1049
1050         for (i = y; i < y + h; i++)
1051                 {
1052                 sp = spi + (i * srs);
1053                 dp = dpi - (i * drs);
1054                 memcpy(dp, sp, w * COLOR_BYTES);
1055                 }
1056
1057         rt->spare_tile = src;
1058         *tile = dest;
1059 }
1060
1061 static void rt_tile_apply_orientation(RendererTiles *rt, gint orientation, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h)
1062 {
1063         switch (orientation)
1064                 {
1065                 case EXIF_ORIENTATION_TOP_LEFT:
1066                         /* normal -- nothing to do */
1067                         break;
1068                 case EXIF_ORIENTATION_TOP_RIGHT:
1069                         /* mirrored */
1070                         {
1071                                 rt_tile_mirror_only(rt, pixbuf, x, y, w, h);
1072                         }
1073                         break;
1074                 case EXIF_ORIENTATION_BOTTOM_RIGHT:
1075                         /* upside down */
1076                         {
1077                                 rt_tile_mirror_and_flip(rt, pixbuf, x, y, w, h);
1078                         }
1079                         break;
1080                 case EXIF_ORIENTATION_BOTTOM_LEFT:
1081                         /* flipped */
1082                         {
1083                                 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1084                         }
1085                         break;
1086                 case EXIF_ORIENTATION_LEFT_TOP:
1087                         {
1088                                 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1089                                 rt_tile_rotate_90_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1090                         }
1091                         break;
1092                 case EXIF_ORIENTATION_RIGHT_TOP:
1093                         /* rotated -90 (270) */
1094                         {
1095                                 rt_tile_rotate_90_clockwise(rt, pixbuf, x, y, w, h);
1096                         }
1097                         break;
1098                 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1099                         {
1100                                 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1101                                 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1102                         }
1103                         break;
1104                 case EXIF_ORIENTATION_LEFT_BOTTOM:
1105                         /* rotated 90 */
1106                         {
1107                                 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, y, w, h);
1108                         }
1109                         break;
1110                 default:
1111                         /* The other values are out of range */
1112                         break;
1113                 }
1114 }
1115
1116 static gboolean rt_source_tile_render(RendererTiles *rt, ImageTile *it,
1117                                       gint x, gint y, gint w, gint h,
1118                                       gboolean new_data, gboolean fast)
1119 {
1120         PixbufRenderer *pr = rt->pr;
1121         GtkWidget *box;
1122         GList *list;
1123         GList *work;
1124         gboolean draw = FALSE;
1125
1126         box = GTK_WIDGET(pr);
1127
1128         if (pr->zoom == 1.0 || pr->scale == 1.0)
1129                 {
1130                 list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE);
1131                 work = list;
1132                 while (work)
1133                         {
1134                         SourceTile *st;
1135                         gint rx, ry, rw, rh;
1136
1137                         st = work->data;
1138                         work = work->next;
1139
1140                         if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1141                                            it->x + x, it->y + y, w, h,
1142                                            &rx, &ry, &rw, &rh))
1143                                 {
1144                                 cairo_t *cr;
1145                                 cr = cairo_create(it->surface);
1146                                 cairo_rectangle (cr, rx - it->x, ry - it->y, rw, rh);
1147
1148                                 if (st->blank)
1149                                         {
1150                                         cairo_set_source_rgb(cr, 0, 0, 0);
1151                                         }
1152                                 else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
1153                                         {
1154                                         gdk_cairo_set_source_pixbuf(cr, st->pixbuf, -it->x + st->x, -it->y + st->y);
1155                                         }
1156                                 cairo_fill (cr);
1157                                 cairo_destroy (cr);
1158                                 }
1159                         }
1160                 }
1161         else
1162                 {
1163                 gdouble scale_x, scale_y;
1164                 gint sx, sy, sw, sh;
1165
1166                 if (pr->image_width == 0 || pr->image_height == 0) return FALSE;
1167                 scale_x = (gdouble)pr->width / pr->image_width;
1168                 scale_y = (gdouble)pr->height / pr->image_height;
1169
1170                 sx = (gdouble)(it->x + x) / scale_x;
1171                 sy = (gdouble)(it->y + y) / scale_y;
1172                 sw = (gdouble)w / scale_x;
1173                 sh = (gdouble)h / scale_y;
1174
1175                 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1176
1177 #if 0
1178                 /* draws red over draw region, to check for leaks (regions not filled) */
1179                 pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255);
1180 #endif
1181
1182                 list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE);
1183                 work = list;
1184                 while (work)
1185                         {
1186                         SourceTile *st;
1187                         gint rx, ry, rw, rh;
1188                         gint stx, sty, stw, sth;
1189
1190                         st = work->data;
1191                         work = work->next;
1192
1193                         stx = floor((gdouble)st->x * scale_x);
1194                         sty = floor((gdouble)st->y * scale_y);
1195                         stw = ceil((gdouble)(st->x + pr->source_tile_width) * scale_x) - stx;
1196                         sth = ceil((gdouble)(st->y + pr->source_tile_height) * scale_y) - sty;
1197
1198                         if (pr_clip_region(stx, sty, stw, sth,
1199                                            it->x + x, it->y + y, w, h,
1200                                            &rx, &ry, &rw, &rh))
1201                                 {
1202
1203                                 if (st->blank)
1204                                         {
1205                                         cairo_t *cr;
1206                                         cr = cairo_create(it->surface);
1207                                         cairo_rectangle (cr, rx - st->x, ry - st->y, rw, rh);
1208                                         cairo_set_source_rgb(cr, 0, 0, 0);
1209                                         cairo_fill (cr);
1210                                         cairo_destroy (cr);
1211                                         }
1212                                 else
1213                                         {
1214                                         gdouble offset_x;
1215                                         gdouble offset_y;
1216
1217                                         /* may need to use unfloored stx,sty values here */
1218                                         offset_x = (gdouble)(stx - it->x);
1219                                         offset_y = (gdouble)(sty - it->y);
1220
1221                                         gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh,
1222                                                  (gdouble) 0.0 + offset_x,
1223                                                  (gdouble) 0.0 + offset_y,
1224                                                  scale_x, scale_y,
1225                                                  (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1226                                         draw = TRUE;
1227                                         }
1228                                 }
1229                         }
1230                 }
1231
1232         g_list_free(list);
1233
1234         return draw;
1235 }
1236
1237 static void rt_tile_get_region(gboolean has_alpha, 
1238                                const GdkPixbuf *src, GdkPixbuf *dest,
1239                                int pb_x, int pb_y, int pb_w, int pb_h,
1240                                double offset_x, double offset_y, double scale_x, double scale_y,
1241                                GdkInterpType interp_type,
1242                                int check_x, int check_y)
1243 {
1244         if (!has_alpha)
1245                 {
1246                 if (scale_x == 1.0 && scale_y == 1.0)
1247                         {
1248                         gdk_pixbuf_copy_area(src,
1249                                              -offset_x + pb_x, -offset_y + pb_y,
1250                                              pb_w, pb_h,
1251                                              dest,
1252                                              pb_x, pb_y);
1253                         } 
1254                 else
1255                         {
1256                         gdk_pixbuf_scale(src, dest, 
1257                                          pb_x, pb_y, pb_w, pb_h,
1258                                          offset_x,
1259                                          offset_y,
1260                                          scale_x, scale_y,
1261                                          interp_type);
1262                         }
1263                 }
1264         else
1265                 {
1266                 gdk_pixbuf_composite_color(src, dest, 
1267                                          pb_x, pb_y, pb_w, pb_h,
1268                                          offset_x,
1269                                          offset_y,
1270                                          scale_x, scale_y,
1271                                          interp_type,
1272                                          255, check_x, check_y,
1273                                          PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
1274                 }
1275 }
1276
1277
1278 static gint rt_get_orientation(RendererTiles *rt)
1279 {
1280         PixbufRenderer *pr = rt->pr;
1281
1282         gint orientation = pr->orientation;
1283         static const gint mirror[]       = {1,   2, 1, 4, 3, 6, 5, 8, 7};
1284         static const gint flip[]         = {1,   4, 3, 2, 1, 8, 7, 6, 5};
1285
1286         if (rt->stereo_mode & PR_STEREO_MIRROR) orientation = mirror[orientation];
1287         if (rt->stereo_mode & PR_STEREO_FLIP) orientation = flip[orientation];
1288         return orientation;
1289 }
1290
1291
1292 static void rt_tile_render(RendererTiles *rt, ImageTile *it,
1293                            gint x, gint y, gint w, gint h,
1294                            gboolean new_data, gboolean fast)
1295 {
1296         PixbufRenderer *pr = rt->pr;
1297         GtkWidget *box;
1298         gboolean has_alpha;
1299         gboolean draw = FALSE;
1300         gint orientation = rt_get_orientation(rt);
1301
1302         if (it->render_todo == TILE_RENDER_NONE && it->surface && !new_data) return;
1303
1304         if (it->render_done != TILE_RENDER_ALL)
1305                 {
1306                 x = 0;
1307                 y = 0;
1308                 w = it->w;
1309                 h = it->h;
1310                 if (!fast) it->render_done = TILE_RENDER_ALL;
1311                 }
1312         else if (it->render_todo != TILE_RENDER_AREA)
1313                 {
1314                 if (!fast) it->render_todo = TILE_RENDER_NONE;
1315                 return;
1316                 }
1317
1318         if (!fast) it->render_todo = TILE_RENDER_NONE;
1319
1320         if (new_data) it->blank = FALSE;
1321
1322         rt_tile_prepare(rt, it);
1323         has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
1324
1325         box = GTK_WIDGET(pr);
1326
1327         /* FIXME checker colors for alpha should be configurable,
1328          * also should be drawn for blank = TRUE
1329          */
1330
1331         if (it->blank)
1332                 {
1333                 /* no data, do fast rect fill */
1334                 cairo_t *cr;
1335                 cr = cairo_create(it->surface);
1336                 cairo_rectangle (cr, 0, 0, it->w, it->h);
1337                 cairo_set_source_rgb(cr, 0, 0, 0);
1338                 cairo_fill (cr);
1339                 cairo_destroy (cr);
1340 #if 0
1341                 gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1342                                    0, 0, it->w, it->h);
1343 #endif
1344                 }
1345         else if (pr->source_tiles_enabled)
1346                 {
1347                 draw = rt_source_tile_render(rt, it, x, y, w, h, new_data, fast);
1348                 }
1349         else if ((pr->zoom == 1.0 || pr->scale == 1.0) &&
1350                  pr->aspect_ratio == 1.0 &&
1351                  !has_alpha &&
1352                  orientation == EXIF_ORIENTATION_TOP_LEFT && 
1353                  !(pr->func_post_process && !(pr->post_process_slow && fast)) &&
1354                  !(rt->stereo_mode & PR_STEREO_ANAGLYPH))
1355                 {
1356                 /* special case: faster, simple, scale 1.0, base orientation, no postprocessing */
1357                 cairo_t *cr;
1358                 cr = cairo_create(it->surface);
1359                 cairo_rectangle (cr, x, y, w, h);
1360                 gdk_cairo_set_source_pixbuf(cr, pr->pixbuf, -it->x - GET_RIGHT_PIXBUF_OFFSET(rt), -it->y);
1361                 cairo_fill (cr);
1362                 cairo_destroy (cr);
1363                 }
1364         else
1365                 {
1366                 gdouble scale_x, scale_y;
1367                 gdouble src_x, src_y;
1368                 gint pb_x, pb_y;
1369                 gint pb_w, pb_h;
1370
1371                 if (pr->image_width == 0 || pr->image_height == 0) return;
1372
1373                 scale_x = (gdouble)pr->width / pr->image_width;
1374                 scale_y = (gdouble)pr->height / pr->image_height;
1375
1376                 pr_tile_coords_map_orientation(orientation, it->x, it->y,
1377                                             pr->width, pr->height,
1378                                             rt->tile_width, rt->tile_height,
1379                                             &src_x, &src_y);
1380                 pr_tile_region_map_orientation(orientation, x, y,
1381                                             rt->tile_width, rt->tile_height,
1382                                             w, h,
1383                                             &pb_x, &pb_y,
1384                                             &pb_w, &pb_h);
1385
1386                 switch (orientation)
1387                         {
1388                         gdouble tmp;
1389                         case EXIF_ORIENTATION_LEFT_TOP:
1390                         case EXIF_ORIENTATION_RIGHT_TOP:
1391                         case EXIF_ORIENTATION_RIGHT_BOTTOM:
1392                         case EXIF_ORIENTATION_LEFT_BOTTOM:
1393                                 tmp = scale_x;
1394                                 scale_x = scale_y;
1395                                 scale_y = tmp;
1396                                 break;
1397                         default:
1398                                 /* nothing to do */
1399                                 break;
1400                         }
1401                 
1402                 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
1403                  * small sizes for anything but GDK_INTERP_NEAREST
1404                  */
1405                 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1406
1407                 rt_tile_get_region(has_alpha,
1408                                    pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
1409                                    (gdouble) 0.0 - src_x - GET_RIGHT_PIXBUF_OFFSET(rt) * scale_x,
1410                                    (gdouble) 0.0 - src_y,
1411                                    scale_x, scale_y,
1412                                    (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1413                                    it->x + pb_x, it->y + pb_y);
1414                 if (rt->stereo_mode & PR_STEREO_ANAGLYPH && 
1415                     (pr->stereo_pixbuf_offset_right > 0 || pr->stereo_pixbuf_offset_left > 0))
1416                         {
1417                         GdkPixbuf *right_pb = rt_get_spare_tile(rt);
1418                         rt_tile_get_region(has_alpha,
1419                                            pr->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h,
1420                                            (gdouble) 0.0 - src_x - GET_LEFT_PIXBUF_OFFSET(rt) * scale_x,
1421                                            (gdouble) 0.0 - src_y,
1422                                            scale_x, scale_y,
1423                                            (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1424                                            it->x + pb_x, it->y + pb_y);
1425                         pr_create_anaglyph(rt->stereo_mode, it->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h);
1426                         /* do not care about freeing spare_tile, it will be reused */
1427                         }
1428                 rt_tile_apply_orientation(rt, orientation, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
1429                 draw = TRUE;
1430                 }
1431
1432         if (draw && it->pixbuf && !it->blank)
1433                 {
1434                 cairo_t *cr;
1435
1436                 if (pr->func_post_process && !(pr->post_process_slow && fast))
1437                         pr->func_post_process(pr, &it->pixbuf, x, y, w, h, pr->post_process_user_data);
1438
1439                 cr = cairo_create(it->surface);
1440                 cairo_rectangle (cr, x, y, w, h);
1441                 gdk_cairo_set_source_pixbuf(cr, it->pixbuf, 0, 0);
1442                 cairo_fill (cr);
1443                 cairo_destroy (cr);
1444                 }
1445 }
1446
1447
1448 static void rt_tile_expose(RendererTiles *rt, ImageTile *it,
1449                            gint x, gint y, gint w, gint h,
1450                            gboolean new_data, gboolean fast)
1451 {
1452         PixbufRenderer *pr = rt->pr;
1453         GtkWidget *box;
1454         GdkWindow *window;
1455         cairo_t *cr;
1456
1457         /* clamp to visible */
1458         if (it->x + x < rt->x_scroll)
1459                 {
1460                 w -= rt->x_scroll - it->x - x;
1461                 x = rt->x_scroll - it->x;
1462                 }
1463         if (it->x + x + w > rt->x_scroll + pr->vis_width)
1464                 {
1465                 w = rt->x_scroll + pr->vis_width - it->x - x; 
1466                 }
1467         if (w < 1) return;
1468         if (it->y + y < rt->y_scroll)
1469                 {
1470                 h -= rt->y_scroll - it->y - y;
1471                 y = rt->y_scroll - it->y;
1472                 }
1473         if (it->y + y + h > rt->y_scroll + pr->vis_height)
1474                 {
1475                 h = rt->y_scroll + pr->vis_height - it->y - y; 
1476                 }
1477         if (h < 1) return;
1478
1479         rt_tile_render(rt, it, x, y, w, h, new_data, fast);
1480
1481         box = GTK_WIDGET(pr);
1482         window = gtk_widget_get_window(box);
1483
1484         cr = gdk_cairo_create(window);
1485         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);
1486         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);
1487         cairo_fill (cr);
1488         cairo_destroy (cr);
1489
1490 #if 0
1491 #if GTK_CHECK_VERSION(2,20,0)
1492         gdk_draw_drawable(window, box->style->fg_gc[gtk_widget_get_state(box)],
1493 #else
1494         gdk_draw_drawable(window, box->style->fg_gc[GTK_WIDGET_STATE(box)],
1495 #endif
1496                           it->pixmap, x, y,
1497                           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);
1498 #endif
1499
1500         if (rt->overlay_list)
1501                 {
1502                 rt_overlay_draw(rt, pr->x_offset + (it->x - rt->x_scroll) + x,
1503                                 pr->y_offset + (it->y - rt->y_scroll) + y,
1504                                 w, h,
1505                                 it);
1506                 }
1507 }
1508
1509
1510 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it)
1511 {
1512         PixbufRenderer *pr = rt->pr;
1513         return (it->x + it->w >= rt->x_scroll && it->x < rt->x_scroll + pr->vis_width &&
1514                 it->y + it->h >= rt->y_scroll && it->y < rt->y_scroll + pr->vis_height);
1515 }
1516
1517 /*
1518  *-------------------------------------------------------------------
1519  * draw queue
1520  *-------------------------------------------------------------------
1521  */
1522
1523 static gint rt_get_queued_area(GList *work)
1524 {
1525         gint area = 0;
1526         
1527         while (work) 
1528                 {
1529                 QueueData *qd = work->data;
1530                 area += qd->w * qd->h;
1531                 work = work->next;
1532                 }
1533         return area;
1534 }
1535
1536
1537 static gboolean rt_queue_schedule_next_draw(RendererTiles *rt, gboolean force_set)
1538 {
1539         PixbufRenderer *pr = rt->pr;
1540         gfloat percent;
1541         gint visible_area = pr->vis_width * pr->vis_height;
1542         
1543         if (!pr->loading)
1544                 {
1545                 /* 2pass prio */ 
1546                 DEBUG_2("redraw priority: 2pass");
1547                 rt->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, rt_queue_draw_idle_cb, rt, NULL);
1548                 return FALSE;
1549                 }
1550         
1551         if (visible_area == 0)
1552                 {
1553                 /* not known yet */
1554                 percent = 100.0;
1555                 }
1556         else
1557                 {
1558                 percent = 100.0 * rt_get_queued_area(rt->draw_queue) / visible_area;
1559                 }
1560         
1561         if (percent > 10.0)
1562                 {
1563                 /* we have enough data for starting intensive redrawing */
1564                 DEBUG_2("redraw priority: high %.2f %%", percent);
1565                 rt->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW, rt_queue_draw_idle_cb, rt, NULL);
1566                 return FALSE;
1567                 }
1568         
1569         if (percent < 1.0 || force_set)
1570                 {
1571                 /* queue is (almost) empty, wait  50 ms*/
1572                 DEBUG_2("redraw priority: wait %.2f %%", percent);
1573                 rt->draw_idle_id = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, 50, rt_queue_draw_idle_cb, rt, NULL);
1574                 return FALSE;
1575                 }
1576         
1577         /* keep the same priority as before */
1578         DEBUG_2("redraw priority: no change %.2f %%", percent);
1579         return TRUE;
1580 }
1581                 
1582
1583 static gboolean rt_queue_draw_idle_cb(gpointer data)
1584 {
1585         RendererTiles *rt = data;
1586         PixbufRenderer *pr = rt->pr;
1587         QueueData *qd;
1588         gboolean fast;
1589
1590
1591         if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
1592             (!rt->draw_queue && !rt->draw_queue_2pass) ||
1593             !rt->draw_idle_id)
1594                 {
1595                 pr_render_complete_signal(pr);
1596
1597                 rt->draw_idle_id = 0;
1598                 return FALSE;
1599                 }
1600
1601         if (rt->draw_queue)
1602                 {
1603                 qd = rt->draw_queue->data;
1604                 fast = (pr->zoom_2pass && ((pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0) || pr->post_process_slow));
1605                 }
1606         else
1607                 {
1608                 if (pr->loading)
1609                         {
1610                         /* still loading, wait till done (also drops the higher priority) */
1611
1612                         return rt_queue_schedule_next_draw(rt, FALSE);
1613                         }
1614
1615                 qd = rt->draw_queue_2pass->data;
1616                 fast = FALSE;
1617                 }
1618
1619 #if GTK_CHECK_VERSION(2,20,0)
1620         if (gtk_widget_get_realized(pr))
1621 #else
1622         if (GTK_WIDGET_REALIZED(pr))
1623 #endif
1624                 {
1625                 if (rt_tile_is_visible(rt, qd->it))
1626                         {
1627                         rt_tile_expose(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1628                         }
1629                 else if (qd->new_data)
1630                         {
1631                         /* if new pixel data, and we already have a pixmap, update the tile */
1632                         qd->it->blank = FALSE;
1633                         if (qd->it->surface && qd->it->render_done == TILE_RENDER_ALL)
1634                                 {
1635                                 rt_tile_render(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1636                                 }
1637                         }
1638                 }
1639
1640         if (rt->draw_queue)
1641                 {
1642                 qd->it->qd = NULL;
1643                 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
1644                 if (fast)
1645                         {
1646                         if (qd->it->qd2)
1647                                 {
1648                                 rt_queue_merge(qd->it->qd2, qd);
1649                                 g_free(qd);
1650                                 }
1651                         else
1652                                 {
1653                                 qd->it->qd2 = qd;
1654                                 rt->draw_queue_2pass = g_list_append(rt->draw_queue_2pass, qd);
1655                                 }
1656                         }
1657                 else
1658                         {
1659                         g_free(qd);
1660                         }
1661                 }
1662         else
1663                 {
1664                 qd->it->qd2 = NULL;
1665                 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
1666                 g_free(qd);
1667                 }
1668
1669         if (!rt->draw_queue && !rt->draw_queue_2pass)
1670                 {
1671                 pr_render_complete_signal(pr);
1672
1673                 rt->draw_idle_id = 0;
1674                 return FALSE;
1675                 }
1676
1677                 return rt_queue_schedule_next_draw(rt, FALSE);
1678 }
1679
1680 static void rt_queue_list_free(GList *list)
1681 {
1682         GList *work;
1683
1684         work = list;
1685         while (work)
1686                 {
1687                 QueueData *qd;
1688
1689                 qd = work->data;
1690                 work = work->next;
1691
1692                 qd->it->qd = NULL;
1693                 qd->it->qd2 = NULL;
1694                 g_free(qd);
1695                 }
1696
1697         g_list_free(list);
1698 }
1699
1700 static void rt_queue_clear(RendererTiles *rt)
1701 {
1702         rt_queue_list_free(rt->draw_queue);
1703         rt->draw_queue = NULL;
1704
1705         rt_queue_list_free(rt->draw_queue_2pass);
1706         rt->draw_queue_2pass = NULL;
1707
1708         if (rt->draw_idle_id)
1709                 {
1710                 g_source_remove(rt->draw_idle_id);
1711                 rt->draw_idle_id = 0;
1712                 }
1713         rt_sync_scroll(rt);
1714 }
1715
1716 static void rt_queue_merge(QueueData *parent, QueueData *qd)
1717 {
1718         if (parent->x + parent->w < qd->x + qd->w)
1719                 {
1720                 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
1721                 }
1722         if (parent->x > qd->x)
1723                 {
1724                 parent->w += parent->x - qd->x;
1725                 parent->x = qd->x;
1726                 }
1727
1728         if (parent->y + parent->h < qd->y + qd->h)
1729                 {
1730                 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
1731                 }
1732         if (parent->y > qd->y)
1733                 {
1734                 parent->h += parent->y - qd->y;
1735                 parent->y = qd->y;
1736                 }
1737
1738         parent->new_data |= qd->new_data;
1739 }
1740
1741 static gboolean rt_clamp_to_visible(RendererTiles *rt, gint *x, gint *y, gint *w, gint *h)
1742 {
1743         PixbufRenderer *pr = rt->pr;
1744         gint nx, ny;
1745         gint nw, nh;
1746         gint vx, vy;
1747         gint vw, vh;
1748
1749         vw = pr->vis_width;
1750         vh = pr->vis_height;
1751
1752         vx = rt->x_scroll;
1753         vy = rt->y_scroll;
1754
1755         if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
1756
1757         /* now clamp it */
1758         nx = CLAMP(*x, vx, vx + vw);
1759         nw = CLAMP(*w - (nx - *x), 1, vw);
1760
1761         ny = CLAMP(*y, vy, vy + vh);
1762         nh = CLAMP(*h - (ny - *y), 1, vh);
1763
1764         *x = nx;
1765         *y = ny;
1766         *w = nw;
1767         *h = nh;
1768
1769         return TRUE;
1770 }
1771
1772 static gboolean rt_queue_to_tiles(RendererTiles *rt, gint x, gint y, gint w, gint h,
1773                                   gboolean clamp, ImageRenderType render,
1774                                   gboolean new_data, gboolean only_existing)
1775 {
1776         PixbufRenderer *pr = rt->pr;
1777         gint i, j;
1778         gint x1, x2;
1779         gint y1, y2;
1780
1781         if (clamp && !rt_clamp_to_visible(rt, &x, &y, &w, &h)) return FALSE;
1782
1783         x1 = ROUND_DOWN(x, rt->tile_width);
1784         x2 = ROUND_UP(x + w, rt->tile_width);
1785
1786         y1 = ROUND_DOWN(y, rt->tile_height);
1787         y2 = ROUND_UP(y + h, rt->tile_height);
1788
1789         for (j = y1; j <= y2; j += rt->tile_height)
1790                 {
1791                 for (i = x1; i <= x2; i += rt->tile_width)
1792                         {
1793                         ImageTile *it;
1794
1795                         it = rt_tile_get(rt, i, j,
1796                                          (only_existing &&
1797                                           (i + rt->tile_width < rt->x_scroll ||
1798                                            i > rt->x_scroll + pr->vis_width ||
1799                                            j + rt->tile_height < rt->y_scroll ||
1800                                            j > rt->y_scroll + pr->vis_height)));
1801                         if (it)
1802                                 {
1803                                 QueueData *qd;
1804
1805                                 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
1806                                     (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
1807                                         {
1808                                         it->render_todo = render;
1809                                         }
1810
1811                                 qd = g_new(QueueData, 1);
1812                                 qd->it = it;
1813                                 qd->new_data = new_data;
1814
1815                                 if (i < x)
1816                                         {
1817                                         qd->x = x - i;
1818                                         }
1819                                 else
1820                                         {
1821                                         qd->x = 0;
1822                                         }
1823                                 qd->w = x + w - i - qd->x;
1824                                 if (qd->x + qd->w > rt->tile_width) qd->w = rt->tile_width - qd->x;
1825
1826                                 if (j < y)
1827                                         {
1828                                         qd->y = y - j;
1829                                         }
1830                                 else
1831                                         {
1832                                         qd->y = 0;
1833                                         }
1834                                 qd->h = y + h - j - qd->y;
1835                                 if (qd->y + qd->h > rt->tile_height) qd->h = rt->tile_height - qd->y;
1836
1837                                 if (qd->w < 1 || qd->h < 1)
1838                                         {
1839                                         g_free(qd);
1840                                         }
1841                                 else if (it->qd)
1842                                         {
1843                                         rt_queue_merge(it->qd, qd);
1844                                         g_free(qd);
1845                                         }
1846                                 else
1847                                         {
1848                                         it->qd = qd;
1849                                         rt->draw_queue = g_list_append(rt->draw_queue, qd);
1850                                         }
1851                                 }
1852                         }
1853                 }
1854
1855         return TRUE;
1856 }
1857
1858 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
1859                      gboolean clamp, ImageRenderType render,
1860                      gboolean new_data, gboolean only_existing)
1861 {
1862         PixbufRenderer *pr = rt->pr;
1863         gint nx, ny;
1864
1865         rt_sync_scroll(rt);
1866
1867         nx = CLAMP(x, 0, pr->width - 1);
1868         ny = CLAMP(y, 0, pr->height - 1);
1869         w -= (nx - x);
1870         h -= (ny - y);
1871         w = CLAMP(w, 0, pr->width - nx);
1872         h = CLAMP(h, 0, pr->height - ny);
1873         if (w < 1 || h < 1) return;
1874
1875         if (rt_queue_to_tiles(rt, nx, ny, w, h, clamp, render, new_data, only_existing) &&
1876             ((!rt->draw_queue && !rt->draw_queue_2pass) || !rt->draw_idle_id))
1877                 {
1878                 if (rt->draw_idle_id)
1879                         {
1880                         g_source_remove(rt->draw_idle_id);
1881                         rt->draw_idle_id = 0;
1882                         }
1883                 rt_queue_schedule_next_draw(rt, TRUE);
1884                 }
1885 }
1886
1887 static void rt_scroll(RendererTiles *rt, gint x_off, gint y_off)
1888 {
1889         PixbufRenderer *pr = rt->pr;
1890
1891         rt_sync_scroll(rt);
1892         if (rt->stereo_mode & PR_STEREO_MIRROR) x_off = -x_off;
1893         if (rt->stereo_mode & PR_STEREO_FLIP) y_off = -y_off; 
1894
1895         gint w = pr->vis_width - abs(x_off);
1896         gint h = pr->vis_height - abs(y_off);
1897
1898         if (w < 1 || h < 1)
1899                 {
1900                 /* scrolled completely to new material */
1901                 rt_queue(rt, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1902                 return;
1903                 }
1904         else
1905                 {
1906                 gint x1, y1;
1907                 gint x2, y2;
1908                 GtkWidget *box;
1909                 GdkWindow *window;
1910 //              GdkGC *gc;
1911                 GdkEvent *event;
1912                 cairo_t *cr;
1913                 cairo_surface_t *surface;
1914
1915                 if (x_off < 0)
1916                         {
1917                         x1 = abs(x_off);
1918                         x2 = 0;
1919                         }
1920                 else
1921                         {
1922                         x1 = 0;
1923                         x2 = abs(x_off);
1924                         }
1925
1926                 if (y_off < 0)
1927                         {
1928                         y1 = abs(y_off);
1929                         y2 = 0;
1930                         }
1931                 else
1932                         {
1933                         y1 = 0;
1934                         y2 = abs(y_off);
1935                         }
1936
1937                 box = GTK_WIDGET(pr);
1938                 window = gtk_widget_get_window(box);
1939
1940                 cr = gdk_cairo_create(window);
1941                 surface = cairo_get_target(cr);
1942                 /* clipping restricts the intermediate surface's size, so it's a good idea
1943                  * to use it. */
1944                 cairo_rectangle(cr, x1 + pr->x_offset + rt->stereo_off_x, y1 + pr->y_offset + rt->stereo_off_y, w, h);
1945                 cairo_clip (cr);
1946                 /* Now push a group to change the target */
1947                 cairo_push_group (cr);
1948                 cairo_set_source_surface(cr, surface, x1 - x2, y1 - y2);
1949                 cairo_paint(cr);
1950                 /* Now copy the intermediate target back */
1951                 cairo_pop_group_to_source(cr);
1952                 cairo_paint(cr);
1953                 cairo_destroy(cr);
1954 #if 0
1955                 gc = gdk_gc_new(window);
1956                 gdk_gc_set_exposures(gc, TRUE);
1957                 gdk_draw_drawable(window, gc,
1958                                   window,
1959                                   x2 + pr->x_offset + rt->stereo_off_x, y2 + pr->y_offset + rt->stereo_off_y,
1960                                   x1 + pr->x_offset + rt->stereo_off_x, y1 + pr->y_offset + rt->stereo_off_y, w, h);
1961                 g_object_unref(gc);
1962 #endif
1963
1964                 rt_overlay_queue_all(rt, x2, y2, x1, y1);
1965
1966                 w = pr->vis_width - w;
1967                 h = pr->vis_height - h;
1968
1969                 if (w > 0)
1970                         {
1971                         rt_queue(rt,
1972                                     x_off > 0 ? rt->x_scroll + (pr->vis_width - w) : rt->x_scroll, rt->y_scroll,
1973                                     w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1974                         }
1975                 if (h > 0)
1976                         {
1977                         /* FIXME, to optimize this, remove overlap */
1978                         rt_queue(rt,
1979                                     rt->x_scroll, y_off > 0 ? rt->y_scroll + (pr->vis_height - h) : rt->y_scroll,
1980                                     pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1981                         }
1982
1983                 /* process exposures here, "expose_event" seems to miss a few with obstructed windows */
1984 #if ! GTK_CHECK_VERSION(2,18,0)
1985                 while ((event = gdk_event_get_graphics_expose(window)) != NULL)
1986                         {
1987                         renderer_redraw((void *) rt, event->expose.area.x, event->expose.area.y, event->expose.area.width, event->expose.area.height,
1988                               FALSE, TILE_RENDER_ALL, FALSE, FALSE);
1989  
1990                         if (event->expose.count == 0)
1991                                 {
1992                                 gdk_event_free(event);
1993                                 break;
1994                                 }
1995                         gdk_event_free(event);
1996                         }
1997 #endif
1998                 }
1999 }
2000
2001 static void renderer_area_changed(void *renderer, gint src_x, gint src_y, gint src_w, gint src_h)
2002 {
2003         RendererTiles *rt = (RendererTiles *)renderer;
2004         PixbufRenderer *pr = rt->pr;
2005         gint x, y, width, height,  x1, y1, x2, y2;
2006
2007         gint orientation = rt_get_orientation(rt);
2008         pr_coords_map_orientation_reverse(orientation,
2009                                      src_x - GET_RIGHT_PIXBUF_OFFSET(rt), src_y,
2010                                      pr->image_width, pr->image_height,
2011                                      src_w, src_h,
2012                                      &x, &y,
2013                                      &width, &height);
2014
2015         if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
2016                 {
2017                 /* increase region when using a zoom quality that may access surrounding pixels */
2018                 y -= 1;
2019                 height += 2;
2020                 }
2021
2022         x1 = (gint)floor((gdouble)x * pr->scale);
2023         y1 = (gint)floor((gdouble)y * pr->scale * pr->aspect_ratio);
2024         x2 = (gint)ceil((gdouble)(x + width) * pr->scale);
2025         y2 = (gint)ceil((gdouble)(y + height) * pr->scale * pr->aspect_ratio);
2026
2027         rt_queue(rt, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
2028 }
2029
2030 static void renderer_redraw(void *renderer, gint x, gint y, gint w, gint h,
2031                      gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing)
2032 {
2033         RendererTiles *rt = (RendererTiles *)renderer;
2034         PixbufRenderer *pr = rt->pr;
2035
2036         x -= rt->stereo_off_x;
2037         y -= rt->stereo_off_y;
2038         
2039         rt_border_draw(rt, x, y, w, h);
2040
2041         x = MAX(0, x - pr->x_offset + pr->x_scroll);
2042         y = MAX(0, y - pr->y_offset + pr->y_scroll);
2043
2044         rt_queue(rt,
2045                  x, y,
2046                  MIN(w, pr->width - x),
2047                  MIN(h, pr->height - y),
2048                  clamp, render, new_data, only_existing);
2049 }
2050
2051 static void renderer_queue_clear(void *renderer)
2052 {
2053         rt_queue_clear((RendererTiles *)renderer);
2054 }
2055
2056 static void renderer_border_clear(void *renderer)
2057 {
2058         rt_border_clear((RendererTiles *)renderer);
2059 }
2060
2061
2062 static void renderer_invalidate_all(void *renderer)
2063 {
2064         rt_tile_invalidate_all((RendererTiles *)renderer);
2065 }
2066
2067 static void renderer_invalidate_region(void *renderer, gint x, gint y, gint w, gint h)
2068 {
2069         rt_tile_invalidate_region((RendererTiles *)renderer, x, y, w, h);
2070 }
2071
2072 static void renderer_overlay_draw(void *renderer, gint x, gint y, gint w, gint h)
2073 {
2074         rt_overlay_draw((RendererTiles *)renderer, x, y, w, h, NULL);
2075 }
2076
2077 static void renderer_update_sizes(void *renderer)
2078 {
2079         RendererTiles *rt = (RendererTiles *)renderer;
2080
2081         rt->stereo_off_x = 0;
2082         rt->stereo_off_y = 0;
2083         
2084         if (rt->stereo_mode & PR_STEREO_RIGHT) 
2085                 {
2086                 if (rt->stereo_mode & PR_STEREO_HORIZ) 
2087                         {
2088                         rt->stereo_off_x = rt->pr->viewport_width;
2089                         }
2090                 else if (rt->stereo_mode & PR_STEREO_VERT) 
2091                         {
2092                         rt->stereo_off_y = rt->pr->viewport_height;
2093                         }
2094                 else if (rt->stereo_mode & PR_STEREO_FIXED) 
2095                         {
2096                         rt->stereo_off_x = rt->pr->stereo_fixed_x_right;
2097                         rt->stereo_off_y = rt->pr->stereo_fixed_y_right;
2098                         }
2099                 }
2100         else
2101                 {
2102                 if (rt->stereo_mode & PR_STEREO_FIXED) 
2103                         {
2104                         rt->stereo_off_x = rt->pr->stereo_fixed_x_left;
2105                         rt->stereo_off_y = rt->pr->stereo_fixed_y_left;
2106                         }
2107                 }
2108         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);
2109         rt_sync_scroll(rt);
2110         rt_overlay_update_sizes(rt);
2111 }
2112
2113 static void renderer_stereo_set(void *renderer, gint stereo_mode)
2114 {
2115         RendererTiles *rt = (RendererTiles *)renderer;
2116
2117         rt->stereo_mode = stereo_mode;
2118 }
2119
2120 static void renderer_free(void *renderer)
2121 {
2122         RendererTiles *rt = (RendererTiles *)renderer;
2123         rt_queue_clear(rt);
2124         rt_tile_free_all(rt);
2125         if (rt->spare_tile) g_object_unref(rt->spare_tile);
2126         if (rt->overlay_buffer) g_object_unref(rt->overlay_buffer);
2127         rt_overlay_list_clear(rt);
2128         /* disconnect "hierarchy-changed" */
2129         g_signal_handlers_disconnect_matched(G_OBJECT(rt->pr), G_SIGNAL_MATCH_DATA,
2130                                                      0, 0, 0, NULL, rt);
2131         g_free(rt);
2132 }
2133
2134 RendererFuncs *renderer_tiles_new(PixbufRenderer *pr)
2135 {
2136         RendererTiles *rt = g_new0(RendererTiles, 1);
2137         
2138         rt->pr = pr;
2139         
2140         rt->f.redraw = renderer_redraw;
2141         rt->f.area_changed = renderer_area_changed;
2142         rt->f.queue_clear = renderer_queue_clear;
2143         rt->f.border_clear = renderer_border_clear;
2144         rt->f.free = renderer_free;
2145         rt->f.invalidate_all = renderer_invalidate_all;
2146         rt->f.invalidate_region = renderer_invalidate_region;
2147         rt->f.scroll = rt_scroll;
2148         rt->f.update_sizes = renderer_update_sizes;
2149
2150
2151         rt->f.overlay_add = renderer_tiles_overlay_add;
2152         rt->f.overlay_set = renderer_tiles_overlay_set;
2153         rt->f.overlay_get = renderer_tiles_overlay_get;
2154         rt->f.overlay_draw = renderer_overlay_draw;
2155
2156         rt->f.stereo_set = renderer_stereo_set;
2157         
2158         rt->tile_width = PR_TILE_SIZE;
2159         rt->tile_height = PR_TILE_SIZE;
2160
2161         rt->tiles = NULL;
2162         rt->tile_cache_size = 0;
2163
2164         rt->tile_cache_max = PR_CACHE_SIZE_DEFAULT;
2165
2166         rt->draw_idle_id = 0;
2167         
2168         rt->stereo_mode = 0;
2169         rt->stereo_off_x = 0;
2170         rt->stereo_off_y = 0;
2171
2172         g_signal_connect(G_OBJECT(pr), "hierarchy-changed",
2173                          G_CALLBACK(rt_hierarchy_changed_cb), rt);
2174
2175         return (RendererFuncs *) rt;
2176 }
2177
2178
2179 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */