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.
22 #include "renderer-tiles.h"
30 #include <gdk-pixbuf/gdk-pixbuf.h>
32 #include <glib-object.h>
38 #include "pixbuf-renderer.h"
41 /* comment this out if not using this from within Geeqie
42 * defining GQ_BUILD does these things:
43 * - Sets the shift-click scroller pixbuf to a nice icon instead of a black box
49 #include "pixbuf-util.h"
51 enum ExifOrientationType {
52 EXIF_ORIENTATION_UNKNOWN = 0,
53 EXIF_ORIENTATION_TOP_LEFT = 1,
54 EXIF_ORIENTATION_TOP_RIGHT = 2,
55 EXIF_ORIENTATION_BOTTOM_RIGHT = 3,
56 EXIF_ORIENTATION_BOTTOM_LEFT = 4,
57 EXIF_ORIENTATION_LEFT_TOP = 5,
58 EXIF_ORIENTATION_RIGHT_TOP = 6,
59 EXIF_ORIENTATION_RIGHT_BOTTOM = 7,
60 EXIF_ORIENTATION_LEFT_BOTTOM = 8
71 cairo_surface_t *surface; /* off screen buffer */
72 GdkPixbuf *pixbuf; /* pixbuf area for zooming */
73 gint x; /* x offset into image */
74 gint y; /* y offset into image */
75 gint w; /* width that is visible (may be less if at edge of image) */
76 gint h; /* height '' */
80 /* render_todo: (explanation)
82 AREA render area of tile, usually only used when loading an image
83 note: will jump to an ALL if render_done is not ALL.
84 ALL render entire tile, if never done before w/ ALL, for expose events *only*
87 ImageRenderType render_todo; /* what to do (see above) */
88 ImageRenderType render_done; /* highest that has been done before on tile */
93 guint size; /* est. memory used by pixmap and pixbuf */
116 OverlayRendererFlags flags;
124 gint tile_cache_max; /* max MiB to use for offscreen buffer */
128 GList *tiles; /* list of buffer tiles */
129 gint tile_cache_size; /* allocated size of pixmaps/pixbufs */
130 GList *draw_queue; /* list of areas to redraw */
131 GList *draw_queue_2pass;/* list when 2 pass is enabled */
134 cairo_surface_t *overlay_buffer;
135 cairo_surface_t *surface;
137 guint draw_idle_id; /* event source id */
139 GdkPixbuf *spare_tile;
145 gint x_scroll; /* allow local adjustment and mirroring */
151 constexpr size_t COLOR_BYTES = 3; /* rgb */
154 inline gint get_right_pixbuf_offset(RendererTiles *rt)
156 return (!!(rt->stereo_mode & PR_STEREO_RIGHT) != !!(rt->stereo_mode & PR_STEREO_SWAP)) ?
157 rt->pr->stereo_pixbuf_offset_right : rt->pr->stereo_pixbuf_offset_left;
160 inline gint get_left_pixbuf_offset(RendererTiles *rt)
162 return (!!(rt->stereo_mode & PR_STEREO_RIGHT) == !!(rt->stereo_mode & PR_STEREO_SWAP)) ?
163 rt->pr->stereo_pixbuf_offset_right : rt->pr->stereo_pixbuf_offset_left;
169 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h, ImageTile *it);
171 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it);
172 static void rt_queue_merge(QueueData *parent, QueueData *qd);
173 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
174 gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing);
176 static gint rt_queue_draw_idle_cb(gpointer data);
179 static void rt_sync_scroll(RendererTiles *rt)
181 PixbufRenderer *pr = rt->pr;
183 rt->x_scroll = (rt->stereo_mode & PR_STEREO_MIRROR) ?
184 pr->width - pr->vis_width - pr->x_scroll
187 rt->y_scroll = (rt->stereo_mode & PR_STEREO_FLIP) ?
188 pr->height - pr->vis_height - pr->y_scroll
193 *-------------------------------------------------------------------
195 *-------------------------------------------------------------------
198 static void rt_border_draw(RendererTiles *rt, gint x, gint y, gint w, gint h)
200 PixbufRenderer *pr = rt->pr;
209 box = GTK_WIDGET(pr);
210 window = gtk_widget_get_window(box);
214 cr = cairo_create(rt->surface);
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, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
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, nullptr);
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, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
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, nullptr);
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, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
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, nullptr);
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, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
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, nullptr);
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, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
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, nullptr);
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)
300 auto it = g_new0(ImageTile, 1);
307 it->render_done = TILE_RENDER_NONE;
312 static void rt_tile_free(ImageTile *it)
316 if (it->pixbuf) g_object_unref(it->pixbuf);
317 if (it->surface) cairo_surface_destroy(it->surface);
322 static void rt_tile_free_all(RendererTiles *rt)
324 g_list_free_full(rt->tiles, reinterpret_cast<GDestroyNotify>(rt_tile_free));
326 rt->tile_cache_size = 0;
329 static ImageTile *rt_tile_add(RendererTiles *rt, gint x, gint y)
331 PixbufRenderer *pr = rt->pr;
334 it = rt_tile_new(x, y, rt->tile_width, rt->tile_height);
336 if (it->x + it->w > pr->width) it->w = pr->width - it->x;
337 if (it->y + it->h > pr->height) it->h = pr->height - it->y;
339 rt->tiles = g_list_prepend(rt->tiles, it);
340 rt->tile_cache_size += it->size;
345 static void rt_tile_remove(RendererTiles *rt, ImageTile *it)
349 QueueData *qd = it->qd;
352 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
358 QueueData *qd = it->qd2;
361 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
365 rt->tiles = g_list_remove(rt->tiles, it);
366 rt->tile_cache_size -= it->size;
371 static void rt_tile_free_space(RendererTiles *rt, guint space, ImageTile *it)
373 PixbufRenderer *pr = rt->pr;
377 work = g_list_last(rt->tiles);
379 if (pr->source_tiles_enabled && pr->scale < 1.0)
383 tiles = (pr->vis_width / rt->tile_width + 1) * (pr->vis_height / rt->tile_height + 1);
384 tile_max = MAX(tiles * rt->tile_width * rt->tile_height * 3,
385 (gint)((gdouble)rt->tile_cache_max * 1048576.0 * pr->scale));
389 tile_max = rt->tile_cache_max * 1048576;
392 while (work && rt->tile_cache_size + space > tile_max)
396 needle = static_cast<ImageTile *>(work->data);
399 ((!needle->qd && !needle->qd2) || !rt_tile_is_visible(rt, needle))) rt_tile_remove(rt, needle);
403 static void rt_tile_invalidate_all(RendererTiles *rt)
405 PixbufRenderer *pr = rt->pr;
413 it = static_cast<ImageTile *>(work->data);
416 it->render_done = TILE_RENDER_NONE;
417 it->render_todo = TILE_RENDER_ALL;
420 it->w = MIN(rt->tile_width, pr->width - it->x);
421 it->h = MIN(rt->tile_height, pr->height - it->y);
425 static void rt_tile_invalidate_region(RendererTiles *rt, gint x, gint y, gint w, gint h)
433 x1 = ROUND_DOWN(x, rt->tile_width);
434 x2 = ROUND_UP(x + w, rt->tile_width);
436 y1 = ROUND_DOWN(y, rt->tile_height);
437 y2 = ROUND_UP(y + h, rt->tile_height);
444 it = static_cast<ImageTile *>(work->data);
447 if (it->x < x2 && it->x + it->w > x1 &&
448 it->y < y2 && it->y + it->h > y1)
450 it->render_done = TILE_RENDER_NONE;
451 it->render_todo = TILE_RENDER_ALL;
456 static ImageTile *rt_tile_get(RendererTiles *rt, gint x, gint y, gboolean only_existing)
465 it = static_cast<ImageTile *>(work->data);
466 if (it->x == x && it->y == y)
468 rt->tiles = g_list_delete_link(rt->tiles, work);
469 rt->tiles = g_list_prepend(rt->tiles, it);
476 if (only_existing) return nullptr;
478 return rt_tile_add(rt, x, y);
481 static gint pixmap_calc_size(cairo_surface_t *)
483 return options->image.tile_size * options->image.tile_size * 4 / 8;
486 static void rt_hidpi_aware_draw(
493 cairo_surface_t *surface;
494 surface = gdk_cairo_surface_create_from_pixbuf(pixbuf, rt->hidpi_scale, nullptr);
495 cairo_set_source_surface(cr, surface, x, y);
497 cairo_surface_destroy(surface);
500 static void rt_tile_prepare(RendererTiles *rt, ImageTile *it)
502 PixbufRenderer *pr = rt->pr;
505 cairo_surface_t *surface;
508 surface = gdk_window_create_similar_surface(gtk_widget_get_window(reinterpret_cast<GtkWidget *>(pr)),
510 rt->tile_width, rt->tile_height);
512 size = pixmap_calc_size(surface) * rt->hidpi_scale * rt->hidpi_scale;
513 rt_tile_free_space(rt, size, it);
515 it->surface = surface;
517 rt->tile_cache_size += size;
524 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, rt->hidpi_scale * rt->tile_width, rt->hidpi_scale * rt->tile_height);
526 size = gdk_pixbuf_get_rowstride(pixbuf) * rt->tile_height * rt->hidpi_scale;
527 rt_tile_free_space(rt, size, it);
531 rt->tile_cache_size += size;
536 *-------------------------------------------------------------------
538 *-------------------------------------------------------------------
541 static void rt_overlay_get_position(RendererTiles *rt, OverlayData *od,
542 gint *x, gint *y, gint *w, gint *h)
544 PixbufRenderer *pr = rt->pr;
550 pw = gdk_pixbuf_get_width(od->pixbuf);
551 ph = gdk_pixbuf_get_height(od->pixbuf);
555 if (od->flags & OVL_RELATIVE)
557 if (px < 0) px = pr->viewport_width - pw + px;
558 if (py < 0) py = pr->viewport_height - ph + py;
567 static void rt_overlay_init_window(RendererTiles *rt, OverlayData *od)
569 PixbufRenderer *pr = rt->pr;
574 GdkWindowAttr attributes;
575 gint attributes_mask;
577 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
579 attributes.window_type = GDK_WINDOW_CHILD;
580 attributes.wclass = GDK_INPUT_OUTPUT;
581 attributes.width = pw;
582 attributes.height = ph;
583 attributes.event_mask = GDK_EXPOSURE_MASK;
586 od->window = gdk_window_new(gtk_widget_get_window(GTK_WIDGET(pr)), &attributes, attributes_mask);
587 gdk_window_set_user_data(od->window, pr);
588 gdk_window_move(od->window, px + rt->stereo_off_x, py + rt->stereo_off_y);
589 gdk_window_show(od->window);
592 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h,
595 PixbufRenderer *pr = rt->pr;
598 work = rt->overlay_list;
611 od = static_cast<OverlayData *>(work->data);
614 if (!od->window) rt_overlay_init_window(rt, od);
616 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
617 if (pr_clip_region(x, y, w, h, px, py, pw, ph, &rx, &ry, &rw, &rh))
619 if (!rt->overlay_buffer)
621 rt->overlay_buffer = gdk_window_create_similar_surface(gtk_widget_get_window(reinterpret_cast<GtkWidget *>(pr)),
623 rt->tile_width, rt->tile_height);
630 cr = cairo_create(rt->overlay_buffer);
631 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);
632 cairo_rectangle(cr, 0, 0, rw, rh);
633 cairo_fill_preserve(cr);
635 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - rx, py - ry);
639 cr = gdk_cairo_create(od->window);
640 cairo_set_source_surface(cr, rt->overlay_buffer, rx - px, ry - py);
641 cairo_rectangle (cr, rx - px, ry - py, rw, rh);
647 /* no ImageTile means region may be larger than our scratch buffer */
651 for (sx = rx; sx < rx + rw; sx += rt->tile_width)
652 for (sy = ry; sy < ry + rh; sy += rt->tile_height)
658 sw = MIN(rx + rw - sx, rt->tile_width);
659 sh = MIN(ry + rh - sy, rt->tile_height);
661 cr = cairo_create(rt->overlay_buffer);
662 cairo_set_source_rgb(cr, 0, 0, 0);
663 cairo_rectangle(cr, 0, 0, sw, sh);
664 cairo_fill_preserve(cr);
666 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - sx, py - sy);
670 cr = gdk_cairo_create(od->window);
671 cairo_set_source_surface(cr, rt->overlay_buffer, sx - px, sy - py);
672 cairo_rectangle (cr, sx - px, sy - py, sw, sh);
681 static void rt_overlay_queue_draw(RendererTiles *rt, OverlayData *od, gint x1, gint y1, gint x2, gint y2)
683 PixbufRenderer *pr = rt->pr;
689 rt_overlay_get_position(rt, od, &x, &y, &w, &h);
697 rt_queue(rt, rt->x_scroll - pr->x_offset + x,
698 rt->y_scroll - pr->y_offset + y,
700 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
702 rt_border_draw(rt, x, y, w, h);
705 static void rt_overlay_queue_all(RendererTiles *rt, gint x1, gint y1, gint x2, gint y2)
709 work = rt->overlay_list;
712 auto od = static_cast<OverlayData *>(work->data);
715 rt_overlay_queue_draw(rt, od, x1, y1, x2, y2);
719 static void rt_overlay_update_sizes(RendererTiles *rt)
723 work = rt->overlay_list;
726 auto od = static_cast<OverlayData *>(work->data);
729 if (!od->window) rt_overlay_init_window(rt, od);
731 if (od->flags & OVL_RELATIVE)
738 rt_overlay_get_position(rt, od, &x, &y, &w, &h);
739 gdk_window_move_resize(od->window, x + rt->stereo_off_x, y + rt->stereo_off_y, w, h);
744 static OverlayData *rt_overlay_find(RendererTiles *rt, gint id)
748 work = rt->overlay_list;
751 auto od = static_cast<OverlayData *>(work->data);
754 if (od->id == id) return od;
761 gint renderer_tiles_overlay_add(void *renderer, GdkPixbuf *pixbuf, gint x, gint y,
762 OverlayRendererFlags flags)
764 auto rt = static_cast<RendererTiles *>(renderer);
765 PixbufRenderer *pr = rt->pr;
768 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
769 g_return_val_if_fail(pixbuf != nullptr, -1);
772 while (rt_overlay_find(rt, id)) id++;
774 auto od = g_new0(OverlayData, 1);
777 g_object_ref(G_OBJECT(od->pixbuf));
782 rt_overlay_init_window(rt, od);
784 rt->overlay_list = g_list_append(rt->overlay_list, od);
786 gtk_widget_queue_draw(GTK_WIDGET(rt->pr));
791 static void rt_overlay_free(RendererTiles *rt, OverlayData *od)
793 rt->overlay_list = g_list_remove(rt->overlay_list, od);
795 if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
796 if (od->window) gdk_window_destroy(od->window);
799 if (!rt->overlay_list && rt->overlay_buffer)
801 cairo_surface_destroy(rt->overlay_buffer);
802 rt->overlay_buffer = nullptr;
806 static void rt_overlay_list_clear(RendererTiles *rt)
808 while (rt->overlay_list)
810 auto od = static_cast<OverlayData *>(rt->overlay_list->data);
811 rt_overlay_free(rt, od);
815 static void rt_overlay_list_reset_window(RendererTiles *rt)
819 if (rt->overlay_buffer) cairo_surface_destroy(rt->overlay_buffer);
820 rt->overlay_buffer = nullptr;
822 work = rt->overlay_list;
825 auto od = static_cast<OverlayData *>(work->data);
827 if (od->window) gdk_window_destroy(od->window);
828 od->window = nullptr;
832 void renderer_tiles_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint, gint)
834 auto rc = static_cast<RendererTiles *>(renderer);
835 PixbufRenderer *pr = rc->pr;
838 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
840 od = rt_overlay_find(rc, id);
845 g_object_ref(G_OBJECT(pixbuf));
846 g_object_unref(G_OBJECT(od->pixbuf));
851 rt_overlay_free(rc, od);
854 gtk_widget_queue_draw(GTK_WIDGET(rc->pr));
857 gboolean renderer_tiles_overlay_get(void *renderer, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
859 auto rt = static_cast<RendererTiles *>(renderer);
860 PixbufRenderer *pr = rt->pr;
863 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
865 od = rt_overlay_find(rt, id);
866 if (!od) return FALSE;
868 if (pixbuf) *pixbuf = od->pixbuf;
875 static void rt_hierarchy_changed_cb(GtkWidget *, GtkWidget *, gpointer data)
877 auto rt = static_cast<RendererTiles *>(data);
878 rt_overlay_list_reset_window(rt);
882 *-------------------------------------------------------------------
884 *-------------------------------------------------------------------
887 static GdkPixbuf *rt_get_spare_tile(RendererTiles *rt)
889 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);
890 return rt->spare_tile;
893 static void rt_tile_rotate_90_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
895 GdkPixbuf *src = *tile;
908 gint tw = rt->tile_width * rt->hidpi_scale;
910 srs = gdk_pixbuf_get_rowstride(src);
911 s_pix = gdk_pixbuf_get_pixels(src);
912 spi = s_pix + (x * COLOR_BYTES);
914 dest = rt_get_spare_tile(rt);
915 drs = gdk_pixbuf_get_rowstride(dest);
916 d_pix = gdk_pixbuf_get_pixels(dest);
917 dpi = d_pix + (tw - 1) * COLOR_BYTES;
919 for (i = y; i < y + h; i++)
921 sp = spi + (i * srs);
922 ip = dpi - (i * COLOR_BYTES);
923 for (j = x; j < x + w; j++)
926 memcpy(dp, sp, COLOR_BYTES);
931 rt->spare_tile = src;
935 static void rt_tile_rotate_90_counter_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
937 GdkPixbuf *src = *tile;
950 gint th = rt->tile_height * 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 + (th - 1) * drs;
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_mirror_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
979 GdkPixbuf *src = *tile;
992 gint tw = rt->tile_width * rt->hidpi_scale;
994 srs = gdk_pixbuf_get_rowstride(src);
995 s_pix = gdk_pixbuf_get_pixels(src);
996 spi = s_pix + (x * COLOR_BYTES);
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;
1003 for (i = y; i < y + h; i++)
1005 sp = spi + (i * srs);
1006 dp = dpi + (i * drs);
1007 for (j = 0; j < w; j++)
1009 memcpy(dp, sp, COLOR_BYTES);
1015 rt->spare_tile = src;
1019 static void rt_tile_mirror_and_flip(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1021 GdkPixbuf *src = *tile;
1032 gint tw = rt->tile_width * rt->hidpi_scale;
1033 gint th = rt->tile_height * rt->hidpi_scale;
1035 srs = gdk_pixbuf_get_rowstride(src);
1036 s_pix = gdk_pixbuf_get_pixels(src);
1038 dest = rt_get_spare_tile(rt);
1039 drs = gdk_pixbuf_get_rowstride(dest);
1040 d_pix = gdk_pixbuf_get_pixels(dest);
1041 dpi = d_pix + (th - 1) * drs + (tw - 1) * COLOR_BYTES;
1043 for (i = y; i < y + h; i++)
1045 sp = s_pix + (i * srs) + (x * COLOR_BYTES);
1046 dp = dpi - (i * drs) - (x * COLOR_BYTES);
1047 for (j = 0; j < w; j++)
1049 memcpy(dp, sp, COLOR_BYTES);
1055 rt->spare_tile = src;
1059 static void rt_tile_flip_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1061 GdkPixbuf *src = *tile;
1072 gint th = rt->tile_height * rt->hidpi_scale;
1074 srs = gdk_pixbuf_get_rowstride(src);
1075 s_pix = gdk_pixbuf_get_pixels(src);
1076 spi = s_pix + (x * COLOR_BYTES);
1078 dest = rt_get_spare_tile(rt);
1079 drs = gdk_pixbuf_get_rowstride(dest);
1080 d_pix = gdk_pixbuf_get_pixels(dest);
1081 dpi = d_pix + (th - 1) * drs + (x * COLOR_BYTES);
1083 for (i = y; i < y + h; i++)
1085 sp = spi + (i * srs);
1086 dp = dpi - (i * drs);
1087 memcpy(dp, sp, w * COLOR_BYTES);
1090 rt->spare_tile = src;
1094 static void rt_tile_apply_orientation(RendererTiles *rt, gint orientation, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h)
1096 switch (orientation)
1098 case EXIF_ORIENTATION_TOP_LEFT:
1099 /* normal -- nothing to do */
1101 case EXIF_ORIENTATION_TOP_RIGHT:
1104 rt_tile_mirror_only(rt, pixbuf, x, y, w, h);
1107 case EXIF_ORIENTATION_BOTTOM_RIGHT:
1110 rt_tile_mirror_and_flip(rt, pixbuf, x, y, w, h);
1113 case EXIF_ORIENTATION_BOTTOM_LEFT:
1116 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1119 case EXIF_ORIENTATION_LEFT_TOP:
1121 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1122 rt_tile_rotate_90_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1125 case EXIF_ORIENTATION_RIGHT_TOP:
1126 /* rotated -90 (270) */
1128 rt_tile_rotate_90_clockwise(rt, pixbuf, x, y, w, h);
1131 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1133 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1134 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1137 case EXIF_ORIENTATION_LEFT_BOTTOM:
1140 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, y, w, h);
1144 /* The other values are out of range */
1149 static gboolean rt_source_tile_render(RendererTiles *rt, ImageTile *it,
1150 gint x, gint y, gint w, gint h,
1151 gboolean, gboolean fast)
1153 PixbufRenderer *pr = rt->pr;
1156 gboolean draw = FALSE;
1158 if (pr->zoom == 1.0 || pr->scale == 1.0)
1160 list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE);
1170 st = static_cast<SourceTile *>(work->data);
1173 if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1174 it->x + x, it->y + y, w, h,
1175 &rx, &ry, &rw, &rh))
1178 cr = cairo_create(it->surface);
1179 cairo_rectangle (cr, rx - it->x, ry - it->y, rw, rh);
1183 cairo_set_source_rgb(cr, 0, 0, 0);
1186 else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
1188 rt_hidpi_aware_draw(rt, cr, st->pixbuf, -it->x + st->x, -it->y + st->y);
1203 if (pr->image_width == 0 || pr->image_height == 0) return FALSE;
1204 scale_x = static_cast<gdouble>(pr->width) / pr->image_width;
1205 scale_y = static_cast<gdouble>(pr->height) / pr->image_height;
1207 sx = static_cast<gdouble>(it->x + x) / scale_x;
1208 sy = static_cast<gdouble>(it->y + y) / scale_y;
1209 sw = static_cast<gdouble>(w) / scale_x;
1210 sh = static_cast<gdouble>(h) / scale_y;
1212 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1215 /* draws red over draw region, to check for leaks (regions not filled) */
1216 pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255);
1219 list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE);
1233 st = static_cast<SourceTile *>(work->data);
1236 stx = floor(static_cast<gdouble>(st->x) * scale_x);
1237 sty = floor(static_cast<gdouble>(st->y) * scale_y);
1238 stw = ceil(static_cast<gdouble>(st->x + pr->source_tile_width) * scale_x) - stx;
1239 sth = ceil(static_cast<gdouble>(st->y + pr->source_tile_height) * scale_y) - sty;
1241 if (pr_clip_region(stx, sty, stw, sth,
1242 it->x + x, it->y + y, w, h,
1243 &rx, &ry, &rw, &rh))
1249 cr = cairo_create(it->surface);
1250 cairo_rectangle (cr, rx - st->x, ry - st->y, rw, rh);
1251 cairo_set_source_rgb(cr, 0, 0, 0);
1260 /* may need to use unfloored stx,sty values here */
1261 offset_x = static_cast<gdouble>(stx - it->x);
1262 offset_y = static_cast<gdouble>(sty - it->y);
1264 gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh,
1265 static_cast<gdouble>(0.0) + offset_x,
1266 static_cast<gdouble>(0.0) + offset_y,
1268 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1283 * @param ignore_alpha
1294 * @param interp_type
1297 * @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
1301 static void rt_tile_get_region(gboolean has_alpha, gboolean ignore_alpha,
1302 const GdkPixbuf *src, GdkPixbuf *dest,
1303 int pb_x, int pb_y, int pb_w, int pb_h,
1304 double offset_x, double offset_y, double scale_x, double scale_y,
1305 GdkInterpType interp_type,
1306 int check_x, int check_y, gboolean wide_image)
1308 GdkPixbuf* tmppixbuf;
1327 if (scale_x == 1.0 && scale_y == 1.0)
1331 srs = gdk_pixbuf_get_rowstride(src);
1332 drs = gdk_pixbuf_get_rowstride(dest);
1333 psrc = gdk_pixbuf_get_pixels(src);
1334 pdst = gdk_pixbuf_get_pixels(dest);
1335 for (y = 0; y < pb_h; y++)
1337 for (x = 0; x < pb_w; x++)
1339 for (c = 0; c < 3; c++)
1341 pdst[(y * drs) + x*3 + c] = psrc[(-static_cast<int>(offset_y) + pb_y + y) * srs + (-static_cast<int>(offset_x) + pb_x+x)*3 + c];
1348 gdk_pixbuf_copy_area(src,
1349 -offset_x + pb_x, -offset_y + pb_y,
1357 gdk_pixbuf_scale(src, dest,
1358 pb_x, pb_y, pb_w, pb_h,
1362 (wide_image && interp_type == GDK_INTERP_NEAREST) ? GDK_INTERP_TILES : interp_type);
1367 red_1=static_cast<guint32>(options->image.alpha_color_1.red * 255) << 16 & 0x00FF0000;
1368 green_1=static_cast<guint32>(options->image.alpha_color_1.green * 255) << 8 & 0x0000FF00;
1369 blue_1=static_cast<guint32>(options->image.alpha_color_1.blue * 255) & 0x000000FF;
1370 red_2=static_cast<guint32>(options->image.alpha_color_2.red * 255) << 16 & 0x00FF0000;
1371 green_2=static_cast<guint32>(options->image.alpha_color_2.green * 255) << 8 & 0x0000FF00;
1372 blue_2=static_cast<guint32>(options->image.alpha_color_2.blue * 255) & 0x000000FF;
1373 alpha_1 = red_1 + green_1 + blue_1;
1374 alpha_2 = red_2 + green_2 + blue_2;
1378 tmppixbuf = gdk_pixbuf_add_alpha(src, FALSE, 0, 0, 0);
1380 pixbuf_ignore_alpha_rect(tmppixbuf, 0, 0, gdk_pixbuf_get_width(src), gdk_pixbuf_get_height(src));
1382 gdk_pixbuf_composite_color(tmppixbuf, dest,
1383 pb_x, pb_y, pb_w, pb_h,
1387 (scale_x == 1.0 && scale_y == 1.0) ? GDK_INTERP_NEAREST : interp_type,
1388 255, check_x, check_y,
1389 PR_ALPHA_CHECK_SIZE,
1392 g_object_unref(tmppixbuf);
1396 gdk_pixbuf_composite_color(src, dest,
1397 pb_x, pb_y, pb_w, pb_h,
1401 (scale_x == 1.0 && scale_y == 1.0) ? GDK_INTERP_NEAREST : interp_type,
1402 255, check_x, check_y,
1403 PR_ALPHA_CHECK_SIZE,
1411 static gint rt_get_orientation(RendererTiles *rt)
1413 PixbufRenderer *pr = rt->pr;
1415 gint orientation = pr->orientation;
1416 static const gint mirror[] = {1, 2, 1, 4, 3, 6, 5, 8, 7};
1417 static const gint flip[] = {1, 4, 3, 2, 1, 8, 7, 6, 5};
1419 if (rt->stereo_mode & PR_STEREO_MIRROR) orientation = mirror[orientation];
1420 if (rt->stereo_mode & PR_STEREO_FLIP) orientation = flip[orientation];
1425 static void rt_tile_render(RendererTiles *rt, ImageTile *it,
1426 gint x, gint y, gint w, gint h,
1427 gboolean new_data, gboolean fast)
1429 PixbufRenderer *pr = rt->pr;
1431 gboolean draw = FALSE;
1432 gint orientation = rt_get_orientation(rt);
1433 gboolean wide_image = FALSE;
1435 if (it->render_todo == TILE_RENDER_NONE && it->surface && !new_data) return;
1437 if (it->render_done != TILE_RENDER_ALL)
1443 if (!fast) it->render_done = TILE_RENDER_ALL;
1445 else if (it->render_todo != TILE_RENDER_AREA)
1447 if (!fast) it->render_todo = TILE_RENDER_NONE;
1451 if (!fast) it->render_todo = TILE_RENDER_NONE;
1453 if (new_data) it->blank = FALSE;
1455 rt_tile_prepare(rt, it);
1456 has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
1458 /** @FIXME checker colors for alpha should be configurable,
1459 * also should be drawn for blank = TRUE
1464 /* no data, do fast rect fill */
1466 cr = cairo_create(it->surface);
1467 cairo_rectangle (cr, 0, 0, it->w, it->h);
1468 cairo_set_source_rgb(cr, 0, 0, 0);
1472 else if (pr->source_tiles_enabled)
1474 draw = rt_source_tile_render(rt, it, x, y, w, h, new_data, fast);
1487 if (pr->image_width == 0 || pr->image_height == 0) return;
1489 scale_x = rt->hidpi_scale * static_cast<gdouble>(pr->width) / pr->image_width;
1490 scale_y = rt->hidpi_scale * static_cast<gdouble>(pr->height) / pr->image_height;
1492 pr_tile_coords_map_orientation(orientation, it->x, it->y,
1493 pr->width, pr->height,
1494 rt->tile_width, rt->tile_height,
1496 pr_tile_region_map_orientation(orientation, x, y,
1497 rt->tile_width, rt->tile_height,
1502 src_x *= rt->hidpi_scale;
1503 src_y *= rt->hidpi_scale;
1504 pb_x *= rt->hidpi_scale;
1505 pb_y *= rt->hidpi_scale;
1506 pb_w *= rt->hidpi_scale;
1507 pb_h *= rt->hidpi_scale;
1509 switch (orientation)
1511 case EXIF_ORIENTATION_LEFT_TOP:
1512 case EXIF_ORIENTATION_RIGHT_TOP:
1513 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1514 case EXIF_ORIENTATION_LEFT_BOTTOM:
1515 std::swap(scale_x, scale_y);
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 static_cast<gdouble>(0.0) - src_x - get_right_pixbuf_offset(rt) * scale_x,
1531 static_cast<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 static_cast<gdouble>(0.0) - src_x - get_left_pixbuf_offset(rt) * scale_x,
1542 static_cast<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;
1575 /* clamp to visible */
1576 if (it->x + x < rt->x_scroll)
1578 w -= rt->x_scroll - it->x - x;
1579 x = rt->x_scroll - it->x;
1581 if (it->x + x + w > rt->x_scroll + pr->vis_width)
1583 w = rt->x_scroll + pr->vis_width - it->x - x;
1586 if (it->y + y < rt->y_scroll)
1588 h -= rt->y_scroll - it->y - y;
1589 y = rt->y_scroll - it->y;
1591 if (it->y + y + h > rt->y_scroll + pr->vis_height)
1593 h = rt->y_scroll + pr->vis_height - it->y - y;
1597 rt_tile_render(rt, it, x, y, w, h, new_data, fast);
1599 cr = cairo_create(rt->surface);
1600 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);
1601 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);
1605 if (rt->overlay_list)
1607 rt_overlay_draw(rt, pr->x_offset + (it->x - rt->x_scroll) + x,
1608 pr->y_offset + (it->y - rt->y_scroll) + y,
1613 gtk_widget_queue_draw(GTK_WIDGET(rt->pr));
1617 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it)
1619 PixbufRenderer *pr = rt->pr;
1620 return (it->x + it->w >= rt->x_scroll && it->x < rt->x_scroll + pr->vis_width &&
1621 it->y + it->h >= rt->y_scroll && it->y < rt->y_scroll + pr->vis_height);
1625 *-------------------------------------------------------------------
1627 *-------------------------------------------------------------------
1630 static gint rt_get_queued_area(GList *work)
1636 auto qd = static_cast<QueueData *>(work->data);
1637 area += qd->w * qd->h;
1644 static gboolean rt_queue_schedule_next_draw(RendererTiles *rt, gboolean force_set)
1646 PixbufRenderer *pr = rt->pr;
1648 gint visible_area = pr->vis_width * pr->vis_height;
1653 DEBUG_2("redraw priority: 2pass");
1654 rt->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, rt_queue_draw_idle_cb, rt, nullptr);
1655 return G_SOURCE_REMOVE;
1658 if (visible_area == 0)
1665 percent = 100.0 * rt_get_queued_area(rt->draw_queue) / visible_area;
1670 /* we have enough data for starting intensive redrawing */
1671 DEBUG_2("redraw priority: high %.2f %%", percent);
1672 rt->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW, rt_queue_draw_idle_cb, rt, nullptr);
1673 return G_SOURCE_REMOVE;
1676 if (percent < 1.0 || force_set)
1678 /* queue is (almost) empty, wait 50 ms*/
1679 DEBUG_2("redraw priority: wait %.2f %%", percent);
1680 rt->draw_idle_id = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, 50, rt_queue_draw_idle_cb, rt, nullptr);
1681 return G_SOURCE_REMOVE;
1684 /* keep the same priority as before */
1685 DEBUG_2("redraw priority: no change %.2f %%", percent);
1686 return G_SOURCE_CONTINUE;
1690 static gboolean rt_queue_draw_idle_cb(gpointer data)
1692 auto rt = static_cast<RendererTiles *>(data);
1693 PixbufRenderer *pr = rt->pr;
1698 if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
1699 (!rt->draw_queue && !rt->draw_queue_2pass) ||
1702 pr_render_complete_signal(pr);
1704 rt->draw_idle_id = 0;
1705 return G_SOURCE_REMOVE;
1710 qd = static_cast<QueueData *>(rt->draw_queue->data);
1711 fast = (pr->zoom_2pass && ((pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0) || pr->post_process_slow));
1717 /* still loading, wait till done (also drops the higher priority) */
1719 return rt_queue_schedule_next_draw(rt, FALSE);
1722 qd = static_cast<QueueData *>(rt->draw_queue_2pass->data);
1726 if (gtk_widget_get_realized(GTK_WIDGET(pr)))
1728 if (rt_tile_is_visible(rt, qd->it))
1730 rt_tile_expose(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1732 else if (qd->new_data)
1734 /* if new pixel data, and we already have a pixmap, update the tile */
1735 qd->it->blank = FALSE;
1736 if (qd->it->surface && qd->it->render_done == TILE_RENDER_ALL)
1738 rt_tile_render(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1745 qd->it->qd = nullptr;
1746 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
1751 rt_queue_merge(qd->it->qd2, qd);
1757 rt->draw_queue_2pass = g_list_append(rt->draw_queue_2pass, qd);
1767 qd->it->qd2 = nullptr;
1768 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
1772 if (!rt->draw_queue && !rt->draw_queue_2pass)
1774 pr_render_complete_signal(pr);
1776 rt->draw_idle_id = 0;
1777 return G_SOURCE_REMOVE;
1780 return rt_queue_schedule_next_draw(rt, FALSE);
1783 static void rt_queue_data_free(gpointer data)
1785 auto *qd = static_cast<QueueData *>(data);
1787 qd->it->qd = nullptr;
1788 qd->it->qd2 = nullptr;
1792 static void rt_queue_clear(RendererTiles *rt)
1794 g_list_free_full(rt->draw_queue, rt_queue_data_free);
1795 rt->draw_queue = nullptr;
1797 g_list_free_full(rt->draw_queue_2pass, rt_queue_data_free);
1798 rt->draw_queue_2pass = nullptr;
1800 if (rt->draw_idle_id)
1802 g_source_remove(rt->draw_idle_id);
1803 rt->draw_idle_id = 0;
1808 static void rt_queue_merge(QueueData *parent, QueueData *qd)
1810 if (parent->x + parent->w < qd->x + qd->w)
1812 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
1814 if (parent->x > qd->x)
1816 parent->w += parent->x - qd->x;
1820 if (parent->y + parent->h < qd->y + qd->h)
1822 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
1824 if (parent->y > qd->y)
1826 parent->h += parent->y - qd->y;
1830 parent->new_data |= qd->new_data;
1833 static gboolean rt_clamp_to_visible(RendererTiles *rt, gint *x, gint *y, gint *w, gint *h)
1835 PixbufRenderer *pr = rt->pr;
1846 vh = pr->vis_height;
1851 if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
1854 nx = CLAMP(*x, vx, vx + vw);
1855 nw = CLAMP(*w - (nx - *x), 1, vw);
1857 ny = CLAMP(*y, vy, vy + vh);
1858 nh = CLAMP(*h - (ny - *y), 1, vh);
1868 static gboolean rt_queue_to_tiles(RendererTiles *rt, gint x, gint y, gint w, gint h,
1869 gboolean clamp, ImageRenderType render,
1870 gboolean new_data, gboolean only_existing)
1872 PixbufRenderer *pr = rt->pr;
1880 if (clamp && !rt_clamp_to_visible(rt, &x, &y, &w, &h)) return FALSE;
1882 x1 = ROUND_DOWN(x, rt->tile_width);
1883 x2 = ROUND_UP(x + w, rt->tile_width);
1885 y1 = ROUND_DOWN(y, rt->tile_height);
1886 y2 = ROUND_UP(y + h, rt->tile_height);
1888 for (j = y1; j <= y2; j += rt->tile_height)
1890 for (i = x1; i <= x2; i += rt->tile_width)
1894 it = rt_tile_get(rt, i, j,
1896 (i + rt->tile_width < rt->x_scroll ||
1897 i > rt->x_scroll + pr->vis_width ||
1898 j + rt->tile_height < rt->y_scroll ||
1899 j > rt->y_scroll + pr->vis_height)));
1902 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
1903 (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
1905 it->render_todo = render;
1908 auto qd = g_new(QueueData, 1);
1910 qd->new_data = new_data;
1920 qd->w = x + w - i - qd->x;
1921 if (qd->x + qd->w > rt->tile_width) qd->w = rt->tile_width - qd->x;
1931 qd->h = y + h - j - qd->y;
1932 if (qd->y + qd->h > rt->tile_height) qd->h = rt->tile_height - qd->y;
1934 if (qd->w < 1 || qd->h < 1)
1940 rt_queue_merge(it->qd, qd);
1946 rt->draw_queue = g_list_append(rt->draw_queue, qd);
1955 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
1956 gboolean clamp, ImageRenderType render,
1957 gboolean new_data, gboolean only_existing)
1959 PixbufRenderer *pr = rt->pr;
1965 nx = CLAMP(x, 0, pr->width - 1);
1966 ny = CLAMP(y, 0, pr->height - 1);
1969 w = CLAMP(w, 0, pr->width - nx);
1970 h = CLAMP(h, 0, pr->height - ny);
1971 if (w < 1 || h < 1) return;
1973 if (rt_queue_to_tiles(rt, nx, ny, w, h, clamp, render, new_data, only_existing) &&
1974 ((!rt->draw_queue && !rt->draw_queue_2pass) || !rt->draw_idle_id))
1976 if (rt->draw_idle_id)
1978 g_source_remove(rt->draw_idle_id);
1979 rt->draw_idle_id = 0;
1981 rt_queue_schedule_next_draw(rt, TRUE);
1985 static void rt_scroll(void *renderer, gint x_off, gint y_off)
1987 auto rt = static_cast<RendererTiles *>(renderer);
1988 PixbufRenderer *pr = rt->pr;
1991 if (rt->stereo_mode & PR_STEREO_MIRROR) x_off = -x_off;
1992 if (rt->stereo_mode & PR_STEREO_FLIP) y_off = -y_off;
1994 gint w = pr->vis_width - abs(x_off);
1995 gint h = pr->vis_height - abs(y_off);
1999 /* scrolled completely to new material */
2000 rt_queue(rt, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2009 cairo_surface_t *surface;
2033 cr = cairo_create(rt->surface);
2034 surface = rt->surface;
2036 /* clipping restricts the intermediate surface's size, so it's a good idea
2038 cairo_rectangle(cr, x1 + pr->x_offset + rt->stereo_off_x, y1 + pr->y_offset + rt->stereo_off_y, w, h);
2040 /* Now push a group to change the target */
2041 cairo_push_group (cr);
2042 cairo_set_source_surface(cr, surface, x1 - x2, y1 - y2);
2044 /* Now copy the intermediate target back */
2045 cairo_pop_group_to_source(cr);
2049 rt_overlay_queue_all(rt, x2, y2, x1, y1);
2051 w = pr->vis_width - w;
2052 h = pr->vis_height - h;
2057 x_off > 0 ? rt->x_scroll + (pr->vis_width - w) : rt->x_scroll, rt->y_scroll,
2058 w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2062 /** @FIXME to optimize this, remove overlap */
2064 rt->x_scroll, y_off > 0 ? rt->y_scroll + (pr->vis_height - h) : rt->y_scroll,
2065 pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2069 static void renderer_area_changed(void *renderer, gint src_x, gint src_y, gint src_w, gint src_h)
2071 auto rt = static_cast<RendererTiles *>(renderer);
2072 PixbufRenderer *pr = rt->pr;
2082 gint orientation = rt_get_orientation(rt);
2083 pr_coords_map_orientation_reverse(orientation,
2084 src_x - get_right_pixbuf_offset(rt), src_y,
2085 pr->image_width, pr->image_height,
2090 if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
2092 /* increase region when using a zoom quality that may access surrounding pixels */
2097 x1 = static_cast<gint>(floor(static_cast<gdouble>(x) * pr->scale));
2098 y1 = static_cast<gint>(floor(static_cast<gdouble>(y) * pr->scale * pr->aspect_ratio));
2099 x2 = static_cast<gint>(ceil(static_cast<gdouble>(x + width) * pr->scale));
2100 y2 = static_cast<gint>(ceil(static_cast<gdouble>(y + height) * pr->scale * pr->aspect_ratio));
2102 rt_queue(rt, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
2105 static void renderer_redraw(RendererTiles *rt, gint x, gint y, gint w, gint h,
2106 gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing)
2108 PixbufRenderer *pr = rt->pr;
2110 x -= rt->stereo_off_x;
2111 y -= rt->stereo_off_y;
2113 rt_border_draw(rt, x, y, w, h);
2115 x = MAX(0, x - pr->x_offset + pr->x_scroll);
2116 y = MAX(0, y - pr->y_offset + pr->y_scroll);
2120 MIN(w, pr->width - x),
2121 MIN(h, pr->height - y),
2122 clamp, render, new_data, only_existing);
2125 static void renderer_update_pixbuf(void *renderer, gboolean)
2127 rt_queue_clear(static_cast<RendererTiles *>(renderer));
2130 static void renderer_update_zoom(void *renderer, gboolean lazy)
2132 auto rt = static_cast<RendererTiles *>(renderer);
2133 PixbufRenderer *pr = rt->pr;
2135 rt_tile_invalidate_all(static_cast<RendererTiles *>(renderer));
2138 renderer_redraw(static_cast<RendererTiles *>(renderer), 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, TRUE, FALSE);
2140 rt_border_clear(rt);
2143 static void renderer_invalidate_region(void *renderer, gint x, gint y, gint w, gint h)
2145 rt_tile_invalidate_region(static_cast<RendererTiles *>(renderer), x, y, w, h);
2148 static void renderer_update_viewport(void *renderer)
2150 auto rt = static_cast<RendererTiles *>(renderer);
2152 rt->stereo_off_x = 0;
2153 rt->stereo_off_y = 0;
2155 if (rt->stereo_mode & PR_STEREO_RIGHT)
2157 if (rt->stereo_mode & PR_STEREO_HORIZ)
2159 rt->stereo_off_x = rt->pr->viewport_width;
2161 else if (rt->stereo_mode & PR_STEREO_VERT)
2163 rt->stereo_off_y = rt->pr->viewport_height;
2165 else if (rt->stereo_mode & PR_STEREO_FIXED)
2167 rt->stereo_off_x = rt->pr->stereo_fixed_x_right;
2168 rt->stereo_off_y = rt->pr->stereo_fixed_y_right;
2173 if (rt->stereo_mode & PR_STEREO_FIXED)
2175 rt->stereo_off_x = rt->pr->stereo_fixed_x_left;
2176 rt->stereo_off_y = rt->pr->stereo_fixed_y_left;
2179 DEBUG_1("update size: %p %d %d %d %d", (void *)rt, rt->stereo_off_x, rt->stereo_off_y, rt->pr->viewport_width, rt->pr->viewport_height);
2181 rt_overlay_update_sizes(rt);
2182 rt_border_clear(rt);
2185 static void renderer_stereo_set(void *renderer, gint stereo_mode)
2187 auto rt = static_cast<RendererTiles *>(renderer);
2189 rt->stereo_mode = stereo_mode;
2192 static void renderer_free(void *renderer)
2194 auto rt = static_cast<RendererTiles *>(renderer);
2196 rt_tile_free_all(rt);
2197 if (rt->spare_tile) g_object_unref(rt->spare_tile);
2198 if (rt->overlay_buffer) g_object_unref(rt->overlay_buffer);
2199 rt_overlay_list_clear(rt);
2200 /* disconnect "hierarchy-changed" */
2201 g_signal_handlers_disconnect_matched(G_OBJECT(rt->pr), G_SIGNAL_MATCH_DATA,
2202 0, 0, nullptr, nullptr, rt);
2206 static gboolean rt_realize_cb(GtkWidget *widget, gpointer data)
2208 auto rt = static_cast<RendererTiles *>(data);
2213 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));
2215 cr = cairo_create(rt->surface);
2216 cairo_set_source_rgb(cr, static_cast<gdouble>(rt->pr->color.red), static_cast<gdouble>(rt->pr->color.green), static_cast<gdouble>(rt->pr->color.blue));
2224 static gboolean rt_size_allocate_cb(GtkWidget *widget, GdkRectangle *allocation, gpointer data)
2226 auto rt = static_cast<RendererTiles *>(data);
2228 cairo_surface_t *old_surface;
2230 if (gtk_widget_get_realized(GTK_WIDGET(rt->pr)))
2232 old_surface = rt->surface;
2233 rt->surface = gdk_window_create_similar_surface(gtk_widget_get_window(widget), CAIRO_CONTENT_COLOR, allocation->width, allocation->height);
2235 cr = cairo_create(rt->surface);
2237 cairo_set_source_rgb(cr, static_cast<gdouble>(options->image.border_color.red), static_cast<gdouble>(options->image.border_color.green), static_cast<gdouble>(options->image.border_color.blue));
2239 cairo_set_source_surface(cr, old_surface, 0, 0);
2242 cairo_surface_destroy(old_surface);
2244 renderer_redraw(rt, allocation->x, allocation->y, allocation->width, allocation->height, FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2250 static gboolean rt_draw_cb(GtkWidget *, cairo_t *cr, gpointer data)
2252 auto rt = static_cast<RendererTiles *>(data);
2256 if (rt->stereo_mode & (PR_STEREO_HORIZ | PR_STEREO_VERT))
2258 cairo_push_group(cr);
2259 cairo_set_source_rgb(cr, static_cast<double>(rt->pr->color.red), static_cast<double>(rt->pr->color.green), static_cast<double>(rt->pr->color.blue));
2261 if (rt->stereo_mode & PR_STEREO_HORIZ)
2263 cairo_rectangle(cr, rt->stereo_off_x, 0, rt->pr->viewport_width, rt->pr->viewport_height);
2267 cairo_rectangle(cr, 0, rt->stereo_off_y, rt->pr->viewport_width, rt->pr->viewport_height);
2272 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);
2274 cairo_set_source_surface(cr, rt->surface, 0, 0);
2277 cairo_pop_group_to_source(cr);
2282 cairo_set_source_surface(cr, rt->surface, 0, 0);
2286 work = rt->overlay_list;
2289 od = static_cast<OverlayData *>(work->data);
2294 pw = gdk_pixbuf_get_width(od->pixbuf);
2295 ph = gdk_pixbuf_get_height(od->pixbuf);
2299 if (od->flags & OVL_RELATIVE)
2301 if (px < 0) px = rt->pr->viewport_width - pw + px;
2302 if (py < 0) py = rt->pr->viewport_height - ph + py;
2305 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px, py);
2313 RendererFuncs *renderer_tiles_new(PixbufRenderer *pr)
2315 auto rt = g_new0(RendererTiles, 1);
2319 rt->f.area_changed = renderer_area_changed;
2320 rt->f.update_pixbuf = renderer_update_pixbuf;
2321 rt->f.free = renderer_free;
2322 rt->f.update_zoom = renderer_update_zoom;
2323 rt->f.invalidate_region = renderer_invalidate_region;
2324 rt->f.scroll = rt_scroll;
2325 rt->f.update_viewport = renderer_update_viewport;
2328 rt->f.overlay_add = renderer_tiles_overlay_add;
2329 rt->f.overlay_set = renderer_tiles_overlay_set;
2330 rt->f.overlay_get = renderer_tiles_overlay_get;
2332 rt->f.stereo_set = renderer_stereo_set;
2334 rt->tile_width = options->image.tile_size;
2335 rt->tile_height = options->image.tile_size;
2337 rt->tiles = nullptr;
2338 rt->tile_cache_size = 0;
2340 rt->tile_cache_max = PR_CACHE_SIZE_DEFAULT;
2342 rt->draw_idle_id = 0;
2344 rt->stereo_mode = 0;
2345 rt->stereo_off_x = 0;
2346 rt->stereo_off_y = 0;
2348 rt->hidpi_scale = gtk_widget_get_scale_factor(GTK_WIDGET(rt->pr));
2350 g_signal_connect(G_OBJECT(pr), "hierarchy-changed",
2351 G_CALLBACK(rt_hierarchy_changed_cb), rt);
2353 g_signal_connect(G_OBJECT(pr), "draw",
2354 G_CALLBACK(rt_draw_cb), rt);
2355 g_signal_connect(G_OBJECT(pr), "realize", G_CALLBACK(rt_realize_cb), rt);
2356 g_signal_connect(G_OBJECT(pr), "size-allocate", G_CALLBACK(rt_size_allocate_cb), rt);
2358 return reinterpret_cast<RendererFuncs *>(rt);
2362 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */