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
53 using QueueData = struct _QueueData;
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)/65535, static_cast<double>(pr->color.green)/65535, static_cast<double>(pr->color.blue)/65535);
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)/65535, static_cast<double>(pr->color.green)/65535, static_cast<double>(pr->color.blue)/65535);
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)/65535, static_cast<double>(pr->color.green)/65535, static_cast<double>(pr->color.blue)/65535);
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)/65535, static_cast<double>(pr->color.green)/65535, static_cast<double>(pr->color.blue)/65535);
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)/65535, static_cast<double>(pr->color.green)/65535, static_cast<double>(pr->color.blue)/65535);
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)
286 it = g_new0(ImageTile, 1);
293 it->render_done = TILE_RENDER_NONE;
298 static void rt_tile_free(ImageTile *it)
302 if (it->pixbuf) g_object_unref(it->pixbuf);
303 if (it->surface) cairo_surface_destroy(it->surface);
308 static void rt_tile_free_all(RendererTiles *rt)
317 it = static_cast<ImageTile *>(work->data);
323 g_list_free(rt->tiles);
325 rt->tile_cache_size = 0;
328 static ImageTile *rt_tile_add(RendererTiles *rt, gint x, gint y)
330 PixbufRenderer *pr = rt->pr;
333 it = rt_tile_new(x, y, rt->tile_width, rt->tile_height);
335 if (it->x + it->w > pr->width) it->w = pr->width - it->x;
336 if (it->y + it->h > pr->height) it->h = pr->height - it->y;
338 rt->tiles = g_list_prepend(rt->tiles, it);
339 rt->tile_cache_size += it->size;
344 static void rt_tile_remove(RendererTiles *rt, ImageTile *it)
348 QueueData *qd = it->qd;
351 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
357 QueueData *qd = it->qd2;
360 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
364 rt->tiles = g_list_remove(rt->tiles, it);
365 rt->tile_cache_size -= it->size;
370 static void rt_tile_free_space(RendererTiles *rt, guint space, ImageTile *it)
372 PixbufRenderer *pr = rt->pr;
376 work = g_list_last(rt->tiles);
378 if (pr->source_tiles_enabled && pr->scale < 1.0)
382 tiles = (pr->vis_width / rt->tile_width + 1) * (pr->vis_height / rt->tile_height + 1);
383 tile_max = MAX(tiles * rt->tile_width * rt->tile_height * 3,
384 (gint)((gdouble)rt->tile_cache_max * 1048576.0 * pr->scale));
388 tile_max = rt->tile_cache_max * 1048576;
391 while (work && rt->tile_cache_size + space > tile_max)
395 needle = static_cast<ImageTile *>(work->data);
398 ((!needle->qd && !needle->qd2) || !rt_tile_is_visible(rt, needle))) rt_tile_remove(rt, needle);
402 static void rt_tile_invalidate_all(RendererTiles *rt)
404 PixbufRenderer *pr = rt->pr;
412 it = static_cast<ImageTile *>(work->data);
415 it->render_done = TILE_RENDER_NONE;
416 it->render_todo = TILE_RENDER_ALL;
419 it->w = MIN(rt->tile_width, pr->width - it->x);
420 it->h = MIN(rt->tile_height, pr->height - it->y);
424 static void rt_tile_invalidate_region(RendererTiles *rt, gint x, gint y, gint w, gint h)
430 x1 = ROUND_DOWN(x, rt->tile_width);
431 x2 = ROUND_UP(x + w, rt->tile_width);
433 y1 = ROUND_DOWN(y, rt->tile_height);
434 y2 = ROUND_UP(y + h, rt->tile_height);
441 it = static_cast<ImageTile *>(work->data);
444 if (it->x < x2 && it->x + it->w > x1 &&
445 it->y < y2 && it->y + it->h > y1)
447 it->render_done = TILE_RENDER_NONE;
448 it->render_todo = TILE_RENDER_ALL;
453 static ImageTile *rt_tile_get(RendererTiles *rt, gint x, gint y, gboolean only_existing)
462 it = static_cast<ImageTile *>(work->data);
463 if (it->x == x && it->y == y)
465 rt->tiles = g_list_delete_link(rt->tiles, work);
466 rt->tiles = g_list_prepend(rt->tiles, it);
473 if (only_existing) return nullptr;
475 return rt_tile_add(rt, x, y);
478 static gint pixmap_calc_size(cairo_surface_t *UNUSED(surface))
482 // d = gdk_drawable_get_depth(pixmap);
483 // gdk_drawable_get_size(pixmap, &w, &h);
484 return options->image.tile_size * options->image.tile_size * 4 / 8;
487 static void rt_hidpi_aware_draw(
494 cairo_surface_t *surface;
495 surface = gdk_cairo_surface_create_from_pixbuf(pixbuf, rt->hidpi_scale, nullptr);
496 cairo_set_source_surface(cr, surface, x, y);
498 cairo_surface_destroy(surface);
501 static void rt_tile_prepare(RendererTiles *rt, ImageTile *it)
503 PixbufRenderer *pr = rt->pr;
506 cairo_surface_t *surface;
509 surface = gdk_window_create_similar_surface(gtk_widget_get_window(reinterpret_cast<GtkWidget *>(pr)),
511 rt->tile_width, rt->tile_height);
513 size = pixmap_calc_size(surface) * rt->hidpi_scale * rt->hidpi_scale;
514 rt_tile_free_space(rt, size, it);
516 it->surface = surface;
518 rt->tile_cache_size += size;
525 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, rt->hidpi_scale * rt->tile_width, rt->hidpi_scale * rt->tile_height);
527 size = gdk_pixbuf_get_rowstride(pixbuf) * rt->tile_height * rt->hidpi_scale;
528 rt_tile_free_space(rt, size, it);
532 rt->tile_cache_size += size;
537 *-------------------------------------------------------------------
539 *-------------------------------------------------------------------
542 static void rt_overlay_get_position(RendererTiles *rt, OverlayData *od,
543 gint *x, gint *y, gint *w, gint *h)
545 PixbufRenderer *pr = rt->pr;
548 pw = gdk_pixbuf_get_width(od->pixbuf);
549 ph = gdk_pixbuf_get_height(od->pixbuf);
553 if (od->flags & OVL_RELATIVE)
555 if (px < 0) px = pr->viewport_width - pw + px;
556 if (py < 0) py = pr->viewport_height - ph + py;
565 static void rt_overlay_init_window(RendererTiles *rt, OverlayData *od)
567 PixbufRenderer *pr = rt->pr;
569 GdkWindowAttr attributes;
570 gint attributes_mask;
572 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
574 attributes.window_type = GDK_WINDOW_CHILD;
575 attributes.wclass = GDK_INPUT_OUTPUT;
576 attributes.width = pw;
577 attributes.height = ph;
578 attributes.event_mask = GDK_EXPOSURE_MASK;
581 od->window = gdk_window_new(gtk_widget_get_window(GTK_WIDGET(pr)), &attributes, attributes_mask);
582 gdk_window_set_user_data(od->window, pr);
583 gdk_window_move(od->window, px + rt->stereo_off_x, py + rt->stereo_off_y);
584 gdk_window_show(od->window);
587 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h,
590 PixbufRenderer *pr = rt->pr;
593 work = rt->overlay_list;
600 od = static_cast<OverlayData *>(work->data);
603 if (!od->window) rt_overlay_init_window(rt, od);
605 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
606 if (pr_clip_region(x, y, w, h, px, py, pw, ph, &rx, &ry, &rw, &rh))
608 if (!rt->overlay_buffer)
610 rt->overlay_buffer = gdk_window_create_similar_surface(gtk_widget_get_window(reinterpret_cast<GtkWidget *>(pr)),
612 rt->tile_width, rt->tile_height);
619 cr = cairo_create(rt->overlay_buffer);
620 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);
621 cairo_rectangle(cr, 0, 0, rw, rh);
622 cairo_fill_preserve(cr);
624 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - rx, py - ry);
628 cr = gdk_cairo_create(od->window);
629 cairo_set_source_surface(cr, rt->overlay_buffer, rx - px, ry - py);
630 cairo_rectangle (cr, rx - px, ry - py, rw, rh);
636 /* no ImageTile means region may be larger than our scratch buffer */
639 for (sx = rx; sx < rx + rw; sx += rt->tile_width)
640 for (sy = ry; sy < ry + rh; sy += rt->tile_height)
645 sw = MIN(rx + rw - sx, rt->tile_width);
646 sh = MIN(ry + rh - sy, rt->tile_height);
648 cr = cairo_create(rt->overlay_buffer);
649 cairo_set_source_rgb(cr, 0, 0, 0);
650 cairo_rectangle(cr, 0, 0, sw, sh);
651 cairo_fill_preserve(cr);
653 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - sx, py - sy);
657 cr = gdk_cairo_create(od->window);
658 cairo_set_source_surface(cr, rt->overlay_buffer, sx - px, sy - py);
659 cairo_rectangle (cr, sx - px, sy - py, sw, sh);
668 static void rt_overlay_queue_draw(RendererTiles *rt, OverlayData *od, gint x1, gint y1, gint x2, gint y2)
670 PixbufRenderer *pr = rt->pr;
673 rt_overlay_get_position(rt, od, &x, &y, &w, &h);
681 rt_queue(rt, rt->x_scroll - pr->x_offset + x,
682 rt->y_scroll - pr->y_offset + y,
684 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
686 rt_border_draw(rt, x, y, w, h);
689 static void rt_overlay_queue_all(RendererTiles *rt, gint x1, gint y1, gint x2, gint y2)
693 work = rt->overlay_list;
696 auto od = static_cast<OverlayData *>(work->data);
699 rt_overlay_queue_draw(rt, od, x1, y1, x2, y2);
703 static void rt_overlay_update_sizes(RendererTiles *rt)
707 work = rt->overlay_list;
710 auto od = static_cast<OverlayData *>(work->data);
713 if (!od->window) rt_overlay_init_window(rt, od);
715 if (od->flags & OVL_RELATIVE)
719 rt_overlay_get_position(rt, od, &x, &y, &w, &h);
720 gdk_window_move_resize(od->window, x + rt->stereo_off_x, y + rt->stereo_off_y, w, h);
725 static OverlayData *rt_overlay_find(RendererTiles *rt, gint id)
729 work = rt->overlay_list;
732 auto od = static_cast<OverlayData *>(work->data);
735 if (od->id == id) return od;
742 gint renderer_tiles_overlay_add(void *renderer, GdkPixbuf *pixbuf, gint x, gint y,
743 OverlayRendererFlags flags)
745 auto rt = static_cast<RendererTiles *>(renderer);
746 PixbufRenderer *pr = rt->pr;
750 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
751 g_return_val_if_fail(pixbuf != nullptr, -1);
754 while (rt_overlay_find(rt, id)) id++;
756 od = g_new0(OverlayData, 1);
759 g_object_ref(G_OBJECT(od->pixbuf));
764 rt_overlay_init_window(rt, od);
766 rt->overlay_list = g_list_append(rt->overlay_list, od);
768 gtk_widget_queue_draw(GTK_WIDGET(rt->pr));
773 static void rt_overlay_free(RendererTiles *rt, OverlayData *od)
775 rt->overlay_list = g_list_remove(rt->overlay_list, od);
777 if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
778 if (od->window) gdk_window_destroy(od->window);
781 if (!rt->overlay_list && rt->overlay_buffer)
783 cairo_surface_destroy(rt->overlay_buffer);
784 rt->overlay_buffer = nullptr;
788 static void rt_overlay_list_clear(RendererTiles *rt)
790 while (rt->overlay_list)
794 od = static_cast<OverlayData *>(rt->overlay_list->data);
795 rt_overlay_free(rt, od);
799 static void rt_overlay_list_reset_window(RendererTiles *rt)
803 if (rt->overlay_buffer) cairo_surface_destroy(rt->overlay_buffer);
804 rt->overlay_buffer = nullptr;
806 work = rt->overlay_list;
809 auto od = static_cast<OverlayData *>(work->data);
811 if (od->window) gdk_window_destroy(od->window);
812 od->window = nullptr;
816 void renderer_tiles_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint UNUSED(x), gint UNUSED(y))
818 auto rc = static_cast<RendererTiles *>(renderer);
819 PixbufRenderer *pr = rc->pr;
822 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
824 od = rt_overlay_find(rc, id);
829 g_object_ref(G_OBJECT(pixbuf));
830 g_object_unref(G_OBJECT(od->pixbuf));
835 rt_overlay_free(rc, od);
838 gtk_widget_queue_draw(GTK_WIDGET(rc->pr));
841 gboolean renderer_tiles_overlay_get(void *renderer, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
843 auto rt = static_cast<RendererTiles *>(renderer);
844 PixbufRenderer *pr = rt->pr;
847 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
849 od = rt_overlay_find(rt, id);
850 if (!od) return FALSE;
852 if (pixbuf) *pixbuf = od->pixbuf;
859 static void rt_hierarchy_changed_cb(GtkWidget *UNUSED(widget), GtkWidget *UNUSED(previous_toplevel), gpointer data)
861 auto rt = static_cast<RendererTiles *>(data);
862 rt_overlay_list_reset_window(rt);
866 *-------------------------------------------------------------------
868 *-------------------------------------------------------------------
871 static GdkPixbuf *rt_get_spare_tile(RendererTiles *rt)
873 if (!rt->spare_tile) rt->spare_tile = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, rt->tile_width * rt->hidpi_scale, rt->tile_height * rt->hidpi_scale);
874 return rt->spare_tile;
877 #define COLOR_BYTES 3 /* rgb */
879 static void rt_tile_rotate_90_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
881 GdkPixbuf *src = *tile;
884 guchar *s_pix, *d_pix;
886 guchar *ip, *spi, *dpi;
888 gint tw = rt->tile_width * rt->hidpi_scale;
890 srs = gdk_pixbuf_get_rowstride(src);
891 s_pix = gdk_pixbuf_get_pixels(src);
892 spi = s_pix + (x * COLOR_BYTES);
894 dest = rt_get_spare_tile(rt);
895 drs = gdk_pixbuf_get_rowstride(dest);
896 d_pix = gdk_pixbuf_get_pixels(dest);
897 dpi = d_pix + (tw - 1) * COLOR_BYTES;
899 for (i = y; i < y + h; i++)
901 sp = spi + (i * srs);
902 ip = dpi - (i * COLOR_BYTES);
903 for (j = x; j < x + w; j++)
906 memcpy(dp, sp, COLOR_BYTES);
911 rt->spare_tile = src;
915 static void rt_tile_rotate_90_counter_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
917 GdkPixbuf *src = *tile;
920 guchar *s_pix, *d_pix;
922 guchar *ip, *spi, *dpi;
924 gint th = rt->tile_height * rt->hidpi_scale;
926 srs = gdk_pixbuf_get_rowstride(src);
927 s_pix = gdk_pixbuf_get_pixels(src);
928 spi = s_pix + (x * COLOR_BYTES);
930 dest = rt_get_spare_tile(rt);
931 drs = gdk_pixbuf_get_rowstride(dest);
932 d_pix = gdk_pixbuf_get_pixels(dest);
933 dpi = d_pix + (th - 1) * drs;
935 for (i = y; i < y + h; i++)
937 sp = spi + (i * srs);
938 ip = dpi + (i * COLOR_BYTES);
939 for (j = x; j < x + w; j++)
942 memcpy(dp, sp, COLOR_BYTES);
947 rt->spare_tile = src;
951 static void rt_tile_mirror_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
953 GdkPixbuf *src = *tile;
956 guchar *s_pix, *d_pix;
961 gint tw = rt->tile_width * rt->hidpi_scale;
963 srs = gdk_pixbuf_get_rowstride(src);
964 s_pix = gdk_pixbuf_get_pixels(src);
965 spi = s_pix + (x * COLOR_BYTES);
967 dest = rt_get_spare_tile(rt);
968 drs = gdk_pixbuf_get_rowstride(dest);
969 d_pix = gdk_pixbuf_get_pixels(dest);
970 dpi = d_pix + (tw - x - 1) * COLOR_BYTES;
972 for (i = y; i < y + h; i++)
974 sp = spi + (i * srs);
975 dp = dpi + (i * drs);
976 for (j = 0; j < w; j++)
978 memcpy(dp, sp, COLOR_BYTES);
984 rt->spare_tile = src;
988 static void rt_tile_mirror_and_flip(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
990 GdkPixbuf *src = *tile;
993 guchar *s_pix, *d_pix;
997 gint tw = rt->tile_width * rt->hidpi_scale;
998 gint th = rt->tile_height * rt->hidpi_scale;
1000 srs = gdk_pixbuf_get_rowstride(src);
1001 s_pix = gdk_pixbuf_get_pixels(src);
1003 dest = rt_get_spare_tile(rt);
1004 drs = gdk_pixbuf_get_rowstride(dest);
1005 d_pix = gdk_pixbuf_get_pixels(dest);
1006 dpi = d_pix + (th - 1) * drs + (tw - 1) * COLOR_BYTES;
1008 for (i = y; i < y + h; i++)
1010 sp = s_pix + (i * srs) + (x * COLOR_BYTES);
1011 dp = dpi - (i * drs) - (x * COLOR_BYTES);
1012 for (j = 0; j < w; j++)
1014 memcpy(dp, sp, COLOR_BYTES);
1020 rt->spare_tile = src;
1024 static void rt_tile_flip_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1026 GdkPixbuf *src = *tile;
1029 guchar *s_pix, *d_pix;
1033 gint th = rt->tile_height * rt->hidpi_scale;
1035 srs = gdk_pixbuf_get_rowstride(src);
1036 s_pix = gdk_pixbuf_get_pixels(src);
1037 spi = s_pix + (x * COLOR_BYTES);
1039 dest = rt_get_spare_tile(rt);
1040 drs = gdk_pixbuf_get_rowstride(dest);
1041 d_pix = gdk_pixbuf_get_pixels(dest);
1042 dpi = d_pix + (th - 1) * drs + (x * COLOR_BYTES);
1044 for (i = y; i < y + h; i++)
1046 sp = spi + (i * srs);
1047 dp = dpi - (i * drs);
1048 memcpy(dp, sp, w * COLOR_BYTES);
1051 rt->spare_tile = src;
1055 static void rt_tile_apply_orientation(RendererTiles *rt, gint orientation, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h)
1057 switch (orientation)
1059 case EXIF_ORIENTATION_TOP_LEFT:
1060 /* normal -- nothing to do */
1062 case EXIF_ORIENTATION_TOP_RIGHT:
1065 rt_tile_mirror_only(rt, pixbuf, x, y, w, h);
1068 case EXIF_ORIENTATION_BOTTOM_RIGHT:
1071 rt_tile_mirror_and_flip(rt, pixbuf, x, y, w, h);
1074 case EXIF_ORIENTATION_BOTTOM_LEFT:
1077 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1080 case EXIF_ORIENTATION_LEFT_TOP:
1082 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1083 rt_tile_rotate_90_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1086 case EXIF_ORIENTATION_RIGHT_TOP:
1087 /* rotated -90 (270) */
1089 rt_tile_rotate_90_clockwise(rt, pixbuf, x, y, w, h);
1092 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1094 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1095 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1098 case EXIF_ORIENTATION_LEFT_BOTTOM:
1101 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, y, w, h);
1105 /* The other values are out of range */
1110 static gboolean rt_source_tile_render(RendererTiles *rt, ImageTile *it,
1111 gint x, gint y, gint w, gint h,
1112 gboolean UNUSED(new_data), gboolean fast)
1114 PixbufRenderer *pr = rt->pr;
1117 gboolean draw = FALSE;
1119 if (pr->zoom == 1.0 || pr->scale == 1.0)
1121 list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE);
1126 gint rx, ry, rw, rh;
1128 st = static_cast<SourceTile *>(work->data);
1131 if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1132 it->x + x, it->y + y, w, h,
1133 &rx, &ry, &rw, &rh))
1136 cr = cairo_create(it->surface);
1137 cairo_rectangle (cr, rx - it->x, ry - it->y, rw, rh);
1141 cairo_set_source_rgb(cr, 0, 0, 0);
1144 else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
1146 rt_hidpi_aware_draw(rt, cr, st->pixbuf, -it->x + st->x, -it->y + st->y);
1154 gdouble scale_x, scale_y;
1155 gint sx, sy, sw, sh;
1157 if (pr->image_width == 0 || pr->image_height == 0) return FALSE;
1158 scale_x = static_cast<gdouble>(pr->width) / pr->image_width;
1159 scale_y = static_cast<gdouble>(pr->height) / pr->image_height;
1161 sx = static_cast<gdouble>(it->x + x) / scale_x;
1162 sy = static_cast<gdouble>(it->y + y) / scale_y;
1163 sw = static_cast<gdouble>(w) / scale_x;
1164 sh = static_cast<gdouble>(h) / scale_y;
1166 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1169 /* draws red over draw region, to check for leaks (regions not filled) */
1170 pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255);
1173 list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE);
1178 gint rx, ry, rw, rh;
1179 gint stx, sty, stw, sth;
1181 st = static_cast<SourceTile *>(work->data);
1184 stx = floor(static_cast<gdouble>(st->x) * scale_x);
1185 sty = floor(static_cast<gdouble>(st->y) * scale_y);
1186 stw = ceil(static_cast<gdouble>(st->x + pr->source_tile_width) * scale_x) - stx;
1187 sth = ceil(static_cast<gdouble>(st->y + pr->source_tile_height) * scale_y) - sty;
1189 if (pr_clip_region(stx, sty, stw, sth,
1190 it->x + x, it->y + y, w, h,
1191 &rx, &ry, &rw, &rh))
1197 cr = cairo_create(it->surface);
1198 cairo_rectangle (cr, rx - st->x, ry - st->y, rw, rh);
1199 cairo_set_source_rgb(cr, 0, 0, 0);
1208 /* may need to use unfloored stx,sty values here */
1209 offset_x = static_cast<gdouble>(stx - it->x);
1210 offset_y = static_cast<gdouble>(sty - it->y);
1212 gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh,
1213 static_cast<gdouble>(0.0) + offset_x,
1214 static_cast<gdouble>(0.0) + offset_y,
1216 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1231 * @param ignore_alpha
1242 * @param interp_type
1245 * @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
1249 static void rt_tile_get_region(gboolean has_alpha, gboolean ignore_alpha,
1250 const GdkPixbuf *src, GdkPixbuf *dest,
1251 int pb_x, int pb_y, int pb_w, int pb_h,
1252 double offset_x, double offset_y, double scale_x, double scale_y,
1253 GdkInterpType interp_type,
1254 int check_x, int check_y, gboolean wide_image)
1256 GdkPixbuf* tmppixbuf;
1267 if (scale_x == 1.0 && scale_y == 1.0)
1271 srs = gdk_pixbuf_get_rowstride(src);
1272 drs = gdk_pixbuf_get_rowstride(dest);
1273 psrc = gdk_pixbuf_get_pixels(src);
1274 pdst = gdk_pixbuf_get_pixels(dest);
1275 for (y = 0; y < pb_h; y++)
1277 for (x = 0; x < pb_w; x++)
1279 for (c = 0; c < 3; c++)
1281 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];
1288 gdk_pixbuf_copy_area(src,
1289 -offset_x + pb_x, -offset_y + pb_y,
1297 gdk_pixbuf_scale(src, dest,
1298 pb_x, pb_y, pb_w, pb_h,
1302 (wide_image && interp_type == GDK_INTERP_NEAREST) ? GDK_INTERP_TILES : interp_type);
1309 tmppixbuf = gdk_pixbuf_add_alpha(src, FALSE, 0, 0, 0);
1311 pixbuf_ignore_alpha_rect(tmppixbuf, 0, 0, gdk_pixbuf_get_width(src), gdk_pixbuf_get_height(src));
1313 gdk_pixbuf_composite_color(tmppixbuf, dest,
1314 pb_x, pb_y, pb_w, pb_h,
1318 (scale_x == 1.0 && scale_y == 1.0) ? GDK_INTERP_NEAREST : interp_type,
1319 255, check_x, check_y,
1320 PR_ALPHA_CHECK_SIZE,
1321 ((options->image.alpha_color_1.red << 8 & 0x00FF0000) +
1322 (options->image.alpha_color_1.green & 0x00FF00) +
1323 (options->image.alpha_color_1.blue >> 8 & 0x00FF)),
1324 ((options->image.alpha_color_2.red << 8 & 0x00FF0000) +
1325 (options->image.alpha_color_2.green & 0x00FF00) +
1326 (options->image.alpha_color_2.blue >> 8 & 0x00FF)));
1327 g_object_unref(tmppixbuf);
1331 gdk_pixbuf_composite_color(src, dest,
1332 pb_x, pb_y, pb_w, pb_h,
1336 (scale_x == 1.0 && scale_y == 1.0) ? GDK_INTERP_NEAREST : interp_type,
1337 255, check_x, check_y,
1338 PR_ALPHA_CHECK_SIZE,
1339 ((options->image.alpha_color_1.red << 8 & 0x00FF0000) +
1340 (options->image.alpha_color_1.green & 0x00FF00) +
1341 (options->image.alpha_color_1.blue >> 8 & 0x00FF)),
1342 ((options->image.alpha_color_2.red << 8 & 0x00FF0000) +
1343 (options->image.alpha_color_2.green & 0x00FF00) +
1344 (options->image.alpha_color_2.blue >> 8 & 0x00FF)));
1350 static gint rt_get_orientation(RendererTiles *rt)
1352 PixbufRenderer *pr = rt->pr;
1354 gint orientation = pr->orientation;
1355 static const gint mirror[] = {1, 2, 1, 4, 3, 6, 5, 8, 7};
1356 static const gint flip[] = {1, 4, 3, 2, 1, 8, 7, 6, 5};
1358 if (rt->stereo_mode & PR_STEREO_MIRROR) orientation = mirror[orientation];
1359 if (rt->stereo_mode & PR_STEREO_FLIP) orientation = flip[orientation];
1364 static void rt_tile_render(RendererTiles *rt, ImageTile *it,
1365 gint x, gint y, gint w, gint h,
1366 gboolean new_data, gboolean fast)
1368 PixbufRenderer *pr = rt->pr;
1370 gboolean draw = FALSE;
1371 gint orientation = rt_get_orientation(rt);
1372 gboolean wide_image = FALSE;
1374 if (it->render_todo == TILE_RENDER_NONE && it->surface && !new_data) return;
1376 if (it->render_done != TILE_RENDER_ALL)
1382 if (!fast) it->render_done = TILE_RENDER_ALL;
1384 else if (it->render_todo != TILE_RENDER_AREA)
1386 if (!fast) it->render_todo = TILE_RENDER_NONE;
1390 if (!fast) it->render_todo = TILE_RENDER_NONE;
1392 if (new_data) it->blank = FALSE;
1394 rt_tile_prepare(rt, it);
1395 has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
1397 /** @FIXME checker colors for alpha should be configurable,
1398 * also should be drawn for blank = TRUE
1403 /* no data, do fast rect fill */
1405 cr = cairo_create(it->surface);
1406 cairo_rectangle (cr, 0, 0, it->w, it->h);
1407 cairo_set_source_rgb(cr, 0, 0, 0);
1411 else if (pr->source_tiles_enabled)
1413 draw = rt_source_tile_render(rt, it, x, y, w, h, new_data, fast);
1417 gdouble scale_x, scale_y;
1418 gdouble src_x, src_y;
1422 if (pr->image_width == 0 || pr->image_height == 0) return;
1424 scale_x = rt->hidpi_scale * static_cast<gdouble>(pr->width) / pr->image_width;
1425 scale_y = rt->hidpi_scale * static_cast<gdouble>(pr->height) / pr->image_height;
1427 pr_tile_coords_map_orientation(orientation, it->x, it->y,
1428 pr->width, pr->height,
1429 rt->tile_width, rt->tile_height,
1431 pr_tile_region_map_orientation(orientation, x, y,
1432 rt->tile_width, rt->tile_height,
1437 src_x *= rt->hidpi_scale;
1438 src_y *= rt->hidpi_scale;
1439 pb_x *= rt->hidpi_scale;
1440 pb_y *= rt->hidpi_scale;
1441 pb_w *= rt->hidpi_scale;
1442 pb_h *= rt->hidpi_scale;
1444 switch (orientation)
1447 case EXIF_ORIENTATION_LEFT_TOP:
1448 case EXIF_ORIENTATION_RIGHT_TOP:
1449 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1450 case EXIF_ORIENTATION_LEFT_BOTTOM:
1460 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
1461 * small sizes for anything but GDK_INTERP_NEAREST
1463 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1464 if (pr->image_width > 32767) wide_image = TRUE;
1466 rt_tile_get_region(has_alpha, pr->ignore_alpha,
1467 pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
1468 static_cast<gdouble>(0.0) - src_x - GET_RIGHT_PIXBUF_OFFSET(rt) * scale_x,
1469 static_cast<gdouble>(0.0) - src_y,
1471 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1472 it->x + pb_x, it->y + pb_y, wide_image);
1473 if (rt->stereo_mode & PR_STEREO_ANAGLYPH &&
1474 (pr->stereo_pixbuf_offset_right > 0 || pr->stereo_pixbuf_offset_left > 0))
1476 GdkPixbuf *right_pb = rt_get_spare_tile(rt);
1477 rt_tile_get_region(has_alpha, pr->ignore_alpha,
1478 pr->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h,
1479 static_cast<gdouble>(0.0) - src_x - GET_LEFT_PIXBUF_OFFSET(rt) * scale_x,
1480 static_cast<gdouble>(0.0) - src_y,
1482 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1483 it->x + pb_x, it->y + pb_y, wide_image);
1484 pr_create_anaglyph(rt->stereo_mode, it->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h);
1485 /* do not care about freeing spare_tile, it will be reused */
1487 rt_tile_apply_orientation(rt, orientation, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
1491 if (draw && it->pixbuf && !it->blank)
1495 if (pr->func_post_process && !(pr->post_process_slow && fast))
1496 pr->func_post_process(pr, &it->pixbuf, x, y, w, h, pr->post_process_user_data);
1498 cr = cairo_create(it->surface);
1499 cairo_rectangle (cr, x, y, w, h);
1500 rt_hidpi_aware_draw(rt, cr, it->pixbuf, 0, 0);
1506 static void rt_tile_expose(RendererTiles *rt, ImageTile *it,
1507 gint x, gint y, gint w, gint h,
1508 gboolean new_data, gboolean fast)
1510 PixbufRenderer *pr = rt->pr;
1512 //~ GdkWindow *window;
1515 /* clamp to visible */
1516 if (it->x + x < rt->x_scroll)
1518 w -= rt->x_scroll - it->x - x;
1519 x = rt->x_scroll - it->x;
1521 if (it->x + x + w > rt->x_scroll + pr->vis_width)
1523 w = rt->x_scroll + pr->vis_width - it->x - x;
1526 if (it->y + y < rt->y_scroll)
1528 h -= rt->y_scroll - it->y - y;
1529 y = rt->y_scroll - it->y;
1531 if (it->y + y + h > rt->y_scroll + pr->vis_height)
1533 h = rt->y_scroll + pr->vis_height - it->y - y;
1537 rt_tile_render(rt, it, x, y, w, h, new_data, fast);
1539 //~ box = GTK_WIDGET(pr);
1540 //~ window = gtk_widget_get_window(box);
1542 cr = cairo_create(rt->surface);
1543 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);
1544 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);
1548 if (rt->overlay_list)
1550 rt_overlay_draw(rt, pr->x_offset + (it->x - rt->x_scroll) + x,
1551 pr->y_offset + (it->y - rt->y_scroll) + y,
1556 gtk_widget_queue_draw(GTK_WIDGET(rt->pr));
1560 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it)
1562 PixbufRenderer *pr = rt->pr;
1563 return (it->x + it->w >= rt->x_scroll && it->x < rt->x_scroll + pr->vis_width &&
1564 it->y + it->h >= rt->y_scroll && it->y < rt->y_scroll + pr->vis_height);
1568 *-------------------------------------------------------------------
1570 *-------------------------------------------------------------------
1573 static gint rt_get_queued_area(GList *work)
1579 auto qd = static_cast<QueueData *>(work->data);
1580 area += qd->w * qd->h;
1587 static gboolean rt_queue_schedule_next_draw(RendererTiles *rt, gboolean force_set)
1589 PixbufRenderer *pr = rt->pr;
1591 gint visible_area = pr->vis_width * pr->vis_height;
1596 DEBUG_2("redraw priority: 2pass");
1597 rt->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, rt_queue_draw_idle_cb, rt, nullptr);
1598 return G_SOURCE_REMOVE;
1601 if (visible_area == 0)
1608 percent = 100.0 * rt_get_queued_area(rt->draw_queue) / visible_area;
1613 /* we have enough data for starting intensive redrawing */
1614 DEBUG_2("redraw priority: high %.2f %%", percent);
1615 rt->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW, rt_queue_draw_idle_cb, rt, nullptr);
1616 return G_SOURCE_REMOVE;
1619 if (percent < 1.0 || force_set)
1621 /* queue is (almost) empty, wait 50 ms*/
1622 DEBUG_2("redraw priority: wait %.2f %%", percent);
1623 rt->draw_idle_id = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, 50, rt_queue_draw_idle_cb, rt, nullptr);
1624 return G_SOURCE_REMOVE;
1627 /* keep the same priority as before */
1628 DEBUG_2("redraw priority: no change %.2f %%", percent);
1629 return G_SOURCE_CONTINUE;
1633 static gboolean rt_queue_draw_idle_cb(gpointer data)
1635 auto rt = static_cast<RendererTiles *>(data);
1636 PixbufRenderer *pr = rt->pr;
1641 if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
1642 (!rt->draw_queue && !rt->draw_queue_2pass) ||
1645 pr_render_complete_signal(pr);
1647 rt->draw_idle_id = 0;
1648 return G_SOURCE_REMOVE;
1653 qd = static_cast<QueueData *>(rt->draw_queue->data);
1654 fast = (pr->zoom_2pass && ((pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0) || pr->post_process_slow));
1660 /* still loading, wait till done (also drops the higher priority) */
1662 return rt_queue_schedule_next_draw(rt, FALSE);
1665 qd = static_cast<QueueData *>(rt->draw_queue_2pass->data);
1669 if (gtk_widget_get_realized(GTK_WIDGET(pr)))
1671 if (rt_tile_is_visible(rt, qd->it))
1673 rt_tile_expose(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1675 else if (qd->new_data)
1677 /* if new pixel data, and we already have a pixmap, update the tile */
1678 qd->it->blank = FALSE;
1679 if (qd->it->surface && qd->it->render_done == TILE_RENDER_ALL)
1681 rt_tile_render(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1688 qd->it->qd = nullptr;
1689 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
1694 rt_queue_merge(qd->it->qd2, qd);
1700 rt->draw_queue_2pass = g_list_append(rt->draw_queue_2pass, qd);
1710 qd->it->qd2 = nullptr;
1711 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
1715 if (!rt->draw_queue && !rt->draw_queue_2pass)
1717 pr_render_complete_signal(pr);
1719 rt->draw_idle_id = 0;
1720 return G_SOURCE_REMOVE;
1723 return rt_queue_schedule_next_draw(rt, FALSE);
1726 static void rt_queue_list_free(GList *list)
1735 qd = static_cast<QueueData *>(work->data);
1738 qd->it->qd = nullptr;
1739 qd->it->qd2 = nullptr;
1746 static void rt_queue_clear(RendererTiles *rt)
1748 rt_queue_list_free(rt->draw_queue);
1749 rt->draw_queue = nullptr;
1751 rt_queue_list_free(rt->draw_queue_2pass);
1752 rt->draw_queue_2pass = nullptr;
1754 if (rt->draw_idle_id)
1756 g_source_remove(rt->draw_idle_id);
1757 rt->draw_idle_id = 0;
1762 static void rt_queue_merge(QueueData *parent, QueueData *qd)
1764 if (parent->x + parent->w < qd->x + qd->w)
1766 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
1768 if (parent->x > qd->x)
1770 parent->w += parent->x - qd->x;
1774 if (parent->y + parent->h < qd->y + qd->h)
1776 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
1778 if (parent->y > qd->y)
1780 parent->h += parent->y - qd->y;
1784 parent->new_data |= qd->new_data;
1787 static gboolean rt_clamp_to_visible(RendererTiles *rt, gint *x, gint *y, gint *w, gint *h)
1789 PixbufRenderer *pr = rt->pr;
1796 vh = pr->vis_height;
1801 if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
1804 nx = CLAMP(*x, vx, vx + vw);
1805 nw = CLAMP(*w - (nx - *x), 1, vw);
1807 ny = CLAMP(*y, vy, vy + vh);
1808 nh = CLAMP(*h - (ny - *y), 1, vh);
1818 static gboolean rt_queue_to_tiles(RendererTiles *rt, gint x, gint y, gint w, gint h,
1819 gboolean clamp, ImageRenderType render,
1820 gboolean new_data, gboolean only_existing)
1822 PixbufRenderer *pr = rt->pr;
1827 if (clamp && !rt_clamp_to_visible(rt, &x, &y, &w, &h)) return FALSE;
1829 x1 = ROUND_DOWN(x, rt->tile_width);
1830 x2 = ROUND_UP(x + w, rt->tile_width);
1832 y1 = ROUND_DOWN(y, rt->tile_height);
1833 y2 = ROUND_UP(y + h, rt->tile_height);
1835 for (j = y1; j <= y2; j += rt->tile_height)
1837 for (i = x1; i <= x2; i += rt->tile_width)
1841 it = rt_tile_get(rt, i, j,
1843 (i + rt->tile_width < rt->x_scroll ||
1844 i > rt->x_scroll + pr->vis_width ||
1845 j + rt->tile_height < rt->y_scroll ||
1846 j > rt->y_scroll + pr->vis_height)));
1851 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
1852 (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
1854 it->render_todo = render;
1857 qd = g_new(QueueData, 1);
1859 qd->new_data = new_data;
1869 qd->w = x + w - i - qd->x;
1870 if (qd->x + qd->w > rt->tile_width) qd->w = rt->tile_width - qd->x;
1880 qd->h = y + h - j - qd->y;
1881 if (qd->y + qd->h > rt->tile_height) qd->h = rt->tile_height - qd->y;
1883 if (qd->w < 1 || qd->h < 1)
1889 rt_queue_merge(it->qd, qd);
1895 rt->draw_queue = g_list_append(rt->draw_queue, qd);
1904 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
1905 gboolean clamp, ImageRenderType render,
1906 gboolean new_data, gboolean only_existing)
1908 PixbufRenderer *pr = rt->pr;
1913 nx = CLAMP(x, 0, pr->width - 1);
1914 ny = CLAMP(y, 0, pr->height - 1);
1917 w = CLAMP(w, 0, pr->width - nx);
1918 h = CLAMP(h, 0, pr->height - ny);
1919 if (w < 1 || h < 1) return;
1921 if (rt_queue_to_tiles(rt, nx, ny, w, h, clamp, render, new_data, only_existing) &&
1922 ((!rt->draw_queue && !rt->draw_queue_2pass) || !rt->draw_idle_id))
1924 if (rt->draw_idle_id)
1926 g_source_remove(rt->draw_idle_id);
1927 rt->draw_idle_id = 0;
1929 rt_queue_schedule_next_draw(rt, TRUE);
1933 static void rt_scroll(void *renderer, gint x_off, gint y_off)
1935 auto rt = static_cast<RendererTiles *>(renderer);
1936 PixbufRenderer *pr = rt->pr;
1939 if (rt->stereo_mode & PR_STEREO_MIRROR) x_off = -x_off;
1940 if (rt->stereo_mode & PR_STEREO_FLIP) y_off = -y_off;
1942 gint w = pr->vis_width - abs(x_off);
1943 gint h = pr->vis_height - abs(y_off);
1947 /* scrolled completely to new material */
1948 rt_queue(rt, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1956 cairo_surface_t *surface;
1980 cr = cairo_create(rt->surface);
1981 surface = rt->surface;
1983 /* clipping restricts the intermediate surface's size, so it's a good idea
1985 cairo_rectangle(cr, x1 + pr->x_offset + rt->stereo_off_x, y1 + pr->y_offset + rt->stereo_off_y, w, h);
1987 /* Now push a group to change the target */
1988 cairo_push_group (cr);
1989 cairo_set_source_surface(cr, surface, x1 - x2, y1 - y2);
1991 /* Now copy the intermediate target back */
1992 cairo_pop_group_to_source(cr);
1996 rt_overlay_queue_all(rt, x2, y2, x1, y1);
1998 w = pr->vis_width - w;
1999 h = pr->vis_height - h;
2004 x_off > 0 ? rt->x_scroll + (pr->vis_width - w) : rt->x_scroll, rt->y_scroll,
2005 w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2009 /** @FIXME to optimize this, remove overlap */
2011 rt->x_scroll, y_off > 0 ? rt->y_scroll + (pr->vis_height - h) : rt->y_scroll,
2012 pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2017 static void renderer_area_changed(void *renderer, gint src_x, gint src_y, gint src_w, gint src_h)
2019 auto rt = static_cast<RendererTiles *>(renderer);
2020 PixbufRenderer *pr = rt->pr;
2021 gint x, y, width, height, x1, y1, x2, y2;
2023 gint orientation = rt_get_orientation(rt);
2024 pr_coords_map_orientation_reverse(orientation,
2025 src_x - GET_RIGHT_PIXBUF_OFFSET(rt), src_y,
2026 pr->image_width, pr->image_height,
2031 if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
2033 /* increase region when using a zoom quality that may access surrounding pixels */
2038 x1 = static_cast<gint>(floor(static_cast<gdouble>(x) * pr->scale));
2039 y1 = static_cast<gint>(floor(static_cast<gdouble>(y) * pr->scale * pr->aspect_ratio));
2040 x2 = static_cast<gint>(ceil(static_cast<gdouble>(x + width) * pr->scale));
2041 y2 = static_cast<gint>(ceil(static_cast<gdouble>(y + height) * pr->scale * pr->aspect_ratio));
2043 rt_queue(rt, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
2046 static void renderer_redraw(RendererTiles *rt, gint x, gint y, gint w, gint h,
2047 gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing)
2049 PixbufRenderer *pr = rt->pr;
2051 x -= rt->stereo_off_x;
2052 y -= rt->stereo_off_y;
2054 rt_border_draw(rt, x, y, w, h);
2056 x = MAX(0, x - pr->x_offset + pr->x_scroll);
2057 y = MAX(0, y - pr->y_offset + pr->y_scroll);
2061 MIN(w, pr->width - x),
2062 MIN(h, pr->height - y),
2063 clamp, render, new_data, only_existing);
2066 static void renderer_update_pixbuf(void *renderer, gboolean UNUSED(lazy))
2068 rt_queue_clear(static_cast<RendererTiles *>(renderer));
2071 static void renderer_update_zoom(void *renderer, gboolean lazy)
2073 auto rt = static_cast<RendererTiles *>(renderer);
2074 PixbufRenderer *pr = rt->pr;
2076 rt_tile_invalidate_all(static_cast<RendererTiles *>(renderer));
2079 renderer_redraw(static_cast<RendererTiles *>(renderer), 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, TRUE, FALSE);
2081 rt_border_clear(rt);
2084 static void renderer_invalidate_region(void *renderer, gint x, gint y, gint w, gint h)
2086 rt_tile_invalidate_region(static_cast<RendererTiles *>(renderer), x, y, w, h);
2089 static void renderer_update_viewport(void *renderer)
2091 auto rt = static_cast<RendererTiles *>(renderer);
2093 rt->stereo_off_x = 0;
2094 rt->stereo_off_y = 0;
2096 if (rt->stereo_mode & PR_STEREO_RIGHT)
2098 if (rt->stereo_mode & PR_STEREO_HORIZ)
2100 rt->stereo_off_x = rt->pr->viewport_width;
2102 else if (rt->stereo_mode & PR_STEREO_VERT)
2104 rt->stereo_off_y = rt->pr->viewport_height;
2106 else if (rt->stereo_mode & PR_STEREO_FIXED)
2108 rt->stereo_off_x = rt->pr->stereo_fixed_x_right;
2109 rt->stereo_off_y = rt->pr->stereo_fixed_y_right;
2114 if (rt->stereo_mode & PR_STEREO_FIXED)
2116 rt->stereo_off_x = rt->pr->stereo_fixed_x_left;
2117 rt->stereo_off_y = rt->pr->stereo_fixed_y_left;
2120 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);
2122 rt_overlay_update_sizes(rt);
2123 rt_border_clear(rt);
2126 static void renderer_stereo_set(void *renderer, gint stereo_mode)
2128 auto rt = static_cast<RendererTiles *>(renderer);
2130 rt->stereo_mode = stereo_mode;
2133 static void renderer_free(void *renderer)
2135 auto rt = static_cast<RendererTiles *>(renderer);
2137 rt_tile_free_all(rt);
2138 if (rt->spare_tile) g_object_unref(rt->spare_tile);
2139 if (rt->overlay_buffer) g_object_unref(rt->overlay_buffer);
2140 rt_overlay_list_clear(rt);
2141 /* disconnect "hierarchy-changed" */
2142 g_signal_handlers_disconnect_matched(G_OBJECT(rt->pr), G_SIGNAL_MATCH_DATA,
2143 0, 0, nullptr, nullptr, rt);
2147 static gboolean rt_realize_cb(GtkWidget *widget, gpointer data)
2149 auto rt = static_cast<RendererTiles *>(data);
2154 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));
2156 cr = cairo_create(rt->surface);
2157 cairo_set_source_rgb(cr, static_cast<gdouble>(rt->pr->color.red) / 65535, static_cast<gdouble>(rt->pr->color.green) / 65535, static_cast<gdouble>(rt->pr->color.blue) / 65535);
2165 static gboolean rt_size_allocate_cb(GtkWidget *widget, GdkRectangle *allocation, gpointer data)
2167 auto rt = static_cast<RendererTiles *>(data);
2169 cairo_surface_t *old_surface;
2171 if (gtk_widget_get_realized(GTK_WIDGET(rt->pr)))
2173 old_surface = rt->surface;
2174 rt->surface = gdk_window_create_similar_surface(gtk_widget_get_window(widget), CAIRO_CONTENT_COLOR, allocation->width, allocation->height);
2176 cr = cairo_create(rt->surface);
2178 cairo_set_source_rgb(cr, static_cast<gdouble>(options->image.border_color.red) / 65535, static_cast<gdouble>(options->image.border_color.green) / 65535, static_cast<gdouble>(options->image.border_color.blue) / 65535);
2180 cairo_set_source_surface(cr, old_surface, 0, 0);
2183 cairo_surface_destroy(old_surface);
2185 renderer_redraw(rt, allocation->x, allocation->y, allocation->width, allocation->height, FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2191 static gboolean rt_draw_cb(GtkWidget *UNUSED(widget), cairo_t *cr, gpointer data)
2193 auto rt = static_cast<RendererTiles *>(data);
2197 if (rt->stereo_mode & (PR_STEREO_HORIZ | PR_STEREO_VERT))
2199 cairo_push_group(cr);
2200 cairo_set_source_rgb(cr, static_cast<double>(rt->pr->color.red) / 65535, static_cast<double>(rt->pr->color.green) / 65535, static_cast<double>(rt->pr->color.blue) / 65535);
2202 if (rt->stereo_mode & PR_STEREO_HORIZ)
2204 cairo_rectangle(cr, rt->stereo_off_x, 0, rt->pr->viewport_width, rt->pr->viewport_height);
2208 cairo_rectangle(cr, 0, rt->stereo_off_y, rt->pr->viewport_width, rt->pr->viewport_height);
2213 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);
2215 cairo_set_source_surface(cr, rt->surface, 0, 0);
2218 cairo_pop_group_to_source(cr);
2223 cairo_set_source_surface(cr, rt->surface, 0, 0);
2227 work = rt->overlay_list;
2230 od = static_cast<OverlayData *>(work->data);
2231 gint px, py, pw, ph;
2232 pw = gdk_pixbuf_get_width(od->pixbuf);
2233 ph = gdk_pixbuf_get_height(od->pixbuf);
2237 if (od->flags & OVL_RELATIVE)
2239 if (px < 0) px = rt->pr->viewport_width - pw + px;
2240 if (py < 0) py = rt->pr->viewport_height - ph + py;
2243 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px, py);
2251 RendererFuncs *renderer_tiles_new(PixbufRenderer *pr)
2253 auto rt = g_new0(RendererTiles, 1);
2257 rt->f.area_changed = renderer_area_changed;
2258 rt->f.update_pixbuf = renderer_update_pixbuf;
2259 rt->f.free = renderer_free;
2260 rt->f.update_zoom = renderer_update_zoom;
2261 rt->f.invalidate_region = renderer_invalidate_region;
2262 rt->f.scroll = rt_scroll;
2263 rt->f.update_viewport = renderer_update_viewport;
2266 rt->f.overlay_add = renderer_tiles_overlay_add;
2267 rt->f.overlay_set = renderer_tiles_overlay_set;
2268 rt->f.overlay_get = renderer_tiles_overlay_get;
2270 rt->f.stereo_set = renderer_stereo_set;
2272 rt->tile_width = options->image.tile_size;
2273 rt->tile_height = options->image.tile_size;
2275 rt->tiles = nullptr;
2276 rt->tile_cache_size = 0;
2278 rt->tile_cache_max = PR_CACHE_SIZE_DEFAULT;
2280 rt->draw_idle_id = 0;
2282 rt->stereo_mode = 0;
2283 rt->stereo_off_x = 0;
2284 rt->stereo_off_y = 0;
2286 rt->hidpi_scale = gtk_widget_get_scale_factor(GTK_WIDGET(rt->pr));
2288 g_signal_connect(G_OBJECT(pr), "hierarchy-changed",
2289 G_CALLBACK(rt_hierarchy_changed_cb), rt);
2291 g_signal_connect(G_OBJECT(pr), "draw",
2292 G_CALLBACK(rt_draw_cb), rt);
2293 g_signal_connect(G_OBJECT(pr), "realize", G_CALLBACK(rt_realize_cb), rt);
2294 g_signal_connect(G_OBJECT(pr), "size-allocate", G_CALLBACK(rt_size_allocate_cb), rt);
2296 return reinterpret_cast<RendererFuncs *>(rt);
2300 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */