Fix #926: Use system background color in window mode
[geeqie.git] / src / renderer-tiles.c
index 6a034e1..5ebf0d5 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Copyright (C) 2006 John Ellis
- * Copyright (C) 2008 - 2016 The Geeqie Team
+ * Copyright (C) 2008 - 2021 The Geeqie Team
  *
  * Author: John Ellis
  *
@@ -58,10 +58,6 @@ typedef enum {
 } ExifOrientationType;
 #endif
 
-
-/* size to use when breaking up image pane for rendering */
-#define PR_TILE_SIZE 128
-
 typedef struct _ImageTile ImageTile;
 typedef struct _QueueData QueueData;
 
@@ -123,7 +119,7 @@ struct _RendererTiles
        RendererFuncs f;
        PixbufRenderer *pr;
 
-       gint tile_cache_max;            /* max mb to use for offscreen buffer */
+       gint tile_cache_max;            /* max MiB to use for offscreen buffer */
 
        gint tile_width;
        gint tile_height;
@@ -135,6 +131,7 @@ struct _RendererTiles
 
        GList *overlay_list;
        cairo_surface_t *overlay_buffer;
+       cairo_surface_t *surface;
 
        guint draw_idle_id; /* event source id */
 
@@ -147,6 +144,7 @@ struct _RendererTiles
        gint x_scroll;  /* allow local adjustment and mirroring */
        gint y_scroll;
 
+       gint hidpi_scale;
 };
 
 
@@ -209,8 +207,11 @@ static void rt_border_draw(RendererTiles *rt, gint x, gint y, gint w, gint h)
 
        if (!window) return;
 
+#if GTK_CHECK_VERSION(3,0,0)
+       cr = cairo_create(rt->surface);
+#else
        cr = gdk_cairo_create(window);
-
+#endif
 
        if (!pr->pixbuf && !pr->source_tiles_enabled)
                {
@@ -496,7 +497,26 @@ static gint pixmap_calc_size(cairo_surface_t *surface)
 
 //     d = gdk_drawable_get_depth(pixmap);
 //     gdk_drawable_get_size(pixmap, &w, &h);
-       return PR_TILE_SIZE * PR_TILE_SIZE * 4 / 8;
+       return options->image.tile_size * options->image.tile_size * 4 / 8;
+}
+
+static void rt_hidpi_aware_draw(
+       RendererTiles *rt,
+       cairo_t *cr,
+       GdkPixbuf *pixbuf,
+       double x,
+       double y)
+{
+#if GTK_CHECK_VERSION(3, 10, 0)
+       cairo_surface_t *surface;
+       surface = gdk_cairo_surface_create_from_pixbuf(pixbuf, rt->hidpi_scale, NULL);
+       cairo_set_source_surface(cr, surface, x, y);
+       cairo_fill(cr);
+       cairo_surface_destroy(surface);
+#else
+       gdk_cairo_set_source_pixbuf(cr, pixbuf, x, y);
+       cairo_fill(cr);
+#endif
 }
 
 static void rt_tile_prepare(RendererTiles *rt, ImageTile *it)
@@ -511,7 +531,7 @@ static void rt_tile_prepare(RendererTiles *rt, ImageTile *it)
                                                            CAIRO_CONTENT_COLOR,
                                                            rt->tile_width, rt->tile_height);
 
-               size = pixmap_calc_size(surface);
+               size = pixmap_calc_size(surface) * rt->hidpi_scale * rt->hidpi_scale;
                rt_tile_free_space(rt, size, it);
 
                it->surface = surface;
@@ -523,9 +543,9 @@ static void rt_tile_prepare(RendererTiles *rt, ImageTile *it)
                {
                GdkPixbuf *pixbuf;
                guint size;
-               pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, rt->tile_width, rt->tile_height);
+               pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, rt->hidpi_scale * rt->tile_width, rt->hidpi_scale * rt->tile_height);
 
-               size = gdk_pixbuf_get_rowstride(pixbuf) * rt->tile_height;
+               size = gdk_pixbuf_get_rowstride(pixbuf) * rt->tile_height * rt->hidpi_scale;
                rt_tile_free_space(rt, size, it);
 
                it->pixbuf = pixbuf;
@@ -582,7 +602,9 @@ static void rt_overlay_init_window(RendererTiles *rt, OverlayData *od)
        od->window = gdk_window_new(gtk_widget_get_window(GTK_WIDGET(pr)), &attributes, attributes_mask);
        gdk_window_set_user_data(od->window, pr);
        gdk_window_move(od->window, px + rt->stereo_off_x, py + rt->stereo_off_y);
+#if !GTK_CHECK_VERSION(3,0,0)
        gdk_window_show(od->window);
+#endif
 }
 
 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h,
@@ -623,7 +645,7 @@ static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h,
                                cairo_fill_preserve(cr);
 
                                gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - rx, py - ry);
-                               cairo_fill (cr);
+                               cairo_fill(cr);
                                cairo_destroy (cr);
 
                                cr = gdk_cairo_create(od->window);
@@ -766,7 +788,11 @@ gint renderer_tiles_overlay_add(void *renderer, GdkPixbuf *pixbuf, gint x, gint
 
        rt->overlay_list = g_list_append(rt->overlay_list, od);
 
+#if GTK_CHECK_VERSION(3,0,0)
+       gtk_widget_queue_draw(GTK_WIDGET(rt->pr));
+#else
        rt_overlay_queue_draw(rt, od, 0, 0, 0, 0);
+#endif
 
        return od->id;
 }
@@ -814,6 +840,32 @@ static void rt_overlay_list_reset_window(RendererTiles *rt)
                }
 }
 
+#if GTK_CHECK_VERSION(3,0,0)
+void renderer_tiles_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint x, gint y)
+{
+       RendererTiles *rc = (RendererTiles *)renderer;
+       PixbufRenderer *pr = rc->pr;
+       OverlayData *od;
+
+       g_return_if_fail(IS_PIXBUF_RENDERER(pr));
+
+       od = rt_overlay_find(rc, id);
+       if (!od) return;
+
+       if (pixbuf)
+               {
+               g_object_ref(G_OBJECT(pixbuf));
+               g_object_unref(G_OBJECT(od->pixbuf));
+               od->pixbuf = pixbuf;
+               }
+       else
+               {
+               rt_overlay_free(rc, od);
+               }
+
+       gtk_widget_queue_draw(GTK_WIDGET(rc->pr));
+}
+#else
 void renderer_tiles_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint x, gint y)
 {
        RendererTiles *rt = (RendererTiles *) renderer;
@@ -848,6 +900,7 @@ void renderer_tiles_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint
                rt_overlay_free(rt, od);
                }
 }
