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