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