+#endif
 
 gboolean renderer_tiles_overlay_get(void *renderer, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
 {
@@ -881,7 +934,7 @@ static void rt_hierarchy_changed_cb(GtkWidget *widget, GtkWidget *previous_tople
 
 static GdkPixbuf *rt_get_spare_tile(RendererTiles *rt)
 {
-       if (!rt->spare_tile) rt->spare_tile = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, rt->tile_width, rt->tile_height);
+       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);
        return rt->spare_tile;
 }
 
@@ -896,7 +949,7 @@ static void rt_tile_rotate_90_clockwise(RendererTiles *rt, GdkPixbuf **tile, gin
        guchar *sp, *dp;
        guchar *ip, *spi, *dpi;
        gint i, j;
-       gint tw = rt->tile_width;
+       gint tw = rt->tile_width * rt->hidpi_scale;
 
        srs = gdk_pixbuf_get_rowstride(src);
        s_pix = gdk_pixbuf_get_pixels(src);
@@ -932,7 +985,7 @@ static void rt_tile_rotate_90_counter_clockwise(RendererTiles *rt, GdkPixbuf **t
        guchar *sp, *dp;
        guchar *ip, *spi, *dpi;
        gint i, j;
-       gint th = rt->tile_height;
+       gint th = rt->tile_height * rt->hidpi_scale;
 
        srs = gdk_pixbuf_get_rowstride(src);
        s_pix = gdk_pixbuf_get_pixels(src);
@@ -969,7 +1022,7 @@ static void rt_tile_mirror_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gin
        guchar *spi, *dpi;
        gint i, j;
 
-       gint tw = rt->tile_width;
+       gint tw = rt->tile_width * rt->hidpi_scale;
 
        srs = gdk_pixbuf_get_rowstride(src);
        s_pix = gdk_pixbuf_get_pixels(src);
@@ -1005,8 +1058,8 @@ static void rt_tile_mirror_and_flip(RendererTiles *rt, GdkPixbuf **tile, gint x,
        guchar *sp, *dp;
        guchar *dpi;
        gint i, j;
-       gint tw = rt->tile_width;
-       gint th = rt->tile_height;
+       gint tw = rt->tile_width * rt->hidpi_scale;
+       gint th = rt->tile_height * rt->hidpi_scale;
 
        srs = gdk_pixbuf_get_rowstride(src);
        s_pix = gdk_pixbuf_get_pixels(src);
@@ -1041,7 +1094,7 @@ static void rt_tile_flip_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint
        guchar *sp, *dp;
        guchar *spi, *dpi;
        gint i;
-       gint th = rt->tile_height;
+       gint th = rt->tile_height * rt->hidpi_scale;
 
        srs = gdk_pixbuf_get_rowstride(src);
        s_pix = gdk_pixbuf_get_pixels(src);
@@ -1150,12 +1203,12 @@ static gboolean rt_source_tile_render(RendererTiles *rt, ImageTile *it,
                                if (st->blank)
                                        {
                                        cairo_set_source_rgb(cr, 0, 0, 0);
+                                       cairo_fill (cr);
                                        }
                                else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
                                        {
-                                       gdk_cairo_set_source_pixbuf(cr, st->pixbuf, -it->x + st->x, -it->y + st->y);
+                                       rt_hidpi_aware_draw(rt, cr, st->pixbuf, -it->x + st->x, -it->y + st->y);
                                        }
-                               cairo_fill (cr);
                                cairo_destroy (cr);
                                }
                        }
@@ -1236,49 +1289,124 @@ static gboolean rt_source_tile_render(RendererTiles *rt, ImageTile *it,
        return draw;
 }
 
-static void rt_tile_get_region(gboolean has_alpha,
+/**
+ * @brief 
+ * @param has_alpha 
+ * @param ignore_alpha 
+ * @param src 
+ * @param dest 
+ * @param pb_x 
+ * @param pb_y 
+ * @param pb_w 
+ * @param pb_h 
+ * @param offset_x 
+ * @param offset_y 
+ * @param scale_x 
+ * @param scale_y 
+ * @param interp_type 
+ * @param check_x 
+ * @param check_y 
+ * @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
+ * 
+ * 
+ */
+static void rt_tile_get_region(gboolean has_alpha, gboolean ignore_alpha,
                                const GdkPixbuf *src, GdkPixbuf *dest,
                                int pb_x, int pb_y, int pb_w, int pb_h,
                                double offset_x, double offset_y, double scale_x, double scale_y,
                                GdkInterpType interp_type,
-                               int check_x, int check_y)
+                               int check_x, int check_y, gboolean wide_image)
 {
+       GdkPixbuf* tmppixbuf;
+       gint srs;
+       gint drs;
+       gint x;
+       gint y;
+       gint c;
+       guchar *psrc;
+       guchar *pdst;
+
        if (!has_alpha)
                {
                if (scale_x == 1.0 && scale_y == 1.0)
                        {
-                       gdk_pixbuf_copy_area(src,
-                                            -offset_x + pb_x, -offset_y + pb_y,
-                                            pb_w, pb_h,
-                                            dest,
-                                            pb_x, pb_y);
+                       if (wide_image)
+                               {
+                               srs = gdk_pixbuf_get_rowstride(src);
+                               drs = gdk_pixbuf_get_rowstride(dest);
+                               psrc = gdk_pixbuf_get_pixels(src);
+                               pdst = gdk_pixbuf_get_pixels(dest);
+                               for (y = 0; y < pb_h; y++)
+                                       {
+                                       for (x = 0; x < pb_w; x++)
+                                               {
+                                               for (c = 0; c < 3; c++)
+                                                       {
+                                                       pdst[(y * drs) + x*3 + c] = psrc[(-(int)offset_y + pb_y + y) * srs + (-(int)offset_x + pb_x+x)*3 + c];
+                                                       }
+                                               }
+                                       }
+                               }
+                       else
+                               {
+                               gdk_pixbuf_copy_area(src,
+                                                                       -offset_x + pb_x, -offset_y + pb_y,
+                                                                       pb_w, pb_h,
+                                                                       dest,
+                                                                       pb_x, pb_y);
+                                       }
                        }
                else
                        {
                        gdk_pixbuf_scale(src, dest,
-                                        pb_x, pb_y, pb_w, pb_h,
-                                        offset_x,
-                                        offset_y,
-                                        scale_x, scale_y,
-                                        interp_type);
+                                                       pb_x, pb_y, pb_w, pb_h,
+                                                       offset_x,
+                                                       offset_y,
+                                                       scale_x, scale_y,
+                                                       (wide_image && interp_type == GDK_INTERP_NEAREST) ? GDK_INTERP_TILES : interp_type);
                        }
                }
        else
                {
-               gdk_pixbuf_composite_color(src, dest,
-                                        pb_x, pb_y, pb_w, pb_h,
-                                        offset_x,
-                                        offset_y,
-                                        scale_x, scale_y,
-                                        interp_type,
-                                        255, check_x, check_y,
-                                        PR_ALPHA_CHECK_SIZE,
-                                        ((options->image.alpha_color_1.red << 8 & 0x00FF0000) +
-                                        (options->image.alpha_color_1.green & 0x00FF00) +
-                                        (options->image.alpha_color_1.blue >> 8 & 0x00FF)),
-                                        ((options->image.alpha_color_2.red << 8 & 0x00FF0000) +
-                                        (options->image.alpha_color_2.green & 0x00FF00) +
-                                        (options->image.alpha_color_2.blue >> 8 & 0x00FF)));
+               if (ignore_alpha)
+                       {
+                       tmppixbuf = gdk_pixbuf_add_alpha(src, FALSE, 0, 0, 0);
+
+                       pixbuf_ignore_alpha_rect(tmppixbuf, 0, 0, gdk_pixbuf_get_width(src), gdk_pixbuf_get_height(src));
+
+                       gdk_pixbuf_composite_color(tmppixbuf, dest,
+                                       pb_x, pb_y, pb_w, pb_h,
+                                       offset_x,
+                                       offset_y,
+                                       scale_x, scale_y,
+                                       (scale_x == 1.0 && scale_y == 1.0) ? GDK_INTERP_NEAREST : interp_type,
+                                       255, check_x, check_y,
+                                       PR_ALPHA_CHECK_SIZE,
+                                       ((options->image.alpha_color_1.red << 8 & 0x00FF0000) +
+                                       (options->image.alpha_color_1.green & 0x00FF00) +
+                                       (options->image.alpha_color_1.blue >> 8 & 0x00FF)),
+                                       ((options->image.alpha_color_2.red << 8 & 0x00FF0000) +
+                                       (options->image.alpha_color_2.green & 0x00FF00) +
+                                       (options->image.alpha_color_2.blue >> 8 & 0x00FF)));
+                       g_object_unref(tmppixbuf);
+                       }
+               else
+                       {
+                       gdk_pixbuf_composite_color(src, dest,
+                                       pb_x, pb_y, pb_w, pb_h,
+                                       offset_x,
+                                       offset_y,
+                                       scale_x, scale_y,
+                                       (scale_x == 1.0 && scale_y == 1.0) ? GDK_INTERP_NEAREST : interp_type,
+                                       255, check_x, check_y,
+                                       PR_ALPHA_CHECK_SIZE,
+                                       ((options->image.alpha_color_1.red << 8 & 0x00FF0000) +
+                                       (options->image.alpha_color_1.green & 0x00FF00) +
+                                       (options->image.alpha_color_1.blue >> 8 & 0x00FF)),
+                                       ((options->image.alpha_color_2.red << 8 & 0x00FF0000) +
+                                       (options->image.alpha_color_2.green & 0x00FF00) +
+                                       (options->image.alpha_color_2.blue >> 8 & 0x00FF)));
+                       }
                }
 }
 
@@ -1305,6 +1433,7 @@ static void rt_tile_render(RendererTiles *rt, ImageTile *it,
        gboolean has_alpha;
        gboolean draw = FALSE;
        gint orientation = rt_get_orientation(rt);
+       gboolean wide_image = FALSE;
 
        if (it->render_todo == TILE_RENDER_NONE && it->surface && !new_data) return;
 
@@ -1329,7 +1458,7 @@ static void rt_tile_render(RendererTiles *rt, ImageTile *it,
        rt_tile_prepare(rt, it);
        has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
 
-       /* FIXME checker colors for alpha should be configurable,
+       /** @FIXME checker colors for alpha should be configurable,
         * also should be drawn for blank = TRUE
         */
 
@@ -1356,8 +1485,8 @@ static void rt_tile_render(RendererTiles *rt, ImageTile *it,
 
                if (pr->image_width == 0 || pr->image_height == 0) return;
 
-               scale_x = (gdouble)pr->width / pr->image_width;
-               scale_y = (gdouble)pr->height / pr->image_height;
+               scale_x = rt->hidpi_scale * (gdouble)pr->width / pr->image_width;
+               scale_y = rt->hidpi_scale * (gdouble)pr->height / pr->image_height;
 
                pr_tile_coords_map_orientation(orientation, it->x, it->y,
                                            pr->width, pr->height,
@@ -1369,6 +1498,13 @@ static void rt_tile_render(RendererTiles *rt, ImageTile *it,
                                            &pb_x, &pb_y,
                                            &pb_w, &pb_h);
 
+               src_x *= rt->hidpi_scale;
+               src_y *= rt->hidpi_scale;
+               pb_x *= rt->hidpi_scale;
+               pb_y *= rt->hidpi_scale;
+               pb_w *= rt->hidpi_scale;
+               pb_h *= rt->hidpi_scale;
+
                switch (orientation)
                        {
                        gdouble tmp;
@@ -1389,25 +1525,26 @@ static void rt_tile_render(RendererTiles *rt, ImageTile *it,
                 * small sizes for anything but GDK_INTERP_NEAREST
                 */
                if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
+               if (pr->image_width > 32767) wide_image = TRUE;
 
-               rt_tile_get_region(has_alpha,
+               rt_tile_get_region(has_alpha, pr->ignore_alpha,
                                   pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
                                   (gdouble) 0.0 - src_x - GET_RIGHT_PIXBUF_OFFSET(rt) * scale_x,
                                   (gdouble) 0.0 - src_y,
                                   scale_x, scale_y,
                                   (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
-                                  it->x + pb_x, it->y + pb_y);
+                                  it->x + pb_x, it->y + pb_y, wide_image);
                if (rt->stereo_mode & PR_STEREO_ANAGLYPH &&
                    (pr->stereo_pixbuf_offset_right > 0 || pr->stereo_pixbuf_offset_left > 0))
                        {
                        GdkPixbuf *right_pb = rt_get_spare_tile(rt);
-                       rt_tile_get_region(has_alpha,
+                       rt_tile_get_region(has_alpha, pr->ignore_alpha,
                                           pr->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h,
                                           (gdouble) 0.0 - src_x - GET_LEFT_PIXBUF_OFFSET(rt) * scale_x,
                                           (gdouble) 0.0 - src_y,
                                           scale_x, scale_y,
                                           (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
-                                          it->x + pb_x, it->y + pb_y);
+                                          it->x + pb_x, it->y + pb_y, wide_image);
                        pr_create_anaglyph(rt->stereo_mode, it->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h);
                        /* do not care about freeing spare_tile, it will be reused */
                        }
@@ -1424,8 +1561,7 @@ static void rt_tile_render(RendererTiles *rt, ImageTile *it,
 
                cr = cairo_create(it->surface);
                cairo_rectangle (cr, x, y, w, h);
-               gdk_cairo_set_source_pixbuf(cr, it->pixbuf, 0, 0);
-               cairo_fill (cr);
+               rt_hidpi_aware_draw(rt, cr, it->pixbuf, 0, 0);
                cairo_destroy (cr);
                }
 }
@@ -1467,7 +1603,11 @@ static void rt_tile_expose(RendererTiles *rt, ImageTile *it,
        box = GTK_WIDGET(pr);
        window = gtk_widget_get_window(box);
 
+#if GTK_CHECK_VERSION(3,0,0)
+       cr = cairo_create(rt->surface);
+#else
        cr = gdk_cairo_create(window);
+#endif
        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);
        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);
        cairo_fill (cr);
@@ -1480,6 +1620,10 @@ static void rt_tile_expose(RendererTiles *rt, ImageTile *it,
                                w, h,
                                it);
                }
+
+#if GTK_CHECK_VERSION(3,0,0)
+       gtk_widget_queue_draw(GTK_WIDGET(rt->pr));
+#endif
 }
 
 
@@ -1878,8 +2022,6 @@ static void rt_scroll(void *renderer, gint x_off, gint y_off)
                {
                gint x1, y1;
                gint x2, y2;
-               GtkWidget *box;
-               GdkWindow *window;
                cairo_t *cr;
                cairo_surface_t *surface;
 
@@ -1905,11 +2047,19 @@ static void rt_scroll(void *renderer, gint x_off, gint y_off)
                        y2 = abs(y_off);
                        }
 
+#if GTK_CHECK_VERSION(3,0,0)
+               cr = cairo_create(rt->surface);
+               surface = rt->surface;
+#else
+               GtkWidget *box;
+               GdkWindow *window;
+
                box = GTK_WIDGET(pr);
                window = gtk_widget_get_window(box);
 
                cr = gdk_cairo_create(window);
                surface = cairo_get_target(cr);
+#endif
                /* clipping restricts the intermediate surface's size, so it's a good idea
                 * to use it. */
                cairo_rectangle(cr, x1 + pr->x_offset + rt->stereo_off_x, y1 + pr->y_offset + rt->stereo_off_y, w, h);
@@ -1936,7 +2086,7 @@ static void rt_scroll(void *renderer, gint x_off, gint y_off)
                        }
                if (h > 0)
                        {
-                       /* FIXME, to optimize this, remove overlap */
+                       /** @FIXME to optimize this, remove overlap */
                        rt_queue(rt,
                                    rt->x_scroll, y_off > 0 ? rt->y_scroll + (pr->vis_height - h) : rt->y_scroll,
                                    pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
@@ -2075,21 +2225,105 @@ static void renderer_free(void *renderer)
 }
 
 #if GTK_CHECK_VERSION(3,0,0)
+static gboolean rt_realize_cb(GtkWidget *widget, gpointer data)
+{
+       RendererTiles *rt = (RendererTiles *)data;
+       cairo_t *cr;
+
+       if (!rt->surface)
+               {
+               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));
+
+               cr = cairo_create(rt->surface);
+               cairo_set_source_rgb(cr, (gdouble)rt->pr->color.red / 65535, (gdouble)rt->pr->color.green / 65535, (gdouble)rt->pr->color.blue / 65535);
+               cairo_paint(cr);
+               cairo_destroy(cr);
+               }
+
+       return FALSE;
+}
+
+static gboolean rt_size_allocate_cb(GtkWidget *widget,  GdkRectangle *allocation, gpointer data)
+{
+       RendererTiles *rt = data;
+       cairo_t *cr;
+       cairo_surface_t *old_surface;
+
+       if (gtk_widget_get_realized(GTK_WIDGET(rt->pr)))
+               {
+               old_surface = rt->surface;
+               rt->surface = gdk_window_create_similar_surface(gtk_widget_get_window(widget), CAIRO_CONTENT_COLOR, allocation->width, allocation->height);
+
+               cr = cairo_create(rt->surface);
+
+               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);
+               cairo_paint(cr);
+               cairo_set_source_surface(cr, old_surface, 0, 0);
+               cairo_paint(cr);
+               cairo_destroy(cr);
+               cairo_surface_destroy(old_surface);
+
+               renderer_redraw(rt, allocation->x, allocation->y, allocation->width, allocation->height, FALSE, TILE_RENDER_ALL, FALSE, FALSE);
+       }
+
+       return FALSE;
+}
 
 static gboolean rt_draw_cb(GtkWidget *widget, cairo_t *cr, gpointer data)
 {
        RendererTiles *rt = (RendererTiles *)data;
-       if (gtk_widget_is_drawable(widget))
+       GList *work;
+       OverlayData *od;
+
+       if (rt->stereo_mode & (PR_STEREO_HORIZ | PR_STEREO_VERT))
                {
-               if (gtk_widget_get_has_window(widget))
+               cairo_push_group(cr);
+               cairo_set_source_rgb(cr, (double)rt->pr->color.red / 65535, (double)rt->pr->color.green / 65535, (double)rt->pr->color.blue / 65535);
+
+               if (rt->stereo_mode & PR_STEREO_HORIZ)
                        {
-                       GdkRectangle area;
-                       if (gdk_cairo_get_clip_rectangle(cr, &area))
-                               {
-                               renderer_redraw(rt, area.x, area.y, area.width, area.height,
-                                               FALSE, TILE_RENDER_ALL, FALSE, FALSE);
-                               }
+                       cairo_rectangle(cr, rt->stereo_off_x, 0, rt->pr->viewport_width, rt->pr->viewport_height);
                        }
+               else
+                       {
+                       cairo_rectangle(cr, 0, rt->stereo_off_y, rt->pr->viewport_width, rt->pr->viewport_height);
+                       }
+               cairo_clip(cr);
+               cairo_paint(cr);
+
+               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);
+               cairo_clip(cr);
+               cairo_set_source_surface(cr, rt->surface, 0, 0);
+               cairo_paint(cr);
+
+               cairo_pop_group_to_source(cr);
+               cairo_paint(cr);
+               }
+       else
+               {
+               cairo_set_source_surface(cr, rt->surface, 0, 0);
+               cairo_paint(cr);
+               }
+
+       work = rt->overlay_list;
+       while (work)
+               {
+               od = work->data;
+               gint px, py, pw, ph;
+               pw = gdk_pixbuf_get_width(od->pixbuf);
+               ph = gdk_pixbuf_get_height(od->pixbuf);
+               px = od->x;
+               py = od->y;
+
+               if (od->flags & OVL_RELATIVE)
+                       {
+                       if (px < 0) px = rt->pr->viewport_width - pw + px;
+                       if (py < 0) py = rt->pr->viewport_height - ph + py;
+                       }
+
+               gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px, py);
+               cairo_paint(cr);
+               work = work->next;
                }
 
        return FALSE;
@@ -2150,8 +2384,8 @@ RendererFuncs *renderer_tiles_new(PixbufRenderer *pr)
 
        rt->f.stereo_set = renderer_stereo_set;
 
-       rt->tile_width = PR_TILE_SIZE;
-       rt->tile_height = PR_TILE_SIZE;
+       rt->tile_width = options->image.tile_size;
+       rt->tile_height = options->image.tile_size;
 
        rt->tiles = NULL;
        rt->tile_cache_size = 0;
@@ -2164,16 +2398,25 @@ RendererFuncs *renderer_tiles_new(PixbufRenderer *pr)
        rt->stereo_off_x = 0;
        rt->stereo_off_y = 0;
 
+#if GTK_CHECK_VERSION(3, 10, 0)
+       rt->hidpi_scale = gtk_widget_get_scale_factor(GTK_WIDGET(rt->pr));
+#else
+       rt->hidpi_scale = 1;
+#endif
+
        g_signal_connect(G_OBJECT(pr), "hierarchy-changed",
                         G_CALLBACK(rt_hierarchy_changed_cb), rt);
 
 #if GTK_CHECK_VERSION(3,0,0)
        g_signal_connect(G_OBJECT(pr), "draw",
                         G_CALLBACK(rt_draw_cb), rt);
+       g_signal_connect(G_OBJECT(pr), "realize", G_CALLBACK(rt_realize_cb), rt);
+       g_signal_connect(G_OBJECT(pr), "size-allocate", G_CALLBACK(rt_size_allocate_cb), rt);
 #else
        g_signal_connect(G_OBJECT(pr), "expose_event",
                         G_CALLBACK(rt_expose_cb), rt);
 #endif
+
        return (RendererFuncs *) rt;
 }