2 * Copyright (C) 2006 John Ellis
3 * Copyright (C) 2008 - 2021 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 #include "renderer-tiles.h"
30 #include <gdk-pixbuf/gdk-pixbuf.h>
32 #include <glib-object.h>
38 #include "pixbuf-renderer.h"
41 /* comment this out if not using this from within Geeqie
42 * defining GQ_BUILD does these things:
43 * - Sets the shift-click scroller pixbuf to a nice icon instead of a black box
49 #include "pixbuf-util.h"
51 enum ExifOrientationType {
52 EXIF_ORIENTATION_UNKNOWN = 0,
53 EXIF_ORIENTATION_TOP_LEFT = 1,
54 EXIF_ORIENTATION_TOP_RIGHT = 2,
55 EXIF_ORIENTATION_BOTTOM_RIGHT = 3,
56 EXIF_ORIENTATION_BOTTOM_LEFT = 4,
57 EXIF_ORIENTATION_LEFT_TOP = 5,
58 EXIF_ORIENTATION_RIGHT_TOP = 6,
59 EXIF_ORIENTATION_RIGHT_BOTTOM = 7,
60 EXIF_ORIENTATION_LEFT_BOTTOM = 8
71 cairo_surface_t *surface; /* off screen buffer */
72 GdkPixbuf *pixbuf; /* pixbuf area for zooming */
73 gint x; /* x offset into image */
74 gint y; /* y offset into image */
75 gint w; /* width that is visible (may be less if at edge of image) */
76 gint h; /* height '' */
80 /* render_todo: (explanation)
82 AREA render area of tile, usually only used when loading an image
83 note: will jump to an ALL if render_done is not ALL.
84 ALL render entire tile, if never done before w/ ALL, for expose events *only*
87 ImageRenderType render_todo; /* what to do (see above) */
88 ImageRenderType render_done; /* highest that has been done before on tile */
93 guint size; /* est. memory used by pixmap and pixbuf */
116 OverlayRendererFlags flags;
124 gint tile_cache_max; /* max MiB to use for offscreen buffer */
128 gint tile_cols; /* count of tile columns */
129 GList *tiles; /* list of buffer tiles */
130 gint tile_cache_size; /* allocated size of pixmaps/pixbufs */
131 GList *draw_queue; /* list of areas to redraw */
132 GList *draw_queue_2pass;/* list when 2 pass is enabled */
135 cairo_surface_t *overlay_buffer;
136 cairo_surface_t *surface;
138 guint draw_idle_id; /* event source id */
140 GdkPixbuf *spare_tile;
146 gint x_scroll; /* allow local adjustment and mirroring */
152 constexpr size_t COLOR_BYTES = 3; /* rgb */
155 inline gint get_right_pixbuf_offset(RendererTiles *rt)
157 return (!!(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;
161 inline gint get_left_pixbuf_offset(RendererTiles *rt)
163 return (!!(rt->stereo_mode & PR_STEREO_RIGHT) == !!(rt->stereo_mode & PR_STEREO_SWAP)) ?
164 rt->pr->stereo_pixbuf_offset_right : rt->pr->stereo_pixbuf_offset_left;
170 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h, ImageTile *it);
172 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it);
173 static void rt_queue_merge(QueueData *parent, QueueData *qd);
174 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
175 gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing);
177 static gint rt_queue_draw_idle_cb(gpointer data);
180 static void rt_sync_scroll(RendererTiles *rt)
182 PixbufRenderer *pr = rt->pr;
184 rt->x_scroll = (rt->stereo_mode & PR_STEREO_MIRROR) ?
185 pr->width - pr->vis_width - pr->x_scroll
188 rt->y_scroll = (rt->stereo_mode & PR_STEREO_FLIP) ?
189 pr->height - pr->vis_height - pr->y_scroll
194 *-------------------------------------------------------------------
196 *-------------------------------------------------------------------
199 static void rt_border_draw(RendererTiles *rt, gint x, gint y, gint w, gint h)
201 PixbufRenderer *pr = rt->pr;
210 box = GTK_WIDGET(pr);
211 window = gtk_widget_get_window(box);
215 cr = cairo_create(rt->surface);
217 if (!pr->pixbuf && !pr->source_tiles_enabled)
219 if (pr_clip_region(x, y, w, h,
221 pr->viewport_width, pr->viewport_height,
224 cairo_set_source_rgb(cr, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
225 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
227 rt_overlay_draw(rt, rx, ry, rw, rh, nullptr);
233 if (pr->vis_width < pr->viewport_width)
235 if (pr->x_offset > 0 &&
236 pr_clip_region(x, y, w, h,
238 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);
246 if (pr->viewport_width - pr->vis_width - pr->x_offset > 0 &&
247 pr_clip_region(x, y, w, h,
248 pr->x_offset + pr->vis_width, 0,
249 pr->viewport_width - pr->vis_width - pr->x_offset, pr->viewport_height,
252 cairo_set_source_rgb(cr, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
253 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
255 rt_overlay_draw(rt, rx, ry, rw, rh, nullptr);
258 if (pr->vis_height < pr->viewport_height)
260 if (pr->y_offset > 0 &&
261 pr_clip_region(x, y, w, h,
263 pr->vis_width, 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);
271 if (pr->viewport_height - pr->vis_height - pr->y_offset > 0 &&
272 pr_clip_region(x, y, w, h,
273 pr->x_offset, pr->y_offset + pr->vis_height,
274 pr->vis_width, pr->viewport_height - pr->vis_height - pr->y_offset,
277 cairo_set_source_rgb(cr, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
278 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
280 rt_overlay_draw(rt, rx, ry, rw, rh, nullptr);
286 static void rt_border_clear(RendererTiles *rt)
288 PixbufRenderer *pr = rt->pr;
289 rt_border_draw(rt, 0, 0, pr->viewport_width, pr->viewport_height);
294 *-------------------------------------------------------------------
296 *-------------------------------------------------------------------
299 static ImageTile *rt_tile_new(gint x, gint y, gint width, gint height)
301 auto it = g_new0(ImageTile, 1);
308 it->render_done = TILE_RENDER_NONE;
313 static void rt_tile_free(ImageTile *it)
317 if (it->pixbuf) g_object_unref(it->pixbuf);
318 if (it->surface) cairo_surface_destroy(it->surface);
323 static void rt_tile_free_all(RendererTiles *rt)
325 g_list_free_full(rt->tiles, reinterpret_cast<GDestroyNotify>(rt_tile_free));
327 rt->tile_cache_size = 0;
330 static ImageTile *rt_tile_add(RendererTiles *rt, gint x, gint y)
332 PixbufRenderer *pr = rt->pr;
335 it = rt_tile_new(x, y, rt->tile_width, rt->tile_height);
337 if (it->x + it->w > pr->width) it->w = pr->width - it->x;
338 if (it->y + it->h > pr->height) it->h = pr->height - it->y;
340 rt->tiles = g_list_prepend(rt->tiles, it);
341 rt->tile_cache_size += it->size;
346 static void rt_tile_remove(RendererTiles *rt, ImageTile *it)
350 QueueData *qd = it->qd;
353 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
359 QueueData *qd = it->qd2;
362 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
366 rt->tiles = g_list_remove(rt->tiles, it);
367 rt->tile_cache_size -= it->size;
372 static void rt_tile_free_space(RendererTiles *rt, guint space, ImageTile *it)
374 PixbufRenderer *pr = rt->pr;
378 work = g_list_last(rt->tiles);
380 if (pr->source_tiles_enabled && pr->scale < 1.0)
384 tiles = (pr->vis_width / rt->tile_width + 1) * (pr->vis_height / rt->tile_height + 1);
385 tile_max = MAX(tiles * rt->tile_width * rt->tile_height * 3,
386 (gint)((gdouble)rt->tile_cache_max * 1048576.0 * pr->scale));
390 tile_max = rt->tile_cache_max * 1048576;
393 while (work && rt->tile_cache_size + space > tile_max)
397 needle = static_cast<ImageTile *>(work->data);
400 ((!needle->qd && !needle->qd2) || !rt_tile_is_visible(rt, needle))) rt_tile_remove(rt, needle);
404 static void rt_tile_invalidate_all(RendererTiles *rt)
406 PixbufRenderer *pr = rt->pr;
414 it = static_cast<ImageTile *>(work->data);
417 it->render_done = TILE_RENDER_NONE;
418 it->render_todo = TILE_RENDER_ALL;
421 it->w = MIN(rt->tile_width, pr->width - it->x);
422 it->h = MIN(rt->tile_height, pr->height - it->y);
426 static void rt_tile_invalidate_region(RendererTiles *rt, gint x, gint y, gint w, gint h)
434 x1 = ROUND_DOWN(x, rt->tile_width);
435 x2 = ROUND_UP(x + w, rt->tile_width);
437 y1 = ROUND_DOWN(y, rt->tile_height);
438 y2 = ROUND_UP(y + h, rt->tile_height);
445 it = static_cast<ImageTile *>(work->data);
448 if (it->x < x2 && it->x + it->w > x1 &&
449 it->y < y2 && it->y + it->h > y1)
451 it->render_done = TILE_RENDER_NONE;
452 it->render_todo = TILE_RENDER_ALL;
457 static ImageTile *rt_tile_get(RendererTiles *rt, gint x, gint y, gboolean only_existing)
466 it = static_cast<ImageTile *>(work->data);
467 if (it->x == x && it->y == y)
469 rt->tiles = g_list_delete_link(rt->tiles, work);
470 rt->tiles = g_list_prepend(rt->tiles, it);
477 if (only_existing) return nullptr;
479 return rt_tile_add(rt, x, y);
482 static gint pixmap_calc_size(cairo_surface_t *)
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;
551 pw = gdk_pixbuf_get_width(od->pixbuf);
552 ph = gdk_pixbuf_get_height(od->pixbuf);
556 if (od->flags & OVL_RELATIVE)
558 if (px < 0) px = pr->viewport_width - pw + px;
559 if (py < 0) py = pr->viewport_height - ph + py;
568 static void rt_overlay_init_window(RendererTiles *rt, OverlayData *od)
570 PixbufRenderer *pr = rt->pr;
575 GdkWindowAttr attributes;
576 gint attributes_mask;
578 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
580 attributes.window_type = GDK_WINDOW_CHILD;
581 attributes.wclass = GDK_INPUT_OUTPUT;
582 attributes.width = pw;
583 attributes.height = ph;
584 attributes.event_mask = GDK_EXPOSURE_MASK;
587 od->window = gdk_window_new(gtk_widget_get_window(GTK_WIDGET(pr)), &attributes, attributes_mask);
588 gdk_window_set_user_data(od->window, pr);
589 gdk_window_move(od->window, px + rt->stereo_off_x, py + rt->stereo_off_y);
590 gdk_window_show(od->window);
593 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h,
596 PixbufRenderer *pr = rt->pr;
599 work = rt->overlay_list;
612 od = static_cast<OverlayData *>(work->data);
615 if (!od->window) rt_overlay_init_window(rt, od);
617 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
618 if (pr_clip_region(x, y, w, h, px, py, pw, ph, &rx, &ry, &rw, &rh))
620 if (!rt->overlay_buffer)
622 rt->overlay_buffer = gdk_window_create_similar_surface(gtk_widget_get_window(reinterpret_cast<GtkWidget *>(pr)),
624 rt->tile_width, rt->tile_height);
631 cr = cairo_create(rt->overlay_buffer);
632 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);
633 cairo_rectangle(cr, 0, 0, rw, rh);
634 cairo_fill_preserve(cr);
636 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - rx, py - ry);
640 cr = gdk_cairo_create(od->window);
641 cairo_set_source_surface(cr, rt->overlay_buffer, rx - px, ry - py);
642 cairo_rectangle (cr, rx - px, ry - py, rw, rh);
648 /* no ImageTile means region may be larger than our scratch buffer */
652 for (sx = rx; sx < rx + rw; sx += rt->tile_width)
653 for (sy = ry; sy < ry + rh; sy += rt->tile_height)
659 sw = MIN(rx + rw - sx, rt->tile_width);
660 sh = MIN(ry + rh - sy, rt->tile_height);
662 cr = cairo_create(rt->overlay_buffer);
663 cairo_set_source_rgb(cr, 0, 0, 0);
664 cairo_rectangle(cr, 0, 0, sw, sh);
665 cairo_fill_preserve(cr);
667 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - sx, py - sy);
671 cr = gdk_cairo_create(od->window);
672 cairo_set_source_surface(cr, rt->overlay_buffer, sx - px, sy - py);
673 cairo_rectangle (cr, sx - px, sy - py, sw, sh);
682 static void rt_overlay_queue_draw(RendererTiles *rt, OverlayData *od, gint x1, gint y1, gint x2, gint y2)
684 PixbufRenderer *pr = rt->pr;
690 rt_overlay_get_position(rt, od, &x, &y, &w, &h);
698 rt_queue(rt, rt->x_scroll - pr->x_offset + x,
699 rt->y_scroll - pr->y_offset + y,
701 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
703 rt_border_draw(rt, x, y, w, h);
706 static void rt_overlay_queue_all(RendererTiles *rt, gint x1, gint y1, gint x2, gint y2)
710 work = rt->overlay_list;
713 auto od = static_cast<OverlayData *>(work->data);
716 rt_overlay_queue_draw(rt, od, x1, y1, x2, y2);
720 static void rt_overlay_update_sizes(RendererTiles *rt)
724 work = rt->overlay_list;
727 auto od = static_cast<OverlayData *>(work->data);
730 if (!od->window) rt_overlay_init_window(rt, od);
732 if (od->flags & OVL_RELATIVE)
739 rt_overlay_get_position(rt, od, &x, &y, &w, &h);
740 gdk_window_move_resize(od->window, x + rt->stereo_off_x, y + rt->stereo_off_y, w, h);
745 static OverlayData *rt_overlay_find(RendererTiles *rt, gint id)
749 work = rt->overlay_list;
752 auto od = static_cast<OverlayData *>(work->data);
755 if (od->id == id) return od;
762 gint renderer_tiles_overlay_add(void *renderer, GdkPixbuf *pixbuf, gint x, gint y,
763 OverlayRendererFlags flags)
765 auto rt = static_cast<RendererTiles *>(renderer);
766 PixbufRenderer *pr = rt->pr;
769 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
770 g_return_val_if_fail(pixbuf != nullptr, -1);
773 while (rt_overlay_find(rt, id)) id++;
775 auto od = g_new0(OverlayData, 1);
778 g_object_ref(G_OBJECT(od->pixbuf));
783 rt_overlay_init_window(rt, od);
785 rt->overlay_list = g_list_append(rt->overlay_list, od);
787 gtk_widget_queue_draw(GTK_WIDGET(rt->pr));
792 static void rt_overlay_free(RendererTiles *rt, OverlayData *od)
794 rt->overlay_list = g_list_remove(rt->overlay_list, od);
796 if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
797 if (od->window) gdk_window_destroy(od->window);
800 if (!rt->overlay_list && rt->overlay_buffer)
802 cairo_surface_destroy(rt->overlay_buffer);
803 rt->overlay_buffer = nullptr;
807 static void rt_overlay_list_clear(RendererTiles *rt)
809 while (rt->overlay_list)
811 auto od = static_cast<OverlayData *>(rt->overlay_list->data);
812 rt_overlay_free(rt, od);
816 static void rt_overlay_list_reset_window(RendererTiles *rt)
820 if (rt->overlay_buffer) cairo_surface_destroy(rt->overlay_buffer);
821 rt->overlay_buffer = nullptr;
823 work = rt->overlay_list;
826 auto od = static_cast<OverlayData *>(work->data);
828 if (od->window) gdk_window_destroy(od->window);
829 od->window = nullptr;
833 void renderer_tiles_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint, gint)
835 auto rc = static_cast<RendererTiles *>(renderer);
836 PixbufRenderer *pr = rc->pr;
839 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
841 od = rt_overlay_find(rc, id);
846 g_object_ref(G_OBJECT(pixbuf));
847 g_object_unref(G_OBJECT(od->pixbuf));
852 rt_overlay_free(rc, od);
855 gtk_widget_queue_draw(GTK_WIDGET(rc->pr));
858 gboolean renderer_tiles_overlay_get(void *renderer, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
860 auto rt = static_cast<RendererTiles *>(renderer);
861 PixbufRenderer *pr = rt->pr;
864 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
866 od = rt_overlay_find(rt, id);
867 if (!od) return FALSE;
869 if (pixbuf) *pixbuf = od->pixbuf;
876 static void rt_hierarchy_changed_cb(GtkWidget *, GtkWidget *, gpointer data)
878 auto rt = static_cast<RendererTiles *>(data);
879 rt_overlay_list_reset_window(rt);
883 *-------------------------------------------------------------------
885 *-------------------------------------------------------------------
888 static GdkPixbuf *rt_get_spare_tile(RendererTiles *rt)
890 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);
891 return rt->spare_tile;
894 static void rt_tile_rotate_90_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
896 GdkPixbuf *src = *tile;
909 gint tw = rt->tile_width * rt->hidpi_scale;
911 srs = gdk_pixbuf_get_rowstride(src);
912 s_pix = gdk_pixbuf_get_pixels(src);
913 spi = s_pix + (x * COLOR_BYTES);
915 dest = rt_get_spare_tile(rt);
916 drs = gdk_pixbuf_get_rowstride(dest);
917 d_pix = gdk_pixbuf_get_pixels(dest);
918 dpi = d_pix + (tw - 1) * COLOR_BYTES;
920 for (i = y; i < y + h; i++)
922 sp = spi + (i * srs);
923 ip = dpi - (i * COLOR_BYTES);
924 for (j = x; j < x + w; j++)
927 memcpy(dp, sp, COLOR_BYTES);
932 rt->spare_tile = src;
936 static void rt_tile_rotate_90_counter_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
938 GdkPixbuf *src = *tile;
951 gint th = rt->tile_height * rt->hidpi_scale;
953 srs = gdk_pixbuf_get_rowstride(src);
954 s_pix = gdk_pixbuf_get_pixels(src);
955 spi = s_pix + (x * COLOR_BYTES);
957 dest = rt_get_spare_tile(rt);
958 drs = gdk_pixbuf_get_rowstride(dest);
959 d_pix = gdk_pixbuf_get_pixels(dest);
960 dpi = d_pix + (th - 1) * drs;
962 for (i = y; i < y + h; i++)
964 sp = spi + (i * srs);
965 ip = dpi + (i * COLOR_BYTES);
966 for (j = x; j < x + w; j++)
969 memcpy(dp, sp, COLOR_BYTES);
974 rt->spare_tile = src;
978 static void rt_tile_mirror_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
980 GdkPixbuf *src = *tile;
993 gint tw = rt->tile_width * rt->hidpi_scale;
995 srs = gdk_pixbuf_get_rowstride(src);
996 s_pix = gdk_pixbuf_get_pixels(src);
997 spi = s_pix + (x * COLOR_BYTES);
999 dest = rt_get_spare_tile(rt);
1000 drs = gdk_pixbuf_get_rowstride(dest);
1001 d_pix = gdk_pixbuf_get_pixels(dest);
1002 dpi = d_pix + (tw - x - 1) * COLOR_BYTES;
1004 for (i = y; i < y + h; i++)
1006 sp = spi + (i * srs);
1007 dp = dpi + (i * drs);
1008 for (j = 0; j < w; j++)
1010 memcpy(dp, sp, COLOR_BYTES);
1016 rt->spare_tile = src;
1020 static void rt_tile_mirror_and_flip(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1022 GdkPixbuf *src = *tile;
1033 gint tw = rt->tile_width * rt->hidpi_scale;
1034 gint th = rt->tile_height * rt->hidpi_scale;
1036 srs = gdk_pixbuf_get_rowstride(src);
1037 s_pix = gdk_pixbuf_get_pixels(src);
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 + (tw - 1) * COLOR_BYTES;
1044 for (i = y; i < y + h; i++)
1046 sp = s_pix + (i * srs) + (x * COLOR_BYTES);
1047 dp = dpi - (i * drs) - (x * COLOR_BYTES);
1048 for (j = 0; j < w; j++)
1050 memcpy(dp, sp, COLOR_BYTES);
1056 rt->spare_tile = src;
1060 static void rt_tile_flip_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1062 GdkPixbuf *src = *tile;
1073 gint th = rt->tile_height * rt->hidpi_scale;
1075 srs = gdk_pixbuf_get_rowstride(src);
1076 s_pix = gdk_pixbuf_get_pixels(src);
1077 spi = s_pix + (x * COLOR_BYTES);
1079 dest = rt_get_spare_tile(rt);
1080 drs = gdk_pixbuf_get_rowstride(dest);
1081 d_pix = gdk_pixbuf_get_pixels(dest);
1082 dpi = d_pix + (th - 1) * drs + (x * COLOR_BYTES);
1084 for (i = y; i < y + h; i++)
1086 sp = spi + (i * srs);
1087 dp = dpi - (i * drs);
1088 memcpy(dp, sp, w * COLOR_BYTES);
1091 rt->spare_tile = src;
1095 static void rt_tile_apply_orientation(RendererTiles *rt, gint orientation, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h)
1097 switch (orientation)
1099 case EXIF_ORIENTATION_TOP_LEFT:
1100 /* normal -- nothing to do */
1102 case EXIF_ORIENTATION_TOP_RIGHT:
1105 rt_tile_mirror_only(rt, pixbuf, x, y, w, h);
1108 case EXIF_ORIENTATION_BOTTOM_RIGHT:
1111 rt_tile_mirror_and_flip(rt, pixbuf, x, y, w, h);
1114 case EXIF_ORIENTATION_BOTTOM_LEFT:
1117 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1120 case EXIF_ORIENTATION_LEFT_TOP:
1122 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1123 rt_tile_rotate_90_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1126 case EXIF_ORIENTATION_RIGHT_TOP:
1127 /* rotated -90 (270) */
1129 rt_tile_rotate_90_clockwise(rt, pixbuf, x, y, w, h);
1132 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1134 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1135 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1138 case EXIF_ORIENTATION_LEFT_BOTTOM:
1141 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, y, w, h);
1145 /* The other values are out of range */
1150 static gboolean rt_source_tile_render(RendererTiles *rt, ImageTile *it,
1151 gint x, gint y, gint w, gint h,
1152 gboolean, gboolean fast)
1154 PixbufRenderer *pr = rt->pr;
1157 gboolean draw = FALSE;
1159 if (pr->zoom == 1.0 || pr->scale == 1.0)
1161 list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE);
1171 st = static_cast<SourceTile *>(work->data);
1174 if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1175 it->x + x, it->y + y, w, h,
1176 &rx, &ry, &rw, &rh))
1179 cr = cairo_create(it->surface);
1180 cairo_rectangle (cr, rx - it->x, ry - it->y, rw, rh);
1184 cairo_set_source_rgb(cr, 0, 0, 0);
1187 else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
1189 rt_hidpi_aware_draw(rt, cr, st->pixbuf, -it->x + st->x, -it->y + st->y);
1204 if (pr->image_width == 0 || pr->image_height == 0) return FALSE;
1205 scale_x = static_cast<gdouble>(pr->width) / pr->image_width;
1206 scale_y = static_cast<gdouble>(pr->height) / pr->image_height;
1208 sx = static_cast<gdouble>(it->x + x) / scale_x;
1209 sy = static_cast<gdouble>(it->y + y) / scale_y;
1210 sw = static_cast<gdouble>(w) / scale_x;
1211 sh = static_cast<gdouble>(h) / scale_y;
1213 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1216 /* draws red over draw region, to check for leaks (regions not filled) */
1217 pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255);
1220 list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE);
1234 st = static_cast<SourceTile *>(work->data);
1237 stx = floor(static_cast<gdouble>(st->x) * scale_x);
1238 sty = floor(static_cast<gdouble>(st->y) * scale_y);
1239 stw = ceil(static_cast<gdouble>(st->x + pr->source_tile_width) * scale_x) - stx;
1240 sth = ceil(static_cast<gdouble>(st->y + pr->source_tile_height) * scale_y) - sty;
1242 if (pr_clip_region(stx, sty, stw, sth,
1243 it->x + x, it->y + y, w, h,
1244 &rx, &ry, &rw, &rh))
1250 cr = cairo_create(it->surface);
1251 cairo_rectangle (cr, rx - st->x, ry - st->y, rw, rh);
1252 cairo_set_source_rgb(cr, 0, 0, 0);
1261 /* may need to use unfloored stx,sty values here */
1262 offset_x = static_cast<gdouble>(stx - it->x);
1263 offset_y = static_cast<gdouble>(sty - it->y);
1265 gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh,
1266 static_cast<gdouble>(0.0) + offset_x,
1267 static_cast<gdouble>(0.0) + offset_y,
1269 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1284 * @param ignore_alpha
1295 * @param interp_type
1298 * @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
1302 static void rt_tile_get_region(gboolean has_alpha, gboolean ignore_alpha,
1303 const GdkPixbuf *src, GdkPixbuf *dest,
1304 int pb_x, int pb_y, int pb_w, int pb_h,
1305 double offset_x, double offset_y, double scale_x, double scale_y,
1306 GdkInterpType interp_type,
1307 int check_x, int check_y, gboolean wide_image)
1309 GdkPixbuf* tmppixbuf;
1328 if (scale_x == 1.0 && scale_y == 1.0)
1332 srs = gdk_pixbuf_get_rowstride(src);
1333 drs = gdk_pixbuf_get_rowstride(dest);
1334 psrc = gdk_pixbuf_get_pixels(src);
1335 pdst = gdk_pixbuf_get_pixels(dest);
1336 for (y = 0; y < pb_h; y++)
1338 for (x = 0; x < pb_w; x++)
1340 for (c = 0; c < 3; c++)
1342 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];
1349 gdk_pixbuf_copy_area(src,
1350 -offset_x + pb_x, -offset_y + pb_y,
1358 gdk_pixbuf_scale(src, dest,
1359 pb_x, pb_y, pb_w, pb_h,
1363 (wide_image && interp_type == GDK_INTERP_NEAREST) ? GDK_INTERP_TILES : interp_type);
1368 red_1=static_cast<guint32>(options->image.alpha_color_1.red * 255) << 16 & 0x00FF0000;
1369 green_1=static_cast<guint32>(options->image.alpha_color_1.green * 255) << 8 & 0x0000FF00;
1370 blue_1=static_cast<guint32>(options->image.alpha_color_1.blue * 255) & 0x000000FF;
1371 red_2=static_cast<guint32>(options->image.alpha_color_2.red * 255) << 16 & 0x00FF0000;
1372 green_2=static_cast<guint32>(options->image.alpha_color_2.green * 255) << 8 & 0x0000FF00;
1373 blue_2=static_cast<guint32>(options->image.alpha_color_2.blue * 255) & 0x000000FF;
1374 alpha_1 = red_1 + green_1 + blue_1;
1375 alpha_2 = red_2 + green_2 + blue_2;
1379 tmppixbuf = gdk_pixbuf_add_alpha(src, FALSE, 0, 0, 0);
1381 pixbuf_ignore_alpha_rect(tmppixbuf, 0, 0, gdk_pixbuf_get_width(src), gdk_pixbuf_get_height(src));
1383 gdk_pixbuf_composite_color(tmppixbuf, dest,
1384 pb_x, pb_y, pb_w, pb_h,
1388 (scale_x == 1.0 && scale_y == 1.0) ? GDK_INTERP_NEAREST : interp_type,
1389 255, check_x, check_y,
1390 PR_ALPHA_CHECK_SIZE,
1393 g_object_unref(tmppixbuf);
1397 gdk_pixbuf_composite_color(src, dest,
1398 pb_x, pb_y, pb_w, pb_h,
1402 (scale_x == 1.0 && scale_y == 1.0) ? GDK_INTERP_NEAREST : interp_type,
1403 255, check_x, check_y,
1404 PR_ALPHA_CHECK_SIZE,
1412 static gint rt_get_orientation(RendererTiles *rt)
1414 PixbufRenderer *pr = rt->pr;
1416 gint orientation = pr->orientation;
1417 static const gint mirror[] = {1, 2, 1, 4, 3, 6, 5, 8, 7};
1418 static const gint flip[] = {1, 4, 3, 2, 1, 8, 7, 6, 5};
1420 if (rt->stereo_mode & PR_STEREO_MIRROR) orientation = mirror[orientation];
1421 if (rt->stereo_mode & PR_STEREO_FLIP) orientation = flip[orientation];
1426 static void rt_tile_render(RendererTiles *rt, ImageTile *it,
1427 gint x, gint y, gint w, gint h,
1428 gboolean new_data, gboolean fast)
1430 PixbufRenderer *pr = rt->pr;
1432 gboolean draw = FALSE;
1433 gint orientation = rt_get_orientation(rt);
1434 gboolean wide_image = FALSE;
1436 if (it->render_todo == TILE_RENDER_NONE && it->surface && !new_data) return;
1438 if (it->render_done != TILE_RENDER_ALL)
1444 if (!fast) it->render_done = TILE_RENDER_ALL;
1446 else if (it->render_todo != TILE_RENDER_AREA)
1448 if (!fast) it->render_todo = TILE_RENDER_NONE;
1452 if (!fast) it->render_todo = TILE_RENDER_NONE;
1454 if (new_data) it->blank = FALSE;
1456 rt_tile_prepare(rt, it);
1457 has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
1459 /** @FIXME checker colors for alpha should be configurable,
1460 * also should be drawn for blank = TRUE
1465 /* no data, do fast rect fill */
1467 cr = cairo_create(it->surface);
1468 cairo_rectangle (cr, 0, 0, it->w, it->h);
1469 cairo_set_source_rgb(cr, 0, 0, 0);
1473 else if (pr->source_tiles_enabled)
1475 draw = rt_source_tile_render(rt, it, x, y, w, h, new_data, fast);
1488 if (pr->image_width == 0 || pr->image_height == 0) return;
1490 scale_x = rt->hidpi_scale * static_cast<gdouble>(pr->width) / pr->image_width;
1491 scale_y = rt->hidpi_scale * static_cast<gdouble>(pr->height) / pr->image_height;
1493 pr_tile_coords_map_orientation(orientation, it->x, it->y,
1494 pr->width, pr->height,
1495 rt->tile_width, rt->tile_height,
1497 pr_tile_region_map_orientation(orientation, x, y,
1498 rt->tile_width, rt->tile_height,
1503 src_x *= rt->hidpi_scale;
1504 src_y *= rt->hidpi_scale;
1505 pb_x *= rt->hidpi_scale;
1506 pb_y *= rt->hidpi_scale;
1507 pb_w *= rt->hidpi_scale;
1508 pb_h *= rt->hidpi_scale;
1510 switch (orientation)
1512 case EXIF_ORIENTATION_LEFT_TOP:
1513 case EXIF_ORIENTATION_RIGHT_TOP:
1514 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1515 case EXIF_ORIENTATION_LEFT_BOTTOM:
1516 std::swap(scale_x, scale_y);
1523 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
1524 * small sizes for anything but GDK_INTERP_NEAREST
1526 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1527 if (pr->image_width > 32767) wide_image = TRUE;
1529 rt_tile_get_region(has_alpha, pr->ignore_alpha,
1530 pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
1531 static_cast<gdouble>(0.0) - src_x - get_right_pixbuf_offset(rt) * scale_x,
1532 static_cast<gdouble>(0.0) - src_y,
1534 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1535 it->x + pb_x, it->y + pb_y, wide_image);
1536 if (rt->stereo_mode & PR_STEREO_ANAGLYPH &&
1537 (pr->stereo_pixbuf_offset_right > 0 || pr->stereo_pixbuf_offset_left > 0))
1539 GdkPixbuf *right_pb = rt_get_spare_tile(rt);
1540 rt_tile_get_region(has_alpha, pr->ignore_alpha,
1541 pr->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h,
1542 static_cast<gdouble>(0.0) - src_x - get_left_pixbuf_offset(rt) * scale_x,
1543 static_cast<gdouble>(0.0) - src_y,
1545 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1546 it->x + pb_x, it->y + pb_y, wide_image);
1547 pr_create_anaglyph(rt->stereo_mode, it->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h);
1548 /* do not care about freeing spare_tile, it will be reused */
1550 rt_tile_apply_orientation(rt, orientation, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
1554 if (draw && it->pixbuf && !it->blank)
1558 if (pr->func_post_process && (!pr->post_process_slow || !fast))
1559 pr->func_post_process(pr, &it->pixbuf, x, y, w, h, pr->post_process_user_data);
1561 cr = cairo_create(it->surface);
1562 cairo_rectangle (cr, x, y, w, h);
1563 rt_hidpi_aware_draw(rt, cr, it->pixbuf, 0, 0);
1569 static void rt_tile_expose(RendererTiles *rt, ImageTile *it,
1570 gint x, gint y, gint w, gint h,
1571 gboolean new_data, gboolean fast)
1573 PixbufRenderer *pr = rt->pr;
1576 /* clamp to visible */
1577 if (it->x + x < rt->x_scroll)
1579 w -= rt->x_scroll - it->x - x;
1580 x = rt->x_scroll - it->x;
1582 if (it->x + x + w > rt->x_scroll + pr->vis_width)
1584 w = rt->x_scroll + pr->vis_width - it->x - x;
1587 if (it->y + y < rt->y_scroll)
1589 h -= rt->y_scroll - it->y - y;
1590 y = rt->y_scroll - it->y;
1592 if (it->y + y + h > rt->y_scroll + pr->vis_height)
1594 h = rt->y_scroll + pr->vis_height - it->y - y;
1598 rt_tile_render(rt, it, x, y, w, h, new_data, fast);
1600 cr = cairo_create(rt->surface);
1601 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);
1602 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);
1606 if (rt->overlay_list)
1608 rt_overlay_draw(rt, pr->x_offset + (it->x - rt->x_scroll) + x,
1609 pr->y_offset + (it->y - rt->y_scroll) + y,
1614 gtk_widget_queue_draw(GTK_WIDGET(rt->pr));
1618 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it)
1620 PixbufRenderer *pr = rt->pr;
1621 return (it->x + it->w >= rt->x_scroll && it->x < rt->x_scroll + pr->vis_width &&
1622 it->y + it->h >= rt->y_scroll && it->y < rt->y_scroll + pr->vis_height);
1626 *-------------------------------------------------------------------
1628 *-------------------------------------------------------------------
1631 static gint rt_get_queued_area(GList *work)
1637 auto qd = static_cast<QueueData *>(work->data);
1638 area += qd->w * qd->h;
1645 static gboolean rt_queue_schedule_next_draw(RendererTiles *rt, gboolean force_set)
1647 PixbufRenderer *pr = rt->pr;
1649 gint visible_area = pr->vis_width * pr->vis_height;
1654 DEBUG_2("redraw priority: 2pass");
1655 rt->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, rt_queue_draw_idle_cb, rt, nullptr);
1656 return G_SOURCE_REMOVE;
1659 if (visible_area == 0)
1666 percent = 100.0 * rt_get_queued_area(rt->draw_queue) / visible_area;
1671 /* we have enough data for starting intensive redrawing */
1672 DEBUG_2("redraw priority: high %.2f %%", percent);
1673 rt->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW, rt_queue_draw_idle_cb, rt, nullptr);
1674 return G_SOURCE_REMOVE;
1677 if (percent < 1.0 || force_set)
1679 /* queue is (almost) empty, wait 50 ms*/
1680 DEBUG_2("redraw priority: wait %.2f %%", percent);
1681 rt->draw_idle_id = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, 50, rt_queue_draw_idle_cb, rt, nullptr);
1682 return G_SOURCE_REMOVE;
1685 /* keep the same priority as before */
1686 DEBUG_2("redraw priority: no change %.2f %%", percent);
1687 return G_SOURCE_CONTINUE;
1691 static gboolean rt_queue_draw_idle_cb(gpointer data)
1693 auto rt = static_cast<RendererTiles *>(data);
1694 PixbufRenderer *pr = rt->pr;
1699 if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
1700 (!rt->draw_queue && !rt->draw_queue_2pass) ||
1703 pr_render_complete_signal(pr);
1705 rt->draw_idle_id = 0;
1706 return G_SOURCE_REMOVE;
1711 qd = static_cast<QueueData *>(rt->draw_queue->data);
1712 fast = (pr->zoom_2pass && ((pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0) || pr->post_process_slow));
1718 /* still loading, wait till done (also drops the higher priority) */
1720 return rt_queue_schedule_next_draw(rt, FALSE);
1723 qd = static_cast<QueueData *>(rt->draw_queue_2pass->data);
1727 if (gtk_widget_get_realized(GTK_WIDGET(pr)))
1729 if (rt_tile_is_visible(rt, qd->it))
1731 rt_tile_expose(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1733 else if (qd->new_data)
1735 /* if new pixel data, and we already have a pixmap, update the tile */
1736 qd->it->blank = FALSE;
1737 if (qd->it->surface && qd->it->render_done == TILE_RENDER_ALL)
1739 rt_tile_render(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1746 qd->it->qd = nullptr;
1747 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
1752 rt_queue_merge(qd->it->qd2, qd);
1758 rt->draw_queue_2pass = g_list_append(rt->draw_queue_2pass, qd);
1768 qd->it->qd2 = nullptr;
1769 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
1773 if (!rt->draw_queue && !rt->draw_queue_2pass)
1775 pr_render_complete_signal(pr);
1777 rt->draw_idle_id = 0;
1778 return G_SOURCE_REMOVE;
1781 return rt_queue_schedule_next_draw(rt, FALSE);
1784 static void rt_queue_list_free(GList *list)
1793 qd = static_cast<QueueData *>(work->data);
1796 qd->it->qd = nullptr;
1797 qd->it->qd2 = nullptr;
1804 static void rt_queue_clear(RendererTiles *rt)
1806 rt_queue_list_free(rt->draw_queue);
1807 rt->draw_queue = nullptr;
1809 rt_queue_list_free(rt->draw_queue_2pass);
1810 rt->draw_queue_2pass = nullptr;
1812 if (rt->draw_idle_id)
1814 g_source_remove(rt->draw_idle_id);
1815 rt->draw_idle_id = 0;
1820 static void rt_queue_merge(QueueData *parent, QueueData *qd)
1822 if (parent->x + parent->w < qd->x + qd->w)
1824 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
1826 if (parent->x > qd->x)
1828 parent->w += parent->x - qd->x;
1832 if (parent->y + parent->h < qd->y + qd->h)
1834 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
1836 if (parent->y > qd->y)
1838 parent->h += parent->y - qd->y;
1842 parent->new_data |= qd->new_data;
1845 static gboolean rt_clamp_to_visible(RendererTiles *rt, gint *x, gint *y, gint *w, gint *h)
1847 PixbufRenderer *pr = rt->pr;
1858 vh = pr->vis_height;
1863 if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
1866 nx = CLAMP(*x, vx, vx + vw);
1867 nw = CLAMP(*w - (nx - *x), 1, vw);
1869 ny = CLAMP(*y, vy, vy + vh);
1870 nh = CLAMP(*h - (ny - *y), 1, vh);
1880 static gboolean rt_queue_to_tiles(RendererTiles *rt, gint x, gint y, gint w, gint h,
1881 gboolean clamp, ImageRenderType render,
1882 gboolean new_data, gboolean only_existing)
1884 PixbufRenderer *pr = rt->pr;
1892 if (clamp && !rt_clamp_to_visible(rt, &x, &y, &w, &h)) return FALSE;
1894 x1 = ROUND_DOWN(x, rt->tile_width);
1895 x2 = ROUND_UP(x + w, rt->tile_width);
1897 y1 = ROUND_DOWN(y, rt->tile_height);
1898 y2 = ROUND_UP(y + h, rt->tile_height);
1900 for (j = y1; j <= y2; j += rt->tile_height)
1902 for (i = x1; i <= x2; i += rt->tile_width)
1906 it = rt_tile_get(rt, i, j,
1908 (i + rt->tile_width < rt->x_scroll ||
1909 i > rt->x_scroll + pr->vis_width ||
1910 j + rt->tile_height < rt->y_scroll ||
1911 j > rt->y_scroll + pr->vis_height)));
1914 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
1915 (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
1917 it->render_todo = render;
1920 auto qd = g_new(QueueData, 1);
1922 qd->new_data = new_data;
1932 qd->w = x + w - i - qd->x;
1933 if (qd->x + qd->w > rt->tile_width) qd->w = rt->tile_width - qd->x;
1943 qd->h = y + h - j - qd->y;
1944 if (qd->y + qd->h > rt->tile_height) qd->h = rt->tile_height - qd->y;
1946 if (qd->w < 1 || qd->h < 1)
1952 rt_queue_merge(it->qd, qd);
1958 rt->draw_queue = g_list_append(rt->draw_queue, qd);
1967 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
1968 gboolean clamp, ImageRenderType render,
1969 gboolean new_data, gboolean only_existing)
1971 PixbufRenderer *pr = rt->pr;
1977 nx = CLAMP(x, 0, pr->width - 1);
1978 ny = CLAMP(y, 0, pr->height - 1);
1981 w = CLAMP(w, 0, pr->width - nx);
1982 h = CLAMP(h, 0, pr->height - ny);
1983 if (w < 1 || h < 1) return;
1985 if (rt_queue_to_tiles(rt, nx, ny, w, h, clamp, render, new_data, only_existing) &&
1986 ((!rt->draw_queue && !rt->draw_queue_2pass) || !rt->draw_idle_id))
1988 if (rt->draw_idle_id)
1990 g_source_remove(rt->draw_idle_id);
1991 rt->draw_idle_id = 0;
1993 rt_queue_schedule_next_draw(rt, TRUE);
1997 static void rt_scroll(void *renderer, gint x_off, gint y_off)
1999 auto rt = static_cast<RendererTiles *>(renderer);
2000 PixbufRenderer *pr = rt->pr;
2003 if (rt->stereo_mode & PR_STEREO_MIRROR) x_off = -x_off;
2004 if (rt->stereo_mode & PR_STEREO_FLIP) y_off = -y_off;
2006 gint w = pr->vis_width - abs(x_off);
2007 gint h = pr->vis_height - abs(y_off);
2011 /* scrolled completely to new material */
2012 rt_queue(rt, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2021 cairo_surface_t *surface;
2045 cr = cairo_create(rt->surface);
2046 surface = rt->surface;
2048 /* clipping restricts the intermediate surface's size, so it's a good idea
2050 cairo_rectangle(cr, x1 + pr->x_offset + rt->stereo_off_x, y1 + pr->y_offset + rt->stereo_off_y, w, h);
2052 /* Now push a group to change the target */
2053 cairo_push_group (cr);
2054 cairo_set_source_surface(cr, surface, x1 - x2, y1 - y2);
2056 /* Now copy the intermediate target back */
2057 cairo_pop_group_to_source(cr);
2061 rt_overlay_queue_all(rt, x2, y2, x1, y1);
2063 w = pr->vis_width - w;
2064 h = pr->vis_height - h;
2069 x_off > 0 ? rt->x_scroll + (pr->vis_width - w) : rt->x_scroll, rt->y_scroll,
2070 w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2074 /** @FIXME to optimize this, remove overlap */
2076 rt->x_scroll, y_off > 0 ? rt->y_scroll + (pr->vis_height - h) : rt->y_scroll,
2077 pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2081 static void renderer_area_changed(void *renderer, gint src_x, gint src_y, gint src_w, gint src_h)
2083 auto rt = static_cast<RendererTiles *>(renderer);
2084 PixbufRenderer *pr = rt->pr;
2094 gint orientation = rt_get_orientation(rt);
2095 pr_coords_map_orientation_reverse(orientation,
2096 src_x - get_right_pixbuf_offset(rt), src_y,
2097 pr->image_width, pr->image_height,
2102 if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
2104 /* increase region when using a zoom quality that may access surrounding pixels */
2109 x1 = static_cast<gint>(floor(static_cast<gdouble>(x) * pr->scale));
2110 y1 = static_cast<gint>(floor(static_cast<gdouble>(y) * pr->scale * pr->aspect_ratio));
2111 x2 = static_cast<gint>(ceil(static_cast<gdouble>(x + width) * pr->scale));
2112 y2 = static_cast<gint>(ceil(static_cast<gdouble>(y + height) * pr->scale * pr->aspect_ratio));
2114 rt_queue(rt, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
2117 static void renderer_redraw(RendererTiles *rt, gint x, gint y, gint w, gint h,
2118 gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing)
2120 PixbufRenderer *pr = rt->pr;
2122 x -= rt->stereo_off_x;
2123 y -= rt->stereo_off_y;
2125 rt_border_draw(rt, x, y, w, h);
2127 x = MAX(0, x - pr->x_offset + pr->x_scroll);
2128 y = MAX(0, y - pr->y_offset + pr->y_scroll);
2132 MIN(w, pr->width - x),
2133 MIN(h, pr->height - y),
2134 clamp, render, new_data, only_existing);
2137 static void renderer_update_pixbuf(void *renderer, gboolean)
2139 rt_queue_clear(static_cast<RendererTiles *>(renderer));
2142 static void renderer_update_zoom(void *renderer, gboolean lazy)
2144 auto rt = static_cast<RendererTiles *>(renderer);
2145 PixbufRenderer *pr = rt->pr;
2147 rt_tile_invalidate_all(static_cast<RendererTiles *>(renderer));
2150 renderer_redraw(static_cast<RendererTiles *>(renderer), 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, TRUE, FALSE);
2152 rt_border_clear(rt);
2155 static void renderer_invalidate_region(void *renderer, gint x, gint y, gint w, gint h)
2157 rt_tile_invalidate_region(static_cast<RendererTiles *>(renderer), x, y, w, h);
2160 static void renderer_update_viewport(void *renderer)
2162 auto rt = static_cast<RendererTiles *>(renderer);
2164 rt->stereo_off_x = 0;
2165 rt->stereo_off_y = 0;
2167 if (rt->stereo_mode & PR_STEREO_RIGHT)
2169 if (rt->stereo_mode & PR_STEREO_HORIZ)
2171 rt->stereo_off_x = rt->pr->viewport_width;
2173 else if (rt->stereo_mode & PR_STEREO_VERT)
2175 rt->stereo_off_y = rt->pr->viewport_height;
2177 else if (rt->stereo_mode & PR_STEREO_FIXED)
2179 rt->stereo_off_x = rt->pr->stereo_fixed_x_right;
2180 rt->stereo_off_y = rt->pr->stereo_fixed_y_right;
2185 if (rt->stereo_mode & PR_STEREO_FIXED)
2187 rt->stereo_off_x = rt->pr->stereo_fixed_x_left;
2188 rt->stereo_off_y = rt->pr->stereo_fixed_y_left;
2191 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);
2193 rt_overlay_update_sizes(rt);
2194 rt_border_clear(rt);
2197 static void renderer_stereo_set(void *renderer, gint stereo_mode)
2199 auto rt = static_cast<RendererTiles *>(renderer);
2201 rt->stereo_mode = stereo_mode;
2204 static void renderer_free(void *renderer)
2206 auto rt = static_cast<RendererTiles *>(renderer);
2208 rt_tile_free_all(rt);
2209 if (rt->spare_tile) g_object_unref(rt->spare_tile);
2210 if (rt->overlay_buffer) g_object_unref(rt->overlay_buffer);
2211 rt_overlay_list_clear(rt);
2212 /* disconnect "hierarchy-changed" */
2213 g_signal_handlers_disconnect_matched(G_OBJECT(rt->pr), G_SIGNAL_MATCH_DATA,
2214 0, 0, nullptr, nullptr, rt);
2218 static gboolean rt_realize_cb(GtkWidget *widget, gpointer data)
2220 auto rt = static_cast<RendererTiles *>(data);
2225 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));
2227 cr = cairo_create(rt->surface);
2228 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));
2236 static gboolean rt_size_allocate_cb(GtkWidget *widget, GdkRectangle *allocation, gpointer data)
2238 auto rt = static_cast<RendererTiles *>(data);
2240 cairo_surface_t *old_surface;
2242 if (gtk_widget_get_realized(GTK_WIDGET(rt->pr)))
2244 old_surface = rt->surface;
2245 rt->surface = gdk_window_create_similar_surface(gtk_widget_get_window(widget), CAIRO_CONTENT_COLOR, allocation->width, allocation->height);
2247 cr = cairo_create(rt->surface);
2249 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));
2251 cairo_set_source_surface(cr, old_surface, 0, 0);
2254 cairo_surface_destroy(old_surface);
2256 renderer_redraw(rt, allocation->x, allocation->y, allocation->width, allocation->height, FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2262 static gboolean rt_draw_cb(GtkWidget *, cairo_t *cr, gpointer data)
2264 auto rt = static_cast<RendererTiles *>(data);
2268 if (rt->stereo_mode & (PR_STEREO_HORIZ | PR_STEREO_VERT))
2270 cairo_push_group(cr);
2271 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));
2273 if (rt->stereo_mode & PR_STEREO_HORIZ)
2275 cairo_rectangle(cr, rt->stereo_off_x, 0, rt->pr->viewport_width, rt->pr->viewport_height);
2279 cairo_rectangle(cr, 0, rt->stereo_off_y, rt->pr->viewport_width, rt->pr->viewport_height);
2284 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);
2286 cairo_set_source_surface(cr, rt->surface, 0, 0);
2289 cairo_pop_group_to_source(cr);
2294 cairo_set_source_surface(cr, rt->surface, 0, 0);
2298 work = rt->overlay_list;
2301 od = static_cast<OverlayData *>(work->data);
2306 pw = gdk_pixbuf_get_width(od->pixbuf);
2307 ph = gdk_pixbuf_get_height(od->pixbuf);
2311 if (od->flags & OVL_RELATIVE)
2313 if (px < 0) px = rt->pr->viewport_width - pw + px;
2314 if (py < 0) py = rt->pr->viewport_height - ph + py;
2317 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px, py);
2325 RendererFuncs *renderer_tiles_new(PixbufRenderer *pr)
2327 auto rt = g_new0(RendererTiles, 1);
2331 rt->f.area_changed = renderer_area_changed;
2332 rt->f.update_pixbuf = renderer_update_pixbuf;
2333 rt->f.free = renderer_free;
2334 rt->f.update_zoom = renderer_update_zoom;
2335 rt->f.invalidate_region = renderer_invalidate_region;
2336 rt->f.scroll = rt_scroll;
2337 rt->f.update_viewport = renderer_update_viewport;
2340 rt->f.overlay_add = renderer_tiles_overlay_add;
2341 rt->f.overlay_set = renderer_tiles_overlay_set;
2342 rt->f.overlay_get = renderer_tiles_overlay_get;
2344 rt->f.stereo_set = renderer_stereo_set;
2346 rt->tile_width = options->image.tile_size;
2347 rt->tile_height = options->image.tile_size;
2349 rt->tiles = nullptr;
2350 rt->tile_cache_size = 0;
2352 rt->tile_cache_max = PR_CACHE_SIZE_DEFAULT;
2354 rt->draw_idle_id = 0;
2356 rt->stereo_mode = 0;
2357 rt->stereo_off_x = 0;
2358 rt->stereo_off_y = 0;
2360 rt->hidpi_scale = gtk_widget_get_scale_factor(GTK_WIDGET(rt->pr));
2362 g_signal_connect(G_OBJECT(pr), "hierarchy-changed",
2363 G_CALLBACK(rt_hierarchy_changed_cb), rt);
2365 g_signal_connect(G_OBJECT(pr), "draw",
2366 G_CALLBACK(rt_draw_cb), rt);
2367 g_signal_connect(G_OBJECT(pr), "realize", G_CALLBACK(rt_realize_cb), rt);
2368 g_signal_connect(G_OBJECT(pr), "size-allocate", G_CALLBACK(rt_size_allocate_cb), rt);
2370 return reinterpret_cast<RendererFuncs *>(rt);
2374 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */