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