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