Bug Fix #233 Custom Border
[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                         cairo_destroy(cr);
226                         rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
227                         }
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, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
1276                 }
1277 }
1278
1279
1280 static gint rt_get_orientation(RendererTiles *rt)
1281 {
1282         PixbufRenderer *pr = rt->pr;
1283
1284         gint orientation = pr->orientation;
1285         static const gint mirror[]       = {1,   2, 1, 4, 3, 6, 5, 8, 7};
1286         static const gint flip[]         = {1,   4, 3, 2, 1, 8, 7, 6, 5};
1287
1288         if (rt->stereo_mode & PR_STEREO_MIRROR) orientation = mirror[orientation];
1289         if (rt->stereo_mode & PR_STEREO_FLIP) orientation = flip[orientation];
1290         return orientation;
1291 }
1292
1293
1294 static void rt_tile_render(RendererTiles *rt, ImageTile *it,
1295                            gint x, gint y, gint w, gint h,
1296                            gboolean new_data, gboolean fast)
1297 {
1298         PixbufRenderer *pr = rt->pr;
1299         gboolean has_alpha;
1300         gboolean draw = FALSE;
1301         gint orientation = rt_get_orientation(rt);
1302
1303         if (it->render_todo == TILE_RENDER_NONE && it->surface && !new_data) return;
1304
1305         if (it->render_done != TILE_RENDER_ALL)
1306                 {
1307                 x = 0;
1308                 y = 0;
1309                 w = it->w;
1310                 h = it->h;
1311                 if (!fast) it->render_done = TILE_RENDER_ALL;
1312                 }
1313         else if (it->render_todo != TILE_RENDER_AREA)
1314                 {
1315                 if (!fast) it->render_todo = TILE_RENDER_NONE;
1316                 return;
1317                 }
1318
1319         if (!fast) it->render_todo = TILE_RENDER_NONE;
1320
1321         if (new_data) it->blank = FALSE;
1322
1323         rt_tile_prepare(rt, it);
1324         has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
1325
1326         /* FIXME checker colors for alpha should be configurable,
1327          * also should be drawn for blank = TRUE
1328          */
1329
1330         if (it->blank)
1331                 {
1332                 /* no data, do fast rect fill */
1333                 cairo_t *cr;
1334                 cr = cairo_create(it->surface);
1335                 cairo_rectangle (cr, 0, 0, it->w, it->h);
1336                 cairo_set_source_rgb(cr, 0, 0, 0);
1337                 cairo_fill (cr);
1338                 cairo_destroy (cr);
1339                 }
1340         else if (pr->source_tiles_enabled)
1341                 {
1342                 draw = rt_source_tile_render(rt, it, x, y, w, h, new_data, fast);
1343                 }
1344         else
1345                 {
1346                 gdouble scale_x, scale_y;
1347                 gdouble src_x, src_y;
1348                 gint pb_x, pb_y;
1349                 gint pb_w, pb_h;
1350
1351                 if (pr->image_width == 0 || pr->image_height == 0) return;
1352
1353                 scale_x = (gdouble)pr->width / pr->image_width;
1354                 scale_y = (gdouble)pr->height / pr->image_height;
1355
1356                 pr_tile_coords_map_orientation(orientation, it->x, it->y,
1357                                             pr->width, pr->height,
1358                                             rt->tile_width, rt->tile_height,
1359                                             &src_x, &src_y);
1360                 pr_tile_region_map_orientation(orientation, x, y,
1361                                             rt->tile_width, rt->tile_height,
1362                                             w, h,
1363                                             &pb_x, &pb_y,
1364                                             &pb_w, &pb_h);
1365
1366                 switch (orientation)
1367                         {
1368                         gdouble tmp;
1369                         case EXIF_ORIENTATION_LEFT_TOP:
1370                         case EXIF_ORIENTATION_RIGHT_TOP:
1371                         case EXIF_ORIENTATION_RIGHT_BOTTOM:
1372                         case EXIF_ORIENTATION_LEFT_BOTTOM:
1373                                 tmp = scale_x;
1374                                 scale_x = scale_y;
1375                                 scale_y = tmp;
1376                                 break;
1377                         default:
1378                                 /* nothing to do */
1379                                 break;
1380                         }
1381
1382                 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
1383                  * small sizes for anything but GDK_INTERP_NEAREST
1384                  */
1385                 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1386
1387                 rt_tile_get_region(has_alpha,
1388                                    pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
1389                                    (gdouble) 0.0 - src_x - GET_RIGHT_PIXBUF_OFFSET(rt) * scale_x,
1390                                    (gdouble) 0.0 - src_y,
1391                                    scale_x, scale_y,
1392                                    (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1393                                    it->x + pb_x, it->y + pb_y);
1394                 if (rt->stereo_mode & PR_STEREO_ANAGLYPH &&
1395                     (pr->stereo_pixbuf_offset_right > 0 || pr->stereo_pixbuf_offset_left > 0))
1396                         {
1397                         GdkPixbuf *right_pb = rt_get_spare_tile(rt);
1398                         rt_tile_get_region(has_alpha,
1399                                            pr->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h,
1400                                            (gdouble) 0.0 - src_x - GET_LEFT_PIXBUF_OFFSET(rt) * scale_x,
1401                                            (gdouble) 0.0 - src_y,
1402                                            scale_x, scale_y,
1403                                            (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1404                                            it->x + pb_x, it->y + pb_y);
1405                         pr_create_anaglyph(rt->stereo_mode, it->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h);
1406                         /* do not care about freeing spare_tile, it will be reused */
1407                         }
1408                 rt_tile_apply_orientation(rt, orientation, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
1409                 draw = TRUE;
1410                 }
1411
1412         if (draw && it->pixbuf && !it->blank)
1413                 {
1414                 cairo_t *cr;
1415
1416                 if (pr->func_post_process && !(pr->post_process_slow && fast))
1417                         pr->func_post_process(pr, &it->pixbuf, x, y, w, h, pr->post_process_user_data);
1418
1419                 cr = cairo_create(it->surface);
1420                 cairo_rectangle (cr, x, y, w, h);
1421                 gdk_cairo_set_source_pixbuf(cr, it->pixbuf, 0, 0);
1422                 cairo_fill (cr);
1423                 cairo_destroy (cr);
1424                 }
1425 }
1426
1427
1428 static void rt_tile_expose(RendererTiles *rt, ImageTile *it,
1429                            gint x, gint y, gint w, gint h,
1430                            gboolean new_data, gboolean fast)
1431 {
1432         PixbufRenderer *pr = rt->pr;
1433         GtkWidget *box;
1434         GdkWindow *window;
1435         cairo_t *cr;
1436
1437         /* clamp to visible */
1438         if (it->x + x < rt->x_scroll)
1439                 {
1440                 w -= rt->x_scroll - it->x - x;
1441                 x = rt->x_scroll - it->x;
1442                 }
1443         if (it->x + x + w > rt->x_scroll + pr->vis_width)
1444                 {
1445                 w = rt->x_scroll + pr->vis_width - it->x - x;
1446                 }
1447         if (w < 1) return;
1448         if (it->y + y < rt->y_scroll)
1449                 {
1450                 h -= rt->y_scroll - it->y - y;
1451                 y = rt->y_scroll - it->y;
1452                 }
1453         if (it->y + y + h > rt->y_scroll + pr->vis_height)
1454                 {
1455                 h = rt->y_scroll + pr->vis_height - it->y - y;
1456                 }
1457         if (h < 1) return;
1458
1459         rt_tile_render(rt, it, x, y, w, h, new_data, fast);
1460
1461         box = GTK_WIDGET(pr);
1462         window = gtk_widget_get_window(box);
1463
1464         cr = gdk_cairo_create(window);
1465         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);
1466         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);
1467         cairo_fill (cr);
1468         cairo_destroy (cr);
1469
1470         if (rt->overlay_list)
1471                 {
1472                 rt_overlay_draw(rt, pr->x_offset + (it->x - rt->x_scroll) + x,
1473                                 pr->y_offset + (it->y - rt->y_scroll) + y,
1474                                 w, h,
1475                                 it);
1476                 }
1477 }
1478
1479
1480 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it)
1481 {
1482         PixbufRenderer *pr = rt->pr;
1483         return (it->x + it->w >= rt->x_scroll && it->x < rt->x_scroll + pr->vis_width &&
1484                 it->y + it->h >= rt->y_scroll && it->y < rt->y_scroll + pr->vis_height);
1485 }
1486
1487 /*
1488  *-------------------------------------------------------------------
1489  * draw queue
1490  *-------------------------------------------------------------------
1491  */
1492
1493 static gint rt_get_queued_area(GList *work)
1494 {
1495         gint area = 0;
1496
1497         while (work)
1498                 {
1499                 QueueData *qd = work->data;
1500                 area += qd->w * qd->h;
1501                 work = work->next;
1502                 }
1503         return area;
1504 }
1505
1506
1507 static gboolean rt_queue_schedule_next_draw(RendererTiles *rt, gboolean force_set)
1508 {
1509         PixbufRenderer *pr = rt->pr;
1510         gfloat percent;
1511         gint visible_area = pr->vis_width * pr->vis_height;
1512
1513         if (!pr->loading)
1514                 {
1515                 /* 2pass prio */
1516                 DEBUG_2("redraw priority: 2pass");
1517                 rt->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, rt_queue_draw_idle_cb, rt, NULL);
1518                 return FALSE;
1519                 }
1520
1521         if (visible_area == 0)
1522                 {
1523                 /* not known yet */
1524                 percent = 100.0;
1525                 }
1526         else
1527                 {
1528                 percent = 100.0 * rt_get_queued_area(rt->draw_queue) / visible_area;
1529                 }
1530
1531         if (percent > 10.0)
1532                 {
1533                 /* we have enough data for starting intensive redrawing */
1534                 DEBUG_2("redraw priority: high %.2f %%", percent);
1535                 rt->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW, rt_queue_draw_idle_cb, rt, NULL);
1536                 return FALSE;
1537                 }
1538
1539         if (percent < 1.0 || force_set)
1540                 {
1541                 /* queue is (almost) empty, wait  50 ms*/
1542                 DEBUG_2("redraw priority: wait %.2f %%", percent);
1543                 rt->draw_idle_id = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, 50, rt_queue_draw_idle_cb, rt, NULL);
1544                 return FALSE;
1545                 }
1546
1547         /* keep the same priority as before */
1548         DEBUG_2("redraw priority: no change %.2f %%", percent);
1549         return TRUE;
1550 }
1551
1552
1553 static gboolean rt_queue_draw_idle_cb(gpointer data)
1554 {
1555         RendererTiles *rt = data;
1556         PixbufRenderer *pr = rt->pr;
1557         QueueData *qd;
1558         gboolean fast;
1559
1560
1561         if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
1562             (!rt->draw_queue && !rt->draw_queue_2pass) ||
1563             !rt->draw_idle_id)
1564                 {
1565                 pr_render_complete_signal(pr);
1566
1567                 rt->draw_idle_id = 0;
1568                 return FALSE;
1569                 }
1570
1571         if (rt->draw_queue)
1572                 {
1573                 qd = rt->draw_queue->data;
1574                 fast = (pr->zoom_2pass && ((pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0) || pr->post_process_slow));
1575                 }
1576         else
1577                 {
1578                 if (pr->loading)
1579                         {
1580                         /* still loading, wait till done (also drops the higher priority) */
1581
1582                         return rt_queue_schedule_next_draw(rt, FALSE);
1583                         }
1584
1585                 qd = rt->draw_queue_2pass->data;
1586                 fast = FALSE;
1587                 }
1588
1589         if (gtk_widget_get_realized(GTK_WIDGET(pr)))
1590                 {
1591                 if (rt_tile_is_visible(rt, qd->it))
1592                         {
1593                         rt_tile_expose(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1594                         }
1595                 else if (qd->new_data)
1596                         {
1597                         /* if new pixel data, and we already have a pixmap, update the tile */
1598                         qd->it->blank = FALSE;
1599                         if (qd->it->surface && qd->it->render_done == TILE_RENDER_ALL)
1600                                 {
1601                                 rt_tile_render(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1602                                 }
1603                         }
1604                 }
1605
1606         if (rt->draw_queue)
1607                 {
1608                 qd->it->qd = NULL;
1609                 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
1610                 if (fast)
1611                         {
1612                         if (qd->it->qd2)
1613                                 {
1614                                 rt_queue_merge(qd->it->qd2, qd);
1615                                 g_free(qd);
1616                                 }
1617                         else
1618                                 {
1619                                 qd->it->qd2 = qd;
1620                                 rt->draw_queue_2pass = g_list_append(rt->draw_queue_2pass, qd);
1621                                 }
1622                         }
1623                 else
1624                         {
1625                         g_free(qd);
1626                         }
1627                 }
1628         else
1629                 {
1630                 qd->it->qd2 = NULL;
1631                 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
1632                 g_free(qd);
1633                 }
1634
1635         if (!rt->draw_queue && !rt->draw_queue_2pass)
1636                 {
1637                 pr_render_complete_signal(pr);
1638
1639                 rt->draw_idle_id = 0;
1640                 return FALSE;
1641                 }
1642
1643                 return rt_queue_schedule_next_draw(rt, FALSE);
1644 }
1645
1646 static void rt_queue_list_free(GList *list)
1647 {
1648         GList *work;
1649
1650         work = list;
1651         while (work)
1652                 {
1653                 QueueData *qd;
1654
1655                 qd = work->data;
1656                 work = work->next;
1657
1658                 qd->it->qd = NULL;
1659                 qd->it->qd2 = NULL;
1660                 g_free(qd);
1661                 }
1662
1663         g_list_free(list);
1664 }
1665
1666 static void rt_queue_clear(RendererTiles *rt)
1667 {
1668         rt_queue_list_free(rt->draw_queue);
1669         rt->draw_queue = NULL;
1670
1671         rt_queue_list_free(rt->draw_queue_2pass);
1672         rt->draw_queue_2pass = NULL;
1673
1674         if (rt->draw_idle_id)
1675                 {
1676                 g_source_remove(rt->draw_idle_id);
1677                 rt->draw_idle_id = 0;
1678                 }
1679         rt_sync_scroll(rt);
1680 }
1681
1682 static void rt_queue_merge(QueueData *parent, QueueData *qd)
1683 {
1684         if (parent->x + parent->w < qd->x + qd->w)
1685                 {
1686                 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
1687                 }
1688         if (parent->x > qd->x)
1689                 {
1690                 parent->w += parent->x - qd->x;
1691                 parent->x = qd->x;
1692                 }
1693
1694         if (parent->y + parent->h < qd->y + qd->h)
1695                 {
1696                 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
1697                 }
1698         if (parent->y > qd->y)
1699                 {
1700                 parent->h += parent->y - qd->y;
1701                 parent->y = qd->y;
1702                 }
1703
1704         parent->new_data |= qd->new_data;
1705 }
1706
1707 static gboolean rt_clamp_to_visible(RendererTiles *rt, gint *x, gint *y, gint *w, gint *h)
1708 {
1709         PixbufRenderer *pr = rt->pr;
1710         gint nx, ny;
1711         gint nw, nh;
1712         gint vx, vy;
1713         gint vw, vh;
1714
1715         vw = pr->vis_width;
1716         vh = pr->vis_height;
1717
1718         vx = rt->x_scroll;
1719         vy = rt->y_scroll;
1720
1721         if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
1722
1723         /* now clamp it */
1724         nx = CLAMP(*x, vx, vx + vw);
1725         nw = CLAMP(*w - (nx - *x), 1, vw);
1726
1727         ny = CLAMP(*y, vy, vy + vh);
1728         nh = CLAMP(*h - (ny - *y), 1, vh);
1729
1730         *x = nx;
1731         *y = ny;
1732         *w = nw;
1733         *h = nh;
1734
1735         return TRUE;
1736 }
1737
1738 static gboolean rt_queue_to_tiles(RendererTiles *rt, gint x, gint y, gint w, gint h,
1739                                   gboolean clamp, ImageRenderType render,
1740                                   gboolean new_data, gboolean only_existing)
1741 {
1742         PixbufRenderer *pr = rt->pr;
1743         gint i, j;
1744         gint x1, x2;
1745         gint y1, y2;
1746
1747         if (clamp && !rt_clamp_to_visible(rt, &x, &y, &w, &h)) return FALSE;
1748
1749         x1 = ROUND_DOWN(x, rt->tile_width);
1750         x2 = ROUND_UP(x + w, rt->tile_width);
1751
1752         y1 = ROUND_DOWN(y, rt->tile_height);
1753         y2 = ROUND_UP(y + h, rt->tile_height);
1754
1755         for (j = y1; j <= y2; j += rt->tile_height)
1756                 {
1757                 for (i = x1; i <= x2; i += rt->tile_width)
1758                         {
1759                         ImageTile *it;
1760
1761                         it = rt_tile_get(rt, i, j,
1762                                          (only_existing &&
1763                                           (i + rt->tile_width < rt->x_scroll ||
1764                                            i > rt->x_scroll + pr->vis_width ||
1765                                            j + rt->tile_height < rt->y_scroll ||
1766                                            j > rt->y_scroll + pr->vis_height)));
1767                         if (it)
1768                                 {
1769                                 QueueData *qd;
1770
1771                                 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
1772                                     (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
1773                                         {
1774                                         it->render_todo = render;
1775                                         }
1776
1777                                 qd = g_new(QueueData, 1);
1778                                 qd->it = it;
1779                                 qd->new_data = new_data;
1780
1781                                 if (i < x)
1782                                         {
1783                                         qd->x = x - i;
1784                                         }
1785                                 else
1786                                         {
1787                                         qd->x = 0;
1788                                         }
1789                                 qd->w = x + w - i - qd->x;
1790                                 if (qd->x + qd->w > rt->tile_width) qd->w = rt->tile_width - qd->x;
1791
1792                                 if (j < y)
1793                                         {
1794                                         qd->y = y - j;
1795                                         }
1796                                 else
1797                                         {
1798                                         qd->y = 0;
1799                                         }
1800                                 qd->h = y + h - j - qd->y;
1801                                 if (qd->y + qd->h > rt->tile_height) qd->h = rt->tile_height - qd->y;
1802
1803                                 if (qd->w < 1 || qd->h < 1)
1804                                         {
1805                                         g_free(qd);
1806                                         }
1807                                 else if (it->qd)
1808                                         {
1809                                         rt_queue_merge(it->qd, qd);
1810                                         g_free(qd);
1811                                         }
1812                                 else
1813                                         {
1814                                         it->qd = qd;
1815                                         rt->draw_queue = g_list_append(rt->draw_queue, qd);
1816                                         }
1817                                 }
1818                         }
1819                 }
1820
1821         return TRUE;
1822 }
1823
1824 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
1825                      gboolean clamp, ImageRenderType render,
1826                      gboolean new_data, gboolean only_existing)
1827 {
1828         PixbufRenderer *pr = rt->pr;
1829         gint nx, ny;
1830
1831         rt_sync_scroll(rt);
1832
1833         nx = CLAMP(x, 0, pr->width - 1);
1834         ny = CLAMP(y, 0, pr->height - 1);
1835         w -= (nx - x);
1836         h -= (ny - y);
1837         w = CLAMP(w, 0, pr->width - nx);
1838         h = CLAMP(h, 0, pr->height - ny);
1839         if (w < 1 || h < 1) return;
1840
1841         if (rt_queue_to_tiles(rt, nx, ny, w, h, clamp, render, new_data, only_existing) &&
1842             ((!rt->draw_queue && !rt->draw_queue_2pass) || !rt->draw_idle_id))
1843                 {
1844                 if (rt->draw_idle_id)
1845                         {
1846                         g_source_remove(rt->draw_idle_id);
1847                         rt->draw_idle_id = 0;
1848                         }
1849                 rt_queue_schedule_next_draw(rt, TRUE);
1850                 }
1851 }
1852
1853 static void rt_scroll(void *renderer, gint x_off, gint y_off)
1854 {
1855         RendererTiles *rt = (RendererTiles *) renderer;
1856         PixbufRenderer *pr = rt->pr;
1857
1858         rt_sync_scroll(rt);
1859         if (rt->stereo_mode & PR_STEREO_MIRROR) x_off = -x_off;
1860         if (rt->stereo_mode & PR_STEREO_FLIP) y_off = -y_off;
1861
1862         gint w = pr->vis_width - abs(x_off);
1863         gint h = pr->vis_height - abs(y_off);
1864
1865         if (w < 1 || h < 1)
1866                 {
1867                 /* scrolled completely to new material */
1868                 rt_queue(rt, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1869                 return;
1870                 }
1871         else
1872                 {
1873                 gint x1, y1;
1874                 gint x2, y2;
1875                 GtkWidget *box;
1876                 GdkWindow *window;
1877                 cairo_t *cr;
1878                 cairo_surface_t *surface;
1879
1880                 if (x_off < 0)
1881                         {
1882                         x1 = abs(x_off);
1883                         x2 = 0;
1884                         }
1885                 else
1886                         {
1887                         x1 = 0;
1888                         x2 = abs(x_off);
1889                         }
1890
1891                 if (y_off < 0)
1892                         {
1893                         y1 = abs(y_off);
1894                         y2 = 0;
1895                         }
1896                 else
1897                         {
1898                         y1 = 0;
1899                         y2 = abs(y_off);
1900                         }
1901
1902                 box = GTK_WIDGET(pr);
1903                 window = gtk_widget_get_window(box);
1904
1905                 cr = gdk_cairo_create(window);
1906                 surface = cairo_get_target(cr);
1907                 /* clipping restricts the intermediate surface's size, so it's a good idea
1908                  * to use it. */
1909                 cairo_rectangle(cr, x1 + pr->x_offset + rt->stereo_off_x, y1 + pr->y_offset + rt->stereo_off_y, w, h);
1910                 cairo_clip (cr);
1911                 /* Now push a group to change the target */
1912                 cairo_push_group (cr);
1913                 cairo_set_source_surface(cr, surface, x1 - x2, y1 - y2);
1914                 cairo_paint(cr);
1915                 /* Now copy the intermediate target back */
1916                 cairo_pop_group_to_source(cr);
1917                 cairo_paint(cr);
1918                 cairo_destroy(cr);
1919
1920                 rt_overlay_queue_all(rt, x2, y2, x1, y1);
1921
1922                 w = pr->vis_width - w;
1923                 h = pr->vis_height - h;
1924
1925                 if (w > 0)
1926                         {
1927                         rt_queue(rt,
1928                                     x_off > 0 ? rt->x_scroll + (pr->vis_width - w) : rt->x_scroll, rt->y_scroll,
1929                                     w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1930                         }
1931                 if (h > 0)
1932                         {
1933                         /* FIXME, to optimize this, remove overlap */
1934                         rt_queue(rt,
1935                                     rt->x_scroll, y_off > 0 ? rt->y_scroll + (pr->vis_height - h) : rt->y_scroll,
1936                                     pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1937                         }
1938                 }
1939 }
1940
1941 static void renderer_area_changed(void *renderer, gint src_x, gint src_y, gint src_w, gint src_h)
1942 {
1943         RendererTiles *rt = (RendererTiles *)renderer;
1944         PixbufRenderer *pr = rt->pr;
1945         gint x, y, width, height,  x1, y1, x2, y2;
1946
1947         gint orientation = rt_get_orientation(rt);
1948         pr_coords_map_orientation_reverse(orientation,
1949                                      src_x - GET_RIGHT_PIXBUF_OFFSET(rt), src_y,
1950                                      pr->image_width, pr->image_height,
1951                                      src_w, src_h,
1952                                      &x, &y,
1953                                      &width, &height);
1954
1955         if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
1956                 {
1957                 /* increase region when using a zoom quality that may access surrounding pixels */
1958                 y -= 1;
1959                 height += 2;
1960                 }
1961
1962         x1 = (gint)floor((gdouble)x * pr->scale);
1963         y1 = (gint)floor((gdouble)y * pr->scale * pr->aspect_ratio);
1964         x2 = (gint)ceil((gdouble)(x + width) * pr->scale);
1965         y2 = (gint)ceil((gdouble)(y + height) * pr->scale * pr->aspect_ratio);
1966
1967         rt_queue(rt, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
1968 }
1969
1970 static void renderer_redraw(RendererTiles *rt, gint x, gint y, gint w, gint h,
1971                      gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing)
1972 {
1973         PixbufRenderer *pr = rt->pr;
1974
1975         x -= rt->stereo_off_x;
1976         y -= rt->stereo_off_y;
1977
1978         rt_border_draw(rt, x, y, w, h);
1979
1980         x = MAX(0, x - pr->x_offset + pr->x_scroll);
1981         y = MAX(0, y - pr->y_offset + pr->y_scroll);
1982
1983         rt_queue(rt,
1984                  x, y,
1985                  MIN(w, pr->width - x),
1986                  MIN(h, pr->height - y),
1987                  clamp, render, new_data, only_existing);
1988 }
1989
1990 static void renderer_update_pixbuf(void *renderer, gboolean lazy)
1991 {
1992         rt_queue_clear((RendererTiles *)renderer);
1993 }
1994
1995 static void renderer_update_zoom(void *renderer, gboolean lazy)
1996 {
1997         RendererTiles *rt = (RendererTiles *)renderer;
1998         PixbufRenderer *pr = rt->pr;
1999
2000         rt_tile_invalidate_all((RendererTiles *)renderer);
2001         if (!lazy)
2002                 {
2003                 renderer_redraw(renderer, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, TRUE, FALSE);
2004                 }
2005         rt_border_clear(rt);
2006 }
2007
2008 static void renderer_invalidate_region(void *renderer, gint x, gint y, gint w, gint h)
2009 {
2010         rt_tile_invalidate_region((RendererTiles *)renderer, x, y, w, h);
2011 }
2012
2013 static void renderer_update_viewport(void *renderer)
2014 {
2015         RendererTiles *rt = (RendererTiles *)renderer;
2016
2017         rt->stereo_off_x = 0;
2018         rt->stereo_off_y = 0;
2019
2020         if (rt->stereo_mode & PR_STEREO_RIGHT)
2021                 {
2022                 if (rt->stereo_mode & PR_STEREO_HORIZ)
2023                         {
2024                         rt->stereo_off_x = rt->pr->viewport_width;
2025                         }
2026                 else if (rt->stereo_mode & PR_STEREO_VERT)
2027                         {
2028                         rt->stereo_off_y = rt->pr->viewport_height;
2029                         }
2030                 else if (rt->stereo_mode & PR_STEREO_FIXED)
2031                         {
2032                         rt->stereo_off_x = rt->pr->stereo_fixed_x_right;
2033                         rt->stereo_off_y = rt->pr->stereo_fixed_y_right;
2034                         }
2035                 }
2036         else
2037                 {
2038                 if (rt->stereo_mode & PR_STEREO_FIXED)
2039                         {
2040                         rt->stereo_off_x = rt->pr->stereo_fixed_x_left;
2041                         rt->stereo_off_y = rt->pr->stereo_fixed_y_left;
2042                         }
2043                 }
2044         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);
2045         rt_sync_scroll(rt);
2046         rt_overlay_update_sizes(rt);
2047         rt_border_clear(rt);
2048 }
2049
2050 static void renderer_stereo_set(void *renderer, gint stereo_mode)
2051 {
2052         RendererTiles *rt = (RendererTiles *)renderer;
2053
2054         rt->stereo_mode = stereo_mode;
2055 }
2056
2057 static void renderer_free(void *renderer)
2058 {
2059         RendererTiles *rt = (RendererTiles *)renderer;
2060         rt_queue_clear(rt);
2061         rt_tile_free_all(rt);
2062         if (rt->spare_tile) g_object_unref(rt->spare_tile);
2063         if (rt->overlay_buffer) g_object_unref(rt->overlay_buffer);
2064         rt_overlay_list_clear(rt);
2065         /* disconnect "hierarchy-changed" */
2066         g_signal_handlers_disconnect_matched(G_OBJECT(rt->pr), G_SIGNAL_MATCH_DATA,
2067                                                      0, 0, 0, NULL, rt);
2068         g_free(rt);
2069 }
2070
2071 #if GTK_CHECK_VERSION(3,0,0)
2072
2073 static gboolean rt_draw_cb(GtkWidget *widget, cairo_t *cr, gpointer data)
2074 {
2075         RendererTiles *rt = (RendererTiles *)data;
2076         if (gtk_widget_is_drawable(widget))
2077                 {
2078                 if (gtk_widget_get_has_window(widget))
2079                         {
2080                         GdkRectangle area;
2081                         if (gdk_cairo_get_clip_rectangle(cr, &area))
2082                                 {
2083                                 renderer_redraw(rt, area.x, area.y, area.width, area.height,
2084                                                 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2085                                 }
2086                         }
2087                 }
2088
2089         return FALSE;
2090 }
2091
2092 #else
2093 static gboolean rt_expose_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
2094 {
2095         RendererTiles *rt = (RendererTiles *)data;
2096         if (gtk_widget_is_drawable(widget))
2097                 {
2098                 if (gtk_widget_get_has_window(widget))
2099                         {
2100                         if (event->window != gtk_widget_get_window(widget))
2101                                 {
2102                                 GdkRectangle area;
2103
2104                                 gdk_window_get_position(event->window, &area.x, &area.y);
2105                                 area.x += event->area.x;
2106                                 area.y += event->area.y;
2107                                 area.width = event->area.width;
2108                                 area.height = event->area.height;
2109                                 renderer_redraw(rt, area.x, area.y, area.width, area.height,
2110                                                 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2111
2112                                 }
2113                         else
2114                                 {
2115                                 renderer_redraw(rt, event->area.x, event->area.y, event->area.width, event->area.height,
2116                                                 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2117                                 }
2118                         }
2119                 }
2120
2121         return FALSE;
2122 }
2123 #endif
2124
2125
2126 RendererFuncs *renderer_tiles_new(PixbufRenderer *pr)
2127 {
2128         RendererTiles *rt = g_new0(RendererTiles, 1);
2129
2130         rt->pr = pr;
2131
2132         rt->f.area_changed = renderer_area_changed;
2133         rt->f.update_pixbuf = renderer_update_pixbuf;
2134         rt->f.free = renderer_free;
2135         rt->f.update_zoom = renderer_update_zoom;
2136         rt->f.invalidate_region = renderer_invalidate_region;
2137         rt->f.scroll = rt_scroll;
2138         rt->f.update_viewport = renderer_update_viewport;
2139
2140
2141         rt->f.overlay_add = renderer_tiles_overlay_add;
2142         rt->f.overlay_set = renderer_tiles_overlay_set;
2143         rt->f.overlay_get = renderer_tiles_overlay_get;
2144
2145         rt->f.stereo_set = renderer_stereo_set;
2146
2147         rt->tile_width = PR_TILE_SIZE;
2148         rt->tile_height = PR_TILE_SIZE;
2149
2150         rt->tiles = NULL;
2151         rt->tile_cache_size = 0;
2152
2153         rt->tile_cache_max = PR_CACHE_SIZE_DEFAULT;
2154
2155         rt->draw_idle_id = 0;
2156
2157         rt->stereo_mode = 0;
2158         rt->stereo_off_x = 0;
2159         rt->stereo_off_y = 0;
2160
2161         g_signal_connect(G_OBJECT(pr), "hierarchy-changed",
2162                          G_CALLBACK(rt_hierarchy_changed_cb), rt);
2163
2164 #if GTK_CHECK_VERSION(3,0,0)
2165         g_signal_connect(G_OBJECT(pr), "draw",
2166                          G_CALLBACK(rt_draw_cb), rt);
2167 #else
2168         g_signal_connect(G_OBJECT(pr), "expose_event",
2169                          G_CALLBACK(rt_expose_cb), rt);
2170 #endif
2171         return (RendererFuncs *) rt;
2172 }
2173
2174
2175 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */