2 * Copyright (C) 2006 John Ellis
3 * Copyright (C) 2008 - 2021 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 #include "renderer-tiles.h"
29 /* comment this out if not using this from within Geeqie
30 * defining GQ_BUILD does these things:
31 * - Sets the shift-click scroller pixbuf to a nice icon instead of a black box
37 #include "pixbuf-util.h"
40 enum ExifOrientationType {
41 EXIF_ORIENTATION_UNKNOWN = 0,
42 EXIF_ORIENTATION_TOP_LEFT = 1,
43 EXIF_ORIENTATION_TOP_RIGHT = 2,
44 EXIF_ORIENTATION_BOTTOM_RIGHT = 3,
45 EXIF_ORIENTATION_BOTTOM_LEFT = 4,
46 EXIF_ORIENTATION_LEFT_TOP = 5,
47 EXIF_ORIENTATION_RIGHT_TOP = 6,
48 EXIF_ORIENTATION_RIGHT_BOTTOM = 7,
49 EXIF_ORIENTATION_LEFT_BOTTOM = 8
57 cairo_surface_t *surface; /* off screen buffer */
58 GdkPixbuf *pixbuf; /* pixbuf area for zooming */
59 gint x; /* x offset into image */
60 gint y; /* y offset into image */
61 gint w; /* width that is visible (may be less if at edge of image) */
62 gint h; /* height '' */
66 /* render_todo: (explanation)
68 AREA render area of tile, usually only used when loading an image
69 note: will jump to an ALL if render_done is not ALL.
70 ALL render entire tile, if never done before w/ ALL, for expose events *only*
73 ImageRenderType render_todo; /* what to do (see above) */
74 ImageRenderType render_done; /* highest that has been done before on tile */
79 guint size; /* est. memory used by pixmap and pixbuf */
102 OverlayRendererFlags flags;
110 gint tile_cache_max; /* max MiB to use for offscreen buffer */
114 gint tile_cols; /* count of tile columns */
115 GList *tiles; /* list of buffer tiles */
116 gint tile_cache_size; /* allocated size of pixmaps/pixbufs */
117 GList *draw_queue; /* list of areas to redraw */
118 GList *draw_queue_2pass;/* list when 2 pass is enabled */
121 cairo_surface_t *overlay_buffer;
122 cairo_surface_t *surface;
124 guint draw_idle_id; /* event source id */
126 GdkPixbuf *spare_tile;
132 gint x_scroll; /* allow local adjustment and mirroring */
140 static void rt_border_draw(RendererTiles *rt, gint x, gint y, gint w, gint h);
141 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h, ImageTile *it);
144 static void rt_tile_free_all(RendererTiles *rt);
145 static void rt_tile_invalidate_region(RendererTiles *rt, gint x, gint y, gint w, gint h);
146 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it);
147 static void rt_queue_clear(RendererTiles *rt);
148 static void rt_queue_merge(QueueData *parent, QueueData *qd);
149 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
150 gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing);
152 static void rt_hierarchy_changed_cb(GtkWidget *widget, GtkWidget *previous_toplevel, gpointer data);
153 static gint rt_queue_draw_idle_cb(gpointer data);
155 #define GET_RIGHT_PIXBUF_OFFSET(rt) \
156 (( ((rt)->stereo_mode & PR_STEREO_RIGHT) && !((rt)->stereo_mode & PR_STEREO_SWAP)) || \
157 (!((rt)->stereo_mode & PR_STEREO_RIGHT) && ((rt)->stereo_mode & PR_STEREO_SWAP)) ? \
158 (rt)->pr->stereo_pixbuf_offset_right : (rt)->pr->stereo_pixbuf_offset_left )
160 #define GET_LEFT_PIXBUF_OFFSET(rt) \
161 ((!((rt)->stereo_mode & PR_STEREO_RIGHT) && !((rt)->stereo_mode & PR_STEREO_SWAP)) || \
162 ( ((rt)->stereo_mode & PR_STEREO_RIGHT) && ((rt)->stereo_mode & PR_STEREO_SWAP)) ? \
163 (rt)->pr->stereo_pixbuf_offset_right : (rt)->pr->stereo_pixbuf_offset_left )
166 static void rt_sync_scroll(RendererTiles *rt)
168 PixbufRenderer *pr = rt->pr;
170 rt->x_scroll = (rt->stereo_mode & PR_STEREO_MIRROR) ?
171 pr->width - pr->vis_width - pr->x_scroll
174 rt->y_scroll = (rt->stereo_mode & PR_STEREO_FLIP) ?
175 pr->height - pr->vis_height - pr->y_scroll
180 *-------------------------------------------------------------------
182 *-------------------------------------------------------------------
185 static void rt_border_draw(RendererTiles *rt, gint x, gint y, gint w, gint h)
187 PixbufRenderer *pr = rt->pr;
196 box = GTK_WIDGET(pr);
197 window = gtk_widget_get_window(box);
201 cr = cairo_create(rt->surface);
203 if (!pr->pixbuf && !pr->source_tiles_enabled)
205 if (pr_clip_region(x, y, w, h,
207 pr->viewport_width, pr->viewport_height,
210 cairo_set_source_rgb(cr, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
211 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
213 rt_overlay_draw(rt, rx, ry, rw, rh, nullptr);
219 if (pr->vis_width < pr->viewport_width)
221 if (pr->x_offset > 0 &&
222 pr_clip_region(x, y, w, h,
224 pr->x_offset, pr->viewport_height,
227 cairo_set_source_rgb(cr, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
228 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
230 rt_overlay_draw(rt, rx, ry, rw, rh, nullptr);
232 if (pr->viewport_width - pr->vis_width - pr->x_offset > 0 &&
233 pr_clip_region(x, y, w, h,
234 pr->x_offset + pr->vis_width, 0,
235 pr->viewport_width - pr->vis_width - pr->x_offset, pr->viewport_height,
238 cairo_set_source_rgb(cr, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
239 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
241 rt_overlay_draw(rt, rx, ry, rw, rh, nullptr);
244 if (pr->vis_height < pr->viewport_height)
246 if (pr->y_offset > 0 &&
247 pr_clip_region(x, y, w, h,
249 pr->vis_width, pr->y_offset,
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);
257 if (pr->viewport_height - pr->vis_height - pr->y_offset > 0 &&
258 pr_clip_region(x, y, w, h,
259 pr->x_offset, pr->y_offset + pr->vis_height,
260 pr->vis_width, pr->viewport_height - pr->vis_height - pr->y_offset,
263 cairo_set_source_rgb(cr, static_cast<double>(pr->color.red), static_cast<double>(pr->color.green), static_cast<double>(pr->color.blue));
264 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
266 rt_overlay_draw(rt, rx, ry, rw, rh, nullptr);
272 static void rt_border_clear(RendererTiles *rt)
274 PixbufRenderer *pr = rt->pr;
275 rt_border_draw(rt, 0, 0, pr->viewport_width, pr->viewport_height);
280 *-------------------------------------------------------------------
282 *-------------------------------------------------------------------
285 static ImageTile *rt_tile_new(gint x, gint y, gint width, gint height)
287 auto it = g_new0(ImageTile, 1);
294 it->render_done = TILE_RENDER_NONE;
299 static void rt_tile_free(ImageTile *it)
303 if (it->pixbuf) g_object_unref(it->pixbuf);
304 if (it->surface) cairo_surface_destroy(it->surface);
309 static void rt_tile_free_all(RendererTiles *rt)
311 g_list_free_full(rt->tiles, reinterpret_cast<GDestroyNotify>(rt_tile_free));
313 rt->tile_cache_size = 0;
316 static ImageTile *rt_tile_add(RendererTiles *rt, gint x, gint y)
318 PixbufRenderer *pr = rt->pr;
321 it = rt_tile_new(x, y, rt->tile_width, rt->tile_height);
323 if (it->x + it->w > pr->width) it->w = pr->width - it->x;
324 if (it->y + it->h > pr->height) it->h = pr->height - it->y;
326 rt->tiles = g_list_prepend(rt->tiles, it);
327 rt->tile_cache_size += it->size;
332 static void rt_tile_remove(RendererTiles *rt, ImageTile *it)
336 QueueData *qd = it->qd;
339 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
345 QueueData *qd = it->qd2;
348 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
352 rt->tiles = g_list_remove(rt->tiles, it);
353 rt->tile_cache_size -= it->size;
358 static void rt_tile_free_space(RendererTiles *rt, guint space, ImageTile *it)
360 PixbufRenderer *pr = rt->pr;
364 work = g_list_last(rt->tiles);
366 if (pr->source_tiles_enabled && pr->scale < 1.0)
370 tiles = (pr->vis_width / rt->tile_width + 1) * (pr->vis_height / rt->tile_height + 1);
371 tile_max = MAX(tiles * rt->tile_width * rt->tile_height * 3,
372 (gint)((gdouble)rt->tile_cache_max * 1048576.0 * pr->scale));
376 tile_max = rt->tile_cache_max * 1048576;
379 while (work && rt->tile_cache_size + space > tile_max)
383 needle = static_cast<ImageTile *>(work->data);
386 ((!needle->qd && !needle->qd2) || !rt_tile_is_visible(rt, needle))) rt_tile_remove(rt, needle);
390 static void rt_tile_invalidate_all(RendererTiles *rt)
392 PixbufRenderer *pr = rt->pr;
400 it = static_cast<ImageTile *>(work->data);
403 it->render_done = TILE_RENDER_NONE;
404 it->render_todo = TILE_RENDER_ALL;
407 it->w = MIN(rt->tile_width, pr->width - it->x);
408 it->h = MIN(rt->tile_height, pr->height - it->y);
412 static void rt_tile_invalidate_region(RendererTiles *rt, gint x, gint y, gint w, gint h)
420 x1 = ROUND_DOWN(x, rt->tile_width);
421 x2 = ROUND_UP(x + w, rt->tile_width);
423 y1 = ROUND_DOWN(y, rt->tile_height);
424 y2 = ROUND_UP(y + h, rt->tile_height);
431 it = static_cast<ImageTile *>(work->data);
434 if (it->x < x2 && it->x + it->w > x1 &&
435 it->y < y2 && it->y + it->h > y1)
437 it->render_done = TILE_RENDER_NONE;
438 it->render_todo = TILE_RENDER_ALL;
443 static ImageTile *rt_tile_get(RendererTiles *rt, gint x, gint y, gboolean only_existing)
452 it = static_cast<ImageTile *>(work->data);
453 if (it->x == x && it->y == y)
455 rt->tiles = g_list_delete_link(rt->tiles, work);
456 rt->tiles = g_list_prepend(rt->tiles, it);
463 if (only_existing) return nullptr;
465 return rt_tile_add(rt, x, y);
468 static gint pixmap_calc_size(cairo_surface_t *)
470 return options->image.tile_size * options->image.tile_size * 4 / 8;
473 static void rt_hidpi_aware_draw(
480 cairo_surface_t *surface;
481 surface = gdk_cairo_surface_create_from_pixbuf(pixbuf, rt->hidpi_scale, nullptr);
482 cairo_set_source_surface(cr, surface, x, y);
484 cairo_surface_destroy(surface);
487 static void rt_tile_prepare(RendererTiles *rt, ImageTile *it)
489 PixbufRenderer *pr = rt->pr;
492 cairo_surface_t *surface;
495 surface = gdk_window_create_similar_surface(gtk_widget_get_window(reinterpret_cast<GtkWidget *>(pr)),
497 rt->tile_width, rt->tile_height);
499 size = pixmap_calc_size(surface) * rt->hidpi_scale * rt->hidpi_scale;
500 rt_tile_free_space(rt, size, it);
502 it->surface = surface;
504 rt->tile_cache_size += size;
511 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, rt->hidpi_scale * rt->tile_width, rt->hidpi_scale * rt->tile_height);
513 size = gdk_pixbuf_get_rowstride(pixbuf) * rt->tile_height * rt->hidpi_scale;
514 rt_tile_free_space(rt, size, it);
518 rt->tile_cache_size += size;
523 *-------------------------------------------------------------------
525 *-------------------------------------------------------------------
528 static void rt_overlay_get_position(RendererTiles *rt, OverlayData *od,
529 gint *x, gint *y, gint *w, gint *h)
531 PixbufRenderer *pr = rt->pr;
537 pw = gdk_pixbuf_get_width(od->pixbuf);
538 ph = gdk_pixbuf_get_height(od->pixbuf);
542 if (od->flags & OVL_RELATIVE)
544 if (px < 0) px = pr->viewport_width - pw + px;
545 if (py < 0) py = pr->viewport_height - ph + py;
554 static void rt_overlay_init_window(RendererTiles *rt, OverlayData *od)
556 PixbufRenderer *pr = rt->pr;
561 GdkWindowAttr attributes;
562 gint attributes_mask;
564 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
566 attributes.window_type = GDK_WINDOW_CHILD;
567 attributes.wclass = GDK_INPUT_OUTPUT;
568 attributes.width = pw;
569 attributes.height = ph;
570 attributes.event_mask = GDK_EXPOSURE_MASK;
573 od->window = gdk_window_new(gtk_widget_get_window(GTK_WIDGET(pr)), &attributes, attributes_mask);
574 gdk_window_set_user_data(od->window, pr);
575 gdk_window_move(od->window, px + rt->stereo_off_x, py + rt->stereo_off_y);
576 gdk_window_show(od->window);
579 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h,
582 PixbufRenderer *pr = rt->pr;
585 work = rt->overlay_list;
598 od = static_cast<OverlayData *>(work->data);
601 if (!od->window) rt_overlay_init_window(rt, od);
603 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
604 if (pr_clip_region(x, y, w, h, px, py, pw, ph, &rx, &ry, &rw, &rh))
606 if (!rt->overlay_buffer)
608 rt->overlay_buffer = gdk_window_create_similar_surface(gtk_widget_get_window(reinterpret_cast<GtkWidget *>(pr)),
610 rt->tile_width, rt->tile_height);
617 cr = cairo_create(rt->overlay_buffer);
618 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);
619 cairo_rectangle(cr, 0, 0, rw, rh);
620 cairo_fill_preserve(cr);
622 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - rx, py - ry);
626 cr = gdk_cairo_create(od->window);
627 cairo_set_source_surface(cr, rt->overlay_buffer, rx - px, ry - py);
628 cairo_rectangle (cr, rx - px, ry - py, rw, rh);
634 /* no ImageTile means region may be larger than our scratch buffer */
638 for (sx = rx; sx < rx + rw; sx += rt->tile_width)
639 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;
676 rt_overlay_get_position(rt, od, &x, &y, &w, &h);
684 rt_queue(rt, rt->x_scroll - pr->x_offset + x,
685 rt->y_scroll - pr->y_offset + y,
687 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
689 rt_border_draw(rt, x, y, w, h);
692 static void rt_overlay_queue_all(RendererTiles *rt, gint x1, gint y1, gint x2, gint y2)
696 work = rt->overlay_list;
699 auto od = static_cast<OverlayData *>(work->data);
702 rt_overlay_queue_draw(rt, od, x1, y1, x2, y2);
706 static void rt_overlay_update_sizes(RendererTiles *rt)
710 work = rt->overlay_list;
713 auto od = static_cast<OverlayData *>(work->data);
716 if (!od->window) rt_overlay_init_window(rt, od);
718 if (od->flags & OVL_RELATIVE)
725 rt_overlay_get_position(rt, od, &x, &y, &w, &h);
726 gdk_window_move_resize(od->window, x + rt->stereo_off_x, y + rt->stereo_off_y, w, h);
731 static OverlayData *rt_overlay_find(RendererTiles *rt, gint id)
735 work = rt->overlay_list;
738 auto od = static_cast<OverlayData *>(work->data);
741 if (od->id == id) return od;
748 gint renderer_tiles_overlay_add(void *renderer, GdkPixbuf *pixbuf, gint x, gint y,
749 OverlayRendererFlags flags)
751 auto rt = static_cast<RendererTiles *>(renderer);
752 PixbufRenderer *pr = rt->pr;
755 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
756 g_return_val_if_fail(pixbuf != nullptr, -1);
759 while (rt_overlay_find(rt, id)) id++;
761 auto od = g_new0(OverlayData, 1);
764 g_object_ref(G_OBJECT(od->pixbuf));
769 rt_overlay_init_window(rt, od);
771 rt->overlay_list = g_list_append(rt->overlay_list, od);
773 gtk_widget_queue_draw(GTK_WIDGET(rt->pr));
778 static void rt_overlay_free(RendererTiles *rt, OverlayData *od)
780 rt->overlay_list = g_list_remove(rt->overlay_list, od);
782 if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
783 if (od->window) gdk_window_destroy(od->window);
786 if (!rt->overlay_list && rt->overlay_buffer)
788 cairo_surface_destroy(rt->overlay_buffer);
789 rt->overlay_buffer = nullptr;
793 static void rt_overlay_list_clear(RendererTiles *rt)
795 while (rt->overlay_list)
797 auto od = static_cast<OverlayData *>(rt->overlay_list->data);
798 rt_overlay_free(rt, od);
802 static void rt_overlay_list_reset_window(RendererTiles *rt)
806 if (rt->overlay_buffer) cairo_surface_destroy(rt->overlay_buffer);
807 rt->overlay_buffer = nullptr;
809 work = rt->overlay_list;
812 auto od = static_cast<OverlayData *>(work->data);
814 if (od->window) gdk_window_destroy(od->window);
815 od->window = nullptr;
819 void renderer_tiles_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint, gint)
821 auto rc = static_cast<RendererTiles *>(renderer);
822 PixbufRenderer *pr = rc->pr;
825 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
827 od = rt_overlay_find(rc, id);
832 g_object_ref(G_OBJECT(pixbuf));
833 g_object_unref(G_OBJECT(od->pixbuf));
838 rt_overlay_free(rc, od);
841 gtk_widget_queue_draw(GTK_WIDGET(rc->pr));
844 gboolean renderer_tiles_overlay_get(void *renderer, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
846 auto rt = static_cast<RendererTiles *>(renderer);
847 PixbufRenderer *pr = rt->pr;
850 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
852 od = rt_overlay_find(rt, id);
853 if (!od) return FALSE;
855 if (pixbuf) *pixbuf = od->pixbuf;
862 static void rt_hierarchy_changed_cb(GtkWidget *, GtkWidget *, gpointer data)
864 auto rt = static_cast<RendererTiles *>(data);
865 rt_overlay_list_reset_window(rt);
869 *-------------------------------------------------------------------
871 *-------------------------------------------------------------------
874 static GdkPixbuf *rt_get_spare_tile(RendererTiles *rt)
876 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);
877 return rt->spare_tile;
881 COLOR_BYTES = 3 /* rgb */
884 static void rt_tile_rotate_90_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
886 GdkPixbuf *src = *tile;
899 gint tw = rt->tile_width * rt->hidpi_scale;
901 srs = gdk_pixbuf_get_rowstride(src);
902 s_pix = gdk_pixbuf_get_pixels(src);
903 spi = s_pix + (x * COLOR_BYTES);
905 dest = rt_get_spare_tile(rt);
906 drs = gdk_pixbuf_get_rowstride(dest);
907 d_pix = gdk_pixbuf_get_pixels(dest);
908 dpi = d_pix + (tw - 1) * COLOR_BYTES;
910 for (i = y; i < y + h; i++)
912 sp = spi + (i * srs);
913 ip = dpi - (i * COLOR_BYTES);
914 for (j = x; j < x + w; j++)
917 memcpy(dp, sp, COLOR_BYTES);
922 rt->spare_tile = src;
926 static void rt_tile_rotate_90_counter_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
928 GdkPixbuf *src = *tile;
941 gint th = rt->tile_height * rt->hidpi_scale;
943 srs = gdk_pixbuf_get_rowstride(src);
944 s_pix = gdk_pixbuf_get_pixels(src);
945 spi = s_pix + (x * COLOR_BYTES);
947 dest = rt_get_spare_tile(rt);
948 drs = gdk_pixbuf_get_rowstride(dest);
949 d_pix = gdk_pixbuf_get_pixels(dest);
950 dpi = d_pix + (th - 1) * drs;
952 for (i = y; i < y + h; i++)
954 sp = spi + (i * srs);
955 ip = dpi + (i * COLOR_BYTES);
956 for (j = x; j < x + w; j++)
959 memcpy(dp, sp, COLOR_BYTES);
964 rt->spare_tile = src;
968 static void rt_tile_mirror_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
970 GdkPixbuf *src = *tile;
983 gint tw = rt->tile_width * rt->hidpi_scale;
985 srs = gdk_pixbuf_get_rowstride(src);
986 s_pix = gdk_pixbuf_get_pixels(src);
987 spi = s_pix + (x * COLOR_BYTES);
989 dest = rt_get_spare_tile(rt);
990 drs = gdk_pixbuf_get_rowstride(dest);
991 d_pix = gdk_pixbuf_get_pixels(dest);
992 dpi = d_pix + (tw - x - 1) * COLOR_BYTES;
994 for (i = y; i < y + h; i++)
996 sp = spi + (i * srs);
997 dp = dpi + (i * drs);
998 for (j = 0; j < w; j++)
1000 memcpy(dp, sp, COLOR_BYTES);
1006 rt->spare_tile = src;
1010 static void rt_tile_mirror_and_flip(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1012 GdkPixbuf *src = *tile;
1023 gint tw = rt->tile_width * rt->hidpi_scale;
1024 gint th = rt->tile_height * rt->hidpi_scale;
1026 srs = gdk_pixbuf_get_rowstride(src);
1027 s_pix = gdk_pixbuf_get_pixels(src);
1029 dest = rt_get_spare_tile(rt);
1030 drs = gdk_pixbuf_get_rowstride(dest);
1031 d_pix = gdk_pixbuf_get_pixels(dest);
1032 dpi = d_pix + (th - 1) * drs + (tw - 1) * COLOR_BYTES;
1034 for (i = y; i < y + h; i++)
1036 sp = s_pix + (i * srs) + (x * COLOR_BYTES);
1037 dp = dpi - (i * drs) - (x * COLOR_BYTES);
1038 for (j = 0; j < w; j++)
1040 memcpy(dp, sp, COLOR_BYTES);
1046 rt->spare_tile = src;
1050 static void rt_tile_flip_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1052 GdkPixbuf *src = *tile;
1063 gint th = rt->tile_height * rt->hidpi_scale;
1065 srs = gdk_pixbuf_get_rowstride(src);
1066 s_pix = gdk_pixbuf_get_pixels(src);
1067 spi = s_pix + (x * COLOR_BYTES);
1069 dest = rt_get_spare_tile(rt);
1070 drs = gdk_pixbuf_get_rowstride(dest);
1071 d_pix = gdk_pixbuf_get_pixels(dest);
1072 dpi = d_pix + (th - 1) * drs + (x * COLOR_BYTES);
1074 for (i = y; i < y + h; i++)
1076 sp = spi + (i * srs);
1077 dp = dpi - (i * drs);
1078 memcpy(dp, sp, w * COLOR_BYTES);
1081 rt->spare_tile = src;
1085 static void rt_tile_apply_orientation(RendererTiles *rt, gint orientation, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h)
1087 switch (orientation)
1089 case EXIF_ORIENTATION_TOP_LEFT:
1090 /* normal -- nothing to do */
1092 case EXIF_ORIENTATION_TOP_RIGHT:
1095 rt_tile_mirror_only(rt, pixbuf, x, y, w, h);
1098 case EXIF_ORIENTATION_BOTTOM_RIGHT:
1101 rt_tile_mirror_and_flip(rt, pixbuf, x, y, w, h);
1104 case EXIF_ORIENTATION_BOTTOM_LEFT:
1107 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1110 case EXIF_ORIENTATION_LEFT_TOP:
1112 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1113 rt_tile_rotate_90_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1116 case EXIF_ORIENTATION_RIGHT_TOP:
1117 /* rotated -90 (270) */
1119 rt_tile_rotate_90_clockwise(rt, pixbuf, x, y, w, h);
1122 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1124 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1125 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1128 case EXIF_ORIENTATION_LEFT_BOTTOM:
1131 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, y, w, h);
1135 /* The other values are out of range */
1140 static gboolean rt_source_tile_render(RendererTiles *rt, ImageTile *it,
1141 gint x, gint y, gint w, gint h,
1142 gboolean, gboolean fast)
1144 PixbufRenderer *pr = rt->pr;
1147 gboolean draw = FALSE;
1149 if (pr->zoom == 1.0 || pr->scale == 1.0)
1151 list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE);
1161 st = static_cast<SourceTile *>(work->data);
1164 if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1165 it->x + x, it->y + y, w, h,
1166 &rx, &ry, &rw, &rh))
1169 cr = cairo_create(it->surface);
1170 cairo_rectangle (cr, rx - it->x, ry - it->y, rw, rh);
1174 cairo_set_source_rgb(cr, 0, 0, 0);
1177 else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
1179 rt_hidpi_aware_draw(rt, cr, st->pixbuf, -it->x + st->x, -it->y + st->y);
1194 if (pr->image_width == 0 || pr->image_height == 0) return FALSE;
1195 scale_x = static_cast<gdouble>(pr->width) / pr->image_width;
1196 scale_y = static_cast<gdouble>(pr->height) / pr->image_height;
1198 sx = static_cast<gdouble>(it->x + x) / scale_x;
1199 sy = static_cast<gdouble>(it->y + y) / scale_y;
1200 sw = static_cast<gdouble>(w) / scale_x;
1201 sh = static_cast<gdouble>(h) / scale_y;
1203 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1206 /* draws red over draw region, to check for leaks (regions not filled) */
1207 pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255);
1210 list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE);
1224 st = static_cast<SourceTile *>(work->data);
1227 stx = floor(static_cast<gdouble>(st->x) * scale_x);
1228 sty = floor(static_cast<gdouble>(st->y) * scale_y);
1229 stw = ceil(static_cast<gdouble>(st->x + pr->source_tile_width) * scale_x) - stx;
1230 sth = ceil(static_cast<gdouble>(st->y + pr->source_tile_height) * scale_y) - sty;
1232 if (pr_clip_region(stx, sty, stw, sth,
1233 it->x + x, it->y + y, w, h,
1234 &rx, &ry, &rw, &rh))
1240 cr = cairo_create(it->surface);
1241 cairo_rectangle (cr, rx - st->x, ry - st->y, rw, rh);
1242 cairo_set_source_rgb(cr, 0, 0, 0);
1251 /* may need to use unfloored stx,sty values here */
1252 offset_x = static_cast<gdouble>(stx - it->x);
1253 offset_y = static_cast<gdouble>(sty - it->y);
1255 gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh,
1256 static_cast<gdouble>(0.0) + offset_x,
1257 static_cast<gdouble>(0.0) + offset_y,
1259 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1274 * @param ignore_alpha
1285 * @param interp_type
1288 * @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
1292 static void rt_tile_get_region(gboolean has_alpha, gboolean ignore_alpha,
1293 const GdkPixbuf *src, GdkPixbuf *dest,
1294 int pb_x, int pb_y, int pb_w, int pb_h,
1295 double offset_x, double offset_y, double scale_x, double scale_y,
1296 GdkInterpType interp_type,
1297 int check_x, int check_y, gboolean wide_image)
1299 GdkPixbuf* tmppixbuf;
1318 if (scale_x == 1.0 && scale_y == 1.0)
1322 srs = gdk_pixbuf_get_rowstride(src);
1323 drs = gdk_pixbuf_get_rowstride(dest);
1324 psrc = gdk_pixbuf_get_pixels(src);
1325 pdst = gdk_pixbuf_get_pixels(dest);
1326 for (y = 0; y < pb_h; y++)
1328 for (x = 0; x < pb_w; x++)
1330 for (c = 0; c < 3; c++)
1332 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];
1339 gdk_pixbuf_copy_area(src,
1340 -offset_x + pb_x, -offset_y + pb_y,
1348 gdk_pixbuf_scale(src, dest,
1349 pb_x, pb_y, pb_w, pb_h,
1353 (wide_image && interp_type == GDK_INTERP_NEAREST) ? GDK_INTERP_TILES : interp_type);
1358 red_1=static_cast<guint32>(options->image.alpha_color_1.red * 255) << 16 & 0x00FF0000;
1359 green_1=static_cast<guint32>(options->image.alpha_color_1.green * 255) << 8 & 0x0000FF00;
1360 blue_1=static_cast<guint32>(options->image.alpha_color_1.blue * 255) & 0x000000FF;
1361 red_2=static_cast<guint32>(options->image.alpha_color_2.red * 255) << 16 & 0x00FF0000;
1362 green_2=static_cast<guint32>(options->image.alpha_color_2.green * 255) << 8 & 0x0000FF00;
1363 blue_2=static_cast<guint32>(options->image.alpha_color_2.blue * 255) & 0x000000FF;
1364 alpha_1 = red_1 + green_1 + blue_1;
1365 alpha_2 = red_2 + green_2 + blue_2;
1369 tmppixbuf = gdk_pixbuf_add_alpha(src, FALSE, 0, 0, 0);
1371 pixbuf_ignore_alpha_rect(tmppixbuf, 0, 0, gdk_pixbuf_get_width(src), gdk_pixbuf_get_height(src));
1373 gdk_pixbuf_composite_color(tmppixbuf, dest,
1374 pb_x, pb_y, pb_w, pb_h,
1378 (scale_x == 1.0 && scale_y == 1.0) ? GDK_INTERP_NEAREST : interp_type,
1379 255, check_x, check_y,
1380 PR_ALPHA_CHECK_SIZE,
1383 g_object_unref(tmppixbuf);
1387 gdk_pixbuf_composite_color(src, dest,
1388 pb_x, pb_y, pb_w, pb_h,
1392 (scale_x == 1.0 && scale_y == 1.0) ? GDK_INTERP_NEAREST : interp_type,
1393 255, check_x, check_y,
1394 PR_ALPHA_CHECK_SIZE,
1402 static gint rt_get_orientation(RendererTiles *rt)
1404 PixbufRenderer *pr = rt->pr;
1406 gint orientation = pr->orientation;
1407 static const gint mirror[] = {1, 2, 1, 4, 3, 6, 5, 8, 7};
1408 static const gint flip[] = {1, 4, 3, 2, 1, 8, 7, 6, 5};
1410 if (rt->stereo_mode & PR_STEREO_MIRROR) orientation = mirror[orientation];
1411 if (rt->stereo_mode & PR_STEREO_FLIP) orientation = flip[orientation];
1416 static void rt_tile_render(RendererTiles *rt, ImageTile *it,
1417 gint x, gint y, gint w, gint h,
1418 gboolean new_data, gboolean fast)
1420 PixbufRenderer *pr = rt->pr;
1422 gboolean draw = FALSE;
1423 gint orientation = rt_get_orientation(rt);
1424 gboolean wide_image = FALSE;
1426 if (it->render_todo == TILE_RENDER_NONE && it->surface && !new_data) return;
1428 if (it->render_done != TILE_RENDER_ALL)
1434 if (!fast) it->render_done = TILE_RENDER_ALL;
1436 else if (it->render_todo != TILE_RENDER_AREA)
1438 if (!fast) it->render_todo = TILE_RENDER_NONE;
1442 if (!fast) it->render_todo = TILE_RENDER_NONE;
1444 if (new_data) it->blank = FALSE;
1446 rt_tile_prepare(rt, it);
1447 has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
1449 /** @FIXME checker colors for alpha should be configurable,
1450 * also should be drawn for blank = TRUE
1455 /* no data, do fast rect fill */
1457 cr = cairo_create(it->surface);
1458 cairo_rectangle (cr, 0, 0, it->w, it->h);
1459 cairo_set_source_rgb(cr, 0, 0, 0);
1463 else if (pr->source_tiles_enabled)
1465 draw = rt_source_tile_render(rt, it, x, y, w, h, new_data, fast);
1478 if (pr->image_width == 0 || pr->image_height == 0) return;
1480 scale_x = rt->hidpi_scale * static_cast<gdouble>(pr->width) / pr->image_width;
1481 scale_y = rt->hidpi_scale * static_cast<gdouble>(pr->height) / pr->image_height;
1483 pr_tile_coords_map_orientation(orientation, it->x, it->y,
1484 pr->width, pr->height,
1485 rt->tile_width, rt->tile_height,
1487 pr_tile_region_map_orientation(orientation, x, y,
1488 rt->tile_width, rt->tile_height,
1493 src_x *= rt->hidpi_scale;
1494 src_y *= rt->hidpi_scale;
1495 pb_x *= rt->hidpi_scale;
1496 pb_y *= rt->hidpi_scale;
1497 pb_w *= rt->hidpi_scale;
1498 pb_h *= rt->hidpi_scale;
1500 switch (orientation)
1503 case EXIF_ORIENTATION_LEFT_TOP:
1504 case EXIF_ORIENTATION_RIGHT_TOP:
1505 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1506 case EXIF_ORIENTATION_LEFT_BOTTOM:
1516 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
1517 * small sizes for anything but GDK_INTERP_NEAREST
1519 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1520 if (pr->image_width > 32767) wide_image = TRUE;
1522 rt_tile_get_region(has_alpha, pr->ignore_alpha,
1523 pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
1524 static_cast<gdouble>(0.0) - src_x - GET_RIGHT_PIXBUF_OFFSET(rt) * scale_x,
1525 static_cast<gdouble>(0.0) - src_y,
1527 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1528 it->x + pb_x, it->y + pb_y, wide_image);
1529 if (rt->stereo_mode & PR_STEREO_ANAGLYPH &&
1530 (pr->stereo_pixbuf_offset_right > 0 || pr->stereo_pixbuf_offset_left > 0))
1532 GdkPixbuf *right_pb = rt_get_spare_tile(rt);
1533 rt_tile_get_region(has_alpha, pr->ignore_alpha,
1534 pr->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h,
1535 static_cast<gdouble>(0.0) - src_x - GET_LEFT_PIXBUF_OFFSET(rt) * scale_x,
1536 static_cast<gdouble>(0.0) - src_y,
1538 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1539 it->x + pb_x, it->y + pb_y, wide_image);
1540 pr_create_anaglyph(rt->stereo_mode, it->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h);
1541 /* do not care about freeing spare_tile, it will be reused */
1543 rt_tile_apply_orientation(rt, orientation, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
1547 if (draw && it->pixbuf && !it->blank)
1551 if (pr->func_post_process && !(pr->post_process_slow && fast))
1552 pr->func_post_process(pr, &it->pixbuf, x, y, w, h, pr->post_process_user_data);
1554 cr = cairo_create(it->surface);
1555 cairo_rectangle (cr, x, y, w, h);
1556 rt_hidpi_aware_draw(rt, cr, it->pixbuf, 0, 0);
1562 static void rt_tile_expose(RendererTiles *rt, ImageTile *it,
1563 gint x, gint y, gint w, gint h,
1564 gboolean new_data, gboolean fast)
1566 PixbufRenderer *pr = rt->pr;
1569 /* clamp to visible */
1570 if (it->x + x < rt->x_scroll)
1572 w -= rt->x_scroll - it->x - x;
1573 x = rt->x_scroll - it->x;
1575 if (it->x + x + w > rt->x_scroll + pr->vis_width)
1577 w = rt->x_scroll + pr->vis_width - it->x - x;
1580 if (it->y + y < rt->y_scroll)
1582 h -= rt->y_scroll - it->y - y;
1583 y = rt->y_scroll - it->y;
1585 if (it->y + y + h > rt->y_scroll + pr->vis_height)
1587 h = rt->y_scroll + pr->vis_height - it->y - y;
1591 rt_tile_render(rt, it, x, y, w, h, new_data, fast);
1593 cr = cairo_create(rt->surface);
1594 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);
1595 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);
1599 if (rt->overlay_list)
1601 rt_overlay_draw(rt, pr->x_offset + (it->x - rt->x_scroll) + x,
1602 pr->y_offset + (it->y - rt->y_scroll) + y,
1607 gtk_widget_queue_draw(GTK_WIDGET(rt->pr));
1611 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it)
1613 PixbufRenderer *pr = rt->pr;
1614 return (it->x + it->w >= rt->x_scroll && it->x < rt->x_scroll + pr->vis_width &&
1615 it->y + it->h >= rt->y_scroll && it->y < rt->y_scroll + pr->vis_height);
1619 *-------------------------------------------------------------------
1621 *-------------------------------------------------------------------
1624 static gint rt_get_queued_area(GList *work)
1630 auto qd = static_cast<QueueData *>(work->data);
1631 area += qd->w * qd->h;
1638 static gboolean rt_queue_schedule_next_draw(RendererTiles *rt, gboolean force_set)
1640 PixbufRenderer *pr = rt->pr;
1642 gint visible_area = pr->vis_width * pr->vis_height;
1647 DEBUG_2("redraw priority: 2pass");
1648 rt->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, rt_queue_draw_idle_cb, rt, nullptr);
1649 return G_SOURCE_REMOVE;
1652 if (visible_area == 0)
1659 percent = 100.0 * rt_get_queued_area(rt->draw_queue) / visible_area;
1664 /* we have enough data for starting intensive redrawing */
1665 DEBUG_2("redraw priority: high %.2f %%", percent);
1666 rt->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW, rt_queue_draw_idle_cb, rt, nullptr);
1667 return G_SOURCE_REMOVE;
1670 if (percent < 1.0 || force_set)
1672 /* queue is (almost) empty, wait 50 ms*/
1673 DEBUG_2("redraw priority: wait %.2f %%", percent);
1674 rt->draw_idle_id = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, 50, rt_queue_draw_idle_cb, rt, nullptr);
1675 return G_SOURCE_REMOVE;
1678 /* keep the same priority as before */
1679 DEBUG_2("redraw priority: no change %.2f %%", percent);
1680 return G_SOURCE_CONTINUE;
1684 static gboolean rt_queue_draw_idle_cb(gpointer data)
1686 auto rt = static_cast<RendererTiles *>(data);
1687 PixbufRenderer *pr = rt->pr;
1692 if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
1693 (!rt->draw_queue && !rt->draw_queue_2pass) ||
1696 pr_render_complete_signal(pr);
1698 rt->draw_idle_id = 0;
1699 return G_SOURCE_REMOVE;
1704 qd = static_cast<QueueData *>(rt->draw_queue->data);
1705 fast = (pr->zoom_2pass && ((pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0) || pr->post_process_slow));
1711 /* still loading, wait till done (also drops the higher priority) */
1713 return rt_queue_schedule_next_draw(rt, FALSE);
1716 qd = static_cast<QueueData *>(rt->draw_queue_2pass->data);
1720 if (gtk_widget_get_realized(GTK_WIDGET(pr)))
1722 if (rt_tile_is_visible(rt, qd->it))
1724 rt_tile_expose(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1726 else if (qd->new_data)
1728 /* if new pixel data, and we already have a pixmap, update the tile */
1729 qd->it->blank = FALSE;
1730 if (qd->it->surface && qd->it->render_done == TILE_RENDER_ALL)
1732 rt_tile_render(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1739 qd->it->qd = nullptr;
1740 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
1745 rt_queue_merge(qd->it->qd2, qd);
1751 rt->draw_queue_2pass = g_list_append(rt->draw_queue_2pass, qd);
1761 qd->it->qd2 = nullptr;
1762 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
1766 if (!rt->draw_queue && !rt->draw_queue_2pass)
1768 pr_render_complete_signal(pr);
1770 rt->draw_idle_id = 0;
1771 return G_SOURCE_REMOVE;
1774 return rt_queue_schedule_next_draw(rt, FALSE);
1777 static void rt_queue_list_free(GList *list)
1786 qd = static_cast<QueueData *>(work->data);
1789 qd->it->qd = nullptr;
1790 qd->it->qd2 = nullptr;
1797 static void rt_queue_clear(RendererTiles *rt)
1799 rt_queue_list_free(rt->draw_queue);
1800 rt->draw_queue = nullptr;
1802 rt_queue_list_free(rt->draw_queue_2pass);
1803 rt->draw_queue_2pass = nullptr;
1805 if (rt->draw_idle_id)
1807 g_source_remove(rt->draw_idle_id);
1808 rt->draw_idle_id = 0;
1813 static void rt_queue_merge(QueueData *parent, QueueData *qd)
1815 if (parent->x + parent->w < qd->x + qd->w)
1817 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
1819 if (parent->x > qd->x)
1821 parent->w += parent->x - qd->x;
1825 if (parent->y + parent->h < qd->y + qd->h)
1827 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
1829 if (parent->y > qd->y)
1831 parent->h += parent->y - qd->y;
1835 parent->new_data |= qd->new_data;
1838 static gboolean rt_clamp_to_visible(RendererTiles *rt, gint *x, gint *y, gint *w, gint *h)
1840 PixbufRenderer *pr = rt->pr;
1851 vh = pr->vis_height;
1856 if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
1859 nx = CLAMP(*x, vx, vx + vw);
1860 nw = CLAMP(*w - (nx - *x), 1, vw);
1862 ny = CLAMP(*y, vy, vy + vh);
1863 nh = CLAMP(*h - (ny - *y), 1, vh);
1873 static gboolean rt_queue_to_tiles(RendererTiles *rt, gint x, gint y, gint w, gint h,
1874 gboolean clamp, ImageRenderType render,
1875 gboolean new_data, gboolean only_existing)
1877 PixbufRenderer *pr = rt->pr;
1885 if (clamp && !rt_clamp_to_visible(rt, &x, &y, &w, &h)) return FALSE;
1887 x1 = ROUND_DOWN(x, rt->tile_width);
1888 x2 = ROUND_UP(x + w, rt->tile_width);
1890 y1 = ROUND_DOWN(y, rt->tile_height);
1891 y2 = ROUND_UP(y + h, rt->tile_height);
1893 for (j = y1; j <= y2; j += rt->tile_height)
1895 for (i = x1; i <= x2; i += rt->tile_width)
1899 it = rt_tile_get(rt, i, j,
1901 (i + rt->tile_width < rt->x_scroll ||
1902 i > rt->x_scroll + pr->vis_width ||
1903 j + rt->tile_height < rt->y_scroll ||
1904 j > rt->y_scroll + pr->vis_height)));
1907 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
1908 (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
1910 it->render_todo = render;
1913 auto qd = g_new(QueueData, 1);
1915 qd->new_data = new_data;
1925 qd->w = x + w - i - qd->x;
1926 if (qd->x + qd->w > rt->tile_width) qd->w = rt->tile_width - qd->x;
1936 qd->h = y + h - j - qd->y;
1937 if (qd->y + qd->h > rt->tile_height) qd->h = rt->tile_height - qd->y;
1939 if (qd->w < 1 || qd->h < 1)
1945 rt_queue_merge(it->qd, qd);
1951 rt->draw_queue = g_list_append(rt->draw_queue, qd);
1960 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
1961 gboolean clamp, ImageRenderType render,
1962 gboolean new_data, gboolean only_existing)
1964 PixbufRenderer *pr = rt->pr;
1970 nx = CLAMP(x, 0, pr->width - 1);
1971 ny = CLAMP(y, 0, pr->height - 1);
1974 w = CLAMP(w, 0, pr->width - nx);
1975 h = CLAMP(h, 0, pr->height - ny);
1976 if (w < 1 || h < 1) return;
1978 if (rt_queue_to_tiles(rt, nx, ny, w, h, clamp, render, new_data, only_existing) &&
1979 ((!rt->draw_queue && !rt->draw_queue_2pass) || !rt->draw_idle_id))
1981 if (rt->draw_idle_id)
1983 g_source_remove(rt->draw_idle_id);
1984 rt->draw_idle_id = 0;
1986 rt_queue_schedule_next_draw(rt, TRUE);
1990 static void rt_scroll(void *renderer, gint x_off, gint y_off)
1992 auto rt = static_cast<RendererTiles *>(renderer);
1993 PixbufRenderer *pr = rt->pr;
1996 if (rt->stereo_mode & PR_STEREO_MIRROR) x_off = -x_off;
1997 if (rt->stereo_mode & PR_STEREO_FLIP) y_off = -y_off;
1999 gint w = pr->vis_width - abs(x_off);
2000 gint h = pr->vis_height - abs(y_off);
2004 /* scrolled completely to new material */
2005 rt_queue(rt, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2014 cairo_surface_t *surface;
2038 cr = cairo_create(rt->surface);
2039 surface = rt->surface;
2041 /* clipping restricts the intermediate surface's size, so it's a good idea
2043 cairo_rectangle(cr, x1 + pr->x_offset + rt->stereo_off_x, y1 + pr->y_offset + rt->stereo_off_y, w, h);
2045 /* Now push a group to change the target */
2046 cairo_push_group (cr);
2047 cairo_set_source_surface(cr, surface, x1 - x2, y1 - y2);
2049 /* Now copy the intermediate target back */
2050 cairo_pop_group_to_source(cr);
2054 rt_overlay_queue_all(rt, x2, y2, x1, y1);
2056 w = pr->vis_width - w;
2057 h = pr->vis_height - h;
2062 x_off > 0 ? rt->x_scroll + (pr->vis_width - w) : rt->x_scroll, rt->y_scroll,
2063 w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2067 /** @FIXME to optimize this, remove overlap */
2069 rt->x_scroll, y_off > 0 ? rt->y_scroll + (pr->vis_height - h) : rt->y_scroll,
2070 pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2074 static void renderer_area_changed(void *renderer, gint src_x, gint src_y, gint src_w, gint src_h)
2076 auto rt = static_cast<RendererTiles *>(renderer);
2077 PixbufRenderer *pr = rt->pr;
2087 gint orientation = rt_get_orientation(rt);
2088 pr_coords_map_orientation_reverse(orientation,
2089 src_x - GET_RIGHT_PIXBUF_OFFSET(rt), src_y,
2090 pr->image_width, pr->image_height,
2095 if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
2097 /* increase region when using a zoom quality that may access surrounding pixels */
2102 x1 = static_cast<gint>(floor(static_cast<gdouble>(x) * pr->scale));
2103 y1 = static_cast<gint>(floor(static_cast<gdouble>(y) * pr->scale * pr->aspect_ratio));
2104 x2 = static_cast<gint>(ceil(static_cast<gdouble>(x + width) * pr->scale));
2105 y2 = static_cast<gint>(ceil(static_cast<gdouble>(y + height) * pr->scale * pr->aspect_ratio));
2107 rt_queue(rt, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
2110 static void renderer_redraw(RendererTiles *rt, gint x, gint y, gint w, gint h,
2111 gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing)
2113 PixbufRenderer *pr = rt->pr;
2115 x -= rt->stereo_off_x;
2116 y -= rt->stereo_off_y;
2118 rt_border_draw(rt, x, y, w, h);
2120 x = MAX(0, x - pr->x_offset + pr->x_scroll);
2121 y = MAX(0, y - pr->y_offset + pr->y_scroll);
2125 MIN(w, pr->width - x),
2126 MIN(h, pr->height - y),
2127 clamp, render, new_data, only_existing);
2130 static void renderer_update_pixbuf(void *renderer, gboolean)
2132 rt_queue_clear(static_cast<RendererTiles *>(renderer));
2135 static void renderer_update_zoom(void *renderer, gboolean lazy)
2137 auto rt = static_cast<RendererTiles *>(renderer);
2138 PixbufRenderer *pr = rt->pr;
2140 rt_tile_invalidate_all(static_cast<RendererTiles *>(renderer));
2143 renderer_redraw(static_cast<RendererTiles *>(renderer), 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, TRUE, FALSE);
2145 rt_border_clear(rt);
2148 static void renderer_invalidate_region(void *renderer, gint x, gint y, gint w, gint h)
2150 rt_tile_invalidate_region(static_cast<RendererTiles *>(renderer), x, y, w, h);
2153 static void renderer_update_viewport(void *renderer)
2155 auto rt = static_cast<RendererTiles *>(renderer);
2157 rt->stereo_off_x = 0;
2158 rt->stereo_off_y = 0;
2160 if (rt->stereo_mode & PR_STEREO_RIGHT)
2162 if (rt->stereo_mode & PR_STEREO_HORIZ)
2164 rt->stereo_off_x = rt->pr->viewport_width;
2166 else if (rt->stereo_mode & PR_STEREO_VERT)
2168 rt->stereo_off_y = rt->pr->viewport_height;
2170 else if (rt->stereo_mode & PR_STEREO_FIXED)
2172 rt->stereo_off_x = rt->pr->stereo_fixed_x_right;
2173 rt->stereo_off_y = rt->pr->stereo_fixed_y_right;
2178 if (rt->stereo_mode & PR_STEREO_FIXED)
2180 rt->stereo_off_x = rt->pr->stereo_fixed_x_left;
2181 rt->stereo_off_y = rt->pr->stereo_fixed_y_left;
2184 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);
2186 rt_overlay_update_sizes(rt);
2187 rt_border_clear(rt);
2190 static void renderer_stereo_set(void *renderer, gint stereo_mode)
2192 auto rt = static_cast<RendererTiles *>(renderer);
2194 rt->stereo_mode = stereo_mode;
2197 static void renderer_free(void *renderer)
2199 auto rt = static_cast<RendererTiles *>(renderer);
2201 rt_tile_free_all(rt);
2202 if (rt->spare_tile) g_object_unref(rt->spare_tile);
2203 if (rt->overlay_buffer) g_object_unref(rt->overlay_buffer);
2204 rt_overlay_list_clear(rt);
2205 /* disconnect "hierarchy-changed" */
2206 g_signal_handlers_disconnect_matched(G_OBJECT(rt->pr), G_SIGNAL_MATCH_DATA,
2207 0, 0, nullptr, nullptr, rt);
2211 static gboolean rt_realize_cb(GtkWidget *widget, gpointer data)
2213 auto rt = static_cast<RendererTiles *>(data);
2218 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));
2220 cr = cairo_create(rt->surface);
2221 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));
2229 static gboolean rt_size_allocate_cb(GtkWidget *widget, GdkRectangle *allocation, gpointer data)
2231 auto rt = static_cast<RendererTiles *>(data);
2233 cairo_surface_t *old_surface;
2235 if (gtk_widget_get_realized(GTK_WIDGET(rt->pr)))
2237 old_surface = rt->surface;
2238 rt->surface = gdk_window_create_similar_surface(gtk_widget_get_window(widget), CAIRO_CONTENT_COLOR, allocation->width, allocation->height);
2240 cr = cairo_create(rt->surface);
2242 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));
2244 cairo_set_source_surface(cr, old_surface, 0, 0);
2247 cairo_surface_destroy(old_surface);
2249 renderer_redraw(rt, allocation->x, allocation->y, allocation->width, allocation->height, FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2255 static gboolean rt_draw_cb(GtkWidget *, cairo_t *cr, gpointer data)
2257 auto rt = static_cast<RendererTiles *>(data);
2261 if (rt->stereo_mode & (PR_STEREO_HORIZ | PR_STEREO_VERT))
2263 cairo_push_group(cr);
2264 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));
2266 if (rt->stereo_mode & PR_STEREO_HORIZ)
2268 cairo_rectangle(cr, rt->stereo_off_x, 0, rt->pr->viewport_width, rt->pr->viewport_height);
2272 cairo_rectangle(cr, 0, rt->stereo_off_y, rt->pr->viewport_width, rt->pr->viewport_height);
2277 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);
2279 cairo_set_source_surface(cr, rt->surface, 0, 0);
2282 cairo_pop_group_to_source(cr);
2287 cairo_set_source_surface(cr, rt->surface, 0, 0);
2291 work = rt->overlay_list;
2294 od = static_cast<OverlayData *>(work->data);
2299 pw = gdk_pixbuf_get_width(od->pixbuf);
2300 ph = gdk_pixbuf_get_height(od->pixbuf);
2304 if (od->flags & OVL_RELATIVE)
2306 if (px < 0) px = rt->pr->viewport_width - pw + px;
2307 if (py < 0) py = rt->pr->viewport_height - ph + py;
2310 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px, py);
2318 RendererFuncs *renderer_tiles_new(PixbufRenderer *pr)
2320 auto rt = g_new0(RendererTiles, 1);
2324 rt->f.area_changed = renderer_area_changed;
2325 rt->f.update_pixbuf = renderer_update_pixbuf;
2326 rt->f.free = renderer_free;
2327 rt->f.update_zoom = renderer_update_zoom;
2328 rt->f.invalidate_region = renderer_invalidate_region;
2329 rt->f.scroll = rt_scroll;
2330 rt->f.update_viewport = renderer_update_viewport;
2333 rt->f.overlay_add = renderer_tiles_overlay_add;
2334 rt->f.overlay_set = renderer_tiles_overlay_set;
2335 rt->f.overlay_get = renderer_tiles_overlay_get;
2337 rt->f.stereo_set = renderer_stereo_set;
2339 rt->tile_width = options->image.tile_size;
2340 rt->tile_height = options->image.tile_size;
2342 rt->tiles = nullptr;
2343 rt->tile_cache_size = 0;
2345 rt->tile_cache_max = PR_CACHE_SIZE_DEFAULT;
2347 rt->draw_idle_id = 0;
2349 rt->stereo_mode = 0;
2350 rt->stereo_off_x = 0;
2351 rt->stereo_off_y = 0;
2353 rt->hidpi_scale = gtk_widget_get_scale_factor(GTK_WIDGET(rt->pr));
2355 g_signal_connect(G_OBJECT(pr), "hierarchy-changed",
2356 G_CALLBACK(rt_hierarchy_changed_cb), rt);
2358 g_signal_connect(G_OBJECT(pr), "draw",
2359 G_CALLBACK(rt_draw_cb), rt);
2360 g_signal_connect(G_OBJECT(pr), "realize", G_CALLBACK(rt_realize_cb), rt);
2361 g_signal_connect(G_OBJECT(pr), "size-allocate", G_CALLBACK(rt_size_allocate_cb), rt);
2363 return reinterpret_cast<RendererFuncs *>(rt);
2367 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */