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