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