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