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