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