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-clutter.h"
30 #include <clutter/clutter.h>
32 #include <clutter-gtk/clutter-gtk.h>
40 #include "pixbuf_util.h"
44 EXIF_ORIENTATION_UNKNOWN = 0,
45 EXIF_ORIENTATION_TOP_LEFT = 1,
46 EXIF_ORIENTATION_TOP_RIGHT = 2,
47 EXIF_ORIENTATION_BOTTOM_RIGHT = 3,
48 EXIF_ORIENTATION_BOTTOM_LEFT = 4,
49 EXIF_ORIENTATION_LEFT_TOP = 5,
50 EXIF_ORIENTATION_RIGHT_TOP = 6,
51 EXIF_ORIENTATION_RIGHT_BOTTOM = 7,
52 EXIF_ORIENTATION_LEFT_BOTTOM = 8
53 } ExifOrientationType;
56 #define GET_RIGHT_PIXBUF_OFFSET(rc) \
57 (( (rc->stereo_mode & PR_STEREO_RIGHT) && !(rc->stereo_mode & PR_STEREO_SWAP)) || \
58 (!(rc->stereo_mode & PR_STEREO_RIGHT) && (rc->stereo_mode & PR_STEREO_SWAP)) ? \
59 rc->pr->stereo_pixbuf_offset_right : rc->pr->stereo_pixbuf_offset_left )
61 #define GET_LEFT_PIXBUF_OFFSET(rc) \
62 ((!(rc->stereo_mode & PR_STEREO_RIGHT) && !(rc->stereo_mode & PR_STEREO_SWAP)) || \
63 ( (rc->stereo_mode & PR_STEREO_RIGHT) && (rc->stereo_mode & PR_STEREO_SWAP)) ? \
64 rc->pr->stereo_pixbuf_offset_right : rc->pr->stereo_pixbuf_offset_left )
67 typedef struct _OverlayData OverlayData;
78 OverlayRendererFlags flags;
81 typedef struct _RendererClutter RendererClutter;
83 struct _RendererClutter
94 GList *pending_updates;
99 GtkWidget *widget; /* widget and stage may be shared with other renderers */
101 ClutterActor *texture;
105 typedef struct _RendererClutterAreaParam RendererClutterAreaParam;
106 struct _RendererClutterAreaParam {
114 static void rc_set_shader(CoglHandle material)
119 shader = cogl_create_shader (COGL_SHADER_TYPE_FRAGMENT);
120 cogl_shader_source (shader,
121 "vec3 checker(vec2 texc, vec3 color0, vec3 color1)"
123 " if (mod(int(floor(texc.x) + floor(texc.y)), 2) == 0)"
129 "uniform sampler2D tex;"
132 " vec3 bg = checker(gl_FragCoord.xy / 16, vec3(0.6, 0.6, 0.6), vec3(0.4, 0.4, 0.4));"
133 " vec4 img4 = texture2D(tex, gl_TexCoord[0].xy);"
134 " vec3 img3 = img4.rgb;"
135 " gl_FragColor = vec4(img3 * img4.a + bg * (1.0 - img4.a), 1.0);"
138 cogl_shader_compile(shader);
139 gchar *err = cogl_shader_get_info_log(shader);
143 program = cogl_create_program ();
144 cogl_program_attach_shader (program, shader);
145 cogl_handle_unref (shader);
146 cogl_program_link (program);
148 gint uniform_no = cogl_program_get_uniform_location (program, "tex");
149 cogl_program_set_uniform_1i (program, uniform_no, 0);
151 cogl_material_set_user_program (material, program);
152 cogl_handle_unref (program);
156 static void rc_sync_actor(RendererClutter *rc)
158 PixbufRenderer *pr = rc->pr;
162 clutter_actor_set_anchor_point(CLUTTER_ACTOR(rc->texture), 0, 0);
164 printf("scale %d %d\n", rc->pr->width, rc->pr->height);
165 printf("pos %d %d\n", rc->pr->x_offset, rc->pr->y_offset);
167 clutter_actor_set_scale(CLUTTER_ACTOR(rc->texture),
168 (gfloat)pr->width / pr->image_width,
169 (gfloat)pr->height / pr->image_height);
171 switch (pr->orientation)
173 case EXIF_ORIENTATION_TOP_LEFT:
175 clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
178 clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
184 case EXIF_ORIENTATION_TOP_RIGHT:
186 clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
189 clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
192 anchor_x = pr->width;
195 case EXIF_ORIENTATION_BOTTOM_RIGHT:
197 clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
200 clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
203 anchor_x = pr->width;
204 anchor_y = pr->height;
206 case EXIF_ORIENTATION_BOTTOM_LEFT:
208 clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
211 clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
215 anchor_y = pr->height;
217 case EXIF_ORIENTATION_LEFT_TOP:
218 clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
221 clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
227 case EXIF_ORIENTATION_RIGHT_TOP:
228 /* rotated -90 (270) */
229 clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
232 clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
236 anchor_y = pr->height;
238 case EXIF_ORIENTATION_RIGHT_BOTTOM:
239 clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
242 clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
245 anchor_x = pr->width;
246 anchor_y = pr->height;
248 case EXIF_ORIENTATION_LEFT_BOTTOM:
250 clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
253 clutter_actor_set_rotation(CLUTTER_ACTOR(rc->texture),
256 anchor_x = pr->width;
260 /* The other values are out of range */
264 clutter_actor_set_position(CLUTTER_ACTOR(rc->texture),
265 pr->x_offset - pr->x_scroll + anchor_x,
266 pr->y_offset - pr->y_scroll + anchor_y);
271 static void renderer_area_clip_add(RendererClutter *rc, gfloat x, gfloat y, gfloat w, gfloat h)
273 PixbufRenderer *pr = rc->pr;
275 gfloat clip_x, clip_y, clip_w, clip_h, clip_x2, clip_y2;
280 clutter_actor_get_clip(rc->texture, &clip_x, &clip_y, &clip_w, &clip_h);
282 clip_x2 = clip_x + clip_w;
283 clip_y2 = clip_y + clip_h;
285 if (clip_x > x) clip_x = x;
286 if (clip_x2 < x2) clip_x2 = x2;
287 if (clip_y > y) clip_y = y;
288 if (clip_y2 < y2) clip_y2 = y2;
290 clip_w = clip_x2 - clip_x;
291 clip_h = clip_y2 - clip_y;
293 printf("clip %f %f %f %f\n", clip_x, clip_y, clip_w, clip_h);
294 clutter_actor_set_clip(rc->texture, clip_x, clip_y, clip_w, clip_h);
297 #define MAX_REGION_AREA (32768 * 1024)
299 static gboolean renderer_area_changed_cb(gpointer data)
301 RendererClutter *rc = (RendererClutter *)data;
302 PixbufRenderer *pr = rc->pr;
304 RendererClutterAreaParam *par = rc->pending_updates->data;
306 gint h = MAX_REGION_AREA / par->w;
308 if (h > par->h) h = par->h;
311 printf("renderer_area_changed_cb %d %d %d %d (%d)\n", par->x, par->y, par->w, h, par->h);
312 DEBUG_0("%s upload start", get_exec_time());
315 CoglHandle texture = clutter_texture_get_cogl_texture(CLUTTER_TEXTURE(rc->texture));
317 cogl_texture_set_region(texture,
318 par->x + GET_RIGHT_PIXBUF_OFFSET(rc),
326 gdk_pixbuf_get_has_alpha(pr->pixbuf) ? COGL_PIXEL_FORMAT_RGBA_8888 : COGL_PIXEL_FORMAT_RGB_888,
327 gdk_pixbuf_get_rowstride(pr->pixbuf),
328 gdk_pixbuf_get_pixels(pr->pixbuf));
330 DEBUG_0("%s upload end", get_exec_time());
331 renderer_area_clip_add(rc, par->x, par->y, par->w, h);
339 rc->pending_updates = g_list_remove(rc->pending_updates, par);
342 if (!rc->pending_updates)
344 clutter_actor_queue_redraw(CLUTTER_ACTOR(rc->texture));
352 static void renderer_area_changed(void *renderer, gint src_x, gint src_y, gint src_w, gint src_h)
354 RendererClutter *rc = (RendererClutter *)renderer;
355 PixbufRenderer *pr = rc->pr;
356 RendererClutterAreaParam *par;
358 gint width = gdk_pixbuf_get_width(pr->pixbuf);
359 gint height = gdk_pixbuf_get_height(pr->pixbuf);
361 if (pr->stereo_data == STEREO_PIXBUF_SBS || pr->stereo_data == STEREO_PIXBUF_CROSS)
366 if (!pr_clip_region(src_x, src_y, src_w, src_h,
367 GET_RIGHT_PIXBUF_OFFSET(rc), 0, width, height,
368 &src_x, &src_y, &src_w, &src_h)) return;
370 par = g_new0(RendererClutterAreaParam, 1);
372 par->x = src_x - GET_RIGHT_PIXBUF_OFFSET(rc);
376 rc->pending_updates = g_list_append(rc->pending_updates, par);
377 if (!rc->idle_update)
379 rc->idle_update = g_idle_add_full(G_PRIORITY_DEFAULT_IDLE, renderer_area_changed_cb, rc, NULL);
383 static void renderer_remove_pending_updates(RendererClutter *rc)
385 if (rc->idle_update) g_idle_remove_by_data(rc);
387 while (rc->pending_updates)
389 RendererClutterAreaParam *par = rc->pending_updates->data;
390 rc->pending_updates = g_list_remove(rc->pending_updates, par);
395 static void renderer_update_pixbuf(void *renderer, gboolean lazy)
397 RendererClutter *rc = (RendererClutter *)renderer;
398 PixbufRenderer *pr = rc->pr;
400 renderer_remove_pending_updates(rc);
404 gint width = gdk_pixbuf_get_width(pr->pixbuf);
405 gint height = gdk_pixbuf_get_height(pr->pixbuf);
407 gint prev_width, prev_height;
409 if (pr->stereo_data == STEREO_PIXBUF_SBS || pr->stereo_data == STEREO_PIXBUF_CROSS)
415 printf("renderer_update_pixbuf\n");
416 clutter_texture_get_base_size(CLUTTER_TEXTURE(rc->texture), &prev_width, &prev_height);
417 printf("change from %d %d to %d %d\n", prev_width, prev_height, width, height);
419 if (width != prev_width || height != prev_height)
421 /* FIXME use CoglMaterial with multiple textures for background, color management, anaglyph, ... */
422 CoglHandle texture = cogl_texture_new_with_size(width,
424 COGL_TEXTURE_NO_AUTO_MIPMAP,
425 gdk_pixbuf_get_has_alpha(pr->pixbuf) ? COGL_PIXEL_FORMAT_RGBA_8888 : COGL_PIXEL_FORMAT_RGB_888);
427 if (texture != COGL_INVALID_HANDLE)
429 clutter_texture_set_cogl_texture(CLUTTER_TEXTURE(rc->texture), texture);
430 cogl_handle_unref(texture);
433 clutter_actor_set_clip(rc->texture, 0, 0, 0, 0); /* visible area is extended as area_changed events arrive */
436 renderer_area_changed(renderer, GET_RIGHT_PIXBUF_OFFSET(rc), 0, width, height);
441 printf("renderer_update_pixbuf\n");
447 static void renderer_update_zoom(void *renderer, gboolean lazy)
449 RendererClutter *rc = (RendererClutter *)renderer;
450 PixbufRenderer *pr = rc->pr;
452 printf("renderer_update_zoom\n");
456 static void renderer_invalidate_region(void *renderer, gint x, gint y, gint w, gint h)
460 static OverlayData *rc_overlay_find(RendererClutter *rc, gint id)
464 work = rc->overlay_list;
467 OverlayData *od = work->data;
470 if (od->id == id) return od;
476 static void rc_overlay_actor_destroy_cb(ClutterActor *actor, gpointer user_data)
478 OverlayData *od = user_data;
482 static void rc_overlay_free(RendererClutter *rc, OverlayData *od)
484 rc->overlay_list = g_list_remove(rc->overlay_list, od);
486 if (od->pixbuf) g_object_unref(G_OBJECT(od->pixbuf));
487 if (od->actor) clutter_actor_destroy(od->actor);
491 static void rc_overlay_update_position(RendererClutter *rc, OverlayData *od)
495 pw = gdk_pixbuf_get_width(od->pixbuf);
496 ph = gdk_pixbuf_get_height(od->pixbuf);
500 if (od->flags & OVL_RELATIVE)
502 if (px < 0) px = rc->pr->viewport_width - pw + px;
503 if (py < 0) py = rc->pr->viewport_height - ph + py;
505 if (od->actor) clutter_actor_set_position(od->actor, px, py);
508 static void rc_overlay_update_positions(RendererClutter *rc)
512 work = rc->overlay_list;
515 OverlayData *od = work->data;
518 rc_overlay_update_position(rc, od);
522 static void rc_overlay_free_all(RendererClutter *rc)
526 work = rc->overlay_list;
529 OverlayData *od = work->data;
532 rc_overlay_free(rc, od);
537 static void renderer_overlay_draw(void *renderer, gint x, gint y, gint w, gint h)
541 static gint renderer_overlay_add(void *renderer, GdkPixbuf *pixbuf, gint x, gint y, OverlayRendererFlags flags)
543 RendererClutter *rc = (RendererClutter *)renderer;
544 PixbufRenderer *pr = rc->pr;
548 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), -1);
549 g_return_val_if_fail(pixbuf != NULL, -1);
552 while (rc_overlay_find(rc, id)) id++;
554 od = g_new0(OverlayData, 1);
557 g_object_ref(G_OBJECT(od->pixbuf));
562 od->actor = gtk_clutter_texture_new();
563 g_signal_connect (od->actor, "destroy", G_CALLBACK(rc_overlay_actor_destroy_cb), od);
565 gtk_clutter_texture_set_from_pixbuf(GTK_CLUTTER_TEXTURE (od->actor), pixbuf, NULL);
566 clutter_container_add_actor(CLUTTER_CONTAINER(rc->group), od->actor);
568 rc->overlay_list = g_list_append(rc->overlay_list, od);
569 rc_overlay_update_position(rc, od);
574 static void renderer_overlay_set(void *renderer, gint id, GdkPixbuf *pixbuf, gint x, gint y)
576 RendererClutter *rc = (RendererClutter *)renderer;
577 PixbufRenderer *pr = rc->pr;
580 g_return_if_fail(IS_PIXBUF_RENDERER(pr));
582 od = rc_overlay_find(rc, id);
587 g_object_ref(G_OBJECT(pixbuf));
588 g_object_unref(G_OBJECT(od->pixbuf));
594 if (od->actor) gtk_clutter_texture_set_from_pixbuf(GTK_CLUTTER_TEXTURE(od->actor), pixbuf, NULL);
595 rc_overlay_update_position(rc, od);
599 rc_overlay_free(rc, od);
603 static gboolean renderer_overlay_get(void *renderer, gint id, GdkPixbuf **pixbuf, gint *x, gint *y)
605 RendererClutter *rc = (RendererClutter *)renderer;
607 PixbufRenderer *pr = rc->pr;
610 g_return_val_if_fail(IS_PIXBUF_RENDERER(pr), FALSE);
612 od = rc_overlay_find(rc, id);
613 if (!od) return FALSE;
615 if (pixbuf) *pixbuf = od->pixbuf;
623 static void renderer_update_sizes(void *renderer)
625 RendererClutter *rc = (RendererClutter *)renderer;
626 ClutterColor stage_color = { 0x00, 0x00, 0x00, 0xff };
628 rc->stereo_off_x = 0;
629 rc->stereo_off_y = 0;
631 if (rc->stereo_mode & PR_STEREO_RIGHT)
633 if (rc->stereo_mode & PR_STEREO_HORIZ)
635 rc->stereo_off_x = rc->pr->viewport_width;
637 else if (rc->stereo_mode & PR_STEREO_VERT)
639 rc->stereo_off_y = rc->pr->viewport_height;
641 else if (rc->stereo_mode & PR_STEREO_FIXED)
643 rc->stereo_off_x = rc->pr->stereo_fixed_x_right;
644 rc->stereo_off_y = rc->pr->stereo_fixed_y_right;
649 if (rc->stereo_mode & PR_STEREO_FIXED)
651 rc->stereo_off_x = rc->pr->stereo_fixed_x_left;
652 rc->stereo_off_y = rc->pr->stereo_fixed_y_left;
655 DEBUG_1("update size: %p %d %d %d %d", rc, rc->stereo_off_x, rc->stereo_off_y, rc->pr->viewport_width, rc->pr->viewport_height);
657 printf("renderer_update_sizes scale %d %d\n", rc->pr->width, rc->pr->height);
659 clutter_stage_set_color(CLUTTER_STAGE(rc->stage), &stage_color);
662 clutter_actor_set_size(rc->group, rc->pr->viewport_width, rc->pr->viewport_height);
663 clutter_actor_set_position(rc->group, rc->stereo_off_x, rc->stereo_off_y);
665 clutter_actor_set_rotation(CLUTTER_ACTOR(rc->group),
667 (rc->stereo_mode & PR_STEREO_MIRROR) ? 180 : 0,
668 rc->pr->viewport_width / 2.0, 0, 0);
670 clutter_actor_set_rotation(CLUTTER_ACTOR(rc->group),
672 (rc->stereo_mode & PR_STEREO_FLIP) ? 180 : 0,
673 0, rc->pr->viewport_height / 2.0, 0);
676 rc_overlay_update_positions(rc);
679 static void renderer_scroll(void *renderer, gint x_off, gint y_off)
681 printf("renderer_scroll\n");
682 RendererClutter *rc = (RendererClutter *)renderer;
683 PixbufRenderer *pr = rc->pr;
688 static void renderer_stereo_set(void *renderer, gint stereo_mode)
690 RendererClutter *rc = (RendererClutter *)renderer;
692 rc->stereo_mode = stereo_mode;
695 static void renderer_free(void *renderer)
697 RendererClutter *rc = (RendererClutter *)renderer;
698 GtkWidget *widget = gtk_bin_get_child(GTK_BIN(rc->pr));
700 renderer_remove_pending_updates(rc);
702 rc_overlay_free_all(rc);
706 /* widget still exists */
707 clutter_actor_destroy(rc->group);
708 if (clutter_group_get_n_children(CLUTTER_GROUP(rc->stage)) == 0)
710 printf("destroy %p\n", rc->widget);
711 /* this was the last user */
712 gtk_widget_destroy(rc->widget);
716 printf("keep %p\n", rc->widget);
717 g_object_unref(G_OBJECT(rc->widget));
723 RendererFuncs *renderer_clutter_new(PixbufRenderer *pr)
725 RendererClutter *rc = g_new0(RendererClutter, 1);
729 rc->f.area_changed = renderer_area_changed;
730 rc->f.update_pixbuf = renderer_update_pixbuf;
731 rc->f.free = renderer_free;
732 rc->f.update_zoom = renderer_update_zoom;
733 rc->f.invalidate_region = renderer_invalidate_region;
734 rc->f.scroll = renderer_scroll;
735 rc->f.update_sizes = renderer_update_sizes;
738 rc->f.overlay_add = renderer_overlay_add;
739 rc->f.overlay_set = renderer_overlay_set;
740 rc->f.overlay_get = renderer_overlay_get;
741 rc->f.overlay_draw = renderer_overlay_draw;
743 rc->f.stereo_set = renderer_stereo_set;
747 rc->stereo_off_x = 0;
748 rc->stereo_off_y = 0;
751 rc->pending_updates = NULL;
753 rc->widget = gtk_bin_get_child(GTK_BIN(rc->pr));
757 if (!GTK_CLUTTER_IS_EMBED(rc->widget))
760 DEBUG_0("pixbuf renderer has a child of other type than gtk_clutter_embed");
766 rc->widget = gtk_clutter_embed_new();
767 gtk_container_add(GTK_CONTAINER(rc->pr), rc->widget);
770 gtk_event_box_set_above_child (GTK_EVENT_BOX(rc->pr), TRUE);
771 rc->stage = gtk_clutter_embed_get_stage (GTK_CLUTTER_EMBED (rc->widget));
773 rc->group = clutter_group_new();
774 clutter_container_add_actor(CLUTTER_CONTAINER(rc->stage), rc->group);
775 clutter_actor_set_clip_to_allocation(CLUTTER_ACTOR(rc->group), TRUE);
777 rc->texture = clutter_texture_new ();
778 clutter_container_add_actor(CLUTTER_CONTAINER(rc->group), rc->texture);
779 rc_set_shader(clutter_texture_get_cogl_material(rc->texture));
780 g_object_ref(G_OBJECT(rc->widget));
782 gtk_widget_show(rc->widget);
783 return (RendererFuncs *) rc;
787 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */