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.
27 #include "renderer-tiles.h"
29 /* comment this out if not using this from within Geeqie
30 * defining GQ_BUILD does these things:
31 * - Sets the shift-click scroller pixbuf to a nice icon instead of a black box
37 #include "pixbuf-util.h"
40 enum ExifOrientationType {
41 EXIF_ORIENTATION_UNKNOWN = 0,
42 EXIF_ORIENTATION_TOP_LEFT = 1,
43 EXIF_ORIENTATION_TOP_RIGHT = 2,
44 EXIF_ORIENTATION_BOTTOM_RIGHT = 3,
45 EXIF_ORIENTATION_BOTTOM_LEFT = 4,
46 EXIF_ORIENTATION_LEFT_TOP = 5,
47 EXIF_ORIENTATION_RIGHT_TOP = 6,
48 EXIF_ORIENTATION_RIGHT_BOTTOM = 7,
49 EXIF_ORIENTATION_LEFT_BOTTOM = 8
57 cairo_surface_t *surface; /* off screen buffer */
58 GdkPixbuf *pixbuf; /* pixbuf area for zooming */
59 gint x; /* x offset into image */
60 gint y; /* y offset into image */
61 gint w; /* width that is visible (may be less if at edge of image) */
62 gint h; /* height '' */
66 /* render_todo: (explanation)
68 AREA render area of tile, usually only used when loading an image
69 note: will jump to an ALL if render_done is not ALL.
70 ALL render entire tile, if never done before w/ ALL, for expose events *only*
73 ImageRenderType render_todo; /* what to do (see above) */
74 ImageRenderType render_done; /* highest that has been done before on tile */
79 guint size; /* est. memory used by pixmap and pixbuf */
102 OverlayRendererFlags flags;
110 gint tile_cache_max; /* max MiB to use for offscreen buffer */
114 gint tile_cols; /* count of tile columns */
115 GList *tiles; /* list of buffer tiles */
116 gint tile_cache_size; /* allocated size of pixmaps/pixbufs */
117 GList *draw_queue; /* list of areas to redraw */
118 GList *draw_queue_2pass;/* list when 2 pass is enabled */
121 cairo_surface_t *overlay_buffer;
122 cairo_surface_t *surface;
124 guint draw_idle_id; /* event source id */
126 GdkPixbuf *spare_tile;
132 gint x_scroll; /* allow local adjustment and mirroring */
140 static void rt_border_draw(RendererTiles *rt, gint x, gint y, gint w, gint h);
141 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h, ImageTile *it);
144 static void rt_tile_free_all(RendererTiles *rt);
145 static void rt_tile_invalidate_region(RendererTiles *rt, gint x, gint y, gint w, gint h);
146 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it);
147 static void rt_queue_clear(RendererTiles *rt);
148 static void rt_queue_merge(QueueData *parent, QueueData *qd);
149 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
150 gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing);
152 static void rt_hierarchy_changed_cb(GtkWidget *widget, GtkWidget *previous_toplevel, gpointer data);
153 static gint rt_queue_draw_idle_cb(gpointer data);
155 #define GET_RIGHT_PIXBUF_OFFSET(rt) \
156 (( ((rt)->stereo_mode & PR_STEREO_RIGHT) && !((rt)->stereo_mode & PR_STEREO_SWAP)) || \
157 (!((rt)->stereo_mode & PR_STEREO_RIGHT) && ((rt)->stereo_mode & PR_STEREO_SWAP)) ? \
158 (rt)->pr->stereo_pixbuf_offset_right : (rt)->pr->stereo_pixbuf_offset_left )
160 #define GET_LEFT_PIXBUF_OFFSET(rt) \
161 ((!((rt)->stereo_mode & PR_STEREO_RIGHT) && !((rt)->stereo_mode & PR_STEREO_SWAP)) || \
162 ( ((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 )
166 static void rt_sync_scroll(RendererTiles *rt)
168 PixbufRenderer *pr = rt->pr;
170 rt->x_scroll = (rt->stereo_mode & PR_STEREO_MIRROR) ?
171 pr->width - pr->vis_width - pr->x_scroll
174 rt->y_scroll = (rt->stereo_mode & PR_STEREO_FLIP) ?
175 pr->height - pr->vis_height - pr->y_scroll
180 *-------------------------------------------------------------------
182 *-------------------------------------------------------------------
185 static void rt_border_draw(RendererTiles *rt, gint x, gint y, gint w, gint h)
187 PixbufRenderer *pr = rt->pr;
193 box = GTK_WIDGET(pr);
194 window = gtk_widget_get_window(box);
198 cr = cairo_create(rt->surface);
200 if (!pr->pixbuf && !pr->source_tiles_enabled)
202 if (pr_clip_region(x, y, w, h,
204 pr->viewport_width, pr->viewport_height,
207 cairo_set_source_rgb(cr, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
208 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
210 rt_overlay_draw(rt, rx, ry, rw, rh, nullptr);
216 if (pr->vis_width < pr->viewport_width)
218 if (pr->x_offset > 0 &&
219 pr_clip_region(x, y, w, h,
221 pr->x_offset, pr->viewport_height,
224 cairo_set_source_rgb(cr, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
225 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
227 rt_overlay_draw(rt, rx, ry, rw, rh, nullptr);
229 if (pr->viewport_width - pr->vis_width - pr->x_offset > 0 &&
230 pr_clip_region(x, y, w, h,
231 pr->x_offset + pr->vis_width, 0,
232 pr->viewport_width - pr->vis_width - pr->x_offset, pr->viewport_height,
235 cairo_set_source_rgb(cr, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
236 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
238 rt_overlay_draw(rt, rx, ry, rw, rh, nullptr);
241 if (pr->vis_height < pr->viewport_height)
243 if (pr->y_offset > 0 &&
244 pr_clip_region(x, y, w, h,
246 pr->vis_width, pr->y_offset,
249 cairo_set_source_rgb(cr, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
250 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
252 rt_overlay_draw(rt, rx, ry, rw, rh, nullptr);
254 if (pr->viewport_height - pr->vis_height - pr->y_offset > 0 &&
255 pr_clip_region(x, y, w, h,
256 pr->x_offset, pr->y_offset + pr->vis_height,
257 pr->vis_width, pr->viewport_height - pr->vis_height - pr->y_offset,
260 cairo_set_source_rgb(cr, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
261 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
263 rt_overlay_draw(rt, rx, ry, rw, rh, nullptr);
269 static void rt_border_clear(RendererTiles *rt)
271 PixbufRenderer *pr = rt->pr;
272 rt_border_draw(rt, 0, 0, pr->viewport_width, pr->viewport_height);
277 *-------------------------------------------------------------------
279 *-------------------------------------------------------------------
282 static ImageTile *rt_tile_new(gint x, gint y, gint width, gint height)
284 auto it = g_new0(ImageTile, 1);
291 it->render_done = TILE_RENDER_NONE;
296 static void rt_tile_free(ImageTile *it)
300 if (it->pixbuf) g_object_unref(it->pixbuf);
301 if (it->surface) cairo_surface_destroy(it->surface);
306 static void rt_tile_free_all(RendererTiles *rt)
308 g_list_free_full(rt->tiles, reinterpret_cast<GDestroyNotify>(rt_tile_free));
310 rt->tile_cache_size = 0;
313 static ImageTile *rt_tile_add(RendererTiles *rt, gint x, gint y)
315 PixbufRenderer *pr = rt->pr;
318 it = rt_tile_new(x, y, rt->tile_width, rt->tile_height);
320 if (it->x + it->w > pr->width) it->w = pr->width - it->x;
321 if (it->y + it->h > pr->height) it->h = pr->height - it->y;
323 rt->tiles = g_list_prepend(rt->tiles, it);
324 rt->tile_cache_size += it->size;
329 static void rt_tile_remove(RendererTiles *rt, ImageTile *it)
333 QueueData *qd = it->qd;
336 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
342 QueueData *qd = it->qd2;
345 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
349 rt->tiles = g_list_remove(rt->tiles, it);
350 rt->tile_cache_size -= it->size;
355 static void rt_tile_free_space(RendererTiles *rt, guint space, ImageTile *it)
357 PixbufRenderer *pr = rt->pr;
361 work = g_list_last(rt->tiles);
363 if (pr->source_tiles_enabled && pr->scale < 1.0)
367 tiles = (pr->vis_width / rt->tile_width + 1) * (pr->vis_height / rt->tile_height + 1);
368 tile_max = MAX(tiles * rt->tile_width * rt->tile_height * 3,
369 (gint)((gdouble)rt->tile_cache_max * 1048576.0 * pr->scale));
373 tile_max = rt->tile_cache_max * 1048576;
376 while (work && rt->tile_cache_size + space > tile_max)
380 needle = static_cast<ImageTile *>(work->data);
383 ((!needle->qd && !needle->qd2) || !rt_tile_is_visible(rt, needle))) rt_tile_remove(rt, needle);
387 static void rt_tile_invalidate_all(RendererTiles *rt)
389 PixbufRenderer *pr = rt->pr;
397 it = static_cast<ImageTile *>(work->data);
400 it->render_done = TILE_RENDER_NONE;
401 it->render_todo = TILE_RENDER_ALL;
404 it->w = MIN(rt->tile_width, pr->width - it->x);
405 it->h = MIN(rt->tile_height, pr->height - it->y);
409 static void rt_tile_invalidate_region(RendererTiles *rt, gint x, gint y, gint w, gint h)
415 x1 = ROUND_DOWN(x, rt->tile_width);
416 x2 = ROUND_UP(x + w, rt->tile_width);
418 y1 = ROUND_DOWN(y, rt->tile_height);
419 y2 = ROUND_UP(y + h, rt->tile_height);
426 it = static_cast<ImageTile *>(work->data);
429 if (it->x < x2 && it->x + it->w > x1 &&
430 it->y < y2 && it->y + it->h > y1)
432 it->render_done = TILE_RENDER_NONE;
433 it->render_todo = TILE_RENDER_ALL;
438 static ImageTile *rt_tile_get(RendererTiles *rt, gint x, gint y, gboolean only_existing)
447 it = static_cast<ImageTile *>(work->data);
448 if (it->x == x && it->y == y)
450 rt->tiles = g_list_delete_link(rt->tiles, work);
451 rt->tiles = g_list_prepend(rt->tiles, it);
458 if (only_existing) return nullptr;
460 return rt_tile_add(rt, x, y);
463 static gint pixmap_calc_size(cairo_surface_t *)
465 return options->image.tile_size * options->image.tile_size * 4 / 8;
468 static void rt_hidpi_aware_draw(
475 cairo_surface_t *surface;
476 surface = gdk_cairo_surface_create_from_pixbuf(pixbuf, rt->hidpi_scale, nullptr);
477 cairo_set_source_surface(cr, surface, x, y);
479 cairo_surface_destroy(surface);
482 static void rt_tile_prepare(RendererTiles *rt, ImageTile *it)
484 PixbufRenderer *pr = rt->pr;
487 cairo_surface_t *surface;
490 surface = gdk_window_create_similar_surface(gtk_widget_get_window(reinterpret_cast<GtkWidget *>(pr)),
492 rt->tile_width, rt->tile_height);
494 size = pixmap_calc_size(surface) * rt->hidpi_scale * rt->hidpi_scale;
495 rt_tile_free_space(rt, size, it);
497 it->surface = surface;
499 rt->tile_cache_size += size;
506 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, rt->hidpi_scale * rt->tile_width, rt->hidpi_scale * rt->tile_height);
508 size = gdk_pixbuf_get_rowstride(pixbuf) * rt->tile_height * rt->hidpi_scale;
509 rt_tile_free_space(rt, size, it);
513 rt->tile_cache_size += size;
518 *-------------------------------------------------------------------
520 *-------------------------------------------------------------------
523 static void rt_overlay_get_position(RendererTiles *rt, OverlayData *od,
524 gint *x, gint *y, gint *w, gint *h)
526 PixbufRenderer *pr = rt->pr;
529 pw = gdk_pixbuf_get_width(od->pixbuf);
530 ph = gdk_pixbuf_get_height(od->pixbuf);
534 if (od->flags & OVL_RELATIVE)
536 if (px < 0) px = pr->viewport_width - pw + px;
537 if (py < 0) py = pr->viewport_height - ph + py;
546 static void rt_overlay_init_window(RendererTiles *rt, OverlayData *od)
548 PixbufRenderer *pr = rt->pr;
550 GdkWindowAttr attributes;
551 gint attributes_mask;
553 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
555 attributes.window_type = GDK_WINDOW_CHILD;
556 attributes.wclass = GDK_INPUT_OUTPUT;
557 attributes.width = pw;
558 attributes.height = ph;
559 attributes.event_mask = GDK_EXPOSURE_MASK;
562 od->window = gdk_window_new(gtk_widget_get_window(GTK_WIDGET(pr)), &attributes, attributes_mask);
563 gdk_window_set_user_data(od->window, pr);
564 gdk_window_move(od->window, px + rt->stereo_off_x, py + rt->stereo_off_y);
565 gdk_window_show(od->window);
568 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h,
571 PixbufRenderer *pr = rt->pr;
574 work = rt->overlay_list;
581 od = static_cast<OverlayData *>(work->data);
584 if (!od->window) rt_overlay_init_window(rt, od);
586 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
587 if (pr_clip_region(x, y, w, h, px, py, pw, ph, &rx, &ry, &rw, &rh))
589 if (!rt->overlay_buffer)
591 rt->overlay_buffer = gdk_window_create_similar_surface(gtk_widget_get_window(reinterpret_cast<GtkWidget *>(pr)),
593 rt->tile_width, rt->tile_height);
600 cr = cairo_create(rt->overlay_buffer);
601 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);
602 cairo_rectangle(cr, 0, 0, rw, rh);
603 cairo_fill_preserve(cr);
605 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - rx, py - ry);
609 cr = gdk_cairo_create(od->window);
610 cairo_set_source_surface(cr, rt->overlay_buffer, rx - px, ry - py);
611 cairo_rectangle (cr, rx - px, ry - py, rw, rh);
617 /* no ImageTile means region may be larger than our scratch buffer */
620 for (sx = rx; sx < rx + rw; sx += rt->tile_width)
621 for (sy = ry; sy < ry + rh; sy += rt->tile_height)
626 sw = MIN(rx + rw - sx, rt->tile_width);
627 sh = MIN(ry + rh - sy, rt->tile_height);
629 cr = cairo_create(rt->overlay_buffer);
630 cairo_set_source_rgb(cr, 0, 0, 0);
631 cairo_rectangle(cr, 0, 0, sw, sh);
632 cairo_fill_preserve(cr);
634 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - sx, py - sy);
638 cr = gdk_cairo_create(od->window);
639 cairo_set_source_surface(cr, rt->overlay_buffer, sx - px, sy - py);
640 cairo_rectangle (cr, sx - px, sy - py, sw, sh);
649 static void rt_overlay_queue_draw(RendererTiles *rt, OverlayData *od, gint x1, gint y1, gint x2, gint y2)
651 PixbufRenderer *pr = rt->pr;
654 rt_overlay_get_position(rt, od, &x, &y, &w, &h);
662 rt_queue(rt, rt->x_scroll - pr->x_offset + x,
663 rt->y_scroll - pr->y_offset + y,
665 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
667 rt_border_draw(rt, x, y, w, h);
670 static void rt_overlay_queue_all(RendererTiles *rt, gint x1, gint y1, gint x2, gint y2)
674 work = rt->overlay_list;
677 auto od = static_cast<OverlayData *>(work->data);
680 rt_overlay_queue_draw(rt, od, x1, y1, x2, y2);
684 static void rt_overlay_update_sizes(RendererTiles *rt)
688 work = rt->overlay_list;
691 auto od = static_cast<OverlayData *>(work->data);
694 if (!od->window) rt_overlay_init_window(rt, od);
696 if (od->flags & OVL_RELATIVE)
700 rt_overlay_get_position(rt, od, &x, &y, &w, &h);
701 gdk_window_move_resize(od->window, x + rt->stereo_off_x, y + rt->stereo_off_y, w, h);
706 static OverlayData *rt_overlay_find(RendererTiles *rt, gint id)
710 work = rt->overlay_list;
713 auto od = static_cast<OverlayData *>(work->data);
716 if (od->id == id) return od;
723 gint renderer_tiles_overlay_add(void *renderer, GdkPixbuf *pixbuf, gint x, gint y,
724 OverlayRendererFlags flags)
726 auto rt = static_cast<RendererTiles *>(renderer);
727 PixbufRenderer *pr = rt->pr;
730 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
731 g_return_val_if_fail(pixbuf != nullptr, -1);
734 while (rt_overlay_find(rt, id)) id++;
736 auto od = g_new0(OverlayData, 1);
739 g_object_ref(G_OBJECT(od->pixbuf));
744 rt_overlay_init_window(rt, od);
746 rt->overlay_list = g_list_append(rt->overlay_list, od);
748 gtk_widget_queue_draw(GTK_WIDGET(rt->pr));
753 static void rt_overlay_free(RendererTiles *rt, OverlayData *od)
755 rt->overlay_list = g_list_remove(rt->overlay_list, od);
757 if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
758 if (od->window) gdk_window_destroy(od->window);
761 if (!rt->overlay_list && rt->overlay_buffer)
763 cairo_surface_destroy(rt->overlay_buffer);
764 rt->overlay_buffer = nullptr;
768 static void rt_overlay_list_clear(RendererTiles *rt)
770 while (rt->overlay_list)
772 auto od = static_cast<OverlayData *>(rt->overlay_list->data);
773 rt_overlay_free(rt, od);
777 static void rt_overlay_list_reset_window(RendererTiles *rt)
781 if (rt->overlay_buffer) cairo_surface_destroy(rt->overlay_buffer);
782 rt->overlay_buffer = nullptr;
784 work = rt->overlay_list;
787 auto od = static_cast<OverlayData *>(work->data);
789 if (od->window) gdk_window_destroy(od->window);
790 od->window = nullptr;
794 void renderer_tiles_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint, gint)
796 auto rc = static_cast<RendererTiles *>(renderer);
797 PixbufRenderer *pr = rc->pr;
800 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
802 od = rt_overlay_find(rc, id);
807 g_object_ref(G_OBJECT(pixbuf));
808 g_object_unref(G_OBJECT(od->pixbuf));
813 rt_overlay_free(rc, od);
816 gtk_widget_queue_draw(GTK_WIDGET(rc->pr));
819 gboolean renderer_tiles_overlay_get(void *renderer, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
821 auto rt = static_cast<RendererTiles *>(renderer);
822 PixbufRenderer *pr = rt->pr;
825 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
827 od = rt_overlay_find(rt, id);
828 if (!od) return FALSE;
830 if (pixbuf) *pixbuf = od->pixbuf;
837 static void rt_hierarchy_changed_cb(GtkWidget *, GtkWidget *, gpointer data)
839 auto rt = static_cast<RendererTiles *>(data);
840 rt_overlay_list_reset_window(rt);
844 *-------------------------------------------------------------------
846 *-------------------------------------------------------------------
849 static GdkPixbuf *rt_get_spare_tile(RendererTiles *rt)
851 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);
852 return rt->spare_tile;
855 #define COLOR_BYTES 3 /* rgb */
857 static void rt_tile_rotate_90_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
859 GdkPixbuf *src = *tile;
862 guchar *s_pix, *d_pix;
864 guchar *ip, *spi, *dpi;
866 gint tw = rt->tile_width * rt->hidpi_scale;
868 srs = gdk_pixbuf_get_rowstride(src);
869 s_pix = gdk_pixbuf_get_pixels(src);
870 spi = s_pix + (x * COLOR_BYTES);
872 dest = rt_get_spare_tile(rt);
873 drs = gdk_pixbuf_get_rowstride(dest);
874 d_pix = gdk_pixbuf_get_pixels(dest);
875 dpi = d_pix + (tw - 1) * COLOR_BYTES;
877 for (i = y; i < y + h; i++)
879 sp = spi + (i * srs);
880 ip = dpi - (i * COLOR_BYTES);
881 for (j = x; j < x + w; j++)
884 memcpy(dp, sp, COLOR_BYTES);
889 rt->spare_tile = src;
893 static void rt_tile_rotate_90_counter_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
895 GdkPixbuf *src = *tile;
898 guchar *s_pix, *d_pix;
900 guchar *ip, *spi, *dpi;
902 gint th = rt->tile_height * rt->hidpi_scale;
904 srs = gdk_pixbuf_get_rowstride(src);
905 s_pix = gdk_pixbuf_get_pixels(src);
906 spi = s_pix + (x * COLOR_BYTES);
908 dest = rt_get_spare_tile(rt);
909 drs = gdk_pixbuf_get_rowstride(dest);
910 d_pix = gdk_pixbuf_get_pixels(dest);
911 dpi = d_pix + (th - 1) * drs;
913 for (i = y; i < y + h; i++)
915 sp = spi + (i * srs);
916 ip = dpi + (i * COLOR_BYTES);
917 for (j = x; j < x + w; j++)
920 memcpy(dp, sp, COLOR_BYTES);
925 rt->spare_tile = src;
929 static void rt_tile_mirror_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
931 GdkPixbuf *src = *tile;
934 guchar *s_pix, *d_pix;
939 gint tw = rt->tile_width * rt->hidpi_scale;
941 srs = gdk_pixbuf_get_rowstride(src);
942 s_pix = gdk_pixbuf_get_pixels(src);
943 spi = s_pix + (x * COLOR_BYTES);
945 dest = rt_get_spare_tile(rt);
946 drs = gdk_pixbuf_get_rowstride(dest);
947 d_pix = gdk_pixbuf_get_pixels(dest);
948 dpi = d_pix + (tw - x - 1) * COLOR_BYTES;
950 for (i = y; i < y + h; i++)
952 sp = spi + (i * srs);
953 dp = dpi + (i * drs);
954 for (j = 0; j < w; j++)
956 memcpy(dp, sp, COLOR_BYTES);
962 rt->spare_tile = src;
966 static void rt_tile_mirror_and_flip(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
968 GdkPixbuf *src = *tile;
971 guchar *s_pix, *d_pix;
975 gint tw = rt->tile_width * rt->hidpi_scale;
976 gint th = rt->tile_height * rt->hidpi_scale;
978 srs = gdk_pixbuf_get_rowstride(src);
979 s_pix = gdk_pixbuf_get_pixels(src);
981 dest = rt_get_spare_tile(rt);
982 drs = gdk_pixbuf_get_rowstride(dest);
983 d_pix = gdk_pixbuf_get_pixels(dest);
984 dpi = d_pix + (th - 1) * drs + (tw - 1) * COLOR_BYTES;
986 for (i = y; i < y + h; i++)
988 sp = s_pix + (i * srs) + (x * COLOR_BYTES);
989 dp = dpi - (i * drs) - (x * COLOR_BYTES);
990 for (j = 0; j < w; j++)
992 memcpy(dp, sp, COLOR_BYTES);
998 rt->spare_tile = src;
1002 static void rt_tile_flip_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1004 GdkPixbuf *src = *tile;
1007 guchar *s_pix, *d_pix;
1011 gint th = rt->tile_height * rt->hidpi_scale;
1013 srs = gdk_pixbuf_get_rowstride(src);
1014 s_pix = gdk_pixbuf_get_pixels(src);
1015 spi = s_pix + (x * COLOR_BYTES);
1017 dest = rt_get_spare_tile(rt);
1018 drs = gdk_pixbuf_get_rowstride(dest);
1019 d_pix = gdk_pixbuf_get_pixels(dest);
1020 dpi = d_pix + (th - 1) * drs + (x * COLOR_BYTES);
1022 for (i = y; i < y + h; i++)
1024 sp = spi + (i * srs);
1025 dp = dpi - (i * drs);
1026 memcpy(dp, sp, w * COLOR_BYTES);
1029 rt->spare_tile = src;
1033 static void rt_tile_apply_orientation(RendererTiles *rt, gint orientation, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h)
1035 switch (orientation)
1037 case EXIF_ORIENTATION_TOP_LEFT:
1038 /* normal -- nothing to do */
1040 case EXIF_ORIENTATION_TOP_RIGHT:
1043 rt_tile_mirror_only(rt, pixbuf, x, y, w, h);
1046 case EXIF_ORIENTATION_BOTTOM_RIGHT:
1049 rt_tile_mirror_and_flip(rt, pixbuf, x, y, w, h);
1052 case EXIF_ORIENTATION_BOTTOM_LEFT:
1055 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1058 case EXIF_ORIENTATION_LEFT_TOP:
1060 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1061 rt_tile_rotate_90_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1064 case EXIF_ORIENTATION_RIGHT_TOP:
1065 /* rotated -90 (270) */
1067 rt_tile_rotate_90_clockwise(rt, pixbuf, x, y, w, h);
1070 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1072 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1073 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1076 case EXIF_ORIENTATION_LEFT_BOTTOM:
1079 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, y, w, h);
1083 /* The other values are out of range */
1088 static gboolean rt_source_tile_render(RendererTiles *rt, ImageTile *it,
1089 gint x, gint y, gint w, gint h,
1090 gboolean, gboolean fast)
1092 PixbufRenderer *pr = rt->pr;
1095 gboolean draw = FALSE;
1097 if (pr->zoom == 1.0 || pr->scale == 1.0)
1099 list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE);
1104 gint rx, ry, rw, rh;
1106 st = static_cast<SourceTile *>(work->data);
1109 if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1110 it->x + x, it->y + y, w, h,
1111 &rx, &ry, &rw, &rh))
1114 cr = cairo_create(it->surface);
1115 cairo_rectangle (cr, rx - it->x, ry - it->y, rw, rh);
1119 cairo_set_source_rgb(cr, 0, 0, 0);
1122 else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
1124 rt_hidpi_aware_draw(rt, cr, st->pixbuf, -it->x + st->x, -it->y + st->y);
1132 gdouble scale_x, scale_y;
1133 gint sx, sy, sw, sh;
1135 if (pr->image_width == 0 || pr->image_height == 0) return FALSE;
1136 scale_x = static_cast<gdouble>(pr->width) / pr->image_width;
1137 scale_y = static_cast<gdouble>(pr->height) / pr->image_height;
1139 sx = static_cast<gdouble>(it->x + x) / scale_x;
1140 sy = static_cast<gdouble>(it->y + y) / scale_y;
1141 sw = static_cast<gdouble>(w) / scale_x;
1142 sh = static_cast<gdouble>(h) / scale_y;
1144 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1147 /* draws red over draw region, to check for leaks (regions not filled) */
1148 pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255);
1151 list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE);
1156 gint rx, ry, rw, rh;
1157 gint stx, sty, stw, sth;
1159 st = static_cast<SourceTile *>(work->data);
1162 stx = floor(static_cast<gdouble>(st->x) * scale_x);
1163 sty = floor(static_cast<gdouble>(st->y) * scale_y);
1164 stw = ceil(static_cast<gdouble>(st->x + pr->source_tile_width) * scale_x) - stx;
1165 sth = ceil(static_cast<gdouble>(st->y + pr->source_tile_height) * scale_y) - sty;
1167 if (pr_clip_region(stx, sty, stw, sth,
1168 it->x + x, it->y + y, w, h,
1169 &rx, &ry, &rw, &rh))
1175 cr = cairo_create(it->surface);
1176 cairo_rectangle (cr, rx - st->x, ry - st->y, rw, rh);
1177 cairo_set_source_rgb(cr, 0, 0, 0);
1186 /* may need to use unfloored stx,sty values here */
1187 offset_x = static_cast<gdouble>(stx - it->x);
1188 offset_y = static_cast<gdouble>(sty - it->y);
1190 gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh,
1191 static_cast<gdouble>(0.0) + offset_x,
1192 static_cast<gdouble>(0.0) + offset_y,
1194 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1209 * @param ignore_alpha
1220 * @param interp_type
1223 * @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
1227 static void rt_tile_get_region(gboolean has_alpha, gboolean ignore_alpha,
1228 const GdkPixbuf *src, GdkPixbuf *dest,
1229 int pb_x, int pb_y, int pb_w, int pb_h,
1230 double offset_x, double offset_y, double scale_x, double scale_y,
1231 GdkInterpType interp_type,
1232 int check_x, int check_y, gboolean wide_image)
1234 GdkPixbuf* tmppixbuf;
1253 if (scale_x == 1.0 && scale_y == 1.0)
1257 srs = gdk_pixbuf_get_rowstride(src);
1258 drs = gdk_pixbuf_get_rowstride(dest);
1259 psrc = gdk_pixbuf_get_pixels(src);
1260 pdst = gdk_pixbuf_get_pixels(dest);
1261 for (y = 0; y < pb_h; y++)
1263 for (x = 0; x < pb_w; x++)
1265 for (c = 0; c < 3; c++)
1267 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];
1274 gdk_pixbuf_copy_area(src,
1275 -offset_x + pb_x, -offset_y + pb_y,
1283 gdk_pixbuf_scale(src, dest,
1284 pb_x, pb_y, pb_w, pb_h,
1288 (wide_image && interp_type == GDK_INTERP_NEAREST) ? GDK_INTERP_TILES : interp_type);
1293 red_1=static_cast<guint32>(options->image.alpha_color_1.red * 255) << 16 & 0x00FF0000;
1294 green_1=static_cast<guint32>(options->image.alpha_color_1.green * 255) << 8 & 0x0000FF00;
1295 blue_1=static_cast<guint32>(options->image.alpha_color_1.blue * 255) & 0x000000FF;
1296 red_2=static_cast<guint32>(options->image.alpha_color_2.red * 255) << 16 & 0x00FF0000;
1297 green_2=static_cast<guint32>(options->image.alpha_color_2.green * 255) << 8 & 0x0000FF00;
1298 blue_2=static_cast<guint32>(options->image.alpha_color_2.blue * 255) & 0x000000FF;
1299 alpha_1 = red_1 + green_1 + blue_1;
1300 alpha_2 = red_2 + green_2 + blue_2;
1304 tmppixbuf = gdk_pixbuf_add_alpha(src, FALSE, 0, 0, 0);
1306 pixbuf_ignore_alpha_rect(tmppixbuf, 0, 0, gdk_pixbuf_get_width(src), gdk_pixbuf_get_height(src));
1308 gdk_pixbuf_composite_color(tmppixbuf, dest,
1309 pb_x, pb_y, pb_w, pb_h,
1313 (scale_x == 1.0 && scale_y == 1.0) ? GDK_INTERP_NEAREST : interp_type,
1314 255, check_x, check_y,
1315 PR_ALPHA_CHECK_SIZE,
1318 g_object_unref(tmppixbuf);
1322 gdk_pixbuf_composite_color(src, dest,
1323 pb_x, pb_y, pb_w, pb_h,
1327 (scale_x == 1.0 && scale_y == 1.0) ? GDK_INTERP_NEAREST : interp_type,
1328 255, check_x, check_y,
1329 PR_ALPHA_CHECK_SIZE,
1337 static gint rt_get_orientation(RendererTiles *rt)
1339 PixbufRenderer *pr = rt->pr;
1341 gint orientation = pr->orientation;
1342 static const gint mirror[] = {1, 2, 1, 4, 3, 6, 5, 8, 7};
1343 static const gint flip[] = {1, 4, 3, 2, 1, 8, 7, 6, 5};
1345 if (rt->stereo_mode & PR_STEREO_MIRROR) orientation = mirror[orientation];
1346 if (rt->stereo_mode & PR_STEREO_FLIP) orientation = flip[orientation];
1351 static void rt_tile_render(RendererTiles *rt, ImageTile *it,
1352 gint x, gint y, gint w, gint h,
1353 gboolean new_data, gboolean fast)
1355 PixbufRenderer *pr = rt->pr;
1357 gboolean draw = FALSE;
1358 gint orientation = rt_get_orientation(rt);
1359 gboolean wide_image = FALSE;
1361 if (it->render_todo == TILE_RENDER_NONE && it->surface && !new_data) return;
1363 if (it->render_done != TILE_RENDER_ALL)
1369 if (!fast) it->render_done = TILE_RENDER_ALL;
1371 else if (it->render_todo != TILE_RENDER_AREA)
1373 if (!fast) it->render_todo = TILE_RENDER_NONE;
1377 if (!fast) it->render_todo = TILE_RENDER_NONE;
1379 if (new_data) it->blank = FALSE;
1381 rt_tile_prepare(rt, it);
1382 has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
1384 /** @FIXME checker colors for alpha should be configurable,
1385 * also should be drawn for blank = TRUE
1390 /* no data, do fast rect fill */
1392 cr = cairo_create(it->surface);
1393 cairo_rectangle (cr, 0, 0, it->w, it->h);
1394 cairo_set_source_rgb(cr, 0, 0, 0);
1398 else if (pr->source_tiles_enabled)
1400 draw = rt_source_tile_render(rt, it, x, y, w, h, new_data, fast);
1404 gdouble scale_x, scale_y;
1405 gdouble src_x, src_y;
1409 if (pr->image_width == 0 || pr->image_height == 0) return;
1411 scale_x = rt->hidpi_scale * static_cast<gdouble>(pr->width) / pr->image_width;
1412 scale_y = rt->hidpi_scale * static_cast<gdouble>(pr->height) / pr->image_height;
1414 pr_tile_coords_map_orientation(orientation, it->x, it->y,
1415 pr->width, pr->height,
1416 rt->tile_width, rt->tile_height,
1418 pr_tile_region_map_orientation(orientation, x, y,
1419 rt->tile_width, rt->tile_height,
1424 src_x *= rt->hidpi_scale;
1425 src_y *= rt->hidpi_scale;
1426 pb_x *= rt->hidpi_scale;
1427 pb_y *= rt->hidpi_scale;
1428 pb_w *= rt->hidpi_scale;
1429 pb_h *= rt->hidpi_scale;
1431 switch (orientation)
1434 case EXIF_ORIENTATION_LEFT_TOP:
1435 case EXIF_ORIENTATION_RIGHT_TOP:
1436 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1437 case EXIF_ORIENTATION_LEFT_BOTTOM:
1447 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
1448 * small sizes for anything but GDK_INTERP_NEAREST
1450 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1451 if (pr->image_width > 32767) wide_image = TRUE;
1453 rt_tile_get_region(has_alpha, pr->ignore_alpha,
1454 pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
1455 static_cast<gdouble>(0.0) - src_x - GET_RIGHT_PIXBUF_OFFSET(rt) * scale_x,
1456 static_cast<gdouble>(0.0) - src_y,
1458 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1459 it->x + pb_x, it->y + pb_y, wide_image);
1460 if (rt->stereo_mode & PR_STEREO_ANAGLYPH &&
1461 (pr->stereo_pixbuf_offset_right > 0 || pr->stereo_pixbuf_offset_left > 0))
1463 GdkPixbuf *right_pb = rt_get_spare_tile(rt);
1464 rt_tile_get_region(has_alpha, pr->ignore_alpha,
1465 pr->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h,
1466 static_cast<gdouble>(0.0) - src_x - GET_LEFT_PIXBUF_OFFSET(rt) * scale_x,
1467 static_cast<gdouble>(0.0) - src_y,
1469 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1470 it->x + pb_x, it->y + pb_y, wide_image);
1471 pr_create_anaglyph(rt->stereo_mode, it->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h);
1472 /* do not care about freeing spare_tile, it will be reused */
1474 rt_tile_apply_orientation(rt, orientation, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
1478 if (draw && it->pixbuf && !it->blank)
1482 if (pr->func_post_process && !(pr->post_process_slow && fast))
1483 pr->func_post_process(pr, &it->pixbuf, x, y, w, h, pr->post_process_user_data);
1485 cr = cairo_create(it->surface);
1486 cairo_rectangle (cr, x, y, w, h);
1487 rt_hidpi_aware_draw(rt, cr, it->pixbuf, 0, 0);
1493 static void rt_tile_expose(RendererTiles *rt, ImageTile *it,
1494 gint x, gint y, gint w, gint h,
1495 gboolean new_data, gboolean fast)
1497 PixbufRenderer *pr = rt->pr;
1500 /* clamp to visible */
1501 if (it->x + x < rt->x_scroll)
1503 w -= rt->x_scroll - it->x - x;
1504 x = rt->x_scroll - it->x;
1506 if (it->x + x + w > rt->x_scroll + pr->vis_width)
1508 w = rt->x_scroll + pr->vis_width - it->x - x;
1511 if (it->y + y < rt->y_scroll)
1513 h -= rt->y_scroll - it->y - y;
1514 y = rt->y_scroll - it->y;
1516 if (it->y + y + h > rt->y_scroll + pr->vis_height)
1518 h = rt->y_scroll + pr->vis_height - it->y - y;
1522 rt_tile_render(rt, it, x, y, w, h, new_data, fast);
1524 cr = cairo_create(rt->surface);
1525 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);
1526 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);
1530 if (rt->overlay_list)
1532 rt_overlay_draw(rt, pr->x_offset + (it->x - rt->x_scroll) + x,
1533 pr->y_offset + (it->y - rt->y_scroll) + y,
1538 gtk_widget_queue_draw(GTK_WIDGET(rt->pr));
1542 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it)
1544 PixbufRenderer *pr = rt->pr;
1545 return (it->x + it->w >= rt->x_scroll && it->x < rt->x_scroll + pr->vis_width &&
1546 it->y + it->h >= rt->y_scroll && it->y < rt->y_scroll + pr->vis_height);
1550 *-------------------------------------------------------------------
1552 *-------------------------------------------------------------------
1555 static gint rt_get_queued_area(GList *work)
1561 auto qd = static_cast<QueueData *>(work->data);
1562 area += qd->w * qd->h;
1569 static gboolean rt_queue_schedule_next_draw(RendererTiles *rt, gboolean force_set)
1571 PixbufRenderer *pr = rt->pr;
1573 gint visible_area = pr->vis_width * pr->vis_height;
1578 DEBUG_2("redraw priority: 2pass");
1579 rt->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, rt_queue_draw_idle_cb, rt, nullptr);
1580 return G_SOURCE_REMOVE;
1583 if (visible_area == 0)
1590 percent = 100.0 * rt_get_queued_area(rt->draw_queue) / visible_area;
1595 /* we have enough data for starting intensive redrawing */
1596 DEBUG_2("redraw priority: high %.2f %%", percent);
1597 rt->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW, rt_queue_draw_idle_cb, rt, nullptr);
1598 return G_SOURCE_REMOVE;
1601 if (percent < 1.0 || force_set)
1603 /* queue is (almost) empty, wait 50 ms*/
1604 DEBUG_2("redraw priority: wait %.2f %%", percent);
1605 rt->draw_idle_id = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, 50, rt_queue_draw_idle_cb, rt, nullptr);
1606 return G_SOURCE_REMOVE;
1609 /* keep the same priority as before */
1610 DEBUG_2("redraw priority: no change %.2f %%", percent);
1611 return G_SOURCE_CONTINUE;
1615 static gboolean rt_queue_draw_idle_cb(gpointer data)
1617 auto rt = static_cast<RendererTiles *>(data);
1618 PixbufRenderer *pr = rt->pr;
1623 if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
1624 (!rt->draw_queue && !rt->draw_queue_2pass) ||
1627 pr_render_complete_signal(pr);
1629 rt->draw_idle_id = 0;
1630 return G_SOURCE_REMOVE;
1635 qd = static_cast<QueueData *>(rt->draw_queue->data);
1636 fast = (pr->zoom_2pass && ((pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0) || pr->post_process_slow));
1642 /* still loading, wait till done (also drops the higher priority) */
1644 return rt_queue_schedule_next_draw(rt, FALSE);
1647 qd = static_cast<QueueData *>(rt->draw_queue_2pass->data);
1651 if (gtk_widget_get_realized(GTK_WIDGET(pr)))
1653 if (rt_tile_is_visible(rt, qd->it))
1655 rt_tile_expose(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1657 else if (qd->new_data)
1659 /* if new pixel data, and we already have a pixmap, update the tile */
1660 qd->it->blank = FALSE;
1661 if (qd->it->surface && qd->it->render_done == TILE_RENDER_ALL)
1663 rt_tile_render(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1670 qd->it->qd = nullptr;
1671 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
1676 rt_queue_merge(qd->it->qd2, qd);
1682 rt->draw_queue_2pass = g_list_append(rt->draw_queue_2pass, qd);
1692 qd->it->qd2 = nullptr;
1693 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
1697 if (!rt->draw_queue && !rt->draw_queue_2pass)
1699 pr_render_complete_signal(pr);
1701 rt->draw_idle_id = 0;
1702 return G_SOURCE_REMOVE;
1705 return rt_queue_schedule_next_draw(rt, FALSE);
1708 static void rt_queue_list_free(GList *list)
1717 qd = static_cast<QueueData *>(work->data);
1720 qd->it->qd = nullptr;
1721 qd->it->qd2 = nullptr;
1728 static void rt_queue_clear(RendererTiles *rt)
1730 rt_queue_list_free(rt->draw_queue);
1731 rt->draw_queue = nullptr;
1733 rt_queue_list_free(rt->draw_queue_2pass);
1734 rt->draw_queue_2pass = nullptr;
1736 if (rt->draw_idle_id)
1738 g_source_remove(rt->draw_idle_id);
1739 rt->draw_idle_id = 0;
1744 static void rt_queue_merge(QueueData *parent, QueueData *qd)
1746 if (parent->x + parent->w < qd->x + qd->w)
1748 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
1750 if (parent->x > qd->x)
1752 parent->w += parent->x - qd->x;
1756 if (parent->y + parent->h < qd->y + qd->h)
1758 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
1760 if (parent->y > qd->y)
1762 parent->h += parent->y - qd->y;
1766 parent->new_data |= qd->new_data;
1769 static gboolean rt_clamp_to_visible(RendererTiles *rt, gint *x, gint *y, gint *w, gint *h)
1771 PixbufRenderer *pr = rt->pr;
1778 vh = pr->vis_height;
1783 if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
1786 nx = CLAMP(*x, vx, vx + vw);
1787 nw = CLAMP(*w - (nx - *x), 1, vw);
1789 ny = CLAMP(*y, vy, vy + vh);
1790 nh = CLAMP(*h - (ny - *y), 1, vh);
1800 static gboolean rt_queue_to_tiles(RendererTiles *rt, gint x, gint y, gint w, gint h,
1801 gboolean clamp, ImageRenderType render,
1802 gboolean new_data, gboolean only_existing)
1804 PixbufRenderer *pr = rt->pr;
1809 if (clamp && !rt_clamp_to_visible(rt, &x, &y, &w, &h)) return FALSE;
1811 x1 = ROUND_DOWN(x, rt->tile_width);
1812 x2 = ROUND_UP(x + w, rt->tile_width);
1814 y1 = ROUND_DOWN(y, rt->tile_height);
1815 y2 = ROUND_UP(y + h, rt->tile_height);
1817 for (j = y1; j <= y2; j += rt->tile_height)
1819 for (i = x1; i <= x2; i += rt->tile_width)
1823 it = rt_tile_get(rt, i, j,
1825 (i + rt->tile_width < rt->x_scroll ||
1826 i > rt->x_scroll + pr->vis_width ||
1827 j + rt->tile_height < rt->y_scroll ||
1828 j > rt->y_scroll + pr->vis_height)));
1831 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
1832 (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
1834 it->render_todo = render;
1837 auto qd = g_new(QueueData, 1);
1839 qd->new_data = new_data;
1849 qd->w = x + w - i - qd->x;
1850 if (qd->x + qd->w > rt->tile_width) qd->w = rt->tile_width - qd->x;
1860 qd->h = y + h - j - qd->y;
1861 if (qd->y + qd->h > rt->tile_height) qd->h = rt->tile_height - qd->y;
1863 if (qd->w < 1 || qd->h < 1)
1869 rt_queue_merge(it->qd, qd);
1875 rt->draw_queue = g_list_append(rt->draw_queue, qd);
1884 static void rt_queue(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;
1893 nx = CLAMP(x, 0, pr->width - 1);
1894 ny = CLAMP(y, 0, pr->height - 1);
1897 w = CLAMP(w, 0, pr->width - nx);
1898 h = CLAMP(h, 0, pr->height - ny);
1899 if (w < 1 || h < 1) return;
1901 if (rt_queue_to_tiles(rt, nx, ny, w, h, clamp, render, new_data, only_existing) &&
1902 ((!rt->draw_queue && !rt->draw_queue_2pass) || !rt->draw_idle_id))
1904 if (rt->draw_idle_id)
1906 g_source_remove(rt->draw_idle_id);
1907 rt->draw_idle_id = 0;
1909 rt_queue_schedule_next_draw(rt, TRUE);
1913 static void rt_scroll(void *renderer, gint x_off, gint y_off)
1915 auto rt = static_cast<RendererTiles *>(renderer);
1916 PixbufRenderer *pr = rt->pr;
1919 if (rt->stereo_mode & PR_STEREO_MIRROR) x_off = -x_off;
1920 if (rt->stereo_mode & PR_STEREO_FLIP) y_off = -y_off;
1922 gint w = pr->vis_width - abs(x_off);
1923 gint h = pr->vis_height - abs(y_off);
1927 /* scrolled completely to new material */
1928 rt_queue(rt, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1935 cairo_surface_t *surface;
1959 cr = cairo_create(rt->surface);
1960 surface = rt->surface;
1962 /* clipping restricts the intermediate surface's size, so it's a good idea
1964 cairo_rectangle(cr, x1 + pr->x_offset + rt->stereo_off_x, y1 + pr->y_offset + rt->stereo_off_y, w, h);
1966 /* Now push a group to change the target */
1967 cairo_push_group (cr);
1968 cairo_set_source_surface(cr, surface, x1 - x2, y1 - y2);
1970 /* Now copy the intermediate target back */
1971 cairo_pop_group_to_source(cr);
1975 rt_overlay_queue_all(rt, x2, y2, x1, y1);
1977 w = pr->vis_width - w;
1978 h = pr->vis_height - h;
1983 x_off > 0 ? rt->x_scroll + (pr->vis_width - w) : rt->x_scroll, rt->y_scroll,
1984 w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1988 /** @FIXME to optimize this, remove overlap */
1990 rt->x_scroll, y_off > 0 ? rt->y_scroll + (pr->vis_height - h) : rt->y_scroll,
1991 pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1995 static void renderer_area_changed(void *renderer, gint src_x, gint src_y, gint src_w, gint src_h)
1997 auto rt = static_cast<RendererTiles *>(renderer);
1998 PixbufRenderer *pr = rt->pr;
1999 gint x, y, width, height, x1, y1, x2, y2;
2001 gint orientation = rt_get_orientation(rt);
2002 pr_coords_map_orientation_reverse(orientation,
2003 src_x - GET_RIGHT_PIXBUF_OFFSET(rt), src_y,
2004 pr->image_width, pr->image_height,
2009 if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
2011 /* increase region when using a zoom quality that may access surrounding pixels */
2016 x1 = static_cast<gint>(floor(static_cast<gdouble>(x) * pr->scale));
2017 y1 = static_cast<gint>(floor(static_cast<gdouble>(y) * pr->scale * pr->aspect_ratio));
2018 x2 = static_cast<gint>(ceil(static_cast<gdouble>(x + width) * pr->scale));
2019 y2 = static_cast<gint>(ceil(static_cast<gdouble>(y + height) * pr->scale * pr->aspect_ratio));
2021 rt_queue(rt, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
2024 static void renderer_redraw(RendererTiles *rt, gint x, gint y, gint w, gint h,
2025 gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing)
2027 PixbufRenderer *pr = rt->pr;
2029 x -= rt->stereo_off_x;
2030 y -= rt->stereo_off_y;
2032 rt_border_draw(rt, x, y, w, h);
2034 x = MAX(0, x - pr->x_offset + pr->x_scroll);
2035 y = MAX(0, y - pr->y_offset + pr->y_scroll);
2039 MIN(w, pr->width - x),
2040 MIN(h, pr->height - y),
2041 clamp, render, new_data, only_existing);
2044 static void renderer_update_pixbuf(void *renderer, gboolean)
2046 rt_queue_clear(static_cast<RendererTiles *>(renderer));
2049 static void renderer_update_zoom(void *renderer, gboolean lazy)
2051 auto rt = static_cast<RendererTiles *>(renderer);
2052 PixbufRenderer *pr = rt->pr;
2054 rt_tile_invalidate_all(static_cast<RendererTiles *>(renderer));
2057 renderer_redraw(static_cast<RendererTiles *>(renderer), 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, TRUE, FALSE);
2059 rt_border_clear(rt);
2062 static void renderer_invalidate_region(void *renderer, gint x, gint y, gint w, gint h)
2064 rt_tile_invalidate_region(static_cast<RendererTiles *>(renderer), x, y, w, h);
2067 static void renderer_update_viewport(void *renderer)
2069 auto rt = static_cast<RendererTiles *>(renderer);
2071 rt->stereo_off_x = 0;
2072 rt->stereo_off_y = 0;
2074 if (rt->stereo_mode & PR_STEREO_RIGHT)
2076 if (rt->stereo_mode & PR_STEREO_HORIZ)
2078 rt->stereo_off_x = rt->pr->viewport_width;
2080 else if (rt->stereo_mode & PR_STEREO_VERT)
2082 rt->stereo_off_y = rt->pr->viewport_height;
2084 else if (rt->stereo_mode & PR_STEREO_FIXED)
2086 rt->stereo_off_x = rt->pr->stereo_fixed_x_right;
2087 rt->stereo_off_y = rt->pr->stereo_fixed_y_right;
2092 if (rt->stereo_mode & PR_STEREO_FIXED)
2094 rt->stereo_off_x = rt->pr->stereo_fixed_x_left;
2095 rt->stereo_off_y = rt->pr->stereo_fixed_y_left;
2098 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);
2100 rt_overlay_update_sizes(rt);
2101 rt_border_clear(rt);
2104 static void renderer_stereo_set(void *renderer, gint stereo_mode)
2106 auto rt = static_cast<RendererTiles *>(renderer);
2108 rt->stereo_mode = stereo_mode;
2111 static void renderer_free(void *renderer)
2113 auto rt = static_cast<RendererTiles *>(renderer);
2115 rt_tile_free_all(rt);
2116 if (rt->spare_tile) g_object_unref(rt->spare_tile);
2117 if (rt->overlay_buffer) g_object_unref(rt->overlay_buffer);
2118 rt_overlay_list_clear(rt);
2119 /* disconnect "hierarchy-changed" */
2120 g_signal_handlers_disconnect_matched(G_OBJECT(rt->pr), G_SIGNAL_MATCH_DATA,
2121 0, 0, nullptr, nullptr, rt);
2125 static gboolean rt_realize_cb(GtkWidget *widget, gpointer data)
2127 auto rt = static_cast<RendererTiles *>(data);
2132 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));
2134 cr = cairo_create(rt->surface);
2135 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));
2143 static gboolean rt_size_allocate_cb(GtkWidget *widget, GdkRectangle *allocation, gpointer data)
2145 auto rt = static_cast<RendererTiles *>(data);
2147 cairo_surface_t *old_surface;
2149 if (gtk_widget_get_realized(GTK_WIDGET(rt->pr)))
2151 old_surface = rt->surface;
2152 rt->surface = gdk_window_create_similar_surface(gtk_widget_get_window(widget), CAIRO_CONTENT_COLOR, allocation->width, allocation->height);
2154 cr = cairo_create(rt->surface);
2156 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));
2158 cairo_set_source_surface(cr, old_surface, 0, 0);
2161 cairo_surface_destroy(old_surface);
2163 renderer_redraw(rt, allocation->x, allocation->y, allocation->width, allocation->height, FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2169 static gboolean rt_draw_cb(GtkWidget *, cairo_t *cr, gpointer data)
2171 auto rt = static_cast<RendererTiles *>(data);
2175 if (rt->stereo_mode & (PR_STEREO_HORIZ | PR_STEREO_VERT))
2177 cairo_push_group(cr);
2178 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));
2180 if (rt->stereo_mode & PR_STEREO_HORIZ)
2182 cairo_rectangle(cr, rt->stereo_off_x, 0, rt->pr->viewport_width, rt->pr->viewport_height);
2186 cairo_rectangle(cr, 0, rt->stereo_off_y, rt->pr->viewport_width, rt->pr->viewport_height);
2191 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);
2193 cairo_set_source_surface(cr, rt->surface, 0, 0);
2196 cairo_pop_group_to_source(cr);
2201 cairo_set_source_surface(cr, rt->surface, 0, 0);
2205 work = rt->overlay_list;
2208 od = static_cast<OverlayData *>(work->data);
2209 gint px, py, pw, ph;
2210 pw = gdk_pixbuf_get_width(od->pixbuf);
2211 ph = gdk_pixbuf_get_height(od->pixbuf);
2215 if (od->flags & OVL_RELATIVE)
2217 if (px < 0) px = rt->pr->viewport_width - pw + px;
2218 if (py < 0) py = rt->pr->viewport_height - ph + py;
2221 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px, py);
2229 RendererFuncs *renderer_tiles_new(PixbufRenderer *pr)
2231 auto rt = g_new0(RendererTiles, 1);
2235 rt->f.area_changed = renderer_area_changed;
2236 rt->f.update_pixbuf = renderer_update_pixbuf;
2237 rt->f.free = renderer_free;
2238 rt->f.update_zoom = renderer_update_zoom;
2239 rt->f.invalidate_region = renderer_invalidate_region;
2240 rt->f.scroll = rt_scroll;
2241 rt->f.update_viewport = renderer_update_viewport;
2244 rt->f.overlay_add = renderer_tiles_overlay_add;
2245 rt->f.overlay_set = renderer_tiles_overlay_set;
2246 rt->f.overlay_get = renderer_tiles_overlay_get;
2248 rt->f.stereo_set = renderer_stereo_set;
2250 rt->tile_width = options->image.tile_size;
2251 rt->tile_height = options->image.tile_size;
2253 rt->tiles = nullptr;
2254 rt->tile_cache_size = 0;
2256 rt->tile_cache_max = PR_CACHE_SIZE_DEFAULT;
2258 rt->draw_idle_id = 0;
2260 rt->stereo_mode = 0;
2261 rt->stereo_off_x = 0;
2262 rt->stereo_off_y = 0;
2264 rt->hidpi_scale = gtk_widget_get_scale_factor(GTK_WIDGET(rt->pr));
2266 g_signal_connect(G_OBJECT(pr), "hierarchy-changed",
2267 G_CALLBACK(rt_hierarchy_changed_cb), rt);
2269 g_signal_connect(G_OBJECT(pr), "draw",
2270 G_CALLBACK(rt_draw_cb), rt);
2271 g_signal_connect(G_OBJECT(pr), "realize", G_CALLBACK(rt_realize_cb), rt);
2272 g_signal_connect(G_OBJECT(pr), "size-allocate", G_CALLBACK(rt_size_allocate_cb), rt);
2274 return reinterpret_cast<RendererFuncs *>(rt);
2278 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */