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