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