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