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.
28 #include "pixbuf-renderer.h"
29 #include "renderer-tiles.h"
37 /* comment this out if not using this from within Geeqie
38 * defining GQ_BUILD does these things:
39 * - Sets the shift-click scroller pixbuf to a nice icon instead of a black box
45 #include "pixbuf_util.h"
49 EXIF_ORIENTATION_UNKNOWN = 0,
50 EXIF_ORIENTATION_TOP_LEFT = 1,
51 EXIF_ORIENTATION_TOP_RIGHT = 2,
52 EXIF_ORIENTATION_BOTTOM_RIGHT = 3,
53 EXIF_ORIENTATION_BOTTOM_LEFT = 4,
54 EXIF_ORIENTATION_LEFT_TOP = 5,
55 EXIF_ORIENTATION_RIGHT_TOP = 6,
56 EXIF_ORIENTATION_RIGHT_BOTTOM = 7,
57 EXIF_ORIENTATION_LEFT_BOTTOM = 8
58 } ExifOrientationType;
62 /* size to use when breaking up image pane for rendering */
63 #define PR_TILE_SIZE 128
65 typedef struct _ImageTile ImageTile;
66 typedef struct _QueueData QueueData;
70 cairo_surface_t *surface; /* off screen buffer */
71 GdkPixbuf *pixbuf; /* pixbuf area for zooming */
72 gint x; /* x offset into image */
73 gint y; /* y offset into image */
74 gint w; /* width that is visible (may be less if at edge of image) */
75 gint h; /* height '' */
79 /* render_todo: (explanation)
81 AREA render area of tile, usually only used when loading an image
82 note: will jump to an ALL if render_done is not ALL.
83 ALL render entire tile, if never done before w/ ALL, for expose events *only*
86 ImageRenderType render_todo; /* what to do (see above) */
87 ImageRenderType render_done; /* highest that has been done before on tile */
92 guint size; /* est. memory used by pixmap and pixbuf */
105 typedef struct _OverlayData OverlayData;
116 OverlayRendererFlags flags;
119 typedef struct _RendererTiles RendererTiles;
121 struct _RendererTiles
126 gint tile_cache_max; /* max MiB to use for offscreen buffer */
130 gint tile_cols; /* count of tile columns */
131 GList *tiles; /* list of buffer tiles */
132 gint tile_cache_size; /* allocated size of pixmaps/pixbufs */
133 GList *draw_queue; /* list of areas to redraw */
134 GList *draw_queue_2pass;/* list when 2 pass is enabled */
137 cairo_surface_t *overlay_buffer;
138 cairo_surface_t *surface;
140 guint draw_idle_id; /* event source id */
142 GdkPixbuf *spare_tile;
148 gint x_scroll; /* allow local adjustment and mirroring */
156 static void rt_border_draw(RendererTiles *rt, gint x, gint y, gint w, gint h);
157 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h, ImageTile *it);
160 static void rt_tile_free_all(RendererTiles *rt);
161 static void rt_tile_invalidate_region(RendererTiles *rt, gint x, gint y, gint w, gint h);
162 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it);
163 static void rt_queue_clear(RendererTiles *rt);
164 static void rt_queue_merge(QueueData *parent, QueueData *qd);
165 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
166 gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing);
168 static void rt_hierarchy_changed_cb(GtkWidget *widget, GtkWidget *previous_toplevel, gpointer data);
169 static gint rt_queue_draw_idle_cb(gpointer data);
171 #define GET_RIGHT_PIXBUF_OFFSET(rt) \
172 (( (rt->stereo_mode & PR_STEREO_RIGHT) && !(rt->stereo_mode & PR_STEREO_SWAP)) || \
173 (!(rt->stereo_mode & PR_STEREO_RIGHT) && (rt->stereo_mode & PR_STEREO_SWAP)) ? \
174 rt->pr->stereo_pixbuf_offset_right : rt->pr->stereo_pixbuf_offset_left )
176 #define GET_LEFT_PIXBUF_OFFSET(rt) \
177 ((!(rt->stereo_mode & PR_STEREO_RIGHT) && !(rt->stereo_mode & PR_STEREO_SWAP)) || \
178 ( (rt->stereo_mode & PR_STEREO_RIGHT) && (rt->stereo_mode & PR_STEREO_SWAP)) ? \
179 rt->pr->stereo_pixbuf_offset_right : rt->pr->stereo_pixbuf_offset_left )
182 static void rt_sync_scroll(RendererTiles *rt)
184 PixbufRenderer *pr = rt->pr;
186 rt->x_scroll = (rt->stereo_mode & PR_STEREO_MIRROR) ?
187 pr->width - pr->vis_width - pr->x_scroll
190 rt->y_scroll = (rt->stereo_mode & PR_STEREO_FLIP) ?
191 pr->height - pr->vis_height - pr->y_scroll
196 *-------------------------------------------------------------------
198 *-------------------------------------------------------------------
201 static void rt_border_draw(RendererTiles *rt, gint x, gint y, gint w, gint h)
203 PixbufRenderer *pr = rt->pr;
209 box = GTK_WIDGET(pr);
210 window = gtk_widget_get_window(box);
214 #if GTK_CHECK_VERSION(3,0,0)
215 cr = cairo_create(rt->surface);
217 cr = gdk_cairo_create(window);
220 if (!pr->pixbuf && !pr->source_tiles_enabled)
222 if (pr_clip_region(x, y, w, h,
224 pr->viewport_width, pr->viewport_height,
227 cairo_set_source_rgb(cr, (double)pr->color.red/65535, (double)pr->color.green/65535, (double)pr->color.blue/65535);
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, NULL);
236 if (pr->vis_width < pr->viewport_width)
238 if (pr->x_offset > 0 &&
239 pr_clip_region(x, y, w, h,
241 pr->x_offset, pr->viewport_height,
244 cairo_set_source_rgb(cr, (double)pr->color.red/65535, (double)pr->color.green/65535, (double)pr->color.blue/65535);
245 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
247 rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
249 if (pr->viewport_width - pr->vis_width - pr->x_offset > 0 &&
250 pr_clip_region(x, y, w, h,
251 pr->x_offset + pr->vis_width, 0,
252 pr->viewport_width - pr->vis_width - pr->x_offset, pr->viewport_height,
255 cairo_set_source_rgb(cr, (double)pr->color.red/65535, (double)pr->color.green/65535, (double)pr->color.blue/65535);
256 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
258 rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
261 if (pr->vis_height < pr->viewport_height)
263 if (pr->y_offset > 0 &&
264 pr_clip_region(x, y, w, h,
266 pr->vis_width, pr->y_offset,
269 cairo_set_source_rgb(cr, (double)pr->color.red/65535, (double)pr->color.green/65535, (double)pr->color.blue/65535);
270 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
272 rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
274 if (pr->viewport_height - pr->vis_height - pr->y_offset > 0 &&
275 pr_clip_region(x, y, w, h,
276 pr->x_offset, pr->y_offset + pr->vis_height,
277 pr->vis_width, pr->viewport_height - pr->vis_height - pr->y_offset,
280 cairo_set_source_rgb(cr, (double)pr->color.red/65535, (double)pr->color.green/65535, (double)pr->color.blue/65535);
281 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
283 rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
289 static void rt_border_clear(RendererTiles *rt)
291 PixbufRenderer *pr = rt->pr;
292 rt_border_draw(rt, 0, 0, pr->viewport_width, pr->viewport_height);
297 *-------------------------------------------------------------------
299 *-------------------------------------------------------------------
302 static ImageTile *rt_tile_new(gint x, gint y, gint width, gint height)
306 it = g_new0(ImageTile, 1);
313 it->render_done = TILE_RENDER_NONE;
318 static void rt_tile_free(ImageTile *it)
322 if (it->pixbuf) g_object_unref(it->pixbuf);
323 if (it->surface) cairo_surface_destroy(it->surface);
328 static void rt_tile_free_all(RendererTiles *rt)
343 g_list_free(rt->tiles);
345 rt->tile_cache_size = 0;
348 static ImageTile *rt_tile_add(RendererTiles *rt, gint x, gint y)
350 PixbufRenderer *pr = rt->pr;
353 it = rt_tile_new(x, y, rt->tile_width, rt->tile_height);
355 if (it->x + it->w > pr->width) it->w = pr->width - it->x;
356 if (it->y + it->h > pr->height) it->h = pr->height - it->y;
358 rt->tiles = g_list_prepend(rt->tiles, it);
359 rt->tile_cache_size += it->size;
364 static void rt_tile_remove(RendererTiles *rt, ImageTile *it)
368 QueueData *qd = it->qd;
371 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
377 QueueData *qd = it->qd2;
380 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
384 rt->tiles = g_list_remove(rt->tiles, it);
385 rt->tile_cache_size -= it->size;
390 static void rt_tile_free_space(RendererTiles *rt, guint space, ImageTile *it)
392 PixbufRenderer *pr = rt->pr;
396 work = g_list_last(rt->tiles);
398 if (pr->source_tiles_enabled && pr->scale < 1.0)
402 tiles = (pr->vis_width / rt->tile_width + 1) * (pr->vis_height / rt->tile_height + 1);
403 tile_max = MAX(tiles * rt->tile_width * rt->tile_height * 3,
404 (gint)((gdouble)rt->tile_cache_max * 1048576.0 * pr->scale));
408 tile_max = rt->tile_cache_max * 1048576;
411 while (work && rt->tile_cache_size + space > tile_max)
418 ((!needle->qd && !needle->qd2) || !rt_tile_is_visible(rt, needle))) rt_tile_remove(rt, needle);
422 static void rt_tile_invalidate_all(RendererTiles *rt)
424 PixbufRenderer *pr = rt->pr;
435 it->render_done = TILE_RENDER_NONE;
436 it->render_todo = TILE_RENDER_ALL;
439 it->w = MIN(rt->tile_width, pr->width - it->x);
440 it->h = MIN(rt->tile_height, pr->height - it->y);
444 static void rt_tile_invalidate_region(RendererTiles *rt, gint x, gint y, gint w, gint h)
450 x1 = ROUND_DOWN(x, rt->tile_width);
451 x2 = ROUND_UP(x + w, rt->tile_width);
453 y1 = ROUND_DOWN(y, rt->tile_height);
454 y2 = ROUND_UP(y + h, rt->tile_height);
464 if (it->x < x2 && it->x + it->w > x1 &&
465 it->y < y2 && it->y + it->h > y1)
467 it->render_done = TILE_RENDER_NONE;
468 it->render_todo = TILE_RENDER_ALL;
473 static ImageTile *rt_tile_get(RendererTiles *rt, gint x, gint y, gboolean only_existing)
483 if (it->x == x && it->y == y)
485 rt->tiles = g_list_delete_link(rt->tiles, work);
486 rt->tiles = g_list_prepend(rt->tiles, it);
493 if (only_existing) return NULL;
495 return rt_tile_add(rt, x, y);
498 static gint pixmap_calc_size(cairo_surface_t *surface)
502 // d = gdk_drawable_get_depth(pixmap);
503 // gdk_drawable_get_size(pixmap, &w, &h);
504 return PR_TILE_SIZE * PR_TILE_SIZE * 4 / 8;
507 static void rt_hidpi_aware_draw(
514 #if GTK_CHECK_VERSION(3, 10, 0)
515 cairo_surface_t *surface;
516 surface = gdk_cairo_surface_create_from_pixbuf(pixbuf, rt->hidpi_scale, NULL);
517 cairo_set_source_surface(cr, surface, x, y);
519 cairo_surface_destroy(surface);
521 gdk_cairo_set_source_pixbuf(cr, pixbuf, x, y);
526 static void rt_tile_prepare(RendererTiles *rt, ImageTile *it)
528 PixbufRenderer *pr = rt->pr;
531 cairo_surface_t *surface;
534 surface = gdk_window_create_similar_surface(gtk_widget_get_window((GtkWidget *)pr),
536 rt->tile_width, rt->tile_height);
538 size = pixmap_calc_size(surface) * rt->hidpi_scale * rt->hidpi_scale;
539 rt_tile_free_space(rt, size, it);
541 it->surface = surface;
543 rt->tile_cache_size += size;
550 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, rt->hidpi_scale * rt->tile_width, rt->hidpi_scale * rt->tile_height);
552 size = gdk_pixbuf_get_rowstride(pixbuf) * rt->tile_height * rt->hidpi_scale;
553 rt_tile_free_space(rt, size, it);
557 rt->tile_cache_size += size;
562 *-------------------------------------------------------------------
564 *-------------------------------------------------------------------
567 static void rt_overlay_get_position(RendererTiles *rt, OverlayData *od,
568 gint *x, gint *y, gint *w, gint *h)
570 PixbufRenderer *pr = rt->pr;
573 pw = gdk_pixbuf_get_width(od->pixbuf);
574 ph = gdk_pixbuf_get_height(od->pixbuf);
578 if (od->flags & OVL_RELATIVE)
580 if (px < 0) px = pr->viewport_width - pw + px;
581 if (py < 0) py = pr->viewport_height - ph + py;
590 static void rt_overlay_init_window(RendererTiles *rt, OverlayData *od)
592 PixbufRenderer *pr = rt->pr;
594 GdkWindowAttr attributes;
595 gint attributes_mask;
597 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
599 attributes.window_type = GDK_WINDOW_CHILD;
600 attributes.wclass = GDK_INPUT_OUTPUT;
601 attributes.width = pw;
602 attributes.height = ph;
603 attributes.event_mask = GDK_EXPOSURE_MASK;
606 od->window = gdk_window_new(gtk_widget_get_window(GTK_WIDGET(pr)), &attributes, attributes_mask);
607 gdk_window_set_user_data(od->window, pr);
608 gdk_window_move(od->window, px + rt->stereo_off_x, py + rt->stereo_off_y);
609 #if !GTK_CHECK_VERSION(3,0,0)
610 gdk_window_show(od->window);
614 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h,
617 PixbufRenderer *pr = rt->pr;
620 work = rt->overlay_list;
630 if (!od->window) rt_overlay_init_window(rt, od);
632 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
633 if (pr_clip_region(x, y, w, h, px, py, pw, ph, &rx, &ry, &rw, &rh))
635 if (!rt->overlay_buffer)
637 rt->overlay_buffer = gdk_window_create_similar_surface(gtk_widget_get_window((GtkWidget *)pr),
639 rt->tile_width, rt->tile_height);
646 cr = cairo_create(rt->overlay_buffer);
647 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);
648 cairo_rectangle(cr, 0, 0, rw, rh);
649 cairo_fill_preserve(cr);
651 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - rx, py - ry);
655 cr = gdk_cairo_create(od->window);
656 cairo_set_source_surface(cr, rt->overlay_buffer, rx - px, ry - py);
657 cairo_rectangle (cr, rx - px, ry - py, rw, rh);
663 /* no ImageTile means region may be larger than our scratch buffer */
666 for (sx = rx; sx < rx + rw; sx += rt->tile_width)
667 for (sy = ry; sy < ry + rh; sy += rt->tile_height)
672 sw = MIN(rx + rw - sx, rt->tile_width);
673 sh = MIN(ry + rh - sy, rt->tile_height);
675 cr = cairo_create(rt->overlay_buffer);
676 cairo_set_source_rgb(cr, 0, 0, 0);
677 cairo_rectangle(cr, 0, 0, sw, sh);
678 cairo_fill_preserve(cr);
680 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - sx, py - sy);
684 cr = gdk_cairo_create(od->window);
685 cairo_set_source_surface(cr, rt->overlay_buffer, sx - px, sy - py);
686 cairo_rectangle (cr, sx - px, sy - py, sw, sh);
695 static void rt_overlay_queue_draw(RendererTiles *rt, OverlayData *od, gint x1, gint y1, gint x2, gint y2)
697 PixbufRenderer *pr = rt->pr;
700 rt_overlay_get_position(rt, od, &x, &y, &w, &h);
708 rt_queue(rt, rt->x_scroll - pr->x_offset + x,
709 rt->y_scroll - pr->y_offset + y,
711 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
713 rt_border_draw(rt, x, y, w, h);
716 static void rt_overlay_queue_all(RendererTiles *rt, gint x1, gint y1, gint x2, gint y2)
720 work = rt->overlay_list;
723 OverlayData *od = work->data;
726 rt_overlay_queue_draw(rt, od, x1, y1, x2, y2);
730 static void rt_overlay_update_sizes(RendererTiles *rt)
734 work = rt->overlay_list;
737 OverlayData *od = work->data;
740 if (!od->window) rt_overlay_init_window(rt, od);
742 if (od->flags & OVL_RELATIVE)
746 rt_overlay_get_position(rt, od, &x, &y, &w, &h);
747 gdk_window_move_resize(od->window, x + rt->stereo_off_x, y + rt->stereo_off_y, w, h);
752 static OverlayData *rt_overlay_find(RendererTiles *rt, gint id)
756 work = rt->overlay_list;
759 OverlayData *od = work->data;
762 if (od->id == id) return od;
769 gint renderer_tiles_overlay_add(void *renderer, GdkPixbuf *pixbuf, gint x, gint y,
770 OverlayRendererFlags flags)
772 RendererTiles *rt = (RendererTiles *) renderer;
773 PixbufRenderer *pr = rt->pr;
777 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
778 g_return_val_if_fail(pixbuf != NULL, -1);
781 while (rt_overlay_find(rt, id)) id++;
783 od = g_new0(OverlayData, 1);
786 g_object_ref(G_OBJECT(od->pixbuf));
791 rt_overlay_init_window(rt, od);
793 rt->overlay_list = g_list_append(rt->overlay_list, od);
795 #if GTK_CHECK_VERSION(3,0,0)
796 gtk_widget_queue_draw(GTK_WIDGET(rt->pr));
798 rt_overlay_queue_draw(rt, od, 0, 0, 0, 0);
804 static void rt_overlay_free(RendererTiles *rt, OverlayData *od)
806 rt->overlay_list = g_list_remove(rt->overlay_list, od);
808 if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
809 if (od->window) gdk_window_destroy(od->window);
812 if (!rt->overlay_list && rt->overlay_buffer)
814 cairo_surface_destroy(rt->overlay_buffer);
815 rt->overlay_buffer = NULL;
819 static void rt_overlay_list_clear(RendererTiles *rt)
821 while (rt->overlay_list)
825 od = rt->overlay_list->data;
826 rt_overlay_free(rt, od);
830 static void rt_overlay_list_reset_window(RendererTiles *rt)
834 if (rt->overlay_buffer) cairo_surface_destroy(rt->overlay_buffer);
835 rt->overlay_buffer = NULL;
837 work = rt->overlay_list;
840 OverlayData *od = work->data;
842 if (od->window) gdk_window_destroy(od->window);
847 #if GTK_CHECK_VERSION(3,0,0)
848 void renderer_tiles_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint x, gint y)
850 RendererTiles *rc = (RendererTiles *)renderer;
851 PixbufRenderer *pr = rc->pr;
854 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
856 od = rt_overlay_find(rc, id);
861 g_object_ref(G_OBJECT(pixbuf));
862 g_object_unref(G_OBJECT(od->pixbuf));
867 rt_overlay_free(rc, od);
871 void renderer_tiles_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint x, gint y)
873 RendererTiles *rt = (RendererTiles *) renderer;
874 PixbufRenderer *pr = rt->pr;
877 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
879 od = rt_overlay_find(rt, id);
886 g_object_ref(G_OBJECT(pixbuf));
887 g_object_unref(G_OBJECT(od->pixbuf));
893 if (!od->window) rt_overlay_init_window(rt, od);
895 rt_overlay_queue_draw(rt, od, 0, 0, 0, 0);
896 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
897 gdk_window_move_resize(od->window, px + rt->stereo_off_x, py + rt->stereo_off_y, pw, ph);
901 rt_overlay_queue_draw(rt, od, 0, 0, 0, 0);
902 rt_overlay_free(rt, od);
907 gboolean renderer_tiles_overlay_get(void *renderer, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
909 RendererTiles *rt = (RendererTiles *) renderer;
910 PixbufRenderer *pr = rt->pr;
913 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
915 od = rt_overlay_find(rt, id);
916 if (!od) return FALSE;
918 if (pixbuf) *pixbuf = od->pixbuf;
925 static void rt_hierarchy_changed_cb(GtkWidget *widget, GtkWidget *previous_toplevel, gpointer data)
927 RendererTiles *rt = data;
928 rt_overlay_list_reset_window(rt);
932 *-------------------------------------------------------------------
934 *-------------------------------------------------------------------
937 static GdkPixbuf *rt_get_spare_tile(RendererTiles *rt)
939 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);
940 return rt->spare_tile;
943 #define COLOR_BYTES 3 /* rgb */
945 static void rt_tile_rotate_90_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
947 GdkPixbuf *src = *tile;
950 guchar *s_pix, *d_pix;
952 guchar *ip, *spi, *dpi;
954 gint tw = rt->tile_width * rt->hidpi_scale;
956 srs = gdk_pixbuf_get_rowstride(src);
957 s_pix = gdk_pixbuf_get_pixels(src);
958 spi = s_pix + (x * COLOR_BYTES);
960 dest = rt_get_spare_tile(rt);
961 drs = gdk_pixbuf_get_rowstride(dest);
962 d_pix = gdk_pixbuf_get_pixels(dest);
963 dpi = d_pix + (tw - 1) * COLOR_BYTES;
965 for (i = y; i < y + h; i++)
967 sp = spi + (i * srs);
968 ip = dpi - (i * COLOR_BYTES);
969 for (j = x; j < x + w; j++)
972 memcpy(dp, sp, COLOR_BYTES);
977 rt->spare_tile = src;
981 static void rt_tile_rotate_90_counter_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
983 GdkPixbuf *src = *tile;
986 guchar *s_pix, *d_pix;
988 guchar *ip, *spi, *dpi;
990 gint th = rt->tile_height * rt->hidpi_scale;
992 srs = gdk_pixbuf_get_rowstride(src);
993 s_pix = gdk_pixbuf_get_pixels(src);
994 spi = s_pix + (x * COLOR_BYTES);
996 dest = rt_get_spare_tile(rt);
997 drs = gdk_pixbuf_get_rowstride(dest);
998 d_pix = gdk_pixbuf_get_pixels(dest);
999 dpi = d_pix + (th - 1) * drs;
1001 for (i = y; i < y + h; i++)
1003 sp = spi + (i * srs);
1004 ip = dpi + (i * COLOR_BYTES);
1005 for (j = x; j < x + w; j++)
1007 dp = ip - (j * drs);
1008 memcpy(dp, sp, COLOR_BYTES);
1013 rt->spare_tile = src;
1017 static void rt_tile_mirror_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1019 GdkPixbuf *src = *tile;
1022 guchar *s_pix, *d_pix;
1027 gint tw = rt->tile_width * rt->hidpi_scale;
1029 srs = gdk_pixbuf_get_rowstride(src);
1030 s_pix = gdk_pixbuf_get_pixels(src);
1031 spi = s_pix + (x * COLOR_BYTES);
1033 dest = rt_get_spare_tile(rt);
1034 drs = gdk_pixbuf_get_rowstride(dest);
1035 d_pix = gdk_pixbuf_get_pixels(dest);
1036 dpi = d_pix + (tw - x - 1) * COLOR_BYTES;
1038 for (i = y; i < y + h; i++)
1040 sp = spi + (i * srs);
1041 dp = dpi + (i * drs);
1042 for (j = 0; j < w; j++)
1044 memcpy(dp, sp, COLOR_BYTES);
1050 rt->spare_tile = src;
1054 static void rt_tile_mirror_and_flip(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1056 GdkPixbuf *src = *tile;
1059 guchar *s_pix, *d_pix;
1063 gint tw = rt->tile_width * rt->hidpi_scale;
1064 gint th = rt->tile_height * rt->hidpi_scale;
1066 srs = gdk_pixbuf_get_rowstride(src);
1067 s_pix = gdk_pixbuf_get_pixels(src);
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 + (tw - 1) * COLOR_BYTES;
1074 for (i = y; i < y + h; i++)
1076 sp = s_pix + (i * srs) + (x * COLOR_BYTES);
1077 dp = dpi - (i * drs) - (x * COLOR_BYTES);
1078 for (j = 0; j < w; j++)
1080 memcpy(dp, sp, COLOR_BYTES);
1086 rt->spare_tile = src;
1090 static void rt_tile_flip_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1092 GdkPixbuf *src = *tile;
1095 guchar *s_pix, *d_pix;
1099 gint th = rt->tile_height * rt->hidpi_scale;
1101 srs = gdk_pixbuf_get_rowstride(src);
1102 s_pix = gdk_pixbuf_get_pixels(src);
1103 spi = s_pix + (x * COLOR_BYTES);
1105 dest = rt_get_spare_tile(rt);
1106 drs = gdk_pixbuf_get_rowstride(dest);
1107 d_pix = gdk_pixbuf_get_pixels(dest);
1108 dpi = d_pix + (th - 1) * drs + (x * COLOR_BYTES);
1110 for (i = y; i < y + h; i++)
1112 sp = spi + (i * srs);
1113 dp = dpi - (i * drs);
1114 memcpy(dp, sp, w * COLOR_BYTES);
1117 rt->spare_tile = src;
1121 static void rt_tile_apply_orientation(RendererTiles *rt, gint orientation, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h)
1123 switch (orientation)
1125 case EXIF_ORIENTATION_TOP_LEFT:
1126 /* normal -- nothing to do */
1128 case EXIF_ORIENTATION_TOP_RIGHT:
1131 rt_tile_mirror_only(rt, pixbuf, x, y, w, h);
1134 case EXIF_ORIENTATION_BOTTOM_RIGHT:
1137 rt_tile_mirror_and_flip(rt, pixbuf, x, y, w, h);
1140 case EXIF_ORIENTATION_BOTTOM_LEFT:
1143 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1146 case EXIF_ORIENTATION_LEFT_TOP:
1148 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1149 rt_tile_rotate_90_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1152 case EXIF_ORIENTATION_RIGHT_TOP:
1153 /* rotated -90 (270) */
1155 rt_tile_rotate_90_clockwise(rt, pixbuf, x, y, w, h);
1158 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1160 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1161 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1164 case EXIF_ORIENTATION_LEFT_BOTTOM:
1167 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, y, w, h);
1171 /* The other values are out of range */
1176 static gboolean rt_source_tile_render(RendererTiles *rt, ImageTile *it,
1177 gint x, gint y, gint w, gint h,
1178 gboolean new_data, gboolean fast)
1180 PixbufRenderer *pr = rt->pr;
1183 gboolean draw = FALSE;
1185 if (pr->zoom == 1.0 || pr->scale == 1.0)
1187 list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE);
1192 gint rx, ry, rw, rh;
1197 if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1198 it->x + x, it->y + y, w, h,
1199 &rx, &ry, &rw, &rh))
1202 cr = cairo_create(it->surface);
1203 cairo_rectangle (cr, rx - it->x, ry - it->y, rw, rh);
1207 cairo_set_source_rgb(cr, 0, 0, 0);
1210 else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
1212 rt_hidpi_aware_draw(rt, cr, st->pixbuf, -it->x + st->x, -it->y + st->y);
1220 gdouble scale_x, scale_y;
1221 gint sx, sy, sw, sh;
1223 if (pr->image_width == 0 || pr->image_height == 0) return FALSE;
1224 scale_x = (gdouble)pr->width / pr->image_width;
1225 scale_y = (gdouble)pr->height / pr->image_height;
1227 sx = (gdouble)(it->x + x) / scale_x;
1228 sy = (gdouble)(it->y + y) / scale_y;
1229 sw = (gdouble)w / scale_x;
1230 sh = (gdouble)h / scale_y;
1232 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1235 /* draws red over draw region, to check for leaks (regions not filled) */
1236 pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255);
1239 list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE);
1244 gint rx, ry, rw, rh;
1245 gint stx, sty, stw, sth;
1250 stx = floor((gdouble)st->x * scale_x);
1251 sty = floor((gdouble)st->y * scale_y);
1252 stw = ceil((gdouble)(st->x + pr->source_tile_width) * scale_x) - stx;
1253 sth = ceil((gdouble)(st->y + pr->source_tile_height) * scale_y) - sty;
1255 if (pr_clip_region(stx, sty, stw, sth,
1256 it->x + x, it->y + y, w, h,
1257 &rx, &ry, &rw, &rh))
1263 cr = cairo_create(it->surface);
1264 cairo_rectangle (cr, rx - st->x, ry - st->y, rw, rh);
1265 cairo_set_source_rgb(cr, 0, 0, 0);
1274 /* may need to use unfloored stx,sty values here */
1275 offset_x = (gdouble)(stx - it->x);
1276 offset_y = (gdouble)(sty - it->y);
1278 gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh,
1279 (gdouble) 0.0 + offset_x,
1280 (gdouble) 0.0 + offset_y,
1282 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1294 static void rt_tile_get_region(gboolean has_alpha, gboolean ignore_alpha,
1295 const GdkPixbuf *src, GdkPixbuf *dest,
1296 int pb_x, int pb_y, int pb_w, int pb_h,
1297 double offset_x, double offset_y, double scale_x, double scale_y,
1298 GdkInterpType interp_type,
1299 int check_x, int check_y)
1301 GdkPixbuf* tmppixbuf;
1305 if (scale_x == 1.0 && scale_y == 1.0)
1307 gdk_pixbuf_copy_area(src,
1308 -offset_x + pb_x, -offset_y + pb_y,
1315 gdk_pixbuf_scale(src, dest,
1316 pb_x, pb_y, pb_w, pb_h,
1327 tmppixbuf = gdk_pixbuf_add_alpha(src, FALSE, 0, 0, 0);
1329 pixbuf_ignore_alpha_rect(tmppixbuf, 0, 0, gdk_pixbuf_get_width(src), gdk_pixbuf_get_height(src));
1331 gdk_pixbuf_composite_color(tmppixbuf, dest,
1332 pb_x, pb_y, pb_w, pb_h,
1336 (scale_x == 1.0 && scale_y == 1.0) ? GDK_INTERP_NEAREST : interp_type,
1337 255, check_x, check_y,
1338 PR_ALPHA_CHECK_SIZE,
1339 ((options->image.alpha_color_1.red << 8 & 0x00FF0000) +
1340 (options->image.alpha_color_1.green & 0x00FF00) +
1341 (options->image.alpha_color_1.blue >> 8 & 0x00FF)),
1342 ((options->image.alpha_color_2.red << 8 & 0x00FF0000) +
1343 (options->image.alpha_color_2.green & 0x00FF00) +
1344 (options->image.alpha_color_2.blue >> 8 & 0x00FF)));
1345 g_object_unref(tmppixbuf);
1349 gdk_pixbuf_composite_color(src, dest,
1350 pb_x, pb_y, pb_w, pb_h,
1354 (scale_x == 1.0 && scale_y == 1.0) ? GDK_INTERP_NEAREST : interp_type,
1355 255, check_x, check_y,
1356 PR_ALPHA_CHECK_SIZE,
1357 ((options->image.alpha_color_1.red << 8 & 0x00FF0000) +
1358 (options->image.alpha_color_1.green & 0x00FF00) +
1359 (options->image.alpha_color_1.blue >> 8 & 0x00FF)),
1360 ((options->image.alpha_color_2.red << 8 & 0x00FF0000) +
1361 (options->image.alpha_color_2.green & 0x00FF00) +
1362 (options->image.alpha_color_2.blue >> 8 & 0x00FF)));
1368 static gint rt_get_orientation(RendererTiles *rt)
1370 PixbufRenderer *pr = rt->pr;
1372 gint orientation = pr->orientation;
1373 static const gint mirror[] = {1, 2, 1, 4, 3, 6, 5, 8, 7};
1374 static const gint flip[] = {1, 4, 3, 2, 1, 8, 7, 6, 5};
1376 if (rt->stereo_mode & PR_STEREO_MIRROR) orientation = mirror[orientation];
1377 if (rt->stereo_mode & PR_STEREO_FLIP) orientation = flip[orientation];
1382 static void rt_tile_render(RendererTiles *rt, ImageTile *it,
1383 gint x, gint y, gint w, gint h,
1384 gboolean new_data, gboolean fast)
1386 PixbufRenderer *pr = rt->pr;
1388 gboolean draw = FALSE;
1389 gint orientation = rt_get_orientation(rt);
1391 if (it->render_todo == TILE_RENDER_NONE && it->surface && !new_data) return;
1393 if (it->render_done != TILE_RENDER_ALL)
1399 if (!fast) it->render_done = TILE_RENDER_ALL;
1401 else if (it->render_todo != TILE_RENDER_AREA)
1403 if (!fast) it->render_todo = TILE_RENDER_NONE;
1407 if (!fast) it->render_todo = TILE_RENDER_NONE;
1409 if (new_data) it->blank = FALSE;
1411 rt_tile_prepare(rt, it);
1412 has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
1414 /* FIXME checker colors for alpha should be configurable,
1415 * also should be drawn for blank = TRUE
1420 /* no data, do fast rect fill */
1422 cr = cairo_create(it->surface);
1423 cairo_rectangle (cr, 0, 0, it->w, it->h);
1424 cairo_set_source_rgb(cr, 0, 0, 0);
1428 else if (pr->source_tiles_enabled)
1430 draw = rt_source_tile_render(rt, it, x, y, w, h, new_data, fast);
1434 gdouble scale_x, scale_y;
1435 gdouble src_x, src_y;
1439 if (pr->image_width == 0 || pr->image_height == 0) return;
1441 scale_x = rt->hidpi_scale * (gdouble)pr->width / pr->image_width;
1442 scale_y = rt->hidpi_scale * (gdouble)pr->height / pr->image_height;
1444 pr_tile_coords_map_orientation(orientation, it->x, it->y,
1445 pr->width, pr->height,
1446 rt->tile_width, rt->tile_height,
1448 pr_tile_region_map_orientation(orientation, x, y,
1449 rt->tile_width, rt->tile_height,
1454 src_x *= rt->hidpi_scale;
1455 src_y *= rt->hidpi_scale;
1456 pb_x *= rt->hidpi_scale;
1457 pb_y *= rt->hidpi_scale;
1458 pb_w *= rt->hidpi_scale;
1459 pb_h *= rt->hidpi_scale;
1461 switch (orientation)
1464 case EXIF_ORIENTATION_LEFT_TOP:
1465 case EXIF_ORIENTATION_RIGHT_TOP:
1466 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1467 case EXIF_ORIENTATION_LEFT_BOTTOM:
1477 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
1478 * small sizes for anything but GDK_INTERP_NEAREST
1480 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1482 rt_tile_get_region(has_alpha, pr->ignore_alpha,
1483 pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
1484 (gdouble) 0.0 - src_x - GET_RIGHT_PIXBUF_OFFSET(rt) * scale_x,
1485 (gdouble) 0.0 - src_y,
1487 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1488 it->x + pb_x, it->y + pb_y);
1489 if (rt->stereo_mode & PR_STEREO_ANAGLYPH &&
1490 (pr->stereo_pixbuf_offset_right > 0 || pr->stereo_pixbuf_offset_left > 0))
1492 GdkPixbuf *right_pb = rt_get_spare_tile(rt);
1493 rt_tile_get_region(has_alpha, pr->ignore_alpha,
1494 pr->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h,
1495 (gdouble) 0.0 - src_x - GET_LEFT_PIXBUF_OFFSET(rt) * scale_x,
1496 (gdouble) 0.0 - src_y,
1498 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1499 it->x + pb_x, it->y + pb_y);
1500 pr_create_anaglyph(rt->stereo_mode, it->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h);
1501 /* do not care about freeing spare_tile, it will be reused */
1503 rt_tile_apply_orientation(rt, orientation, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
1507 if (draw && it->pixbuf && !it->blank)
1511 if (pr->func_post_process && !(pr->post_process_slow && fast))
1512 pr->func_post_process(pr, &it->pixbuf, x, y, w, h, pr->post_process_user_data);
1514 cr = cairo_create(it->surface);
1515 cairo_rectangle (cr, x, y, w, h);
1516 rt_hidpi_aware_draw(rt, cr, it->pixbuf, 0, 0);
1522 static void rt_tile_expose(RendererTiles *rt, ImageTile *it,
1523 gint x, gint y, gint w, gint h,
1524 gboolean new_data, gboolean fast)
1526 PixbufRenderer *pr = rt->pr;
1531 /* clamp to visible */
1532 if (it->x + x < rt->x_scroll)
1534 w -= rt->x_scroll - it->x - x;
1535 x = rt->x_scroll - it->x;
1537 if (it->x + x + w > rt->x_scroll + pr->vis_width)
1539 w = rt->x_scroll + pr->vis_width - it->x - x;
1542 if (it->y + y < rt->y_scroll)
1544 h -= rt->y_scroll - it->y - y;
1545 y = rt->y_scroll - it->y;
1547 if (it->y + y + h > rt->y_scroll + pr->vis_height)
1549 h = rt->y_scroll + pr->vis_height - it->y - y;
1553 rt_tile_render(rt, it, x, y, w, h, new_data, fast);
1555 box = GTK_WIDGET(pr);
1556 window = gtk_widget_get_window(box);
1558 #if GTK_CHECK_VERSION(3,0,0)
1559 cr = cairo_create(rt->surface);
1561 cr = gdk_cairo_create(window);
1563 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);
1564 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);
1568 if (rt->overlay_list)
1570 rt_overlay_draw(rt, pr->x_offset + (it->x - rt->x_scroll) + x,
1571 pr->y_offset + (it->y - rt->y_scroll) + y,
1576 #if GTK_CHECK_VERSION(3,0,0)
1577 gtk_widget_queue_draw(GTK_WIDGET(rt->pr));
1582 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it)
1584 PixbufRenderer *pr = rt->pr;
1585 return (it->x + it->w >= rt->x_scroll && it->x < rt->x_scroll + pr->vis_width &&
1586 it->y + it->h >= rt->y_scroll && it->y < rt->y_scroll + pr->vis_height);
1590 *-------------------------------------------------------------------
1592 *-------------------------------------------------------------------
1595 static gint rt_get_queued_area(GList *work)
1601 QueueData *qd = work->data;
1602 area += qd->w * qd->h;
1609 static gboolean rt_queue_schedule_next_draw(RendererTiles *rt, gboolean force_set)
1611 PixbufRenderer *pr = rt->pr;
1613 gint visible_area = pr->vis_width * pr->vis_height;
1618 DEBUG_2("redraw priority: 2pass");
1619 rt->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, rt_queue_draw_idle_cb, rt, NULL);
1623 if (visible_area == 0)
1630 percent = 100.0 * rt_get_queued_area(rt->draw_queue) / visible_area;
1635 /* we have enough data for starting intensive redrawing */
1636 DEBUG_2("redraw priority: high %.2f %%", percent);
1637 rt->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW, rt_queue_draw_idle_cb, rt, NULL);
1641 if (percent < 1.0 || force_set)
1643 /* queue is (almost) empty, wait 50 ms*/
1644 DEBUG_2("redraw priority: wait %.2f %%", percent);
1645 rt->draw_idle_id = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, 50, rt_queue_draw_idle_cb, rt, NULL);
1649 /* keep the same priority as before */
1650 DEBUG_2("redraw priority: no change %.2f %%", percent);
1655 static gboolean rt_queue_draw_idle_cb(gpointer data)
1657 RendererTiles *rt = data;
1658 PixbufRenderer *pr = rt->pr;
1663 if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
1664 (!rt->draw_queue && !rt->draw_queue_2pass) ||
1667 pr_render_complete_signal(pr);
1669 rt->draw_idle_id = 0;
1675 qd = rt->draw_queue->data;
1676 fast = (pr->zoom_2pass && ((pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0) || pr->post_process_slow));
1682 /* still loading, wait till done (also drops the higher priority) */
1684 return rt_queue_schedule_next_draw(rt, FALSE);
1687 qd = rt->draw_queue_2pass->data;
1691 if (gtk_widget_get_realized(GTK_WIDGET(pr)))
1693 if (rt_tile_is_visible(rt, qd->it))
1695 rt_tile_expose(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1697 else if (qd->new_data)
1699 /* if new pixel data, and we already have a pixmap, update the tile */
1700 qd->it->blank = FALSE;
1701 if (qd->it->surface && qd->it->render_done == TILE_RENDER_ALL)
1703 rt_tile_render(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1711 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
1716 rt_queue_merge(qd->it->qd2, qd);
1722 rt->draw_queue_2pass = g_list_append(rt->draw_queue_2pass, qd);
1733 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
1737 if (!rt->draw_queue && !rt->draw_queue_2pass)
1739 pr_render_complete_signal(pr);
1741 rt->draw_idle_id = 0;
1745 return rt_queue_schedule_next_draw(rt, FALSE);
1748 static void rt_queue_list_free(GList *list)
1768 static void rt_queue_clear(RendererTiles *rt)
1770 rt_queue_list_free(rt->draw_queue);
1771 rt->draw_queue = NULL;
1773 rt_queue_list_free(rt->draw_queue_2pass);
1774 rt->draw_queue_2pass = NULL;
1776 if (rt->draw_idle_id)
1778 g_source_remove(rt->draw_idle_id);
1779 rt->draw_idle_id = 0;
1784 static void rt_queue_merge(QueueData *parent, QueueData *qd)
1786 if (parent->x + parent->w < qd->x + qd->w)
1788 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
1790 if (parent->x > qd->x)
1792 parent->w += parent->x - qd->x;
1796 if (parent->y + parent->h < qd->y + qd->h)
1798 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
1800 if (parent->y > qd->y)
1802 parent->h += parent->y - qd->y;
1806 parent->new_data |= qd->new_data;
1809 static gboolean rt_clamp_to_visible(RendererTiles *rt, gint *x, gint *y, gint *w, gint *h)
1811 PixbufRenderer *pr = rt->pr;
1818 vh = pr->vis_height;
1823 if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
1826 nx = CLAMP(*x, vx, vx + vw);
1827 nw = CLAMP(*w - (nx - *x), 1, vw);
1829 ny = CLAMP(*y, vy, vy + vh);
1830 nh = CLAMP(*h - (ny - *y), 1, vh);
1840 static gboolean rt_queue_to_tiles(RendererTiles *rt, gint x, gint y, gint w, gint h,
1841 gboolean clamp, ImageRenderType render,
1842 gboolean new_data, gboolean only_existing)
1844 PixbufRenderer *pr = rt->pr;
1849 if (clamp && !rt_clamp_to_visible(rt, &x, &y, &w, &h)) return FALSE;
1851 x1 = ROUND_DOWN(x, rt->tile_width);
1852 x2 = ROUND_UP(x + w, rt->tile_width);
1854 y1 = ROUND_DOWN(y, rt->tile_height);
1855 y2 = ROUND_UP(y + h, rt->tile_height);
1857 for (j = y1; j <= y2; j += rt->tile_height)
1859 for (i = x1; i <= x2; i += rt->tile_width)
1863 it = rt_tile_get(rt, i, j,
1865 (i + rt->tile_width < rt->x_scroll ||
1866 i > rt->x_scroll + pr->vis_width ||
1867 j + rt->tile_height < rt->y_scroll ||
1868 j > rt->y_scroll + pr->vis_height)));
1873 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
1874 (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
1876 it->render_todo = render;
1879 qd = g_new(QueueData, 1);
1881 qd->new_data = new_data;
1891 qd->w = x + w - i - qd->x;
1892 if (qd->x + qd->w > rt->tile_width) qd->w = rt->tile_width - qd->x;
1902 qd->h = y + h - j - qd->y;
1903 if (qd->y + qd->h > rt->tile_height) qd->h = rt->tile_height - qd->y;
1905 if (qd->w < 1 || qd->h < 1)
1911 rt_queue_merge(it->qd, qd);
1917 rt->draw_queue = g_list_append(rt->draw_queue, qd);
1926 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
1927 gboolean clamp, ImageRenderType render,
1928 gboolean new_data, gboolean only_existing)
1930 PixbufRenderer *pr = rt->pr;
1935 nx = CLAMP(x, 0, pr->width - 1);
1936 ny = CLAMP(y, 0, pr->height - 1);
1939 w = CLAMP(w, 0, pr->width - nx);
1940 h = CLAMP(h, 0, pr->height - ny);
1941 if (w < 1 || h < 1) return;
1943 if (rt_queue_to_tiles(rt, nx, ny, w, h, clamp, render, new_data, only_existing) &&
1944 ((!rt->draw_queue && !rt->draw_queue_2pass) || !rt->draw_idle_id))
1946 if (rt->draw_idle_id)
1948 g_source_remove(rt->draw_idle_id);
1949 rt->draw_idle_id = 0;
1951 rt_queue_schedule_next_draw(rt, TRUE);
1955 static void rt_scroll(void *renderer, gint x_off, gint y_off)
1957 RendererTiles *rt = (RendererTiles *) renderer;
1958 PixbufRenderer *pr = rt->pr;
1961 if (rt->stereo_mode & PR_STEREO_MIRROR) x_off = -x_off;
1962 if (rt->stereo_mode & PR_STEREO_FLIP) y_off = -y_off;
1964 gint w = pr->vis_width - abs(x_off);
1965 gint h = pr->vis_height - abs(y_off);
1969 /* scrolled completely to new material */
1970 rt_queue(rt, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1980 cairo_surface_t *surface;
2004 #if GTK_CHECK_VERSION(3,0,0)
2005 cr = cairo_create(rt->surface);
2006 surface = rt->surface;
2008 box = GTK_WIDGET(pr);
2009 window = gtk_widget_get_window(box);
2011 cr = gdk_cairo_create(window);
2012 surface = cairo_get_target(cr);
2014 /* clipping restricts the intermediate surface's size, so it's a good idea
2016 cairo_rectangle(cr, x1 + pr->x_offset + rt->stereo_off_x, y1 + pr->y_offset + rt->stereo_off_y, w, h);
2018 /* Now push a group to change the target */
2019 cairo_push_group (cr);
2020 cairo_set_source_surface(cr, surface, x1 - x2, y1 - y2);
2022 /* Now copy the intermediate target back */
2023 cairo_pop_group_to_source(cr);
2027 rt_overlay_queue_all(rt, x2, y2, x1, y1);
2029 w = pr->vis_width - w;
2030 h = pr->vis_height - h;
2035 x_off > 0 ? rt->x_scroll + (pr->vis_width - w) : rt->x_scroll, rt->y_scroll,
2036 w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2040 /* FIXME, to optimize this, remove overlap */
2042 rt->x_scroll, y_off > 0 ? rt->y_scroll + (pr->vis_height - h) : rt->y_scroll,
2043 pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
2048 static void renderer_area_changed(void *renderer, gint src_x, gint src_y, gint src_w, gint src_h)
2050 RendererTiles *rt = (RendererTiles *)renderer;
2051 PixbufRenderer *pr = rt->pr;
2052 gint x, y, width, height, x1, y1, x2, y2;
2054 gint orientation = rt_get_orientation(rt);
2055 pr_coords_map_orientation_reverse(orientation,
2056 src_x - GET_RIGHT_PIXBUF_OFFSET(rt), src_y,
2057 pr->image_width, pr->image_height,
2062 if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
2064 /* increase region when using a zoom quality that may access surrounding pixels */
2069 x1 = (gint)floor((gdouble)x * pr->scale);
2070 y1 = (gint)floor((gdouble)y * pr->scale * pr->aspect_ratio);
2071 x2 = (gint)ceil((gdouble)(x + width) * pr->scale);
2072 y2 = (gint)ceil((gdouble)(y + height) * pr->scale * pr->aspect_ratio);
2074 rt_queue(rt, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
2077 static void renderer_redraw(RendererTiles *rt, gint x, gint y, gint w, gint h,
2078 gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing)
2080 PixbufRenderer *pr = rt->pr;
2082 x -= rt->stereo_off_x;
2083 y -= rt->stereo_off_y;
2085 rt_border_draw(rt, x, y, w, h);
2087 x = MAX(0, x - pr->x_offset + pr->x_scroll);
2088 y = MAX(0, y - pr->y_offset + pr->y_scroll);
2092 MIN(w, pr->width - x),
2093 MIN(h, pr->height - y),
2094 clamp, render, new_data, only_existing);
2097 static void renderer_update_pixbuf(void *renderer, gboolean lazy)
2099 rt_queue_clear((RendererTiles *)renderer);
2102 static void renderer_update_zoom(void *renderer, gboolean lazy)
2104 RendererTiles *rt = (RendererTiles *)renderer;
2105 PixbufRenderer *pr = rt->pr;
2107 rt_tile_invalidate_all((RendererTiles *)renderer);
2110 renderer_redraw(renderer, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, TRUE, FALSE);
2112 rt_border_clear(rt);
2115 static void renderer_invalidate_region(void *renderer, gint x, gint y, gint w, gint h)
2117 rt_tile_invalidate_region((RendererTiles *)renderer, x, y, w, h);
2120 static void renderer_update_viewport(void *renderer)
2122 RendererTiles *rt = (RendererTiles *)renderer;
2124 rt->stereo_off_x = 0;
2125 rt->stereo_off_y = 0;
2127 if (rt->stereo_mode & PR_STEREO_RIGHT)
2129 if (rt->stereo_mode & PR_STEREO_HORIZ)
2131 rt->stereo_off_x = rt->pr->viewport_width;
2133 else if (rt->stereo_mode & PR_STEREO_VERT)
2135 rt->stereo_off_y = rt->pr->viewport_height;
2137 else if (rt->stereo_mode & PR_STEREO_FIXED)
2139 rt->stereo_off_x = rt->pr->stereo_fixed_x_right;
2140 rt->stereo_off_y = rt->pr->stereo_fixed_y_right;
2145 if (rt->stereo_mode & PR_STEREO_FIXED)
2147 rt->stereo_off_x = rt->pr->stereo_fixed_x_left;
2148 rt->stereo_off_y = rt->pr->stereo_fixed_y_left;
2151 DEBUG_1("update size: %p %d %d %d %d", rt, rt->stereo_off_x, rt->stereo_off_y, rt->pr->viewport_width, rt->pr->viewport_height);
2153 rt_overlay_update_sizes(rt);
2154 rt_border_clear(rt);
2157 static void renderer_stereo_set(void *renderer, gint stereo_mode)
2159 RendererTiles *rt = (RendererTiles *)renderer;
2161 rt->stereo_mode = stereo_mode;
2164 static void renderer_free(void *renderer)
2166 RendererTiles *rt = (RendererTiles *)renderer;
2168 rt_tile_free_all(rt);
2169 if (rt->spare_tile) g_object_unref(rt->spare_tile);
2170 if (rt->overlay_buffer) g_object_unref(rt->overlay_buffer);
2171 rt_overlay_list_clear(rt);
2172 /* disconnect "hierarchy-changed" */
2173 g_signal_handlers_disconnect_matched(G_OBJECT(rt->pr), G_SIGNAL_MATCH_DATA,
2178 #if GTK_CHECK_VERSION(3,0,0)
2179 static gboolean rt_realize_cb(GtkWidget *widget, gpointer data)
2181 RendererTiles *rt = (RendererTiles *)data;
2186 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));
2188 cr = cairo_create(rt->surface);
2189 cairo_set_source_rgb(cr, (gdouble)options->image.border_color.red /65535, (gdouble)options->image.border_color.green / 65535, (gdouble)options->image.border_color.blue / 65535);
2197 static gboolean rt_size_allocate_cb(GtkWidget *widget, GdkRectangle *allocation, gpointer data)
2199 RendererTiles *rt = data;
2201 cairo_surface_t *old_surface;
2203 if (gtk_widget_get_realized(GTK_WIDGET(rt->pr)))
2205 old_surface = rt->surface;
2206 rt->surface = gdk_window_create_similar_surface(gtk_widget_get_window(widget), CAIRO_CONTENT_COLOR, allocation->width, allocation->height);
2208 cr = cairo_create(rt->surface);
2210 cairo_set_source_rgb(cr, (gdouble)options->image.border_color.red / 65535, (gdouble)options->image.border_color.green / 65535, (gdouble)options->image.border_color.blue / 65535);
2212 cairo_set_source_surface(cr, old_surface, 0, 0);
2215 cairo_surface_destroy(old_surface);
2217 renderer_redraw(rt, allocation->x, allocation->y, allocation->width, allocation->height, FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2223 static gboolean rt_draw_cb(GtkWidget *widget, cairo_t *cr, gpointer data)
2225 RendererTiles *rt = (RendererTiles *)data;
2229 cairo_set_source_surface (cr, rt->surface, 0, 0);
2232 work = rt->overlay_list;
2236 gint px, py, pw, ph;
2237 pw = gdk_pixbuf_get_width(od->pixbuf);
2238 ph = gdk_pixbuf_get_height(od->pixbuf);
2242 if (od->flags & OVL_RELATIVE)
2244 if (px < 0) px = rt->pr->viewport_width - pw + px;
2245 if (py < 0) py = rt->pr->viewport_height - ph + py;
2248 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px, py);
2257 static gboolean rt_expose_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
2259 RendererTiles *rt = (RendererTiles *)data;
2260 if (gtk_widget_is_drawable(widget))
2262 if (gtk_widget_get_has_window(widget))
2264 if (event->window != gtk_widget_get_window(widget))
2268 gdk_window_get_position(event->window, &area.x, &area.y);
2269 area.x += event->area.x;
2270 area.y += event->area.y;
2271 area.width = event->area.width;
2272 area.height = event->area.height;
2273 renderer_redraw(rt, area.x, area.y, area.width, area.height,
2274 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2279 renderer_redraw(rt, event->area.x, event->area.y, event->area.width, event->area.height,
2280 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2290 RendererFuncs *renderer_tiles_new(PixbufRenderer *pr)
2292 RendererTiles *rt = g_new0(RendererTiles, 1);
2296 rt->f.area_changed = renderer_area_changed;
2297 rt->f.update_pixbuf = renderer_update_pixbuf;
2298 rt->f.free = renderer_free;
2299 rt->f.update_zoom = renderer_update_zoom;
2300 rt->f.invalidate_region = renderer_invalidate_region;
2301 rt->f.scroll = rt_scroll;
2302 rt->f.update_viewport = renderer_update_viewport;
2305 rt->f.overlay_add = renderer_tiles_overlay_add;
2306 rt->f.overlay_set = renderer_tiles_overlay_set;
2307 rt->f.overlay_get = renderer_tiles_overlay_get;
2309 rt->f.stereo_set = renderer_stereo_set;
2311 rt->tile_width = PR_TILE_SIZE;
2312 rt->tile_height = PR_TILE_SIZE;
2315 rt->tile_cache_size = 0;
2317 rt->tile_cache_max = PR_CACHE_SIZE_DEFAULT;
2319 rt->draw_idle_id = 0;
2321 rt->stereo_mode = 0;
2322 rt->stereo_off_x = 0;
2323 rt->stereo_off_y = 0;
2325 #if GTK_CHECK_VERSION(3, 10, 0)
2326 rt->hidpi_scale = gtk_widget_get_scale_factor(GTK_WIDGET(rt->pr));
2328 rt->hidpi_scale = 1;
2331 g_signal_connect(G_OBJECT(pr), "hierarchy-changed",
2332 G_CALLBACK(rt_hierarchy_changed_cb), rt);
2334 #if GTK_CHECK_VERSION(3,0,0)
2335 g_signal_connect(G_OBJECT(pr), "draw",
2336 G_CALLBACK(rt_draw_cb), rt);
2337 g_signal_connect(G_OBJECT(pr), "realize", G_CALLBACK(rt_realize_cb), rt);
2338 g_signal_connect(G_OBJECT(pr), "size-allocate", G_CALLBACK(rt_size_allocate_cb), rt);
2340 g_signal_connect(G_OBJECT(pr), "expose_event",
2341 G_CALLBACK(rt_expose_cb), rt);
2344 return (RendererFuncs *) rt;
2348 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */