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 auto draw = [rt, pr, cr](gint x, gint y, gint w, gint h)
218 cairo_set_source_rgb(cr, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
219 cairo_rectangle(cr, x + rt->stereo_off_x, y + rt->stereo_off_y, w, h);
221 rt_overlay_draw(rt, x, y, w, h, nullptr);
224 if (!pr->pixbuf && !pr->source_tiles_enabled)
226 if (pr_clip_region(x, y, w, h,
228 pr->viewport_width, pr->viewport_height,
231 draw(rx, ry, rw, rh);
237 if (pr->vis_width < pr->viewport_width)
239 if (pr->x_offset > 0 &&
240 pr_clip_region(x, y, w, h,
242 pr->x_offset, pr->viewport_height,
245 draw(rx, ry, rw, rh);
247 if (pr->viewport_width - pr->vis_width - pr->x_offset > 0 &&
248 pr_clip_region(x, y, w, h,
249 pr->x_offset + pr->vis_width, 0,
250 pr->viewport_width - pr->vis_width - pr->x_offset, pr->viewport_height,
253 draw(rx, ry, rw, rh);
256 if (pr->vis_height < pr->viewport_height)
258 if (pr->y_offset > 0 &&
259 pr_clip_region(x, y, w, h,
261 pr->vis_width, pr->y_offset,
264 draw(rx, ry, rw, rh);
266 if (pr->viewport_height - pr->vis_height - pr->y_offset > 0 &&
267 pr_clip_region(x, y, w, h,
268 pr->x_offset, pr->y_offset + pr->vis_height,
269 pr->vis_width, pr->viewport_height - pr->vis_height - pr->y_offset,
272 draw(rx, ry, rw, rh);
278 static void rt_border_clear(RendererTiles *rt)
280 PixbufRenderer *pr = rt->pr;
281 rt_border_draw(rt, 0, 0, pr->viewport_width, pr->viewport_height);
286 *-------------------------------------------------------------------
288 *-------------------------------------------------------------------
291 static ImageTile *rt_tile_new(gint x, gint y, gint width, gint height)
293 auto it = g_new0(ImageTile, 1);
300 it->render_done = TILE_RENDER_NONE;
305 static void rt_tile_free(ImageTile *it)
309 if (it->pixbuf) g_object_unref(it->pixbuf);
310 if (it->surface) cairo_surface_destroy(it->surface);
315 static void rt_tile_free_all(RendererTiles *rt)
317 g_list_free_full(rt->tiles, reinterpret_cast<GDestroyNotify>(rt_tile_free));
319 rt->tile_cache_size = 0;
322 static ImageTile *rt_tile_add(RendererTiles *rt, gint x, gint y)
324 PixbufRenderer *pr = rt->pr;
327 it = rt_tile_new(x, y, rt->tile_width, rt->tile_height);
329 if (it->x + it->w > pr->width) it->w = pr->width - it->x;
330 if (it->y + it->h > pr->height) it->h = pr->height - it->y;
332 rt->tiles = g_list_prepend(rt->tiles, it);
333 rt->tile_cache_size += it->size;
338 static void rt_tile_remove(RendererTiles *rt, ImageTile *it)
342 QueueData *qd = it->qd;
345 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
351 QueueData *qd = it->qd2;
354 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
358 rt->tiles = g_list_remove(rt->tiles, it);
359 rt->tile_cache_size -= it->size;
364 static void rt_tile_free_space(RendererTiles *rt, guint space, ImageTile *it)
366 PixbufRenderer *pr = rt->pr;
370 work = g_list_last(rt->tiles);
372 if (pr->source_tiles_enabled && pr->scale < 1.0)
376 tiles = (pr->vis_width / rt->tile_width + 1) * (pr->vis_height / rt->tile_height + 1);
377 tile_max = MAX(tiles * rt->tile_width * rt->tile_height * 3,
378 (gint)((gdouble)rt->tile_cache_max * 1048576.0 * pr->scale));
382 tile_max = rt->tile_cache_max * 1048576;
385 while (work && rt->tile_cache_size + space > tile_max)
389 needle = static_cast<ImageTile *>(work->data);
392 ((!needle->qd && !needle->qd2) || !rt_tile_is_visible(rt, needle))) rt_tile_remove(rt, needle);
396 static void rt_tile_invalidate_all(RendererTiles *rt)
398 PixbufRenderer *pr = rt->pr;
406 it = static_cast<ImageTile *>(work->data);
409 it->render_done = TILE_RENDER_NONE;
410 it->render_todo = TILE_RENDER_ALL;
413 it->w = MIN(rt->tile_width, pr->width - it->x);
414 it->h = MIN(rt->tile_height, pr->height - it->y);
418 static void rt_tile_invalidate_region(RendererTiles *rt, gint x, gint y, gint w, gint h)
426 x1 = ROUND_DOWN(x, rt->tile_width);
427 x2 = ROUND_UP(x + w, rt->tile_width);
429 y1 = ROUND_DOWN(y, rt->tile_height);
430 y2 = ROUND_UP(y + h, rt->tile_height);
437 it = static_cast<ImageTile *>(work->data);
440 if (it->x < x2 && it->x + it->w > x1 &&
441 it->y < y2 && it->y + it->h > y1)
443 it->render_done = TILE_RENDER_NONE;
444 it->render_todo = TILE_RENDER_ALL;
449 static ImageTile *rt_tile_get(RendererTiles *rt, gint x, gint y, gboolean only_existing)
458 it = static_cast<ImageTile *>(work->data);
459 if (it->x == x && it->y == y)
461 rt->tiles = g_list_delete_link(rt->tiles, work);
462 rt->tiles = g_list_prepend(rt->tiles, it);
469 if (only_existing) return nullptr;
471 return rt_tile_add(rt, x, y);
474 static gint pixmap_calc_size(cairo_surface_t *)
476 return options->image.tile_size * options->image.tile_size * 4 / 8;
479 static void rt_hidpi_aware_draw(
486 cairo_surface_t *surface;
487 surface = gdk_cairo_surface_create_from_pixbuf(pixbuf, rt->hidpi_scale, nullptr);
488 cairo_set_source_surface(cr, surface, x, y);
490 cairo_surface_destroy(surface);
493 static void rt_tile_prepare(RendererTiles *rt, ImageTile *it)
495 PixbufRenderer *pr = rt->pr;
498 cairo_surface_t *surface;
501 surface = gdk_window_create_similar_surface(gtk_widget_get_window(reinterpret_cast<GtkWidget *>(pr)),
503 rt->tile_width, rt->tile_height);
505 size = pixmap_calc_size(surface) * rt->hidpi_scale * rt->hidpi_scale;
506 rt_tile_free_space(rt, size, it);
508 it->surface = surface;
510 rt->tile_cache_size += size;
517 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, rt->hidpi_scale * rt->tile_width, rt->hidpi_scale * rt->tile_height);
519 size = gdk_pixbuf_get_rowstride(pixbuf) * rt->tile_height * rt->hidpi_scale;
520 rt_tile_free_space(rt, size, it);
524 rt->tile_cache_size += size;
529 *-------------------------------------------------------------------
531 *-------------------------------------------------------------------
534 static void rt_overlay_get_position(const RendererTiles *rt, const OverlayData *od,
535 gint &x, gint &y, gint &w, gint &h)
537 PixbufRenderer *pr = rt->pr;
541 w = gdk_pixbuf_get_width(od->pixbuf);
542 h = gdk_pixbuf_get_height(od->pixbuf);
544 if (od->flags & OVL_RELATIVE)
546 if (x < 0) x = pr->viewport_width - w + x;
547 if (y < 0) y = pr->viewport_height - h + y;
551 static void rt_overlay_init_window(RendererTiles *rt, OverlayData *od)
553 PixbufRenderer *pr = rt->pr;
558 GdkWindowAttr attributes;
559 gint attributes_mask;
561 rt_overlay_get_position(rt, od, px, py, pw, ph);
563 attributes.window_type = GDK_WINDOW_CHILD;
564 attributes.wclass = GDK_INPUT_OUTPUT;
565 attributes.width = pw;
566 attributes.height = ph;
567 attributes.event_mask = GDK_EXPOSURE_MASK;
570 od->window = gdk_window_new(gtk_widget_get_window(GTK_WIDGET(pr)), &attributes, attributes_mask);
571 gdk_window_set_user_data(od->window, pr);
572 gdk_window_move(od->window, px + rt->stereo_off_x, py + rt->stereo_off_y);
573 gdk_window_show(od->window);
576 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h,
579 PixbufRenderer *pr = rt->pr;
582 work = rt->overlay_list;
595 od = static_cast<OverlayData *>(work->data);
598 if (!od->window) rt_overlay_init_window(rt, od);
600 rt_overlay_get_position(rt, od, px, py, pw, ph);
601 if (pr_clip_region(x, y, w, h, px, py, pw, ph, &rx, &ry, &rw, &rh))
603 if (!rt->overlay_buffer)
605 rt->overlay_buffer = gdk_window_create_similar_surface(gtk_widget_get_window(reinterpret_cast<GtkWidget *>(pr)),
607 rt->tile_width, rt->tile_height);
614 cr = cairo_create(rt->overlay_buffer);
615 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);
616 cairo_rectangle(cr, 0, 0, rw, rh);
617 cairo_fill_preserve(cr);
619 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - rx, py - ry);
623 cr = gdk_cairo_create(od->window);
624 cairo_set_source_surface(cr, rt->overlay_buffer, rx - px, ry - py);
625 cairo_rectangle (cr, rx - px, ry - py, rw, rh);
631 /* no ImageTile means region may be larger than our scratch buffer */
635 for (sx = rx; sx < rx + rw; sx += rt->tile_width)
636 for (sy = ry; sy < ry + rh; sy += rt->tile_height)
642 sw = MIN(rx + rw - sx, rt->tile_width);
643 sh = MIN(ry + rh - sy, rt->tile_height);
645 cr = cairo_create(rt->overlay_buffer);
646 cairo_set_source_rgb(cr, 0, 0, 0);
647 cairo_rectangle(cr, 0, 0, sw, sh);
648 cairo_fill_preserve(cr);
650 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - sx, py - sy);
654 cr = gdk_cairo_create(od->window);
655 cairo_set_source_surface(cr, rt->overlay_buffer, sx - px, sy - py);
656 cairo_rectangle (cr, sx - px, sy - py, sw, sh);
665 static void rt_overlay_queue_draw(RendererTiles *rt, OverlayData *od, gint x1, gint y1, gint x2, gint y2)
667 PixbufRenderer *pr = rt->pr;
673 rt_overlay_get_position(rt, od, x, y, w, h);
681 rt_queue(rt, rt->x_scroll - pr->x_offset + x,
682 rt->y_scroll - pr->y_offset + y,
684 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
686 rt_border_draw(rt, x, y, w, h);
689 static void rt_overlay_queue_all(RendererTiles *rt, gint x1, gint y1, gint x2, gint y2)
693 work = rt->overlay_list;
696 auto od = static_cast<OverlayData *>(work->data);
699 rt_overlay_queue_draw(rt, od, x1, y1, x2, y2);
703 static void rt_overlay_update_sizes(RendererTiles *rt)
707 work = rt->overlay_list;
710 auto od = static_cast<OverlayData *>(work->data);
713 if (!od->window) rt_overlay_init_window(rt, od);
715 if (od->flags & OVL_RELATIVE)
722 rt_overlay_get_position(rt, od, x, y, w, h);
723 gdk_window_move_resize(od->window, x + rt->stereo_off_x, y + rt->stereo_off_y, w, h);
728 static OverlayData *rt_overlay_find(RendererTiles *rt, gint id)
732 work = rt->overlay_list;
735 auto od = static_cast<OverlayData *>(work->data);
738 if (od->id == id) return od;
745 gint renderer_tiles_overlay_add(void *renderer, GdkPixbuf *pixbuf, gint x, gint y,
746 OverlayRendererFlags flags)
748 auto rt = static_cast<RendererTiles *>(renderer);
749 PixbufRenderer *pr = rt->pr;
752 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
753 g_return_val_if_fail(pixbuf != nullptr, -1);
756 while (rt_overlay_find(rt, id)) id++;
758 auto od = g_new0(OverlayData, 1);
761 g_object_ref(G_OBJECT(od->pixbuf));
766 rt_overlay_init_window(rt, od);
768 rt->overlay_list = g_list_append(rt->overlay_list, od);
770 gtk_widget_queue_draw(GTK_WIDGET(rt->pr));
775 static void rt_overlay_free(RendererTiles *rt, OverlayData *od)
777 rt->overlay_list = g_list_remove(rt->overlay_list, od);
779 if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
780 if (od->window) gdk_window_destroy(od->window);
783 if (!rt->overlay_list && rt->overlay_buffer)
785 cairo_surface_destroy(rt->overlay_buffer);
786 rt->overlay_buffer = nullptr;
790 static void rt_overlay_list_clear(RendererTiles *rt)
792 while (rt->overlay_list)
794 auto od = static_cast<OverlayData *>(rt->overlay_list->data);
795 rt_overlay_free(rt, od);
799 static void rt_overlay_list_reset_window(RendererTiles *rt)
803 if (rt->overlay_buffer) cairo_surface_destroy(rt->overlay_buffer);
804 rt->overlay_buffer = nullptr;
806 work = rt->overlay_list;
809 auto od = static_cast<OverlayData *>(work->data);
811 if (od->window) gdk_window_destroy(od->window);
812 od->window = nullptr;
816 void renderer_tiles_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint, gint)
818 auto rc = static_cast<RendererTiles *>(renderer);
819 PixbufRenderer *pr = rc->pr;
822 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
824 od = rt_overlay_find(rc, id);
829 g_object_ref(G_OBJECT(pixbuf));
830 g_object_unref(G_OBJECT(od->pixbuf));
835 rt_overlay_free(rc, od);
838 gtk_widget_queue_draw(GTK_WIDGET(rc->pr));
841 gboolean renderer_tiles_overlay_get(void *renderer, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
843 auto rt = static_cast<RendererTiles *>(renderer);
844 PixbufRenderer *pr = rt->pr;
847 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
849 od = rt_overlay_find(rt, id);
850 if (!od) return FALSE;
852 if (pixbuf) *pixbuf = od->pixbuf;
859 static void rt_hierarchy_changed_cb(GtkWidget *, GtkWidget *, gpointer data)
861 auto rt = static_cast<RendererTiles *>(data);
862 rt_overlay_list_reset_window(rt);
866 *-------------------------------------------------------------------
868 *-------------------------------------------------------------------
871 static GdkPixbuf *rt_get_spare_tile(RendererTiles *rt)
873 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);
874 return rt->spare_tile;
877 static void rt_tile_rotate_90_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
879 GdkPixbuf *src = *tile;
892 gint tw = rt->tile_width * rt->hidpi_scale;
894 srs = gdk_pixbuf_get_rowstride(src);
895 s_pix = gdk_pixbuf_get_pixels(src);
896 spi = s_pix + (x * COLOR_BYTES);
898 dest = rt_get_spare_tile(rt);
899 drs = gdk_pixbuf_get_rowstride(dest);
900 d_pix = gdk_pixbuf_get_pixels(dest);
901 dpi = d_pix + (tw - 1) * COLOR_BYTES;
903 for (i = y; i < y + h; i++)
905 sp = spi + (i * srs);
906 ip = dpi - (i * COLOR_BYTES);
907 for (j = x; j < x + w; j++)
910 memcpy(dp, sp, COLOR_BYTES);
915 rt->spare_tile = src;
919 static void rt_tile_rotate_90_counter_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
921 GdkPixbuf *src = *tile;
934 gint th = rt->tile_height * rt->hidpi_scale;
936 srs = gdk_pixbuf_get_rowstride(src);
937 s_pix = gdk_pixbuf_get_pixels(src);
938 spi = s_pix + (x * COLOR_BYTES);
940 dest = rt_get_spare_tile(rt);
941 drs = gdk_pixbuf_get_rowstride(dest);
942 d_pix = gdk_pixbuf_get_pixels(dest);
943 dpi = d_pix + (th - 1) * drs;
945 for (i = y; i < y + h; i++)
947 sp = spi + (i * srs);
948 ip = dpi + (i * COLOR_BYTES);
949 for (j = x; j < x + w; j++)
952 memcpy(dp, sp, COLOR_BYTES);
957 rt->spare_tile = src;
961 static void rt_tile_mirror_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
963 GdkPixbuf *src = *tile;
976 gint tw = rt->tile_width * rt->hidpi_scale;
978 srs = gdk_pixbuf_get_rowstride(src);
979 s_pix = gdk_pixbuf_get_pixels(src);
980 spi = s_pix + (x * COLOR_BYTES);
982 dest = rt_get_spare_tile(rt);
983 drs = gdk_pixbuf_get_rowstride(dest);
984 d_pix = gdk_pixbuf_get_pixels(dest);
985 dpi = d_pix + (tw - x - 1) * COLOR_BYTES;
987 for (i = y; i < y + h; i++)
989 sp = spi + (i * srs);
990 dp = dpi + (i * drs);
991 for (j = 0; j < w; j++)
993 memcpy(dp, sp, COLOR_BYTES);
999 rt->spare_tile = src;
1003 static void rt_tile_mirror_and_flip(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1005 GdkPixbuf *src = *tile;
1016 gint tw = rt->tile_width * rt->hidpi_scale;
1017 gint th = rt->tile_height * rt->hidpi_scale;
1019 srs = gdk_pixbuf_get_rowstride(src);
1020 s_pix = gdk_pixbuf_get_pixels(src);
1022 dest = rt_get_spare_tile(rt);
1023 drs = gdk_pixbuf_get_rowstride(dest);
1024 d_pix = gdk_pixbuf_get_pixels(dest);
1025 dpi = d_pix + (th - 1) * drs + (tw - 1) * COLOR_BYTES;
1027 for (i = y; i < y + h; i++)
1029 sp = s_pix + (i * srs) + (x * COLOR_BYTES);
1030 dp = dpi - (i * drs) - (x * COLOR_BYTES);
1031 for (j = 0; j < w; j++)
1033 memcpy(dp, sp, COLOR_BYTES);
1039 rt->spare_tile = src;
1043 static void rt_tile_flip_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1045 GdkPixbuf *src = *tile;
1056 gint th = rt->tile_height * rt->hidpi_scale;
1058 srs = gdk_pixbuf_get_rowstride(src);
1059 s_pix = gdk_pixbuf_get_pixels(src);
1060 spi = s_pix + (x * COLOR_BYTES);
1062 dest = rt_get_spare_tile(rt);
1063 drs = gdk_pixbuf_get_rowstride(dest);
1064 d_pix = gdk_pixbuf_get_pixels(dest);
1065 dpi = d_pix + (th - 1) * drs + (x * COLOR_BYTES);
1067 for (i = y; i < y + h; i++)
1069 sp = spi + (i * srs);
1070 dp = dpi - (i * drs);
1071 memcpy(dp, sp, w * COLOR_BYTES);
1074 rt->spare_tile = src;
1078 static void rt_tile_apply_orientation(RendererTiles *rt, gint orientation, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h)
1080 switch (orientation)
1082 case EXIF_ORIENTATION_TOP_LEFT:
1083 /* normal -- nothing to do */
1085 case EXIF_ORIENTATION_TOP_RIGHT:
1088 rt_tile_mirror_only(rt, pixbuf, x, y, w, h);
1091 case EXIF_ORIENTATION_BOTTOM_RIGHT:
1094 rt_tile_mirror_and_flip(rt, pixbuf, x, y, w, h);
1097 case EXIF_ORIENTATION_BOTTOM_LEFT:
1100 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1103 case EXIF_ORIENTATION_LEFT_TOP:
1105 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1106 rt_tile_rotate_90_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1109 case EXIF_ORIENTATION_RIGHT_TOP:
1110 /* rotated -90 (270) */
1112 rt_tile_rotate_90_clockwise(rt, pixbuf, x, y, w, h);
1115 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1117 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1118 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1121 case EXIF_ORIENTATION_LEFT_BOTTOM:
1124 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, y, w, h);
1128 /* The other values are out of range */
1133 static gboolean rt_source_tile_render(RendererTiles *rt, ImageTile *it,
1134 gint x, gint y, gint w, gint h,
1135 gboolean, gboolean fast)
1137 PixbufRenderer *pr = rt->pr;
1140 gboolean draw = FALSE;
1142 if (pr->zoom == 1.0 || pr->scale == 1.0)
1144 list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE);
1154 st = static_cast<SourceTile *>(work->data);
1157 if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1158 it->x + x, it->y + y, w, h,
1159 &rx, &ry, &rw, &rh))
1162 cr = cairo_create(it->surface);
1163 cairo_rectangle (cr, rx - it->x, ry - it->y, rw, rh);
1167 cairo_set_source_rgb(cr, 0, 0, 0);
1170 else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
1172 rt_hidpi_aware_draw(rt, cr, st->pixbuf, -it->x + st->x, -it->y + st->y);
1187 if (pr->image_width == 0 || pr->image_height == 0) return FALSE;
1188 scale_x = static_cast<gdouble>(pr->width) / pr->image_width;
1189 scale_y = static_cast<gdouble>(pr->height) / pr->image_height;
1191 sx = static_cast<gdouble>(it->x + x) / scale_x;
1192 sy = static_cast<gdouble>(it->y + y) / scale_y;
1193 sw = static_cast<gdouble>(w) / scale_x;
1194 sh = static_cast<gdouble>(h) / scale_y;
1196 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1199 /* draws red over draw region, to check for leaks (regions not filled) */
1200 pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255);
1203 list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE);
1217 st = static_cast<SourceTile *>(work->data);
1220 stx = floor(static_cast<gdouble>(st->x) * scale_x);
1221 sty = floor(static_cast<gdouble>(st->y) * scale_y);
1222 stw = ceil(static_cast<gdouble>(st->x + pr->source_tile_width) * scale_x) - stx;
1223 sth = ceil(static_cast<gdouble>(st->y + pr->source_tile_height) * scale_y) - sty;
1225 if (pr_clip_region(stx, sty, stw, sth,
1226 it->x + x, it->y + y, w, h,
1227 &rx, &ry, &rw, &rh))
1233 cr = cairo_create(it->surface);
1234 cairo_rectangle (cr, rx - st->x, ry - st->y, rw, rh);
1235 cairo_set_source_rgb(cr, 0, 0, 0);
1244 /* may need to use unfloored stx,sty values here */
1245 offset_x = static_cast<gdouble>(stx - it->x);
1246 offset_y = static_cast<gdouble>(sty - it->y);
1248 gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh,
1249 static_cast<gdouble>(0.0) + offset_x,
1250 static_cast<gdouble>(0.0) + offset_y,
1252 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1267 * @param ignore_alpha
1278 * @param interp_type
1281 * @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
1285 static void rt_tile_get_region(gboolean has_alpha, gboolean ignore_alpha,
1286 const GdkPixbuf *src, GdkPixbuf *dest,
1287 int pb_x, int pb_y, int pb_w, int pb_h,
1288 double offset_x, double offset_y, double scale_x, double scale_y,
1289 GdkInterpType interp_type,
1290 int check_x, int check_y, gboolean wide_image)
1292 GdkPixbuf* tmppixbuf;
1311 if (scale_x == 1.0 && scale_y == 1.0)
1315 srs = gdk_pixbuf_get_rowstride(src);
1316 drs = gdk_pixbuf_get_rowstride(dest);
1317 psrc = gdk_pixbuf_get_pixels(src);
1318 pdst = gdk_pixbuf_get_pixels(dest);
1319 for (y = 0; y < pb_h; y++)
1321 for (x = 0; x < pb_w; x++)
1323 for (c = 0; c < 3; c++)
1325 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];
1332 gdk_pixbuf_copy_area(src,
1333 -offset_x + pb_x, -offset_y + pb_y,
1341 gdk_pixbuf_scale(src, dest,
1342 pb_x, pb_y, pb_w, pb_h,
1346 (wide_image && interp_type == GDK_INTERP_NEAREST) ? GDK_INTERP_TILES : interp_type);
1351 red_1=static_cast<guint32>(options->image.alpha_color_1.red * 255) << 16 & 0x00FF0000;
1352 green_1=static_cast<guint32>(options->image.alpha_color_1.green * 255) << 8 & 0x0000FF00;
1353 blue_1=static_cast<guint32>(options->image.alpha_color_1.blue * 255) & 0x000000FF;
1354 red_2=static_cast<guint32>(options->image.alpha_color_2.red * 255) << 16 & 0x00FF0000;
1355 green_2=static_cast<guint32>(options->image.alpha_color_2.green * 255) << 8 & 0x0000FF00;
1356 blue_2=static_cast<guint32>(options->image.alpha_color_2.blue * 255) & 0x000000FF;
1357 alpha_1 = red_1 + green_1 + blue_1;
1358 alpha_2 = red_2 + green_2 + blue_2;
1362 tmppixbuf = gdk_pixbuf_add_alpha(src, FALSE, 0, 0, 0);
1364 pixbuf_ignore_alpha_rect(tmppixbuf, 0, 0, gdk_pixbuf_get_width(src), gdk_pixbuf_get_height(src));
1366 gdk_pixbuf_composite_color(tmppixbuf, dest,
1367 pb_x, pb_y, pb_w, pb_h,
1371 (scale_x == 1.0 && scale_y == 1.0) ? GDK_INTERP_NEAREST : interp_type,
1372 255, check_x, check_y,
1373 PR_ALPHA_CHECK_SIZE,
1376 g_object_unref(tmppixbuf);
1380 gdk_pixbuf_composite_color(src, dest,
1381 pb_x, pb_y, pb_w, pb_h,
1385 (scale_x == 1.0 && scale_y == 1.0) ? GDK_INTERP_NEAREST : interp_type,
1386 255, check_x, check_y,
1387 PR_ALPHA_CHECK_SIZE,
1395 static gint rt_get_orientation(RendererTiles *rt)
1397 PixbufRenderer *pr = rt->pr;
1399 gint orientation = pr->orientation;
1400 static const gint mirror[] = {1, 2, 1, 4, 3, 6, 5, 8, 7};
1401 static const gint flip[] = {1, 4, 3, 2, 1, 8, 7, 6, 5};
1403 if (rt->stereo_mode & PR_STEREO_MIRROR) orientation = mirror[orientation];
1404 if (rt->stereo_mode & PR_STEREO_FLIP) orientation = flip[orientation];
1409 static void rt_tile_render(RendererTiles *rt, ImageTile *it,
1410 gint x, gint y, gint w, gint h,
1411 gboolean new_data, gboolean fast)
1413 PixbufRenderer *pr = rt->pr;
1415 gboolean draw = FALSE;
1416 gint orientation = rt_get_orientation(rt);
1417 gboolean wide_image = FALSE;
1419 if (it->render_todo == TILE_RENDER_NONE && it->surface && !new_data) return;
1421 if (it->render_done != TILE_RENDER_ALL)
1427 if (!fast) it->render_done = TILE_RENDER_ALL;
1429 else if (it->render_todo != TILE_RENDER_AREA)
1431 if (!fast) it->render_todo = TILE_RENDER_NONE;
1435 if (!fast) it->render_todo = TILE_RENDER_NONE;
1437 if (new_data) it->blank = FALSE;
1439 rt_tile_prepare(rt, it);
1440 has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
1442 /** @FIXME checker colors for alpha should be configurable,
1443 * also should be drawn for blank = TRUE
1448 /* no data, do fast rect fill */
1450 cr = cairo_create(it->surface);
1451 cairo_rectangle (cr, 0, 0, it->w, it->h);
1452 cairo_set_source_rgb(cr, 0, 0, 0);
1456 else if (pr->source_tiles_enabled)
1458 draw = rt_source_tile_render(rt, it, x, y, w, h, new_data, fast);
1471 if (pr->image_width == 0 || pr->image_height == 0) return;
1473 scale_x = rt->hidpi_scale * static_cast<gdouble>(pr->width) / pr->image_width;
1474 scale_y = rt->hidpi_scale * static_cast<gdouble>(pr->height) / pr->image_height;
1476 pr_tile_coords_map_orientation(orientation, it->x, it->y,
1477 pr->width, pr->height,
1478 rt->tile_width, rt->tile_height,
1480 pr_tile_region_map_orientation(orientation, x, y,
1481 rt->tile_width, rt->tile_height,
1486 src_x *= rt->hidpi_scale;
1487 src_y *= rt->hidpi_scale;
1488 pb_x *= rt->hidpi_scale;
1489 pb_y *= rt->hidpi_scale;
1490 pb_w *= rt->hidpi_scale;
1491 pb_h *= rt->hidpi_scale;
1493 switch (orientation)
1495 case EXIF_ORIENTATION_LEFT_TOP:
1496 case EXIF_ORIENTATION_RIGHT_TOP:
1497 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1498 case EXIF_ORIENTATION_LEFT_BOTTOM:
1499 std::swap(scale_x, scale_y);
1506 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
1507 * small sizes for anything but GDK_INTERP_NEAREST
1509 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1510 if (pr->image_width > 32767) wide_image = TRUE;
1512 rt_tile_get_region(has_alpha, pr->ignore_alpha,
1513 pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
1514 static_cast<gdouble>(0.0) - src_x - get_right_pixbuf_offset(rt) * scale_x,
1515 static_cast<gdouble>(0.0) - src_y,
1517 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1518 it->x + pb_x, it->y + pb_y, wide_image);
1519 if (rt->stereo_mode & PR_STEREO_ANAGLYPH &&
1520 (pr->stereo_pixbuf_offset_right > 0 || pr->stereo_pixbuf_offset_left > 0))
1522 GdkPixbuf *right_pb = rt_get_spare_tile(rt);
1523 rt_tile_get_region(has_alpha, pr->ignore_alpha,
1524 pr->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h,
1525 static_cast<gdouble>(0.0) - src_x - get_left_pixbuf_offset(rt) * scale_x,
1526 static_cast<gdouble>(0.0) - src_y,
1528 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1529 it->x + pb_x, it->y + pb_y, wide_image);
1530 pr_create_anaglyph(rt->stereo_mode, it->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h);
1531 /* do not care about freeing spare_tile, it will be reused */
1533 rt_tile_apply_orientation(rt, orientation, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
1537 if (draw && it->pixbuf && !it->blank)
1541 if (pr->func_post_process && (!pr->post_process_slow || !fast))
1542 pr->func_post_process(pr, &it->pixbuf, x, y, w, h, pr->post_process_user_data);
1544 cr = cairo_create(it->surface);
1545 cairo_rectangle (cr, x, y, w, h);
1546 rt_hidpi_aware_draw(rt, cr, it->pixbuf, 0, 0);
1552 static void rt_tile_expose(RendererTiles *rt, ImageTile *it,
1553 gint x, gint y, gint w, gint h,
1554 gboolean new_data, gboolean fast)
1556 PixbufRenderer *pr = rt->pr;
1559 /* clamp to visible */
1560 if (it->x + x < rt->x_scroll)
1562 w -= rt->x_scroll - it->x - x;
1563 x = rt->x_scroll - it->x;
1565 if (it->x + x + w > rt->x_scroll + pr->vis_width)
1567 w = rt->x_scroll + pr->vis_width - it->x - x;
1570 if (it->y + y < rt->y_scroll)
1572 h -= rt->y_scroll - it->y - y;
1573 y = rt->y_scroll - it->y;
1575 if (it->y + y + h > rt->y_scroll + pr->vis_height)
1577 h = rt->y_scroll + pr->vis_height - it->y - y;
1581 rt_tile_render(rt, it, x, y, w, h, new_data, fast);
1583 cr = cairo_create(rt->surface);
1584 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);
1585 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);
1589 if (rt->overlay_list)
1591 rt_overlay_draw(rt, pr->x_offset + (it->x - rt->x_scroll) + x,
1592 pr->y_offset + (it->y - rt->y_scroll) + y,
1597 gtk_widget_queue_draw(GTK_WIDGET(rt->pr));
1601 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it)
1603 PixbufRenderer *pr = rt->pr;
1604 return (it->x + it->w >= rt->x_scroll && it->x < rt->x_scroll + pr->vis_width &&
1605 it->y + it->h >= rt->y_scroll && it->y < rt->y_scroll + pr->vis_height);
1609 *-------------------------------------------------------------------
1611 *-------------------------------------------------------------------
1614 static gint rt_get_queued_area(GList *work)
1620 auto qd = static_cast<QueueData *>(work->data);
1621 area += qd->w * qd->h;
1628 static gboolean rt_queue_schedule_next_draw(RendererTiles *rt, gboolean force_set)
1630 PixbufRenderer *pr = rt->pr;
1632 gint visible_area = pr->vis_width * pr->vis_height;
1637 DEBUG_2("redraw priority: 2pass");
1638 rt->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, rt_queue_draw_idle_cb, rt, nullptr);
1639 return G_SOURCE_REMOVE;
1642 if (visible_area == 0)
1649 percent = 100.0 * rt_get_queued_area(rt->draw_queue) / visible_area;
1654 /* we have enough data for starting intensive redrawing */
1655 DEBUG_2("redraw priority: high %.2f %%", percent);
1656 rt->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW, rt_queue_draw_idle_cb, rt, nullptr);
1657 return G_SOURCE_REMOVE;
1660 if (percent < 1.0 || force_set)
1662 /* queue is (almost) empty, wait 50 ms*/
1663 DEBUG_2("redraw priority: wait %.2f %%", percent);
1664 rt->draw_idle_id = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, 50, rt_queue_draw_idle_cb, rt, nullptr);
1665 return G_SOURCE_REMOVE;
1668 /* keep the same priority as before */
1669 DEBUG_2("redraw priority: no change %.2f %%", percent);
1670 return G_SOURCE_CONTINUE;
1674 static gboolean rt_queue_draw_idle_cb(gpointer data)
1676 auto rt = static_cast<RendererTiles *>(data);
1677 PixbufRenderer *pr = rt->pr;
1682 if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
1683 (!rt->draw_queue && !rt->draw_queue_2pass) ||
1686 pr_render_complete_signal(pr);
1688 rt->draw_idle_id = 0;
1689 return G_SOURCE_REMOVE;
1694 qd = static_cast<QueueData *>(rt->draw_queue->data);
1695 fast = (pr->zoom_2pass && ((pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0) || pr->post_process_slow));
1701 /* still loading, wait till done (also drops the higher priority) */
1703 return rt_queue_schedule_next_draw(rt, FALSE);
1706 qd = static_cast<QueueData *>(rt->draw_queue_2pass->data);
1710 if (gtk_widget_get_realized(GTK_WIDGET(pr)))
1712 if (rt_tile_is_visible(rt, qd->it))
1714 rt_tile_expose(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1716 else if (qd->new_data)
1718 /* if new pixel data, and we already have a pixmap, update the tile */
1719 qd->it->blank = FALSE;
1720 if (qd->it->surface && qd->it->render_done == TILE_RENDER_ALL)
1722 rt_tile_render(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1729 qd->it->qd = nullptr;
1730 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
1735 rt_queue_merge(qd->it->qd2, qd);
1741 rt->draw_queue_2pass = g_list_append(rt->draw_queue_2pass, qd);
1751 qd->it->qd2 = nullptr;
1752 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
1756 if (!rt->draw_queue && !rt->draw_queue_2pass)
1758 pr_render_complete_signal(pr);
1760 rt->draw_idle_id = 0;
1761 return G_SOURCE_REMOVE;
1764 return rt_queue_schedule_next_draw(rt, FALSE);
1767 static void rt_queue_data_free(gpointer data)
1769 auto *qd = static_cast<QueueData *>(data);
1771 qd->it->qd = nullptr;
1772 qd->it->qd2 = nullptr;
1776 static void rt_queue_clear(RendererTiles *rt)
1778 g_list_free_full(rt->draw_queue, rt_queue_data_free);
1779 rt->draw_queue = nullptr;
1781 g_list_free_full(rt->draw_queue_2pass, rt_queue_data_free);
1782 rt->draw_queue_2pass = nullptr;
1784 if (rt->draw_idle_id)
1786 g_source_remove(rt->draw_idle_id);
1787 rt->draw_idle_id = 0;
1792 static void rt_queue_merge(QueueData *parent, QueueData *qd)
1794 if (parent->x + parent->w < qd->x + qd->w)
1796 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
1798 if (parent->x > qd->x)
1800 parent->w += parent->x - qd->x;
1804 if (parent->y + parent->h < qd->y + qd->h)
1806 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
1808 if (parent->y > qd->y)
1810 parent->h += parent->y - qd->y;
1814 parent->new_data |= qd->new_data;
1817 static gboolean rt_clamp_to_visible(RendererTiles *rt, gint *x, gint *y, gint *w, gint *h)
1819 PixbufRenderer *pr = rt->pr;
1830 vh = pr->vis_height;
1835 if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
1838 nx = CLAMP(*x, vx, vx + vw);
1839 nw = CLAMP(*w - (nx - *x), 1, vw);
1841 ny = CLAMP(*y, vy, vy + vh);
1842 nh = CLAMP(*h - (ny - *y), 1, vh);
1852 static gboolean rt_queue_to_tiles(RendererTiles *rt, gint x, gint y, gint w, gint h,
1853 gboolean clamp, ImageRenderType render,
1854 gboolean new_data, gboolean only_existing)
1856 PixbufRenderer *pr = rt->pr;
1864 if (clamp && !rt_clamp_to_visible(rt, &x, &y, &w, &h)) return FALSE;
1866 x1 = ROUND_DOWN(x, rt->tile_width);
1867 x2 = ROUND_UP(x + w, rt->tile_width);
1869 y1 = ROUND_DOWN(y, rt->tile_height);
1870 y2 = ROUND_UP(y + h, rt->tile_height);
1872 for (j = y1; j <= y2; j += rt->tile_height)
1874 for (i = x1; i <= x2; i += rt->tile_width)
1878 it = rt_tile_get(rt, i, j,
1880 (i + rt->tile_width < rt->x_scroll ||
1881 i > rt->x_scroll + pr->vis_width ||
1882 j + rt->tile_height < rt->y_scroll ||
1883 j > rt->y_scroll + pr->vis_height)));
1886 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
1887 (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
1889 it->render_todo = render;
1892 auto qd = g_new(QueueData, 1);
1894 qd->new_data = new_data;
1904 qd->w = x + w - i - qd->x;
1905 if (qd->x + qd->w > rt->tile_width) qd->w = rt->tile_width - qd->x;
1915 qd->h = y + h - j - qd->y;
1916 if (qd->y + qd->h > rt->tile_height) qd->h = rt->tile_height - qd->y;
1918 if (qd->w < 1 || qd->h < 1)
1924 rt_queue_merge(it->qd, qd);
1930 rt->draw_queue = g_list_append(rt->draw_queue, qd);
1939 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
1940 gboolean clamp, ImageRenderType render,
1941 gboolean new_data, gboolean only_existing)
1943 PixbufRenderer *pr = rt->pr;
1949 nx = CLAMP(x, 0, pr->width - 1);
1950 ny = CLAMP(y, 0, pr->height - 1);
1953 w = CLAMP(w, 0, pr->width - nx);
1954 h = CLAMP(h, 0, pr->height - ny);
1955 if (w < 1 || h < 1) return;
1957 if (rt_queue_to_tiles(rt, nx, ny, w, h, clamp, render, new_data, only_existing) &&
1958 ((!rt->draw_queue && !rt->draw_queue_2pass) || !rt->draw_idle_id))
1960 if (rt->draw_idle_id)
1962 g_source_remove(rt->draw_idle_id);
1963 rt->draw_idle_id = 0;
1965 rt_queue_schedule_next_draw(rt, TRUE);
1969 static void rt_scroll(void *renderer, gint x_off, gint y_off)
1971 auto rt = static_cast<RendererTiles *>(renderer);
1972 PixbufRenderer *pr = rt->pr;
1975 if (rt->stereo_mode & PR_STEREO_MIRROR) x_off = -x_off;
1976 if (rt->stereo_mode & PR_STEREO_FLIP) y_off = -y_off;
1978 gint w = pr->vis_width - abs(x_off);
1979 gint h = pr->vis_height - abs(y_off);
1983 /* scrolled completely to new material */
1984 rt_queue(rt, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1993 cairo_surface_t *surface;
2017 cr = cairo_create(rt->surface);
2018 surface = rt->surface;
2020 /* clipping restricts the intermediate surface's size, so it's a good idea
2022 cairo_rectangle(cr, x1 + pr->x_offset + rt->stereo_off_x, y1 + pr->y_offset + rt->stereo_off_y, w, h);
2024 /* Now push a group to change the target */
2025 cairo_push_group (cr);
2026 cairo_set_source_surface(cr, surface, x1 - x2, y1 - y2);
2028 /* Now copy the intermediate target back */
2029 cairo_pop_group_to_source(cr);
2033 rt_overlay_queue_all(rt, x2, y2, x1, y1);
2035 w = pr->vis_width - w;
2036 h = pr->vis_height - h;
2041 x_off > 0 ? rt->x_scroll + (pr->vis_width - w) : rt->x_scroll, rt->y_scroll,
2042 w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2046 /** @FIXME to optimize this, remove overlap */
2048 rt->x_scroll, y_off > 0 ? rt->y_scroll + (pr->vis_height - h) : rt->y_scroll,
2049 pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2053 static void renderer_area_changed(void *renderer, gint src_x, gint src_y, gint src_w, gint src_h)
2055 auto rt = static_cast<RendererTiles *>(renderer);
2056 PixbufRenderer *pr = rt->pr;
2066 gint orientation = rt_get_orientation(rt);
2067 pr_coords_map_orientation_reverse(orientation,
2068 src_x - get_right_pixbuf_offset(rt), src_y,
2069 pr->image_width, pr->image_height,
2074 if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
2076 /* increase region when using a zoom quality that may access surrounding pixels */
2081 x1 = static_cast<gint>(floor(static_cast<gdouble>(x) * pr->scale));
2082 y1 = static_cast<gint>(floor(static_cast<gdouble>(y) * pr->scale * pr->aspect_ratio));
2083 x2 = static_cast<gint>(ceil(static_cast<gdouble>(x + width) * pr->scale));
2084 y2 = static_cast<gint>(ceil(static_cast<gdouble>(y + height) * pr->scale * pr->aspect_ratio));
2086 rt_queue(rt, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
2089 static void renderer_redraw(RendererTiles *rt, gint x, gint y, gint w, gint h,
2090 gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing)
2092 PixbufRenderer *pr = rt->pr;
2094 x -= rt->stereo_off_x;
2095 y -= rt->stereo_off_y;
2097 rt_border_draw(rt, x, y, w, h);
2099 x = MAX(0, x - pr->x_offset + pr->x_scroll);
2100 y = MAX(0, y - pr->y_offset + pr->y_scroll);
2104 MIN(w, pr->width - x),
2105 MIN(h, pr->height - y),
2106 clamp, render, new_data, only_existing);
2109 static void renderer_update_pixbuf(void *renderer, gboolean)
2111 rt_queue_clear(static_cast<RendererTiles *>(renderer));
2114 static void renderer_update_zoom(void *renderer, gboolean lazy)
2116 auto rt = static_cast<RendererTiles *>(renderer);
2117 PixbufRenderer *pr = rt->pr;
2119 rt_tile_invalidate_all(static_cast<RendererTiles *>(renderer));
2122 renderer_redraw(static_cast<RendererTiles *>(renderer), 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, TRUE, FALSE);
2124 rt_border_clear(rt);
2127 static void renderer_invalidate_region(void *renderer, gint x, gint y, gint w, gint h)
2129 rt_tile_invalidate_region(static_cast<RendererTiles *>(renderer), x, y, w, h);
2132 static void renderer_update_viewport(void *renderer)
2134 auto rt = static_cast<RendererTiles *>(renderer);
2136 rt->stereo_off_x = 0;
2137 rt->stereo_off_y = 0;
2139 if (rt->stereo_mode & PR_STEREO_RIGHT)
2141 if (rt->stereo_mode & PR_STEREO_HORIZ)
2143 rt->stereo_off_x = rt->pr->viewport_width;
2145 else if (rt->stereo_mode & PR_STEREO_VERT)
2147 rt->stereo_off_y = rt->pr->viewport_height;
2149 else if (rt->stereo_mode & PR_STEREO_FIXED)
2151 rt->stereo_off_x = rt->pr->stereo_fixed_x_right;
2152 rt->stereo_off_y = rt->pr->stereo_fixed_y_right;
2157 if (rt->stereo_mode & PR_STEREO_FIXED)
2159 rt->stereo_off_x = rt->pr->stereo_fixed_x_left;
2160 rt->stereo_off_y = rt->pr->stereo_fixed_y_left;
2163 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);
2165 rt_overlay_update_sizes(rt);
2166 rt_border_clear(rt);
2169 static void renderer_stereo_set(void *renderer, gint stereo_mode)
2171 auto rt = static_cast<RendererTiles *>(renderer);
2173 rt->stereo_mode = stereo_mode;
2176 static void renderer_free(void *renderer)
2178 auto rt = static_cast<RendererTiles *>(renderer);
2180 rt_tile_free_all(rt);
2181 if (rt->spare_tile) g_object_unref(rt->spare_tile);
2182 if (rt->overlay_buffer) g_object_unref(rt->overlay_buffer);
2183 rt_overlay_list_clear(rt);
2184 /* disconnect "hierarchy-changed" */
2185 g_signal_handlers_disconnect_matched(G_OBJECT(rt->pr), G_SIGNAL_MATCH_DATA,
2186 0, 0, nullptr, nullptr, rt);
2190 static gboolean rt_realize_cb(GtkWidget *widget, gpointer data)
2192 auto rt = static_cast<RendererTiles *>(data);
2197 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));
2199 cr = cairo_create(rt->surface);
2200 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));
2208 static gboolean rt_size_allocate_cb(GtkWidget *widget, GdkRectangle *allocation, gpointer data)
2210 auto rt = static_cast<RendererTiles *>(data);
2212 cairo_surface_t *old_surface;
2214 if (gtk_widget_get_realized(GTK_WIDGET(rt->pr)))
2216 old_surface = rt->surface;
2217 rt->surface = gdk_window_create_similar_surface(gtk_widget_get_window(widget), CAIRO_CONTENT_COLOR, allocation->width, allocation->height);
2219 cr = cairo_create(rt->surface);
2221 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));
2223 cairo_set_source_surface(cr, old_surface, 0, 0);
2226 cairo_surface_destroy(old_surface);
2228 renderer_redraw(rt, allocation->x, allocation->y, allocation->width, allocation->height, FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2234 static gboolean rt_draw_cb(GtkWidget *, cairo_t *cr, gpointer data)
2236 auto rt = static_cast<RendererTiles *>(data);
2240 if (rt->stereo_mode & (PR_STEREO_HORIZ | PR_STEREO_VERT))
2242 cairo_push_group(cr);
2243 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));
2245 if (rt->stereo_mode & PR_STEREO_HORIZ)
2247 cairo_rectangle(cr, rt->stereo_off_x, 0, rt->pr->viewport_width, rt->pr->viewport_height);
2251 cairo_rectangle(cr, 0, rt->stereo_off_y, rt->pr->viewport_width, rt->pr->viewport_height);
2256 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);
2258 cairo_set_source_surface(cr, rt->surface, 0, 0);
2261 cairo_pop_group_to_source(cr);
2266 cairo_set_source_surface(cr, rt->surface, 0, 0);
2270 work = rt->overlay_list;
2273 od = static_cast<OverlayData *>(work->data);
2279 rt_overlay_get_position(rt, od, px, py, pw, ph);
2281 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px, py);
2289 RendererFuncs *renderer_tiles_new(PixbufRenderer *pr)
2291 auto rt = g_new0(RendererTiles, 1);
2295 rt->f.area_changed = renderer_area_changed;
2296 rt->f.update_pixbuf = renderer_update_pixbuf;
2297 rt->f.free = renderer_free;
2298 rt->f.update_zoom = renderer_update_zoom;
2299 rt->f.invalidate_region = renderer_invalidate_region;
2300 rt->f.scroll = rt_scroll;
2301 rt->f.update_viewport = renderer_update_viewport;
2304 rt->f.overlay_add = renderer_tiles_overlay_add;
2305 rt->f.overlay_set = renderer_tiles_overlay_set;
2306 rt->f.overlay_get = renderer_tiles_overlay_get;
2308 rt->f.stereo_set = renderer_stereo_set;
2310 rt->tile_width = options->image.tile_size;
2311 rt->tile_height = options->image.tile_size;
2313 rt->tiles = nullptr;
2314 rt->tile_cache_size = 0;
2316 rt->tile_cache_max = PR_CACHE_SIZE_DEFAULT;
2318 rt->draw_idle_id = 0;
2320 rt->stereo_mode = 0;
2321 rt->stereo_off_x = 0;
2322 rt->stereo_off_y = 0;
2324 rt->hidpi_scale = gtk_widget_get_scale_factor(GTK_WIDGET(rt->pr));
2326 g_signal_connect(G_OBJECT(pr), "hierarchy-changed",
2327 G_CALLBACK(rt_hierarchy_changed_cb), rt);
2329 g_signal_connect(G_OBJECT(pr), "draw",
2330 G_CALLBACK(rt_draw_cb), rt);
2331 g_signal_connect(G_OBJECT(pr), "realize", G_CALLBACK(rt_realize_cb), rt);
2332 g_signal_connect(G_OBJECT(pr), "size-allocate", G_CALLBACK(rt_size_allocate_cb), rt);
2334 return reinterpret_cast<RendererFuncs *>(rt);
2338 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */