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