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