2 * Copyright (C) 2006 John Ellis
3 * Copyright (C) 2008 - 2016 The Geeqie Team
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 #include "pixbuf-renderer.h"
29 #include "renderer-tiles.h"
37 /* comment this out if not using this from within Geeqie
38 * defining GQ_BUILD does these things:
39 * - Sets the shift-click scroller pixbuf to a nice icon instead of a black box
45 #include "pixbuf_util.h"
49 EXIF_ORIENTATION_UNKNOWN = 0,
50 EXIF_ORIENTATION_TOP_LEFT = 1,
51 EXIF_ORIENTATION_TOP_RIGHT = 2,
52 EXIF_ORIENTATION_BOTTOM_RIGHT = 3,
53 EXIF_ORIENTATION_BOTTOM_LEFT = 4,
54 EXIF_ORIENTATION_LEFT_TOP = 5,
55 EXIF_ORIENTATION_RIGHT_TOP = 6,
56 EXIF_ORIENTATION_RIGHT_BOTTOM = 7,
57 EXIF_ORIENTATION_LEFT_BOTTOM = 8
58 } ExifOrientationType;
62 /* size to use when breaking up image pane for rendering */
63 #define PR_TILE_SIZE 128
65 typedef struct _ImageTile ImageTile;
66 typedef struct _QueueData QueueData;
70 cairo_surface_t *surface; /* off screen buffer */
71 GdkPixbuf *pixbuf; /* pixbuf area for zooming */
72 gint x; /* x offset into image */
73 gint y; /* y offset into image */
74 gint w; /* width that is visible (may be less if at edge of image) */
75 gint h; /* height '' */
79 /* render_todo: (explanation)
81 AREA render area of tile, usually only used when loading an image
82 note: will jump to an ALL if render_done is not ALL.
83 ALL render entire tile, if never done before w/ ALL, for expose events *only*
86 ImageRenderType render_todo; /* what to do (see above) */
87 ImageRenderType render_done; /* highest that has been done before on tile */
92 guint size; /* est. memory used by pixmap and pixbuf */
105 typedef struct _OverlayData OverlayData;
116 OverlayRendererFlags flags;
119 typedef struct _RendererTiles RendererTiles;
121 struct _RendererTiles
126 gint tile_cache_max; /* max mb to use for offscreen buffer */
130 gint tile_cols; /* count of tile columns */
131 GList *tiles; /* list of buffer tiles */
132 gint tile_cache_size; /* allocated size of pixmaps/pixbufs */
133 GList *draw_queue; /* list of areas to redraw */
134 GList *draw_queue_2pass;/* list when 2 pass is enabled */
137 cairo_surface_t *overlay_buffer;
139 guint draw_idle_id; /* event source id */
141 GdkPixbuf *spare_tile;
147 gint x_scroll; /* allow local adjustment and mirroring */
154 static void rt_border_draw(RendererTiles *rt, gint x, gint y, gint w, gint h);
155 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h, ImageTile *it);
158 static void rt_tile_free_all(RendererTiles *rt);
159 static void rt_tile_invalidate_region(RendererTiles *rt, gint x, gint y, gint w, gint h);
160 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it);
161 static void rt_queue_clear(RendererTiles *rt);
162 static void rt_queue_merge(QueueData *parent, QueueData *qd);
163 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
164 gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing);
166 static void rt_hierarchy_changed_cb(GtkWidget *widget, GtkWidget *previous_toplevel, gpointer data);
167 static gint rt_queue_draw_idle_cb(gpointer data);
169 #define GET_RIGHT_PIXBUF_OFFSET(rt) \
170 (( (rt->stereo_mode & PR_STEREO_RIGHT) && !(rt->stereo_mode & PR_STEREO_SWAP)) || \
171 (!(rt->stereo_mode & PR_STEREO_RIGHT) && (rt->stereo_mode & PR_STEREO_SWAP)) ? \
172 rt->pr->stereo_pixbuf_offset_right : rt->pr->stereo_pixbuf_offset_left )
174 #define GET_LEFT_PIXBUF_OFFSET(rt) \
175 ((!(rt->stereo_mode & PR_STEREO_RIGHT) && !(rt->stereo_mode & PR_STEREO_SWAP)) || \
176 ( (rt->stereo_mode & PR_STEREO_RIGHT) && (rt->stereo_mode & PR_STEREO_SWAP)) ? \
177 rt->pr->stereo_pixbuf_offset_right : rt->pr->stereo_pixbuf_offset_left )
180 static void rt_sync_scroll(RendererTiles *rt)
182 PixbufRenderer *pr = rt->pr;
184 rt->x_scroll = (rt->stereo_mode & PR_STEREO_MIRROR) ?
185 pr->width - pr->vis_width - pr->x_scroll
188 rt->y_scroll = (rt->stereo_mode & PR_STEREO_FLIP) ?
189 pr->height - pr->vis_height - pr->y_scroll
194 *-------------------------------------------------------------------
196 *-------------------------------------------------------------------
199 static void rt_border_draw(RendererTiles *rt, gint x, gint y, gint w, gint h)
201 PixbufRenderer *pr = rt->pr;
207 box = GTK_WIDGET(pr);
208 window = gtk_widget_get_window(box);
212 cr = gdk_cairo_create(window);
215 if (!pr->pixbuf && !pr->source_tiles_enabled)
217 if (pr_clip_region(x, y, w, h,
219 pr->viewport_width, pr->viewport_height,
222 cairo_set_source_rgb(cr, (double)pr->color.red/65535, (double)pr->color.green/65535, (double)pr->color.blue/65535);
223 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
225 rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
231 if (pr->vis_width < pr->viewport_width)
233 if (pr->x_offset > 0 &&
234 pr_clip_region(x, y, w, h,
236 pr->x_offset, pr->viewport_height,
239 cairo_set_source_rgb(cr, (double)pr->color.red/65535, (double)pr->color.green/65535, (double)pr->color.blue/65535);
240 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
242 rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
244 if (pr->viewport_width - pr->vis_width - pr->x_offset > 0 &&
245 pr_clip_region(x, y, w, h,
246 pr->x_offset + pr->vis_width, 0,
247 pr->viewport_width - pr->vis_width - pr->x_offset, pr->viewport_height,
250 cairo_set_source_rgb(cr, (double)pr->color.red/65535, (double)pr->color.green/65535, (double)pr->color.blue/65535);
251 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
253 rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
256 if (pr->vis_height < pr->viewport_height)
258 if (pr->y_offset > 0 &&
259 pr_clip_region(x, y, w, h,
261 pr->vis_width, pr->y_offset,
264 cairo_set_source_rgb(cr, (double)pr->color.red/65535, (double)pr->color.green/65535, (double)pr->color.blue/65535);
265 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
267 rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
269 if (pr->viewport_height - pr->vis_height - pr->y_offset > 0 &&
270 pr_clip_region(x, y, w, h,
271 pr->x_offset, pr->y_offset + pr->vis_height,
272 pr->vis_width, pr->viewport_height - pr->vis_height - pr->y_offset,
275 cairo_set_source_rgb(cr, (double)pr->color.red/65535, (double)pr->color.green/65535, (double)pr->color.blue/65535);
276 cairo_rectangle(cr, rx + rt->stereo_off_x, ry + rt->stereo_off_y, rw, rh);
278 rt_overlay_draw(rt, rx, ry, rw, rh, NULL);
284 static void rt_border_clear(RendererTiles *rt)
286 PixbufRenderer *pr = rt->pr;
287 rt_border_draw(rt, 0, 0, pr->viewport_width, pr->viewport_height);
292 *-------------------------------------------------------------------
294 *-------------------------------------------------------------------
297 static ImageTile *rt_tile_new(gint x, gint y, gint width, gint height)
301 it = g_new0(ImageTile, 1);
308 it->render_done = TILE_RENDER_NONE;
313 static void rt_tile_free(ImageTile *it)
317 if (it->pixbuf) g_object_unref(it->pixbuf);
318 if (it->surface) cairo_surface_destroy(it->surface);
323 static void rt_tile_free_all(RendererTiles *rt)
338 g_list_free(rt->tiles);
340 rt->tile_cache_size = 0;
343 static ImageTile *rt_tile_add(RendererTiles *rt, gint x, gint y)
345 PixbufRenderer *pr = rt->pr;
348 it = rt_tile_new(x, y, rt->tile_width, rt->tile_height);
350 if (it->x + it->w > pr->width) it->w = pr->width - it->x;
351 if (it->y + it->h > pr->height) it->h = pr->height - it->y;
353 rt->tiles = g_list_prepend(rt->tiles, it);
354 rt->tile_cache_size += it->size;
359 static void rt_tile_remove(RendererTiles *rt, ImageTile *it)
363 QueueData *qd = it->qd;
366 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
372 QueueData *qd = it->qd2;
375 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
379 rt->tiles = g_list_remove(rt->tiles, it);
380 rt->tile_cache_size -= it->size;
385 static void rt_tile_free_space(RendererTiles *rt, guint space, ImageTile *it)
387 PixbufRenderer *pr = rt->pr;
391 work = g_list_last(rt->tiles);
393 if (pr->source_tiles_enabled && pr->scale < 1.0)
397 tiles = (pr->vis_width / rt->tile_width + 1) * (pr->vis_height / rt->tile_height + 1);
398 tile_max = MAX(tiles * rt->tile_width * rt->tile_height * 3,
399 (gint)((gdouble)rt->tile_cache_max * 1048576.0 * pr->scale));
403 tile_max = rt->tile_cache_max * 1048576;
406 while (work && rt->tile_cache_size + space > tile_max)
413 ((!needle->qd && !needle->qd2) || !rt_tile_is_visible(rt, needle))) rt_tile_remove(rt, needle);
417 static void rt_tile_invalidate_all(RendererTiles *rt)
419 PixbufRenderer *pr = rt->pr;
430 it->render_done = TILE_RENDER_NONE;
431 it->render_todo = TILE_RENDER_ALL;
434 it->w = MIN(rt->tile_width, pr->width - it->x);
435 it->h = MIN(rt->tile_height, pr->height - it->y);
439 static void rt_tile_invalidate_region(RendererTiles *rt, gint x, gint y, gint w, gint h)
445 x1 = ROUND_DOWN(x, rt->tile_width);
446 x2 = ROUND_UP(x + w, rt->tile_width);
448 y1 = ROUND_DOWN(y, rt->tile_height);
449 y2 = ROUND_UP(y + h, rt->tile_height);
459 if (it->x < x2 && it->x + it->w > x1 &&
460 it->y < y2 && it->y + it->h > y1)
462 it->render_done = TILE_RENDER_NONE;
463 it->render_todo = TILE_RENDER_ALL;
468 static ImageTile *rt_tile_get(RendererTiles *rt, gint x, gint y, gboolean only_existing)
478 if (it->x == x && it->y == y)
480 rt->tiles = g_list_delete_link(rt->tiles, work);
481 rt->tiles = g_list_prepend(rt->tiles, it);
488 if (only_existing) return NULL;
490 return rt_tile_add(rt, x, y);
493 static gint pixmap_calc_size(cairo_surface_t *surface)
497 // d = gdk_drawable_get_depth(pixmap);
498 // gdk_drawable_get_size(pixmap, &w, &h);
499 return PR_TILE_SIZE * PR_TILE_SIZE * 4 / 8;
502 static void rt_tile_prepare(RendererTiles *rt, ImageTile *it)
504 PixbufRenderer *pr = rt->pr;
507 cairo_surface_t *surface;
510 surface = gdk_window_create_similar_surface(gtk_widget_get_window((GtkWidget *)pr),
512 rt->tile_width, rt->tile_height);
514 size = pixmap_calc_size(surface);
515 rt_tile_free_space(rt, size, it);
517 it->surface = surface;
519 rt->tile_cache_size += size;
526 pixbuf = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, rt->tile_width, rt->tile_height);
528 size = gdk_pixbuf_get_rowstride(pixbuf) * rt->tile_height;
529 rt_tile_free_space(rt, size, it);
533 rt->tile_cache_size += size;
538 *-------------------------------------------------------------------
540 *-------------------------------------------------------------------
543 static void rt_overlay_get_position(RendererTiles *rt, OverlayData *od,
544 gint *x, gint *y, gint *w, gint *h)
546 PixbufRenderer *pr = rt->pr;
549 pw = gdk_pixbuf_get_width(od->pixbuf);
550 ph = gdk_pixbuf_get_height(od->pixbuf);
554 if (od->flags & OVL_RELATIVE)
556 if (px < 0) px = pr->viewport_width - pw + px;
557 if (py < 0) py = pr->viewport_height - ph + py;
566 static void rt_overlay_init_window(RendererTiles *rt, OverlayData *od)
568 PixbufRenderer *pr = rt->pr;
570 GdkWindowAttr attributes;
571 gint attributes_mask;
573 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
575 attributes.window_type = GDK_WINDOW_CHILD;
576 attributes.wclass = GDK_INPUT_OUTPUT;
577 attributes.width = pw;
578 attributes.height = ph;
579 attributes.event_mask = GDK_EXPOSURE_MASK;
582 od->window = gdk_window_new(gtk_widget_get_window(GTK_WIDGET(pr)), &attributes, attributes_mask);
583 gdk_window_set_user_data(od->window, pr);
584 gdk_window_move(od->window, px + rt->stereo_off_x, py + rt->stereo_off_y);
585 gdk_window_show(od->window);
588 static void rt_overlay_draw(RendererTiles *rt, gint x, gint y, gint w, gint h,
591 PixbufRenderer *pr = rt->pr;
594 work = rt->overlay_list;
604 if (!od->window) rt_overlay_init_window(rt, od);
606 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
607 if (pr_clip_region(x, y, w, h, px, py, pw, ph, &rx, &ry, &rw, &rh))
609 if (!rt->overlay_buffer)
611 rt->overlay_buffer = gdk_window_create_similar_surface(gtk_widget_get_window((GtkWidget *)pr),
613 rt->tile_width, rt->tile_height);
620 cr = cairo_create(rt->overlay_buffer);
621 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);
622 cairo_rectangle(cr, 0, 0, rw, rh);
623 cairo_fill_preserve(cr);
625 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - rx, py - ry);
629 cr = gdk_cairo_create(od->window);
630 cairo_set_source_surface(cr, rt->overlay_buffer, rx - px, ry - py);
631 cairo_rectangle (cr, rx - px, ry - py, rw, rh);
637 /* no ImageTile means region may be larger than our scratch buffer */
640 for (sx = rx; sx < rx + rw; sx += rt->tile_width)
641 for (sy = ry; sy < ry + rh; sy += rt->tile_height)
646 sw = MIN(rx + rw - sx, rt->tile_width);
647 sh = MIN(ry + rh - sy, rt->tile_height);
649 cr = cairo_create(rt->overlay_buffer);
650 cairo_set_source_rgb(cr, 0, 0, 0);
651 cairo_rectangle(cr, 0, 0, sw, sh);
652 cairo_fill_preserve(cr);
654 gdk_cairo_set_source_pixbuf(cr, od->pixbuf, px - sx, py - sy);
658 cr = gdk_cairo_create(od->window);
659 cairo_set_source_surface(cr, rt->overlay_buffer, sx - px, sy - py);
660 cairo_rectangle (cr, sx - px, sy - py, sw, sh);
669 static void rt_overlay_queue_draw(RendererTiles *rt, OverlayData *od, gint x1, gint y1, gint x2, gint y2)
671 PixbufRenderer *pr = rt->pr;
674 rt_overlay_get_position(rt, od, &x, &y, &w, &h);
682 rt_queue(rt, rt->x_scroll - pr->x_offset + x,
683 rt->y_scroll - pr->y_offset + y,
685 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
687 rt_border_draw(rt, x, y, w, h);
690 static void rt_overlay_queue_all(RendererTiles *rt, gint x1, gint y1, gint x2, gint y2)
694 work = rt->overlay_list;
697 OverlayData *od = work->data;
700 rt_overlay_queue_draw(rt, od, x1, y1, x2, y2);
704 static void rt_overlay_update_sizes(RendererTiles *rt)
708 work = rt->overlay_list;
711 OverlayData *od = work->data;
714 if (!od->window) rt_overlay_init_window(rt, od);
716 if (od->flags & OVL_RELATIVE)
720 rt_overlay_get_position(rt, od, &x, &y, &w, &h);
721 gdk_window_move_resize(od->window, x + rt->stereo_off_x, y + rt->stereo_off_y, w, h);
726 static OverlayData *rt_overlay_find(RendererTiles *rt, gint id)
730 work = rt->overlay_list;
733 OverlayData *od = work->data;
736 if (od->id == id) return od;
743 gint renderer_tiles_overlay_add(void *renderer, GdkPixbuf *pixbuf, gint x, gint y,
744 OverlayRendererFlags flags)
746 RendererTiles *rt = (RendererTiles *) renderer;
747 PixbufRenderer *pr = rt->pr;
751 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
752 g_return_val_if_fail(pixbuf != NULL, -1);
755 while (rt_overlay_find(rt, id)) id++;
757 od = g_new0(OverlayData, 1);
760 g_object_ref(G_OBJECT(od->pixbuf));
765 rt_overlay_init_window(rt, od);
767 rt->overlay_list = g_list_append(rt->overlay_list, od);
769 rt_overlay_queue_draw(rt, od, 0, 0, 0, 0);
774 static void rt_overlay_free(RendererTiles *rt, OverlayData *od)
776 rt->overlay_list = g_list_remove(rt->overlay_list, od);
778 if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
779 if (od->window) gdk_window_destroy(od->window);
782 if (!rt->overlay_list && rt->overlay_buffer)
784 cairo_surface_destroy(rt->overlay_buffer);
785 rt->overlay_buffer = NULL;
789 static void rt_overlay_list_clear(RendererTiles *rt)
791 while (rt->overlay_list)
795 od = rt->overlay_list->data;
796 rt_overlay_free(rt, od);
800 static void rt_overlay_list_reset_window(RendererTiles *rt)
804 if (rt->overlay_buffer) cairo_surface_destroy(rt->overlay_buffer);
805 rt->overlay_buffer = NULL;
807 work = rt->overlay_list;
810 OverlayData *od = work->data;
812 if (od->window) gdk_window_destroy(od->window);
817 void renderer_tiles_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint x, gint y)
819 RendererTiles *rt = (RendererTiles *) renderer;
820 PixbufRenderer *pr = rt->pr;
823 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
825 od = rt_overlay_find(rt, id);
832 g_object_ref(G_OBJECT(pixbuf));
833 g_object_unref(G_OBJECT(od->pixbuf));
839 if (!od->window) rt_overlay_init_window(rt, od);
841 rt_overlay_queue_draw(rt, od, 0, 0, 0, 0);
842 rt_overlay_get_position(rt, od, &px, &py, &pw, &ph);
843 gdk_window_move_resize(od->window, px + rt->stereo_off_x, py + rt->stereo_off_y, pw, ph);
847 rt_overlay_queue_draw(rt, od, 0, 0, 0, 0);
848 rt_overlay_free(rt, od);
852 gboolean renderer_tiles_overlay_get(void *renderer, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
854 RendererTiles *rt = (RendererTiles *) renderer;
855 PixbufRenderer *pr = rt->pr;
858 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
860 od = rt_overlay_find(rt, id);
861 if (!od) return FALSE;
863 if (pixbuf) *pixbuf = od->pixbuf;
870 static void rt_hierarchy_changed_cb(GtkWidget *widget, GtkWidget *previous_toplevel, gpointer data)
872 RendererTiles *rt = data;
873 rt_overlay_list_reset_window(rt);
877 *-------------------------------------------------------------------
879 *-------------------------------------------------------------------
882 static GdkPixbuf *rt_get_spare_tile(RendererTiles *rt)
884 if (!rt->spare_tile) rt->spare_tile = gdk_pixbuf_new(GDK_COLORSPACE_RGB, FALSE, 8, rt->tile_width, rt->tile_height);
885 return rt->spare_tile;
888 #define COLOR_BYTES 3 /* rgb */
890 static void rt_tile_rotate_90_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
892 GdkPixbuf *src = *tile;
895 guchar *s_pix, *d_pix;
897 guchar *ip, *spi, *dpi;
899 gint tw = rt->tile_width;
901 srs = gdk_pixbuf_get_rowstride(src);
902 s_pix = gdk_pixbuf_get_pixels(src);
903 spi = s_pix + (x * COLOR_BYTES);
905 dest = rt_get_spare_tile(rt);
906 drs = gdk_pixbuf_get_rowstride(dest);
907 d_pix = gdk_pixbuf_get_pixels(dest);
908 dpi = d_pix + (tw - 1) * COLOR_BYTES;
910 for (i = y; i < y + h; i++)
912 sp = spi + (i * srs);
913 ip = dpi - (i * COLOR_BYTES);
914 for (j = x; j < x + w; j++)
917 memcpy(dp, sp, COLOR_BYTES);
922 rt->spare_tile = src;
926 static void rt_tile_rotate_90_counter_clockwise(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
928 GdkPixbuf *src = *tile;
931 guchar *s_pix, *d_pix;
933 guchar *ip, *spi, *dpi;
935 gint th = rt->tile_height;
937 srs = gdk_pixbuf_get_rowstride(src);
938 s_pix = gdk_pixbuf_get_pixels(src);
939 spi = s_pix + (x * COLOR_BYTES);
941 dest = rt_get_spare_tile(rt);
942 drs = gdk_pixbuf_get_rowstride(dest);
943 d_pix = gdk_pixbuf_get_pixels(dest);
944 dpi = d_pix + (th - 1) * drs;
946 for (i = y; i < y + h; i++)
948 sp = spi + (i * srs);
949 ip = dpi + (i * COLOR_BYTES);
950 for (j = x; j < x + w; j++)
953 memcpy(dp, sp, COLOR_BYTES);
958 rt->spare_tile = src;
962 static void rt_tile_mirror_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
964 GdkPixbuf *src = *tile;
967 guchar *s_pix, *d_pix;
972 gint tw = rt->tile_width;
974 srs = gdk_pixbuf_get_rowstride(src);
975 s_pix = gdk_pixbuf_get_pixels(src);
976 spi = s_pix + (x * COLOR_BYTES);
978 dest = rt_get_spare_tile(rt);
979 drs = gdk_pixbuf_get_rowstride(dest);
980 d_pix = gdk_pixbuf_get_pixels(dest);
981 dpi = d_pix + (tw - x - 1) * COLOR_BYTES;
983 for (i = y; i < y + h; i++)
985 sp = spi + (i * srs);
986 dp = dpi + (i * drs);
987 for (j = 0; j < w; j++)
989 memcpy(dp, sp, COLOR_BYTES);
995 rt->spare_tile = src;
999 static void rt_tile_mirror_and_flip(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1001 GdkPixbuf *src = *tile;
1004 guchar *s_pix, *d_pix;
1008 gint tw = rt->tile_width;
1009 gint th = rt->tile_height;
1011 srs = gdk_pixbuf_get_rowstride(src);
1012 s_pix = gdk_pixbuf_get_pixels(src);
1014 dest = rt_get_spare_tile(rt);
1015 drs = gdk_pixbuf_get_rowstride(dest);
1016 d_pix = gdk_pixbuf_get_pixels(dest);
1017 dpi = d_pix + (th - 1) * drs + (tw - 1) * COLOR_BYTES;
1019 for (i = y; i < y + h; i++)
1021 sp = s_pix + (i * srs) + (x * COLOR_BYTES);
1022 dp = dpi - (i * drs) - (x * COLOR_BYTES);
1023 for (j = 0; j < w; j++)
1025 memcpy(dp, sp, COLOR_BYTES);
1031 rt->spare_tile = src;
1035 static void rt_tile_flip_only(RendererTiles *rt, GdkPixbuf **tile, gint x, gint y, gint w, gint h)
1037 GdkPixbuf *src = *tile;
1040 guchar *s_pix, *d_pix;
1044 gint th = rt->tile_height;
1046 srs = gdk_pixbuf_get_rowstride(src);
1047 s_pix = gdk_pixbuf_get_pixels(src);
1048 spi = s_pix + (x * COLOR_BYTES);
1050 dest = rt_get_spare_tile(rt);
1051 drs = gdk_pixbuf_get_rowstride(dest);
1052 d_pix = gdk_pixbuf_get_pixels(dest);
1053 dpi = d_pix + (th - 1) * drs + (x * COLOR_BYTES);
1055 for (i = y; i < y + h; i++)
1057 sp = spi + (i * srs);
1058 dp = dpi - (i * drs);
1059 memcpy(dp, sp, w * COLOR_BYTES);
1062 rt->spare_tile = src;
1066 static void rt_tile_apply_orientation(RendererTiles *rt, gint orientation, GdkPixbuf **pixbuf, gint x, gint y, gint w, gint h)
1068 switch (orientation)
1070 case EXIF_ORIENTATION_TOP_LEFT:
1071 /* normal -- nothing to do */
1073 case EXIF_ORIENTATION_TOP_RIGHT:
1076 rt_tile_mirror_only(rt, pixbuf, x, y, w, h);
1079 case EXIF_ORIENTATION_BOTTOM_RIGHT:
1082 rt_tile_mirror_and_flip(rt, pixbuf, x, y, w, h);
1085 case EXIF_ORIENTATION_BOTTOM_LEFT:
1088 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1091 case EXIF_ORIENTATION_LEFT_TOP:
1093 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1094 rt_tile_rotate_90_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1097 case EXIF_ORIENTATION_RIGHT_TOP:
1098 /* rotated -90 (270) */
1100 rt_tile_rotate_90_clockwise(rt, pixbuf, x, y, w, h);
1103 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1105 rt_tile_flip_only(rt, pixbuf, x, y, w, h);
1106 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, rt->tile_height - y - h, w, h);
1109 case EXIF_ORIENTATION_LEFT_BOTTOM:
1112 rt_tile_rotate_90_counter_clockwise(rt, pixbuf, x, y, w, h);
1116 /* The other values are out of range */
1121 static gboolean rt_source_tile_render(RendererTiles *rt, ImageTile *it,
1122 gint x, gint y, gint w, gint h,
1123 gboolean new_data, gboolean fast)
1125 PixbufRenderer *pr = rt->pr;
1128 gboolean draw = FALSE;
1130 if (pr->zoom == 1.0 || pr->scale == 1.0)
1132 list = pr_source_tile_compute_region(pr, it->x + x, it->y + y, w, h, TRUE);
1137 gint rx, ry, rw, rh;
1142 if (pr_clip_region(st->x, st->y, pr->source_tile_width, pr->source_tile_height,
1143 it->x + x, it->y + y, w, h,
1144 &rx, &ry, &rw, &rh))
1147 cr = cairo_create(it->surface);
1148 cairo_rectangle (cr, rx - it->x, ry - it->y, rw, rh);
1152 cairo_set_source_rgb(cr, 0, 0, 0);
1154 else /* (pr->zoom == 1.0 || pr->scale == 1.0) */
1156 gdk_cairo_set_source_pixbuf(cr, st->pixbuf, -it->x + st->x, -it->y + st->y);
1165 gdouble scale_x, scale_y;
1166 gint sx, sy, sw, sh;
1168 if (pr->image_width == 0 || pr->image_height == 0) return FALSE;
1169 scale_x = (gdouble)pr->width / pr->image_width;
1170 scale_y = (gdouble)pr->height / pr->image_height;
1172 sx = (gdouble)(it->x + x) / scale_x;
1173 sy = (gdouble)(it->y + y) / scale_y;
1174 sw = (gdouble)w / scale_x;
1175 sh = (gdouble)h / scale_y;
1177 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1180 /* draws red over draw region, to check for leaks (regions not filled) */
1181 pixbuf_set_rect_fill(it->pixbuf, x, y, w, h, 255, 0, 0, 255);
1184 list = pr_source_tile_compute_region(pr, sx, sy, sw, sh, TRUE);
1189 gint rx, ry, rw, rh;
1190 gint stx, sty, stw, sth;
1195 stx = floor((gdouble)st->x * scale_x);
1196 sty = floor((gdouble)st->y * scale_y);
1197 stw = ceil((gdouble)(st->x + pr->source_tile_width) * scale_x) - stx;
1198 sth = ceil((gdouble)(st->y + pr->source_tile_height) * scale_y) - sty;
1200 if (pr_clip_region(stx, sty, stw, sth,
1201 it->x + x, it->y + y, w, h,
1202 &rx, &ry, &rw, &rh))
1208 cr = cairo_create(it->surface);
1209 cairo_rectangle (cr, rx - st->x, ry - st->y, rw, rh);
1210 cairo_set_source_rgb(cr, 0, 0, 0);
1219 /* may need to use unfloored stx,sty values here */
1220 offset_x = (gdouble)(stx - it->x);
1221 offset_y = (gdouble)(sty - it->y);
1223 gdk_pixbuf_scale(st->pixbuf, it->pixbuf, rx - it->x, ry - it->y, rw, rh,
1224 (gdouble) 0.0 + offset_x,
1225 (gdouble) 0.0 + offset_y,
1227 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality);
1239 static void rt_tile_get_region(gboolean has_alpha,
1240 const GdkPixbuf *src, GdkPixbuf *dest,
1241 int pb_x, int pb_y, int pb_w, int pb_h,
1242 double offset_x, double offset_y, double scale_x, double scale_y,
1243 GdkInterpType interp_type,
1244 int check_x, int check_y)
1248 if (scale_x == 1.0 && scale_y == 1.0)
1250 gdk_pixbuf_copy_area(src,
1251 -offset_x + pb_x, -offset_y + pb_y,
1258 gdk_pixbuf_scale(src, dest,
1259 pb_x, pb_y, pb_w, pb_h,
1268 gdk_pixbuf_composite_color(src, dest,
1269 pb_x, pb_y, pb_w, pb_h,
1274 255, check_x, check_y,
1275 PR_ALPHA_CHECK_SIZE,
1276 ((options->image.alpha_color_1.red << 8 & 0x00FF0000) +
1277 (options->image.alpha_color_1.green & 0x00FF00) +
1278 (options->image.alpha_color_1.blue >> 8 & 0x00FF)),
1279 ((options->image.alpha_color_2.red << 8 & 0x00FF0000) +
1280 (options->image.alpha_color_2.green & 0x00FF00) +
1281 (options->image.alpha_color_2.blue >> 8 & 0x00FF)));
1286 static gint rt_get_orientation(RendererTiles *rt)
1288 PixbufRenderer *pr = rt->pr;
1290 gint orientation = pr->orientation;
1291 static const gint mirror[] = {1, 2, 1, 4, 3, 6, 5, 8, 7};
1292 static const gint flip[] = {1, 4, 3, 2, 1, 8, 7, 6, 5};
1294 if (rt->stereo_mode & PR_STEREO_MIRROR) orientation = mirror[orientation];
1295 if (rt->stereo_mode & PR_STEREO_FLIP) orientation = flip[orientation];
1300 static void rt_tile_render(RendererTiles *rt, ImageTile *it,
1301 gint x, gint y, gint w, gint h,
1302 gboolean new_data, gboolean fast)
1304 PixbufRenderer *pr = rt->pr;
1306 gboolean draw = FALSE;
1307 gint orientation = rt_get_orientation(rt);
1309 if (it->render_todo == TILE_RENDER_NONE && it->surface && !new_data) return;
1311 if (it->render_done != TILE_RENDER_ALL)
1317 if (!fast) it->render_done = TILE_RENDER_ALL;
1319 else if (it->render_todo != TILE_RENDER_AREA)
1321 if (!fast) it->render_todo = TILE_RENDER_NONE;
1325 if (!fast) it->render_todo = TILE_RENDER_NONE;
1327 if (new_data) it->blank = FALSE;
1329 rt_tile_prepare(rt, it);
1330 has_alpha = (pr->pixbuf && gdk_pixbuf_get_has_alpha(pr->pixbuf));
1332 /* FIXME checker colors for alpha should be configurable,
1333 * also should be drawn for blank = TRUE
1338 /* no data, do fast rect fill */
1340 cr = cairo_create(it->surface);
1341 cairo_rectangle (cr, 0, 0, it->w, it->h);
1342 cairo_set_source_rgb(cr, 0, 0, 0);
1346 else if (pr->source_tiles_enabled)
1348 draw = rt_source_tile_render(rt, it, x, y, w, h, new_data, fast);
1352 gdouble scale_x, scale_y;
1353 gdouble src_x, src_y;
1357 if (pr->image_width == 0 || pr->image_height == 0) return;
1359 scale_x = (gdouble)pr->width / pr->image_width;
1360 scale_y = (gdouble)pr->height / pr->image_height;
1362 pr_tile_coords_map_orientation(orientation, it->x, it->y,
1363 pr->width, pr->height,
1364 rt->tile_width, rt->tile_height,
1366 pr_tile_region_map_orientation(orientation, x, y,
1367 rt->tile_width, rt->tile_height,
1372 switch (orientation)
1375 case EXIF_ORIENTATION_LEFT_TOP:
1376 case EXIF_ORIENTATION_RIGHT_TOP:
1377 case EXIF_ORIENTATION_RIGHT_BOTTOM:
1378 case EXIF_ORIENTATION_LEFT_BOTTOM:
1388 /* HACK: The pixbuf scalers get kinda buggy(crash) with extremely
1389 * small sizes for anything but GDK_INTERP_NEAREST
1391 if (pr->width < PR_MIN_SCALE_SIZE || pr->height < PR_MIN_SCALE_SIZE) fast = TRUE;
1393 rt_tile_get_region(has_alpha,
1394 pr->pixbuf, it->pixbuf, pb_x, pb_y, pb_w, pb_h,
1395 (gdouble) 0.0 - src_x - GET_RIGHT_PIXBUF_OFFSET(rt) * scale_x,
1396 (gdouble) 0.0 - src_y,
1398 (fast) ? GDK_INTERP_NEAREST : pr->zoom_quality,
1399 it->x + pb_x, it->y + pb_y);
1400 if (rt->stereo_mode & PR_STEREO_ANAGLYPH &&
1401 (pr->stereo_pixbuf_offset_right > 0 || pr->stereo_pixbuf_offset_left > 0))
1403 GdkPixbuf *right_pb = rt_get_spare_tile(rt);
1404 rt_tile_get_region(has_alpha,
1405 pr->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h,
1406 (gdouble) 0.0 - src_x - GET_LEFT_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 pr_create_anaglyph(rt->stereo_mode, it->pixbuf, right_pb, pb_x, pb_y, pb_w, pb_h);
1412 /* do not care about freeing spare_tile, it will be reused */
1414 rt_tile_apply_orientation(rt, orientation, &it->pixbuf, pb_x, pb_y, pb_w, pb_h);
1418 if (draw && it->pixbuf && !it->blank)
1422 if (pr->func_post_process && !(pr->post_process_slow && fast))
1423 pr->func_post_process(pr, &it->pixbuf, x, y, w, h, pr->post_process_user_data);
1425 cr = cairo_create(it->surface);
1426 cairo_rectangle (cr, x, y, w, h);
1427 gdk_cairo_set_source_pixbuf(cr, it->pixbuf, 0, 0);
1434 static void rt_tile_expose(RendererTiles *rt, ImageTile *it,
1435 gint x, gint y, gint w, gint h,
1436 gboolean new_data, gboolean fast)
1438 PixbufRenderer *pr = rt->pr;
1443 /* clamp to visible */
1444 if (it->x + x < rt->x_scroll)
1446 w -= rt->x_scroll - it->x - x;
1447 x = rt->x_scroll - it->x;
1449 if (it->x + x + w > rt->x_scroll + pr->vis_width)
1451 w = rt->x_scroll + pr->vis_width - it->x - x;
1454 if (it->y + y < rt->y_scroll)
1456 h -= rt->y_scroll - it->y - y;
1457 y = rt->y_scroll - it->y;
1459 if (it->y + y + h > rt->y_scroll + pr->vis_height)
1461 h = rt->y_scroll + pr->vis_height - it->y - y;
1465 rt_tile_render(rt, it, x, y, w, h, new_data, fast);
1467 box = GTK_WIDGET(pr);
1468 window = gtk_widget_get_window(box);
1470 cr = gdk_cairo_create(window);
1471 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);
1472 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);
1476 if (rt->overlay_list)
1478 rt_overlay_draw(rt, pr->x_offset + (it->x - rt->x_scroll) + x,
1479 pr->y_offset + (it->y - rt->y_scroll) + y,
1486 static gboolean rt_tile_is_visible(RendererTiles *rt, ImageTile *it)
1488 PixbufRenderer *pr = rt->pr;
1489 return (it->x + it->w >= rt->x_scroll && it->x < rt->x_scroll + pr->vis_width &&
1490 it->y + it->h >= rt->y_scroll && it->y < rt->y_scroll + pr->vis_height);
1494 *-------------------------------------------------------------------
1496 *-------------------------------------------------------------------
1499 static gint rt_get_queued_area(GList *work)
1505 QueueData *qd = work->data;
1506 area += qd->w * qd->h;
1513 static gboolean rt_queue_schedule_next_draw(RendererTiles *rt, gboolean force_set)
1515 PixbufRenderer *pr = rt->pr;
1517 gint visible_area = pr->vis_width * pr->vis_height;
1522 DEBUG_2("redraw priority: 2pass");
1523 rt->draw_idle_id = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, rt_queue_draw_idle_cb, rt, NULL);
1527 if (visible_area == 0)
1534 percent = 100.0 * rt_get_queued_area(rt->draw_queue) / visible_area;
1539 /* we have enough data for starting intensive redrawing */
1540 DEBUG_2("redraw priority: high %.2f %%", percent);
1541 rt->draw_idle_id = g_idle_add_full(GDK_PRIORITY_REDRAW, rt_queue_draw_idle_cb, rt, NULL);
1545 if (percent < 1.0 || force_set)
1547 /* queue is (almost) empty, wait 50 ms*/
1548 DEBUG_2("redraw priority: wait %.2f %%", percent);
1549 rt->draw_idle_id = g_timeout_add_full(G_PRIORITY_DEFAULT_IDLE, 50, rt_queue_draw_idle_cb, rt, NULL);
1553 /* keep the same priority as before */
1554 DEBUG_2("redraw priority: no change %.2f %%", percent);
1559 static gboolean rt_queue_draw_idle_cb(gpointer data)
1561 RendererTiles *rt = data;
1562 PixbufRenderer *pr = rt->pr;
1567 if ((!pr->pixbuf && !pr->source_tiles_enabled) ||
1568 (!rt->draw_queue && !rt->draw_queue_2pass) ||
1571 pr_render_complete_signal(pr);
1573 rt->draw_idle_id = 0;
1579 qd = rt->draw_queue->data;
1580 fast = (pr->zoom_2pass && ((pr->zoom_quality != GDK_INTERP_NEAREST && pr->scale != 1.0) || pr->post_process_slow));
1586 /* still loading, wait till done (also drops the higher priority) */
1588 return rt_queue_schedule_next_draw(rt, FALSE);
1591 qd = rt->draw_queue_2pass->data;
1595 if (gtk_widget_get_realized(GTK_WIDGET(pr)))
1597 if (rt_tile_is_visible(rt, qd->it))
1599 rt_tile_expose(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1601 else if (qd->new_data)
1603 /* if new pixel data, and we already have a pixmap, update the tile */
1604 qd->it->blank = FALSE;
1605 if (qd->it->surface && qd->it->render_done == TILE_RENDER_ALL)
1607 rt_tile_render(rt, qd->it, qd->x, qd->y, qd->w, qd->h, qd->new_data, fast);
1615 rt->draw_queue = g_list_remove(rt->draw_queue, qd);
1620 rt_queue_merge(qd->it->qd2, qd);
1626 rt->draw_queue_2pass = g_list_append(rt->draw_queue_2pass, qd);
1637 rt->draw_queue_2pass = g_list_remove(rt->draw_queue_2pass, qd);
1641 if (!rt->draw_queue && !rt->draw_queue_2pass)
1643 pr_render_complete_signal(pr);
1645 rt->draw_idle_id = 0;
1649 return rt_queue_schedule_next_draw(rt, FALSE);
1652 static void rt_queue_list_free(GList *list)
1672 static void rt_queue_clear(RendererTiles *rt)
1674 rt_queue_list_free(rt->draw_queue);
1675 rt->draw_queue = NULL;
1677 rt_queue_list_free(rt->draw_queue_2pass);
1678 rt->draw_queue_2pass = NULL;
1680 if (rt->draw_idle_id)
1682 g_source_remove(rt->draw_idle_id);
1683 rt->draw_idle_id = 0;
1688 static void rt_queue_merge(QueueData *parent, QueueData *qd)
1690 if (parent->x + parent->w < qd->x + qd->w)
1692 parent->w += (qd->x + qd->w) - (parent->x + parent->w);
1694 if (parent->x > qd->x)
1696 parent->w += parent->x - qd->x;
1700 if (parent->y + parent->h < qd->y + qd->h)
1702 parent->h += (qd->y + qd->h) - (parent->y + parent->h);
1704 if (parent->y > qd->y)
1706 parent->h += parent->y - qd->y;
1710 parent->new_data |= qd->new_data;
1713 static gboolean rt_clamp_to_visible(RendererTiles *rt, gint *x, gint *y, gint *w, gint *h)
1715 PixbufRenderer *pr = rt->pr;
1722 vh = pr->vis_height;
1727 if (*x + *w < vx || *x > vx + vw || *y + *h < vy || *y > vy + vh) return FALSE;
1730 nx = CLAMP(*x, vx, vx + vw);
1731 nw = CLAMP(*w - (nx - *x), 1, vw);
1733 ny = CLAMP(*y, vy, vy + vh);
1734 nh = CLAMP(*h - (ny - *y), 1, vh);
1744 static gboolean rt_queue_to_tiles(RendererTiles *rt, gint x, gint y, gint w, gint h,
1745 gboolean clamp, ImageRenderType render,
1746 gboolean new_data, gboolean only_existing)
1748 PixbufRenderer *pr = rt->pr;
1753 if (clamp && !rt_clamp_to_visible(rt, &x, &y, &w, &h)) return FALSE;
1755 x1 = ROUND_DOWN(x, rt->tile_width);
1756 x2 = ROUND_UP(x + w, rt->tile_width);
1758 y1 = ROUND_DOWN(y, rt->tile_height);
1759 y2 = ROUND_UP(y + h, rt->tile_height);
1761 for (j = y1; j <= y2; j += rt->tile_height)
1763 for (i = x1; i <= x2; i += rt->tile_width)
1767 it = rt_tile_get(rt, i, j,
1769 (i + rt->tile_width < rt->x_scroll ||
1770 i > rt->x_scroll + pr->vis_width ||
1771 j + rt->tile_height < rt->y_scroll ||
1772 j > rt->y_scroll + pr->vis_height)));
1777 if ((render == TILE_RENDER_ALL && it->render_done != TILE_RENDER_ALL) ||
1778 (render == TILE_RENDER_AREA && it->render_todo != TILE_RENDER_ALL))
1780 it->render_todo = render;
1783 qd = g_new(QueueData, 1);
1785 qd->new_data = new_data;
1795 qd->w = x + w - i - qd->x;
1796 if (qd->x + qd->w > rt->tile_width) qd->w = rt->tile_width - qd->x;
1806 qd->h = y + h - j - qd->y;
1807 if (qd->y + qd->h > rt->tile_height) qd->h = rt->tile_height - qd->y;
1809 if (qd->w < 1 || qd->h < 1)
1815 rt_queue_merge(it->qd, qd);
1821 rt->draw_queue = g_list_append(rt->draw_queue, qd);
1830 static void rt_queue(RendererTiles *rt, gint x, gint y, gint w, gint h,
1831 gboolean clamp, ImageRenderType render,
1832 gboolean new_data, gboolean only_existing)
1834 PixbufRenderer *pr = rt->pr;
1839 nx = CLAMP(x, 0, pr->width - 1);
1840 ny = CLAMP(y, 0, pr->height - 1);
1843 w = CLAMP(w, 0, pr->width - nx);
1844 h = CLAMP(h, 0, pr->height - ny);
1845 if (w < 1 || h < 1) return;
1847 if (rt_queue_to_tiles(rt, nx, ny, w, h, clamp, render, new_data, only_existing) &&
1848 ((!rt->draw_queue && !rt->draw_queue_2pass) || !rt->draw_idle_id))
1850 if (rt->draw_idle_id)
1852 g_source_remove(rt->draw_idle_id);
1853 rt->draw_idle_id = 0;
1855 rt_queue_schedule_next_draw(rt, TRUE);
1859 static void rt_scroll(void *renderer, gint x_off, gint y_off)
1861 RendererTiles *rt = (RendererTiles *) renderer;
1862 PixbufRenderer *pr = rt->pr;
1865 if (rt->stereo_mode & PR_STEREO_MIRROR) x_off = -x_off;
1866 if (rt->stereo_mode & PR_STEREO_FLIP) y_off = -y_off;
1868 gint w = pr->vis_width - abs(x_off);
1869 gint h = pr->vis_height - abs(y_off);
1873 /* scrolled completely to new material */
1874 rt_queue(rt, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1884 cairo_surface_t *surface;
1908 box = GTK_WIDGET(pr);
1909 window = gtk_widget_get_window(box);
1911 cr = gdk_cairo_create(window);
1912 surface = cairo_get_target(cr);
1913 /* clipping restricts the intermediate surface's size, so it's a good idea
1915 cairo_rectangle(cr, x1 + pr->x_offset + rt->stereo_off_x, y1 + pr->y_offset + rt->stereo_off_y, w, h);
1917 /* Now push a group to change the target */
1918 cairo_push_group (cr);
1919 cairo_set_source_surface(cr, surface, x1 - x2, y1 - y2);
1921 /* Now copy the intermediate target back */
1922 cairo_pop_group_to_source(cr);
1926 rt_overlay_queue_all(rt, x2, y2, x1, y1);
1928 w = pr->vis_width - w;
1929 h = pr->vis_height - h;
1934 x_off > 0 ? rt->x_scroll + (pr->vis_width - w) : rt->x_scroll, rt->y_scroll,
1935 w, pr->vis_height, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1939 /* FIXME, to optimize this, remove overlap */
1941 rt->x_scroll, y_off > 0 ? rt->y_scroll + (pr->vis_height - h) : rt->y_scroll,
1942 pr->vis_width, h, TRUE, TILE_RENDER_ALL, FALSE, FALSE);
1947 static void renderer_area_changed(void *renderer, gint src_x, gint src_y, gint src_w, gint src_h)
1949 RendererTiles *rt = (RendererTiles *)renderer;
1950 PixbufRenderer *pr = rt->pr;
1951 gint x, y, width, height, x1, y1, x2, y2;
1953 gint orientation = rt_get_orientation(rt);
1954 pr_coords_map_orientation_reverse(orientation,
1955 src_x - GET_RIGHT_PIXBUF_OFFSET(rt), src_y,
1956 pr->image_width, pr->image_height,
1961 if (pr->scale != 1.0 && pr->zoom_quality != GDK_INTERP_NEAREST)
1963 /* increase region when using a zoom quality that may access surrounding pixels */
1968 x1 = (gint)floor((gdouble)x * pr->scale);
1969 y1 = (gint)floor((gdouble)y * pr->scale * pr->aspect_ratio);
1970 x2 = (gint)ceil((gdouble)(x + width) * pr->scale);
1971 y2 = (gint)ceil((gdouble)(y + height) * pr->scale * pr->aspect_ratio);
1973 rt_queue(rt, x1, y1, x2 - x1, y2 - y1, FALSE, TILE_RENDER_AREA, TRUE, TRUE);
1976 static void renderer_redraw(RendererTiles *rt, gint x, gint y, gint w, gint h,
1977 gint clamp, ImageRenderType render, gboolean new_data, gboolean only_existing)
1979 PixbufRenderer *pr = rt->pr;
1981 x -= rt->stereo_off_x;
1982 y -= rt->stereo_off_y;
1984 rt_border_draw(rt, x, y, w, h);
1986 x = MAX(0, x - pr->x_offset + pr->x_scroll);
1987 y = MAX(0, y - pr->y_offset + pr->y_scroll);
1991 MIN(w, pr->width - x),
1992 MIN(h, pr->height - y),
1993 clamp, render, new_data, only_existing);
1996 static void renderer_update_pixbuf(void *renderer, gboolean lazy)
1998 rt_queue_clear((RendererTiles *)renderer);
2001 static void renderer_update_zoom(void *renderer, gboolean lazy)
2003 RendererTiles *rt = (RendererTiles *)renderer;
2004 PixbufRenderer *pr = rt->pr;
2006 rt_tile_invalidate_all((RendererTiles *)renderer);
2009 renderer_redraw(renderer, 0, 0, pr->width, pr->height, TRUE, TILE_RENDER_ALL, TRUE, FALSE);
2011 rt_border_clear(rt);
2014 static void renderer_invalidate_region(void *renderer, gint x, gint y, gint w, gint h)
2016 rt_tile_invalidate_region((RendererTiles *)renderer, x, y, w, h);
2019 static void renderer_update_viewport(void *renderer)
2021 RendererTiles *rt = (RendererTiles *)renderer;
2023 rt->stereo_off_x = 0;
2024 rt->stereo_off_y = 0;
2026 if (rt->stereo_mode & PR_STEREO_RIGHT)
2028 if (rt->stereo_mode & PR_STEREO_HORIZ)
2030 rt->stereo_off_x = rt->pr->viewport_width;
2032 else if (rt->stereo_mode & PR_STEREO_VERT)
2034 rt->stereo_off_y = rt->pr->viewport_height;
2036 else if (rt->stereo_mode & PR_STEREO_FIXED)
2038 rt->stereo_off_x = rt->pr->stereo_fixed_x_right;
2039 rt->stereo_off_y = rt->pr->stereo_fixed_y_right;
2044 if (rt->stereo_mode & PR_STEREO_FIXED)
2046 rt->stereo_off_x = rt->pr->stereo_fixed_x_left;
2047 rt->stereo_off_y = rt->pr->stereo_fixed_y_left;
2050 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);
2052 rt_overlay_update_sizes(rt);
2053 rt_border_clear(rt);
2056 static void renderer_stereo_set(void *renderer, gint stereo_mode)
2058 RendererTiles *rt = (RendererTiles *)renderer;
2060 rt->stereo_mode = stereo_mode;
2063 static void renderer_free(void *renderer)
2065 RendererTiles *rt = (RendererTiles *)renderer;
2067 rt_tile_free_all(rt);
2068 if (rt->spare_tile) g_object_unref(rt->spare_tile);
2069 if (rt->overlay_buffer) g_object_unref(rt->overlay_buffer);
2070 rt_overlay_list_clear(rt);
2071 /* disconnect "hierarchy-changed" */
2072 g_signal_handlers_disconnect_matched(G_OBJECT(rt->pr), G_SIGNAL_MATCH_DATA,
2077 #if GTK_CHECK_VERSION(3,0,0)
2079 static gboolean rt_draw_cb(GtkWidget *widget, cairo_t *cr, gpointer data)
2081 RendererTiles *rt = (RendererTiles *)data;
2082 if (gtk_widget_is_drawable(widget))
2084 if (gtk_widget_get_has_window(widget))
2087 if (gdk_cairo_get_clip_rectangle(cr, &area))
2089 renderer_redraw(rt, area.x, area.y, area.width, area.height,
2090 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2099 static gboolean rt_expose_cb(GtkWidget *widget, GdkEventExpose *event, gpointer data)
2101 RendererTiles *rt = (RendererTiles *)data;
2102 if (gtk_widget_is_drawable(widget))
2104 if (gtk_widget_get_has_window(widget))
2106 if (event->window != gtk_widget_get_window(widget))
2110 gdk_window_get_position(event->window, &area.x, &area.y);
2111 area.x += event->area.x;
2112 area.y += event->area.y;
2113 area.width = event->area.width;
2114 area.height = event->area.height;
2115 renderer_redraw(rt, area.x, area.y, area.width, area.height,
2116 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2121 renderer_redraw(rt, event->area.x, event->area.y, event->area.width, event->area.height,
2122 FALSE, TILE_RENDER_ALL, FALSE, FALSE);
2132 RendererFuncs *renderer_tiles_new(PixbufRenderer *pr)
2134 RendererTiles *rt = g_new0(RendererTiles, 1);
2138 rt->f.area_changed = renderer_area_changed;
2139 rt->f.update_pixbuf = renderer_update_pixbuf;
2140 rt->f.free = renderer_free;
2141 rt->f.update_zoom = renderer_update_zoom;
2142 rt->f.invalidate_region = renderer_invalidate_region;
2143 rt->f.scroll = rt_scroll;
2144 rt->f.update_viewport = renderer_update_viewport;
2147 rt->f.overlay_add = renderer_tiles_overlay_add;
2148 rt->f.overlay_set = renderer_tiles_overlay_set;
2149 rt->f.overlay_get = renderer_tiles_overlay_get;
2151 rt->f.stereo_set = renderer_stereo_set;
2153 rt->tile_width = PR_TILE_SIZE;
2154 rt->tile_height = PR_TILE_SIZE;
2157 rt->tile_cache_size = 0;
2159 rt->tile_cache_max = PR_CACHE_SIZE_DEFAULT;
2161 rt->draw_idle_id = 0;
2163 rt->stereo_mode = 0;
2164 rt->stereo_off_x = 0;
2165 rt->stereo_off_y = 0;
2167 g_signal_connect(G_OBJECT(pr), "hierarchy-changed",
2168 G_CALLBACK(rt_hierarchy_changed_cb), rt);
2170 #if GTK_CHECK_VERSION(3,0,0)
2171 g_signal_connect(G_OBJECT(pr), "draw",
2172 G_CALLBACK(rt_draw_cb), rt);
2174 g_signal_connect(G_OBJECT(pr), "expose_event",
2175 G_CALLBACK(rt_expose_cb), rt);
2177 return (RendererFuncs *) rt;
2181 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */