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