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 gdk_draw_rectangle(it->pixmap, box->style->black_gc, TRUE,
1343 0, 0, it->w, it->h);
1346 else if (pr->source_tiles_enabled)
1348 draw = rt_source_tile_render(rt, it, x, y, w, h, new_data, fast);
1350 else if ((pr->zoom == 1.0 || pr->scale == 1.0) &&
1351 pr->aspect_ratio == 1.0 &&
1353 orientation == EXIF_ORIENTATION_TOP_LEFT &&
1354 !(pr->func_post_process && !(pr->post_process_slow && fast)) &&
1355 !(rt->stereo_mode & PR_STEREO_ANAGLYPH))
1357 /* special case: faster, simple, scale 1.0, base orientation, no postprocessing */
1359 cr = cairo_create(it->surface);
1360 cairo_rectangle (cr, x, y, w, h);
1361 gdk_cairo_set_source_pixbuf(cr, pr->pixbuf, -it->x - GET_RIGHT_PIXBUF_OFFSET(rt), -it->y);
1367 gdouble scale_x, scale_y;
1368 gdouble src_x, src_y;
1372 if (pr->image_width == 0 || pr->image_height == 0) return;
1374 scale_x = (gdouble)pr->width / pr->image_width;
1375 scale_y = (gdouble)pr->height / pr->image_height;
1377 pr_tile_coords_map_orientation(orientation, it->x, it->y,
1378 pr->width, pr->height,
1379 rt->tile_width, rt->tile_height,
1381 pr_tile_region_map_orientation(orientation, x, y,
1382 rt->tile_width, rt->tile_height,
1387 switch (orientation)
1390 case EXIF_ORIENTATION_LEFT_TOP:
1391 case EXIF_ORIENTATION_RIGHT_TOP:
1392 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1393 case EXIF_ORIENTATION_LEFT_BOTTOM:
1403 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
1404 * small sizes for anything but GDK_INTERP_NEAREST
1406 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1408 rt_tile_get_region(has_alpha,
1409 pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
1410 (gdouble) 0.0 - src_x - GET_RIGHT_PIXBUF_OFFSET(rt) * scale_x,
1411 (gdouble) 0.0 - src_y,
1413 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1414 it->x + pb_x, it->y + pb_y);
1415 if (rt->stereo_mode & PR_STEREO_ANAGLYPH &&
1416 (pr->stereo_pixbuf_offset_right > 0 || pr->stereo_pixbuf_offset_left > 0))
1418 GdkPixbuf *right_pb = rt_get_spare_tile(rt);
1419 rt_tile_get_region(has_alpha,
1420 pr->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h,
1421 (gdouble) 0.0 - src_x - GET_LEFT_PIXBUF_OFFSET(rt) * scale_x,
1422 (gdouble) 0.0 - src_y,
1424 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1425 it->x + pb_x, it->y + pb_y);
1426 pr_create_anaglyph(rt->stereo_mode, it->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h);
1427 /* do not care about freeing spare_tile, it will be reused */
1429 rt_tile_apply_orientation(rt, orientation, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
1433 if (draw && it->pixbuf && !it->blank)
1437 if (pr->func_post_process && !(pr->post_process_slow && fast))
1438 pr->func_post_process(pr, &it->pixbuf, x, y, w, h, pr->post_process_user_data);
1440 cr = cairo_create(it->surface);
1441 cairo_rectangle (cr, x, y, w, h);
1442 gdk_cairo_set_source_pixbuf(cr, it->pixbuf, 0, 0);
1449 static void rt_tile_expose(RendererTiles *rt, ImageTile *it,
1450 gint x, gint y, gint w, gint h,
1451 gboolean new_data, gboolean fast)
1453 PixbufRenderer *pr = rt->pr;
1458 /* clamp to visible */
1459 if (it->x + x < rt->x_scroll)
1461 w -= rt->x_scroll - it->x - x;
1462 x = rt->x_scroll - it->x;
1464 if (it->x + x + w > rt->x_scroll + pr->vis_width)
1466 w = rt->x_scroll + pr->vis_width - it->x - x;
1469 if (it->y + y < rt->y_scroll)
1471 h -= rt->y_scroll - it->y - y;
1472 y = rt->y_scroll - it->y;
1474 if (it->y + y + h > rt->y_scroll + pr->vis_height)
1476 h = rt->y_scroll + pr->vis_height - it->y - y;
1480 rt_tile_render(rt, it, x, y, w, h, new_data, fast);
1482 box = GTK_WIDGET(pr);
1483 window = gtk_widget_get_window(box);
1485 cr = gdk_cairo_create(window);
1486 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);
1487 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);
1491 if (rt->overlay_list)
1493 rt_overlay_draw(rt, pr->x_offset + (it->x - rt->x_scroll) + x,
1494 pr->y_offset + (it->y - rt->y_scroll) + y,
1501 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it)
1503 PixbufRenderer *pr = rt->pr;
1504 return (it->x + it->w >= rt->x_scroll && it->x < rt->x_scroll + pr->vis_width &&
1505 it->y + it->h >= rt->y_scroll && it->y < rt->y_scroll + pr->vis_height);
1509 *-------------------------------------------------------------------
1511 *-------------------------------------------------------------------
1514 static gint rt_get_queued_area(GList *work)
1520 QueueData *qd = work->data;
1521 area += qd->w * qd->h;
1528 static gboolean rt_queue_schedule_next_draw(RendererTiles *rt, gboolean force_set)
1530 PixbufRenderer *pr = rt->pr;
1532 gint visible_area = pr->vis_width * pr->vis_height;
1537 DEBUG_2("redraw priority: 2pass");
1538 rt->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, rt_queue_draw_idle_cb, rt, NULL);
1542 if (visible_area == 0)
1549 percent = 100.0 * rt_get_queued_area(rt->draw_queue) / visible_area;
1554 /* we have enough data for starting intensive redrawing */
1555 DEBUG_2("redraw priority: high %.2f %%", percent);
1556 rt->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW, rt_queue_draw_idle_cb, rt, NULL);
1560 if (percent < 1.0 || force_set)
1562 /* queue is (almost) empty, wait 50 ms*/
1563 DEBUG_2("redraw priority: wait %.2f %%", percent);
1564 rt->draw_idle_id = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, 50, rt_queue_draw_idle_cb, rt, NULL);
1568 /* keep the same priority as before */
1569 DEBUG_2("redraw priority: no change %.2f %%", percent);
1574 static gboolean rt_queue_draw_idle_cb(gpointer data)
1576 RendererTiles *rt = data;
1577 PixbufRenderer *pr = rt->pr;
1582 if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
1583 (!rt->draw_queue && !rt->draw_queue_2pass) ||
1586 pr_render_complete_signal(pr);
1588 rt->draw_idle_id = 0;
1594 qd = rt->draw_queue->data;
1595 fast = (pr->zoom_2pass && ((pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0) || pr->post_process_slow));
1601 /* still loading, wait till done (also drops the higher priority) */
1603 return rt_queue_schedule_next_draw(rt, FALSE);
1606 qd = rt->draw_queue_2pass->data;
1610 if (gtk_widget_get_realized(GTK_WIDGET(pr)))
1612 if (rt_tile_is_visible(rt, qd->it))
1614 rt_tile_expose(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1616 else if (qd->new_data)
1618 /* if new pixel data, and we already have a pixmap, update the tile */
1619 qd->it->blank = FALSE;
1620 if (qd->it->surface && qd->it->render_done == TILE_RENDER_ALL)
1622 rt_tile_render(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1630 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
1635 rt_queue_merge(qd->it->qd2, qd);
1641 rt->draw_queue_2pass = g_list_append(rt->draw_queue_2pass, qd);
1652 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
1656 if (!rt->draw_queue && !rt->draw_queue_2pass)
1658 pr_render_complete_signal(pr);
1660 rt->draw_idle_id = 0;
1664 return rt_queue_schedule_next_draw(rt, FALSE);
1667 static void rt_queue_list_free(GList *list)
1687 static void rt_queue_clear(RendererTiles *rt)
1689 rt_queue_list_free(rt->draw_queue);
1690 rt->draw_queue = NULL;
1692 rt_queue_list_free(rt->draw_queue_2pass);
1693 rt->draw_queue_2pass = NULL;
1695 if (rt->draw_idle_id)
1697 g_source_remove(rt->draw_idle_id);
1698 rt->draw_idle_id = 0;
1703 static void rt_queue_merge(QueueData *parent, QueueData *qd)
1705 if (parent->x + parent->w < qd->x + qd->w)
1707 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
1709 if (parent->x > qd->x)
1711 parent->w += parent->x - qd->x;
1715 if (parent->y + parent->h < qd->y + qd->h)
1717 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
1719 if (parent->y > qd->y)
1721 parent->h += parent->y - qd->y;
1725 parent->new_data |= qd->new_data;
1728 static gboolean rt_clamp_to_visible(RendererTiles *rt, gint *x, gint *y, gint *w, gint *h)
1730 PixbufRenderer *pr = rt->pr;
1737 vh = pr->vis_height;
1742 if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
1745 nx = CLAMP(*x, vx, vx + vw);
1746 nw = CLAMP(*w - (nx - *x), 1, vw);
1748 ny = CLAMP(*y, vy, vy + vh);
1749 nh = CLAMP(*h - (ny - *y), 1, vh);
1759 static gboolean rt_queue_to_tiles(RendererTiles *rt, gint x, gint y, gint w, gint h,
1760 gboolean clamp, ImageRenderType render,
1761 gboolean new_data, gboolean only_existing)
1763 PixbufRenderer *pr = rt->pr;
1768 if (clamp && !rt_clamp_to_visible(rt, &x, &y, &w, &h)) return FALSE;
1770 x1 = ROUND_DOWN(x, rt->tile_width);
1771 x2 = ROUND_UP(x + w, rt->tile_width);
1773 y1 = ROUND_DOWN(y, rt->tile_height);
1774 y2 = ROUND_UP(y + h, rt->tile_height);
1776 for (j = y1; j <= y2; j += rt->tile_height)
1778 for (i = x1; i <= x2; i += rt->tile_width)
1782 it = rt_tile_get(rt, i, j,
1784 (i + rt->tile_width < rt->x_scroll ||
1785 i > rt->x_scroll + pr->vis_width ||
1786 j + rt->tile_height < rt->y_scroll ||
1787 j > rt->y_scroll + pr->vis_height)));
1792 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
1793 (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
1795 it->render_todo = render;
1798 qd = g_new(QueueData, 1);
1800 qd->new_data = new_data;
1810 qd->w = x + w - i - qd->x;
1811 if (qd->x + qd->w > rt->tile_width) qd->w = rt->tile_width - qd->x;
1821 qd->h = y + h - j - qd->y;
1822 if (qd->y + qd->h > rt->tile_height) qd->h = rt->tile_height - qd->y;
1824 if (qd->w < 1 || qd->h < 1)
1830 rt_queue_merge(it->qd, qd);
1836 rt->draw_queue = g_list_append(rt->draw_queue, qd);
1845 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
1846 gboolean clamp, ImageRenderType render,
1847 gboolean new_data, gboolean only_existing)
1849 PixbufRenderer *pr = rt->pr;
1854 nx = CLAMP(x, 0, pr->width - 1);
1855 ny = CLAMP(y, 0, pr->height - 1);
1858 w = CLAMP(w, 0, pr->width - nx);
1859 h = CLAMP(h, 0, pr->height - ny);
1860 if (w < 1 || h < 1) return;
1862 if (rt_queue_to_tiles(rt, nx, ny, w, h, clamp, render, new_data, only_existing) &&
1863 ((!rt->draw_queue && !rt->draw_queue_2pass) || !rt->draw_idle_id))
1865 if (rt->draw_idle_id)
1867 g_source_remove(rt->draw_idle_id);
1868 rt->draw_idle_id = 0;
1870 rt_queue_schedule_next_draw(rt, TRUE);
1874 static void rt_scroll(void *renderer, gint x_off, gint y_off)
1876 RendererTiles *rt = (RendererTiles *) renderer;
1877 PixbufRenderer *pr = rt->pr;
1880 if (rt->stereo_mode & PR_STEREO_MIRROR) x_off = -x_off;
1881 if (rt->stereo_mode & PR_STEREO_FLIP) y_off = -y_off;
1883 gint w = pr->vis_width - abs(x_off);
1884 gint h = pr->vis_height - abs(y_off);
1888 /* scrolled completely to new material */
1889 rt_queue(rt, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1899 cairo_surface_t *surface;
1923 box = GTK_WIDGET(pr);
1924 window = gtk_widget_get_window(box);
1926 cr = gdk_cairo_create(window);
1927 surface = cairo_get_target(cr);
1928 /* clipping restricts the intermediate surface's size, so it's a good idea
1930 cairo_rectangle(cr, x1 + pr->x_offset + rt->stereo_off_x, y1 + pr->y_offset + rt->stereo_off_y, w, h);
1932 /* Now push a group to change the target */
1933 cairo_push_group (cr);
1934 cairo_set_source_surface(cr, surface, x1 - x2, y1 - y2);
1936 /* Now copy the intermediate target back */
1937 cairo_pop_group_to_source(cr);
1941 rt_overlay_queue_all(rt, x2, y2, x1, y1);
1943 w = pr->vis_width - w;
1944 h = pr->vis_height - h;
1949 x_off > 0 ? rt->x_scroll + (pr->vis_width - w) : rt->x_scroll, rt->y_scroll,
1950 w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1954 /* FIXME, to optimize this, remove overlap */
1956 rt->x_scroll, y_off > 0 ? rt->y_scroll + (pr->vis_height - h) : rt->y_scroll,
1957 pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1962 static void renderer_area_changed(void *renderer, gint src_x, gint src_y, gint src_w, gint src_h)
1964 RendererTiles *rt = (RendererTiles *)renderer;
1965 PixbufRenderer *pr = rt->pr;
1966 gint x, y, width, height, x1, y1, x2, y2;
1968 gint orientation = rt_get_orientation(rt);
1969 pr_coords_map_orientation_reverse(orientation,
1970 src_x - GET_RIGHT_PIXBUF_OFFSET(rt), src_y,
1971 pr->image_width, pr->image_height,
1976 if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
1978 /* increase region when using a zoom quality that may access surrounding pixels */
1983 x1 = (gint)floor((gdouble)x * pr->scale);
1984 y1 = (gint)floor((gdouble)y * pr->scale * pr->aspect_ratio);
1985 x2 = (gint)ceil((gdouble)(x + width) * pr->scale);
1986 y2 = (gint)ceil((gdouble)(y + height) * pr->scale * pr->aspect_ratio);
1988 rt_queue(rt, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
1991 static void renderer_redraw(RendererTiles *rt, gint x, gint y, gint w, gint h,
1992 gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing)
1994 PixbufRenderer *pr = rt->pr;
1996 x -= rt->stereo_off_x;
1997 y -= rt->stereo_off_y;
1999 rt_border_draw(rt, x, y, w, h);
2001 x = MAX(0, x - pr->x_offset + pr->x_scroll);
2002 y = MAX(0, y - pr->y_offset + pr->y_scroll);
2006 MIN(w, pr->width - x),
2007 MIN(h, pr->height - y),
2008 clamp, render, new_data, only_existing);
2011 static void renderer_update_pixbuf(void *renderer, gboolean lazy)
2013 rt_queue_clear((RendererTiles *)renderer);
2016 static void renderer_update_zoom(void *renderer, gboolean lazy)
2018 RendererTiles *rt = (RendererTiles *)renderer;
2019 PixbufRenderer *pr = rt->pr;
2021 rt_tile_invalidate_all((RendererTiles *)renderer);
2024 renderer_redraw(renderer, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, TRUE, FALSE);
2026 rt_border_clear(rt);
2029 static void renderer_invalidate_region(void *renderer, gint x, gint y, gint w, gint h)
2031 rt_tile_invalidate_region((RendererTiles *)renderer, x, y, w, h);
2034 static void renderer_update_viewport(void *renderer)
2036 RendererTiles *rt = (RendererTiles *)renderer;
2038 rt->stereo_off_x = 0;
2039 rt->stereo_off_y = 0;
2041 if (rt->stereo_mode & PR_STEREO_RIGHT)
2043 if (rt->stereo_mode & PR_STEREO_HORIZ)
2045 rt->stereo_off_x = rt->pr->viewport_width;
2047 else if (rt->stereo_mode & PR_STEREO_VERT)
2049 rt->stereo_off_y = rt->pr->viewport_height;
2051 else if (rt->stereo_mode & PR_STEREO_FIXED)
2053 rt->stereo_off_x = rt->pr->stereo_fixed_x_right;
2054 rt->stereo_off_y = rt->pr->stereo_fixed_y_right;
2059 if (rt->stereo_mode & PR_STEREO_FIXED)
2061 rt->stereo_off_x = rt->pr->stereo_fixed_x_left;
2062 rt->stereo_off_y = rt->pr->stereo_fixed_y_left;
2065 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);
2067 rt_overlay_update_sizes(rt);
2068 rt_border_clear(rt);
2071 static void renderer_stereo_set(void *renderer, gint stereo_mode)
2073 RendererTiles *rt = (RendererTiles *)renderer;
2075 rt->stereo_mode = stereo_mode;
2078 static void renderer_free(void *renderer)
2080 RendererTiles *rt = (RendererTiles *)renderer;
2082 rt_tile_free_all(rt);
2083 if (rt->spare_tile) g_object_unref(rt->spare_tile);
2084 if (rt->overlay_buffer) g_object_unref(rt->overlay_buffer);
2085 rt_overlay_list_clear(rt);
2086 /* disconnect "hierarchy-changed" */
2087 g_signal_handlers_disconnect_matched(G_OBJECT(rt->pr), G_SIGNAL_MATCH_DATA,
2092 #if GTK_CHECK_VERSION(3,0,0)
2094 static gboolean rt_draw_cb(GtkWidget *widget, cairo_t *cr, gpointer data)
2096 RendererTiles *rt = (RendererTiles *)data;
2097 if (gtk_widget_is_drawable(widget))
2099 if (gtk_widget_get_has_window(widget))
2102 if (gdk_cairo_get_clip_rectangle(cr, &area))
2104 renderer_redraw(rt, area.x, area.y, area.width, area.height,
2105 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2114 static gboolean rt_expose_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
2116 RendererTiles *rt = (RendererTiles *)data;
2117 if (gtk_widget_is_drawable(widget))
2119 if (gtk_widget_get_has_window(widget))
2121 if (event->window != gtk_widget_get_window(widget))
2125 gdk_window_get_position(event->window, &area.x, &area.y);
2126 area.x += event->area.x;
2127 area.y += event->area.y;
2128 area.width = event->area.width;
2129 area.height = event->area.height;
2130 renderer_redraw(rt, area.x, area.y, area.width, area.height,
2131 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2136 renderer_redraw(rt, event->area.x, event->area.y, event->area.width, event->area.height,
2137 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2147 RendererFuncs *renderer_tiles_new(PixbufRenderer *pr)
2149 RendererTiles *rt = g_new0(RendererTiles, 1);
2153 rt->f.area_changed = renderer_area_changed;
2154 rt->f.update_pixbuf = renderer_update_pixbuf;
2155 rt->f.free = renderer_free;
2156 rt->f.update_zoom = renderer_update_zoom;
2157 rt->f.invalidate_region = renderer_invalidate_region;
2158 rt->f.scroll = rt_scroll;
2159 rt->f.update_viewport = renderer_update_viewport;
2162 rt->f.overlay_add = renderer_tiles_overlay_add;
2163 rt->f.overlay_set = renderer_tiles_overlay_set;
2164 rt->f.overlay_get = renderer_tiles_overlay_get;
2166 rt->f.stereo_set = renderer_stereo_set;
2168 rt->tile_width = PR_TILE_SIZE;
2169 rt->tile_height = PR_TILE_SIZE;
2172 rt->tile_cache_size = 0;
2174 rt->tile_cache_max = PR_CACHE_SIZE_DEFAULT;
2176 rt->draw_idle_id = 0;
2178 rt->stereo_mode = 0;
2179 rt->stereo_off_x = 0;
2180 rt->stereo_off_y = 0;
2182 g_signal_connect(G_OBJECT(pr), "hierarchy-changed",
2183 G_CALLBACK(rt_hierarchy_changed_cb), rt);
2185 #if GTK_CHECK_VERSION(3,0,0)
2186 g_signal_connect(G_OBJECT(pr), "draw",
2187 G_CALLBACK(rt_draw_cb), rt);
2189 g_signal_connect(G_OBJECT(pr), "expose_event",
2190 G_CALLBACK(rt_expose_cb), rt);
2192 return (RendererFuncs *) rt;
2196 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */