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
67 constexpr size_t COLOR_BYTES = 3; /* rgb */
75 cairo_surface_t *surface; /* off screen buffer */
76 GdkPixbuf *pixbuf; /* pixbuf area for zooming */
77 gint x; /* x offset into image */
78 gint y; /* y offset into image */
79 gint w; /* width that is visible (may be less if at edge of image) */
80 gint h; /* height '' */
84 /* render_todo: (explanation)
86 AREA render area of tile, usually only used when loading an image
87 note: will jump to an ALL if render_done is not ALL.
88 ALL render entire tile, if never done before w/ ALL, for expose events *only*
91 ImageRenderType render_todo; /* what to do (see above) */
92 ImageRenderType render_done; /* highest that has been done before on tile */
97 guint size; /* est. memory used by pixmap and pixbuf */
120 OverlayRendererFlags flags;
128 gint tile_cache_max; /* max MiB to use for offscreen buffer */
132 gint tile_cols; /* count of tile columns */
133 GList *tiles; /* list of buffer tiles */
134 gint tile_cache_size; /* allocated size of pixmaps/pixbufs */
135 GList *draw_queue; /* list of areas to redraw */
136 GList *draw_queue_2pass;/* list when 2 pass is enabled */
139 cairo_surface_t *overlay_buffer;
140 cairo_surface_t *surface;
142 guint draw_idle_id; /* event source id */
144 GdkPixbuf *spare_tile;
150 gint x_scroll; /* allow local adjustment and mirroring */
158 static void rt_border_draw(RendererTiles *rt, gint x, gint y, gint w, gint h);
159 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h, ImageTile *it);
162 static void rt_tile_free_all(RendererTiles *rt);
163 static void rt_tile_invalidate_region(RendererTiles *rt, gint x, gint y, gint w, gint h);
164 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it);
165 static void rt_queue_clear(RendererTiles *rt);
166 static void rt_queue_merge(QueueData *parent, QueueData *qd);
167 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
168 gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing);
170 static void rt_hierarchy_changed_cb(GtkWidget *widget, GtkWidget *previous_toplevel, gpointer data);
171 static gint rt_queue_draw_idle_cb(gpointer data);
173 #define GET_RIGHT_PIXBUF_OFFSET(rt) \
174 (( ((rt)->stereo_mode & PR_STEREO_RIGHT) && !((rt)->stereo_mode & PR_STEREO_SWAP)) || \
175 (!((rt)->stereo_mode & PR_STEREO_RIGHT) && ((rt)->stereo_mode & PR_STEREO_SWAP)) ? \
176 (rt)->pr->stereo_pixbuf_offset_right : (rt)->pr->stereo_pixbuf_offset_left )
178 #define GET_LEFT_PIXBUF_OFFSET(rt) \
179 ((!((rt)->stereo_mode & PR_STEREO_RIGHT) && !((rt)->stereo_mode & PR_STEREO_SWAP)) || \
180 ( ((rt)->stereo_mode & PR_STEREO_RIGHT) && ((rt)->stereo_mode & PR_STEREO_SWAP)) ? \
181 (rt)->pr->stereo_pixbuf_offset_right : (rt)->pr->stereo_pixbuf_offset_left )
184 static void rt_sync_scroll(RendererTiles *rt)
186 PixbufRenderer *pr = rt->pr;
188 rt->x_scroll = (rt->stereo_mode & PR_STEREO_MIRROR) ?
189 pr->width - pr->vis_width - pr->x_scroll
192 rt->y_scroll = (rt->stereo_mode & PR_STEREO_FLIP) ?
193 pr->height - pr->vis_height - pr->y_scroll
198 *-------------------------------------------------------------------
200 *-------------------------------------------------------------------
203 static void rt_border_draw(RendererTiles *rt, gint x, gint y, gint w, gint h)
205 PixbufRenderer *pr = rt->pr;
214 box = GTK_WIDGET(pr);
215 window = gtk_widget_get_window(box);
219 cr = cairo_create(rt->surface);
221 if (!pr->pixbuf && !pr->source_tiles_enabled)
223 if (pr_clip_region(x, y, w, h,
225 pr->viewport_width, pr->viewport_height,
228 cairo_set_source_rgb(cr, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
229 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
231 rt_overlay_draw(rt, rx, ry, rw, rh, nullptr);
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 cairo_set_source_rgb(cr, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
246 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
248 rt_overlay_draw(rt, rx, ry, rw, rh, nullptr);
250 if (pr->viewport_width - pr->vis_width - pr->x_offset > 0 &&
251 pr_clip_region(x, y, w, h,
252 pr->x_offset + pr->vis_width, 0,
253 pr->viewport_width - pr->vis_width - pr->x_offset, pr->viewport_height,
256 cairo_set_source_rgb(cr, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
257 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
259 rt_overlay_draw(rt, rx, ry, rw, rh, nullptr);
262 if (pr->vis_height < pr->viewport_height)
264 if (pr->y_offset > 0 &&
265 pr_clip_region(x, y, w, h,
267 pr->vis_width, pr->y_offset,
270 cairo_set_source_rgb(cr, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
271 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
273 rt_overlay_draw(rt, rx, ry, rw, rh, nullptr);
275 if (pr->viewport_height - pr->vis_height - pr->y_offset > 0 &&
276 pr_clip_region(x, y, w, h,
277 pr->x_offset, pr->y_offset + pr->vis_height,
278 pr->vis_width, pr->viewport_height - pr->vis_height - pr->y_offset,
281 cairo_set_source_rgb(cr, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
282 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
284 rt_overlay_draw(rt, rx, ry, rw, rh, nullptr);
290 static void rt_border_clear(RendererTiles *rt)
292 PixbufRenderer *pr = rt->pr;
293 rt_border_draw(rt, 0, 0, pr->viewport_width, pr->viewport_height);
298 *-------------------------------------------------------------------
300 *-------------------------------------------------------------------
303 static ImageTile *rt_tile_new(gint x, gint y, gint width, gint height)
305 auto it = g_new0(ImageTile, 1);
312 it->render_done = TILE_RENDER_NONE;
317 static void rt_tile_free(ImageTile *it)
321 if (it->pixbuf) g_object_unref(it->pixbuf);
322 if (it->surface) cairo_surface_destroy(it->surface);
327 static void rt_tile_free_all(RendererTiles *rt)
329 g_list_free_full(rt->tiles, reinterpret_cast<GDestroyNotify>(rt_tile_free));
331 rt->tile_cache_size = 0;
334 static ImageTile *rt_tile_add(RendererTiles *rt, gint x, gint y)
336 PixbufRenderer *pr = rt->pr;
339 it = rt_tile_new(x, y, rt->tile_width, rt->tile_height);
341 if (it->x + it->w > pr->width) it->w = pr->width - it->x;
342 if (it->y + it->h > pr->height) it->h = pr->height - it->y;
344 rt->tiles = g_list_prepend(rt->tiles, it);
345 rt->tile_cache_size += it->size;
350 static void rt_tile_remove(RendererTiles *rt, ImageTile *it)
354 QueueData *qd = it->qd;
357 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
363 QueueData *qd = it->qd2;
366 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
370 rt->tiles = g_list_remove(rt->tiles, it);
371 rt->tile_cache_size -= it->size;
376 static void rt_tile_free_space(RendererTiles *rt, guint space, ImageTile *it)
378 PixbufRenderer *pr = rt->pr;
382 work = g_list_last(rt->tiles);
384 if (pr->source_tiles_enabled && pr->scale < 1.0)
388 tiles = (pr->vis_width / rt->tile_width + 1) * (pr->vis_height / rt->tile_height + 1);
389 tile_max = MAX(tiles * rt->tile_width * rt->tile_height * 3,
390 (gint)((gdouble)rt->tile_cache_max * 1048576.0 * pr->scale));
394 tile_max = rt->tile_cache_max * 1048576;
397 while (work && rt->tile_cache_size + space > tile_max)
401 needle = static_cast<ImageTile *>(work->data);
404 ((!needle->qd && !needle->qd2) || !rt_tile_is_visible(rt, needle))) rt_tile_remove(rt, needle);
408 static void rt_tile_invalidate_all(RendererTiles *rt)
410 PixbufRenderer *pr = rt->pr;
418 it = static_cast<ImageTile *>(work->data);
421 it->render_done = TILE_RENDER_NONE;
422 it->render_todo = TILE_RENDER_ALL;
425 it->w = MIN(rt->tile_width, pr->width - it->x);
426 it->h = MIN(rt->tile_height, pr->height - it->y);
430 static void rt_tile_invalidate_region(RendererTiles *rt, gint x, gint y, gint w, gint h)
438 x1 = ROUND_DOWN(x, rt->tile_width);
439 x2 = ROUND_UP(x + w, rt->tile_width);
441 y1 = ROUND_DOWN(y, rt->tile_height);
442 y2 = ROUND_UP(y + h, rt->tile_height);
449 it = static_cast<ImageTile *>(work->data);
452 if (it->x < x2 && it->x + it->w > x1 &&
453 it->y < y2 && it->y + it->h > y1)
455 it->render_done = TILE_RENDER_NONE;
456 it->render_todo = TILE_RENDER_ALL;
461 static ImageTile *rt_tile_get(RendererTiles *rt, gint x, gint y, gboolean only_existing)
470 it = static_cast<ImageTile *>(work->data);
471 if (it->x == x && it->y == y)
473 rt->tiles = g_list_delete_link(rt->tiles, work);
474 rt->tiles = g_list_prepend(rt->tiles, it);
481 if (only_existing) return nullptr;
483 return rt_tile_add(rt, x, y);
486 static gint pixmap_calc_size(cairo_surface_t *)
488 return options->image.tile_size * options->image.tile_size * 4 / 8;
491 static void rt_hidpi_aware_draw(
498 cairo_surface_t *surface;
499 surface = gdk_cairo_surface_create_from_pixbuf(pixbuf, rt->hidpi_scale, nullptr);
500 cairo_set_source_surface(cr, surface, x, y);
502 cairo_surface_destroy(surface);
505 static void rt_tile_prepare(RendererTiles *rt, ImageTile *it)
507 PixbufRenderer *pr = rt->pr;
510 cairo_surface_t *surface;
513 surface = gdk_window_create_similar_surface(gtk_widget_get_window(reinterpret_cast<GtkWidget *>(pr)),
515 rt->tile_width, rt->tile_height);
517 size = pixmap_calc_size(surface) * rt->hidpi_scale * rt->hidpi_scale;
518 rt_tile_free_space(rt, size, it);
520 it->surface = surface;
522 rt->tile_cache_size += size;
529 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, rt->hidpi_scale * rt->tile_width, rt->hidpi_scale * rt->tile_height);
531 size = gdk_pixbuf_get_rowstride(pixbuf) * rt->tile_height * rt->hidpi_scale;
532 rt_tile_free_space(rt, size, it);
536 rt->tile_cache_size += size;
541 *-------------------------------------------------------------------
543 *-------------------------------------------------------------------
546 static void rt_overlay_get_position(RendererTiles *rt, OverlayData *od,
547 gint *x, gint *y, gint *w, gint *h)
549 PixbufRenderer *pr = rt->pr;
555 pw = gdk_pixbuf_get_width(od->pixbuf);
556 ph = gdk_pixbuf_get_height(od->pixbuf);
560 if (od->flags & OVL_RELATIVE)
562 if (px < 0) px = pr->viewport_width - pw + px;
563 if (py < 0) py = pr->viewport_height - ph + py;
572 static void rt_overlay_init_window(RendererTiles *rt, OverlayData *od)
574 PixbufRenderer *pr = rt->pr;
579 GdkWindowAttr attributes;
580 gint attributes_mask;
582 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
584 attributes.window_type = GDK_WINDOW_CHILD;
585 attributes.wclass = GDK_INPUT_OUTPUT;
586 attributes.width = pw;
587 attributes.height = ph;
588 attributes.event_mask = GDK_EXPOSURE_MASK;
591 od->window = gdk_window_new(gtk_widget_get_window(GTK_WIDGET(pr)), &attributes, attributes_mask);
592 gdk_window_set_user_data(od->window, pr);
593 gdk_window_move(od->window, px + rt->stereo_off_x, py + rt->stereo_off_y);
594 gdk_window_show(od->window);
597 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h,
600 PixbufRenderer *pr = rt->pr;
603 work = rt->overlay_list;
616 od = static_cast<OverlayData *>(work->data);
619 if (!od->window) rt_overlay_init_window(rt, od);
621 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
622 if (pr_clip_region(x, y, w, h, px, py, pw, ph, &rx, &ry, &rw, &rh))
624 if (!rt->overlay_buffer)
626 rt->overlay_buffer = gdk_window_create_similar_surface(gtk_widget_get_window(reinterpret_cast<GtkWidget *>(pr)),
628 rt->tile_width, rt->tile_height);
635 cr = cairo_create(rt->overlay_buffer);
636 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);
637 cairo_rectangle(cr, 0, 0, rw, rh);
638 cairo_fill_preserve(cr);
640 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - rx, py - ry);
644 cr = gdk_cairo_create(od->window);
645 cairo_set_source_surface(cr, rt->overlay_buffer, rx - px, ry - py);
646 cairo_rectangle (cr, rx - px, ry - py, rw, rh);
652 /* no ImageTile means region may be larger than our scratch buffer */
656 for (sx = rx; sx < rx + rw; sx += rt->tile_width)
657 for (sy = ry; sy < ry + rh; sy += rt->tile_height)
663 sw = MIN(rx + rw - sx, rt->tile_width);
664 sh = MIN(ry + rh - sy, rt->tile_height);
666 cr = cairo_create(rt->overlay_buffer);
667 cairo_set_source_rgb(cr, 0, 0, 0);
668 cairo_rectangle(cr, 0, 0, sw, sh);
669 cairo_fill_preserve(cr);
671 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - sx, py - sy);
675 cr = gdk_cairo_create(od->window);
676 cairo_set_source_surface(cr, rt->overlay_buffer, sx - px, sy - py);
677 cairo_rectangle (cr, sx - px, sy - py, sw, sh);
686 static void rt_overlay_queue_draw(RendererTiles *rt, OverlayData *od, gint x1, gint y1, gint x2, gint y2)
688 PixbufRenderer *pr = rt->pr;
694 rt_overlay_get_position(rt, od, &x, &y, &w, &h);
702 rt_queue(rt, rt->x_scroll - pr->x_offset + x,
703 rt->y_scroll - pr->y_offset + y,
705 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
707 rt_border_draw(rt, x, y, w, h);
710 static void rt_overlay_queue_all(RendererTiles *rt, gint x1, gint y1, gint x2, gint y2)
714 work = rt->overlay_list;
717 auto od = static_cast<OverlayData *>(work->data);
720 rt_overlay_queue_draw(rt, od, x1, y1, x2, y2);
724 static void rt_overlay_update_sizes(RendererTiles *rt)
728 work = rt->overlay_list;
731 auto od = static_cast<OverlayData *>(work->data);
734 if (!od->window) rt_overlay_init_window(rt, od);
736 if (od->flags & OVL_RELATIVE)
743 rt_overlay_get_position(rt, od, &x, &y, &w, &h);
744 gdk_window_move_resize(od->window, x + rt->stereo_off_x, y + rt->stereo_off_y, w, h);
749 static OverlayData *rt_overlay_find(RendererTiles *rt, gint id)
753 work = rt->overlay_list;
756 auto od = static_cast<OverlayData *>(work->data);
759 if (od->id == id) return od;
766 gint renderer_tiles_overlay_add(void *renderer, GdkPixbuf *pixbuf, gint x, gint y,
767 OverlayRendererFlags flags)
769 auto rt = static_cast<RendererTiles *>(renderer);
770 PixbufRenderer *pr = rt->pr;
773 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
774 g_return_val_if_fail(pixbuf != nullptr, -1);
777 while (rt_overlay_find(rt, id)) id++;
779 auto od = g_new0(OverlayData, 1);
782 g_object_ref(G_OBJECT(od->pixbuf));
787 rt_overlay_init_window(rt, od);
789 rt->overlay_list = g_list_append(rt->overlay_list, od);
791 gtk_widget_queue_draw(GTK_WIDGET(rt->pr));
796 static void rt_overlay_free(RendererTiles *rt, OverlayData *od)
798 rt->overlay_list = g_list_remove(rt->overlay_list, od);
800 if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
801 if (od->window) gdk_window_destroy(od->window);
804 if (!rt->overlay_list && rt->overlay_buffer)
806 cairo_surface_destroy(rt->overlay_buffer);
807 rt->overlay_buffer = nullptr;
811 static void rt_overlay_list_clear(RendererTiles *rt)
813 while (rt->overlay_list)
815 auto od = static_cast<OverlayData *>(rt->overlay_list->data);
816 rt_overlay_free(rt, od);
820 static void rt_overlay_list_reset_window(RendererTiles *rt)
824 if (rt->overlay_buffer) cairo_surface_destroy(rt->overlay_buffer);
825 rt->overlay_buffer = nullptr;
827 work = rt->overlay_list;
830 auto od = static_cast<OverlayData *>(work->data);
832 if (od->window) gdk_window_destroy(od->window);
833 od->window = nullptr;
837 void renderer_tiles_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint, gint)
839 auto rc = static_cast<RendererTiles *>(renderer);
840 PixbufRenderer *pr = rc->pr;
843 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
845 od = rt_overlay_find(rc, id);
850 g_object_ref(G_OBJECT(pixbuf));
851 g_object_unref(G_OBJECT(od->pixbuf));
856 rt_overlay_free(rc, od);
859 gtk_widget_queue_draw(GTK_WIDGET(rc->pr));
862 gboolean renderer_tiles_overlay_get(void *renderer, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
864 auto rt = static_cast<RendererTiles *>(renderer);
865 PixbufRenderer *pr = rt->pr;
868 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
870 od = rt_overlay_find(rt, id);
871 if (!od) return FALSE;
873 if (pixbuf) *pixbuf = od->pixbuf;
880 static void rt_hierarchy_changed_cb(GtkWidget *, GtkWidget *, gpointer data)
882 auto rt = static_cast<RendererTiles *>(data);
883 rt_overlay_list_reset_window(rt);
887 *-------------------------------------------------------------------
889 *-------------------------------------------------------------------
892 static GdkPixbuf *rt_get_spare_tile(RendererTiles *rt)
894 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);
895 return rt->spare_tile;
898 static void rt_tile_rotate_90_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
900 GdkPixbuf *src = *tile;
913 gint tw = rt->tile_width * rt->hidpi_scale;
915 srs = gdk_pixbuf_get_rowstride(src);
916 s_pix = gdk_pixbuf_get_pixels(src);
917 spi = s_pix + (x * COLOR_BYTES);
919 dest = rt_get_spare_tile(rt);
920 drs = gdk_pixbuf_get_rowstride(dest);
921 d_pix = gdk_pixbuf_get_pixels(dest);
922 dpi = d_pix + (tw - 1) * COLOR_BYTES;
924 for (i = y; i < y + h; i++)
926 sp = spi + (i * srs);
927 ip = dpi - (i * COLOR_BYTES);
928 for (j = x; j < x + w; j++)
931 memcpy(dp, sp, COLOR_BYTES);
936 rt->spare_tile = src;
940 static void rt_tile_rotate_90_counter_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
942 GdkPixbuf *src = *tile;
955 gint th = rt->tile_height * rt->hidpi_scale;
957 srs = gdk_pixbuf_get_rowstride(src);
958 s_pix = gdk_pixbuf_get_pixels(src);
959 spi = s_pix + (x * COLOR_BYTES);
961 dest = rt_get_spare_tile(rt);
962 drs = gdk_pixbuf_get_rowstride(dest);
963 d_pix = gdk_pixbuf_get_pixels(dest);
964 dpi = d_pix + (th - 1) * drs;
966 for (i = y; i < y + h; i++)
968 sp = spi + (i * srs);
969 ip = dpi + (i * COLOR_BYTES);
970 for (j = x; j < x + w; j++)
973 memcpy(dp, sp, COLOR_BYTES);
978 rt->spare_tile = src;
982 static void rt_tile_mirror_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
984 GdkPixbuf *src = *tile;
997 gint tw = rt->tile_width * rt->hidpi_scale;
999 srs = gdk_pixbuf_get_rowstride(src);
1000 s_pix = gdk_pixbuf_get_pixels(src);
1001 spi = s_pix + (x * COLOR_BYTES);
1003 dest = rt_get_spare_tile(rt);
1004 drs = gdk_pixbuf_get_rowstride(dest);
1005 d_pix = gdk_pixbuf_get_pixels(dest);
1006 dpi = d_pix + (tw - x - 1) * COLOR_BYTES;
1008 for (i = y; i < y + h; i++)
1010 sp = spi + (i * srs);
1011 dp = dpi + (i * drs);
1012 for (j = 0; j < w; j++)
1014 memcpy(dp, sp, COLOR_BYTES);
1020 rt->spare_tile = src;
1024 static void rt_tile_mirror_and_flip(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1026 GdkPixbuf *src = *tile;
1037 gint tw = rt->tile_width * rt->hidpi_scale;
1038 gint th = rt->tile_height * rt->hidpi_scale;
1040 srs = gdk_pixbuf_get_rowstride(src);
1041 s_pix = gdk_pixbuf_get_pixels(src);
1043 dest = rt_get_spare_tile(rt);
1044 drs = gdk_pixbuf_get_rowstride(dest);
1045 d_pix = gdk_pixbuf_get_pixels(dest);
1046 dpi = d_pix + (th - 1) * drs + (tw - 1) * COLOR_BYTES;
1048 for (i = y; i < y + h; i++)
1050 sp = s_pix + (i * srs) + (x * COLOR_BYTES);
1051 dp = dpi - (i * drs) - (x * COLOR_BYTES);
1052 for (j = 0; j < w; j++)
1054 memcpy(dp, sp, COLOR_BYTES);
1060 rt->spare_tile = src;
1064 static void rt_tile_flip_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1066 GdkPixbuf *src = *tile;
1077 gint th = rt->tile_height * rt->hidpi_scale;
1079 srs = gdk_pixbuf_get_rowstride(src);
1080 s_pix = gdk_pixbuf_get_pixels(src);
1081 spi = s_pix + (x * COLOR_BYTES);
1083 dest = rt_get_spare_tile(rt);
1084 drs = gdk_pixbuf_get_rowstride(dest);
1085 d_pix = gdk_pixbuf_get_pixels(dest);
1086 dpi = d_pix + (th - 1) * drs + (x * COLOR_BYTES);
1088 for (i = y; i < y + h; i++)
1090 sp = spi + (i * srs);
1091 dp = dpi - (i * drs);
1092 memcpy(dp, sp, w * COLOR_BYTES);
1095 rt->spare_tile = src;
1099 static void rt_tile_apply_orientation(RendererTiles *rt, gint orientation, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h)
1101 switch (orientation)
1103 case EXIF_ORIENTATION_TOP_LEFT:
1104 /* normal -- nothing to do */
1106 case EXIF_ORIENTATION_TOP_RIGHT:
1109 rt_tile_mirror_only(rt, pixbuf, x, y, w, h);
1112 case EXIF_ORIENTATION_BOTTOM_RIGHT:
1115 rt_tile_mirror_and_flip(rt, pixbuf, x, y, w, h);
1118 case EXIF_ORIENTATION_BOTTOM_LEFT:
1121 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1124 case EXIF_ORIENTATION_LEFT_TOP:
1126 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1127 rt_tile_rotate_90_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1130 case EXIF_ORIENTATION_RIGHT_TOP:
1131 /* rotated -90 (270) */
1133 rt_tile_rotate_90_clockwise(rt, pixbuf, x, y, w, h);
1136 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1138 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1139 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1142 case EXIF_ORIENTATION_LEFT_BOTTOM:
1145 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, y, w, h);
1149 /* The other values are out of range */
1154 static gboolean rt_source_tile_render(RendererTiles *rt, ImageTile *it,
1155 gint x, gint y, gint w, gint h,
1156 gboolean, gboolean fast)
1158 PixbufRenderer *pr = rt->pr;
1161 gboolean draw = FALSE;
1163 if (pr->zoom == 1.0 || pr->scale == 1.0)
1165 list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE);
1175 st = static_cast<SourceTile *>(work->data);
1178 if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1179 it->x + x, it->y + y, w, h,
1180 &rx, &ry, &rw, &rh))
1183 cr = cairo_create(it->surface);
1184 cairo_rectangle (cr, rx - it->x, ry - it->y, rw, rh);
1188 cairo_set_source_rgb(cr, 0, 0, 0);
1191 else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
1193 rt_hidpi_aware_draw(rt, cr, st->pixbuf, -it->x + st->x, -it->y + st->y);
1208 if (pr->image_width == 0 || pr->image_height == 0) return FALSE;
1209 scale_x = static_cast<gdouble>(pr->width) / pr->image_width;
1210 scale_y = static_cast<gdouble>(pr->height) / pr->image_height;
1212 sx = static_cast<gdouble>(it->x + x) / scale_x;
1213 sy = static_cast<gdouble>(it->y + y) / scale_y;
1214 sw = static_cast<gdouble>(w) / scale_x;
1215 sh = static_cast<gdouble>(h) / scale_y;
1217 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1220 /* draws red over draw region, to check for leaks (regions not filled) */
1221 pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255);
1224 list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE);
1238 st = static_cast<SourceTile *>(work->data);
1241 stx = floor(static_cast<gdouble>(st->x) * scale_x);
1242 sty = floor(static_cast<gdouble>(st->y) * scale_y);
1243 stw = ceil(static_cast<gdouble>(st->x + pr->source_tile_width) * scale_x) - stx;
1244 sth = ceil(static_cast<gdouble>(st->y + pr->source_tile_height) * scale_y) - sty;
1246 if (pr_clip_region(stx, sty, stw, sth,
1247 it->x + x, it->y + y, w, h,
1248 &rx, &ry, &rw, &rh))
1254 cr = cairo_create(it->surface);
1255 cairo_rectangle (cr, rx - st->x, ry - st->y, rw, rh);
1256 cairo_set_source_rgb(cr, 0, 0, 0);
1265 /* may need to use unfloored stx,sty values here */
1266 offset_x = static_cast<gdouble>(stx - it->x);
1267 offset_y = static_cast<gdouble>(sty - it->y);
1269 gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh,
1270 static_cast<gdouble>(0.0) + offset_x,
1271 static_cast<gdouble>(0.0) + offset_y,
1273 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1288 * @param ignore_alpha
1299 * @param interp_type
1302 * @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
1306 static void rt_tile_get_region(gboolean has_alpha, gboolean ignore_alpha,
1307 const GdkPixbuf *src, GdkPixbuf *dest,
1308 int pb_x, int pb_y, int pb_w, int pb_h,
1309 double offset_x, double offset_y, double scale_x, double scale_y,
1310 GdkInterpType interp_type,
1311 int check_x, int check_y, gboolean wide_image)
1313 GdkPixbuf* tmppixbuf;
1332 if (scale_x == 1.0 && scale_y == 1.0)
1336 srs = gdk_pixbuf_get_rowstride(src);
1337 drs = gdk_pixbuf_get_rowstride(dest);
1338 psrc = gdk_pixbuf_get_pixels(src);
1339 pdst = gdk_pixbuf_get_pixels(dest);
1340 for (y = 0; y < pb_h; y++)
1342 for (x = 0; x < pb_w; x++)
1344 for (c = 0; c < 3; c++)
1346 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];
1353 gdk_pixbuf_copy_area(src,
1354 -offset_x + pb_x, -offset_y + pb_y,
1362 gdk_pixbuf_scale(src, dest,
1363 pb_x, pb_y, pb_w, pb_h,
1367 (wide_image && interp_type == GDK_INTERP_NEAREST) ? GDK_INTERP_TILES : interp_type);
1372 red_1=static_cast<guint32>(options->image.alpha_color_1.red * 255) << 16 & 0x00FF0000;
1373 green_1=static_cast<guint32>(options->image.alpha_color_1.green * 255) << 8 & 0x0000FF00;
1374 blue_1=static_cast<guint32>(options->image.alpha_color_1.blue * 255) & 0x000000FF;
1375 red_2=static_cast<guint32>(options->image.alpha_color_2.red * 255) << 16 & 0x00FF0000;
1376 green_2=static_cast<guint32>(options->image.alpha_color_2.green * 255) << 8 & 0x0000FF00;
1377 blue_2=static_cast<guint32>(options->image.alpha_color_2.blue * 255) & 0x000000FF;
1378 alpha_1 = red_1 + green_1 + blue_1;
1379 alpha_2 = red_2 + green_2 + blue_2;
1383 tmppixbuf = gdk_pixbuf_add_alpha(src, FALSE, 0, 0, 0);
1385 pixbuf_ignore_alpha_rect(tmppixbuf, 0, 0, gdk_pixbuf_get_width(src), gdk_pixbuf_get_height(src));
1387 gdk_pixbuf_composite_color(tmppixbuf, dest,
1388 pb_x, pb_y, pb_w, pb_h,
1392 (scale_x == 1.0 && scale_y == 1.0) ? GDK_INTERP_NEAREST : interp_type,
1393 255, check_x, check_y,
1394 PR_ALPHA_CHECK_SIZE,
1397 g_object_unref(tmppixbuf);
1401 gdk_pixbuf_composite_color(src, dest,
1402 pb_x, pb_y, pb_w, pb_h,
1406 (scale_x == 1.0 && scale_y == 1.0) ? GDK_INTERP_NEAREST : interp_type,
1407 255, check_x, check_y,
1408 PR_ALPHA_CHECK_SIZE,
1416 static gint rt_get_orientation(RendererTiles *rt)
1418 PixbufRenderer *pr = rt->pr;
1420 gint orientation = pr->orientation;
1421 static const gint mirror[] = {1, 2, 1, 4, 3, 6, 5, 8, 7};
1422 static const gint flip[] = {1, 4, 3, 2, 1, 8, 7, 6, 5};
1424 if (rt->stereo_mode & PR_STEREO_MIRROR) orientation = mirror[orientation];
1425 if (rt->stereo_mode & PR_STEREO_FLIP) orientation = flip[orientation];
1430 static void rt_tile_render(RendererTiles *rt, ImageTile *it,
1431 gint x, gint y, gint w, gint h,
1432 gboolean new_data, gboolean fast)
1434 PixbufRenderer *pr = rt->pr;
1436 gboolean draw = FALSE;
1437 gint orientation = rt_get_orientation(rt);
1438 gboolean wide_image = FALSE;
1440 if (it->render_todo == TILE_RENDER_NONE && it->surface && !new_data) return;
1442 if (it->render_done != TILE_RENDER_ALL)
1448 if (!fast) it->render_done = TILE_RENDER_ALL;
1450 else if (it->render_todo != TILE_RENDER_AREA)
1452 if (!fast) it->render_todo = TILE_RENDER_NONE;
1456 if (!fast) it->render_todo = TILE_RENDER_NONE;
1458 if (new_data) it->blank = FALSE;
1460 rt_tile_prepare(rt, it);
1461 has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
1463 /** @FIXME checker colors for alpha should be configurable,
1464 * also should be drawn for blank = TRUE
1469 /* no data, do fast rect fill */
1471 cr = cairo_create(it->surface);
1472 cairo_rectangle (cr, 0, 0, it->w, it->h);
1473 cairo_set_source_rgb(cr, 0, 0, 0);
1477 else if (pr->source_tiles_enabled)
1479 draw = rt_source_tile_render(rt, it, x, y, w, h, new_data, fast);
1492 if (pr->image_width == 0 || pr->image_height == 0) return;
1494 scale_x = rt->hidpi_scale * static_cast<gdouble>(pr->width) / pr->image_width;
1495 scale_y = rt->hidpi_scale * static_cast<gdouble>(pr->height) / pr->image_height;
1497 pr_tile_coords_map_orientation(orientation, it->x, it->y,
1498 pr->width, pr->height,
1499 rt->tile_width, rt->tile_height,
1501 pr_tile_region_map_orientation(orientation, x, y,
1502 rt->tile_width, rt->tile_height,
1507 src_x *= rt->hidpi_scale;
1508 src_y *= rt->hidpi_scale;
1509 pb_x *= rt->hidpi_scale;
1510 pb_y *= rt->hidpi_scale;
1511 pb_w *= rt->hidpi_scale;
1512 pb_h *= rt->hidpi_scale;
1514 switch (orientation)
1516 case EXIF_ORIENTATION_LEFT_TOP:
1517 case EXIF_ORIENTATION_RIGHT_TOP:
1518 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1519 case EXIF_ORIENTATION_LEFT_BOTTOM:
1520 std::swap(scale_x, scale_y);
1527 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
1528 * small sizes for anything but GDK_INTERP_NEAREST
1530 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1531 if (pr->image_width > 32767) wide_image = TRUE;
1533 rt_tile_get_region(has_alpha, pr->ignore_alpha,
1534 pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
1535 static_cast<gdouble>(0.0) - src_x - GET_RIGHT_PIXBUF_OFFSET(rt) * scale_x,
1536 static_cast<gdouble>(0.0) - src_y,
1538 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1539 it->x + pb_x, it->y + pb_y, wide_image);
1540 if (rt->stereo_mode & PR_STEREO_ANAGLYPH &&
1541 (pr->stereo_pixbuf_offset_right > 0 || pr->stereo_pixbuf_offset_left > 0))
1543 GdkPixbuf *right_pb = rt_get_spare_tile(rt);
1544 rt_tile_get_region(has_alpha, pr->ignore_alpha,
1545 pr->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h,
1546 static_cast<gdouble>(0.0) - src_x - GET_LEFT_PIXBUF_OFFSET(rt) * scale_x,
1547 static_cast<gdouble>(0.0) - src_y,
1549 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1550 it->x + pb_x, it->y + pb_y, wide_image);
1551 pr_create_anaglyph(rt->stereo_mode, it->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h);
1552 /* do not care about freeing spare_tile, it will be reused */
1554 rt_tile_apply_orientation(rt, orientation, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
1558 if (draw && it->pixbuf && !it->blank)
1562 if (pr->func_post_process && !(pr->post_process_slow && fast))
1563 pr->func_post_process(pr, &it->pixbuf, x, y, w, h, pr->post_process_user_data);
1565 cr = cairo_create(it->surface);
1566 cairo_rectangle (cr, x, y, w, h);
1567 rt_hidpi_aware_draw(rt, cr, it->pixbuf, 0, 0);
1573 static void rt_tile_expose(RendererTiles *rt, ImageTile *it,
1574 gint x, gint y, gint w, gint h,
1575 gboolean new_data, gboolean fast)
1577 PixbufRenderer *pr = rt->pr;
1580 /* clamp to visible */
1581 if (it->x + x < rt->x_scroll)
1583 w -= rt->x_scroll - it->x - x;
1584 x = rt->x_scroll - it->x;
1586 if (it->x + x + w > rt->x_scroll + pr->vis_width)
1588 w = rt->x_scroll + pr->vis_width - it->x - x;
1591 if (it->y + y < rt->y_scroll)
1593 h -= rt->y_scroll - it->y - y;
1594 y = rt->y_scroll - it->y;
1596 if (it->y + y + h > rt->y_scroll + pr->vis_height)
1598 h = rt->y_scroll + pr->vis_height - it->y - y;
1602 rt_tile_render(rt, it, x, y, w, h, new_data, fast);
1604 cr = cairo_create(rt->surface);
1605 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);
1606 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);
1610 if (rt->overlay_list)
1612 rt_overlay_draw(rt, pr->x_offset + (it->x - rt->x_scroll) + x,
1613 pr->y_offset + (it->y - rt->y_scroll) + y,
1618 gtk_widget_queue_draw(GTK_WIDGET(rt->pr));
1622 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it)
1624 PixbufRenderer *pr = rt->pr;
1625 return (it->x + it->w >= rt->x_scroll && it->x < rt->x_scroll + pr->vis_width &&
1626 it->y + it->h >= rt->y_scroll && it->y < rt->y_scroll + pr->vis_height);
1630 *-------------------------------------------------------------------
1632 *-------------------------------------------------------------------
1635 static gint rt_get_queued_area(GList *work)
1641 auto qd = static_cast<QueueData *>(work->data);
1642 area += qd->w * qd->h;
1649 static gboolean rt_queue_schedule_next_draw(RendererTiles *rt, gboolean force_set)
1651 PixbufRenderer *pr = rt->pr;
1653 gint visible_area = pr->vis_width * pr->vis_height;
1658 DEBUG_2("redraw priority: 2pass");
1659 rt->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, rt_queue_draw_idle_cb, rt, nullptr);
1660 return G_SOURCE_REMOVE;
1663 if (visible_area == 0)
1670 percent = 100.0 * rt_get_queued_area(rt->draw_queue) / visible_area;
1675 /* we have enough data for starting intensive redrawing */
1676 DEBUG_2("redraw priority: high %.2f %%", percent);
1677 rt->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW, rt_queue_draw_idle_cb, rt, nullptr);
1678 return G_SOURCE_REMOVE;
1681 if (percent < 1.0 || force_set)
1683 /* queue is (almost) empty, wait 50 ms*/
1684 DEBUG_2("redraw priority: wait %.2f %%", percent);
1685 rt->draw_idle_id = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, 50, rt_queue_draw_idle_cb, rt, nullptr);
1686 return G_SOURCE_REMOVE;
1689 /* keep the same priority as before */
1690 DEBUG_2("redraw priority: no change %.2f %%", percent);
1691 return G_SOURCE_CONTINUE;
1695 static gboolean rt_queue_draw_idle_cb(gpointer data)
1697 auto rt = static_cast<RendererTiles *>(data);
1698 PixbufRenderer *pr = rt->pr;
1703 if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
1704 (!rt->draw_queue && !rt->draw_queue_2pass) ||
1707 pr_render_complete_signal(pr);
1709 rt->draw_idle_id = 0;
1710 return G_SOURCE_REMOVE;
1715 qd = static_cast<QueueData *>(rt->draw_queue->data);
1716 fast = (pr->zoom_2pass && ((pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0) || pr->post_process_slow));
1722 /* still loading, wait till done (also drops the higher priority) */
1724 return rt_queue_schedule_next_draw(rt, FALSE);
1727 qd = static_cast<QueueData *>(rt->draw_queue_2pass->data);
1731 if (gtk_widget_get_realized(GTK_WIDGET(pr)))
1733 if (rt_tile_is_visible(rt, qd->it))
1735 rt_tile_expose(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1737 else if (qd->new_data)
1739 /* if new pixel data, and we already have a pixmap, update the tile */
1740 qd->it->blank = FALSE;
1741 if (qd->it->surface && qd->it->render_done == TILE_RENDER_ALL)
1743 rt_tile_render(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1750 qd->it->qd = nullptr;
1751 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
1756 rt_queue_merge(qd->it->qd2, qd);
1762 rt->draw_queue_2pass = g_list_append(rt->draw_queue_2pass, qd);
1772 qd->it->qd2 = nullptr;
1773 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
1777 if (!rt->draw_queue && !rt->draw_queue_2pass)
1779 pr_render_complete_signal(pr);
1781 rt->draw_idle_id = 0;
1782 return G_SOURCE_REMOVE;
1785 return rt_queue_schedule_next_draw(rt, FALSE);
1788 static void rt_queue_list_free(GList *list)
1797 qd = static_cast<QueueData *>(work->data);
1800 qd->it->qd = nullptr;
1801 qd->it->qd2 = nullptr;
1808 static void rt_queue_clear(RendererTiles *rt)
1810 rt_queue_list_free(rt->draw_queue);
1811 rt->draw_queue = nullptr;
1813 rt_queue_list_free(rt->draw_queue_2pass);
1814 rt->draw_queue_2pass = nullptr;
1816 if (rt->draw_idle_id)
1818 g_source_remove(rt->draw_idle_id);
1819 rt->draw_idle_id = 0;
1824 static void rt_queue_merge(QueueData *parent, QueueData *qd)
1826 if (parent->x + parent->w < qd->x + qd->w)
1828 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
1830 if (parent->x > qd->x)
1832 parent->w += parent->x - qd->x;
1836 if (parent->y + parent->h < qd->y + qd->h)
1838 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
1840 if (parent->y > qd->y)
1842 parent->h += parent->y - qd->y;
1846 parent->new_data |= qd->new_data;
1849 static gboolean rt_clamp_to_visible(RendererTiles *rt, gint *x, gint *y, gint *w, gint *h)
1851 PixbufRenderer *pr = rt->pr;
1862 vh = pr->vis_height;
1867 if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
1870 nx = CLAMP(*x, vx, vx + vw);
1871 nw = CLAMP(*w - (nx - *x), 1, vw);
1873 ny = CLAMP(*y, vy, vy + vh);
1874 nh = CLAMP(*h - (ny - *y), 1, vh);
1884 static gboolean rt_queue_to_tiles(RendererTiles *rt, gint x, gint y, gint w, gint h,
1885 gboolean clamp, ImageRenderType render,
1886 gboolean new_data, gboolean only_existing)
1888 PixbufRenderer *pr = rt->pr;
1896 if (clamp && !rt_clamp_to_visible(rt, &x, &y, &w, &h)) return FALSE;
1898 x1 = ROUND_DOWN(x, rt->tile_width);
1899 x2 = ROUND_UP(x + w, rt->tile_width);
1901 y1 = ROUND_DOWN(y, rt->tile_height);
1902 y2 = ROUND_UP(y + h, rt->tile_height);
1904 for (j = y1; j <= y2; j += rt->tile_height)
1906 for (i = x1; i <= x2; i += rt->tile_width)
1910 it = rt_tile_get(rt, i, j,
1912 (i + rt->tile_width < rt->x_scroll ||
1913 i > rt->x_scroll + pr->vis_width ||
1914 j + rt->tile_height < rt->y_scroll ||
1915 j > rt->y_scroll + pr->vis_height)));
1918 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
1919 (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
1921 it->render_todo = render;
1924 auto qd = g_new(QueueData, 1);
1926 qd->new_data = new_data;
1936 qd->w = x + w - i - qd->x;
1937 if (qd->x + qd->w > rt->tile_width) qd->w = rt->tile_width - qd->x;
1947 qd->h = y + h - j - qd->y;
1948 if (qd->y + qd->h > rt->tile_height) qd->h = rt->tile_height - qd->y;
1950 if (qd->w < 1 || qd->h < 1)
1956 rt_queue_merge(it->qd, qd);
1962 rt->draw_queue = g_list_append(rt->draw_queue, qd);
1971 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
1972 gboolean clamp, ImageRenderType render,
1973 gboolean new_data, gboolean only_existing)
1975 PixbufRenderer *pr = rt->pr;
1981 nx = CLAMP(x, 0, pr->width - 1);
1982 ny = CLAMP(y, 0, pr->height - 1);
1985 w = CLAMP(w, 0, pr->width - nx);
1986 h = CLAMP(h, 0, pr->height - ny);
1987 if (w < 1 || h < 1) return;
1989 if (rt_queue_to_tiles(rt, nx, ny, w, h, clamp, render, new_data, only_existing) &&
1990 ((!rt->draw_queue && !rt->draw_queue_2pass) || !rt->draw_idle_id))
1992 if (rt->draw_idle_id)
1994 g_source_remove(rt->draw_idle_id);
1995 rt->draw_idle_id = 0;
1997 rt_queue_schedule_next_draw(rt, TRUE);
2001 static void rt_scroll(void *renderer, gint x_off, gint y_off)
2003 auto rt = static_cast<RendererTiles *>(renderer);
2004 PixbufRenderer *pr = rt->pr;
2007 if (rt->stereo_mode & PR_STEREO_MIRROR) x_off = -x_off;
2008 if (rt->stereo_mode & PR_STEREO_FLIP) y_off = -y_off;
2010 gint w = pr->vis_width - abs(x_off);
2011 gint h = pr->vis_height - abs(y_off);
2015 /* scrolled completely to new material */
2016 rt_queue(rt, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2025 cairo_surface_t *surface;
2049 cr = cairo_create(rt->surface);
2050 surface = rt->surface;
2052 /* clipping restricts the intermediate surface's size, so it's a good idea
2054 cairo_rectangle(cr, x1 + pr->x_offset + rt->stereo_off_x, y1 + pr->y_offset + rt->stereo_off_y, w, h);
2056 /* Now push a group to change the target */
2057 cairo_push_group (cr);
2058 cairo_set_source_surface(cr, surface, x1 - x2, y1 - y2);
2060 /* Now copy the intermediate target back */
2061 cairo_pop_group_to_source(cr);
2065 rt_overlay_queue_all(rt, x2, y2, x1, y1);
2067 w = pr->vis_width - w;
2068 h = pr->vis_height - h;
2073 x_off > 0 ? rt->x_scroll + (pr->vis_width - w) : rt->x_scroll, rt->y_scroll,
2074 w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2078 /** @FIXME to optimize this, remove overlap */
2080 rt->x_scroll, y_off > 0 ? rt->y_scroll + (pr->vis_height - h) : rt->y_scroll,
2081 pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2085 static void renderer_area_changed(void *renderer, gint src_x, gint src_y, gint src_w, gint src_h)
2087 auto rt = static_cast<RendererTiles *>(renderer);
2088 PixbufRenderer *pr = rt->pr;
2098 gint orientation = rt_get_orientation(rt);
2099 pr_coords_map_orientation_reverse(orientation,
2100 src_x - GET_RIGHT_PIXBUF_OFFSET(rt), src_y,
2101 pr->image_width, pr->image_height,
2106 if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
2108 /* increase region when using a zoom quality that may access surrounding pixels */
2113 x1 = static_cast<gint>(floor(static_cast<gdouble>(x) * pr->scale));
2114 y1 = static_cast<gint>(floor(static_cast<gdouble>(y) * pr->scale * pr->aspect_ratio));
2115 x2 = static_cast<gint>(ceil(static_cast<gdouble>(x + width) * pr->scale));
2116 y2 = static_cast<gint>(ceil(static_cast<gdouble>(y + height) * pr->scale * pr->aspect_ratio));
2118 rt_queue(rt, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
2121 static void renderer_redraw(RendererTiles *rt, gint x, gint y, gint w, gint h,
2122 gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing)
2124 PixbufRenderer *pr = rt->pr;
2126 x -= rt->stereo_off_x;
2127 y -= rt->stereo_off_y;
2129 rt_border_draw(rt, x, y, w, h);
2131 x = MAX(0, x - pr->x_offset + pr->x_scroll);
2132 y = MAX(0, y - pr->y_offset + pr->y_scroll);
2136 MIN(w, pr->width - x),
2137 MIN(h, pr->height - y),
2138 clamp, render, new_data, only_existing);
2141 static void renderer_update_pixbuf(void *renderer, gboolean)
2143 rt_queue_clear(static_cast<RendererTiles *>(renderer));
2146 static void renderer_update_zoom(void *renderer, gboolean lazy)
2148 auto rt = static_cast<RendererTiles *>(renderer);
2149 PixbufRenderer *pr = rt->pr;
2151 rt_tile_invalidate_all(static_cast<RendererTiles *>(renderer));
2154 renderer_redraw(static_cast<RendererTiles *>(renderer), 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, TRUE, FALSE);
2156 rt_border_clear(rt);
2159 static void renderer_invalidate_region(void *renderer, gint x, gint y, gint w, gint h)
2161 rt_tile_invalidate_region(static_cast<RendererTiles *>(renderer), x, y, w, h);
2164 static void renderer_update_viewport(void *renderer)
2166 auto rt = static_cast<RendererTiles *>(renderer);
2168 rt->stereo_off_x = 0;
2169 rt->stereo_off_y = 0;
2171 if (rt->stereo_mode & PR_STEREO_RIGHT)
2173 if (rt->stereo_mode & PR_STEREO_HORIZ)
2175 rt->stereo_off_x = rt->pr->viewport_width;
2177 else if (rt->stereo_mode & PR_STEREO_VERT)
2179 rt->stereo_off_y = rt->pr->viewport_height;
2181 else if (rt->stereo_mode & PR_STEREO_FIXED)
2183 rt->stereo_off_x = rt->pr->stereo_fixed_x_right;
2184 rt->stereo_off_y = rt->pr->stereo_fixed_y_right;
2189 if (rt->stereo_mode & PR_STEREO_FIXED)
2191 rt->stereo_off_x = rt->pr->stereo_fixed_x_left;
2192 rt->stereo_off_y = rt->pr->stereo_fixed_y_left;
2195 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);
2197 rt_overlay_update_sizes(rt);
2198 rt_border_clear(rt);
2201 static void renderer_stereo_set(void *renderer, gint stereo_mode)
2203 auto rt = static_cast<RendererTiles *>(renderer);
2205 rt->stereo_mode = stereo_mode;
2208 static void renderer_free(void *renderer)
2210 auto rt = static_cast<RendererTiles *>(renderer);
2212 rt_tile_free_all(rt);
2213 if (rt->spare_tile) g_object_unref(rt->spare_tile);
2214 if (rt->overlay_buffer) g_object_unref(rt->overlay_buffer);
2215 rt_overlay_list_clear(rt);
2216 /* disconnect "hierarchy-changed" */
2217 g_signal_handlers_disconnect_matched(G_OBJECT(rt->pr), G_SIGNAL_MATCH_DATA,
2218 0, 0, nullptr, nullptr, rt);
2222 static gboolean rt_realize_cb(GtkWidget *widget, gpointer data)
2224 auto rt = static_cast<RendererTiles *>(data);
2229 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));
2231 cr = cairo_create(rt->surface);
2232 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));
2240 static gboolean rt_size_allocate_cb(GtkWidget *widget, GdkRectangle *allocation, gpointer data)
2242 auto rt = static_cast<RendererTiles *>(data);
2244 cairo_surface_t *old_surface;
2246 if (gtk_widget_get_realized(GTK_WIDGET(rt->pr)))
2248 old_surface = rt->surface;
2249 rt->surface = gdk_window_create_similar_surface(gtk_widget_get_window(widget), CAIRO_CONTENT_COLOR, allocation->width, allocation->height);
2251 cr = cairo_create(rt->surface);
2253 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));
2255 cairo_set_source_surface(cr, old_surface, 0, 0);
2258 cairo_surface_destroy(old_surface);
2260 renderer_redraw(rt, allocation->x, allocation->y, allocation->width, allocation->height, FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2266 static gboolean rt_draw_cb(GtkWidget *, cairo_t *cr, gpointer data)
2268 auto rt = static_cast<RendererTiles *>(data);
2272 if (rt->stereo_mode & (PR_STEREO_HORIZ | PR_STEREO_VERT))
2274 cairo_push_group(cr);
2275 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));
2277 if (rt->stereo_mode & PR_STEREO_HORIZ)
2279 cairo_rectangle(cr, rt->stereo_off_x, 0, rt->pr->viewport_width, rt->pr->viewport_height);
2283 cairo_rectangle(cr, 0, rt->stereo_off_y, rt->pr->viewport_width, rt->pr->viewport_height);
2288 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);
2290 cairo_set_source_surface(cr, rt->surface, 0, 0);
2293 cairo_pop_group_to_source(cr);
2298 cairo_set_source_surface(cr, rt->surface, 0, 0);
2302 work = rt->overlay_list;
2305 od = static_cast<OverlayData *>(work->data);
2310 pw = gdk_pixbuf_get_width(od->pixbuf);
2311 ph = gdk_pixbuf_get_height(od->pixbuf);
2315 if (od->flags & OVL_RELATIVE)
2317 if (px < 0) px = rt->pr->viewport_width - pw + px;
2318 if (py < 0) py = rt->pr->viewport_height - ph + py;
2321 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px, py);
2329 RendererFuncs *renderer_tiles_new(PixbufRenderer *pr)
2331 auto rt = g_new0(RendererTiles, 1);
2335 rt->f.area_changed = renderer_area_changed;
2336 rt->f.update_pixbuf = renderer_update_pixbuf;
2337 rt->f.free = renderer_free;
2338 rt->f.update_zoom = renderer_update_zoom;
2339 rt->f.invalidate_region = renderer_invalidate_region;
2340 rt->f.scroll = rt_scroll;
2341 rt->f.update_viewport = renderer_update_viewport;
2344 rt->f.overlay_add = renderer_tiles_overlay_add;
2345 rt->f.overlay_set = renderer_tiles_overlay_set;
2346 rt->f.overlay_get = renderer_tiles_overlay_get;
2348 rt->f.stereo_set = renderer_stereo_set;
2350 rt->tile_width = options->image.tile_size;
2351 rt->tile_height = options->image.tile_size;
2353 rt->tiles = nullptr;
2354 rt->tile_cache_size = 0;
2356 rt->tile_cache_max = PR_CACHE_SIZE_DEFAULT;
2358 rt->draw_idle_id = 0;
2360 rt->stereo_mode = 0;
2361 rt->stereo_off_x = 0;
2362 rt->stereo_off_y = 0;
2364 rt->hidpi_scale = gtk_widget_get_scale_factor(GTK_WIDGET(rt->pr));
2366 g_signal_connect(G_OBJECT(pr), "hierarchy-changed",
2367 G_CALLBACK(rt_hierarchy_changed_cb), rt);
2369 g_signal_connect(G_OBJECT(pr), "draw",
2370 G_CALLBACK(rt_draw_cb), rt);
2371 g_signal_connect(G_OBJECT(pr), "realize", G_CALLBACK(rt_realize_cb), rt);
2372 g_signal_connect(G_OBJECT(pr), "size-allocate", G_CALLBACK(rt_size_allocate_cb), rt);
2374 return reinterpret_cast<RendererFuncs *>(rt);
2378 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */