4 * Copyright (C) 2008 - 2012 The Geeqie Team
7 * Author: Vladimir Nadvornik
9 * This software is released under the GNU General Public License (GNU GPL).
10 * Please read the included file COPYING for more information.
11 * This software comes with no warranty of any kind, use at your own risk!
20 #include "pixbuf-renderer.h"
21 #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"
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
50 } ExifOrientationType;
54 /* size to use when breaking up image pane for rendering */
55 #define PR_TILE_SIZE 128
57 typedef struct _ImageTile ImageTile;
58 typedef struct _QueueData QueueData;
62 cairo_surface_t *surface; /* off screen buffer */
63 GdkPixbuf *pixbuf; /* pixbuf area for zooming */
64 gint x; /* x offset into image */
65 gint y; /* y offset into image */
66 gint w; /* width that is visible (may be less if at edge of image) */
67 gint h; /* height '' */
71 /* render_todo: (explanation)
73 AREA render area of tile, usually only used when loading an image
74 note: will jump to an ALL if render_done is not ALL.
75 ALL render entire tile, if never done before w/ ALL, for expose events *only*
78 ImageRenderType render_todo; /* what to do (see above) */
79 ImageRenderType render_done; /* highest that has been done before on tile */
84 guint size; /* est. memory used by pixmap and pixbuf */
97 typedef struct _OverlayData OverlayData;
108 OverlayRendererFlags flags;
111 typedef struct _RendererTiles RendererTiles;
113 struct _RendererTiles
118 gint tile_cache_max; /* max mb to use for offscreen buffer */
122 gint tile_cols; /* count of tile columns */
123 GList *tiles; /* list of buffer tiles */
124 gint tile_cache_size; /* allocated size of pixmaps/pixbufs */
125 GList *draw_queue; /* list of areas to redraw */
126 GList *draw_queue_2pass;/* list when 2 pass is enabled */
129 cairo_surface_t *overlay_buffer;
131 guint draw_idle_id; /* event source id */
133 GdkPixbuf *spare_tile;
139 gint x_scroll; /* allow local adjustment and mirroring */
146 static void rt_border_draw(RendererTiles *rt, gint x, gint y, gint w, gint h);
147 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h, ImageTile *it);
150 static void rt_tile_free_all(RendererTiles *rt);
151 static void rt_tile_invalidate_region(RendererTiles *rt, gint x, gint y, gint w, gint h);
152 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it);
153 static void rt_queue_clear(RendererTiles *rt);
154 static void rt_queue_merge(QueueData *parent, QueueData *qd);
155 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
156 gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing);
158 static void rt_hierarchy_changed_cb(GtkWidget *widget, GtkWidget *previous_toplevel, gpointer data);
159 static gint rt_queue_draw_idle_cb(gpointer data);
161 #define GET_RIGHT_PIXBUF_OFFSET(rt) \
162 (( (rt->stereo_mode & PR_STEREO_RIGHT) && !(rt->stereo_mode & PR_STEREO_SWAP)) || \
163 (!(rt->stereo_mode & PR_STEREO_RIGHT) && (rt->stereo_mode & PR_STEREO_SWAP)) ? \
164 rt->pr->stereo_pixbuf_offset_right : rt->pr->stereo_pixbuf_offset_left )
166 #define GET_LEFT_PIXBUF_OFFSET(rt) \
167 ((!(rt->stereo_mode & PR_STEREO_RIGHT) && !(rt->stereo_mode & PR_STEREO_SWAP)) || \
168 ( (rt->stereo_mode & PR_STEREO_RIGHT) && (rt->stereo_mode & PR_STEREO_SWAP)) ? \
169 rt->pr->stereo_pixbuf_offset_right : rt->pr->stereo_pixbuf_offset_left )
172 static void rt_sync_scroll(RendererTiles *rt)
174 PixbufRenderer *pr = rt->pr;
176 rt->x_scroll = (rt->stereo_mode & PR_STEREO_MIRROR) ?
177 pr->width - pr->vis_width - pr->x_scroll
180 rt->y_scroll = (rt->stereo_mode & PR_STEREO_FLIP) ?
181 pr->height - pr->vis_height - pr->y_scroll
186 *-------------------------------------------------------------------
188 *-------------------------------------------------------------------
191 static void rt_border_draw(RendererTiles *rt, gint x, gint y, gint w, gint h)
193 PixbufRenderer *pr = rt->pr;
199 box = GTK_WIDGET(pr);
200 window = gtk_widget_get_window(box);
204 cr = gdk_cairo_create(window);
207 if (!pr->pixbuf && !pr->source_tiles_enabled)
209 if (pr_clip_region(x, y, w, h,
211 pr->viewport_width, pr->viewport_height,
214 cairo_set_source_rgb(cr, 0, 0, 0);
215 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
218 rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
223 if (pr->vis_width < pr->viewport_width)
225 if (pr->x_offset > 0 &&
226 pr_clip_region(x, y, w, h,
228 pr->x_offset, pr->viewport_height,
231 cairo_set_source_rgb(cr, 0, 0, 0);
232 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
234 rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
236 if (pr->viewport_width - pr->vis_width - pr->x_offset > 0 &&
237 pr_clip_region(x, y, w, h,
238 pr->x_offset + pr->vis_width, 0,
239 pr->viewport_width - pr->vis_width - pr->x_offset, pr->viewport_height,
242 cairo_set_source_rgb(cr, 0, 0, 0);
243 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
245 rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
248 if (pr->vis_height < pr->viewport_height)
250 if (pr->y_offset > 0 &&
251 pr_clip_region(x, y, w, h,
253 pr->vis_width, pr->y_offset,
256 cairo_set_source_rgb(cr, 0, 0, 0);
257 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
259 rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
261 if (pr->viewport_height - pr->vis_height - pr->y_offset > 0 &&
262 pr_clip_region(x, y, w, h,
263 pr->x_offset, pr->y_offset + pr->vis_height,
264 pr->vis_width, pr->viewport_height - pr->vis_height - pr->y_offset,
267 cairo_set_source_rgb(cr, 0, 0, 0);
268 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
270 rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
276 static void rt_border_clear(RendererTiles *rt)
278 PixbufRenderer *pr = rt->pr;
279 rt_border_draw(rt, 0, 0, pr->viewport_width, pr->viewport_height);
284 *-------------------------------------------------------------------
286 *-------------------------------------------------------------------
289 static ImageTile *rt_tile_new(gint x, gint y, gint width, gint height)
293 it = g_new0(ImageTile, 1);
300 it->render_done = TILE_RENDER_NONE;
305 static void rt_tile_free(ImageTile *it)
309 if (it->pixbuf) g_object_unref(it->pixbuf);
310 if (it->surface) cairo_surface_destroy(it->surface);
315 static void rt_tile_free_all(RendererTiles *rt)
330 g_list_free(rt->tiles);
332 rt->tile_cache_size = 0;
335 static ImageTile *rt_tile_add(RendererTiles *rt, gint x, gint y)
337 PixbufRenderer *pr = rt->pr;
340 it = rt_tile_new(x, y, rt->tile_width, rt->tile_height);
342 if (it->x + it->w > pr->width) it->w = pr->width - it->x;
343 if (it->y + it->h > pr->height) it->h = pr->height - it->y;
345 rt->tiles = g_list_prepend(rt->tiles, it);
346 rt->tile_cache_size += it->size;
351 static void rt_tile_remove(RendererTiles *rt, ImageTile *it)
355 QueueData *qd = it->qd;
358 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
364 QueueData *qd = it->qd2;
367 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
371 rt->tiles = g_list_remove(rt->tiles, it);
372 rt->tile_cache_size -= it->size;
377 static void rt_tile_free_space(RendererTiles *rt, guint space, ImageTile *it)
379 PixbufRenderer *pr = rt->pr;
383 work = g_list_last(rt->tiles);
385 if (pr->source_tiles_enabled && pr->scale < 1.0)
389 tiles = (pr->vis_width / rt->tile_width + 1) * (pr->vis_height / rt->tile_height + 1);
390 tile_max = MAX(tiles * rt->tile_width * rt->tile_height * 3,
391 (gint)((gdouble)rt->tile_cache_max * 1048576.0 * pr->scale));
395 tile_max = rt->tile_cache_max * 1048576;
398 while (work && rt->tile_cache_size + space > tile_max)
405 ((!needle->qd && !needle->qd2) || !rt_tile_is_visible(rt, needle))) rt_tile_remove(rt, needle);
409 static void rt_tile_invalidate_all(RendererTiles *rt)
411 PixbufRenderer *pr = rt->pr;
422 it->render_done = TILE_RENDER_NONE;
423 it->render_todo = TILE_RENDER_ALL;
426 it->w = MIN(rt->tile_width, pr->width - it->x);
427 it->h = MIN(rt->tile_height, pr->height - it->y);
431 static void rt_tile_invalidate_region(RendererTiles *rt, gint x, gint y, gint w, gint h)
437 x1 = ROUND_DOWN(x, rt->tile_width);
438 x2 = ROUND_UP(x + w, rt->tile_width);
440 y1 = ROUND_DOWN(y, rt->tile_height);
441 y2 = ROUND_UP(y + h, rt->tile_height);
451 if (it->x < x2 && it->x + it->w > x1 &&
452 it->y < y2 && it->y + it->h > y1)
454 it->render_done = TILE_RENDER_NONE;
455 it->render_todo = TILE_RENDER_ALL;
460 static ImageTile *rt_tile_get(RendererTiles *rt, gint x, gint y, gboolean only_existing)
470 if (it->x == x && it->y == y)
472 rt->tiles = g_list_delete_link(rt->tiles, work);
473 rt->tiles = g_list_prepend(rt->tiles, it);
480 if (only_existing) return NULL;
482 return rt_tile_add(rt, x, y);
485 static gint pixmap_calc_size(cairo_surface_t *surface)
489 // d = gdk_drawable_get_depth(pixmap);
490 // gdk_drawable_get_size(pixmap, &w, &h);
491 return PR_TILE_SIZE * PR_TILE_SIZE * 4 / 8;
494 static void rt_tile_prepare(RendererTiles *rt, ImageTile *it)
496 PixbufRenderer *pr = rt->pr;
499 cairo_surface_t *surface;
502 surface = gdk_window_create_similar_surface(gtk_widget_get_window((GtkWidget *)pr),
504 rt->tile_width, rt->tile_height);
506 size = pixmap_calc_size(surface);
507 rt_tile_free_space(rt, size, it);
509 it->surface = surface;
511 rt->tile_cache_size += size;
518 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, rt->tile_width, rt->tile_height);
520 size = gdk_pixbuf_get_rowstride(pixbuf) * rt->tile_height;
521 rt_tile_free_space(rt, size, it);
525 rt->tile_cache_size += size;
530 *-------------------------------------------------------------------
532 *-------------------------------------------------------------------
535 static void rt_overlay_get_position(RendererTiles *rt, OverlayData *od,
536 gint *x, gint *y, gint *w, gint *h)
538 PixbufRenderer *pr = rt->pr;
541 pw = gdk_pixbuf_get_width(od->pixbuf);
542 ph = gdk_pixbuf_get_height(od->pixbuf);
546 if (od->flags & OVL_RELATIVE)
548 if (px < 0) px = pr->viewport_width - pw + px;
549 if (py < 0) py = pr->viewport_height - ph + py;
558 static void rt_overlay_init_window(RendererTiles *rt, OverlayData *od)
560 PixbufRenderer *pr = rt->pr;
562 GdkWindowAttr attributes;
563 gint attributes_mask;
565 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
567 attributes.window_type = GDK_WINDOW_CHILD;
568 attributes.wclass = GDK_INPUT_OUTPUT;
569 attributes.width = pw;
570 attributes.height = ph;
571 attributes.event_mask = GDK_EXPOSURE_MASK;
574 od->window = gdk_window_new(gtk_widget_get_window(GTK_WIDGET(pr)), &attributes, attributes_mask);
575 gdk_window_set_user_data(od->window, pr);
576 gdk_window_move(od->window, px + rt->stereo_off_x, py + rt->stereo_off_y);
577 gdk_window_show(od->window);
580 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h,
583 PixbufRenderer *pr = rt->pr;
587 box = GTK_WIDGET(pr);
589 work = rt->overlay_list;
599 if (!od->window) rt_overlay_init_window(rt, od);
601 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
602 if (pr_clip_region(x, y, w, h, px, py, pw, ph, &rx, &ry, &rw, &rh))
604 if (!rt->overlay_buffer)
606 rt->overlay_buffer = gdk_window_create_similar_surface(gtk_widget_get_window((GtkWidget *)pr),
608 rt->tile_width, rt->tile_height);
615 cr = cairo_create(rt->overlay_buffer);
616 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);
617 cairo_rectangle(cr, 0, 0, rw, rh);
618 cairo_fill_preserve(cr);
620 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - rx, py - ry);
624 cr = gdk_cairo_create(od->window);
625 cairo_set_source_surface(cr, rt->overlay_buffer, rx - px, ry - py);
626 cairo_rectangle (cr, rx - px, ry - py, rw, rh);
632 /* no ImageTile means region may be larger than our scratch buffer */
635 for (sx = rx; sx < rx + rw; sx += rt->tile_width)
636 for (sy = ry; sy < ry + rh; sy += rt->tile_height)
641 sw = MIN(rx + rw - sx, rt->tile_width);
642 sh = MIN(ry + rh - sy, rt->tile_height);
644 cr = cairo_create(rt->overlay_buffer);
645 cairo_set_source_rgb(cr, 0, 0, 0);
646 cairo_rectangle(cr, 0, 0, sw, sh);
647 cairo_fill_preserve(cr);
649 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - sx, py - sy);
653 cr = gdk_cairo_create(od->window);
654 cairo_set_source_surface(cr, rt->overlay_buffer, sx - px, sy - py);
655 cairo_rectangle (cr, sx - px, sy - py, sw, sh);
664 static void rt_overlay_queue_draw(RendererTiles *rt, OverlayData *od, gint x1, gint y1, gint x2, gint y2)
666 PixbufRenderer *pr = rt->pr;
669 rt_overlay_get_position(rt, od, &x, &y, &w, &h);
677 rt_queue(rt, rt->x_scroll - pr->x_offset + x,
678 rt->y_scroll - pr->y_offset + y,
680 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
682 rt_border_draw(rt, x, y, w, h);
685 static void rt_overlay_queue_all(RendererTiles *rt, gint x1, gint y1, gint x2, gint y2)
689 work = rt->overlay_list;
692 OverlayData *od = work->data;
695 rt_overlay_queue_draw(rt, od, x1, y1, x2, y2);
699 static void rt_overlay_update_sizes(RendererTiles *rt)
703 work = rt->overlay_list;
706 OverlayData *od = work->data;
709 if (!od->window) rt_overlay_init_window(rt, od);
711 if (od->flags & OVL_RELATIVE)
715 rt_overlay_get_position(rt, od, &x, &y, &w, &h);
716 gdk_window_move_resize(od->window, x + rt->stereo_off_x, y + rt->stereo_off_y, w, h);
721 static OverlayData *rt_overlay_find(RendererTiles *rt, gint id)
725 work = rt->overlay_list;
728 OverlayData *od = work->data;
731 if (od->id == id) return od;
738 gint renderer_tiles_overlay_add(void *renderer, GdkPixbuf *pixbuf, gint x, gint y,
739 OverlayRendererFlags flags)
741 RendererTiles *rt = (RendererTiles *) renderer;
742 PixbufRenderer *pr = rt->pr;
746 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
747 g_return_val_if_fail(pixbuf != NULL, -1);
750 while (rt_overlay_find(rt, id)) id++;
752 od = g_new0(OverlayData, 1);
755 g_object_ref(G_OBJECT(od->pixbuf));
760 rt_overlay_init_window(rt, od);
762 rt->overlay_list = g_list_append(rt->overlay_list, od);
764 rt_overlay_queue_draw(rt, od, 0, 0, 0, 0);
769 static void rt_overlay_free(RendererTiles *rt, OverlayData *od)
771 rt->overlay_list = g_list_remove(rt->overlay_list, od);
773 if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
774 if (od->window) gdk_window_destroy(od->window);
777 if (!rt->overlay_list && rt->overlay_buffer)
779 cairo_surface_destroy(rt->overlay_buffer);
780 rt->overlay_buffer = NULL;
784 static void rt_overlay_list_clear(RendererTiles *rt)
786 while (rt->overlay_list)
790 od = rt->overlay_list->data;
791 rt_overlay_free(rt, od);
795 static void rt_overlay_list_reset_window(RendererTiles *rt)
799 if (rt->overlay_buffer) cairo_surface_destroy(rt->overlay_buffer);
800 rt->overlay_buffer = NULL;
802 work = rt->overlay_list;
805 OverlayData *od = work->data;
807 if (od->window) gdk_window_destroy(od->window);
812 void renderer_tiles_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint x, gint y)
814 RendererTiles *rt = (RendererTiles *) renderer;
815 PixbufRenderer *pr = rt->pr;
818 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
820 od = rt_overlay_find(rt, id);
827 g_object_ref(G_OBJECT(pixbuf));
828 g_object_unref(G_OBJECT(od->pixbuf));
834 if (!od->window) rt_overlay_init_window(rt, od);
836 rt_overlay_queue_draw(rt, od, 0, 0, 0, 0);
837 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
838 gdk_window_move_resize(od->window, px + rt->stereo_off_x, py + rt->stereo_off_y, pw, ph);
842 rt_overlay_queue_draw(rt, od, 0, 0, 0, 0);
843 rt_overlay_free(rt, od);
847 gboolean renderer_tiles_overlay_get(void *renderer, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
849 RendererTiles *rt = (RendererTiles *) renderer;
850 PixbufRenderer *pr = rt->pr;
853 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
855 od = rt_overlay_find(rt, id);
856 if (!od) return FALSE;
858 if (pixbuf) *pixbuf = od->pixbuf;
865 static void rt_hierarchy_changed_cb(GtkWidget *widget, GtkWidget *previous_toplevel, gpointer data)
867 RendererTiles *rt = data;
868 rt_overlay_list_reset_window(rt);
872 *-------------------------------------------------------------------
874 *-------------------------------------------------------------------
877 static GdkPixbuf *rt_get_spare_tile(RendererTiles *rt)
879 if (!rt->spare_tile) rt->spare_tile = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, rt->tile_width, rt->tile_height);
880 return rt->spare_tile;
883 #define COLOR_BYTES 3 /* rgb */
885 static void rt_tile_rotate_90_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
887 GdkPixbuf *src = *tile;
890 guchar *s_pix, *d_pix;
892 guchar *ip, *spi, *dpi;
894 gint tw = rt->tile_width;
896 srs = gdk_pixbuf_get_rowstride(src);
897 s_pix = gdk_pixbuf_get_pixels(src);
898 spi = s_pix + (x * COLOR_BYTES);
900 dest = rt_get_spare_tile(rt);
901 drs = gdk_pixbuf_get_rowstride(dest);
902 d_pix = gdk_pixbuf_get_pixels(dest);
903 dpi = d_pix + (tw - 1) * COLOR_BYTES;
905 for (i = y; i < y + h; i++)
907 sp = spi + (i * srs);
908 ip = dpi - (i * COLOR_BYTES);
909 for (j = x; j < x + w; j++)
912 memcpy(dp, sp, COLOR_BYTES);
917 rt->spare_tile = src;
921 static void rt_tile_rotate_90_counter_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
923 GdkPixbuf *src = *tile;
926 guchar *s_pix, *d_pix;
928 guchar *ip, *spi, *dpi;
930 gint th = rt->tile_height;
932 srs = gdk_pixbuf_get_rowstride(src);
933 s_pix = gdk_pixbuf_get_pixels(src);
934 spi = s_pix + (x * COLOR_BYTES);
936 dest = rt_get_spare_tile(rt);
937 drs = gdk_pixbuf_get_rowstride(dest);
938 d_pix = gdk_pixbuf_get_pixels(dest);
939 dpi = d_pix + (th - 1) * drs;
941 for (i = y; i < y + h; i++)
943 sp = spi + (i * srs);
944 ip = dpi + (i * COLOR_BYTES);
945 for (j = x; j < x + w; j++)
948 memcpy(dp, sp, COLOR_BYTES);
953 rt->spare_tile = src;
957 static void rt_tile_mirror_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
959 GdkPixbuf *src = *tile;
962 guchar *s_pix, *d_pix;
967 gint tw = rt->tile_width;
969 srs = gdk_pixbuf_get_rowstride(src);
970 s_pix = gdk_pixbuf_get_pixels(src);
971 spi = s_pix + (x * COLOR_BYTES);
973 dest = rt_get_spare_tile(rt);
974 drs = gdk_pixbuf_get_rowstride(dest);
975 d_pix = gdk_pixbuf_get_pixels(dest);
976 dpi = d_pix + (tw - x - 1) * COLOR_BYTES;
978 for (i = y; i < y + h; i++)
980 sp = spi + (i * srs);
981 dp = dpi + (i * drs);
982 for (j = 0; j < w; j++)
984 memcpy(dp, sp, COLOR_BYTES);
990 rt->spare_tile = src;
994 static void rt_tile_mirror_and_flip(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
996 GdkPixbuf *src = *tile;
999 guchar *s_pix, *d_pix;
1003 gint tw = rt->tile_width;
1004 gint th = rt->tile_height;
1006 srs = gdk_pixbuf_get_rowstride(src);
1007 s_pix = gdk_pixbuf_get_pixels(src);
1008 spi = s_pix + (x * COLOR_BYTES);
1010 dest = rt_get_spare_tile(rt);
1011 drs = gdk_pixbuf_get_rowstride(dest);
1012 d_pix = gdk_pixbuf_get_pixels(dest);
1013 dpi = d_pix + (th - 1) * drs + (tw - 1) * COLOR_BYTES;
1015 for (i = y; i < y + h; i++)
1017 sp = s_pix + (i * srs) + (x * COLOR_BYTES);
1018 dp = dpi - (i * drs) - (x * COLOR_BYTES);
1019 for (j = 0; j < w; j++)
1021 memcpy(dp, sp, COLOR_BYTES);
1027 rt->spare_tile = src;
1031 static void rt_tile_flip_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1033 GdkPixbuf *src = *tile;
1036 guchar *s_pix, *d_pix;
1040 gint th = rt->tile_height;
1042 srs = gdk_pixbuf_get_rowstride(src);
1043 s_pix = gdk_pixbuf_get_pixels(src);
1044 spi = s_pix + (x * COLOR_BYTES);
1046 dest = rt_get_spare_tile(rt);
1047 drs = gdk_pixbuf_get_rowstride(dest);
1048 d_pix = gdk_pixbuf_get_pixels(dest);
1049 dpi = d_pix + (th - 1) * drs + (x * COLOR_BYTES);
1051 for (i = y; i < y + h; i++)
1053 sp = spi + (i * srs);
1054 dp = dpi - (i * drs);
1055 memcpy(dp, sp, w * COLOR_BYTES);
1058 rt->spare_tile = src;
1062 static void rt_tile_apply_orientation(RendererTiles *rt, gint orientation, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h)
1064 switch (orientation)
1066 case EXIF_ORIENTATION_TOP_LEFT:
1067 /* normal -- nothing to do */
1069 case EXIF_ORIENTATION_TOP_RIGHT:
1072 rt_tile_mirror_only(rt, pixbuf, x, y, w, h);
1075 case EXIF_ORIENTATION_BOTTOM_RIGHT:
1078 rt_tile_mirror_and_flip(rt, pixbuf, x, y, w, h);
1081 case EXIF_ORIENTATION_BOTTOM_LEFT:
1084 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1087 case EXIF_ORIENTATION_LEFT_TOP:
1089 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1090 rt_tile_rotate_90_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1093 case EXIF_ORIENTATION_RIGHT_TOP:
1094 /* rotated -90 (270) */
1096 rt_tile_rotate_90_clockwise(rt, pixbuf, x, y, w, h);
1099 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1101 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1102 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1105 case EXIF_ORIENTATION_LEFT_BOTTOM:
1108 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, y, w, h);
1112 /* The other values are out of range */
1117 static gboolean rt_source_tile_render(RendererTiles *rt, ImageTile *it,
1118 gint x, gint y, gint w, gint h,
1119 gboolean new_data, gboolean fast)
1121 PixbufRenderer *pr = rt->pr;
1125 gboolean draw = FALSE;
1127 box = GTK_WIDGET(pr);
1129 if (pr->zoom == 1.0 || pr->scale == 1.0)
1131 list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE);
1136 gint rx, ry, rw, rh;
1141 if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1142 it->x + x, it->y + y, w, h,
1143 &rx, &ry, &rw, &rh))
1146 cr = cairo_create(it->surface);
1147 cairo_rectangle (cr, rx - it->x, ry - it->y, rw, rh);
1151 cairo_set_source_rgb(cr, 0, 0, 0);
1153 else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
1155 gdk_cairo_set_source_pixbuf(cr, st->pixbuf, -it->x + st->x, -it->y + st->y);
1164 gdouble scale_x, scale_y;
1165 gint sx, sy, sw, sh;
1167 if (pr->image_width == 0 || pr->image_height == 0) return FALSE;
1168 scale_x = (gdouble)pr->width / pr->image_width;
1169 scale_y = (gdouble)pr->height / pr->image_height;
1171 sx = (gdouble)(it->x + x) / scale_x;
1172 sy = (gdouble)(it->y + y) / scale_y;
1173 sw = (gdouble)w / scale_x;
1174 sh = (gdouble)h / scale_y;
1176 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1179 /* draws red over draw region, to check for leaks (regions not filled) */
1180 pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255);
1183 list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE);
1188 gint rx, ry, rw, rh;
1189 gint stx, sty, stw, sth;
1194 stx = floor((gdouble)st->x * scale_x);
1195 sty = floor((gdouble)st->y * scale_y);
1196 stw = ceil((gdouble)(st->x + pr->source_tile_width) * scale_x) - stx;
1197 sth = ceil((gdouble)(st->y + pr->source_tile_height) * scale_y) - sty;
1199 if (pr_clip_region(stx, sty, stw, sth,
1200 it->x + x, it->y + y, w, h,
1201 &rx, &ry, &rw, &rh))
1207 cr = cairo_create(it->surface);
1208 cairo_rectangle (cr, rx - st->x, ry - st->y, rw, rh);
1209 cairo_set_source_rgb(cr, 0, 0, 0);
1218 /* may need to use unfloored stx,sty values here */
1219 offset_x = (gdouble)(stx - it->x);
1220 offset_y = (gdouble)(sty - it->y);
1222 gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh,
1223 (gdouble) 0.0 + offset_x,
1224 (gdouble) 0.0 + offset_y,
1226 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1238 static void rt_tile_get_region(gboolean has_alpha,
1239 const GdkPixbuf *src, GdkPixbuf *dest,
1240 int pb_x, int pb_y, int pb_w, int pb_h,
1241 double offset_x, double offset_y, double scale_x, double scale_y,
1242 GdkInterpType interp_type,
1243 int check_x, int check_y)
1247 if (scale_x == 1.0 && scale_y == 1.0)
1249 gdk_pixbuf_copy_area(src,
1250 -offset_x + pb_x, -offset_y + pb_y,
1257 gdk_pixbuf_scale(src, dest,
1258 pb_x, pb_y, pb_w, pb_h,
1267 gdk_pixbuf_composite_color(src, dest,
1268 pb_x, pb_y, pb_w, pb_h,
1273 255, check_x, check_y,
1274 PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
1279 static gint rt_get_orientation(RendererTiles *rt)
1281 PixbufRenderer *pr = rt->pr;
1283 gint orientation = pr->orientation;
1284 static const gint mirror[] = {1, 2, 1, 4, 3, 6, 5, 8, 7};
1285 static const gint flip[] = {1, 4, 3, 2, 1, 8, 7, 6, 5};
1287 if (rt->stereo_mode & PR_STEREO_MIRROR) orientation = mirror[orientation];
1288 if (rt->stereo_mode & PR_STEREO_FLIP) orientation = flip[orientation];
1293 static void rt_tile_render(RendererTiles *rt, ImageTile *it,
1294 gint x, gint y, gint w, gint h,
1295 gboolean new_data, gboolean fast)
1297 PixbufRenderer *pr = rt->pr;
1300 gboolean draw = FALSE;
1301 gint orientation = rt_get_orientation(rt);
1303 if (it->render_todo == TILE_RENDER_NONE && it->surface && !new_data) return;
1305 if (it->render_done != TILE_RENDER_ALL)
1311 if (!fast) it->render_done = TILE_RENDER_ALL;
1313 else if (it->render_todo != TILE_RENDER_AREA)
1315 if (!fast) it->render_todo = TILE_RENDER_NONE;
1319 if (!fast) it->render_todo = TILE_RENDER_NONE;
1321 if (new_data) it->blank = FALSE;
1323 rt_tile_prepare(rt, it);
1324 has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
1326 box = GTK_WIDGET(pr);
1328 /* FIXME checker colors for alpha should be configurable,
1329 * also should be drawn for blank = TRUE
1334 /* no data, do fast rect fill */
1336 cr = cairo_create(it->surface);
1337 cairo_rectangle (cr, 0, 0, it->w, it->h);
1338 cairo_set_source_rgb(cr, 0, 0, 0);
1342 else if (pr->source_tiles_enabled)
1344 draw = rt_source_tile_render(rt, it, x, y, w, h, new_data, fast);
1346 else if ((pr->zoom == 1.0 || pr->scale == 1.0) &&
1347 pr->aspect_ratio == 1.0 &&
1349 orientation == EXIF_ORIENTATION_TOP_LEFT &&
1350 !(pr->func_post_process && !(pr->post_process_slow && fast)) &&
1351 !(rt->stereo_mode & PR_STEREO_ANAGLYPH))
1353 /* special case: faster, simple, scale 1.0, base orientation, no postprocessing */
1355 cr = cairo_create(it->surface);
1356 cairo_rectangle (cr, x, y, w, h);
1357 gdk_cairo_set_source_pixbuf(cr, pr->pixbuf, -it->x - GET_RIGHT_PIXBUF_OFFSET(rt), -it->y);
1363 gdouble scale_x, scale_y;
1364 gdouble src_x, src_y;
1368 if (pr->image_width == 0 || pr->image_height == 0) return;
1370 scale_x = (gdouble)pr->width / pr->image_width;
1371 scale_y = (gdouble)pr->height / pr->image_height;
1373 pr_tile_coords_map_orientation(orientation, it->x, it->y,
1374 pr->width, pr->height,
1375 rt->tile_width, rt->tile_height,
1377 pr_tile_region_map_orientation(orientation, x, y,
1378 rt->tile_width, rt->tile_height,
1383 switch (orientation)
1386 case EXIF_ORIENTATION_LEFT_TOP:
1387 case EXIF_ORIENTATION_RIGHT_TOP:
1388 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1389 case EXIF_ORIENTATION_LEFT_BOTTOM:
1399 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
1400 * small sizes for anything but GDK_INTERP_NEAREST
1402 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1404 rt_tile_get_region(has_alpha,
1405 pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
1406 (gdouble) 0.0 - src_x - GET_RIGHT_PIXBUF_OFFSET(rt) * scale_x,
1407 (gdouble) 0.0 - src_y,
1409 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1410 it->x + pb_x, it->y + pb_y);
1411 if (rt->stereo_mode & PR_STEREO_ANAGLYPH &&
1412 (pr->stereo_pixbuf_offset_right > 0 || pr->stereo_pixbuf_offset_left > 0))
1414 GdkPixbuf *right_pb = rt_get_spare_tile(rt);
1415 rt_tile_get_region(has_alpha,
1416 pr->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h,
1417 (gdouble) 0.0 - src_x - GET_LEFT_PIXBUF_OFFSET(rt) * scale_x,
1418 (gdouble) 0.0 - src_y,
1420 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1421 it->x + pb_x, it->y + pb_y);
1422 pr_create_anaglyph(rt->stereo_mode, it->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h);
1423 /* do not care about freeing spare_tile, it will be reused */
1425 rt_tile_apply_orientation(rt, orientation, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
1429 if (draw && it->pixbuf && !it->blank)
1433 if (pr->func_post_process && !(pr->post_process_slow && fast))
1434 pr->func_post_process(pr, &it->pixbuf, x, y, w, h, pr->post_process_user_data);
1436 cr = cairo_create(it->surface);
1437 cairo_rectangle (cr, x, y, w, h);
1438 gdk_cairo_set_source_pixbuf(cr, it->pixbuf, 0, 0);
1445 static void rt_tile_expose(RendererTiles *rt, ImageTile *it,
1446 gint x, gint y, gint w, gint h,
1447 gboolean new_data, gboolean fast)
1449 PixbufRenderer *pr = rt->pr;
1454 /* clamp to visible */
1455 if (it->x + x < rt->x_scroll)
1457 w -= rt->x_scroll - it->x - x;
1458 x = rt->x_scroll - it->x;
1460 if (it->x + x + w > rt->x_scroll + pr->vis_width)
1462 w = rt->x_scroll + pr->vis_width - it->x - x;
1465 if (it->y + y < rt->y_scroll)
1467 h -= rt->y_scroll - it->y - y;
1468 y = rt->y_scroll - it->y;
1470 if (it->y + y + h > rt->y_scroll + pr->vis_height)
1472 h = rt->y_scroll + pr->vis_height - it->y - y;
1476 rt_tile_render(rt, it, x, y, w, h, new_data, fast);
1478 box = GTK_WIDGET(pr);
1479 window = gtk_widget_get_window(box);
1481 cr = gdk_cairo_create(window);
1482 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);
1483 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);
1487 if (rt->overlay_list)
1489 rt_overlay_draw(rt, pr->x_offset + (it->x - rt->x_scroll) + x,
1490 pr->y_offset + (it->y - rt->y_scroll) + y,
1497 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it)
1499 PixbufRenderer *pr = rt->pr;
1500 return (it->x + it->w >= rt->x_scroll && it->x < rt->x_scroll + pr->vis_width &&
1501 it->y + it->h >= rt->y_scroll && it->y < rt->y_scroll + pr->vis_height);
1505 *-------------------------------------------------------------------
1507 *-------------------------------------------------------------------
1510 static gint rt_get_queued_area(GList *work)
1516 QueueData *qd = work->data;
1517 area += qd->w * qd->h;
1524 static gboolean rt_queue_schedule_next_draw(RendererTiles *rt, gboolean force_set)
1526 PixbufRenderer *pr = rt->pr;
1528 gint visible_area = pr->vis_width * pr->vis_height;
1533 DEBUG_2("redraw priority: 2pass");
1534 rt->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, rt_queue_draw_idle_cb, rt, NULL);
1538 if (visible_area == 0)
1545 percent = 100.0 * rt_get_queued_area(rt->draw_queue) / visible_area;
1550 /* we have enough data for starting intensive redrawing */
1551 DEBUG_2("redraw priority: high %.2f %%", percent);
1552 rt->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW, rt_queue_draw_idle_cb, rt, NULL);
1556 if (percent < 1.0 || force_set)
1558 /* queue is (almost) empty, wait 50 ms*/
1559 DEBUG_2("redraw priority: wait %.2f %%", percent);
1560 rt->draw_idle_id = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, 50, rt_queue_draw_idle_cb, rt, NULL);
1564 /* keep the same priority as before */
1565 DEBUG_2("redraw priority: no change %.2f %%", percent);
1570 static gboolean rt_queue_draw_idle_cb(gpointer data)
1572 RendererTiles *rt = data;
1573 PixbufRenderer *pr = rt->pr;
1578 if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
1579 (!rt->draw_queue && !rt->draw_queue_2pass) ||
1582 pr_render_complete_signal(pr);
1584 rt->draw_idle_id = 0;
1590 qd = rt->draw_queue->data;
1591 fast = (pr->zoom_2pass && ((pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0) || pr->post_process_slow));
1597 /* still loading, wait till done (also drops the higher priority) */
1599 return rt_queue_schedule_next_draw(rt, FALSE);
1602 qd = rt->draw_queue_2pass->data;
1606 if (gtk_widget_get_realized(GTK_WIDGET(pr)))
1608 if (rt_tile_is_visible(rt, qd->it))
1610 rt_tile_expose(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1612 else if (qd->new_data)
1614 /* if new pixel data, and we already have a pixmap, update the tile */
1615 qd->it->blank = FALSE;
1616 if (qd->it->surface && qd->it->render_done == TILE_RENDER_ALL)
1618 rt_tile_render(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1626 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
1631 rt_queue_merge(qd->it->qd2, qd);
1637 rt->draw_queue_2pass = g_list_append(rt->draw_queue_2pass, qd);
1648 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
1652 if (!rt->draw_queue && !rt->draw_queue_2pass)
1654 pr_render_complete_signal(pr);
1656 rt->draw_idle_id = 0;
1660 return rt_queue_schedule_next_draw(rt, FALSE);
1663 static void rt_queue_list_free(GList *list)
1683 static void rt_queue_clear(RendererTiles *rt)
1685 rt_queue_list_free(rt->draw_queue);
1686 rt->draw_queue = NULL;
1688 rt_queue_list_free(rt->draw_queue_2pass);
1689 rt->draw_queue_2pass = NULL;
1691 if (rt->draw_idle_id)
1693 g_source_remove(rt->draw_idle_id);
1694 rt->draw_idle_id = 0;
1699 static void rt_queue_merge(QueueData *parent, QueueData *qd)
1701 if (parent->x + parent->w < qd->x + qd->w)
1703 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
1705 if (parent->x > qd->x)
1707 parent->w += parent->x - qd->x;
1711 if (parent->y + parent->h < qd->y + qd->h)
1713 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
1715 if (parent->y > qd->y)
1717 parent->h += parent->y - qd->y;
1721 parent->new_data |= qd->new_data;
1724 static gboolean rt_clamp_to_visible(RendererTiles *rt, gint *x, gint *y, gint *w, gint *h)
1726 PixbufRenderer *pr = rt->pr;
1733 vh = pr->vis_height;
1738 if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
1741 nx = CLAMP(*x, vx, vx + vw);
1742 nw = CLAMP(*w - (nx - *x), 1, vw);
1744 ny = CLAMP(*y, vy, vy + vh);
1745 nh = CLAMP(*h - (ny - *y), 1, vh);
1755 static gboolean rt_queue_to_tiles(RendererTiles *rt, gint x, gint y, gint w, gint h,
1756 gboolean clamp, ImageRenderType render,
1757 gboolean new_data, gboolean only_existing)
1759 PixbufRenderer *pr = rt->pr;
1764 if (clamp && !rt_clamp_to_visible(rt, &x, &y, &w, &h)) return FALSE;
1766 x1 = ROUND_DOWN(x, rt->tile_width);
1767 x2 = ROUND_UP(x + w, rt->tile_width);
1769 y1 = ROUND_DOWN(y, rt->tile_height);
1770 y2 = ROUND_UP(y + h, rt->tile_height);
1772 for (j = y1; j <= y2; j += rt->tile_height)
1774 for (i = x1; i <= x2; i += rt->tile_width)
1778 it = rt_tile_get(rt, i, j,
1780 (i + rt->tile_width < rt->x_scroll ||
1781 i > rt->x_scroll + pr->vis_width ||
1782 j + rt->tile_height < rt->y_scroll ||
1783 j > rt->y_scroll + pr->vis_height)));
1788 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
1789 (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
1791 it->render_todo = render;
1794 qd = g_new(QueueData, 1);
1796 qd->new_data = new_data;
1806 qd->w = x + w - i - qd->x;
1807 if (qd->x + qd->w > rt->tile_width) qd->w = rt->tile_width - qd->x;
1817 qd->h = y + h - j - qd->y;
1818 if (qd->y + qd->h > rt->tile_height) qd->h = rt->tile_height - qd->y;
1820 if (qd->w < 1 || qd->h < 1)
1826 rt_queue_merge(it->qd, qd);
1832 rt->draw_queue = g_list_append(rt->draw_queue, qd);
1841 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
1842 gboolean clamp, ImageRenderType render,
1843 gboolean new_data, gboolean only_existing)
1845 PixbufRenderer *pr = rt->pr;
1850 nx = CLAMP(x, 0, pr->width - 1);
1851 ny = CLAMP(y, 0, pr->height - 1);
1854 w = CLAMP(w, 0, pr->width - nx);
1855 h = CLAMP(h, 0, pr->height - ny);
1856 if (w < 1 || h < 1) return;
1858 if (rt_queue_to_tiles(rt, nx, ny, w, h, clamp, render, new_data, only_existing) &&
1859 ((!rt->draw_queue && !rt->draw_queue_2pass) || !rt->draw_idle_id))
1861 if (rt->draw_idle_id)
1863 g_source_remove(rt->draw_idle_id);
1864 rt->draw_idle_id = 0;
1866 rt_queue_schedule_next_draw(rt, TRUE);
1870 static void rt_scroll(void *renderer, gint x_off, gint y_off)
1872 RendererTiles *rt = (RendererTiles *) renderer;
1873 PixbufRenderer *pr = rt->pr;
1876 if (rt->stereo_mode & PR_STEREO_MIRROR) x_off = -x_off;
1877 if (rt->stereo_mode & PR_STEREO_FLIP) y_off = -y_off;
1879 gint w = pr->vis_width - abs(x_off);
1880 gint h = pr->vis_height - abs(y_off);
1884 /* scrolled completely to new material */
1885 rt_queue(rt, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1895 cairo_surface_t *surface;
1919 box = GTK_WIDGET(pr);
1920 window = gtk_widget_get_window(box);
1922 cr = gdk_cairo_create(window);
1923 surface = cairo_get_target(cr);
1924 /* clipping restricts the intermediate surface's size, so it's a good idea
1926 cairo_rectangle(cr, x1 + pr->x_offset + rt->stereo_off_x, y1 + pr->y_offset + rt->stereo_off_y, w, h);
1928 /* Now push a group to change the target */
1929 cairo_push_group (cr);
1930 cairo_set_source_surface(cr, surface, x1 - x2, y1 - y2);
1932 /* Now copy the intermediate target back */
1933 cairo_pop_group_to_source(cr);
1937 rt_overlay_queue_all(rt, x2, y2, x1, y1);
1939 w = pr->vis_width - w;
1940 h = pr->vis_height - h;
1945 x_off > 0 ? rt->x_scroll + (pr->vis_width - w) : rt->x_scroll, rt->y_scroll,
1946 w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1950 /* FIXME, to optimize this, remove overlap */
1952 rt->x_scroll, y_off > 0 ? rt->y_scroll + (pr->vis_height - h) : rt->y_scroll,
1953 pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1958 static void renderer_area_changed(void *renderer, gint src_x, gint src_y, gint src_w, gint src_h)
1960 RendererTiles *rt = (RendererTiles *)renderer;
1961 PixbufRenderer *pr = rt->pr;
1962 gint x, y, width, height, x1, y1, x2, y2;
1964 gint orientation = rt_get_orientation(rt);
1965 pr_coords_map_orientation_reverse(orientation,
1966 src_x - GET_RIGHT_PIXBUF_OFFSET(rt), src_y,
1967 pr->image_width, pr->image_height,
1972 if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
1974 /* increase region when using a zoom quality that may access surrounding pixels */
1979 x1 = (gint)floor((gdouble)x * pr->scale);
1980 y1 = (gint)floor((gdouble)y * pr->scale * pr->aspect_ratio);
1981 x2 = (gint)ceil((gdouble)(x + width) * pr->scale);
1982 y2 = (gint)ceil((gdouble)(y + height) * pr->scale * pr->aspect_ratio);
1984 rt_queue(rt, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
1987 static void renderer_redraw(RendererTiles *rt, gint x, gint y, gint w, gint h,
1988 gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing)
1990 PixbufRenderer *pr = rt->pr;
1992 x -= rt->stereo_off_x;
1993 y -= rt->stereo_off_y;
1995 rt_border_draw(rt, x, y, w, h);
1997 x = MAX(0, x - pr->x_offset + pr->x_scroll);
1998 y = MAX(0, y - pr->y_offset + pr->y_scroll);
2002 MIN(w, pr->width - x),
2003 MIN(h, pr->height - y),
2004 clamp, render, new_data, only_existing);
2007 static void renderer_update_pixbuf(void *renderer, gboolean lazy)
2009 rt_queue_clear((RendererTiles *)renderer);
2012 static void renderer_update_zoom(void *renderer, gboolean lazy)
2014 RendererTiles *rt = (RendererTiles *)renderer;
2015 PixbufRenderer *pr = rt->pr;
2017 rt_tile_invalidate_all((RendererTiles *)renderer);
2020 renderer_redraw(renderer, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, TRUE, FALSE);
2022 rt_border_clear(rt);
2025 static void renderer_invalidate_region(void *renderer, gint x, gint y, gint w, gint h)
2027 rt_tile_invalidate_region((RendererTiles *)renderer, x, y, w, h);
2030 static void renderer_update_viewport(void *renderer)
2032 RendererTiles *rt = (RendererTiles *)renderer;
2034 rt->stereo_off_x = 0;
2035 rt->stereo_off_y = 0;
2037 if (rt->stereo_mode & PR_STEREO_RIGHT)
2039 if (rt->stereo_mode & PR_STEREO_HORIZ)
2041 rt->stereo_off_x = rt->pr->viewport_width;
2043 else if (rt->stereo_mode & PR_STEREO_VERT)
2045 rt->stereo_off_y = rt->pr->viewport_height;
2047 else if (rt->stereo_mode & PR_STEREO_FIXED)
2049 rt->stereo_off_x = rt->pr->stereo_fixed_x_right;
2050 rt->stereo_off_y = rt->pr->stereo_fixed_y_right;
2055 if (rt->stereo_mode & PR_STEREO_FIXED)
2057 rt->stereo_off_x = rt->pr->stereo_fixed_x_left;
2058 rt->stereo_off_y = rt->pr->stereo_fixed_y_left;
2061 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);
2063 rt_overlay_update_sizes(rt);
2064 rt_border_clear(rt);
2067 static void renderer_stereo_set(void *renderer, gint stereo_mode)
2069 RendererTiles *rt = (RendererTiles *)renderer;
2071 rt->stereo_mode = stereo_mode;
2074 static void renderer_free(void *renderer)
2076 RendererTiles *rt = (RendererTiles *)renderer;
2078 rt_tile_free_all(rt);
2079 if (rt->spare_tile) g_object_unref(rt->spare_tile);
2080 if (rt->overlay_buffer) g_object_unref(rt->overlay_buffer);
2081 rt_overlay_list_clear(rt);
2082 /* disconnect "hierarchy-changed" */
2083 g_signal_handlers_disconnect_matched(G_OBJECT(rt->pr), G_SIGNAL_MATCH_DATA,
2088 #if GTK_CHECK_VERSION(3,0,0)
2090 static gboolean rt_draw_cb(GtkWidget *widget, cairo_t *cr, gpointer data)
2092 RendererTiles *rt = (RendererTiles *)data;
2093 if (gtk_widget_is_drawable(widget))
2095 if (gtk_widget_get_has_window(widget))
2098 if (gdk_cairo_get_clip_rectangle(cr, &area))
2100 renderer_redraw(rt, area.x, area.y, area.width, area.height,
2101 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2110 static gboolean rt_expose_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
2112 RendererTiles *rt = (RendererTiles *)data;
2113 if (gtk_widget_is_drawable(widget))
2115 if (gtk_widget_get_has_window(widget))
2117 if (event->window != gtk_widget_get_window(widget))
2121 gdk_window_get_position(event->window, &area.x, &area.y);
2122 area.x += event->area.x;
2123 area.y += event->area.y;
2124 area.width = event->area.width;
2125 area.height = event->area.height;
2126 renderer_redraw(rt, area.x, area.y, area.width, area.height,
2127 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2132 renderer_redraw(rt, event->area.x, event->area.y, event->area.width, event->area.height,
2133 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2143 RendererFuncs *renderer_tiles_new(PixbufRenderer *pr)
2145 RendererTiles *rt = g_new0(RendererTiles, 1);
2149 rt->f.area_changed = renderer_area_changed;
2150 rt->f.update_pixbuf = renderer_update_pixbuf;
2151 rt->f.free = renderer_free;
2152 rt->f.update_zoom = renderer_update_zoom;
2153 rt->f.invalidate_region = renderer_invalidate_region;
2154 rt->f.scroll = rt_scroll;
2155 rt->f.update_viewport = renderer_update_viewport;
2158 rt->f.overlay_add = renderer_tiles_overlay_add;
2159 rt->f.overlay_set = renderer_tiles_overlay_set;
2160 rt->f.overlay_get = renderer_tiles_overlay_get;
2162 rt->f.stereo_set = renderer_stereo_set;
2164 rt->tile_width = PR_TILE_SIZE;
2165 rt->tile_height = PR_TILE_SIZE;
2168 rt->tile_cache_size = 0;
2170 rt->tile_cache_max = PR_CACHE_SIZE_DEFAULT;
2172 rt->draw_idle_id = 0;
2174 rt->stereo_mode = 0;
2175 rt->stereo_off_x = 0;
2176 rt->stereo_off_y = 0;
2178 g_signal_connect(G_OBJECT(pr), "hierarchy-changed",
2179 G_CALLBACK(rt_hierarchy_changed_cb), rt);
2181 #if GTK_CHECK_VERSION(3,0,0)
2182 g_signal_connect(G_OBJECT(pr), "draw",
2183 G_CALLBACK(rt_draw_cb), rt);
2185 g_signal_connect(G_OBJECT(pr), "expose_event",
2186 G_CALLBACK(rt_expose_cb), rt);
2188 return (RendererFuncs *) rt;
2192 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */