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;
586 work = rt->overlay_list;
596 if (!od->window) rt_overlay_init_window(rt, od);
598 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
599 if (pr_clip_region(x, y, w, h, px, py, pw, ph, &rx, &ry, &rw, &rh))
601 if (!rt->overlay_buffer)
603 rt->overlay_buffer = gdk_window_create_similar_surface(gtk_widget_get_window((GtkWidget *)pr),
605 rt->tile_width, rt->tile_height);
612 cr = cairo_create(rt->overlay_buffer);
613 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);
614 cairo_rectangle(cr, 0, 0, rw, rh);
615 cairo_fill_preserve(cr);
617 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - rx, py - ry);
621 cr = gdk_cairo_create(od->window);
622 cairo_set_source_surface(cr, rt->overlay_buffer, rx - px, ry - py);
623 cairo_rectangle (cr, rx - px, ry - py, rw, rh);
629 /* no ImageTile means region may be larger than our scratch buffer */
632 for (sx = rx; sx < rx + rw; sx += rt->tile_width)
633 for (sy = ry; sy < ry + rh; sy += rt->tile_height)
638 sw = MIN(rx + rw - sx, rt->tile_width);
639 sh = MIN(ry + rh - sy, rt->tile_height);
641 cr = cairo_create(rt->overlay_buffer);
642 cairo_set_source_rgb(cr, 0, 0, 0);
643 cairo_rectangle(cr, 0, 0, sw, sh);
644 cairo_fill_preserve(cr);
646 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - sx, py - sy);
650 cr = gdk_cairo_create(od->window);
651 cairo_set_source_surface(cr, rt->overlay_buffer, sx - px, sy - py);
652 cairo_rectangle (cr, sx - px, sy - py, sw, sh);
661 static void rt_overlay_queue_draw(RendererTiles *rt, OverlayData *od, gint x1, gint y1, gint x2, gint y2)
663 PixbufRenderer *pr = rt->pr;
666 rt_overlay_get_position(rt, od, &x, &y, &w, &h);
674 rt_queue(rt, rt->x_scroll - pr->x_offset + x,
675 rt->y_scroll - pr->y_offset + y,
677 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
679 rt_border_draw(rt, x, y, w, h);
682 static void rt_overlay_queue_all(RendererTiles *rt, gint x1, gint y1, gint x2, gint y2)
686 work = rt->overlay_list;
689 OverlayData *od = work->data;
692 rt_overlay_queue_draw(rt, od, x1, y1, x2, y2);
696 static void rt_overlay_update_sizes(RendererTiles *rt)
700 work = rt->overlay_list;
703 OverlayData *od = work->data;
706 if (!od->window) rt_overlay_init_window(rt, od);
708 if (od->flags & OVL_RELATIVE)
712 rt_overlay_get_position(rt, od, &x, &y, &w, &h);
713 gdk_window_move_resize(od->window, x + rt->stereo_off_x, y + rt->stereo_off_y, w, h);
718 static OverlayData *rt_overlay_find(RendererTiles *rt, gint id)
722 work = rt->overlay_list;
725 OverlayData *od = work->data;
728 if (od->id == id) return od;
735 gint renderer_tiles_overlay_add(void *renderer, GdkPixbuf *pixbuf, gint x, gint y,
736 OverlayRendererFlags flags)
738 RendererTiles *rt = (RendererTiles *) renderer;
739 PixbufRenderer *pr = rt->pr;
743 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
744 g_return_val_if_fail(pixbuf != NULL, -1);
747 while (rt_overlay_find(rt, id)) id++;
749 od = g_new0(OverlayData, 1);
752 g_object_ref(G_OBJECT(od->pixbuf));
757 rt_overlay_init_window(rt, od);
759 rt->overlay_list = g_list_append(rt->overlay_list, od);
761 rt_overlay_queue_draw(rt, od, 0, 0, 0, 0);
766 static void rt_overlay_free(RendererTiles *rt, OverlayData *od)
768 rt->overlay_list = g_list_remove(rt->overlay_list, od);
770 if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
771 if (od->window) gdk_window_destroy(od->window);
774 if (!rt->overlay_list && rt->overlay_buffer)
776 cairo_surface_destroy(rt->overlay_buffer);
777 rt->overlay_buffer = NULL;
781 static void rt_overlay_list_clear(RendererTiles *rt)
783 while (rt->overlay_list)
787 od = rt->overlay_list->data;
788 rt_overlay_free(rt, od);
792 static void rt_overlay_list_reset_window(RendererTiles *rt)
796 if (rt->overlay_buffer) cairo_surface_destroy(rt->overlay_buffer);
797 rt->overlay_buffer = NULL;
799 work = rt->overlay_list;
802 OverlayData *od = work->data;
804 if (od->window) gdk_window_destroy(od->window);
809 void renderer_tiles_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint x, gint y)
811 RendererTiles *rt = (RendererTiles *) renderer;
812 PixbufRenderer *pr = rt->pr;
815 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
817 od = rt_overlay_find(rt, id);
824 g_object_ref(G_OBJECT(pixbuf));
825 g_object_unref(G_OBJECT(od->pixbuf));
831 if (!od->window) rt_overlay_init_window(rt, od);
833 rt_overlay_queue_draw(rt, od, 0, 0, 0, 0);
834 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
835 gdk_window_move_resize(od->window, px + rt->stereo_off_x, py + rt->stereo_off_y, pw, ph);
839 rt_overlay_queue_draw(rt, od, 0, 0, 0, 0);
840 rt_overlay_free(rt, od);
844 gboolean renderer_tiles_overlay_get(void *renderer, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
846 RendererTiles *rt = (RendererTiles *) renderer;
847 PixbufRenderer *pr = rt->pr;
850 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
852 od = rt_overlay_find(rt, id);
853 if (!od) return FALSE;
855 if (pixbuf) *pixbuf = od->pixbuf;
862 static void rt_hierarchy_changed_cb(GtkWidget *widget, GtkWidget *previous_toplevel, gpointer data)
864 RendererTiles *rt = data;
865 rt_overlay_list_reset_window(rt);
869 *-------------------------------------------------------------------
871 *-------------------------------------------------------------------
874 static GdkPixbuf *rt_get_spare_tile(RendererTiles *rt)
876 if (!rt->spare_tile) rt->spare_tile = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, rt->tile_width, rt->tile_height);
877 return rt->spare_tile;
880 #define COLOR_BYTES 3 /* rgb */
882 static void rt_tile_rotate_90_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
884 GdkPixbuf *src = *tile;
887 guchar *s_pix, *d_pix;
889 guchar *ip, *spi, *dpi;
891 gint tw = rt->tile_width;
893 srs = gdk_pixbuf_get_rowstride(src);
894 s_pix = gdk_pixbuf_get_pixels(src);
895 spi = s_pix + (x * COLOR_BYTES);
897 dest = rt_get_spare_tile(rt);
898 drs = gdk_pixbuf_get_rowstride(dest);
899 d_pix = gdk_pixbuf_get_pixels(dest);
900 dpi = d_pix + (tw - 1) * COLOR_BYTES;
902 for (i = y; i < y + h; i++)
904 sp = spi + (i * srs);
905 ip = dpi - (i * COLOR_BYTES);
906 for (j = x; j < x + w; j++)
909 memcpy(dp, sp, COLOR_BYTES);
914 rt->spare_tile = src;
918 static void rt_tile_rotate_90_counter_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
920 GdkPixbuf *src = *tile;
923 guchar *s_pix, *d_pix;
925 guchar *ip, *spi, *dpi;
927 gint th = rt->tile_height;
929 srs = gdk_pixbuf_get_rowstride(src);
930 s_pix = gdk_pixbuf_get_pixels(src);
931 spi = s_pix + (x * COLOR_BYTES);
933 dest = rt_get_spare_tile(rt);
934 drs = gdk_pixbuf_get_rowstride(dest);
935 d_pix = gdk_pixbuf_get_pixels(dest);
936 dpi = d_pix + (th - 1) * drs;
938 for (i = y; i < y + h; i++)
940 sp = spi + (i * srs);
941 ip = dpi + (i * COLOR_BYTES);
942 for (j = x; j < x + w; j++)
945 memcpy(dp, sp, COLOR_BYTES);
950 rt->spare_tile = src;
954 static void rt_tile_mirror_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
956 GdkPixbuf *src = *tile;
959 guchar *s_pix, *d_pix;
964 gint tw = rt->tile_width;
966 srs = gdk_pixbuf_get_rowstride(src);
967 s_pix = gdk_pixbuf_get_pixels(src);
968 spi = s_pix + (x * COLOR_BYTES);
970 dest = rt_get_spare_tile(rt);
971 drs = gdk_pixbuf_get_rowstride(dest);
972 d_pix = gdk_pixbuf_get_pixels(dest);
973 dpi = d_pix + (tw - x - 1) * COLOR_BYTES;
975 for (i = y; i < y + h; i++)
977 sp = spi + (i * srs);
978 dp = dpi + (i * drs);
979 for (j = 0; j < w; j++)
981 memcpy(dp, sp, COLOR_BYTES);
987 rt->spare_tile = src;
991 static void rt_tile_mirror_and_flip(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
993 GdkPixbuf *src = *tile;
996 guchar *s_pix, *d_pix;
1000 gint tw = rt->tile_width;
1001 gint th = rt->tile_height;
1003 srs = gdk_pixbuf_get_rowstride(src);
1004 s_pix = gdk_pixbuf_get_pixels(src);
1006 dest = rt_get_spare_tile(rt);
1007 drs = gdk_pixbuf_get_rowstride(dest);
1008 d_pix = gdk_pixbuf_get_pixels(dest);
1009 dpi = d_pix + (th - 1) * drs + (tw - 1) * COLOR_BYTES;
1011 for (i = y; i < y + h; i++)
1013 sp = s_pix + (i * srs) + (x * COLOR_BYTES);
1014 dp = dpi - (i * drs) - (x * COLOR_BYTES);
1015 for (j = 0; j < w; j++)
1017 memcpy(dp, sp, COLOR_BYTES);
1023 rt->spare_tile = src;
1027 static void rt_tile_flip_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1029 GdkPixbuf *src = *tile;
1032 guchar *s_pix, *d_pix;
1036 gint th = rt->tile_height;
1038 srs = gdk_pixbuf_get_rowstride(src);
1039 s_pix = gdk_pixbuf_get_pixels(src);
1040 spi = s_pix + (x * COLOR_BYTES);
1042 dest = rt_get_spare_tile(rt);
1043 drs = gdk_pixbuf_get_rowstride(dest);
1044 d_pix = gdk_pixbuf_get_pixels(dest);
1045 dpi = d_pix + (th - 1) * drs + (x * COLOR_BYTES);
1047 for (i = y; i < y + h; i++)
1049 sp = spi + (i * srs);
1050 dp = dpi - (i * drs);
1051 memcpy(dp, sp, w * COLOR_BYTES);
1054 rt->spare_tile = src;
1058 static void rt_tile_apply_orientation(RendererTiles *rt, gint orientation, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h)
1060 switch (orientation)
1062 case EXIF_ORIENTATION_TOP_LEFT:
1063 /* normal -- nothing to do */
1065 case EXIF_ORIENTATION_TOP_RIGHT:
1068 rt_tile_mirror_only(rt, pixbuf, x, y, w, h);
1071 case EXIF_ORIENTATION_BOTTOM_RIGHT:
1074 rt_tile_mirror_and_flip(rt, pixbuf, x, y, w, h);
1077 case EXIF_ORIENTATION_BOTTOM_LEFT:
1080 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1083 case EXIF_ORIENTATION_LEFT_TOP:
1085 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1086 rt_tile_rotate_90_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1089 case EXIF_ORIENTATION_RIGHT_TOP:
1090 /* rotated -90 (270) */
1092 rt_tile_rotate_90_clockwise(rt, pixbuf, x, y, w, h);
1095 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1097 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1098 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1101 case EXIF_ORIENTATION_LEFT_BOTTOM:
1104 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, y, w, h);
1108 /* The other values are out of range */
1113 static gboolean rt_source_tile_render(RendererTiles *rt, ImageTile *it,
1114 gint x, gint y, gint w, gint h,
1115 gboolean new_data, gboolean fast)
1117 PixbufRenderer *pr = rt->pr;
1120 gboolean draw = FALSE;
1122 if (pr->zoom == 1.0 || pr->scale == 1.0)
1124 list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE);
1129 gint rx, ry, rw, rh;
1134 if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1135 it->x + x, it->y + y, w, h,
1136 &rx, &ry, &rw, &rh))
1139 cr = cairo_create(it->surface);
1140 cairo_rectangle (cr, rx - it->x, ry - it->y, rw, rh);
1144 cairo_set_source_rgb(cr, 0, 0, 0);
1146 else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
1148 gdk_cairo_set_source_pixbuf(cr, st->pixbuf, -it->x + st->x, -it->y + st->y);
1157 gdouble scale_x, scale_y;
1158 gint sx, sy, sw, sh;
1160 if (pr->image_width == 0 || pr->image_height == 0) return FALSE;
1161 scale_x = (gdouble)pr->width / pr->image_width;
1162 scale_y = (gdouble)pr->height / pr->image_height;
1164 sx = (gdouble)(it->x + x) / scale_x;
1165 sy = (gdouble)(it->y + y) / scale_y;
1166 sw = (gdouble)w / scale_x;
1167 sh = (gdouble)h / scale_y;
1169 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1172 /* draws red over draw region, to check for leaks (regions not filled) */
1173 pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255);
1176 list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE);
1181 gint rx, ry, rw, rh;
1182 gint stx, sty, stw, sth;
1187 stx = floor((gdouble)st->x * scale_x);
1188 sty = floor((gdouble)st->y * scale_y);
1189 stw = ceil((gdouble)(st->x + pr->source_tile_width) * scale_x) - stx;
1190 sth = ceil((gdouble)(st->y + pr->source_tile_height) * scale_y) - sty;
1192 if (pr_clip_region(stx, sty, stw, sth,
1193 it->x + x, it->y + y, w, h,
1194 &rx, &ry, &rw, &rh))
1200 cr = cairo_create(it->surface);
1201 cairo_rectangle (cr, rx - st->x, ry - st->y, rw, rh);
1202 cairo_set_source_rgb(cr, 0, 0, 0);
1211 /* may need to use unfloored stx,sty values here */
1212 offset_x = (gdouble)(stx - it->x);
1213 offset_y = (gdouble)(sty - it->y);
1215 gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh,
1216 (gdouble) 0.0 + offset_x,
1217 (gdouble) 0.0 + offset_y,
1219 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1231 static void rt_tile_get_region(gboolean has_alpha,
1232 const GdkPixbuf *src, GdkPixbuf *dest,
1233 int pb_x, int pb_y, int pb_w, int pb_h,
1234 double offset_x, double offset_y, double scale_x, double scale_y,
1235 GdkInterpType interp_type,
1236 int check_x, int check_y)
1240 if (scale_x == 1.0 && scale_y == 1.0)
1242 gdk_pixbuf_copy_area(src,
1243 -offset_x + pb_x, -offset_y + pb_y,
1250 gdk_pixbuf_scale(src, dest,
1251 pb_x, pb_y, pb_w, pb_h,
1260 gdk_pixbuf_composite_color(src, dest,
1261 pb_x, pb_y, pb_w, pb_h,
1266 255, check_x, check_y,
1267 PR_ALPHA_CHECK_SIZE, PR_ALPHA_CHECK1, PR_ALPHA_CHECK2);
1272 static gint rt_get_orientation(RendererTiles *rt)
1274 PixbufRenderer *pr = rt->pr;
1276 gint orientation = pr->orientation;
1277 static const gint mirror[] = {1, 2, 1, 4, 3, 6, 5, 8, 7};
1278 static const gint flip[] = {1, 4, 3, 2, 1, 8, 7, 6, 5};
1280 if (rt->stereo_mode & PR_STEREO_MIRROR) orientation = mirror[orientation];
1281 if (rt->stereo_mode & PR_STEREO_FLIP) orientation = flip[orientation];
1286 static void rt_tile_render(RendererTiles *rt, ImageTile *it,
1287 gint x, gint y, gint w, gint h,
1288 gboolean new_data, gboolean fast)
1290 PixbufRenderer *pr = rt->pr;
1292 gboolean draw = FALSE;
1293 gint orientation = rt_get_orientation(rt);
1295 if (it->render_todo == TILE_RENDER_NONE && it->surface && !new_data) return;
1297 if (it->render_done != TILE_RENDER_ALL)
1303 if (!fast) it->render_done = TILE_RENDER_ALL;
1305 else if (it->render_todo != TILE_RENDER_AREA)
1307 if (!fast) it->render_todo = TILE_RENDER_NONE;
1311 if (!fast) it->render_todo = TILE_RENDER_NONE;
1313 if (new_data) it->blank = FALSE;
1315 rt_tile_prepare(rt, it);
1316 has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
1318 /* FIXME checker colors for alpha should be configurable,
1319 * also should be drawn for blank = TRUE
1324 /* no data, do fast rect fill */
1326 cr = cairo_create(it->surface);
1327 cairo_rectangle (cr, 0, 0, it->w, it->h);
1328 cairo_set_source_rgb(cr, 0, 0, 0);
1332 else if (pr->source_tiles_enabled)
1334 draw = rt_source_tile_render(rt, it, x, y, w, h, new_data, fast);
1336 else if ((pr->zoom == 1.0 || pr->scale == 1.0) &&
1337 pr->aspect_ratio == 1.0 &&
1339 orientation == EXIF_ORIENTATION_TOP_LEFT &&
1340 !(pr->func_post_process && !(pr->post_process_slow && fast)) &&
1341 !(rt->stereo_mode & PR_STEREO_ANAGLYPH))
1343 /* special case: faster, simple, scale 1.0, base orientation, no postprocessing */
1345 cr = cairo_create(it->surface);
1346 cairo_rectangle (cr, x, y, w, h);
1347 gdk_cairo_set_source_pixbuf(cr, pr->pixbuf, -it->x - GET_RIGHT_PIXBUF_OFFSET(rt), -it->y);
1353 gdouble scale_x, scale_y;
1354 gdouble src_x, src_y;
1358 if (pr->image_width == 0 || pr->image_height == 0) return;
1360 scale_x = (gdouble)pr->width / pr->image_width;
1361 scale_y = (gdouble)pr->height / pr->image_height;
1363 pr_tile_coords_map_orientation(orientation, it->x, it->y,
1364 pr->width, pr->height,
1365 rt->tile_width, rt->tile_height,
1367 pr_tile_region_map_orientation(orientation, x, y,
1368 rt->tile_width, rt->tile_height,
1373 switch (orientation)
1376 case EXIF_ORIENTATION_LEFT_TOP:
1377 case EXIF_ORIENTATION_RIGHT_TOP:
1378 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1379 case EXIF_ORIENTATION_LEFT_BOTTOM:
1389 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
1390 * small sizes for anything but GDK_INTERP_NEAREST
1392 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1394 rt_tile_get_region(has_alpha,
1395 pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
1396 (gdouble) 0.0 - src_x - GET_RIGHT_PIXBUF_OFFSET(rt) * scale_x,
1397 (gdouble) 0.0 - src_y,
1399 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1400 it->x + pb_x, it->y + pb_y);
1401 if (rt->stereo_mode & PR_STEREO_ANAGLYPH &&
1402 (pr->stereo_pixbuf_offset_right > 0 || pr->stereo_pixbuf_offset_left > 0))
1404 GdkPixbuf *right_pb = rt_get_spare_tile(rt);
1405 rt_tile_get_region(has_alpha,
1406 pr->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h,
1407 (gdouble) 0.0 - src_x - GET_LEFT_PIXBUF_OFFSET(rt) * scale_x,
1408 (gdouble) 0.0 - src_y,
1410 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1411 it->x + pb_x, it->y + pb_y);
1412 pr_create_anaglyph(rt->stereo_mode, it->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h);
1413 /* do not care about freeing spare_tile, it will be reused */
1415 rt_tile_apply_orientation(rt, orientation, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
1419 if (draw && it->pixbuf && !it->blank)
1423 if (pr->func_post_process && !(pr->post_process_slow && fast))
1424 pr->func_post_process(pr, &it->pixbuf, x, y, w, h, pr->post_process_user_data);
1426 cr = cairo_create(it->surface);
1427 cairo_rectangle (cr, x, y, w, h);
1428 gdk_cairo_set_source_pixbuf(cr, it->pixbuf, 0, 0);
1435 static void rt_tile_expose(RendererTiles *rt, ImageTile *it,
1436 gint x, gint y, gint w, gint h,
1437 gboolean new_data, gboolean fast)
1439 PixbufRenderer *pr = rt->pr;
1444 /* clamp to visible */
1445 if (it->x + x < rt->x_scroll)
1447 w -= rt->x_scroll - it->x - x;
1448 x = rt->x_scroll - it->x;
1450 if (it->x + x + w > rt->x_scroll + pr->vis_width)
1452 w = rt->x_scroll + pr->vis_width - it->x - x;
1455 if (it->y + y < rt->y_scroll)
1457 h -= rt->y_scroll - it->y - y;
1458 y = rt->y_scroll - it->y;
1460 if (it->y + y + h > rt->y_scroll + pr->vis_height)
1462 h = rt->y_scroll + pr->vis_height - it->y - y;
1466 rt_tile_render(rt, it, x, y, w, h, new_data, fast);
1468 box = GTK_WIDGET(pr);
1469 window = gtk_widget_get_window(box);
1471 cr = gdk_cairo_create(window);
1472 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);
1473 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);
1477 if (rt->overlay_list)
1479 rt_overlay_draw(rt, pr->x_offset + (it->x - rt->x_scroll) + x,
1480 pr->y_offset + (it->y - rt->y_scroll) + y,
1487 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it)
1489 PixbufRenderer *pr = rt->pr;
1490 return (it->x + it->w >= rt->x_scroll && it->x < rt->x_scroll + pr->vis_width &&
1491 it->y + it->h >= rt->y_scroll && it->y < rt->y_scroll + pr->vis_height);
1495 *-------------------------------------------------------------------
1497 *-------------------------------------------------------------------
1500 static gint rt_get_queued_area(GList *work)
1506 QueueData *qd = work->data;
1507 area += qd->w * qd->h;
1514 static gboolean rt_queue_schedule_next_draw(RendererTiles *rt, gboolean force_set)
1516 PixbufRenderer *pr = rt->pr;
1518 gint visible_area = pr->vis_width * pr->vis_height;
1523 DEBUG_2("redraw priority: 2pass");
1524 rt->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, rt_queue_draw_idle_cb, rt, NULL);
1528 if (visible_area == 0)
1535 percent = 100.0 * rt_get_queued_area(rt->draw_queue) / visible_area;
1540 /* we have enough data for starting intensive redrawing */
1541 DEBUG_2("redraw priority: high %.2f %%", percent);
1542 rt->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW, rt_queue_draw_idle_cb, rt, NULL);
1546 if (percent < 1.0 || force_set)
1548 /* queue is (almost) empty, wait 50 ms*/
1549 DEBUG_2("redraw priority: wait %.2f %%", percent);
1550 rt->draw_idle_id = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, 50, rt_queue_draw_idle_cb, rt, NULL);
1554 /* keep the same priority as before */
1555 DEBUG_2("redraw priority: no change %.2f %%", percent);
1560 static gboolean rt_queue_draw_idle_cb(gpointer data)
1562 RendererTiles *rt = data;
1563 PixbufRenderer *pr = rt->pr;
1568 if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
1569 (!rt->draw_queue && !rt->draw_queue_2pass) ||
1572 pr_render_complete_signal(pr);
1574 rt->draw_idle_id = 0;
1580 qd = rt->draw_queue->data;
1581 fast = (pr->zoom_2pass && ((pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0) || pr->post_process_slow));
1587 /* still loading, wait till done (also drops the higher priority) */
1589 return rt_queue_schedule_next_draw(rt, FALSE);
1592 qd = rt->draw_queue_2pass->data;
1596 if (gtk_widget_get_realized(GTK_WIDGET(pr)))
1598 if (rt_tile_is_visible(rt, qd->it))
1600 rt_tile_expose(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1602 else if (qd->new_data)
1604 /* if new pixel data, and we already have a pixmap, update the tile */
1605 qd->it->blank = FALSE;
1606 if (qd->it->surface && qd->it->render_done == TILE_RENDER_ALL)
1608 rt_tile_render(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1616 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
1621 rt_queue_merge(qd->it->qd2, qd);
1627 rt->draw_queue_2pass = g_list_append(rt->draw_queue_2pass, qd);
1638 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
1642 if (!rt->draw_queue && !rt->draw_queue_2pass)
1644 pr_render_complete_signal(pr);
1646 rt->draw_idle_id = 0;
1650 return rt_queue_schedule_next_draw(rt, FALSE);
1653 static void rt_queue_list_free(GList *list)
1673 static void rt_queue_clear(RendererTiles *rt)
1675 rt_queue_list_free(rt->draw_queue);
1676 rt->draw_queue = NULL;
1678 rt_queue_list_free(rt->draw_queue_2pass);
1679 rt->draw_queue_2pass = NULL;
1681 if (rt->draw_idle_id)
1683 g_source_remove(rt->draw_idle_id);
1684 rt->draw_idle_id = 0;
1689 static void rt_queue_merge(QueueData *parent, QueueData *qd)
1691 if (parent->x + parent->w < qd->x + qd->w)
1693 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
1695 if (parent->x > qd->x)
1697 parent->w += parent->x - qd->x;
1701 if (parent->y + parent->h < qd->y + qd->h)
1703 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
1705 if (parent->y > qd->y)
1707 parent->h += parent->y - qd->y;
1711 parent->new_data |= qd->new_data;
1714 static gboolean rt_clamp_to_visible(RendererTiles *rt, gint *x, gint *y, gint *w, gint *h)
1716 PixbufRenderer *pr = rt->pr;
1723 vh = pr->vis_height;
1728 if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
1731 nx = CLAMP(*x, vx, vx + vw);
1732 nw = CLAMP(*w - (nx - *x), 1, vw);
1734 ny = CLAMP(*y, vy, vy + vh);
1735 nh = CLAMP(*h - (ny - *y), 1, vh);
1745 static gboolean rt_queue_to_tiles(RendererTiles *rt, gint x, gint y, gint w, gint h,
1746 gboolean clamp, ImageRenderType render,
1747 gboolean new_data, gboolean only_existing)
1749 PixbufRenderer *pr = rt->pr;
1754 if (clamp && !rt_clamp_to_visible(rt, &x, &y, &w, &h)) return FALSE;
1756 x1 = ROUND_DOWN(x, rt->tile_width);
1757 x2 = ROUND_UP(x + w, rt->tile_width);
1759 y1 = ROUND_DOWN(y, rt->tile_height);
1760 y2 = ROUND_UP(y + h, rt->tile_height);
1762 for (j = y1; j <= y2; j += rt->tile_height)
1764 for (i = x1; i <= x2; i += rt->tile_width)
1768 it = rt_tile_get(rt, i, j,
1770 (i + rt->tile_width < rt->x_scroll ||
1771 i > rt->x_scroll + pr->vis_width ||
1772 j + rt->tile_height < rt->y_scroll ||
1773 j > rt->y_scroll + pr->vis_height)));
1778 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
1779 (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
1781 it->render_todo = render;
1784 qd = g_new(QueueData, 1);
1786 qd->new_data = new_data;
1796 qd->w = x + w - i - qd->x;
1797 if (qd->x + qd->w > rt->tile_width) qd->w = rt->tile_width - qd->x;
1807 qd->h = y + h - j - qd->y;
1808 if (qd->y + qd->h > rt->tile_height) qd->h = rt->tile_height - qd->y;
1810 if (qd->w < 1 || qd->h < 1)
1816 rt_queue_merge(it->qd, qd);
1822 rt->draw_queue = g_list_append(rt->draw_queue, qd);
1831 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
1832 gboolean clamp, ImageRenderType render,
1833 gboolean new_data, gboolean only_existing)
1835 PixbufRenderer *pr = rt->pr;
1840 nx = CLAMP(x, 0, pr->width - 1);
1841 ny = CLAMP(y, 0, pr->height - 1);
1844 w = CLAMP(w, 0, pr->width - nx);
1845 h = CLAMP(h, 0, pr->height - ny);
1846 if (w < 1 || h < 1) return;
1848 if (rt_queue_to_tiles(rt, nx, ny, w, h, clamp, render, new_data, only_existing) &&
1849 ((!rt->draw_queue && !rt->draw_queue_2pass) || !rt->draw_idle_id))
1851 if (rt->draw_idle_id)
1853 g_source_remove(rt->draw_idle_id);
1854 rt->draw_idle_id = 0;
1856 rt_queue_schedule_next_draw(rt, TRUE);
1860 static void rt_scroll(void *renderer, gint x_off, gint y_off)
1862 RendererTiles *rt = (RendererTiles *) renderer;
1863 PixbufRenderer *pr = rt->pr;
1866 if (rt->stereo_mode & PR_STEREO_MIRROR) x_off = -x_off;
1867 if (rt->stereo_mode & PR_STEREO_FLIP) y_off = -y_off;
1869 gint w = pr->vis_width - abs(x_off);
1870 gint h = pr->vis_height - abs(y_off);
1874 /* scrolled completely to new material */
1875 rt_queue(rt, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1885 cairo_surface_t *surface;
1909 box = GTK_WIDGET(pr);
1910 window = gtk_widget_get_window(box);
1912 cr = gdk_cairo_create(window);
1913 surface = cairo_get_target(cr);
1914 /* clipping restricts the intermediate surface's size, so it's a good idea
1916 cairo_rectangle(cr, x1 + pr->x_offset + rt->stereo_off_x, y1 + pr->y_offset + rt->stereo_off_y, w, h);
1918 /* Now push a group to change the target */
1919 cairo_push_group (cr);
1920 cairo_set_source_surface(cr, surface, x1 - x2, y1 - y2);
1922 /* Now copy the intermediate target back */
1923 cairo_pop_group_to_source(cr);
1927 rt_overlay_queue_all(rt, x2, y2, x1, y1);
1929 w = pr->vis_width - w;
1930 h = pr->vis_height - h;
1935 x_off > 0 ? rt->x_scroll + (pr->vis_width - w) : rt->x_scroll, rt->y_scroll,
1936 w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1940 /* FIXME, to optimize this, remove overlap */
1942 rt->x_scroll, y_off > 0 ? rt->y_scroll + (pr->vis_height - h) : rt->y_scroll,
1943 pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1948 static void renderer_area_changed(void *renderer, gint src_x, gint src_y, gint src_w, gint src_h)
1950 RendererTiles *rt = (RendererTiles *)renderer;
1951 PixbufRenderer *pr = rt->pr;
1952 gint x, y, width, height, x1, y1, x2, y2;
1954 gint orientation = rt_get_orientation(rt);
1955 pr_coords_map_orientation_reverse(orientation,
1956 src_x - GET_RIGHT_PIXBUF_OFFSET(rt), src_y,
1957 pr->image_width, pr->image_height,
1962 if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
1964 /* increase region when using a zoom quality that may access surrounding pixels */
1969 x1 = (gint)floor((gdouble)x * pr->scale);
1970 y1 = (gint)floor((gdouble)y * pr->scale * pr->aspect_ratio);
1971 x2 = (gint)ceil((gdouble)(x + width) * pr->scale);
1972 y2 = (gint)ceil((gdouble)(y + height) * pr->scale * pr->aspect_ratio);
1974 rt_queue(rt, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
1977 static void renderer_redraw(RendererTiles *rt, gint x, gint y, gint w, gint h,
1978 gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing)
1980 PixbufRenderer *pr = rt->pr;
1982 x -= rt->stereo_off_x;
1983 y -= rt->stereo_off_y;
1985 rt_border_draw(rt, x, y, w, h);
1987 x = MAX(0, x - pr->x_offset + pr->x_scroll);
1988 y = MAX(0, y - pr->y_offset + pr->y_scroll);
1992 MIN(w, pr->width - x),
1993 MIN(h, pr->height - y),
1994 clamp, render, new_data, only_existing);
1997 static void renderer_update_pixbuf(void *renderer, gboolean lazy)
1999 rt_queue_clear((RendererTiles *)renderer);
2002 static void renderer_update_zoom(void *renderer, gboolean lazy)
2004 RendererTiles *rt = (RendererTiles *)renderer;
2005 PixbufRenderer *pr = rt->pr;
2007 rt_tile_invalidate_all((RendererTiles *)renderer);
2010 renderer_redraw(renderer, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, TRUE, FALSE);
2012 rt_border_clear(rt);
2015 static void renderer_invalidate_region(void *renderer, gint x, gint y, gint w, gint h)
2017 rt_tile_invalidate_region((RendererTiles *)renderer, x, y, w, h);
2020 static void renderer_update_viewport(void *renderer)
2022 RendererTiles *rt = (RendererTiles *)renderer;
2024 rt->stereo_off_x = 0;
2025 rt->stereo_off_y = 0;
2027 if (rt->stereo_mode & PR_STEREO_RIGHT)
2029 if (rt->stereo_mode & PR_STEREO_HORIZ)
2031 rt->stereo_off_x = rt->pr->viewport_width;
2033 else if (rt->stereo_mode & PR_STEREO_VERT)
2035 rt->stereo_off_y = rt->pr->viewport_height;
2037 else if (rt->stereo_mode & PR_STEREO_FIXED)
2039 rt->stereo_off_x = rt->pr->stereo_fixed_x_right;
2040 rt->stereo_off_y = rt->pr->stereo_fixed_y_right;
2045 if (rt->stereo_mode & PR_STEREO_FIXED)
2047 rt->stereo_off_x = rt->pr->stereo_fixed_x_left;
2048 rt->stereo_off_y = rt->pr->stereo_fixed_y_left;
2051 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);
2053 rt_overlay_update_sizes(rt);
2054 rt_border_clear(rt);
2057 static void renderer_stereo_set(void *renderer, gint stereo_mode)
2059 RendererTiles *rt = (RendererTiles *)renderer;
2061 rt->stereo_mode = stereo_mode;
2064 static void renderer_free(void *renderer)
2066 RendererTiles *rt = (RendererTiles *)renderer;
2068 rt_tile_free_all(rt);
2069 if (rt->spare_tile) g_object_unref(rt->spare_tile);
2070 if (rt->overlay_buffer) g_object_unref(rt->overlay_buffer);
2071 rt_overlay_list_clear(rt);
2072 /* disconnect "hierarchy-changed" */
2073 g_signal_handlers_disconnect_matched(G_OBJECT(rt->pr), G_SIGNAL_MATCH_DATA,
2078 #if GTK_CHECK_VERSION(3,0,0)
2080 static gboolean rt_draw_cb(GtkWidget *widget, cairo_t *cr, gpointer data)
2082 RendererTiles *rt = (RendererTiles *)data;
2083 if (gtk_widget_is_drawable(widget))
2085 if (gtk_widget_get_has_window(widget))
2088 if (gdk_cairo_get_clip_rectangle(cr, &area))
2090 renderer_redraw(rt, area.x, area.y, area.width, area.height,
2091 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2100 static gboolean rt_expose_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
2102 RendererTiles *rt = (RendererTiles *)data;
2103 if (gtk_widget_is_drawable(widget))
2105 if (gtk_widget_get_has_window(widget))
2107 if (event->window != gtk_widget_get_window(widget))
2111 gdk_window_get_position(event->window, &area.x, &area.y);
2112 area.x += event->area.x;
2113 area.y += event->area.y;
2114 area.width = event->area.width;
2115 area.height = event->area.height;
2116 renderer_redraw(rt, area.x, area.y, area.width, area.height,
2117 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2122 renderer_redraw(rt, event->area.x, event->area.y, event->area.width, event->area.height,
2123 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2133 RendererFuncs *renderer_tiles_new(PixbufRenderer *pr)
2135 RendererTiles *rt = g_new0(RendererTiles, 1);
2139 rt->f.area_changed = renderer_area_changed;
2140 rt->f.update_pixbuf = renderer_update_pixbuf;
2141 rt->f.free = renderer_free;
2142 rt->f.update_zoom = renderer_update_zoom;
2143 rt->f.invalidate_region = renderer_invalidate_region;
2144 rt->f.scroll = rt_scroll;
2145 rt->f.update_viewport = renderer_update_viewport;
2148 rt->f.overlay_add = renderer_tiles_overlay_add;
2149 rt->f.overlay_set = renderer_tiles_overlay_set;
2150 rt->f.overlay_get = renderer_tiles_overlay_get;
2152 rt->f.stereo_set = renderer_stereo_set;
2154 rt->tile_width = PR_TILE_SIZE;
2155 rt->tile_height = PR_TILE_SIZE;
2158 rt->tile_cache_size = 0;
2160 rt->tile_cache_max = PR_CACHE_SIZE_DEFAULT;
2162 rt->draw_idle_id = 0;
2164 rt->stereo_mode = 0;
2165 rt->stereo_off_x = 0;
2166 rt->stereo_off_y = 0;
2168 g_signal_connect(G_OBJECT(pr), "hierarchy-changed",
2169 G_CALLBACK(rt_hierarchy_changed_cb), rt);
2171 #if GTK_CHECK_VERSION(3,0,0)
2172 g_signal_connect(G_OBJECT(pr), "draw",
2173 G_CALLBACK(rt_draw_cb), rt);
2175 g_signal_connect(G_OBJECT(pr), "expose_event",
2176 G_CALLBACK(rt_expose_cb), rt);
2178 return (RendererFuncs *) rt;
2182 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */