2 * Copyright (C) 2006 John Ellis
3 * Copyright (C) 2008 - 2021 The Geeqie Team
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.
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.
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.
28 #include "pixbuf-renderer.h"
29 #include "renderer-tiles.h"
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
45 #include "pixbuf_util.h"
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;
61 typedef struct _ImageTile ImageTile;
62 typedef struct _QueueData QueueData;
66 cairo_surface_t *surface; /* off screen buffer */
67 GdkPixbuf *pixbuf; /* pixbuf area for zooming */
68 gint x; /* x offset into image */
69 gint y; /* y offset into image */
70 gint w; /* width that is visible (may be less if at edge of image) */
71 gint h; /* height '' */
75 /* render_todo: (explanation)
77 AREA render area of tile, usually only used when loading an image
78 note: will jump to an ALL if render_done is not ALL.
79 ALL render entire tile, if never done before w/ ALL, for expose events *only*
82 ImageRenderType render_todo; /* what to do (see above) */
83 ImageRenderType render_done; /* highest that has been done before on tile */
88 guint size; /* est. memory used by pixmap and pixbuf */
101 typedef struct _OverlayData OverlayData;
112 OverlayRendererFlags flags;
115 typedef struct _RendererTiles RendererTiles;
117 struct _RendererTiles
122 gint tile_cache_max; /* max MiB to use for offscreen buffer */
126 gint tile_cols; /* count of tile columns */
127 GList *tiles; /* list of buffer tiles */
128 gint tile_cache_size; /* allocated size of pixmaps/pixbufs */
129 GList *draw_queue; /* list of areas to redraw */
130 GList *draw_queue_2pass;/* list when 2 pass is enabled */
133 cairo_surface_t *overlay_buffer;
134 cairo_surface_t *surface;
136 guint draw_idle_id; /* event source id */
138 GdkPixbuf *spare_tile;
144 gint x_scroll; /* allow local adjustment and mirroring */
152 static void rt_border_draw(RendererTiles *rt, gint x, gint y, gint w, gint h);
153 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h, ImageTile *it);
156 static void rt_tile_free_all(RendererTiles *rt);
157 static void rt_tile_invalidate_region(RendererTiles *rt, gint x, gint y, gint w, gint h);
158 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it);
159 static void rt_queue_clear(RendererTiles *rt);
160 static void rt_queue_merge(QueueData *parent, QueueData *qd);
161 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
162 gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing);
164 static void rt_hierarchy_changed_cb(GtkWidget *widget, GtkWidget *previous_toplevel, gpointer data);
165 static gint rt_queue_draw_idle_cb(gpointer data);
167 #define GET_RIGHT_PIXBUF_OFFSET(rt) \
168 (( (rt->stereo_mode & PR_STEREO_RIGHT) && !(rt->stereo_mode & PR_STEREO_SWAP)) || \
169 (!(rt->stereo_mode & PR_STEREO_RIGHT) && (rt->stereo_mode & PR_STEREO_SWAP)) ? \
170 rt->pr->stereo_pixbuf_offset_right : rt->pr->stereo_pixbuf_offset_left )
172 #define GET_LEFT_PIXBUF_OFFSET(rt) \
173 ((!(rt->stereo_mode & PR_STEREO_RIGHT) && !(rt->stereo_mode & PR_STEREO_SWAP)) || \
174 ( (rt->stereo_mode & PR_STEREO_RIGHT) && (rt->stereo_mode & PR_STEREO_SWAP)) ? \
175 rt->pr->stereo_pixbuf_offset_right : rt->pr->stereo_pixbuf_offset_left )
178 static void rt_sync_scroll(RendererTiles *rt)
180 PixbufRenderer *pr = rt->pr;
182 rt->x_scroll = (rt->stereo_mode & PR_STEREO_MIRROR) ?
183 pr->width - pr->vis_width - pr->x_scroll
186 rt->y_scroll = (rt->stereo_mode & PR_STEREO_FLIP) ?
187 pr->height - pr->vis_height - pr->y_scroll
192 *-------------------------------------------------------------------
194 *-------------------------------------------------------------------
197 static void rt_border_draw(RendererTiles *rt, gint x, gint y, gint w, gint h)
199 PixbufRenderer *pr = rt->pr;
205 box = GTK_WIDGET(pr);
206 window = gtk_widget_get_window(box);
210 #if GTK_CHECK_VERSION(3,0,0)
211 cr = cairo_create(rt->surface);
213 cr = gdk_cairo_create(window);
216 if (!pr->pixbuf && !pr->source_tiles_enabled)
218 if (pr_clip_region(x, y, w, h,
220 pr->viewport_width, pr->viewport_height,
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);
226 rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
232 if (pr->vis_width < pr->viewport_width)
234 if (pr->x_offset > 0 &&
235 pr_clip_region(x, y, w, h,
237 pr->x_offset, pr->viewport_height,
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);
243 rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
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,
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);
254 rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
257 if (pr->vis_height < pr->viewport_height)
259 if (pr->y_offset > 0 &&
260 pr_clip_region(x, y, w, h,
262 pr->vis_width, pr->y_offset,
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);
268 rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
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,
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);
279 rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
285 static void rt_border_clear(RendererTiles *rt)
287 PixbufRenderer *pr = rt->pr;
288 rt_border_draw(rt, 0, 0, pr->viewport_width, pr->viewport_height);
293 *-------------------------------------------------------------------
295 *-------------------------------------------------------------------
298 static ImageTile *rt_tile_new(gint x, gint y, gint width, gint height)
302 it = g_new0(ImageTile, 1);
309 it->render_done = TILE_RENDER_NONE;
314 static void rt_tile_free(ImageTile *it)
318 if (it->pixbuf) g_object_unref(it->pixbuf);
319 if (it->surface) cairo_surface_destroy(it->surface);
324 static void rt_tile_free_all(RendererTiles *rt)
339 g_list_free(rt->tiles);
341 rt->tile_cache_size = 0;
344 static ImageTile *rt_tile_add(RendererTiles *rt, gint x, gint y)
346 PixbufRenderer *pr = rt->pr;
349 it = rt_tile_new(x, y, rt->tile_width, rt->tile_height);
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;
354 rt->tiles = g_list_prepend(rt->tiles, it);
355 rt->tile_cache_size += it->size;
360 static void rt_tile_remove(RendererTiles *rt, ImageTile *it)
364 QueueData *qd = it->qd;
367 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
373 QueueData *qd = it->qd2;
376 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
380 rt->tiles = g_list_remove(rt->tiles, it);
381 rt->tile_cache_size -= it->size;
386 static void rt_tile_free_space(RendererTiles *rt, guint space, ImageTile *it)
388 PixbufRenderer *pr = rt->pr;
392 work = g_list_last(rt->tiles);
394 if (pr->source_tiles_enabled && pr->scale < 1.0)
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));
404 tile_max = rt->tile_cache_max * 1048576;
407 while (work && rt->tile_cache_size + space > tile_max)
414 ((!needle->qd && !needle->qd2) || !rt_tile_is_visible(rt, needle))) rt_tile_remove(rt, needle);
418 static void rt_tile_invalidate_all(RendererTiles *rt)
420 PixbufRenderer *pr = rt->pr;
431 it->render_done = TILE_RENDER_NONE;
432 it->render_todo = TILE_RENDER_ALL;
435 it->w = MIN(rt->tile_width, pr->width - it->x);
436 it->h = MIN(rt->tile_height, pr->height - it->y);
440 static void rt_tile_invalidate_region(RendererTiles *rt, gint x, gint y, gint w, gint h)
446 x1 = ROUND_DOWN(x, rt->tile_width);
447 x2 = ROUND_UP(x + w, rt->tile_width);
449 y1 = ROUND_DOWN(y, rt->tile_height);
450 y2 = ROUND_UP(y + h, rt->tile_height);
460 if (it->x < x2 && it->x + it->w > x1 &&
461 it->y < y2 && it->y + it->h > y1)
463 it->render_done = TILE_RENDER_NONE;
464 it->render_todo = TILE_RENDER_ALL;
469 static ImageTile *rt_tile_get(RendererTiles *rt, gint x, gint y, gboolean only_existing)
479 if (it->x == x && it->y == y)
481 rt->tiles = g_list_delete_link(rt->tiles, work);
482 rt->tiles = g_list_prepend(rt->tiles, it);
489 if (only_existing) return NULL;
491 return rt_tile_add(rt, x, y);
494 static gint pixmap_calc_size(cairo_surface_t *surface)
498 // d = gdk_drawable_get_depth(pixmap);
499 // gdk_drawable_get_size(pixmap, &w, &h);
500 return options->image.tile_size * options->image.tile_size * 4 / 8;
503 static void rt_hidpi_aware_draw(
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);
515 cairo_surface_destroy(surface);
517 gdk_cairo_set_source_pixbuf(cr, pixbuf, x, y);
522 static void rt_tile_prepare(RendererTiles *rt, ImageTile *it)
524 PixbufRenderer *pr = rt->pr;
527 cairo_surface_t *surface;
530 surface = gdk_window_create_similar_surface(gtk_widget_get_window((GtkWidget *)pr),
532 rt->tile_width, rt->tile_height);
534 size = pixmap_calc_size(surface) * rt->hidpi_scale * rt->hidpi_scale;
535 rt_tile_free_space(rt, size, it);
537 it->surface = surface;
539 rt->tile_cache_size += size;
546 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, rt->hidpi_scale * rt->tile_width, rt->hidpi_scale * rt->tile_height);
548 size = gdk_pixbuf_get_rowstride(pixbuf) * rt->tile_height * rt->hidpi_scale;
549 rt_tile_free_space(rt, size, it);
553 rt->tile_cache_size += size;
558 *-------------------------------------------------------------------
560 *-------------------------------------------------------------------
563 static void rt_overlay_get_position(RendererTiles *rt, OverlayData *od,
564 gint *x, gint *y, gint *w, gint *h)
566 PixbufRenderer *pr = rt->pr;
569 pw = gdk_pixbuf_get_width(od->pixbuf);
570 ph = gdk_pixbuf_get_height(od->pixbuf);
574 if (od->flags & OVL_RELATIVE)
576 if (px < 0) px = pr->viewport_width - pw + px;
577 if (py < 0) py = pr->viewport_height - ph + py;
586 static void rt_overlay_init_window(RendererTiles *rt, OverlayData *od)
588 PixbufRenderer *pr = rt->pr;
590 GdkWindowAttr attributes;
591 gint attributes_mask;
593 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
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;
602 od->window = gdk_window_new(gtk_widget_get_window(GTK_WIDGET(pr)), &attributes, attributes_mask);
603 gdk_window_set_user_data(od->window, pr);
604 gdk_window_move(od->window, px + rt->stereo_off_x, py + rt->stereo_off_y);
605 #if !GTK_CHECK_VERSION(3,0,0)
606 gdk_window_show(od->window);
610 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h,
613 PixbufRenderer *pr = rt->pr;
616 work = rt->overlay_list;
626 if (!od->window) rt_overlay_init_window(rt, od);
628 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
629 if (pr_clip_region(x, y, w, h, px, py, pw, ph, &rx, &ry, &rw, &rh))
631 if (!rt->overlay_buffer)
633 rt->overlay_buffer = gdk_window_create_similar_surface(gtk_widget_get_window((GtkWidget *)pr),
635 rt->tile_width, rt->tile_height);
642 cr = cairo_create(rt->overlay_buffer);
643 cairo_set_source_surface(cr, it->surface, (pr->x_offset + (it->x - rt->x_scroll)) - rx, (pr->y_offset + (it->y - rt->y_scroll)) - ry);
644 cairo_rectangle(cr, 0, 0, rw, rh);
645 cairo_fill_preserve(cr);
647 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - rx, py - ry);
651 cr = gdk_cairo_create(od->window);
652 cairo_set_source_surface(cr, rt->overlay_buffer, rx - px, ry - py);
653 cairo_rectangle (cr, rx - px, ry - py, rw, rh);
659 /* no ImageTile means region may be larger than our scratch buffer */
662 for (sx = rx; sx < rx + rw; sx += rt->tile_width)
663 for (sy = ry; sy < ry + rh; sy += rt->tile_height)
668 sw = MIN(rx + rw - sx, rt->tile_width);
669 sh = MIN(ry + rh - sy, rt->tile_height);
671 cr = cairo_create(rt->overlay_buffer);
672 cairo_set_source_rgb(cr, 0, 0, 0);
673 cairo_rectangle(cr, 0, 0, sw, sh);
674 cairo_fill_preserve(cr);
676 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - sx, py - sy);
680 cr = gdk_cairo_create(od->window);
681 cairo_set_source_surface(cr, rt->overlay_buffer, sx - px, sy - py);
682 cairo_rectangle (cr, sx - px, sy - py, sw, sh);
691 static void rt_overlay_queue_draw(RendererTiles *rt, OverlayData *od, gint x1, gint y1, gint x2, gint y2)
693 PixbufRenderer *pr = rt->pr;
696 rt_overlay_get_position(rt, od, &x, &y, &w, &h);
704 rt_queue(rt, rt->x_scroll - pr->x_offset + x,
705 rt->y_scroll - pr->y_offset + y,
707 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
709 rt_border_draw(rt, x, y, w, h);
712 static void rt_overlay_queue_all(RendererTiles *rt, gint x1, gint y1, gint x2, gint y2)
716 work = rt->overlay_list;
719 OverlayData *od = work->data;
722 rt_overlay_queue_draw(rt, od, x1, y1, x2, y2);
726 static void rt_overlay_update_sizes(RendererTiles *rt)
730 work = rt->overlay_list;
733 OverlayData *od = work->data;
736 if (!od->window) rt_overlay_init_window(rt, od);
738 if (od->flags & OVL_RELATIVE)
742 rt_overlay_get_position(rt, od, &x, &y, &w, &h);
743 gdk_window_move_resize(od->window, x + rt->stereo_off_x, y + rt->stereo_off_y, w, h);
748 static OverlayData *rt_overlay_find(RendererTiles *rt, gint id)
752 work = rt->overlay_list;
755 OverlayData *od = work->data;
758 if (od->id == id) return od;
765 gint renderer_tiles_overlay_add(void *renderer, GdkPixbuf *pixbuf, gint x, gint y,
766 OverlayRendererFlags flags)
768 RendererTiles *rt = (RendererTiles *) renderer;
769 PixbufRenderer *pr = rt->pr;
773 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
774 g_return_val_if_fail(pixbuf != NULL, -1);
777 while (rt_overlay_find(rt, id)) id++;
779 od = g_new0(OverlayData, 1);
782 g_object_ref(G_OBJECT(od->pixbuf));
787 rt_overlay_init_window(rt, od);
789 rt->overlay_list = g_list_append(rt->overlay_list, od);
791 #if GTK_CHECK_VERSION(3,0,0)
792 gtk_widget_queue_draw(GTK_WIDGET(rt->pr));
794 rt_overlay_queue_draw(rt, od, 0, 0, 0, 0);
800 static void rt_overlay_free(RendererTiles *rt, OverlayData *od)
802 rt->overlay_list = g_list_remove(rt->overlay_list, od);
804 if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
805 if (od->window) gdk_window_destroy(od->window);
808 if (!rt->overlay_list && rt->overlay_buffer)
810 cairo_surface_destroy(rt->overlay_buffer);
811 rt->overlay_buffer = NULL;
815 static void rt_overlay_list_clear(RendererTiles *rt)
817 while (rt->overlay_list)
821 od = rt->overlay_list->data;
822 rt_overlay_free(rt, od);
826 static void rt_overlay_list_reset_window(RendererTiles *rt)
830 if (rt->overlay_buffer) cairo_surface_destroy(rt->overlay_buffer);
831 rt->overlay_buffer = NULL;
833 work = rt->overlay_list;
836 OverlayData *od = work->data;
838 if (od->window) gdk_window_destroy(od->window);
843 #if GTK_CHECK_VERSION(3,0,0)
844 void renderer_tiles_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint x, gint y)
846 RendererTiles *rc = (RendererTiles *)renderer;
847 PixbufRenderer *pr = rc->pr;
850 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
852 od = rt_overlay_find(rc, id);
857 g_object_ref(G_OBJECT(pixbuf));
858 g_object_unref(G_OBJECT(od->pixbuf));
863 rt_overlay_free(rc, od);
867 void renderer_tiles_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint x, gint y)
869 RendererTiles *rt = (RendererTiles *) renderer;
870 PixbufRenderer *pr = rt->pr;
873 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
875 od = rt_overlay_find(rt, id);
882 g_object_ref(G_OBJECT(pixbuf));
883 g_object_unref(G_OBJECT(od->pixbuf));
889 if (!od->window) rt_overlay_init_window(rt, od);
891 rt_overlay_queue_draw(rt, od, 0, 0, 0, 0);
892 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
893 gdk_window_move_resize(od->window, px + rt->stereo_off_x, py + rt->stereo_off_y, pw, ph);
897 rt_overlay_queue_draw(rt, od, 0, 0, 0, 0);
898 rt_overlay_free(rt, od);
903 gboolean renderer_tiles_overlay_get(void *renderer, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
905 RendererTiles *rt = (RendererTiles *) renderer;
906 PixbufRenderer *pr = rt->pr;
909 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
911 od = rt_overlay_find(rt, id);
912 if (!od) return FALSE;
914 if (pixbuf) *pixbuf = od->pixbuf;
921 static void rt_hierarchy_changed_cb(GtkWidget *widget, GtkWidget *previous_toplevel, gpointer data)
923 RendererTiles *rt = data;
924 rt_overlay_list_reset_window(rt);
928 *-------------------------------------------------------------------
930 *-------------------------------------------------------------------
933 static GdkPixbuf *rt_get_spare_tile(RendererTiles *rt)
935 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);
936 return rt->spare_tile;
939 #define COLOR_BYTES 3 /* rgb */
941 static void rt_tile_rotate_90_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
943 GdkPixbuf *src = *tile;
946 guchar *s_pix, *d_pix;
948 guchar *ip, *spi, *dpi;
950 gint tw = rt->tile_width * rt->hidpi_scale;
952 srs = gdk_pixbuf_get_rowstride(src);
953 s_pix = gdk_pixbuf_get_pixels(src);
954 spi = s_pix + (x * COLOR_BYTES);
956 dest = rt_get_spare_tile(rt);
957 drs = gdk_pixbuf_get_rowstride(dest);
958 d_pix = gdk_pixbuf_get_pixels(dest);
959 dpi = d_pix + (tw - 1) * COLOR_BYTES;
961 for (i = y; i < y + h; i++)
963 sp = spi + (i * srs);
964 ip = dpi - (i * COLOR_BYTES);
965 for (j = x; j < x + w; j++)
968 memcpy(dp, sp, COLOR_BYTES);
973 rt->spare_tile = src;
977 static void rt_tile_rotate_90_counter_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
979 GdkPixbuf *src = *tile;
982 guchar *s_pix, *d_pix;
984 guchar *ip, *spi, *dpi;
986 gint th = rt->tile_height * rt->hidpi_scale;
988 srs = gdk_pixbuf_get_rowstride(src);
989 s_pix = gdk_pixbuf_get_pixels(src);
990 spi = s_pix + (x * COLOR_BYTES);
992 dest = rt_get_spare_tile(rt);
993 drs = gdk_pixbuf_get_rowstride(dest);
994 d_pix = gdk_pixbuf_get_pixels(dest);
995 dpi = d_pix + (th - 1) * drs;
997 for (i = y; i < y + h; i++)
999 sp = spi + (i * srs);
1000 ip = dpi + (i * COLOR_BYTES);
1001 for (j = x; j < x + w; j++)
1003 dp = ip - (j * drs);
1004 memcpy(dp, sp, COLOR_BYTES);
1009 rt->spare_tile = src;
1013 static void rt_tile_mirror_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1015 GdkPixbuf *src = *tile;
1018 guchar *s_pix, *d_pix;
1023 gint tw = rt->tile_width * rt->hidpi_scale;
1025 srs = gdk_pixbuf_get_rowstride(src);
1026 s_pix = gdk_pixbuf_get_pixels(src);
1027 spi = s_pix + (x * COLOR_BYTES);
1029 dest = rt_get_spare_tile(rt);
1030 drs = gdk_pixbuf_get_rowstride(dest);
1031 d_pix = gdk_pixbuf_get_pixels(dest);
1032 dpi = d_pix + (tw - x - 1) * COLOR_BYTES;
1034 for (i = y; i < y + h; i++)
1036 sp = spi + (i * srs);
1037 dp = dpi + (i * drs);
1038 for (j = 0; j < w; j++)
1040 memcpy(dp, sp, COLOR_BYTES);
1046 rt->spare_tile = src;
1050 static void rt_tile_mirror_and_flip(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1052 GdkPixbuf *src = *tile;
1055 guchar *s_pix, *d_pix;
1059 gint tw = rt->tile_width * rt->hidpi_scale;
1060 gint th = rt->tile_height * rt->hidpi_scale;
1062 srs = gdk_pixbuf_get_rowstride(src);
1063 s_pix = gdk_pixbuf_get_pixels(src);
1065 dest = rt_get_spare_tile(rt);
1066 drs = gdk_pixbuf_get_rowstride(dest);
1067 d_pix = gdk_pixbuf_get_pixels(dest);
1068 dpi = d_pix + (th - 1) * drs + (tw - 1) * COLOR_BYTES;
1070 for (i = y; i < y + h; i++)
1072 sp = s_pix + (i * srs) + (x * COLOR_BYTES);
1073 dp = dpi - (i * drs) - (x * COLOR_BYTES);
1074 for (j = 0; j < w; j++)
1076 memcpy(dp, sp, COLOR_BYTES);
1082 rt->spare_tile = src;
1086 static void rt_tile_flip_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1088 GdkPixbuf *src = *tile;
1091 guchar *s_pix, *d_pix;
1095 gint th = rt->tile_height * rt->hidpi_scale;
1097 srs = gdk_pixbuf_get_rowstride(src);
1098 s_pix = gdk_pixbuf_get_pixels(src);
1099 spi = s_pix + (x * COLOR_BYTES);
1101 dest = rt_get_spare_tile(rt);
1102 drs = gdk_pixbuf_get_rowstride(dest);
1103 d_pix = gdk_pixbuf_get_pixels(dest);
1104 dpi = d_pix + (th - 1) * drs + (x * COLOR_BYTES);
1106 for (i = y; i < y + h; i++)
1108 sp = spi + (i * srs);
1109 dp = dpi - (i * drs);
1110 memcpy(dp, sp, w * COLOR_BYTES);
1113 rt->spare_tile = src;
1117 static void rt_tile_apply_orientation(RendererTiles *rt, gint orientation, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h)
1119 switch (orientation)
1121 case EXIF_ORIENTATION_TOP_LEFT:
1122 /* normal -- nothing to do */
1124 case EXIF_ORIENTATION_TOP_RIGHT:
1127 rt_tile_mirror_only(rt, pixbuf, x, y, w, h);
1130 case EXIF_ORIENTATION_BOTTOM_RIGHT:
1133 rt_tile_mirror_and_flip(rt, pixbuf, x, y, w, h);
1136 case EXIF_ORIENTATION_BOTTOM_LEFT:
1139 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1142 case EXIF_ORIENTATION_LEFT_TOP:
1144 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1145 rt_tile_rotate_90_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1148 case EXIF_ORIENTATION_RIGHT_TOP:
1149 /* rotated -90 (270) */
1151 rt_tile_rotate_90_clockwise(rt, pixbuf, x, y, w, h);
1154 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1156 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1157 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1160 case EXIF_ORIENTATION_LEFT_BOTTOM:
1163 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, y, w, h);
1167 /* The other values are out of range */
1172 static gboolean rt_source_tile_render(RendererTiles *rt, ImageTile *it,
1173 gint x, gint y, gint w, gint h,
1174 gboolean new_data, gboolean fast)
1176 PixbufRenderer *pr = rt->pr;
1179 gboolean draw = FALSE;
1181 if (pr->zoom == 1.0 || pr->scale == 1.0)
1183 list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE);
1188 gint rx, ry, rw, rh;
1193 if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1194 it->x + x, it->y + y, w, h,
1195 &rx, &ry, &rw, &rh))
1198 cr = cairo_create(it->surface);
1199 cairo_rectangle (cr, rx - it->x, ry - it->y, rw, rh);
1203 cairo_set_source_rgb(cr, 0, 0, 0);
1206 else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
1208 rt_hidpi_aware_draw(rt, cr, st->pixbuf, -it->x + st->x, -it->y + st->y);
1216 gdouble scale_x, scale_y;
1217 gint sx, sy, sw, sh;
1219 if (pr->image_width == 0 || pr->image_height == 0) return FALSE;
1220 scale_x = (gdouble)pr->width / pr->image_width;
1221 scale_y = (gdouble)pr->height / pr->image_height;
1223 sx = (gdouble)(it->x + x) / scale_x;
1224 sy = (gdouble)(it->y + y) / scale_y;
1225 sw = (gdouble)w / scale_x;
1226 sh = (gdouble)h / scale_y;
1228 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1231 /* draws red over draw region, to check for leaks (regions not filled) */
1232 pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255);
1235 list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE);
1240 gint rx, ry, rw, rh;
1241 gint stx, sty, stw, sth;
1246 stx = floor((gdouble)st->x * scale_x);
1247 sty = floor((gdouble)st->y * scale_y);
1248 stw = ceil((gdouble)(st->x + pr->source_tile_width) * scale_x) - stx;
1249 sth = ceil((gdouble)(st->y + pr->source_tile_height) * scale_y) - sty;
1251 if (pr_clip_region(stx, sty, stw, sth,
1252 it->x + x, it->y + y, w, h,
1253 &rx, &ry, &rw, &rh))
1259 cr = cairo_create(it->surface);
1260 cairo_rectangle (cr, rx - st->x, ry - st->y, rw, rh);
1261 cairo_set_source_rgb(cr, 0, 0, 0);
1270 /* may need to use unfloored stx,sty values here */
1271 offset_x = (gdouble)(stx - it->x);
1272 offset_y = (gdouble)(sty - it->y);
1274 gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh,
1275 (gdouble) 0.0 + offset_x,
1276 (gdouble) 0.0 + offset_y,
1278 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1293 * @param ignore_alpha
1304 * @param interp_type
1307 * @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
1311 static void rt_tile_get_region(gboolean has_alpha, gboolean ignore_alpha,
1312 const GdkPixbuf *src, GdkPixbuf *dest,
1313 int pb_x, int pb_y, int pb_w, int pb_h,
1314 double offset_x, double offset_y, double scale_x, double scale_y,
1315 GdkInterpType interp_type,
1316 int check_x, int check_y, gboolean wide_image)
1318 GdkPixbuf* tmppixbuf;
1329 if (scale_x == 1.0 && scale_y == 1.0)
1333 srs = gdk_pixbuf_get_rowstride(src);
1334 drs = gdk_pixbuf_get_rowstride(dest);
1335 psrc = gdk_pixbuf_get_pixels(src);
1336 pdst = gdk_pixbuf_get_pixels(dest);
1337 for (y = 0; y < pb_h; y++)
1339 for (x = 0; x < pb_w; x++)
1341 for (c = 0; c < 3; c++)
1343 pdst[(y * drs) + x*3 + c] = psrc[(-(int)offset_y + pb_y + y) * srs + (-(int)offset_x + pb_x+x)*3 + c];
1350 gdk_pixbuf_copy_area(src,
1351 -offset_x + pb_x, -offset_y + pb_y,
1359 gdk_pixbuf_scale(src, dest,
1360 pb_x, pb_y, pb_w, pb_h,
1364 (wide_image && interp_type == GDK_INTERP_NEAREST) ? GDK_INTERP_TILES : interp_type);
1371 tmppixbuf = gdk_pixbuf_add_alpha(src, FALSE, 0, 0, 0);
1373 pixbuf_ignore_alpha_rect(tmppixbuf, 0, 0, gdk_pixbuf_get_width(src), gdk_pixbuf_get_height(src));
1375 gdk_pixbuf_composite_color(tmppixbuf, dest,
1376 pb_x, pb_y, pb_w, pb_h,
1380 (scale_x == 1.0 && scale_y == 1.0) ? GDK_INTERP_NEAREST : interp_type,
1381 255, check_x, check_y,
1382 PR_ALPHA_CHECK_SIZE,
1383 ((options->image.alpha_color_1.red << 8 & 0x00FF0000) +
1384 (options->image.alpha_color_1.green & 0x00FF00) +
1385 (options->image.alpha_color_1.blue >> 8 & 0x00FF)),
1386 ((options->image.alpha_color_2.red << 8 & 0x00FF0000) +
1387 (options->image.alpha_color_2.green & 0x00FF00) +
1388 (options->image.alpha_color_2.blue >> 8 & 0x00FF)));
1389 g_object_unref(tmppixbuf);
1393 gdk_pixbuf_composite_color(src, dest,
1394 pb_x, pb_y, pb_w, pb_h,
1398 (scale_x == 1.0 && scale_y == 1.0) ? GDK_INTERP_NEAREST : interp_type,
1399 255, check_x, check_y,
1400 PR_ALPHA_CHECK_SIZE,
1401 ((options->image.alpha_color_1.red << 8 & 0x00FF0000) +
1402 (options->image.alpha_color_1.green & 0x00FF00) +
1403 (options->image.alpha_color_1.blue >> 8 & 0x00FF)),
1404 ((options->image.alpha_color_2.red << 8 & 0x00FF0000) +
1405 (options->image.alpha_color_2.green & 0x00FF00) +
1406 (options->image.alpha_color_2.blue >> 8 & 0x00FF)));
1412 static gint rt_get_orientation(RendererTiles *rt)
1414 PixbufRenderer *pr = rt->pr;
1416 gint orientation = pr->orientation;
1417 static const gint mirror[] = {1, 2, 1, 4, 3, 6, 5, 8, 7};
1418 static const gint flip[] = {1, 4, 3, 2, 1, 8, 7, 6, 5};
1420 if (rt->stereo_mode & PR_STEREO_MIRROR) orientation = mirror[orientation];
1421 if (rt->stereo_mode & PR_STEREO_FLIP) orientation = flip[orientation];
1426 static void rt_tile_render(RendererTiles *rt, ImageTile *it,
1427 gint x, gint y, gint w, gint h,
1428 gboolean new_data, gboolean fast)
1430 PixbufRenderer *pr = rt->pr;
1432 gboolean draw = FALSE;
1433 gint orientation = rt_get_orientation(rt);
1434 gboolean wide_image = FALSE;
1436 if (it->render_todo == TILE_RENDER_NONE && it->surface && !new_data) return;
1438 if (it->render_done != TILE_RENDER_ALL)
1444 if (!fast) it->render_done = TILE_RENDER_ALL;
1446 else if (it->render_todo != TILE_RENDER_AREA)
1448 if (!fast) it->render_todo = TILE_RENDER_NONE;
1452 if (!fast) it->render_todo = TILE_RENDER_NONE;
1454 if (new_data) it->blank = FALSE;
1456 rt_tile_prepare(rt, it);
1457 has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
1459 /** @FIXME checker colors for alpha should be configurable,
1460 * also should be drawn for blank = TRUE
1465 /* no data, do fast rect fill */
1467 cr = cairo_create(it->surface);
1468 cairo_rectangle (cr, 0, 0, it->w, it->h);
1469 cairo_set_source_rgb(cr, 0, 0, 0);
1473 else if (pr->source_tiles_enabled)
1475 draw = rt_source_tile_render(rt, it, x, y, w, h, new_data, fast);
1479 gdouble scale_x, scale_y;
1480 gdouble src_x, src_y;
1484 if (pr->image_width == 0 || pr->image_height == 0) return;
1486 scale_x = rt->hidpi_scale * (gdouble)pr->width / pr->image_width;
1487 scale_y = rt->hidpi_scale * (gdouble)pr->height / pr->image_height;
1489 pr_tile_coords_map_orientation(orientation, it->x, it->y,
1490 pr->width, pr->height,
1491 rt->tile_width, rt->tile_height,
1493 pr_tile_region_map_orientation(orientation, x, y,
1494 rt->tile_width, rt->tile_height,
1499 src_x *= rt->hidpi_scale;
1500 src_y *= rt->hidpi_scale;
1501 pb_x *= rt->hidpi_scale;
1502 pb_y *= rt->hidpi_scale;
1503 pb_w *= rt->hidpi_scale;
1504 pb_h *= rt->hidpi_scale;
1506 switch (orientation)
1509 case EXIF_ORIENTATION_LEFT_TOP:
1510 case EXIF_ORIENTATION_RIGHT_TOP:
1511 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1512 case EXIF_ORIENTATION_LEFT_BOTTOM:
1522 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
1523 * small sizes for anything but GDK_INTERP_NEAREST
1525 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1526 if (pr->image_width > 32767) wide_image = TRUE;
1528 rt_tile_get_region(has_alpha, pr->ignore_alpha,
1529 pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
1530 (gdouble) 0.0 - src_x - GET_RIGHT_PIXBUF_OFFSET(rt) * scale_x,
1531 (gdouble) 0.0 - src_y,
1533 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1534 it->x + pb_x, it->y + pb_y, wide_image);
1535 if (rt->stereo_mode & PR_STEREO_ANAGLYPH &&
1536 (pr->stereo_pixbuf_offset_right > 0 || pr->stereo_pixbuf_offset_left > 0))
1538 GdkPixbuf *right_pb = rt_get_spare_tile(rt);
1539 rt_tile_get_region(has_alpha, pr->ignore_alpha,
1540 pr->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h,
1541 (gdouble) 0.0 - src_x - GET_LEFT_PIXBUF_OFFSET(rt) * scale_x,
1542 (gdouble) 0.0 - src_y,
1544 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1545 it->x + pb_x, it->y + pb_y, wide_image);
1546 pr_create_anaglyph(rt->stereo_mode, it->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h);
1547 /* do not care about freeing spare_tile, it will be reused */
1549 rt_tile_apply_orientation(rt, orientation, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
1553 if (draw && it->pixbuf && !it->blank)
1557 if (pr->func_post_process && !(pr->post_process_slow && fast))
1558 pr->func_post_process(pr, &it->pixbuf, x, y, w, h, pr->post_process_user_data);
1560 cr = cairo_create(it->surface);
1561 cairo_rectangle (cr, x, y, w, h);
1562 rt_hidpi_aware_draw(rt, cr, it->pixbuf, 0, 0);
1568 static void rt_tile_expose(RendererTiles *rt, ImageTile *it,
1569 gint x, gint y, gint w, gint h,
1570 gboolean new_data, gboolean fast)
1572 PixbufRenderer *pr = rt->pr;
1577 /* clamp to visible */
1578 if (it->x + x < rt->x_scroll)
1580 w -= rt->x_scroll - it->x - x;
1581 x = rt->x_scroll - it->x;
1583 if (it->x + x + w > rt->x_scroll + pr->vis_width)
1585 w = rt->x_scroll + pr->vis_width - it->x - x;
1588 if (it->y + y < rt->y_scroll)
1590 h -= rt->y_scroll - it->y - y;
1591 y = rt->y_scroll - it->y;
1593 if (it->y + y + h > rt->y_scroll + pr->vis_height)
1595 h = rt->y_scroll + pr->vis_height - it->y - y;
1599 rt_tile_render(rt, it, x, y, w, h, new_data, fast);
1601 box = GTK_WIDGET(pr);
1602 window = gtk_widget_get_window(box);
1604 #if GTK_CHECK_VERSION(3,0,0)
1605 cr = cairo_create(rt->surface);
1607 cr = gdk_cairo_create(window);
1609 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);
1610 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);
1614 if (rt->overlay_list)
1616 rt_overlay_draw(rt, pr->x_offset + (it->x - rt->x_scroll) + x,
1617 pr->y_offset + (it->y - rt->y_scroll) + y,
1622 #if GTK_CHECK_VERSION(3,0,0)
1623 gtk_widget_queue_draw(GTK_WIDGET(rt->pr));
1628 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it)
1630 PixbufRenderer *pr = rt->pr;
1631 return (it->x + it->w >= rt->x_scroll && it->x < rt->x_scroll + pr->vis_width &&
1632 it->y + it->h >= rt->y_scroll && it->y < rt->y_scroll + pr->vis_height);
1636 *-------------------------------------------------------------------
1638 *-------------------------------------------------------------------
1641 static gint rt_get_queued_area(GList *work)
1647 QueueData *qd = work->data;
1648 area += qd->w * qd->h;
1655 static gboolean rt_queue_schedule_next_draw(RendererTiles *rt, gboolean force_set)
1657 PixbufRenderer *pr = rt->pr;
1659 gint visible_area = pr->vis_width * pr->vis_height;
1664 DEBUG_2("redraw priority: 2pass");
1665 rt->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, rt_queue_draw_idle_cb, rt, NULL);
1669 if (visible_area == 0)
1676 percent = 100.0 * rt_get_queued_area(rt->draw_queue) / visible_area;
1681 /* we have enough data for starting intensive redrawing */
1682 DEBUG_2("redraw priority: high %.2f %%", percent);
1683 rt->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW, rt_queue_draw_idle_cb, rt, NULL);
1687 if (percent < 1.0 || force_set)
1689 /* queue is (almost) empty, wait 50 ms*/
1690 DEBUG_2("redraw priority: wait %.2f %%", percent);
1691 rt->draw_idle_id = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, 50, rt_queue_draw_idle_cb, rt, NULL);
1695 /* keep the same priority as before */
1696 DEBUG_2("redraw priority: no change %.2f %%", percent);
1701 static gboolean rt_queue_draw_idle_cb(gpointer data)
1703 RendererTiles *rt = data;
1704 PixbufRenderer *pr = rt->pr;
1709 if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
1710 (!rt->draw_queue && !rt->draw_queue_2pass) ||
1713 pr_render_complete_signal(pr);
1715 rt->draw_idle_id = 0;
1721 qd = rt->draw_queue->data;
1722 fast = (pr->zoom_2pass && ((pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0) || pr->post_process_slow));
1728 /* still loading, wait till done (also drops the higher priority) */
1730 return rt_queue_schedule_next_draw(rt, FALSE);
1733 qd = rt->draw_queue_2pass->data;
1737 if (gtk_widget_get_realized(GTK_WIDGET(pr)))
1739 if (rt_tile_is_visible(rt, qd->it))
1741 rt_tile_expose(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1743 else if (qd->new_data)
1745 /* if new pixel data, and we already have a pixmap, update the tile */
1746 qd->it->blank = FALSE;
1747 if (qd->it->surface && qd->it->render_done == TILE_RENDER_ALL)
1749 rt_tile_render(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1757 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
1762 rt_queue_merge(qd->it->qd2, qd);
1768 rt->draw_queue_2pass = g_list_append(rt->draw_queue_2pass, qd);
1779 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
1783 if (!rt->draw_queue && !rt->draw_queue_2pass)
1785 pr_render_complete_signal(pr);
1787 rt->draw_idle_id = 0;
1791 return rt_queue_schedule_next_draw(rt, FALSE);
1794 static void rt_queue_list_free(GList *list)
1814 static void rt_queue_clear(RendererTiles *rt)
1816 rt_queue_list_free(rt->draw_queue);
1817 rt->draw_queue = NULL;
1819 rt_queue_list_free(rt->draw_queue_2pass);
1820 rt->draw_queue_2pass = NULL;
1822 if (rt->draw_idle_id)
1824 g_source_remove(rt->draw_idle_id);
1825 rt->draw_idle_id = 0;
1830 static void rt_queue_merge(QueueData *parent, QueueData *qd)
1832 if (parent->x + parent->w < qd->x + qd->w)
1834 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
1836 if (parent->x > qd->x)
1838 parent->w += parent->x - qd->x;
1842 if (parent->y + parent->h < qd->y + qd->h)
1844 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
1846 if (parent->y > qd->y)
1848 parent->h += parent->y - qd->y;
1852 parent->new_data |= qd->new_data;
1855 static gboolean rt_clamp_to_visible(RendererTiles *rt, gint *x, gint *y, gint *w, gint *h)
1857 PixbufRenderer *pr = rt->pr;
1864 vh = pr->vis_height;
1869 if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
1872 nx = CLAMP(*x, vx, vx + vw);
1873 nw = CLAMP(*w - (nx - *x), 1, vw);
1875 ny = CLAMP(*y, vy, vy + vh);
1876 nh = CLAMP(*h - (ny - *y), 1, vh);
1886 static gboolean rt_queue_to_tiles(RendererTiles *rt, gint x, gint y, gint w, gint h,
1887 gboolean clamp, ImageRenderType render,
1888 gboolean new_data, gboolean only_existing)
1890 PixbufRenderer *pr = rt->pr;
1895 if (clamp && !rt_clamp_to_visible(rt, &x, &y, &w, &h)) return FALSE;
1897 x1 = ROUND_DOWN(x, rt->tile_width);
1898 x2 = ROUND_UP(x + w, rt->tile_width);
1900 y1 = ROUND_DOWN(y, rt->tile_height);
1901 y2 = ROUND_UP(y + h, rt->tile_height);
1903 for (j = y1; j <= y2; j += rt->tile_height)
1905 for (i = x1; i <= x2; i += rt->tile_width)
1909 it = rt_tile_get(rt, i, j,
1911 (i + rt->tile_width < rt->x_scroll ||
1912 i > rt->x_scroll + pr->vis_width ||
1913 j + rt->tile_height < rt->y_scroll ||
1914 j > rt->y_scroll + pr->vis_height)));
1919 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
1920 (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
1922 it->render_todo = render;
1925 qd = g_new(QueueData, 1);
1927 qd->new_data = new_data;
1937 qd->w = x + w - i - qd->x;
1938 if (qd->x + qd->w > rt->tile_width) qd->w = rt->tile_width - qd->x;
1948 qd->h = y + h - j - qd->y;
1949 if (qd->y + qd->h > rt->tile_height) qd->h = rt->tile_height - qd->y;
1951 if (qd->w < 1 || qd->h < 1)
1957 rt_queue_merge(it->qd, qd);
1963 rt->draw_queue = g_list_append(rt->draw_queue, qd);
1972 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
1973 gboolean clamp, ImageRenderType render,
1974 gboolean new_data, gboolean only_existing)
1976 PixbufRenderer *pr = rt->pr;
1981 nx = CLAMP(x, 0, pr->width - 1);
1982 ny = CLAMP(y, 0, pr->height - 1);
1985 w = CLAMP(w, 0, pr->width - nx);
1986 h = CLAMP(h, 0, pr->height - ny);
1987 if (w < 1 || h < 1) return;
1989 if (rt_queue_to_tiles(rt, nx, ny, w, h, clamp, render, new_data, only_existing) &&
1990 ((!rt->draw_queue && !rt->draw_queue_2pass) || !rt->draw_idle_id))
1992 if (rt->draw_idle_id)
1994 g_source_remove(rt->draw_idle_id);
1995 rt->draw_idle_id = 0;
1997 rt_queue_schedule_next_draw(rt, TRUE);
2001 static void rt_scroll(void *renderer, gint x_off, gint y_off)
2003 RendererTiles *rt = (RendererTiles *) renderer;
2004 PixbufRenderer *pr = rt->pr;
2007 if (rt->stereo_mode & PR_STEREO_MIRROR) x_off = -x_off;
2008 if (rt->stereo_mode & PR_STEREO_FLIP) y_off = -y_off;
2010 gint w = pr->vis_width - abs(x_off);
2011 gint h = pr->vis_height - abs(y_off);
2015 /* scrolled completely to new material */
2016 rt_queue(rt, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2024 cairo_surface_t *surface;
2048 #if GTK_CHECK_VERSION(3,0,0)
2049 cr = cairo_create(rt->surface);
2050 surface = rt->surface;
2055 box = GTK_WIDGET(pr);
2056 window = gtk_widget_get_window(box);
2058 cr = gdk_cairo_create(window);
2059 surface = cairo_get_target(cr);
2061 /* clipping restricts the intermediate surface's size, so it's a good idea
2063 cairo_rectangle(cr, x1 + pr->x_offset + rt->stereo_off_x, y1 + pr->y_offset + rt->stereo_off_y, w, h);
2065 /* Now push a group to change the target */
2066 cairo_push_group (cr);
2067 cairo_set_source_surface(cr, surface, x1 - x2, y1 - y2);
2069 /* Now copy the intermediate target back */
2070 cairo_pop_group_to_source(cr);
2074 rt_overlay_queue_all(rt, x2, y2, x1, y1);
2076 w = pr->vis_width - w;
2077 h = pr->vis_height - h;
2082 x_off > 0 ? rt->x_scroll + (pr->vis_width - w) : rt->x_scroll, rt->y_scroll,
2083 w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2087 /** @FIXME to optimize this, remove overlap */
2089 rt->x_scroll, y_off > 0 ? rt->y_scroll + (pr->vis_height - h) : rt->y_scroll,
2090 pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2095 static void renderer_area_changed(void *renderer, gint src_x, gint src_y, gint src_w, gint src_h)
2097 RendererTiles *rt = (RendererTiles *)renderer;
2098 PixbufRenderer *pr = rt->pr;
2099 gint x, y, width, height, x1, y1, x2, y2;
2101 gint orientation = rt_get_orientation(rt);
2102 pr_coords_map_orientation_reverse(orientation,
2103 src_x - GET_RIGHT_PIXBUF_OFFSET(rt), src_y,
2104 pr->image_width, pr->image_height,
2109 if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
2111 /* increase region when using a zoom quality that may access surrounding pixels */
2116 x1 = (gint)floor((gdouble)x * pr->scale);
2117 y1 = (gint)floor((gdouble)y * pr->scale * pr->aspect_ratio);
2118 x2 = (gint)ceil((gdouble)(x + width) * pr->scale);
2119 y2 = (gint)ceil((gdouble)(y + height) * pr->scale * pr->aspect_ratio);
2121 rt_queue(rt, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
2124 static void renderer_redraw(RendererTiles *rt, gint x, gint y, gint w, gint h,
2125 gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing)
2127 PixbufRenderer *pr = rt->pr;
2129 x -= rt->stereo_off_x;
2130 y -= rt->stereo_off_y;
2132 rt_border_draw(rt, x, y, w, h);
2134 x = MAX(0, x - pr->x_offset + pr->x_scroll);
2135 y = MAX(0, y - pr->y_offset + pr->y_scroll);
2139 MIN(w, pr->width - x),
2140 MIN(h, pr->height - y),
2141 clamp, render, new_data, only_existing);
2144 static void renderer_update_pixbuf(void *renderer, gboolean lazy)
2146 rt_queue_clear((RendererTiles *)renderer);
2149 static void renderer_update_zoom(void *renderer, gboolean lazy)
2151 RendererTiles *rt = (RendererTiles *)renderer;
2152 PixbufRenderer *pr = rt->pr;
2154 rt_tile_invalidate_all((RendererTiles *)renderer);
2157 renderer_redraw(renderer, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, TRUE, FALSE);
2159 rt_border_clear(rt);
2162 static void renderer_invalidate_region(void *renderer, gint x, gint y, gint w, gint h)
2164 rt_tile_invalidate_region((RendererTiles *)renderer, x, y, w, h);
2167 static void renderer_update_viewport(void *renderer)
2169 RendererTiles *rt = (RendererTiles *)renderer;
2171 rt->stereo_off_x = 0;
2172 rt->stereo_off_y = 0;
2174 if (rt->stereo_mode & PR_STEREO_RIGHT)
2176 if (rt->stereo_mode & PR_STEREO_HORIZ)
2178 rt->stereo_off_x = rt->pr->viewport_width;
2180 else if (rt->stereo_mode & PR_STEREO_VERT)
2182 rt->stereo_off_y = rt->pr->viewport_height;
2184 else if (rt->stereo_mode & PR_STEREO_FIXED)
2186 rt->stereo_off_x = rt->pr->stereo_fixed_x_right;
2187 rt->stereo_off_y = rt->pr->stereo_fixed_y_right;
2192 if (rt->stereo_mode & PR_STEREO_FIXED)
2194 rt->stereo_off_x = rt->pr->stereo_fixed_x_left;
2195 rt->stereo_off_y = rt->pr->stereo_fixed_y_left;
2198 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);
2200 rt_overlay_update_sizes(rt);
2201 rt_border_clear(rt);
2204 static void renderer_stereo_set(void *renderer, gint stereo_mode)
2206 RendererTiles *rt = (RendererTiles *)renderer;
2208 rt->stereo_mode = stereo_mode;
2211 static void renderer_free(void *renderer)
2213 RendererTiles *rt = (RendererTiles *)renderer;
2215 rt_tile_free_all(rt);
2216 if (rt->spare_tile) g_object_unref(rt->spare_tile);
2217 if (rt->overlay_buffer) g_object_unref(rt->overlay_buffer);
2218 rt_overlay_list_clear(rt);
2219 /* disconnect "hierarchy-changed" */
2220 g_signal_handlers_disconnect_matched(G_OBJECT(rt->pr), G_SIGNAL_MATCH_DATA,
2225 #if GTK_CHECK_VERSION(3,0,0)
2226 static gboolean rt_realize_cb(GtkWidget *widget, gpointer data)
2228 RendererTiles *rt = (RendererTiles *)data;
2233 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));
2235 cr = cairo_create(rt->surface);
2236 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);
2244 static gboolean rt_size_allocate_cb(GtkWidget *widget, GdkRectangle *allocation, gpointer data)
2246 RendererTiles *rt = data;
2248 cairo_surface_t *old_surface;
2250 if (gtk_widget_get_realized(GTK_WIDGET(rt->pr)))
2252 old_surface = rt->surface;
2253 rt->surface = gdk_window_create_similar_surface(gtk_widget_get_window(widget), CAIRO_CONTENT_COLOR, allocation->width, allocation->height);
2255 cr = cairo_create(rt->surface);
2257 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);
2259 cairo_set_source_surface(cr, old_surface, 0, 0);
2262 cairo_surface_destroy(old_surface);
2264 renderer_redraw(rt, allocation->x, allocation->y, allocation->width, allocation->height, FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2270 static gboolean rt_draw_cb(GtkWidget *widget, cairo_t *cr, gpointer data)
2272 RendererTiles *rt = (RendererTiles *)data;
2276 if (rt->stereo_mode & (PR_STEREO_HORIZ | PR_STEREO_VERT))
2278 cairo_push_group(cr);
2279 cairo_set_source_rgb(cr, (double)rt->pr->color.red / 65535, (double)rt->pr->color.green / 65535, (double)rt->pr->color.blue / 65535);
2281 if (rt->stereo_mode & PR_STEREO_HORIZ)
2283 cairo_rectangle(cr, rt->stereo_off_x, 0, rt->pr->viewport_width, rt->pr->viewport_height);
2287 cairo_rectangle(cr, 0, rt->stereo_off_y, rt->pr->viewport_width, rt->pr->viewport_height);
2292 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);
2294 cairo_set_source_surface(cr, rt->surface, 0, 0);
2297 cairo_pop_group_to_source(cr);
2302 cairo_set_source_surface(cr, rt->surface, 0, 0);
2306 work = rt->overlay_list;
2310 gint px, py, pw, ph;
2311 pw = gdk_pixbuf_get_width(od->pixbuf);
2312 ph = gdk_pixbuf_get_height(od->pixbuf);
2316 if (od->flags & OVL_RELATIVE)
2318 if (px < 0) px = rt->pr->viewport_width - pw + px;
2319 if (py < 0) py = rt->pr->viewport_height - ph + py;
2322 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px, py);
2331 static gboolean rt_expose_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
2333 RendererTiles *rt = (RendererTiles *)data;
2334 if (gtk_widget_is_drawable(widget))
2336 if (gtk_widget_get_has_window(widget))
2338 if (event->window != gtk_widget_get_window(widget))
2342 gdk_window_get_position(event->window, &area.x, &area.y);
2343 area.x += event->area.x;
2344 area.y += event->area.y;
2345 area.width = event->area.width;
2346 area.height = event->area.height;
2347 renderer_redraw(rt, area.x, area.y, area.width, area.height,
2348 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2353 renderer_redraw(rt, event->area.x, event->area.y, event->area.width, event->area.height,
2354 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2364 RendererFuncs *renderer_tiles_new(PixbufRenderer *pr)
2366 RendererTiles *rt = g_new0(RendererTiles, 1);
2370 rt->f.area_changed = renderer_area_changed;
2371 rt->f.update_pixbuf = renderer_update_pixbuf;
2372 rt->f.free = renderer_free;
2373 rt->f.update_zoom = renderer_update_zoom;
2374 rt->f.invalidate_region = renderer_invalidate_region;
2375 rt->f.scroll = rt_scroll;
2376 rt->f.update_viewport = renderer_update_viewport;
2379 rt->f.overlay_add = renderer_tiles_overlay_add;
2380 rt->f.overlay_set = renderer_tiles_overlay_set;
2381 rt->f.overlay_get = renderer_tiles_overlay_get;
2383 rt->f.stereo_set = renderer_stereo_set;
2385 rt->tile_width = options->image.tile_size;
2386 rt->tile_height = options->image.tile_size;
2389 rt->tile_cache_size = 0;
2391 rt->tile_cache_max = PR_CACHE_SIZE_DEFAULT;
2393 rt->draw_idle_id = 0;
2395 rt->stereo_mode = 0;
2396 rt->stereo_off_x = 0;
2397 rt->stereo_off_y = 0;
2399 #if GTK_CHECK_VERSION(3, 10, 0)
2400 rt->hidpi_scale = gtk_widget_get_scale_factor(GTK_WIDGET(rt->pr));
2402 rt->hidpi_scale = 1;
2405 g_signal_connect(G_OBJECT(pr), "hierarchy-changed",
2406 G_CALLBACK(rt_hierarchy_changed_cb), rt);
2408 #if GTK_CHECK_VERSION(3,0,0)
2409 g_signal_connect(G_OBJECT(pr), "draw",
2410 G_CALLBACK(rt_draw_cb), rt);
2411 g_signal_connect(G_OBJECT(pr), "realize", G_CALLBACK(rt_realize_cb), rt);
2412 g_signal_connect(G_OBJECT(pr), "size-allocate", G_CALLBACK(rt_size_allocate_cb), rt);
2414 g_signal_connect(G_OBJECT(pr), "expose_event",
2415 G_CALLBACK(rt_expose_cb), rt);
2418 return (RendererFuncs *) rt;
2422 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */