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"
33 /* comment this out if not using this from within Geeqie
34 * defining GQ_BUILD does these things:
35 * - Sets the shift-click scroller pixbuf to a nice icon instead of a black box
41 #include "pixbuf-util.h"
43 enum ExifOrientationType {
44 EXIF_ORIENTATION_UNKNOWN = 0,
45 EXIF_ORIENTATION_TOP_LEFT = 1,
46 EXIF_ORIENTATION_TOP_RIGHT = 2,
47 EXIF_ORIENTATION_BOTTOM_RIGHT = 3,
48 EXIF_ORIENTATION_BOTTOM_LEFT = 4,
49 EXIF_ORIENTATION_LEFT_TOP = 5,
50 EXIF_ORIENTATION_RIGHT_TOP = 6,
51 EXIF_ORIENTATION_RIGHT_BOTTOM = 7,
52 EXIF_ORIENTATION_LEFT_BOTTOM = 8
60 cairo_surface_t *surface; /* off screen buffer */
61 GdkPixbuf *pixbuf; /* pixbuf area for zooming */
62 gint x; /* x offset into image */
63 gint y; /* y offset into image */
64 gint w; /* width that is visible (may be less if at edge of image) */
65 gint h; /* height '' */
69 /* render_todo: (explanation)
71 AREA render area of tile, usually only used when loading an image
72 note: will jump to an ALL if render_done is not ALL.
73 ALL render entire tile, if never done before w/ ALL, for expose events *only*
76 ImageRenderType render_todo; /* what to do (see above) */
77 ImageRenderType render_done; /* highest that has been done before on tile */
82 guint size; /* est. memory used by pixmap and pixbuf */
105 OverlayRendererFlags flags;
113 gint tile_cache_max; /* max MiB to use for offscreen buffer */
117 gint tile_cols; /* count of tile columns */
118 GList *tiles; /* list of buffer tiles */
119 gint tile_cache_size; /* allocated size of pixmaps/pixbufs */
120 GList *draw_queue; /* list of areas to redraw */
121 GList *draw_queue_2pass;/* list when 2 pass is enabled */
124 cairo_surface_t *overlay_buffer;
125 cairo_surface_t *surface;
127 guint draw_idle_id; /* event source id */
129 GdkPixbuf *spare_tile;
135 gint x_scroll; /* allow local adjustment and mirroring */
143 static void rt_border_draw(RendererTiles *rt, gint x, gint y, gint w, gint h);
144 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h, ImageTile *it);
147 static void rt_tile_free_all(RendererTiles *rt);
148 static void rt_tile_invalidate_region(RendererTiles *rt, gint x, gint y, gint w, gint h);
149 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it);
150 static void rt_queue_clear(RendererTiles *rt);
151 static void rt_queue_merge(QueueData *parent, QueueData *qd);
152 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
153 gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing);
155 static void rt_hierarchy_changed_cb(GtkWidget *widget, GtkWidget *previous_toplevel, gpointer data);
156 static gint rt_queue_draw_idle_cb(gpointer data);
158 #define GET_RIGHT_PIXBUF_OFFSET(rt) \
159 (( ((rt)->stereo_mode & PR_STEREO_RIGHT) && !((rt)->stereo_mode & PR_STEREO_SWAP)) || \
160 (!((rt)->stereo_mode & PR_STEREO_RIGHT) && ((rt)->stereo_mode & PR_STEREO_SWAP)) ? \
161 (rt)->pr->stereo_pixbuf_offset_right : (rt)->pr->stereo_pixbuf_offset_left )
163 #define GET_LEFT_PIXBUF_OFFSET(rt) \
164 ((!((rt)->stereo_mode & PR_STEREO_RIGHT) && !((rt)->stereo_mode & PR_STEREO_SWAP)) || \
165 ( ((rt)->stereo_mode & PR_STEREO_RIGHT) && ((rt)->stereo_mode & PR_STEREO_SWAP)) ? \
166 (rt)->pr->stereo_pixbuf_offset_right : (rt)->pr->stereo_pixbuf_offset_left )
169 static void rt_sync_scroll(RendererTiles *rt)
171 PixbufRenderer *pr = rt->pr;
173 rt->x_scroll = (rt->stereo_mode & PR_STEREO_MIRROR) ?
174 pr->width - pr->vis_width - pr->x_scroll
177 rt->y_scroll = (rt->stereo_mode & PR_STEREO_FLIP) ?
178 pr->height - pr->vis_height - pr->y_scroll
183 *-------------------------------------------------------------------
185 *-------------------------------------------------------------------
188 static void rt_border_draw(RendererTiles *rt, gint x, gint y, gint w, gint h)
190 PixbufRenderer *pr = rt->pr;
199 box = GTK_WIDGET(pr);
200 window = gtk_widget_get_window(box);
204 cr = cairo_create(rt->surface);
206 if (!pr->pixbuf && !pr->source_tiles_enabled)
208 if (pr_clip_region(x, y, w, h,
210 pr->viewport_width, pr->viewport_height,
213 cairo_set_source_rgb(cr, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
214 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
216 rt_overlay_draw(rt, rx, ry, rw, rh, nullptr);
222 if (pr->vis_width < pr->viewport_width)
224 if (pr->x_offset > 0 &&
225 pr_clip_region(x, y, w, h,
227 pr->x_offset, pr->viewport_height,
230 cairo_set_source_rgb(cr, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
231 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
233 rt_overlay_draw(rt, rx, ry, rw, rh, nullptr);
235 if (pr->viewport_width - pr->vis_width - pr->x_offset > 0 &&
236 pr_clip_region(x, y, w, h,
237 pr->x_offset + pr->vis_width, 0,
238 pr->viewport_width - pr->vis_width - pr->x_offset, pr->viewport_height,
241 cairo_set_source_rgb(cr, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
242 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
244 rt_overlay_draw(rt, rx, ry, rw, rh, nullptr);
247 if (pr->vis_height < pr->viewport_height)
249 if (pr->y_offset > 0 &&
250 pr_clip_region(x, y, w, h,
252 pr->vis_width, pr->y_offset,
255 cairo_set_source_rgb(cr, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
256 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
258 rt_overlay_draw(rt, rx, ry, rw, rh, nullptr);
260 if (pr->viewport_height - pr->vis_height - pr->y_offset > 0 &&
261 pr_clip_region(x, y, w, h,
262 pr->x_offset, pr->y_offset + pr->vis_height,
263 pr->vis_width, pr->viewport_height - pr->vis_height - pr->y_offset,
266 cairo_set_source_rgb(cr, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
267 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
269 rt_overlay_draw(rt, rx, ry, rw, rh, nullptr);
275 static void rt_border_clear(RendererTiles *rt)
277 PixbufRenderer *pr = rt->pr;
278 rt_border_draw(rt, 0, 0, pr->viewport_width, pr->viewport_height);
283 *-------------------------------------------------------------------
285 *-------------------------------------------------------------------
288 static ImageTile *rt_tile_new(gint x, gint y, gint width, gint height)
290 auto it = g_new0(ImageTile, 1);
297 it->render_done = TILE_RENDER_NONE;
302 static void rt_tile_free(ImageTile *it)
306 if (it->pixbuf) g_object_unref(it->pixbuf);
307 if (it->surface) cairo_surface_destroy(it->surface);
312 static void rt_tile_free_all(RendererTiles *rt)
314 g_list_free_full(rt->tiles, reinterpret_cast<GDestroyNotify>(rt_tile_free));
316 rt->tile_cache_size = 0;
319 static ImageTile *rt_tile_add(RendererTiles *rt, gint x, gint y)
321 PixbufRenderer *pr = rt->pr;
324 it = rt_tile_new(x, y, rt->tile_width, rt->tile_height);
326 if (it->x + it->w > pr->width) it->w = pr->width - it->x;
327 if (it->y + it->h > pr->height) it->h = pr->height - it->y;
329 rt->tiles = g_list_prepend(rt->tiles, it);
330 rt->tile_cache_size += it->size;
335 static void rt_tile_remove(RendererTiles *rt, ImageTile *it)
339 QueueData *qd = it->qd;
342 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
348 QueueData *qd = it->qd2;
351 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
355 rt->tiles = g_list_remove(rt->tiles, it);
356 rt->tile_cache_size -= it->size;
361 static void rt_tile_free_space(RendererTiles *rt, guint space, ImageTile *it)
363 PixbufRenderer *pr = rt->pr;
367 work = g_list_last(rt->tiles);
369 if (pr->source_tiles_enabled && pr->scale < 1.0)
373 tiles = (pr->vis_width / rt->tile_width + 1) * (pr->vis_height / rt->tile_height + 1);
374 tile_max = MAX(tiles * rt->tile_width * rt->tile_height * 3,
375 (gint)((gdouble)rt->tile_cache_max * 1048576.0 * pr->scale));
379 tile_max = rt->tile_cache_max * 1048576;
382 while (work && rt->tile_cache_size + space > tile_max)
386 needle = static_cast<ImageTile *>(work->data);
389 ((!needle->qd && !needle->qd2) || !rt_tile_is_visible(rt, needle))) rt_tile_remove(rt, needle);
393 static void rt_tile_invalidate_all(RendererTiles *rt)
395 PixbufRenderer *pr = rt->pr;
403 it = static_cast<ImageTile *>(work->data);
406 it->render_done = TILE_RENDER_NONE;
407 it->render_todo = TILE_RENDER_ALL;
410 it->w = MIN(rt->tile_width, pr->width - it->x);
411 it->h = MIN(rt->tile_height, pr->height - it->y);
415 static void rt_tile_invalidate_region(RendererTiles *rt, gint x, gint y, gint w, gint h)
423 x1 = ROUND_DOWN(x, rt->tile_width);
424 x2 = ROUND_UP(x + w, rt->tile_width);
426 y1 = ROUND_DOWN(y, rt->tile_height);
427 y2 = ROUND_UP(y + h, rt->tile_height);
434 it = static_cast<ImageTile *>(work->data);
437 if (it->x < x2 && it->x + it->w > x1 &&
438 it->y < y2 && it->y + it->h > y1)
440 it->render_done = TILE_RENDER_NONE;
441 it->render_todo = TILE_RENDER_ALL;
446 static ImageTile *rt_tile_get(RendererTiles *rt, gint x, gint y, gboolean only_existing)
455 it = static_cast<ImageTile *>(work->data);
456 if (it->x == x && it->y == y)
458 rt->tiles = g_list_delete_link(rt->tiles, work);
459 rt->tiles = g_list_prepend(rt->tiles, it);
466 if (only_existing) return nullptr;
468 return rt_tile_add(rt, x, y);
471 static gint pixmap_calc_size(cairo_surface_t *)
473 return options->image.tile_size * options->image.tile_size * 4 / 8;
476 static void rt_hidpi_aware_draw(
483 cairo_surface_t *surface;
484 surface = gdk_cairo_surface_create_from_pixbuf(pixbuf, rt->hidpi_scale, nullptr);
485 cairo_set_source_surface(cr, surface, x, y);
487 cairo_surface_destroy(surface);
490 static void rt_tile_prepare(RendererTiles *rt, ImageTile *it)
492 PixbufRenderer *pr = rt->pr;
495 cairo_surface_t *surface;
498 surface = gdk_window_create_similar_surface(gtk_widget_get_window(reinterpret_cast<GtkWidget *>(pr)),
500 rt->tile_width, rt->tile_height);
502 size = pixmap_calc_size(surface) * rt->hidpi_scale * rt->hidpi_scale;
503 rt_tile_free_space(rt, size, it);
505 it->surface = surface;
507 rt->tile_cache_size += size;
514 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, rt->hidpi_scale * rt->tile_width, rt->hidpi_scale * rt->tile_height);
516 size = gdk_pixbuf_get_rowstride(pixbuf) * rt->tile_height * rt->hidpi_scale;
517 rt_tile_free_space(rt, size, it);
521 rt->tile_cache_size += size;
526 *-------------------------------------------------------------------
528 *-------------------------------------------------------------------
531 static void rt_overlay_get_position(RendererTiles *rt, OverlayData *od,
532 gint *x, gint *y, gint *w, gint *h)
534 PixbufRenderer *pr = rt->pr;
540 pw = gdk_pixbuf_get_width(od->pixbuf);
541 ph = gdk_pixbuf_get_height(od->pixbuf);
545 if (od->flags & OVL_RELATIVE)
547 if (px < 0) px = pr->viewport_width - pw + px;
548 if (py < 0) py = pr->viewport_height - ph + py;
557 static void rt_overlay_init_window(RendererTiles *rt, OverlayData *od)
559 PixbufRenderer *pr = rt->pr;
564 GdkWindowAttr attributes;
565 gint attributes_mask;
567 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
569 attributes.window_type = GDK_WINDOW_CHILD;
570 attributes.wclass = GDK_INPUT_OUTPUT;
571 attributes.width = pw;
572 attributes.height = ph;
573 attributes.event_mask = GDK_EXPOSURE_MASK;
576 od->window = gdk_window_new(gtk_widget_get_window(GTK_WIDGET(pr)), &attributes, attributes_mask);
577 gdk_window_set_user_data(od->window, pr);
578 gdk_window_move(od->window, px + rt->stereo_off_x, py + rt->stereo_off_y);
579 gdk_window_show(od->window);
582 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h,
585 PixbufRenderer *pr = rt->pr;
588 work = rt->overlay_list;
601 od = static_cast<OverlayData *>(work->data);
604 if (!od->window) rt_overlay_init_window(rt, od);
606 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
607 if (pr_clip_region(x, y, w, h, px, py, pw, ph, &rx, &ry, &rw, &rh))
609 if (!rt->overlay_buffer)
611 rt->overlay_buffer = gdk_window_create_similar_surface(gtk_widget_get_window(reinterpret_cast<GtkWidget *>(pr)),
613 rt->tile_width, rt->tile_height);
620 cr = cairo_create(rt->overlay_buffer);
621 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);
622 cairo_rectangle(cr, 0, 0, rw, rh);
623 cairo_fill_preserve(cr);
625 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - rx, py - ry);
629 cr = gdk_cairo_create(od->window);
630 cairo_set_source_surface(cr, rt->overlay_buffer, rx - px, ry - py);
631 cairo_rectangle (cr, rx - px, ry - py, rw, rh);
637 /* no ImageTile means region may be larger than our scratch buffer */
641 for (sx = rx; sx < rx + rw; sx += rt->tile_width)
642 for (sy = ry; sy < ry + rh; sy += rt->tile_height)
648 sw = MIN(rx + rw - sx, rt->tile_width);
649 sh = MIN(ry + rh - sy, rt->tile_height);
651 cr = cairo_create(rt->overlay_buffer);
652 cairo_set_source_rgb(cr, 0, 0, 0);
653 cairo_rectangle(cr, 0, 0, sw, sh);
654 cairo_fill_preserve(cr);
656 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - sx, py - sy);
660 cr = gdk_cairo_create(od->window);
661 cairo_set_source_surface(cr, rt->overlay_buffer, sx - px, sy - py);
662 cairo_rectangle (cr, sx - px, sy - py, sw, sh);
671 static void rt_overlay_queue_draw(RendererTiles *rt, OverlayData *od, gint x1, gint y1, gint x2, gint y2)
673 PixbufRenderer *pr = rt->pr;
679 rt_overlay_get_position(rt, od, &x, &y, &w, &h);
687 rt_queue(rt, rt->x_scroll - pr->x_offset + x,
688 rt->y_scroll - pr->y_offset + y,
690 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
692 rt_border_draw(rt, x, y, w, h);
695 static void rt_overlay_queue_all(RendererTiles *rt, gint x1, gint y1, gint x2, gint y2)
699 work = rt->overlay_list;
702 auto od = static_cast<OverlayData *>(work->data);
705 rt_overlay_queue_draw(rt, od, x1, y1, x2, y2);
709 static void rt_overlay_update_sizes(RendererTiles *rt)
713 work = rt->overlay_list;
716 auto od = static_cast<OverlayData *>(work->data);
719 if (!od->window) rt_overlay_init_window(rt, od);
721 if (od->flags & OVL_RELATIVE)
728 rt_overlay_get_position(rt, od, &x, &y, &w, &h);
729 gdk_window_move_resize(od->window, x + rt->stereo_off_x, y + rt->stereo_off_y, w, h);
734 static OverlayData *rt_overlay_find(RendererTiles *rt, gint id)
738 work = rt->overlay_list;
741 auto od = static_cast<OverlayData *>(work->data);
744 if (od->id == id) return od;
751 gint renderer_tiles_overlay_add(void *renderer, GdkPixbuf *pixbuf, gint x, gint y,
752 OverlayRendererFlags flags)
754 auto rt = static_cast<RendererTiles *>(renderer);
755 PixbufRenderer *pr = rt->pr;
758 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
759 g_return_val_if_fail(pixbuf != nullptr, -1);
762 while (rt_overlay_find(rt, id)) id++;
764 auto od = g_new0(OverlayData, 1);
767 g_object_ref(G_OBJECT(od->pixbuf));
772 rt_overlay_init_window(rt, od);
774 rt->overlay_list = g_list_append(rt->overlay_list, od);
776 gtk_widget_queue_draw(GTK_WIDGET(rt->pr));
781 static void rt_overlay_free(RendererTiles *rt, OverlayData *od)
783 rt->overlay_list = g_list_remove(rt->overlay_list, od);
785 if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
786 if (od->window) gdk_window_destroy(od->window);
789 if (!rt->overlay_list && rt->overlay_buffer)
791 cairo_surface_destroy(rt->overlay_buffer);
792 rt->overlay_buffer = nullptr;
796 static void rt_overlay_list_clear(RendererTiles *rt)
798 while (rt->overlay_list)
800 auto od = static_cast<OverlayData *>(rt->overlay_list->data);
801 rt_overlay_free(rt, od);
805 static void rt_overlay_list_reset_window(RendererTiles *rt)
809 if (rt->overlay_buffer) cairo_surface_destroy(rt->overlay_buffer);
810 rt->overlay_buffer = nullptr;
812 work = rt->overlay_list;
815 auto od = static_cast<OverlayData *>(work->data);
817 if (od->window) gdk_window_destroy(od->window);
818 od->window = nullptr;
822 void renderer_tiles_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint, gint)
824 auto rc = static_cast<RendererTiles *>(renderer);
825 PixbufRenderer *pr = rc->pr;
828 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
830 od = rt_overlay_find(rc, id);
835 g_object_ref(G_OBJECT(pixbuf));
836 g_object_unref(G_OBJECT(od->pixbuf));
841 rt_overlay_free(rc, od);
844 gtk_widget_queue_draw(GTK_WIDGET(rc->pr));
847 gboolean renderer_tiles_overlay_get(void *renderer, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
849 auto rt = static_cast<RendererTiles *>(renderer);
850 PixbufRenderer *pr = rt->pr;
853 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
855 od = rt_overlay_find(rt, id);
856 if (!od) return FALSE;
858 if (pixbuf) *pixbuf = od->pixbuf;
865 static void rt_hierarchy_changed_cb(GtkWidget *, GtkWidget *, gpointer data)
867 auto rt = static_cast<RendererTiles *>(data);
868 rt_overlay_list_reset_window(rt);
872 *-------------------------------------------------------------------
874 *-------------------------------------------------------------------
877 static GdkPixbuf *rt_get_spare_tile(RendererTiles *rt)
879 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);
880 return rt->spare_tile;
884 COLOR_BYTES = 3 /* rgb */
887 static void rt_tile_rotate_90_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
889 GdkPixbuf *src = *tile;
902 gint tw = rt->tile_width * 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 + (tw - 1) * COLOR_BYTES;
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_rotate_90_counter_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
931 GdkPixbuf *src = *tile;
944 gint th = rt->tile_height * rt->hidpi_scale;
946 srs = gdk_pixbuf_get_rowstride(src);
947 s_pix = gdk_pixbuf_get_pixels(src);
948 spi = s_pix + (x * COLOR_BYTES);
950 dest = rt_get_spare_tile(rt);
951 drs = gdk_pixbuf_get_rowstride(dest);
952 d_pix = gdk_pixbuf_get_pixels(dest);
953 dpi = d_pix + (th - 1) * drs;
955 for (i = y; i < y + h; i++)
957 sp = spi + (i * srs);
958 ip = dpi + (i * COLOR_BYTES);
959 for (j = x; j < x + w; j++)
962 memcpy(dp, sp, COLOR_BYTES);
967 rt->spare_tile = src;
971 static void rt_tile_mirror_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
973 GdkPixbuf *src = *tile;
986 gint tw = rt->tile_width * rt->hidpi_scale;
988 srs = gdk_pixbuf_get_rowstride(src);
989 s_pix = gdk_pixbuf_get_pixels(src);
990 spi = s_pix + (x * COLOR_BYTES);
992 dest = rt_get_spare_tile(rt);
993 drs = gdk_pixbuf_get_rowstride(dest);
994 d_pix = gdk_pixbuf_get_pixels(dest);
995 dpi = d_pix + (tw - x - 1) * COLOR_BYTES;
997 for (i = y; i < y + h; i++)
999 sp = spi + (i * srs);
1000 dp = dpi + (i * drs);
1001 for (j = 0; j < w; j++)
1003 memcpy(dp, sp, COLOR_BYTES);
1009 rt->spare_tile = src;
1013 static void rt_tile_mirror_and_flip(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1015 GdkPixbuf *src = *tile;
1026 gint tw = rt->tile_width * rt->hidpi_scale;
1027 gint th = rt->tile_height * rt->hidpi_scale;
1029 srs = gdk_pixbuf_get_rowstride(src);
1030 s_pix = gdk_pixbuf_get_pixels(src);
1032 dest = rt_get_spare_tile(rt);
1033 drs = gdk_pixbuf_get_rowstride(dest);
1034 d_pix = gdk_pixbuf_get_pixels(dest);
1035 dpi = d_pix + (th - 1) * drs + (tw - 1) * COLOR_BYTES;
1037 for (i = y; i < y + h; i++)
1039 sp = s_pix + (i * srs) + (x * COLOR_BYTES);
1040 dp = dpi - (i * drs) - (x * COLOR_BYTES);
1041 for (j = 0; j < w; j++)
1043 memcpy(dp, sp, COLOR_BYTES);
1049 rt->spare_tile = src;
1053 static void rt_tile_flip_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1055 GdkPixbuf *src = *tile;
1066 gint th = rt->tile_height * rt->hidpi_scale;
1068 srs = gdk_pixbuf_get_rowstride(src);
1069 s_pix = gdk_pixbuf_get_pixels(src);
1070 spi = s_pix + (x * COLOR_BYTES);
1072 dest = rt_get_spare_tile(rt);
1073 drs = gdk_pixbuf_get_rowstride(dest);
1074 d_pix = gdk_pixbuf_get_pixels(dest);
1075 dpi = d_pix + (th - 1) * drs + (x * COLOR_BYTES);
1077 for (i = y; i < y + h; i++)
1079 sp = spi + (i * srs);
1080 dp = dpi - (i * drs);
1081 memcpy(dp, sp, w * COLOR_BYTES);
1084 rt->spare_tile = src;
1088 static void rt_tile_apply_orientation(RendererTiles *rt, gint orientation, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h)
1090 switch (orientation)
1092 case EXIF_ORIENTATION_TOP_LEFT:
1093 /* normal -- nothing to do */
1095 case EXIF_ORIENTATION_TOP_RIGHT:
1098 rt_tile_mirror_only(rt, pixbuf, x, y, w, h);
1101 case EXIF_ORIENTATION_BOTTOM_RIGHT:
1104 rt_tile_mirror_and_flip(rt, pixbuf, x, y, w, h);
1107 case EXIF_ORIENTATION_BOTTOM_LEFT:
1110 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1113 case EXIF_ORIENTATION_LEFT_TOP:
1115 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1116 rt_tile_rotate_90_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1119 case EXIF_ORIENTATION_RIGHT_TOP:
1120 /* rotated -90 (270) */
1122 rt_tile_rotate_90_clockwise(rt, pixbuf, x, y, w, h);
1125 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1127 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1128 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1131 case EXIF_ORIENTATION_LEFT_BOTTOM:
1134 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, y, w, h);
1138 /* The other values are out of range */
1143 static gboolean rt_source_tile_render(RendererTiles *rt, ImageTile *it,
1144 gint x, gint y, gint w, gint h,
1145 gboolean, gboolean fast)
1147 PixbufRenderer *pr = rt->pr;
1150 gboolean draw = FALSE;
1152 if (pr->zoom == 1.0 || pr->scale == 1.0)
1154 list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE);
1164 st = static_cast<SourceTile *>(work->data);
1167 if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1168 it->x + x, it->y + y, w, h,
1169 &rx, &ry, &rw, &rh))
1172 cr = cairo_create(it->surface);
1173 cairo_rectangle (cr, rx - it->x, ry - it->y, rw, rh);
1177 cairo_set_source_rgb(cr, 0, 0, 0);
1180 else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
1182 rt_hidpi_aware_draw(rt, cr, st->pixbuf, -it->x + st->x, -it->y + st->y);
1197 if (pr->image_width == 0 || pr->image_height == 0) return FALSE;
1198 scale_x = static_cast<gdouble>(pr->width) / pr->image_width;
1199 scale_y = static_cast<gdouble>(pr->height) / pr->image_height;
1201 sx = static_cast<gdouble>(it->x + x) / scale_x;
1202 sy = static_cast<gdouble>(it->y + y) / scale_y;
1203 sw = static_cast<gdouble>(w) / scale_x;
1204 sh = static_cast<gdouble>(h) / scale_y;
1206 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1209 /* draws red over draw region, to check for leaks (regions not filled) */
1210 pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255);
1213 list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE);
1227 st = static_cast<SourceTile *>(work->data);
1230 stx = floor(static_cast<gdouble>(st->x) * scale_x);
1231 sty = floor(static_cast<gdouble>(st->y) * scale_y);
1232 stw = ceil(static_cast<gdouble>(st->x + pr->source_tile_width) * scale_x) - stx;
1233 sth = ceil(static_cast<gdouble>(st->y + pr->source_tile_height) * scale_y) - sty;
1235 if (pr_clip_region(stx, sty, stw, sth,
1236 it->x + x, it->y + y, w, h,
1237 &rx, &ry, &rw, &rh))
1243 cr = cairo_create(it->surface);
1244 cairo_rectangle (cr, rx - st->x, ry - st->y, rw, rh);
1245 cairo_set_source_rgb(cr, 0, 0, 0);
1254 /* may need to use unfloored stx,sty values here */
1255 offset_x = static_cast<gdouble>(stx - it->x);
1256 offset_y = static_cast<gdouble>(sty - it->y);
1258 gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh,
1259 static_cast<gdouble>(0.0) + offset_x,
1260 static_cast<gdouble>(0.0) + offset_y,
1262 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1277 * @param ignore_alpha
1288 * @param interp_type
1291 * @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
1295 static void rt_tile_get_region(gboolean has_alpha, gboolean ignore_alpha,
1296 const GdkPixbuf *src, GdkPixbuf *dest,
1297 int pb_x, int pb_y, int pb_w, int pb_h,
1298 double offset_x, double offset_y, double scale_x, double scale_y,
1299 GdkInterpType interp_type,
1300 int check_x, int check_y, gboolean wide_image)
1302 GdkPixbuf* tmppixbuf;
1321 if (scale_x == 1.0 && scale_y == 1.0)
1325 srs = gdk_pixbuf_get_rowstride(src);
1326 drs = gdk_pixbuf_get_rowstride(dest);
1327 psrc = gdk_pixbuf_get_pixels(src);
1328 pdst = gdk_pixbuf_get_pixels(dest);
1329 for (y = 0; y < pb_h; y++)
1331 for (x = 0; x < pb_w; x++)
1333 for (c = 0; c < 3; c++)
1335 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];
1342 gdk_pixbuf_copy_area(src,
1343 -offset_x + pb_x, -offset_y + pb_y,
1351 gdk_pixbuf_scale(src, dest,
1352 pb_x, pb_y, pb_w, pb_h,
1356 (wide_image && interp_type == GDK_INTERP_NEAREST) ? GDK_INTERP_TILES : interp_type);
1361 red_1=static_cast<guint32>(options->image.alpha_color_1.red * 255) << 16 & 0x00FF0000;
1362 green_1=static_cast<guint32>(options->image.alpha_color_1.green * 255) << 8 & 0x0000FF00;
1363 blue_1=static_cast<guint32>(options->image.alpha_color_1.blue * 255) & 0x000000FF;
1364 red_2=static_cast<guint32>(options->image.alpha_color_2.red * 255) << 16 & 0x00FF0000;
1365 green_2=static_cast<guint32>(options->image.alpha_color_2.green * 255) << 8 & 0x0000FF00;
1366 blue_2=static_cast<guint32>(options->image.alpha_color_2.blue * 255) & 0x000000FF;
1367 alpha_1 = red_1 + green_1 + blue_1;
1368 alpha_2 = red_2 + green_2 + blue_2;
1372 tmppixbuf = gdk_pixbuf_add_alpha(src, FALSE, 0, 0, 0);
1374 pixbuf_ignore_alpha_rect(tmppixbuf, 0, 0, gdk_pixbuf_get_width(src), gdk_pixbuf_get_height(src));
1376 gdk_pixbuf_composite_color(tmppixbuf, dest,
1377 pb_x, pb_y, pb_w, pb_h,
1381 (scale_x == 1.0 && scale_y == 1.0) ? GDK_INTERP_NEAREST : interp_type,
1382 255, check_x, check_y,
1383 PR_ALPHA_CHECK_SIZE,
1386 g_object_unref(tmppixbuf);
1390 gdk_pixbuf_composite_color(src, dest,
1391 pb_x, pb_y, pb_w, pb_h,
1395 (scale_x == 1.0 && scale_y == 1.0) ? GDK_INTERP_NEAREST : interp_type,
1396 255, check_x, check_y,
1397 PR_ALPHA_CHECK_SIZE,
1405 static gint rt_get_orientation(RendererTiles *rt)
1407 PixbufRenderer *pr = rt->pr;
1409 gint orientation = pr->orientation;
1410 static const gint mirror[] = {1, 2, 1, 4, 3, 6, 5, 8, 7};
1411 static const gint flip[] = {1, 4, 3, 2, 1, 8, 7, 6, 5};
1413 if (rt->stereo_mode & PR_STEREO_MIRROR) orientation = mirror[orientation];
1414 if (rt->stereo_mode & PR_STEREO_FLIP) orientation = flip[orientation];
1419 static void rt_tile_render(RendererTiles *rt, ImageTile *it,
1420 gint x, gint y, gint w, gint h,
1421 gboolean new_data, gboolean fast)
1423 PixbufRenderer *pr = rt->pr;
1425 gboolean draw = FALSE;
1426 gint orientation = rt_get_orientation(rt);
1427 gboolean wide_image = FALSE;
1429 if (it->render_todo == TILE_RENDER_NONE && it->surface && !new_data) return;
1431 if (it->render_done != TILE_RENDER_ALL)
1437 if (!fast) it->render_done = TILE_RENDER_ALL;
1439 else if (it->render_todo != TILE_RENDER_AREA)
1441 if (!fast) it->render_todo = TILE_RENDER_NONE;
1445 if (!fast) it->render_todo = TILE_RENDER_NONE;
1447 if (new_data) it->blank = FALSE;
1449 rt_tile_prepare(rt, it);
1450 has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
1452 /** @FIXME checker colors for alpha should be configurable,
1453 * also should be drawn for blank = TRUE
1458 /* no data, do fast rect fill */
1460 cr = cairo_create(it->surface);
1461 cairo_rectangle (cr, 0, 0, it->w, it->h);
1462 cairo_set_source_rgb(cr, 0, 0, 0);
1466 else if (pr->source_tiles_enabled)
1468 draw = rt_source_tile_render(rt, it, x, y, w, h, new_data, fast);
1481 if (pr->image_width == 0 || pr->image_height == 0) return;
1483 scale_x = rt->hidpi_scale * static_cast<gdouble>(pr->width) / pr->image_width;
1484 scale_y = rt->hidpi_scale * static_cast<gdouble>(pr->height) / pr->image_height;
1486 pr_tile_coords_map_orientation(orientation, it->x, it->y,
1487 pr->width, pr->height,
1488 rt->tile_width, rt->tile_height,
1490 pr_tile_region_map_orientation(orientation, x, y,
1491 rt->tile_width, rt->tile_height,
1496 src_x *= rt->hidpi_scale;
1497 src_y *= rt->hidpi_scale;
1498 pb_x *= rt->hidpi_scale;
1499 pb_y *= rt->hidpi_scale;
1500 pb_w *= rt->hidpi_scale;
1501 pb_h *= rt->hidpi_scale;
1503 switch (orientation)
1506 case EXIF_ORIENTATION_LEFT_TOP:
1507 case EXIF_ORIENTATION_RIGHT_TOP:
1508 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1509 case EXIF_ORIENTATION_LEFT_BOTTOM:
1519 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
1520 * small sizes for anything but GDK_INTERP_NEAREST
1522 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1523 if (pr->image_width > 32767) wide_image = TRUE;
1525 rt_tile_get_region(has_alpha, pr->ignore_alpha,
1526 pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
1527 static_cast<gdouble>(0.0) - src_x - GET_RIGHT_PIXBUF_OFFSET(rt) * scale_x,
1528 static_cast<gdouble>(0.0) - src_y,
1530 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1531 it->x + pb_x, it->y + pb_y, wide_image);
1532 if (rt->stereo_mode & PR_STEREO_ANAGLYPH &&
1533 (pr->stereo_pixbuf_offset_right > 0 || pr->stereo_pixbuf_offset_left > 0))
1535 GdkPixbuf *right_pb = rt_get_spare_tile(rt);
1536 rt_tile_get_region(has_alpha, pr->ignore_alpha,
1537 pr->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h,
1538 static_cast<gdouble>(0.0) - src_x - GET_LEFT_PIXBUF_OFFSET(rt) * scale_x,
1539 static_cast<gdouble>(0.0) - src_y,
1541 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1542 it->x + pb_x, it->y + pb_y, wide_image);
1543 pr_create_anaglyph(rt->stereo_mode, it->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h);
1544 /* do not care about freeing spare_tile, it will be reused */
1546 rt_tile_apply_orientation(rt, orientation, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
1550 if (draw && it->pixbuf && !it->blank)
1554 if (pr->func_post_process && !(pr->post_process_slow && fast))
1555 pr->func_post_process(pr, &it->pixbuf, x, y, w, h, pr->post_process_user_data);
1557 cr = cairo_create(it->surface);
1558 cairo_rectangle (cr, x, y, w, h);
1559 rt_hidpi_aware_draw(rt, cr, it->pixbuf, 0, 0);
1565 static void rt_tile_expose(RendererTiles *rt, ImageTile *it,
1566 gint x, gint y, gint w, gint h,
1567 gboolean new_data, gboolean fast)
1569 PixbufRenderer *pr = rt->pr;
1572 /* clamp to visible */
1573 if (it->x + x < rt->x_scroll)
1575 w -= rt->x_scroll - it->x - x;
1576 x = rt->x_scroll - it->x;
1578 if (it->x + x + w > rt->x_scroll + pr->vis_width)
1580 w = rt->x_scroll + pr->vis_width - it->x - x;
1583 if (it->y + y < rt->y_scroll)
1585 h -= rt->y_scroll - it->y - y;
1586 y = rt->y_scroll - it->y;
1588 if (it->y + y + h > rt->y_scroll + pr->vis_height)
1590 h = rt->y_scroll + pr->vis_height - it->y - y;
1594 rt_tile_render(rt, it, x, y, w, h, new_data, fast);
1596 cr = cairo_create(rt->surface);
1597 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);
1598 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);
1602 if (rt->overlay_list)
1604 rt_overlay_draw(rt, pr->x_offset + (it->x - rt->x_scroll) + x,
1605 pr->y_offset + (it->y - rt->y_scroll) + y,
1610 gtk_widget_queue_draw(GTK_WIDGET(rt->pr));
1614 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it)
1616 PixbufRenderer *pr = rt->pr;
1617 return (it->x + it->w >= rt->x_scroll && it->x < rt->x_scroll + pr->vis_width &&
1618 it->y + it->h >= rt->y_scroll && it->y < rt->y_scroll + pr->vis_height);
1622 *-------------------------------------------------------------------
1624 *-------------------------------------------------------------------
1627 static gint rt_get_queued_area(GList *work)
1633 auto qd = static_cast<QueueData *>(work->data);
1634 area += qd->w * qd->h;
1641 static gboolean rt_queue_schedule_next_draw(RendererTiles *rt, gboolean force_set)
1643 PixbufRenderer *pr = rt->pr;
1645 gint visible_area = pr->vis_width * pr->vis_height;
1650 DEBUG_2("redraw priority: 2pass");
1651 rt->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, rt_queue_draw_idle_cb, rt, nullptr);
1652 return G_SOURCE_REMOVE;
1655 if (visible_area == 0)
1662 percent = 100.0 * rt_get_queued_area(rt->draw_queue) / visible_area;
1667 /* we have enough data for starting intensive redrawing */
1668 DEBUG_2("redraw priority: high %.2f %%", percent);
1669 rt->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW, rt_queue_draw_idle_cb, rt, nullptr);
1670 return G_SOURCE_REMOVE;
1673 if (percent < 1.0 || force_set)
1675 /* queue is (almost) empty, wait 50 ms*/
1676 DEBUG_2("redraw priority: wait %.2f %%", percent);
1677 rt->draw_idle_id = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, 50, rt_queue_draw_idle_cb, rt, nullptr);
1678 return G_SOURCE_REMOVE;
1681 /* keep the same priority as before */
1682 DEBUG_2("redraw priority: no change %.2f %%", percent);
1683 return G_SOURCE_CONTINUE;
1687 static gboolean rt_queue_draw_idle_cb(gpointer data)
1689 auto rt = static_cast<RendererTiles *>(data);
1690 PixbufRenderer *pr = rt->pr;
1695 if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
1696 (!rt->draw_queue && !rt->draw_queue_2pass) ||
1699 pr_render_complete_signal(pr);
1701 rt->draw_idle_id = 0;
1702 return G_SOURCE_REMOVE;
1707 qd = static_cast<QueueData *>(rt->draw_queue->data);
1708 fast = (pr->zoom_2pass && ((pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0) || pr->post_process_slow));
1714 /* still loading, wait till done (also drops the higher priority) */
1716 return rt_queue_schedule_next_draw(rt, FALSE);
1719 qd = static_cast<QueueData *>(rt->draw_queue_2pass->data);
1723 if (gtk_widget_get_realized(GTK_WIDGET(pr)))
1725 if (rt_tile_is_visible(rt, qd->it))
1727 rt_tile_expose(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1729 else if (qd->new_data)
1731 /* if new pixel data, and we already have a pixmap, update the tile */
1732 qd->it->blank = FALSE;
1733 if (qd->it->surface && qd->it->render_done == TILE_RENDER_ALL)
1735 rt_tile_render(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1742 qd->it->qd = nullptr;
1743 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
1748 rt_queue_merge(qd->it->qd2, qd);
1754 rt->draw_queue_2pass = g_list_append(rt->draw_queue_2pass, qd);
1764 qd->it->qd2 = nullptr;
1765 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
1769 if (!rt->draw_queue && !rt->draw_queue_2pass)
1771 pr_render_complete_signal(pr);
1773 rt->draw_idle_id = 0;
1774 return G_SOURCE_REMOVE;
1777 return rt_queue_schedule_next_draw(rt, FALSE);
1780 static void rt_queue_list_free(GList *list)
1789 qd = static_cast<QueueData *>(work->data);
1792 qd->it->qd = nullptr;
1793 qd->it->qd2 = nullptr;
1800 static void rt_queue_clear(RendererTiles *rt)
1802 rt_queue_list_free(rt->draw_queue);
1803 rt->draw_queue = nullptr;
1805 rt_queue_list_free(rt->draw_queue_2pass);
1806 rt->draw_queue_2pass = nullptr;
1808 if (rt->draw_idle_id)
1810 g_source_remove(rt->draw_idle_id);
1811 rt->draw_idle_id = 0;
1816 static void rt_queue_merge(QueueData *parent, QueueData *qd)
1818 if (parent->x + parent->w < qd->x + qd->w)
1820 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
1822 if (parent->x > qd->x)
1824 parent->w += parent->x - qd->x;
1828 if (parent->y + parent->h < qd->y + qd->h)
1830 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
1832 if (parent->y > qd->y)
1834 parent->h += parent->y - qd->y;
1838 parent->new_data |= qd->new_data;
1841 static gboolean rt_clamp_to_visible(RendererTiles *rt, gint *x, gint *y, gint *w, gint *h)
1843 PixbufRenderer *pr = rt->pr;
1854 vh = pr->vis_height;
1859 if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
1862 nx = CLAMP(*x, vx, vx + vw);
1863 nw = CLAMP(*w - (nx - *x), 1, vw);
1865 ny = CLAMP(*y, vy, vy + vh);
1866 nh = CLAMP(*h - (ny - *y), 1, vh);
1876 static gboolean rt_queue_to_tiles(RendererTiles *rt, gint x, gint y, gint w, gint h,
1877 gboolean clamp, ImageRenderType render,
1878 gboolean new_data, gboolean only_existing)
1880 PixbufRenderer *pr = rt->pr;
1888 if (clamp && !rt_clamp_to_visible(rt, &x, &y, &w, &h)) return FALSE;
1890 x1 = ROUND_DOWN(x, rt->tile_width);
1891 x2 = ROUND_UP(x + w, rt->tile_width);
1893 y1 = ROUND_DOWN(y, rt->tile_height);
1894 y2 = ROUND_UP(y + h, rt->tile_height);
1896 for (j = y1; j <= y2; j += rt->tile_height)
1898 for (i = x1; i <= x2; i += rt->tile_width)
1902 it = rt_tile_get(rt, i, j,
1904 (i + rt->tile_width < rt->x_scroll ||
1905 i > rt->x_scroll + pr->vis_width ||
1906 j + rt->tile_height < rt->y_scroll ||
1907 j > rt->y_scroll + pr->vis_height)));
1910 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
1911 (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
1913 it->render_todo = render;
1916 auto qd = g_new(QueueData, 1);
1918 qd->new_data = new_data;
1928 qd->w = x + w - i - qd->x;
1929 if (qd->x + qd->w > rt->tile_width) qd->w = rt->tile_width - qd->x;
1939 qd->h = y + h - j - qd->y;
1940 if (qd->y + qd->h > rt->tile_height) qd->h = rt->tile_height - qd->y;
1942 if (qd->w < 1 || qd->h < 1)
1948 rt_queue_merge(it->qd, qd);
1954 rt->draw_queue = g_list_append(rt->draw_queue, qd);
1963 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
1964 gboolean clamp, ImageRenderType render,
1965 gboolean new_data, gboolean only_existing)
1967 PixbufRenderer *pr = rt->pr;
1973 nx = CLAMP(x, 0, pr->width - 1);
1974 ny = CLAMP(y, 0, pr->height - 1);
1977 w = CLAMP(w, 0, pr->width - nx);
1978 h = CLAMP(h, 0, pr->height - ny);
1979 if (w < 1 || h < 1) return;
1981 if (rt_queue_to_tiles(rt, nx, ny, w, h, clamp, render, new_data, only_existing) &&
1982 ((!rt->draw_queue && !rt->draw_queue_2pass) || !rt->draw_idle_id))
1984 if (rt->draw_idle_id)
1986 g_source_remove(rt->draw_idle_id);
1987 rt->draw_idle_id = 0;
1989 rt_queue_schedule_next_draw(rt, TRUE);
1993 static void rt_scroll(void *renderer, gint x_off, gint y_off)
1995 auto rt = static_cast<RendererTiles *>(renderer);
1996 PixbufRenderer *pr = rt->pr;
1999 if (rt->stereo_mode & PR_STEREO_MIRROR) x_off = -x_off;
2000 if (rt->stereo_mode & PR_STEREO_FLIP) y_off = -y_off;
2002 gint w = pr->vis_width - abs(x_off);
2003 gint h = pr->vis_height - abs(y_off);
2007 /* scrolled completely to new material */
2008 rt_queue(rt, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2017 cairo_surface_t *surface;
2041 cr = cairo_create(rt->surface);
2042 surface = rt->surface;
2044 /* clipping restricts the intermediate surface's size, so it's a good idea
2046 cairo_rectangle(cr, x1 + pr->x_offset + rt->stereo_off_x, y1 + pr->y_offset + rt->stereo_off_y, w, h);
2048 /* Now push a group to change the target */
2049 cairo_push_group (cr);
2050 cairo_set_source_surface(cr, surface, x1 - x2, y1 - y2);
2052 /* Now copy the intermediate target back */
2053 cairo_pop_group_to_source(cr);
2057 rt_overlay_queue_all(rt, x2, y2, x1, y1);
2059 w = pr->vis_width - w;
2060 h = pr->vis_height - h;
2065 x_off > 0 ? rt->x_scroll + (pr->vis_width - w) : rt->x_scroll, rt->y_scroll,
2066 w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2070 /** @FIXME to optimize this, remove overlap */
2072 rt->x_scroll, y_off > 0 ? rt->y_scroll + (pr->vis_height - h) : rt->y_scroll,
2073 pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2077 static void renderer_area_changed(void *renderer, gint src_x, gint src_y, gint src_w, gint src_h)
2079 auto rt = static_cast<RendererTiles *>(renderer);
2080 PixbufRenderer *pr = rt->pr;
2090 gint orientation = rt_get_orientation(rt);
2091 pr_coords_map_orientation_reverse(orientation,
2092 src_x - GET_RIGHT_PIXBUF_OFFSET(rt), src_y,
2093 pr->image_width, pr->image_height,
2098 if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
2100 /* increase region when using a zoom quality that may access surrounding pixels */
2105 x1 = static_cast<gint>(floor(static_cast<gdouble>(x) * pr->scale));
2106 y1 = static_cast<gint>(floor(static_cast<gdouble>(y) * pr->scale * pr->aspect_ratio));
2107 x2 = static_cast<gint>(ceil(static_cast<gdouble>(x + width) * pr->scale));
2108 y2 = static_cast<gint>(ceil(static_cast<gdouble>(y + height) * pr->scale * pr->aspect_ratio));
2110 rt_queue(rt, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
2113 static void renderer_redraw(RendererTiles *rt, gint x, gint y, gint w, gint h,
2114 gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing)
2116 PixbufRenderer *pr = rt->pr;
2118 x -= rt->stereo_off_x;
2119 y -= rt->stereo_off_y;
2121 rt_border_draw(rt, x, y, w, h);
2123 x = MAX(0, x - pr->x_offset + pr->x_scroll);
2124 y = MAX(0, y - pr->y_offset + pr->y_scroll);
2128 MIN(w, pr->width - x),
2129 MIN(h, pr->height - y),
2130 clamp, render, new_data, only_existing);
2133 static void renderer_update_pixbuf(void *renderer, gboolean)
2135 rt_queue_clear(static_cast<RendererTiles *>(renderer));
2138 static void renderer_update_zoom(void *renderer, gboolean lazy)
2140 auto rt = static_cast<RendererTiles *>(renderer);
2141 PixbufRenderer *pr = rt->pr;
2143 rt_tile_invalidate_all(static_cast<RendererTiles *>(renderer));
2146 renderer_redraw(static_cast<RendererTiles *>(renderer), 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, TRUE, FALSE);
2148 rt_border_clear(rt);
2151 static void renderer_invalidate_region(void *renderer, gint x, gint y, gint w, gint h)
2153 rt_tile_invalidate_region(static_cast<RendererTiles *>(renderer), x, y, w, h);
2156 static void renderer_update_viewport(void *renderer)
2158 auto rt = static_cast<RendererTiles *>(renderer);
2160 rt->stereo_off_x = 0;
2161 rt->stereo_off_y = 0;
2163 if (rt->stereo_mode & PR_STEREO_RIGHT)
2165 if (rt->stereo_mode & PR_STEREO_HORIZ)
2167 rt->stereo_off_x = rt->pr->viewport_width;
2169 else if (rt->stereo_mode & PR_STEREO_VERT)
2171 rt->stereo_off_y = rt->pr->viewport_height;
2173 else if (rt->stereo_mode & PR_STEREO_FIXED)
2175 rt->stereo_off_x = rt->pr->stereo_fixed_x_right;
2176 rt->stereo_off_y = rt->pr->stereo_fixed_y_right;
2181 if (rt->stereo_mode & PR_STEREO_FIXED)
2183 rt->stereo_off_x = rt->pr->stereo_fixed_x_left;
2184 rt->stereo_off_y = rt->pr->stereo_fixed_y_left;
2187 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);
2189 rt_overlay_update_sizes(rt);
2190 rt_border_clear(rt);
2193 static void renderer_stereo_set(void *renderer, gint stereo_mode)
2195 auto rt = static_cast<RendererTiles *>(renderer);
2197 rt->stereo_mode = stereo_mode;
2200 static void renderer_free(void *renderer)
2202 auto rt = static_cast<RendererTiles *>(renderer);
2204 rt_tile_free_all(rt);
2205 if (rt->spare_tile) g_object_unref(rt->spare_tile);
2206 if (rt->overlay_buffer) g_object_unref(rt->overlay_buffer);
2207 rt_overlay_list_clear(rt);
2208 /* disconnect "hierarchy-changed" */
2209 g_signal_handlers_disconnect_matched(G_OBJECT(rt->pr), G_SIGNAL_MATCH_DATA,
2210 0, 0, nullptr, nullptr, rt);
2214 static gboolean rt_realize_cb(GtkWidget *widget, gpointer data)
2216 auto rt = static_cast<RendererTiles *>(data);
2221 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));
2223 cr = cairo_create(rt->surface);
2224 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));
2232 static gboolean rt_size_allocate_cb(GtkWidget *widget, GdkRectangle *allocation, gpointer data)
2234 auto rt = static_cast<RendererTiles *>(data);
2236 cairo_surface_t *old_surface;
2238 if (gtk_widget_get_realized(GTK_WIDGET(rt->pr)))
2240 old_surface = rt->surface;
2241 rt->surface = gdk_window_create_similar_surface(gtk_widget_get_window(widget), CAIRO_CONTENT_COLOR, allocation->width, allocation->height);
2243 cr = cairo_create(rt->surface);
2245 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));
2247 cairo_set_source_surface(cr, old_surface, 0, 0);
2250 cairo_surface_destroy(old_surface);
2252 renderer_redraw(rt, allocation->x, allocation->y, allocation->width, allocation->height, FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2258 static gboolean rt_draw_cb(GtkWidget *, cairo_t *cr, gpointer data)
2260 auto rt = static_cast<RendererTiles *>(data);
2264 if (rt->stereo_mode & (PR_STEREO_HORIZ | PR_STEREO_VERT))
2266 cairo_push_group(cr);
2267 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));
2269 if (rt->stereo_mode & PR_STEREO_HORIZ)
2271 cairo_rectangle(cr, rt->stereo_off_x, 0, rt->pr->viewport_width, rt->pr->viewport_height);
2275 cairo_rectangle(cr, 0, rt->stereo_off_y, rt->pr->viewport_width, rt->pr->viewport_height);
2280 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);
2282 cairo_set_source_surface(cr, rt->surface, 0, 0);
2285 cairo_pop_group_to_source(cr);
2290 cairo_set_source_surface(cr, rt->surface, 0, 0);
2294 work = rt->overlay_list;
2297 od = static_cast<OverlayData *>(work->data);
2302 pw = gdk_pixbuf_get_width(od->pixbuf);
2303 ph = gdk_pixbuf_get_height(od->pixbuf);
2307 if (od->flags & OVL_RELATIVE)
2309 if (px < 0) px = rt->pr->viewport_width - pw + px;
2310 if (py < 0) py = rt->pr->viewport_height - ph + py;
2313 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px, py);
2321 RendererFuncs *renderer_tiles_new(PixbufRenderer *pr)
2323 auto rt = g_new0(RendererTiles, 1);
2327 rt->f.area_changed = renderer_area_changed;
2328 rt->f.update_pixbuf = renderer_update_pixbuf;
2329 rt->f.free = renderer_free;
2330 rt->f.update_zoom = renderer_update_zoom;
2331 rt->f.invalidate_region = renderer_invalidate_region;
2332 rt->f.scroll = rt_scroll;
2333 rt->f.update_viewport = renderer_update_viewport;
2336 rt->f.overlay_add = renderer_tiles_overlay_add;
2337 rt->f.overlay_set = renderer_tiles_overlay_set;
2338 rt->f.overlay_get = renderer_tiles_overlay_get;
2340 rt->f.stereo_set = renderer_stereo_set;
2342 rt->tile_width = options->image.tile_size;
2343 rt->tile_height = options->image.tile_size;
2345 rt->tiles = nullptr;
2346 rt->tile_cache_size = 0;
2348 rt->tile_cache_max = PR_CACHE_SIZE_DEFAULT;
2350 rt->draw_idle_id = 0;
2352 rt->stereo_mode = 0;
2353 rt->stereo_off_x = 0;
2354 rt->stereo_off_y = 0;
2356 rt->hidpi_scale = gtk_widget_get_scale_factor(GTK_WIDGET(rt->pr));
2358 g_signal_connect(G_OBJECT(pr), "hierarchy-changed",
2359 G_CALLBACK(rt_hierarchy_changed_cb), rt);
2361 g_signal_connect(G_OBJECT(pr), "draw",
2362 G_CALLBACK(rt_draw_cb), rt);
2363 g_signal_connect(G_OBJECT(pr), "realize", G_CALLBACK(rt_realize_cb), rt);
2364 g_signal_connect(G_OBJECT(pr), "size-allocate", G_CALLBACK(rt_size_allocate_cb), rt);
2366 return reinterpret_cast<RendererFuncs *>(rt);
2370 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */